From 86eaa44a3a9f5ee315aa1a6a676aeeb911942fd8 Mon Sep 17 00:00:00 2001 From: tokyo4j Date: Tue, 19 Nov 2024 10:13:23 +0900 Subject: [PATCH] wlr_keyboard: don't emit key event for duplicated keycodes This fixes the memory leak in wlr_keyboard_group.keys. The leak happened because wlr_keyboard.keycodes never contains duplicated keycodes while wlr_keyboard_group.keys can, so calling wlr_keyboard_finish() for all the wlr_keyboards in wlr_keyboard_group doesn't always free all the keys in wlr_keyboard_group.keys. (cherry picked from commit e21899037a97dcade40149f05ab54229c0018644) --- include/types/wlr_keyboard.h | 2 +- types/wlr_keyboard.c | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/types/wlr_keyboard.h b/include/types/wlr_keyboard.h index 7cd26e8a5..bdae09bdd 100644 --- a/include/types/wlr_keyboard.h +++ b/include/types/wlr_keyboard.h @@ -1,6 +1,6 @@ #include -void keyboard_key_update(struct wlr_keyboard *keyboard, +bool keyboard_key_update(struct wlr_keyboard *keyboard, struct wlr_keyboard_key_event *event); bool keyboard_modifier_update(struct wlr_keyboard *keyboard); diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index 98978ee1d..ad15032c6 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -65,8 +65,9 @@ bool keyboard_modifier_update(struct wlr_keyboard *keyboard) { return true; } -void keyboard_key_update(struct wlr_keyboard *keyboard, +bool keyboard_key_update(struct wlr_keyboard *keyboard, struct wlr_keyboard_key_event *event) { + size_t old_num_keycodes = keyboard->num_keycodes; if (event->state == WL_KEYBOARD_KEY_STATE_PRESSED) { set_add(keyboard->keycodes, &keyboard->num_keycodes, WLR_KEYBOARD_KEYS_CAP, event->keycode); @@ -77,6 +78,8 @@ void keyboard_key_update(struct wlr_keyboard *keyboard, } assert(keyboard->num_keycodes <= WLR_KEYBOARD_KEYS_CAP); + + return old_num_keycodes != keyboard->num_keycodes; } void wlr_keyboard_notify_modifiers(struct wlr_keyboard *keyboard, @@ -98,8 +101,9 @@ void wlr_keyboard_notify_modifiers(struct wlr_keyboard *keyboard, void wlr_keyboard_notify_key(struct wlr_keyboard *keyboard, struct wlr_keyboard_key_event *event) { - keyboard_key_update(keyboard, event); - wl_signal_emit_mutable(&keyboard->events.key, event); + if (keyboard_key_update(keyboard, event)) { + wl_signal_emit_mutable(&keyboard->events.key, event); + } if (keyboard->xkb_state == NULL) { return;