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:
authorMoonEyes <ducoinbenjamin@yahoo.fr>2026-11-28 19:12:09 +0100
committerGitHub <noreply@github.com>2026-11-28 19:12:09 +0100
commit423521c111b416ea1cb9e3341105348e08c6c2b3 (patch)
tree62de3282dc2a8aec8c06ff5ce0d32c31601ab52a /runtime/shims/gameinput.cpp
parentdc93dd3c188ded377b3b96f8753f6eb23a058839 (diff)
parent361c3abe61c8e76c281688ce351bbfc9d74d6928 (diff)
Merge pull request #2086 from alexandersato0830/codex/gameinput-cursor-source
Reset the mouse baseline when the cursor source changes (RT-KEYBOARD-PAD-CURSOR-SOURCE)
Diffstat (limited to 'runtime/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp29
1 files changed, 20 insertions, 9 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index 9cfe1231b..0d6d89b5c 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -1698,10 +1698,10 @@ bool async_key_down(int vk) { return (GetAsyncKeyState(vk) & 0x8000) != 0; }
1698// 1698//
1699// The cursor stops at the edge of the screen, so cursor deltas stop too. Raw input reports what the mouse itself 1699// The cursor stops at the edge of the screen, so cursor deltas stop too. Raw input reports what the mouse itself
1700// moved, with no edge and no pointer acceleration. A message-only window (never shown) on its own thread receives 1700// moved, with no edge and no pointer acceleration. A message-only window (never shown) on its own thread receives
1701// WM_INPUT and adds each movement to a running total; system_cursor_pos then returns that total as an edgeless 1701// WM_INPUT and adds each movement to a running total; raw_cursor_pos then returns that total as an edgeless
1702// "cursor", and update_keyboard_pad takes the difference between polls exactly as it did with the real cursor. 1702// "cursor", and update_keyboard_pad takes the difference between polls exactly as it did with the real cursor.
1703std::atomic<long long> g_raw_x{0}, g_raw_y{0}; // total raw movement since the thread started, mouse counts 1703std::atomic<long long> g_raw_x{0}, g_raw_y{0}; // total raw movement since the thread started, mouse counts
1704std::atomic<bool> g_raw_ready{false}; // registration worked; system_cursor_pos uses the totals 1704std::atomic<bool> g_raw_ready{false}; // registration worked; the pad can use the totals
1705std::atomic<bool> g_raw_started{false}; // the thread is started once per process 1705std::atomic<bool> g_raw_started{false}; // the thread is started once per process
1706 1706
1707LRESULT CALLBACK raw_mouse_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { 1707LRESULT CALLBACK raw_mouse_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
@@ -1758,12 +1758,13 @@ void start_raw_mouse() {
1758 if (!g_raw_started.exchange(true)) std::thread(raw_mouse_thread).detach(); 1758 if (!g_raw_started.exchange(true)) std::thread(raw_mouse_thread).detach();
1759} 1759}
1760 1760
1761bool raw_cursor_pos(int* x, int* y) {
1762 *x = static_cast<int>(g_raw_x.load());
1763 *y = static_cast<int>(g_raw_y.load());
1764 return true;
1765}
1766
1761bool system_cursor_pos(int* x, int* y) { 1767bool system_cursor_pos(int* x, int* y) {
1762 if (g_raw_ready) { // the edgeless total; it wraps only after ±2^31 counts, and the poll uses differences
1763 *x = static_cast<int>(g_raw_x.load());
1764 *y = static_cast<int>(g_raw_y.load());
1765 return true;
1766 }
1767 POINT p; 1768 POINT p;
1768 if (!GetCursorPos(&p)) return false; 1769 if (!GetCursorPos(&p)) return false;
1769 *x = p.x; 1770 *x = p.x;
@@ -1784,6 +1785,7 @@ std::atomic<rt::gameinput::CursorPosFn> g_cursor_pos{system_cursor_pos};
1784struct KeyboardPad : rt::gameinput::detail::KeyboardPadConnection<SDL_JoystickID, SDL_Joystick*> { 1785struct KeyboardPad : rt::gameinput::detail::KeyboardPadConnection<SDL_JoystickID, SDL_Joystick*> {
1785 int right_x = 0, right_y = 0; // the mouse's right stick, SDL axis units (+Y down) 1786 int right_x = 0, right_y = 0; // the mouse's right stick, SDL axis units (+Y down)
1786 int cursor_x = 0, cursor_y = 0; // the cursor at the last poll 1787 int cursor_x = 0, cursor_y = 0; // the cursor at the last poll
1788 rt::gameinput::CursorPosFn cursor_source = nullptr;
1787 bool have_cursor = false; // cursor_x/y are valid; false after focus loss, so the first poll moves nothing 1789 bool have_cursor = false; // cursor_x/y are valid; false after focus loss, so the first poll moves nothing
1788}; 1790};
1789KeyboardPad g_keyboard; 1791KeyboardPad g_keyboard;
@@ -1884,6 +1886,7 @@ void update_keyboard_pad() {
1884 bool down[SDL_GAMEPAD_BUTTON_COUNT] = {}; 1886 bool down[SDL_GAMEPAD_BUTTON_COUNT] = {};
1885 int lx = 0, ly = 0; 1887 int lx = 0, ly = 0;
1886 bool aim = false, fire = false; 1888 bool aim = false, fire = false;
1889 bool raw_mouse = false;
1887 // Focus gates physical keyboard and mouse input. Read the cursor afresh on return to avoid a jump. 1890 // Focus gates physical keyboard and mouse input. Read the cursor afresh on return to avoid a jump.
1888 // Scripted presses still run while the window is unfocused. 1891 // Scripted presses still run while the window is unfocused.
1889 const bool active = mode == rt::gameinput::KeyboardPadMode::Always || g_has_focus.load()(); 1892 const bool active = mode == rt::gameinput::KeyboardPadMode::Always || g_has_focus.load()();
@@ -1896,7 +1899,15 @@ void update_keyboard_pad() {
1896 fire = key_down(VK_LBUTTON); 1899 fire = key_down(VK_LBUTTON);
1897 1900
1898 int x = 0, y = 0; 1901 int x = 0, y = 0;
1899 const bool have = g_cursor_pos.load()(&x, &y); 1902 auto cursor_source = g_cursor_pos.load();
1903 raw_mouse = cursor_source == system_cursor_pos && g_raw_ready;
1904 if (raw_mouse) cursor_source = raw_cursor_pos;
1905 // The first sample from a new coordinate source establishes its baseline.
1906 if (cursor_source != g_keyboard.cursor_source) {
1907 g_keyboard.cursor_source = cursor_source;
1908 g_keyboard.have_cursor = false;
1909 }
1910 const bool have = cursor_source(&x, &y);
1900 const bool moved = have && g_keyboard.have_cursor; 1911 const bool moved = have && g_keyboard.have_cursor;
1901 // Raw input exposes the low bits of a running total. Keep subtraction modular across a wrap. 1912 // Raw input exposes the low bits of a running total. Keep subtraction modular across a wrap.
1902 auto delta = [](int current, int previous) { 1913 auto delta = [](int current, int previous) {
@@ -1915,7 +1926,7 @@ void update_keyboard_pad() {
1915 g_keyboard.right_x = g_keyboard.right_y = 0; 1926 g_keyboard.right_x = g_keyboard.right_y = 0;
1916 g_keyboard.have_cursor = false; 1927 g_keyboard.have_cursor = false;
1917 } 1928 }
1918 clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours()); 1929 clip_cursor(raw_mouse && foreground_is_ours());
1919 for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); 1930 for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]);
1920 // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard 1931 // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard
1921 // pad, in seconds since the pad's first update, each held 300 ms or its own button@seconds:hold, and then until the 1932 // pad, in seconds since the pad's first update, each held 300 ms or its own button@seconds:hold, and then until the