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.cpp127
1 files changed, 127 insertions, 0 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp
index d1923ceaf..a65b444a1 100644
--- a/runtime/test/gameinput_test.cpp
+++ b/runtime/test/gameinput_test.cpp
@@ -393,6 +393,33 @@ void test_keyboard_pad_sticks(IGameInput* input, IGameInputDevice* kb) {
393 } 393 }
394 check(monotonic && polls < 100 && sticks_at_rest(st), "keyboard pad: right stick decays to 0 when the mouse stops"); 394 check(monotonic && polls < 100 && sticks_at_rest(st), "keyboard pad: right stick decays to 0 when the mouse stops");
395 395
396 // LEONIDA_MOUSE_SENSITIVITY scales the axis units per count: 2 doubles, 0.5 halves; out of range clamps.
397 auto settle = [&] {
398 for (int i = 0; i < 200 && !sticks_at_rest(pad_state(input, kb)); ++i) rt::gameinput::poll();
399 };
400 set_env("LEONIDA_MOUSE_SENSITIVITY", "2");
401 rt::gameinput::reload_config();
402 g_cursor_x += 10;
403 rt::gameinput::poll();
404 check(approx(pad_state(input, kb).rightThumbstickX, 20480 / 32767.0f), "keyboard pad: mouse sensitivity 2 doubles the stick");
405 settle();
406 set_env("LEONIDA_MOUSE_SENSITIVITY", "0.5");
407 rt::gameinput::reload_config();
408 g_cursor_x -= 10;
409 rt::gameinput::poll();
410 check(approx(pad_state(input, kb).rightThumbstickX, -5120 / 32767.0f), "keyboard pad: mouse sensitivity 0.5 halves the stick");
411 settle();
412 set_env("LEONIDA_MOUSE_SENSITIVITY", "100");
413 rt::gameinput::reload_config();
414 check(rt::gameinput::config().mouse_sensitivity == 10.0f, "keyboard pad: mouse sensitivity clamps to 10");
415 set_env("LEONIDA_MOUSE_SENSITIVITY", "0");
416 rt::gameinput::reload_config();
417 check(approx(rt::gameinput::config().mouse_sensitivity, 0.1f), "keyboard pad: mouse sensitivity clamps to 0.1");
418 set_env("LEONIDA_MOUSE_SENSITIVITY", nullptr);
419 rt::gameinput::reload_config();
420 check(rt::gameinput::config().mouse_sensitivity == 1.0f && rt::gameinput::config().ds_trigger_gain == 1.0f,
421 "keyboard pad: mouse sensitivity defaults to 1 and leaves the trigger gain alone");
422
396 g_keys[VK_RBUTTON] = true; 423 g_keys[VK_RBUTTON] = true;
397 rt::gameinput::poll(); 424 rt::gameinput::poll();
398 st = pad_state(input, kb); 425 st = pad_state(input, kb);
@@ -577,6 +604,94 @@ bool found_is(IGameInput* input, IGameInputDevice* dev) {
577} 604}
578 605
579// Pads SDL cannot tell apart: the same name, vendor and product, no serial, no path. Runs with no other pad connected. 606// Pads SDL cannot tell apart: the same name, vendor and product, no serial, no path. Runs with no other pad connected.
607void test_aggregate_device(IGameInput* input) {
608 check(input->CreateAggregateDevice(GameInputKindGamepad, nullptr) == E_POINTER,
609 "CreateAggregateDevice rejects null output pointer");
610 IGameInputDevice* agg = nullptr;
611 check(input->CreateAggregateDevice(GameInputKindUnknown, &agg) == E_INVALIDARG && !agg,
612 "CreateAggregateDevice rejects GameInputKindUnknown");
613 check(input->CreateAggregateDevice(GameInputKindGamepad | GameInputKindKeyboard, &agg) == E_INVALIDARG && !agg,
614 "CreateAggregateDevice rejects multiple combined kind flags");
615 check(input->CreateAggregateDevice(0x80000000, &agg) == E_INVALIDARG && !agg,
616 "CreateAggregateDevice rejects unsupported kind flag");
617
618 check(input->CreateAggregateDevice(GameInputKindGamepad, &agg) == S_OK && agg != nullptr,
619 "CreateAggregateDevice creates an aggregate device for GameInputKindGamepad");
620 if (agg) {
621 const GameInputDeviceInfo* info = agg->GetDeviceInfo();
622 check(info && info->deviceFamily == GameInputFamilyAggregate,
623 "aggregate device reports GameInputFamilyAggregate");
624 check(info && info->supportedInput == GameInputKindGamepad,
625 "aggregate device reports supportedInput == GameInputKindGamepad");
626 check(agg->GetDeviceStatus() == GameInputDeviceConnected,
627 "aggregate device status is GameInputDeviceConnected");
628
629 IGameInputDevice* agg2 = nullptr;
630 check(input->CreateAggregateDevice(GameInputKindGamepad, &agg2) == S_OK && agg2 == agg,
631 "CreateAggregateDevice returns existing aggregate instance for the same kind");
632 if (agg2) agg2->Release();
633
634 // Reading test with no connected pad
635 IGameInputReading* r = nullptr;
636 check(input->GetCurrentReading(GameInputKindGamepad, agg, &r) == GAMEINPUT_E_READING_NOT_FOUND && !r,
637 "aggregate device returns READING_NOT_FOUND when no pads are connected");
638 check(input->GetCurrentReading(GameInputKindKeyboard, agg, &r) == GAMEINPUT_E_READING_NOT_FOUND && !r,
639 "aggregate device returns READING_NOT_FOUND for kind mismatch with no pads");
640
641 // Callback registration guards for aggregate device
642 GameInputCallbackToken reading_tok = 0xbeef;
643 check(input->RegisterReadingCallback(agg, GameInputKindGamepad, 0.0f, nullptr, on_reading, &reading_tok) == E_NOTIMPL &&
644 reading_tok == kInvalidCallbackToken,
645 "RegisterReadingCallback rejects aggregate device with E_NOTIMPL and invalid token");
646
647 GameInputCallbackToken device_tok = 0xbeef;
648 check(input->RegisterDeviceCallback(agg, GameInputKindGamepad, GameInputDeviceConnected,
649 GameInputBlockingEnumeration, nullptr, on_device, &device_tok) == E_NOTIMPL &&
650 device_tok == kInvalidCallbackToken,
651 "RegisterDeviceCallback rejects aggregate device with E_NOTIMPL and invalid token");
652
653 // Attach a virtual pad and verify aggregate device yields readings from it
654 Pad xbox;
655 attach(xbox, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true);
656 rt::gameinput::poll();
657
658 SDL_SetJoystickVirtualButton(xbox.js, SDL_GAMEPAD_BUTTON_SOUTH, true);
659 rt::gameinput::poll();
660
661 check(input->GetCurrentReading(GameInputKindGamepad, agg, &r) == S_OK && r != nullptr,
662 "aggregate device yields reading from connected pad");
663 if (r) {
664 check(buttons_of(r) == GameInputGamepadA, "aggregate device reading reflects pad input state");
665 r->Release();
666 }
667
668 IGameInputReading* r_mismatch = nullptr;
669 check(input->GetCurrentReading(GameInputKindKeyboard, agg, &r_mismatch) == GAMEINPUT_E_READING_NOT_FOUND && !r_mismatch,
670 "aggregate device rejects kind mismatch even when pad is connected");
671
672 SDL_SetJoystickVirtualButton(xbox.js, SDL_GAMEPAD_BUTTON_SOUTH, false);
673 rt::gameinput::poll();
674 IGameInputReading* r2 = nullptr;
675 check(input->GetCurrentReading(GameInputKindGamepad, agg, &r2) == S_OK && r2 != nullptr,
676 "aggregate device yields updated reading after button release");
677 if (r2) {
678 check(buttons_of(r2) == 0, "aggregate reading shows button released");
679 IGameInputReading* prev = nullptr;
680 check(input->GetPreviousReading(r2, GameInputKindGamepad, agg, &prev) == S_OK && prev != nullptr,
681 "GetPreviousReading with aggregate device filter finds prior reading");
682 if (prev) {
683 check(buttons_of(prev) == GameInputGamepadA, "prior reading has pressed button");
684 prev->Release();
685 }
686 r2->Release();
687 }
688
689 detach(xbox);
690 rt::gameinput::poll();
691 agg->Release();
692 }
693}
694
580void test_twins(IGameInput* input) { 695void test_twins(IGameInput* input) {
581 Pad pads[3]; 696 Pad pads[3];
582 IGameInputDevice* dev[3] = {}; 697 IGameInputDevice* dev[3] = {};
@@ -992,6 +1107,17 @@ int main() {
992 check(input->GetPreviousReading(idle, GameInputKindGamepad, dev, nullptr) == E_POINTER && 1107 check(input->GetPreviousReading(idle, GameInputKindGamepad, dev, nullptr) == E_POINTER &&
993 input->GetPreviousReading(nullptr, GameInputKindGamepad, dev, &hit) == E_INVALIDARG && !hit, 1108 input->GetPreviousReading(nullptr, GameInputKindGamepad, dev, &hit) == E_INVALIDARG && !hit,
994 "history: a null output is E_POINTER, a null reference E_INVALIDARG"); 1109 "history: a null output is E_POINTER, a null reference E_INVALIDARG");
1110 // GetTemporalReading: E_NOTIMPL with a null reading, as GameInput v0 itself, for a timestamp in the history, one
1111 // before it and the current time, with and without a device filter.
1112 const uint64_t idle_us = idle->GetTimestamp();
1113 bool temporal_notimpl = true;
1114 for (const uint64_t at : {idle_us, idle_us - 1'000'000, input->GetCurrentTimestamp()})
1115 for (IGameInputDevice* filter : {dev, static_cast<IGameInputDevice*>(nullptr)}) {
1116 IGameInputReading* temporal = idle; // a stale value the call must clear
1117 temporal_notimpl &= input->GetTemporalReading(at, filter, &temporal) == E_NOTIMPL && !temporal;
1118 }
1119 check(temporal_notimpl && input->GetTemporalReading(idle_us, dev, nullptr) == E_POINTER,
1120 "history: GetTemporalReading is E_NOTIMPL with a null reading, as in GameInput v0");
995 1121
996 // Two pads in one stream, one poll each: A on the Xbox pad, B on the DualSense, Y on the Xbox pad. 1122 // Two pads in one stream, one poll each: A on the Xbox pad, B on the DualSense, Y on the Xbox pad.
997 IGameInputReading* start = current(input, dev); 1123 IGameInputReading* start = current(input, dev);
@@ -1099,6 +1225,7 @@ int main() {
1099 test_twins(input); 1225 test_twins(input);
1100 test_keyboard_pad(input); 1226 test_keyboard_pad(input);
1101 test_motion(input); 1227 test_motion(input);
1228 test_aggregate_device(input);
1102 input->Release(); 1229 input->Release();
1103 1230
1104 std::printf("%d checks, %d failures\n", g_checks, g_failures); 1231 std::printf("%d checks, %d failures\n", g_checks, g_failures);