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 13:16:07 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 13:16:07 -0500
commitc1543e3449b819997ad9dcf629f423fe20ad47c5 (patch)
treef8d0c2a4cf642dedbb6b07e76bfe8931a3c7f71e /runtime/test/gameinput_test.cpp
parent3d293c2c8243971412afe1b330d88072a9034ac4 (diff)
parentaf330fcce33843c94c2f446986b82f66cef00538 (diff)
Merge pull request #2552 from Leonida local test
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
-rw-r--r--runtime/test/gameinput_test.cpp320
1 files changed, 320 insertions, 0 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp
index a65b444a1..93eda4085 100644
--- a/runtime/test/gameinput_test.cpp
+++ b/runtime/test/gameinput_test.cpp
@@ -763,6 +763,324 @@ void test_twins(IGameInput* input) {
763 check(rt::gameinput::connected_count() == 0, "twins: removed at the end"); 763 check(rt::gameinput::connected_count() == 0, "twins: removed at the end");
764} 764}
765 765
766// ---- force feedback and haptic motors: a fake SDL haptic backend ----
767// SDL's virtual joysticks report no haptics, so the shim's haptic calls go through this table instead. It records what
768// the shim asked SDL to do and reports the capabilities a force-feedback wheel has.
769struct FakeHaptic {
770 uint32_t features = SDL_HAPTIC_CONSTANT | SDL_HAPTIC_SINE | SDL_HAPTIC_SQUARE | SDL_HAPTIC_TRIANGLE |
771 SDL_HAPTIC_SAWTOOTHUP | SDL_HAPTIC_SAWTOOTHDOWN | SDL_HAPTIC_RAMP | SDL_HAPTIC_SPRING |
772 SDL_HAPTIC_FRICTION | SDL_HAPTIC_DAMPER | SDL_HAPTIC_INERTIA;
773 int max_effects = 8;
774 int opens = 0, closes = 0, creates = 0, updates = 0, runs = 0, stops = 0, destroys = 0;
775 int gain_calls = 0, last_gain = -1;
776 uint32_t last_iterations = 0;
777 SDL_HapticEffect last{};
778};
779FakeHaptic g_fake;
780bool g_fake_open = false; // pads attached while this is set are opened as haptic devices
781
782void* fake_open(void*) {
783 if (!g_fake_open) return nullptr;
784 ++g_fake.opens;
785 return &g_fake;
786}
787void fake_close(void*) { ++g_fake.closes; }
788uint32_t fake_features(void* h) { return static_cast<FakeHaptic*>(h)->features; }
789int fake_max_effects(void* h) { return static_cast<FakeHaptic*>(h)->max_effects; }
790int fake_create(void* h, const void* e) {
791 FakeHaptic* f = static_cast<FakeHaptic*>(h);
792 ++f->creates;
793 f->last = *static_cast<const SDL_HapticEffect*>(e);
794 return 1;
795}
796bool fake_update(void* h, int, const void* e) {
797 FakeHaptic* f = static_cast<FakeHaptic*>(h);
798 ++f->updates;
799 f->last = *static_cast<const SDL_HapticEffect*>(e);
800 return true;
801}
802bool fake_run(void* h, int, uint32_t iterations) {
803 FakeHaptic* f = static_cast<FakeHaptic*>(h);
804 ++f->runs;
805 f->last_iterations = iterations;
806 return true;
807}
808bool fake_stop(void* h, int) {
809 ++static_cast<FakeHaptic*>(h)->stops;
810 return true;
811}
812void fake_destroy(void* h, int) { ++static_cast<FakeHaptic*>(h)->destroys; }
813bool fake_gain(void* h, int gain) {
814 FakeHaptic* f = static_cast<FakeHaptic*>(h);
815 ++f->gain_calls;
816 f->last_gain = gain;
817 return true;
818}
819const rt::gameinput::HapticApi kFakeHaptic{fake_open, fake_close, fake_features, fake_max_effects, fake_create,
820 fake_update, fake_run, fake_stop, fake_destroy, fake_gain};
821
822// The motors of one device, and the effect object its force-feedback motor hands out. Runs with no other pad.
823void test_force_feedback(IGameInput* input) {
824 rt::gameinput::set_haptic_source(&kFakeHaptic);
825 g_fake = {};
826 g_fake_open = true;
827 Pad wheel;
828 attach(wheel, "Leonida virtual wheel", 0x046D, 0xC24F, false);
829 const size_t before = g_device_events.size();
830 rt::gameinput::poll();
831 IGameInputDevice* dev = g_device_events.size() > before ? g_device_events.back().device : nullptr;
832 check(dev && rt::gameinput::connected_count() == 1 && g_fake.opens == 1,
833 "force feedback: the pad's joystick is opened as an SDL haptic device");
834 if (!dev) return;
835 dev->AddRef();
836 rt::gameinput::OutputState out{};
837 check(rt::gameinput::output_state(0, &out) && out.force_feedback, "force feedback: the output state says so");
838 IGameInputReading* r = current(input, dev);
839 check(r != nullptr, "force feedback: the pad still reads normally with a haptic device open");
840 if (r) r->Release();
841
842 const GameInputDeviceInfo* info = dev->GetDeviceInfo();
843 check(info->forceFeedbackMotorCount == 1 && info->forceFeedbackMotorInfo != nullptr,
844 "force feedback: the device reports one force-feedback motor");
845 const GameInputForceFeedbackMotorInfo* motor =
846 static_cast<const GameInputForceFeedbackMotorInfo*>(info->forceFeedbackMotorInfo);
847 check(motor->supportedAxes == GameInputFeedbackAxisLinearX && motor->location == GameInputLocationChassis &&
848 motor->maxSimultaneousEffects == 8,
849 "force feedback: motorInfo carries the axis, the location and the effect limit");
850 check(motor->isConstantEffectSupported && motor->isRampEffectSupported && motor->isSpringEffectSupported &&
851 motor->isSawtoothDownWaveEffectSupported,
852 "force feedback: the effect kinds come from SDL's feature bits");
853 check(info->hapticFeedbackMotorCount == 0, "force feedback: a wheel without trigger motors reports no haptics");
854 check(dev->IsForceFeedbackMotorPoweredOn(0) && !dev->IsForceFeedbackMotorPoweredOn(1),
855 "force feedback: motor 0 is powered on, and motor 1 does not exist");
856
857 IGameInputForceFeedbackEffect* effect = nullptr;
858 GameInputForceFeedbackParams params{};
859 params.kind = GameInputForceFeedbackConstant;
860 params.data.constant.magnitude.linearX = 0.5f;
861 check(dev->CreateForceFeedbackEffect(1, &params, reinterpret_cast<void**>(&effect)) == E_INVALIDARG && !effect,
862 "force feedback: motor 1 does not exist");
863 check(dev->CreateForceFeedbackEffect(0, &params, nullptr) == E_POINTER, "force feedback: a null effect is E_POINTER");
864 check(dev->CreateForceFeedbackEffect(0, nullptr, reinterpret_cast<void**>(&effect)) == E_INVALIDARG,
865 "force feedback: null parameters are E_INVALIDARG");
866 GameInputForceFeedbackParams unknown{};
867 unknown.kind = static_cast<GameInputForceFeedbackEffectKind>(11);
868 check(dev->CreateForceFeedbackEffect(0, &unknown, reinterpret_cast<void**>(&effect)) == E_INVALIDARG,
869 "force feedback: an effect kind outside the enum is E_INVALIDARG");
870 check(dev->CreateForceFeedbackEffect(0, &params, reinterpret_cast<void**>(&effect)) == S_OK && effect,
871 "force feedback: CreateForceFeedbackEffect");
872 if (!effect) { // leave nothing behind for the next test
873 detach(wheel);
874 dev->Release();
875 rt::gameinput::poll();
876 rt::gameinput::set_haptic_source(nullptr);
877 return;
878 }
879 check(effect->GetMotorIndex() == 0 && approx(effect->GetGain(), 1.0f) && effect->GetState() == GameInputFeedbackRunning,
880 "force feedback: a new effect is on motor 0, at full gain, running");
881 IGameInputDevice* owner = nullptr;
882 effect->GetDevice(&owner);
883 check(owner == dev, "force feedback: the effect names its device");
884 if (owner) owner->Release();
885
886 // Nothing is asked of SDL before the poll thread runs: the title only wrote the parameters.
887 const int runs0 = g_fake.runs;
888 rt::gameinput::poll();
889 check(g_fake.creates == 1 && g_fake.runs == runs0 + 1, "force feedback: the poll thread creates and runs the effect");
890 check(g_fake.last.type == SDL_HAPTIC_CONSTANT && g_fake.last.constant.level == 16384,
891 "force feedback: a constant 0.5 becomes SDL_HAPTIC_CONSTANT level 16384");
892 check(g_fake.last.constant.length == 60000 && g_fake.last_iterations == 0,
893 "force feedback: no duration in GameInput is SDL's 60 s hold, run until stopped");
894 rt::gameinput::poll();
895 check(g_fake.runs == runs0 + 1, "force feedback: an unchanged effect is not re-sent every poll");
896
897 params.data.constant.magnitude.linearX = -1.0f;
898 check(effect->SetParams(&params), "force feedback: SetParams");
899 rt::gameinput::poll();
900 check(g_fake.updates == 1 && g_fake.runs == runs0 + 2 && g_fake.last.constant.level == -32767,
901 "force feedback: changed parameters update the SDL effect once");
902 GameInputForceFeedbackParams read{};
903 effect->GetParams(&read);
904 check(read.kind == GameInputForceFeedbackConstant && read.data.constant.magnitude.linearX == -1.0f,
905 "force feedback: GetParams returns what was set");
906
907 effect->SetGain(0.5f);
908 rt::gameinput::poll();
909 check(approx(effect->GetGain(), 0.5f) && g_fake.last.constant.level == -16384,
910 "force feedback: the effect's gain scales the SDL level");
911 // The device gain is SDL's, sent once when the pad is opened (100) and once per change.
912 check(g_fake.last_gain == 100 && g_fake.gain_calls == 1, "force feedback: the device gain starts at SDL's full 100");
913 dev->SetForceFeedbackMotorGain(0, 0.5f);
914 rt::gameinput::poll();
915 check(g_fake.last_gain == 50 && g_fake.gain_calls == 2,
916 "force feedback: SetForceFeedbackMotorGain sets SDL's device gain once");
917 dev->SetForceFeedbackMotorGain(0, 1.0f);
918 effect->SetGain(1.0f);
919 rt::gameinput::poll();
920 check(g_fake.last_gain == 100, "force feedback: the master gain follows the request");
921
922 // A wave with GameInput's envelope: 50 Hz for 90 degrees over 100 ms, played once.
923 GameInputForceFeedbackParams wave{};
924 wave.kind = GameInputForceFeedbackSineWave;
925 wave.data.sineWave.envelope.sustainDuration = 100'000; // microseconds
926 wave.data.sineWave.envelope.sustainGain = 1.0f;
927 wave.data.sineWave.envelope.playCount = 1;
928 wave.data.sineWave.magnitude.linearX = 1.0f;
929 wave.data.sineWave.frequency = 50.0f;
930 wave.data.sineWave.phase = 90.0f;
931 check(effect->SetParams(&wave), "force feedback: SetParams for a sine wave");
932 rt::gameinput::poll();
933 check(g_fake.last.type == SDL_HAPTIC_SINE && g_fake.last.periodic.period == 20 &&
934 g_fake.last.periodic.magnitude == 32767 && g_fake.last.periodic.phase == 9000,
935 "force feedback: a 50 Hz sine with a 90 degree phase becomes SDL's period and hundredths of a degree");
936 check(g_fake.last.periodic.length == 100 && g_fake.last_iterations == 1,
937 "force feedback: the envelope's 100 ms is SDL's length and its playCount the iteration count");
938 Sleep(130);
939 rt::gameinput::poll();
940 check(effect->GetState() == GameInputFeedbackStopped && g_fake.stops >= 1,
941 "force feedback: an effect that ran its duration reports stopped");
942
943 // A condition effect: coefficients, saturations, dead zone and centre.
944 GameInputForceFeedbackParams spring{};
945 spring.kind = GameInputForceFeedbackSpring;
946 spring.data.spring.magnitude.linearX = 0.5f;
947 spring.data.spring.positiveCoefficient = 0.5f;
948 spring.data.spring.negativeCoefficient = 0.25f;
949 spring.data.spring.maxPositiveMagnitude = 1.0f;
950 spring.data.spring.maxNegativeMagnitude = 0.5f;
951 spring.data.spring.deadZone = 0.1f;
952 check(effect->SetParams(&spring), "force feedback: SetParams for a spring");
953 effect->SetState(GameInputFeedbackRunning);
954 rt::gameinput::poll();
955 check(g_fake.last.type == SDL_HAPTIC_SPRING && g_fake.last.condition.right_coeff[0] == 16384 &&
956 g_fake.last.condition.left_coeff[0] == -8192 && g_fake.last.condition.right_sat[0] == 65535 &&
957 g_fake.last.condition.left_sat[0] == 32768 && g_fake.last.condition.deadband[0] == 6554 &&
958 g_fake.last.condition.center[0] == 16384,
959 "force feedback: a spring maps its coefficients, saturations, dead zone and centre");
960
961 effect->SetState(GameInputFeedbackPaused);
962 const int stops0 = g_fake.stops;
963 rt::gameinput::poll();
964 check(g_fake.stops == stops0 + 1 && effect->GetState() == GameInputFeedbackPaused,
965 "force feedback: a paused effect is stopped in SDL");
966
967 // LEONIDA_RUMBLE=0 silences this path too: a running effect is stopped, and it runs again when the mute lifts.
968 effect->SetState(GameInputFeedbackRunning);
969 rt::gameinput::poll();
970 const int runs1 = g_fake.runs, stops1 = g_fake.stops;
971 check(g_fake.runs == runs1, "force feedback: the effect is running before the mute");
972 set_env("LEONIDA_RUMBLE", "0");
973 rt::gameinput::reload_config();
974 rt::gameinput::poll();
975 check(g_fake.runs == runs1 && g_fake.stops == stops1 + 1, "force feedback: LEONIDA_RUMBLE=0 stops the effect");
976 set_env("LEONIDA_RUMBLE", nullptr);
977 rt::gameinput::reload_config();
978 rt::gameinput::poll();
979 check(g_fake.runs == runs1 + 1, "force feedback: and it runs again once the mute is lifted");
980
981 check(effect->Release() == 0, "force feedback: the effect is freed once the title releases it");
982 rt::gameinput::poll();
983 check(g_fake.destroys == 1, "force feedback: its SDL effect is destroyed on the poll thread");
984
985 detach(wheel);
986 rt::gameinput::poll();
987 check(g_fake.closes == 1 && !dev->IsForceFeedbackMotorPoweredOn(0),
988 "force feedback: unplugging closes the haptic device");
989 dev->Release();
990
991 // A pad SDL opens no haptic device for keeps the old answer: no motor, and no effect.
992 g_fake_open = false;
993 Pad plain;
994 attach(plain, "Leonida virtual plain pad", 0x045E, 0x02FF, false);
995 const size_t plain_before = g_device_events.size();
996 rt::gameinput::poll();
997 IGameInputDevice* plaindev = g_device_events.size() > plain_before ? g_device_events.back().device : nullptr;
998 check(plaindev && plaindev->GetDeviceInfo()->forceFeedbackMotorCount == 0 &&
999 plaindev->GetDeviceInfo()->forceFeedbackMotorInfo == nullptr,
1000 "force feedback: a pad without SDL haptics reports no motor");
1001 if (plaindev) {
1002 effect = nullptr;
1003 check(plaindev->CreateForceFeedbackEffect(0, &params, reinterpret_cast<void**>(&effect)) == E_INVALIDARG && !effect,
1004 "force feedback: so CreateForceFeedbackEffect is E_INVALIDARG");
1005 check(!plaindev->IsForceFeedbackMotorPoweredOn(0), "force feedback: and no motor is powered on");
1006 }
1007 detach(plain);
1008 rt::gameinput::poll();
1009 rt::gameinput::set_haptic_source(nullptr);
1010}
1011
1012// The trigger haptic motors of a pad that has them. Runs with no other pad.
1013void test_haptic_motors(IGameInput* input) {
1014 set_env("LEONIDA_DS_TRIGGERS", nullptr);
1015 rt::gameinput::reload_config();
1016 Pad xbox;
1017 attach(xbox, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true);
1018 const size_t before = g_device_events.size();
1019 rt::gameinput::poll();
1020 IGameInputDevice* dev = g_device_events.size() > before ? g_device_events.back().device : nullptr;
1021 check(dev && rt::gameinput::connected_count() == 1, "haptics: the pad with trigger motors is connected");
1022 if (!dev) return;
1023 dev->AddRef();
1024 const GameInputDeviceInfo* info = dev->GetDeviceInfo();
1025 check(info->hapticFeedbackMotorCount == 2 && info->hapticFeedbackMotorInfo != nullptr,
1026 "haptics: the pad reports two haptic motors");
1027 const GameInputHapticFeedbackMotorInfo* motors =
1028 static_cast<const GameInputHapticFeedbackMotorInfo*>(info->hapticFeedbackMotorInfo);
1029 check(motors[0].mappedRumbleMotors == GameInputRumbleLeftTrigger &&
1030 motors[1].mappedRumbleMotors == GameInputRumbleRightTrigger && motors[0].waveformCount == 1 &&
1031 motors[0].waveformInfo != nullptr && motors[0].waveformInfo->isIntensitySupported,
1032 "haptics: they are the left and right trigger, one waveform each, an intensity");
1033 check(sticks_at_rest(pad_state(input, dev)), "haptics: the pad still reads normally");
1034
1035 GameInputHapticFeedbackParams p{};
1036 p.intensity = 0.5f;
1037 p.duration = 100'000; // microseconds
1038 p.playCount = 1;
1039 check(dev->SetHapticMotorState(2, &p) == E_INVALIDARG, "haptics: motor 2 does not exist");
1040 GameInputHapticFeedbackParams other_wave = p;
1041 other_wave.waveformIndex = 1;
1042 check(dev->SetHapticMotorState(0, &other_wave) == E_INVALIDARG, "haptics: waveform 1 does not exist");
1043 check(dev->SetHapticMotorState(0, &p) == S_OK, "haptics: SetHapticMotorState on the left trigger");
1044 rt::gameinput::poll();
1045 check(xbox.left == 32768 && xbox.right == 0, "haptics: the intensity reaches the trigger motor");
1046 Sleep(130);
1047 rt::gameinput::poll();
1048 check(xbox.left == 0, "haptics: the request ends with its duration");
1049
1050 p.intensity = 1.0f;
1051 p.playCount = 0; // until changed
1052 check(dev->SetHapticMotorState(1, &p) == S_OK, "haptics: playCount 0 plays until changed");
1053 rt::gameinput::poll();
1054 check(xbox.right == 65535 && xbox.left == 0, "haptics: the right trigger takes its own motor");
1055 Sleep(130);
1056 rt::gameinput::poll();
1057 check(xbox.right == 65535, "haptics: and keeps playing past the duration");
1058 check(dev->SetHapticMotorState(1, nullptr) == S_OK, "haptics: a null state stops the motor");
1059 rt::gameinput::poll();
1060 check(xbox.right == 0 && xbox.left == 0, "haptics: both triggers are silent again");
1061
1062 // SetRumbleState still works on the same pad, and the two share the trigger output.
1063 GameInputRumbleParams rumble{0, 0, 0.25f, 0};
1064 dev->SetRumbleState(&rumble);
1065 rt::gameinput::poll();
1066 check(xbox.left == 16384, "haptics: SetRumbleState still drives the trigger motor");
1067 p.intensity = 1.0f;
1068 p.playCount = 1;
1069 p.duration = 0; // no deadline at playCount 1 either: the level holds until it is set again
1070 dev->SetHapticMotorState(0, &p);
1071 rt::gameinput::poll();
1072 check(xbox.left == 65535, "haptics: the stronger of the two wins on one trigger");
1073 dev->SetRumbleState(nullptr);
1074 dev->SetHapticMotorState(0, nullptr);
1075 rt::gameinput::poll();
1076 check(xbox.left == 0, "haptics: releasing both leaves the trigger silent");
1077
1078 detach(xbox);
1079 rt::gameinput::poll();
1080 check(rt::gameinput::connected_count() == 0, "haptics: detached");
1081 dev->Release();
1082}
1083
766} // namespace 1084} // namespace
767 1085
768int main() { 1086int main() {
@@ -1226,6 +1544,8 @@ int main() {
1226 test_keyboard_pad(input); 1544 test_keyboard_pad(input);
1227 test_motion(input); 1545 test_motion(input);
1228 test_aggregate_device(input); 1546 test_aggregate_device(input);
1547 test_force_feedback(input);
1548 test_haptic_motors(input);
1229 input->Release(); 1549 input->Release();
1230 1550
1231 std::printf("%d checks, %d failures\n", g_checks, g_failures); 1551 std::printf("%d checks, %d failures\n", g_checks, g_failures);