diff options
| author | Bora Sevim <sevimbora@gmail.com> | 2026-11-22 22:35:50 +0300 |
|---|---|---|
| committer | Bora Sevim <sevimbora@gmail.com> | 2026-11-22 22:35:50 +0300 |
| commit | 364fdcbd28feb1708fd28b4727b3cc8f4a1ad1a7 (patch) | |
| tree | acce8da5f81d727993a9cbc8bb11400d89fecda9 /runtime/shims/gameinput.cpp | |
| parent | 162821017ea3c286c8bec860f3ff94f037912061 (diff) | |
gameinput: raw HID reports and raw device I/O through SDL's hidapi (RT-175)
The shim opens each pad's HID device with SDL_hid_* (by SDL's device path, else the one game-pad collection with the
pad's vendor and product id) and reads its report descriptor for every input, output and feature report's id and
size. Such a pad reports GameInputKindRawDeviceReport and GameInputDeviceRawIoEnabled and fills inputReportInfo,
outputReportInfo and featureReportInfo (no item tables). Readings carry the newest raw input report, and
IGameInputReading::GetRawReport returns it as an IGameInputRawDeviceReport. CreateRawDeviceReport,
GetRawDeviceFeature, SetRawDeviceFeature and SendRawDeviceOutput work through the backend; an undeclared id or kind
is E_INVALIDARG, a failed transfer E_FAIL, and a pad without a HID device keeps E_NOTIMPL.
gameinput_abi.h declares IGameInputRawDeviceReport, GameInputRawDeviceReportKind and the three raw report info
structs, checked against Microsoft's v0 header (offsetof for every field, the 12 vtable slots, the IID). Xbox pads on
Windows go through XInput, whose HID interface SDL's hidapi skips, so they have no raw I/O; the log says so.
gameinput_test covers the descriptor parser and the whole path with a fake HID backend (24 checks);
gameinput_probe prints each pad's report tables and newest raw input report. docs/gameinput.md: "Raw HID".
Diffstat (limited to 'runtime/shims/gameinput.cpp')
| -rw-r--r-- | runtime/shims/gameinput.cpp | 376 |
1 files changed, 358 insertions, 18 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 21924801c..003efdc32 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp | |||
| @@ -52,7 +52,8 @@ namespace { | |||
| 52 | 52 | ||
| 53 | constexpr uint32_t kBaseKinds = | 53 | constexpr uint32_t kBaseKinds = |
| 54 | GameInputKindControllerAxis | GameInputKindControllerButton | GameInputKindGamepad | GameInputKindUiNavigation; | 54 | GameInputKindControllerAxis | GameInputKindControllerButton | GameInputKindGamepad | GameInputKindUiNavigation; |
| 55 | constexpr uint32_t kSupportedKinds = kBaseKinds | GameInputKindMotion; // everything a reading may carry | 55 | // Everything a reading may carry: motion on pads with sensors, raw reports on pads opened through HID (RT-175). |
| 56 | constexpr uint32_t kSupportedKinds = kBaseKinds | GameInputKindMotion | GameInputKindRawDeviceReport; | ||
| 56 | constexpr uint32_t kAxisCount = 6; // LX, LY, RX, RY, LT, RT | 57 | constexpr uint32_t kAxisCount = 6; // LX, LY, RX, RY, LT, RT |
| 57 | constexpr uint32_t kButtonCount = SDL_GAMEPAD_BUTTON_COUNT; | 58 | constexpr uint32_t kButtonCount = SDL_GAMEPAD_BUTTON_COUNT; |
| 58 | constexpr uint32_t kConnectedStatus = GameInputDeviceConnected | GameInputDeviceInputEnabled | GameInputDeviceOutputEnabled; | 59 | constexpr uint32_t kConnectedStatus = GameInputDeviceConnected | GameInputDeviceInputEnabled | GameInputDeviceOutputEnabled; |
| @@ -146,12 +147,18 @@ struct Snapshot { | |||
| 146 | bool guide = false; | 147 | bool guide = false; |
| 147 | GameInputMotionState motion{}; // valid only when has_motion; does not take part in operator== | 148 | GameInputMotionState motion{}; // valid only when has_motion; does not take part in operator== |
| 148 | bool has_motion = false; | 149 | bool has_motion = false; |
| 150 | // The newest raw input report of a pad opened through HID (RT-175): its report id and payload, without the id | ||
| 151 | // byte. A new report with other bytes is a change, so it makes a reading of its own. | ||
| 152 | bool has_raw = false; | ||
| 153 | uint32_t raw_id = 0; | ||
| 154 | std::vector<uint8_t> raw; | ||
| 149 | bool operator==(const Snapshot& o) const { // field by field: the padding is not initialised | 155 | bool operator==(const Snapshot& o) const { // field by field: the padding is not initialised |
| 150 | const GameInputGamepadState &a = gamepad, &b = o.gamepad; | 156 | const GameInputGamepadState &a = gamepad, &b = o.gamepad; |
| 151 | return a.buttons == b.buttons && a.leftTrigger == b.leftTrigger && a.rightTrigger == b.rightTrigger && | 157 | return a.buttons == b.buttons && a.leftTrigger == b.leftTrigger && a.rightTrigger == b.rightTrigger && |
| 152 | a.leftThumbstickX == b.leftThumbstickX && a.leftThumbstickY == b.leftThumbstickY && | 158 | a.leftThumbstickX == b.leftThumbstickX && a.leftThumbstickY == b.leftThumbstickY && |
| 153 | a.rightThumbstickX == b.rightThumbstickX && a.rightThumbstickY == b.rightThumbstickY && ui == o.ui && | 159 | a.rightThumbstickX == b.rightThumbstickX && a.rightThumbstickY == b.rightThumbstickY && ui == o.ui && |
| 154 | std::memcmp(buttons, o.buttons, sizeof buttons) == 0 && guide == o.guide; | 160 | std::memcmp(buttons, o.buttons, sizeof buttons) == 0 && guide == o.guide && has_raw == o.has_raw && |
| 161 | raw_id == o.raw_id && raw == o.raw; | ||
| 155 | } | 162 | } |
| 156 | }; | 163 | }; |
| 157 | 164 | ||
| @@ -178,15 +185,13 @@ public: | |||
| 178 | } | 185 | } |
| 179 | 186 | ||
| 180 | uint32_t STDMETHODCALLTYPE GetInputKind() override { | 187 | uint32_t STDMETHODCALLTYPE GetInputKind() override { |
| 181 | return s_.has_motion ? kSupportedKinds : kBaseKinds; // a reading of a pad without sensors carries no motion | 188 | // A reading of a pad without sensors carries no motion, and one without a raw report no raw report. |
| 189 | return kBaseKinds | (s_.has_motion ? GameInputKindMotion : 0u) | (s_.has_raw ? GameInputKindRawDeviceReport : 0u); | ||
| 182 | } | 190 | } |
| 183 | uint64_t STDMETHODCALLTYPE GetSequenceNumber(uint32_t) override { return seq_; } | 191 | uint64_t STDMETHODCALLTYPE GetSequenceNumber(uint32_t) override { return seq_; } |
| 184 | uint64_t STDMETHODCALLTYPE GetTimestamp() override { return ts_; } | 192 | uint64_t STDMETHODCALLTYPE GetTimestamp() override { return ts_; } |
| 185 | void STDMETHODCALLTYPE GetDevice(IGameInputDevice** device) override; | 193 | void STDMETHODCALLTYPE GetDevice(IGameInputDevice** device) override; |
| 186 | bool STDMETHODCALLTYPE GetRawReport(void** report) override { | 194 | bool STDMETHODCALLTYPE GetRawReport(void** report) override; |
| 187 | if (report) *report = nullptr; | ||
| 188 | return false; | ||
| 189 | } | ||
| 190 | uint32_t STDMETHODCALLTYPE GetControllerAxisCount() override { return kAxisCount; } | 195 | uint32_t STDMETHODCALLTYPE GetControllerAxisCount() override { return kAxisCount; } |
| 191 | uint32_t STDMETHODCALLTYPE GetControllerAxisState(uint32_t count, float* out) override { | 196 | uint32_t STDMETHODCALLTYPE GetControllerAxisState(uint32_t count, float* out) override { |
| 192 | const uint32_t n = out ? std::min(count, kAxisCount) : 0; | 197 | const uint32_t n = out ? std::min(count, kAxisCount) : 0; |
| @@ -271,16 +276,11 @@ public: | |||
| 271 | void STDMETHODCALLTYPE SetInputSynchronizationState(bool) override {} | 276 | void STDMETHODCALLTYPE SetInputSynchronizationState(bool) override {} |
| 272 | void STDMETHODCALLTYPE SendInputSynchronizationHint() override {} | 277 | void STDMETHODCALLTYPE SendInputSynchronizationHint() override {} |
| 273 | void STDMETHODCALLTYPE PowerOff() override {} | 278 | void STDMETHODCALLTYPE PowerOff() override {} |
| 274 | HRESULT STDMETHODCALLTYPE CreateRawDeviceReport(uint32_t, int32_t, void** report) override { | 279 | // Raw HID (RT-175): reports of the pad's HID device through rt::gameinput::HidBackend (SDL_hid_* by default). |
| 275 | if (report) *report = nullptr; | 280 | HRESULT STDMETHODCALLTYPE CreateRawDeviceReport(uint32_t reportId, int32_t reportKind, void** report) override; |
| 276 | return E_NOTIMPL; | 281 | HRESULT STDMETHODCALLTYPE GetRawDeviceFeature(uint32_t reportId, void** report) override; |
| 277 | } | 282 | HRESULT STDMETHODCALLTYPE SetRawDeviceFeature(void* report) override; |
| 278 | HRESULT STDMETHODCALLTYPE GetRawDeviceFeature(uint32_t, void** report) override { | 283 | HRESULT STDMETHODCALLTYPE SendRawDeviceOutput(void* report) override; |
| 279 | if (report) *report = nullptr; | ||
| 280 | return E_NOTIMPL; | ||
| 281 | } | ||
| 282 | HRESULT STDMETHODCALLTYPE SetRawDeviceFeature(void*) override { return E_NOTIMPL; } | ||
| 283 | HRESULT STDMETHODCALLTYPE SendRawDeviceOutput(void*) override { return E_NOTIMPL; } | ||
| 284 | HRESULT STDMETHODCALLTYPE SendRawDeviceOutputWithResponse(void*, void** response) override { | 284 | HRESULT STDMETHODCALLTYPE SendRawDeviceOutputWithResponse(void*, void** response) override { |
| 285 | if (response) *response = nullptr; | 285 | if (response) *response = nullptr; |
| 286 | return E_NOTIMPL; | 286 | return E_NOTIMPL; |
| @@ -321,6 +321,17 @@ public: | |||
| 321 | uint64_t motors_sent_at = 0, triggers_sent_at = 0, battery_at = 0; | 321 | uint64_t motors_sent_at = 0, triggers_sent_at = 0, battery_at = 0; |
| 322 | bool ds_sent_once = false; | 322 | bool ds_sent_once = false; |
| 323 | 323 | ||
| 324 | // Raw HID (RT-175). hid is the backend's handle, null when the pad has no HID device the backend could open; | ||
| 325 | // hid_mu serialises every call on it, from the poll thread (reads) and the title's threads (output, features). | ||
| 326 | // The report tables come from the HID report descriptor; info.input/output/featureReportInfo point into them. | ||
| 327 | std::mutex hid_mu; | ||
| 328 | void* hid = nullptr; | ||
| 329 | const rt::gameinput::HidBackend* hid_backend = nullptr; // the backend hid was opened with | ||
| 330 | bool hid_ids = false; // the descriptor declares report ids, so an input report starts with its id byte | ||
| 331 | std::vector<GameInputRawDeviceReportInfo> raw_in, raw_out, raw_feature; | ||
| 332 | const GameInputRawDeviceReportInfo* find_raw(int32_t kind, uint32_t report_id) const; | ||
| 333 | HRESULT send_raw(void* report, GameInputRawDeviceReportKind kind); | ||
| 334 | |||
| 324 | private: | 335 | private: |
| 325 | std::atomic<ULONG> refs_{1}; | 336 | std::atomic<ULONG> refs_{1}; |
| 326 | }; | 337 | }; |
| @@ -336,6 +347,179 @@ void Reading::GetDevice(IGameInputDevice** device) { | |||
| 336 | *device = device_; | 347 | *device = device_; |
| 337 | } | 348 | } |
| 338 | 349 | ||
| 350 | // ---- raw HID (RT-175) ------------------------------------------------------------------------------------------- | ||
| 351 | |||
| 352 | // One raw report: its own copy of the report info (so it outlives a reconnect that rebuilds the device's tables) and | ||
| 353 | // payload. There are no item tables (itemCount 0), so the item calls answer false; see docs/gameinput.md "Raw HID". | ||
| 354 | class RawReport final : public IGameInputRawDeviceReport { | ||
| 355 | public: | ||
| 356 | RawReport(Device* device, const GameInputRawDeviceReportInfo& info, const uint8_t* data, size_t size) | ||
| 357 | : device_(device), info_(info), data_(info.size, 0) { | ||
| 358 | info_.itemCount = 0; | ||
| 359 | info_.items = nullptr; | ||
| 360 | if (data) std::memcpy(data_.data(), data, std::min(size, data_.size())); | ||
| 361 | device_->AddRef(); | ||
| 362 | } | ||
| 363 | ~RawReport() { device_->Release(); } | ||
| 364 | |||
| 365 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** out) override { | ||
| 366 | if (!out) return E_POINTER; | ||
| 367 | if (same_guid(riid, IID_IUnknown) || same_guid(riid, IID_IGameInputRawDeviceReport)) { | ||
| 368 | AddRef(); | ||
| 369 | *out = static_cast<IGameInputRawDeviceReport*>(this); | ||
| 370 | return S_OK; | ||
| 371 | } | ||
| 372 | *out = nullptr; | ||
| 373 | return E_NOINTERFACE; | ||
| 374 | } | ||
| 375 | ULONG STDMETHODCALLTYPE AddRef() override { return ++refs_; } | ||
| 376 | ULONG STDMETHODCALLTYPE Release() override { | ||
| 377 | const ULONG r = --refs_; | ||
| 378 | if (r == 0) delete this; | ||
| 379 | return r; | ||
| 380 | } | ||
| 381 | |||
| 382 | void STDMETHODCALLTYPE GetDevice(IGameInputDevice** device) override; | ||
| 383 | const GameInputRawDeviceReportInfo* STDMETHODCALLTYPE GetReportInfo() override { return &info_; } | ||
| 384 | size_t STDMETHODCALLTYPE GetRawDataSize() override { return data_.size(); } | ||
| 385 | size_t STDMETHODCALLTYPE GetRawData(size_t bufferSize, void* buffer) override { | ||
| 386 | const size_t n = buffer ? std::min(bufferSize, data_.size()) : 0; | ||
| 387 | if (n) std::memcpy(buffer, data_.data(), n); | ||
| 388 | return n; | ||
| 389 | } | ||
| 390 | // The whole payload at once: a buffer of another size is refused, so a report is never half old, half new. | ||
| 391 | bool STDMETHODCALLTYPE SetRawData(size_t bufferSize, const void* buffer) override { | ||
| 392 | if (!buffer || bufferSize != data_.size()) return false; | ||
| 393 | std::memcpy(data_.data(), buffer, bufferSize); | ||
| 394 | return true; | ||
| 395 | } | ||
| 396 | bool STDMETHODCALLTYPE GetItemValue(uint32_t, int64_t* value) override { | ||
| 397 | if (value) *value = 0; | ||
| 398 | return false; | ||
| 399 | } | ||
| 400 | bool STDMETHODCALLTYPE SetItemValue(uint32_t, int64_t) override { return false; } | ||
| 401 | bool STDMETHODCALLTYPE ResetItemValue(uint32_t) override { return false; } | ||
| 402 | bool STDMETHODCALLTYPE ResetAllItems() override { return false; } | ||
| 403 | |||
| 404 | private: | ||
| 405 | std::atomic<ULONG> refs_{1}; | ||
| 406 | Device* device_; | ||
| 407 | GameInputRawDeviceReportInfo info_; | ||
| 408 | std::vector<uint8_t> data_; | ||
| 409 | }; | ||
| 410 | |||
| 411 | void RawReport::GetDevice(IGameInputDevice** device) { | ||
| 412 | if (!device) return; | ||
| 413 | device_->AddRef(); | ||
| 414 | *device = device_; | ||
| 415 | } | ||
| 416 | |||
| 417 | // The default backend: SDL's hidapi. A pad is opened by the path SDL reports for it; with no such HID path, by its | ||
| 418 | // vendor and product id when exactly one game-pad collection (usage page 1, usage 5) matches, so twins never mix. | ||
| 419 | // A pad without a path (SDL virtual joysticks, the keyboard pad) gets no HID device. | ||
| 420 | void* sdl_hid_open(const char* path, uint16_t vendor, uint16_t product) { | ||
| 421 | if (!path || !*path) return nullptr; | ||
| 422 | static const bool ok = SDL_hid_init() == 0; | ||
| 423 | if (!ok) return nullptr; | ||
| 424 | if (SDL_hid_device* h = SDL_hid_open_path(path)) return h; | ||
| 425 | SDL_hid_device_info* list = SDL_hid_enumerate(vendor, product); | ||
| 426 | const char* only = nullptr; | ||
| 427 | int matches = 0; | ||
| 428 | for (SDL_hid_device_info* i = list; i; i = i->next) | ||
| 429 | if (i->usage_page == 0x01 && i->usage == 0x05 && ++matches == 1) only = i->path; | ||
| 430 | SDL_hid_device* h = (matches == 1 && only) ? SDL_hid_open_path(only) : nullptr; | ||
| 431 | SDL_hid_free_enumeration(list); | ||
| 432 | return h; | ||
| 433 | } | ||
| 434 | void sdl_hid_close(void* h) { SDL_hid_close(static_cast<SDL_hid_device*>(h)); } | ||
| 435 | int sdl_hid_descriptor(void* h, uint8_t* buf, size_t size) { | ||
| 436 | return SDL_hid_get_report_descriptor(static_cast<SDL_hid_device*>(h), buf, size); | ||
| 437 | } | ||
| 438 | int sdl_hid_read(void* h, uint8_t* buf, size_t size) { | ||
| 439 | return SDL_hid_read_timeout(static_cast<SDL_hid_device*>(h), buf, size, 0); | ||
| 440 | } | ||
| 441 | int sdl_hid_write(void* h, const uint8_t* buf, size_t size) { | ||
| 442 | return SDL_hid_write(static_cast<SDL_hid_device*>(h), buf, size); | ||
| 443 | } | ||
| 444 | int sdl_hid_get_feature(void* h, uint8_t* buf, size_t size) { | ||
| 445 | return SDL_hid_get_feature_report(static_cast<SDL_hid_device*>(h), buf, size); | ||
| 446 | } | ||
| 447 | int sdl_hid_send_feature(void* h, const uint8_t* buf, size_t size) { | ||
| 448 | return SDL_hid_send_feature_report(static_cast<SDL_hid_device*>(h), buf, size); | ||
| 449 | } | ||
| 450 | const rt::gameinput::HidBackend kSdlHid = {sdl_hid_open, sdl_hid_close, sdl_hid_descriptor, sdl_hid_read, | ||
| 451 | sdl_hid_write, sdl_hid_get_feature, sdl_hid_send_feature}; | ||
| 452 | std::atomic<const rt::gameinput::HidBackend*> g_hid{&kSdlHid}; | ||
| 453 | |||
| 454 | bool Reading::GetRawReport(void** report) { | ||
| 455 | if (!report) return false; | ||
| 456 | *report = nullptr; | ||
| 457 | if (!s_.has_raw) return false; | ||
| 458 | const GameInputRawDeviceReportInfo* info = device_->find_raw(GameInputRawInputReport, s_.raw_id); | ||
| 459 | if (!info) return false; | ||
| 460 | *report = static_cast<IGameInputRawDeviceReport*>(new RawReport(device_, *info, s_.raw.data(), s_.raw.size())); | ||
| 461 | return true; | ||
| 462 | } | ||
| 463 | |||
| 464 | const GameInputRawDeviceReportInfo* Device::find_raw(int32_t kind, uint32_t report_id) const { | ||
| 465 | const std::vector<GameInputRawDeviceReportInfo>* list = kind == GameInputRawInputReport ? &raw_in | ||
| 466 | : kind == GameInputRawOutputReport ? &raw_out | ||
| 467 | : kind == GameInputRawFeatureReport ? &raw_feature | ||
| 468 | : nullptr; | ||
| 469 | if (!list) return nullptr; | ||
| 470 | for (const GameInputRawDeviceReportInfo& r : *list) | ||
| 471 | if (r.id == report_id) return &r; | ||
| 472 | return nullptr; | ||
| 473 | } | ||
| 474 | |||
| 475 | HRESULT Device::CreateRawDeviceReport(uint32_t reportId, int32_t reportKind, void** report) { | ||
| 476 | if (!report) return E_POINTER; | ||
| 477 | *report = nullptr; | ||
| 478 | std::lock_guard lock(hid_mu); | ||
| 479 | if (!hid) return E_NOTIMPL; // no raw I/O on this pad (GameInputDeviceRawIoEnabled is not set) | ||
| 480 | const GameInputRawDeviceReportInfo* ri = find_raw(reportKind, reportId); | ||
| 481 | if (!ri) return E_INVALIDARG; | ||
| 482 | *report = static_cast<IGameInputRawDeviceReport*>(new RawReport(this, *ri, nullptr, 0)); | ||
| 483 | return S_OK; | ||
| 484 | } | ||
| 485 | |||
| 486 | HRESULT Device::GetRawDeviceFeature(uint32_t reportId, void** report) { | ||
| 487 | if (!report) return E_POINTER; | ||
| 488 | *report = nullptr; | ||
| 489 | std::lock_guard lock(hid_mu); | ||
| 490 | if (!hid) return E_NOTIMPL; | ||
| 491 | const GameInputRawDeviceReportInfo* ri = find_raw(GameInputRawFeatureReport, reportId); | ||
| 492 | if (!ri) return E_INVALIDARG; | ||
| 493 | std::vector<uint8_t> buf(ri->size + 1, 0); | ||
| 494 | buf[0] = static_cast<uint8_t>(reportId); // hidapi: the id goes first, 0 for a device without ids | ||
| 495 | const int n = hid_backend->get_feature(hid, buf.data(), buf.size()); | ||
| 496 | if (n < 1) return E_FAIL; | ||
| 497 | *report = static_cast<IGameInputRawDeviceReport*>(new RawReport(this, *ri, buf.data() + 1, static_cast<size_t>(n) - 1)); | ||
| 498 | return S_OK; | ||
| 499 | } | ||
| 500 | |||
| 501 | // SetRawDeviceFeature and SendRawDeviceOutput: any IGameInputRawDeviceReport of the right kind whose id this pad | ||
| 502 | // declares; the payload is sent with the id byte in front, as hidapi takes it. | ||
| 503 | HRESULT Device::send_raw(void* report, GameInputRawDeviceReportKind kind) { | ||
| 504 | if (!report) return E_INVALIDARG; | ||
| 505 | auto* r = static_cast<IGameInputRawDeviceReport*>(report); | ||
| 506 | const GameInputRawDeviceReportInfo* given = r->GetReportInfo(); | ||
| 507 | std::lock_guard lock(hid_mu); | ||
| 508 | if (!hid) return E_NOTIMPL; | ||
| 509 | if (!given || given->kind != kind) return E_INVALIDARG; | ||
| 510 | const GameInputRawDeviceReportInfo* ri = find_raw(kind, given->id); | ||
| 511 | if (!ri) return E_INVALIDARG; | ||
| 512 | std::vector<uint8_t> buf(ri->size + 1, 0); | ||
| 513 | buf[0] = static_cast<uint8_t>(ri->id); | ||
| 514 | r->GetRawData(ri->size, buf.data() + 1); | ||
| 515 | const rt::gameinput::HidBackend* b = hid_backend; | ||
| 516 | const int n = kind == GameInputRawFeatureReport ? b->send_feature(hid, buf.data(), buf.size()) | ||
| 517 | : b->write(hid, buf.data(), buf.size()); | ||
| 518 | return n < 0 ? E_FAIL : S_OK; | ||
| 519 | } | ||
| 520 | HRESULT Device::SetRawDeviceFeature(void* report) { return send_raw(report, GameInputRawFeatureReport); } | ||
| 521 | HRESULT Device::SendRawDeviceOutput(void* report) { return send_raw(report, GameInputRawOutputReport); } | ||
| 522 | |||
| 339 | // ---- callbacks -------------------------------------------------------------------------------------------------- | 523 | // ---- callbacks -------------------------------------------------------------------------------------------------- |
| 340 | 524 | ||
| 341 | enum class CbType { Device, Reading, Guide, KeyboardLayout }; | 525 | enum class CbType { Device, Reading, Guide, KeyboardLayout }; |
| @@ -651,6 +835,92 @@ Reading* push_reading(Device* d, const Snapshot& snap, uint64_t ts) { | |||
| 651 | return h.back(); | 835 | return h.back(); |
| 652 | } | 836 | } |
| 653 | 837 | ||
| 838 | // Raw HID (RT-175): opens the pad's HID device with the current backend and builds the report tables from its report | ||
| 839 | // descriptor. A pad the backend cannot open, or whose descriptor cannot be read or declares no report, gets no raw | ||
| 840 | // I/O: no GameInputKindRawDeviceReport, no report tables, and E_NOTIMPL from the raw device calls. Under g().mu. | ||
| 841 | void open_raw(Device* d, const char* path) { | ||
| 842 | std::lock_guard lock(d->hid_mu); | ||
| 843 | if (d->hid) d->hid_backend->close(d->hid); | ||
| 844 | d->hid = nullptr; | ||
| 845 | d->hid_ids = false; | ||
| 846 | d->raw_in.clear(); | ||
| 847 | d->raw_out.clear(); | ||
| 848 | d->raw_feature.clear(); | ||
| 849 | const rt::gameinput::HidBackend* b = g_hid.load(); | ||
| 850 | void* h = b->open(path, d->info.vendorId, d->info.productId); | ||
| 851 | if (!h) { | ||
| 852 | // SDL reads Xbox pads on Windows through XInput ("XInput#n"); SDL's hidapi skips their XInput HID interface | ||
| 853 | // (&IG_ in the path), which carries no full HID reports, so they have no raw I/O here. | ||
| 854 | const bool xinput = path && std::strncmp(path, "XInput#", 7) == 0; | ||
| 855 | std::fprintf(stderr, "[rt] gameinput: %s: no raw HID (%s)\n", d->name.c_str(), | ||
| 856 | xinput ? "an XInput pad: SDL's hidapi does not open XInput HID interfaces" | ||
| 857 | : path && *path ? "its HID device could not be opened" | ||
| 858 | : "SDL reports no device path"); | ||
| 859 | return; | ||
| 860 | } | ||
| 861 | std::vector<uint8_t> desc(4096); | ||
| 862 | const int n = b->descriptor(h, desc.data(), desc.size()); | ||
| 863 | std::vector<rt::gameinput::RawReportSize> reports; | ||
| 864 | const bool parsed = n > 0 && rt::gameinput::parse_report_descriptor(desc.data(), static_cast<size_t>(n), &reports); | ||
| 865 | if (!parsed || reports.empty()) { | ||
| 866 | std::fprintf(stderr, "[rt] gameinput: %s: no raw HID (%s)\n", d->name.c_str(), | ||
| 867 | n <= 0 ? "no report descriptor" : !parsed ? "malformed report descriptor" : "no reports declared"); | ||
| 868 | b->close(h); | ||
| 869 | return; | ||
| 870 | } | ||
| 871 | for (const rt::gameinput::RawReportSize& r : reports) { | ||
| 872 | const GameInputRawDeviceReportInfo info{static_cast<GameInputRawDeviceReportKind>(r.kind), r.id, r.bytes, 0, nullptr}; | ||
| 873 | (r.kind == GameInputRawInputReport ? d->raw_in : r.kind == GameInputRawOutputReport ? d->raw_out : d->raw_feature) | ||
| 874 | .push_back(info); | ||
| 875 | d->hid_ids |= r.id != 0; | ||
| 876 | } | ||
| 877 | d->hid = h; | ||
| 878 | d->hid_backend = b; | ||
| 879 | GameInputDeviceInfo& i = d->info; | ||
| 880 | i.supportedInput |= GameInputKindRawDeviceReport; | ||
| 881 | i.inputReportCount = static_cast<uint32_t>(d->raw_in.size()); | ||
| 882 | i.inputReportInfo = d->raw_in.empty() ? nullptr : d->raw_in.data(); | ||
| 883 | i.outputReportCount = static_cast<uint32_t>(d->raw_out.size()); | ||
| 884 | i.outputReportInfo = d->raw_out.empty() ? nullptr : d->raw_out.data(); | ||
| 885 | i.featureReportCount = static_cast<uint32_t>(d->raw_feature.size()); | ||
| 886 | i.featureReportInfo = d->raw_feature.empty() ? nullptr : d->raw_feature.data(); | ||
| 887 | } | ||
| 888 | |||
| 889 | void close_raw(Device* d) { | ||
| 890 | std::lock_guard lock(d->hid_mu); | ||
| 891 | if (d->hid) d->hid_backend->close(d->hid); | ||
| 892 | d->hid = nullptr; | ||
| 893 | } | ||
| 894 | |||
| 895 | // Drains the input reports pending since the last poll; the newest one the pad declares goes into snap. With none | ||
| 896 | // pending, snap keeps the previous report, so a quiet pad makes no new reading. Poll thread. | ||
| 897 | void read_raw(Device* d, Snapshot* snap) { | ||
| 898 | snap->has_raw = d->last_snap.has_raw; | ||
| 899 | snap->raw_id = d->last_snap.raw_id; | ||
| 900 | snap->raw = d->last_snap.raw; | ||
| 901 | std::lock_guard lock(d->hid_mu); | ||
| 902 | if (!d->hid) return; | ||
| 903 | std::vector<uint8_t> buf(4096); | ||
| 904 | for (int k = 0; k < 64; ++k) { // bounded: a pad that floods cannot hold up the poll | ||
| 905 | const int n = d->hid_backend->read(d->hid, buf.data(), buf.size()); | ||
| 906 | if (n <= 0) break; | ||
| 907 | uint32_t id = 0; | ||
| 908 | const uint8_t* p = buf.data(); | ||
| 909 | size_t len = static_cast<size_t>(n); | ||
| 910 | if (d->hid_ids) { | ||
| 911 | id = buf[0]; | ||
| 912 | ++p; | ||
| 913 | --len; | ||
| 914 | } | ||
| 915 | const GameInputRawDeviceReportInfo* info = d->find_raw(GameInputRawInputReport, id); | ||
| 916 | if (!info) continue; | ||
| 917 | snap->has_raw = true; | ||
| 918 | snap->raw_id = id; | ||
| 919 | snap->raw.assign(p, p + std::min(len, static_cast<size_t>(info->size))); | ||
| 920 | snap->raw.resize(info->size, 0); | ||
| 921 | } | ||
| 922 | } | ||
| 923 | |||
| 654 | void open_pad(SDL_JoystickID jid, uint64_t ts) { | 924 | void open_pad(SDL_JoystickID jid, uint64_t ts) { |
| 655 | State& s = g(); | 925 | State& s = g(); |
| 656 | SDL_Gamepad* p = SDL_OpenGamepad(jid); | 926 | SDL_Gamepad* p = SDL_OpenGamepad(jid); |
| @@ -710,6 +980,7 @@ void open_pad(SDL_JoystickID jid, uint64_t ts) { | |||
| 710 | d->motors_sent_at = d->triggers_sent_at = d->battery_at = 0; | 980 | d->motors_sent_at = d->triggers_sent_at = d->battery_at = 0; |
| 711 | d->wanted = {}; | 981 | d->wanted = {}; |
| 712 | fill_info(d, p, id); | 982 | fill_info(d, p, id); |
| 983 | open_raw(d, path); | ||
| 713 | 984 | ||
| 714 | // Player LEDs and SDL's default lightbar colour follow the slot: the lowest one not in use. | 985 | // Player LEDs and SDL's default lightbar colour follow the slot: the lowest one not in use. |
| 715 | int player = 0; | 986 | int player = 0; |
| @@ -725,6 +996,7 @@ void open_pad(SDL_JoystickID jid, uint64_t ts) { | |||
| 725 | 996 | ||
| 726 | uint32_t status = kConnectedStatus; | 997 | uint32_t status = kConnectedStatus; |
| 727 | if (d->info.capabilities & GameInputDeviceCapabilityWireless) status |= GameInputDeviceWireless; | 998 | if (d->info.capabilities & GameInputDeviceCapabilityWireless) status |= GameInputDeviceWireless; |
| 999 | if (d->hid) status |= GameInputDeviceRawIoEnabled; | ||
| 728 | d->status = status; | 1000 | d->status = status; |
| 729 | s.connected.push_back(d); | 1001 | s.connected.push_back(d); |
| 730 | 1002 | ||
| @@ -742,6 +1014,7 @@ void close_pad(Device* d, uint64_t ts) { | |||
| 742 | std::lock_guard lock(s.mu); | 1014 | std::lock_guard lock(s.mu); |
| 743 | const uint32_t old = d->status.exchange(GameInputDeviceNoStatus); | 1015 | const uint32_t old = d->status.exchange(GameInputDeviceNoStatus); |
| 744 | SDL_CloseGamepad(d->pad); | 1016 | SDL_CloseGamepad(d->pad); |
| 1017 | close_raw(d); | ||
| 745 | d->pad = nullptr; | 1018 | d->pad = nullptr; |
| 746 | d->player = -1; | 1019 | d->player = -1; |
| 747 | if (!d->history.empty()) d->evicted_order = d->history.back()->order(); // the history ends with the session | 1020 | if (!d->history.empty()) d->evicted_order = d->history.back()->order(); // the history ends with the session |
| @@ -1151,7 +1424,8 @@ void poll_once() { | |||
| 1151 | connected = s.connected; | 1424 | connected = s.connected; |
| 1152 | } | 1425 | } |
| 1153 | for (Device* d : connected) { | 1426 | for (Device* d : connected) { |
| 1154 | const Snapshot snap = read_pad(d); | 1427 | Snapshot snap = read_pad(d); |
| 1428 | read_raw(d, &snap); | ||
| 1155 | const bool guide_changed = d->last_snap.guide != snap.guide; | 1429 | const bool guide_changed = d->last_snap.guide != snap.guide; |
| 1156 | const bool changed = !(d->last_snap == snap); | 1430 | const bool changed = !(d->last_snap == snap); |
| 1157 | d->last_snap = snap; | 1431 | d->last_snap = snap; |
| @@ -1612,6 +1886,72 @@ const KeyBinding* keyboard_bindings(int* count) { | |||
| 1612 | return kKeyBindings; | 1886 | return kKeyBindings; |
| 1613 | } | 1887 | } |
| 1614 | 1888 | ||
| 1889 | // HID 1.11 section 6.2.2: a short item is one prefix byte (bSize in bits 0-1, 3 meaning 4 bytes; bType in bits 2-3; | ||
| 1890 | // bTag in bits 4-7) and up to four data bytes, little-endian. 0xFE starts a long item (data size, tag, data). | ||
| 1891 | bool parse_report_descriptor(const uint8_t* data, size_t size, std::vector<RawReportSize>* out) { | ||
| 1892 | if (!out || (!data && size)) return false; | ||
| 1893 | out->clear(); | ||
| 1894 | struct Globals { | ||
| 1895 | uint32_t report_size = 0, report_count = 0, report_id = 0; | ||
| 1896 | }; | ||
| 1897 | Globals g; | ||
| 1898 | std::vector<Globals> stack; | ||
| 1899 | struct Key { | ||
| 1900 | int kind; | ||
| 1901 | uint32_t id; | ||
| 1902 | uint64_t bits; | ||
| 1903 | }; | ||
| 1904 | std::vector<Key> reports; // first-seen order; sorted at the end | ||
| 1905 | size_t i = 0; | ||
| 1906 | while (i < size) { | ||
| 1907 | const uint8_t prefix = data[i++]; | ||
| 1908 | if (prefix == 0xFE) { // long item: bDataSize, bLongItemTag, data | ||
| 1909 | if (i + 2 > size) return false; | ||
| 1910 | i += 2 + data[i]; | ||
| 1911 | if (i > size) return false; | ||
| 1912 | continue; | ||
| 1913 | } | ||
| 1914 | const size_t n = (prefix & 3) == 3 ? 4 : (prefix & 3); | ||
| 1915 | if (i + n > size) return false; | ||
| 1916 | uint32_t v = 0; | ||
| 1917 | for (size_t k = 0; k < n; ++k) v |= static_cast<uint32_t>(data[i + k]) << (8 * k); | ||
| 1918 | i += n; | ||
| 1919 | const int type = (prefix >> 2) & 3, tag = prefix >> 4; | ||
| 1920 | if (type == 1) { // global | ||
| 1921 | if (tag == 7) g.report_size = v; | ||
| 1922 | else if (tag == 8) { | ||
| 1923 | if (v == 0 || v > 255) return false; | ||
| 1924 | g.report_id = v; | ||
| 1925 | } else if (tag == 9) g.report_count = v; | ||
| 1926 | else if (tag == 10) stack.push_back(g); | ||
| 1927 | else if (tag == 11) { | ||
| 1928 | if (stack.empty()) return false; | ||
| 1929 | g = stack.back(); | ||
| 1930 | stack.pop_back(); | ||
| 1931 | } | ||
| 1932 | } else if (type == 0) { // main: Input 8, Output 9, Feature 11 (Collection 10 and End Collection 12 add nothing) | ||
| 1933 | const int kind = tag == 8 ? 0 : tag == 9 ? 1 : tag == 11 ? 2 : -1; | ||
| 1934 | if (kind < 0) continue; | ||
| 1935 | const uint64_t add = static_cast<uint64_t>(g.report_size) * g.report_count; | ||
| 1936 | Key* k = nullptr; | ||
| 1937 | for (Key& r : reports) | ||
| 1938 | if (r.kind == kind && r.id == g.report_id) k = &r; | ||
| 1939 | if (!k) { | ||
| 1940 | reports.push_back({kind, g.report_id, 0}); | ||
| 1941 | k = &reports.back(); | ||
| 1942 | } | ||
| 1943 | k->bits += add; | ||
| 1944 | if (k->bits > 65536ull * 8) return false; | ||
| 1945 | } | ||
| 1946 | } | ||
| 1947 | for (const Key& k : reports) out->push_back({k.kind, k.id, static_cast<uint32_t>((k.bits + 7) / 8)}); | ||
| 1948 | std::sort(out->begin(), out->end(), | ||
| 1949 | [](const RawReportSize& a, const RawReportSize& b) { return a.kind != b.kind ? a.kind < b.kind : a.id < b.id; }); | ||
| 1950 | return true; | ||
| 1951 | } | ||
| 1952 | |||
| 1953 | void set_hid_backend(const HidBackend* backend) { g_hid = backend ? backend : &kSdlHid; } | ||
| 1954 | |||
| 1615 | } // namespace rt::gameinput | 1955 | } // namespace rt::gameinput |
| 1616 | 1956 | ||
| 1617 | SHIM_TABLE(gameinput, | 1957 | SHIM_TABLE(gameinput, |