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:
authorBora Sevim <sevimbora@gmail.com>2026-11-22 22:35:50 +0300
committerBora Sevim <sevimbora@gmail.com>2026-11-22 22:35:50 +0300
commit364fdcbd28feb1708fd28b4727b3cc8f4a1ad1a7 (patch)
treeacce8da5f81d727993a9cbc8bb11400d89fecda9 /runtime/test/gameinput_test.cpp
parent162821017ea3c286c8bec860f3ff94f037912061 (diff)
gameinput: raw HID reports and raw device I/O through SDL's hidapi (RT-175)
The shim opens each pad's HID device with SDL_hid_* (by SDL's device path, else the one game-pad collection with the pad's vendor and product id) and reads its report descriptor for every input, output and feature report's id and size. Such a pad reports GameInputKindRawDeviceReport and GameInputDeviceRawIoEnabled and fills inputReportInfo, outputReportInfo and featureReportInfo (no item tables). Readings carry the newest raw input report, and IGameInputReading::GetRawReport returns it as an IGameInputRawDeviceReport. CreateRawDeviceReport, GetRawDeviceFeature, SetRawDeviceFeature and SendRawDeviceOutput work through the backend; an undeclared id or kind is E_INVALIDARG, a failed transfer E_FAIL, and a pad without a HID device keeps E_NOTIMPL. gameinput_abi.h declares IGameInputRawDeviceReport, GameInputRawDeviceReportKind and the three raw report info structs, checked against Microsoft's v0 header (offsetof for every field, the 12 vtable slots, the IID). Xbox pads on Windows go through XInput, whose HID interface SDL's hidapi skips, so they have no raw I/O; the log says so. gameinput_test covers the descriptor parser and the whole path with a fake HID backend (24 checks); gameinput_probe prints each pad's report tables and newest raw input report. docs/gameinput.md: "Raw HID".
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 a65b444a1..47fd599c7 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
@@ -229,6 +231,221 @@ GameInputMotionState motion_state(IGameInput* input, IGameInputDevice* dev, bool
229 return ms; 231 return ms;
230} 232}
231 233
234// ---- raw HID (RT-175) -------------------------------------------------------------------------------------------
235
236using rt::gameinput::RawReportSize;
237
238bool same_reports(const std::vector<RawReportSize>& got, std::initializer_list<RawReportSize> want) {
239 if (got.size() != want.size()) return false;
240 size_t i = 0;
241 for (const RawReportSize& w : want) {
242 if (got[i].kind != w.kind || got[i].id != w.id || got[i].bytes != w.bytes) return false;
243 ++i;
244 }
245 return true;
246}
247
248// A game pad with numbered reports: input 1 (3 bytes), output 2 (2 bytes), feature 3 (4 bytes).
249const uint8_t kRawPadDescriptor[] = {
250 0x05, 0x01, 0x09, 0x05, 0xA1, 0x01, // usage page generic desktop, usage game pad, collection application
251 0x85, 0x01, 0x75, 0x08, 0x95, 0x03, 0x81, 0x02, // report id 1, size 8, count 3, input
252 0x85, 0x02, 0x95, 0x02, 0x91, 0x02, // report id 2, count 2, output
253 0x85, 0x03, 0x95, 0x04, 0xB1, 0x02, // report id 3, count 4, feature
254 0xC0, // end collection
255};
256
257void test_parse_report_descriptor() {
258 std::vector<RawReportSize> r;
259 check(rt::gameinput::parse_report_descriptor(kRawPadDescriptor, sizeof kRawPadDescriptor, &r) &&
260 same_reports(r, {{0, 1, 3}, {1, 2, 2}, {2, 3, 4}}),
261 "raw hid: a descriptor with report ids gives input 1 (3), output 2 (2), feature 3 (4)");
262 // No report id: one 16-bit input report under id 0, from two main items that add up (12 + 4 bits).
263 const uint8_t no_ids[] = {0x05, 0x01, 0x09, 0x05, 0xA1, 0x01, 0x75, 0x0C, 0x95, 0x01, 0x81, 0x02,
264 0x75, 0x04, 0x81, 0x03, 0xC0};
265 check(rt::gameinput::parse_report_descriptor(no_ids, sizeof no_ids, &r) && same_reports(r, {{0, 0, 2}}),
266 "raw hid: without report ids one input report of the summed bits (12 + 4 bits = 2 bytes)");
267 // Push saves the globals (size 8, count 2) and Pop restores them after a 32-bit item; a long item is skipped.
268 const uint8_t push_pop[] = {0x75, 0x08, 0x95, 0x02, 0xA4, 0x75, 0x20, 0x95, 0x01, 0x81, 0x02, 0xB4,
269 0xFE, 0x02, 0x10, 0xAA, 0xBB, 0x81, 0x02};
270 check(rt::gameinput::parse_report_descriptor(push_pop, sizeof push_pop, &r) && same_reports(r, {{0, 0, 6}}),
271 "raw hid: Push/Pop restore Report Size and Count, a long item is skipped (4 + 2 bytes)");
272 const uint8_t truncated[] = {0x75, 0x08, 0x96, 0x01}; // a two-byte Report Count cut after one byte
273 const uint8_t id_zero[] = {0x85, 0x00, 0x81, 0x02}; // Report ID 0 is reserved
274 const uint8_t pop_empty[] = {0xB4, 0x81, 0x02}; // Pop without Push
275 const uint8_t huge[] = {0x77, 0xFF, 0xFF, 0x00, 0x00, 0x97, 0xFF, 0xFF, 0x00, 0x00, 0x81, 0x02}; // > 64 KiB
276 check(!rt::gameinput::parse_report_descriptor(truncated, sizeof truncated, &r) &&
277 !rt::gameinput::parse_report_descriptor(id_zero, sizeof id_zero, &r) &&
278 !rt::gameinput::parse_report_descriptor(pop_empty, sizeof pop_empty, &r) &&
279 !rt::gameinput::parse_report_descriptor(huge, sizeof huge, &r),
280 "raw hid: a truncated item, Report ID 0, Pop without Push and a report over 64 KiB are refused");
281 check(rt::gameinput::parse_report_descriptor(nullptr, 0, &r) && r.empty(), "raw hid: an empty descriptor declares nothing");
282}
283
284// The fake HID device behind the raw pad: input reports to hand out, what the shim wrote, the feature reply.
285struct FakeHid {
286 uint16_t vendor = 0x1209, product = 0x0001; // pid.codes test ids, not a real pad
287 int opens = 0, closes = 0;
288 std::deque<std::vector<uint8_t>> inputs;
289 std::vector<std::vector<uint8_t>> writes, features_sent;
290 std::vector<uint8_t> feature_reply;
291 bool fail = false;
292};
293FakeHid g_fake;
294void* fake_open(const char*, uint16_t vendor, uint16_t product) {
295 if (vendor != g_fake.vendor || product != g_fake.product) return nullptr;
296 ++g_fake.opens;
297 return &g_fake;
298}
299void fake_close(void*) { ++g_fake.closes; }
300int fake_descriptor(void*, uint8_t* buf, size_t size) {
301 if (size < sizeof kRawPadDescriptor) return -1;
302 std::memcpy(buf, kRawPadDescriptor, sizeof kRawPadDescriptor);
303 return static_cast<int>(sizeof kRawPadDescriptor);
304}
305int fake_read(void*, uint8_t* buf, size_t size) {
306 if (g_fake.inputs.empty()) return 0;
307 const std::vector<uint8_t> r = g_fake.inputs.front();
308 g_fake.inputs.pop_front();
309 const size_t n = std::min(size, r.size());
310 std::memcpy(buf, r.data(), n);
311 return static_cast<int>(n);
312}
313int fake_write(void*, const uint8_t* buf, size_t size) {
314 if (g_fake.fail) return -1;
315 g_fake.writes.emplace_back(buf, buf + size);
316 return static_cast<int>(size);
317}
318int fake_get_feature(void*, uint8_t* buf, size_t size) {
319 if (g_fake.fail || g_fake.feature_reply.empty() || g_fake.feature_reply[0] != buf[0]) return -1;
320 const size_t n = std::min(size, g_fake.feature_reply.size());
321 std::memcpy(buf, g_fake.feature_reply.data(), n);
322 return static_cast<int>(n);
323}
324int fake_send_feature(void*, const uint8_t* buf, size_t size) {
325 if (g_fake.fail) return -1;
326 g_fake.features_sent.emplace_back(buf, buf + size);
327 return static_cast<int>(size);
328}
329const rt::gameinput::HidBackend kFakeHid = {fake_open, fake_close, fake_descriptor, fake_read,
330 fake_write, fake_get_feature, fake_send_feature};
331
332std::vector<uint8_t> raw_bytes(IGameInputRawDeviceReport* r) {
333 std::vector<uint8_t> v(r ? r->GetRawDataSize() : 0);
334 if (r) r->GetRawData(v.size(), v.data());
335 return v;
336}
337
338void test_raw_hid(IGameInput* input) {
339 test_parse_report_descriptor();
340 rt::gameinput::set_hid_backend(&kFakeHid);
341 const size_t events_before = g_device_events.size();
342 Pad raw_pad, plain;
343 attach(raw_pad, "Leonida virtual raw pad", g_fake.vendor, g_fake.product, false);
344 attach(plain, "Leonida virtual pad without HID", 0x045E, 0x02FF, false);
345 rt::gameinput::poll();
346 std::vector<IGameInputDevice*> found; // attach order: the raw pad first (see test_motion)
347 for (size_t i = events_before; i < g_device_events.size() && found.size() < 2; ++i) {
348 const DeviceEvent& e = g_device_events[i];
349 if ((e.current & GameInputDeviceConnected) && std::find(found.begin(), found.end(), e.device) == found.end())
350 found.push_back(e.device);
351 }
352 IGameInputDevice* rdev = found.size() > 0 ? found[0] : nullptr;
353 IGameInputDevice* pdev = found.size() > 1 ? found[1] : nullptr;
354 check(rdev && pdev && g_fake.opens == 1, "raw hid: both pads connected, the backend opened the raw pad only");
355 if (!rdev || !pdev) return;
356 rdev->AddRef();
357 pdev->AddRef();
358
359 const GameInputDeviceInfo* ri = rdev->GetDeviceInfo();
360 check((ri->supportedInput & GameInputKindRawDeviceReport) && (rdev->GetDeviceStatus() & GameInputDeviceRawIoEnabled),
361 "raw hid: the raw pad reports GameInputKindRawDeviceReport and GameInputDeviceRawIoEnabled");
362 const auto* in = static_cast<const GameInputRawDeviceReportInfo*>(ri->inputReportInfo);
363 const auto* out = static_cast<const GameInputRawDeviceReportInfo*>(ri->outputReportInfo);
364 const auto* feat = static_cast<const GameInputRawDeviceReportInfo*>(ri->featureReportInfo);
365 check(ri->inputReportCount == 1 && in && in->kind == GameInputRawInputReport && in->id == 1 && in->size == 3 &&
366 in->itemCount == 0 && ri->outputReportCount == 1 && out && out->id == 2 && out->size == 2 &&
367 ri->featureReportCount == 1 && feat && feat->id == 3 && feat->size == 4,
368 "raw hid: the report tables follow the descriptor (input 1/3, output 2/2, feature 3/4, no items)");
369 const GameInputDeviceInfo* pi = pdev->GetDeviceInfo();
370 check(!(pi->supportedInput & GameInputKindRawDeviceReport) && pi->inputReportCount == 0 &&
371 !(pdev->GetDeviceStatus() & GameInputDeviceRawIoEnabled),
372 "raw hid: a pad without a HID device has no raw reports");
373
374 IGameInputReading* reading = nullptr;
375 void* report = nullptr;
376 check(input->GetCurrentReading(GameInputKindGamepad, rdev, &reading) == S_OK && reading &&
377 !(reading->GetInputKind() & GameInputKindRawDeviceReport) && !reading->GetRawReport(&report) && !report,
378 "raw hid: before the first input report a reading carries no raw report");
379 if (reading) reading->Release();
380
381 g_fake.inputs.push_back({0x01, 0x11, 0x22, 0x33});
382 g_fake.inputs.push_back({0x09, 0x99}); // an id the descriptor does not declare: ignored
383 rt::gameinput::poll();
384 reading = nullptr;
385 check(input->GetCurrentReading(GameInputKindRawDeviceReport, rdev, &reading) == S_OK && reading &&
386 (reading->GetInputKind() & GameInputKindRawDeviceReport),
387 "raw hid: an input report makes a reading of kind GameInputKindRawDeviceReport");
388 IGameInputRawDeviceReport* rr = nullptr;
389 if (reading && reading->GetRawReport(&report)) rr = static_cast<IGameInputRawDeviceReport*>(report);
390 const GameInputRawDeviceReportInfo* rinfo = rr ? rr->GetReportInfo() : nullptr;
391 IGameInputDevice* owner = nullptr;
392 if (rr) rr->GetDevice(&owner);
393 check(rr && rinfo && rinfo->kind == GameInputRawInputReport && rinfo->id == 1 &&
394 raw_bytes(rr) == std::vector<uint8_t>({0x11, 0x22, 0x33}) && owner == rdev,
395 "raw hid: GetRawReport gives the newest declared input report, without its id byte, of this device");
396 if (owner) owner->Release();
397 int64_t item = 7;
398 check(rr && !rr->GetItemValue(0, &item) && item == 0 && !rr->SetItemValue(0, 1) && !rr->ResetAllItems(),
399 "raw hid: the item calls answer false (no item tables)");
400 if (rr) rr->Release();
401 if (reading) reading->Release();
402
403 void* created = nullptr;
404 check(rdev->CreateRawDeviceReport(2, GameInputRawOutputReport, &created) == S_OK && created,
405 "raw hid: CreateRawDeviceReport(2, output)");
406 auto* outr = static_cast<IGameInputRawDeviceReport*>(created);
407 const uint8_t payload[] = {0xAA, 0xBB};
408 check(outr && outr->GetRawDataSize() == 2 && raw_bytes(outr) == std::vector<uint8_t>({0, 0}) &&
409 !outr->SetRawData(1, payload) && outr->SetRawData(2, payload),
410 "raw hid: a new report is zeroed, and SetRawData takes exactly its size");
411 check(rdev->SendRawDeviceOutput(outr) == S_OK && !g_fake.writes.empty() &&
412 g_fake.writes.back() == std::vector<uint8_t>({0x02, 0xAA, 0xBB}),
413 "raw hid: SendRawDeviceOutput writes the id byte then the payload");
414 check(rdev->SetRawDeviceFeature(outr) == E_INVALIDARG && rdev->SendRawDeviceOutput(nullptr) == E_INVALIDARG,
415 "raw hid: an output report is not a feature report, and a null report is refused");
416 g_fake.fail = true;
417 check(rdev->SendRawDeviceOutput(outr) == E_FAIL, "raw hid: a failed write is E_FAIL");
418 g_fake.fail = false;
419 if (outr) outr->Release();
420
421 g_fake.feature_reply = {0x03, 0x01, 0x02, 0x03, 0x04};
422 void* fr = nullptr;
423 check(rdev->GetRawDeviceFeature(3, &fr) == S_OK && fr &&
424 raw_bytes(static_cast<IGameInputRawDeviceReport*>(fr)) == std::vector<uint8_t>({1, 2, 3, 4}),
425 "raw hid: GetRawDeviceFeature(3) reads the feature report");
426 check(fr && rdev->SetRawDeviceFeature(fr) == S_OK && !g_fake.features_sent.empty() &&
427 g_fake.features_sent.back() == std::vector<uint8_t>({0x03, 1, 2, 3, 4}),
428 "raw hid: SetRawDeviceFeature sends it back with its id byte");
429 if (fr) static_cast<IGameInputRawDeviceReport*>(fr)->Release();
430 void* none = reinterpret_cast<void*>(1);
431 check(rdev->CreateRawDeviceReport(9, GameInputRawOutputReport, &none) == E_INVALIDARG && !none &&
432 rdev->CreateRawDeviceReport(1, 7, &none) == E_INVALIDARG && rdev->GetRawDeviceFeature(2, &none) == E_INVALIDARG,
433 "raw hid: an undeclared id or kind is E_INVALIDARG");
434 check(pdev->CreateRawDeviceReport(1, GameInputRawInputReport, &none) == E_NOTIMPL && !none &&
435 pdev->GetRawDeviceFeature(3, &none) == E_NOTIMPL,
436 "raw hid: a pad without raw I/O answers E_NOTIMPL, as before");
437
438 detach(raw_pad);
439 detach(plain);
440 rt::gameinput::poll();
441 check(g_fake.closes == 1 && rt::gameinput::connected_count() == 0, "raw hid: unplugging closes the HID device");
442 check(rdev->CreateRawDeviceReport(2, GameInputRawOutputReport, &none) == E_NOTIMPL,
443 "raw hid: no raw I/O once the pad is gone");
444 rdev->Release();
445 pdev->Release();
446 rt::gameinput::set_hid_backend(nullptr);
447}
448
232void test_motion(IGameInput* input) { 449void test_motion(IGameInput* input) {
233 const size_t events_before = g_device_events.size(); // other tests' events are still queued in the vector 450 const size_t events_before = g_device_events.size(); // other tests' events are still queued in the vector
234 Pad gyro_pad, plain; 451 Pad gyro_pad, plain;
@@ -1226,6 +1443,7 @@ int main() {
1226 test_keyboard_pad(input); 1443 test_keyboard_pad(input);
1227 test_motion(input); 1444 test_motion(input);
1228 test_aggregate_device(input); 1445 test_aggregate_device(input);
1446 test_raw_hid(input);
1229 input->Release(); 1447 input->Release();
1230 1448
1231 std::printf("%d checks, %d failures\n", g_checks, g_failures); 1449 std::printf("%d checks, %d failures\n", g_checks, g_failures);