diff --git a/components/hid-rp/include/hid-rp-report-map.hpp b/components/hid-rp/include/hid-rp-report-map.hpp index da9ad55a26..b6c3130acb 100644 --- a/components/hid-rp/include/hid-rp-report-map.hpp +++ b/components/hid-rp/include/hid-rp-report-map.hpp @@ -576,13 +576,17 @@ 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 (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) { 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..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 @@ -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,89 @@ 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); + CHECK(k.mouse); // its relative axes read as pointer motion + } + + 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.