-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesktopCommandExecutor.cs
More file actions
429 lines (375 loc) · 17.4 KB
/
Copy pathDesktopCommandExecutor.cs
File metadata and controls
429 lines (375 loc) · 17.4 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
using System.Text.Json;
using SwitchifyPc.Protocol;
namespace SwitchifyPc.Core.Input;
public sealed record CommandExecutionResult(bool Ok, string? Code = null, string? Message = null)
{
public static CommandExecutionResult Success { get; } = new(true);
public static CommandExecutionResult Failure(string code, string message) => new(false, code, message);
}
public sealed class DesktopCommandExecutor
{
private const int TextStreamTtlMs = 60_000;
private readonly IDesktopInputAdapter adapter;
private readonly ICursorOverlayNotifier? cursorOverlay;
private readonly IModifierKeyOverlayNotifier? modifierOverlay;
private readonly Func<double> now;
private readonly Dictionary<string, TextInputStreamState> textInputStreams = new(StringComparer.Ordinal);
private readonly HashSet<string> activeModifierKeys = new(StringComparer.Ordinal);
private string? activeDragButton;
public DesktopCommandExecutor(
IDesktopInputAdapter adapter,
ICursorOverlayNotifier? cursorOverlay = null,
Func<double>? now = null,
IModifierKeyOverlayNotifier? modifierOverlay = null)
{
this.adapter = adapter;
this.cursorOverlay = cursorOverlay;
this.modifierOverlay = modifierOverlay;
this.now = now ?? (() => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());
}
public async Task<CommandExecutionResult> ExecuteAsync(JsonElement command, CancellationToken cancellationToken = default)
{
try
{
string type = command.GetProperty("type").GetString() ?? "";
JsonElement payload = command.GetProperty("payload");
if (IsMouseCommand(type))
{
cursorOverlay?.MarkControlActive();
}
else
{
cursorOverlay?.Hide();
}
return type switch
{
"mouse.move" => await MoveMouseAsync(payload, cancellationToken),
"mouse.dragStart" => await StartDragAsync(payload.GetProperty("button").GetString() ?? "", cancellationToken),
"mouse.dragEnd" => await EndDragAsync(cancellationToken),
"mouse.click" => await ClickMouseAsync(payload.GetProperty("button").GetString() ?? "", cancellationToken),
"mouse.doubleClick" => await DoubleClickMouseAsync(payload.GetProperty("button").GetString() ?? "", cancellationToken),
"mouse.rightClick" => await RightClickMouseAsync(cancellationToken),
"mouse.scroll" => await ScrollMouseAsync(payload, cancellationToken),
"keyboard.key" => await PressKeyAsync(payload.GetProperty("key").GetString() ?? "", cancellationToken),
"keyboard.modifierDown" => await SetModifierAsync(payload.GetProperty("key").GetString() ?? "", down: true, cancellationToken),
"keyboard.modifierUp" => await SetModifierAsync(payload.GetProperty("key").GetString() ?? "", down: false, cancellationToken),
"keyboard.shortcut" => await PressShortcutAsync(payload.GetProperty("keys"), cancellationToken),
"keyboard.typeText" => await TypeTextAsync(payload.GetProperty("text").GetString() ?? "", cancellationToken),
"keyboard.textStream.open" => OpenTextInputStream(command.GetProperty("deviceId").GetString() ?? "", payload.GetProperty("streamId").GetString() ?? ""),
"keyboard.textStream.char" => await ExecuteTextStreamItemAsync(command, payload, () => adapter.TypeCharacterAsync(payload.GetProperty("text").GetString() ?? "", cancellationToken), "Text stream character insertion failed."),
"keyboard.textStream.chunk" => await ExecuteTextStreamItemAsync(command, payload, () => TypeTextStreamChunkAsync(payload.GetProperty("text").GetString() ?? "", cancellationToken), "Text stream chunk insertion failed."),
"keyboard.textStream.key" => await ExecuteTextStreamItemAsync(command, payload, () => adapter.PressKeyAsync(payload.GetProperty("key").GetString() ?? "", cancellationToken), "Text stream key insertion failed."),
"keyboard.textStream.close" => CloseTextInputStream(command.GetProperty("deviceId").GetString() ?? "", payload.GetProperty("streamId").GetString() ?? "", payload.GetProperty("expectedCount").GetInt32()),
"media.control" => await MediaControlAsync(payload.GetProperty("action").GetString() ?? "", cancellationToken),
"window.control" => await WindowControlAsync(payload.GetProperty("action").GetString() ?? "", cancellationToken),
"connection.ping" => CommandExecutionResult.Success,
"connection.disconnecting" => CommandExecutionResult.Failure("unsupported_command", "Disconnect intent must be handled by the server."),
"pointer.profile" => CommandExecutionResult.Failure("unsupported_command", "Pointer profile must be handled by the server."),
_ => CommandExecutionResult.Failure("unsupported_command", "Unsupported command.")
};
}
catch (DesktopInputException error)
{
return CommandExecutionResult.Failure(error.Code, error.Message);
}
catch (Exception error)
{
return CommandExecutionResult.Failure("adapter_failure", error.Message);
}
}
public async Task ReleaseHeldMouseButtonsAsync(CancellationToken cancellationToken = default)
{
await ReleaseHeldInputsAsync(cancellationToken).ConfigureAwait(false);
}
public async Task ReleaseHeldInputsAsync(CancellationToken cancellationToken = default)
{
await ReleaseHeldMouseButtonAsync(cancellationToken).ConfigureAwait(false);
await ReleaseHeldModifiersAsync(cancellationToken).ConfigureAwait(false);
}
public void EndControlSession()
{
activeDragButton = null;
activeModifierKeys.Clear();
cursorOverlay?.EndControlSession();
modifierOverlay?.EndControlSession();
}
private async Task<CommandExecutionResult> MoveMouseAsync(JsonElement payload, CancellationToken cancellationToken)
{
double dx = payload.GetProperty("dx").GetDouble();
double dy = payload.GetProperty("dy").GetDouble();
AssertBoundedNumber(dx, ProtocolConstants.MaxPointerDelta, "dx");
AssertBoundedNumber(dy, ProtocolConstants.MaxPointerDelta, "dy");
await adapter.MoveMouseByAsync(dx, dy, cancellationToken);
cursorOverlay?.Show(activeDragButton is null ? "move" : "drag");
return CommandExecutionResult.Success;
}
private async Task<CommandExecutionResult> StartDragAsync(string button, CancellationToken cancellationToken)
{
if (activeDragButton == button) return CommandExecutionResult.Success;
if (activeDragButton is not null)
{
await adapter.SetMouseButtonDownAsync(activeDragButton, false, cancellationToken);
activeDragButton = null;
}
await adapter.SetMouseButtonDownAsync(button, true, cancellationToken);
activeDragButton = button;
cursorOverlay?.SetDragActive(true);
cursorOverlay?.Show("drag");
return CommandExecutionResult.Success;
}
private async Task<CommandExecutionResult> EndDragAsync(CancellationToken cancellationToken)
{
if (activeDragButton is not null)
{
string button = activeDragButton;
await adapter.SetMouseButtonDownAsync(button, false, cancellationToken);
activeDragButton = null;
}
cursorOverlay?.SetDragActive(false);
cursorOverlay?.Show("move");
return CommandExecutionResult.Success;
}
private async Task<CommandExecutionResult> ClickMouseAsync(string button, CancellationToken cancellationToken)
{
await adapter.ClickMouseAsync(button, cancellationToken);
cursorOverlay?.Show("click");
return CommandExecutionResult.Success;
}
private async Task<CommandExecutionResult> DoubleClickMouseAsync(string button, CancellationToken cancellationToken)
{
await adapter.DoubleClickMouseAsync(button, cancellationToken);
cursorOverlay?.Show("click");
return CommandExecutionResult.Success;
}
private async Task<CommandExecutionResult> RightClickMouseAsync(CancellationToken cancellationToken)
{
await adapter.ClickMouseAsync("right", cancellationToken);
cursorOverlay?.Show("click");
return CommandExecutionResult.Success;
}
private async Task<CommandExecutionResult> ScrollMouseAsync(JsonElement payload, CancellationToken cancellationToken)
{
double dx = payload.GetProperty("dx").GetDouble();
double dy = payload.GetProperty("dy").GetDouble();
AssertBoundedNumber(dx, ProtocolConstants.MaxScrollDelta, "dx");
AssertBoundedNumber(dy, ProtocolConstants.MaxScrollDelta, "dy");
await adapter.ScrollMouseAsync(dx, dy, cancellationToken);
return CommandExecutionResult.Success;
}
private async Task<CommandExecutionResult> PressKeyAsync(string key, CancellationToken cancellationToken)
{
await adapter.PressKeyAsync(key, cancellationToken);
return CommandExecutionResult.Success;
}
private async Task<CommandExecutionResult> SetModifierAsync(string key, bool down, CancellationToken cancellationToken)
{
if (down)
{
if (activeModifierKeys.Contains(key))
{
return CommandExecutionResult.Success;
}
await adapter.SetKeyDownAsync(key, true, cancellationToken);
activeModifierKeys.Add(key);
UpdateModifierOverlay();
return CommandExecutionResult.Success;
}
if (!activeModifierKeys.Remove(key))
{
return CommandExecutionResult.Success;
}
try
{
await adapter.SetKeyDownAsync(key, false, cancellationToken);
return CommandExecutionResult.Success;
}
finally
{
UpdateModifierOverlay();
}
}
private async Task<CommandExecutionResult> PressShortcutAsync(JsonElement keysElement, CancellationToken cancellationToken)
{
string[] keys = keysElement.EnumerateArray().Select(key => key.GetString() ?? "").ToArray();
if (keys.Length == 0 || keys.Length > ProtocolConstants.MaxShortcutKeys)
{
return CommandExecutionResult.Failure("unsafe_payload", "Shortcut key count is invalid.");
}
await adapter.PressShortcutAsync(keys, cancellationToken);
return CommandExecutionResult.Success;
}
private async Task<CommandExecutionResult> TypeTextAsync(string text, CancellationToken cancellationToken)
{
if (text.Length > ProtocolConstants.MaxTextLength)
{
return CommandExecutionResult.Failure("unsafe_payload", "Text payload is too long.");
}
await adapter.TypeTextAsync(text, cancellationToken);
return CommandExecutionResult.Success;
}
private CommandExecutionResult OpenTextInputStream(string deviceId, string streamId)
{
ExpireTextInputStreams();
double currentTime = now();
textInputStreams[TextInputStreamKey(deviceId, streamId)] = new TextInputStreamState(deviceId, streamId, currentTime);
return CommandExecutionResult.Success;
}
private async Task<CommandExecutionResult> ExecuteTextStreamItemAsync(JsonElement command, JsonElement payload, Func<Task> execute, string failureMessage)
{
string deviceId = command.GetProperty("deviceId").GetString() ?? "";
string streamId = payload.GetProperty("streamId").GetString() ?? "";
int seq = payload.GetProperty("seq").GetInt32();
string key = TextInputStreamKey(deviceId, streamId);
if (!textInputStreams.TryGetValue(key, out TextInputStreamState? stream))
{
return CommandExecutionResult.Failure("adapter_failure", "Text stream is not open.");
}
stream.UpdatedAtMs = now();
if (seq < stream.NextSeq)
{
return stream.Failed
? CommandExecutionResult.Failure("adapter_failure", stream.ErrorMessage ?? "Text stream failed.")
: CommandExecutionResult.Success;
}
if (seq > stream.NextSeq)
{
stream.Failed = true;
stream.ErrorMessage = "Text stream sequence mismatch.";
stream.NextSeq = Math.Max(stream.NextSeq, seq + 1);
return CommandExecutionResult.Failure("adapter_failure", stream.ErrorMessage);
}
stream.NextSeq = seq + 1;
if (stream.Failed)
{
return CommandExecutionResult.Failure("adapter_failure", stream.ErrorMessage ?? "Text stream failed.");
}
try
{
await execute();
return CommandExecutionResult.Success;
}
catch
{
stream.Failed = true;
stream.ErrorMessage = failureMessage;
return CommandExecutionResult.Failure("adapter_failure", failureMessage);
}
}
private CommandExecutionResult CloseTextInputStream(string deviceId, string streamId, int expectedCount)
{
ExpireTextInputStreams();
string key = TextInputStreamKey(deviceId, streamId);
if (!textInputStreams.Remove(key, out TextInputStreamState? stream))
{
return CommandExecutionResult.Failure("adapter_failure", "Text stream is not open.");
}
if (expectedCount != stream.NextSeq)
{
return CommandExecutionResult.Failure("adapter_failure", "Text stream did not receive every item.");
}
return stream.Failed
? CommandExecutionResult.Failure("adapter_failure", stream.ErrorMessage ?? "Text stream failed.")
: CommandExecutionResult.Success;
}
private async Task TypeTextStreamChunkAsync(string text, CancellationToken cancellationToken)
{
foreach (System.Text.Rune rune in text.EnumerateRunes())
{
await adapter.TypeCharacterAsync(rune.ToString(), cancellationToken);
}
}
private async Task<CommandExecutionResult> MediaControlAsync(string action, CancellationToken cancellationToken)
{
await adapter.MediaControlAsync(action, cancellationToken);
return CommandExecutionResult.Success;
}
private async Task<CommandExecutionResult> WindowControlAsync(string action, CancellationToken cancellationToken)
{
await adapter.ControlWindowAsync(action, cancellationToken);
return CommandExecutionResult.Success;
}
private void ExpireTextInputStreams()
{
double expiresBefore = now() - TextStreamTtlMs;
foreach (string key in textInputStreams.Where(entry => entry.Value.UpdatedAtMs < expiresBefore).Select(entry => entry.Key).ToArray())
{
textInputStreams.Remove(key);
}
}
private static void AssertBoundedNumber(double value, int maxAbsValue, string label)
{
if (!double.IsFinite(value) || Math.Abs(value) > maxAbsValue)
{
throw new DesktopInputException("unsafe_payload", $"{label} is outside allowed bounds.");
}
}
private static bool IsMouseCommand(string type)
{
return type is "mouse.move" or "mouse.click" or "mouse.doubleClick" or "mouse.rightClick" or "mouse.scroll" or "mouse.dragStart" or "mouse.dragEnd";
}
private async Task ReleaseHeldMouseButtonAsync(CancellationToken cancellationToken)
{
if (activeDragButton is null) return;
string button = activeDragButton;
activeDragButton = null;
await adapter.SetMouseButtonDownAsync(button, false, cancellationToken);
cursorOverlay?.SetDragActive(false);
cursorOverlay?.Hide();
}
private async Task ReleaseHeldModifiersAsync(CancellationToken cancellationToken)
{
foreach (string key in new[] { "Meta", "Shift", "Alt", "Ctrl" })
{
if (!activeModifierKeys.Remove(key)) continue;
try
{
await adapter.SetKeyDownAsync(key, false, cancellationToken);
}
finally
{
UpdateModifierOverlay();
}
}
}
private void UpdateModifierOverlay()
{
modifierOverlay?.SetActiveModifiers(ActiveModifierLabels());
}
private IReadOnlyList<string> ActiveModifierLabels()
{
List<string> labels = [];
foreach (string key in new[] { "Ctrl", "Alt", "Shift", "Meta" })
{
if (activeModifierKeys.Contains(key))
{
labels.Add(DisplayModifierLabel(key));
}
}
return labels;
}
private static string DisplayModifierLabel(string key)
{
return key == "Meta" ? "Start" : key;
}
private static string TextInputStreamKey(string deviceId, string streamId)
{
return $"{deviceId}:{streamId}";
}
private sealed class TextInputStreamState
{
public TextInputStreamState(string deviceId, string streamId, double openedAtMs)
{
DeviceId = deviceId;
StreamId = streamId;
OpenedAtMs = openedAtMs;
UpdatedAtMs = openedAtMs;
}
public string DeviceId { get; }
public string StreamId { get; }
public int NextSeq { get; set; }
public bool Failed { get; set; }
public string? ErrorMessage { get; set; }
public double OpenedAtMs { get; }
public double UpdatedAtMs { get; set; }
}
}