From 006c1f6bc8468f2e53df84fe2568d3de085d5f6c Mon Sep 17 00:00:00 2001 From: Vladimir Stoiakin Date: Mon, 10 Nov 2025 07:51:49 +0000 Subject: [PATCH] buffyboard: fix initialization of uinput --- buffyboard/uinput_device.c | 74 +++++++++++++++++++------------------- buffyboard/uinput_device.h | 14 ++++---- 2 files changed, 43 insertions(+), 45 deletions(-) diff --git a/buffyboard/uinput_device.c b/buffyboard/uinput_device.c index 66523a7..99f266e 100644 --- a/buffyboard/uinput_device.c +++ b/buffyboard/uinput_device.c @@ -6,13 +6,18 @@ #include "uinput_device.h" +#include "../shared/log.h" + +#include +#include #include #include #include #include -#include - +#if UINPUT_VERSION < 5 +#error Buffyboard does not have support for uinput < 5 +#endif /** * Static variables @@ -70,57 +75,50 @@ static bool uinput_device_synchronise() { * Public functions */ -bool bb_uinput_device_init(const int * const scancodes, int num_scancodes) { +bool bb_uinput_device_init(const int * const keycodes, int num_keycodes) { fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK); - if (fd < 0) { - perror("Could not open /dev/uinput"); - return false; - } + if (fd < 0) { + bbx_log(BBX_LOG_LEVEL_ERROR, "Could not open /dev/uinput"); + return false; + } - if (ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0) { - perror("Could not set EVBIT for EV_KEY"); - return false; - } + if (ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0) { + bbx_log(BBX_LOG_LEVEL_ERROR, "Could not set EVBIT for EV_KEY"); + return false; + } - if (ioctl(fd, UI_SET_EVBIT, EV_SYN) < 0) { - perror("Could not set EVBIT for EV_SYN"); - return false; - } - - for (int i = 0; i < num_scancodes; ++i) { - if (ioctl(fd, UI_SET_KEYBIT, scancodes[i]) < 0) { - perror("Could not set KEYBIT"); + for (int i = 0; i < num_keycodes; ++i) { + if (ioctl(fd, UI_SET_KEYBIT, keycodes[i]) < 0) { + bbx_log(BBX_LOG_LEVEL_ERROR, "Could not set KEYBIT"); return false; } } - struct uinput_user_dev device; - memset(&device, 0, sizeof(device)); - strcpy(device.name, "buffyboard"); - device.id.bustype = BUS_USB; - device.id.vendor = 1; - device.id.product = 1; - device.id.version = 1; + static_assert(sizeof("buffyboard") <= UINPUT_MAX_NAME_SIZE); - if (ioctl(fd, UI_DEV_SETUP, &device) < 0) { - perror("Could not set up uinput device"); - return false; - } + struct uinput_setup usetup = { 0 }; + usetup.id.bustype = BUS_VIRTUAL; + strcpy(usetup.name, "buffyboard"); - if (ioctl(fd, UI_DEV_CREATE) < 0) { - perror("Could not create uinput device"); - return false; - } + if (ioctl(fd, UI_DEV_SETUP, &usetup) < 0) { + bbx_log(BBX_LOG_LEVEL_ERROR, "Could not set up uinput device"); + return false; + } + + if (ioctl(fd, UI_DEV_CREATE) < 0) { + bbx_log(BBX_LOG_LEVEL_ERROR, "Could not create uinput device"); + return false; + } memset(&event, 0, sizeof(event)); return true; } -bool bb_uinput_device_emit_key_down(int scancode) { - return uinput_device_emit(EV_KEY, scancode, 1) && uinput_device_synchronise(); +bool bb_uinput_device_emit_key_down(int keycode) { + return uinput_device_emit(EV_KEY, keycode, 1) && uinput_device_synchronise(); } -bool bb_uinput_device_emit_key_up(int scancode) { - return uinput_device_emit(EV_KEY, scancode, 0) && uinput_device_synchronise(); +bool bb_uinput_device_emit_key_up(int keycode) { + return uinput_device_emit(EV_KEY, keycode, 0) && uinput_device_synchronise(); } diff --git a/buffyboard/uinput_device.h b/buffyboard/uinput_device.h index 27055ca..95e2cf5 100644 --- a/buffyboard/uinput_device.h +++ b/buffyboard/uinput_device.h @@ -12,26 +12,26 @@ /** * Initialise the uinput keyboard device * - * @param scancodes array of scancodes the device can emit - * @param num_scancodes number of scancodes the device can emit + * @param keycodes array of keycodes the device can emit + * @param num_keycodes number of keycodes the device can emit * @return true if creating the device was successful, false otherwise */ -bool bb_uinput_device_init(const int * const scancodes, int num_scancodes); +bool bb_uinput_device_init(const int * const keycodes, int num_keycodes); /** * Emit a key down event * - * @param scancode the key's scancode + * @param keycode the key's keycode * @return true if emitting the event was successful, false otherwise */ -bool bb_uinput_device_emit_key_down(int scancode); +bool bb_uinput_device_emit_key_down(int keycode); /** * Emit a key up event * - * @param scancode the key's scancode + * @param keycode the key's keycode * @return true if emitting the event was successful, false otherwise */ -bool bb_uinput_device_emit_key_up(int scancode); +bool bb_uinput_device_emit_key_up(int keycode); #endif /* BB_UINPUT_DEVICE_H */