From f8c8da2473615d593ecc6e91abd9903462744580 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Tue, 1 Dec 2026 23:08:04 -0600 Subject: [PATCH 1/2] fix(hid-rp): a mouse is not a gamepad (looks_like_gamepad needs an absolute stick, hat or d-pad) GamepadDecoder::looks_like_gamepad() took any Button-page field plus a Generic Desktop X / Y / hat / d-pad as a gamepad, without checking whether the axes were relative. Relative X / Y is pointer motion, so a plain boot mouse, a keyboard with a built-in pointer and a SpaceMouse all classified as gamepads; decoded as one, a mouse's left click came out as South (A) and its motion was dropped. Only absolute direction fields count now. Keyboards were never affected (no Button-page fields). Tests: every hid-rp controller descriptor (Xbox, DualSense, DualShock 4, Switch Pro) is still a gamepad and only a gamepad; a boot mouse, a SpaceMouse, a boot keyboard and a keyboard + pointer combo are not; the new checks fail under the old rule. The Tab5 USB host example still recognises a SpaceMouse by vendor id first, since its descriptor now reads as a mouse. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU --- .../hid-rp/include/hid-rp-report-map.hpp | 7 +- .../hid-rp/test/hid_inspector_host_test.cpp | 6 +- .../test/hid_rp_report_map_host_test.cpp | 86 +++++++++++++++++++ components/usb_host/tab5_example/README.md | 4 +- .../main/usb_host_tab5_example.cpp | 4 +- 5 files changed, 98 insertions(+), 9 deletions(-) diff --git a/components/hid-rp/include/hid-rp-report-map.hpp b/components/hid-rp/include/hid-rp-report-map.hpp index da9ad55a26..a94e98d568 100644 --- a/components/hid-rp/include/hid-rp-report-map.hpp +++ b/components/hid-rp/include/hid-rp-report-map.hpp @@ -576,13 +576,16 @@ class GamepadDecoder { latest_.resize(report_ids_.size()); } - /// Whether a map looks like a gamepad (buttons plus a direction source). + /// Whether a map looks like a gamepad: buttons plus an absolute direction + /// source (a stick, hat or d-pad). Relative X / Y is pointer motion, so a + /// mouse, a keyboard with a built-in pointer or a SpaceMouse is not a + /// gamepad; a keyboard has no Button-page fields at all. static bool looks_like_gamepad(const ReportMap &map) { bool buttons = false, direction = false; for (const auto &f : map.fields()) { if (f.usage_page == usage::PAGE_BUTTON) buttons = true; - if (f.usage_page == usage::PAGE_GENERIC_DESKTOP && + if (f.usage_page == usage::PAGE_GENERIC_DESKTOP && !f.relative && (f.usage == usage::GD_HAT || f.usage == usage::GD_X || f.usage == usage::GD_Y || (f.usage >= usage::GD_DPAD_UP && f.usage <= usage::GD_DPAD_LEFT))) direction = true; diff --git a/components/hid-rp/test/hid_inspector_host_test.cpp b/components/hid-rp/test/hid_inspector_host_test.cpp index a0b6d71017..2a4c03d8fc 100644 --- a/components/hid-rp/test/hid_inspector_host_test.cpp +++ b/components/hid-rp/test/hid_inspector_host_test.cpp @@ -57,12 +57,12 @@ TEST_CASE("the Xbox descriptor: items, fields, a gamepad with the Android layout CHECK(has(j, R"("output_fields":[{)")); // the rumble output report } -// What the decoders conclude, not what the device is: the SpaceMouse's axes -// and buttons make it look like a gamepad and a mouse to them. +// What the decoders conclude, not what the device is: the SpaceMouse's +// relative axes read as pointer motion (a mouse), not as a gamepad's sticks. TEST_CASE("the SpaceMouse, as the decoders classify it") { const auto j = parse_json(preset("SpaceMouse").descriptor); CHECK(has(j, "\"ok\":true")); - CHECK(has(j, R"("kind":{"gamepad":true,"keyboard":false,"mouse":true})")); + CHECK(has(j, R"("kind":{"gamepad":false,"keyboard":false,"mouse":true})")); } TEST_CASE("a real controller's report, as a pass-through sees it") { diff --git a/components/hid-rp/test/hid_rp_report_map_host_test.cpp b/components/hid-rp/test/hid_rp_report_map_host_test.cpp index a6ab7884c6..0b8d7132e5 100644 --- a/components/hid-rp/test/hid_rp_report_map_host_test.cpp +++ b/components/hid-rp/test/hid_rp_report_map_host_test.cpp @@ -9,12 +9,16 @@ #include #include #include +#include #include #include #include +#include "hid-rp-3dconnexion.hpp" #include "hid-rp-gamepad.hpp" +#include "hid-rp-playstation.hpp" +#include "hid-rp-ps4.hpp" #include "hid-rp-report-map.hpp" #include "hid-rp-switch-pro.hpp" #include "hid-rp-xbox.hpp" @@ -286,6 +290,88 @@ TEST_CASE("mouse") { CHECK(m.wheel == -1); } +// What each decoder's looks_like_*() concludes. Relative X / Y is pointer +// motion: a mouse, a keyboard with a built-in pointer and a SpaceMouse are not +// gamepads (they have buttons, but no absolute stick, hat or d-pad). +TEST_CASE("classification: gamepads, mice, keyboards, a SpaceMouse") { + using espp::hid_rp::GamepadDecoder; + using espp::hid_rp::KeyboardDecoder; + using espp::hid_rp::MouseDecoder; + using espp::hid_rp::ReportMap; + struct Kind { + bool gamepad, keyboard, mouse; + }; + auto kind = [](std::span desc) { + auto map = ReportMap::parse(desc); + REQUIRE(map.has_value()); + return Kind{GamepadDecoder::looks_like_gamepad(*map), + KeyboardDecoder::looks_like_keyboard(*map), MouseDecoder::looks_like_mouse(*map)}; + }; + auto bytes = [](const auto &desc) { return std::vector(desc.begin(), desc.end()); }; + + SUBCASE("hid-rp's controllers are gamepads, and nothing else") { + const std::vector>> pads{ + {"xbox", bytes(espp::xbox_descriptor())}, + {"dualsense", bytes(espp::playstation_dualsense_ble_descriptor())}, + {"dualshock4", bytes(espp::ps4_dualshock4_descriptor())}, + {"switch pro", bytes(espp::switch_pro_descriptor())}, + }; + for (const auto &[name, desc] : pads) { + CAPTURE(name); + const auto k = kind(desc); + CHECK(k.gamepad); + CHECK_FALSE(k.keyboard); + CHECK_FALSE(k.mouse); + } + } + + SUBCASE("a mouse is not a gamepad") { + // the HID 1.11 appendix B.2 boot mouse: 3 buttons, relative X / Y + const std::vector mouse{0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01, 0xa1, 0x00, + 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, 0x15, 0x00, 0x25, 0x01, + 0x95, 0x03, 0x75, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x05, + 0x81, 0x01, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x15, 0x81, + 0x25, 0x7f, 0x75, 0x08, 0x95, 0x02, 0x81, 0x06, 0xc0, 0xc0}; + const auto k = kind(mouse); + CHECK_FALSE(k.gamepad); + CHECK_FALSE(k.keyboard); + CHECK(k.mouse); + } + + SUBCASE("a SpaceMouse is not a gamepad") { + const auto k = kind(bytes(espp::spacemouse_descriptor())); + CHECK_FALSE(k.gamepad); + CHECK_FALSE(k.keyboard); + } + + SUBCASE("a keyboard is not a gamepad, with or without a built-in pointer") { + // the boot keyboard: modifiers, a reserved byte, six key slots + const std::vector keyboard{ + 0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 0x25, + 0x01, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x01, 0x95, 0x06, + 0x75, 0x08, 0x15, 0x00, 0x25, 0x65, 0x05, 0x07, 0x19, 0x00, 0x29, 0x65, 0x81, 0x00, 0xc0}; + const auto k = kind(keyboard); + CHECK_FALSE(k.gamepad); + CHECK(k.keyboard); + CHECK_FALSE(k.mouse); + + // the same keyboard (report 1) plus a touchpad / trackpoint mouse (report + // 2) in one descriptor, as wireless keyboards often send + std::vector combo{0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x85, 0x01}; + combo.insert(combo.end(), keyboard.begin() + 6, keyboard.end()); + const std::vector pointer{ + 0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x85, 0x02, 0x09, 0x01, 0xa1, 0x00, 0x05, + 0x09, 0x19, 0x01, 0x29, 0x03, 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01, + 0x81, 0x02, 0x95, 0x01, 0x75, 0x05, 0x81, 0x01, 0x05, 0x01, 0x09, 0x30, 0x09, + 0x31, 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 0x95, 0x02, 0x81, 0x06, 0xc0, 0xc0}; + combo.insert(combo.end(), pointer.begin(), pointer.end()); + const auto kc = kind(combo); + CHECK_FALSE(kc.gamepad); + CHECK(kc.keyboard); + CHECK(kc.mouse); + } +} + // Report IDs, a Consumer-page array item, and DPad usages (the Xbox-style // layout many mobile controllers use). TEST_CASE("report ids consumer dpad") { diff --git a/components/usb_host/tab5_example/README.md b/components/usb_host/tab5_example/README.md index beb86eb0d5..6971244b14 100644 --- a/components/usb_host/tab5_example/README.md +++ b/components/usb_host/tab5_example/README.md @@ -112,5 +112,5 @@ class driver come from the registry, as in the plain `usb_host` example). interfaces. - A SpaceMouse is recognised by 3Dconnexion's vendor id (`0x256F`), or by the product ids of the early Logitech-branded SpaceNavigator / SpaceExplorer / - SpacePilot (`0x046D:C62x`), before the descriptor is looked at: its six axes - and buttons would otherwise pass for a gamepad. + SpacePilot (`0x046D:C62x`), before the descriptor is looked at: its six + relative axes and buttons would otherwise pass for a mouse. diff --git a/components/usb_host/tab5_example/main/usb_host_tab5_example.cpp b/components/usb_host/tab5_example/main/usb_host_tab5_example.cpp index b7d424f710..4bb8516397 100644 --- a/components/usb_host/tab5_example/main/usb_host_tab5_example.cpp +++ b/components/usb_host/tab5_example/main/usb_host_tab5_example.cpp @@ -145,8 +145,8 @@ extern "C" void app_main(void) { .protocol = params.protocol, .report_descriptor_bytes = device->report_descriptor().size(), .kind = Gui::Kind::Unknown}; - // A SpaceMouse first, by vendor id: its descriptor (six axes and - // buttons) would otherwise pass for a gamepad. Everything else is + // A SpaceMouse first, by vendor id: its descriptor (six relative + // axes and buttons) would otherwise pass for a mouse. Everything else is // classified from its report descriptor; UsbHost has already asked a // boot-subclass interface for the report protocol, so the descriptor // describes what the device actually sends. From 6ad539ca456b05bb56ac3e3a0ee18588b7af9b07 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Tue, 1 Dec 2026 23:27:50 -0600 Subject: [PATCH 2/2] test(hid-rp): the SpaceMouse classifies as a mouse; doc wording (review) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU --- components/hid-rp/include/hid-rp-report-map.hpp | 3 ++- components/hid-rp/test/hid_rp_report_map_host_test.cpp | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/components/hid-rp/include/hid-rp-report-map.hpp b/components/hid-rp/include/hid-rp-report-map.hpp index a94e98d568..b6c3130acb 100644 --- a/components/hid-rp/include/hid-rp-report-map.hpp +++ b/components/hid-rp/include/hid-rp-report-map.hpp @@ -577,7 +577,8 @@ class GamepadDecoder { } /// Whether a map looks like a gamepad: buttons plus an absolute direction - /// source (a stick, hat or d-pad). Relative X / Y is pointer motion, so a + /// source (an absolute X or Y axis, a hat or a d-pad). Relative X / Y is + /// pointer motion, so a /// mouse, a keyboard with a built-in pointer or a SpaceMouse is not a /// gamepad; a keyboard has no Button-page fields at all. static bool looks_like_gamepad(const ReportMap &map) { diff --git a/components/hid-rp/test/hid_rp_report_map_host_test.cpp b/components/hid-rp/test/hid_rp_report_map_host_test.cpp index 0b8d7132e5..89c350f3c5 100644 --- a/components/hid-rp/test/hid_rp_report_map_host_test.cpp +++ b/components/hid-rp/test/hid_rp_report_map_host_test.cpp @@ -342,6 +342,7 @@ TEST_CASE("classification: gamepads, mice, keyboards, a SpaceMouse") { const auto k = kind(bytes(espp::spacemouse_descriptor())); CHECK_FALSE(k.gamepad); CHECK_FALSE(k.keyboard); + CHECK(k.mouse); // its relative axes read as pointer motion } SUBCASE("a keyboard is not a gamepad, with or without a built-in pointer") {