Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load_keymap: handle error without corrupting global state #353

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions i3lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down
Loading