aboutsummaryrefslogtreecommitdiff
path: root/tools/gsr-global-hotkeys/keyboard_event.h
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2024-12-16 02:21:38 +0100
committerdec05eba <dec05eba@protonmail.com>2024-12-16 02:21:38 +0100
commitebd8c2726b8caac6adc00cf15c5631e51d05ba1f (patch)
treefb60d1908154aeec0ff6b568b269e97b9601d70b /tools/gsr-global-hotkeys/keyboard_event.h
parent970d87975b01cd083749179df489477b94a35f41 (diff)
Rewrite linux global hotkey to not depend on any libraries (also allows it to work on non-systemd systems(?)), remove unused gsr-window-name
Diffstat (limited to 'tools/gsr-global-hotkeys/keyboard_event.h')
-rw-r--r--tools/gsr-global-hotkeys/keyboard_event.h63
1 files changed, 63 insertions, 0 deletions
diff --git a/tools/gsr-global-hotkeys/keyboard_event.h b/tools/gsr-global-hotkeys/keyboard_event.h
new file mode 100644
index 0000000..5084659
--- /dev/null
+++ b/tools/gsr-global-hotkeys/keyboard_event.h
@@ -0,0 +1,63 @@
+#ifndef KEYBOARD_EVENT_H
+#define KEYBOARD_EVENT_H
+
+/* Read keyboard input from linux /dev/input/eventN devices, with hotplug support */
+
+#include "hotplug.h"
+
+/* C stdlib */
+#include <stdbool.h>
+#include <stdint.h>
+
+/* POSIX */
+#include <sys/poll.h>
+
+/* LINUX */
+#include <linux/input-event-codes.h>
+
+#define MAX_EVENT_POLLS 32
+
+typedef enum {
+ KEYBOARD_MODKEY_ALT = 1 << 0,
+ KEYBOARD_MODKEY_SUPER = 1 << 1,
+ KEYBOARD_MODKEY_CTRL = 1 << 2,
+ KEYBOARD_MODKEY_SHIFT = 1 << 3
+} keyboard_modkeys;
+
+typedef enum {
+ KEYBOARD_BUTTON_RELEASED,
+ KEYBOARD_BUTTON_PRESSED
+} keyboard_button_state;
+
+typedef struct {
+ struct pollfd event_polls[MAX_EVENT_POLLS]; /* Current size is |num_event_polls| */
+ int dev_input_ids[MAX_EVENT_POLLS]; /* Current size is |num_event_polls| */
+ int num_event_polls;
+
+ int stdout_event_index;
+ int hotplug_event_index;
+ bool stdout_failed;
+
+ hotplug_event hotplug_ev;
+
+ keyboard_button_state lshift_button_state;
+ keyboard_button_state rshift_button_state;
+ keyboard_button_state lctrl_button_state;
+ keyboard_button_state rctrl_button_state;
+ keyboard_button_state lalt_button_state;
+ keyboard_button_state ralt_button_state;
+ keyboard_button_state lmeta_button_state;
+ keyboard_button_state rmeta_button_state;
+} keyboard_event;
+
+/* |key| is a KEY_ from linux/input-event-codes.h. |modifiers| is a bitmask of keyboard_modkeys. |press_status| is 0 for released, 1 for pressed and 2 for repeat */
+typedef void (*key_callback)(uint32_t key, uint32_t modifiers, int press_status, void *userdata);
+
+bool keyboard_event_init(keyboard_event *self, bool poll_stdout_error);
+void keyboard_event_deinit(keyboard_event *self);
+
+/* If |timeout_milliseconds| is -1 then wait until an event is received */
+void keyboard_event_poll_events(keyboard_event *self, int timeout_milliseconds, key_callback callback, void *userdata);
+bool keyboard_event_stdout_has_failed(keyboard_event *self);
+
+#endif /* KEYBOARD_EVENT_H */