upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/gameinput_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
-rw-r--r--runtime/test/gameinput_test.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp
index df0a7f919..4c48a35e6 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
43bool approx(float a, float b) { return std::fabs(a - b) < 0.002f; } 43bool 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.
48enum : 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
54template <class T>
55T 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.
62bool 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.
46struct Pad { 75struct Pad {
47 SDL_JoystickID id = 0; 76 SDL_JoystickID id = 0;
@@ -1730,6 +1759,50 @@ int main() {
1730 check(info->controllerAxisCount == 6 && info->controllerAxisInfo && info->gamepadInfo && info->displayName && 1759 check(info->controllerAxisCount == 6 && info->controllerAxisInfo && info->gamepadInfo && info->displayName &&
1731 std::strcmp(info->displayName->data, "Leonida virtual Xbox pad") == 0, 1760 std::strcmp(info->displayName->data, "Leonida virtual Xbox pad") == 0,
1732 "info axis table, gamepad info and display name"); 1761 "info axis table, gamepad info and display name");
1762 // The label tables: 14 and 21 labels in the public v0 order. Every pad is presented as an Xbox One pad and carries
1763 // the Xbox labels; the UI navigation ones follow the shim's own mapping (triggers page up and down, no label for
1764 // a stick).
1765 check(labels_are("gamepadInfo", info->gamepadInfo,
1766 {kXboxMenu, kXboxView, kXboxA, kXboxB, kXboxX, kXboxY, kXboxUp, kXboxDown, kXboxLeft, kXboxRight,
1767 kXboxLeftShoulder, kXboxRightShoulder, kXboxLeftStick, kXboxRightStick}),
1768 "info.gamepadInfo: menu, view, A B X Y, d-pad, shoulders and stick buttons carry the Xbox labels");
1769 check(labels_are("uiNavigationInfo", info->uiNavigationInfo,
1770 {kXboxMenu, kXboxView, kXboxA, kXboxB, kXboxUp, kXboxDown, kXboxLeft, kXboxRight, kXboxX, kXboxY,
1771 kXboxLeftStick, kXboxRightStick, kXboxLeftTrigger, kXboxRightTrigger, kXboxLeftShoulder,
1772 kXboxRightShoulder, kLabelNone, kLabelNone, kLabelNone, kLabelNone, kXboxGuide}),
1773 "info.uiNavigationInfo: the labels of the controls its UI navigation state reads");
1774 // One axis is 48 bytes and one button 32: mappedInputKinds, the label at 4, then for an axis isContinuous at 8,
1775 // hasRestValue at 11, restValue at 12, resolution at 16 and the legacy indices at 24 and 26; for a button the
1776 // legacy indices at 8 and 10 and rawReportIndex at 12.
1777 const char* stick = reinterpret_cast<const char*>(info->controllerAxisInfo);
1778 const char* trigger = stick + 4 * 48; // LX, LY, RX, RY, LT, RT
1779 std::printf(" axis 0: label=%d continuous=%u hasRest=%u rest=%.3f; axis 4: label=%d rest=%.3f dinput=%u\n",
1780 field_at<int32_t>(stick, 4), field_at<uint8_t>(stick, 8), field_at<uint8_t>(stick, 11),
1781 field_at<float>(stick, 12), field_at<int32_t>(trigger, 4), field_at<float>(trigger, 12),
1782 field_at<uint16_t>(trigger, 24));
1783 check(field_at<uint32_t>(stick, 0) == GameInputKindGamepad && field_at<int32_t>(stick, 4) == kLabelNone &&
1784 field_at<uint8_t>(stick, 8) == 1 && field_at<uint8_t>(stick, 11) == 1 &&
1785 approx(field_at<float>(stick, 12), 0.5f) && field_at<uint64_t>(stick, 16) == 65536,
1786 "info.controllerAxisInfo[0]: a stick axis has no label, is continuous and rests at 0.5");
1787 check(field_at<int32_t>(trigger, 4) == kXboxLeftTrigger && field_at<uint8_t>(trigger, 8) == 1 &&
1788 field_at<uint8_t>(trigger, 11) == 1 && field_at<float>(trigger, 12) == 0.0f &&
1789 field_at<uint16_t>(trigger, 24) == 4 && field_at<uint16_t>(trigger, 26) == 4,
1790 "info.controllerAxisInfo[4]: the left trigger's label, rest value 0 and legacy indices");
1791 const char* button_table = reinterpret_cast<const char*>(info->controllerButtonInfo);
1792 const char* east = button_table + SDL_GAMEPAD_BUTTON_EAST * 32;
1793 std::printf(" button %d: label=%d dinput=%u hid=%u raw=%u; guide label=%d, misc1 label=%d\n",
1794 SDL_GAMEPAD_BUTTON_EAST, field_at<int32_t>(east, 4), field_at<uint16_t>(east, 8),
1795 field_at<uint16_t>(east, 10), field_at<uint32_t>(east, 12),
1796 field_at<int32_t>(button_table, SDL_GAMEPAD_BUTTON_GUIDE * 32 + 4),
1797 field_at<int32_t>(button_table, SDL_GAMEPAD_BUTTON_MISC1 * 32 + 4));
1798 check(info->controllerButtonCount == SDL_GAMEPAD_BUTTON_COUNT &&
1799 field_at<uint32_t>(east, 0) == GameInputKindGamepad && field_at<int32_t>(east, 4) == kXboxB &&
1800 field_at<uint16_t>(east, 8) == SDL_GAMEPAD_BUTTON_EAST &&
1801 field_at<uint16_t>(east, 10) == SDL_GAMEPAD_BUTTON_EAST && field_at<uint32_t>(east, 12) == 0,
1802 "info.controllerButtonInfo: the east button is B, with its legacy indices and rawReportIndex 0");
1803 check(field_at<int32_t>(button_table, SDL_GAMEPAD_BUTTON_GUIDE * 32 + 4) == kXboxGuide &&
1804 field_at<int32_t>(button_table, SDL_GAMEPAD_BUTTON_MISC1 * 32 + 4) == kLabelUnknown,
1805 "info.controllerButtonInfo: the guide button's label, and unknown for a button with no gamepad meaning");
1733 check(dev->GetDeviceStatus() & GameInputDeviceConnected, "GetDeviceStatus: connected"); 1806 check(dev->GetDeviceStatus() & GameInputDeviceConnected, "GetDeviceStatus: connected");
1734 check(xbox.player == 0, "player index 0 for the first pad"); 1807 check(xbox.player == 0, "player index 0 for the first pad");
1735 1808
@@ -1848,6 +1921,9 @@ int main() {
1848 check(dsdev && dsdev != dev && (dsdev->GetDeviceInfo()->supportedRumbleMotors & GameInputRumbleLeftTrigger), 1921 check(dsdev && dsdev != dev && (dsdev->GetDeviceInfo()->supportedRumbleMotors & GameInputRumbleLeftTrigger),
1849 "DualSense device reported with trigger motors"); 1922 "DualSense device reported with trigger motors");
1850 if (!dsdev) return 1; 1923 if (!dsdev) return 1;
1924 check(labels_are("DualSense gamepadInfo", dsdev->GetDeviceInfo()->gamepadInfo,
1925 {kXboxMenu, kXboxView, kXboxA, kXboxB, kXboxX, kXboxY, kXboxUp}),
1926 "a DualSense is presented as an Xbox pad: its labels are the Xbox ones, not the shapes printed on it");
1851 1927
1852 GameInputRumbleParams ds_rumble{0.0f, 0.0f, 1.0f, 0.5f}; 1928 GameInputRumbleParams ds_rumble{0.0f, 0.0f, 1.0f, 0.5f};
1853 dsdev->SetRumbleState(&ds_rumble); 1929 dsdev->SetRumbleState(&ds_rumble);
@@ -1903,6 +1979,9 @@ int main() {
1903 check(plaindev && (plaindev->GetDeviceInfo()->supportedRumbleMotors & GameInputRumbleLeftTrigger) == 0, 1979 check(plaindev && (plaindev->GetDeviceInfo()->supportedRumbleMotors & GameInputRumbleLeftTrigger) == 0,
1904 "a pad without trigger motors does not report them"); 1980 "a pad without trigger motors does not report them");
1905 if (plaindev) { 1981 if (plaindev) {
1982 check(labels_are("Switch Pro gamepadInfo", plaindev->GetDeviceInfo()->gamepadInfo,
1983 {kXboxMenu, kXboxView, kXboxA, kXboxB, kXboxX, kXboxY, kXboxUp}),
1984 "a Switch Pro is presented as an Xbox pad too: Xbox labels by position");
1906 GameInputRumbleParams p{0.25f, 0.25f, 1.0f, 1.0f}; 1985 GameInputRumbleParams p{0.25f, 0.25f, 1.0f, 1.0f};
1907 plaindev->SetRumbleState(&p); 1986 plaindev->SetRumbleState(&p);
1908 rt::gameinput::poll(); 1987 rt::gameinput::poll();