labwc/include/input/key-state.h
John Lindgren 7571c4bed3 keyboard: avoid stuck keys due to keybindings (alternate approach)
Before commit e77330bc3f, there were issues with keys becoming "stuck"
if other keys were pressed at the time a keybinding was matched, because
those other keys were included in the "bound" set and the release events
were incorrectly eaten by labwc.

Commit e77330bc3f solved that issue with the "big hammer" approach of
preventing keybindings from working at all if other keys were pressed:

        if (key_state_nr_pressed_keys() > 1) {
                return false;
        }

This is an alternate approach to solving the original problem, by (1)
not including those other keys in the "bound" set and (2) making sure we
always forward release events for un-bound keys to clients (even if a
menu or OSD is displayed).

Details:

- Since we only ever want to store the single matched keycode as bound,
  key_state_store_pressed_keys_as_bound() doesn't really make sense in
  the plural, so rename it to key_state_store_pressed_key_as_bound() and
  pass in the keycode.

- The calls to key_state_store_pressed_keys_as_bound() within
  handle_keybinding() appear to be redundant since it is also called
  from the parent function (handle_compositor_keybindings()). So remove
  these calls.

- Finally, rework the logic for handling key-release events so that we
  always forward release events for keys not in the "bound" set.

This PR does not remove the "key_state_nr_pressed_keys() > 1" check, and
because of that should not result in any functional change. It should
however make it possible to relax or remove that check in future.
2023-11-12 17:37:30 +00:00

29 lines
1 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef LABWC_KEY_STATE_H
#define LABWC_KEY_STATE_H
#include <stdbool.h>
#include <stdint.h>
/*
* All keycodes in these functions are (Linux) libinput evdev scancodes which is
* what 'wlr_keyboard' uses (e.g. 'seat->keyboard_group->keyboard->keycodes').
* Note: These keycodes are different to XKB scancodes by a value of 8.
*/
/**
* key_state_pressed_sent_keycodes - generate array of pressed+sent keys
* Note: The array is generated by subtracting any bound keys from _all_ pressed
* keys (because bound keys were not forwarded to clients).
*/
uint32_t *key_state_pressed_sent_keycodes(void);
int key_state_nr_pressed_sent_keycodes(void);
void key_state_set_pressed(uint32_t keycode, bool ispressed);
void key_state_store_pressed_key_as_bound(uint32_t keycode);
bool key_state_corresponding_press_event_was_bound(uint32_t keycode);
void key_state_bound_key_remove(uint32_t keycode);
int key_state_nr_bound_keys(void);
int key_state_nr_pressed_keys(void);
#endif /* LABWC_KEY_STATE_H */