summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/rocklua.c
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2009-05-21 19:01:41 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2009-05-21 19:01:41 +0000
commitcf87597226f5d6b269f1f2c4d6f402aa1eccb852 (patch)
tree4ba1f3ae53b3bd9cae0e2c6c4dd57836b43a5ece /apps/plugins/lua/rocklua.c
parentc483efadc63eaed35b5fb5e4e02c2282daf32470 (diff)
downloadrockbox-cf87597226f5d6b269f1f2c4d6f402aa1eccb852.zip
rockbox-cf87597226f5d6b269f1f2c4d6f402aa1eccb852.tar.gz
rockbox-cf87597226f5d6b269f1f2c4d6f402aa1eccb852.tar.bz2
rockbox-cf87597226f5d6b269f1f2c4d6f402aa1eccb852.tar.xz
Commit FS#9174: Lua scripting language by Dan Everton
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21020 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/lua/rocklua.c')
-rw-r--r--apps/plugins/lua/rocklua.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/apps/plugins/lua/rocklua.c b/apps/plugins/lua/rocklua.c
new file mode 100644
index 0000000..4bb89ae
--- /dev/null
+++ b/apps/plugins/lua/rocklua.c
@@ -0,0 +1,105 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2008 Dan Everton (safetydan)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#include "plugin.h"
+#include "lib/pluginlib_exit.h"
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+#include "rocklib.h"
+#include "rockmalloc.h"
+
+PLUGIN_HEADER
+
+static const luaL_Reg lualibs[] = {
+ {"", luaopen_base},
+ {LUA_TABLIBNAME, luaopen_table},
+ {LUA_STRLIBNAME, luaopen_string},
+ {LUA_ROCKLIBNAME, luaopen_rock},
+ {NULL, NULL}
+};
+
+static void rocklua_openlibs(lua_State *L) {
+ const luaL_Reg *lib = lualibs;
+ for (; lib->func; lib++) {
+ lua_pushcfunction(L, lib->func);
+ lua_pushstring(L, lib->name);
+ lua_call(L, 1, 0);
+ }
+}
+
+char curpath[MAX_PATH];
+static void fill_curpath(const char* filename)
+{
+ char* pos = rb->strrchr(filename, '/');
+
+ if(pos != NULL)
+ {
+ int len = (int)(pos - filename);
+
+ if(len > 0)
+ memcpy(curpath, filename, len);
+
+ curpath[len] = '\0';
+ }
+}
+
+
+/***************** Plugin Entry Point *****************/
+enum plugin_status plugin_start(const void* parameter)
+{
+ const char* filename;
+ int status;
+
+ PLUGINLIB_EXIT_INIT
+
+ if (parameter == NULL)
+ {
+ rb->splash(HZ, "Play a .lua file!");
+ return PLUGIN_ERROR;
+ }
+ else
+ {
+ filename = (char*) parameter;
+ fill_curpath(filename);
+
+ lua_State *L = luaL_newstate();
+
+ rocklua_openlibs(L);
+ status = luaL_loadfile(L, filename);
+ if (!status) {
+ rb->lcd_clear_display();
+ status = lua_pcall(L, 0, 0, 0);
+ }
+
+ dlmalloc_stats();
+
+ if (status) {
+ rb->splashf(5 * HZ, "%s", lua_tostring(L, -1));
+ lua_pop(L, 1);
+ }
+
+ lua_close(L);
+ }
+
+ return PLUGIN_OK;
+}
+