Xbox Cloud Gaming app for PS5 Homebrew
C
feat(ps4, input): native in-app universal USB controller support (OneFamily)
Rafael Abud Ferreira979a15416248
diff --git a/CMakeLists.txt b/CMakeLists.txtindex ea269a1..6ac89fe 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -49,6 +49,7 @@ add_library(xcloud_core STATICsrc/input/pad_map.cppsrc/input/keyboard_nav.cppsrc/input/pad_usb.cpp+ src/input/pad_usb_hid.cppsrc/input/sdl_mapping.cppsrc/xcloud/social.cppsrc/xcloud/achievements.cpp@@ -200,6 +201,7 @@ elseif(XC_TARGET_PS4)src/display/display_ps4.cppsrc/media/hw_decoder_ps4.cppsrc/input/controller_ps4.cpp+ src/input/pad_usb_ps4.cppsrc/input/kbm_ps4.cppsrc/media/audio_out_ps4.cppsrc/media/audio_in_ps4.cppdiff --git a/notes.md b/notes.mdindex 064ccf9..2e9deac 100644--- a/notes.md+++ b/notes.md@@ -12,6 +12,10 @@- **Eliminated Ghost Phone Icon**: Fixed virtual gamepad detection so inactive WebUI connections do not display a phantom phone controller icon on desktop slot 0.- **Low-Latency Presentation & Handheld Detection**: Vulkan NV12 decode pipeline and mailbox presentation for Linux desktop and handheld devices (Steam Deck / ROG Ally / Legion Go).+## 🎮 PS4 Native USB Controller Support (OneFamily Integration)+- **Universal USB Controller Drivers**: Direct in-app support on PS4 (using `libSceUsbd`) for PS3 (DualShock 3), PS4 (wired/third-party), PS5 (DualSense), Xbox 360, Xbox One / Series X|S (GIP), 8BitDo adapters/controllers, Nintendo Switch Pro & Input-Only, Google Stadia, and generic DirectInput/HID gamepads.+- **Autonomous & Zero-Dependency**: Works out of the box directly in the application without requiring background daemons or external GoldHEN payloads.+---**Installation on PS5**:- Install directly from **ProsperoStore** or **RecompStore**, or download the signed `PPSA99810.zip` and install manually via Homebrew / FTP.@@ -36,6 +40,10 @@- **Eliminação do Ícone Fantasma de Celular**: Corrigida a detecção do controle virtual da WebUI para evitar a exibição indevida de ícone de celular no slot 0 do desktop.- **Apresentação de Baixa Latência & Detecção de Handhelds**: Pipeline Vulkan NV12 e modo de apresentação mailbox para desktop Linux e dispositivos portáteis (Steam Deck / ROG Ally / Legion Go).+## 🎮 Suporte Nativo a Controles USB no PS4 (Integração OneFamily)+- **Drivers Universais de Controles USB**: Suporte nativo direto no app para PS4 (via `libSceUsbd`) para controles de PS3 (DualShock 3), PS4 (com fio/paralelos), PS5 (DualSense), Xbox 360, Xbox One / Series X|S (GIP), adaptadores/controles 8BitDo, Nintendo Switch Pro e Input-Only, Google Stadia e gamepads HID/DirectInput genéricos.+- **Autônomo e Sem Dependências Externas**: Funciona imediatamente dentro do próprio aplicativo sem necessidade de daemons ou payloads externos do GoldHEN em segundo plano.+---**Instalação no PS5**:- Instale diretamente pela **ProsperoStore** ou **RecompStore**, ou baixe o pacote assinado `PPSA99810.zip` e instale manualmente via Homebrew / FTP.diff --git a/ps4/compat/libusb.h b/ps4/compat/libusb.hnew file mode 100644index 0000000..429795d--- /dev/null+++ b/ps4/compat/libusb.h@@ -0,0 +1,23 @@+#ifndef _PS4_COMPAT_LIBUSB_H_+#define _PS4_COMPAT_LIBUSB_H_++#include <stdint.h>++// OpenOrbis Usbd.h includes <libusb.h>, but places types in <orbis/_types/libusb.h>+// and forward declares struct libusb_endpoint_descriptor without defining it.+struct libusb_endpoint_descriptor {+ uint8_t bLength;+ uint8_t bDescriptorType;+ uint8_t bEndpointAddress;+ uint8_t bmAttributes;+ uint16_t wMaxPacketSize;+ uint8_t bInterval;+ uint8_t bRefresh;+ uint8_t bSynchAddress;+ const unsigned char *extra;+ int32_t extra_length;+};++#include <orbis/_types/libusb.h>++#endif // _PS4_COMPAT_LIBUSB_H_diff --git a/src/input/controller_ps4.cpp b/src/input/controller_ps4.cppindex 464ddc8..c35c7f2 100644--- a/src/input/controller_ps4.cpp+++ b/src/input/controller_ps4.cpp@@ -3,6 +3,7 @@#include "input/controller.h"#include "input/controller_backend.h"#include "input/input_ext.h"+#include "input/pad_usb.h"#include "util/log.h"#include <algorithm>@@ -264,6 +265,7 @@ bool init() {XC_LOGW("sceUserServiceGetInitialUser: 0x%08x (uid=%d), will retry in refreshPads", static_cast<unsigned>(rc), uid);}refreshPads();+ usbPadsStart();return true;}@@ -313,6 +315,7 @@ void refreshPads() {}void shutdown() {+ usbPadsStop();for (int i = 0; i < 4; ++i) closePad(i);}diff --git a/src/input/pad_usb.cpp b/src/input/pad_usb.cppindex 0d6123b..3d61548 100644--- a/src/input/pad_usb.cpp+++ b/src/input/pad_usb.cpp@@ -4,6 +4,7 @@// pollUsbPad(). The report layouts are those of the Linux xpad driver and// OmniPad CE (usb_controllers.c).#include "input/pad_usb.h"+#include "input/pad_usb_hid.h"#include "input/keyboard_nav.h"#include "platform/platform.h"@@ -207,10 +208,386 @@ bool parseGipReport(const uint8_t* data, size_t len, ControllerState& s) {return true;}+const char* usbPadProtocolName(UsbPadProtocol p) {+ switch (p) {+ case UsbPadProtocol::XInput: return "XInput";+ case UsbPadProtocol::Gip: return "GIP";+ case UsbPadProtocol::DualShock3: return "DualShock 3";+ case UsbPadProtocol::DualShock4: return "DualShock 4";+ case UsbPadProtocol::DualSense: return "DualSense";+ case UsbPadProtocol::SwitchPro: return "Switch Pro";+ case UsbPadProtocol::SwitchInputOnly: return "Switch";+ case UsbPadProtocol::Stadia: return "Stadia";+ case UsbPadProtocol::GenericHid: return "Generic HID";+ case UsbPadProtocol::None: break;+ }+ return "none";+}++bool parseDualShock3Report(const uint8_t* data, size_t len, ControllerState& s) {+ if (!data || len < 14 || data[0] != 0x01) return false;+ const uint8_t b1 = data[2];+ const uint8_t b2 = data[3];+ const uint8_t b3 = (len > 4) ? data[4] : 0;++ s.btnTouchpad = (b1 & 0x01) != 0; // Select+ s.btnL3 = (b1 & 0x02) != 0;+ s.btnR3 = (b1 & 0x04) != 0;+ s.btnOptions = (b1 & 0x08) != 0; // Start+ s.dpadUp = (b1 & 0x10) != 0;+ s.dpadRight = (b1 & 0x20) != 0;+ s.dpadDown = (b1 & 0x40) != 0;+ s.dpadLeft = (b1 & 0x80) != 0;++ s.btnL1 = (b2 & 0x04) != 0;+ s.btnR1 = (b2 & 0x08) != 0;+ s.btnY = (b2 & 0x10) != 0; // Triangle+ s.btnB = (b2 & 0x20) != 0; // Circle+ s.btnA = (b2 & 0x40) != 0; // Cross+ s.btnX = (b2 & 0x80) != 0; // Square++ s.btnNexus = (b3 & 0x01) != 0; // PS button++ if (len >= 10) {+ s.rawLeftX = s.leftStickX = (static_cast<float>(data[6]) - 128.0f) / 127.5f;+ s.rawLeftY = s.leftStickY = (static_cast<float>(data[7]) - 128.0f) / 127.5f;+ s.rawRightX = s.rightStickX = (static_cast<float>(data[8]) - 128.0f) / 127.5f;+ s.rawRightY = s.rightStickY = (static_cast<float>(data[9]) - 128.0f) / 127.5f;+ }+ if (len >= 20) {+ s.triggerL2 = static_cast<float>(data[18]) / 255.0f;+ s.triggerR2 = static_cast<float>(data[19]) / 255.0f;+ } else {+ s.triggerL2 = (b2 & 0x01) ? 1.0f : 0.0f;+ s.triggerR2 = (b2 & 0x02) ? 1.0f : 0.0f;+ }++ s.connected = true;+ return true;+}++bool parseDualShock4Report(const uint8_t* data, size_t len, ControllerState& s) {+ if (!data || len < 10) return false;+ const uint8_t* p = (data[0] == 0x01 && len >= 11) ? data + 1 : data;+ const size_t effectiveLen = (data[0] == 0x01 && len >= 11) ? (len - 1) : len;+ if (effectiveLen < 9) return false;++ s.rawLeftX = s.leftStickX = (static_cast<float>(p[0]) - 128.0f) / 127.5f;+ s.rawLeftY = s.leftStickY = (static_cast<float>(p[1]) - 128.0f) / 127.5f;+ s.rawRightX = s.rightStickX = (static_cast<float>(p[2]) - 128.0f) / 127.5f;+ s.rawRightY = s.rightStickY = (static_cast<float>(p[3]) - 128.0f) / 127.5f;++ const uint8_t b0 = p[4];+ const uint8_t hat = b0 & 0x0F;+ s.dpadUp = s.dpadDown = s.dpadLeft = s.dpadRight = false;+ switch (hat) {+ case 0: s.dpadUp = true; break;+ case 1: s.dpadUp = s.dpadRight = true; break;+ case 2: s.dpadRight = true; break;+ case 3: s.dpadDown = s.dpadRight = true; break;+ case 4: s.dpadDown = true; break;+ case 5: s.dpadDown = s.dpadLeft = true; break;+ case 6: s.dpadLeft = true; break;+ case 7: s.dpadUp = s.dpadLeft = true; break;+ default: break;+ }+ s.btnX = (b0 & 0x10) != 0; // Square+ s.btnA = (b0 & 0x20) != 0; // Cross+ s.btnB = (b0 & 0x40) != 0; // Circle+ s.btnY = (b0 & 0x80) != 0; // Triangle++ const uint8_t b1 = p[5];+ s.btnL1 = (b1 & 0x01) != 0;+ s.btnR1 = (b1 & 0x02) != 0;+ s.btnOptions = (b1 & 0x20) != 0; // Options+ s.btnL3 = (b1 & 0x40) != 0;+ s.btnR3 = (b1 & 0x80) != 0;++ const uint8_t b2 = p[6];+ s.btnNexus = (b2 & 0x01) != 0; // PS button+ s.btnTouchpad = ((b1 & 0x10) != 0) || ((b2 & 0x02) != 0); // Share or Touchpad click++ if (effectiveLen >= 9) {+ s.triggerL2 = static_cast<float>(p[7]) / 255.0f;+ s.triggerR2 = static_cast<float>(p[8]) / 255.0f;+ } else {+ s.triggerL2 = (b1 & 0x04) ? 1.0f : 0.0f;+ s.triggerR2 = (b1 & 0x08) ? 1.0f : 0.0f;+ }++ if (effectiveLen >= 39) {+ if ((p[34] & 0x80) == 0) { // active touch 1+ s.touching = true;+ uint16_t tx = static_cast<uint16_t>(p[35] | ((p[36] & 0x0F) << 8));+ uint16_t ty = static_cast<uint16_t>((p[36] >> 4) | (p[37] << 4));+ s.touchX = std::clamp(static_cast<float>(tx) / 1919.0f, 0.0f, 1.0f);+ s.touchY = std::clamp(static_cast<float>(ty) / 941.0f, 0.0f, 1.0f);+ s.touchSecond = (effectiveLen >= 43 && (p[38] & 0x80) == 0);+ } else {+ s.touching = false;+ s.touchSecond = false;+ }+ }++ s.connected = true;+ return true;+}++bool parseDualSenseReport(const uint8_t* data, size_t len, ControllerState& s) {+ if (!data || len < 10) return false;+ const uint8_t* p = (data[0] == 0x01 && len >= 11) ? data + 1 : data;+ const size_t effectiveLen = (data[0] == 0x01 && len >= 11) ? (len - 1) : len;+ if (effectiveLen < 10) return false;++ s.rawLeftX = s.leftStickX = (static_cast<float>(p[0]) - 128.0f) / 127.5f;+ s.rawLeftY = s.leftStickY = (static_cast<float>(p[1]) - 128.0f) / 127.5f;+ s.rawRightX = s.rightStickX = (static_cast<float>(p[2]) - 128.0f) / 127.5f;+ s.rawRightY = s.rightStickY = (static_cast<float>(p[3]) - 128.0f) / 127.5f;+ s.triggerL2 = static_cast<float>(p[4]) / 255.0f;+ s.triggerR2 = static_cast<float>(p[5]) / 255.0f;++ const uint8_t b0 = p[7];+ const uint8_t hat = b0 & 0x0F;+ s.dpadUp = s.dpadDown = s.dpadLeft = s.dpadRight = false;+ switch (hat) {+ case 0: s.dpadUp = true; break;+ case 1: s.dpadUp = s.dpadRight = true; break;+ case 2: s.dpadRight = true; break;+ case 3: s.dpadDown = s.dpadRight = true; break;+ case 4: s.dpadDown = true; break;+ case 5: s.dpadDown = s.dpadLeft = true; break;+ case 6: s.dpadLeft = true; break;+ case 7: s.dpadUp = s.dpadLeft = true; break;+ default: break;+ }+ s.btnX = (b0 & 0x10) != 0; // Square+ s.btnA = (b0 & 0x20) != 0; // Cross+ s.btnB = (b0 & 0x40) != 0; // Circle+ s.btnY = (b0 & 0x80) != 0; // Triangle++ const uint8_t b1 = p[8];+ s.btnL1 = (b1 & 0x01) != 0;+ s.btnR1 = (b1 & 0x02) != 0;+ s.btnOptions = (b1 & 0x20) != 0; // Options+ s.btnL3 = (b1 & 0x40) != 0;+ s.btnR3 = (b1 & 0x80) != 0;++ const uint8_t b2 = p[9];+ s.btnNexus = (b2 & 0x01) != 0; // PS button+ s.btnTouchpad = ((b1 & 0x10) != 0) || ((b2 & 0x02) != 0); // Create/Share or Touchpad click++ if (effectiveLen >= 36) {+ if ((p[32] & 0x80) == 0) { // active touch 1+ s.touching = true;+ uint16_t tx = static_cast<uint16_t>(p[33] | ((p[34] & 0x0F) << 8));+ uint16_t ty = static_cast<uint16_t>((p[34] >> 4) | (p[35] << 4));+ s.touchX = std::clamp(static_cast<float>(tx) / 1919.0f, 0.0f, 1.0f);+ s.touchY = std::clamp(static_cast<float>(ty) / 1079.0f, 0.0f, 1.0f);+ s.touchSecond = (effectiveLen >= 40 && (p[36] & 0x80) == 0);+ } else {+ s.touching = false;+ s.touchSecond = false;+ }+ }++ s.connected = true;+ return true;+}++bool parseSwitchProReport(const uint8_t* data, size_t len, ControllerState& s) {+ if (!data || len < 11) return false;+ // Full reports (0x30, 0x21, 0x31)+ if (data[0] == 0x30 || data[0] == 0x21 || data[0] == 0x31) {+ if (len < 12) return false;+ const uint8_t btnRight = data[3];+ const uint8_t btnShared = data[4];+ const uint8_t btnLeft = data[5];++ s.btnX = (btnRight & 0x01) != 0; // Y -> Square+ s.btnY = (btnRight & 0x02) != 0; // X -> Triangle+ s.btnA = (btnRight & 0x04) != 0; // B -> Cross+ s.btnB = (btnRight & 0x08) != 0; // A -> Circle+ s.btnR1 = (btnRight & 0x40) != 0;+ s.triggerR2 = (btnRight & 0x80) ? 1.0f : 0.0f;++ s.btnOptions = (btnShared & 0x02) != 0; // Plus+ s.btnR3 = (btnShared & 0x04) != 0;+ s.btnL3 = (btnShared & 0x08) != 0;+ s.btnNexus = (btnShared & 0x10) != 0; // Home+ s.btnTouchpad = ((btnShared & 0x01) != 0) || ((btnShared & 0x20) != 0); // Minus or Capture++ s.dpadDown = (btnLeft & 0x01) != 0;+ s.dpadUp = (btnLeft & 0x02) != 0;+ s.dpadRight = (btnLeft & 0x04) != 0;+ s.dpadLeft = (btnLeft & 0x08) != 0;+ s.btnL1 = (btnLeft & 0x40) != 0;+ s.triggerL2 = (btnLeft & 0x80) ? 1.0f : 0.0f;++ uint16_t lx12 = static_cast<uint16_t>(data[6] | ((data[7] & 0x0F) << 8));+ uint16_t ly12 = static_cast<uint16_t>((data[7] >> 4) | (data[8] << 4));+ uint16_t rx12 = static_cast<uint16_t>(data[9] | ((data[10] & 0x0F) << 8));+ uint16_t ry12 = static_cast<uint16_t>((data[10] >> 4) | (data[11] << 4));++ auto normStick = [](uint16_t raw) {+ return std::clamp((static_cast<float>(raw) - 2048.0f) / 2047.0f * 1.42f, -1.0f, 1.0f);+ };+ s.rawLeftX = s.leftStickX = normStick(lx12);+ // Switch Pro native Y axis: 0 is top (-1.0f in ControllerState), 4095 is bottom (+1.0f)+ s.rawLeftY = s.leftStickY = normStick(ly12);+ s.rawRightX = s.rightStickX = normStick(rx12);+ s.rawRightY = s.rightStickY = normStick(ry12);++ s.connected = true;+ return true;+ }+ // Simple reports (0x3F)+ if (data[0] == 0x3F && len >= 11) {+ const uint8_t b0 = data[1];+ const uint8_t b1 = data[2];+ const uint8_t hat = data[3];++ s.btnX = (b0 & 0x01) != 0; // Y -> Square+ s.btnA = (b0 & 0x02) != 0; // B -> Cross+ s.btnB = (b0 & 0x04) != 0; // A -> Circle+ s.btnY = (b0 & 0x08) != 0; // X -> Triangle+ s.btnL1 = (b0 & 0x10) != 0;+ s.btnR1 = (b0 & 0x20) != 0;+ s.triggerL2 = (b0 & 0x40) ? 1.0f : 0.0f;+ s.triggerR2 = (b0 & 0x80) ? 1.0f : 0.0f;++ s.btnOptions = (b1 & 0x02) != 0; // Plus+ s.btnL3 = (b1 & 0x04) != 0;+ s.btnR3 = (b1 & 0x08) != 0;+ s.btnNexus = (b1 & 0x10) != 0; // Home+ s.btnTouchpad = ((b1 & 0x01) != 0) || ((b1 & 0x20) != 0); // Minus or Capture++ s.dpadUp = s.dpadDown = s.dpadLeft = s.dpadRight = false;+ switch (hat) {+ case 0: s.dpadUp = true; break;+ case 1: s.dpadUp = s.dpadRight = true; break;+ case 2: s.dpadRight = true; break;+ case 3: s.dpadDown = s.dpadRight = true; break;+ case 4: s.dpadDown = true; break;+ case 5: s.dpadDown = s.dpadLeft = true; break;+ case 6: s.dpadLeft = true; break;+ case 7: s.dpadUp = s.dpadLeft = true; break;+ default: break;+ }++ s.rawLeftX = s.leftStickX = (static_cast<float>(data[5]) - 128.0f) / 127.5f;+ s.rawLeftY = s.leftStickY = (static_cast<float>(data[7]) - 128.0f) / 127.5f;+ s.rawRightX = s.rightStickX = (static_cast<float>(data[9]) - 128.0f) / 127.5f;+ s.rawRightY = s.rightStickY = (static_cast<float>(data[11]) - 128.0f) / 127.5f;++ s.connected = true;+ return true;+ }+ return false;+}++bool parseSwitchInputOnlyReport(const uint8_t* data, size_t len, ControllerState& s) {+ if (!data || len < 7) return false;+ const uint8_t b0 = data[0];+ const uint8_t b1 = data[1];+ const uint8_t hat = data[2];++ s.btnX = (b0 & 0x01) != 0; // Y -> Square+ s.btnA = (b0 & 0x02) != 0; // B -> Cross+ s.btnB = (b0 & 0x04) != 0; // A -> Circle+ s.btnY = (b0 & 0x08) != 0; // X -> Triangle+ s.btnL1 = (b0 & 0x10) != 0;+ s.btnR1 = (b0 & 0x20) != 0;+ s.triggerL2 = (b0 & 0x40) ? 1.0f : 0.0f;+ s.triggerR2 = (b0 & 0x80) ? 1.0f : 0.0f;++ s.btnOptions = (b1 & 0x02) != 0; // Plus+ s.btnL3 = (b1 & 0x04) != 0;+ s.btnR3 = (b1 & 0x08) != 0;+ s.btnNexus = (b1 & 0x10) != 0; // Home+ s.btnTouchpad = ((b1 & 0x01) != 0) || ((b1 & 0x20) != 0); // Minus or Capture++ s.dpadUp = s.dpadDown = s.dpadLeft = s.dpadRight = false;+ switch (hat) {+ case 0: s.dpadUp = true; break;+ case 1: s.dpadUp = s.dpadRight = true; break;+ case 2: s.dpadRight = true; break;+ case 3: s.dpadDown = s.dpadRight = true; break;+ case 4: s.dpadDown = true; break;+ case 5: s.dpadDown = s.dpadLeft = true; break;+ case 6: s.dpadLeft = true; break;+ case 7: s.dpadUp = s.dpadLeft = true; break;+ default: break;+ }++ s.rawLeftX = s.leftStickX = (static_cast<float>(data[3]) - 128.0f) / 127.5f;+ s.rawLeftY = s.leftStickY = (static_cast<float>(data[4]) - 128.0f) / 127.5f;+ s.rawRightX = s.rightStickX = (static_cast<float>(data[5]) - 128.0f) / 127.5f;+ s.rawRightY = s.rightStickY = (static_cast<float>(data[6]) - 128.0f) / 127.5f;++ s.connected = true;+ return true;+}++bool parseStadiaReport(const uint8_t* data, size_t len, ControllerState& s) {+ if (!data || len < 9) return false;+ const uint8_t* p = (data[0] == 0x03 && len >= 10) ? data + 1 : data;+ const size_t effectiveLen = (data[0] == 0x03 && len >= 10) ? (len - 1) : len;+ if (effectiveLen < 9) return false;++ const uint8_t hat = p[0];+ s.dpadUp = s.dpadDown = s.dpadLeft = s.dpadRight = false;+ switch (hat) {+ case 0: s.dpadUp = true; break;+ case 1: s.dpadUp = s.dpadRight = true; break;+ case 2: s.dpadRight = true; break;+ case 3: s.dpadDown = s.dpadRight = true; break;+ case 4: s.dpadDown = true; break;+ case 5: s.dpadDown = s.dpadLeft = true; break;+ case 6: s.dpadLeft = true; break;+ case 7: s.dpadUp = s.dpadLeft = true; break;+ default: break;+ }++ const uint8_t bMain = p[1];+ s.btnNexus = (bMain & 0x10) != 0; // Stadia button+ s.btnOptions = (bMain & 0x20) != 0; // Menu+ s.btnTouchpad = ((bMain & 0x03) != 0) || ((bMain & 0x40) != 0); // Capture / Assistant or Options+ s.btnR3 = (bMain & 0x80) != 0;++ const uint8_t bExtra = p[2];+ s.btnL3 = (bExtra & 0x01) != 0;+ s.btnR1 = (bExtra & 0x02) != 0;+ s.btnL1 = (bExtra & 0x04) != 0;+ s.btnY = (bExtra & 0x08) != 0;+ s.btnX = (bExtra & 0x10) != 0;+ s.btnB = (bExtra & 0x20) != 0;+ s.btnA = (bExtra & 0x40) != 0;++ s.rawLeftX = s.leftStickX = (static_cast<float>(p[3]) - 128.0f) / 127.5f;+ s.rawLeftY = s.leftStickY = (static_cast<float>(p[4]) - 128.0f) / 127.5f;+ s.rawRightX = s.rightStickX = (static_cast<float>(p[5]) - 128.0f) / 127.5f;+ s.rawRightY = s.rightStickY = (static_cast<float>(p[6]) - 128.0f) / 127.5f;++ s.triggerL2 = static_cast<float>(p[7]) / 255.0f;+ s.triggerR2 = static_cast<float>(p[8]) / 255.0f;+ if (bMain & 0x04) s.triggerL2 = std::max(s.triggerL2, 1.0f);+ if (bMain & 0x08) s.triggerR2 = std::max(s.triggerR2, 1.0f);++ s.connected = true;+ return true;+}+bool parseUsbPadReport(UsbPadProtocol protocol, const uint8_t* data, size_t len, ControllerState& state) {switch (protocol) {case UsbPadProtocol::XInput: return parseXInputReport(data, len, state);case UsbPadProtocol::Gip: return parseGipReport(data, len, state);+ case UsbPadProtocol::DualShock3: return parseDualShock3Report(data, len, state);+ case UsbPadProtocol::DualShock4: return parseDualShock4Report(data, len, state);+ case UsbPadProtocol::DualSense: return parseDualSenseReport(data, len, state);+ case UsbPadProtocol::SwitchPro: return parseSwitchProReport(data, len, state);+ case UsbPadProtocol::SwitchInputOnly: return parseSwitchInputOnlyReport(data, len, state);+ case UsbPadProtocol::Stadia: return parseStadiaReport(data, len, state);+ case UsbPadProtocol::GenericHid: return parseGenericHidFallback(data, len, state);case UsbPadProtocol::None: break;}return false;diff --git a/src/input/pad_usb.h b/src/input/pad_usb.hindex 72f76cd..1a3f35a 100644--- a/src/input/pad_usb.h+++ b/src/input/pad_usb.h@@ -25,7 +25,19 @@ namespace xc::input {// XInput: subclass 0x5D, protocol 0x01 (Xbox 360 and its clones: 8BitDo,// GameSir, Machenike... in their PC mode);// GIP: subclass 0x47, protocol 0xD0 (Xbox One and Series pads).-enum class UsbPadProtocol { None, XInput, Gip };+enum class UsbPadProtocol {+ None,+ XInput,+ Gip,+ DualShock3,+ DualShock4,+ DualSense,+ SwitchPro,+ SwitchInputOnly,+ Stadia,+ GenericHid+};+const char* usbPadProtocolName(UsbPadProtocol protocol);UsbPadProtocol usbPadProtocolFor(uint8_t ifaceClass, uint8_t ifaceSubclass, uint8_t ifaceProtocol);// The pad's interface in a configuration descriptor (as USB_GET_FULL_DESC@@ -49,6 +61,12 @@ std::vector<std::array<uint8_t, 13>> gipAcksFor(const uint8_t* data, size_t len)// rumble and status reports, GIP's announce / keep-alive...).bool parseXInputReport(const uint8_t* data, size_t len, ControllerState& state);bool parseGipReport(const uint8_t* data, size_t len, ControllerState& state);+bool parseDualShock3Report(const uint8_t* data, size_t len, ControllerState& state);+bool parseDualShock4Report(const uint8_t* data, size_t len, ControllerState& state);+bool parseDualSenseReport(const uint8_t* data, size_t len, ControllerState& state);+bool parseSwitchProReport(const uint8_t* data, size_t len, ControllerState& state);+bool parseSwitchInputOnlyReport(const uint8_t* data, size_t len, ControllerState& state);+bool parseStadiaReport(const uint8_t* data, size_t len, ControllerState& state);bool parseUsbPadReport(UsbPadProtocol protocol, const uint8_t* data, size_t len, ControllerState& state);// The transport tells the shared code about each pad (slot 0..kMaxUsbPads-1).diff --git a/src/input/pad_usb_hid.cpp b/src/input/pad_usb_hid.cppnew file mode 100644index 0000000..a16da59--- /dev/null+++ b/src/input/pad_usb_hid.cpp@@ -0,0 +1,473 @@+// SPDX-License-Identifier: GPL-3.0-or-later+// Generic USB HID gamepad parser and descriptor interpreter.+// Ported from OneFamily (ChiricoG/OneFamily, MIT) and AnyPad-PS4 (Vld439 / elmonomalvad0).+#include "input/pad_usb_hid.h"++#include <algorithm>+#include <cstring>++namespace xc::input {++namespace {++struct HidGlobals {+ uint16_t page = 0;+ int32_t lmin = 0;+ int32_t lmax = 0;+ uint32_t size = 0;+ uint32_t count = 0;+ uint8_t rid = 0;+};++int32_t itemValue(const uint8_t* p, int size, bool isSigned) {+ uint32_t v = 0;+ for (int i = 0; i < size; ++i) v |= static_cast<uint32_t>(p[i]) << (8 * i);+ if (isSigned && size > 0 && size < 4 && (v & (1u << (8 * size - 1)))) {+ v |= ~0u << (8 * size);+ }+ return static_cast<int32_t>(v);+}++void putField(GenericHidContext& g, uint32_t usage, uint16_t page, const HidGlobals& gl, uint16_t off) {+ HidField f;+ uint16_t pg = (usage >> 16) ? static_cast<uint16_t>(usage >> 16) : page;+ uint16_t u = static_cast<uint16_t>(usage);+ HidField* slot = nullptr;++ f.present = 1;+ f.rid = gl.rid;+ f.off = off;+ f.size = static_cast<uint16_t>(gl.size);+ f.min = gl.lmin;+ f.max = gl.lmax;++ if (pg == 0x01 && u >= 0x30 && u <= 0x37) {+ slot = &g.axis[AX_X + (u - 0x30)];+ } else if (pg == 0x01 && u == 0x39) {+ slot = &g.axis[AX_HAT];+ } else if (pg == 0x02 && u == 0xC5) {+ slot = &g.axis[AX_BRAKE];+ } else if (pg == 0x02 && u == 0xC4) {+ slot = &g.axis[AX_ACCEL];+ } else if (pg == 0x09 && u >= 1 && u <= kMaxHidButtons) {+ slot = &g.button[u - 1];+ if (static_cast<int>(u) > g.nbuttons) g.nbuttons = static_cast<int>(u);+ } else if (pg == 0x0C && u == 0x223) {+ slot = &g.home;+ } else if (pg == 0x0C && u == 0x224) {+ slot = &g.back;+ }++ if (slot && !slot->present) *slot = f;+}++uint32_t getBits(const uint8_t* d, int len, const HidField* f) {+ if (!f || !f->present || f->size == 0) return 0;+ int bitOff = f->off;+ int byteOff = bitOff / 8;+ int bitInByte = bitOff % 8;+ if (byteOff >= len) return 0;+ uint32_t val = 0;+ int bitsLeft = f->size;+ int destBit = 0;+ while (bitsLeft > 0 && byteOff < len) {+ int take = 8 - bitInByte;+ if (take > bitsLeft) take = bitsLeft;+ uint32_t chunk = (d[byteOff] >> bitInByte) & ((1u << take) - 1);+ val |= chunk << destBit;+ destBit += take;+ bitsLeft -= take;+ byteOff++;+ bitInByte = 0;+ }+ return val;+}++uint8_t toByte(const HidField* f, uint32_t raw) {+ if (!f || f->max <= f->min) return 128;+ int64_t val = static_cast<int64_t>(raw);+ if (f->min < 0 && f->size <= 32) {+ if (raw & (1u << (f->size - 1))) {+ val = static_cast<int64_t>(raw) - (1ULL << f->size);+ }+ }+ if (val < f->min) val = f->min;+ if (val > f->max) val = f->max;+ int64_t scaled = ((val - f->min) * 255) / (f->max - f->min);+ return static_cast<uint8_t>(std::clamp<int64_t>(scaled, 0, 255));+}++void applyBuiltinMapping(GenericHidContext& g, uint16_t vid, uint16_t pid) {+ // Default button mapping+ static const uint32_t kDefaultMap[14] = {+ HID_BTN_SQUARE, HID_BTN_CROSS, HID_BTN_CIRCLE, HID_BTN_TRIANGLE,+ HID_BTN_L1, HID_BTN_R1, HID_BTN_L2, HID_BTN_R2,+ HID_BTN_SHARE, HID_BTN_OPTIONS, HID_BTN_L3, HID_BTN_R3,+ HID_BTN_PS, HID_BTN_TOUCHPAD+ };+ for (int i = 0; i < kMaxHidButtons; ++i) {+ g.buttonMap[i] = (i < 14) ? kDefaultMap[i] : 0;+ }++ g.role[ROLE_LX] = AX_X;+ g.role[ROLE_LY] = AX_Y;++ if (g.axis[AX_RX].present && g.axis[AX_RY].present) {+ g.role[ROLE_RX] = AX_RX;+ g.role[ROLE_RY] = AX_RY;+ if (g.axis[AX_Z].present && !g.axis[AX_BRAKE].present) g.role[ROLE_L2] = AX_Z;+ if (g.axis[AX_RZ].present && !g.axis[AX_ACCEL].present) g.role[ROLE_R2] = AX_RZ;+ } else if (g.axis[AX_Z].present && g.axis[AX_RZ].present) {+ g.role[ROLE_RX] = AX_Z;+ g.role[ROLE_RY] = AX_RZ;+ } else {+ g.role[ROLE_RX] = g.axis[AX_RX].present ? AX_RX : (g.axis[AX_Z].present ? AX_Z : AX_NONE);+ g.role[ROLE_RY] = g.axis[AX_RY].present ? AX_RY : (g.axis[AX_RZ].present ? AX_RZ : AX_NONE);+ }+ if (g.axis[AX_BRAKE].present) g.role[ROLE_L2] = AX_BRAKE;+ if (g.axis[AX_ACCEL].present) g.role[ROLE_R2] = AX_ACCEL;++ for (int i = 0; i < ROLE_N; ++i) g.invert[i] = 0;++ // Logitech DirectInput controllers (F310, F710, RumblePad 2 in D-mode)+ if (vid == 0x046D && (pid == 0xC216 || pid == 0xC219 || pid == 0xC218)) {+ g.buttonMap[0] = HID_BTN_SQUARE;+ g.buttonMap[1] = HID_BTN_CROSS;+ g.buttonMap[2] = HID_BTN_CIRCLE;+ g.buttonMap[3] = HID_BTN_TRIANGLE;+ g.buttonMap[4] = HID_BTN_L1;+ g.buttonMap[5] = HID_BTN_R1;+ g.buttonMap[6] = HID_BTN_L2;+ g.buttonMap[7] = HID_BTN_R2;+ g.buttonMap[8] = HID_BTN_SHARE;+ g.buttonMap[9] = HID_BTN_OPTIONS;+ g.buttonMap[10] = HID_BTN_L3;+ g.buttonMap[11] = HID_BTN_R3;+ g.role[ROLE_RX] = AX_Z;+ g.role[ROLE_RY] = AX_RZ;+ return;+ }++ // ShanWan 2.4G wireless dongles / BSP-G6+ if (vid == 0x2563 && (pid == 0x0119 || pid == 0x0523 || pid == 0x0575)) {+ g.buttonMap[0] = HID_BTN_SQUARE;+ g.buttonMap[1] = HID_BTN_CROSS;+ g.buttonMap[2] = HID_BTN_CIRCLE;+ g.buttonMap[3] = HID_BTN_TRIANGLE;+ g.buttonMap[4] = HID_BTN_L1;+ g.buttonMap[5] = HID_BTN_R1;+ g.buttonMap[6] = HID_BTN_L2;+ g.buttonMap[7] = HID_BTN_R2;+ g.buttonMap[8] = HID_BTN_SHARE;+ g.buttonMap[9] = HID_BTN_OPTIONS;+ g.buttonMap[10] = HID_BTN_L3;+ g.buttonMap[11] = HID_BTN_R3;+ g.role[ROLE_RX] = AX_Z;+ g.role[ROLE_RY] = AX_RZ;+ return;+ }++ // 8BitDo DirectInput / Android controllers (SN30 Pro, Ultimate 2.4G, etc.)+ if (vid == 0x2DC8 && (pid == 0x2100 || pid == 0x2101 || pid == 0x3012 || pid == 0x310B || pid == 0x310A || pid == 0x6012)) {+ g.buttonMap[0] = HID_BTN_CROSS;+ g.buttonMap[1] = HID_BTN_CIRCLE;+ g.buttonMap[2] = 0;+ g.buttonMap[3] = HID_BTN_SQUARE;+ g.buttonMap[4] = HID_BTN_TRIANGLE;+ g.buttonMap[5] = 0;+ g.buttonMap[6] = HID_BTN_L1;+ g.buttonMap[7] = HID_BTN_R1;+ g.buttonMap[8] = HID_BTN_L2;+ g.buttonMap[9] = HID_BTN_R2;+ g.buttonMap[10] = HID_BTN_SHARE;+ g.buttonMap[11] = HID_BTN_OPTIONS;+ g.buttonMap[12] = HID_BTN_PS;+ g.buttonMap[13] = HID_BTN_L3;+ g.buttonMap[14] = HID_BTN_R3;+ return;+ }++ // Generic Android layout (GameSir, Betop, Xiaomi, Razer Kishi, etc.)+ if ((vid == 0xFFFF && (pid == 0x046E || pid == 0x046F || pid == 0x0450)) ||+ (vid == 0x05AC && (pid == 0x022D || pid == 0x056B)) ||+ (vid == 0x2717 && pid == 0x3144) ||+ (vid == 0x1532 && pid == 0x0900)) {+ g.buttonMap[0] = HID_BTN_CROSS;+ g.buttonMap[1] = HID_BTN_CIRCLE;+ g.buttonMap[2] = 0;+ g.buttonMap[3] = HID_BTN_SQUARE;+ g.buttonMap[4] = HID_BTN_TRIANGLE;+ g.buttonMap[5] = 0;+ g.buttonMap[6] = HID_BTN_L1;+ g.buttonMap[7] = HID_BTN_R1;+ g.buttonMap[8] = HID_BTN_L2;+ g.buttonMap[9] = HID_BTN_R2;+ g.buttonMap[10] = HID_BTN_SHARE;+ g.buttonMap[11] = HID_BTN_OPTIONS;+ g.buttonMap[12] = HID_BTN_PS;+ g.buttonMap[13] = HID_BTN_L3;+ g.buttonMap[14] = HID_BTN_R3;+ return;+ }+}++} // namespace++bool parseHidDescriptor(GenericHidContext& g, const uint8_t* d, size_t n, uint16_t vid, uint16_t pid) {+ if (!d || n == 0) return false;+ g = GenericHidContext{};++ HidGlobals gl{};+ HidGlobals stack[4]{};+ int depth = 0;+ size_t i = 0;+ uint32_t usages[64]{};+ uint32_t umin = 0, umax = 0;+ int nusages = 0, hasRange = 0;+ uint16_t offset[256]{};+ uint16_t outOffset[256]{};++ while (i < n) {+ uint8_t p = d[i];+ int size = p & 0x03;+ if (size == 3) size = 4;+ int type = (p >> 2) & 0x03;+ int tag = (p >> 4) & 0x0F;+ const uint8_t* v = d + i + 1;+ if (i + 1 + static_cast<size_t>(size) > n) break;+ i += 1 + size;++ if (type == 0) { // Main+ switch (tag) {+ case 8: { // Input+ uint16_t* off = &offset[gl.rid];+ if (gl.rid) *off = std::max<uint16_t>(*off, 8);+ for (uint32_t k = 0; k < gl.count; ++k) {+ uint32_t u = 0;+ if (hasRange) {+ u = (umin + k <= umax) ? (umin + k) : umax;+ } else if (k < static_cast<uint32_t>(nusages)) {+ u = usages[k];+ } else if (nusages > 0) {+ u = usages[nusages - 1];+ }+ putField(g, u, gl.page, gl, *off);+ *off += static_cast<uint16_t>(gl.size);+ }+ nusages = 0;+ hasRange = 0;+ break;+ }+ case 9: { // Output+ uint16_t* off = &outOffset[gl.rid];+ if (gl.rid) *off = std::max<uint16_t>(*off, 8);+ uint16_t total = static_cast<uint16_t>(gl.size * gl.count);+ if (!g.outPresent && total >= 8) {+ g.outPresent = 1;+ g.outHasIds = gl.rid ? 1 : 0;+ g.outRid = gl.rid;+ g.outDataBitOff = *off;+ g.outBits = total;+ g.outFieldSize = static_cast<uint8_t>(gl.size);+ }+ *off += total;+ nusages = 0;+ hasRange = 0;+ break;+ }+ case 10: // Collection+ case 12: // End Collection+ nusages = 0;+ hasRange = 0;+ break;+ default:+ nusages = 0;+ hasRange = 0;+ break;+ }+ } else if (type == 1) { // Global+ switch (tag) {+ case 0: gl.page = static_cast<uint16_t>(itemValue(v, size, false)); break;+ case 1: gl.lmin = itemValue(v, size, true); break;+ case 2: gl.lmax = itemValue(v, size, true); break;+ case 7: gl.size = static_cast<uint32_t>(itemValue(v, size, false)); break;+ case 8:+ gl.rid = static_cast<uint8_t>(itemValue(v, size, false));+ g.hasIds = 1;+ break;+ case 9: gl.count = static_cast<uint32_t>(itemValue(v, size, false)); break;+ case 10:+ if (depth < 4) stack[depth++] = gl;+ break;+ case 11:+ if (depth > 0) gl = stack[--depth];+ break;+ }+ } else if (type == 2) { // Local+ switch (tag) {+ case 0:+ if (nusages < 64) usages[nusages++] = static_cast<uint32_t>(itemValue(v, size, false));+ break;+ case 1: umin = static_cast<uint32_t>(itemValue(v, size, false)); hasRange = 1; break;+ case 2: umax = static_cast<uint32_t>(itemValue(v, size, false)); hasRange = 1; break;+ }+ }+ }++ applyBuiltinMapping(g, vid, pid);+ return true;+}++bool parseGenericHidReport(const GenericHidContext& ctx, const uint8_t* rx, size_t len, ControllerState& s) {+ if (!rx || len < 4) return false;++ uint8_t rid = ctx.hasIds ? rx[0] : 0;+ const uint8_t* d = rx;++ uint8_t axes[ROLE_N] = {128, 128, 128, 128, 0, 0};+ uint32_t b = 0;+ bool used = false;+ bool trig[2] = {false, false};++ // Update axes+ for (int i = 0; i < ROLE_N; ++i) {+ if (ctx.role[i] == AX_NONE) continue;+ const HidField* f = &ctx.axis[ctx.role[i]];+ if (!f->present || f->rid != rid) continue;+ uint8_t v = toByte(f, getBits(d, static_cast<int>(len), f));+ axes[i] = ctx.invert[i] ? static_cast<uint8_t>(255 - v) : v;+ if (i == ROLE_L2) trig[0] = true;+ if (i == ROLE_R2) trig[1] = true;+ used = true;+ }++ // Update digital buttons+ for (int i = 0; i < ctx.nbuttons && i < kMaxHidButtons; ++i) {+ const HidField* f = &ctx.button[i];+ if (!f->present || f->rid != rid) continue;+ if (getBits(d, static_cast<int>(len), f)) {+ b |= ctx.buttonMap[i];+ }+ used = true;+ }++ // Update Hat D-Pad+ if (ctx.axis[AX_HAT].present && ctx.axis[AX_HAT].rid == rid) {+ const HidField* f = &ctx.axis[AX_HAT];+ int32_t h = static_cast<int32_t>(getBits(d, static_cast<int>(len), f)) - f->min;+ s.dpadUp = s.dpadDown = s.dpadLeft = s.dpadRight = false;+ if (h >= 0 && h < 8) {+ switch (h) {+ case 0: s.dpadUp = true; break;+ case 1: s.dpadUp = s.dpadRight = true; break;+ case 2: s.dpadRight = true; break;+ case 3: s.dpadDown = s.dpadRight = true; break;+ case 4: s.dpadDown = true; break;+ case 5: s.dpadDown = s.dpadLeft = true; break;+ case 6: s.dpadLeft = true; break;+ case 7: s.dpadUp = s.dpadLeft = true; break;+ }+ }+ used = true;+ }++ // Home / Back+ if (ctx.home.present && ctx.home.rid == rid) {+ if (getBits(d, static_cast<int>(len), &ctx.home)) b |= HID_BTN_PS;+ used = true;+ }+ if (ctx.back.present && ctx.back.rid == rid) {+ if (getBits(d, static_cast<int>(len), &ctx.back)) b |= HID_BTN_SHARE;+ used = true;+ }++ if (!used) {+ return false;+ }++ s.rawLeftX = s.leftStickX = (static_cast<float>(axes[ROLE_LX]) - 128.0f) / 127.5f;+ s.rawLeftY = s.leftStickY = (static_cast<float>(axes[ROLE_LY]) - 128.0f) / 127.5f;+ s.rawRightX = s.rightStickX = (static_cast<float>(axes[ROLE_RX]) - 128.0f) / 127.5f;+ s.rawRightY = s.rightStickY = (static_cast<float>(axes[ROLE_RY]) - 128.0f) / 127.5f;++ s.triggerL2 = trig[0] ? (static_cast<float>(axes[ROLE_L2]) / 255.0f) : ((b & HID_BTN_L2) ? 1.0f : 0.0f);+ s.triggerR2 = trig[1] ? (static_cast<float>(axes[ROLE_R2]) / 255.0f) : ((b & HID_BTN_R2) ? 1.0f : 0.0f);++ s.btnX = (b & HID_BTN_SQUARE) != 0;+ s.btnA = (b & HID_BTN_CROSS) != 0;+ s.btnB = (b & HID_BTN_CIRCLE) != 0;+ s.btnY = (b & HID_BTN_TRIANGLE) != 0;+ s.btnL1 = (b & HID_BTN_L1) != 0;+ s.btnR1 = (b & HID_BTN_R1) != 0;+ s.btnTouchpad = (b & (HID_BTN_SHARE | HID_BTN_TOUCHPAD)) != 0;+ s.btnOptions = (b & HID_BTN_OPTIONS) != 0;+ s.btnL3 = (b & HID_BTN_L3) != 0;+ s.btnR3 = (b & HID_BTN_R3) != 0;+ s.btnNexus = (b & HID_BTN_PS) != 0;++ // OneFamily combo for 12-button controllers without a physical Guide/PS button:+ // Back/Select/Share + Start/Options -> Home / Nexus+ if (!s.btnNexus && s.btnTouchpad && s.btnOptions) {+ s.btnNexus = true;+ }++ s.connected = true;+ return true;+}++bool parseGenericHidFallback(const uint8_t* rx, size_t len, ControllerState& s) {+ if (!rx || len < 5) return false;+ size_t off = 0;+ if (rx[0] <= 4 && len >= 6 && rx[1] >= 80 && rx[1] <= 180) {+ off = 1; // Report ID+ }+ if (off + 4 > len) return false;++ s.rawLeftX = s.leftStickX = (static_cast<float>(rx[off + 0]) - 128.0f) / 127.5f;+ s.rawLeftY = s.leftStickY = (static_cast<float>(rx[off + 1]) - 128.0f) / 127.5f;+ s.rawRightX = s.rightStickX = (static_cast<float>(rx[off + 2]) - 128.0f) / 127.5f;+ s.rawRightY = s.rightStickY = (static_cast<float>(rx[off + 3]) - 128.0f) / 127.5f;++ if (off + 5 <= len) {+ uint8_t hat = rx[off + 4] & 0x0F;+ s.dpadUp = s.dpadDown = s.dpadLeft = s.dpadRight = false;+ if (hat < 8) {+ switch (hat) {+ case 0: s.dpadUp = true; break;+ case 1: s.dpadUp = s.dpadRight = true; break;+ case 2: s.dpadRight = true; break;+ case 3: s.dpadDown = s.dpadRight = true; break;+ case 4: s.dpadDown = true; break;+ case 5: s.dpadDown = s.dpadLeft = true; break;+ case 6: s.dpadLeft = true; break;+ case 7: s.dpadUp = s.dpadLeft = true; break;+ }+ }+ }++ if (off + 7 <= len) {+ uint16_t btns = static_cast<uint16_t>(rx[off + 5] | (rx[off + 6] << 8));+ s.btnX = (btns & 0x0001) != 0;+ s.btnA = (btns & 0x0002) != 0;+ s.btnB = (btns & 0x0004) != 0;+ s.btnY = (btns & 0x0008) != 0;+ s.btnL1 = (btns & 0x0010) != 0;+ s.btnR1 = (btns & 0x0020) != 0;+ if (btns & 0x0040) s.triggerL2 = 1.0f;+ if (btns & 0x0080) s.triggerR2 = 1.0f;+ s.btnTouchpad = (btns & 0x0100) != 0;+ s.btnOptions = (btns & 0x0200) != 0;+ s.btnL3 = (btns & 0x0400) != 0;+ s.btnR3 = (btns & 0x0800) != 0;+ s.btnNexus = (btns & 0x1000) != 0;++ if (!s.btnNexus && s.btnTouchpad && s.btnOptions) {+ s.btnNexus = true;+ }+ }++ s.connected = true;+ return true;+}++} // namespace xc::inputdiff --git a/src/input/pad_usb_hid.h b/src/input/pad_usb_hid.hnew file mode 100644index 0000000..549f760--- /dev/null+++ b/src/input/pad_usb_hid.h@@ -0,0 +1,96 @@+// SPDX-License-Identifier: GPL-3.0-or-later+// Generic USB HID gamepad parser and descriptor interpreter.+// Ported from OneFamily (ChiricoG/OneFamily, MIT) and AnyPad-PS4 (Vld439 / elmonomalvad0).+#pragma once++#include "input/controller.h"++#include <cstddef>+#include <cstdint>++namespace xc::input {++enum {+ AX_X = 0,+ AX_Y,+ AX_Z,+ AX_RX,+ AX_RY,+ AX_RZ,+ AX_SLIDER,+ AX_DIAL,+ AX_BRAKE,+ AX_ACCEL,+ AX_HAT,+ AX_N,+ AX_NONE = 0xFF+};++enum {+ ROLE_LX = 0,+ ROLE_LY,+ ROLE_RX,+ ROLE_RY,+ ROLE_L2,+ ROLE_R2,+ ROLE_N+};++constexpr int kMaxHidButtons = 32;++enum {+ HID_BTN_SQUARE = 0x0001,+ HID_BTN_CROSS = 0x0002,+ HID_BTN_CIRCLE = 0x0004,+ HID_BTN_TRIANGLE = 0x0008,+ HID_BTN_L1 = 0x0010,+ HID_BTN_R1 = 0x0020,+ HID_BTN_L2 = 0x0040,+ HID_BTN_R2 = 0x0080,+ HID_BTN_SHARE = 0x0100,+ HID_BTN_OPTIONS = 0x0200,+ HID_BTN_L3 = 0x0400,+ HID_BTN_R3 = 0x0800,+ HID_BTN_PS = 0x1000,+ HID_BTN_TOUCHPAD = 0x2000+};++struct HidField {+ uint8_t present = 0;+ uint8_t rid = 0;+ uint16_t off = 0;+ uint16_t size = 0;+ int32_t min = 0;+ int32_t max = 0;+};++struct GenericHidContext {+ int hasIds = 0;+ HidField axis[AX_N]{};+ HidField button[kMaxHidButtons]{};+ HidField home{};+ HidField back{};+ int nbuttons = 0;++ uint8_t role[ROLE_N]{};+ uint8_t invert[ROLE_N]{};+ uint32_t buttonMap[kMaxHidButtons]{};++ uint8_t outPresent = 0;+ uint8_t outHasIds = 0;+ uint8_t outRid = 0;+ uint16_t outBits = 0;+ uint16_t outDataBitOff = 0;+ uint8_t outFieldSize = 0;+};++// Parses a raw USB HID Report Descriptor and configures the context with standard/builtin mappings.+bool parseHidDescriptor(GenericHidContext& ctx, const uint8_t* desc, size_t len, uint16_t vid, uint16_t pid);++// Decodes an incoming HID interrupt report into ControllerState.+bool parseGenericHidReport(const GenericHidContext& ctx, const uint8_t* data, size_t len, ControllerState& state);++// Fallback for unconfigured HID devices (interprets standard raw reports).+bool parseGenericHidFallback(const uint8_t* data, size_t len, ControllerState& state);++} // namespace xc::inputdiff --git a/src/input/pad_usb_ps4.cpp b/src/input/pad_usb_ps4.cppnew file mode 100644index 0000000..8dea54d--- /dev/null+++ b/src/input/pad_usb_ps4.cpp@@ -0,0 +1,817 @@+// SPDX-License-Identifier: GPL-3.0-or-later+// Copyright (C) 2026 RafaelNGP+// USB pads on the PS4 (pad_usb.h).+// Native in-app USB controller driver subsystem using libSceUsbd (Orbis OS userland USB).+// Ported and adapted from OneFamily (ChiricoG/OneFamily, MIT) and OmniPad CE.+#include "input/pad_usb.h"+#include "input/pad_usb_hid.h"+#include "util/log.h"++#include <stdint.h>+#include <cstring>+#include <algorithm>+#include <atomic>+#include <chrono>+#include <memory>+#include <mutex>+#include <set>+#include <string>+#include <thread>+#include <vector>++#include <orbis/Usbd.h>++extern "C" {+int sceKernelLoadStartModule(const char* path, size_t argc, const void* argv, uint32_t flags, void* opt, int* res);+int32_t sceKernelDlsym(int32_t handle, const char* symbol, void** address);+int sceKernelStopUnloadModule(int handle, size_t argc, const void* argv, uint32_t flags, void* opt, int* res);+}++namespace xc::input {++namespace {++// libSceUsbd dynamic function pointer types+typedef int32_t (*sceUsbdInit_t)(void);+typedef int32_t (*sceUsbdExit_t)(void);+typedef int32_t (*sceUsbdGetDeviceList_t)(libusb_device***);+typedef int32_t (*sceUsbdFreeDeviceList_t)(libusb_device**);+typedef int32_t (*sceUsbdGetDeviceDescriptor_t)(libusb_device*, struct libusb_device_descriptor*);+typedef int32_t (*sceUsbdGetActiveConfigDescriptor_t)(libusb_device*, struct libusb_config_descriptor**);+typedef int32_t (*sceUsbdGetConfigDescriptor_t)(libusb_device*, uint8_t, struct libusb_config_descriptor**);+typedef int32_t (*sceUsbdFreeConfigDescriptor_t)(struct libusb_config_descriptor*);+typedef int32_t (*sceUsbdOpen_t)(libusb_device*, libusb_device_handle**);+typedef void (*sceUsbdClose_t)(libusb_device_handle*);+typedef int32_t (*sceUsbdClaimInterface_t)(libusb_device_handle*, int32_t);+typedef int32_t (*sceUsbdReleaseInterface_t)(libusb_device_handle*, int32_t);+typedef int32_t (*sceUsbdSetInterfaceAltSetting_t)(libusb_device_handle*, int32_t, int32_t);+typedef int32_t (*sceUsbdInterruptTransfer_t)(libusb_device_handle*, unsigned char, unsigned char*, int32_t, int32_t*, uint32_t);+typedef int32_t (*sceUsbdControlTransfer_t)(libusb_device_handle*, uint8_t, uint8_t, uint16_t, uint16_t, unsigned char*, uint16_t, uint32_t);+typedef int32_t (*sceUsbdCheckConnected_t)(libusb_device_handle*);+typedef int32_t (*sceUsbdClearHalt_t)(libusb_device_handle*, unsigned char);+typedef void (*sceUsbdDetachKernelDriver_t)(libusb_device_handle*, int32_t);+typedef int32_t (*sceUsbdGetStringDescriptorAscii_t)(libusb_device_handle*, uint8_t, uint16_t, unsigned char*, int32_t);+typedef uint8_t (*sceUsbdGetBusNumber_t)(libusb_device*);+typedef uint8_t (*sceUsbdGetDeviceAddress_t)(libusb_device*);++static int g_hUsbd = 0;+static sceUsbdInit_t p_sceUsbdInit = nullptr;+static sceUsbdExit_t p_sceUsbdExit = nullptr;+static sceUsbdGetDeviceList_t p_sceUsbdGetDeviceList = nullptr;+static sceUsbdFreeDeviceList_t p_sceUsbdFreeDeviceList = nullptr;+static sceUsbdGetDeviceDescriptor_t p_sceUsbdGetDeviceDescriptor = nullptr;+static sceUsbdGetActiveConfigDescriptor_t p_sceUsbdGetActiveConfigDescriptor = nullptr;+static sceUsbdGetConfigDescriptor_t p_sceUsbdGetConfigDescriptor = nullptr;+static sceUsbdFreeConfigDescriptor_t p_sceUsbdFreeConfigDescriptor = nullptr;+static sceUsbdOpen_t p_sceUsbdOpen = nullptr;+static sceUsbdClose_t p_sceUsbdClose = nullptr;+static sceUsbdClaimInterface_t p_sceUsbdClaimInterface = nullptr;+static sceUsbdReleaseInterface_t p_sceUsbdReleaseInterface = nullptr;+static sceUsbdSetInterfaceAltSetting_t p_sceUsbdSetInterfaceAltSetting = nullptr;+static sceUsbdInterruptTransfer_t p_sceUsbdInterruptTransfer = nullptr;+static sceUsbdControlTransfer_t p_sceUsbdControlTransfer = nullptr;+static sceUsbdCheckConnected_t p_sceUsbdCheckConnected = nullptr;+static sceUsbdClearHalt_t p_sceUsbdClearHalt = nullptr;+static sceUsbdDetachKernelDriver_t p_sceUsbdDetachKernelDriver = nullptr;+static sceUsbdGetStringDescriptorAscii_t p_sceUsbdGetStringDescriptorAscii = nullptr;+static sceUsbdGetBusNumber_t p_sceUsbdGetBusNumber = nullptr;+static sceUsbdGetDeviceAddress_t p_sceUsbdGetDeviceAddress = nullptr;++bool loadUsbd() {+ if (g_hUsbd > 0) return true;+ int res = 0;+ int h = sceKernelLoadStartModule("/system/common/lib/libSceUsbd.sprx", 0, nullptr, 0, nullptr, &res);+ if (h < 0) {+ h = sceKernelLoadStartModule("libSceUsbd.sprx", 0, nullptr, 0, nullptr, &res);+ }+ if (h <= 0) {+ XC_LOGW("usb pad: failed to load libSceUsbd.sprx: 0x%08x", static_cast<unsigned>(h));+ return false;+ }+ g_hUsbd = h;++#define RESOLVE(fn) sceKernelDlsym(g_hUsbd, #fn, reinterpret_cast<void**>(&p_##fn))+ RESOLVE(sceUsbdInit);+ RESOLVE(sceUsbdExit);+ RESOLVE(sceUsbdGetDeviceList);+ RESOLVE(sceUsbdFreeDeviceList);+ RESOLVE(sceUsbdGetDeviceDescriptor);+ RESOLVE(sceUsbdGetActiveConfigDescriptor);+ RESOLVE(sceUsbdGetConfigDescriptor);+ RESOLVE(sceUsbdFreeConfigDescriptor);+ RESOLVE(sceUsbdOpen);+ RESOLVE(sceUsbdClose);+ RESOLVE(sceUsbdClaimInterface);+ RESOLVE(sceUsbdReleaseInterface);+ RESOLVE(sceUsbdSetInterfaceAltSetting);+ RESOLVE(sceUsbdInterruptTransfer);+ RESOLVE(sceUsbdControlTransfer);+ RESOLVE(sceUsbdCheckConnected);+ RESOLVE(sceUsbdClearHalt);+ RESOLVE(sceUsbdDetachKernelDriver);+ RESOLVE(sceUsbdGetStringDescriptorAscii);+ RESOLVE(sceUsbdGetBusNumber);+ RESOLVE(sceUsbdGetDeviceAddress);+#undef RESOLVE++ if (!p_sceUsbdInit || !p_sceUsbdGetDeviceList || !p_sceUsbdOpen || !p_sceUsbdInterruptTransfer) {+ XC_LOGE("usb pad: failed to resolve required libSceUsbd symbols");+ return false;+ }+ p_sceUsbdInit();+ XC_LOGI("usb pad: libSceUsbd loaded and initialized (handle 0x%08x)", g_hUsbd);+ return true;+}++int32_t getActiveConfigDescriptor(libusb_device* dev, struct libusb_config_descriptor** config) {+ if (p_sceUsbdGetActiveConfigDescriptor && p_sceUsbdGetActiveConfigDescriptor(dev, config) == 0 && *config) {+ return 0;+ }+ if (p_sceUsbdGetConfigDescriptor && p_sceUsbdGetConfigDescriptor(dev, 0, config) == 0 && *config) {+ return 0;+ }+ return -1;+}++void freeConfigDescriptor(struct libusb_config_descriptor* config) {+ if (p_sceUsbdFreeConfigDescriptor && config) {+ p_sceUsbdFreeConfigDescriptor(config);+ }+}++// Controller hardware identifiers and interface masks+constexpr uint16_t SONY_VID = 0x054C;+constexpr uint16_t SONY_DS3_PID = 0x0268;+constexpr uint16_t SONY_DS4_V1_PID = 0x05C4;+constexpr uint16_t SONY_DS4_V2_PID = 0x09CC;+constexpr uint16_t SONY_DS4_PROT_PID = 0x05C5;+constexpr uint16_t SONY_DS4_DONGLE_PID = 0x0BA0;+constexpr uint16_t SONY_DUALSENSE_PID = 0x0CE6;+constexpr uint16_t SONY_DUALSENSE_EDGE_PID = 0x0DF2;++constexpr uint16_t XBOX_VID = 0x045E;+constexpr uint16_t XBOX360_PID = 0x028E;+constexpr uint16_t PDP_VID = 0x0E6F;+constexpr uint16_t VICTRIX_PRO_BFG_PID = 0x024C;++constexpr uint16_t SWITCH_PRO_VID = 0x057E;+constexpr uint16_t SWITCH_PRO_PID = 0x2009;+constexpr uint16_t SWITCH_ROCKCANDY_PID = 0x0187;++constexpr uint16_t GOOGLE_VID = 0x18D1;+constexpr uint16_t STADIA_PID = 0x9400;++bool isNativePs4Controller(uint16_t vid, uint16_t pid) {+ if (vid == SONY_VID) {+ if (pid == SONY_DS4_V1_PID || pid == SONY_DS4_V2_PID || pid == SONY_DS4_PROT_PID || pid == SONY_DS4_DONGLE_PID) {+ return true;+ }+ }+ return false;+}++bool isXboxOnePid(uint16_t pid) {+ static const uint16_t pids[] = {0x02D1, 0x02DD, 0x02E3, 0x02EA, 0x0B00, 0x0B0A, 0x0B12};+ for (uint16_t p : pids) {+ if (p == pid) return true;+ }+ return false;+}++bool isSwitchProPid(uint16_t pid) {+ static const uint16_t pids[] = {0x2009, 0x2006, 0x2007, 0x200E, 0x2017, 0x2019, 0x201E};+ for (uint16_t p : pids) {+ if (p == pid) return true;+ }+ return false;+}++struct DetectedPad {+ UsbPadProtocol protocol = UsbPadProtocol::None;+ uint8_t interfaceNum = 0;+ uint8_t altSetting = 0;+ uint8_t epIn = 0;+ uint8_t epOut = 0;+ uint8_t kbdInterfaceNum = 0xFF;+ std::string name;+};++bool detectXboxByDescriptor(libusb_device* dev, DetectedPad& out) {+ struct libusb_config_descriptor* cfg = nullptr;+ if (getActiveConfigDescriptor(dev, &cfg) != 0 || !cfg) return false;++ bool found = false;+ for (int i = 0; i < cfg->bNumInterfaces && !found; ++i) {+ const struct libusb_interface* intf = &cfg->interface[i];+ for (int a = 0; a < intf->num_altsetting && !found; ++a) {+ const struct libusb_interface_descriptor* alt = &intf->altsetting[a];+ if (alt->bInterfaceClass != 0xFF) continue;++ UsbPadProtocol proto = UsbPadProtocol::None;+ if (alt->bInterfaceSubClass == 0x5D && (alt->bInterfaceProtocol == 0x01 || alt->bInterfaceProtocol == 0x81 || alt->bInterfaceProtocol == 0x00)) {+ proto = UsbPadProtocol::XInput;+ } else if (alt->bInterfaceSubClass == 0x47 && alt->bInterfaceProtocol == 0xD0) {+ proto = UsbPadProtocol::Gip;+ } else {+ continue;+ }++ uint8_t epIn = 0, epOut = 0;+ for (int k = 0; k < alt->bNumEndpoints; ++k) {+ const struct libusb_endpoint_descriptor* ep = &alt->endpoint[k];+ if ((ep->bmAttributes & 0x03) != 0x03) continue; // Interrupt+ if (ep->bEndpointAddress & 0x80) {+ if (!epIn) epIn = ep->bEndpointAddress;+ } else {+ if (!epOut) epOut = ep->bEndpointAddress;+ }+ }+ if (!epIn) continue;++ out.protocol = proto;+ out.interfaceNum = alt->bInterfaceNumber;+ out.altSetting = alt->bAlternateSetting;+ out.epIn = epIn;+ out.epOut = epOut;+ out.kbdInterfaceNum = 0xFF;+ found = true;+ }+ }++ if (found) {+ // Scan for composite keyboard interface+ for (int i = 0; i < cfg->bNumInterfaces; ++i) {+ const struct libusb_interface* intf = &cfg->interface[i];+ for (int a = 0; a < intf->num_altsetting; ++a) {+ const struct libusb_interface_descriptor* alt = &intf->altsetting[a];+ if (alt->bInterfaceClass == 0x03 && alt->bInterfaceSubClass == 1 && alt->bInterfaceProtocol == 1) {+ out.kbdInterfaceNum = alt->bInterfaceNumber;+ break;+ }+ }+ }+ }++ freeConfigDescriptor(cfg);+ return found;+}++bool detectGenericHidEndpoints(libusb_device* dev, DetectedPad& out) {+ struct libusb_config_descriptor* cfg = nullptr;+ if (getActiveConfigDescriptor(dev, &cfg) != 0 || !cfg) return false;++ bool found = false;+ for (int i = 0; i < cfg->bNumInterfaces && !found; ++i) {+ const struct libusb_interface* intf = &cfg->interface[i];+ for (int a = 0; a < intf->num_altsetting && !found; ++a) {+ const struct libusb_interface_descriptor* alt = &intf->altsetting[a];+ if (alt->bInterfaceClass != 0x03) continue; // HID+ if (alt->bInterfaceSubClass == 1 && alt->bInterfaceProtocol == 1) continue; // Skip keyboard++ uint8_t epIn = 0, epOut = 0;+ for (int k = 0; k < alt->bNumEndpoints; ++k) {+ const struct libusb_endpoint_descriptor* ep = &alt->endpoint[k];+ if ((ep->bmAttributes & 0x03) != 0x03) continue;+ if (ep->bEndpointAddress & 0x80) {+ if (!epIn) epIn = ep->bEndpointAddress;+ } else {+ if (!epOut) epOut = ep->bEndpointAddress;+ }+ }+ if (!epIn) continue;++ out.protocol = UsbPadProtocol::GenericHid;+ out.interfaceNum = alt->bInterfaceNumber;+ out.altSetting = alt->bAlternateSetting;+ out.epIn = epIn;+ out.epOut = epOut;+ out.kbdInterfaceNum = 0xFF;+ found = true;+ }+ }++ if (found) {+ for (int i = 0; i < cfg->bNumInterfaces; ++i) {+ const struct libusb_interface* intf = &cfg->interface[i];+ for (int a = 0; a < intf->num_altsetting; ++a) {+ const struct libusb_interface_descriptor* alt = &intf->altsetting[a];+ if (alt->bInterfaceClass == 0x03 && alt->bInterfaceSubClass == 1 && alt->bInterfaceProtocol == 1 &&+ alt->bInterfaceNumber != out.interfaceNum) {+ out.kbdInterfaceNum = alt->bInterfaceNumber;+ break;+ }+ }+ }+ }++ freeConfigDescriptor(cfg);+ return found;+}++bool isDualShock4Controller(uint16_t vid, uint16_t pid) {+ if (vid == SONY_VID && (pid == SONY_DS4_V1_PID || pid == SONY_DS4_V2_PID || pid == SONY_DS4_PROT_PID || pid == SONY_DS4_DONGLE_PID)) {+ return false; // Handled natively by Orbis ScePad+ }+ if (vid == 0x146B && (pid == 0x0601 || pid == 0x0602 || pid == 0x0604 || pid == 0x0609)) return true; // Nacon+ if (vid == 0x1532 && (pid == 0x0401 || pid == 0x1000 || pid == 0x1004 || pid == 0x1007 || pid == 0x1008)) return true; // Razer+ if (vid == 0x0F0D && (pid == 0x0055 || pid == 0x0066 || pid == 0x0084 || pid == 0x00A0 || pid == 0x00EE)) return true; // Hori+ if (vid == 0x2C22 && (pid == 0x2000 || pid == 0x2300 || pid == 0x2500)) return true; // Qanba+ if (vid == 0x0C12 && (pid == 0x0E10 || pid == 0x0CF2 || pid == 0x1CF2)) return true; // Brook / Zeroplus+ return false;+}++bool identifyController(libusb_device* dev, const struct libusb_device_descriptor& desc, DetectedPad& out) {+ uint16_t vid = desc.idVendor;+ uint16_t pid = desc.idProduct;++ // 0. Skip native PS4 controllers (handled natively by ScePad)+ if (isNativePs4Controller(vid, pid)) return false;++ // 1. Sony DualShock 3+ if (vid == SONY_VID && pid == SONY_DS3_PID) {+ out.protocol = UsbPadProtocol::DualShock3;+ out.interfaceNum = 0;+ out.epIn = 0x81;+ out.epOut = 0x02;+ out.name = "DualShock 3";+ return true;+ }++ // 1b. Third-party DualShock 4 controllers (Nacon, Razer, Hori, Qanba, Brook, etc.)+ if (isDualShock4Controller(vid, pid)) {+ out.protocol = UsbPadProtocol::DualShock4;+ out.name = "PS4 Controller";+ if (!detectGenericHidEndpoints(dev, out)) {+ out.interfaceNum = 0;+ out.epIn = 0x81;+ out.epOut = 0x01;+ }+ out.protocol = UsbPadProtocol::DualShock4;+ return true;+ }++ // 2. Sony DualSense / DualSense Edge+ if (vid == SONY_VID && (pid == SONY_DUALSENSE_PID || pid == SONY_DUALSENSE_EDGE_PID)) {+ out.protocol = UsbPadProtocol::DualSense;+ out.name = (pid == SONY_DUALSENSE_EDGE_PID) ? "DualSense Edge" : "DualSense";+ if (!detectGenericHidEndpoints(dev, out)) {+ out.interfaceNum = 3;+ out.epIn = 0x84;+ out.epOut = 0x03;+ }+ out.protocol = UsbPadProtocol::DualSense;+ return true;+ }++ // 3. Official Xbox 360 & Xbox One+ if (vid == XBOX_VID) {+ if (pid == XBOX360_PID) {+ out.protocol = UsbPadProtocol::XInput;+ out.interfaceNum = 0;+ out.epIn = 0x81;+ out.epOut = 0x01;+ out.name = "Xbox 360 Controller";+ return true;+ }+ if (isXboxOnePid(pid)) {+ out.protocol = UsbPadProtocol::Gip;+ out.name = "Xbox Wireless Controller";+ if (!detectXboxByDescriptor(dev, out)) {+ out.interfaceNum = 0;+ out.altSetting = 1;+ out.epIn = 0x82;+ out.epOut = 0x02;+ }+ return true;+ }+ }++ // 4. PDP Victrix Pro BFG Xbox+ if (vid == PDP_VID && pid == VICTRIX_PRO_BFG_PID) {+ out.protocol = UsbPadProtocol::Gip;+ out.name = "Victrix Pro BFG";+ if (!detectXboxByDescriptor(dev, out)) {+ out.interfaceNum = 0;+ out.altSetting = 1;+ out.epIn = 0x82;+ out.epOut = 0x02;+ }+ return true;+ }++ // 5. Nintendo Switch Pro & Joy-Con Grip+ if (vid == SWITCH_PRO_VID && isSwitchProPid(pid)) {+ out.protocol = UsbPadProtocol::SwitchPro;+ out.name = "Nintendo Switch Pro Controller";+ if (!detectGenericHidEndpoints(dev, out)) {+ out.interfaceNum = 0;+ out.epIn = 0x81;+ out.epOut = 0x01;+ }+ out.protocol = UsbPadProtocol::SwitchPro;+ return true;+ }++ // 6. Switch Input-Only (PDP Rock Candy)+ if (vid == PDP_VID && pid == SWITCH_ROCKCANDY_PID) {+ out.protocol = UsbPadProtocol::SwitchInputOnly;+ out.interfaceNum = 0;+ out.epIn = 0x81;+ out.epOut = 0;+ out.name = "Switch Input-Only Controller";+ return true;+ }++ // 7. Google Stadia+ if (vid == GOOGLE_VID && pid == STADIA_PID) {+ out.protocol = UsbPadProtocol::Stadia;+ out.interfaceNum = 0;+ out.epIn = 0x81;+ out.epOut = 0x01;+ out.name = "Google Stadia Controller";+ return true;+ }++ // 8. Descriptor-based detection (XInput 8BitDo/clones and GIP pads)+ if (detectXboxByDescriptor(dev, out)) {+ out.name = (out.protocol == UsbPadProtocol::Gip) ? "GIP Controller" : "XInput Controller";+ return true;+ }++ // 9. Generic HID fallback+ if (detectGenericHidEndpoints(dev, out)) {+ out.name = "Generic USB Gamepad";+ return true;+ }++ return false;+}++struct Device {+ libusb_device_handle* handle = nullptr;+ int slot = -1;+ uint8_t bus = 0;+ uint8_t addr = 0;+ uint16_t vid = 0, pid = 0;+ std::string name;+ UsbPadProtocol protocol = UsbPadProtocol::None;+ uint8_t interfaceNum = 0;+ uint8_t altSetting = 0;+ uint8_t epIn = 0;+ uint8_t epOut = 0;+ uint8_t kbdInterfaceNum = 0xFF;+ bool hidConfigured = false;+ GenericHidContext hidCtx{};+ int disconnectErrors = 0;+ std::atomic<bool> stop{false};+ std::atomic<bool> dead{false};+ std::atomic<bool> attached{false};+ std::thread thread;+};++std::mutex g_mutex;+std::vector<std::unique_ptr<Device>> g_devices;+std::thread g_scanner;+std::atomic<bool> g_running{false};++int freeSlot() {+ for (int s = 0; s < kMaxUsbPads; ++s) {+ bool used = false;+ for (const auto& d : g_devices) used |= d->slot == s;+ if (!used) return s;+ }+ return -1;+}++void sendInitHandshake(Device& d) {+ if (!d.handle) return;++ if (d.protocol == UsbPadProtocol::XInput) {+ uint8_t dummy[20] = {};+ if (p_sceUsbdControlTransfer) {+ p_sceUsbdControlTransfer(d.handle, 0xC1, 0x01, 0x0100, d.interfaceNum, dummy, sizeof(dummy), 100);+ }+ if (d.epOut && p_sceUsbdInterruptTransfer) {+ uint8_t ledPkt[3] = {0x01, 0x03, 0x02}; // Player 1 LED+ int transferred = 0;+ p_sceUsbdInterruptTransfer(d.handle, d.epOut, ledPkt, sizeof(ledPkt), &transferred, 100);+ }+ XC_LOGI("usb pad %d: XInput wake-up sent", d.slot);+ } else if (d.protocol == UsbPadProtocol::Gip) {+ static uint8_t s_seq = 1;+ auto sendPacket = [&](const uint8_t* p, size_t sz) {+ if (!d.epOut || !p_sceUsbdInterruptTransfer) return;+ int transferred = 0;+ p_sceUsbdInterruptTransfer(d.handle, d.epOut, const_cast<uint8_t*>(p), static_cast<int32_t>(sz), &transferred, 100);+ std::this_thread::sleep_for(std::chrono::milliseconds(10));+ };+ const uint8_t powerOn[] = {0x05, 0x20, s_seq++, 0x01, 0x00};+ const uint8_t sInit[] = {0x05, 0x20, s_seq++, 0x0F, 0x06};+ const uint8_t horiInit[] = {0x01, 0x20, s_seq++, 0x09, 0x00, 0x04, 0x20, 0x3a, 0x00, 0x00, 0x00, 0x80, 0x00};+ const uint8_t pdpInit1[] = {0x0a, 0x20, s_seq++, 0x03, 0x00, 0x01, 0x14};+ const uint8_t pdpInit2[] = {0x06, 0x20, s_seq++, 0x02, 0x01, 0x00};+ sendPacket(powerOn, sizeof(powerOn));+ sendPacket(sInit, sizeof(sInit));+ sendPacket(horiInit, sizeof(horiInit));+ sendPacket(pdpInit1, sizeof(pdpInit1));+ sendPacket(pdpInit2, sizeof(pdpInit2));+ XC_LOGI("usb pad %d: GIP initialization handshake sent", d.slot);+ } else if (d.protocol == UsbPadProtocol::DualShock3) {+ uint8_t magic[5] = {0xF4, 0x42, 0x03, 0x00, 0x00};+ if (p_sceUsbdControlTransfer) {+ p_sceUsbdControlTransfer(d.handle, 0x21, 0x09, 0x03F4, d.interfaceNum, magic, sizeof(magic), 1000);+ uint8_t dummy[64] = {};+ p_sceUsbdControlTransfer(d.handle, 0xA1, 0x01, 0x03F2, d.interfaceNum, dummy, 17, 100);+ p_sceUsbdControlTransfer(d.handle, 0xA1, 0x01, 0x03F5, d.interfaceNum, dummy, 8, 100);+ }+ XC_LOGI("usb pad %d: DualShock 3 operational handshake sent", d.slot);+ } else if (d.protocol == UsbPadProtocol::DualSense) {+ if (p_sceUsbdControlTransfer) {+ uint8_t desc[512] = {};+ p_sceUsbdControlTransfer(d.handle, 0x81, 0x06, 0x2200, d.interfaceNum, desc, sizeof(desc), 200);+ uint8_t pairBuf[20] = {};+ p_sceUsbdControlTransfer(d.handle, 0xA1, 0x01, 0x0309, d.interfaceNum, pairBuf, sizeof(pairBuf), 200);+ uint8_t fwBuf[64] = {};+ p_sceUsbdControlTransfer(d.handle, 0xA1, 0x01, 0x0320, d.interfaceNum, fwBuf, sizeof(fwBuf), 200);+ uint8_t calBuf[41] = {};+ p_sceUsbdControlTransfer(d.handle, 0xA1, 0x01, 0x0305, d.interfaceNum, calBuf, sizeof(calBuf), 200);+ }+ if (p_sceUsbdClearHalt) {+ p_sceUsbdClearHalt(d.handle, d.epIn);+ if (d.epOut) p_sceUsbdClearHalt(d.handle, d.epOut);+ }+ std::this_thread::sleep_for(std::chrono::milliseconds(25));+ if (d.epOut && p_sceUsbdInterruptTransfer) {+ uint8_t resetPkt[64] = {};+ resetPkt[0] = 0x02;+ resetPkt[39] = 0x02;+ resetPkt[42] = 0x02;+ int transferred = 0;+ p_sceUsbdInterruptTransfer(d.handle, d.epOut, resetPkt, sizeof(resetPkt), &transferred, 150);+ std::this_thread::sleep_for(std::chrono::milliseconds(15));+ uint8_t ledPkt[64] = {};+ ledPkt[0] = 0x02;+ ledPkt[2] = 0x04 | 0x10;+ ledPkt[44] = 0x04; // Player 1 LED+ ledPkt[47] = 128; // Lightbar blue+ p_sceUsbdInterruptTransfer(d.handle, d.epOut, ledPkt, sizeof(ledPkt), &transferred, 150);+ }+ XC_LOGI("usb pad %d: DualSense USB handshake sent", d.slot);+ } else if (d.protocol == UsbPadProtocol::SwitchPro) {+ if (d.epIn && d.epOut && p_sceUsbdInterruptTransfer) {+ uint8_t rx[64];+ int transferred = 0;+ for (int i = 0; i < 3; ++i) {+ p_sceUsbdInterruptTransfer(d.handle, d.epIn, rx, sizeof(rx), &transferred, 10);+ }+ uint8_t pkt[64] = {};+ pkt[0] = 0x80; pkt[1] = 0x02;+ p_sceUsbdInterruptTransfer(d.handle, d.epOut, pkt, sizeof(pkt), &transferred, 50);+ pkt[1] = 0x04;+ p_sceUsbdInterruptTransfer(d.handle, d.epOut, pkt, sizeof(pkt), &transferred, 50);+ // Subcommand 0x03 (Set report mode to 0x30 Full Mode)+ std::memset(pkt, 0, sizeof(pkt));+ pkt[0] = 0x01; pkt[1] = 0x00; pkt[10] = 0x03; pkt[11] = 0x30;+ p_sceUsbdInterruptTransfer(d.handle, d.epOut, pkt, sizeof(pkt), &transferred, 100);+ // Subcommand 0x40 (Enable IMU)+ pkt[10] = 0x40; pkt[11] = 0x01;+ p_sceUsbdInterruptTransfer(d.handle, d.epOut, pkt, sizeof(pkt), &transferred, 100);+ // Subcommand 0x30 (Player 1 LED)+ pkt[10] = 0x30; pkt[11] = 0x01;+ p_sceUsbdInterruptTransfer(d.handle, d.epOut, pkt, sizeof(pkt), &transferred, 100);+ }+ XC_LOGI("usb pad %d: Switch Pro wake-up sent", d.slot);+ } else if (d.protocol == UsbPadProtocol::GenericHid) {+ if (p_sceUsbdControlTransfer) {+ uint8_t reportDesc[512] = {};+ int r = p_sceUsbdControlTransfer(d.handle, 0x81, 0x06, 0x2200, d.interfaceNum, reportDesc, sizeof(reportDesc), 200);+ if (r > 0 && parseHidDescriptor(d.hidCtx, reportDesc, static_cast<size_t>(r), d.vid, d.pid)) {+ d.hidConfigured = true;+ XC_LOGI("usb pad %d: Generic HID descriptor parsed (%d bytes)", d.slot, r);+ }+ }+ }+}++void readLoop(Device* d) {+ XC_LOGI("usb pad %d: read thread started for %s (%04x:%04x, %s)", d->slot, d->name.c_str(), d->vid, d->pid,+ usbPadProtocolName(d->protocol));+ usbPadAttached(d->slot, d->name);+ d->attached = true;+ sendInitHandshake(*d);++ ControllerState state;+ uint8_t buf[256];+ int errors = 0;+ while (!d->stop) {+ int transferred = 0;+ int rc = p_sceUsbdInterruptTransfer(d->handle, d->epIn, buf, sizeof(buf), &transferred, 20);+ bool isTimeout = (rc == -7 || rc == static_cast<int>(0x80240007));+ if (rc != 0 || transferred <= 0) {+ if (rc != 0 && !isTimeout) {+ if (++errors >= 50) {+ XC_LOGW("usb pad %d: %d transfer errors (0x%08x): closed", d->slot, errors, static_cast<unsigned>(rc));+ break;+ }+ } else {+ errors = 0;+ }+ std::this_thread::sleep_for(std::chrono::milliseconds(2));+ continue;+ }+ errors = 0;++ // GIP ACK responses using standard gipAcksFor+ if (d->protocol == UsbPadProtocol::Gip && d->epOut && p_sceUsbdInterruptTransfer) {+ for (const auto& ack : gipAcksFor(buf, static_cast<size_t>(transferred))) {+ int sent = 0;+ p_sceUsbdInterruptTransfer(d->handle, d->epOut, const_cast<uint8_t*>(ack.data()), static_cast<int32_t>(ack.size()), &sent, 40);+ }+ }++ bool ok = false;+ if (d->protocol == UsbPadProtocol::GenericHid) {+ if (d->hidConfigured) {+ ok = parseGenericHidReport(d->hidCtx, buf, static_cast<size_t>(transferred), state);+ } else {+ ok = parseGenericHidFallback(buf, static_cast<size_t>(transferred), state);+ }+ } else {+ ok = parseUsbPadReport(d->protocol, buf, static_cast<size_t>(transferred), state);+ }+ if (ok) {+ usbPadState(d->slot, state);+ }+ }++ d->dead = true;+ XC_LOGI("usb pad %d: read thread finished", d->slot);+}++void closeDevice(Device& d) {+ d.stop = true;+ if (d.thread.joinable()) d.thread.join();+ if (d.attached) usbPadDetached(d.slot);+ if (d.handle) {+ if (d.kbdInterfaceNum != 0xFF && p_sceUsbdReleaseInterface) {+ p_sceUsbdReleaseInterface(d.handle, d.kbdInterfaceNum);+ }+ if (p_sceUsbdReleaseInterface) {+ p_sceUsbdReleaseInterface(d.handle, d.interfaceNum);+ }+ if (p_sceUsbdClose) {+ p_sceUsbdClose(d.handle);+ }+ d.handle = nullptr;+ }+}++void scan() {+ std::lock_guard<std::mutex> lock(g_mutex);++ // 1. Remove disconnected / dead devices+ for (auto it = g_devices.begin(); it != g_devices.end();) {+ Device& d = **it;+ bool disconnected = d.dead;+ if (!disconnected && d.handle && p_sceUsbdCheckConnected) {+ if (p_sceUsbdCheckConnected(d.handle) != 0) {+ if (++d.disconnectErrors >= 3) disconnected = true;+ } else {+ d.disconnectErrors = 0;+ }+ }+ if (disconnected) {+ XC_LOGI("usb pad %d: disconnecting %s", d.slot, d.name.c_str());+ closeDevice(d);+ it = g_devices.erase(it);+ } else {+ ++it;+ }+ }++ // 2. Discover new devices+ libusb_device** list = nullptr;+ int32_t count = p_sceUsbdGetDeviceList(&list);+ if (count <= 0 || !list) return;++ for (int i = 0; i < count; ++i) {+ libusb_device* dev = list[i];+ if (!dev) continue;++ uint8_t bus = p_sceUsbdGetBusNumber ? p_sceUsbdGetBusNumber(dev) : 0;+ uint8_t addr = p_sceUsbdGetDeviceAddress ? p_sceUsbdGetDeviceAddress(dev) : 0;++ struct libusb_device_descriptor desc{};+ if (p_sceUsbdGetDeviceDescriptor(dev, &desc) != 0) continue;++ // Skip if already managed (by bus/address or VID/PID/interface if bus/addr unavailable)+ bool alreadyOpen = false;+ for (const auto& d : g_devices) {+ if (bus != 0 && addr != 0) {+ if (d->bus == bus && d->addr == addr) {+ alreadyOpen = true;+ break;+ }+ } else if (d->vid == desc.idVendor && d->pid == desc.idProduct) {+ alreadyOpen = true;+ break;+ }+ }+ if (alreadyOpen) continue;++ DetectedPad det;+ if (!identifyController(dev, desc, det)) continue;++ int slot = freeSlot();+ if (slot < 0) {+ XC_LOGW("usb pad: all %d slots taken, cannot attach %s", kMaxUsbPads, det.name.c_str());+ continue;+ }++ libusb_device_handle* h = nullptr;+ if (p_sceUsbdOpen(dev, &h) != 0 || !h) {+ XC_LOGW("usb pad: sceUsbdOpen failed for %s (%04x:%04x)", det.name.c_str(), desc.idVendor, desc.idProduct);+ continue;+ }++ // Detach secondary composite keyboard if present (prevents OS input hijack)+ if (det.kbdInterfaceNum != 0xFF) {+ if (p_sceUsbdDetachKernelDriver) p_sceUsbdDetachKernelDriver(h, det.kbdInterfaceNum);+ if (p_sceUsbdClaimInterface) p_sceUsbdClaimInterface(h, det.kbdInterfaceNum);+ XC_LOGI("usb pad: detached secondary composite keyboard interface %d", det.kbdInterfaceNum);+ }++ // Set alt setting if needed (e.g. Xbox One GIP alt 1)+ if (det.altSetting > 0 && p_sceUsbdSetInterfaceAltSetting) {+ p_sceUsbdSetInterfaceAltSetting(h, det.interfaceNum, det.altSetting);+ }++ if (p_sceUsbdDetachKernelDriver) p_sceUsbdDetachKernelDriver(h, det.interfaceNum);+ if (p_sceUsbdClaimInterface && p_sceUsbdClaimInterface(h, det.interfaceNum) != 0) {+ XC_LOGW("usb pad: claim interface %d failed for %s", det.interfaceNum, det.name.c_str());+ p_sceUsbdClose(h);+ continue;+ }++ auto d = std::make_unique<Device>();+ d->handle = h;+ d->slot = slot;+ d->bus = bus;+ d->addr = addr;+ d->vid = desc.idVendor;+ d->pid = desc.idProduct;+ d->name = det.name;+ d->protocol = det.protocol;+ d->interfaceNum = det.interfaceNum;+ d->altSetting = det.altSetting;+ d->epIn = det.epIn;+ d->epOut = det.epOut;+ d->kbdInterfaceNum = det.kbdInterfaceNum;+ d->thread = std::thread(readLoop, d.get());++ g_devices.push_back(std::move(d));+ }++ p_sceUsbdFreeDeviceList(list);+}++void scanLoop() {+ XC_LOGI("usb pad: starting PS4 USB scan loop");+ while (g_running) {+ scan();+ for (int i = 0; i < 15 && g_running; ++i) {+ std::this_thread::sleep_for(std::chrono::milliseconds(100));+ }+ }+ std::lock_guard<std::mutex> lock(g_mutex);+ for (auto& d : g_devices) closeDevice(*d);+ g_devices.clear();+ XC_LOGI("usb pad: PS4 USB scan loop terminated");+}++} // namespace++void usbPadsStart() {+ if (g_running.exchange(true)) return;+ if (!loadUsbd()) {+ g_running = false;+ return;+ }+ XC_LOGI("usb pad: looking for controllers on PS4 USB (libSceUsbd)");+ g_scanner = std::thread(scanLoop);+}++void usbPadsStop() {+ if (!g_running.exchange(false)) return;+ if (g_scanner.joinable()) g_scanner.join();+ if (p_sceUsbdExit) {+ p_sceUsbdExit();+ }+ if (g_hUsbd > 0) {+ int res = 0;+ sceKernelStopUnloadModule(g_hUsbd, 0, nullptr, 0, nullptr, &res);+ g_hUsbd = 0;+ }+}++} // namespace xc::inputdiff --git a/src/input/pad_usb_ps5.cpp b/src/input/pad_usb_ps5.cppindex ad8e80d..0b4e7c3 100644--- a/src/input/pad_usb_ps5.cpp+++ b/src/input/pad_usb_ps5.cpp@@ -63,7 +63,7 @@ std::thread g_scanner;std::atomic<bool> g_running{false};const char* protocolName(UsbPadProtocol p) {- return p == UsbPadProtocol::Gip ? "GIP" : p == UsbPadProtocol::XInput ? "XInput" : "none";+ return usbPadProtocolName(p);}std::string hex(const uint8_t* p, int n) {diff --git a/src/platform/ps4/trophy_backend_ps4.cpp b/src/platform/ps4/trophy_backend_ps4.cppindex 0f4089d..e05c6e2 100644--- a/src/platform/ps4/trophy_backend_ps4.cpp+++ b/src/platform/ps4/trophy_backend_ps4.cpp@@ -163,4 +163,8 @@ ITrophyBackend* createPlatformTrophyBackend() {return new TrophyBackendPS4();}+void runPsboxNativeSelfTest() {+ XC_LOGI("psbox selftest: no-op on PS4");+}+} // namespace xc::trophydiff --git a/tests/test_main.cpp b/tests/test_main.cppindex f1525e8..cedb3a2 100644--- a/tests/test_main.cpp+++ b/tests/test_main.cpp@@ -21,6 +21,7 @@#include "input/gyro.h"#include "input/tuning.h"#include "input/pad_usb.h"+#include "input/pad_usb_hid.h"#include "net/http.h"#include "platform/platform.h"#include "platform/toasts.h"@@ -4219,6 +4220,251 @@ static void testUsbPadReports() {const std::array<uint8_t, 13> ack = {0x01, 0x20, 0x01, 0x09, 0x00, 0x07, 0x20, 0x02, 0, 0, 0, 0, 0};CHECK(!acks.empty() && acks[0] == ack);CHECK(input::gipAcksFor(gip, sizeof gip).empty()); // input is not acknowledged++ // DualShock 3 (PS3): Select + DPad Up + Cross + L1 + PS button, sticks, triggers+ uint8_t ds3[49] = {};+ ds3[0] = 0x01;+ ds3[2] = 0x11; // Select (0x01) + DPad Up (0x10)+ ds3[3] = 0x44; // Cross (0x40) + L1 (0x04)+ ds3[4] = 0x01; // PS button (0x01)+ ds3[6] = 255; // LX right (1.0f)+ ds3[7] = 0; // LY up (-1.0f)+ ds3[8] = 128; // RX center (0.0f)+ ds3[9] = 255; // RY down (1.0f)+ ds3[18] = 255; // L2 trigger (1.0f)+ ds3[19] = 128; // R2 trigger (~0.5f)+ s = {};+ CHECK(input::parseDualShock3Report(ds3, sizeof ds3, s));+ CHECK(s.connected && s.btnA && s.btnL1 && s.dpadUp && s.btnTouchpad && s.btnNexus);+ CHECK(!s.btnB && !s.btnX && !s.btnY && !s.btnR1 && !s.dpadDown && !s.btnOptions);+ CHECK(near(s.rawLeftX, 1.0f) && near(s.rawLeftY, -1.0f));+ CHECK(near(s.rawRightX, 0.0f) && near(s.rawRightY, 1.0f));+ CHECK(near(s.triggerL2, 1.0f) && near(s.triggerR2, 0.5f));+ CHECK(input::parseUsbPadReport(input::UsbPadProtocol::DualShock3, ds3, sizeof ds3, s));++ // DualSense (PS5 USB): Cross + DPad Right + R1 + Options + PS button + touch point+ uint8_t ds5[64] = {};+ ds5[0] = 0x01; // Report ID+ ds5[1] = 0; // LX left (-1.0f)+ ds5[2] = 255; // LY down (1.0f)+ ds5[3] = 255; // RX right (1.0f)+ ds5[4] = 128; // RY center (0.0f)+ ds5[5] = 255; // L2 (1.0f)+ ds5[6] = 0; // R2 (0.0f)+ ds5[8] = 0x22; // Hat 2 (Right) + Cross (0x20)+ ds5[9] = 0x22; // R1 (0x02) + Options (0x20)+ ds5[10] = 0x01; // PS button (0x01)+ ds5[33] = 0x00; // Touch point 0 active (contact bit 7 == 0)+ ds5[34] = 0x80; // tx lo+ ds5[35] = 0x43; // tx hi (0x3), ty lo (0x4)+ ds5[36] = 0x20; // ty hi (0x20)+ s = {};+ CHECK(input::parseDualSenseReport(ds5, sizeof ds5, s));+ CHECK(s.connected && s.btnA && s.dpadRight && s.btnR1 && s.btnOptions && s.btnNexus);+ CHECK(!s.btnB && !s.btnX && !s.btnY && !s.btnL1 && !s.dpadUp);+ CHECK(near(s.rawLeftX, -1.0f) && near(s.rawLeftY, 1.0f));+ CHECK(near(s.rawRightX, 1.0f) && near(s.rawRightY, 0.0f));+ CHECK(near(s.triggerL2, 1.0f) && near(s.triggerR2, 0.0f));+ CHECK(s.touching);+ CHECK(input::parseUsbPadReport(input::UsbPadProtocol::DualSense, ds5, sizeof ds5, s));++ // Nintendo Switch Pro (full report 0x30): B (btnA) + R (btnR1) + Plus (btnOptions) + Home (btnNexus) + Up+ uint8_t sw[12] = {};+ sw[0] = 0x30;+ sw[3] = 0x04 | 0x40; // B (0x04) + R (0x40)+ sw[4] = 0x02 | 0x10; // Plus (0x02) + Home (0x10)+ sw[5] = 0x02 | 0x80; // Up (0x02) + ZL (0x80)+ sw[6] = 0x00; sw[7] = 0x08; sw[8] = 0x80; // 12-bit LX=2048, LY=2048 (center)+ sw[9] = 0x00; sw[10] = 0x08; sw[11] = 0x80; // 12-bit RX=2048, RY=2048 (center)+ s = {};+ CHECK(input::parseSwitchProReport(sw, sizeof sw, s));+ CHECK(s.connected && s.btnA && s.btnR1 && s.btnOptions && s.btnNexus && s.dpadUp);+ CHECK(near(s.triggerL2, 1.0f) && near(s.triggerR2, 0.0f));+ CHECK(near(s.rawLeftX, 0.0f) && near(s.rawLeftY, 0.0f));+ CHECK(input::parseUsbPadReport(input::UsbPadProtocol::SwitchPro, sw, sizeof sw, s));++ // Nintendo Switch Pro (simple report 0x3F)+ uint8_t swSimple[12] = {};+ swSimple[0] = 0x3F;+ swSimple[1] = 0x02 | 0x20; // B (btnA) + R (btnR1)+ swSimple[2] = 0x02 | 0x10; // Plus (btnOptions) + Home (btnNexus)+ swSimple[3] = 0; // Hat 0 = Up+ swSimple[5] = 255; // LX right+ swSimple[7] = 0; // LY up+ swSimple[9] = 128; // RX center+ swSimple[11] = 255; // RY down+ s = {};+ CHECK(input::parseSwitchProReport(swSimple, sizeof swSimple, s));+ CHECK(s.connected && s.btnA && s.btnR1 && s.btnOptions && s.btnNexus && s.dpadUp);+ CHECK(near(s.rawLeftX, 1.0f) && near(s.rawLeftY, -1.0f));+ CHECK(near(s.rawRightX, 0.0f) && near(s.rawRightY, 1.0f));++ // Nintendo Switch Input-Only (7 bytes)+ uint8_t swIn[7] = {0x02 | 0x10, 0x10 | 0x01, 4, 255, 0, 128, 255}; // B+L, Home+Minus, Hat 4=Down, sticks+ s = {};+ CHECK(input::parseSwitchInputOnlyReport(swIn, sizeof swIn, s));+ CHECK(s.connected && s.btnA && s.btnL1 && s.btnNexus && s.btnTouchpad && s.dpadDown);+ CHECK(near(s.rawLeftX, 1.0f) && near(s.rawLeftY, -1.0f));+ CHECK(near(s.rawRightX, 0.0f) && near(s.rawRightY, 1.0f));+ CHECK(input::parseUsbPadReport(input::UsbPadProtocol::SwitchInputOnly, swIn, sizeof swIn, s));++ // Google Stadia (10 bytes)+ uint8_t stadia[10] = {0x03, 6, 0x10 | 0x20, 0x40 | 0x02, 0, 128, 128, 255, 255, 0};+ s = {};+ CHECK(input::parseStadiaReport(stadia, sizeof stadia, s));+ CHECK(s.connected && s.dpadLeft && s.btnNexus && s.btnOptions && s.btnA && s.btnR1);+ CHECK(near(s.triggerL2, 1.0f) && near(s.triggerR2, 0.0f));+ CHECK(near(s.rawLeftX, -1.0f) && near(s.rawRightY, 1.0f));+ CHECK(input::parseUsbPadReport(input::UsbPadProtocol::Stadia, stadia, sizeof stadia, s));++ // DualShock 4 (PS4 third-party): 64-byte report with ID 0x01+ // Cross + DPad Left + L1 + Options + PS + Touchpad click + touches+ uint8_t ds4[64] = {};+ ds4[0] = 0x01; // Report ID+ ds4[1] = 0; // LX left (-1.0f)+ ds4[2] = 255; // LY down (1.0f)+ ds4[3] = 255; // RX right (1.0f)+ ds4[4] = 0; // RY up (-1.0f)+ ds4[5] = 0x26; // Hat 6 (Left) + Cross (0x20)+ ds4[6] = 0x21; // L1 (0x01) + Options (0x20)+ ds4[7] = 0x03; // PS (0x01) + Touchpad click (0x02)+ ds4[8] = 255; // L2 trigger (1.0f)+ ds4[9] = 128; // R2 trigger (~0.5f)+ ds4[35] = 0x00; // Touch point 0 active (contact bit 7 == 0)+ ds4[36] = 0x80; // tx lo+ ds4[37] = 0x43; // tx hi (0x3), ty lo (0x4)+ ds4[38] = 0x20; // ty hi (0x20)+ s = {};+ CHECK(input::parseDualShock4Report(ds4, sizeof ds4, s));+ CHECK(s.connected && s.btnA && s.dpadLeft && s.btnL1 && s.btnOptions && s.btnNexus && s.btnTouchpad);+ CHECK(!s.btnB && !s.btnX && !s.btnY && !s.btnR1 && !s.dpadUp && !s.dpadRight && !s.dpadDown);+ CHECK(near(s.rawLeftX, -1.0f) && near(s.rawLeftY, 1.0f));+ CHECK(near(s.rawRightX, 1.0f) && near(s.rawRightY, -1.0f));+ CHECK(near(s.triggerL2, 1.0f) && near(s.triggerR2, 0.5f));+ CHECK(s.touching);+ CHECK(input::parseUsbPadReport(input::UsbPadProtocol::DualShock4, ds4, sizeof ds4, s));++ // DualShock 4 release D-Pad and Touch: hat = 8 (neutral), touch active bit 7 = 1+ ds4[5] = 0x08; // hat neutral, no face buttons+ ds4[35] = 0x80; // touch inactive+ CHECK(input::parseDualShock4Report(ds4, sizeof ds4, s));+ CHECK(!s.dpadLeft && !s.dpadUp && !s.dpadDown && !s.dpadRight);+ CHECK(!s.btnA && !s.touching);++ // DualSense release D-Pad and Touch+ ds5[8] = 0x08; // hat neutral+ ds5[33] = 0x80; // touch 0 inactive+ CHECK(input::parseDualSenseReport(ds5, sizeof ds5, s));+ CHECK(!s.dpadRight && !s.dpadUp && !s.dpadDown && !s.dpadLeft);+ CHECK(!s.touching);++ // Switch Pro 0x30 stick deflections: LY up (0) should produce rawLeftY = -1.0f+ sw[5] = 0x00; // D-Pad neutral+ sw[6] = 0x00; sw[7] = 0x08; sw[8] = 0x00; // 12-bit LX=2048 (center), LY=0 (full UP)+ CHECK(input::parseSwitchProReport(sw, sizeof sw, s));+ CHECK(!s.dpadUp && !s.dpadDown && !s.dpadLeft && !s.dpadRight);+ CHECK(near(s.rawLeftX, 0.0f) && near(s.rawLeftY, -1.0f));++ // Switch Pro 0x3F hat release+ swSimple[3] = 8; // neutral hat+ CHECK(input::parseSwitchProReport(swSimple, sizeof swSimple, s));+ CHECK(!s.dpadUp && !s.dpadDown && !s.dpadLeft && !s.dpadRight);++ // Switch Input-Only hat release+ swIn[2] = 8; // neutral hat+ CHECK(input::parseSwitchInputOnlyReport(swIn, sizeof swIn, s));+ CHECK(!s.dpadDown && !s.dpadUp && !s.dpadLeft && !s.dpadRight);++ // Stadia hat release+ stadia[1] = 8; // neutral hat+ CHECK(input::parseStadiaReport(stadia, sizeof stadia, s));+ CHECK(!s.dpadLeft && !s.dpadUp && !s.dpadDown && !s.dpadRight);++ // Generic HID fallback (raw axes + buttons)+ uint8_t hidRaw[7] = {255, 0, 128, 255, 0, 0x02 | 0x10, 0x10}; // LX=1.0, LY=-1.0, RX=0.0, RY=1.0, Hat=Up, Cross+L1, PS+ s = {};+ CHECK(input::parseGenericHidFallback(hidRaw, sizeof hidRaw, s));+ CHECK(s.connected && s.btnA && s.btnL1 && s.dpadUp && s.btnNexus);+ CHECK(near(s.rawLeftX, 1.0f) && near(s.rawLeftY, -1.0f));+ CHECK(input::parseUsbPadReport(input::UsbPadProtocol::GenericHid, hidRaw, sizeof hidRaw, s));++ // Generic HID fallback hat release+ hidRaw[4] = 8; // neutral hat+ CHECK(input::parseGenericHidFallback(hidRaw, sizeof hidRaw, s));+ CHECK(!s.dpadUp && !s.dpadDown && !s.dpadLeft && !s.dpadRight);++ // Generic HID descriptor with Rx/Ry right stick, Z/Rz triggers, and 12-button combo+ const uint8_t hidGamepadDesc[] = {+ 0x05, 0x01, // Usage Page (Generic Desktop)+ 0x09, 0x05, // Usage (Gamepad)+ 0xA1, 0x01, // Collection (Application)+ 0x05, 0x01, // Usage Page (Generic Desktop)+ 0x09, 0x30, // Usage (X)+ 0x09, 0x31, // Usage (Y)+ 0x09, 0x32, // Usage (Z)+ 0x09, 0x35, // Usage (Rz)+ 0x09, 0x33, // Usage (Rx)+ 0x09, 0x34, // Usage (Ry)+ 0x15, 0x00, // Logical Minimum (0)+ 0x26, 0xFF, 0x00, // Logical Maximum (255)+ 0x75, 0x08, // Report Size (8)+ 0x95, 0x06, // Report Count (6)+ 0x81, 0x02, // Input (Data, Var, Abs)+ 0x09, 0x39, // Usage (Hat switch)+ 0x15, 0x00, // Logical Minimum (0)+ 0x25, 0x07, // Logical Maximum (7)+ 0x75, 0x04, // Report Size (4)+ 0x95, 0x01, // Report Count (1)+ 0x81, 0x42, // Input (Data, Var, Abs, Null)+ 0x75, 0x04, // Report Size (4)+ 0x95, 0x01, // Report Count (1)+ 0x81, 0x01, // Input (Const)+ 0x05, 0x09, // Usage Page (Button)+ 0x19, 0x01, // Usage Minimum (1)+ 0x29, 0x0C, // Usage Maximum (12)+ 0x15, 0x00, // Logical Minimum (0)+ 0x25, 0x01, // Logical Maximum (1)+ 0x75, 0x01, // Report Size (1)+ 0x95, 0x0C, // Report Count (12)+ 0x81, 0x02, // Input (Data, Var, Abs)+ 0x75, 0x04, // Report Size (4)+ 0x95, 0x01, // Report Count (1)+ 0x81, 0x01, // Input (Const)+ 0xC0 // End Collection+ };+ input::GenericHidContext hidCtx{};+ CHECK(input::parseHidDescriptor(hidCtx, hidGamepadDesc, sizeof(hidGamepadDesc), 0x1234, 0x5678));+ CHECK(hidCtx.role[input::ROLE_LX] == input::AX_X);+ CHECK(hidCtx.role[input::ROLE_LY] == input::AX_Y);+ CHECK(hidCtx.role[input::ROLE_RX] == input::AX_RX);+ CHECK(hidCtx.role[input::ROLE_RY] == input::AX_RY);+ CHECK(hidCtx.role[input::ROLE_L2] == input::AX_Z);+ CHECK(hidCtx.role[input::ROLE_R2] == input::AX_RZ);++ // Feed a report to hidCtx:+ // LX=255, LY=0, Z=255 (L2), Rz=128 (R2), Rx=0, Ry=255, Hat=2 (Right), Buttons: Select (btn 9, bit 8) + Start (btn 10, bit 9)+ uint8_t hidReport[9] = {+ 255, 0, 255, 128, 0, 255,+ 0x02, // Hat=2 (Right)+ 0x00, 0x03 // bits 8 and 9 (Select + Start)+ };+ s = {};+ CHECK(input::parseGenericHidReport(hidCtx, hidReport, sizeof(hidReport), s));+ CHECK(s.connected && s.dpadRight);+ CHECK(near(s.rawLeftX, 1.0f) && near(s.rawLeftY, -1.0f));+ CHECK(near(s.rawRightX, -1.0f) && near(s.rawRightY, 1.0f));+ CHECK(near(s.triggerL2, 1.0f) && near(s.triggerR2, 0.5f));+ CHECK(s.btnTouchpad && s.btnOptions);+ CHECK(s.btnNexus); // 12-button combo active!++ // Protocol names+ CHECK(std::string(input::usbPadProtocolName(input::UsbPadProtocol::DualShock3)) == "DualShock 3");+ CHECK(std::string(input::usbPadProtocolName(input::UsbPadProtocol::DualShock4)) == "DualShock 4");+ CHECK(std::string(input::usbPadProtocolName(input::UsbPadProtocol::DualSense)) == "DualSense");+ CHECK(std::string(input::usbPadProtocolName(input::UsbPadProtocol::SwitchPro)) == "Switch Pro");+ CHECK(std::string(input::usbPadProtocolName(input::UsbPadProtocol::SwitchInputOnly)) == "Switch");+ CHECK(std::string(input::usbPadProtocolName(input::UsbPadProtocol::Stadia)) == "Stadia");+ CHECK(std::string(input::usbPadProtocolName(input::UsbPadProtocol::GenericHid)) == "Generic HID");}static void testUsbPadDescriptors() {