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:
authorICUMADETHIS <icumadethis@gmail.com>2026-11-22 21:15:21 +0000
committerICUMADETHIS <icumadethis@gmail.com>2026-11-22 21:17:56 +0000
commit1fc162f4bab039a6de2254864b445804b782efa3 (patch)
tree9e718a96ace450b0a3442960ae519e56c2d3f105 /runtime/shims/gameinput.cpp
parentf847748cc76bd3777f9c070c4034eb2fc1c04e64 (diff)
gameinput: track which kind of device the player used last
The shim tells the title every pad is an Xbox One pad, so nothing could say what the player is really holding. rt::gameinput::active_input() now returns the family of the device used last (Xbox, PlayStation 5, PlayStation 4, PlayStation 3, Nintendo, or the keyboard pad), a generation that counts changes, and whether LEONIDA_PROMPTS forced it. It is groundwork for button prompts that follow the device in use. Only deliberate input counts: a button going down, a trigger past 0.3 or a stick leaving a 0.35 radius. Held input, stick drift and noise do not; a pad's first reading after it connects is its baseline; and after a change the next one waits LEONIDA_PROMPTS_SWITCH_MS (300 ms). The rules live in runtime/src/active_input.h with no SDL, tested by active_input_test; gameinput_test drives them on SDL virtual pads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 89b66364d..b6dd6b00d 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
@@ -169,6 +171,19 @@ rt::gameinput::Config read_config() {
169 else if (!k.empty() && k != "0" && k != "off") 171 else if (!k.empty() && k != "0" && k != "off")
170 std::fprintf(stderr, "[rt] gameinput: LEONIDA_KEYBOARD_PAD=%s unknown (1, always or off), keyboard pad off\n", 172 std::fprintf(stderr, "[rt] gameinput: LEONIDA_KEYBOARD_PAD=%s unknown (1, always or off), keyboard pad off\n",
171 k.c_str()); 173 k.c_str());
174 using leonida::InputFamily;
175 const std::string p = env("LEONIDA_PROMPTS");
176 if (p == "xbox") c.prompts = InputFamily::Xbox;
177 else if (p == "playstation" || p == "ps5") c.prompts = InputFamily::PlayStation5;
178 else if (p == "ps4") c.prompts = InputFamily::PlayStation4;
179 else if (p == "ps3") c.prompts = InputFamily::PlayStation3;
180 else if (p == "nintendo" || p == "switch") c.prompts = InputFamily::Nintendo;
181 else if (p == "keyboard") c.prompts = InputFamily::Keyboard;
182 else if (!p.empty() && p != "auto")
183 std::fprintf(stderr, "[rt] gameinput: LEONIDA_PROMPTS=%s unknown (auto, xbox, playstation, ps5, ps4, ps3, "
184 "nintendo or keyboard), following the devices\n", p.c_str());
185 if (const std::string m = env("LEONIDA_PROMPTS_SWITCH_MS"); !m.empty())
186 c.prompts_switch_ms = static_cast<uint32_t>(std::clamp(std::atoi(m.c_str()), 0, 2000));
172 return c; 187 return c;
173} 188}
174 189
@@ -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;
@@ -738,6 +754,79 @@ float axis(SDL_Gamepad* p, SDL_GamepadAxis a) { return std::max(-1.0f, SDL_GetGa
738 754
739float norm(float v, float max) { return std::clamp(v / max, -1.0f, 1.0f); } 755float norm(float v, float max) { return std::clamp(v / max, -1.0f, 1.0f); }
740 756
757bool is_keyboard_pad(SDL_JoystickID jid); // with the keyboard pad below
758
759// What kind of device a pad really is, for active_input(): SDL's gamepad type, which it takes from the vendor and
760// product id. Anything SDL does not place (a generic or unknown pad, a GameCube adapter) counts as Xbox, the layout
761// GameInput itself describes.
762leonida::InputFamily family_of(SDL_JoystickID jid, SDL_Gamepad* p) {
763 using leonida::InputFamily;
764 if (is_keyboard_pad(jid)) return InputFamily::Keyboard;
765 switch (SDL_GetGamepadType(p)) {
766 case SDL_GAMEPAD_TYPE_PS5: return InputFamily::PlayStation5;
767 case SDL_GAMEPAD_TYPE_PS4: return InputFamily::PlayStation4;
768 case SDL_GAMEPAD_TYPE_PS3: return InputFamily::PlayStation3;
769 case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO:
770 case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT:
771 case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT:
772 case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR: return InputFamily::Nintendo;
773 default: return InputFamily::Xbox;
774 }
775}
776
777// ---- active input (src/active_input.h) ----
778//
779// The tracker is poll-thread state, under poll_mu like the rest of the poll. What it answers is published in one word,
780// so active_input() needs no lock and never sees a family from one change with the generation of another: the family
781// in bits 0-7, forced in bit 8, the generation from bit 16.
782leonida::ActiveInputTracker g_tracker;
783std::atomic<uint64_t> g_active{0};
784
785void publish_active() {
786 g_active.store(static_cast<uint64_t>(g_tracker.active()) | (g_tracker.forced() ? 1ull << 8 : 0) |
787 (g_tracker.generation() << 16),
788 std::memory_order_release);
789}
790
791leonida::InputSample sample_of(const Snapshot& s) {
792 static_assert(kButtonCount <= 32, "every SDL gamepad button needs a bit");
793 leonida::InputSample out;
794 for (uint32_t i = 0; i < kButtonCount; ++i) // all of SDL's buttons: guide, touchpad and paddles included
795 if (s.buttons[i]) out.buttons |= 1u << i;
796 out.left_trigger = s.gamepad.leftTrigger;
797 out.right_trigger = s.gamepad.rightTrigger;
798 out.left_x = s.gamepad.leftThumbstickX;
799 out.left_y = s.gamepad.leftThumbstickY;
800 out.right_x = s.gamepad.rightThumbstickX;
801 out.right_y = s.gamepad.rightThumbstickY;
802 return out;
803}
804
805// LEONIDA_PROMPTS and LEONIDA_PROMPTS_SWITCH_MS, applied at each poll: reload_config can change them at any time.
806void apply_prompt_config() {
807 std::optional<leonida::InputFamily> want;
808 uint32_t switch_ms;
809 {
810 std::lock_guard lock(g().mu);
811 want = g().config.prompts;
812 switch_ms = g().config.prompts_switch_ms;
813 }
814 leonida::ActiveInputTracker::Thresholds t = g_tracker.thresholds();
815 t.min_switch_us = uint64_t{switch_ms} * 1000;
816 g_tracker.set_thresholds(t);
817 const std::optional<leonida::InputFamily> now =
818 g_tracker.forced() ? std::optional<leonida::InputFamily>(g_tracker.active()) : std::nullopt;
819 if (want == now) return;
820 g_tracker.force(want);
821 publish_active();
822 if (want)
823 std::fprintf(stderr, "[rt] gameinput: LEONIDA_PROMPTS: input family set to %s\n",
824 leonida::input_family_name(*want));
825 else
826 std::fprintf(stderr, "[rt] gameinput: LEONIDA_PROMPTS cleared: input family follows the devices (%s now)\n",
827 leonida::input_family_name(g_tracker.active()));
828}
829
741Snapshot read_pad(Device* d) { 830Snapshot read_pad(Device* d) {
742 SDL_Gamepad* p = d->pad; 831 SDL_Gamepad* p = d->pad;
743 Snapshot s; 832 Snapshot s;
@@ -972,6 +1061,7 @@ void open_pad(SDL_JoystickID jid, uint64_t ts) {
972 } 1061 }
973 d->pad = p; 1062 d->pad = p;
974 d->id = jid; 1063 d->id = jid;
1064 d->family = family_of(jid, p);
975 d->is_dualsense = SDL_GetGamepadType(p) == SDL_GAMEPAD_TYPE_PS5; 1065 d->is_dualsense = SDL_GetGamepadType(p) == SDL_GAMEPAD_TYPE_PS5;
976 d->has_rumble = SDL_GetBooleanProperty(props, SDL_PROP_GAMEPAD_CAP_RUMBLE_BOOLEAN, false); 1066 d->has_rumble = SDL_GetBooleanProperty(props, SDL_PROP_GAMEPAD_CAP_RUMBLE_BOOLEAN, false);
977 d->has_trigger_rumble = SDL_GetBooleanProperty(props, SDL_PROP_GAMEPAD_CAP_TRIGGER_RUMBLE_BOOLEAN, false); 1067 d->has_trigger_rumble = SDL_GetBooleanProperty(props, SDL_PROP_GAMEPAD_CAP_TRIGGER_RUMBLE_BOOLEAN, false);
@@ -1070,6 +1160,7 @@ void open_pad(SDL_JoystickID jid, uint64_t ts) {
1070 1160
1071void close_pad(Device* d, uint64_t ts) { 1161void close_pad(Device* d, uint64_t ts) {
1072 State& s = g(); 1162 State& s = g();
1163 g_tracker.forget(d->id); // poll-thread state: a pad that comes back starts from a fresh baseline
1073 std::lock_guard lock(s.mu); 1164 std::lock_guard lock(s.mu);
1074 const uint32_t old = d->status.exchange(GameInputDeviceNoStatus); 1165 const uint32_t old = d->status.exchange(GameInputDeviceNoStatus);
1075 SDL_CloseGamepad(d->pad); 1166 SDL_CloseGamepad(d->pad);
@@ -1362,6 +1453,8 @@ struct KeyboardPad {
1362}; 1453};
1363KeyboardPad g_keyboard; 1454KeyboardPad g_keyboard;
1364 1455
1456bool is_keyboard_pad(SDL_JoystickID jid) { return jid != 0 && jid == g_keyboard.id; }
1457
1365void detach_keyboard_pad() { 1458void detach_keyboard_pad() {
1366 if (g_keyboard.js) SDL_CloseJoystick(g_keyboard.js); 1459 if (g_keyboard.js) SDL_CloseJoystick(g_keyboard.js);
1367 if (g_keyboard.id) SDL_DetachVirtualJoystick(g_keyboard.id); 1460 if (g_keyboard.id) SDL_DetachVirtualJoystick(g_keyboard.id);
@@ -1508,6 +1601,7 @@ void poll_once() {
1508 State& s = g(); 1601 State& s = g();
1509 std::lock_guard pl(s.poll_mu); 1602 std::lock_guard pl(s.poll_mu);
1510 if (!init_sdl()) return; 1603 if (!init_sdl()) return;
1604 apply_prompt_config();
1511 update_keyboard_pad(); 1605 update_keyboard_pad();
1512 SDL_UpdateGamepads(); 1606 SDL_UpdateGamepads();
1513 const uint64_t now = now_us(); 1607 const uint64_t now = now_us();
@@ -1543,6 +1637,15 @@ void poll_once() {
1543 const bool guide_changed = d->last_snap.guide != snap.guide; 1637 const bool guide_changed = d->last_snap.guide != snap.guide;
1544 const bool changed = !(d->last_snap == snap); 1638 const bool changed = !(d->last_snap == snap);
1545 d->last_snap = snap; 1639 d->last_snap = snap;
1640 // A newly opened pad's first sample here is its baseline (active_input.h), so connecting a pad with a button
1641 // held changes nothing.
1642 if (const leonida::InputEdge edge = g_tracker.observe(d->id, d->family, sample_of(snap), now);
1643 edge != leonida::InputEdge::None) {
1644 publish_active();
1645 if (!g_tracker.forced())
1646 std::fprintf(stderr, "[rt] gameinput: input in use: %s (%s, %s)\n",
1647 leonida::input_family_name(d->family), d->name.c_str(), leonida::input_edge_name(edge));
1648 }
1546 // A pad with sensors streams readings at ~125 Hz even when nothing else changes, so motion the title reads 1649 // A pad with sensors streams readings at ~125 Hz even when nothing else changes, so motion the title reads
1547 // between its frames is never stuck at the last button event; pads without sensors only read on change, as 1650 // between its frames is never stuck at the last button event; pads without sensors only read on change, as
1548 // before. push_reading stamps last_reading_us, so the cadence follows the actual history. 1651 // before. push_reading stamps last_reading_us, so the cadence follows the actual history.
@@ -2172,6 +2275,11 @@ int connected_count() {
2172 return static_cast<int>(g().connected.size()); 2275 return static_cast<int>(g().connected.size());
2173} 2276}
2174 2277
2278ActiveInput active_input() {
2279 const uint64_t v = g_active.load(std::memory_order_acquire);
2280 return {static_cast<leonida::InputFamily>(v & 0xFF), v >> 16, ((v >> 8) & 1) != 0};
2281}
2282
2175bool output_state(int i, OutputState* out) { 2283bool output_state(int i, OutputState* out) {
2176 std::lock_guard lock(g().mu); 2284 std::lock_guard lock(g().mu);
2177 if (i < 0 || i >= static_cast<int>(g().connected.size()) || !out) return false; 2285 if (i < 0 || i >= static_cast<int>(g().connected.size()) || !out) return false;