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>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#define MAX_PRESSED_KEYS (16)
|
|
|
|
|
|
|
|
|
|
struct key_array {
|
|
|
|
|
uint32_t keys[MAX_PRESSED_KEYS];
|
|
|
|
|
int nr_keys;
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-20 20:46:39 +01:00
|
|
|
static struct key_array pressed, bound, pressed_sent;
|
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)
|
|
|
|
|
{
|
|
|
|
|
array->keys[array->nr_keys++] = keycode;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 20:46:39 +01:00
|
|
|
uint32_t *
|
|
|
|
|
key_state_pressed_sent_keycodes(void)
|
|
|
|
|
{
|
|
|
|
|
/* 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]);
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
key_state_set_pressed(uint32_t keycode, bool ispressed)
|
|
|
|
|
{
|
|
|
|
|
if (ispressed) {
|
|
|
|
|
add_key(&pressed, keycode);
|
|
|
|
|
} else {
|
|
|
|
|
remove_key(&pressed, keycode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
key_state_store_pressed_keys_as_bound(void)
|
|
|
|
|
{
|
|
|
|
|
memcpy(bound.keys, pressed.keys, MAX_PRESSED_KEYS * sizeof(uint32_t));
|
|
|
|
|
bound.nr_keys = pressed.nr_keys;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
key_state_corresponding_press_event_was_bound(uint32_t keycode)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < bound.nr_keys; ++i) {
|
|
|
|
|
if (bound.keys[i] == keycode) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
key_state_nr_keys(void)
|
|
|
|
|
{
|
|
|
|
|
return bound.nr_keys;
|
|
|
|
|
}
|