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 | 5fbcc642a52de0f269be65a9dce208dcb87f00fe (patch) | |
| tree | 518cbe01fe433c290c4cdcd5955bf4585facd68a /runtime/shims/gameinput.cpp | |
| parent | f7e422ee8a307ced9a41e11fb02330b4cb7abdaa (diff) | |
| parent | 9c49bd5332327e689c5c957786c31b10efb3f32b (diff) | |
Merge upstream main into PR #1559
Diffstat (limited to 'runtime/shims/gameinput.cpp')
| -rw-r--r-- | runtime/shims/gameinput.cpp | 273 |
1 files changed, 224 insertions, 49 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 6acabae38..9cfe1231b 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp | |||
| @@ -45,14 +45,17 @@ | |||
| 45 | #include <cstring> | 45 | #include <cstring> |
| 46 | #include <deque> | 46 | #include <deque> |
| 47 | #include <mutex> | 47 | #include <mutex> |
| 48 | #include <map> | ||
| 48 | #include <string> | 49 | #include <string> |
| 49 | #include <thread> | 50 | #include <thread> |
| 50 | #include <utility> | 51 | #include <utility> |
| 51 | #include <vector> | 52 | #include <vector> |
| 52 | 53 | ||
| 53 | #include "gameinput.h" | 54 | #include "gameinput.h" |
| 55 | #include "gameinput_clip.h" | ||
| 54 | #include "gameinput_abi.h" | 56 | #include "gameinput_abi.h" |
| 55 | #include "gameinput_script.h" | 57 | #include "gameinput_script.h" |
| 58 | #include "keyboard_pad_connection.h" | ||
| 56 | #include "shims.h" | 59 | #include "shims.h" |
| 57 | 60 | ||
| 58 | using namespace gi; | 61 | using namespace gi; |
| @@ -1013,6 +1016,9 @@ leonida::InputFamily family_of(SDL_JoystickID jid, SDL_Gamepad* p) { | |||
| 1013 | // in bits 0-7, forced in bit 8, the generation from bit 16. | 1016 | // in bits 0-7, forced in bit 8, the generation from bit 16. |
| 1014 | leonida::ActiveInputTracker g_tracker; | 1017 | leonida::ActiveInputTracker g_tracker; |
| 1015 | std::atomic<uint64_t> g_active{0}; | 1018 | std::atomic<uint64_t> g_active{0}; |
| 1019 | // GetCurrentReading calls so far, any device: a scripted press is held until the title has read the pads a few | ||
| 1020 | // times since it began (update_keyboard_pad). | ||
| 1021 | std::atomic<uint64_t> g_reading_calls{0}; | ||
| 1016 | 1022 | ||
| 1017 | void publish_active() { | 1023 | void publish_active() { |
| 1018 | g_active.store(static_cast<uint64_t>(g_tracker.active()) | (g_tracker.forced() ? 1ull << 8 : 0) | | 1024 | g_active.store(static_cast<uint64_t>(g_tracker.active()) | (g_tracker.forced() ? 1ull << 8 : 0) | |
| @@ -1775,10 +1781,7 @@ std::atomic<rt::gameinput::KeyDownFn> g_key_down{async_key_down}; | |||
| 1775 | std::atomic<rt::gameinput::HasFocusFn> g_has_focus{foreground_is_ours}; | 1781 | std::atomic<rt::gameinput::HasFocusFn> g_has_focus{foreground_is_ours}; |
| 1776 | std::atomic<rt::gameinput::CursorPosFn> g_cursor_pos{system_cursor_pos}; | 1782 | std::atomic<rt::gameinput::CursorPosFn> g_cursor_pos{system_cursor_pos}; |
| 1777 | 1783 | ||
| 1778 | struct KeyboardPad { | 1784 | struct KeyboardPad : rt::gameinput::detail::KeyboardPadConnection<SDL_JoystickID, SDL_Joystick*> { |
| 1779 | SDL_JoystickID id = 0; | ||
| 1780 | SDL_Joystick* js = nullptr; | ||
| 1781 | bool failed = false; // attaching failed once; not retried every 2 ms | ||
| 1782 | int right_x = 0, right_y = 0; // the mouse's right stick, SDL axis units (+Y down) | 1785 | int right_x = 0, right_y = 0; // the mouse's right stick, SDL axis units (+Y down) |
| 1783 | int cursor_x = 0, cursor_y = 0; // the cursor at the last poll | 1786 | int cursor_x = 0, cursor_y = 0; // the cursor at the last poll |
| 1784 | bool have_cursor = false; // cursor_x/y are valid; false after focus loss, so the first poll moves nothing | 1787 | bool have_cursor = false; // cursor_x/y are valid; false after focus loss, so the first poll moves nothing |
| @@ -1787,21 +1790,32 @@ KeyboardPad g_keyboard; | |||
| 1787 | 1790 | ||
| 1788 | bool is_keyboard_pad(SDL_JoystickID jid) { return jid != 0 && jid == g_keyboard.id; } | 1791 | bool is_keyboard_pad(SDL_JoystickID jid) { return jid != 0 && jid == g_keyboard.id; } |
| 1789 | 1792 | ||
| 1790 | void detach_keyboard_pad() { | ||
| 1791 | if (g_keyboard.js) SDL_CloseJoystick(g_keyboard.js); | ||
| 1792 | if (g_keyboard.id) SDL_DetachVirtualJoystick(g_keyboard.id); | ||
| 1793 | if (g_keyboard.id) std::fprintf(stderr, "[rt] gameinput: keyboard pad removed\n"); | ||
| 1794 | g_keyboard = {}; | ||
| 1795 | } | ||
| 1796 | |||
| 1797 | // Keeps the cursor inside the game window while it has focus, so a click cannot land on another window, and releases | 1793 | // Keeps the cursor inside the game window while it has focus, so a click cannot land on another window, and releases |
| 1798 | // it on focus loss. Only with raw input (a clipped plain cursor would stop the stick at the window edge) and only for | 1794 | // it on focus loss. Only with raw input (a clipped plain cursor would stop the stick at the window edge) and only for |
| 1799 | // a real foreground window of this process: not KeyboardPadMode::Always over a terminal, nor a test's fake focus. | 1795 | // a real foreground window of this process: not KeyboardPadMode::Always over a terminal, nor a test's fake focus. |
| 1800 | // Re-applied every 100 ms, because Windows drops the clip on some focus and resize changes. | 1796 | // Re-applied every 100 ms, because Windows drops the clip on some focus and resize changes. Never while the window is |
| 1797 | // dragged or sized, or a press that began on its title bar or border is held (gameinput_clip.h has why). | ||
| 1801 | bool g_clipped = false; | 1798 | bool g_clipped = false; |
| 1802 | uint64_t g_clip_at = 0; | 1799 | uint64_t g_clip_at = 0; |
| 1800 | rt::gameinput::detail::CursorClipGate g_clip_gate; | ||
| 1803 | 1801 | ||
| 1804 | void clip_cursor(bool want) { | 1802 | void clip_cursor(bool want) { |
| 1803 | const HWND w = want ? GetForegroundWindow() : nullptr; | ||
| 1804 | if (w) { | ||
| 1805 | GUITHREADINFO gui{}; | ||
| 1806 | gui.cbSize = sizeof(gui); | ||
| 1807 | const bool modal_loop = GetGUIThreadInfo(GetWindowThreadProcessId(w, nullptr), &gui) && | ||
| 1808 | (gui.flags & (GUI_INMOVESIZE | GUI_INMENUMODE | GUI_SYSTEMMENUMODE)); | ||
| 1809 | const bool button_down = ((GetAsyncKeyState(VK_LBUTTON) | GetAsyncKeyState(VK_RBUTTON) | | ||
| 1810 | GetAsyncKeyState(VK_MBUTTON)) & 0x8000) != 0; | ||
| 1811 | POINT cursor; | ||
| 1812 | RECT client; | ||
| 1813 | const bool in_client = GetCursorPos(&cursor) && ScreenToClient(w, &cursor) && GetClientRect(w, &client) && | ||
| 1814 | PtInRect(&client, cursor); | ||
| 1815 | want = g_clip_gate.update(true, modal_loop, button_down, in_client); | ||
| 1816 | } else { | ||
| 1817 | want = g_clip_gate.update(false, false, false, false); | ||
| 1818 | } | ||
| 1805 | if (!want) { | 1819 | if (!want) { |
| 1806 | if (g_clipped) ClipCursor(nullptr); | 1820 | if (g_clipped) ClipCursor(nullptr); |
| 1807 | g_clipped = false; | 1821 | g_clipped = false; |
| @@ -1809,9 +1823,8 @@ void clip_cursor(bool want) { | |||
| 1809 | } | 1823 | } |
| 1810 | const uint64_t now = now_us(); | 1824 | const uint64_t now = now_us(); |
| 1811 | if (g_clipped && now - g_clip_at < 100'000) return; | 1825 | if (g_clipped && now - g_clip_at < 100'000) return; |
| 1812 | const HWND w = GetForegroundWindow(); | ||
| 1813 | RECT r; | 1826 | RECT r; |
| 1814 | if (!w || !GetClientRect(w, &r)) return; | 1827 | if (!GetClientRect(w, &r)) return; |
| 1815 | POINT top_left{r.left, r.top}, bottom_right{r.right, r.bottom}; | 1828 | POINT top_left{r.left, r.top}, bottom_right{r.right, r.bottom}; |
| 1816 | if (!ClientToScreen(w, &top_left) || !ClientToScreen(w, &bottom_right)) return; | 1829 | if (!ClientToScreen(w, &top_left) || !ClientToScreen(w, &bottom_right)) return; |
| 1817 | const RECT screen{top_left.x, top_left.y, bottom_right.x, bottom_right.y}; | 1830 | const RECT screen{top_left.x, top_left.y, bottom_right.x, bottom_right.y}; |
| @@ -1821,29 +1834,34 @@ void clip_cursor(bool want) { | |||
| 1821 | } | 1834 | } |
| 1822 | } | 1835 | } |
| 1823 | 1836 | ||
| 1824 | bool attach_keyboard_pad() { | 1837 | struct KeyboardPadBackend { |
| 1825 | SDL_VirtualJoystickDesc d; | 1838 | static uint64_t now() { return now_us(); } |
| 1826 | SDL_INIT_INTERFACE(&d); | 1839 | static SDL_JoystickID attach() { |
| 1827 | d.type = SDL_JOYSTICK_TYPE_GAMEPAD; | 1840 | SDL_VirtualJoystickDesc d; |
| 1828 | d.naxes = SDL_GAMEPAD_AXIS_COUNT; | 1841 | SDL_INIT_INTERFACE(&d); |
| 1829 | d.nbuttons = SDL_GAMEPAD_BUTTON_COUNT; | 1842 | d.type = SDL_JOYSTICK_TYPE_GAMEPAD; |
| 1830 | d.button_mask = (1u << SDL_GAMEPAD_BUTTON_COUNT) - 1; | 1843 | d.naxes = SDL_GAMEPAD_AXIS_COUNT; |
| 1831 | d.axis_mask = (1u << SDL_GAMEPAD_AXIS_COUNT) - 1; | 1844 | d.nbuttons = SDL_GAMEPAD_BUTTON_COUNT; |
| 1832 | d.name = "Keyboard pad (Leonida)"; | 1845 | d.button_mask = (1u << SDL_GAMEPAD_BUTTON_COUNT) - 1; |
| 1833 | g_keyboard.id = SDL_AttachVirtualJoystick(&d); | 1846 | d.axis_mask = (1u << SDL_GAMEPAD_AXIS_COUNT) - 1; |
| 1834 | g_keyboard.js = g_keyboard.id ? SDL_OpenJoystick(g_keyboard.id) : nullptr; | 1847 | d.name = "Keyboard pad (Leonida)"; |
| 1835 | if (!g_keyboard.js) { | 1848 | return SDL_AttachVirtualJoystick(&d); |
| 1849 | } | ||
| 1850 | static SDL_Joystick* open(SDL_JoystickID id) { return SDL_OpenJoystick(id); } | ||
| 1851 | static void close(SDL_Joystick* js) { SDL_CloseJoystick(js); } | ||
| 1852 | static bool detach(SDL_JoystickID id) { return SDL_DetachVirtualJoystick(id); } | ||
| 1853 | static bool is_virtual(SDL_JoystickID id) { return SDL_IsJoystickVirtual(id); } | ||
| 1854 | static void removed() { std::fprintf(stderr, "[rt] gameinput: keyboard pad removed\n"); } | ||
| 1855 | static void failed() { | ||
| 1836 | std::fprintf(stderr, "[rt] gameinput: cannot attach the keyboard pad: %s\n", SDL_GetError()); | 1856 | std::fprintf(stderr, "[rt] gameinput: cannot attach the keyboard pad: %s\n", SDL_GetError()); |
| 1837 | detach_keyboard_pad(); | ||
| 1838 | g_keyboard.failed = true; | ||
| 1839 | return false; | ||
| 1840 | } | 1857 | } |
| 1841 | std::fprintf(stderr, "[rt] gameinput: keyboard pad attached (arrows d-pad, Enter A, Backspace B, X X, Y Y, " | 1858 | static void attached() { |
| 1842 | "Q/E shoulders, Esc Menu, Tab View, WASD left stick, mouse right stick, right/left " | 1859 | std::fprintf(stderr, "[rt] gameinput: keyboard pad attached (arrows d-pad, Enter A, Backspace B, X X, Y Y, " |
| 1843 | "button LT/RT)\n"); | 1860 | "Q/E shoulders, Esc Menu, Tab View, WASD left stick, mouse right stick, right/left " |
| 1844 | start_raw_mouse(); | 1861 | "button LT/RT)\n"); |
| 1845 | return true; | 1862 | start_raw_mouse(); |
| 1846 | } | 1863 | } |
| 1864 | }; | ||
| 1847 | 1865 | ||
| 1848 | // Before SDL_UpdateGamepads, so the buttons set here are in this poll's reading. | 1866 | // Before SDL_UpdateGamepads, so the buttons set here are in this poll's reading. |
| 1849 | void update_keyboard_pad() { | 1867 | void update_keyboard_pad() { |
| @@ -1854,13 +1872,14 @@ void update_keyboard_pad() { | |||
| 1854 | mode = g().config.keyboard_pad; | 1872 | mode = g().config.keyboard_pad; |
| 1855 | sens = g().config.mouse_sensitivity; | 1873 | sens = g().config.mouse_sensitivity; |
| 1856 | } | 1874 | } |
| 1857 | if (mode == rt::gameinput::KeyboardPadMode::Off) { | 1875 | KeyboardPadBackend backend; |
| 1876 | const bool enabled = mode != rt::gameinput::KeyboardPadMode::Off; | ||
| 1877 | if (!enabled) { | ||
| 1858 | clip_cursor(false); | 1878 | clip_cursor(false); |
| 1859 | if (g_keyboard.id) detach_keyboard_pad(); | 1879 | g_keyboard.right_x = g_keyboard.right_y = 0; |
| 1860 | g_keyboard.failed = false; | 1880 | g_keyboard.have_cursor = false; |
| 1861 | return; | ||
| 1862 | } | 1881 | } |
| 1863 | if (!g_keyboard.js && (g_keyboard.failed || !attach_keyboard_pad())) return; | 1882 | if (!g_keyboard.update(enabled, backend)) return; |
| 1864 | 1883 | ||
| 1865 | bool down[SDL_GAMEPAD_BUTTON_COUNT] = {}; | 1884 | bool down[SDL_GAMEPAD_BUTTON_COUNT] = {}; |
| 1866 | int lx = 0, ly = 0; | 1885 | int lx = 0, ly = 0; |
| @@ -1879,13 +1898,17 @@ void update_keyboard_pad() { | |||
| 1879 | int x = 0, y = 0; | 1898 | int x = 0, y = 0; |
| 1880 | const bool have = g_cursor_pos.load()(&x, &y); | 1899 | const bool have = g_cursor_pos.load()(&x, &y); |
| 1881 | const bool moved = have && g_keyboard.have_cursor; | 1900 | 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. | ||
| 1902 | auto delta = [](int current, int previous) { | ||
| 1903 | return static_cast<int>(static_cast<unsigned>(current) - static_cast<unsigned>(previous)); | ||
| 1904 | }; | ||
| 1882 | auto step = [sens](int v, int pixels) { | 1905 | auto step = [sens](int v, int pixels) { |
| 1883 | const long long n = static_cast<long long>(v) * kMouseDecayNum / kMouseDecayDen + | 1906 | const long long n = static_cast<long long>(v) * kMouseDecayNum / kMouseDecayDen + |
| 1884 | std::lround(static_cast<long long>(pixels) * kMouseAxisPerPixel * sens); | 1907 | std::llround(static_cast<long long>(pixels) * kMouseAxisPerPixel * sens); |
| 1885 | return static_cast<int>(std::clamp<long long>(n, -kStickFull, kStickFull)); | 1908 | return static_cast<int>(std::clamp<long long>(n, -kStickFull, kStickFull)); |
| 1886 | }; | 1909 | }; |
| 1887 | g_keyboard.right_x = step(g_keyboard.right_x, moved ? x - g_keyboard.cursor_x : 0); | 1910 | g_keyboard.right_x = step(g_keyboard.right_x, moved ? delta(x, g_keyboard.cursor_x) : 0); |
| 1888 | g_keyboard.right_y = step(g_keyboard.right_y, moved ? y - g_keyboard.cursor_y : 0); | 1911 | g_keyboard.right_y = step(g_keyboard.right_y, moved ? delta(y, g_keyboard.cursor_y) : 0); |
| 1889 | if (have) g_keyboard.cursor_x = x, g_keyboard.cursor_y = y; | 1912 | if (have) g_keyboard.cursor_x = x, g_keyboard.cursor_y = y; |
| 1890 | g_keyboard.have_cursor = have; | 1913 | g_keyboard.have_cursor = have; |
| 1891 | } else { | 1914 | } else { |
| @@ -1895,7 +1918,9 @@ void update_keyboard_pad() { | |||
| 1895 | clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours()); | 1918 | clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours()); |
| 1896 | for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); | 1919 | for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); |
| 1897 | // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard | 1920 | // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard |
| 1898 | // pad, in seconds since the pad's first update, each held 300 ms. For unattended boots: the title's first prompt | 1921 | // pad, in seconds since the pad's first update, each held 300 ms or its own button@seconds:hold, and then until the |
| 1922 | // title has read the pads kReads times since it began (never past kMaxHold seconds, or the hold if it is longer, so | ||
| 1923 | // a title that stops reading does not keep the button). For unattended boots: the title's first prompt | ||
| 1899 | // waits for a button with no timeout (docs/re/notes/firstframe.md, step 5). The parser, the timeline and the | 1924 | // waits for a button with no timeout (docs/re/notes/firstframe.md, step 5). The parser, the timeline and the |
| 1900 | // button names are gameinput_script.h's, shared with the scripted pad of a build without SDL3. | 1925 | // button names are gameinput_script.h's, shared with the scripted pad of a build without SDL3. |
| 1901 | namespace script = rt::gameinput::script; | 1926 | namespace script = rt::gameinput::script; |
| @@ -1907,13 +1932,82 @@ void update_keyboard_pad() { | |||
| 1907 | s.presses.size(), s.rejected); | 1932 | s.presses.size(), s.rejected); |
| 1908 | return s; | 1933 | return s; |
| 1909 | }(); | 1934 | }(); |
| 1910 | if (!scripted.empty()) { | 1935 | // LEONIDA_INPUT_FILE=<path> (local, for headless runs whose display takes no keyboard input): every line appended |
| 1936 | // to the file names one button of gameinput_script.h's table ("a", "down", ...) and becomes a press at once, held | ||
| 1937 | // like a scripted one (300 ms and six readings, at most 5 s). The file is read again at most every 250 ms. | ||
| 1938 | struct FilePress { | ||
| 1939 | uint32_t bit; | ||
| 1940 | double at; | ||
| 1941 | uint64_t began; | ||
| 1942 | bool started; | ||
| 1943 | }; | ||
| 1944 | static std::vector<FilePress> file_presses; | ||
| 1945 | static const std::string input_file = [] { | ||
| 1946 | const char* v = std::getenv("LEONIDA_INPUT_FILE"); | ||
| 1947 | return std::string(v ? v : ""); | ||
| 1948 | }(); | ||
| 1949 | if (!scripted.empty() || !input_file.empty()) { | ||
| 1911 | static const ULONGLONG origin = GetTickCount64(); | 1950 | static const ULONGLONG origin = GetTickCount64(); |
| 1912 | const double now = (GetTickCount64() - origin) / 1000.0; | 1951 | const double now = (GetTickCount64() - origin) / 1000.0; |
| 1913 | const script::Held held = script::held_at(scripted, now); | 1952 | script::Held held{}; |
| 1953 | static constexpr uint64_t kReads = 6; | ||
| 1954 | static constexpr double kMaxHold = 5.0; | ||
| 1955 | static std::vector<uint64_t> began(scripted.presses.size(), 0); | ||
| 1956 | static std::vector<char> started(scripted.presses.size(), 0); | ||
| 1957 | const uint64_t calls = g_reading_calls.load(std::memory_order_relaxed); | ||
| 1958 | if (!input_file.empty()) { | ||
| 1959 | static ULONGLONG last_read = 0; | ||
| 1960 | static std::size_t lines_done = 0; | ||
| 1961 | const ULONGLONG tick = GetTickCount64(); | ||
| 1962 | if (tick - last_read >= 250) { | ||
| 1963 | last_read = tick; | ||
| 1964 | if (FILE* f = std::fopen(input_file.c_str(), "r")) { | ||
| 1965 | char line[64]; | ||
| 1966 | std::size_t n = 0; | ||
| 1967 | while (std::fgets(line, sizeof(line), f)) { | ||
| 1968 | if (++n <= lines_done) continue; | ||
| 1969 | lines_done = n; | ||
| 1970 | std::string name(line); | ||
| 1971 | while (!name.empty() && (name.back() == '\n' || name.back() == '\r' || name.back() == ' ')) | ||
| 1972 | name.pop_back(); | ||
| 1973 | bool known = false; | ||
| 1974 | for (const script::Button& b : script::kButtons) | ||
| 1975 | if (name == b.name) { | ||
| 1976 | file_presses.push_back({b.bit, now, 0, false}); | ||
| 1977 | known = true; | ||
| 1978 | } | ||
| 1979 | std::fprintf(stderr, "[rt] gameinput: input file line %zu \"%s\" at %.1f s%s\n", n, | ||
| 1980 | name.c_str(), now, known ? "" : " (unknown button, ignored)"); | ||
| 1981 | } | ||
| 1982 | std::fclose(f); | ||
| 1983 | } | ||
| 1984 | } | ||
| 1985 | for (auto& p : file_presses) { | ||
| 1986 | if (now < p.at || now >= p.at + kMaxHold) continue; | ||
| 1987 | if (!p.started) { | ||
| 1988 | p.started = true; | ||
| 1989 | p.began = calls; | ||
| 1990 | } | ||
| 1991 | if (now < p.at + 0.3 || calls - p.began < kReads) held.buttons |= p.bit; | ||
| 1992 | } | ||
| 1993 | } | ||
| 1994 | for (std::size_t i = 0; i < scripted.presses.size(); ++i) { | ||
| 1995 | const auto& press = scripted.presses[i]; | ||
| 1996 | bool active = false; | ||
| 1997 | if (now >= press.at && now < press.at + std::max(press.hold, kMaxHold)) { | ||
| 1998 | if (!started[i]) { | ||
| 1999 | started[i] = 1; | ||
| 2000 | began[i] = calls; | ||
| 2001 | } | ||
| 2002 | active = now < press.at + press.hold || calls - began[i] < kReads; | ||
| 2003 | } | ||
| 2004 | if (!active) continue; | ||
| 2005 | if (press.stick) held.stick = press.stick; | ||
| 2006 | else held.buttons |= press.button; | ||
| 2007 | } | ||
| 1914 | if (held.stick) right_y = held.stick > 0 ? -kStickFull : kStickFull; // SDL's +Y is down | 2008 | if (held.stick) right_y = held.stick > 0 ? -kStickFull : kStickFull; // SDL's +Y is down |
| 1915 | for (const script::Button& b : script::kButtons) { | 2009 | for (const script::Button& b : script::kButtons) { |
| 1916 | if (!(scripted.named & b.bit)) continue; | 2010 | if (!(scripted.named & b.bit) && input_file.empty()) continue; |
| 1917 | const auto button = static_cast<SDL_GamepadButton>(b.sdl); | 2011 | const auto button = static_cast<SDL_GamepadButton>(b.sdl); |
| 1918 | SDL_SetJoystickVirtualButton(g_keyboard.js, button, (held.buttons & b.bit) || down[button]); | 2012 | SDL_SetJoystickVirtualButton(g_keyboard.js, button, (held.buttons & b.bit) || down[button]); |
| 1919 | } | 2013 | } |
| @@ -1968,6 +2062,10 @@ void poll_once() { | |||
| 1968 | SDL_JoystickID* ids = SDL_GetGamepads(&count); | 2062 | SDL_JoystickID* ids = SDL_GetGamepads(&count); |
| 1969 | std::vector<SDL_JoystickID> present(ids, ids + (ids ? count : 0)); | 2063 | std::vector<SDL_JoystickID> present(ids, ids + (ids ? count : 0)); |
| 1970 | SDL_free(ids); | 2064 | SDL_free(ids); |
| 2065 | // An unopened virtual device may still be waiting for SDL to finish detaching it. | ||
| 2066 | // Do not advertise it to the title until it has a working keyboard handle. | ||
| 2067 | if (g_keyboard.id && !g_keyboard.js) | ||
| 2068 | present.erase(std::remove(present.begin(), present.end(), g_keyboard.id), present.end()); | ||
| 1971 | 2069 | ||
| 1972 | std::vector<Device*> connected; | 2070 | std::vector<Device*> connected; |
| 1973 | { | 2071 | { |
| @@ -2426,7 +2524,80 @@ public: | |||
| 2426 | 2524 | ||
| 2427 | uint64_t STDMETHODCALLTYPE GetCurrentTimestamp() override { return now_us(); } | 2525 | uint64_t STDMETHODCALLTYPE GetCurrentTimestamp() override { return now_us(); } |
| 2428 | 2526 | ||
| 2527 | // LEONIDA_INPUT_STATS=1 (diagnostic): every 10 s, how the title asked for readings (by method and device | ||
| 2528 | // argument) and what it got (by the reading's device, and whether a gamepad button was down in it). | ||
| 2529 | static void note_reading(int method, IGameInputDevice* asked, HRESULT hr, IGameInputReading* got) { | ||
| 2530 | static const bool on = env("LEONIDA_INPUT_STATS") == "1"; | ||
| 2531 | if (!on) return; | ||
| 2532 | struct Stats { | ||
| 2533 | std::mutex mu; | ||
| 2534 | uint64_t calls[3][3] = {}; // method x (null, aggregate, a device) | ||
| 2535 | uint64_t found[3] = {}, pressed[3] = {}, a_pressed[3] = {}; | ||
| 2536 | std::map<std::string, std::pair<uint64_t, uint64_t>> by_device; // name -> (readings, with buttons) | ||
| 2537 | uint64_t since = 0; | ||
| 2538 | }; | ||
| 2539 | static Stats st; | ||
| 2540 | std::string name; | ||
| 2541 | bool buttons = false, a_button = false; | ||
| 2542 | if (SUCCEEDED(hr) && got) { | ||
| 2543 | IGameInputDevice* d = nullptr; | ||
| 2544 | got->GetDevice(&d); | ||
| 2545 | if (d) { | ||
| 2546 | name = as_device(d)->name; | ||
| 2547 | d->Release(); | ||
| 2548 | } | ||
| 2549 | GameInputGamepadState g{}; | ||
| 2550 | if (got->GetGamepadState(&g)) { | ||
| 2551 | buttons = g.buttons != 0; | ||
| 2552 | a_button = (g.buttons & GameInputGamepadA) != 0; | ||
| 2553 | } | ||
| 2554 | } | ||
| 2555 | const int arg = !asked ? 0 : as_device(asked)->info.deviceFamily == GameInputFamilyAggregate ? 1 : 2; | ||
| 2556 | std::lock_guard lock(st.mu); | ||
| 2557 | ++st.calls[method][arg]; | ||
| 2558 | if (SUCCEEDED(hr) && got) { | ||
| 2559 | ++st.found[method]; | ||
| 2560 | st.pressed[method] += buttons; | ||
| 2561 | st.a_pressed[method] += a_button; | ||
| 2562 | auto& e = st.by_device[name]; | ||
| 2563 | ++e.first; | ||
| 2564 | e.second += buttons; | ||
| 2565 | } | ||
| 2566 | const uint64_t now = now_us(); | ||
| 2567 | if (!st.since) st.since = now; | ||
| 2568 | if (now - st.since < 10'000'000) return; | ||
| 2569 | static const char* methods[] = {"GetCurrentReading", "GetNextReading", "GetPreviousReading"}; | ||
| 2570 | std::string line; | ||
| 2571 | for (int m = 0; m < 3; ++m) { | ||
| 2572 | char t[200]; | ||
| 2573 | std::snprintf(t, sizeof(t), " %s %llu/%llu/%llu found %llu pressed %llu A %llu;", methods[m], | ||
| 2574 | (unsigned long long)st.calls[m][0], (unsigned long long)st.calls[m][1], | ||
| 2575 | (unsigned long long)st.calls[m][2], (unsigned long long)st.found[m], | ||
| 2576 | (unsigned long long)st.pressed[m], (unsigned long long)st.a_pressed[m]); | ||
| 2577 | line += t; | ||
| 2578 | } | ||
| 2579 | for (const auto& [n, c] : st.by_device) { | ||
| 2580 | char t[160]; | ||
| 2581 | std::snprintf(t, sizeof(t), " [%s: %llu readings, %llu with buttons]", n.c_str(), (unsigned long long)c.first, | ||
| 2582 | (unsigned long long)c.second); | ||
| 2583 | line += t; | ||
| 2584 | } | ||
| 2585 | std::fprintf(stderr, "[rt] gameinput stats (calls null/aggregate/device): %s\n", line.c_str()); | ||
| 2586 | std::memset(st.calls, 0, sizeof(st.calls)); | ||
| 2587 | std::memset(st.found, 0, sizeof(st.found)); | ||
| 2588 | std::memset(st.pressed, 0, sizeof(st.pressed)); | ||
| 2589 | std::memset(st.a_pressed, 0, sizeof(st.a_pressed)); | ||
| 2590 | st.by_device.clear(); | ||
| 2591 | st.since = now; | ||
| 2592 | } | ||
| 2593 | |||
| 2429 | HRESULT STDMETHODCALLTYPE GetCurrentReading(uint32_t kind, IGameInputDevice* device, IGameInputReading** out) override { | 2594 | HRESULT STDMETHODCALLTYPE GetCurrentReading(uint32_t kind, IGameInputDevice* device, IGameInputReading** out) override { |
| 2595 | g_reading_calls.fetch_add(1, std::memory_order_relaxed); | ||
| 2596 | const HRESULT hr = current_reading(kind, device, out); | ||
| 2597 | note_reading(0, device, hr, out ? *out : nullptr); | ||
| 2598 | return hr; | ||
| 2599 | } | ||
| 2600 | HRESULT current_reading(uint32_t kind, IGameInputDevice* device, IGameInputReading** out) { | ||
| 2430 | if (!out) return E_POINTER; | 2601 | if (!out) return E_POINTER; |
| 2431 | *out = nullptr; | 2602 | *out = nullptr; |
| 2432 | if (kind != GameInputKindUnknown && !(kind & kSupportedKinds)) return GAMEINPUT_E_READING_NOT_FOUND; | 2603 | if (kind != GameInputKindUnknown && !(kind & kSupportedKinds)) return GAMEINPUT_E_READING_NOT_FOUND; |
| @@ -2460,11 +2631,15 @@ public: | |||
| 2460 | 2631 | ||
| 2461 | HRESULT STDMETHODCALLTYPE GetNextReading(IGameInputReading* ref, uint32_t kind, IGameInputDevice* device, | 2632 | HRESULT STDMETHODCALLTYPE GetNextReading(IGameInputReading* ref, uint32_t kind, IGameInputDevice* device, |
| 2462 | IGameInputReading** out) override { | 2633 | IGameInputReading** out) override { |
| 2463 | return find_reading(ref, kind, device, out, true); | 2634 | const HRESULT hr = find_reading(ref, kind, device, out, true); |
| 2635 | note_reading(1, device, hr, out ? *out : nullptr); | ||
| 2636 | return hr; | ||
| 2464 | } | 2637 | } |
| 2465 | HRESULT STDMETHODCALLTYPE GetPreviousReading(IGameInputReading* ref, uint32_t kind, IGameInputDevice* device, | 2638 | HRESULT STDMETHODCALLTYPE GetPreviousReading(IGameInputReading* ref, uint32_t kind, IGameInputDevice* device, |
| 2466 | IGameInputReading** out) override { | 2639 | IGameInputReading** out) override { |
| 2467 | return find_reading(ref, kind, device, out, false); | 2640 | const HRESULT hr = find_reading(ref, kind, device, out, false); |
| 2641 | note_reading(2, device, hr, out ? *out : nullptr); | ||
| 2642 | return hr; | ||
| 2468 | } | 2643 | } |
| 2469 | // E_NOTIMPL is GameInput v0's own answer: Microsoft's reference page says the method "was never implemented in | 2644 | // E_NOTIMPL is GameInput v0's own answer: Microsoft's reference page says the method "was never implemented in |
| 2470 | // v0" and returns E_NOTIMPL, and v1 removed it. A history lookup here would give a title what no GameInput does. | 2645 | // v0" and returns E_NOTIMPL, and v1 removed it. A history lookup here would give a title what no GameInput does. |