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:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 17:03:25 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 17:03:25 -0500
commit793e35af551467941c9d5550ab70836ea5a229e6 (patch)
tree960838fdfb81b66928accd408f3b3399be3299bd /runtime/test/gameinput_test.cpp
parente92964df1eeb9c734e0dd8a1ed39a05d8b140a40 (diff)
parent1fc162f4bab039a6de2254864b445804b782efa3 (diff)
Merge pull request #3685 from ICUMADETHIS
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
-rw-r--r--runtime/test/gameinput_test.cpp154
1 files changed, 153 insertions, 1 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp
index 1389b8f6d..679adf19f 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>
@@ -794,6 +795,156 @@ void test_keyboard_pad(IGameInput* input) {
794 rt::gameinput::set_cursor_source(nullptr); 795 rt::gameinput::set_cursor_source(nullptr);
795} 796}
796 797
798// active_input(): which kind of device the player used last. Virtual pads with a DualSense, a DualShock 4 and an Xbox
799// pad's vendor and product id (SDL takes the gamepad type from them) plus the keyboard pad; first with no wait
800// between changes (LEONIDA_PROMPTS_SWITCH_MS=0), then with the default 300 ms window on the real clock, then a family
801// forced by LEONIDA_PROMPTS.
802void test_active_input() {
803 using leonida::InputFamily;
804 const auto family = [] { return rt::gameinput::active_input().family; };
805 const auto generation = [] { return rt::gameinput::active_input().generation; };
806
807 set_env("LEONIDA_PROMPTS", nullptr);
808 set_env("LEONIDA_PROMPTS_SWITCH_MS", "0");
809 rt::gameinput::reload_config();
810 rt::gameinput::poll();
811 check(!rt::gameinput::active_input().forced, "active input: not forced without LEONIDA_PROMPTS");
812
813 Pad ds, ds4, xbox;
814 attach(ds, "Leonida virtual DualSense", 0x054C, 0x0CE6, false);
815 attach(ds4, "Leonida virtual DualShock 4", 0x054C, 0x09CC, false);
816 attach(xbox, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true);
817 SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_SOUTH, true); // held while it connects
818 const uint64_t at_connect = generation();
819 rt::gameinput::poll();
820 check(rt::gameinput::connected_count() == 3, "active input: three pads open");
821 check(generation() == at_connect, "active input: connecting pads (one with a button held) changes nothing");
822
823 auto press = [](Pad& p, SDL_GamepadButton b) {
824 SDL_SetJoystickVirtualButton(p.js, b, true);
825 rt::gameinput::poll();
826 SDL_SetJoystickVirtualButton(p.js, b, false);
827 rt::gameinput::poll();
828 };
829 SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_SOUTH, false); // the held button, let go
830 rt::gameinput::poll();
831 check(generation() == at_connect, "active input: letting go of a button is not input");
832
833 // The tests before this one leave whichever pad they used last; an Xbox press makes the start known.
834 press(xbox, SDL_GAMEPAD_BUTTON_SOUTH);
835 check(family() == InputFamily::Xbox, "active input: the Xbox pad in use");
836 const uint64_t g0 = generation();
837
838 press(ds, SDL_GAMEPAD_BUTTON_SOUTH);
839 check(family() == InputFamily::PlayStation5 && generation() == g0 + 1, "active input: DualSense press");
840 press(ds4, SDL_GAMEPAD_BUTTON_EAST);
841 check(family() == InputFamily::PlayStation4 && generation() == g0 + 2, "active input: DualShock 4 press");
842
843 // Stick drift on the Xbox pad (0.18) is not input; the stick pushed over is.
844 SDL_SetJoystickVirtualAxis(xbox.js, SDL_GAMEPAD_AXIS_LEFTX, 6000);
845 rt::gameinput::poll();
846 check(family() == InputFamily::PlayStation4 && generation() == g0 + 2, "active input: stick drift changes nothing");
847 SDL_SetJoystickVirtualAxis(xbox.js, SDL_GAMEPAD_AXIS_LEFTX, 32767);
848 rt::gameinput::poll();
849 check(family() == InputFamily::Xbox && generation() == g0 + 3, "active input: Xbox stick pushed over");
850 SDL_SetJoystickVirtualAxis(xbox.js, SDL_GAMEPAD_AXIS_LEFTX, 0);
851 rt::gameinput::poll();
852
853 // Buttons GameInput's gamepad state leaves out count too: the DualSense touchpad.
854 press(ds, SDL_GAMEPAD_BUTTON_TOUCHPAD);
855 check(family() == InputFamily::PlayStation5 && generation() == g0 + 4, "active input: DualSense touchpad click");
856 // A trigger pulled on the DualShock 4.
857 SDL_SetJoystickVirtualAxis(ds4.js, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 32767);
858 rt::gameinput::poll();
859 check(family() == InputFamily::PlayStation4 && generation() == g0 + 5, "active input: DualShock 4 trigger pull");
860 SDL_SetJoystickVirtualAxis(ds4.js, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, -32768);
861 rt::gameinput::poll();
862
863 // The keyboard pad: a key, and the mouse through the keyboard pad's right stick.
864 rt::gameinput::set_keyboard_source(fake_key_down, fake_focus);
865 rt::gameinput::set_cursor_source(fake_cursor);
866 std::memset(g_keys, 0, sizeof g_keys);
867 g_cursor_x = g_cursor_y = 0;
868 set_env("LEONIDA_KEYBOARD_PAD", "always");
869 rt::gameinput::reload_config();
870 rt::gameinput::poll(); // attaches the keyboard pad
871 rt::gameinput::poll(); // opens it: its first sample is the baseline
872 check(rt::gameinput::connected_count() == 4 && generation() == g0 + 5,
873 "active input: the keyboard pad connecting changes nothing");
874 g_keys[VK_RETURN] = true;
875 rt::gameinput::poll();
876 g_keys[VK_RETURN] = false;
877 rt::gameinput::poll();
878 check(family() == InputFamily::Keyboard && generation() == g0 + 6, "active input: Enter on the keyboard pad");
879 press(xbox, SDL_GAMEPAD_BUTTON_SOUTH);
880 check(family() == InputFamily::Xbox && generation() == g0 + 7, "active input: back to the Xbox pad");
881 g_cursor_x += 1; // a 1-count jitter stays well inside the stick radius
882 rt::gameinput::poll();
883 check(family() == InputFamily::Xbox, "active input: mouse jitter changes nothing");
884 g_cursor_x += 20; // a real movement pushes the right stick past the radius
885 rt::gameinput::poll();
886 check(family() == InputFamily::Keyboard && generation() == g0 + 8, "active input: mouse movement");
887 for (int i = 0; i < 80; ++i) rt::gameinput::poll(); // the stick falls back to rest
888
889 // The default window on the real clock: a second change within 300 ms of the last is ignored.
890 set_env("LEONIDA_PROMPTS_SWITCH_MS", nullptr);
891 rt::gameinput::reload_config();
892 check(rt::gameinput::config().prompts_switch_ms == 300, "active input: 300 ms by default");
893 Sleep(350);
894 press(ds, SDL_GAMEPAD_BUTTON_NORTH);
895 check(family() == InputFamily::PlayStation5 && generation() == g0 + 9, "active input: DualSense after the window");
896 press(xbox, SDL_GAMEPAD_BUTTON_WEST);
897 check(family() == InputFamily::PlayStation5 && generation() == g0 + 9,
898 "active input: an Xbox press right after a change is ignored");
899 Sleep(350);
900 press(xbox, SDL_GAMEPAD_BUTTON_WEST);
901 check(family() == InputFamily::Xbox && generation() == g0 + 10,
902 "active input: the Xbox press once the window passed");
903
904 // LEONIDA_PROMPTS forces a family; tracking goes on underneath and takes over again when it is cleared.
905 set_env("LEONIDA_PROMPTS_SWITCH_MS", "0");
906 set_env("LEONIDA_PROMPTS", "keyboard");
907 rt::gameinput::reload_config();
908 rt::gameinput::poll();
909 check(family() == InputFamily::Keyboard && rt::gameinput::active_input().forced && generation() == g0 + 11,
910 "active input: LEONIDA_PROMPTS=keyboard");
911 press(ds, SDL_GAMEPAD_BUTTON_SOUTH);
912 check(family() == InputFamily::Keyboard && generation() == g0 + 11,
913 "active input: forced, a DualSense press shows nothing");
914 set_env("LEONIDA_PROMPTS", "PlayStation"); // case does not matter
915 rt::gameinput::reload_config();
916 rt::gameinput::poll();
917 check(family() == InputFamily::PlayStation5 && rt::gameinput::active_input().forced && generation() == g0 + 12,
918 "active input: LEONIDA_PROMPTS=PlayStation (any case) is PlayStation 5");
919 set_env("LEONIDA_PROMPTS", "ps4");
920 rt::gameinput::reload_config();
921 rt::gameinput::poll();
922 check(family() == InputFamily::PlayStation4 && generation() == g0 + 13, "active input: LEONIDA_PROMPTS=ps4");
923 set_env("LEONIDA_PROMPTS", "nonsense"); // warns and follows the devices
924 rt::gameinput::reload_config();
925 rt::gameinput::poll();
926 check(!rt::gameinput::active_input().forced && family() == InputFamily::PlayStation5 && generation() == g0 + 14,
927 "active input: an unknown LEONIDA_PROMPTS follows the devices (the DualSense pressed while forced)");
928 set_env("LEONIDA_PROMPTS", "playstation");
929 rt::gameinput::reload_config();
930 rt::gameinput::poll();
931 check(family() == InputFamily::PlayStation5 && rt::gameinput::active_input().forced && generation() == g0 + 14,
932 "active input: forcing the family already in use changes no generation");
933
934 // Clean up: everything off and unplugged.
935 set_env("LEONIDA_PROMPTS", nullptr);
936 set_env("LEONIDA_PROMPTS_SWITCH_MS", nullptr);
937 set_env("LEONIDA_KEYBOARD_PAD", nullptr);
938 rt::gameinput::reload_config();
939 detach(ds);
940 detach(ds4);
941 detach(xbox);
942 rt::gameinput::poll();
943 check(rt::gameinput::connected_count() == 0, "active input: all pads closed at the end");
944 rt::gameinput::set_keyboard_source(nullptr, nullptr);
945 rt::gameinput::set_cursor_source(nullptr);
946}
947
797bool same_id(const AppLocalDeviceId& a, const AppLocalDeviceId& b) { return std::memcmp(&a, &b, sizeof a) == 0; } 948bool same_id(const AppLocalDeviceId& a, const AppLocalDeviceId& b) { return std::memcmp(&a, &b, sizeof a) == 0; }
798 949
799// The id the shim derives from SDL: the GUID, then two FNV-1a hashes of "s:" serial, "p:" path or nothing, with 950// The id the shim derives from SDL: the GUID, then two FNV-1a hashes of "s:" serial, "p:" path or nothing, with
@@ -1815,6 +1966,7 @@ int main() {
1815 rt::gameinput::poll(); 1966 rt::gameinput::poll();
1816 check(rt::gameinput::connected_count() == 0, "all pads closed after detaching"); 1967 check(rt::gameinput::connected_count() == 0, "all pads closed after detaching");
1817 1968
1969 test_active_input();
1818 test_twins(input); 1970 test_twins(input);
1819 test_keyboard_pad(input); 1971 test_keyboard_pad(input);
1820 test_motion(input); 1972 test_motion(input);