From 0beacb8c34e16faabb297af64851d21e7ab71fd3 Mon Sep 17 00:00:00 2001 From: Rui Guo Date: Wed, 10 Jun 2009 22:32:24 +0800 Subject: [PATCH] Further code clean up. 1. export (un)register_listener function to header. 2. Group Lua function managment function together. 3. Correctly print func name in message if we have it. --- src/lua.c | 130 +++++++++++++++++++++++++++++++++-------------------------- src/script.h | 2 + 2 files changed, 75 insertions(+), 57 deletions(-) diff --git a/src/lua.c b/src/lua.c index 1c07288..544cd3e 100644 --- a/src/lua.c +++ b/src/lua.c @@ -904,61 +904,7 @@ int LuaCall(char *func, char **argv) return 1; } -/* Push nil if failed */ -void -LuaPushHandler(lua_State *L, struct lua_handler * lh) -{ - if (lh->type == LUA_HANDLER_TYPE_N) - lua_getfield(L, LUA_GLOBALSINDEX, lh->u.name); - else - { - luaL_getmetatable(L, "screen"); - lua_pushstring(L, "_keyfunc"); - lua_rawget(L, -2); - lua_rawgeti(L, -1, lh->u.reference); - lua_replace(L, -3); - lua_pop(L, 1); - } -} - -static int -LuaDispatch(void *handler, const char *params, va_list va) -{ - struct lua_handler *lh = handler; - int argc, retvalue; - - StackDump(L, "before dispatch"); - - LuaPushHandler(L, lh); - if (lua_isnil(L, -1)) - { - lua_pop(L, 1); - return 0; - } - argc = LuaPushParams(L, params, va); - - if (lua_pcall(L, argc, 1, 0) == LUA_ERRRUN && lua_isstring(L, -1)) - { - StackDump(L, "After LuaDispatch\n"); - LuaShowErr(L); - return 0; - } - retvalue = lua_tonumber(L, -1); - lua_pop(L, 1); - return retvalue; -} - -int LuaForeWindowChanged(void) -{ - struct fn_def params[] = { - {push_display, &display}, - {push_window, display ? &D_fore : &fore}, - {NULL, NULL} - }; - if (!L) - return 0; - return LuaCallProcess("fore_changed", params); -} +/*** Lua callback handler management {{{ */ int LuaPushHTable(lua_State *L, int screen, const char * t) @@ -971,6 +917,17 @@ LuaPushHTable(lua_State *L, int screen, const char * t) return lua_gettop(L); } +void +LuaPushHandler(lua_State *L, struct lua_handler * lh) +{ + int keyfunc; + luaL_getmetatable(L, "screen"); + keyfunc = LuaPushHTable(L, -1, "_keyfunc"); + lua_rawgeti(L, keyfunc, lh->u.reference); + lua_replace(L, -3); + lua_pop(L, 1); +} + int LuaFuncKey(lua_State *L, int idx) { @@ -1055,6 +1012,23 @@ LuaHSetName(lua_State *L, int key, int name) lua_pop(L, 2); } +/* Do not hold the return value for long */ +const char * +LuaHGetName(lua_State *L, int key) +{ + int keyfunc, sc; + const char * name; + luaL_getmetatable(L, "screen"); + sc = lua_gettop(L); + keyfunc = LuaPushHTable(L, sc, "_keyfunc"); + + lua_rawgeti(L, keyfunc, key); + lua_rawget(L, keyfunc); + name = lua_tostring(L, -1); + lua_pop(L, 3); + return name; +} + struct lua_handler * LuaCheckHandler(lua_State *L, int idx, int reg) { @@ -1083,6 +1057,47 @@ LuaCheckHandler(lua_State *L, int idx, int reg) return LuaAllocHandler(NULL, key); } +/* }}} **/ + +static int +LuaDispatch(void *handler, const char *params, va_list va) +{ + struct lua_handler *lh = handler; + int argc, retvalue; + + StackDump(L, "before dispatch"); + + LuaPushHandler(L, lh); + if (lua_isnil(L, -1)) + { + lua_pop(L, 1); + return 0; + } + argc = LuaPushParams(L, params, va); + + if (lua_pcall(L, argc, 1, 0) == LUA_ERRRUN && lua_isstring(L, -1)) + { + StackDump(L, "After LuaDispatch\n"); + LuaShowErr(L); + return 0; + } + retvalue = lua_tonumber(L, -1); + lua_pop(L, 1); + return retvalue; +} + +int LuaForeWindowChanged(void) +{ + struct fn_def params[] = { + {push_display, &display}, + {push_window, display ? &D_fore : &fore}, + {NULL, NULL} + }; + if (!L) + return 0; + return LuaCallProcess("fore_changed", params); +} + #define SEVNAME_MAX 30 static int LuaRegEvent(lua_State *L) @@ -1140,9 +1155,10 @@ LuaRegEvent(lua_State *L) l->dispatcher = LuaDispatch; if (register_listener(sev, l)) { + const char *fname = LuaHGetName(L, lh->u.reference); free(l); - if (lh->type == LUA_HANDLER_TYPE_N) - return luaL_error(L, "Handler %s has already been registered", lh->u.name); + if (fname) + return luaL_error(L, "Handler %s has already been registered", fname); else return luaL_error(L, "Handler has already been registered"); } diff --git a/src/script.h b/src/script.h index c268eb5..307cb69 100644 --- a/src/script.h +++ b/src/script.h @@ -80,6 +80,8 @@ struct script_event }; struct script_event* object_get_event __P((char *obj, const char *name)); int trigger_sevent(struct script_event *ev, VA_DOTS); +int register_listener(struct script_event *ev, struct listener *l); +void unregister_listener(struct listener *l); struct gevents { struct script_event cmdexecuted; -- 2.11.4.GIT