diff options
| author | Cozma <65982369+Cozmaaa@users.noreply.github.com> | 2026-11-22 06:05:38 +0200 |
|---|---|---|
| committer | Cozma <65982369+Cozmaaa@users.noreply.github.com> | 2026-11-22 06:05:38 +0200 |
| commit | 5097ea041aef13bbb6b11c7f6f92dd2ee515bc53 (patch) | |
| tree | c77a10ab1d8b67175ecdf5693b0d74c70b0953d9 /runtime/test/gameinput_test.cpp | |
| parent | 4a36bb19979b611f525d027f92e0fefad1626546 (diff) | |
GameInput shim: keyboard pad (LEONIDA_KEYBOARD_PAD)
The title reads gamepads only, so keys reach it as one more pad: an SDL
virtual gamepad whose buttons are pressed from GetAsyncKeyState on each
poll. It goes through open_pad/read_pad like a real controller (device
object, player slot, readings, callbacks, UI navigation) and reports no
motors.
Arrows d-pad, Enter A, Backspace B, X X, Y Y, Q/E shoulders, Esc Menu,
Tab View. Off by default; =1 reads keys only while a window of this
process is in the foreground and releases everything on focus loss,
=always ignores focus (console tools). Sticks and triggers stay at rest.
gameinput_test drives it through set_keyboard_source with fake keys and
focus: 31 new checks.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 1a154a7a9..e042a3d8c 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp | |||
| @@ -132,6 +132,137 @@ void CALLBACK on_guide(GameInputCallbackToken, void*, IGameInputDevice*, uint64_ | |||
| 132 | 132 | ||
| 133 | void set_env(const char* name, const char* value) { SetEnvironmentVariableA(name, value); } | 133 | void set_env(const char* name, const char* value) { SetEnvironmentVariableA(name, value); } |
| 134 | 134 | ||
| 135 | // ---- keyboard pad: a fake key source instead of GetAsyncKeyState ---- | ||
| 136 | bool g_keys[256] = {}; | ||
| 137 | bool g_focus = false; | ||
| 138 | bool fake_key_down(int vk) { return vk >= 0 && vk < 256 && g_keys[vk]; } | ||
| 139 | bool fake_focus() { return g_focus; } | ||
| 140 | |||
| 141 | GameInputGamepadState pad_state(IGameInput* input, IGameInputDevice* dev, uint32_t* ui = nullptr) { | ||
| 142 | GameInputGamepadState st{}; | ||
| 143 | IGameInputReading* r = nullptr; | ||
| 144 | if (input->GetCurrentReading(GameInputKindGamepad, dev, &r) == S_OK && r) { | ||
| 145 | r->GetGamepadState(&st); | ||
| 146 | if (ui) { | ||
| 147 | GameInputUiNavigationState nav{}; | ||
| 148 | r->GetUiNavigationState(&nav); | ||
| 149 | *ui = nav.buttons; | ||
| 150 | } | ||
| 151 | r->Release(); | ||
| 152 | } | ||
| 153 | return st; | ||
| 154 | } | ||
| 155 | |||
| 156 | // Runs with no other pad connected and the device callback from main still registered. | ||
| 157 | void test_keyboard_pad(IGameInput* input) { | ||
| 158 | rt::gameinput::set_keyboard_source(fake_key_down, fake_focus); | ||
| 159 | |||
| 160 | set_env("LEONIDA_KEYBOARD_PAD", nullptr); | ||
| 161 | rt::gameinput::reload_config(); | ||
| 162 | rt::gameinput::poll(); | ||
| 163 | check(rt::gameinput::connected_count() == 0, "keyboard pad: off by default"); | ||
| 164 | |||
| 165 | set_env("LEONIDA_KEYBOARD_PAD", "1"); | ||
| 166 | rt::gameinput::reload_config(); | ||
| 167 | check(rt::gameinput::config().keyboard_pad == rt::gameinput::KeyboardPadMode::Focused, | ||
| 168 | "keyboard pad: LEONIDA_KEYBOARD_PAD=1 is the focused mode"); | ||
| 169 | const size_t events_before = g_device_events.size(); | ||
| 170 | rt::gameinput::poll(); | ||
| 171 | check(rt::gameinput::connected_count() == 1, "keyboard pad: attached as one gamepad"); | ||
| 172 | // Main leaves three device callbacks registered (blocking, async and blocking enumeration), so one connect is | ||
| 173 | // reported to each of them: all with the same device. | ||
| 174 | IGameInputDevice* kb = g_device_events.size() > events_before ? g_device_events.back().device : nullptr; | ||
| 175 | bool connect_events = g_device_events.size() == events_before + 3; | ||
| 176 | for (size_t i = events_before; i < g_device_events.size(); ++i) | ||
| 177 | connect_events &= g_device_events[i].device == kb && (g_device_events[i].current & GameInputDeviceConnected) && | ||
| 178 | g_device_events[i].previous == GameInputDeviceNoStatus; | ||
| 179 | check(connect_events, "keyboard pad: device callbacks on connect"); | ||
| 180 | if (!kb) return; | ||
| 181 | const GameInputDeviceInfo* info = kb->GetDeviceInfo(); | ||
| 182 | check(info->displayName && std::strcmp(info->displayName->data, "Keyboard pad (Leonida)") == 0, | ||
| 183 | "keyboard pad: display name"); | ||
| 184 | check((info->supportedInput & GameInputKindGamepad) && info->supportedRumbleMotors == 0, | ||
| 185 | "keyboard pad: a gamepad without motors"); | ||
| 186 | check(pad_state(input, kb).buttons == 0, "keyboard pad: at rest"); | ||
| 187 | |||
| 188 | // Out of focus, keys are ignored. | ||
| 189 | g_focus = false; | ||
| 190 | g_keys[VK_RETURN] = true; | ||
| 191 | rt::gameinput::poll(); | ||
| 192 | check(pad_state(input, kb).buttons == 0, "keyboard pad: no buttons while another window has focus"); | ||
| 193 | |||
| 194 | // In focus, every binding presses its button. | ||
| 195 | g_focus = true; | ||
| 196 | g_keys[VK_RETURN] = false; | ||
| 197 | struct Expect { int vk; uint32_t button; const char* what; }; | ||
| 198 | const Expect expect[] = { | ||
| 199 | {VK_UP, GameInputGamepadDPadUp, "keyboard pad: Up is d-pad up"}, | ||
| 200 | {VK_DOWN, GameInputGamepadDPadDown, "keyboard pad: Down is d-pad down"}, | ||
| 201 | {VK_LEFT, GameInputGamepadDPadLeft, "keyboard pad: Left is d-pad left"}, | ||
| 202 | {VK_RIGHT, GameInputGamepadDPadRight, "keyboard pad: Right is d-pad right"}, | ||
| 203 | {VK_RETURN, GameInputGamepadA, "keyboard pad: Enter is A"}, | ||
| 204 | {VK_BACK, GameInputGamepadB, "keyboard pad: Backspace is B"}, | ||
| 205 | {'X', GameInputGamepadX, "keyboard pad: X is X"}, | ||
| 206 | {'Y', GameInputGamepadY, "keyboard pad: Y is Y"}, | ||
| 207 | {'Q', GameInputGamepadLeftShoulder, "keyboard pad: Q is the left shoulder"}, | ||
| 208 | {'E', GameInputGamepadRightShoulder, "keyboard pad: E is the right shoulder"}, | ||
| 209 | {VK_ESCAPE, GameInputGamepadMenu, "keyboard pad: Esc is Menu"}, | ||
| 210 | {VK_TAB, GameInputGamepadView, "keyboard pad: Tab is View"}, | ||
| 211 | }; | ||
| 212 | for (const Expect& e : expect) { | ||
| 213 | g_keys[e.vk] = true; | ||
| 214 | rt::gameinput::poll(); | ||
| 215 | check(pad_state(input, kb).buttons == e.button, e.what); | ||
| 216 | g_keys[e.vk] = false; | ||
| 217 | } | ||
| 218 | int bindings = 0; | ||
| 219 | rt::gameinput::keyboard_bindings(&bindings); | ||
| 220 | check(bindings == static_cast<int>(std::size(expect)), "keyboard pad: every binding is tested"); | ||
| 221 | |||
| 222 | // Two keys at once, and UI navigation from the d-pad and A. | ||
| 223 | g_keys[VK_DOWN] = g_keys[VK_RETURN] = true; | ||
| 224 | rt::gameinput::poll(); | ||
| 225 | uint32_t ui = 0; | ||
| 226 | const GameInputGamepadState both = pad_state(input, kb, &ui); | ||
| 227 | check(both.buttons == (GameInputGamepadDPadDown | GameInputGamepadA), "keyboard pad: Down and Enter together"); | ||
| 228 | check((ui & GameInputUiNavigationDown) && (ui & GameInputUiNavigationAccept), "keyboard pad: UI navigation down and accept"); | ||
| 229 | check(approx(both.leftTrigger, 0) && approx(both.rightTrigger, 0) && approx(both.leftThumbstickX, 0) && | ||
| 230 | approx(both.rightThumbstickY, 0), | ||
| 231 | "keyboard pad: sticks and triggers stay at rest"); | ||
| 232 | |||
| 233 | // Losing focus releases held keys. | ||
| 234 | g_focus = false; | ||
| 235 | rt::gameinput::poll(); | ||
| 236 | check(pad_state(input, kb).buttons == 0, "keyboard pad: losing focus releases every button"); | ||
| 237 | |||
| 238 | // The always mode ignores focus. | ||
| 239 | set_env("LEONIDA_KEYBOARD_PAD", "always"); | ||
| 240 | rt::gameinput::reload_config(); | ||
| 241 | rt::gameinput::poll(); | ||
| 242 | check(pad_state(input, kb).buttons == (GameInputGamepadDPadDown | GameInputGamepadA), | ||
| 243 | "keyboard pad: LEONIDA_KEYBOARD_PAD=always reads keys without focus"); | ||
| 244 | g_keys[VK_DOWN] = g_keys[VK_RETURN] = false; | ||
| 245 | |||
| 246 | // Turning it off removes the pad; turning it on again brings back the same device object. | ||
| 247 | set_env("LEONIDA_KEYBOARD_PAD", "off"); | ||
| 248 | rt::gameinput::reload_config(); | ||
| 249 | rt::gameinput::poll(); | ||
| 250 | check(rt::gameinput::connected_count() == 0 && kb->GetDeviceStatus() == GameInputDeviceNoStatus, | ||
| 251 | "keyboard pad: off removes it"); | ||
| 252 | set_env("LEONIDA_KEYBOARD_PAD", "1"); | ||
| 253 | rt::gameinput::reload_config(); | ||
| 254 | rt::gameinput::poll(); | ||
| 255 | check(rt::gameinput::connected_count() == 1 && g_device_events.back().device == kb && | ||
| 256 | (kb->GetDeviceStatus() & GameInputDeviceConnected), | ||
| 257 | "keyboard pad: on again is the same device object"); | ||
| 258 | |||
| 259 | set_env("LEONIDA_KEYBOARD_PAD", nullptr); | ||
| 260 | rt::gameinput::reload_config(); | ||
| 261 | rt::gameinput::poll(); | ||
| 262 | check(rt::gameinput::connected_count() == 0, "keyboard pad: removed at the end"); | ||
| 263 | rt::gameinput::set_keyboard_source(nullptr, nullptr); | ||
| 264 | } | ||
| 265 | |||
| 135 | } // namespace | 266 | } // namespace |
| 136 | 267 | ||
| 137 | int main() { | 268 | int main() { |
| @@ -451,6 +582,8 @@ int main() { | |||
| 451 | detach(ds); | 582 | detach(ds); |
| 452 | rt::gameinput::poll(); | 583 | rt::gameinput::poll(); |
| 453 | check(rt::gameinput::connected_count() == 0, "all pads closed after detaching"); | 584 | check(rt::gameinput::connected_count() == 0, "all pads closed after detaching"); |
| 585 | |||
| 586 | test_keyboard_pad(input); | ||
| 454 | input->Release(); | 587 | input->Release(); |
| 455 | 588 | ||
| 456 | std::printf("%d checks, %d failures\n", g_checks, g_failures); | 589 | std::printf("%d checks, %d failures\n", g_checks, g_failures); |