Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/FreeTrackInputTests.cpp

Source at documentation-generation time. See Test catalog for fixture associations.

1#include "../Config/InputConfig.h"
2#include "../Viewscreen/FreeTrackInput.h"
3#include <cstdio>
4#include <cstddef>
5#include <cstdlib>
6#include <cstring>
7#include <limits>
8
9static void Check(bool condition, const char* message)
10{
11 if (!condition) { std::fprintf(stderr, "FAIL: %s\n", message); std::exit(1); }
12}
13
14struct Tracker
15{
16 FreeTrackData pose{};
17 unsigned calls = 0;
18 bool connected = true;
19 bool freshOnly = false;
20 std::uint32_t lastRead = 0;
21
22 bool Read(FreeTrackData* output)
23 {
24 ++calls;
25 if (!connected || (freshOnly && pose.dataID == lastRead)) return false;
26 lastRead = pose.dataID;
27 *output = pose;
28 return true;
29 }
30};
31
32static void ExerciseStream(bool freshOnly)
33{
34 FreeTrackInput input;
35 Tracker tracker;
36 tracker.freshOnly = freshOnly;
37 auto read = [&](FreeTrackData* output) { return tracker.Read(output); };
38 float lateral = .7f, vertical = -.8f;
39 Check(!input.Poll(false, false, 100, read) && tracker.calls == 0, "unselected device is never polled");
40 tracker.connected = false;
41 Check(!input.Poll(true, false, 100, read), "absent tracker leaves manual targets intact");
42 Check(!input.Tracking(100), "absent tracker is not live");
43 tracker.connected = true;
44 Check(!input.Poll(true, false, 110, read), "empty record cannot overwrite mouse");
45
46 tracker.pose.dataID = 1;
47 tracker.pose.yaw = .2f;
48 tracker.pose.pitch = -.3f;
49 tracker.pose.roll = .4f;
50 tracker.pose.x = 25;
51 tracker.pose.y = 50;
52 tracker.pose.z = 75;
53 // Both client contracts require a moving counter after enable. The legacy
54 // client returned the empty baseline; the newer client skips empty frames.
55 const bool firstPose = input.Poll(true, false, 120, read);
56 Check(firstPose == !freshOnly, "enable establishes a baseline before accepting data");
57 tracker.pose.dataID = 2;
58 Check(input.Poll(true, false, 130, read), "moving lateral tracker accepted");
59 input.Apply(true, false, false, false, lateral, vertical);
60 Check(lateral == -.2f && vertical == -.8f, "lateral-only tracking preserves mouse/joystick vertical target");
61 Check(input.roll == 0 && input.x == 0 && input.y == 0 && input.z == 0, "mixed input has no extra roll or translation");
62 input.Apply(true, false, true, false, lateral, vertical);
63 Check(lateral == .2f && vertical == -.8f, "lateral invert is independent");
64 Check(!input.Poll(true, false, 140, read), "duplicate frames never rewrite camera");
65
66 Check(!input.Poll(false, true, 150, read), "switching axes resets liveness and waits for a fresh baseline");
67 tracker.pose.dataID = 3;
68 input.Poll(false, true, 160, read);
69 tracker.pose.dataID = 4;
70 Check(input.Poll(false, true, 170, read), "vertical tracker accepted");
71 input.Apply(false, true, false, true, lateral, vertical);
72 Check(lateral == .2f && vertical == -.3f, "vertical-only invert preserves lateral target");
73 input.Apply(false, false, false, false, lateral, vertical);
74 Check(lateral == .2f && vertical == -.3f, "camera permission gates leave targets intact");
75
76 input.Poll(true, true, 180, read);
77 tracker.pose.dataID = 5;
78 input.Poll(true, true, 190, read);
79 tracker.pose.dataID = 6;
80 Check(input.Poll(true, true, 200, read), "both axes accepted");
81 Check(input.roll == .4f && input.x < 0 && input.y < 0 && input.z > 0, "both assigned axes retain existing six-DOF movement");
82 input.Apply(true, true, false, false, lateral, vertical);
83 Check(lateral == -.2f && vertical == .3f, "both targets use current tracker pose");
84 Check(input.Tracking(1199) && !input.Tracking(1200), "liveness expires at the timeout boundary");
85 Check(!input.Poll(true, true, 1200, read), "stopped producer cannot replay a pose");
86 Check(input.roll == 0 && input.x == 0 && input.y == 0 && input.z == 0 && input.rate == 0, "timeout clears retained head movement and rate");
87
88 tracker.pose.dataID = 7;
89 Check(input.Poll(true, true, 1210, read) && input.Tracking(1210), "restarted producer re-arms");
90 tracker.pose.dataID = UINT32_MAX;
91 Check(input.Poll(true, true, 1220, read), "high counter accepted");
92 tracker.pose = {};
93 Check(input.Poll(true, true, 1230, read), "counter wrap with centered pose accepted");
94 input.Apply(true, true, false, false, lateral, vertical);
95 Check(lateral == 0 && vertical == 0, "centered tracking is valid");
96 tracker.pose.dataID = 1;
97 tracker.pose.yaw = std::numeric_limits<float>::quiet_NaN();
98 Check(!input.Poll(true, true, 1240, read), "nonfinite pose rejected");
99 tracker.pose.dataID = 2;
100 tracker.pose.yaw = .25f;
101 Check(input.Poll(true, true, 1250, read), "valid frame after invalid pose accepted");
102
103 const unsigned calls = tracker.calls;
104 Check(!input.Poll(false, false, 1260, read) && tracker.calls == calls, "deselecting last channel stops DLL calls immediately");
105 Check(!input.Tracking(1260) && input.roll == 0 && input.x == 0, "disable clears transient state");
106 Check(!input.Poll(true, true, 1270, read), "re-enable cannot replay retained frame");
107 tracker.pose.dataID = 3;
108 input.Poll(true, true, 1280, read);
109 for (unsigned frame = 4; frame <= 104; ++frame)
110 {
111 tracker.pose.dataID = frame;
112 input.Poll(true, true, 1280 + (frame - 3) * 10, read);
113 }
114 Check(input.Tracking(2290) && input.rate >= 99 && input.rate <= 101, "status reports observed update rate");
115}
116
117int main()
118{
119 static_assert(offsetof(InputConfig, g_pJoystick) == 32, "Persisted input record remains 32 bytes");
120 InputConfig config;
121 config.guid = MYGUID_NULL;
122 Check(config.UsesMouse() && !config.IsFreeTrack(), "old mouse assignment remains mouse");
123 config.guid = MYGUID_FREETRACK;
124 config.invert = true;
125 Check(config.IsFreeTrack() && !config.UsesMouse(), "virtual device never falls through to mouse");
126 // Config::Save/Load uses the first 32 bytes in both architectures.
127 unsigned char saved[32];
128 std::memcpy(saved, &config, sizeof(saved));
129 InputConfig restored;
130 std::memcpy(&restored, saved, sizeof(saved));
131 Check(restored.IsFreeTrack() && restored.invert && !restored.g_pJoystick, "saved virtual device and invert survive restart");
132 ExerciseStream(false); // Legacy clients return repeated records.
133 ExerciseStream(true); // Newer clients return only changed records.
134 std::puts("PASS FreeTrack opt-in, mixed axes, invert, camera gates, stale/invalid data, timeout/restart, wrap, rate, persistence; legacy and fresh-only clients");
135}
const GUID MYGUID_FREETRACK
Definition Guids.h:8
const GUID MYGUID_NULL
Definition Guids.h:3
void Apply(bool lateral, bool vertical, bool invertLateral, bool invertVertical, float &targetLateral, float &targetVertical) const
bool Poll(bool lateral, bool vertical, std::uint64_t now, Reader read)
bool Tracking(std::uint64_t now) const
std::uint32_t dataID
bool IsFreeTrack() const
Definition InputConfig.h:23
LPDIRECTINPUTDEVICE8 g_pJoystick
Definition InputConfig.h:22
bool UsesMouse() const
Definition InputConfig.h:24