upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/gameinput.cpp
diff options
context:
space:
mode:
authorCozma <65982369+Cozmaaa@users.noreply.github.com>2026-11-22 06:05:38 +0200
committerCozma <65982369+Cozmaaa@users.noreply.github.com>2026-11-22 06:05:38 +0200
commit5097ea041aef13bbb6b11c7f6f92dd2ee515bc53 (patch)
treec77a10ab1d8b67175ecdf5693b0d74c70b0953d9 /runtime/shims/gameinput.cpp
parent4a36bb19979b611f525d027f92e0fefad1626546 (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/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp113
1 files changed, 113 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
733constexpr 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
748bool async_key_down(int vk) { return (GetAsyncKeyState(vk) & 0x8000) != 0; }
749
750bool foreground_is_ours() {
751 const HWND w = GetForegroundWindow();
752 DWORD pid = 0;
753 return w && GetWindowThreadProcessId(w, &pid) && pid == GetCurrentProcessId();
754}
755
756std::atomic<rt::gameinput::KeyDownFn> g_key_down{async_key_down};
757std::atomic<rt::gameinput::HasFocusFn> g_has_focus{foreground_is_ours};
758
759struct KeyboardPad {
760 SDL_JoystickID id = 0;
761 SDL_Joystick* js = nullptr;
762 bool failed = false; // attaching failed once; not retried every 2 ms
763};
764KeyboardPad g_keyboard;
765
766void 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
773bool 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.
799void 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
719bool init_sdl() { 821bool 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
1237void 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
1242const 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
1136SHIM_TABLE(gameinput, 1249SHIM_TABLE(gameinput,