From d0953cd6cab2a99951a985d4e54d05e643455a1e Mon Sep 17 00:00:00 2001 From: KurdtNervanna <136639095+KurdtNervanna@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:50:02 -0500 Subject: [PATCH] input: a pointer is no gamepad; document Bluetooth keyboards and mice A trackpad, tablet or a pad's touchpad node (absolute X, 4+ buttons, no KEY_A) was taken for an unconfigured gamepad and got a player port. Move the node classification into classify_dev() and exclude pointers from the generic heuristic (explicit pad bits still win). Test: make check-input (classify). docs/input-design.md: what works for USB/Bluetooth keyboards and mice today and what is not built (core keyboard/mouse forwarding, menu cursor). Co-Authored-By: Claude Sonnet 5.5 --- README.md | 2 +- docs/input-design.md | 19 +++++++++++++++++ frontend/src/input/input.c | 33 ++++++++++++++++++++++++----- frontend/tests/test_input.c | 42 +++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 488c35b..6060678 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ layer cake, and every setting lives in one menu. - RetroPie hotkeys: **Select+Start** exit, **Select+R/L** save/load state, **Select+Left/Right** state slot, **Select+X** in-game menu, **Select+B** reset. - **Select+R2** fast-forward, **Select+L2** screenshot, **Select+Y** game switcher (jump between your recent games, each resumed where you left it). - Remaps per system and per game, including the RetroStone2's optional 6-button Mega Drive layout. -- **Keyboard** (menus): arrows move, **Enter** = A (open, confirm), **Esc** or **Backspace** = B (back), **Tab** = Select, **Space** = Start, X / Y, **Page Up / Page Down** = L / R, **Home / End** = L2 / R2. A keyboard is not a game controller (games need a pad or the built-in buttons), but in a game **Esc** opens the in-game menu (with Exit) and **Tab** is the hotkey button (**Tab+Space** exits). +- **Keyboard** (menus): arrows move, **Enter** = A (open, confirm), **Esc** or **Backspace** = B (back), **Tab** = Select, **Space** = Start, X / Y, **Page Up / Page Down** = L / R, **Home / End** = L2 / R2. A keyboard is not a game controller (games need a pad or the built-in buttons), but in a game **Esc** opens the in-game menu (with Exit) and **Tab** is the hotkey button (**Tab+Space** exits). A Bluetooth keyboard works the same once paired (Settings > Network > Bluetooth devices); mice are not used yet. **Finding games** - **Letter jump** and **search** (in one list or across all games). diff --git a/docs/input-design.md b/docs/input-design.md index e3c0db1..a774d1f 100644 --- a/docs/input-design.md +++ b/docs/input-design.md @@ -100,6 +100,25 @@ Additional handling: a keyboard can still leave it: **Esc opens the in-game menu** (in the menus Esc stays B), and **Tab works as the hotkey button** of section 4 (Tab+Space exits, Tab+X menu, Tab+Page Down / Page Up save / load a state...), from any keyboard whatever the player order. Test: `make check-input` (`keyboard_in_game`). +- **Keyboards and mice (USB and Bluetooth)**: a Bluetooth keyboard or mouse reaches the frontend as an ordinary + `/dev/input/event*` node (BlueZ HID over `uhid`, docs/build.md "Networking"), so the rules are the same for USB and + Bluetooth. `classify_dev()` in `src/input/input.c` sorts a node by its capability bits: a pad (`BTN_GAMEPAD`, + `BTN_JOYSTICK`, `BTN_TRIGGER_HAPPY`, or an absolute X with 4+ buttons), a keyboard (`KEY_ENTER` and `KEY_A`) or + nothing. What works today: + - **Keyboard, menus and the in-game hotkeys**: yes, as above. A keyboard's media-key or system-control nodes are + separate devices and are ignored (a system "power" key does what the power button does). + - **Mouse, trackpad, tablet** (and a pad's touchpad node): **ignored**. There is no cursor in the menus, and the host + does not offer `RETRO_DEVICE_MOUSE` / `POINTER` / `KEYBOARD` to cores (`GET_INPUT_DEVICE_CAPABILITIES` is + joypad + analog, `SET_KEYBOARD_CALLBACK` is refused, `input_state_cb` returns 0 for everything else), so DOSBox + Pure and ScummVM see no mouse and no physical keyboard from any USB or Bluetooth device; they use their on-screen + keyboard and move their cursor with the pad. A pointer node is no longer taken for an unconfigured gamepad + (it used to when it had an absolute X and 4+ buttons). Test: `make check-input` (`classify`). + - **Pairing**: Settings > Network > Bluetooth devices pairs with Just Works (no PIN). A keyboard or mouse that + needs a PIN or a passkey typed on it cannot be paired from the console (no agent). The list does not say what + kind of device an entry is. TODO(hw): confirm with a real keyboard and mouse. + - **Not built (bigger)**: forwarding keyboard (`RETRO_ENVIRONMENT_SET_KEYBOARD_CALLBACK`, `RETRO_DEVICE_KEYBOARD`) + and mouse (`RETRO_DEVICE_MOUSE`/`POINTER`, relative axes + buttons per port) to cores, a menu cursor, and a + device-type label in the Bluetooth list (`bluetoothctl info` `Icon:`). - **Rumble** (done, batch 2): the libretro rumble interface (`retro_set_rumble_state`) is forwarded to an evdev FF_RUMBLE effect on pads that support it (`input_rumble()` in `src/input/input.c`). The pad's node is opened read-write (read-only fallback: no rumble); `EV_FF` + `FF_RUMBLE` in its capabilities marks it. One effect per pad, diff --git a/frontend/src/input/input.c b/frontend/src/input/input.c index 1707448..ac1df22 100644 --- a/frontend/src/input/input.c +++ b/frontend/src/input/input.c @@ -1595,6 +1595,31 @@ static bool name_is_builtin_stick(const struct input *in, const char *name) return s && *s && !strcmp(name, s); } +/* + * What an unknown evdev node is, from its capability bits (no ioctl, so the + * host tests can feed it). USB and Bluetooth (uhid) devices look the same. + * A pointer (mouse, trackpad, tablet, a pad's touchpad node) is no gamepad + * even with 4+ keys and an absolute X: the generic heuristic would hand it a + * player port. Pointers are not used by this layer yet + * (docs/input-design.md, "Keyboards and mice"). + */ +enum dev_kind { DK_OTHER, DK_PAD, DK_KEYBOARD }; + +static enum dev_kind classify_dev(const unsigned long *keybits, const unsigned long *absbits, int nkeys) +{ + bool pointer = TEST_BIT(BTN_LEFT, keybits) || TEST_BIT(BTN_TOUCH, keybits) || + TEST_BIT(BTN_TOOL_FINGER, keybits) || TEST_BIT(BTN_TOOL_PEN, keybits); + + if (TEST_BIT(BTN_GAMEPAD, keybits) || TEST_BIT(BTN_JOYSTICK, keybits) || + TEST_BIT(BTN_TRIGGER_HAPPY1, keybits)) + return DK_PAD; + if (TEST_BIT(KEY_ENTER, keybits) && TEST_BIT(KEY_A, keybits)) + return DK_KEYBOARD; + if (TEST_BIT(ABS_X, absbits) && nkeys >= 4 && !pointer) + return DK_PAD; + return DK_OTHER; +} + static int dev_open(struct input *in, const char *node) { char path[64]; @@ -1650,13 +1675,11 @@ static int dev_open(struct input *in, const char *node) if (d->builtin_stick) d->builtin = true; if (!d->builtin) { - bool pad = TEST_BIT(BTN_GAMEPAD, keybits) || TEST_BIT(BTN_JOYSTICK, keybits) || - TEST_BIT(BTN_TRIGGER_HAPPY1, keybits) || - (TEST_BIT(ABS_X, absbits) && nkeys >= 4 && !TEST_BIT(KEY_A, keybits)); + enum dev_kind kind = classify_dev(keybits, absbits, nkeys); - if (pad) { + if (kind == DK_PAD) { /* external gamepad */ - } else if (TEST_BIT(KEY_ENTER, keybits) && TEST_BIT(KEY_A, keybits)) { + } else if (kind == DK_KEYBOARD) { d->keyboard = true; } else if (TEST_BIT(KEY_POWER, keybits) && nkeys <= 4 && in->cfg.handle_power_key) { d->power_only = true; diff --git a/frontend/tests/test_input.c b/frontend/tests/test_input.c index 1bd66b2..91fc7d3 100644 --- a/frontend/tests/test_input.c +++ b/frontend/tests/test_input.c @@ -304,6 +304,47 @@ static void keyboard_in_game(void) rig_close(&r); } +/* ------------------------------ Bluetooth/USB keyboards and mice (classify) */ +static void set_keys(unsigned long *bits, const int *codes, int n) +{ + for (int i = 0; i < n; i++) + bits[codes[i] / BITS_PER_LONG_] |= 1ul << (codes[i] % BITS_PER_LONG_); +} + +static enum dev_kind kind_of(const int *keys, int nk, const int *abs, int na) +{ + unsigned long kb[NBITS(KEY_CNT)] = { 0 }, ab[NBITS(ABS_CNT)] = { 0 }; + + set_keys(kb, keys, nk); + set_keys(ab, abs, na); + return classify_dev(kb, ab, nk); +} + +static void classify(void) +{ + static const int kbd[] = { KEY_ENTER, KEY_A, KEY_ESC, KEY_TAB, KEY_SPACE }; + static const int mouse[] = { BTN_LEFT, BTN_RIGHT, BTN_MIDDLE, BTN_SIDE, BTN_EXTRA }; + static const int trackpad[] = { BTN_LEFT, BTN_TOUCH, BTN_TOOL_FINGER, BTN_TOOL_DOUBLETAP }; + static const int media[] = { KEY_VOLUMEUP, KEY_VOLUMEDOWN, KEY_MUTE, KEY_PLAYPAUSE }; + static const int pad[] = { BTN_SOUTH, BTN_EAST, BTN_NORTH, BTN_WEST }; + static const int cheap[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_PINKIE }; + static const int absxy[] = { ABS_X, ABS_Y }; + + puts("input: what a Bluetooth/USB keyboard, mouse or trackpad is"); + CHECK(kind_of(kbd, 5, NULL, 0) == DK_KEYBOARD, "a keyboard is a keyboard"); + CHECK(kind_of(mouse, 5, NULL, 0) == DK_OTHER, "a relative mouse is ignored"); + CHECK(kind_of(trackpad, 4, absxy, 2) == DK_OTHER, "an absolute trackpad is no gamepad"); + CHECK(kind_of(media, 4, NULL, 0) == DK_OTHER, "a keyboard's media-key node is ignored"); + CHECK(kind_of(pad, 4, absxy, 2) == DK_PAD, "a gamepad is a pad"); + CHECK(kind_of(cheap, 4, absxy, 2) == DK_PAD, "a joystick-style pad is a pad"); + CHECK(kind_of(cheap, 4, NULL, 0) == DK_PAD, "joystick buttons alone are a pad"); + { + static const int hmm[] = { 0x2c0 /* BTN_TRIGGER_HAPPY1 */ }; + + CHECK(kind_of(hmm, 1, NULL, 0) == DK_PAD, "a pad with only BTN_TRIGGER_HAPPY keys"); + } +} + int main(void) { puts("input: overflow recovery with stale events in the read buffer"); @@ -313,5 +354,6 @@ int main(void) menu_to_game(); stick_detect(); keyboard_in_game(); + classify(); return failures ? 1 : 0; }