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:
authorBora Sevim <sevimbora@gmail.com>2026-11-22 23:05:47 +0300
committerBora Sevim <sevimbora@gmail.com>2026-11-22 23:05:47 +0300
commit76d1add3dab43a082332579ca33f3f039746ce6b (patch)
tree2b05cfb8571fa15ce31e0da8941533ed874218f7 /runtime/test/gameinput_test.cpp
parent364fdcbd28feb1708fd28b4727b3cc8f4a1ad1a7 (diff)
parentf847748cc76bd3777f9c070c4034eb2fc1c04e64 (diff)
Merge origin/main into rt-175-raw-hid
Keep RT-160's force-feedback interface and tests next to the raw HID ones. The raw HID test fakes are renamed (FakeRawHid, raw_*) so they no longer clash with the haptic fakes. The API table gets one IGameInputDevice row with both feature sets; the stale duplicate IGameInputDevice and IGameInputDispatcher rows are dropped.
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
-rw-r--r--runtime/test/gameinput_test.cpp423
1 files changed, 386 insertions, 37 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp
index 47fd599c7..ff4109c01 100644
--- a/runtime/test/gameinput_test.cpp
+++ b/runtime/test/gameinput_test.cpp
@@ -132,8 +132,11 @@ void CALLBACK on_device(GameInputCallbackToken token, void*, IGameInputDevice* d
132 uint32_t previous) { 132 uint32_t previous) {
133 g_device_events.push_back({device, current, previous, token}); 133 g_device_events.push_back({device, current, previous, token});
134} 134}
135DWORD g_reading_sleep_ms = 0; // makes on_reading slow, for the Dispatch quota checks
136
135void CALLBACK on_reading(GameInputCallbackToken, void*, IGameInputReading* reading, bool) { 137void CALLBACK on_reading(GameInputCallbackToken, void*, IGameInputReading* reading, bool) {
136 if (reading) ++g_reading_events; 138 if (reading) ++g_reading_events;
139 if (g_reading_sleep_ms) Sleep(g_reading_sleep_ms);
137} 140}
138void CALLBACK on_guide(GameInputCallbackToken, void*, IGameInputDevice*, uint64_t, bool pressed) { 141void CALLBACK on_guide(GameInputCallbackToken, void*, IGameInputDevice*, uint64_t, bool pressed) {
139 g_guide_events.push_back(pressed); 142 g_guide_events.push_back(pressed);
@@ -282,7 +285,7 @@ void test_parse_report_descriptor() {
282} 285}
283 286
284// The fake HID device behind the raw pad: input reports to hand out, what the shim wrote, the feature reply. 287// The fake HID device behind the raw pad: input reports to hand out, what the shim wrote, the feature reply.
285struct FakeHid { 288struct FakeRawHid {
286 uint16_t vendor = 0x1209, product = 0x0001; // pid.codes test ids, not a real pad 289 uint16_t vendor = 0x1209, product = 0x0001; // pid.codes test ids, not a real pad
287 int opens = 0, closes = 0; 290 int opens = 0, closes = 0;
288 std::deque<std::vector<uint8_t>> inputs; 291 std::deque<std::vector<uint8_t>> inputs;
@@ -290,44 +293,44 @@ struct FakeHid {
290 std::vector<uint8_t> feature_reply; 293 std::vector<uint8_t> feature_reply;
291 bool fail = false; 294 bool fail = false;
292}; 295};
293FakeHid g_fake; 296FakeRawHid g_raw;
294void* fake_open(const char*, uint16_t vendor, uint16_t product) { 297void* raw_open(const char*, uint16_t vendor, uint16_t product) {
295 if (vendor != g_fake.vendor || product != g_fake.product) return nullptr; 298 if (vendor != g_raw.vendor || product != g_raw.product) return nullptr;
296 ++g_fake.opens; 299 ++g_raw.opens;
297 return &g_fake; 300 return &g_raw;
298} 301}
299void fake_close(void*) { ++g_fake.closes; } 302void raw_close(void*) { ++g_raw.closes; }
300int fake_descriptor(void*, uint8_t* buf, size_t size) { 303int raw_descriptor(void*, uint8_t* buf, size_t size) {
301 if (size < sizeof kRawPadDescriptor) return -1; 304 if (size < sizeof kRawPadDescriptor) return -1;
302 std::memcpy(buf, kRawPadDescriptor, sizeof kRawPadDescriptor); 305 std::memcpy(buf, kRawPadDescriptor, sizeof kRawPadDescriptor);
303 return static_cast<int>(sizeof kRawPadDescriptor); 306 return static_cast<int>(sizeof kRawPadDescriptor);
304} 307}
305int fake_read(void*, uint8_t* buf, size_t size) { 308int raw_read(void*, uint8_t* buf, size_t size) {
306 if (g_fake.inputs.empty()) return 0; 309 if (g_raw.inputs.empty()) return 0;
307 const std::vector<uint8_t> r = g_fake.inputs.front(); 310 const std::vector<uint8_t> r = g_raw.inputs.front();
308 g_fake.inputs.pop_front(); 311 g_raw.inputs.pop_front();
309 const size_t n = std::min(size, r.size()); 312 const size_t n = std::min(size, r.size());
310 std::memcpy(buf, r.data(), n); 313 std::memcpy(buf, r.data(), n);
311 return static_cast<int>(n); 314 return static_cast<int>(n);
312} 315}
313int fake_write(void*, const uint8_t* buf, size_t size) { 316int raw_write(void*, const uint8_t* buf, size_t size) {
314 if (g_fake.fail) return -1; 317 if (g_raw.fail) return -1;
315 g_fake.writes.emplace_back(buf, buf + size); 318 g_raw.writes.emplace_back(buf, buf + size);
316 return static_cast<int>(size); 319 return static_cast<int>(size);
317} 320}
318int fake_get_feature(void*, uint8_t* buf, size_t size) { 321int raw_get_feature(void*, uint8_t* buf, size_t size) {
319 if (g_fake.fail || g_fake.feature_reply.empty() || g_fake.feature_reply[0] != buf[0]) return -1; 322 if (g_raw.fail || g_raw.feature_reply.empty() || g_raw.feature_reply[0] != buf[0]) return -1;
320 const size_t n = std::min(size, g_fake.feature_reply.size()); 323 const size_t n = std::min(size, g_raw.feature_reply.size());
321 std::memcpy(buf, g_fake.feature_reply.data(), n); 324 std::memcpy(buf, g_raw.feature_reply.data(), n);
322 return static_cast<int>(n); 325 return static_cast<int>(n);
323} 326}
324int fake_send_feature(void*, const uint8_t* buf, size_t size) { 327int raw_send_feature(void*, const uint8_t* buf, size_t size) {
325 if (g_fake.fail) return -1; 328 if (g_raw.fail) return -1;
326 g_fake.features_sent.emplace_back(buf, buf + size); 329 g_raw.features_sent.emplace_back(buf, buf + size);
327 return static_cast<int>(size); 330 return static_cast<int>(size);
328} 331}
329const rt::gameinput::HidBackend kFakeHid = {fake_open, fake_close, fake_descriptor, fake_read, 332const rt::gameinput::HidBackend kFakeRawHid = {raw_open, raw_close, raw_descriptor, raw_read,
330 fake_write, fake_get_feature, fake_send_feature}; 333 raw_write, raw_get_feature, raw_send_feature};
331 334
332std::vector<uint8_t> raw_bytes(IGameInputRawDeviceReport* r) { 335std::vector<uint8_t> raw_bytes(IGameInputRawDeviceReport* r) {
333 std::vector<uint8_t> v(r ? r->GetRawDataSize() : 0); 336 std::vector<uint8_t> v(r ? r->GetRawDataSize() : 0);
@@ -337,10 +340,10 @@ std::vector<uint8_t> raw_bytes(IGameInputRawDeviceReport* r) {
337 340
338void test_raw_hid(IGameInput* input) { 341void test_raw_hid(IGameInput* input) {
339 test_parse_report_descriptor(); 342 test_parse_report_descriptor();
340 rt::gameinput::set_hid_backend(&kFakeHid); 343 rt::gameinput::set_hid_backend(&kFakeRawHid);
341 const size_t events_before = g_device_events.size(); 344 const size_t events_before = g_device_events.size();
342 Pad raw_pad, plain; 345 Pad raw_pad, plain;
343 attach(raw_pad, "Leonida virtual raw pad", g_fake.vendor, g_fake.product, false); 346 attach(raw_pad, "Leonida virtual raw pad", g_raw.vendor, g_raw.product, false);
344 attach(plain, "Leonida virtual pad without HID", 0x045E, 0x02FF, false); 347 attach(plain, "Leonida virtual pad without HID", 0x045E, 0x02FF, false);
345 rt::gameinput::poll(); 348 rt::gameinput::poll();
346 std::vector<IGameInputDevice*> found; // attach order: the raw pad first (see test_motion) 349 std::vector<IGameInputDevice*> found; // attach order: the raw pad first (see test_motion)
@@ -351,7 +354,7 @@ void test_raw_hid(IGameInput* input) {
351 } 354 }
352 IGameInputDevice* rdev = found.size() > 0 ? found[0] : nullptr; 355 IGameInputDevice* rdev = found.size() > 0 ? found[0] : nullptr;
353 IGameInputDevice* pdev = found.size() > 1 ? found[1] : nullptr; 356 IGameInputDevice* pdev = found.size() > 1 ? found[1] : nullptr;
354 check(rdev && pdev && g_fake.opens == 1, "raw hid: both pads connected, the backend opened the raw pad only"); 357 check(rdev && pdev && g_raw.opens == 1, "raw hid: both pads connected, the backend opened the raw pad only");
355 if (!rdev || !pdev) return; 358 if (!rdev || !pdev) return;
356 rdev->AddRef(); 359 rdev->AddRef();
357 pdev->AddRef(); 360 pdev->AddRef();
@@ -378,8 +381,8 @@ void test_raw_hid(IGameInput* input) {
378 "raw hid: before the first input report a reading carries no raw report"); 381 "raw hid: before the first input report a reading carries no raw report");
379 if (reading) reading->Release(); 382 if (reading) reading->Release();
380 383
381 g_fake.inputs.push_back({0x01, 0x11, 0x22, 0x33}); 384 g_raw.inputs.push_back({0x01, 0x11, 0x22, 0x33});
382 g_fake.inputs.push_back({0x09, 0x99}); // an id the descriptor does not declare: ignored 385 g_raw.inputs.push_back({0x09, 0x99}); // an id the descriptor does not declare: ignored
383 rt::gameinput::poll(); 386 rt::gameinput::poll();
384 reading = nullptr; 387 reading = nullptr;
385 check(input->GetCurrentReading(GameInputKindRawDeviceReport, rdev, &reading) == S_OK && reading && 388 check(input->GetCurrentReading(GameInputKindRawDeviceReport, rdev, &reading) == S_OK && reading &&
@@ -408,23 +411,23 @@ void test_raw_hid(IGameInput* input) {
408 check(outr && outr->GetRawDataSize() == 2 && raw_bytes(outr) == std::vector<uint8_t>({0, 0}) && 411 check(outr && outr->GetRawDataSize() == 2 && raw_bytes(outr) == std::vector<uint8_t>({0, 0}) &&
409 !outr->SetRawData(1, payload) && outr->SetRawData(2, payload), 412 !outr->SetRawData(1, payload) && outr->SetRawData(2, payload),
410 "raw hid: a new report is zeroed, and SetRawData takes exactly its size"); 413 "raw hid: a new report is zeroed, and SetRawData takes exactly its size");
411 check(rdev->SendRawDeviceOutput(outr) == S_OK && !g_fake.writes.empty() && 414 check(rdev->SendRawDeviceOutput(outr) == S_OK && !g_raw.writes.empty() &&
412 g_fake.writes.back() == std::vector<uint8_t>({0x02, 0xAA, 0xBB}), 415 g_raw.writes.back() == std::vector<uint8_t>({0x02, 0xAA, 0xBB}),
413 "raw hid: SendRawDeviceOutput writes the id byte then the payload"); 416 "raw hid: SendRawDeviceOutput writes the id byte then the payload");
414 check(rdev->SetRawDeviceFeature(outr) == E_INVALIDARG && rdev->SendRawDeviceOutput(nullptr) == E_INVALIDARG, 417 check(rdev->SetRawDeviceFeature(outr) == E_INVALIDARG && rdev->SendRawDeviceOutput(nullptr) == E_INVALIDARG,
415 "raw hid: an output report is not a feature report, and a null report is refused"); 418 "raw hid: an output report is not a feature report, and a null report is refused");
416 g_fake.fail = true; 419 g_raw.fail = true;
417 check(rdev->SendRawDeviceOutput(outr) == E_FAIL, "raw hid: a failed write is E_FAIL"); 420 check(rdev->SendRawDeviceOutput(outr) == E_FAIL, "raw hid: a failed write is E_FAIL");
418 g_fake.fail = false; 421 g_raw.fail = false;
419 if (outr) outr->Release(); 422 if (outr) outr->Release();
420 423
421 g_fake.feature_reply = {0x03, 0x01, 0x02, 0x03, 0x04}; 424 g_raw.feature_reply = {0x03, 0x01, 0x02, 0x03, 0x04};
422 void* fr = nullptr; 425 void* fr = nullptr;
423 check(rdev->GetRawDeviceFeature(3, &fr) == S_OK && fr && 426 check(rdev->GetRawDeviceFeature(3, &fr) == S_OK && fr &&
424 raw_bytes(static_cast<IGameInputRawDeviceReport*>(fr)) == std::vector<uint8_t>({1, 2, 3, 4}), 427 raw_bytes(static_cast<IGameInputRawDeviceReport*>(fr)) == std::vector<uint8_t>({1, 2, 3, 4}),
425 "raw hid: GetRawDeviceFeature(3) reads the feature report"); 428 "raw hid: GetRawDeviceFeature(3) reads the feature report");
426 check(fr && rdev->SetRawDeviceFeature(fr) == S_OK && !g_fake.features_sent.empty() && 429 check(fr && rdev->SetRawDeviceFeature(fr) == S_OK && !g_raw.features_sent.empty() &&
427 g_fake.features_sent.back() == std::vector<uint8_t>({0x03, 1, 2, 3, 4}), 430 g_raw.features_sent.back() == std::vector<uint8_t>({0x03, 1, 2, 3, 4}),
428 "raw hid: SetRawDeviceFeature sends it back with its id byte"); 431 "raw hid: SetRawDeviceFeature sends it back with its id byte");
429 if (fr) static_cast<IGameInputRawDeviceReport*>(fr)->Release(); 432 if (fr) static_cast<IGameInputRawDeviceReport*>(fr)->Release();
430 void* none = reinterpret_cast<void*>(1); 433 void* none = reinterpret_cast<void*>(1);
@@ -438,7 +441,7 @@ void test_raw_hid(IGameInput* input) {
438 detach(raw_pad); 441 detach(raw_pad);
439 detach(plain); 442 detach(plain);
440 rt::gameinput::poll(); 443 rt::gameinput::poll();
441 check(g_fake.closes == 1 && rt::gameinput::connected_count() == 0, "raw hid: unplugging closes the HID device"); 444 check(g_raw.closes == 1 && rt::gameinput::connected_count() == 0, "raw hid: unplugging closes the HID device");
442 check(rdev->CreateRawDeviceReport(2, GameInputRawOutputReport, &none) == E_NOTIMPL, 445 check(rdev->CreateRawDeviceReport(2, GameInputRawOutputReport, &none) == E_NOTIMPL,
443 "raw hid: no raw I/O once the pad is gone"); 446 "raw hid: no raw I/O once the pad is gone");
444 rdev->Release(); 447 rdev->Release();
@@ -980,6 +983,324 @@ void test_twins(IGameInput* input) {
980 check(rt::gameinput::connected_count() == 0, "twins: removed at the end"); 983 check(rt::gameinput::connected_count() == 0, "twins: removed at the end");
981} 984}
982 985
986// ---- force feedback and haptic motors: a fake SDL haptic backend ----
987// SDL's virtual joysticks report no haptics, so the shim's haptic calls go through this table instead. It records what
988// the shim asked SDL to do and reports the capabilities a force-feedback wheel has.
989struct FakeHaptic {
990 uint32_t features = SDL_HAPTIC_CONSTANT | SDL_HAPTIC_SINE | SDL_HAPTIC_SQUARE | SDL_HAPTIC_TRIANGLE |
991 SDL_HAPTIC_SAWTOOTHUP | SDL_HAPTIC_SAWTOOTHDOWN | SDL_HAPTIC_RAMP | SDL_HAPTIC_SPRING |
992 SDL_HAPTIC_FRICTION | SDL_HAPTIC_DAMPER | SDL_HAPTIC_INERTIA;
993 int max_effects = 8;
994 int opens = 0, closes = 0, creates = 0, updates = 0, runs = 0, stops = 0, destroys = 0;
995 int gain_calls = 0, last_gain = -1;
996 uint32_t last_iterations = 0;
997 SDL_HapticEffect last{};
998};
999FakeHaptic g_fake;
1000bool g_fake_open = false; // pads attached while this is set are opened as haptic devices
1001
1002void* fake_open(void*) {
1003 if (!g_fake_open) return nullptr;
1004 ++g_fake.opens;
1005 return &g_fake;
1006}
1007void fake_close(void*) { ++g_fake.closes; }
1008uint32_t fake_features(void* h) { return static_cast<FakeHaptic*>(h)->features; }
1009int fake_max_effects(void* h) { return static_cast<FakeHaptic*>(h)->max_effects; }
1010int fake_create(void* h, const void* e) {
1011 FakeHaptic* f = static_cast<FakeHaptic*>(h);
1012 ++f->creates;
1013 f->last = *static_cast<const SDL_HapticEffect*>(e);
1014 return 1;
1015}
1016bool fake_update(void* h, int, const void* e) {
1017 FakeHaptic* f = static_cast<FakeHaptic*>(h);
1018 ++f->updates;
1019 f->last = *static_cast<const SDL_HapticEffect*>(e);
1020 return true;
1021}
1022bool fake_run(void* h, int, uint32_t iterations) {
1023 FakeHaptic* f = static_cast<FakeHaptic*>(h);
1024 ++f->runs;
1025 f->last_iterations = iterations;
1026 return true;
1027}
1028bool fake_stop(void* h, int) {
1029 ++static_cast<FakeHaptic*>(h)->stops;
1030 return true;
1031}
1032void fake_destroy(void* h, int) { ++static_cast<FakeHaptic*>(h)->destroys; }
1033bool fake_gain(void* h, int gain) {
1034 FakeHaptic* f = static_cast<FakeHaptic*>(h);
1035 ++f->gain_calls;
1036 f->last_gain = gain;
1037 return true;
1038}
1039const rt::gameinput::HapticApi kFakeHaptic{fake_open, fake_close, fake_features, fake_max_effects, fake_create,
1040 fake_update, fake_run, fake_stop, fake_destroy, fake_gain};
1041
1042// The motors of one device, and the effect object its force-feedback motor hands out. Runs with no other pad.
1043void test_force_feedback(IGameInput* input) {
1044 rt::gameinput::set_haptic_source(&kFakeHaptic);
1045 g_fake = {};
1046 g_fake_open = true;
1047 Pad wheel;
1048 attach(wheel, "Leonida virtual wheel", 0x046D, 0xC24F, false);
1049 const size_t before = g_device_events.size();
1050 rt::gameinput::poll();
1051 IGameInputDevice* dev = g_device_events.size() > before ? g_device_events.back().device : nullptr;
1052 check(dev && rt::gameinput::connected_count() == 1 && g_fake.opens == 1,
1053 "force feedback: the pad's joystick is opened as an SDL haptic device");
1054 if (!dev) return;
1055 dev->AddRef();
1056 rt::gameinput::OutputState out{};
1057 check(rt::gameinput::output_state(0, &out) && out.force_feedback, "force feedback: the output state says so");
1058 IGameInputReading* r = current(input, dev);
1059 check(r != nullptr, "force feedback: the pad still reads normally with a haptic device open");
1060 if (r) r->Release();
1061
1062 const GameInputDeviceInfo* info = dev->GetDeviceInfo();
1063 check(info->forceFeedbackMotorCount == 1 && info->forceFeedbackMotorInfo != nullptr,
1064 "force feedback: the device reports one force-feedback motor");
1065 const GameInputForceFeedbackMotorInfo* motor =
1066 static_cast<const GameInputForceFeedbackMotorInfo*>(info->forceFeedbackMotorInfo);
1067 check(motor->supportedAxes == GameInputFeedbackAxisLinearX && motor->location == GameInputLocationChassis &&
1068 motor->maxSimultaneousEffects == 8,
1069 "force feedback: motorInfo carries the axis, the location and the effect limit");
1070 check(motor->isConstantEffectSupported && motor->isRampEffectSupported && motor->isSpringEffectSupported &&
1071 motor->isSawtoothDownWaveEffectSupported,
1072 "force feedback: the effect kinds come from SDL's feature bits");
1073 check(info->hapticFeedbackMotorCount == 0, "force feedback: a wheel without trigger motors reports no haptics");
1074 check(dev->IsForceFeedbackMotorPoweredOn(0) && !dev->IsForceFeedbackMotorPoweredOn(1),
1075 "force feedback: motor 0 is powered on, and motor 1 does not exist");
1076
1077 IGameInputForceFeedbackEffect* effect = nullptr;
1078 GameInputForceFeedbackParams params{};
1079 params.kind = GameInputForceFeedbackConstant;
1080 params.data.constant.magnitude.linearX = 0.5f;
1081 check(dev->CreateForceFeedbackEffect(1, &params, reinterpret_cast<void**>(&effect)) == E_INVALIDARG && !effect,
1082 "force feedback: motor 1 does not exist");
1083 check(dev->CreateForceFeedbackEffect(0, &params, nullptr) == E_POINTER, "force feedback: a null effect is E_POINTER");
1084 check(dev->CreateForceFeedbackEffect(0, nullptr, reinterpret_cast<void**>(&effect)) == E_INVALIDARG,
1085 "force feedback: null parameters are E_INVALIDARG");
1086 GameInputForceFeedbackParams unknown{};
1087 unknown.kind = static_cast<GameInputForceFeedbackEffectKind>(11);
1088 check(dev->CreateForceFeedbackEffect(0, &unknown, reinterpret_cast<void**>(&effect)) == E_INVALIDARG,
1089 "force feedback: an effect kind outside the enum is E_INVALIDARG");
1090 check(dev->CreateForceFeedbackEffect(0, &params, reinterpret_cast<void**>(&effect)) == S_OK && effect,
1091 "force feedback: CreateForceFeedbackEffect");
1092 if (!effect) { // leave nothing behind for the next test
1093 detach(wheel);
1094 dev->Release();
1095 rt::gameinput::poll();
1096 rt::gameinput::set_haptic_source(nullptr);
1097 return;
1098 }
1099 check(effect->GetMotorIndex() == 0 && approx(effect->GetGain(), 1.0f) && effect->GetState() == GameInputFeedbackRunning,
1100 "force feedback: a new effect is on motor 0, at full gain, running");
1101 IGameInputDevice* owner = nullptr;
1102 effect->GetDevice(&owner);
1103 check(owner == dev, "force feedback: the effect names its device");
1104 if (owner) owner->Release();
1105
1106 // Nothing is asked of SDL before the poll thread runs: the title only wrote the parameters.
1107 const int runs0 = g_fake.runs;
1108 rt::gameinput::poll();
1109 check(g_fake.creates == 1 && g_fake.runs == runs0 + 1, "force feedback: the poll thread creates and runs the effect");
1110 check(g_fake.last.type == SDL_HAPTIC_CONSTANT && g_fake.last.constant.level == 16384,
1111 "force feedback: a constant 0.5 becomes SDL_HAPTIC_CONSTANT level 16384");
1112 check(g_fake.last.constant.length == 60000 && g_fake.last_iterations == 0,
1113 "force feedback: no duration in GameInput is SDL's 60 s hold, run until stopped");
1114 rt::gameinput::poll();
1115 check(g_fake.runs == runs0 + 1, "force feedback: an unchanged effect is not re-sent every poll");
1116
1117 params.data.constant.magnitude.linearX = -1.0f;
1118 check(effect->SetParams(&params), "force feedback: SetParams");
1119 rt::gameinput::poll();
1120 check(g_fake.updates == 1 && g_fake.runs == runs0 + 2 && g_fake.last.constant.level == -32767,
1121 "force feedback: changed parameters update the SDL effect once");
1122 GameInputForceFeedbackParams read{};
1123 effect->GetParams(&read);
1124 check(read.kind == GameInputForceFeedbackConstant && read.data.constant.magnitude.linearX == -1.0f,
1125 "force feedback: GetParams returns what was set");
1126
1127 effect->SetGain(0.5f);
1128 rt::gameinput::poll();
1129 check(approx(effect->GetGain(), 0.5f) && g_fake.last.constant.level == -16384,
1130 "force feedback: the effect's gain scales the SDL level");
1131 // The device gain is SDL's, sent once when the pad is opened (100) and once per change.
1132 check(g_fake.last_gain == 100 && g_fake.gain_calls == 1, "force feedback: the device gain starts at SDL's full 100");
1133 dev->SetForceFeedbackMotorGain(0, 0.5f);
1134 rt::gameinput::poll();
1135 check(g_fake.last_gain == 50 && g_fake.gain_calls == 2,
1136 "force feedback: SetForceFeedbackMotorGain sets SDL's device gain once");
1137 dev->SetForceFeedbackMotorGain(0, 1.0f);
1138 effect->SetGain(1.0f);
1139 rt::gameinput::poll();
1140 check(g_fake.last_gain == 100, "force feedback: the master gain follows the request");
1141
1142 // A wave with GameInput's envelope: 50 Hz for 90 degrees over 100 ms, played once.
1143 GameInputForceFeedbackParams wave{};
1144 wave.kind = GameInputForceFeedbackSineWave;
1145 wave.data.sineWave.envelope.sustainDuration = 100'000; // microseconds
1146 wave.data.sineWave.envelope.sustainGain = 1.0f;
1147 wave.data.sineWave.envelope.playCount = 1;
1148 wave.data.sineWave.magnitude.linearX = 1.0f;
1149 wave.data.sineWave.frequency = 50.0f;
1150 wave.data.sineWave.phase = 90.0f;
1151 check(effect->SetParams(&wave), "force feedback: SetParams for a sine wave");
1152 rt::gameinput::poll();
1153 check(g_fake.last.type == SDL_HAPTIC_SINE && g_fake.last.periodic.period == 20 &&
1154 g_fake.last.periodic.magnitude == 32767 && g_fake.last.periodic.phase == 9000,
1155 "force feedback: a 50 Hz sine with a 90 degree phase becomes SDL's period and hundredths of a degree");
1156 check(g_fake.last.periodic.length == 100 && g_fake.last_iterations == 1,
1157 "force feedback: the envelope's 100 ms is SDL's length and its playCount the iteration count");
1158 Sleep(130);
1159 rt::gameinput::poll();
1160 check(effect->GetState() == GameInputFeedbackStopped && g_fake.stops >= 1,
1161 "force feedback: an effect that ran its duration reports stopped");
1162
1163 // A condition effect: coefficients, saturations, dead zone and centre.
1164 GameInputForceFeedbackParams spring{};
1165 spring.kind = GameInputForceFeedbackSpring;
1166 spring.data.spring.magnitude.linearX = 0.5f;
1167 spring.data.spring.positiveCoefficient = 0.5f;
1168 spring.data.spring.negativeCoefficient = 0.25f;
1169 spring.data.spring.maxPositiveMagnitude = 1.0f;
1170 spring.data.spring.maxNegativeMagnitude = 0.5f;
1171 spring.data.spring.deadZone = 0.1f;
1172 check(effect->SetParams(&spring), "force feedback: SetParams for a spring");
1173 effect->SetState(GameInputFeedbackRunning);
1174 rt::gameinput::poll();
1175 check(g_fake.last.type == SDL_HAPTIC_SPRING && g_fake.last.condition.right_coeff[0] == 16384 &&
1176 g_fake.last.condition.left_coeff[0] == -8192 && g_fake.last.condition.right_sat[0] == 65535 &&
1177 g_fake.last.condition.left_sat[0] == 32768 && g_fake.last.condition.deadband[0] == 6554 &&
1178 g_fake.last.condition.center[0] == 16384,
1179 "force feedback: a spring maps its coefficients, saturations, dead zone and centre");
1180
1181 effect->SetState(GameInputFeedbackPaused);
1182 const int stops0 = g_fake.stops;
1183 rt::gameinput::poll();
1184 check(g_fake.stops == stops0 + 1 && effect->GetState() == GameInputFeedbackPaused,
1185 "force feedback: a paused effect is stopped in SDL");
1186
1187 // LEONIDA_RUMBLE=0 silences this path too: a running effect is stopped, and it runs again when the mute lifts.
1188 effect->SetState(GameInputFeedbackRunning);
1189 rt::gameinput::poll();
1190 const int runs1 = g_fake.runs, stops1 = g_fake.stops;
1191 check(g_fake.runs == runs1, "force feedback: the effect is running before the mute");
1192 set_env("LEONIDA_RUMBLE", "0");
1193 rt::gameinput::reload_config();
1194 rt::gameinput::poll();
1195 check(g_fake.runs == runs1 && g_fake.stops == stops1 + 1, "force feedback: LEONIDA_RUMBLE=0 stops the effect");
1196 set_env("LEONIDA_RUMBLE", nullptr);
1197 rt::gameinput::reload_config();
1198 rt::gameinput::poll();
1199 check(g_fake.runs == runs1 + 1, "force feedback: and it runs again once the mute is lifted");
1200
1201 check(effect->Release() == 0, "force feedback: the effect is freed once the title releases it");
1202 rt::gameinput::poll();
1203 check(g_fake.destroys == 1, "force feedback: its SDL effect is destroyed on the poll thread");
1204
1205 detach(wheel);
1206 rt::gameinput::poll();
1207 check(g_fake.closes == 1 && !dev->IsForceFeedbackMotorPoweredOn(0),
1208 "force feedback: unplugging closes the haptic device");
1209 dev->Release();
1210
1211 // A pad SDL opens no haptic device for keeps the old answer: no motor, and no effect.
1212 g_fake_open = false;
1213 Pad plain;
1214 attach(plain, "Leonida virtual plain pad", 0x045E, 0x02FF, false);
1215 const size_t plain_before = g_device_events.size();
1216 rt::gameinput::poll();
1217 IGameInputDevice* plaindev = g_device_events.size() > plain_before ? g_device_events.back().device : nullptr;
1218 check(plaindev && plaindev->GetDeviceInfo()->forceFeedbackMotorCount == 0 &&
1219 plaindev->GetDeviceInfo()->forceFeedbackMotorInfo == nullptr,
1220 "force feedback: a pad without SDL haptics reports no motor");
1221 if (plaindev) {
1222 effect = nullptr;
1223 check(plaindev->CreateForceFeedbackEffect(0, &params, reinterpret_cast<void**>(&effect)) == E_INVALIDARG && !effect,
1224 "force feedback: so CreateForceFeedbackEffect is E_INVALIDARG");
1225 check(!plaindev->IsForceFeedbackMotorPoweredOn(0), "force feedback: and no motor is powered on");
1226 }
1227 detach(plain);
1228 rt::gameinput::poll();
1229 rt::gameinput::set_haptic_source(nullptr);
1230}
1231
1232// The trigger haptic motors of a pad that has them. Runs with no other pad.
1233void test_haptic_motors(IGameInput* input) {
1234 set_env("LEONIDA_DS_TRIGGERS", nullptr);
1235 rt::gameinput::reload_config();
1236 Pad xbox;
1237 attach(xbox, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true);
1238 const size_t before = g_device_events.size();
1239 rt::gameinput::poll();
1240 IGameInputDevice* dev = g_device_events.size() > before ? g_device_events.back().device : nullptr;
1241 check(dev && rt::gameinput::connected_count() == 1, "haptics: the pad with trigger motors is connected");
1242 if (!dev) return;
1243 dev->AddRef();
1244 const GameInputDeviceInfo* info = dev->GetDeviceInfo();
1245 check(info->hapticFeedbackMotorCount == 2 && info->hapticFeedbackMotorInfo != nullptr,
1246 "haptics: the pad reports two haptic motors");
1247 const GameInputHapticFeedbackMotorInfo* motors =
1248 static_cast<const GameInputHapticFeedbackMotorInfo*>(info->hapticFeedbackMotorInfo);
1249 check(motors[0].mappedRumbleMotors == GameInputRumbleLeftTrigger &&
1250 motors[1].mappedRumbleMotors == GameInputRumbleRightTrigger && motors[0].waveformCount == 1 &&
1251 motors[0].waveformInfo != nullptr && motors[0].waveformInfo->isIntensitySupported,
1252 "haptics: they are the left and right trigger, one waveform each, an intensity");
1253 check(sticks_at_rest(pad_state(input, dev)), "haptics: the pad still reads normally");
1254
1255 GameInputHapticFeedbackParams p{};
1256 p.intensity = 0.5f;
1257 p.duration = 100'000; // microseconds
1258 p.playCount = 1;
1259 check(dev->SetHapticMotorState(2, &p) == E_INVALIDARG, "haptics: motor 2 does not exist");
1260 GameInputHapticFeedbackParams other_wave = p;
1261 other_wave.waveformIndex = 1;
1262 check(dev->SetHapticMotorState(0, &other_wave) == E_INVALIDARG, "haptics: waveform 1 does not exist");
1263 check(dev->SetHapticMotorState(0, &p) == S_OK, "haptics: SetHapticMotorState on the left trigger");
1264 rt::gameinput::poll();
1265 check(xbox.left == 32768 && xbox.right == 0, "haptics: the intensity reaches the trigger motor");
1266 Sleep(130);
1267 rt::gameinput::poll();
1268 check(xbox.left == 0, "haptics: the request ends with its duration");
1269
1270 p.intensity = 1.0f;
1271 p.playCount = 0; // until changed
1272 check(dev->SetHapticMotorState(1, &p) == S_OK, "haptics: playCount 0 plays until changed");
1273 rt::gameinput::poll();
1274 check(xbox.right == 65535 && xbox.left == 0, "haptics: the right trigger takes its own motor");
1275 Sleep(130);
1276 rt::gameinput::poll();
1277 check(xbox.right == 65535, "haptics: and keeps playing past the duration");
1278 check(dev->SetHapticMotorState(1, nullptr) == S_OK, "haptics: a null state stops the motor");
1279 rt::gameinput::poll();
1280 check(xbox.right == 0 && xbox.left == 0, "haptics: both triggers are silent again");
1281
1282 // SetRumbleState still works on the same pad, and the two share the trigger output.
1283 GameInputRumbleParams rumble{0, 0, 0.25f, 0};
1284 dev->SetRumbleState(&rumble);
1285 rt::gameinput::poll();
1286 check(xbox.left == 16384, "haptics: SetRumbleState still drives the trigger motor");
1287 p.intensity = 1.0f;
1288 p.playCount = 1;
1289 p.duration = 0; // no deadline at playCount 1 either: the level holds until it is set again
1290 dev->SetHapticMotorState(0, &p);
1291 rt::gameinput::poll();
1292 check(xbox.left == 65535, "haptics: the stronger of the two wins on one trigger");
1293 dev->SetRumbleState(nullptr);
1294 dev->SetHapticMotorState(0, nullptr);
1295 rt::gameinput::poll();
1296 check(xbox.left == 0, "haptics: releasing both leaves the trigger silent");
1297
1298 detach(xbox);
1299 rt::gameinput::poll();
1300 check(rt::gameinput::connected_count() == 0, "haptics: detached");
1301 dev->Release();
1302}
1303
983} // namespace 1304} // namespace
984 1305
985int main() { 1306int main() {
@@ -1241,6 +1562,32 @@ int main() {
1241 check(wait && WaitForSingleObject(wait, 0) == WAIT_OBJECT_0, "the wait handle is signalled"); 1562 check(wait && WaitForSingleObject(wait, 0) == WAIT_OBJECT_0, "the wait handle is signalled");
1242 if (dispatcher) dispatcher->Dispatch(1000); 1563 if (dispatcher) dispatcher->Dispatch(1000);
1243 check(g_reading_events > before_dispatch, "Dispatch runs them"); 1564 check(g_reading_events > before_dispatch, "Dispatch runs them");
1565
1566 // The quota: Dispatch runs at least one queued event, takes no more once the quota has passed, and returns whether
1567 // events remain (re-signalling the wait handle). Four button changes queue four reading events.
1568 auto queue_changes = [&](int n) {
1569 for (int i = 0; i < n; ++i) {
1570 SDL_SetJoystickVirtualButton(xbox.js, SDL_GAMEPAD_BUTTON_NORTH, i % 2 == 1); // NORTH is down now: up, down, up, down
1571 rt::gameinput::poll();
1572 }
1573 };
1574 if (dispatcher) {
1575 while (dispatcher->Dispatch(UINT64_MAX)) {}
1576 queue_changes(4);
1577 int before_quota = g_reading_events;
1578 if (wait) WaitForSingleObject(wait, 0); // consume the signal from queueing
1579 check(dispatcher->Dispatch(0) && g_reading_events == before_quota + 1, "Dispatch(0) runs exactly one event and reports more");
1580 check(wait && WaitForSingleObject(wait, 0) == WAIT_OBJECT_0, "the wait handle is signalled again while events remain");
1581 g_reading_sleep_ms = 5;
1582 before_quota = g_reading_events;
1583 check(dispatcher->Dispatch(1000) && g_reading_events == before_quota + 1,
1584 "a 1 ms quota stops after a 5 ms callback and reports more");
1585 g_reading_sleep_ms = 0;
1586 before_quota = g_reading_events;
1587 check(!dispatcher->Dispatch(1'000'000) && g_reading_events >= before_quota + 2,
1588 "a large quota drains the queue and returns false");
1589 check(!dispatcher->Dispatch(0), "Dispatch on an empty queue returns false");
1590 }
1244 if (wait) CloseHandle(wait); 1591 if (wait) CloseHandle(wait);
1245 if (dispatcher) dispatcher->Release(); 1592 if (dispatcher) dispatcher->Release();
1246 1593
@@ -1443,6 +1790,8 @@ int main() {
1443 test_keyboard_pad(input); 1790 test_keyboard_pad(input);
1444 test_motion(input); 1791 test_motion(input);
1445 test_aggregate_device(input); 1792 test_aggregate_device(input);
1793 test_force_feedback(input);
1794 test_haptic_motors(input);
1446 test_raw_hid(input); 1795 test_raw_hid(input);
1447 input->Release(); 1796 input->Release();
1448 1797