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:
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
-rw-r--r--runtime/test/gameinput_test.cpp171
1 files changed, 170 insertions, 1 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp
index 1a154a7a9..671dbbbe5 100644
--- a/runtime/test/gameinput_test.cpp
+++ b/runtime/test/gameinput_test.cpp
@@ -5,7 +5,8 @@
5// (Sony vendor/product id, so SDL treats it as one and the shim takes the trigger-effect path) and a plain pad 5// (Sony vendor/product id, so SDL treats it as one and the shim takes the trigger-effect path) and a plain pad
6// without trigger motors. The test covers enumeration and device callbacks, readings and their button/axis mapping, 6// without trigger motors. The test covers enumeration and device callbacks, readings and their button/axis mapping,
7// UI navigation, the guide button, rumble and trigger rumble as SDL receives them, the DualSense effect bytes for 7// UI navigation, the guide button, rumble and trigger rumble as SDL receives them, the DualSense effect bytes for
8// each LEONIDA_DS_TRIGGERS mode, the lightbar, a dispatcher, and unplugging and replugging. No game data. 8// each LEONIDA_DS_TRIGGERS mode, the lightbar, a dispatcher, unplugging and replugging, and the reading history behind
9// GetNextReading/GetPreviousReading. No game data.
9#include <windows.h> 10#include <windows.h>
10 11
11#include <SDL3/SDL.h> 12#include <SDL3/SDL.h>
@@ -132,6 +133,46 @@ void CALLBACK on_guide(GameInputCallbackToken, void*, IGameInputDevice*, uint64_
132 133
133void set_env(const char* name, const char* value) { SetEnvironmentVariableA(name, value); } 134void set_env(const char* name, const char* value) { SetEnvironmentVariableA(name, value); }
134 135
136uint32_t buttons_of(IGameInputReading* r) {
137 GameInputGamepadState s{};
138 return r && r->GetGamepadState(&s) ? s.buttons : ~0u;
139}
140
141IGameInputDevice* device_of(IGameInputReading* r) { // devices live for the process: no reference is kept
142 IGameInputDevice* d = nullptr;
143 if (r) r->GetDevice(&d);
144 if (d) d->Release();
145 return d;
146}
147
148IGameInputReading* current(IGameInput* input, IGameInputDevice* device) {
149 IGameInputReading* r = nullptr;
150 input->GetCurrentReading(GameInputKindGamepad, device, &r);
151 return r;
152}
153
154// Steps GetNextReading (forward) or GetPreviousReading from ref until a step fails or limit readings are collected.
155// The readings are referenced; *last is the final result.
156std::vector<IGameInputReading*> walk(IGameInput* input, IGameInputReading* ref, IGameInputDevice* device, bool forward,
157 size_t limit, HRESULT* last = nullptr) {
158 std::vector<IGameInputReading*> out;
159 HRESULT hr = E_INVALIDARG;
160 for (IGameInputReading* at = ref; at && out.size() < limit; at = out.back()) {
161 IGameInputReading* r = nullptr;
162 hr = forward ? input->GetNextReading(at, GameInputKindGamepad, device, &r)
163 : input->GetPreviousReading(at, GameInputKindGamepad, device, &r);
164 if (hr != S_OK) break;
165 out.push_back(r);
166 }
167 if (last) *last = hr;
168 return out;
169}
170
171void release_all(std::vector<IGameInputReading*>& v) {
172 for (IGameInputReading* r : v) r->Release();
173 v.clear();
174}
175
135} // namespace 176} // namespace
136 177
137int main() { 178int main() {
@@ -445,6 +486,134 @@ int main() {
445 nullptr, on_device, &blocking_token); 486 nullptr, on_device, &blocking_token);
446 check(g_device_events.size() == before.size() + 2, "blocking enumeration reports both pads before returning"); 487 check(g_device_events.size() == before.size() + 2, "blocking enumeration reports both pads before returning");
447 488
489 // ---- reading history: GetNextReading and GetPreviousReading ----
490 // A tap of A on the Xbox pad, pressed and released before the title reads again.
491 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_WEST, false);
492 SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_EAST, false);
493 rt::gameinput::poll();
494 IGameInputReading* idle = current(input, dev);
495 check(buttons_of(idle) == 0, "history: the reference reading is at rest");
496 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_SOUTH, true);
497 rt::gameinput::poll();
498 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_SOUTH, false);
499 rt::gameinput::poll();
500 HRESULT hr = S_OK;
501 std::vector<IGameInputReading*> seen = walk(input, idle, nullptr, true, 8, &hr);
502 check(seen.size() == 2 && buttons_of(seen[0]) == GameInputGamepadA && buttons_of(seen[1]) == 0 &&
503 hr == GAMEINPUT_E_READING_NOT_FOUND,
504 "history: GetNextReading gives a tap's press, its release, then READING_NOT_FOUND");
505 std::vector<IGameInputReading*> again = walk(input, seen.empty() ? nullptr : seen.back(), nullptr, false, 2);
506 check(again.size() == 2 && again[0] == seen[0] && again[1] == idle,
507 "history: GetPreviousReading walks back over the press to the reference");
508 release_all(again);
509 release_all(seen);
510 IGameInputReading* hit = nullptr;
511 check(input->GetNextReading(idle, GameInputKindGamepad | GameInputKindKeyboard, dev, &hit) == S_OK &&
512 buttons_of(hit) == GameInputGamepadA,
513 "history: a kind mask that includes the gamepad matches");
514 if (hit) hit->Release();
515 check(input->GetNextReading(idle, GameInputKindKeyboard, dev, &hit) == GAMEINPUT_E_READING_NOT_FOUND && !hit,
516 "history: keyboard alone finds no reading");
517 check(input->GetPreviousReading(idle, GameInputKindGamepad, dev, nullptr) == E_POINTER &&
518 input->GetPreviousReading(nullptr, GameInputKindGamepad, dev, &hit) == E_INVALIDARG && !hit,
519 "history: a null output is E_POINTER, a null reference E_INVALIDARG");
520
521 // Two pads in one stream, one poll each: A on the Xbox pad, B on the DualSense, Y on the Xbox pad.
522 IGameInputReading* start = current(input, dev);
523 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_SOUTH, true);
524 rt::gameinput::poll();
525 SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_EAST, true);
526 rt::gameinput::poll();
527 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_NORTH, true);
528 rt::gameinput::poll();
529 seen = walk(input, start, nullptr, true, 8, &hr);
530 const bool interleaved = seen.size() == 3 && device_of(seen[0]) == dev && device_of(seen[1]) == dsdev &&
531 device_of(seen[2]) == dev && buttons_of(seen[1]) == GameInputGamepadB;
532 check(interleaved && hr == GAMEINPUT_E_READING_NOT_FOUND,
533 "history: with no device filter, both pads' readings come in the order they were taken");
534 if (interleaved) {
535 again = walk(input, seen[2], nullptr, false, 3);
536 check(again.size() == 3 && again[0] == seen[1] && again[1] == seen[0] && again[2] == start,
537 "history: GetPreviousReading gives the exact reverse");
538 release_all(again);
539 again = walk(input, start, dev, true, 8);
540 check(again.size() == 2 && again[0] == seen[0] && again[1] == seen[2], "history: a device filter skips the other pad");
541 release_all(again);
542 check(input->GetNextReading(seen[0], GameInputKindGamepad, dsdev, &hit) == S_OK && hit == seen[1],
543 "history: filtering on the other pad gives its next reading after the reference");
544 if (hit) hit->Release();
545 check(input->GetNextReading(seen[1], GameInputKindGamepad, dsdev, &hit) == GAMEINPUT_E_READING_NOT_FOUND && !hit,
546 "history: and never one of the reference's pad");
547 }
548 release_all(seen);
549 if (start) start->Release();
550
551 // Both pads change in one poll and their readings share a timestamp: each still comes once, in both directions.
552 IGameInputReading* pair_ref = current(input, dev);
553 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_NORTH, false);
554 SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_EAST, false);
555 rt::gameinput::poll();
556 seen = walk(input, pair_ref, nullptr, true, 8, &hr);
557 const bool paired = seen.size() == 2 && seen[0]->GetTimestamp() == seen[1]->GetTimestamp() &&
558 device_of(seen[0]) != device_of(seen[1]);
559 check(paired && hr == GAMEINPUT_E_READING_NOT_FOUND, "history: two readings with one timestamp both come, once each");
560 if (paired) {
561 again = walk(input, seen[1], nullptr, false, 2);
562 check(again.size() == 2 && again[0] == seen[0] && again[1] == pair_ref, "history: and in reverse");
563 release_all(again);
564 }
565 release_all(seen);
566 if (pair_ref) pair_ref->Release();
567
568 // Ageing: a reading leaves the history once the reading after it is more than half a second old.
569 IGameInputReading* aged = current(input, dev);
570 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, true);
571 rt::gameinput::poll();
572 Sleep(600);
573 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, false);
574 rt::gameinput::poll();
575 const uint32_t a_ls = GameInputGamepadA | GameInputGamepadLeftShoulder;
576 check(input->GetNextReading(aged, GameInputKindGamepad, dev, &hit) == S_OK && buttons_of(hit) == a_ls,
577 "history: a reference that has aged out still leads to its successor while that is kept");
578 if (hit) hit->Release();
579 check(input->GetPreviousReading(aged, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_REFERENCE_READING_TOO_OLD && !hit,
580 "history: GetPreviousReading on an aged-out reference is REFERENCE_READING_TOO_OLD");
581 Sleep(600);
582 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, true);
583 rt::gameinput::poll();
584 check(input->GetNextReading(aged, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_REFERENCE_READING_TOO_OLD && !hit &&
585 input->GetNextReading(aged, GameInputKindGamepad, nullptr, &hit) == GAMEINPUT_E_REFERENCE_READING_TOO_OLD && !hit,
586 "history: once a reading after the reference is dropped, GetNextReading is REFERENCE_READING_TOO_OLD");
587 check(buttons_of(aged) == GameInputGamepadA && device_of(aged) == dev,
588 "history: a reading the title holds stays readable after it leaves the history");
589 IGameInputReading* fresh = current(input, dev);
590 check(fresh && input->GetNextReading(fresh, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_READING_NOT_FOUND && !hit,
591 "history: after GetCurrentReading the title is caught up");
592
593 // Unplugging drops the pad's history; plugged back in, its last reading leads to the reconnect snapshot.
594 const uint64_t last_seq = fresh ? fresh->GetSequenceNumber(GameInputKindGamepad) : 0;
595 detach(xbox2);
596 rt::gameinput::poll();
597 check(input->GetNextReading(fresh, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_DEVICE_DISCONNECTED && !hit &&
598 input->GetPreviousReading(fresh, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_DEVICE_DISCONNECTED && !hit,
599 "history: filtering on an unplugged pad is DEVICE_DISCONNECTED");
600 check(input->GetNextReading(fresh, GameInputKindGamepad, nullptr, &hit) == GAMEINPUT_E_READING_NOT_FOUND && !hit,
601 "history: with no filter, nothing follows an unplugged pad's last reading");
602 check(buttons_of(fresh) == a_ls && device_of(fresh) == dev, "history: a held reading stays readable after unplugging");
603 attach(xbox2, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true);
604 rt::gameinput::poll();
605 IGameInputReading* reconnect = nullptr;
606 check(input->GetNextReading(fresh, GameInputKindGamepad, dev, &reconnect) == S_OK && device_of(reconnect) == dev &&
607 buttons_of(reconnect) == 0 && reconnect->GetSequenceNumber(GameInputKindGamepad) > last_seq,
608 "history: after replugging, the last reading before the unplug leads to the reconnect snapshot");
609 check(reconnect && input->GetPreviousReading(reconnect, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_READING_NOT_FOUND && !hit,
610 "history: nothing comes before the reconnect snapshot");
611 check(input->GetNextReading(aged, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_REFERENCE_READING_TOO_OLD && !hit,
612 "history: an older reading from before the unplug is REFERENCE_READING_TOO_OLD");
613 if (reconnect) reconnect->Release();
614 check(idle && aged && fresh && idle->Release() == 0 && aged->Release() == 0 && fresh->Release() == 0,
615 "history: dropped readings are freed once the title releases them");
616
448 if (reading) reading->Release(); 617 if (reading) reading->Release();
449 dev->Release(); 618 dev->Release();
450 detach(xbox2); 619 detach(xbox2);