From 94ba184fd3a9ec6f7751799bc0261d3e25779365 Mon Sep 17 00:00:00 2001 From: buffet Date: Wed, 15 Jan 2020 22:47:29 +0000 Subject: [PATCH] Add keboard:on_destroy --- include/input/keyboard.h | 2 ++ kiwmi/input/keyboard.c | 19 ++++++++++++++ kiwmi/luak/kiwmi_keyboard.c | 49 +++++++++++++++++++++++++++++++++++++ lua_docs.md | 5 ++++ 4 files changed, 75 insertions(+) diff --git a/include/input/keyboard.h b/include/input/keyboard.h index d31253a..2fdd2cb 100644 --- a/include/input/keyboard.h +++ b/include/input/keyboard.h @@ -17,10 +17,12 @@ struct kiwmi_keyboard { struct wlr_input_device *device; struct wl_listener modifiers; struct wl_listener key; + struct wl_listener device_destroy; struct { struct wl_signal key_down; struct wl_signal key_up; + struct wl_signal destroy; } events; }; diff --git a/kiwmi/input/keyboard.c b/kiwmi/input/keyboard.c index 1c59155..1553ff2 100644 --- a/kiwmi/input/keyboard.c +++ b/kiwmi/input/keyboard.c @@ -93,6 +93,21 @@ keyboard_key_notify(struct wl_listener *listener, void *data) } } +static void +keyboard_destroy_notify(struct wl_listener *listener, void *UNUSED(data)) +{ + struct kiwmi_keyboard *keyboard = wl_container_of(listener, keyboard, device_destroy); + + wl_list_remove(&keyboard->link); + wl_list_remove(&keyboard->modifiers.link); + wl_list_remove(&keyboard->key.link); + wl_list_remove(&keyboard->device_destroy.link); + + wl_signal_emit(&keyboard->events.destroy, keyboard); + + free(keyboard); +} + struct kiwmi_keyboard * keyboard_create(struct kiwmi_server *server, struct wlr_input_device *device) { @@ -117,6 +132,9 @@ keyboard_create(struct kiwmi_server *server, struct wlr_input_device *device) keyboard->key.notify = keyboard_key_notify; wl_signal_add(&device->keyboard->events.key, &keyboard->key); + keyboard->device_destroy.notify = keyboard_destroy_notify; + wl_signal_add(&device->events.destroy, &keyboard->device_destroy); + wlr_keyboard_set_keymap(device->keyboard, keymap); xkb_keymap_unref(keymap); xkb_context_unref(context); @@ -126,6 +144,7 @@ keyboard_create(struct kiwmi_server *server, struct wlr_input_device *device) wl_signal_init(&keyboard->events.key_down); wl_signal_init(&keyboard->events.key_up); + wl_signal_init(&keyboard->events.destroy); wl_signal_emit(&server->input.events.keyboard_new, keyboard); diff --git a/kiwmi/luak/kiwmi_keyboard.c b/kiwmi/luak/kiwmi_keyboard.c index 45bba68..39aa84c 100644 --- a/kiwmi/luak/kiwmi_keyboard.c +++ b/kiwmi/luak/kiwmi_keyboard.c @@ -59,6 +59,31 @@ static const luaL_Reg kiwmi_keyboard_methods[] = { {NULL, NULL}, }; +static void +kiwmi_keyboard_on_destroy_notify(struct wl_listener *listener, void *data) +{ + struct kiwmi_lua_callback *lc = wl_container_of(listener, lc, listener); + struct kiwmi_server *server = lc->server; + lua_State *L = server->lua->L; + struct kiwmi_keyboard *keyboard = data; + + lua_rawgeti(L, LUA_REGISTRYINDEX, lc->callback_ref); + + lua_pushcfunction(L, luaK_kiwmi_keyboard_new); + lua_pushlightuserdata(L, keyboard); + if (lua_pcall(L, 1, 1, 0)) { + wlr_log(WLR_ERROR, "%s", lua_tostring(L, -1)); + lua_pop(L, 1); + return; + } + + if (lua_pcall(L, 1, 1, 0)) { + wlr_log(WLR_ERROR, "%s", lua_tostring(L, -1)); + lua_pop(L, 1); + return; + } +} + static void kiwmi_keyboard_on_key_down_or_up_notify( struct wl_listener *listener, @@ -110,6 +135,29 @@ kiwmi_keyboard_on_key_down_or_up_notify( } } +static int +l_kiwmi_keyboard_on_destroy(lua_State *L) +{ + struct kiwmi_keyboard *keyboard = + *(struct kiwmi_keyboard **)luaL_checkudata(L, 1, "kiwmi_keyboard"); + luaL_checktype(L, 2, LUA_TFUNCTION); + + struct kiwmi_server *server = keyboard->server; + + lua_pushcfunction(L, luaK_kiwmi_lua_callback_new); + lua_pushlightuserdata(L, server); + lua_pushvalue(L, 2); + lua_pushlightuserdata(L, kiwmi_keyboard_on_destroy_notify); + lua_pushlightuserdata(L, &keyboard->events.destroy); + + if (lua_pcall(L, 4, 1, 0)) { + wlr_log(WLR_ERROR, "%s", lua_tostring(L, -1)); + return 0; + } + + return 1; +} + static int l_kiwmi_keyboard_on_key_down(lua_State *L) { @@ -157,6 +205,7 @@ l_kiwmi_keyboard_on_key_up(lua_State *L) } static const luaL_Reg kiwmi_keyboard_events[] = { + {"destroy", l_kiwmi_keyboard_on_destroy}, {"key_down", l_kiwmi_keyboard_on_key_down}, {"key_up", l_kiwmi_keyboard_on_key_up}, {NULL, NULL}, diff --git a/lua_docs.md b/lua_docs.md index 580d372..a07b1b4 100644 --- a/lua_docs.md +++ b/lua_docs.md @@ -114,6 +114,11 @@ Used to register event listeners. ### Events +#### destroy + +The keyboard is getting destroyed. +Callback receives the keyboard. + #### key_down A key got pressed.