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:
authoralexandersato0830 <66799221+alexandersato0830@users.noreply.github.com>2026-11-25 13:50:42 -0600
committeralexandersato0830 <66799221+alexandersato0830@users.noreply.github.com>2026-11-25 13:51:01 -0600
commit361c3abe61c8e76c281688ce351bbfc9d74d6928 (patch)
treec497f356fd49785755778bcda0977148ccc7a957 /runtime/shims/gameinput.cpp
parent0afcf9b5980e040192644bbd78fa054a635f5237 (diff)
Reset mouse delta baseline when the cursor source changes
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 47e9aa941..957965df3 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -1683,10 +1683,10 @@ bool async_key_down(int vk) { return (GetAsyncKeyState(vk) & 0x8000) != 0; }
1683// 1683//
1684// The cursor stops at the edge of the screen, so cursor deltas stop too. Raw input reports what the mouse itself 1684// The cursor stops at the edge of the screen, so cursor deltas stop too. Raw input reports what the mouse itself
1685// moved, with no edge and no pointer acceleration. A message-only window (never shown) on its own thread receives 1685// moved, with no edge and no pointer acceleration. A message-only window (never shown) on its own thread receives
1686// WM_INPUT and adds each movement to a running total; system_cursor_pos then returns that total as an edgeless 1686// WM_INPUT and adds each movement to a running total; raw_cursor_pos then returns that total as an edgeless
1687// "cursor", and update_keyboard_pad takes the difference between polls exactly as it did with the real cursor. 1687// "cursor", and update_keyboard_pad takes the difference between polls exactly as it did with the real cursor.
1688std::atomic<long long> g_raw_x{0}, g_raw_y{0}; // total raw movement since the thread started, mouse counts 1688std::atomic<long long> g_raw_x{0}, g_raw_y{0}; // total raw movement since the thread started, mouse counts
1689std::atomic<bool> g_raw_ready{false}; // registration worked; system_cursor_pos uses the totals 1689std::atomic<bool> g_raw_ready{false}; // registration worked; the pad can use the totals
1690std::atomic<bool> g_raw_started{false}; // the thread is started once per process 1690std::atomic<bool> g_raw_started{false}; // the thread is started once per process
1691 1691
1692LRESULT CALLBACK raw_mouse_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { 1692LRESULT CALLBACK raw_mouse_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
@@ -1743,12 +1743,13 @@ void start_raw_mouse() {
1743 if (!g_raw_started.exchange(true)) std::thread(raw_mouse_thread).detach(); 1743 if (!g_raw_started.exchange(true)) std::thread(raw_mouse_thread).detach();
1744} 1744}
1745 1745
1746bool raw_cursor_pos(int* x, int* y) {
1747 *x = static_cast<int>(g_raw_x.load());
1748 *y = static_cast<int>(g_raw_y.load());
1749 return true;
1750}
1751
1746bool system_cursor_pos(int* x, int* y) { 1752bool system_cursor_pos(int* x, int* y) {
1747 if (g_raw_ready) { // the edgeless total; it wraps only after ±2^31 counts, and the poll uses differences
1748 *x = static_cast<int>(g_raw_x.load());
1749 *y = static_cast<int>(g_raw_y.load());
1750 return true;
1751 }
1752 POINT p; 1753 POINT p;
1753 if (!GetCursorPos(&p)) return false; 1754 if (!GetCursorPos(&p)) return false;
1754 *x = p.x; 1755 *x = p.x;
@@ -1772,6 +1773,7 @@ struct KeyboardPad {
1772 bool failed = false; // attaching failed once; not retried every 2 ms 1773 bool failed = false; // attaching failed once; not retried every 2 ms
1773 int right_x = 0, right_y = 0; // the mouse's right stick, SDL axis units (+Y down) 1774 int right_x = 0, right_y = 0; // the mouse's right stick, SDL axis units (+Y down)
1774 int cursor_x = 0, cursor_y = 0; // the cursor at the last poll 1775 int cursor_x = 0, cursor_y = 0; // the cursor at the last poll
1776 rt::gameinput::CursorPosFn cursor_source = nullptr;
1775 bool have_cursor = false; // cursor_x/y are valid; false after focus loss, so the first poll moves nothing 1777 bool have_cursor = false; // cursor_x/y are valid; false after focus loss, so the first poll moves nothing
1776}; 1778};
1777KeyboardPad g_keyboard; 1779KeyboardPad g_keyboard;
@@ -1856,6 +1858,7 @@ void update_keyboard_pad() {
1856 bool down[SDL_GAMEPAD_BUTTON_COUNT] = {}; 1858 bool down[SDL_GAMEPAD_BUTTON_COUNT] = {};
1857 int lx = 0, ly = 0; 1859 int lx = 0, ly = 0;
1858 bool aim = false, fire = false; 1860 bool aim = false, fire = false;
1861 bool raw_mouse = false;
1859 // Focus gates physical keyboard and mouse input. Read the cursor afresh on return to avoid a jump. 1862 // Focus gates physical keyboard and mouse input. Read the cursor afresh on return to avoid a jump.
1860 // Scripted presses still run while the window is unfocused. 1863 // Scripted presses still run while the window is unfocused.
1861 const bool active = mode == rt::gameinput::KeyboardPadMode::Always || g_has_focus.load()(); 1864 const bool active = mode == rt::gameinput::KeyboardPadMode::Always || g_has_focus.load()();
@@ -1868,7 +1871,15 @@ void update_keyboard_pad() {
1868 fire = key_down(VK_LBUTTON); 1871 fire = key_down(VK_LBUTTON);
1869 1872
1870 int x = 0, y = 0; 1873 int x = 0, y = 0;
1871 const bool have = g_cursor_pos.load()(&x, &y); 1874 auto cursor_source = g_cursor_pos.load();
1875 raw_mouse = cursor_source == system_cursor_pos && g_raw_ready;
1876 if (raw_mouse) cursor_source = raw_cursor_pos;
1877 // The first sample from a new coordinate source establishes its baseline.
1878 if (cursor_source != g_keyboard.cursor_source) {
1879 g_keyboard.cursor_source = cursor_source;
1880 g_keyboard.have_cursor = false;
1881 }
1882 const bool have = cursor_source(&x, &y);
1872 const bool moved = have && g_keyboard.have_cursor; 1883 const bool moved = have && g_keyboard.have_cursor;
1873 auto step = [sens](int v, int pixels) { 1884 auto step = [sens](int v, int pixels) {
1874 const long long n = static_cast<long long>(v) * kMouseDecayNum / kMouseDecayDen + 1885 const long long n = static_cast<long long>(v) * kMouseDecayNum / kMouseDecayDen +
@@ -1883,7 +1894,7 @@ void update_keyboard_pad() {
1883 g_keyboard.right_x = g_keyboard.right_y = 0; 1894 g_keyboard.right_x = g_keyboard.right_y = 0;
1884 g_keyboard.have_cursor = false; 1895 g_keyboard.have_cursor = false;
1885 } 1896 }
1886 clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours()); 1897 clip_cursor(raw_mouse && foreground_is_ours());
1887 for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); 1898 for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]);
1888 // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard 1899 // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard
1889 // pad, in seconds since the pad's first update, each held 300 ms. For unattended boots: the title's first prompt 1900 // pad, in seconds since the pad's first update, each held 300 ms. For unattended boots: the title's first prompt