keydev: add low-level filter for async keyboard handlers

This commit is contained in:
Lephe 2022-11-08 22:22:21 +01:00
parent 48db9bf09d
commit 252bd4eb41
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
2 changed files with 30 additions and 0 deletions

View file

@ -10,6 +10,7 @@ extern "C" {
#endif
#include <gint/keyboard.h>
#include <stdbool.h>
/* Size of the buffer event queue */
#define KEYBOARD_QUEUE_SIZE 32
@ -128,6 +129,9 @@ typedef struct {
} GPACKEDENUM keydev_transform_t;
/* keydev_async_filter_t: Low-level asynchronous event filter. */
typedef bool (*keydev_async_filter_t)(key_event_t event);
/* keydev_t: Keyboard device
This structure represents the state and settings of a keyboard device that
@ -159,6 +163,13 @@ typedef struct {
/* Event transforms */
keydev_transform_t tr;
/* Asynchronous event filter. This is a low-level filter which is
called when events are generated to process and filter them. It
provides the unique ability to run keyboard-triggered code even if
the program's main thread is busy, for instance running some sort of
infinite loop. */
keydev_async_filter_t async_filter;
// <Delayed Modifiers>
/* delayed_* is set when the delayed modifier is active (after a press/
@ -250,6 +261,12 @@ key_event_t keydev_unqueue_event(keydev_t *d);
must be terminated by a 0 keycode. */
bool keydev_idle(keydev_t *d, ...);
/* keydev_async_filter(): Obtain current async filter */
keydev_async_filter_t keydev_async_filter(keydev_t const *d);
/* keydev_set_async_filter(): Set low-level async filter */
void keydev_set_async_filter(keydev_t *d, keydev_async_filter_t filter);
//---
// High-level API to read from the device
//---

View file

@ -31,6 +31,9 @@ static int standard_repeater(GUNUSED int key, GUNUSED int duration, int count)
Returns false if the event cannot be pushed. */
bool keydev_queue_push(keydev_t *d, key_event_t ev)
{
if(d->async_filter && !d->async_filter(ev))
return true;
int next = (d->queue_end + 1) % KEYBOARD_QUEUE_SIZE;
if(next == d->queue_next)
{
@ -147,6 +150,16 @@ void keydev_tick(keydev_t *d, uint us)
}
}
keydev_async_filter_t keydev_async_filter(keydev_t const *d)
{
return d->async_filter;
}
void keydev_set_async_filter(keydev_t *d, keydev_async_filter_t filter)
{
d->async_filter = filter;
}
//---
// Keyboard event generation
//---