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