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:
authorprampa <218078045+prampa@users.noreply.github.com>2026-11-24 00:02:27 +0100
committerprampa <218078045+prampa@users.noreply.github.com>2026-11-24 00:02:27 +0100
commit2462d00e7f0a45f85781cf24f6598d587e05b379 (patch)
tree475064f89a697d9df80e7284b8352daa1609eaf7 /runtime/test/gameinput_test.cpp
parent1699d00620d69e402969dcfbd77de27d0a9274df (diff)
parent2d53a8055656f1138e57de5367960fcc0714283a (diff)
Merge current Leonida main and retain last complete boot call
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
-rw-r--r--runtime/test/gameinput_test.cpp136
1 files changed, 136 insertions, 0 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp
index 7217dc37b..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;
@@ -1555,6 +1584,8 @@ void test_haptic_motors(IGameInput* input) {
1555 motors[0].waveformInfo != nullptr && motors[0].waveformInfo->isIntensitySupported, 1584 motors[0].waveformInfo != nullptr && motors[0].waveformInfo->isIntensitySupported,
1556 "haptics: they are the left and right trigger, one waveform each, an intensity"); 1585 "haptics: they are the left and right trigger, one waveform each, an intensity");
1557 check(sticks_at_rest(pad_state(input, dev)), "haptics: the pad still reads normally"); 1586 check(sticks_at_rest(pad_state(input, dev)), "haptics: the pad still reads normally");
1587 check(motors[0].waveformInfo->isRepeatDelaySupported && motors[1].waveformInfo->isRepeatDelaySupported,
1588 "haptics: both waveforms advertise repeat delay");
1558 1589
1559 GameInputHapticFeedbackParams p{}; 1590 GameInputHapticFeedbackParams p{};
1560 p.intensity = 0.5f; 1591 p.intensity = 0.5f;
@@ -1599,6 +1630,61 @@ void test_haptic_motors(IGameInput* input) {
1599 rt::gameinput::poll(); 1630 rt::gameinput::poll();
1600 check(xbox.left == 0, "haptics: releasing both leaves the trigger silent"); 1631 check(xbox.left == 0, "haptics: releasing both leaves the trigger silent");
1601 1632
1633 // A request too long for a uint64 microsecond deadline plays until it is set again: the end time saturates
1634 // instead of wrapping into the past (never plays) or into the near future (stops early).
1635 p.intensity = 1.0f;
1636 p.playCount = 1;
1637 p.duration = UINT64_MAX;
1638 p.repeatDelay = 0;
1639 check(dev->SetHapticMotorState(0, &p) == S_OK, "haptics: a duration of UINT64_MAX is accepted");
1640 rt::gameinput::poll();
1641 check(xbox.left == 65535, "haptics: and plays (its end time does not wrap into the past)");
1642 p.duration = (1ull << 63) + 50'000; // times 2 wraps to 100 ms
1643 p.playCount = 2;
1644 check(dev->SetHapticMotorState(1, &p) == S_OK, "haptics: duration * playCount past UINT64_MAX is accepted");
1645 rt::gameinput::poll();
1646 Sleep(130);
1647 rt::gameinput::poll();
1648 check(xbox.right == 65535, "haptics: and keeps playing (the product does not wrap to a short request)");
1649 dev->SetHapticMotorState(0, nullptr);
1650 dev->SetHapticMotorState(1, nullptr);
1651 rt::gameinput::poll();
1652 check(xbox.left == 0 && xbox.right == 0, "haptics: both long requests stop when the state is cleared");
1653
1654 // Observe the output at the virtual pad. The left pulse plays twice over ordinary rumble; the right repeats
1655 // until stopped. Poll for each transition so the test need not wake on an exact millisecond boundary.
1656 dev->SetRumbleState(&rumble);
1657 p.duration = 120'000;
1658 p.repeatDelay = 180'000;
1659 p.playCount = 2;
1660 dev->SetHapticMotorState(0, &p);
1661 p.playCount = 0;
1662 dev->SetHapticMotorState(1, &p);
1663 auto reaches = [&](uint16_t left, uint16_t right) {
1664 const uint64_t deadline = input->GetCurrentTimestamp() + 900'000;
1665 do {
1666 rt::gameinput::poll();
1667 if (xbox.left == left && xbox.right == right) return true;
1668 Sleep(5);
1669 } while (input->GetCurrentTimestamp() < deadline);
1670 return false;
1671 };
1672 check(reaches(65535, 65535), "haptics: both pulse trains start at full intensity");
1673 check(reaches(16384, 0), "haptics: only ordinary rumble remains between pulses");
1674 check(reaches(65535, 65535), "haptics: both motors resume for the second pulse");
1675 check(reaches(16384, 0), "haptics: both motors release after the second pulse");
1676 check(reaches(16384, 65535), "haptics: only the indefinitely repeating motor starts a third pulse");
1677 dev->SetHapticMotorState(1, nullptr);
1678 dev->SetRumbleState(nullptr);
1679 check(reaches(0, 0), "haptics: cancelling the repeating waveform releases the trigger");
1680
1681 p.duration = 80'000;
1682 p.repeatDelay = UINT64_MAX - p.duration + 1; // duration + delay would wrap to zero
1683 dev->SetHapticMotorState(0, &p);
1684 check(reaches(65535, 0), "haptics: an indefinitely repeated pulse accepts a very long delay");
1685 check(reaches(0, 0), "haptics: the motor enters the quiet interval even if the cycle exceeds UINT64_MAX");
1686 dev->SetHapticMotorState(0, nullptr);
1687
1602 detach(xbox); 1688 detach(xbox);
1603 rt::gameinput::poll(); 1689 rt::gameinput::poll();
1604 check(rt::gameinput::connected_count() == 0, "haptics: detached"); 1690 check(rt::gameinput::connected_count() == 0, "haptics: detached");
@@ -1673,6 +1759,50 @@ int main() {
1673 check(info->controllerAxisCount == 6 && info->controllerAxisInfo && info->gamepadInfo && info->displayName && 1759 check(info->controllerAxisCount == 6 && info->controllerAxisInfo && info->gamepadInfo && info->displayName &&
1674 std::strcmp(info->displayName->data, "Leonida virtual Xbox pad") == 0, 1760 std::strcmp(info->displayName->data, "Leonida virtual Xbox pad") == 0,
1675 "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");
1676 check(dev->GetDeviceStatus() & GameInputDeviceConnected, "GetDeviceStatus: connected"); 1806 check(dev->GetDeviceStatus() & GameInputDeviceConnected, "GetDeviceStatus: connected");
1677 check(xbox.player == 0, "player index 0 for the first pad"); 1807 check(xbox.player == 0, "player index 0 for the first pad");
1678 1808
@@ -1791,6 +1921,9 @@ int main() {
1791 check(dsdev && dsdev != dev && (dsdev->GetDeviceInfo()->supportedRumbleMotors & GameInputRumbleLeftTrigger), 1921 check(dsdev && dsdev != dev && (dsdev->GetDeviceInfo()->supportedRumbleMotors & GameInputRumbleLeftTrigger),
1792 "DualSense device reported with trigger motors"); 1922 "DualSense device reported with trigger motors");
1793 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");
1794 1927
1795 GameInputRumbleParams ds_rumble{0.0f, 0.0f, 1.0f, 0.5f}; 1928 GameInputRumbleParams ds_rumble{0.0f, 0.0f, 1.0f, 0.5f};
1796 dsdev->SetRumbleState(&ds_rumble); 1929 dsdev->SetRumbleState(&ds_rumble);
@@ -1846,6 +1979,9 @@ int main() {
1846 check(plaindev && (plaindev->GetDeviceInfo()->supportedRumbleMotors & GameInputRumbleLeftTrigger) == 0, 1979 check(plaindev && (plaindev->GetDeviceInfo()->supportedRumbleMotors & GameInputRumbleLeftTrigger) == 0,
1847 "a pad without trigger motors does not report them"); 1980 "a pad without trigger motors does not report them");
1848 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");
1849 GameInputRumbleParams p{0.25f, 0.25f, 1.0f, 1.0f}; 1985 GameInputRumbleParams p{0.25f, 0.25f, 1.0f, 1.0f};
1850 plaindev->SetRumbleState(&p); 1986 plaindev->SetRumbleState(&p);
1851 rt::gameinput::poll(); 1987 rt::gameinput::poll();