naive keyboard support

This commit is contained in:
daydensteve
2025-12-14 10:48:27 -05:00
parent 0d0e22de61
commit f0651d5c32
7 changed files with 82 additions and 4 deletions

View File

@@ -23,6 +23,7 @@ src_geninput := \
io-thread.c \
kbd.c \
kbd-data.c \
kbd-naive.c \
mapper.c \
mapper-s11n.c \
mouse.c \

View File

@@ -28,6 +28,7 @@ EXPORTS
light_iter_is_valid
light_iter_next
light_iter_free
kbd_is_naive
mapper_config_load
mapper_config_save
mapper_clear_action_map

View File

@@ -0,0 +1,36 @@
#include <stdlib.h>
#include "geninput/hid.h"
#include "geninput/kbd-naive.h"
#include "geninput/kbd.h"
static struct hid_stub *kbd_naive_stub = NULL;
static struct hid_ri *kbd_naive_hid_ri = NULL;
static const char *kbd_naive_dev_node = "kbd-naive";
static const wchar_t *kbd_naive_dev_name = L"Any (naive binding)";
void kbd_naive_init(void)
{
kbd_naive_stub = hid_mgr_get_named_stub(kbd_naive_dev_node);
kbd_create(&kbd_naive_hid_ri, kbd_naive_dev_node, kbd_naive_dev_name);
hid_stub_attach(kbd_naive_stub, (struct hid *) kbd_naive_hid_ri);
}
void kbd_naive_fini(void)
{
free(kbd_naive_stub);
kbd_naive_stub = NULL;
}
void kbd_naive_event_notify(RAWINPUT *ri)
{
hid_ri_handle_event(kbd_naive_hid_ri, ri);
}
bool kbd_is_naive(struct hid_stub *stub)
{
return (stub == kbd_naive_stub);
}

View File

@@ -0,0 +1,13 @@
#ifndef GENINPUT_KBD_NAIVE_H
#define GENINPUT_KBD_NAIVE_H
#include <windows.h>
#include "geninput/hid-mgr.h"
void kbd_naive_init(void);
void kbd_naive_fini(void);
void kbd_naive_event_notify(RAWINPUT *ri);
bool kbd_is_naive(struct hid_stub *stub);
#endif

View File

@@ -89,7 +89,8 @@ static void kbd_static_init(void)
}
}
void kbd_create(struct hid_ri **hid_ri, const char *dev_node)
void kbd_create(
struct hid_ri **hid_ri, const char *dev_node, const wchar_t *name_override)
{
struct kbd *kbd;
char *tmp;
@@ -98,7 +99,11 @@ void kbd_create(struct hid_ri **hid_ri, const char *dev_node)
kbd = xcalloc(sizeof(*kbd));
kbd->super.vptr = &kbd_vtbl;
kbd->name = hid_ri_init_name(&kbd_guid, dev_node);
if (name_override) {
kbd->name = wstr_dup(name_override);
} else {
kbd->name = hid_ri_init_name(&kbd_guid, dev_node);
}
*hid_ri = &kbd->super;

View File

@@ -6,6 +6,13 @@
#define KBD_DEVICE_USAGE_KEYBOARD 0x00010006
#define KBD_DEVICE_USAGE_KEYPAD 0x00010007
void kbd_create(struct hid_ri **hid_ri, const char *dev_node);
/**
* Creates a keyboard device for use with key bindings
*
* @note name_override can be NULL and is only intended to be populated when
* the desired device name is not the name as indicated by the dev_node
*/
void kbd_create(
struct hid_ri **hid_ri, const char *dev_node, const wchar_t *name_override);
#endif

View File

@@ -3,6 +3,7 @@
#include "geninput/hid-mgr.h"
#include "geninput/hid.h"
#include "geninput/kbd.h"
#include "geninput/kbd-naive.h"
#include "geninput/mouse.h"
#include "geninput/ri.h"
@@ -45,6 +46,11 @@ void ri_init(HWND hwnd)
(unsigned int) GetLastError());
}
/* Init the naive keyboard before scanning for devices. This allows the
* actual hid devices to be preferred when using the bind dialog in the
* config ui */
kbd_naive_init();
ri_scan_devices();
}
@@ -104,7 +110,7 @@ void ri_scan_devices(void)
if (ridl[i].dwType == RIM_TYPEKEYBOARD) {
ri_handles[i].fake_fd = ridl[i].hDevice;
kbd_create(&ri_handles[i].hid_ri, dev_node);
kbd_create(&ri_handles[i].hid_ri, dev_node, NULL);
hid_stub_attach(stub, (struct hid *) ri_handles[i].hid_ri);
} else if (ridl[i].dwType == RIM_TYPEMOUSE) {
@@ -139,6 +145,13 @@ void ri_handle_msg(HRAWINPUT msg)
return;
}
/* Perform naive kdb updates prior to checking device. If the inputs were
generated by another program instead of from an actual hid device, we
don't want to miss them due to the hDevice check below. */
if (ri.header.dwType == RIM_TYPEKEYBOARD) {
kbd_naive_event_notify(&ri);
}
if (ri.header.hDevice == NULL) {
/* WTF?? I've seen this happen while remote-controlling someone's
desktop using TeamViewer, possibly due to an API hook injected by TV
@@ -161,6 +174,8 @@ void ri_fini(void)
{
RAWINPUTDEVICE filter[3];
kbd_naive_fini();
free(ri_handles);
ri_handles = NULL;
ri_ndevs = 0;