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:
authorquackottu <224479491+quackottu@users.noreply.github.com>2026-11-22 12:49:29 +0530
committerquackottu <224479491+quackottu@users.noreply.github.com>2026-11-22 12:49:29 +0530
commit851eb563b62c821ac2f7ee9aa375b21cb58b60f3 (patch)
treed66d4a9e0f52887d8684787c124b7a318076355f /runtime/shims/gameinput.cpp
parentc63773d51235d72250d48699f29830ac9ddafd60 (diff)
gameinput: keyboard pad moves and aims: WASD left stick, mouse right stick, mouse buttons LT/RT
The keyboard pad only pressed buttons, enough for menus. WASD now drives the left stick (opposite keys cancel, diagonals on the round gate), cursor movement the right stick (decaying back to center), and the right/left mouse buttons the left/right triggers. Everything is released on focus loss. A set_cursor_source seam sits beside set_keyboard_source for tests.
Diffstat (limited to 'runtime/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp70
1 files changed, 64 insertions, 6 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index ecb2702ff..2b55022f4 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -12,7 +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// - the keyboard pad (LEONIDA_KEYBOARD_PAD): one SDL virtual gamepad driven by the keyboard and mouse
16// 16//
17// 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
18// 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
@@ -779,6 +779,7 @@ void refresh_battery(Device* d, uint64_t now) {
779// code presses buttons on each poll. It then goes through open_pad and read_pad like any controller, so it gets a 779// code presses buttons on each poll. It then goes through open_pad and read_pad like any controller, so it gets a
780// device object, a player slot, readings, callbacks and UI navigation the same way. It has no motors, so rumble sent 780// device object, a player slot, readings, callbacks and UI navigation the same way. It has no motors, so rumble sent
781// to it is dropped. Keys are read with GetAsyncKeyState, which needs no window or message loop and works under Wine. 781// to it is dropped. Keys are read with GetAsyncKeyState, which needs no window or message loop and works under Wine.
782// WASD is the left stick, the mouse the right stick, and the right/left mouse buttons the left/right triggers.
782 783
783constexpr rt::gameinput::KeyBinding kKeyBindings[] = { 784constexpr rt::gameinput::KeyBinding kKeyBindings[] = {
784 {VK_UP, SDL_GAMEPAD_BUTTON_DPAD_UP, "Up"}, 785 {VK_UP, SDL_GAMEPAD_BUTTON_DPAD_UP, "Up"},
@@ -795,8 +796,30 @@ constexpr rt::gameinput::KeyBinding kKeyBindings[] = {
795 {VK_TAB, SDL_GAMEPAD_BUTTON_BACK, "Tab"}, 796 {VK_TAB, SDL_GAMEPAD_BUTTON_BACK, "Tab"},
796}; 797};
797 798
799// WASD on the left stick: a full press is the edge of the stick; a diagonal sits on the round gate, as a real stick
800// reports it, so each axis is 1/sqrt(2) of full.
801constexpr int kStickFull = 32767;
802constexpr int kStickDiagonal = 23170; // 32767 / sqrt(2)
803
804// The mouse on the right stick, in SDL axis units per poll (2 ms with the background thread): each poll the stick
805// keeps kMouseDecayNum/kMouseDecayDen of its value and adds kMouseAxisPerPixel for every pixel the cursor moved, then
806// is clamped to full. Steady movement settles at 8 x 1024 units per pixel per poll, so 4 pixels per poll (2000
807// pixels a second) is full deflection; a stopped mouse falls back from full to exactly 0 in 67 polls (134 ms).
808// Integer math rounds toward zero, so the stick always reaches center.
809constexpr int kMouseAxisPerPixel = 1024;
810constexpr int kMouseDecayNum = 7, kMouseDecayDen = 8;
811
798bool async_key_down(int vk) { return (GetAsyncKeyState(vk) & 0x8000) != 0; } 812bool async_key_down(int vk) { return (GetAsyncKeyState(vk) & 0x8000) != 0; }
799 813
814// shortcut: plain cursor deltas stop at the screen edge; capture or recenter the cursor if that matters in game.
815bool system_cursor_pos(int* x, int* y) {
816 POINT p;
817 if (!GetCursorPos(&p)) return false;
818 *x = p.x;
819 *y = p.y;
820 return true;
821}
822
800bool foreground_is_ours() { 823bool foreground_is_ours() {
801 const HWND w = GetForegroundWindow(); 824 const HWND w = GetForegroundWindow();
802 DWORD pid = 0; 825 DWORD pid = 0;
@@ -805,11 +828,15 @@ bool foreground_is_ours() {
805 828
806std::atomic<rt::gameinput::KeyDownFn> g_key_down{async_key_down}; 829std::atomic<rt::gameinput::KeyDownFn> g_key_down{async_key_down};
807std::atomic<rt::gameinput::HasFocusFn> g_has_focus{foreground_is_ours}; 830std::atomic<rt::gameinput::HasFocusFn> g_has_focus{foreground_is_ours};
831std::atomic<rt::gameinput::CursorPosFn> g_cursor_pos{system_cursor_pos};
808 832
809struct KeyboardPad { 833struct KeyboardPad {
810 SDL_JoystickID id = 0; 834 SDL_JoystickID id = 0;
811 SDL_Joystick* js = nullptr; 835 SDL_Joystick* js = nullptr;
812 bool failed = false; // attaching failed once; not retried every 2 ms 836 bool failed = false; // attaching failed once; not retried every 2 ms
837 int right_x = 0, right_y = 0; // the mouse's right stick, SDL axis units (+Y down)
838 int cursor_x = 0, cursor_y = 0; // the cursor at the last poll
839 bool have_cursor = false; // cursor_x/y are valid; false after focus loss, so the first poll moves nothing
813}; 840};
814KeyboardPad g_keyboard; 841KeyboardPad g_keyboard;
815 842
@@ -837,11 +864,9 @@ bool attach_keyboard_pad() {
837 g_keyboard.failed = true; 864 g_keyboard.failed = true;
838 return false; 865 return false;
839 } 866 }
840 // Triggers at rest: SDL maps a full-range virtual axis to the trigger, so -32768 is released, 0 half pulled.
841 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, -32768);
842 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, -32768);
843 std::fprintf(stderr, "[rt] gameinput: keyboard pad attached (arrows d-pad, Enter A, Backspace B, X X, Y Y, " 867 std::fprintf(stderr, "[rt] gameinput: keyboard pad attached (arrows d-pad, Enter A, Backspace B, X X, Y Y, "
844 "Q/E shoulders, Esc Menu, Tab View)\n"); 868 "Q/E shoulders, Esc Menu, Tab View, WASD left stick, mouse right stick, right/left "
869 "button LT/RT)\n");
845 return true; 870 return true;
846} 871}
847 872
@@ -860,12 +885,43 @@ void update_keyboard_pad() {
860 if (!g_keyboard.js && (g_keyboard.failed || !attach_keyboard_pad())) return; 885 if (!g_keyboard.js && (g_keyboard.failed || !attach_keyboard_pad())) return;
861 886
862 bool down[SDL_GAMEPAD_BUTTON_COUNT] = {}; 887 bool down[SDL_GAMEPAD_BUTTON_COUNT] = {};
863 // Out of focus every button is released, so a key held while switching windows does not stay pressed. 888 int lx = 0, ly = 0;
889 bool aim = false, fire = false;
890 // Out of focus every button, stick and trigger is released, so a key held while switching windows does not stay
891 // pressed, and the cursor is read afresh on return, so moving it in another window does not swing the stick.
864 if (mode == rt::gameinput::KeyboardPadMode::Always || g_has_focus.load()()) { 892 if (mode == rt::gameinput::KeyboardPadMode::Always || g_has_focus.load()()) {
865 const rt::gameinput::KeyDownFn key_down = g_key_down.load(); 893 const rt::gameinput::KeyDownFn key_down = g_key_down.load();
866 for (const auto& b : kKeyBindings) down[b.sdl_button] |= key_down(b.virtual_key); 894 for (const auto& b : kKeyBindings) down[b.sdl_button] |= key_down(b.virtual_key);
895 lx = key_down('D') - key_down('A'); // opposite keys cancel
896 ly = key_down('S') - key_down('W'); // SDL's +Y is down
897 aim = key_down(VK_RBUTTON);
898 fire = key_down(VK_LBUTTON);
899
900 int x = 0, y = 0;
901 const bool have = g_cursor_pos.load()(&x, &y);
902 const bool moved = have && g_keyboard.have_cursor;
903 auto step = [](int v, int pixels) {
904 const long long n = static_cast<long long>(v) * kMouseDecayNum / kMouseDecayDen +
905 static_cast<long long>(pixels) * kMouseAxisPerPixel;
906 return static_cast<int>(std::clamp<long long>(n, -kStickFull, kStickFull));
907 };
908 g_keyboard.right_x = step(g_keyboard.right_x, moved ? x - g_keyboard.cursor_x : 0);
909 g_keyboard.right_y = step(g_keyboard.right_y, moved ? y - g_keyboard.cursor_y : 0);
910 if (have) g_keyboard.cursor_x = x, g_keyboard.cursor_y = y;
911 g_keyboard.have_cursor = have;
912 } else {
913 g_keyboard.right_x = g_keyboard.right_y = 0;
914 g_keyboard.have_cursor = false;
867 } 915 }
868 for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); 916 for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]);
917 const int reach = lx && ly ? kStickDiagonal : kStickFull;
918 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTX, static_cast<Sint16>(lx * reach));
919 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTY, static_cast<Sint16>(ly * reach));
920 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHTX, static_cast<Sint16>(g_keyboard.right_x));
921 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHTY, static_cast<Sint16>(g_keyboard.right_y));
922 // SDL maps a full-range virtual axis to the trigger, so -32768 is released, 0 half pulled, 32767 full.
923 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, aim ? 32767 : -32768);
924 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, fire ? 32767 : -32768);
869} 925}
870 926
871bool init_sdl() { 927bool init_sdl() {
@@ -1309,6 +1365,8 @@ void set_keyboard_source(KeyDownFn key_down, HasFocusFn has_focus) {
1309 g_has_focus = has_focus ? has_focus : foreground_is_ours; 1365 g_has_focus = has_focus ? has_focus : foreground_is_ours;
1310} 1366}
1311 1367
1368void set_cursor_source(CursorPosFn cursor_pos) { g_cursor_pos = cursor_pos ? cursor_pos : system_cursor_pos; }
1369
1312const KeyBinding* keyboard_bindings(int* count) { 1370const KeyBinding* keyboard_bindings(int* count) {
1313 if (count) *count = static_cast<int>(std::size(kKeyBindings)); 1371 if (count) *count = static_cast<int>(std::size(kKeyBindings));
1314 return kKeyBindings; 1372 return kKeyBindings;