upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/active_input_test.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/test/active_input_test.cpp
parent9b693ecd8a2f90799f6c47f8f06cf062feb9820c (diff)
parent7b7d73f890f9fbbf5967bbb694390bbc99e33ba6 (diff)
Merge origin/main: DXGI device fields coexist with the console-ABI device state
Diffstat (limited to 'runtime/test/active_input_test.cpp')
-rw-r--r--runtime/test/active_input_test.cpp236
1 files changed, 236 insertions, 0 deletions
diff --git a/runtime/test/active_input_test.cpp b/runtime/test/active_input_test.cpp
new file mode 100644
index 000000000..11c628320
--- /dev/null
+++ b/runtime/test/active_input_test.cpp
@@ -0,0 +1,236 @@
1// ActiveInputTracker (src/active_input.h) on synthetic samples with its own clock: what counts as deliberate input
2// (edges only, thresholds, baselines), the minimum time between changes, several devices of one family, a device that
3// goes and comes back, and a forced family. No SDL, no controller, no GPU.
4#include "../src/active_input.h"
5
6#include <cstdio>
7#include <iterator>
8#include <string_view>
9
10using namespace leonida;
11
12#define EXPECT(condition) do { \
13 if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \
14} while (0)
15
16namespace {
17
18constexpr std::uint64_t ms = 1000; // the tracker's clock is in microseconds
19
20InputSample rest() { return {}; }
21
22InputSample button(std::uint32_t bits) {
23 InputSample s;
24 s.buttons = bits;
25 return s;
26}
27
28InputSample left_stick(float x, float y) {
29 InputSample s;
30 s.left_x = x;
31 s.left_y = y;
32 return s;
33}
34
35InputSample right_stick(float x, float y) {
36 InputSample s;
37 s.right_x = x;
38 s.right_y = y;
39 return s;
40}
41
42InputSample triggers(float left, float right) {
43 InputSample s;
44 s.left_trigger = left;
45 s.right_trigger = right;
46 return s;
47}
48
49// Device keys and families used below.
50constexpr std::uint64_t xbox = 1, dualsense = 2, ds4 = 3, keyboard = 4, xbox2 = 5, switch_pro = 6;
51
52int test_start_and_baseline() {
53 ActiveInputTracker t;
54 EXPECT(t.active() == InputFamily::Xbox && t.generation() == 0 && !t.forced());
55
56 // A first sample is a baseline, whatever it holds: a pad connecting with a button down changes nothing.
57 EXPECT(t.observe(dualsense, InputFamily::PlayStation5, button(1), 0) == InputEdge::None);
58 EXPECT(t.active() == InputFamily::Xbox && t.generation() == 0);
59 // Still held: no edge.
60 EXPECT(t.observe(dualsense, InputFamily::PlayStation5, button(1), 10 * ms) == InputEdge::None);
61 // Released, then pressed again: that press is the first deliberate input, and the first change needs no wait.
62 EXPECT(t.observe(dualsense, InputFamily::PlayStation5, rest(), 20 * ms) == InputEdge::None);
63 EXPECT(t.observe(dualsense, InputFamily::PlayStation5, button(1), 30 * ms) == InputEdge::Button);
64 EXPECT(t.active() == InputFamily::PlayStation5 && t.generation() == 1);
65 return 0;
66}
67
68int test_drift_and_thresholds() {
69 ActiveInputTracker t;
70 EXPECT(t.observe(xbox, InputFamily::Xbox, rest(), 0) == InputEdge::None);
71 EXPECT(t.observe(dualsense, InputFamily::PlayStation5, rest(), 0) == InputEdge::None);
72
73 // A worn stick drifting around inside the radius never counts, however long it wanders.
74 for (int i = 0; i < 200; ++i) {
75 const float d = 0.05f + 0.25f * static_cast<float>(i % 7) / 6.0f; // 0.05 .. 0.30
76 EXPECT(t.observe(dualsense, InputFamily::PlayStation5, left_stick(d, -d * 0.5f), (1 + i) * ms) ==
77 InputEdge::None);
78 }
79 // Just inside the radius (0.35 along a diagonal is 0.247 per axis): nothing.
80 EXPECT(t.observe(dualsense, InputFamily::PlayStation5, left_stick(0.24f, 0.24f), 300 * ms) == InputEdge::None);
81 // Leaving it is an edge.
82 EXPECT(t.observe(dualsense, InputFamily::PlayStation5, left_stick(0.3f, 0.3f), 310 * ms) == InputEdge::Stick);
83 EXPECT(t.active() == InputFamily::PlayStation5 && t.generation() == 1);
84 // Held over the radius: no further edge from the same stick.
85 EXPECT(t.observe(dualsense, InputFamily::PlayStation5, left_stick(1.0f, 0.0f), 320 * ms) == InputEdge::None);
86
87 // Triggers: resting at 0.29 is not a pull; rising past 0.3 is.
88 EXPECT(t.observe(xbox, InputFamily::Xbox, triggers(0.29f, 0.0f), 700 * ms) == InputEdge::None);
89 EXPECT(t.active() == InputFamily::PlayStation5);
90 EXPECT(t.observe(xbox, InputFamily::Xbox, triggers(0.31f, 0.0f), 710 * ms) == InputEdge::Trigger);
91 EXPECT(t.active() == InputFamily::Xbox && t.generation() == 2);
92 // A trigger held further down does not count again.
93 EXPECT(t.observe(xbox, InputFamily::Xbox, triggers(0.9f, 0.0f), 720 * ms) == InputEdge::None);
94 return 0;
95}
96
97int test_min_switch_time() {
98 ActiveInputTracker t;
99 t.observe(xbox, InputFamily::Xbox, rest(), 0);
100 t.observe(dualsense, InputFamily::PlayStation5, rest(), 0);
101
102 EXPECT(t.observe(dualsense, InputFamily::PlayStation5, button(2), 1000 * ms) == InputEdge::Button);
103 EXPECT(t.active() == InputFamily::PlayStation5 && t.generation() == 1);
104 // 100 ms later the Xbox pad's press is inside the window: ignored, and not remembered for later.
105 EXPECT(t.observe(xbox, InputFamily::Xbox, button(1), 1100 * ms) == InputEdge::None);
106 EXPECT(t.active() == InputFamily::PlayStation5);
107 EXPECT(t.observe(xbox, InputFamily::Xbox, button(1), 1400 * ms) == InputEdge::None); // still held: no edge
108 EXPECT(t.active() == InputFamily::PlayStation5);
109 // Released and pressed again after the window: that one counts.
110 t.observe(xbox, InputFamily::Xbox, rest(), 1500 * ms);
111 EXPECT(t.observe(xbox, InputFamily::Xbox, button(1), 1600 * ms) == InputEdge::Button);
112 EXPECT(t.active() == InputFamily::Xbox && t.generation() == 2);
113 // Exactly at the window's end counts (the window is "less than min_switch_us").
114 EXPECT(t.observe(dualsense, InputFamily::PlayStation5, button(2 | 4), 1900 * ms) == InputEdge::Button);
115 EXPECT(t.active() == InputFamily::PlayStation5 && t.generation() == 3);
116
117 // A zero window lets every deliberate input switch at once.
118 ActiveInputTracker fast(InputFamily::Xbox, {0.35f, 0.3f, 0});
119 fast.observe(xbox, InputFamily::Xbox, rest(), 0);
120 fast.observe(keyboard, InputFamily::Keyboard, rest(), 0);
121 EXPECT(fast.observe(keyboard, InputFamily::Keyboard, button(8), 1) == InputEdge::Button);
122 EXPECT(fast.observe(xbox, InputFamily::Xbox, button(8), 2) == InputEdge::Button);
123 EXPECT(fast.active() == InputFamily::Xbox && fast.generation() == 2);
124 return 0;
125}
126
127int test_same_family_and_holds() {
128 ActiveInputTracker t;
129 for (std::uint64_t d : {xbox, xbox2}) t.observe(d, InputFamily::Xbox, rest(), 0);
130 t.observe(keyboard, InputFamily::Keyboard, rest(), 0);
131
132 // Input from a second pad of the family already in use changes nothing.
133 EXPECT(t.observe(xbox2, InputFamily::Xbox, button(1), 10 * ms) == InputEdge::None);
134 EXPECT(t.active() == InputFamily::Xbox && t.generation() == 0);
135
136 // The keyboard pad: W held pushes the left stick to full. The first push is the edge; holding it is not.
137 EXPECT(t.observe(keyboard, InputFamily::Keyboard, left_stick(0.0f, 1.0f), 20 * ms) == InputEdge::Stick);
138 EXPECT(t.active() == InputFamily::Keyboard && t.generation() == 1);
139 // The player picks up the Xbox pad while W is still held down: the pad takes over after the window and the held
140 // key does not take it back.
141 EXPECT(t.observe(xbox, InputFamily::Xbox, button(1), 400 * ms) == InputEdge::Button);
142 for (int i = 0; i < 50; ++i)
143 EXPECT(t.observe(keyboard, InputFamily::Keyboard, left_stick(0.0f, 1.0f), (410 + 10 * i) * ms) ==
144 InputEdge::None);
145 EXPECT(t.active() == InputFamily::Xbox && t.generation() == 2);
146
147 // The mouse moves the keyboard pad's right stick: a small jitter stays inside the radius, a real movement
148 // leaves it.
149 t.observe(keyboard, InputFamily::Keyboard, rest(), 1000 * ms);
150 EXPECT(t.observe(keyboard, InputFamily::Keyboard, right_stick(0.03f, 0.0f), 1010 * ms) == InputEdge::None);
151 EXPECT(t.observe(keyboard, InputFamily::Keyboard, right_stick(0.5f, -0.1f), 1020 * ms) == InputEdge::Stick);
152 EXPECT(t.active() == InputFamily::Keyboard && t.generation() == 3);
153 return 0;
154}
155
156int test_reconnect() {
157 ActiveInputTracker t;
158 t.observe(ds4, InputFamily::PlayStation4, rest(), 0);
159 EXPECT(t.observe(ds4, InputFamily::PlayStation4, button(1), 10 * ms) == InputEdge::Button);
160 EXPECT(t.active() == InputFamily::PlayStation4);
161
162 // Unplugged while the button was down; it comes back with the button still down. The new first sample is a
163 // baseline, so there is no edge to count, and no stale state from before.
164 t.forget(ds4);
165 t.observe(switch_pro, InputFamily::Nintendo, rest(), 0);
166 EXPECT(t.observe(switch_pro, InputFamily::Nintendo, button(1), 500 * ms) == InputEdge::Button);
167 EXPECT(t.active() == InputFamily::Nintendo);
168 EXPECT(t.observe(ds4, InputFamily::PlayStation4, button(1), 900 * ms) == InputEdge::None);
169 EXPECT(t.active() == InputFamily::Nintendo);
170 t.observe(ds4, InputFamily::PlayStation4, rest(), 910 * ms);
171 EXPECT(t.observe(ds4, InputFamily::PlayStation4, button(1), 920 * ms) == InputEdge::Button);
172 EXPECT(t.active() == InputFamily::PlayStation4 && t.generation() == 3);
173 return 0;
174}
175
176int test_forced() {
177 ActiveInputTracker t;
178 t.observe(dualsense, InputFamily::PlayStation5, rest(), 0);
179 t.observe(keyboard, InputFamily::Keyboard, rest(), 0);
180
181 t.force(InputFamily::Keyboard);
182 EXPECT(t.forced() && t.active() == InputFamily::Keyboard && t.generation() == 1);
183 // Forcing the same family again changes nothing.
184 t.force(InputFamily::Keyboard);
185 EXPECT(t.generation() == 1);
186
187 // Tracking still runs underneath: the DualSense becomes the tracked family, but the answer stays forced and the
188 // generation does not move.
189 EXPECT(t.observe(dualsense, InputFamily::PlayStation5, button(1), 10 * ms) == InputEdge::Button);
190 EXPECT(t.tracked() == InputFamily::PlayStation5 && t.active() == InputFamily::Keyboard && t.generation() == 1);
191
192 // Clearing the force goes straight back to the device in use.
193 t.force(std::nullopt);
194 EXPECT(!t.forced() && t.active() == InputFamily::PlayStation5 && t.generation() == 2);
195
196 // Forcing the family that is already tracked does not change the answer, so no new generation.
197 t.force(InputFamily::PlayStation5);
198 EXPECT(t.forced() && t.active() == InputFamily::PlayStation5 && t.generation() == 2);
199 t.force(std::nullopt);
200 EXPECT(t.generation() == 2);
201 return 0;
202}
203
204int test_names() {
205 EXPECT(std::string_view(input_family_name(InputFamily::PlayStation5)) == "PlayStation 5");
206 EXPECT(std::string_view(input_family_name(InputFamily::Keyboard)) == "keyboard and mouse");
207 EXPECT(std::string_view(input_edge_name(InputEdge::Stick)) == "stick movement");
208 return 0;
209}
210
211} // namespace
212
213int main() {
214 struct Case {
215 const char* name;
216 int (*run)();
217 };
218 const Case cases[] = {
219 {"start and baseline", test_start_and_baseline},
220 {"drift and thresholds", test_drift_and_thresholds},
221 {"minimum time between changes", test_min_switch_time},
222 {"one family, held input", test_same_family_and_holds},
223 {"reconnect", test_reconnect},
224 {"forced family", test_forced},
225 {"names", test_names},
226 };
227 int failures = 0;
228 for (const Case& c : cases) {
229 const int r = c.run();
230 std::printf("%s: %s\n", c.name, r ? "FAIL" : "ok");
231 failures += r;
232 }
233 const int total = static_cast<int>(std::size(cases));
234 std::printf("active input: %d of %d cases pass\n", total - failures, total);
235 return failures ? 1 : 0;
236}