diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 23:15:39 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 23:15:39 -0500 |
| commit | 12e53efa62078ca408a8fcaf4df909f91a4b2bb2 (patch) | |
| tree | 4f97b83c624065221e26cb73ba1767160a8381be /runtime | |
| parent | 169205d19dcc837d5c364a02540fca8bba356ff8 (diff) | |
| parent | 5097ea041aef13bbb6b11c7f6f92dd2ee515bc53 (diff) | |
Merge pull request #875 from Cozma
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/shims/gameinput.cpp | 113 | ||||
| -rw-r--r-- | runtime/shims/gameinput.h | 20 | ||||
| -rw-r--r-- | runtime/test/gameinput_test.cpp | 133 |
3 files changed, 266 insertions, 0 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 4b5d693ce..256a2c47c 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp | |||
| @@ -11,6 +11,7 @@ | |||
| 11 | // effect (SDL_SendGamepadEffect), as set by LEONIDA_DS_TRIGGERS. That is an approximation of the Xbox impulse | 11 | // effect (SDL_SendGamepadEffect), as set by LEONIDA_DS_TRIGGERS. That is an approximation of the Xbox impulse |
| 12 | // trigger signal, not the PS5 version's authored effects. | 12 | // trigger signal, not the PS5 version's authored effects. |
| 13 | // - DualSense/DualShock 4 lightbar colour (LEONIDA_DS_LIGHTBAR) and the player LEDs by connection order | 13 | // - DualSense/DualShock 4 lightbar colour (LEONIDA_DS_LIGHTBAR) and the player LEDs by connection order |
| 14 | // - the keyboard pad (LEONIDA_KEYBOARD_PAD): one SDL virtual gamepad whose buttons the keyboard presses, for menus | ||
| 14 | // | 15 | // |
| 15 | // One thread owns SDL: the background poll thread by default, or the caller of rt::gameinput::poll() when | 16 | // One thread owns SDL: the background poll thread by default, or the caller of rt::gameinput::poll() when |
| 16 | // LEONIDA_GAMEINPUT_THREAD=0. Title calls never touch SDL: they read the latest snapshot and leave rumble requests | 17 | // LEONIDA_GAMEINPUT_THREAD=0. Title calls never touch SDL: they read the latest snapshot and leave rumble requests |
| @@ -109,6 +110,12 @@ rt::gameinput::Config read_config() { | |||
| 109 | } | 110 | } |
| 110 | if (env("LEONIDA_RUMBLE") == "0") c.rumble = false; | 111 | if (env("LEONIDA_RUMBLE") == "0") c.rumble = false; |
| 111 | if (env("LEONIDA_GAMEINPUT_THREAD") == "0") c.background_thread = false; | 112 | if (env("LEONIDA_GAMEINPUT_THREAD") == "0") c.background_thread = false; |
| 113 | const std::string k = env("LEONIDA_KEYBOARD_PAD"); | ||
| 114 | if (k == "1" || k == "on" || k == "focus") c.keyboard_pad = rt::gameinput::KeyboardPadMode::Focused; | ||
| 115 | else if (k == "always") c.keyboard_pad = rt::gameinput::KeyboardPadMode::Always; | ||
| 116 | else if (!k.empty() && k != "0" && k != "off") | ||
| 117 | std::fprintf(stderr, "[rt] gameinput: LEONIDA_KEYBOARD_PAD=%s unknown (1, always or off), keyboard pad off\n", | ||
| 118 | k.c_str()); | ||
| 112 | return c; | 119 | return c; |
| 113 | } | 120 | } |
| 114 | 121 | ||
| @@ -716,6 +723,101 @@ void refresh_battery(Device* d, uint64_t now) { | |||
| 716 | d->battery = b; | 723 | d->battery = b; |
| 717 | } | 724 | } |
| 718 | 725 | ||
| 726 | // ---- keyboard pad (poll thread only) ---------------------------------------------------------------------------- | ||
| 727 | // | ||
| 728 | // The title reads gamepads, not the keyboard, so keys reach it as one more pad: an SDL virtual gamepad that this | ||
| 729 | // code presses buttons on each poll. It then goes through open_pad and read_pad like any controller, so it gets a | ||
| 730 | // device object, a player slot, readings, callbacks and UI navigation the same way. It has no motors, so rumble sent | ||
| 731 | // to it is dropped. Keys are read with GetAsyncKeyState, which needs no window or message loop and works under Wine. | ||
| 732 | |||
| 733 | constexpr rt::gameinput::KeyBinding kKeyBindings[] = { | ||
| 734 | {VK_UP, SDL_GAMEPAD_BUTTON_DPAD_UP, "Up"}, | ||
| 735 | {VK_DOWN, SDL_GAMEPAD_BUTTON_DPAD_DOWN, "Down"}, | ||
| 736 | {VK_LEFT, SDL_GAMEPAD_BUTTON_DPAD_LEFT, "Left"}, | ||
| 737 | {VK_RIGHT, SDL_GAMEPAD_BUTTON_DPAD_RIGHT, "Right"}, | ||
| 738 | {VK_RETURN, SDL_GAMEPAD_BUTTON_SOUTH, "Enter"}, | ||
| 739 | {VK_BACK, SDL_GAMEPAD_BUTTON_EAST, "Backspace"}, | ||
| 740 | {'X', SDL_GAMEPAD_BUTTON_WEST, "X"}, | ||
| 741 | {'Y', SDL_GAMEPAD_BUTTON_NORTH, "Y"}, | ||
| 742 | {'Q', SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, "Q"}, | ||
| 743 | {'E', SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, "E"}, | ||
| 744 | {VK_ESCAPE, SDL_GAMEPAD_BUTTON_START, "Esc"}, | ||
| 745 | {VK_TAB, SDL_GAMEPAD_BUTTON_BACK, "Tab"}, | ||
| 746 | }; | ||
| 747 | |||
| 748 | bool async_key_down(int vk) { return (GetAsyncKeyState(vk) & 0x8000) != 0; } | ||
| 749 | |||
| 750 | bool foreground_is_ours() { | ||
| 751 | const HWND w = GetForegroundWindow(); | ||
| 752 | DWORD pid = 0; | ||
| 753 | return w && GetWindowThreadProcessId(w, &pid) && pid == GetCurrentProcessId(); | ||
| 754 | } | ||
| 755 | |||
| 756 | std::atomic<rt::gameinput::KeyDownFn> g_key_down{async_key_down}; | ||
| 757 | std::atomic<rt::gameinput::HasFocusFn> g_has_focus{foreground_is_ours}; | ||
| 758 | |||
| 759 | struct KeyboardPad { | ||
| 760 | SDL_JoystickID id = 0; | ||
| 761 | SDL_Joystick* js = nullptr; | ||
| 762 | bool failed = false; // attaching failed once; not retried every 2 ms | ||
| 763 | }; | ||
| 764 | KeyboardPad g_keyboard; | ||
| 765 | |||
| 766 | void detach_keyboard_pad() { | ||
| 767 | if (g_keyboard.js) SDL_CloseJoystick(g_keyboard.js); | ||
| 768 | if (g_keyboard.id) SDL_DetachVirtualJoystick(g_keyboard.id); | ||
| 769 | if (g_keyboard.id) std::fprintf(stderr, "[rt] gameinput: keyboard pad removed\n"); | ||
| 770 | g_keyboard = {}; | ||
| 771 | } | ||
| 772 | |||
| 773 | bool attach_keyboard_pad() { | ||
| 774 | SDL_VirtualJoystickDesc d; | ||
| 775 | SDL_INIT_INTERFACE(&d); | ||
| 776 | d.type = SDL_JOYSTICK_TYPE_GAMEPAD; | ||
| 777 | d.naxes = SDL_GAMEPAD_AXIS_COUNT; | ||
| 778 | d.nbuttons = SDL_GAMEPAD_BUTTON_COUNT; | ||
| 779 | d.button_mask = (1u << SDL_GAMEPAD_BUTTON_COUNT) - 1; | ||
| 780 | d.axis_mask = (1u << SDL_GAMEPAD_AXIS_COUNT) - 1; | ||
| 781 | d.name = "Keyboard pad (Leonida)"; | ||
| 782 | g_keyboard.id = SDL_AttachVirtualJoystick(&d); | ||
| 783 | g_keyboard.js = g_keyboard.id ? SDL_OpenJoystick(g_keyboard.id) : nullptr; | ||
| 784 | if (!g_keyboard.js) { | ||
| 785 | std::fprintf(stderr, "[rt] gameinput: cannot attach the keyboard pad: %s\n", SDL_GetError()); | ||
| 786 | detach_keyboard_pad(); | ||
| 787 | g_keyboard.failed = true; | ||
| 788 | return false; | ||
| 789 | } | ||
| 790 | // Triggers at rest: SDL maps a full-range virtual axis to the trigger, so -32768 is released, 0 half pulled. | ||
| 791 | SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, -32768); | ||
| 792 | SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, -32768); | ||
| 793 | std::fprintf(stderr, "[rt] gameinput: keyboard pad attached (arrows d-pad, Enter A, Backspace B, X X, Y Y, " | ||
| 794 | "Q/E shoulders, Esc Menu, Tab View)\n"); | ||
| 795 | return true; | ||
| 796 | } | ||
| 797 | |||
| 798 | // Before SDL_UpdateGamepads, so the buttons set here are in this poll's reading. | ||
| 799 | void update_keyboard_pad() { | ||
| 800 | rt::gameinput::KeyboardPadMode mode; | ||
| 801 | { | ||
| 802 | std::lock_guard lock(g().mu); | ||
| 803 | mode = g().config.keyboard_pad; | ||
| 804 | } | ||
| 805 | if (mode == rt::gameinput::KeyboardPadMode::Off) { | ||
| 806 | if (g_keyboard.id) detach_keyboard_pad(); | ||
| 807 | g_keyboard.failed = false; | ||
| 808 | return; | ||
| 809 | } | ||
| 810 | if (!g_keyboard.js && (g_keyboard.failed || !attach_keyboard_pad())) return; | ||
| 811 | |||
| 812 | bool down[SDL_GAMEPAD_BUTTON_COUNT] = {}; | ||
| 813 | // Out of focus every button is released, so a key held while switching windows does not stay pressed. | ||
| 814 | if (mode == rt::gameinput::KeyboardPadMode::Always || g_has_focus.load()()) { | ||
| 815 | const rt::gameinput::KeyDownFn key_down = g_key_down.load(); | ||
| 816 | for (const auto& b : kKeyBindings) down[b.sdl_button] |= key_down(b.virtual_key); | ||
| 817 | } | ||
| 818 | for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); | ||
| 819 | } | ||
| 820 | |||
| 719 | bool init_sdl() { | 821 | bool init_sdl() { |
| 720 | State& s = g(); | 822 | State& s = g(); |
| 721 | if (s.sdl_ready) return true; | 823 | if (s.sdl_ready) return true; |
| @@ -745,6 +847,7 @@ void poll_once() { | |||
| 745 | State& s = g(); | 847 | State& s = g(); |
| 746 | std::lock_guard pl(s.poll_mu); | 848 | std::lock_guard pl(s.poll_mu); |
| 747 | if (!init_sdl()) return; | 849 | if (!init_sdl()) return; |
| 850 | update_keyboard_pad(); | ||
| 748 | SDL_UpdateGamepads(); | 851 | SDL_UpdateGamepads(); |
| 749 | const uint64_t now = now_us(); | 852 | const uint64_t now = now_us(); |
| 750 | 853 | ||
| @@ -1131,6 +1234,16 @@ bool output_state(int i, OutputState* out) { | |||
| 1131 | return true; | 1234 | return true; |
| 1132 | } | 1235 | } |
| 1133 | 1236 | ||
| 1237 | void set_keyboard_source(KeyDownFn key_down, HasFocusFn has_focus) { | ||
| 1238 | g_key_down = key_down ? key_down : async_key_down; | ||
| 1239 | g_has_focus = has_focus ? has_focus : foreground_is_ours; | ||
| 1240 | } | ||
| 1241 | |||
| 1242 | const KeyBinding* keyboard_bindings(int* count) { | ||
| 1243 | if (count) *count = static_cast<int>(std::size(kKeyBindings)); | ||
| 1244 | return kKeyBindings; | ||
| 1245 | } | ||
| 1246 | |||
| 1134 | } // namespace rt::gameinput | 1247 | } // namespace rt::gameinput |
| 1135 | 1248 | ||
| 1136 | SHIM_TABLE(gameinput, | 1249 | SHIM_TABLE(gameinput, |
diff --git a/runtime/shims/gameinput.h b/runtime/shims/gameinput.h index 0b8ed817e..aef8ae392 100644 --- a/runtime/shims/gameinput.h +++ b/runtime/shims/gameinput.h | |||
| @@ -8,6 +8,10 @@ namespace rt::gameinput { | |||
| 8 | // How the impulse-trigger motors (GameInputRumbleParams::leftTrigger/rightTrigger) reach a DualSense. | 8 | // How the impulse-trigger motors (GameInputRumbleParams::leftTrigger/rightTrigger) reach a DualSense. |
| 9 | enum class DsTriggerMode { Off, Rumble, Resistance }; | 9 | enum class DsTriggerMode { Off, Rumble, Resistance }; |
| 10 | 10 | ||
| 11 | // The keyboard pad: one extra virtual gamepad driven by the keyboard. Focused reads keys only while a window of this | ||
| 12 | // process is in the foreground; Always reads them whatever has focus (console tools such as gameinput_probe). | ||
| 13 | enum class KeyboardPadMode { Off, Focused, Always }; | ||
| 14 | |||
| 11 | struct Config { | 15 | struct Config { |
| 12 | DsTriggerMode ds_triggers = DsTriggerMode::Rumble; // LEONIDA_DS_TRIGGERS=off|rumble|resistance | 16 | DsTriggerMode ds_triggers = DsTriggerMode::Rumble; // LEONIDA_DS_TRIGGERS=off|rumble|resistance |
| 13 | float ds_trigger_gain = 1.0f; // LEONIDA_DS_TRIGGER_GAIN, 0..4, scales the effect strength | 17 | float ds_trigger_gain = 1.0f; // LEONIDA_DS_TRIGGER_GAIN, 0..4, scales the effect strength |
| @@ -16,6 +20,7 @@ struct Config { | |||
| 16 | uint8_t lightbar[3] = {0, 0, 0}; // player colour | 20 | uint8_t lightbar[3] = {0, 0, 0}; // player colour |
| 17 | bool rumble = true; // LEONIDA_RUMBLE=0 turns all rumble off | 21 | bool rumble = true; // LEONIDA_RUMBLE=0 turns all rumble off |
| 18 | bool background_thread = true; // LEONIDA_GAMEINPUT_THREAD=0: poll only from poll() | 22 | bool background_thread = true; // LEONIDA_GAMEINPUT_THREAD=0: poll only from poll() |
| 23 | KeyboardPadMode keyboard_pad = KeyboardPadMode::Off; // LEONIDA_KEYBOARD_PAD=1|focus|always|off | ||
| 19 | }; | 24 | }; |
| 20 | 25 | ||
| 21 | // The configuration in use. Read from the environment on the first GameInputCreate. | 26 | // The configuration in use. Read from the environment on the first GameInputCreate. |
| @@ -42,4 +47,19 @@ struct OutputState { | |||
| 42 | }; | 47 | }; |
| 43 | bool output_state(int i, OutputState* out); | 48 | bool output_state(int i, OutputState* out); |
| 44 | 49 | ||
| 50 | // Where the keyboard pad reads keys from. Test seam: key_down(virtual_key) replaces GetAsyncKeyState, has_focus | ||
| 51 | // replaces the foreground-window check of KeyboardPadMode::Focused. nullptr restores the default for either. | ||
| 52 | using KeyDownFn = bool (*)(int virtual_key); | ||
| 53 | using HasFocusFn = bool (*)(); | ||
| 54 | void set_keyboard_source(KeyDownFn key_down, HasFocusFn has_focus); | ||
| 55 | |||
| 56 | // One row of the keyboard pad's key map: a Windows virtual-key code and the SDL gamepad button it presses. | ||
| 57 | struct KeyBinding { | ||
| 58 | int virtual_key; | ||
| 59 | int sdl_button; // SDL_GamepadButton | ||
| 60 | const char* key_name; | ||
| 61 | }; | ||
| 62 | // The fixed key map (docs/gameinput.md lists it). `count` receives the number of rows. | ||
| 63 | const KeyBinding* keyboard_bindings(int* count); | ||
| 64 | |||
| 45 | } // namespace rt::gameinput | 65 | } // namespace rt::gameinput |
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); |