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/shims/gameinput.cpp | |
| parent | b72fc5a4fec776ffa4b19d37c7bf8d62836e3df5 (diff) | |
| parent | 067d3e8b97189fcf272dc699fdbb871eae496660 (diff) | |
Merge keyboard-pad coverage with controller reading history
Diffstat (limited to 'runtime/shims/gameinput.cpp')
| -rw-r--r-- | runtime/shims/gameinput.cpp | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index ad347babc..1d4c35b32 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp | |||
| @@ -12,6 +12,7 @@ | |||
| 12 | // effect (SDL_SendGamepadEffect), as set by LEONIDA_DS_TRIGGERS. That is an approximation of the Xbox impulse | 12 | // effect (SDL_SendGamepadEffect), as set by LEONIDA_DS_TRIGGERS. That is an approximation of the Xbox impulse |
| 13 | // trigger signal, not the PS5 version's authored effects. | 13 | // trigger signal, not the PS5 version's authored effects. |
| 14 | // - DualSense/DualShock 4 lightbar colour (LEONIDA_DS_LIGHTBAR) and the player LEDs by connection order | 14 | // - DualSense/DualShock 4 lightbar colour (LEONIDA_DS_LIGHTBAR) and the player LEDs by connection order |
| 15 | // - the keyboard pad (LEONIDA_KEYBOARD_PAD): one SDL virtual gamepad whose buttons the keyboard presses, for menus | ||
| 15 | // | 16 | // |
| 16 | // One thread owns SDL: the background poll thread by default, or the caller of rt::gameinput::poll() when | 17 | // One thread owns SDL: the background poll thread by default, or the caller of rt::gameinput::poll() when |
| 17 | // LEONIDA_GAMEINPUT_THREAD=0. Title calls never touch SDL: they read the latest snapshot and leave rumble requests | 18 | // LEONIDA_GAMEINPUT_THREAD=0. Title calls never touch SDL: they read the latest snapshot and leave rumble requests |
| @@ -113,6 +114,12 @@ rt::gameinput::Config read_config() { | |||
| 113 | } | 114 | } |
| 114 | if (env("LEONIDA_RUMBLE") == "0") c.rumble = false; | 115 | if (env("LEONIDA_RUMBLE") == "0") c.rumble = false; |
| 115 | if (env("LEONIDA_GAMEINPUT_THREAD") == "0") c.background_thread = false; | 116 | if (env("LEONIDA_GAMEINPUT_THREAD") == "0") c.background_thread = false; |
| 117 | const std::string k = env("LEONIDA_KEYBOARD_PAD"); | ||
| 118 | if (k == "1" || k == "on" || k == "focus") c.keyboard_pad = rt::gameinput::KeyboardPadMode::Focused; | ||
| 119 | else if (k == "always") c.keyboard_pad = rt::gameinput::KeyboardPadMode::Always; | ||
| 120 | else if (!k.empty() && k != "0" && k != "off") | ||
| 121 | std::fprintf(stderr, "[rt] gameinput: LEONIDA_KEYBOARD_PAD=%s unknown (1, always or off), keyboard pad off\n", | ||
| 122 | k.c_str()); | ||
| 116 | return c; | 123 | return c; |
| 117 | } | 124 | } |
| 118 | 125 | ||
| @@ -741,6 +748,101 @@ void refresh_battery(Device* d, uint64_t now) { | |||
| 741 | d->battery = b; | 748 | d->battery = b; |
| 742 | } | 749 | } |
| 743 | 750 | ||
| 751 | // ---- keyboard pad (poll thread only) ---------------------------------------------------------------------------- | ||
| 752 | // | ||
| 753 | // The title reads gamepads, not the keyboard, so keys reach it as one more pad: an SDL virtual gamepad that this | ||
| 754 | // code presses buttons on each poll. It then goes through open_pad and read_pad like any controller, so it gets a | ||
| 755 | // device object, a player slot, readings, callbacks and UI navigation the same way. It has no motors, so rumble sent | ||
| 756 | // to it is dropped. Keys are read with GetAsyncKeyState, which needs no window or message loop and works under Wine. | ||
| 757 | |||
| 758 | constexpr rt::gameinput::KeyBinding kKeyBindings[] = { | ||
| 759 | {VK_UP, SDL_GAMEPAD_BUTTON_DPAD_UP, "Up"}, | ||
| 760 | {VK_DOWN, SDL_GAMEPAD_BUTTON_DPAD_DOWN, "Down"}, | ||
| 761 | {VK_LEFT, SDL_GAMEPAD_BUTTON_DPAD_LEFT, "Left"}, | ||
| 762 | {VK_RIGHT, SDL_GAMEPAD_BUTTON_DPAD_RIGHT, "Right"}, | ||
| 763 | {VK_RETURN, SDL_GAMEPAD_BUTTON_SOUTH, "Enter"}, | ||
| 764 | {VK_BACK, SDL_GAMEPAD_BUTTON_EAST, "Backspace"}, | ||
| 765 | {'X', SDL_GAMEPAD_BUTTON_WEST, "X"}, | ||
| 766 | {'Y', SDL_GAMEPAD_BUTTON_NORTH, "Y"}, | ||
| 767 | {'Q', SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, "Q"}, | ||
| 768 | {'E', SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, "E"}, | ||
| 769 | {VK_ESCAPE, SDL_GAMEPAD_BUTTON_START, "Esc"}, | ||
| 770 | {VK_TAB, SDL_GAMEPAD_BUTTON_BACK, "Tab"}, | ||
| 771 | }; | ||
| 772 | |||
| 773 | bool async_key_down(int vk) { return (GetAsyncKeyState(vk) & 0x8000) != 0; } | ||
| 774 | |||
| 775 | bool foreground_is_ours() { | ||
| 776 | const HWND w = GetForegroundWindow(); | ||
| 777 | DWORD pid = 0; | ||
| 778 | return w && GetWindowThreadProcessId(w, &pid) && pid == GetCurrentProcessId(); | ||
| 779 | } | ||
| 780 | |||
| 781 | std::atomic<rt::gameinput::KeyDownFn> g_key_down{async_key_down}; | ||
| 782 | std::atomic<rt::gameinput::HasFocusFn> g_has_focus{foreground_is_ours}; | ||
| 783 | |||
| 784 | struct KeyboardPad { | ||
| 785 | SDL_JoystickID id = 0; | ||
| 786 | SDL_Joystick* js = nullptr; | ||
| 787 | bool failed = false; // attaching failed once; not retried every 2 ms | ||
| 788 | }; | ||
| 789 | KeyboardPad g_keyboard; | ||
| 790 | |||
| 791 | void detach_keyboard_pad() { | ||
| 792 | if (g_keyboard.js) SDL_CloseJoystick(g_keyboard.js); | ||
| 793 | if (g_keyboard.id) SDL_DetachVirtualJoystick(g_keyboard.id); | ||
| 794 | if (g_keyboard.id) std::fprintf(stderr, "[rt] gameinput: keyboard pad removed\n"); | ||
| 795 | g_keyboard = {}; | ||
| 796 | } | ||
| 797 | |||
| 798 | bool attach_keyboard_pad() { | ||
| 799 | SDL_VirtualJoystickDesc d; | ||
| 800 | SDL_INIT_INTERFACE(&d); | ||
| 801 | d.type = SDL_JOYSTICK_TYPE_GAMEPAD; | ||
| 802 | d.naxes = SDL_GAMEPAD_AXIS_COUNT; | ||
| 803 | d.nbuttons = SDL_GAMEPAD_BUTTON_COUNT; | ||
| 804 | d.button_mask = (1u << SDL_GAMEPAD_BUTTON_COUNT) - 1; | ||
| 805 | d.axis_mask = (1u << SDL_GAMEPAD_AXIS_COUNT) - 1; | ||
| 806 | d.name = "Keyboard pad (Leonida)"; | ||
| 807 | g_keyboard.id = SDL_AttachVirtualJoystick(&d); | ||
| 808 | g_keyboard.js = g_keyboard.id ? SDL_OpenJoystick(g_keyboard.id) : nullptr; | ||
| 809 | if (!g_keyboard.js) { | ||
| 810 | std::fprintf(stderr, "[rt] gameinput: cannot attach the keyboard pad: %s\n", SDL_GetError()); | ||
| 811 | detach_keyboard_pad(); | ||
| 812 | g_keyboard.failed = true; | ||
| 813 | return false; | ||
| 814 | } | ||
| 815 | // Triggers at rest: SDL maps a full-range virtual axis to the trigger, so -32768 is released, 0 half pulled. | ||
| 816 | SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, -32768); | ||
| 817 | SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, -32768); | ||
| 818 | std::fprintf(stderr, "[rt] gameinput: keyboard pad attached (arrows d-pad, Enter A, Backspace B, X X, Y Y, " | ||
| 819 | "Q/E shoulders, Esc Menu, Tab View)\n"); | ||
| 820 | return true; | ||
| 821 | } | ||
| 822 | |||
| 823 | // Before SDL_UpdateGamepads, so the buttons set here are in this poll's reading. | ||
| 824 | void update_keyboard_pad() { | ||
| 825 | rt::gameinput::KeyboardPadMode mode; | ||
| 826 | { | ||
| 827 | std::lock_guard lock(g().mu); | ||
| 828 | mode = g().config.keyboard_pad; | ||
| 829 | } | ||
| 830 | if (mode == rt::gameinput::KeyboardPadMode::Off) { | ||
| 831 | if (g_keyboard.id) detach_keyboard_pad(); | ||
| 832 | g_keyboard.failed = false; | ||
| 833 | return; | ||
| 834 | } | ||
| 835 | if (!g_keyboard.js && (g_keyboard.failed || !attach_keyboard_pad())) return; | ||
| 836 | |||
| 837 | bool down[SDL_GAMEPAD_BUTTON_COUNT] = {}; | ||
| 838 | // Out of focus every button is released, so a key held while switching windows does not stay pressed. | ||
| 839 | if (mode == rt::gameinput::KeyboardPadMode::Always || g_has_focus.load()()) { | ||
| 840 | const rt::gameinput::KeyDownFn key_down = g_key_down.load(); | ||
| 841 | for (const auto& b : kKeyBindings) down[b.sdl_button] |= key_down(b.virtual_key); | ||
| 842 | } | ||
| 843 | for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); | ||
| 844 | } | ||
| 845 | |||
| 744 | bool init_sdl() { | 846 | bool init_sdl() { |
| 745 | State& s = g(); | 847 | State& s = g(); |
| 746 | if (s.sdl_ready) return true; | 848 | if (s.sdl_ready) return true; |
| @@ -770,6 +872,7 @@ void poll_once() { | |||
| 770 | State& s = g(); | 872 | State& s = g(); |
| 771 | std::lock_guard pl(s.poll_mu); | 873 | std::lock_guard pl(s.poll_mu); |
| 772 | if (!init_sdl()) return; | 874 | if (!init_sdl()) return; |
| 875 | update_keyboard_pad(); | ||
| 773 | SDL_UpdateGamepads(); | 876 | SDL_UpdateGamepads(); |
| 774 | const uint64_t now = now_us(); | 877 | const uint64_t now = now_us(); |
| 775 | 878 | ||
| @@ -1176,6 +1279,16 @@ bool output_state(int i, OutputState* out) { | |||
| 1176 | return true; | 1279 | return true; |
| 1177 | } | 1280 | } |
| 1178 | 1281 | ||
| 1282 | void set_keyboard_source(KeyDownFn key_down, HasFocusFn has_focus) { | ||
| 1283 | g_key_down = key_down ? key_down : async_key_down; | ||
| 1284 | g_has_focus = has_focus ? has_focus : foreground_is_ours; | ||
| 1285 | } | ||
| 1286 | |||
| 1287 | const KeyBinding* keyboard_bindings(int* count) { | ||
| 1288 | if (count) *count = static_cast<int>(std::size(kKeyBindings)); | ||
| 1289 | return kKeyBindings; | ||
| 1290 | } | ||
| 1291 | |||
| 1179 | } // namespace rt::gameinput | 1292 | } // namespace rt::gameinput |
| 1180 | 1293 | ||
| 1181 | SHIM_TABLE(gameinput, | 1294 | SHIM_TABLE(gameinput, |