2021-12-26 23:29:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2021-12-21 22:25:59 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdint.h>
|
2023-11-14 18:57:22 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2021-12-21 22:25:59 +00:00
|
|
|
#include <string.h>
|
2023-11-14 18:57:22 +00:00
|
|
|
#include <wlr/util/log.h>
|
2023-09-03 17:39:48 +02:00
|
|
|
#include "input/key-state.h"
|
2021-12-21 22:25:59 +00:00
|
|
|
|
|
|
|
|
#define MAX_PRESSED_KEYS (16)
|
|
|
|
|
|
|
|
|
|
struct key_array {
|
|
|
|
|
uint32_t keys[MAX_PRESSED_KEYS];
|
|
|
|
|
int nr_keys;
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-10 23:43:46 -05:00
|
|
|
static struct key_array pressed, pressed_mods, bound, pressed_sent;
|
2021-12-21 22:25:59 +00:00
|
|
|
|
2023-11-14 18:57:22 +00:00
|
|
|
static void
|
|
|
|
|
report(struct key_array *array, const char *msg)
|
|
|
|
|
{
|
|
|
|
|
static char *should_print;
|
|
|
|
|
static bool has_run;
|
|
|
|
|
|
|
|
|
|
if (!has_run) {
|
|
|
|
|
should_print = getenv("LABWC_DEBUG_KEY_STATE");
|
|
|
|
|
has_run = true;
|
|
|
|
|
}
|
|
|
|
|
if (!should_print) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
printf("%s", msg);
|
|
|
|
|
for (int i = 0; i < array->nr_keys; ++i) {
|
|
|
|
|
printf("%d,", array->keys[i]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 16:26:33 -04:00
|
|
|
static bool
|
|
|
|
|
key_present(struct key_array *array, uint32_t keycode)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < array->nr_keys; ++i) {
|
|
|
|
|
if (array->keys[i] == keycode) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 22:25:59 +00:00
|
|
|
static void
|
|
|
|
|
remove_key(struct key_array *array, uint32_t keycode)
|
|
|
|
|
{
|
|
|
|
|
bool shifting = false;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_PRESSED_KEYS; ++i) {
|
|
|
|
|
if (array->keys[i] == keycode) {
|
|
|
|
|
--array->nr_keys;
|
|
|
|
|
shifting = true;
|
|
|
|
|
}
|
|
|
|
|
if (shifting) {
|
|
|
|
|
array->keys[i] = i < MAX_PRESSED_KEYS - 1
|
|
|
|
|
? array->keys[i + 1] : 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
add_key(struct key_array *array, uint32_t keycode)
|
|
|
|
|
{
|
2022-11-02 16:26:33 -04:00
|
|
|
if (!key_present(array, keycode) && array->nr_keys < MAX_PRESSED_KEYS) {
|
|
|
|
|
array->keys[array->nr_keys++] = keycode;
|
|
|
|
|
}
|
2021-12-21 22:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
2022-09-20 20:46:39 +01:00
|
|
|
uint32_t *
|
|
|
|
|
key_state_pressed_sent_keycodes(void)
|
|
|
|
|
{
|
2023-11-14 18:57:22 +00:00
|
|
|
report(&pressed, "before - pressed:");
|
|
|
|
|
report(&bound, "before - bound:");
|
|
|
|
|
|
2022-09-20 20:46:39 +01:00
|
|
|
/* pressed_sent = pressed - bound */
|
|
|
|
|
memcpy(pressed_sent.keys, pressed.keys,
|
|
|
|
|
MAX_PRESSED_KEYS * sizeof(uint32_t));
|
|
|
|
|
pressed_sent.nr_keys = pressed.nr_keys;
|
|
|
|
|
for (int i = 0; i < bound.nr_keys; ++i) {
|
|
|
|
|
remove_key(&pressed_sent, bound.keys[i]);
|
|
|
|
|
}
|
2023-11-14 18:57:22 +00:00
|
|
|
|
|
|
|
|
report(&pressed_sent, "after - pressed_sent:");
|
|
|
|
|
|
2022-09-20 20:46:39 +01:00
|
|
|
return pressed_sent.keys;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
key_state_nr_pressed_sent_keycodes(void)
|
|
|
|
|
{
|
|
|
|
|
return pressed_sent.nr_keys;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 22:25:59 +00:00
|
|
|
void
|
2023-11-10 23:43:46 -05:00
|
|
|
key_state_set_pressed(uint32_t keycode, bool is_pressed, bool is_modifier)
|
2021-12-21 22:25:59 +00:00
|
|
|
{
|
2023-11-10 23:43:46 -05:00
|
|
|
if (is_pressed) {
|
2021-12-21 22:25:59 +00:00
|
|
|
add_key(&pressed, keycode);
|
2023-11-10 23:43:46 -05:00
|
|
|
if (is_modifier) {
|
|
|
|
|
add_key(&pressed_mods, keycode);
|
|
|
|
|
}
|
2021-12-21 22:25:59 +00:00
|
|
|
} else {
|
|
|
|
|
remove_key(&pressed, keycode);
|
2023-11-10 23:43:46 -05:00
|
|
|
remove_key(&pressed_mods, keycode);
|
2021-12-21 22:25:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2023-11-04 01:23:43 -04:00
|
|
|
key_state_store_pressed_key_as_bound(uint32_t keycode)
|
2021-12-21 22:25:59 +00:00
|
|
|
{
|
2023-11-04 01:23:43 -04:00
|
|
|
add_key(&bound, keycode);
|
2023-11-10 23:43:46 -05:00
|
|
|
/*
|
|
|
|
|
* Also store any pressed modifiers as bound. This prevents
|
|
|
|
|
* applications from seeing and handling the release event for
|
|
|
|
|
* a modifier key that was part of a keybinding (e.g. Firefox
|
|
|
|
|
* displays its menu bar for a lone Alt press + release).
|
|
|
|
|
*/
|
|
|
|
|
for (int i = 0; i < pressed_mods.nr_keys; ++i) {
|
|
|
|
|
add_key(&bound, pressed_mods.keys[i]);
|
|
|
|
|
}
|
2021-12-21 22:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
key_state_corresponding_press_event_was_bound(uint32_t keycode)
|
|
|
|
|
{
|
2022-11-02 16:26:33 -04:00
|
|
|
return key_present(&bound, keycode);
|
2021-12-21 22:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
2022-07-11 17:05:08 +01:00
|
|
|
void
|
2021-12-21 22:25:59 +00:00
|
|
|
key_state_bound_key_remove(uint32_t keycode)
|
|
|
|
|
{
|
|
|
|
|
remove_key(&bound, keycode);
|
|
|
|
|
}
|
2022-08-30 15:47:00 +01:00
|
|
|
|
|
|
|
|
int
|
2023-09-30 09:19:59 +01:00
|
|
|
key_state_nr_bound_keys(void)
|
2022-08-30 15:47:00 +01:00
|
|
|
{
|
|
|
|
|
return bound.nr_keys;
|
|
|
|
|
}
|
2023-09-26 17:51:54 +01:00
|
|
|
|
|
|
|
|
int
|
|
|
|
|
key_state_nr_pressed_keys(void)
|
|
|
|
|
{
|
|
|
|
|
return pressed.nr_keys;
|
|
|
|
|
}
|