diff options
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
| -rw-r--r-- | runtime/test/gameinput_test.cpp | 150 |
1 files changed, 149 insertions, 1 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 679adf19f..7217dc37b 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp | |||
| @@ -13,12 +13,14 @@ | |||
| 13 | #include <SDL3/SDL.h> | 13 | #include <SDL3/SDL.h> |
| 14 | 14 | ||
| 15 | #include <algorithm> | 15 | #include <algorithm> |
| 16 | #include <atomic> | ||
| 16 | #include <cmath> | 17 | #include <cmath> |
| 17 | #include <cstdint> | 18 | #include <cstdint> |
| 18 | #include <cstdio> | 19 | #include <cstdio> |
| 19 | #include <cstring> | 20 | #include <cstring> |
| 20 | #include <deque> | 21 | #include <deque> |
| 21 | #include <string> | 22 | #include <string> |
| 23 | #include <thread> | ||
| 22 | #include <vector> | 24 | #include <vector> |
| 23 | 25 | ||
| 24 | #include "gameinput.h" | 26 | #include "gameinput.h" |
| @@ -242,6 +244,20 @@ GameInputMotionState motion_state(IGameInput* input, IGameInputDevice* dev, bool | |||
| 242 | return ms; | 244 | return ms; |
| 243 | } | 245 | } |
| 244 | 246 | ||
| 247 | struct MotionReadings { | ||
| 248 | IGameInputDevice* gyro; | ||
| 249 | IGameInputDevice* plain; | ||
| 250 | int gyro_calls, plain_calls; | ||
| 251 | }; | ||
| 252 | void CALLBACK on_motion_reading(GameInputCallbackToken, void* context, IGameInputReading* reading, bool) { | ||
| 253 | auto* seen = static_cast<MotionReadings*>(context); | ||
| 254 | IGameInputDevice* dev = nullptr; | ||
| 255 | if (reading) reading->GetDevice(&dev); | ||
| 256 | if (dev == seen->gyro) ++seen->gyro_calls; | ||
| 257 | if (dev == seen->plain) ++seen->plain_calls; | ||
| 258 | if (dev) dev->Release(); | ||
| 259 | } | ||
| 260 | |||
| 245 | // ---- raw HID (RT-175) ------------------------------------------------------------------------------------------- | 261 | // ---- raw HID (RT-175) ------------------------------------------------------------------------------------------- |
| 246 | 262 | ||
| 247 | using rt::gameinput::RawReportSize; | 263 | using rt::gameinput::RawReportSize; |
| @@ -300,11 +316,16 @@ struct FakeRawHid { | |||
| 300 | std::vector<std::vector<uint8_t>> writes, features_sent; | 316 | std::vector<std::vector<uint8_t>> writes, features_sent; |
| 301 | std::vector<uint8_t> feature_reply; | 317 | std::vector<uint8_t> feature_reply; |
| 302 | bool fail = false; | 318 | bool fail = false; |
| 319 | HANDLE in_open = nullptr, release_open = nullptr; | ||
| 303 | }; | 320 | }; |
| 304 | FakeRawHid g_raw; | 321 | FakeRawHid g_raw; |
| 305 | void* raw_open(const char*, uint16_t vendor, uint16_t product) { | 322 | void* raw_open(const char*, uint16_t vendor, uint16_t product) { |
| 306 | if (vendor != g_raw.vendor || product != g_raw.product) return nullptr; | 323 | if (vendor != g_raw.vendor || product != g_raw.product) return nullptr; |
| 307 | ++g_raw.opens; | 324 | ++g_raw.opens; |
| 325 | if (g_raw.release_open) { | ||
| 326 | SetEvent(g_raw.in_open); | ||
| 327 | WaitForSingleObject(g_raw.release_open, 5000); | ||
| 328 | } | ||
| 308 | return &g_raw; | 329 | return &g_raw; |
| 309 | } | 330 | } |
| 310 | void raw_close(void*) { ++g_raw.closes; } | 331 | void raw_close(void*) { ++g_raw.closes; } |
| @@ -346,6 +367,14 @@ std::vector<uint8_t> raw_bytes(IGameInputRawDeviceReport* r) { | |||
| 346 | return v; | 367 | return v; |
| 347 | } | 368 | } |
| 348 | 369 | ||
| 370 | // RT-262: the poll thread takes a reference on a force-feedback effect only while it is alive. | ||
| 371 | void test_add_ref_if_live() { | ||
| 372 | std::atomic<unsigned long> live{1}, dying{0}; | ||
| 373 | check(rt::gameinput::add_ref_if_live(live) && live.load() == 2, "add_ref_if_live: a live count goes up by one"); | ||
| 374 | check(!rt::gameinput::add_ref_if_live(dying) && dying.load() == 0, | ||
| 375 | "add_ref_if_live: a count of 0 stays 0, so an effect being destroyed isn't revived"); | ||
| 376 | } | ||
| 377 | |||
| 349 | void test_raw_hid(IGameInput* input) { | 378 | void test_raw_hid(IGameInput* input) { |
| 350 | test_parse_report_descriptor(); | 379 | test_parse_report_descriptor(); |
| 351 | rt::gameinput::set_hid_backend(&kFakeRawHid); | 380 | rt::gameinput::set_hid_backend(&kFakeRawHid); |
| @@ -457,6 +486,64 @@ void test_raw_hid(IGameInput* input) { | |||
| 457 | rt::gameinput::set_hid_backend(nullptr); | 486 | rt::gameinput::set_hid_backend(nullptr); |
| 458 | } | 487 | } |
| 459 | 488 | ||
| 489 | void test_raw_report_during_reconnect(IGameInput* input) { | ||
| 490 | rt::gameinput::set_hid_backend(&kFakeRawHid); | ||
| 491 | Pad raw_pad; | ||
| 492 | attach(raw_pad, "Leonida virtual raw pad", g_raw.vendor, g_raw.product, false); | ||
| 493 | rt::gameinput::poll(); | ||
| 494 | g_raw.inputs.push_back({0x01, 0x44, 0x55, 0x66}); | ||
| 495 | rt::gameinput::poll(); | ||
| 496 | IGameInputReading* held = nullptr; | ||
| 497 | check(input->GetCurrentReading(GameInputKindRawDeviceReport, nullptr, &held) == S_OK && held, | ||
| 498 | "raw hid reconnect: a reading with a raw report to hold"); | ||
| 499 | detach(raw_pad); | ||
| 500 | rt::gameinput::poll(); | ||
| 501 | if (!held) { | ||
| 502 | rt::gameinput::set_hid_backend(nullptr); | ||
| 503 | return; | ||
| 504 | } | ||
| 505 | |||
| 506 | g_raw.in_open = CreateEventW(nullptr, TRUE, FALSE, nullptr); | ||
| 507 | g_raw.release_open = CreateEventW(nullptr, TRUE, FALSE, nullptr); | ||
| 508 | HANDLE read_done = CreateEventW(nullptr, TRUE, FALSE, nullptr); | ||
| 509 | attach(raw_pad, "Leonida virtual raw pad", g_raw.vendor, g_raw.product, false); | ||
| 510 | std::thread poller([] { rt::gameinput::poll(); }); | ||
| 511 | const bool in_open = WaitForSingleObject(g_raw.in_open, 5000) == WAIT_OBJECT_0; | ||
| 512 | bool got = false; | ||
| 513 | void* report = nullptr; | ||
| 514 | std::thread reader([&] { | ||
| 515 | got = held->GetRawReport(&report); | ||
| 516 | SetEvent(read_done); | ||
| 517 | }); | ||
| 518 | const bool waited = WaitForSingleObject(read_done, 200) == WAIT_TIMEOUT; | ||
| 519 | SetEvent(g_raw.release_open); | ||
| 520 | poller.join(); | ||
| 521 | const bool finished = WaitForSingleObject(read_done, 5000) == WAIT_OBJECT_0; | ||
| 522 | reader.join(); | ||
| 523 | check(in_open && waited, "raw hid reconnect: GetRawReport waits while open_raw rebuilds the report tables"); | ||
| 524 | auto* rr = static_cast<IGameInputRawDeviceReport*>(report); | ||
| 525 | check(finished && got && rr && raw_bytes(rr) == std::vector<uint8_t>({0x44, 0x55, 0x66}), | ||
| 526 | "raw hid reconnect: then it gives the held reading's report"); | ||
| 527 | if (rr) rr->Release(); | ||
| 528 | IGameInputDevice* dev = nullptr; | ||
| 529 | held->GetDevice(&dev); | ||
| 530 | void* created = nullptr; | ||
| 531 | check(dev && (dev->GetDeviceStatus() & GameInputDeviceRawIoEnabled) && | ||
| 532 | dev->CreateRawDeviceReport(2, GameInputRawOutputReport, &created) == S_OK && created, | ||
| 533 | "raw hid reconnect: and the reconnected pad has raw I/O again"); | ||
| 534 | if (created) static_cast<IGameInputRawDeviceReport*>(created)->Release(); | ||
| 535 | if (dev) dev->Release(); | ||
| 536 | held->Release(); | ||
| 537 | |||
| 538 | CloseHandle(read_done); | ||
| 539 | CloseHandle(g_raw.in_open); | ||
| 540 | CloseHandle(g_raw.release_open); | ||
| 541 | g_raw.in_open = g_raw.release_open = nullptr; | ||
| 542 | detach(raw_pad); | ||
| 543 | rt::gameinput::poll(); | ||
| 544 | rt::gameinput::set_hid_backend(nullptr); | ||
| 545 | } | ||
| 546 | |||
| 460 | void test_motion(IGameInput* input) { | 547 | void test_motion(IGameInput* input) { |
| 461 | const size_t events_before = g_device_events.size(); // other tests' events are still queued in the vector | 548 | const size_t events_before = g_device_events.size(); // other tests' events are still queued in the vector |
| 462 | Pad gyro_pad, plain; | 549 | Pad gyro_pad, plain; |
| @@ -559,6 +646,19 @@ void test_motion(IGameInput* input) { | |||
| 559 | } | 646 | } |
| 560 | if (first) first->Release(); | 647 | if (first) first->Release(); |
| 561 | 648 | ||
| 649 | MotionReadings seen{mdev, pdev, 0, 0}; | ||
| 650 | GameInputCallbackToken motion_token = 0; | ||
| 651 | check(input->RegisterReadingCallback(nullptr, GameInputKindMotion, 0.0f, &seen, on_motion_reading, &motion_token) == S_OK, | ||
| 652 | "motion: RegisterReadingCallback(GameInputKindMotion)"); | ||
| 653 | for (int i = 0; i < 4; ++i) { | ||
| 654 | SDL_SetJoystickVirtualButton(gyro_pad.js, SDL_GAMEPAD_BUTTON_SOUTH, i % 2 == 0); | ||
| 655 | SDL_SetJoystickVirtualButton(plain.js, SDL_GAMEPAD_BUTTON_SOUTH, i % 2 == 0); | ||
| 656 | rt::gameinput::poll(); | ||
| 657 | } | ||
| 658 | input->UnregisterCallback(motion_token, 0); | ||
| 659 | check(seen.gyro_calls > 0 && seen.plain_calls == 0, | ||
| 660 | "motion: a Motion reading callback fires for the gyro pad's readings, not for the plain pad's"); | ||
| 661 | |||
| 562 | detach(gyro_pad); | 662 | detach(gyro_pad); |
| 563 | detach(plain); | 663 | detach(plain); |
| 564 | rt::gameinput::poll(); | 664 | rt::gameinput::poll(); |
| @@ -1149,6 +1249,7 @@ struct FakeHaptic { | |||
| 1149 | SDL_HAPTIC_SAWTOOTHUP | SDL_HAPTIC_SAWTOOTHDOWN | SDL_HAPTIC_RAMP | SDL_HAPTIC_SPRING | | 1249 | SDL_HAPTIC_SAWTOOTHUP | SDL_HAPTIC_SAWTOOTHDOWN | SDL_HAPTIC_RAMP | SDL_HAPTIC_SPRING | |
| 1150 | SDL_HAPTIC_FRICTION | SDL_HAPTIC_DAMPER | SDL_HAPTIC_INERTIA; | 1250 | SDL_HAPTIC_FRICTION | SDL_HAPTIC_DAMPER | SDL_HAPTIC_INERTIA; |
| 1151 | int max_effects = 8; | 1251 | int max_effects = 8; |
| 1252 | int create_result = 1; // what create returns; negative is SDL refusing the effect | ||
| 1152 | int opens = 0, closes = 0, creates = 0, updates = 0, runs = 0, stops = 0, destroys = 0; | 1253 | int opens = 0, closes = 0, creates = 0, updates = 0, runs = 0, stops = 0, destroys = 0; |
| 1153 | int gain_calls = 0, last_gain = -1; | 1254 | int gain_calls = 0, last_gain = -1; |
| 1154 | uint32_t last_iterations = 0; | 1255 | uint32_t last_iterations = 0; |
| @@ -1168,8 +1269,12 @@ int fake_max_effects(void* h) { return static_cast<FakeHaptic*>(h)->max_effects; | |||
| 1168 | int fake_create(void* h, const void* e) { | 1269 | int fake_create(void* h, const void* e) { |
| 1169 | FakeHaptic* f = static_cast<FakeHaptic*>(h); | 1270 | FakeHaptic* f = static_cast<FakeHaptic*>(h); |
| 1170 | ++f->creates; | 1271 | ++f->creates; |
| 1272 | if (f->create_result < 0) { | ||
| 1273 | SDL_SetError("fake: refused"); | ||
| 1274 | return -1; | ||
| 1275 | } | ||
| 1171 | f->last = *static_cast<const SDL_HapticEffect*>(e); | 1276 | f->last = *static_cast<const SDL_HapticEffect*>(e); |
| 1172 | return 1; | 1277 | return f->create_result; |
| 1173 | } | 1278 | } |
| 1174 | bool fake_update(void* h, int, const void* e) { | 1279 | bool fake_update(void* h, int, const void* e) { |
| 1175 | FakeHaptic* f = static_cast<FakeHaptic*>(h); | 1280 | FakeHaptic* f = static_cast<FakeHaptic*>(h); |
| @@ -1265,6 +1370,9 @@ void test_force_feedback(IGameInput* input) { | |||
| 1265 | const int runs0 = g_fake.runs; | 1370 | const int runs0 = g_fake.runs; |
| 1266 | rt::gameinput::poll(); | 1371 | rt::gameinput::poll(); |
| 1267 | check(g_fake.creates == 1 && g_fake.runs == runs0 + 1, "force feedback: the poll thread creates and runs the effect"); | 1372 | check(g_fake.creates == 1 && g_fake.runs == runs0 + 1, "force feedback: the poll thread creates and runs the effect"); |
| 1373 | rt::gameinput::OutputState sent{}; | ||
| 1374 | check(rt::gameinput::output_state(0, &sent) && sent.effect_id == 1 && sent.effect_ran && !sent.effect_error[0], | ||
| 1375 | "force feedback: the output state reports the effect SDL created and is running"); | ||
| 1268 | check(g_fake.last.type == SDL_HAPTIC_CONSTANT && g_fake.last.constant.level == 16384, | 1376 | check(g_fake.last.type == SDL_HAPTIC_CONSTANT && g_fake.last.constant.level == 16384, |
| 1269 | "force feedback: a constant 0.5 becomes SDL_HAPTIC_CONSTANT level 16384"); | 1377 | "force feedback: a constant 0.5 becomes SDL_HAPTIC_CONSTANT level 16384"); |
| 1270 | check(g_fake.last.constant.length == 60000 && g_fake.last_iterations == 0, | 1378 | check(g_fake.last.constant.length == 60000 && g_fake.last_iterations == 0, |
| @@ -1272,6 +1380,22 @@ void test_force_feedback(IGameInput* input) { | |||
| 1272 | rt::gameinput::poll(); | 1380 | rt::gameinput::poll(); |
| 1273 | check(g_fake.runs == runs0 + 1, "force feedback: an unchanged effect is not re-sent every poll"); | 1381 | check(g_fake.runs == runs0 + 1, "force feedback: an unchanged effect is not re-sent every poll"); |
| 1274 | 1382 | ||
| 1383 | // SDL refusing an effect is visible, not swallowed: the output state carries the failure and SDL's error. | ||
| 1384 | g_fake.create_result = -1; | ||
| 1385 | IGameInputForceFeedbackEffect* refused = nullptr; | ||
| 1386 | check(dev->CreateForceFeedbackEffect(0, ¶ms, reinterpret_cast<void**>(&refused)) == S_OK && refused, | ||
| 1387 | "force feedback: GameInput accepts an effect SDL will refuse"); | ||
| 1388 | if (refused) { | ||
| 1389 | rt::gameinput::poll(); | ||
| 1390 | rt::gameinput::OutputState refused_out{}; | ||
| 1391 | check(rt::gameinput::output_state(0, &refused_out) && refused_out.effect_id < 0 && !refused_out.effect_ran && | ||
| 1392 | std::strstr(refused_out.effect_error, "fake: refused"), | ||
| 1393 | "force feedback: SDL refusing the effect is recorded with its error"); | ||
| 1394 | refused->Release(); | ||
| 1395 | rt::gameinput::poll(); // the released effect leaves the device | ||
| 1396 | } | ||
| 1397 | g_fake.create_result = 1; | ||
| 1398 | |||
| 1275 | params.data.constant.magnitude.linearX = -1.0f; | 1399 | params.data.constant.magnitude.linearX = -1.0f; |
| 1276 | check(effect->SetParams(¶ms), "force feedback: SetParams"); | 1400 | check(effect->SetParams(¶ms), "force feedback: SetParams"); |
| 1277 | rt::gameinput::poll(); | 1401 | rt::gameinput::poll(); |
| @@ -1360,6 +1484,28 @@ void test_force_feedback(IGameInput* input) { | |||
| 1360 | rt::gameinput::poll(); | 1484 | rt::gameinput::poll(); |
| 1361 | check(g_fake.destroys == 1, "force feedback: its SDL effect is destroyed on the poll thread"); | 1485 | check(g_fake.destroys == 1, "force feedback: its SDL effect is destroyed on the poll thread"); |
| 1362 | 1486 | ||
| 1487 | // RT-262: the title releases effects while the poll thread applies them. The poll thread used to AddRef an effect | ||
| 1488 | // whose count had already reached 0 (its destructor waiting for the lock), use it after it was freed, and free it | ||
| 1489 | // a second time with its own Release. | ||
| 1490 | { | ||
| 1491 | std::atomic<bool> stop{false}; | ||
| 1492 | std::thread poller([&] { | ||
| 1493 | while (!stop.load()) rt::gameinput::poll(); | ||
| 1494 | }); | ||
| 1495 | int created = 0; | ||
| 1496 | for (int i = 0; i < 20000; ++i) { | ||
| 1497 | IGameInputForceFeedbackEffect* e = nullptr; | ||
| 1498 | if (dev->CreateForceFeedbackEffect(0, ¶ms, reinterpret_cast<void**>(&e)) == S_OK && e) { | ||
| 1499 | ++created; | ||
| 1500 | e->Release(); | ||
| 1501 | } | ||
| 1502 | } | ||
| 1503 | stop = true; | ||
| 1504 | poller.join(); | ||
| 1505 | rt::gameinput::poll(); | ||
| 1506 | check(created == 20000, "force feedback: 20,000 effects created and released while the poll thread runs"); | ||
| 1507 | } | ||
| 1508 | |||
| 1363 | detach(wheel); | 1509 | detach(wheel); |
| 1364 | rt::gameinput::poll(); | 1510 | rt::gameinput::poll(); |
| 1365 | check(g_fake.closes == 1 && !dev->IsForceFeedbackMotorPoweredOn(0), | 1511 | check(g_fake.closes == 1 && !dev->IsForceFeedbackMotorPoweredOn(0), |
| @@ -1470,6 +1616,7 @@ int main() { | |||
| 1470 | set_env("LEONIDA_DS_TRIGGERS", nullptr); | 1616 | set_env("LEONIDA_DS_TRIGGERS", nullptr); |
| 1471 | set_env("LEONIDA_DS_LIGHTBAR", nullptr); | 1617 | set_env("LEONIDA_DS_LIGHTBAR", nullptr); |
| 1472 | set_env("LEONIDA_RUMBLE", nullptr); | 1618 | set_env("LEONIDA_RUMBLE", nullptr); |
| 1619 | test_add_ref_if_live(); | ||
| 1473 | 1620 | ||
| 1474 | auto create = reinterpret_cast<GameInputCreateFn>(rt::shims::find("gameinput.dll", "GameInputCreate")); | 1621 | auto create = reinterpret_cast<GameInputCreateFn>(rt::shims::find("gameinput.dll", "GameInputCreate")); |
| 1475 | check(create != nullptr, "gameinput.dll!GameInputCreate is registered"); | 1622 | check(create != nullptr, "gameinput.dll!GameInputCreate is registered"); |
| @@ -1974,6 +2121,7 @@ int main() { | |||
| 1974 | test_force_feedback(input); | 2121 | test_force_feedback(input); |
| 1975 | test_haptic_motors(input); | 2122 | test_haptic_motors(input); |
| 1976 | test_raw_hid(input); | 2123 | test_raw_hid(input); |
| 2124 | test_raw_report_during_reconnect(input); | ||
| 1977 | input->Release(); | 2125 | input->Release(); |
| 1978 | 2126 | ||
| 1979 | std::printf("%d checks, %d failures\n", g_checks, g_failures); | 2127 | std::printf("%d checks, %d failures\n", g_checks, g_failures); |