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:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 19:04:24 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 19:04:24 -0500
commit8d832fd10913064e1c8aec74191e3ba8f70211c8 (patch)
treee365199d7d86ebf592a5a0dc2f07cda4b74b58a7 /runtime/shims/gameinput.cpp
parent5da3b6be3cefc1499452ba8c8370a50092674d61 (diff)
parentc50ed94b35b37b697ab9c5fd176f3738480b4db5 (diff)
Merge main into rt-title-every-budget (#2122)
#2098 split the every-pass loop of refresh_title_textures by mode: with LEONIDA_TEXTURE_REFRESH_ASYNC every persistent mapping is polled each pass and only stale unmapped textures are visited, without it all mapped or stale ones are. The async branch is kept as main has it. LEONIDA_TITLE_EVERY_BUDGET_US applies to the synchronous pass, the one before a submission on the submitting thread; under async the pass runs on the refresh worker and the limit does not apply (README says so). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp176
1 files changed, 136 insertions, 40 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index 47e9aa941..fa5889d6a 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -45,6 +45,7 @@
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>
@@ -53,6 +54,7 @@
53#include "gameinput.h" 54#include "gameinput.h"
54#include "gameinput_abi.h" 55#include "gameinput_abi.h"
55#include "gameinput_script.h" 56#include "gameinput_script.h"
57#include "keyboard_pad_connection.h"
56#include "shims.h" 58#include "shims.h"
57 59
58using namespace gi; 60using namespace gi;
@@ -1004,6 +1006,9 @@ leonida::InputFamily family_of(SDL_JoystickID jid, SDL_Gamepad* p) {
1004// in bits 0-7, forced in bit 8, the generation from bit 16. 1006// in bits 0-7, forced in bit 8, the generation from bit 16.
1005leonida::ActiveInputTracker g_tracker; 1007leonida::ActiveInputTracker g_tracker;
1006std::atomic<uint64_t> g_active{0}; 1008std::atomic<uint64_t> g_active{0};
1009// GetCurrentReading calls so far, any device: a scripted press is held until the title has read the pads a few
1010// times since it began (update_keyboard_pad).
1011std::atomic<uint64_t> g_reading_calls{0};
1007 1012
1008void publish_active() { 1013void publish_active() {
1009 g_active.store(static_cast<uint64_t>(g_tracker.active()) | (g_tracker.forced() ? 1ull << 8 : 0) | 1014 g_active.store(static_cast<uint64_t>(g_tracker.active()) | (g_tracker.forced() ? 1ull << 8 : 0) |
@@ -1766,10 +1771,7 @@ std::atomic<rt::gameinput::KeyDownFn> g_key_down{async_key_down};
1766std::atomic<rt::gameinput::HasFocusFn> g_has_focus{foreground_is_ours}; 1771std::atomic<rt::gameinput::HasFocusFn> g_has_focus{foreground_is_ours};
1767std::atomic<rt::gameinput::CursorPosFn> g_cursor_pos{system_cursor_pos}; 1772std::atomic<rt::gameinput::CursorPosFn> g_cursor_pos{system_cursor_pos};
1768 1773
1769struct KeyboardPad { 1774struct KeyboardPad : rt::gameinput::detail::KeyboardPadConnection<SDL_JoystickID, SDL_Joystick*> {
1770 SDL_JoystickID id = 0;
1771 SDL_Joystick* js = nullptr;
1772 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) 1775 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 1776 int cursor_x = 0, cursor_y = 0; // the cursor at the last poll
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
@@ -1778,13 +1780,6 @@ KeyboardPad g_keyboard;
1778 1780
1779bool is_keyboard_pad(SDL_JoystickID jid) { return jid != 0 && jid == g_keyboard.id; } 1781bool is_keyboard_pad(SDL_JoystickID jid) { return jid != 0 && jid == g_keyboard.id; }
1780 1782
1781void detach_keyboard_pad() {
1782 if (g_keyboard.js) SDL_CloseJoystick(g_keyboard.js);
1783 if (g_keyboard.id) SDL_DetachVirtualJoystick(g_keyboard.id);
1784 if (g_keyboard.id) std::fprintf(stderr, "[rt] gameinput: keyboard pad removed\n");
1785 g_keyboard = {};
1786}
1787
1788// Keeps the cursor inside the game window while it has focus, so a click cannot land on another window, and releases 1783// Keeps the cursor inside the game window while it has focus, so a click cannot land on another window, and releases
1789// it on focus loss. Only with raw input (a clipped plain cursor would stop the stick at the window edge) and only for 1784// it on focus loss. Only with raw input (a clipped plain cursor would stop the stick at the window edge) and only for
1790// a real foreground window of this process: not KeyboardPadMode::Always over a terminal, nor a test's fake focus. 1785// a real foreground window of this process: not KeyboardPadMode::Always over a terminal, nor a test's fake focus.
@@ -1812,29 +1807,34 @@ void clip_cursor(bool want) {
1812 } 1807 }
1813} 1808}
1814 1809
1815bool attach_keyboard_pad() { 1810struct KeyboardPadBackend {
1816 SDL_VirtualJoystickDesc d; 1811 static uint64_t now() { return now_us(); }
1817 SDL_INIT_INTERFACE(&d); 1812 static SDL_JoystickID attach() {
1818 d.type = SDL_JOYSTICK_TYPE_GAMEPAD; 1813 SDL_VirtualJoystickDesc d;
1819 d.naxes = SDL_GAMEPAD_AXIS_COUNT; 1814 SDL_INIT_INTERFACE(&d);
1820 d.nbuttons = SDL_GAMEPAD_BUTTON_COUNT; 1815 d.type = SDL_JOYSTICK_TYPE_GAMEPAD;
1821 d.button_mask = (1u << SDL_GAMEPAD_BUTTON_COUNT) - 1; 1816 d.naxes = SDL_GAMEPAD_AXIS_COUNT;
1822 d.axis_mask = (1u << SDL_GAMEPAD_AXIS_COUNT) - 1; 1817 d.nbuttons = SDL_GAMEPAD_BUTTON_COUNT;
1823 d.name = "Keyboard pad (Leonida)"; 1818 d.button_mask = (1u << SDL_GAMEPAD_BUTTON_COUNT) - 1;
1824 g_keyboard.id = SDL_AttachVirtualJoystick(&d); 1819 d.axis_mask = (1u << SDL_GAMEPAD_AXIS_COUNT) - 1;
1825 g_keyboard.js = g_keyboard.id ? SDL_OpenJoystick(g_keyboard.id) : nullptr; 1820 d.name = "Keyboard pad (Leonida)";
1826 if (!g_keyboard.js) { 1821 return SDL_AttachVirtualJoystick(&d);
1822 }
1823 static SDL_Joystick* open(SDL_JoystickID id) { return SDL_OpenJoystick(id); }
1824 static void close(SDL_Joystick* js) { SDL_CloseJoystick(js); }
1825 static bool detach(SDL_JoystickID id) { return SDL_DetachVirtualJoystick(id); }
1826 static bool is_virtual(SDL_JoystickID id) { return SDL_IsJoystickVirtual(id); }
1827 static void removed() { std::fprintf(stderr, "[rt] gameinput: keyboard pad removed\n"); }
1828 static void failed() {
1827 std::fprintf(stderr, "[rt] gameinput: cannot attach the keyboard pad: %s\n", SDL_GetError()); 1829 std::fprintf(stderr, "[rt] gameinput: cannot attach the keyboard pad: %s\n", SDL_GetError());
1828 detach_keyboard_pad();
1829 g_keyboard.failed = true;
1830 return false;
1831 } 1830 }
1832 std::fprintf(stderr, "[rt] gameinput: keyboard pad attached (arrows d-pad, Enter A, Backspace B, X X, Y Y, " 1831 static void attached() {
1833 "Q/E shoulders, Esc Menu, Tab View, WASD left stick, mouse right stick, right/left " 1832 std::fprintf(stderr, "[rt] gameinput: keyboard pad attached (arrows d-pad, Enter A, Backspace B, X X, Y Y, "
1834 "button LT/RT)\n"); 1833 "Q/E shoulders, Esc Menu, Tab View, WASD left stick, mouse right stick, right/left "
1835 start_raw_mouse(); 1834 "button LT/RT)\n");
1836 return true; 1835 start_raw_mouse();
1837} 1836 }
1837};
1838 1838
1839// Before SDL_UpdateGamepads, so the buttons set here are in this poll's reading. 1839// Before SDL_UpdateGamepads, so the buttons set here are in this poll's reading.
1840void update_keyboard_pad() { 1840void update_keyboard_pad() {
@@ -1845,13 +1845,14 @@ void update_keyboard_pad() {
1845 mode = g().config.keyboard_pad; 1845 mode = g().config.keyboard_pad;
1846 sens = g().config.mouse_sensitivity; 1846 sens = g().config.mouse_sensitivity;
1847 } 1847 }
1848 if (mode == rt::gameinput::KeyboardPadMode::Off) { 1848 KeyboardPadBackend backend;
1849 const bool enabled = mode != rt::gameinput::KeyboardPadMode::Off;
1850 if (!enabled) {
1849 clip_cursor(false); 1851 clip_cursor(false);
1850 if (g_keyboard.id) detach_keyboard_pad(); 1852 g_keyboard.right_x = g_keyboard.right_y = 0;
1851 g_keyboard.failed = false; 1853 g_keyboard.have_cursor = false;
1852 return;
1853 } 1854 }
1854 if (!g_keyboard.js && (g_keyboard.failed || !attach_keyboard_pad())) return; 1855 if (!g_keyboard.update(enabled, backend)) return;
1855 1856
1856 bool down[SDL_GAMEPAD_BUTTON_COUNT] = {}; 1857 bool down[SDL_GAMEPAD_BUTTON_COUNT] = {};
1857 int lx = 0, ly = 0; 1858 int lx = 0, ly = 0;
@@ -1901,7 +1902,26 @@ void update_keyboard_pad() {
1901 if (!scripted.empty()) { 1902 if (!scripted.empty()) {
1902 static const ULONGLONG origin = GetTickCount64(); 1903 static const ULONGLONG origin = GetTickCount64();
1903 const double now = (GetTickCount64() - origin) / 1000.0; 1904 const double now = (GetTickCount64() - origin) / 1000.0;
1904 const script::Held held = script::held_at(scripted, now); 1905 script::Held held{};
1906 static constexpr uint64_t kReads = 6;
1907 static constexpr double kMaxHold = 5.0;
1908 static std::vector<uint64_t> began(scripted.presses.size(), 0);
1909 static std::vector<char> started(scripted.presses.size(), 0);
1910 const uint64_t calls = g_reading_calls.load(std::memory_order_relaxed);
1911 for (std::size_t i = 0; i < scripted.presses.size(); ++i) {
1912 const auto& press = scripted.presses[i];
1913 bool active = false;
1914 if (now >= press.at && now < press.at + kMaxHold) {
1915 if (!started[i]) {
1916 started[i] = 1;
1917 began[i] = calls;
1918 }
1919 active = now < press.at + 0.3 || calls - began[i] < kReads;
1920 }
1921 if (!active) continue;
1922 if (press.stick) held.stick = press.stick;
1923 else held.buttons |= press.button;
1924 }
1905 if (held.stick) right_y = held.stick > 0 ? -kStickFull : kStickFull; // SDL's +Y is down 1925 if (held.stick) right_y = held.stick > 0 ? -kStickFull : kStickFull; // SDL's +Y is down
1906 for (const script::Button& b : script::kButtons) { 1926 for (const script::Button& b : script::kButtons) {
1907 if (!(scripted.named & b.bit)) continue; 1927 if (!(scripted.named & b.bit)) continue;
@@ -1959,6 +1979,10 @@ void poll_once() {
1959 SDL_JoystickID* ids = SDL_GetGamepads(&count); 1979 SDL_JoystickID* ids = SDL_GetGamepads(&count);
1960 std::vector<SDL_JoystickID> present(ids, ids + (ids ? count : 0)); 1980 std::vector<SDL_JoystickID> present(ids, ids + (ids ? count : 0));
1961 SDL_free(ids); 1981 SDL_free(ids);
1982 // An unopened virtual device may still be waiting for SDL to finish detaching it.
1983 // Do not advertise it to the title until it has a working keyboard handle.
1984 if (g_keyboard.id && !g_keyboard.js)
1985 present.erase(std::remove(present.begin(), present.end(), g_keyboard.id), present.end());
1962 1986
1963 std::vector<Device*> connected; 1987 std::vector<Device*> connected;
1964 { 1988 {
@@ -2411,7 +2435,75 @@ public:
2411 2435
2412 uint64_t STDMETHODCALLTYPE GetCurrentTimestamp() override { return now_us(); } 2436 uint64_t STDMETHODCALLTYPE GetCurrentTimestamp() override { return now_us(); }
2413 2437
2438 // LEONIDA_INPUT_STATS=1 (diagnostic): every 10 s, how the title asked for readings (by method and device
2439 // argument) and what it got (by the reading's device, and whether a gamepad button was down in it).
2440 static void note_reading(int method, IGameInputDevice* asked, HRESULT hr, IGameInputReading* got) {
2441 static const bool on = env("LEONIDA_INPUT_STATS") == "1";
2442 if (!on) return;
2443 struct Stats {
2444 std::mutex mu;
2445 uint64_t calls[3][3] = {}; // method x (null, aggregate, a device)
2446 uint64_t found[3] = {}, pressed[3] = {};
2447 std::map<std::string, std::pair<uint64_t, uint64_t>> by_device; // name -> (readings, with buttons)
2448 uint64_t since = 0;
2449 };
2450 static Stats st;
2451 std::string name;
2452 bool buttons = false;
2453 if (SUCCEEDED(hr) && got) {
2454 IGameInputDevice* d = nullptr;
2455 got->GetDevice(&d);
2456 if (d) {
2457 name = as_device(d)->name;
2458 d->Release();
2459 }
2460 GameInputGamepadState g{};
2461 if (got->GetGamepadState(&g)) buttons = g.buttons != 0;
2462 }
2463 const int arg = !asked ? 0 : as_device(asked)->info.deviceFamily == GameInputFamilyAggregate ? 1 : 2;
2464 std::lock_guard lock(st.mu);
2465 ++st.calls[method][arg];
2466 if (SUCCEEDED(hr) && got) {
2467 ++st.found[method];
2468 st.pressed[method] += buttons;
2469 auto& e = st.by_device[name];
2470 ++e.first;
2471 e.second += buttons;
2472 }
2473 const uint64_t now = now_us();
2474 if (!st.since) st.since = now;
2475 if (now - st.since < 10'000'000) return;
2476 static const char* methods[] = {"GetCurrentReading", "GetNextReading", "GetPreviousReading"};
2477 std::string line;
2478 for (int m = 0; m < 3; ++m) {
2479 char t[200];
2480 std::snprintf(t, sizeof(t), " %s %llu/%llu/%llu found %llu pressed %llu;", methods[m],
2481 (unsigned long long)st.calls[m][0], (unsigned long long)st.calls[m][1],
2482 (unsigned long long)st.calls[m][2], (unsigned long long)st.found[m],
2483 (unsigned long long)st.pressed[m]);
2484 line += t;
2485 }
2486 for (const auto& [n, c] : st.by_device) {
2487 char t[160];
2488 std::snprintf(t, sizeof(t), " [%s: %llu readings, %llu with buttons]", n.c_str(), (unsigned long long)c.first,
2489 (unsigned long long)c.second);
2490 line += t;
2491 }
2492 std::fprintf(stderr, "[rt] gameinput stats (calls null/aggregate/device): %s\n", line.c_str());
2493 std::memset(st.calls, 0, sizeof(st.calls));
2494 std::memset(st.found, 0, sizeof(st.found));
2495 std::memset(st.pressed, 0, sizeof(st.pressed));
2496 st.by_device.clear();
2497 st.since = now;
2498 }
2499
2414 HRESULT STDMETHODCALLTYPE GetCurrentReading(uint32_t kind, IGameInputDevice* device, IGameInputReading** out) override { 2500 HRESULT STDMETHODCALLTYPE GetCurrentReading(uint32_t kind, IGameInputDevice* device, IGameInputReading** out) override {
2501 g_reading_calls.fetch_add(1, std::memory_order_relaxed);
2502 const HRESULT hr = current_reading(kind, device, out);
2503 note_reading(0, device, hr, out ? *out : nullptr);
2504 return hr;
2505 }
2506 HRESULT current_reading(uint32_t kind, IGameInputDevice* device, IGameInputReading** out) {
2415 if (!out) return E_POINTER; 2507 if (!out) return E_POINTER;
2416 *out = nullptr; 2508 *out = nullptr;
2417 if (kind != GameInputKindUnknown && !(kind & kSupportedKinds)) return GAMEINPUT_E_READING_NOT_FOUND; 2509 if (kind != GameInputKindUnknown && !(kind & kSupportedKinds)) return GAMEINPUT_E_READING_NOT_FOUND;
@@ -2445,11 +2537,15 @@ public:
2445 2537
2446 HRESULT STDMETHODCALLTYPE GetNextReading(IGameInputReading* ref, uint32_t kind, IGameInputDevice* device, 2538 HRESULT STDMETHODCALLTYPE GetNextReading(IGameInputReading* ref, uint32_t kind, IGameInputDevice* device,
2447 IGameInputReading** out) override { 2539 IGameInputReading** out) override {
2448 return find_reading(ref, kind, device, out, true); 2540 const HRESULT hr = find_reading(ref, kind, device, out, true);
2541 note_reading(1, device, hr, out ? *out : nullptr);
2542 return hr;
2449 } 2543 }
2450 HRESULT STDMETHODCALLTYPE GetPreviousReading(IGameInputReading* ref, uint32_t kind, IGameInputDevice* device, 2544 HRESULT STDMETHODCALLTYPE GetPreviousReading(IGameInputReading* ref, uint32_t kind, IGameInputDevice* device,
2451 IGameInputReading** out) override { 2545 IGameInputReading** out) override {
2452 return find_reading(ref, kind, device, out, false); 2546 const HRESULT hr = find_reading(ref, kind, device, out, false);
2547 note_reading(2, device, hr, out ? *out : nullptr);
2548 return hr;
2453 } 2549 }
2454 // E_NOTIMPL is GameInput v0's own answer: Microsoft's reference page says the method "was never implemented in 2550 // E_NOTIMPL is GameInput v0's own answer: Microsoft's reference page says the method "was never implemented in
2455 // v0" and returns E_NOTIMPL, and v1 removed it. A history lookup here would give a title what no GameInput does. 2551 // v0" and returns E_NOTIMPL, and v1 removed it. A history lookup here would give a title what no GameInput does.