diff options
| author | Maraco91 <43787609+Maraco91@users.noreply.github.com> | 2026-11-23 23:15:46 +0100 |
|---|---|---|
| committer | Maraco91 <43787609+Maraco91@users.noreply.github.com> | 2026-11-23 23:15:46 +0100 |
| commit | 29bd7a11a68574e1aa79908805ee6f56a8ae8e76 (patch) | |
| tree | 61d46a5d5cf8300c8a39e9a61e2802c3c112358d /runtime/test/gameinput_test.cpp | |
| parent | 5d74c272363f2703265d44e6aaab1a1df8ddfaf3 (diff) | |
gameinput: the device info has its GameInputLabel members
gameinput_abi.h left out the labels of the public v0 layout. In
GameInputControllerAxisInfo and GameInputControllerButtonInfo a label
sits at offset 4, in what is otherwise padding, so the sizes matched
while isContinuous, restValue, legacyDInputIndex and rawReportIndex were
four bytes early. GameInputGamepadInfo and GameInputUiNavigationInfo are
tables of 14 and 21 labels; they were declared empty, and the pointers
in GameInputDeviceInfo led to one-byte members, so a title reading a
label read whatever followed.
The header declares the enum, the two tables and the two label members
and pins each offset that moved. fill_info gives every control the Xbox
label of its position on every pad, since every pad is presented as an
Xbox One pad.
gameinput_test reads the tables by the public header's byte offsets, so
the shim's own header cannot agree with itself.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
| -rw-r--r-- | runtime/test/gameinput_test.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 7217dc37b..88583146f 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp | |||
| @@ -42,6 +42,35 @@ void check(bool condition, const char* what) { | |||
| 42 | 42 | ||
| 43 | bool approx(float a, float b) { return std::fabs(a - b) < 0.002f; } | 43 | bool approx(float a, float b) { return std::fabs(a - b) < 0.002f; } |
| 44 | 44 | ||
| 45 | // A device's info tables as a title built against Microsoft's public v0 GameInput.h reads them: by that header's byte | ||
| 46 | // offsets, not through gameinput_abi.h, so a member the shim's header misplaces fails here instead of agreeing with | ||
| 47 | // itself. The labels are GameInputLabel values (Microsoft Learn's GameInputLabel page), written out for that reason. | ||
| 48 | enum : int32_t { | ||
| 49 | kLabelUnknown = -1, kLabelNone = 0, kXboxGuide = 1, kXboxMenu = 4, kXboxView = 5, kXboxA = 7, kXboxB = 8, | ||
| 50 | kXboxX = 9, kXboxY = 10, kXboxUp = 11, kXboxDown = 12, kXboxLeft = 13, kXboxRight = 14, kXboxLeftShoulder = 15, | ||
| 51 | kXboxLeftTrigger = 16, kXboxLeftStick = 17, kXboxRightShoulder = 18, kXboxRightTrigger = 19, kXboxRightStick = 20, | ||
| 52 | }; | ||
| 53 | |||
| 54 | template <class T> | ||
| 55 | T field_at(const void* table, size_t offset) { | ||
| 56 | T v{}; | ||
| 57 | std::memcpy(&v, static_cast<const char*>(table) + offset, sizeof v); | ||
| 58 | return v; | ||
| 59 | } | ||
| 60 | |||
| 61 | // Whether `table` starts with these labels, four bytes each; prints the ones it holds. | ||
| 62 | bool labels_are(const char* name, const void* table, std::initializer_list<int32_t> want) { | ||
| 63 | bool same = table != nullptr; | ||
| 64 | std::printf(" %s:", name); | ||
| 65 | for (size_t i = 0; table && i < want.size(); ++i) { | ||
| 66 | const int32_t got = field_at<int32_t>(table, 4 * i); | ||
| 67 | std::printf(" %d", got); | ||
| 68 | same = same && got == want.begin()[i]; | ||
| 69 | } | ||
| 70 | std::printf("\n"); | ||
| 71 | return same; | ||
| 72 | } | ||
| 73 | |||
| 45 | // What SDL handed to one virtual pad. | 74 | // What SDL handed to one virtual pad. |
| 46 | struct Pad { | 75 | struct Pad { |
| 47 | SDL_JoystickID id = 0; | 76 | SDL_JoystickID id = 0; |
| @@ -1673,6 +1702,50 @@ int main() { | |||
| 1673 | check(info->controllerAxisCount == 6 && info->controllerAxisInfo && info->gamepadInfo && info->displayName && | 1702 | check(info->controllerAxisCount == 6 && info->controllerAxisInfo && info->gamepadInfo && info->displayName && |
| 1674 | std::strcmp(info->displayName->data, "Leonida virtual Xbox pad") == 0, | 1703 | std::strcmp(info->displayName->data, "Leonida virtual Xbox pad") == 0, |
| 1675 | "info axis table, gamepad info and display name"); | 1704 | "info axis table, gamepad info and display name"); |
| 1705 | // The label tables: 14 and 21 labels in the public v0 order. Every pad is presented as an Xbox One pad and carries | ||
| 1706 | // the Xbox labels; the UI navigation ones follow the shim's own mapping (triggers page up and down, no label for | ||
| 1707 | // a stick). | ||
| 1708 | check(labels_are("gamepadInfo", info->gamepadInfo, | ||
| 1709 | {kXboxMenu, kXboxView, kXboxA, kXboxB, kXboxX, kXboxY, kXboxUp, kXboxDown, kXboxLeft, kXboxRight, | ||
| 1710 | kXboxLeftShoulder, kXboxRightShoulder, kXboxLeftStick, kXboxRightStick}), | ||
| 1711 | "info.gamepadInfo: menu, view, A B X Y, d-pad, shoulders and stick buttons carry the Xbox labels"); | ||
| 1712 | check(labels_are("uiNavigationInfo", info->uiNavigationInfo, | ||
| 1713 | {kXboxMenu, kXboxView, kXboxA, kXboxB, kXboxUp, kXboxDown, kXboxLeft, kXboxRight, kXboxX, kXboxY, | ||
| 1714 | kXboxLeftStick, kXboxRightStick, kXboxLeftTrigger, kXboxRightTrigger, kXboxLeftShoulder, | ||
| 1715 | kXboxRightShoulder, kLabelNone, kLabelNone, kLabelNone, kLabelNone, kXboxGuide}), | ||
| 1716 | "info.uiNavigationInfo: the labels of the controls its UI navigation state reads"); | ||
| 1717 | // One axis is 48 bytes and one button 32: mappedInputKinds, the label at 4, then for an axis isContinuous at 8, | ||
| 1718 | // hasRestValue at 11, restValue at 12, resolution at 16 and the legacy indices at 24 and 26; for a button the | ||
| 1719 | // legacy indices at 8 and 10 and rawReportIndex at 12. | ||
| 1720 | const char* stick = reinterpret_cast<const char*>(info->controllerAxisInfo); | ||
| 1721 | const char* trigger = stick + 4 * 48; // LX, LY, RX, RY, LT, RT | ||
| 1722 | std::printf(" axis 0: label=%d continuous=%u hasRest=%u rest=%.3f; axis 4: label=%d rest=%.3f dinput=%u\n", | ||
| 1723 | field_at<int32_t>(stick, 4), field_at<uint8_t>(stick, 8), field_at<uint8_t>(stick, 11), | ||
| 1724 | field_at<float>(stick, 12), field_at<int32_t>(trigger, 4), field_at<float>(trigger, 12), | ||
| 1725 | field_at<uint16_t>(trigger, 24)); | ||
| 1726 | check(field_at<uint32_t>(stick, 0) == GameInputKindGamepad && field_at<int32_t>(stick, 4) == kLabelNone && | ||
| 1727 | field_at<uint8_t>(stick, 8) == 1 && field_at<uint8_t>(stick, 11) == 1 && | ||
| 1728 | approx(field_at<float>(stick, 12), 0.5f) && field_at<uint64_t>(stick, 16) == 65536, | ||
| 1729 | "info.controllerAxisInfo[0]: a stick axis has no label, is continuous and rests at 0.5"); | ||
| 1730 | check(field_at<int32_t>(trigger, 4) == kXboxLeftTrigger && field_at<uint8_t>(trigger, 8) == 1 && | ||
| 1731 | field_at<uint8_t>(trigger, 11) == 1 && field_at<float>(trigger, 12) == 0.0f && | ||
| 1732 | field_at<uint16_t>(trigger, 24) == 4 && field_at<uint16_t>(trigger, 26) == 4, | ||
| 1733 | "info.controllerAxisInfo[4]: the left trigger's label, rest value 0 and legacy indices"); | ||
| 1734 | const char* button_table = reinterpret_cast<const char*>(info->controllerButtonInfo); | ||
| 1735 | const char* east = button_table + SDL_GAMEPAD_BUTTON_EAST * 32; | ||
| 1736 | std::printf(" button %d: label=%d dinput=%u hid=%u raw=%u; guide label=%d, misc1 label=%d\n", | ||
| 1737 | SDL_GAMEPAD_BUTTON_EAST, field_at<int32_t>(east, 4), field_at<uint16_t>(east, 8), | ||
| 1738 | field_at<uint16_t>(east, 10), field_at<uint32_t>(east, 12), | ||
| 1739 | field_at<int32_t>(button_table, SDL_GAMEPAD_BUTTON_GUIDE * 32 + 4), | ||
| 1740 | field_at<int32_t>(button_table, SDL_GAMEPAD_BUTTON_MISC1 * 32 + 4)); | ||
| 1741 | check(info->controllerButtonCount == SDL_GAMEPAD_BUTTON_COUNT && | ||
| 1742 | field_at<uint32_t>(east, 0) == GameInputKindGamepad && field_at<int32_t>(east, 4) == kXboxB && | ||
| 1743 | field_at<uint16_t>(east, 8) == SDL_GAMEPAD_BUTTON_EAST && | ||
| 1744 | field_at<uint16_t>(east, 10) == SDL_GAMEPAD_BUTTON_EAST && field_at<uint32_t>(east, 12) == 0, | ||
| 1745 | "info.controllerButtonInfo: the east button is B, with its legacy indices and rawReportIndex 0"); | ||
| 1746 | check(field_at<int32_t>(button_table, SDL_GAMEPAD_BUTTON_GUIDE * 32 + 4) == kXboxGuide && | ||
| 1747 | field_at<int32_t>(button_table, SDL_GAMEPAD_BUTTON_MISC1 * 32 + 4) == kLabelUnknown, | ||
| 1748 | "info.controllerButtonInfo: the guide button's label, and unknown for a button with no gamepad meaning"); | ||
| 1676 | check(dev->GetDeviceStatus() & GameInputDeviceConnected, "GetDeviceStatus: connected"); | 1749 | check(dev->GetDeviceStatus() & GameInputDeviceConnected, "GetDeviceStatus: connected"); |
| 1677 | check(xbox.player == 0, "player index 0 for the first pad"); | 1750 | check(xbox.player == 0, "player index 0 for the first pad"); |
| 1678 | 1751 | ||
| @@ -1791,6 +1864,9 @@ int main() { | |||
| 1791 | check(dsdev && dsdev != dev && (dsdev->GetDeviceInfo()->supportedRumbleMotors & GameInputRumbleLeftTrigger), | 1864 | check(dsdev && dsdev != dev && (dsdev->GetDeviceInfo()->supportedRumbleMotors & GameInputRumbleLeftTrigger), |
| 1792 | "DualSense device reported with trigger motors"); | 1865 | "DualSense device reported with trigger motors"); |
| 1793 | if (!dsdev) return 1; | 1866 | if (!dsdev) return 1; |
| 1867 | check(labels_are("DualSense gamepadInfo", dsdev->GetDeviceInfo()->gamepadInfo, | ||
| 1868 | {kXboxMenu, kXboxView, kXboxA, kXboxB, kXboxX, kXboxY, kXboxUp}), | ||
| 1869 | "a DualSense is presented as an Xbox pad: its labels are the Xbox ones, not the shapes printed on it"); | ||
| 1794 | 1870 | ||
| 1795 | GameInputRumbleParams ds_rumble{0.0f, 0.0f, 1.0f, 0.5f}; | 1871 | GameInputRumbleParams ds_rumble{0.0f, 0.0f, 1.0f, 0.5f}; |
| 1796 | dsdev->SetRumbleState(&ds_rumble); | 1872 | dsdev->SetRumbleState(&ds_rumble); |
| @@ -1846,6 +1922,9 @@ int main() { | |||
| 1846 | check(plaindev && (plaindev->GetDeviceInfo()->supportedRumbleMotors & GameInputRumbleLeftTrigger) == 0, | 1922 | check(plaindev && (plaindev->GetDeviceInfo()->supportedRumbleMotors & GameInputRumbleLeftTrigger) == 0, |
| 1847 | "a pad without trigger motors does not report them"); | 1923 | "a pad without trigger motors does not report them"); |
| 1848 | if (plaindev) { | 1924 | if (plaindev) { |
| 1925 | check(labels_are("Switch Pro gamepadInfo", plaindev->GetDeviceInfo()->gamepadInfo, | ||
| 1926 | {kXboxMenu, kXboxView, kXboxA, kXboxB, kXboxX, kXboxY, kXboxUp}), | ||
| 1927 | "a Switch Pro is presented as an Xbox pad too: Xbox labels by position"); | ||
| 1849 | GameInputRumbleParams p{0.25f, 0.25f, 1.0f, 1.0f}; | 1928 | GameInputRumbleParams p{0.25f, 0.25f, 1.0f, 1.0f}; |
| 1850 | plaindev->SetRumbleState(&p); | 1929 | plaindev->SetRumbleState(&p); |
| 1851 | rt::gameinput::poll(); | 1930 | rt::gameinput::poll(); |