Add keboard:on_destroy

This commit is contained in:
buffet 2020-01-15 22:47:29 +00:00
parent 352e91258b
commit 598731af12
4 changed files with 75 additions and 0 deletions

View file

@ -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;
};

View file

@ -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);

View file

@ -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},

View file

@ -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.