From 0f7e4e36aeb95f3e39a940d6e19748d910d40d92 Mon Sep 17 00:00:00 2001 From: Maurus Cuelenaere Date: Fri, 22 May 2009 22:44:34 +0000 Subject: [PATCH] Lua: * add action_get_touchscreen_press wrapper * fix kbd_input wrapper * rework luaL_loadfile * add rb.contexts git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21046 a1c6a512-1295-4272-9138-f99709370657 --- apps/plugin.c | 4 ++ apps/plugin.h | 6 +- apps/plugins/lua/action_helper.pl | 31 ++++++++--- apps/plugins/lua/lauxlib.c | 91 ++++++++++++++++--------------- apps/plugins/lua/rocklib.c | 29 ++++++++-- apps/plugins/lua/rocklua.c | 18 ------ 6 files changed, 105 insertions(+), 74 deletions(-) diff --git a/apps/plugin.c b/apps/plugin.c index f03b9337f9..05a27ea591 100644 --- a/apps/plugin.c +++ b/apps/plugin.c @@ -644,6 +644,10 @@ static const struct plugin_api rockbox_api = { appsversion, /* new stuff at the end, sort into place next time the API gets incompatible */ + +#ifdef HAVE_TOUCHSCREEN + action_get_touchscreen_press, +#endif }; int plugin_load(const char* plugin, const void* parameter) diff --git a/apps/plugin.h b/apps/plugin.h index 5eb4b88f28..8ec0f19572 100644 --- a/apps/plugin.h +++ b/apps/plugin.h @@ -128,7 +128,7 @@ void* plugin_get_buffer(size_t *buffer_size); #define PLUGIN_MAGIC 0x526F634B /* RocK */ /* increase this every time the api struct changes */ -#define PLUGIN_API_VERSION 151 +#define PLUGIN_API_VERSION 152 /* update this to latest version if a change to the api struct breaks backwards compatibility (and please take the opportunity to sort in any @@ -806,6 +806,10 @@ struct plugin_api { const char *appsversion; /* new stuff at the end, sort into place next time the API gets incompatible */ + +#ifdef HAVE_TOUCHSCREEN + int (*action_get_touchscreen_press)(short *x, short *y); +#endif }; /* plugin header */ diff --git a/apps/plugins/lua/action_helper.pl b/apps/plugins/lua/action_helper.pl index 8460b6cb2a..f1a1e12d60 100755 --- a/apps/plugins/lua/action_helper.pl +++ b/apps/plugins/lua/action_helper.pl @@ -22,21 +22,38 @@ $input = $ARGV[0] . "/../../action.h"; open(ACTION, "<$input") or die "Can't open $input!"; -print "-- Don't change this file!\n"; -print "-- It is automatically generated of action.h\n"; -print "rb.actions = {\n"; - $i = 0; +$j = 0; while(my $line = ) { chomp($line); - if($line =~ /^\s*(ACTION_[^\s]+)(\s*=.*)?,$/) + if($line =~ /^\s*(ACTION_[^\s]+)(\s*=.*)?,\s*$/) { - printf "\t%s = %d,\n", $1, $i; + $actions[$i] = sprintf("\t%s = %d,\n", $1, $i); $i++; } + elsif($line =~ /^\s*(CONTEXT_[^\s]+)(\s*=.*)?,\s*$/) + { + $contexts[$j] = sprintf("\t%s = %d,\n", $1, $j); + $j++; + } } +close(ACTION); + +print "-- Don't change this file!\n"; +printf "-- It is automatically generated of action.h %s\n", '$Revision'; + +print "rb.actions = {\n"; +foreach $action(@actions) +{ + print $action; +} print "}\n"; -close(ACTION); +print "rb.contexts = {\n"; +foreach $context(@contexts) +{ + print $context; +} +print "}\n"; diff --git a/apps/plugins/lua/lauxlib.c b/apps/plugins/lua/lauxlib.c index a755266bde..88abc3cde0 100644 --- a/apps/plugins/lua/lauxlib.c +++ b/apps/plugins/lua/lauxlib.c @@ -547,60 +547,62 @@ static int errfile (lua_State *L, const char *what, int fnameindex) { return LUA_ERRFILE; } +static void make_path(char* dest, size_t dest_size, char* curfile, char* newfile) +{ + char* pos = rb->strrchr(curfile, '/'); + if(pos != NULL) + { + unsigned int len = (unsigned int)(pos - curfile); + len = len + 1 > dest_size ? dest_size - 1 : len; + + if(len > 0) + memcpy(dest, curfile, len); + + dest[len] = '/'; + dest[len+1] = '\0'; + } + else + dest[0] = '\0'; + + strncat(dest, newfile, dest_size - strlen(dest)); +} LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { LoadF lf; - int status; //, readstatus; + int status; char buffer[MAX_PATH]; -// int c; int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ lf.extraline = 0; -// if (filename == NULL) { -// lua_pushliteral(L, "=stdin"); -// lf.f = stdin; -// } -// else { - lua_pushfstring(L, "@%s", filename); - lf.f = rb->open(filename, O_RDONLY); - if (lf.f < 0) - { - /* Fallback */ - snprintf(buffer, sizeof(buffer), "%s/%s", curpath, filename); - lf.f = rb->open(buffer, O_RDONLY); + lf.f = rb->open(filename, O_RDONLY); + if(lf.f < 0) { + /* Fallback */ - if(lf.f < 0) - { - snprintf(buffer, sizeof(buffer), "%s/%s", VIEWERS_DIR, filename); - lf.f = rb->open(buffer, O_RDONLY); + lua_Debug ar; + if(lua_getstack(L, 1, &ar)) { + lua_getinfo(L, "S", &ar); - if(lf.f < 0) - return errfile(L, "open", fnameindex); - } + /* Try determining the base path of the current Lua chunk + and prepend it to filename in buffer. */ + make_path(buffer, sizeof(buffer), (char*)&ar.source[1], (char*)filename); + lf.f = rb->open(buffer, O_RDONLY); } -// } -// c = getc(lf.f); -// if (c == '#') { /* Unix exec. file? */ -// lf.extraline = 1; -// while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ -// if (c == '\n') c = getc(lf.f); -// } -// if (c == LUA_SIGNATURE[0]) { // && lf.f != stdin) { /* binary file? */ -// rb->close(lf.f); -// lf.f = rb->open(filename, O_RDONLY); /* reopen in binary mode */ -// if (lf.f < 0) return errfile(L, "reopen", fnameindex); - /* skip eventual `#!...' */ -// while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; -// lf.extraline = 0; -// } -// ungetc(c, lf.f); + + if(lf.f < 0) { + snprintf(buffer, sizeof(buffer), "%s/%s", VIEWERS_DIR, filename); + lf.f = rb->open(buffer, O_RDONLY); + + if(lf.f < 0) + return errfile(L, "open", fnameindex); + } + + if(lf.f >= 0) + lua_pushfstring(L, "@%s", buffer); + } + else + lua_pushfstring(L, "@%s", filename); + status = lua_load(L, getF, &lf, lua_tostring(L, -1)); -// readstatus = ferror(lf.f); - //if (lf.f != stdin) rb->close(lf.f); /* close file (even in case of errors) */ rb->close(lf.f); -// if (readstatus) { -// lua_settop(L, fnameindex); /* ignore results from `lua_load' */ -// return errfile(L, "read", fnameindex); -// } lua_remove(L, fnameindex); return status; } @@ -653,9 +655,10 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { static int panic (lua_State *L) { - (void)L; /* to avoid warnings */ DEBUGF("PANIC: unprotected error in call to Lua API (%s)\n", lua_tostring(L, -1)); + rb->splashf(5 * HZ, "PANIC: unprotected error in call to Lua API (%s)", + lua_tostring(L, -1)); return 0; } diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c index 99bc44c9a4..c99400c938 100644 --- a/apps/plugins/lua/rocklib.c +++ b/apps/plugins/lua/rocklib.c @@ -251,6 +251,19 @@ RB_WRAP(get_action) return 1; } +#ifdef HAVE_TOUCHSCREEN +RB_WRAP(action_get_touchscreen_press) +{ + short x, y; + int result = rb->action_get_touchscreen_press(&x, &y); + + lua_pushinteger(L, result); + lua_pushinteger(L, x); + lua_pushinteger(L, y); + return 3; +} +#endif + RB_WRAP(action_userabort) { int timeout = luaL_checkint(L, 1); @@ -261,10 +274,15 @@ RB_WRAP(action_userabort) RB_WRAP(kbd_input) { - char* buffer = (char*)luaL_checkstring(L, 1); - int buflen = luaL_checkint(L, 2); - int result = rb->kbd_input(buffer, buflen); - lua_pushinteger(L, result); + luaL_Buffer b; + luaL_buffinit(L, &b); + + char *buffer = luaL_prepbuffer(&b); + buffer[0] = '\0'; + rb->kbd_input(buffer, LUAL_BUFFERSIZE); + luaL_addsize(&b, strlen(buffer)); + + luaL_pushresult(&b); return 1; } @@ -467,6 +485,9 @@ static const luaL_Reg rocklib[] = #endif R(get_action), R(action_userabort), +#ifdef HAVE_TOUCHSCREEN + R(action_get_touchscreen_press), +#endif R(kbd_input), /* Hardware */ diff --git a/apps/plugins/lua/rocklua.c b/apps/plugins/lua/rocklua.c index e72591445f..fcc8a3f9c8 100644 --- a/apps/plugins/lua/rocklua.c +++ b/apps/plugins/lua/rocklua.c @@ -46,23 +46,6 @@ static void rocklua_openlibs(lua_State *L) { } } -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) { @@ -79,7 +62,6 @@ enum plugin_status plugin_start(const void* parameter) else { filename = (char*) parameter; - fill_curpath(filename); lua_State *L = luaL_newstate();