From df1bff3b0b78e2d9840790c0ec5d8f29afb5a76e Mon Sep 17 00:00:00 2001 From: Sadrul Habib Chowdhury Date: Tue, 9 Jun 2009 18:32:02 -0400 Subject: [PATCH] Complete unhook for the handlers. This allows unhooking from events. Why were we using the function name before? Do we need it for something? --- src/lua.c | 15 ++++++++------- src/scripts/cmdcallback.lua | 17 +++++++++++++++-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/lua.c b/src/lua.c index 0cee0ee..161b47e 100644 --- a/src/lua.c +++ b/src/lua.c @@ -924,7 +924,7 @@ LuaRegEvent(lua_State *L) else { lh.type = LUA_HANDLER_TYPE_N; - lh.u.name = luaL_checkstring(L, idx++); + lh.u.name = SaveStr(luaL_checkstring(L, idx++)); } StackDump(L, "In RegEvent\n"); @@ -965,11 +965,10 @@ LuaRegEvent(lua_State *L) static int LuaUnRegEvent(lua_State *L) { - /* signature: release([obj], ticket, handler) + /* signature: unhook([obj], ticket) * returns: true of success, false otherwise */ int idx = 1; struct listener *l; - const char *handler; /* If the param is not as expected */ if (!lua_islightuserdata(L, idx)) @@ -982,21 +981,23 @@ LuaUnRegEvent(lua_State *L) } l = (struct listener*)lua_touserdata(L, idx++); - handler = luaL_checkstring(L, idx++); /* Validate the listener structure */ - if (!l || !l->handler - || strncmp((char *)handler, (char *)l->handler, SEVNAME_MAX)) + if (!l || !l->handler) { /* invalid */ lua_pushboolean(L,0); } else { + struct lua_handler *lh = l->handler; + if (lh->type == LUA_HANDLER_TYPE_N) + Free(lh->u.name); + Free(l->handler); unregister_listener(l); lua_pushboolean(L, 1); } - + return 1; } diff --git a/src/scripts/cmdcallback.lua b/src/scripts/cmdcallback.lua index 84bb380..54fa4eb 100644 --- a/src/scripts/cmdcallback.lua +++ b/src/scripts/cmdcallback.lua @@ -1,5 +1,6 @@ --[[ For now, this sample function will simply record all the commands executed ]]-- -screen.hook("cmdexecuted", function(name, args) + +ticket1 = screen.hook("cmdexecuted", function(name, args) os.execute('mkdir -p /tmp/debug') local f = io.open('/tmp/debug/22', 'a') f:write("Command executed: " .. name) @@ -27,5 +28,17 @@ function cmd(name, args) return 0 end -screen.hook("cmdexecuted", "cmd") +ticket2 = screen.hook("cmdexecuted", "cmd") + +function unhook() + if ticket1 ~= nil then + screen.unhook(ticket1) + ticket1 = nil + end + + if ticket2 ~= nil then + screen.unhook(ticket2) + ticket2 = nil + end +end -- 2.11.4.GIT