Repository navigation
Expand file tree
/
Copy pathService.cpp
More file actions
329 lines (326 loc) · 13.3 KB
/
Copy pathService.cpp
File metadata and controls
329 lines (326 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "Service.h"
#include "Bridge.h"
#include "DriverMonitor.h"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <climits>
#include <mutex>
#include <stdexcept>
#include <system_error>
#include <unordered_map>
namespace gpr {
namespace {
std::string Utf8(const wchar_t *text) {
if (!text || !*text)
return {};
const int length =
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, text, -1, nullptr, 0, nullptr, nullptr);
if (length <= 0)
return "Invalid Windows diagnostic text";
std::string result(static_cast<std::size_t>(length), '\0');
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, text, -1, result.data(), length, nullptr,
nullptr);
result.pop_back();
return result;
}
// std::system_category().message() uses the process ANSI code page on MSVC.
// Keep the native error_code/category, but never append that byte string to a
// UTF-8 diagnostic consumed by the UI's strict UTF-8 conversion.
class WindowsFailure final : public std::system_error {
std::string text_;
public:
WindowsFailure(std::uint32_t code, const std::string &operation)
: std::system_error(static_cast<int>(code), std::system_category()) {
wchar_t *raw = nullptr;
const auto length = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, code, 0, reinterpret_cast<LPWSTR>(&raw), 0, nullptr);
const std::unique_ptr<wchar_t, void (*)(wchar_t *)> message(
raw, [](wchar_t *value) { if (value) LocalFree(value); });
auto detail = length ? Utf8(raw) : "Windows error " + std::to_string(code);
while (!detail.empty() && (detail.back() == '\r' || detail.back() == '\n'))
detail.pop_back();
text_ = operation.empty() ? detail : operation + ": " + detail;
}
const char *what() const noexcept override { return text_.c_str(); }
};
GR_Snapshot NativeSnapshot() {
GR_Snapshot result{};
result.size = sizeof(result);
const auto error = GR_GetSnapshot(&result);
if (error)
throw WindowsFailure(error, "Read capture status");
if (result.version != 2)
throw std::runtime_error("The capture engine version does not match this application.");
return result;
}
} // namespace
struct RemapperService::Impl {
struct ProgramSlot {
std::shared_ptr<const ScriptProgram> Program;
ParameterValues Values;
std::uint64_t Generation;
ProgramSlot(std::shared_ptr<const ScriptProgram> program, const ParameterValues &values,
std::uint64_t generation)
: Program(std::move(program)), Values(values), Generation(generation) {}
};
struct RuntimeDevice {
std::unique_ptr<ScriptInstance> Script;
ControlFrame Frame;
std::int64_t LastMicroseconds = 0;
};
std::mutex gate, errorGate, cacheGate;
std::unordered_map<std::uint32_t, RuntimeDevice> devices;
std::shared_ptr<const ProgramSlot> pending, executing;
std::string startupMessage, callbackError;
bool started = false;
std::uint64_t generation = 0;
ServiceSnapshot cached;
DriverMonitor monitor;
explicit Impl(DriverMonitor::Factory factory)
: monitor([] { GR_MonitorSnapshotChanged(); }, std::move(factory)) {
startupMessage = "No script capture active. Passive monitoring runs independently.";
cached.Status = startupMessage;
}
void SetError(std::string text) {
std::lock_guard<std::mutex> lock(errorGate);
callbackError = std::move(text);
}
static int __cdecl Transform(std::uint32_t key, std::uint32_t family, const int *physical,
const std::uint8_t *available, int *output, std::uint32_t flags,
void *user) noexcept {
auto &self = *static_cast<Impl *>(user);
try {
if (flags == 3) {
self.devices.erase(key);
return 0;
}
const auto current = std::atomic_load_explicit(&self.pending, std::memory_order_acquire);
if (current != self.executing) {
if (!current || !self.executing || current->Generation != self.executing->Generation)
self.devices.clear();
else
for (auto &entry : self.devices)
entry.second.Script->SetParameters(current->Values);
self.executing = current;
}
if (!current) {
std::copy_n(physical, 256, output);
return 0;
}
auto found = self.devices.find(key);
if (found == self.devices.end()) {
RuntimeDevice device;
device.Script = current->Program->CreateInstance();
device.Script->SetParameters(current->Values);
found = self.devices.emplace(key, std::move(device)).first;
}
auto &device = found->second;
auto &frame = device.Frame;
frame.DeviceKey = key;
frame.Family = family;
std::copy_n(physical, 256, frame.Physical.begin());
for (std::size_t i = 0; i < 256; ++i)
frame.Available[i] = available[i] != 0;
const auto now = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
const int elapsed = device.LastMicroseconds == 0
? 0
: static_cast<int>(std::clamp<std::int64_t>(
(now - device.LastMicroseconds) / 1000, 0, INT_MAX));
device.Script->Execute(frame, now, elapsed);
device.LastMicroseconds = now;
std::copy(frame.Values.begin(), frame.Values.end(), output);
return device.Script->NextWakeMilliseconds();
} catch (const ScriptException &error) {
try {
self.SetError(error.Diagnostic());
} catch (...) {
}
} catch (const std::exception &error) {
try {
self.SetError(error.what());
} catch (...) {
}
} catch (...) {
try {
self.SetError("Unexpected script runtime failure.");
} catch (...) {
}
}
std::copy_n(physical, 256, output);
return -1;
}
};
RemapperService::RemapperService(DriverMonitor::Factory factory)
: impl_(std::make_unique<Impl>(std::move(factory))) {}
RemapperService::~RemapperService() {
try {
Stop();
} catch (...) {
}
StopMonitoring();
}
void RemapperService::Inject(std::shared_ptr<const ScriptProgram> program) {
if (!program)
throw std::invalid_argument("A compiled script is required.");
const auto values = program->Interface().Defaults();
Inject(std::move(program), values);
}
void RemapperService::Inject(std::shared_ptr<const ScriptProgram> program,
const ParameterValues &values) {
if (!program)
throw std::invalid_argument("A compiled script is required.");
program->Interface().Validate(values);
auto &self = *impl_;
std::lock_guard<std::mutex> lock(self.gate);
std::atomic_store_explicit(
&self.pending,
std::make_shared<const Impl::ProgramSlot>(std::move(program), values, ++self.generation),
std::memory_order_release);
self.SetError({});
if (self.started && NativeSnapshot().running) {
GR_Wake();
return;
}
if (self.started) {
GR_Stop();
self.started = false;
}
self.devices.clear();
self.executing.reset();
wchar_t text[1024]{};
auto error = GR_Probe(text, 1024);
if (error)
throw WindowsFailure(error, Utf8(text));
error = GR_Start(Impl::Transform, &self);
if (error) {
const auto failed = NativeSnapshot();
const auto detail = Utf8(failed.error);
throw WindowsFailure(error, detail.empty() ? "Capture could not start" : detail);
}
self.started = true;
}
bool RemapperService::TryUpdate(std::shared_ptr<const ScriptProgram> program) {
if (!program)
throw std::invalid_argument("A compiled script is required.");
const auto values = program->Interface().Defaults();
return TryUpdate(std::move(program), values);
}
bool RemapperService::TryUpdate(std::shared_ptr<const ScriptProgram> program,
const ParameterValues &values) {
if (!program)
throw std::invalid_argument("A compiled script is required.");
program->Interface().Validate(values);
auto &self = *impl_;
std::lock_guard<std::mutex> lock(self.gate);
if (!self.started || !NativeSnapshot().running)
return false;
std::atomic_store_explicit(
&self.pending,
std::make_shared<const Impl::ProgramSlot>(std::move(program), values, ++self.generation),
std::memory_order_release);
self.SetError({});
GR_Wake();
return true;
}
bool RemapperService::SetParameters(const std::shared_ptr<const ScriptProgram> &program,
const ParameterValues &values) {
if (!program)
throw std::invalid_argument("A compiled script is required.");
program->Interface().Validate(values);
auto &self = *impl_;
std::lock_guard<std::mutex> lock(self.gate);
const auto current = std::atomic_load_explicit(&self.pending, std::memory_order_acquire);
if (!self.started || !current || current->Program != program || !NativeSnapshot().running)
return false;
std::atomic_store_explicit(
&self.pending, std::make_shared<const Impl::ProgramSlot>(program, values, current->Generation),
std::memory_order_release);
GR_ParametersChanged();
return true;
}
void RemapperService::Stop() {
auto &self = *impl_;
std::lock_guard<std::mutex> lock(self.gate);
if (self.started) {
GR_Stop();
self.started = false;
}
self.devices.clear();
self.executing.reset();
std::atomic_store_explicit(&self.pending, std::shared_ptr<const Impl::ProgramSlot>{},
std::memory_order_release);
}
void RemapperService::StartMonitoring() { impl_->monitor.Start(); }
void RemapperService::StopMonitoring() { impl_->monitor.Stop(); }
bool RemapperService::IsRunning() { return GetSnapshot().Running; }
ServiceSnapshot RemapperService::GetSnapshot() {
auto &self = *impl_;
const auto observed = self.monitor.Snapshot();
const auto applyMonitor = [&](ServiceSnapshot &result) {
result.Monitoring = observed.Running;
result.MonitorStatus = observed.Status;
result.MonitorError = observed.Error;
result.MonitorFrames = observed.Frames;
result.MonitorDropped = observed.Dropped;
result.MonitorTimestampFrequency = observed.TimestampFrequency;
if (observed.Requested) result.Devices = observed.Devices;
};
std::unique_lock<std::mutex> lock(self.gate, std::try_to_lock);
if (!lock) {
std::lock_guard<std::mutex> cache(self.cacheGate);
auto result = self.cached;
applyMonitor(result);
return result;
}
ServiceSnapshot result;
const auto native = NativeSnapshot();
result.Running = native.running != 0;
result.Status = self.started || native.received ? Utf8(native.status) : self.startupMessage;
result.Error = Utf8(native.error);
result.Received = native.received;
result.Forwarded = native.forwarded;
result.Changed = native.changed;
result.TimerOutputs = native.timer_outputs;
result.Faults = native.faults;
result.ProcessingMicroseconds = native.processing_microseconds;
{
std::lock_guard<std::mutex> errors(self.errorGate);
if (!self.callbackError.empty())
result.Error = self.callbackError + (result.Error.empty() ? "" : "\n" + result.Error);
}
const auto count = observed.Requested ? 0 : std::min(native.device_count, 256u);
result.Devices.reserve(count);
for (std::uint32_t i = 0; i < count; ++i) {
GR_Device source{};
source.size = sizeof(source);
if (GR_GetDevice(i, &source))
break;
ServiceDevice device;
device.Key = source.key;
device.Family = source.family;
device.Name = Utf8(source.name);
device.Token = Utf8(source.token);
device.Diagnostic = Utf8(source.diagnostic);
device.PhysicalValid = (source.flags & GR_PHYSICAL_VALID) != 0;
device.OutputValid = (source.flags & GR_OUTPUT_VALID) != 0;
std::copy_n(source.values, 256, device.Values.begin());
std::copy_n(source.output_values, 256, device.OutputValues.begin());
for (std::size_t slot = 0; slot < 256; ++slot) {
device.Available[slot] = (source.available[slot] & 1) != 0;
device.InputAvailable[slot] = device.Available[slot];
device.OutputAvailable[slot] = (source.available[slot] & 2) != 0;
}
result.Devices.emplace_back(std::move(device));
}
applyMonitor(result);
{
std::lock_guard<std::mutex> cache(self.cacheGate);
self.cached = result;
}
return result;
}
} // namespace gpr