buffyboard: fix initialization of uinput

This commit is contained in:
Vladimir Stoiakin 2025-11-10 07:51:49 +00:00 committed by Johannes Marbach
parent 040c147ebc
commit 006c1f6bc8
2 changed files with 43 additions and 45 deletions

View file

@ -6,13 +6,18 @@
#include "uinput_device.h"
#include "../shared/log.h"
#include <linux/uinput.h>
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/uinput.h>
#if UINPUT_VERSION < 5
#error Buffyboard does not have support for uinput < 5
#endif
/**
* Static variables
@ -70,45 +75,38 @@ 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");
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");
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");
struct uinput_setup usetup = { 0 };
usetup.id.bustype = BUS_VIRTUAL;
strcpy(usetup.name, "buffyboard");
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) {
perror("Could not create uinput device");
bbx_log(BBX_LOG_LEVEL_ERROR, "Could not create uinput device");
return false;
}
@ -117,10 +115,10 @@ bool bb_uinput_device_init(const int * const scancodes, int num_scancodes) {
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();
}

View file

@ -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 */