diff options
| author | nicolas <44988633+nschlaepfer@users.noreply.github.com> | 2026-11-21 21:58:55 -0700 |
|---|---|---|
| committer | nicolas <44988633+nschlaepfer@users.noreply.github.com> | 2026-11-21 21:58:55 -0700 |
| commit | 5190d597c8c6efd4cefa615df79e7c1a5f008775 (patch) | |
| tree | b61d71980b1fdbfb7552873b320a84416263c770 /runtime/test/gameinput_test.cpp | |
| parent | b72fc5a4fec776ffa4b19d37c7bf8d62836e3df5 (diff) | |
| parent | 067d3e8b97189fcf272dc699fdbb871eae496660 (diff) | |
Merge keyboard-pad coverage with controller reading history
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
| -rw-r--r-- | runtime/test/gameinput_test.cpp | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 671dbbbe5..531268d1a 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp | |||
| @@ -173,6 +173,137 @@ void release_all(std::vector<IGameInputReading*>& v) { | |||
| 173 | v.clear(); | 173 | v.clear(); |
| 174 | } | 174 | } |
| 175 | 175 | ||
| 176 | // ---- keyboard pad: a fake key source instead of GetAsyncKeyState ---- | ||
| 177 | bool g_keys[256] = {}; | ||
| 178 | bool g_focus = false; | ||
| 179 | bool fake_key_down(int vk) { return vk >= 0 && vk < 256 && g_keys[vk]; } | ||
| 180 | bool fake_focus() { return g_focus; } | ||
| 181 | |||
| 182 | GameInputGamepadState pad_state(IGameInput* input, IGameInputDevice* dev, uint32_t* ui = nullptr) { | ||
| 183 | GameInputGamepadState st{}; | ||
| 184 | IGameInputReading* r = nullptr; | ||
| 185 | if (input->GetCurrentReading(GameInputKindGamepad, dev, &r) == S_OK && r) { | ||
| 186 | r->GetGamepadState(&st); | ||
| 187 | if (ui) { | ||
| 188 | GameInputUiNavigationState nav{}; | ||
| 189 | r->GetUiNavigationState(&nav); | ||
| 190 | *ui = nav.buttons; | ||
| 191 | } | ||
| 192 | r->Release(); | ||
| 193 | } | ||
| 194 | return st; | ||
| 195 | } | ||
| 196 | |||
| 197 | // Runs with no other pad connected and the device callback from main still registered. | ||
| 198 | void test_keyboard_pad(IGameInput* input) { | ||
| 199 | rt::gameinput::set_keyboard_source(fake_key_down, fake_focus); | ||
| 200 | |||
| 201 | set_env("LEONIDA_KEYBOARD_PAD", nullptr); | ||
| 202 | rt::gameinput::reload_config(); | ||
| 203 | rt::gameinput::poll(); | ||
| 204 | check(rt::gameinput::connected_count() == 0, "keyboard pad: off by default"); | ||
| 205 | |||
| 206 | set_env("LEONIDA_KEYBOARD_PAD", "1"); | ||
| 207 | rt::gameinput::reload_config(); | ||
| 208 | check(rt::gameinput::config().keyboard_pad == rt::gameinput::KeyboardPadMode::Focused, | ||
| 209 | "keyboard pad: LEONIDA_KEYBOARD_PAD=1 is the focused mode"); | ||
| 210 | const size_t events_before = g_device_events.size(); | ||
| 211 | rt::gameinput::poll(); | ||
| 212 | check(rt::gameinput::connected_count() == 1, "keyboard pad: attached as one gamepad"); | ||
| 213 | // Main leaves three device callbacks registered (blocking, async and blocking enumeration), so one connect is | ||
| 214 | // reported to each of them: all with the same device. | ||
| 215 | IGameInputDevice* kb = g_device_events.size() > events_before ? g_device_events.back().device : nullptr; | ||
| 216 | bool connect_events = g_device_events.size() == events_before + 3; | ||
| 217 | for (size_t i = events_before; i < g_device_events.size(); ++i) | ||
| 218 | connect_events &= g_device_events[i].device == kb && (g_device_events[i].current & GameInputDeviceConnected) && | ||
| 219 | g_device_events[i].previous == GameInputDeviceNoStatus; | ||
| 220 | check(connect_events, "keyboard pad: device callbacks on connect"); | ||
| 221 | if (!kb) return; | ||
| 222 | const GameInputDeviceInfo* info = kb->GetDeviceInfo(); | ||
| 223 | check(info->displayName && std::strcmp(info->displayName->data, "Keyboard pad (Leonida)") == 0, | ||
| 224 | "keyboard pad: display name"); | ||
| 225 | check((info->supportedInput & GameInputKindGamepad) && info->supportedRumbleMotors == 0, | ||
| 226 | "keyboard pad: a gamepad without motors"); | ||
| 227 | check(pad_state(input, kb).buttons == 0, "keyboard pad: at rest"); | ||
| 228 | |||
| 229 | // Out of focus, keys are ignored. | ||
| 230 | g_focus = false; | ||
| 231 | g_keys[VK_RETURN] = true; | ||
| 232 | rt::gameinput::poll(); | ||
| 233 | check(pad_state(input, kb).buttons == 0, "keyboard pad: no buttons while another window has focus"); | ||
| 234 | |||
| 235 | // In focus, every binding presses its button. | ||
| 236 | g_focus = true; | ||
| 237 | g_keys[VK_RETURN] = false; | ||
| 238 | struct Expect { int vk; uint32_t button; const char* what; }; | ||
| 239 | const Expect expect[] = { | ||
| 240 | {VK_UP, GameInputGamepadDPadUp, "keyboard pad: Up is d-pad up"}, | ||
| 241 | {VK_DOWN, GameInputGamepadDPadDown, "keyboard pad: Down is d-pad down"}, | ||
| 242 | {VK_LEFT, GameInputGamepadDPadLeft, "keyboard pad: Left is d-pad left"}, | ||
| 243 | {VK_RIGHT, GameInputGamepadDPadRight, "keyboard pad: Right is d-pad right"}, | ||
| 244 | {VK_RETURN, GameInputGamepadA, "keyboard pad: Enter is A"}, | ||
| 245 | {VK_BACK, GameInputGamepadB, "keyboard pad: Backspace is B"}, | ||
| 246 | {'X', GameInputGamepadX, "keyboard pad: X is X"}, | ||
| 247 | {'Y', GameInputGamepadY, "keyboard pad: Y is Y"}, | ||
| 248 | {'Q', GameInputGamepadLeftShoulder, "keyboard pad: Q is the left shoulder"}, | ||
| 249 | {'E', GameInputGamepadRightShoulder, "keyboard pad: E is the right shoulder"}, | ||
| 250 | {VK_ESCAPE, GameInputGamepadMenu, "keyboard pad: Esc is Menu"}, | ||
| 251 | {VK_TAB, GameInputGamepadView, "keyboard pad: Tab is View"}, | ||
| 252 | }; | ||
| 253 | for (const Expect& e : expect) { | ||
| 254 | g_keys[e.vk] = true; | ||
| 255 | rt::gameinput::poll(); | ||
| 256 | check(pad_state(input, kb).buttons == e.button, e.what); | ||
| 257 | g_keys[e.vk] = false; | ||
| 258 | } | ||
| 259 | int bindings = 0; | ||
| 260 | rt::gameinput::keyboard_bindings(&bindings); | ||
| 261 | check(bindings == static_cast<int>(std::size(expect)), "keyboard pad: every binding is tested"); | ||
| 262 | |||
| 263 | // Two keys at once, and UI navigation from the d-pad and A. | ||
| 264 | g_keys[VK_DOWN] = g_keys[VK_RETURN] = true; | ||
| 265 | rt::gameinput::poll(); | ||
| 266 | uint32_t ui = 0; | ||
| 267 | const GameInputGamepadState both = pad_state(input, kb, &ui); | ||
| 268 | check(both.buttons == (GameInputGamepadDPadDown | GameInputGamepadA), "keyboard pad: Down and Enter together"); | ||
| 269 | check((ui & GameInputUiNavigationDown) && (ui & GameInputUiNavigationAccept), "keyboard pad: UI navigation down and accept"); | ||
| 270 | check(approx(both.leftTrigger, 0) && approx(both.rightTrigger, 0) && approx(both.leftThumbstickX, 0) && | ||
| 271 | approx(both.rightThumbstickY, 0), | ||
| 272 | "keyboard pad: sticks and triggers stay at rest"); | ||
| 273 | |||
| 274 | // Losing focus releases held keys. | ||
| 275 | g_focus = false; | ||
| 276 | rt::gameinput::poll(); | ||
| 277 | check(pad_state(input, kb).buttons == 0, "keyboard pad: losing focus releases every button"); | ||
| 278 | |||
| 279 | // The always mode ignores focus. | ||
| 280 | set_env("LEONIDA_KEYBOARD_PAD", "always"); | ||
| 281 | rt::gameinput::reload_config(); | ||
| 282 | rt::gameinput::poll(); | ||
| 283 | check(pad_state(input, kb).buttons == (GameInputGamepadDPadDown | GameInputGamepadA), | ||
| 284 | "keyboard pad: LEONIDA_KEYBOARD_PAD=always reads keys without focus"); | ||
| 285 | g_keys[VK_DOWN] = g_keys[VK_RETURN] = false; | ||
| 286 | |||
| 287 | // Turning it off removes the pad; turning it on again brings back the same device object. | ||
| 288 | set_env("LEONIDA_KEYBOARD_PAD", "off"); | ||
| 289 | rt::gameinput::reload_config(); | ||
| 290 | rt::gameinput::poll(); | ||
| 291 | check(rt::gameinput::connected_count() == 0 && kb->GetDeviceStatus() == GameInputDeviceNoStatus, | ||
| 292 | "keyboard pad: off removes it"); | ||
| 293 | set_env("LEONIDA_KEYBOARD_PAD", "1"); | ||
| 294 | rt::gameinput::reload_config(); | ||
| 295 | rt::gameinput::poll(); | ||
| 296 | check(rt::gameinput::connected_count() == 1 && g_device_events.back().device == kb && | ||
| 297 | (kb->GetDeviceStatus() & GameInputDeviceConnected), | ||
| 298 | "keyboard pad: on again is the same device object"); | ||
| 299 | |||
| 300 | set_env("LEONIDA_KEYBOARD_PAD", nullptr); | ||
| 301 | rt::gameinput::reload_config(); | ||
| 302 | rt::gameinput::poll(); | ||
| 303 | check(rt::gameinput::connected_count() == 0, "keyboard pad: removed at the end"); | ||
| 304 | rt::gameinput::set_keyboard_source(nullptr, nullptr); | ||
| 305 | } | ||
| 306 | |||
| 176 | } // namespace | 307 | } // namespace |
| 177 | 308 | ||
| 178 | int main() { | 309 | int main() { |
| @@ -620,6 +751,8 @@ int main() { | |||
| 620 | detach(ds); | 751 | detach(ds); |
| 621 | rt::gameinput::poll(); | 752 | rt::gameinput::poll(); |
| 622 | check(rt::gameinput::connected_count() == 0, "all pads closed after detaching"); | 753 | check(rt::gameinput::connected_count() == 0, "all pads closed after detaching"); |
| 754 | |||
| 755 | test_keyboard_pad(input); | ||
| 623 | input->Release(); | 756 | input->Release(); |
| 624 | 757 | ||
| 625 | std::printf("%d checks, %d failures\n", g_checks, g_failures); | 758 | std::printf("%d checks, %d failures\n", g_checks, g_failures); |