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:
authorNolan Cotter <186326284+NolanCotter@users.noreply.github.com>2026-11-22 20:39:59 -0700
committerNolan Cotter <186326284+NolanCotter@users.noreply.github.com>2026-11-22 20:39:59 -0700
commitfa5f26db8eafd2cc27f627b68c0a5a5949bcf2ff (patch)
treefc57e238be829295fc216dff89a6d93e64c16b50 /runtime/shims/gameinput.cpp
parent9b693ecd8a2f90799f6c47f8f06cf062feb9820c (diff)
parent7b7d73f890f9fbbf5967bbb694390bbc99e33ba6 (diff)
Merge origin/main: DXGI device fields coexist with the console-ABI device state
Diffstat (limited to 'runtime/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index 69707f75a..2bc486042 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -16,6 +16,8 @@
16// SDL_CreateHapticEffect/RunHapticEffect (IGameInputForceFeedbackEffect); haptic trigger motors, reported from the 16// SDL_CreateHapticEffect/RunHapticEffect (IGameInputForceFeedbackEffect); haptic trigger motors, reported from the
17// trigger motors the pad already has, played as an intensity through the same trigger output as SetRumbleState 17// trigger motors the pad already has, played as an intensity through the same trigger output as SetRumbleState
18// - the keyboard pad (LEONIDA_KEYBOARD_PAD): one SDL virtual gamepad driven by the keyboard and mouse 18// - the keyboard pad (LEONIDA_KEYBOARD_PAD): one SDL virtual gamepad driven by the keyboard and mouse
19// - which kind of device the player used last (rt::gameinput::active_input, LEONIDA_PROMPTS), for the runtime's
20// own use: the title is told every pad is an Xbox One pad
19// 21//
20// One thread owns SDL: the background poll thread by default, or the caller of rt::gameinput::poll() when 22// One thread owns SDL: the background poll thread by default, or the caller of rt::gameinput::poll() when
21// LEONIDA_GAMEINPUT_THREAD=0. Title calls never touch SDL: they read the latest snapshot and leave rumble requests 23// LEONIDA_GAMEINPUT_THREAD=0. Title calls never touch SDL: they read the latest snapshot and leave rumble requests
@@ -170,6 +172,19 @@ rt::gameinput::Config read_config() {
170 else if (!k.empty() && k != "0" && k != "off") 172 else if (!k.empty() && k != "0" && k != "off")
171 std::fprintf(stderr, "[rt] gameinput: LEONIDA_KEYBOARD_PAD=%s unknown (1, always or off), keyboard pad off\n", 173 std::fprintf(stderr, "[rt] gameinput: LEONIDA_KEYBOARD_PAD=%s unknown (1, always or off), keyboard pad off\n",
172 k.c_str()); 174 k.c_str());
175 using leonida::InputFamily;
176 const std::string p = env("LEONIDA_PROMPTS");
177 if (p == "xbox") c.prompts = InputFamily::Xbox;
178 else if (p == "playstation" || p == "ps5") c.prompts = InputFamily::PlayStation5;
179 else if (p == "ps4") c.prompts = InputFamily::PlayStation4;
180 else if (p == "ps3") c.prompts = InputFamily::PlayStation3;
181 else if (p == "nintendo" || p == "switch") c.prompts = InputFamily::Nintendo;
182 else if (p == "keyboard") c.prompts = InputFamily::Keyboard;
183 else if (!p.empty() && p != "auto")
184 std::fprintf(stderr, "[rt] gameinput: LEONIDA_PROMPTS=%s unknown (auto, xbox, playstation, ps5, ps4, ps3, "
185 "nintendo or keyboard), following the devices\n", p.c_str());
186 if (const std::string m = env("LEONIDA_PROMPTS_SWITCH_MS"); !m.empty())
187 c.prompts_switch_ms = static_cast<uint32_t>(std::clamp(std::atoi(m.c_str()), 0, 2000));
173 return c; 188 return c;
174} 189}
175 190
@@ -508,6 +523,7 @@ public:
508 int sent_gain = -1; // device gain 0..100 last sent, so a steady gain costs one call 523 int sent_gain = -1; // device gain 0..100 last sent, so a steady gain costs one call
509 uint32_t haptic_motors = 0; // trigger haptic motors the pad reports (0 or 2) 524 uint32_t haptic_motors = 0; // trigger haptic motors the pad reports (0 or 2)
510 rt::gameinput::OutputState sent{}; 525 rt::gameinput::OutputState sent{};
526 leonida::InputFamily family = leonida::InputFamily::Xbox; // what kind of device it really is (active_input)
511 Snapshot last_snap{}; // what the latest reading holds 527 Snapshot last_snap{}; // what the latest reading holds
512 uint64_t last_reading_us = 0; // for the motion cadence: readings stream at ~125 Hz on pads with sensors 528 uint64_t last_reading_us = 0; // for the motion cadence: readings stream at ~125 Hz on pads with sensors
513 uint64_t motors_sent_at = 0, triggers_sent_at = 0, battery_at = 0; 529 uint64_t motors_sent_at = 0, triggers_sent_at = 0, battery_at = 0;
@@ -923,6 +939,79 @@ float axis(SDL_Gamepad* p, SDL_GamepadAxis a) { return std::max(-1.0f, SDL_GetGa
923 939
924float norm(float v, float max) { return std::clamp(v / max, -1.0f, 1.0f); } 940float norm(float v, float max) { return std::clamp(v / max, -1.0f, 1.0f); }
925 941
942bool is_keyboard_pad(SDL_JoystickID jid); // with the keyboard pad below
943
944// What kind of device a pad really is, for active_input(): SDL's gamepad type, which it takes from the vendor and
945// product id. Anything SDL does not place (a generic or unknown pad, a GameCube adapter) counts as Xbox, the layout
946// GameInput itself describes.
947leonida::InputFamily family_of(SDL_JoystickID jid, SDL_Gamepad* p) {
948 using leonida::InputFamily;
949 if (is_keyboard_pad(jid)) return InputFamily::Keyboard;
950 switch (SDL_GetGamepadType(p)) {
951 case SDL_GAMEPAD_TYPE_PS5: return InputFamily::PlayStation5;
952 case SDL_GAMEPAD_TYPE_PS4: return InputFamily::PlayStation4;
953 case SDL_GAMEPAD_TYPE_PS3: return InputFamily::PlayStation3;
954 case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO:
955 case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT:
956 case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT:
957 case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR: return InputFamily::Nintendo;
958 default: return InputFamily::Xbox;
959 }
960}
961
962// ---- active input (src/active_input.h) ----
963//
964// The tracker is poll-thread state, under poll_mu like the rest of the poll. What it answers is published in one word,
965// so active_input() needs no lock and never sees a family from one change with the generation of another: the family
966// in bits 0-7, forced in bit 8, the generation from bit 16.
967leonida::ActiveInputTracker g_tracker;
968std::atomic<uint64_t> g_active{0};
969
970void publish_active() {
971 g_active.store(static_cast<uint64_t>(g_tracker.active()) | (g_tracker.forced() ? 1ull << 8 : 0) |
972 (g_tracker.generation() << 16),
973 std::memory_order_release);
974}
975
976leonida::InputSample sample_of(const Snapshot& s) {
977 static_assert(kButtonCount <= 32, "every SDL gamepad button needs a bit");
978 leonida::InputSample out;
979 for (uint32_t i = 0; i < kButtonCount; ++i) // all of SDL's buttons: guide, touchpad and paddles included
980 if (s.buttons[i]) out.buttons |= 1u << i;
981 out.left_trigger = s.gamepad.leftTrigger;
982 out.right_trigger = s.gamepad.rightTrigger;
983 out.left_x = s.gamepad.leftThumbstickX;
984 out.left_y = s.gamepad.leftThumbstickY;
985 out.right_x = s.gamepad.rightThumbstickX;
986 out.right_y = s.gamepad.rightThumbstickY;
987 return out;
988}
989
990// LEONIDA_PROMPTS and LEONIDA_PROMPTS_SWITCH_MS, applied at each poll: reload_config can change them at any time.
991void apply_prompt_config() {
992 std::optional<leonida::InputFamily> want;
993 uint32_t switch_ms;
994 {
995 std::lock_guard lock(g().mu);
996 want = g().config.prompts;
997 switch_ms = g().config.prompts_switch_ms;
998 }
999 leonida::ActiveInputTracker::Thresholds t = g_tracker.thresholds();
1000 t.min_switch_us = uint64_t{switch_ms} * 1000;
1001 g_tracker.set_thresholds(t);
1002 const std::optional<leonida::InputFamily> now =
1003 g_tracker.forced() ? std::optional<leonida::InputFamily>(g_tracker.active()) : std::nullopt;
1004 if (want == now) return;
1005 g_tracker.force(want);
1006 publish_active();
1007 if (want)
1008 std::fprintf(stderr, "[rt] gameinput: LEONIDA_PROMPTS: input family set to %s\n",
1009 leonida::input_family_name(*want));
1010 else
1011 std::fprintf(stderr, "[rt] gameinput: LEONIDA_PROMPTS cleared: input family follows the devices (%s now)\n",
1012 leonida::input_family_name(g_tracker.active()));
1013}
1014
926Snapshot read_pad(Device* d) { 1015Snapshot read_pad(Device* d) {
927 SDL_Gamepad* p = d->pad; 1016 SDL_Gamepad* p = d->pad;
928 Snapshot s; 1017 Snapshot s;
@@ -1243,6 +1332,7 @@ void open_pad(SDL_JoystickID jid, uint64_t ts) {
1243 } 1332 }
1244 d->pad = p; 1333 d->pad = p;
1245 d->id = jid; 1334 d->id = jid;
1335 d->family = family_of(jid, p);
1246 d->is_dualsense = SDL_GetGamepadType(p) == SDL_GAMEPAD_TYPE_PS5; 1336 d->is_dualsense = SDL_GetGamepadType(p) == SDL_GAMEPAD_TYPE_PS5;
1247 d->has_rumble = SDL_GetBooleanProperty(props, SDL_PROP_GAMEPAD_CAP_RUMBLE_BOOLEAN, false); 1337 d->has_rumble = SDL_GetBooleanProperty(props, SDL_PROP_GAMEPAD_CAP_RUMBLE_BOOLEAN, false);
1248 d->has_trigger_rumble = SDL_GetBooleanProperty(props, SDL_PROP_GAMEPAD_CAP_TRIGGER_RUMBLE_BOOLEAN, false); 1338 d->has_trigger_rumble = SDL_GetBooleanProperty(props, SDL_PROP_GAMEPAD_CAP_TRIGGER_RUMBLE_BOOLEAN, false);
@@ -1343,6 +1433,7 @@ void open_pad(SDL_JoystickID jid, uint64_t ts) {
1343 1433
1344void close_pad(Device* d, uint64_t ts) { 1434void close_pad(Device* d, uint64_t ts) {
1345 State& s = g(); 1435 State& s = g();
1436 g_tracker.forget(d->id); // poll-thread state: a pad that comes back starts from a fresh baseline
1346 std::lock_guard lock(s.mu); 1437 std::lock_guard lock(s.mu);
1347 const uint32_t old = d->status.exchange(GameInputDeviceNoStatus); 1438 const uint32_t old = d->status.exchange(GameInputDeviceNoStatus);
1348 SDL_CloseGamepad(d->pad); 1439 SDL_CloseGamepad(d->pad);
@@ -1636,6 +1727,8 @@ struct KeyboardPad {
1636}; 1727};
1637KeyboardPad g_keyboard; 1728KeyboardPad g_keyboard;
1638 1729
1730bool is_keyboard_pad(SDL_JoystickID jid) { return jid != 0 && jid == g_keyboard.id; }
1731
1639void detach_keyboard_pad() { 1732void detach_keyboard_pad() {
1640 if (g_keyboard.js) SDL_CloseJoystick(g_keyboard.js); 1733 if (g_keyboard.js) SDL_CloseJoystick(g_keyboard.js);
1641 if (g_keyboard.id) SDL_DetachVirtualJoystick(g_keyboard.id); 1734 if (g_keyboard.id) SDL_DetachVirtualJoystick(g_keyboard.id);
@@ -1782,6 +1875,7 @@ void poll_once() {
1782 State& s = g(); 1875 State& s = g();
1783 std::lock_guard pl(s.poll_mu); 1876 std::lock_guard pl(s.poll_mu);
1784 if (!init_sdl()) return; 1877 if (!init_sdl()) return;
1878 apply_prompt_config();
1785 update_keyboard_pad(); 1879 update_keyboard_pad();
1786 SDL_UpdateGamepads(); 1880 SDL_UpdateGamepads();
1787 const uint64_t now = now_us(); 1881 const uint64_t now = now_us();
@@ -1818,6 +1912,15 @@ void poll_once() {
1818 const bool guide_changed = d->last_snap.guide != snap.guide; 1912 const bool guide_changed = d->last_snap.guide != snap.guide;
1819 const bool changed = !(d->last_snap == snap); 1913 const bool changed = !(d->last_snap == snap);
1820 d->last_snap = snap; 1914 d->last_snap = snap;
1915 // A newly opened pad's first sample here is its baseline (active_input.h), so connecting a pad with a button
1916 // held changes nothing.
1917 if (const leonida::InputEdge edge = g_tracker.observe(d->id, d->family, sample_of(snap), now);
1918 edge != leonida::InputEdge::None) {
1919 publish_active();
1920 if (!g_tracker.forced())
1921 std::fprintf(stderr, "[rt] gameinput: input in use: %s (%s, %s)\n",
1922 leonida::input_family_name(d->family), d->name.c_str(), leonida::input_edge_name(edge));
1923 }
1821 // A pad with sensors streams readings at ~125 Hz even when nothing else changes, so motion the title reads 1924 // A pad with sensors streams readings at ~125 Hz even when nothing else changes, so motion the title reads
1822 // between its frames is never stuck at the last button event; pads without sensors only read on change, as 1925 // between its frames is never stuck at the last button event; pads without sensors only read on change, as
1823 // before. push_reading stamps last_reading_us, so the cadence follows the actual history. 1926 // before. push_reading stamps last_reading_us, so the cadence follows the actual history.
@@ -2447,6 +2550,11 @@ int connected_count() {
2447 return static_cast<int>(g().connected.size()); 2550 return static_cast<int>(g().connected.size());
2448} 2551}
2449 2552
2553ActiveInput active_input() {
2554 const uint64_t v = g_active.load(std::memory_order_acquire);
2555 return {static_cast<leonida::InputFamily>(v & 0xFF), v >> 16, ((v >> 8) & 1) != 0};
2556}
2557
2450bool output_state(int i, OutputState* out) { 2558bool output_state(int i, OutputState* out) {
2451 std::lock_guard lock(g().mu); 2559 std::lock_guard lock(g().mu);
2452 if (i < 0 || i >= static_cast<int>(g().connected.size()) || !out) return false; 2560 if (i < 0 || i >= static_cast<int>(g().connected.size()) || !out) return false;