diff options
| author | ICUMADETHIS <icumadethis@gmail.com> | 2026-11-22 21:15:21 +0000 |
|---|---|---|
| committer | ICUMADETHIS <icumadethis@gmail.com> | 2026-11-22 21:17:56 +0000 |
| commit | 1fc162f4bab039a6de2254864b445804b782efa3 (patch) | |
| tree | 9e718a96ace450b0a3442960ae519e56c2d3f105 /runtime/test/gameinput_test.cpp | |
| parent | f847748cc76bd3777f9c070c4034eb2fc1c04e64 (diff) | |
gameinput: track which kind of device the player used last
The shim tells the title every pad is an Xbox One pad, so nothing could
say what the player is really holding. rt::gameinput::active_input()
now returns the family of the device used last (Xbox, PlayStation 5,
PlayStation 4, PlayStation 3, Nintendo, or the keyboard pad), a
generation that counts changes, and whether LEONIDA_PROMPTS forced it.
It is groundwork for button prompts that follow the device in use.
Only deliberate input counts: a button going down, a trigger past 0.3
or a stick leaving a 0.35 radius. Held input, stick drift and noise do
not; a pad's first reading after it connects is its baseline; and
after a change the next one waits LEONIDA_PROMPTS_SWITCH_MS (300 ms).
The rules live in runtime/src/active_input.h with no SDL, tested by
active_input_test; gameinput_test drives them on SDL virtual pads.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
| -rw-r--r-- | runtime/test/gameinput_test.cpp | 154 |
1 files changed, 153 insertions, 1 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 99e74a082..7d31f6043 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp | |||
| @@ -6,7 +6,8 @@ | |||
| 6 | // without trigger motors. The test covers enumeration and device callbacks, readings and their button/axis mapping, | 6 | // without trigger motors. The test covers enumeration and device callbacks, readings and their button/axis mapping, |
| 7 | // UI navigation, the guide button, rumble and trigger rumble as SDL receives them, the DualSense effect bytes for | 7 | // UI navigation, the guide button, rumble and trigger rumble as SDL receives them, the DualSense effect bytes for |
| 8 | // each LEONIDA_DS_TRIGGERS mode, the lightbar, a dispatcher, unplug/replug, reading history, | 8 | // each LEONIDA_DS_TRIGGERS mode, the lightbar, a dispatcher, unplug/replug, reading history, |
| 9 | // and identification of two or three otherwise indistinguishable pads. No game data. | 9 | // identification of two or three otherwise indistinguishable pads, and which kind of device was used last |
| 10 | // (active_input, LEONIDA_PROMPTS). No game data. | ||
| 10 | #include <windows.h> | 11 | #include <windows.h> |
| 11 | 12 | ||
| 12 | #include <SDL3/SDL.h> | 13 | #include <SDL3/SDL.h> |
| @@ -570,6 +571,156 @@ void test_keyboard_pad(IGameInput* input) { | |||
| 570 | rt::gameinput::set_cursor_source(nullptr); | 571 | rt::gameinput::set_cursor_source(nullptr); |
| 571 | } | 572 | } |
| 572 | 573 | ||
| 574 | // active_input(): which kind of device the player used last. Virtual pads with a DualSense, a DualShock 4 and an Xbox | ||
| 575 | // pad's vendor and product id (SDL takes the gamepad type from them) plus the keyboard pad; first with no wait | ||
| 576 | // between changes (LEONIDA_PROMPTS_SWITCH_MS=0), then with the default 300 ms window on the real clock, then a family | ||
| 577 | // forced by LEONIDA_PROMPTS. | ||
| 578 | void test_active_input() { | ||
| 579 | using leonida::InputFamily; | ||
| 580 | const auto family = [] { return rt::gameinput::active_input().family; }; | ||
| 581 | const auto generation = [] { return rt::gameinput::active_input().generation; }; | ||
| 582 | |||
| 583 | set_env("LEONIDA_PROMPTS", nullptr); | ||
| 584 | set_env("LEONIDA_PROMPTS_SWITCH_MS", "0"); | ||
| 585 | rt::gameinput::reload_config(); | ||
| 586 | rt::gameinput::poll(); | ||
| 587 | check(!rt::gameinput::active_input().forced, "active input: not forced without LEONIDA_PROMPTS"); | ||
| 588 | |||
| 589 | Pad ds, ds4, xbox; | ||
| 590 | attach(ds, "Leonida virtual DualSense", 0x054C, 0x0CE6, false); | ||
| 591 | attach(ds4, "Leonida virtual DualShock 4", 0x054C, 0x09CC, false); | ||
| 592 | attach(xbox, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true); | ||
| 593 | SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_SOUTH, true); // held while it connects | ||
| 594 | const uint64_t at_connect = generation(); | ||
| 595 | rt::gameinput::poll(); | ||
| 596 | check(rt::gameinput::connected_count() == 3, "active input: three pads open"); | ||
| 597 | check(generation() == at_connect, "active input: connecting pads (one with a button held) changes nothing"); | ||
| 598 | |||
| 599 | auto press = [](Pad& p, SDL_GamepadButton b) { | ||
| 600 | SDL_SetJoystickVirtualButton(p.js, b, true); | ||
| 601 | rt::gameinput::poll(); | ||
| 602 | SDL_SetJoystickVirtualButton(p.js, b, false); | ||
| 603 | rt::gameinput::poll(); | ||
| 604 | }; | ||
| 605 | SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_SOUTH, false); // the held button, let go | ||
| 606 | rt::gameinput::poll(); | ||
| 607 | check(generation() == at_connect, "active input: letting go of a button is not input"); | ||
| 608 | |||
| 609 | // The tests before this one leave whichever pad they used last; an Xbox press makes the start known. | ||
| 610 | press(xbox, SDL_GAMEPAD_BUTTON_SOUTH); | ||
| 611 | check(family() == InputFamily::Xbox, "active input: the Xbox pad in use"); | ||
| 612 | const uint64_t g0 = generation(); | ||
| 613 | |||
| 614 | press(ds, SDL_GAMEPAD_BUTTON_SOUTH); | ||
| 615 | check(family() == InputFamily::PlayStation5 && generation() == g0 + 1, "active input: DualSense press"); | ||
| 616 | press(ds4, SDL_GAMEPAD_BUTTON_EAST); | ||
| 617 | check(family() == InputFamily::PlayStation4 && generation() == g0 + 2, "active input: DualShock 4 press"); | ||
| 618 | |||
| 619 | // Stick drift on the Xbox pad (0.18) is not input; the stick pushed over is. | ||
| 620 | SDL_SetJoystickVirtualAxis(xbox.js, SDL_GAMEPAD_AXIS_LEFTX, 6000); | ||
| 621 | rt::gameinput::poll(); | ||
| 622 | check(family() == InputFamily::PlayStation4 && generation() == g0 + 2, "active input: stick drift changes nothing"); | ||
| 623 | SDL_SetJoystickVirtualAxis(xbox.js, SDL_GAMEPAD_AXIS_LEFTX, 32767); | ||
| 624 | rt::gameinput::poll(); | ||
| 625 | check(family() == InputFamily::Xbox && generation() == g0 + 3, "active input: Xbox stick pushed over"); | ||
| 626 | SDL_SetJoystickVirtualAxis(xbox.js, SDL_GAMEPAD_AXIS_LEFTX, 0); | ||
| 627 | rt::gameinput::poll(); | ||
| 628 | |||
| 629 | // Buttons GameInput's gamepad state leaves out count too: the DualSense touchpad. | ||
| 630 | press(ds, SDL_GAMEPAD_BUTTON_TOUCHPAD); | ||
| 631 | check(family() == InputFamily::PlayStation5 && generation() == g0 + 4, "active input: DualSense touchpad click"); | ||
| 632 | // A trigger pulled on the DualShock 4. | ||
| 633 | SDL_SetJoystickVirtualAxis(ds4.js, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 32767); | ||
| 634 | rt::gameinput::poll(); | ||
| 635 | check(family() == InputFamily::PlayStation4 && generation() == g0 + 5, "active input: DualShock 4 trigger pull"); | ||
| 636 | SDL_SetJoystickVirtualAxis(ds4.js, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, -32768); | ||
| 637 | rt::gameinput::poll(); | ||
| 638 | |||
| 639 | // The keyboard pad: a key, and the mouse through the keyboard pad's right stick. | ||
| 640 | rt::gameinput::set_keyboard_source(fake_key_down, fake_focus); | ||
| 641 | rt::gameinput::set_cursor_source(fake_cursor); | ||
| 642 | std::memset(g_keys, 0, sizeof g_keys); | ||
| 643 | g_cursor_x = g_cursor_y = 0; | ||
| 644 | set_env("LEONIDA_KEYBOARD_PAD", "always"); | ||
| 645 | rt::gameinput::reload_config(); | ||
| 646 | rt::gameinput::poll(); // attaches the keyboard pad | ||
| 647 | rt::gameinput::poll(); // opens it: its first sample is the baseline | ||
| 648 | check(rt::gameinput::connected_count() == 4 && generation() == g0 + 5, | ||
| 649 | "active input: the keyboard pad connecting changes nothing"); | ||
| 650 | g_keys[VK_RETURN] = true; | ||
| 651 | rt::gameinput::poll(); | ||
| 652 | g_keys[VK_RETURN] = false; | ||
| 653 | rt::gameinput::poll(); | ||
| 654 | check(family() == InputFamily::Keyboard && generation() == g0 + 6, "active input: Enter on the keyboard pad"); | ||
| 655 | press(xbox, SDL_GAMEPAD_BUTTON_SOUTH); | ||
| 656 | check(family() == InputFamily::Xbox && generation() == g0 + 7, "active input: back to the Xbox pad"); | ||
| 657 | g_cursor_x += 1; // a 1-count jitter stays well inside the stick radius | ||
| 658 | rt::gameinput::poll(); | ||
| 659 | check(family() == InputFamily::Xbox, "active input: mouse jitter changes nothing"); | ||
| 660 | g_cursor_x += 20; // a real movement pushes the right stick past the radius | ||
| 661 | rt::gameinput::poll(); | ||
| 662 | check(family() == InputFamily::Keyboard && generation() == g0 + 8, "active input: mouse movement"); | ||
| 663 | for (int i = 0; i < 80; ++i) rt::gameinput::poll(); // the stick falls back to rest | ||
| 664 | |||
| 665 | // The default window on the real clock: a second change within 300 ms of the last is ignored. | ||
| 666 | set_env("LEONIDA_PROMPTS_SWITCH_MS", nullptr); | ||
| 667 | rt::gameinput::reload_config(); | ||
| 668 | check(rt::gameinput::config().prompts_switch_ms == 300, "active input: 300 ms by default"); | ||
| 669 | Sleep(350); | ||
| 670 | press(ds, SDL_GAMEPAD_BUTTON_NORTH); | ||
| 671 | check(family() == InputFamily::PlayStation5 && generation() == g0 + 9, "active input: DualSense after the window"); | ||
| 672 | press(xbox, SDL_GAMEPAD_BUTTON_WEST); | ||
| 673 | check(family() == InputFamily::PlayStation5 && generation() == g0 + 9, | ||
| 674 | "active input: an Xbox press right after a change is ignored"); | ||
| 675 | Sleep(350); | ||
| 676 | press(xbox, SDL_GAMEPAD_BUTTON_WEST); | ||
| 677 | check(family() == InputFamily::Xbox && generation() == g0 + 10, | ||
| 678 | "active input: the Xbox press once the window passed"); | ||
| 679 | |||
| 680 | // LEONIDA_PROMPTS forces a family; tracking goes on underneath and takes over again when it is cleared. | ||
| 681 | set_env("LEONIDA_PROMPTS_SWITCH_MS", "0"); | ||
| 682 | set_env("LEONIDA_PROMPTS", "keyboard"); | ||
| 683 | rt::gameinput::reload_config(); | ||
| 684 | rt::gameinput::poll(); | ||
| 685 | check(family() == InputFamily::Keyboard && rt::gameinput::active_input().forced && generation() == g0 + 11, | ||
| 686 | "active input: LEONIDA_PROMPTS=keyboard"); | ||
| 687 | press(ds, SDL_GAMEPAD_BUTTON_SOUTH); | ||
| 688 | check(family() == InputFamily::Keyboard && generation() == g0 + 11, | ||
| 689 | "active input: forced, a DualSense press shows nothing"); | ||
| 690 | set_env("LEONIDA_PROMPTS", "PlayStation"); // case does not matter | ||
| 691 | rt::gameinput::reload_config(); | ||
| 692 | rt::gameinput::poll(); | ||
| 693 | check(family() == InputFamily::PlayStation5 && rt::gameinput::active_input().forced && generation() == g0 + 12, | ||
| 694 | "active input: LEONIDA_PROMPTS=PlayStation (any case) is PlayStation 5"); | ||
| 695 | set_env("LEONIDA_PROMPTS", "ps4"); | ||
| 696 | rt::gameinput::reload_config(); | ||
| 697 | rt::gameinput::poll(); | ||
| 698 | check(family() == InputFamily::PlayStation4 && generation() == g0 + 13, "active input: LEONIDA_PROMPTS=ps4"); | ||
| 699 | set_env("LEONIDA_PROMPTS", "nonsense"); // warns and follows the devices | ||
| 700 | rt::gameinput::reload_config(); | ||
| 701 | rt::gameinput::poll(); | ||
| 702 | check(!rt::gameinput::active_input().forced && family() == InputFamily::PlayStation5 && generation() == g0 + 14, | ||
| 703 | "active input: an unknown LEONIDA_PROMPTS follows the devices (the DualSense pressed while forced)"); | ||
| 704 | set_env("LEONIDA_PROMPTS", "playstation"); | ||
| 705 | rt::gameinput::reload_config(); | ||
| 706 | rt::gameinput::poll(); | ||
| 707 | check(family() == InputFamily::PlayStation5 && rt::gameinput::active_input().forced && generation() == g0 + 14, | ||
| 708 | "active input: forcing the family already in use changes no generation"); | ||
| 709 | |||
| 710 | // Clean up: everything off and unplugged. | ||
| 711 | set_env("LEONIDA_PROMPTS", nullptr); | ||
| 712 | set_env("LEONIDA_PROMPTS_SWITCH_MS", nullptr); | ||
| 713 | set_env("LEONIDA_KEYBOARD_PAD", nullptr); | ||
| 714 | rt::gameinput::reload_config(); | ||
| 715 | detach(ds); | ||
| 716 | detach(ds4); | ||
| 717 | detach(xbox); | ||
| 718 | rt::gameinput::poll(); | ||
| 719 | check(rt::gameinput::connected_count() == 0, "active input: all pads closed at the end"); | ||
| 720 | rt::gameinput::set_keyboard_source(nullptr, nullptr); | ||
| 721 | rt::gameinput::set_cursor_source(nullptr); | ||
| 722 | } | ||
| 723 | |||
| 573 | bool same_id(const AppLocalDeviceId& a, const AppLocalDeviceId& b) { return std::memcmp(&a, &b, sizeof a) == 0; } | 724 | bool same_id(const AppLocalDeviceId& a, const AppLocalDeviceId& b) { return std::memcmp(&a, &b, sizeof a) == 0; } |
| 574 | 725 | ||
| 575 | // The id the shim derives from SDL: the GUID, then two FNV-1a hashes of "s:" serial, "p:" path or nothing, with | 726 | // The id the shim derives from SDL: the GUID, then two FNV-1a hashes of "s:" serial, "p:" path or nothing, with |
| @@ -1569,6 +1720,7 @@ int main() { | |||
| 1569 | rt::gameinput::poll(); | 1720 | rt::gameinput::poll(); |
| 1570 | check(rt::gameinput::connected_count() == 0, "all pads closed after detaching"); | 1721 | check(rt::gameinput::connected_count() == 0, "all pads closed after detaching"); |
| 1571 | 1722 | ||
| 1723 | test_active_input(); | ||
| 1572 | test_twins(input); | 1724 | test_twins(input); |
| 1573 | test_keyboard_pad(input); | 1725 | test_keyboard_pad(input); |
| 1574 | test_motion(input); | 1726 | test_motion(input); |