upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/gameinput_test.cpp
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 15:15:26 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 15:15:26 -0500
commitc77e9403a3343f3b3fbd8fb6a1b1520788722653 (patch)
tree3bad1b351788e14254c0d388504690173e2b8d1f /runtime/test/gameinput_test.cpp
parent30757f16b6e3ca01a5c266749a0028ae638327f0 (diff)
parent76d1add3dab43a082332579ca33f3f039746ce6b (diff)
Merge pull request #3172 from Bora Sevim
# Conflicts: # docs/gameinput.md
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
-rw-r--r--runtime/test/gameinput_test.cpp218
1 files changed, 218 insertions, 0 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp
index 0a739e781..1389b8f6d 100644
--- a/runtime/test/gameinput_test.cpp
+++ b/runtime/test/gameinput_test.cpp
@@ -11,10 +11,12 @@
11 11
12#include <SDL3/SDL.h> 12#include <SDL3/SDL.h>
13 13
14#include <algorithm>
14#include <cmath> 15#include <cmath>
15#include <cstdint> 16#include <cstdint>
16#include <cstdio> 17#include <cstdio>
17#include <cstring> 18#include <cstring>
19#include <deque>
18#include <string> 20#include <string>
19#include <vector> 21#include <vector>
20 22
@@ -239,6 +241,221 @@ GameInputMotionState motion_state(IGameInput* input, IGameInputDevice* dev, bool
239 return ms; 241 return ms;
240} 242}
241 243
244// ---- raw HID (RT-175) -------------------------------------------------------------------------------------------
245
246using rt::gameinput::RawReportSize;
247
248bool same_reports(const std::vector<RawReportSize>& got, std::initializer_list<RawReportSize> want) {
249 if (got.size() != want.size()) return false;
250 size_t i = 0;
251 for (const RawReportSize& w : want) {
252 if (got[i].kind != w.kind || got[i].id != w.id || got[i].bytes != w.bytes) return false;
253 ++i;
254 }
255 return true;
256}
257
258// A game pad with numbered reports: input 1 (3 bytes), output 2 (2 bytes), feature 3 (4 bytes).
259const uint8_t kRawPadDescriptor[] = {
260 0x05, 0x01, 0x09, 0x05, 0xA1, 0x01, // usage page generic desktop, usage game pad, collection application
261 0x85, 0x01, 0x75, 0x08, 0x95, 0x03, 0x81, 0x02, // report id 1, size 8, count 3, input
262 0x85, 0x02, 0x95, 0x02, 0x91, 0x02, // report id 2, count 2, output
263 0x85, 0x03, 0x95, 0x04, 0xB1, 0x02, // report id 3, count 4, feature
264 0xC0, // end collection
265};
266
267void test_parse_report_descriptor() {
268 std::vector<RawReportSize> r;
269 check(rt::gameinput::parse_report_descriptor(kRawPadDescriptor, sizeof kRawPadDescriptor, &r) &&
270 same_reports(r, {{0, 1, 3}, {1, 2, 2}, {2, 3, 4}}),
271 "raw hid: a descriptor with report ids gives input 1 (3), output 2 (2), feature 3 (4)");
272 // No report id: one 16-bit input report under id 0, from two main items that add up (12 + 4 bits).
273 const uint8_t no_ids[] = {0x05, 0x01, 0x09, 0x05, 0xA1, 0x01, 0x75, 0x0C, 0x95, 0x01, 0x81, 0x02,
274 0x75, 0x04, 0x81, 0x03, 0xC0};
275 check(rt::gameinput::parse_report_descriptor(no_ids, sizeof no_ids, &r) && same_reports(r, {{0, 0, 2}}),
276 "raw hid: without report ids one input report of the summed bits (12 + 4 bits = 2 bytes)");
277 // Push saves the globals (size 8, count 2) and Pop restores them after a 32-bit item; a long item is skipped.
278 const uint8_t push_pop[] = {0x75, 0x08, 0x95, 0x02, 0xA4, 0x75, 0x20, 0x95, 0x01, 0x81, 0x02, 0xB4,
279 0xFE, 0x02, 0x10, 0xAA, 0xBB, 0x81, 0x02};
280 check(rt::gameinput::parse_report_descriptor(push_pop, sizeof push_pop, &r) && same_reports(r, {{0, 0, 6}}),
281 "raw hid: Push/Pop restore Report Size and Count, a long item is skipped (4 + 2 bytes)");
282 const uint8_t truncated[] = {0x75, 0x08, 0x96, 0x01}; // a two-byte Report Count cut after one byte
283 const uint8_t id_zero[] = {0x85, 0x00, 0x81, 0x02}; // Report ID 0 is reserved
284 const uint8_t pop_empty[] = {0xB4, 0x81, 0x02}; // Pop without Push
285 const uint8_t huge[] = {0x77, 0xFF, 0xFF, 0x00, 0x00, 0x97, 0xFF, 0xFF, 0x00, 0x00, 0x81, 0x02}; // > 64 KiB
286 check(!rt::gameinput::parse_report_descriptor(truncated, sizeof truncated, &r) &&
287 !rt::gameinput::parse_report_descriptor(id_zero, sizeof id_zero, &r) &&
288 !rt::gameinput::parse_report_descriptor(pop_empty, sizeof pop_empty, &r) &&
289 !rt::gameinput::parse_report_descriptor(huge, sizeof huge, &r),
290 "raw hid: a truncated item, Report ID 0, Pop without Push and a report over 64 KiB are refused");
291 check(rt::gameinput::parse_report_descriptor(nullptr, 0, &r) && r.empty(), "raw hid: an empty descriptor declares nothing");
292}
293
294// The fake HID device behind the raw pad: input reports to hand out, what the shim wrote, the feature reply.
295struct FakeRawHid {
296 uint16_t vendor = 0x1209, product = 0x0001; // pid.codes test ids, not a real pad
297 int opens = 0, closes = 0;
298 std::deque<std::vector<uint8_t>> inputs;
299 std::vector<std::vector<uint8_t>> writes, features_sent;
300 std::vector<uint8_t> feature_reply;
301 bool fail = false;
302};
303FakeRawHid g_raw;
304void* raw_open(const char*, uint16_t vendor, uint16_t product) {
305 if (vendor != g_raw.vendor || product != g_raw.product) return nullptr;
306 ++g_raw.opens;
307 return &g_raw;
308}
309void raw_close(void*) { ++g_raw.closes; }
310int raw_descriptor(void*, uint8_t* buf, size_t size) {
311 if (size < sizeof kRawPadDescriptor) return -1;
312 std::memcpy(buf, kRawPadDescriptor, sizeof kRawPadDescriptor);
313 return static_cast<int>(sizeof kRawPadDescriptor);
314}
315int raw_read(void*, uint8_t* buf, size_t size) {
316 if (g_raw.inputs.empty()) return 0;
317 const std::vector<uint8_t> r = g_raw.inputs.front();
318 g_raw.inputs.pop_front();
319 const size_t n = std::min(size, r.size());
320 std::memcpy(buf, r.data(), n);
321 return static_cast<int>(n);
322}
323int raw_write(void*, const uint8_t* buf, size_t size) {
324 if (g_raw.fail) return -1;
325 g_raw.writes.emplace_back(buf, buf + size);
326 return static_cast<int>(size);
327}
328int raw_get_feature(void*, uint8_t* buf, size_t size) {
329 if (g_raw.fail || g_raw.feature_reply.empty() || g_raw.feature_reply[0] != buf[0]) return -1;
330 const size_t n = std::min(size, g_raw.feature_reply.size());
331 std::memcpy(buf, g_raw.feature_reply.data(), n);
332 return static_cast<int>(n);
333}
334int raw_send_feature(void*, const uint8_t* buf, size_t size) {
335 if (g_raw.fail) return -1;
336 g_raw.features_sent.emplace_back(buf, buf + size);
337 return static_cast<int>(size);
338}
339const rt::gameinput::HidBackend kFakeRawHid = {raw_open, raw_close, raw_descriptor, raw_read,
340 raw_write, raw_get_feature, raw_send_feature};
341
342std::vector<uint8_t> raw_bytes(IGameInputRawDeviceReport* r) {
343 std::vector<uint8_t> v(r ? r->GetRawDataSize() : 0);
344 if (r) r->GetRawData(v.size(), v.data());
345 return v;
346}
347
348void test_raw_hid(IGameInput* input) {
349 test_parse_report_descriptor();
350 rt::gameinput::set_hid_backend(&kFakeRawHid);
351 const size_t events_before = g_device_events.size();
352 Pad raw_pad, plain;
353 attach(raw_pad, "Leonida virtual raw pad", g_raw.vendor, g_raw.product, false);
354 attach(plain, "Leonida virtual pad without HID", 0x045E, 0x02FF, false);
355 rt::gameinput::poll();
356 std::vector<IGameInputDevice*> found; // attach order: the raw pad first (see test_motion)
357 for (size_t i = events_before; i < g_device_events.size() && found.size() < 2; ++i) {
358 const DeviceEvent& e = g_device_events[i];
359 if ((e.current & GameInputDeviceConnected) && std::find(found.begin(), found.end(), e.device) == found.end())
360 found.push_back(e.device);
361 }
362 IGameInputDevice* rdev = found.size() > 0 ? found[0] : nullptr;
363 IGameInputDevice* pdev = found.size() > 1 ? found[1] : nullptr;
364 check(rdev && pdev && g_raw.opens == 1, "raw hid: both pads connected, the backend opened the raw pad only");
365 if (!rdev || !pdev) return;
366 rdev->AddRef();
367 pdev->AddRef();
368
369 const GameInputDeviceInfo* ri = rdev->GetDeviceInfo();
370 check((ri->supportedInput & GameInputKindRawDeviceReport) && (rdev->GetDeviceStatus() & GameInputDeviceRawIoEnabled),
371 "raw hid: the raw pad reports GameInputKindRawDeviceReport and GameInputDeviceRawIoEnabled");
372 const auto* in = static_cast<const GameInputRawDeviceReportInfo*>(ri->inputReportInfo);
373 const auto* out = static_cast<const GameInputRawDeviceReportInfo*>(ri->outputReportInfo);
374 const auto* feat = static_cast<const GameInputRawDeviceReportInfo*>(ri->featureReportInfo);
375 check(ri->inputReportCount == 1 && in && in->kind == GameInputRawInputReport && in->id == 1 && in->size == 3 &&
376 in->itemCount == 0 && ri->outputReportCount == 1 && out && out->id == 2 && out->size == 2 &&
377 ri->featureReportCount == 1 && feat && feat->id == 3 && feat->size == 4,
378 "raw hid: the report tables follow the descriptor (input 1/3, output 2/2, feature 3/4, no items)");
379 const GameInputDeviceInfo* pi = pdev->GetDeviceInfo();
380 check(!(pi->supportedInput & GameInputKindRawDeviceReport) && pi->inputReportCount == 0 &&
381 !(pdev->GetDeviceStatus() & GameInputDeviceRawIoEnabled),
382 "raw hid: a pad without a HID device has no raw reports");
383
384 IGameInputReading* reading = nullptr;
385 void* report = nullptr;
386 check(input->GetCurrentReading(GameInputKindGamepad, rdev, &reading) == S_OK && reading &&
387 !(reading->GetInputKind() & GameInputKindRawDeviceReport) && !reading->GetRawReport(&report) && !report,
388 "raw hid: before the first input report a reading carries no raw report");
389 if (reading) reading->Release();
390
391 g_raw.inputs.push_back({0x01, 0x11, 0x22, 0x33});
392 g_raw.inputs.push_back({0x09, 0x99}); // an id the descriptor does not declare: ignored
393 rt::gameinput::poll();
394 reading = nullptr;
395 check(input->GetCurrentReading(GameInputKindRawDeviceReport, rdev, &reading) == S_OK && reading &&
396 (reading->GetInputKind() & GameInputKindRawDeviceReport),
397 "raw hid: an input report makes a reading of kind GameInputKindRawDeviceReport");
398 IGameInputRawDeviceReport* rr = nullptr;
399 if (reading && reading->GetRawReport(&report)) rr = static_cast<IGameInputRawDeviceReport*>(report);
400 const GameInputRawDeviceReportInfo* rinfo = rr ? rr->GetReportInfo() : nullptr;
401 IGameInputDevice* owner = nullptr;
402 if (rr) rr->GetDevice(&owner);
403 check(rr && rinfo && rinfo->kind == GameInputRawInputReport && rinfo->id == 1 &&
404 raw_bytes(rr) == std::vector<uint8_t>({0x11, 0x22, 0x33}) && owner == rdev,
405 "raw hid: GetRawReport gives the newest declared input report, without its id byte, of this device");
406 if (owner) owner->Release();
407 int64_t item = 7;
408 check(rr && !rr->GetItemValue(0, &item) && item == 0 && !rr->SetItemValue(0, 1) && !rr->ResetAllItems(),
409 "raw hid: the item calls answer false (no item tables)");
410 if (rr) rr->Release();
411 if (reading) reading->Release();
412
413 void* created = nullptr;
414 check(rdev->CreateRawDeviceReport(2, GameInputRawOutputReport, &created) == S_OK && created,
415 "raw hid: CreateRawDeviceReport(2, output)");
416 auto* outr = static_cast<IGameInputRawDeviceReport*>(created);
417 const uint8_t payload[] = {0xAA, 0xBB};
418 check(outr && outr->GetRawDataSize() == 2 && raw_bytes(outr) == std::vector<uint8_t>({0, 0}) &&
419 !outr->SetRawData(1, payload) && outr->SetRawData(2, payload),
420 "raw hid: a new report is zeroed, and SetRawData takes exactly its size");
421 check(rdev->SendRawDeviceOutput(outr) == S_OK && !g_raw.writes.empty() &&
422 g_raw.writes.back() == std::vector<uint8_t>({0x02, 0xAA, 0xBB}),
423 "raw hid: SendRawDeviceOutput writes the id byte then the payload");
424 check(rdev->SetRawDeviceFeature(outr) == E_INVALIDARG && rdev->SendRawDeviceOutput(nullptr) == E_INVALIDARG,
425 "raw hid: an output report is not a feature report, and a null report is refused");
426 g_raw.fail = true;
427 check(rdev->SendRawDeviceOutput(outr) == E_FAIL, "raw hid: a failed write is E_FAIL");
428 g_raw.fail = false;
429 if (outr) outr->Release();
430
431 g_raw.feature_reply = {0x03, 0x01, 0x02, 0x03, 0x04};
432 void* fr = nullptr;
433 check(rdev->GetRawDeviceFeature(3, &fr) == S_OK && fr &&
434 raw_bytes(static_cast<IGameInputRawDeviceReport*>(fr)) == std::vector<uint8_t>({1, 2, 3, 4}),
435 "raw hid: GetRawDeviceFeature(3) reads the feature report");
436 check(fr && rdev->SetRawDeviceFeature(fr) == S_OK && !g_raw.features_sent.empty() &&
437 g_raw.features_sent.back() == std::vector<uint8_t>({0x03, 1, 2, 3, 4}),
438 "raw hid: SetRawDeviceFeature sends it back with its id byte");
439 if (fr) static_cast<IGameInputRawDeviceReport*>(fr)->Release();
440 void* none = reinterpret_cast<void*>(1);
441 check(rdev->CreateRawDeviceReport(9, GameInputRawOutputReport, &none) == E_INVALIDARG && !none &&
442 rdev->CreateRawDeviceReport(1, 7, &none) == E_INVALIDARG && rdev->GetRawDeviceFeature(2, &none) == E_INVALIDARG,
443 "raw hid: an undeclared id or kind is E_INVALIDARG");
444 check(pdev->CreateRawDeviceReport(1, GameInputRawInputReport, &none) == E_NOTIMPL && !none &&
445 pdev->GetRawDeviceFeature(3, &none) == E_NOTIMPL,
446 "raw hid: a pad without raw I/O answers E_NOTIMPL, as before");
447
448 detach(raw_pad);
449 detach(plain);
450 rt::gameinput::poll();
451 check(g_raw.closes == 1 && rt::gameinput::connected_count() == 0, "raw hid: unplugging closes the HID device");
452 check(rdev->CreateRawDeviceReport(2, GameInputRawOutputReport, &none) == E_NOTIMPL,
453 "raw hid: no raw I/O once the pad is gone");
454 rdev->Release();
455 pdev->Release();
456 rt::gameinput::set_hid_backend(nullptr);
457}
458
242void test_motion(IGameInput* input) { 459void test_motion(IGameInput* input) {
243 const size_t events_before = g_device_events.size(); // other tests' events are still queued in the vector 460 const size_t events_before = g_device_events.size(); // other tests' events are still queued in the vector
244 Pad gyro_pad, plain; 461 Pad gyro_pad, plain;
@@ -1604,6 +1821,7 @@ int main() {
1604 test_aggregate_device(input); 1821 test_aggregate_device(input);
1605 test_force_feedback(input); 1822 test_force_feedback(input);
1606 test_haptic_motors(input); 1823 test_haptic_motors(input);
1824 test_raw_hid(input);
1607 input->Release(); 1825 input->Release();
1608 1826
1609 std::printf("%d checks, %d failures\n", g_checks, g_failures); 1827 std::printf("%d checks, %d failures\n", g_checks, g_failures);