load_keymap: handle error without corrupting global state (#353)

fixes https://github.com/i3/i3lock/issues/351
main
Michael Stapelberg 2024-04-10 08:09:55 +02:00 committed by GitHub
parent d8e853e986
commit a7ebe81de1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 5 deletions

View File

@ -114,6 +114,9 @@ static void u8_dec(char *s, int *i) {
* Necessary so that we can properly let xkbcommon track the keyboard state and
* translate keypresses to utf-8.
*
* This function can be called when the user changes the XKB configuration,
* so it must not leave unusable global state behind
*
*/
static bool load_keymap(void) {
if (xkb_context == NULL) {
@ -123,25 +126,26 @@ static bool load_keymap(void) {
}
}
xkb_keymap_unref(xkb_keymap);
int32_t device_id = xkb_x11_get_core_keyboard_device_id(conn);
DEBUG("device = %d\n", device_id);
if ((xkb_keymap = xkb_x11_keymap_new_from_device(xkb_context, conn, device_id, 0)) == NULL) {
struct xkb_keymap *new_keymap = xkb_x11_keymap_new_from_device(xkb_context, conn, device_id, 0);
if (new_keymap == NULL) {
fprintf(stderr, "[i3lock] xkb_x11_keymap_new_from_device failed\n");
return false;
}
struct xkb_state *new_state =
xkb_x11_state_new_from_device(xkb_keymap, conn, device_id);
xkb_x11_state_new_from_device(new_keymap, conn, device_id);
if (new_state == NULL) {
fprintf(stderr, "[i3lock] xkb_x11_state_new_from_device failed\n");
return false;
}
/* Only update global state on success */
xkb_state_unref(xkb_state);
xkb_keymap_unref(xkb_keymap);
xkb_state = new_state;
xkb_keymap = new_keymap;
return true;
}