diff options
| author | Дмитрий Корлодный <deusex1998@gmail.com> | 2026-11-28 13:59:13 +0000 |
|---|---|---|
| committer | Дмитрий Корлодный <deusex1998@gmail.com> | 2026-11-28 13:59:13 +0000 |
| commit | ab95ff8427caa30a5f87d85af097580d7e7d086f (patch) | |
| tree | d4625cfe634303abfa7f555c24c95f5274ba71db /runtime/shims/gameinput.cpp | |
| parent | 7ffcb8e260d3cedfb0077d924b0be189293c5a2c (diff) | |
| parent | 9c49bd5332327e689c5c957786c31b10efb3f32b (diff) | |
Merge upstream main into PR #1528
Diffstat (limited to 'runtime/shims/gameinput.cpp')
| -rw-r--r-- | runtime/shims/gameinput.cpp | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 238c98ba6..2ee7bf77d 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp | |||
| @@ -52,6 +52,7 @@ | |||
| 52 | #include <vector> | 52 | #include <vector> |
| 53 | 53 | ||
| 54 | #include "gameinput.h" | 54 | #include "gameinput.h" |
| 55 | #include "gameinput_clip.h" | ||
| 55 | #include "gameinput_abi.h" | 56 | #include "gameinput_abi.h" |
| 56 | #include "gameinput_script.h" | 57 | #include "gameinput_script.h" |
| 57 | #include "keyboard_pad_connection.h" | 58 | #include "keyboard_pad_connection.h" |
| @@ -1786,11 +1787,29 @@ bool is_keyboard_pad(SDL_JoystickID jid) { return jid != 0 && jid == g_keyboard. | |||
| 1786 | // Keeps the cursor inside the game window while it has focus, so a click cannot land on another window, and releases | 1787 | // Keeps the cursor inside the game window while it has focus, so a click cannot land on another window, and releases |
| 1787 | // it on focus loss. Only with raw input (a clipped plain cursor would stop the stick at the window edge) and only for | 1788 | // it on focus loss. Only with raw input (a clipped plain cursor would stop the stick at the window edge) and only for |
| 1788 | // a real foreground window of this process: not KeyboardPadMode::Always over a terminal, nor a test's fake focus. | 1789 | // a real foreground window of this process: not KeyboardPadMode::Always over a terminal, nor a test's fake focus. |
| 1789 | // Re-applied every 100 ms, because Windows drops the clip on some focus and resize changes. | 1790 | // Re-applied every 100 ms, because Windows drops the clip on some focus and resize changes. Never while the window is |
| 1791 | // dragged or sized, or a press that began on its title bar or border is held (gameinput_clip.h has why). | ||
| 1790 | bool g_clipped = false; | 1792 | bool g_clipped = false; |
| 1791 | uint64_t g_clip_at = 0; | 1793 | uint64_t g_clip_at = 0; |
| 1794 | rt::gameinput::detail::CursorClipGate g_clip_gate; | ||
| 1792 | 1795 | ||
| 1793 | void clip_cursor(bool want) { | 1796 | void clip_cursor(bool want) { |
| 1797 | const HWND w = want ? GetForegroundWindow() : nullptr; | ||
| 1798 | if (w) { | ||
| 1799 | GUITHREADINFO gui{}; | ||
| 1800 | gui.cbSize = sizeof(gui); | ||
| 1801 | const bool modal_loop = GetGUIThreadInfo(GetWindowThreadProcessId(w, nullptr), &gui) && | ||
| 1802 | (gui.flags & (GUI_INMOVESIZE | GUI_INMENUMODE | GUI_SYSTEMMENUMODE)); | ||
| 1803 | const bool button_down = ((GetAsyncKeyState(VK_LBUTTON) | GetAsyncKeyState(VK_RBUTTON) | | ||
| 1804 | GetAsyncKeyState(VK_MBUTTON)) & 0x8000) != 0; | ||
| 1805 | POINT cursor; | ||
| 1806 | RECT client; | ||
| 1807 | const bool in_client = GetCursorPos(&cursor) && ScreenToClient(w, &cursor) && GetClientRect(w, &client) && | ||
| 1808 | PtInRect(&client, cursor); | ||
| 1809 | want = g_clip_gate.update(true, modal_loop, button_down, in_client); | ||
| 1810 | } else { | ||
| 1811 | want = g_clip_gate.update(false, false, false, false); | ||
| 1812 | } | ||
| 1794 | if (!want) { | 1813 | if (!want) { |
| 1795 | if (g_clipped) ClipCursor(nullptr); | 1814 | if (g_clipped) ClipCursor(nullptr); |
| 1796 | g_clipped = false; | 1815 | g_clipped = false; |
| @@ -1798,9 +1817,8 @@ void clip_cursor(bool want) { | |||
| 1798 | } | 1817 | } |
| 1799 | const uint64_t now = now_us(); | 1818 | const uint64_t now = now_us(); |
| 1800 | if (g_clipped && now - g_clip_at < 100'000) return; | 1819 | if (g_clipped && now - g_clip_at < 100'000) return; |
| 1801 | const HWND w = GetForegroundWindow(); | ||
| 1802 | RECT r; | 1820 | RECT r; |
| 1803 | if (!w || !GetClientRect(w, &r)) return; | 1821 | if (!GetClientRect(w, &r)) return; |
| 1804 | POINT top_left{r.left, r.top}, bottom_right{r.right, r.bottom}; | 1822 | POINT top_left{r.left, r.top}, bottom_right{r.right, r.bottom}; |
| 1805 | if (!ClientToScreen(w, &top_left) || !ClientToScreen(w, &bottom_right)) return; | 1823 | if (!ClientToScreen(w, &top_left) || !ClientToScreen(w, &bottom_right)) return; |
| 1806 | const RECT screen{top_left.x, top_left.y, bottom_right.x, bottom_right.y}; | 1824 | const RECT screen{top_left.x, top_left.y, bottom_right.x, bottom_right.y}; |
| @@ -1874,13 +1892,17 @@ void update_keyboard_pad() { | |||
| 1874 | int x = 0, y = 0; | 1892 | int x = 0, y = 0; |
| 1875 | const bool have = g_cursor_pos.load()(&x, &y); | 1893 | const bool have = g_cursor_pos.load()(&x, &y); |
| 1876 | const bool moved = have && g_keyboard.have_cursor; | 1894 | const bool moved = have && g_keyboard.have_cursor; |
| 1895 | // Raw input exposes the low bits of a running total. Keep subtraction modular across a wrap. | ||
| 1896 | auto delta = [](int current, int previous) { | ||
| 1897 | return static_cast<int>(static_cast<unsigned>(current) - static_cast<unsigned>(previous)); | ||
| 1898 | }; | ||
| 1877 | auto step = [sens](int v, int pixels) { | 1899 | auto step = [sens](int v, int pixels) { |
| 1878 | const long long n = static_cast<long long>(v) * kMouseDecayNum / kMouseDecayDen + | 1900 | const long long n = static_cast<long long>(v) * kMouseDecayNum / kMouseDecayDen + |
| 1879 | std::lround(static_cast<long long>(pixels) * kMouseAxisPerPixel * sens); | 1901 | std::llround(static_cast<long long>(pixels) * kMouseAxisPerPixel * sens); |
| 1880 | return static_cast<int>(std::clamp<long long>(n, -kStickFull, kStickFull)); | 1902 | return static_cast<int>(std::clamp<long long>(n, -kStickFull, kStickFull)); |
| 1881 | }; | 1903 | }; |
| 1882 | g_keyboard.right_x = step(g_keyboard.right_x, moved ? x - g_keyboard.cursor_x : 0); | 1904 | g_keyboard.right_x = step(g_keyboard.right_x, moved ? delta(x, g_keyboard.cursor_x) : 0); |
| 1883 | g_keyboard.right_y = step(g_keyboard.right_y, moved ? y - g_keyboard.cursor_y : 0); | 1905 | g_keyboard.right_y = step(g_keyboard.right_y, moved ? delta(y, g_keyboard.cursor_y) : 0); |
| 1884 | if (have) g_keyboard.cursor_x = x, g_keyboard.cursor_y = y; | 1906 | if (have) g_keyboard.cursor_x = x, g_keyboard.cursor_y = y; |
| 1885 | g_keyboard.have_cursor = have; | 1907 | g_keyboard.have_cursor = have; |
| 1886 | } else { | 1908 | } else { |