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