upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/active_input.h
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 22:41:03 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 22:41:03 -0500
commit2d3369a4c82e6f36c5190c4c534d3df600e135ec (patch)
tree645518e91fee1ab6299170a3251c035495000c75 /runtime/src/active_input.h
parentdb51b7d3f999ab57898cf039f8289d807b63523c (diff)
parent7b7d73f890f9fbbf5967bbb694390bbc99e33ba6 (diff)
Merge remote-tracking branch 'origin/main'
Diffstat (limited to 'runtime/src/active_input.h')
-rw-r--r--runtime/src/active_input.h146
1 files changed, 146 insertions, 0 deletions
diff --git a/runtime/src/active_input.h b/runtime/src/active_input.h
new file mode 100644
index 000000000..c0f7451b1
--- /dev/null
+++ b/runtime/src/active_input.h
@@ -0,0 +1,146 @@
1#pragma once
2
3// Which kind of input device the player used last: an Xbox-style pad, a PlayStation pad, a Nintendo pad or the
4// keyboard pad (docs/gameinput.md). The GameInput shim reports every pad to the title as an Xbox One pad, so the title
5// itself cannot tell; this is the port's own record, for whatever wants to follow the device in use (button prompt
6// glyphs first). No SDL, no Windows headers: the shim feeds it one sample per device per poll, and tests feed it
7// synthetic samples with their own times.
8//
9// The rules, chosen so that only deliberate input changes the answer:
10// * Only an edge counts: a button going down, a trigger rising past `trigger`, a stick leaving the `stick` radius.
11// A button held down, a trigger held, a stick held over, stick drift that stays inside the radius and analog
12// noise never count, so a pad lying on the desk cannot take over. The keyboard pad's right stick follows the
13// mouse, so a real mouse movement counts as keyboard input through the same stick rule.
14// * A device's first sample is its baseline and counts for nothing: a pad that connects with a button held or a
15// worn stick resting past the radius does not switch anything until it produces a new edge.
16// * After a change, another change waits until `min_switch_us` has passed. An edge from another family inside that
17// window is ignored (not queued), so two devices used at once cannot make the answer flicker.
18// * Edges from a device of the family already active change nothing.
19// * Before any edge the answer is `initial` (Xbox unless the caller says otherwise).
20// * A forced family (LEONIDA_PROMPTS) wins over all of this; the tracking still runs underneath, so clearing the
21// force goes straight back to the device in use.
22// `generation` counts changes of the answer (tracked or forced), so a reader can tell that something changed without
23// comparing families.
24
25#include <cmath>
26#include <cstdint>
27#include <optional>
28#include <unordered_map>
29
30namespace leonida {
31
32enum class InputFamily : std::uint8_t {
33 Xbox, // Xbox One/Series, Xbox 360, and any pad SDL does not know better (generic, unknown)
34 PlayStation5, // DualSense, DualSense Edge
35 PlayStation4, // DualShock 4
36 PlayStation3, // DualShock 3
37 Nintendo, // Switch Pro controller and Joy-Cons
38 Keyboard, // the keyboard pad: keyboard and mouse
39};
40
41inline const char* input_family_name(InputFamily f) {
42 switch (f) {
43 case InputFamily::Xbox: return "Xbox";
44 case InputFamily::PlayStation5: return "PlayStation 5";
45 case InputFamily::PlayStation4: return "PlayStation 4";
46 case InputFamily::PlayStation3: return "PlayStation 3";
47 case InputFamily::Nintendo: return "Nintendo";
48 case InputFamily::Keyboard: return "keyboard and mouse";
49 }
50 return "unknown";
51}
52
53// One device's state at one poll, in GameInput's units: sticks -1..1, triggers 0..1. `buttons` is any bit set per
54// pressed button (the shim passes the GameInput gamepad bits plus one bit for the guide button); only its changes
55// matter.
56struct InputSample {
57 std::uint32_t buttons = 0;
58 float left_trigger = 0, right_trigger = 0;
59 float left_x = 0, left_y = 0, right_x = 0, right_y = 0;
60};
61
62// What made the answer change, for the log line.
63enum class InputEdge : std::uint8_t { None, Button, Trigger, Stick };
64
65inline const char* input_edge_name(InputEdge e) {
66 switch (e) {
67 case InputEdge::None: return "none";
68 case InputEdge::Button: return "button press";
69 case InputEdge::Trigger: return "trigger pull";
70 case InputEdge::Stick: return "stick movement";
71 }
72 return "unknown";
73}
74
75class ActiveInputTracker {
76public:
77 struct Thresholds {
78 float stick = 0.35f; // radius a stick has to leave
79 float trigger = 0.3f; // a trigger has to rise past this
80 std::uint64_t min_switch_us = 300'000; // shortest time between two changes
81 };
82
83 explicit ActiveInputTracker(InputFamily initial = InputFamily::Xbox) : tracked_(initial) {}
84 ActiveInputTracker(InputFamily initial, Thresholds t) : thresholds_(t), tracked_(initial) {}
85
86 // One sample of one device (`device` is any key that stays the same while the device is connected). Returns the
87 // edge that changed the tracked family, or None when it did not change. With a family forced, the tracked family
88 // still follows the devices, but active() does not move.
89 InputEdge observe(std::uint64_t device, InputFamily family, const InputSample& now, std::uint64_t time_us) {
90 auto it = devices_.find(device);
91 if (it == devices_.end()) {
92 devices_.emplace(device, now); // the baseline: counts for nothing
93 return InputEdge::None;
94 }
95 const InputEdge edge = edge_between(it->second, now);
96 it->second = now;
97 if (edge == InputEdge::None || family == tracked_) return InputEdge::None;
98 if (changed_once_ && time_us - changed_at_ < thresholds_.min_switch_us) return InputEdge::None;
99 const InputFamily before = active();
100 tracked_ = family;
101 changed_at_ = time_us;
102 changed_once_ = true;
103 if (active() != before) ++generation_;
104 return edge;
105 }
106
107 // The device disconnected: its next sample, if it comes back, is a baseline again.
108 void forget(std::uint64_t device) { devices_.erase(device); }
109
110 // Forces the answer (a family) or returns to tracking (nullopt). Changing what active() returns bumps generation.
111 void force(std::optional<InputFamily> family) {
112 const InputFamily before = active();
113 forced_ = family;
114 if (active() != before) ++generation_;
115 }
116
117 InputFamily active() const { return forced_ ? *forced_ : tracked_; }
118 InputFamily tracked() const { return tracked_; }
119 bool forced() const { return forced_.has_value(); }
120 std::uint64_t generation() const { return generation_; }
121 const Thresholds& thresholds() const { return thresholds_; }
122 void set_thresholds(const Thresholds& t) { thresholds_ = t; }
123
124private:
125 InputEdge edge_between(const InputSample& was, const InputSample& now) const {
126 if (now.buttons & ~was.buttons) return InputEdge::Button;
127 const float t = thresholds_.trigger;
128 if ((was.left_trigger <= t && now.left_trigger > t) || (was.right_trigger <= t && now.right_trigger > t))
129 return InputEdge::Trigger;
130 const auto out = [this](float x, float y) { return std::hypot(x, y) > thresholds_.stick; };
131 if ((!out(was.left_x, was.left_y) && out(now.left_x, now.left_y)) ||
132 (!out(was.right_x, was.right_y) && out(now.right_x, now.right_y)))
133 return InputEdge::Stick;
134 return InputEdge::None;
135 }
136
137 Thresholds thresholds_{};
138 InputFamily tracked_;
139 std::optional<InputFamily> forced_;
140 std::uint64_t generation_ = 0;
141 std::uint64_t changed_at_ = 0;
142 bool changed_once_ = false;
143 std::unordered_map<std::uint64_t, InputSample> devices_;
144};
145
146} // namespace leonida