From 9fb2b2ce43acb76fa61fa59a2c64453a7969049a Mon Sep 17 00:00:00 2001 From: Charlotte Meyer Date: Tue, 31 Dec 2019 00:09:06 +0000 Subject: [PATCH] Add luaK_fini --- include/luak.h | 2 ++ kiwmi/luak.c | 22 ++++++++++++++++++++++ kiwmi/server.c | 2 ++ 3 files changed, 26 insertions(+) diff --git a/include/luak.h b/include/luak.h index 7714fa6..c3f548a 100644 --- a/include/luak.h +++ b/include/luak.h @@ -16,9 +16,11 @@ struct kiwmi_lua { lua_State *L; + struct wl_list callbacks; // lua_callback::link }; bool luaK_init(struct kiwmi_server *server); bool luaK_dofile(struct kiwmi_lua *lua, const char *config_path); +void luaK_fini(struct kiwmi_lua *lua); #endif /* KIWMI_LUAK_H */ diff --git a/kiwmi/luak.c b/kiwmi/luak.c index 91d7366..ff6125d 100644 --- a/kiwmi/luak.c +++ b/kiwmi/luak.c @@ -21,6 +21,7 @@ #include "input/input.h" struct lua_callback { + struct wl_list link; struct kiwmi_server *server; int callback_ref; struct wl_listener listener; @@ -34,6 +35,8 @@ l_lua_callback_cancel(lua_State *L) lua_pop(L, 1); wl_list_remove(&lc->listener.link); + wl_list_remove(&lc->link); + free(lc); return 0; @@ -170,6 +173,8 @@ on_cursor_button(lua_State *L) struct kiwmi_server *server = get_server(L); struct kiwmi_cursor *cursor = server->input.cursor; + wl_list_insert(&server->lua->callbacks, &lc->link); + lc->server = server; lc->callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); @@ -269,6 +274,8 @@ luaK_init(struct kiwmi_server *server) lua->L = L; server->lua = lua; + wl_list_init(&lua->callbacks); + return true; } @@ -283,3 +290,18 @@ luaK_dofile(struct kiwmi_lua *lua, const char *config_path) return true; } + +void +luaK_fini(struct kiwmi_lua *lua) +{ + struct lua_callback *lc; + struct lua_callback *tmp; + wl_list_for_each_safe(lc, tmp, &lua->callbacks, link) { + wl_list_remove(&lc->listener.link); + wl_list_remove(&lc->link); + + free(lc); + } + + lua_close(lua->L); +} diff --git a/kiwmi/server.c b/kiwmi/server.c index bf29c53..3cebd5b 100644 --- a/kiwmi/server.c +++ b/kiwmi/server.c @@ -128,5 +128,7 @@ server_fini(struct kiwmi_server *server) wl_display_destroy_clients(server->wl_display); wl_display_destroy(server->wl_display); + luaK_fini(server->lua); + free(server->config_path); }