diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 15:15:26 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 15:15:26 -0500 |
| commit | c77e9403a3343f3b3fbd8fb6a1b1520788722653 (patch) | |
| tree | 3bad1b351788e14254c0d388504690173e2b8d1f /runtime/shims/gameinput.cpp | |
| parent | 30757f16b6e3ca01a5c266749a0028ae638327f0 (diff) | |
| parent | 76d1add3dab43a082332579ca33f3f039746ce6b (diff) | |
Merge pull request #3172 from Bora Sevim
# Conflicts:
# docs/gameinput.md
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 a6940f6ad..69707f75a 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp | |||
| @@ -56,7 +56,8 @@ namespace { | |||
| 56 | 56 | ||
| 57 | constexpr uint32_t kBaseKinds = | 57 | constexpr uint32_t kBaseKinds = |
| 58 | GameInputKindControllerAxis | GameInputKindControllerButton | GameInputKindGamepad | GameInputKindUiNavigation; | 58 | GameInputKindControllerAxis | GameInputKindControllerButton | GameInputKindGamepad | GameInputKindUiNavigation; |
| 59 | constexpr uint32_t kSupportedKinds = kBaseKinds | GameInputKindMotion; // everything a reading may carry | 59 | // Everything a reading may carry: motion on pads with sensors, raw reports on pads opened through HID (RT-175). |
| 60 | constexpr uint32_t kSupportedKinds = kBaseKinds | GameInputKindMotion | GameInputKindRawDeviceReport; | ||
| 60 | constexpr uint32_t kAxisCount = 6; // LX, LY, RX, RY, LT, RT | 61 | constexpr uint32_t kAxisCount = 6; // LX, LY, RX, RY, LT, RT |
| 61 | constexpr uint32_t kButtonCount = SDL_GAMEPAD_BUTTON_COUNT; | 62 | constexpr uint32_t kButtonCount = SDL_GAMEPAD_BUTTON_COUNT; |
| 62 | constexpr uint32_t kConnectedStatus = GameInputDeviceConnected | GameInputDeviceInputEnabled | GameInputDeviceOutputEnabled; | 63 | constexpr uint32_t kConnectedStatus = GameInputDeviceConnected | GameInputDeviceInputEnabled | GameInputDeviceOutputEnabled; |
| @@ -185,12 +186,18 @@ struct Snapshot { | |||
| 185 | bool guide = false; | 186 | bool guide = false; |
| 186 | GameInputMotionState motion{}; // valid only when has_motion; does not take part in operator== | 187 | GameInputMotionState motion{}; // valid only when has_motion; does not take part in operator== |
| 187 | bool has_motion = false; | 188 | bool has_motion = false; |
| 189 | // The newest raw input report of a pad opened through HID (RT-175): its report id and payload, without the id | ||
| 190 | // byte. A new report with other bytes is a change, so it makes a reading of its own. | ||
| 191 | bool has_raw = false; | ||
| 192 | uint32_t raw_id = 0; | ||
| 193 | std::vector<uint8_t> raw; | ||
| 188 | bool operator==(const Snapshot& o) const { // field by field: the padding is not initialised | 194 | bool operator==(const Snapshot& o) const { // field by field: the padding is not initialised |
| 189 | const GameInputGamepadState &a = gamepad, &b = o.gamepad; | 195 | const GameInputGamepadState &a = gamepad, &b = o.gamepad; |
| 190 | return a.buttons == b.buttons && a.leftTrigger == b.leftTrigger && a.rightTrigger == b.rightTrigger && | 196 | return a.buttons == b.buttons && a.leftTrigger == b.leftTrigger && a.rightTrigger == b.rightTrigger && |
| 191 | a.leftThumbstickX == b.leftThumbstickX && a.leftThumbstickY == b.leftThumbstickY && | 197 | a.leftThumbstickX == b.leftThumbstickX && a.leftThumbstickY == b.leftThumbstickY && |
| 192 | a.rightThumbstickX == b.rightThumbstickX && a.rightThumbstickY == b.rightThumbstickY && ui == o.ui && | 198 | a.rightThumbstickX == b.rightThumbstickX && a.rightThumbstickY == b.rightThumbstickY && ui == o.ui && |
| 193 | std::memcmp(buttons, o.buttons, sizeof buttons) == 0 && guide == o.guide; | 199 | std::memcmp(buttons, o.buttons, sizeof buttons) == 0 && guide == o.guide && has_raw == o.has_raw && |
| 200 | raw_id == o.raw_id && raw == o.raw; | ||
| 194 | } | 201 | } |
| 195 | }; | 202 | }; |
| 196 | 203 | ||
| @@ -217,15 +224,13 @@ public: | |||
| 217 | } | 224 | } |
| 218 | 225 | ||
| 219 | uint32_t STDMETHODCALLTYPE GetInputKind() override { | 226 | uint32_t STDMETHODCALLTYPE GetInputKind() override { |
| 220 | return s_.has_motion ? kSupportedKinds : kBaseKinds; // a reading of a pad without sensors carries no motion | 227 | // A reading of a pad without sensors carries no motion, and one without a raw report no raw report. |
| 228 | return kBaseKinds | (s_.has_motion ? GameInputKindMotion : 0u) | (s_.has_raw ? GameInputKindRawDeviceReport : 0u); | ||
| 221 | } | 229 | } |
| 222 | uint64_t STDMETHODCALLTYPE GetSequenceNumber(uint32_t) override { return seq_; } | 230 | uint64_t STDMETHODCALLTYPE GetSequenceNumber(uint32_t) override { return seq_; } |
| 223 | uint64_t STDMETHODCALLTYPE GetTimestamp() override { return ts_; } | 231 | uint64_t STDMETHODCALLTYPE GetTimestamp() override { return ts_; } |
| 224 | void STDMETHODCALLTYPE GetDevice(IGameInputDevice** device) override; | 232 | void STDMETHODCALLTYPE GetDevice(IGameInputDevice** device) override; |
| 225 | bool STDMETHODCALLTYPE GetRawReport(void** report) override { | 233 | bool STDMETHODCALLTYPE GetRawReport(void** report) override; |
| 226 | if (report) *report = nullptr; | ||
| 227 | return false; | ||
| 228 | } | ||
| 229 | uint32_t STDMETHODCALLTYPE GetControllerAxisCount() override { return kAxisCount; } | 234 | uint32_t STDMETHODCALLTYPE GetControllerAxisCount() override { return kAxisCount; } |
| 230 | uint32_t STDMETHODCALLTYPE GetControllerAxisState(uint32_t count, float* out) override { | 235 | uint32_t STDMETHODCALLTYPE GetControllerAxisState(uint32_t count, float* out) override { |
| 231 | const uint32_t n = out ? std::min(count, kAxisCount) : 0; | 236 | const uint32_t n = out ? std::min(count, kAxisCount) : 0; |
| @@ -450,16 +455,11 @@ public: | |||
| 450 | void STDMETHODCALLTYPE SetInputSynchronizationState(bool) override {} | 455 | void STDMETHODCALLTYPE SetInputSynchronizationState(bool) override {} |
| 451 | void STDMETHODCALLTYPE SendInputSynchronizationHint() override {} | 456 | void STDMETHODCALLTYPE SendInputSynchronizationHint() override {} |
| 452 | void STDMETHODCALLTYPE PowerOff() override {} | 457 | void STDMETHODCALLTYPE PowerOff() override {} |
| 453 | HRESULT STDMETHODCALLTYPE CreateRawDeviceReport(uint32_t, int32_t, void** report) override { | 458 | // Raw HID (RT-175): reports of the pad's HID device through rt::gameinput::HidBackend (SDL_hid_* by default). |
| 454 | if (report) *report = nullptr; | 459 | HRESULT STDMETHODCALLTYPE CreateRawDeviceReport(uint32_t reportId, int32_t reportKind, void** report) override; |
| 455 | return E_NOTIMPL; | 460 | HRESULT STDMETHODCALLTYPE GetRawDeviceFeature(uint32_t reportId, void** report) override; |
| 456 | } | 461 | HRESULT STDMETHODCALLTYPE SetRawDeviceFeature(void* report) override; |
| 457 | HRESULT STDMETHODCALLTYPE GetRawDeviceFeature(uint32_t, void** report) override { | 462 | HRESULT STDMETHODCALLTYPE SendRawDeviceOutput(void* report) override; |
| 458 | if (report) *report = nullptr; | ||
| 459 | return E_NOTIMPL; | ||
| 460 | } | ||
| 461 | HRESULT STDMETHODCALLTYPE SetRawDeviceFeature(void*) override { return E_NOTIMPL; } | ||
| 462 | HRESULT STDMETHODCALLTYPE SendRawDeviceOutput(void*) override { return E_NOTIMPL; } | ||
| 463 | HRESULT STDMETHODCALLTYPE SendRawDeviceOutputWithResponse(void*, void** response) override { | 463 | HRESULT STDMETHODCALLTYPE SendRawDeviceOutputWithResponse(void*, void** response) override { |
| 464 | if (response) *response = nullptr; | 464 | if (response) *response = nullptr; |
| 465 | return E_NOTIMPL; | 465 | return E_NOTIMPL; |
| @@ -513,6 +513,17 @@ public: | |||
| 513 | uint64_t motors_sent_at = 0, triggers_sent_at = 0, battery_at = 0; | 513 | uint64_t motors_sent_at = 0, triggers_sent_at = 0, battery_at = 0; |
| 514 | bool ds_sent_once = false; | 514 | bool ds_sent_once = false; |
| 515 | 515 | ||
| 516 | // Raw HID (RT-175). hid is the backend's handle, null when the pad has no HID device the backend could open; | ||
| 517 | // hid_mu serialises every call on it, from the poll thread (reads) and the title's threads (output, features). | ||
| 518 | // The report tables come from the HID report descriptor; info.input/output/featureReportInfo point into them. | ||
| 519 | std::mutex hid_mu; | ||
| 520 | void* hid = nullptr; | ||
| 521 | const rt::gameinput::HidBackend* hid_backend = nullptr; // the backend hid was opened with | ||
| 522 | bool hid_ids = false; // the descriptor declares report ids, so an input report starts with its id byte | ||
| 523 | std::vector<GameInputRawDeviceReportInfo> raw_in, raw_out, raw_feature; | ||
| 524 | const GameInputRawDeviceReportInfo* find_raw(int32_t kind, uint32_t report_id) const; | ||
| 525 | HRESULT send_raw(void* report, GameInputRawDeviceReportKind kind); | ||
| 526 | |||
| 516 | private: | 527 | private: |
| 517 | std::atomic<ULONG> refs_{1}; | 528 | std::atomic<ULONG> refs_{1}; |
| 518 | }; | 529 | }; |
| @@ -528,6 +539,179 @@ void Reading::GetDevice(IGameInputDevice** device) { | |||
| 528 | *device = device_; | 539 | *device = device_; |
| 529 | } | 540 | } |
| 530 | 541 | ||
| 542 | // ---- raw HID (RT-175) ------------------------------------------------------------------------------------------- | ||
| 543 | |||
| 544 | // One raw report: its own copy of the report info (so it outlives a reconnect that rebuilds the device's tables) and | ||
| 545 | // payload. There are no item tables (itemCount 0), so the item calls answer false; see docs/gameinput.md "Raw HID". | ||
| 546 | class RawReport final : public IGameInputRawDeviceReport { | ||
| 547 | public: | ||
| 548 | RawReport(Device* device, const GameInputRawDeviceReportInfo& info, const uint8_t* data, size_t size) | ||
| 549 | : device_(device), info_(info), data_(info.size, 0) { | ||
| 550 | info_.itemCount = 0; | ||
| 551 | info_.items = nullptr; | ||
| 552 | if (data) std::memcpy(data_.data(), data, std::min(size, data_.size())); | ||
| 553 | device_->AddRef(); | ||
| 554 | } | ||
| 555 | ~RawReport() { device_->Release(); } | ||
| 556 | |||
| 557 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** out) override { | ||
| 558 | if (!out) return E_POINTER; | ||
| 559 | if (same_guid(riid, IID_IUnknown) || same_guid(riid, IID_IGameInputRawDeviceReport)) { | ||
| 560 | AddRef(); | ||
| 561 | *out = static_cast<IGameInputRawDeviceReport*>(this); | ||
| 562 | return S_OK; | ||
| 563 | } | ||
| 564 | *out = nullptr; | ||
| 565 | return E_NOINTERFACE; | ||
| 566 | } | ||
| 567 | ULONG STDMETHODCALLTYPE AddRef() override { return ++refs_; } | ||
| 568 | ULONG STDMETHODCALLTYPE Release() override { | ||
| 569 | const ULONG r = --refs_; | ||
| 570 | if (r == 0) delete this; | ||
| 571 | return r; | ||
| 572 | } | ||
| 573 | |||
| 574 | void STDMETHODCALLTYPE GetDevice(IGameInputDevice** device) override; | ||
| 575 | const GameInputRawDeviceReportInfo* STDMETHODCALLTYPE GetReportInfo() override { return &info_; } | ||
| 576 | size_t STDMETHODCALLTYPE GetRawDataSize() override { return data_.size(); } | ||
| 577 | size_t STDMETHODCALLTYPE GetRawData(size_t bufferSize, void* buffer) override { | ||
| 578 | const size_t n = buffer ? std::min(bufferSize, data_.size()) : 0; | ||
| 579 | if (n) std::memcpy(buffer, data_.data(), n); | ||
| 580 | return n; | ||
| 581 | } | ||
| 582 | // The whole payload at once: a buffer of another size is refused, so a report is never half old, half new. | ||
| 583 | bool STDMETHODCALLTYPE SetRawData(size_t bufferSize, const void* buffer) override { | ||
| 584 | if (!buffer || bufferSize != data_.size()) return false; | ||
| 585 | std::memcpy(data_.data(), buffer, bufferSize); | ||
| 586 | return true; | ||
| 587 | } | ||
| 588 | bool STDMETHODCALLTYPE GetItemValue(uint32_t, int64_t* value) override { | ||
| 589 | if (value) *value = 0; | ||
| 590 | return false; | ||
| 591 | } | ||
| 592 | bool STDMETHODCALLTYPE SetItemValue(uint32_t, int64_t) override { return false; } | ||
| 593 | bool STDMETHODCALLTYPE ResetItemValue(uint32_t) override { return false; } | ||
| 594 | bool STDMETHODCALLTYPE ResetAllItems() override { return false; } | ||
| 595 | |||
| 596 | private: | ||
| 597 | std::atomic<ULONG> refs_{1}; | ||
| 598 | Device* device_; | ||
| 599 | GameInputRawDeviceReportInfo info_; | ||
| 600 | std::vector<uint8_t> data_; | ||
| 601 | }; | ||
| 602 | |||
| 603 | void RawReport::GetDevice(IGameInputDevice** device) { | ||
| 604 | if (!device) return; | ||
| 605 | device_->AddRef(); | ||
| 606 | *device = device_; | ||
| 607 | } | ||
| 608 | |||
| 609 | // The default backend: SDL's hidapi. A pad is opened by the path SDL reports for it; with no such HID path, by its | ||
| 610 | // vendor and product id when exactly one game-pad collection (usage page 1, usage 5) matches, so twins never mix. | ||
| 611 | // A pad without a path (SDL virtual joysticks, the keyboard pad) gets no HID device. | ||
| 612 | void* sdl_hid_open(const char* path, uint16_t vendor, uint16_t product) { | ||
| 613 | if (!path || !*path) return nullptr; | ||
| 614 | static const bool ok = SDL_hid_init() == 0; | ||
| 615 | if (!ok) return nullptr; | ||
| 616 | if (SDL_hid_device* h = SDL_hid_open_path(path)) return h; | ||
| 617 | SDL_hid_device_info* list = SDL_hid_enumerate(vendor, product); | ||
| 618 | const char* only = nullptr; | ||
| 619 | int matches = 0; | ||
| 620 | for (SDL_hid_device_info* i = list; i; i = i->next) | ||
| 621 | if (i->usage_page == 0x01 && i->usage == 0x05 && ++matches == 1) only = i->path; | ||
| 622 | SDL_hid_device* h = (matches == 1 && only) ? SDL_hid_open_path(only) : nullptr; | ||
| 623 | SDL_hid_free_enumeration(list); | ||
| 624 | return h; | ||
| 625 | } | ||
| 626 | void sdl_hid_close(void* h) { SDL_hid_close(static_cast<SDL_hid_device*>(h)); } | ||
| 627 | int sdl_hid_descriptor(void* h, uint8_t* buf, size_t size) { | ||
| 628 | return SDL_hid_get_report_descriptor(static_cast<SDL_hid_device*>(h), buf, size); | ||
| 629 | } | ||
| 630 | int sdl_hid_read(void* h, uint8_t* buf, size_t size) { | ||
| 631 | return SDL_hid_read_timeout(static_cast<SDL_hid_device*>(h), buf, size, 0); | ||
| 632 | } | ||
| 633 | int sdl_hid_write(void* h, const uint8_t* buf, size_t size) { | ||
| 634 | return SDL_hid_write(static_cast<SDL_hid_device*>(h), buf, size); | ||
| 635 | } | ||
| 636 | int sdl_hid_get_feature(void* h, uint8_t* buf, size_t size) { | ||
| 637 | return SDL_hid_get_feature_report(static_cast<SDL_hid_device*>(h), buf, size); | ||
| 638 | } | ||
| 639 | int sdl_hid_send_feature(void* h, const uint8_t* buf, size_t size) { | ||
| 640 | return SDL_hid_send_feature_report(static_cast<SDL_hid_device*>(h), buf, size); | ||
| 641 | } | ||
| 642 | const rt::gameinput::HidBackend kSdlHid = {sdl_hid_open, sdl_hid_close, sdl_hid_descriptor, sdl_hid_read, | ||
| 643 | sdl_hid_write, sdl_hid_get_feature, sdl_hid_send_feature}; | ||
| 644 | std::atomic<const rt::gameinput::HidBackend*> g_hid{&kSdlHid}; | ||
| 645 | |||
| 646 | bool Reading::GetRawReport(void** report) { | ||
| 647 | if (!report) return false; | ||
| 648 | *report = nullptr; | ||
| 649 | if (!s_.has_raw) return false; | ||
| 650 | const GameInputRawDeviceReportInfo* info = device_->find_raw(GameInputRawInputReport, s_.raw_id); | ||
| 651 | if (!info) return false; | ||
| 652 | *report = static_cast<IGameInputRawDeviceReport*>(new RawReport(device_, *info, s_.raw.data(), s_.raw.size())); | ||
| 653 | return true; | ||
| 654 | } | ||
| 655 | |||
| 656 | const GameInputRawDeviceReportInfo* Device::find_raw(int32_t kind, uint32_t report_id) const { | ||
| 657 | const std::vector<GameInputRawDeviceReportInfo>* list = kind == GameInputRawInputReport ? &raw_in | ||
| 658 | : kind == GameInputRawOutputReport ? &raw_out | ||
| 659 | : kind == GameInputRawFeatureReport ? &raw_feature | ||
| 660 | : nullptr; | ||
| 661 | if (!list) return nullptr; | ||
| 662 | for (const GameInputRawDeviceReportInfo& r : *list) | ||
| 663 | if (r.id == report_id) return &r; | ||
| 664 | return nullptr; | ||
| 665 | } | ||
| 666 | |||
| 667 | HRESULT Device::CreateRawDeviceReport(uint32_t reportId, int32_t reportKind, void** report) { | ||
| 668 | if (!report) return E_POINTER; | ||
| 669 | *report = nullptr; | ||
| 670 | std::lock_guard lock(hid_mu); | ||
| 671 | if (!hid) return E_NOTIMPL; // no raw I/O on this pad (GameInputDeviceRawIoEnabled is not set) | ||
| 672 | const GameInputRawDeviceReportInfo* ri = find_raw(reportKind, reportId); | ||
| 673 | if (!ri) return E_INVALIDARG; | ||
| 674 | *report = static_cast<IGameInputRawDeviceReport*>(new RawReport(this, *ri, nullptr, 0)); | ||
| 675 | return S_OK; | ||
| 676 | } | ||
| 677 | |||
| 678 | HRESULT Device::GetRawDeviceFeature(uint32_t reportId, void** report) { | ||
| 679 | if (!report) return E_POINTER; | ||
| 680 | *report = nullptr; | ||
| 681 | std::lock_guard lock(hid_mu); | ||
| 682 | if (!hid) return E_NOTIMPL; | ||
| 683 | const GameInputRawDeviceReportInfo* ri = find_raw(GameInputRawFeatureReport, reportId); | ||
| 684 | if (!ri) return E_INVALIDARG; | ||
| 685 | std::vector<uint8_t> buf(ri->size + 1, 0); | ||
| 686 | buf[0] = static_cast<uint8_t>(reportId); // hidapi: the id goes first, 0 for a device without ids | ||
| 687 | const int n = hid_backend->get_feature(hid, buf.data(), buf.size()); | ||
| 688 | if (n < 1) return E_FAIL; | ||
| 689 | *report = static_cast<IGameInputRawDeviceReport*>(new RawReport(this, *ri, buf.data() + 1, static_cast<size_t>(n) - 1)); | ||
| 690 | return S_OK; | ||
| 691 | } | ||
| 692 | |||
| 693 | // SetRawDeviceFeature and SendRawDeviceOutput: any IGameInputRawDeviceReport of the right kind whose id this pad | ||
| 694 | // declares; the payload is sent with the id byte in front, as hidapi takes it. | ||
| 695 | HRESULT Device::send_raw(void* report, GameInputRawDeviceReportKind kind) { | ||
| 696 | if (!report) return E_INVALIDARG; | ||
| 697 | auto* r = static_cast<IGameInputRawDeviceReport*>(report); | ||
| 698 | const GameInputRawDeviceReportInfo* given = r->GetReportInfo(); | ||
| 699 | std::lock_guard lock(hid_mu); | ||
| 700 | if (!hid) return E_NOTIMPL; | ||
| 701 | if (!given || given->kind != kind) return E_INVALIDARG; | ||
| 702 | const GameInputRawDeviceReportInfo* ri = find_raw(kind, given->id); | ||
| 703 | if (!ri) return E_INVALIDARG; | ||
| 704 | std::vector<uint8_t> buf(ri->size + 1, 0); | ||
| 705 | buf[0] = static_cast<uint8_t>(ri->id); | ||
| 706 | r->GetRawData(ri->size, buf.data() + 1); | ||
| 707 | const rt::gameinput::HidBackend* b = hid_backend; | ||
| 708 | const int n = kind == GameInputRawFeatureReport ? b->send_feature(hid, buf.data(), buf.size()) | ||
| 709 | : b->write(hid, buf.data(), buf.size()); | ||
| 710 | return n < 0 ? E_FAIL : S_OK; | ||
| 711 | } | ||
| 712 | HRESULT Device::SetRawDeviceFeature(void* report) { return send_raw(report, GameInputRawFeatureReport); } | ||
| 713 | HRESULT Device::SendRawDeviceOutput(void* report) { return send_raw(report, GameInputRawOutputReport); } | ||
| 714 | |||
| 531 | // ---- callbacks -------------------------------------------------------------------------------------------------- | 715 | // ---- callbacks -------------------------------------------------------------------------------------------------- |
| 532 | 716 | ||
| 533 | enum class CbType { Device, Reading, Guide, KeyboardLayout }; | 717 | enum class CbType { Device, Reading, Guide, KeyboardLayout }; |
| @@ -931,6 +1115,92 @@ Reading* push_reading(Device* d, const Snapshot& snap, uint64_t ts) { | |||
| 931 | return h.back(); | 1115 | return h.back(); |
| 932 | } | 1116 | } |
| 933 | 1117 | ||
| 1118 | // Raw HID (RT-175): opens the pad's HID device with the current backend and builds the report tables from its report | ||
| 1119 | // descriptor. A pad the backend cannot open, or whose descriptor cannot be read or declares no report, gets no raw | ||
| 1120 | // I/O: no GameInputKindRawDeviceReport, no report tables, and E_NOTIMPL from the raw device calls. Under g().mu. | ||
| 1121 | void open_raw(Device* d, const char* path) { | ||
| 1122 | std::lock_guard lock(d->hid_mu); | ||
| 1123 | if (d->hid) d->hid_backend->close(d->hid); | ||
| 1124 | d->hid = nullptr; | ||
| 1125 | d->hid_ids = false; | ||
| 1126 | d->raw_in.clear(); | ||
| 1127 | d->raw_out.clear(); | ||
| 1128 | d->raw_feature.clear(); | ||
| 1129 | const rt::gameinput::HidBackend* b = g_hid.load(); | ||
| 1130 | void* h = b->open(path, d->info.vendorId, d->info.productId); | ||
| 1131 | if (!h) { | ||
| 1132 | // SDL reads Xbox pads on Windows through XInput ("XInput#n"); SDL's hidapi skips their XInput HID interface | ||
| 1133 | // (&IG_ in the path), which carries no full HID reports, so they have no raw I/O here. | ||
| 1134 | const bool xinput = path && std::strncmp(path, "XInput#", 7) == 0; | ||
| 1135 | std::fprintf(stderr, "[rt] gameinput: %s: no raw HID (%s)\n", d->name.c_str(), | ||
| 1136 | xinput ? "an XInput pad: SDL's hidapi does not open XInput HID interfaces" | ||
| 1137 | : path && *path ? "its HID device could not be opened" | ||
| 1138 | : "SDL reports no device path"); | ||
| 1139 | return; | ||
| 1140 | } | ||
| 1141 | std::vector<uint8_t> desc(4096); | ||
| 1142 | const int n = b->descriptor(h, desc.data(), desc.size()); | ||
| 1143 | std::vector<rt::gameinput::RawReportSize> reports; | ||
| 1144 | const bool parsed = n > 0 && rt::gameinput::parse_report_descriptor(desc.data(), static_cast<size_t>(n), &reports); | ||
| 1145 | if (!parsed || reports.empty()) { | ||
| 1146 | std::fprintf(stderr, "[rt] gameinput: %s: no raw HID (%s)\n", d->name.c_str(), | ||
| 1147 | n <= 0 ? "no report descriptor" : !parsed ? "malformed report descriptor" : "no reports declared"); | ||
| 1148 | b->close(h); | ||
| 1149 | return; | ||
| 1150 | } | ||
| 1151 | for (const rt::gameinput::RawReportSize& r : reports) { | ||
| 1152 | const GameInputRawDeviceReportInfo info{static_cast<GameInputRawDeviceReportKind>(r.kind), r.id, r.bytes, 0, nullptr}; | ||
| 1153 | (r.kind == GameInputRawInputReport ? d->raw_in : r.kind == GameInputRawOutputReport ? d->raw_out : d->raw_feature) | ||
| 1154 | .push_back(info); | ||
| 1155 | d->hid_ids |= r.id != 0; | ||
| 1156 | } | ||
| 1157 | d->hid = h; | ||
| 1158 | d->hid_backend = b; | ||
| 1159 | GameInputDeviceInfo& i = d->info; | ||
| 1160 | i.supportedInput |= GameInputKindRawDeviceReport; | ||
| 1161 | i.inputReportCount = static_cast<uint32_t>(d->raw_in.size()); | ||
| 1162 | i.inputReportInfo = d->raw_in.empty() ? nullptr : d->raw_in.data(); | ||
| 1163 | i.outputReportCount = static_cast<uint32_t>(d->raw_out.size()); | ||
| 1164 | i.outputReportInfo = d->raw_out.empty() ? nullptr : d->raw_out.data(); | ||
| 1165 | i.featureReportCount = static_cast<uint32_t>(d->raw_feature.size()); | ||
| 1166 | i.featureReportInfo = d->raw_feature.empty() ? nullptr : d->raw_feature.data(); | ||
| 1167 | } | ||
| 1168 | |||
| 1169 | void close_raw(Device* d) { | ||
| 1170 | std::lock_guard lock(d->hid_mu); | ||
| 1171 | if (d->hid) d->hid_backend->close(d->hid); | ||
| 1172 | d->hid = nullptr; | ||
| 1173 | } | ||
| 1174 | |||
| 1175 | // Drains the input reports pending since the last poll; the newest one the pad declares goes into snap. With none | ||
| 1176 | // pending, snap keeps the previous report, so a quiet pad makes no new reading. Poll thread. | ||
| 1177 | void read_raw(Device* d, Snapshot* snap) { | ||
| 1178 | snap->has_raw = d->last_snap.has_raw; | ||
| 1179 | snap->raw_id = d->last_snap.raw_id; | ||
| 1180 | snap->raw = d->last_snap.raw; | ||
| 1181 | std::lock_guard lock(d->hid_mu); | ||
| 1182 | if (!d->hid) return; | ||
| 1183 | std::vector<uint8_t> buf(4096); | ||
| 1184 | for (int k = 0; k < 64; ++k) { // bounded: a pad that floods cannot hold up the poll | ||
| 1185 | const int n = d->hid_backend->read(d->hid, buf.data(), buf.size()); | ||
| 1186 | if (n <= 0) break; | ||
| 1187 | uint32_t id = 0; | ||
| 1188 | const uint8_t* p = buf.data(); | ||
| 1189 | size_t len = static_cast<size_t>(n); | ||
| 1190 | if (d->hid_ids) { | ||
| 1191 | id = buf[0]; | ||
| 1192 | ++p; | ||
| 1193 | --len; | ||
| 1194 | } | ||
| 1195 | const GameInputRawDeviceReportInfo* info = d->find_raw(GameInputRawInputReport, id); | ||
| 1196 | if (!info) continue; | ||
| 1197 | snap->has_raw = true; | ||
| 1198 | snap->raw_id = id; | ||
| 1199 | snap->raw.assign(p, p + std::min(len, static_cast<size_t>(info->size))); | ||
| 1200 | snap->raw.resize(info->size, 0); | ||
| 1201 | } | ||
| 1202 | } | ||
| 1203 | |||
| 934 | void open_pad(SDL_JoystickID jid, uint64_t ts) { | 1204 | void open_pad(SDL_JoystickID jid, uint64_t ts) { |
| 935 | State& s = g(); | 1205 | State& s = g(); |
| 936 | SDL_Gamepad* p = SDL_OpenGamepad(jid); | 1206 | SDL_Gamepad* p = SDL_OpenGamepad(jid); |
| @@ -1041,6 +1311,7 @@ void open_pad(SDL_JoystickID jid, uint64_t ts) { | |||
| 1041 | d->motors_sent_at = d->triggers_sent_at = d->battery_at = 0; | 1311 | d->motors_sent_at = d->triggers_sent_at = d->battery_at = 0; |
| 1042 | d->wanted = {}; | 1312 | d->wanted = {}; |
| 1043 | fill_info(d, p, id); | 1313 | fill_info(d, p, id); |
| 1314 | open_raw(d, path); | ||
| 1044 | 1315 | ||
| 1045 | // Player LEDs and SDL's default lightbar colour follow the slot: the lowest one not in use. | 1316 | // Player LEDs and SDL's default lightbar colour follow the slot: the lowest one not in use. |
| 1046 | int player = 0; | 1317 | int player = 0; |
| @@ -1056,6 +1327,7 @@ void open_pad(SDL_JoystickID jid, uint64_t ts) { | |||
| 1056 | 1327 | ||
| 1057 | uint32_t status = kConnectedStatus; | 1328 | uint32_t status = kConnectedStatus; |
| 1058 | if (d->info.capabilities & GameInputDeviceCapabilityWireless) status |= GameInputDeviceWireless; | 1329 | if (d->info.capabilities & GameInputDeviceCapabilityWireless) status |= GameInputDeviceWireless; |
| 1330 | if (d->hid) status |= GameInputDeviceRawIoEnabled; | ||
| 1059 | d->status = status; | 1331 | d->status = status; |
| 1060 | s.connected.push_back(d); | 1332 | s.connected.push_back(d); |
| 1061 | 1333 | ||
| @@ -1074,6 +1346,7 @@ void close_pad(Device* d, uint64_t ts) { | |||
| 1074 | std::lock_guard lock(s.mu); | 1346 | std::lock_guard lock(s.mu); |
| 1075 | const uint32_t old = d->status.exchange(GameInputDeviceNoStatus); | 1347 | const uint32_t old = d->status.exchange(GameInputDeviceNoStatus); |
| 1076 | SDL_CloseGamepad(d->pad); | 1348 | SDL_CloseGamepad(d->pad); |
| 1349 | close_raw(d); | ||
| 1077 | d->pad = nullptr; | 1350 | d->pad = nullptr; |
| 1078 | d->player = -1; | 1351 | d->player = -1; |
| 1079 | // The haptic device belongs to the joystick, which goes with the gamepad. Effects a title still holds stay alive | 1352 | // The haptic device belongs to the joystick, which goes with the gamepad. Effects a title still holds stay alive |
| @@ -1540,7 +1813,8 @@ void poll_once() { | |||
| 1540 | connected = s.connected; | 1813 | connected = s.connected; |
| 1541 | } | 1814 | } |
| 1542 | for (Device* d : connected) { | 1815 | for (Device* d : connected) { |
| 1543 | const Snapshot snap = read_pad(d); | 1816 | Snapshot snap = read_pad(d); |
| 1817 | read_raw(d, &snap); | ||
| 1544 | const bool guide_changed = d->last_snap.guide != snap.guide; | 1818 | const bool guide_changed = d->last_snap.guide != snap.guide; |
| 1545 | const bool changed = !(d->last_snap == snap); | 1819 | const bool changed = !(d->last_snap == snap); |
| 1546 | d->last_snap = snap; | 1820 | d->last_snap = snap; |
| @@ -2194,6 +2468,72 @@ const KeyBinding* keyboard_bindings(int* count) { | |||
| 2194 | return kKeyBindings; | 2468 | return kKeyBindings; |
| 2195 | } | 2469 | } |
| 2196 | 2470 | ||
| 2471 | // 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; | ||
| 2472 | // bTag in bits 4-7) and up to four data bytes, little-endian. 0xFE starts a long item (data size, tag, data). | ||
| 2473 | bool parse_report_descriptor(const uint8_t* data, size_t size, std::vector<RawReportSize>* out) { | ||
| 2474 | if (!out || (!data && size)) return false; | ||
| 2475 | out->clear(); | ||
| 2476 | struct Globals { | ||
| 2477 | uint32_t report_size = 0, report_count = 0, report_id = 0; | ||
| 2478 | }; | ||
| 2479 | Globals g; | ||
| 2480 | std::vector<Globals> stack; | ||
| 2481 | struct Key { | ||
| 2482 | int kind; | ||
| 2483 | uint32_t id; | ||
| 2484 | uint64_t bits; | ||
| 2485 | }; | ||
| 2486 | std::vector<Key> reports; // first-seen order; sorted at the end | ||
| 2487 | size_t i = 0; | ||
| 2488 | while (i < size) { | ||
| 2489 | const uint8_t prefix = data[i++]; | ||
| 2490 | if (prefix == 0xFE) { // long item: bDataSize, bLongItemTag, data | ||
| 2491 | if (i + 2 > size) return false; | ||
| 2492 | i += 2 + data[i]; | ||
| 2493 | if (i > size) return false; | ||
| 2494 | continue; | ||
| 2495 | } | ||
| 2496 | const size_t n = (prefix & 3) == 3 ? 4 : (prefix & 3); | ||
| 2497 | if (i + n > size) return false; | ||
| 2498 | uint32_t v = 0; | ||
| 2499 | for (size_t k = 0; k < n; ++k) v |= static_cast<uint32_t>(data[i + k]) << (8 * k); | ||
| 2500 | i += n; | ||
| 2501 | const int type = (prefix >> 2) & 3, tag = prefix >> 4; | ||
| 2502 | if (type == 1) { // global | ||
| 2503 | if (tag == 7) g.report_size = v; | ||
| 2504 | else if (tag == 8) { | ||
| 2505 | if (v == 0 || v > 255) return false; | ||
| 2506 | g.report_id = v; | ||
| 2507 | } else if (tag == 9) g.report_count = v; | ||
| 2508 | else if (tag == 10) stack.push_back(g); | ||
| 2509 | else if (tag == 11) { | ||
| 2510 | if (stack.empty()) return false; | ||
| 2511 | g = stack.back(); | ||
| 2512 | stack.pop_back(); | ||
| 2513 | } | ||
| 2514 | } else if (type == 0) { // main: Input 8, Output 9, Feature 11 (Collection 10 and End Collection 12 add nothing) | ||
| 2515 | const int kind = tag == 8 ? 0 : tag == 9 ? 1 : tag == 11 ? 2 : -1; | ||
| 2516 | if (kind < 0) continue; | ||
| 2517 | const uint64_t add = static_cast<uint64_t>(g.report_size) * g.report_count; | ||
| 2518 | Key* k = nullptr; | ||
| 2519 | for (Key& r : reports) | ||
| 2520 | if (r.kind == kind && r.id == g.report_id) k = &r; | ||
| 2521 | if (!k) { | ||
| 2522 | reports.push_back({kind, g.report_id, 0}); | ||
| 2523 | k = &reports.back(); | ||
| 2524 | } | ||
| 2525 | k->bits += add; | ||
| 2526 | if (k->bits > 65536ull * 8) return false; | ||
| 2527 | } | ||
| 2528 | } | ||
| 2529 | for (const Key& k : reports) out->push_back({k.kind, k.id, static_cast<uint32_t>((k.bits + 7) / 8)}); | ||
| 2530 | std::sort(out->begin(), out->end(), | ||
| 2531 | [](const RawReportSize& a, const RawReportSize& b) { return a.kind != b.kind ? a.kind < b.kind : a.id < b.id; }); | ||
| 2532 | return true; | ||
| 2533 | } | ||
| 2534 | |||
| 2535 | void set_hid_backend(const HidBackend* backend) { g_hid = backend ? backend : &kSdlHid; } | ||
| 2536 | |||
| 2197 | } // namespace rt::gameinput | 2537 | } // namespace rt::gameinput |
| 2198 | 2538 | ||
| 2199 | SHIM_TABLE(gameinput, | 2539 | SHIM_TABLE(gameinput, |