@copylee/dsh-computer-use 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +14 -8
- package/helper/CuHelper.cs +1382 -1248
- package/helper/deepseek-white.png +0 -0
- package/lib/index.js +208 -48
- package/lib/types/computer.d.ts +16 -2
- package/lib/types/helper-client.d.ts +2 -1
- package/lib/types/index.d.ts +5 -0
- package/lib/types/overlay.d.ts +43 -4
- package/package.json +3 -2
package/helper/CuHelper.cs
CHANGED
|
@@ -1,1248 +1,1382 @@
|
|
|
1
|
-
// dsh-computer-use helper: a long-lived Windows process driven over stdin/stdout
|
|
2
|
-
// (one JSON object per line). Compiled on first use with the .NET Framework
|
|
3
|
-
// csc.exe that ships with Windows, so the npm package carries no native binary.
|
|
4
|
-
//
|
|
5
|
-
// Language level: C# 5 (no string interpolation, no ?. operator, no expression
|
|
6
|
-
// bodies) because that is what the in-box csc.exe understands.
|
|
7
|
-
//
|
|
8
|
-
// Request: {"id": 1, "cmd": "screenshot", ...}
|
|
9
|
-
// Response: {"id": 1, "ok": true, "result": {...}} | {"id": 1, "ok": false, "error": "..."}
|
|
10
|
-
// Event: {"event": "stop", "reason": "esc"} (no id)
|
|
11
|
-
//
|
|
12
|
-
// All coordinates are physical pixels in virtual-screen space: the process is
|
|
13
|
-
// Per-Monitor-V2 DPI aware before any GDI / WinForms code runs.
|
|
14
|
-
|
|
15
|
-
using System;
|
|
16
|
-
using System.Collections;
|
|
17
|
-
using System.Collections.Generic;
|
|
18
|
-
using System.Diagnostics;
|
|
19
|
-
using System.Drawing;
|
|
20
|
-
using System.Drawing.Drawing2D;
|
|
21
|
-
using System.Drawing.Imaging;
|
|
22
|
-
using System.Drawing.Text;
|
|
23
|
-
using System.IO;
|
|
24
|
-
using System.Runtime.InteropServices;
|
|
25
|
-
using System.Text;
|
|
26
|
-
using System.Threading;
|
|
27
|
-
using System.Web.Script.Serialization;
|
|
28
|
-
using System.Windows.Automation;
|
|
29
|
-
using System.Windows.Forms;
|
|
30
|
-
|
|
31
|
-
namespace DshComputerUse
|
|
32
|
-
{
|
|
33
|
-
internal static class Native
|
|
34
|
-
{
|
|
35
|
-
[DllImport("user32.dll")] public static extern bool SetProcessDpiAwarenessContext(IntPtr value);
|
|
36
|
-
[DllImport("user32.dll")] public static extern bool SetProcessDPIAware();
|
|
37
|
-
|
|
38
|
-
[StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; public POINT(int x, int y) { X = x; Y = y; } }
|
|
39
|
-
[StructLayout(LayoutKind.Sequential)] public struct SIZE { public int cx; public int cy; public SIZE(int w, int h) { cx = w; cy = h; } }
|
|
40
|
-
[StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left, Top, Right, Bottom; }
|
|
41
|
-
|
|
42
|
-
[StructLayout(LayoutKind.Sequential)]
|
|
43
|
-
public struct MOUSEINPUT { public int dx; public int dy; public uint mouseData; public uint dwFlags; public uint time; public IntPtr dwExtraInfo; }
|
|
44
|
-
[StructLayout(LayoutKind.Sequential)]
|
|
45
|
-
public struct KEYBDINPUT { public ushort wVk; public ushort wScan; public uint dwFlags; public uint time; public IntPtr dwExtraInfo; }
|
|
46
|
-
[StructLayout(LayoutKind.Explicit)]
|
|
47
|
-
public struct InputUnion { [FieldOffset(0)] public MOUSEINPUT mi; [FieldOffset(0)] public KEYBDINPUT ki; }
|
|
48
|
-
[StructLayout(LayoutKind.Sequential)]
|
|
49
|
-
public struct INPUT { public uint type; public InputUnion u; }
|
|
50
|
-
|
|
51
|
-
[DllImport("user32.dll", SetLastError = true)] public static extern uint SendInput(uint n, INPUT[] inputs, int size);
|
|
52
|
-
[DllImport("user32.dll")] public static extern bool SetCursorPos(int x, int y);
|
|
53
|
-
[DllImport("user32.dll")] public static extern bool GetCursorPos(out POINT p);
|
|
54
|
-
[DllImport("user32.dll")] public static extern uint MapVirtualKey(uint code, uint mapType);
|
|
55
|
-
|
|
56
|
-
public delegate bool MonitorEnumProc(IntPtr hMonitor, IntPtr hdc, ref RECT rect, IntPtr data);
|
|
57
|
-
[DllImport("user32.dll")] public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr clip, MonitorEnumProc proc, IntPtr data);
|
|
58
|
-
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
59
|
-
public struct MONITORINFOEX { public int cbSize; public RECT rcMonitor; public RECT rcWork; public uint dwFlags; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string szDevice; }
|
|
60
|
-
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFOEX info);
|
|
61
|
-
[DllImport("shcore.dll")] public static extern int GetDpiForMonitor(IntPtr hMonitor, int type, out uint dpiX, out uint dpiY);
|
|
62
|
-
|
|
63
|
-
[DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hwnd);
|
|
64
|
-
[DllImport("user32.dll")] public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
|
|
65
|
-
[DllImport("gdi32.dll")] public static extern bool BitBlt(IntPtr dst, int x, int y, int w, int h, IntPtr src, int sx, int sy, uint rop);
|
|
66
|
-
[DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
|
|
67
|
-
[DllImport("gdi32.dll")] public static extern bool DeleteDC(IntPtr hdc);
|
|
68
|
-
[DllImport("gdi32.dll")] public static extern IntPtr SelectObject(IntPtr hdc, IntPtr obj);
|
|
69
|
-
[DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr obj);
|
|
70
|
-
|
|
71
|
-
public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr data);
|
|
72
|
-
[DllImport("user32.dll")] public static extern bool EnumWindows(EnumWindowsProc proc, IntPtr data);
|
|
73
|
-
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hwnd);
|
|
74
|
-
[DllImport("user32.dll")] public static extern bool IsWindow(IntPtr hwnd);
|
|
75
|
-
[DllImport("user32.dll")] public static extern bool IsIconic(IntPtr hwnd);
|
|
76
|
-
[DllImport("user32.dll")] public static extern bool IsZoomed(IntPtr hwnd);
|
|
77
|
-
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int GetWindowText(IntPtr hwnd, StringBuilder sb, int max);
|
|
78
|
-
[DllImport("user32.dll")] public static extern int GetWindowTextLength(IntPtr hwnd);
|
|
79
|
-
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int GetClassName(IntPtr hwnd, StringBuilder sb, int max);
|
|
80
|
-
[DllImport("user32.dll")] public static extern IntPtr GetWindow(IntPtr hwnd, uint cmd);
|
|
81
|
-
[DllImport("user32.dll")] public static extern int GetWindowLong(IntPtr hwnd, int index);
|
|
82
|
-
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hwnd, out uint pid);
|
|
83
|
-
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
|
|
84
|
-
[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hwnd);
|
|
85
|
-
[DllImport("user32.dll")] public static extern bool BringWindowToTop(IntPtr hwnd);
|
|
86
|
-
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hwnd, int cmd);
|
|
87
|
-
[DllImport("user32.dll")] public static extern void SwitchToThisWindow(IntPtr hwnd, bool altTab);
|
|
88
|
-
[DllImport("user32.dll")] public static extern bool AttachThreadInput(uint a, uint b, bool attach);
|
|
89
|
-
[DllImport("kernel32.dll")] public static extern uint GetCurrentThreadId();
|
|
90
|
-
[DllImport("user32.dll")] public static extern bool PostMessage(IntPtr hwnd, uint msg, IntPtr w, IntPtr l);
|
|
91
|
-
[DllImport("user32.dll")] public static extern IntPtr WindowFromPoint(POINT p);
|
|
92
|
-
[DllImport("user32.dll")] public static extern IntPtr GetAncestor(IntPtr hwnd, uint flags);
|
|
93
|
-
[DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hwnd, IntPtr after, int x, int y, int w, int h, uint flags);
|
|
94
|
-
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr hwnd, out RECT r);
|
|
95
|
-
[
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
[DllImport("
|
|
99
|
-
[DllImport("
|
|
100
|
-
[DllImport("
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
[
|
|
104
|
-
|
|
105
|
-
[DllImport("
|
|
106
|
-
|
|
107
|
-
[
|
|
108
|
-
public
|
|
109
|
-
[DllImport("user32.dll"
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
[DllImport("user32.dll")] public static extern bool
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
public
|
|
119
|
-
|
|
120
|
-
public
|
|
121
|
-
|
|
122
|
-
public
|
|
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
|
-
static
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
var
|
|
183
|
-
var
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
var
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
string
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
reply["
|
|
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
|
-
case "
|
|
251
|
-
case "
|
|
252
|
-
case "
|
|
253
|
-
case "
|
|
254
|
-
case "
|
|
255
|
-
case "
|
|
256
|
-
case "
|
|
257
|
-
case "
|
|
258
|
-
case "
|
|
259
|
-
case "
|
|
260
|
-
case "
|
|
261
|
-
case "
|
|
262
|
-
case "
|
|
263
|
-
case "
|
|
264
|
-
case "
|
|
265
|
-
case "
|
|
266
|
-
case "
|
|
267
|
-
case "
|
|
268
|
-
case "
|
|
269
|
-
case "
|
|
270
|
-
case "
|
|
271
|
-
case "
|
|
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
|
-
d
|
|
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
|
-
static Native.INPUT
|
|
373
|
-
{
|
|
374
|
-
var i = new Native.INPUT(); i.type = Native.
|
|
375
|
-
i.u.
|
|
376
|
-
i
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
i.u.ki.
|
|
386
|
-
i
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
Native.
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
static void
|
|
407
|
-
{
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
if (
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
Thread.Sleep(60);
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
return null;
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
public static object
|
|
524
|
-
{
|
|
525
|
-
var
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
if (restore
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
d["
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
return true;
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
return true;
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
{
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
var
|
|
811
|
-
{
|
|
812
|
-
try
|
|
813
|
-
{
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
var
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
}
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
var
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
Ui(delegate
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
return
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
}
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1
|
+
// dsh-computer-use helper: a long-lived Windows process driven over stdin/stdout
|
|
2
|
+
// (one JSON object per line). Compiled on first use with the .NET Framework
|
|
3
|
+
// csc.exe that ships with Windows, so the npm package carries no native binary.
|
|
4
|
+
//
|
|
5
|
+
// Language level: C# 5 (no string interpolation, no ?. operator, no expression
|
|
6
|
+
// bodies) because that is what the in-box csc.exe understands.
|
|
7
|
+
//
|
|
8
|
+
// Request: {"id": 1, "cmd": "screenshot", ...}
|
|
9
|
+
// Response: {"id": 1, "ok": true, "result": {...}} | {"id": 1, "ok": false, "error": "..."}
|
|
10
|
+
// Event: {"event": "stop", "reason": "esc"} (no id)
|
|
11
|
+
//
|
|
12
|
+
// All coordinates are physical pixels in virtual-screen space: the process is
|
|
13
|
+
// Per-Monitor-V2 DPI aware before any GDI / WinForms code runs.
|
|
14
|
+
|
|
15
|
+
using System;
|
|
16
|
+
using System.Collections;
|
|
17
|
+
using System.Collections.Generic;
|
|
18
|
+
using System.Diagnostics;
|
|
19
|
+
using System.Drawing;
|
|
20
|
+
using System.Drawing.Drawing2D;
|
|
21
|
+
using System.Drawing.Imaging;
|
|
22
|
+
using System.Drawing.Text;
|
|
23
|
+
using System.IO;
|
|
24
|
+
using System.Runtime.InteropServices;
|
|
25
|
+
using System.Text;
|
|
26
|
+
using System.Threading;
|
|
27
|
+
using System.Web.Script.Serialization;
|
|
28
|
+
using System.Windows.Automation;
|
|
29
|
+
using System.Windows.Forms;
|
|
30
|
+
|
|
31
|
+
namespace DshComputerUse
|
|
32
|
+
{
|
|
33
|
+
internal static class Native
|
|
34
|
+
{
|
|
35
|
+
[DllImport("user32.dll")] public static extern bool SetProcessDpiAwarenessContext(IntPtr value);
|
|
36
|
+
[DllImport("user32.dll")] public static extern bool SetProcessDPIAware();
|
|
37
|
+
|
|
38
|
+
[StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; public POINT(int x, int y) { X = x; Y = y; } }
|
|
39
|
+
[StructLayout(LayoutKind.Sequential)] public struct SIZE { public int cx; public int cy; public SIZE(int w, int h) { cx = w; cy = h; } }
|
|
40
|
+
[StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left, Top, Right, Bottom; }
|
|
41
|
+
|
|
42
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
43
|
+
public struct MOUSEINPUT { public int dx; public int dy; public uint mouseData; public uint dwFlags; public uint time; public IntPtr dwExtraInfo; }
|
|
44
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
45
|
+
public struct KEYBDINPUT { public ushort wVk; public ushort wScan; public uint dwFlags; public uint time; public IntPtr dwExtraInfo; }
|
|
46
|
+
[StructLayout(LayoutKind.Explicit)]
|
|
47
|
+
public struct InputUnion { [FieldOffset(0)] public MOUSEINPUT mi; [FieldOffset(0)] public KEYBDINPUT ki; }
|
|
48
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
49
|
+
public struct INPUT { public uint type; public InputUnion u; }
|
|
50
|
+
|
|
51
|
+
[DllImport("user32.dll", SetLastError = true)] public static extern uint SendInput(uint n, INPUT[] inputs, int size);
|
|
52
|
+
[DllImport("user32.dll")] public static extern bool SetCursorPos(int x, int y);
|
|
53
|
+
[DllImport("user32.dll")] public static extern bool GetCursorPos(out POINT p);
|
|
54
|
+
[DllImport("user32.dll")] public static extern uint MapVirtualKey(uint code, uint mapType);
|
|
55
|
+
|
|
56
|
+
public delegate bool MonitorEnumProc(IntPtr hMonitor, IntPtr hdc, ref RECT rect, IntPtr data);
|
|
57
|
+
[DllImport("user32.dll")] public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr clip, MonitorEnumProc proc, IntPtr data);
|
|
58
|
+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
59
|
+
public struct MONITORINFOEX { public int cbSize; public RECT rcMonitor; public RECT rcWork; public uint dwFlags; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string szDevice; }
|
|
60
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFOEX info);
|
|
61
|
+
[DllImport("shcore.dll")] public static extern int GetDpiForMonitor(IntPtr hMonitor, int type, out uint dpiX, out uint dpiY);
|
|
62
|
+
|
|
63
|
+
[DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hwnd);
|
|
64
|
+
[DllImport("user32.dll")] public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
|
|
65
|
+
[DllImport("gdi32.dll")] public static extern bool BitBlt(IntPtr dst, int x, int y, int w, int h, IntPtr src, int sx, int sy, uint rop);
|
|
66
|
+
[DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
|
|
67
|
+
[DllImport("gdi32.dll")] public static extern bool DeleteDC(IntPtr hdc);
|
|
68
|
+
[DllImport("gdi32.dll")] public static extern IntPtr SelectObject(IntPtr hdc, IntPtr obj);
|
|
69
|
+
[DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr obj);
|
|
70
|
+
|
|
71
|
+
public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr data);
|
|
72
|
+
[DllImport("user32.dll")] public static extern bool EnumWindows(EnumWindowsProc proc, IntPtr data);
|
|
73
|
+
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hwnd);
|
|
74
|
+
[DllImport("user32.dll")] public static extern bool IsWindow(IntPtr hwnd);
|
|
75
|
+
[DllImport("user32.dll")] public static extern bool IsIconic(IntPtr hwnd);
|
|
76
|
+
[DllImport("user32.dll")] public static extern bool IsZoomed(IntPtr hwnd);
|
|
77
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int GetWindowText(IntPtr hwnd, StringBuilder sb, int max);
|
|
78
|
+
[DllImport("user32.dll")] public static extern int GetWindowTextLength(IntPtr hwnd);
|
|
79
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int GetClassName(IntPtr hwnd, StringBuilder sb, int max);
|
|
80
|
+
[DllImport("user32.dll")] public static extern IntPtr GetWindow(IntPtr hwnd, uint cmd);
|
|
81
|
+
[DllImport("user32.dll")] public static extern int GetWindowLong(IntPtr hwnd, int index);
|
|
82
|
+
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hwnd, out uint pid);
|
|
83
|
+
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
|
|
84
|
+
[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hwnd);
|
|
85
|
+
[DllImport("user32.dll")] public static extern bool BringWindowToTop(IntPtr hwnd);
|
|
86
|
+
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hwnd, int cmd);
|
|
87
|
+
[DllImport("user32.dll")] public static extern void SwitchToThisWindow(IntPtr hwnd, bool altTab);
|
|
88
|
+
[DllImport("user32.dll")] public static extern bool AttachThreadInput(uint a, uint b, bool attach);
|
|
89
|
+
[DllImport("kernel32.dll")] public static extern uint GetCurrentThreadId();
|
|
90
|
+
[DllImport("user32.dll")] public static extern bool PostMessage(IntPtr hwnd, uint msg, IntPtr w, IntPtr l);
|
|
91
|
+
[DllImport("user32.dll")] public static extern IntPtr WindowFromPoint(POINT p);
|
|
92
|
+
[DllImport("user32.dll")] public static extern IntPtr GetAncestor(IntPtr hwnd, uint flags);
|
|
93
|
+
[DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hwnd, IntPtr after, int x, int y, int w, int h, uint flags);
|
|
94
|
+
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr hwnd, out RECT r);
|
|
95
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
96
|
+
public struct WINDOWPLACEMENT { public int length; public int flags; public int showCmd; public POINT ptMinPosition; public POINT ptMaxPosition; public RECT rcNormalPosition; }
|
|
97
|
+
[DllImport("user32.dll")] public static extern bool GetWindowPlacement(IntPtr hwnd, ref WINDOWPLACEMENT wp);
|
|
98
|
+
[DllImport("user32.dll")] public static extern bool SetWindowPlacement(IntPtr hwnd, ref WINDOWPLACEMENT wp);
|
|
99
|
+
[DllImport("dwmapi.dll")] public static extern int DwmGetWindowAttribute(IntPtr hwnd, int attr, out RECT value, int size);
|
|
100
|
+
[DllImport("dwmapi.dll")] public static extern int DwmGetWindowAttribute(IntPtr hwnd, int attr, out int value, int size);
|
|
101
|
+
|
|
102
|
+
[DllImport("kernel32.dll")] public static extern IntPtr OpenProcess(uint access, bool inherit, uint pid);
|
|
103
|
+
[DllImport("kernel32.dll")] public static extern bool CloseHandle(IntPtr h);
|
|
104
|
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] public static extern bool QueryFullProcessImageName(IntPtr h, int flags, StringBuilder sb, ref int size);
|
|
105
|
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr GetModuleHandle(string name);
|
|
106
|
+
|
|
107
|
+
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
108
|
+
public struct BLENDFUNCTION { public byte BlendOp; public byte BlendFlags; public byte SourceConstantAlpha; public byte AlphaFormat; }
|
|
109
|
+
[DllImport("user32.dll", SetLastError = true)]
|
|
110
|
+
public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pptSrc, int crKey, ref BLENDFUNCTION pblend, int flags);
|
|
111
|
+
[DllImport("user32.dll", SetLastError = true)]
|
|
112
|
+
public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, IntPtr pptDst, IntPtr psize, IntPtr hdcSrc, IntPtr pptSrc, int crKey, ref BLENDFUNCTION pblend, int flags);
|
|
113
|
+
[DllImport("user32.dll")] public static extern bool SetWindowDisplayAffinity(IntPtr hwnd, uint affinity);
|
|
114
|
+
|
|
115
|
+
public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
|
|
116
|
+
[DllImport("user32.dll", SetLastError = true)] public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc fn, IntPtr hMod, uint threadId);
|
|
117
|
+
[DllImport("user32.dll")] public static extern bool UnhookWindowsHookEx(IntPtr hook);
|
|
118
|
+
[DllImport("user32.dll")] public static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wParam, IntPtr lParam);
|
|
119
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
120
|
+
public struct KBDLLHOOKSTRUCT { public uint vkCode; public uint scanCode; public uint flags; public uint time; public IntPtr dwExtraInfo; }
|
|
121
|
+
|
|
122
|
+
public const uint INPUT_MOUSE = 0, INPUT_KEYBOARD = 1;
|
|
123
|
+
public const uint KEYEVENTF_EXTENDEDKEY = 0x1, KEYEVENTF_KEYUP = 0x2, KEYEVENTF_UNICODE = 0x4;
|
|
124
|
+
public const uint MOUSEEVENTF_MOVE = 0x1, MOUSEEVENTF_LEFTDOWN = 0x2, MOUSEEVENTF_LEFTUP = 0x4, MOUSEEVENTF_RIGHTDOWN = 0x8, MOUSEEVENTF_RIGHTUP = 0x10,
|
|
125
|
+
MOUSEEVENTF_MIDDLEDOWN = 0x20, MOUSEEVENTF_MIDDLEUP = 0x40, MOUSEEVENTF_WHEEL = 0x800, MOUSEEVENTF_HWHEEL = 0x1000;
|
|
126
|
+
public static readonly IntPtr Marker = new IntPtr(0x44534843); // "DSHC"
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
internal static class Args
|
|
130
|
+
{
|
|
131
|
+
public static bool Has(Dictionary<string, object> d, string k) { object v; return d.TryGetValue(k, out v) && v != null; }
|
|
132
|
+
public static int Int(Dictionary<string, object> d, string k, int def)
|
|
133
|
+
{
|
|
134
|
+
object v; if (!d.TryGetValue(k, out v) || v == null) return def;
|
|
135
|
+
return (int)Math.Round(Convert.ToDouble(v));
|
|
136
|
+
}
|
|
137
|
+
public static string Str(Dictionary<string, object> d, string k, string def)
|
|
138
|
+
{
|
|
139
|
+
object v; if (!d.TryGetValue(k, out v) || v == null) return def;
|
|
140
|
+
return Convert.ToString(v);
|
|
141
|
+
}
|
|
142
|
+
public static bool Bool(Dictionary<string, object> d, string k, bool def)
|
|
143
|
+
{
|
|
144
|
+
object v; if (!d.TryGetValue(k, out v) || v == null) return def;
|
|
145
|
+
return Convert.ToBoolean(v);
|
|
146
|
+
}
|
|
147
|
+
public static List<int> Ints(Dictionary<string, object> d, string k)
|
|
148
|
+
{
|
|
149
|
+
var list = new List<int>(); object v;
|
|
150
|
+
if (d.TryGetValue(k, out v) && v is IEnumerable && !(v is string))
|
|
151
|
+
foreach (var item in (IEnumerable)v) list.Add((int)Math.Round(Convert.ToDouble(item)));
|
|
152
|
+
return list;
|
|
153
|
+
}
|
|
154
|
+
public static List<List<int>> IntLists(Dictionary<string, object> d, string k)
|
|
155
|
+
{
|
|
156
|
+
var list = new List<List<int>>(); object v;
|
|
157
|
+
if (d.TryGetValue(k, out v) && v is IEnumerable && !(v is string))
|
|
158
|
+
foreach (var row in (IEnumerable)v)
|
|
159
|
+
{
|
|
160
|
+
var inner = new List<int>();
|
|
161
|
+
foreach (var item in (IEnumerable)row) inner.Add((int)Math.Round(Convert.ToDouble(item)));
|
|
162
|
+
list.Add(inner);
|
|
163
|
+
}
|
|
164
|
+
return list;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
internal static class Program
|
|
169
|
+
{
|
|
170
|
+
static readonly object OutLock = new object();
|
|
171
|
+
static TextWriter Out;
|
|
172
|
+
public const string Version = "1";
|
|
173
|
+
|
|
174
|
+
[STAThread]
|
|
175
|
+
static int Main(string[] argv)
|
|
176
|
+
{
|
|
177
|
+
// Must precede every GDI / WinForms call, or the process is stuck in
|
|
178
|
+
// DPI-virtualised coordinates.
|
|
179
|
+
try { if (!Native.SetProcessDpiAwarenessContext(new IntPtr(-4))) Native.SetProcessDPIAware(); }
|
|
180
|
+
catch (Exception) { try { Native.SetProcessDPIAware(); } catch (Exception) { } }
|
|
181
|
+
|
|
182
|
+
var utf8 = new UTF8Encoding(false);
|
|
183
|
+
var writer = new StreamWriter(Console.OpenStandardOutput(), utf8);
|
|
184
|
+
writer.AutoFlush = true;
|
|
185
|
+
Out = writer;
|
|
186
|
+
var input = new StreamReader(Console.OpenStandardInput(), utf8);
|
|
187
|
+
var parser = new JavaScriptSerializer();
|
|
188
|
+
parser.MaxJsonLength = int.MaxValue;
|
|
189
|
+
|
|
190
|
+
var hello = new Dictionary<string, object>();
|
|
191
|
+
hello["event"] = "ready"; hello["version"] = Version; hello["pid"] = Process.GetCurrentProcess().Id;
|
|
192
|
+
Emit(hello);
|
|
193
|
+
|
|
194
|
+
string line;
|
|
195
|
+
while ((line = input.ReadLine()) != null)
|
|
196
|
+
{
|
|
197
|
+
if (line.Trim().Length == 0) continue;
|
|
198
|
+
object id = null;
|
|
199
|
+
var reply = new Dictionary<string, object>();
|
|
200
|
+
try
|
|
201
|
+
{
|
|
202
|
+
var req = (Dictionary<string, object>)parser.DeserializeObject(line);
|
|
203
|
+
if (req.ContainsKey("id")) id = req["id"];
|
|
204
|
+
reply["id"] = id;
|
|
205
|
+
reply["result"] = Commands.Run(req);
|
|
206
|
+
reply["ok"] = true;
|
|
207
|
+
}
|
|
208
|
+
catch (Exception ex)
|
|
209
|
+
{
|
|
210
|
+
reply["id"] = id;
|
|
211
|
+
reply["ok"] = false;
|
|
212
|
+
reply["error"] = ex.Message;
|
|
213
|
+
}
|
|
214
|
+
Emit(reply);
|
|
215
|
+
}
|
|
216
|
+
Overlay.Shutdown();
|
|
217
|
+
return 0;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
public static void Emit(Dictionary<string, object> msg)
|
|
221
|
+
{
|
|
222
|
+
lock (OutLock)
|
|
223
|
+
{
|
|
224
|
+
var s = new JavaScriptSerializer();
|
|
225
|
+
s.MaxJsonLength = int.MaxValue;
|
|
226
|
+
Out.Write(s.Serialize(msg));
|
|
227
|
+
Out.Write('\n');
|
|
228
|
+
Out.Flush();
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
public static void EmitEvent(string name, string reason)
|
|
233
|
+
{
|
|
234
|
+
ThreadPool.QueueUserWorkItem(delegate
|
|
235
|
+
{
|
|
236
|
+
var e = new Dictionary<string, object>();
|
|
237
|
+
e["event"] = name; e["reason"] = reason;
|
|
238
|
+
try { Emit(e); } catch (Exception) { }
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
internal static class Commands
|
|
244
|
+
{
|
|
245
|
+
public static object Run(Dictionary<string, object> r)
|
|
246
|
+
{
|
|
247
|
+
string cmd = Args.Str(r, "cmd", "");
|
|
248
|
+
switch (cmd)
|
|
249
|
+
{
|
|
250
|
+
case "ping": { var d = new Dictionary<string, object>(); d["version"] = Program.Version; return d; }
|
|
251
|
+
case "displays": return Screen2.Displays();
|
|
252
|
+
case "screenshot": return Screen2.Capture(r);
|
|
253
|
+
case "cursor": { Native.POINT p; Native.GetCursorPos(out p); return Pt(p.X, p.Y); }
|
|
254
|
+
case "move": Overlay.Dodge(Args.Int(r, "x", 0), Args.Int(r, "y", 0)); Input.Move(Args.Int(r, "x", 0), Args.Int(r, "y", 0)); return null;
|
|
255
|
+
case "click": return Input.Click(r);
|
|
256
|
+
case "button": return Input.Button(r);
|
|
257
|
+
case "drag": return Input.Drag(r);
|
|
258
|
+
case "scroll": return Input.Scroll(r);
|
|
259
|
+
case "keys": return Input.Keys(r);
|
|
260
|
+
case "key_state": return Input.KeyState(r);
|
|
261
|
+
case "type": return Input.Type(Args.Str(r, "text", ""), Args.Int(r, "chunkDelay", 8));
|
|
262
|
+
case "paste": return Input.Paste(Args.Str(r, "text", ""), Args.Bool(r, "restore", true));
|
|
263
|
+
case "windows": return Windows.List();
|
|
264
|
+
case "foreground": return Windows.Describe(Native.GetForegroundWindow());
|
|
265
|
+
case "window_at": return Windows.At(Args.Int(r, "x", 0), Args.Int(r, "y", 0));
|
|
266
|
+
case "focus": return Windows.Focus(new IntPtr(Convert.ToInt64(r["hwnd"])));
|
|
267
|
+
case "window_cmd": return Windows.Command(new IntPtr(Convert.ToInt64(r["hwnd"])), Args.Str(r, "op", ""));
|
|
268
|
+
case "apps": return Apps.Search(Args.Str(r, "query", ""), Args.Int(r, "limit", 8));
|
|
269
|
+
case "launch": return Apps.Launch(r);
|
|
270
|
+
case "clipboard_get": { var d = new Dictionary<string, object>(); d["text"] = Clip.Get(); return d; }
|
|
271
|
+
case "clipboard_set": Clip.Set(Args.Str(r, "text", "")); return null;
|
|
272
|
+
case "ui": return Uia.Elements(r);
|
|
273
|
+
case "overlay_show": Overlay.ExcludeFromCapture = Args.Bool(r, "excludeFromCapture", true); Overlay.Show(r); return null;
|
|
274
|
+
case "overlay_pause": Overlay.SetPaused(Args.Bool(r, "paused", false)); return null;
|
|
275
|
+
case "window_card": return Windows.Card(new IntPtr(Convert.ToInt64(r["hwnd"])), Args.Int(r, "x", 0), Args.Int(r, "y", 0), Args.Int(r, "width", 0), Args.Int(r, "height", 0));
|
|
276
|
+
case "window_uncard": return Windows.Uncard(new IntPtr(Convert.ToInt64(r["hwnd"])));
|
|
277
|
+
case "overlay_status": Overlay.Status(Args.Str(r, "status", "")); return null;
|
|
278
|
+
case "overlay_hide": Overlay.Hide(); return null;
|
|
279
|
+
default: throw new Exception("unknown command: " + cmd);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
public static Dictionary<string, object> Pt(int x, int y) { var d = new Dictionary<string, object>(); d["x"] = x; d["y"] = y; return d; }
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
internal static class Screen2
|
|
287
|
+
{
|
|
288
|
+
public static List<object> Displays()
|
|
289
|
+
{
|
|
290
|
+
var list = new List<object>();
|
|
291
|
+
Native.MonitorEnumProc cb = delegate (IntPtr h, IntPtr hdc, ref Native.RECT rc, IntPtr data)
|
|
292
|
+
{
|
|
293
|
+
var info = new Native.MONITORINFOEX();
|
|
294
|
+
info.cbSize = Marshal.SizeOf(typeof(Native.MONITORINFOEX));
|
|
295
|
+
Native.GetMonitorInfo(h, ref info);
|
|
296
|
+
uint dx = 96, dy = 96;
|
|
297
|
+
try { Native.GetDpiForMonitor(h, 0, out dx, out dy); } catch (Exception) { }
|
|
298
|
+
var d = new Dictionary<string, object>();
|
|
299
|
+
d["x"] = info.rcMonitor.Left; d["y"] = info.rcMonitor.Top;
|
|
300
|
+
d["width"] = info.rcMonitor.Right - info.rcMonitor.Left; d["height"] = info.rcMonitor.Bottom - info.rcMonitor.Top;
|
|
301
|
+
d["workX"] = info.rcWork.Left; d["workY"] = info.rcWork.Top;
|
|
302
|
+
d["workWidth"] = info.rcWork.Right - info.rcWork.Left; d["workHeight"] = info.rcWork.Bottom - info.rcWork.Top;
|
|
303
|
+
d["primary"] = (info.dwFlags & 1) != 0;
|
|
304
|
+
d["dpi"] = (int)dx;
|
|
305
|
+
d["name"] = info.szDevice;
|
|
306
|
+
list.Add(d);
|
|
307
|
+
return true;
|
|
308
|
+
};
|
|
309
|
+
Native.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, cb, IntPtr.Zero);
|
|
310
|
+
GC.KeepAlive(cb);
|
|
311
|
+
return list;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
static ImageCodecInfo jpegCodec;
|
|
315
|
+
|
|
316
|
+
public static Dictionary<string, object> Capture(Dictionary<string, object> r)
|
|
317
|
+
{
|
|
318
|
+
int x = Args.Int(r, "x", 0), y = Args.Int(r, "y", 0), w = Args.Int(r, "width", 0), h = Args.Int(r, "height", 0);
|
|
319
|
+
int ow = Args.Int(r, "outWidth", w), oh = Args.Int(r, "outHeight", h);
|
|
320
|
+
int quality = Args.Int(r, "quality", 80);
|
|
321
|
+
if (w <= 0 || h <= 0 || ow <= 0 || oh <= 0) throw new Exception("invalid capture size");
|
|
322
|
+
using (var bmp = new Bitmap(w, h, PixelFormat.Format24bppRgb))
|
|
323
|
+
{
|
|
324
|
+
using (var g = Graphics.FromImage(bmp))
|
|
325
|
+
{
|
|
326
|
+
IntPtr dst = g.GetHdc();
|
|
327
|
+
IntPtr src = Native.GetDC(IntPtr.Zero);
|
|
328
|
+
try { Native.BitBlt(dst, 0, 0, w, h, src, x, y, 0x00CC0020 | 0x40000000); }
|
|
329
|
+
finally { Native.ReleaseDC(IntPtr.Zero, src); g.ReleaseHdc(dst); }
|
|
330
|
+
}
|
|
331
|
+
Bitmap output = bmp;
|
|
332
|
+
Bitmap scaled = null;
|
|
333
|
+
try
|
|
334
|
+
{
|
|
335
|
+
if (ow != w || oh != h)
|
|
336
|
+
{
|
|
337
|
+
scaled = new Bitmap(ow, oh, PixelFormat.Format24bppRgb);
|
|
338
|
+
using (var g2 = Graphics.FromImage(scaled))
|
|
339
|
+
using (var attrs = new ImageAttributes())
|
|
340
|
+
{
|
|
341
|
+
attrs.SetWrapMode(WrapMode.TileFlipXY);
|
|
342
|
+
g2.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
343
|
+
g2.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
|
344
|
+
g2.CompositingQuality = CompositingQuality.HighQuality;
|
|
345
|
+
g2.DrawImage(bmp, new Rectangle(0, 0, ow, oh), 0, 0, w, h, GraphicsUnit.Pixel, attrs);
|
|
346
|
+
}
|
|
347
|
+
output = scaled;
|
|
348
|
+
}
|
|
349
|
+
if (jpegCodec == null)
|
|
350
|
+
foreach (var c in ImageCodecInfo.GetImageEncoders()) if (c.MimeType == "image/jpeg") jpegCodec = c;
|
|
351
|
+
using (var ms = new MemoryStream())
|
|
352
|
+
using (var ep = new EncoderParameters(1))
|
|
353
|
+
{
|
|
354
|
+
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
|
|
355
|
+
output.Save(ms, jpegCodec, ep);
|
|
356
|
+
var d = new Dictionary<string, object>();
|
|
357
|
+
d["data"] = Convert.ToBase64String(ms.ToArray());
|
|
358
|
+
d["width"] = ow; d["height"] = oh;
|
|
359
|
+
return d;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
finally { if (scaled != null) scaled.Dispose(); }
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
internal static class Input
|
|
368
|
+
{
|
|
369
|
+
static readonly int InputSize = Marshal.SizeOf(typeof(Native.INPUT));
|
|
370
|
+
static readonly HashSet<int> Extended = new HashSet<int> { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x2C, 0x2D, 0x2E, 0x5B, 0x5C, 0x5D, 0x6F, 0x90, 0xA3, 0xA5 };
|
|
371
|
+
|
|
372
|
+
static Native.INPUT Mouse(uint flags, uint data)
|
|
373
|
+
{
|
|
374
|
+
var i = new Native.INPUT(); i.type = Native.INPUT_MOUSE;
|
|
375
|
+
i.u.mi.dwFlags = flags; i.u.mi.mouseData = data; i.u.mi.dwExtraInfo = Native.Marker;
|
|
376
|
+
return i;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
static Native.INPUT Key(int vk, bool up)
|
|
380
|
+
{
|
|
381
|
+
var i = new Native.INPUT(); i.type = Native.INPUT_KEYBOARD;
|
|
382
|
+
i.u.ki.wVk = (ushort)vk;
|
|
383
|
+
i.u.ki.wScan = (ushort)Native.MapVirtualKey((uint)vk, 0);
|
|
384
|
+
i.u.ki.dwFlags = (up ? Native.KEYEVENTF_KEYUP : 0) | (Extended.Contains(vk) ? Native.KEYEVENTF_EXTENDEDKEY : 0);
|
|
385
|
+
i.u.ki.dwExtraInfo = Native.Marker;
|
|
386
|
+
return i;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
static Native.INPUT Unicode(char c, bool up)
|
|
390
|
+
{
|
|
391
|
+
var i = new Native.INPUT(); i.type = Native.INPUT_KEYBOARD;
|
|
392
|
+
i.u.ki.wVk = 0; i.u.ki.wScan = c;
|
|
393
|
+
i.u.ki.dwFlags = Native.KEYEVENTF_UNICODE | (up ? Native.KEYEVENTF_KEYUP : 0);
|
|
394
|
+
i.u.ki.dwExtraInfo = Native.Marker;
|
|
395
|
+
return i;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
static void Send(List<Native.INPUT> inputs)
|
|
399
|
+
{
|
|
400
|
+
if (inputs.Count == 0) return;
|
|
401
|
+
uint sent = Native.SendInput((uint)inputs.Count, inputs.ToArray(), InputSize);
|
|
402
|
+
if (sent != inputs.Count)
|
|
403
|
+
throw new Exception("SendInput was blocked (error " + Marshal.GetLastWin32Error() + "). The target may be running elevated (as administrator) or the secure desktop (UAC / lock screen) is active.");
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
public static void Move(int x, int y)
|
|
407
|
+
{
|
|
408
|
+
Native.SetCursorPos(x, y);
|
|
409
|
+
// A zero relative move lets apps that only watch WM_MOUSEMOVE notice the cursor.
|
|
410
|
+
Send(new List<Native.INPUT> { Mouse(Native.MOUSEEVENTF_MOVE, 0) });
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
static void Flags(string button, out uint down, out uint up)
|
|
414
|
+
{
|
|
415
|
+
switch (button)
|
|
416
|
+
{
|
|
417
|
+
case "right": down = Native.MOUSEEVENTF_RIGHTDOWN; up = Native.MOUSEEVENTF_RIGHTUP; break;
|
|
418
|
+
case "middle": down = Native.MOUSEEVENTF_MIDDLEDOWN; up = Native.MOUSEEVENTF_MIDDLEUP; break;
|
|
419
|
+
default: down = Native.MOUSEEVENTF_LEFTDOWN; up = Native.MOUSEEVENTF_LEFTUP; break;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
static void Modifiers(List<int> vks, bool up)
|
|
424
|
+
{
|
|
425
|
+
var list = new List<Native.INPUT>();
|
|
426
|
+
if (up) for (int i = vks.Count - 1; i >= 0; i--) list.Add(Key(vks[i], true));
|
|
427
|
+
else foreach (var vk in vks) list.Add(Key(vk, false));
|
|
428
|
+
Send(list);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
public static object Click(Dictionary<string, object> r)
|
|
432
|
+
{
|
|
433
|
+
if (Args.Has(r, "x"))
|
|
434
|
+
{
|
|
435
|
+
int x = Args.Int(r, "x", 0), y = Args.Int(r, "y", 0);
|
|
436
|
+
Overlay.Dodge(x, y);
|
|
437
|
+
Move(x, y);
|
|
438
|
+
Thread.Sleep(30);
|
|
439
|
+
}
|
|
440
|
+
uint down, up; Flags(Args.Str(r, "button", "left"), out down, out up);
|
|
441
|
+
int count = Math.Max(1, Math.Min(3, Args.Int(r, "count", 1)));
|
|
442
|
+
var mods = Args.Ints(r, "modifiers");
|
|
443
|
+
Modifiers(mods, false);
|
|
444
|
+
try
|
|
445
|
+
{
|
|
446
|
+
for (int i = 0; i < count; i++)
|
|
447
|
+
{
|
|
448
|
+
Send(new List<Native.INPUT> { Mouse(down, 0) });
|
|
449
|
+
Thread.Sleep(20);
|
|
450
|
+
Send(new List<Native.INPUT> { Mouse(up, 0) });
|
|
451
|
+
if (i < count - 1) Thread.Sleep(40);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
finally { Modifiers(mods, true); }
|
|
455
|
+
return null;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
public static object Button(Dictionary<string, object> r)
|
|
459
|
+
{
|
|
460
|
+
if (Args.Has(r, "x")) { int x = Args.Int(r, "x", 0), y = Args.Int(r, "y", 0); Overlay.Dodge(x, y); Move(x, y); Thread.Sleep(20); }
|
|
461
|
+
uint down, up; Flags(Args.Str(r, "button", "left"), out down, out up);
|
|
462
|
+
Send(new List<Native.INPUT> { Mouse(Args.Bool(r, "up", false) ? up : down, 0) });
|
|
463
|
+
return null;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
public static object Drag(Dictionary<string, object> r)
|
|
467
|
+
{
|
|
468
|
+
int x1 = Args.Int(r, "x1", 0), y1 = Args.Int(r, "y1", 0), x2 = Args.Int(r, "x2", 0), y2 = Args.Int(r, "y2", 0);
|
|
469
|
+
uint down, up; Flags(Args.Str(r, "button", "left"), out down, out up);
|
|
470
|
+
Overlay.Dodge(x1, y1); Overlay.Dodge(x2, y2);
|
|
471
|
+
Move(x1, y1); Thread.Sleep(40);
|
|
472
|
+
Send(new List<Native.INPUT> { Mouse(down, 0) });
|
|
473
|
+
try
|
|
474
|
+
{
|
|
475
|
+
Thread.Sleep(60);
|
|
476
|
+
int steps = Math.Max(8, Math.Min(40, (int)(Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) / 20)));
|
|
477
|
+
for (int i = 1; i <= steps; i++)
|
|
478
|
+
{
|
|
479
|
+
Move(x1 + (x2 - x1) * i / steps, y1 + (y2 - y1) * i / steps);
|
|
480
|
+
Thread.Sleep(12);
|
|
481
|
+
}
|
|
482
|
+
Thread.Sleep(60);
|
|
483
|
+
}
|
|
484
|
+
finally { Send(new List<Native.INPUT> { Mouse(up, 0) }); }
|
|
485
|
+
return null;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
public static object Scroll(Dictionary<string, object> r)
|
|
489
|
+
{
|
|
490
|
+
if (Args.Has(r, "x")) { int x = Args.Int(r, "x", 0), y = Args.Int(r, "y", 0); Overlay.Dodge(x, y); Move(x, y); Thread.Sleep(30); }
|
|
491
|
+
int dx = Args.Int(r, "dx", 0), dy = Args.Int(r, "dy", 0);
|
|
492
|
+
var mods = Args.Ints(r, "modifiers");
|
|
493
|
+
Modifiers(mods, false);
|
|
494
|
+
try
|
|
495
|
+
{
|
|
496
|
+
// One notch per event, spaced out, so smooth-scrolling apps keep up.
|
|
497
|
+
for (int i = 0; i < Math.Abs(dy); i++) { Send(new List<Native.INPUT> { Mouse(Native.MOUSEEVENTF_WHEEL, (uint)(dy > 0 ? -120 : 120)) }); Thread.Sleep(15); }
|
|
498
|
+
for (int i = 0; i < Math.Abs(dx); i++) { Send(new List<Native.INPUT> { Mouse(Native.MOUSEEVENTF_HWHEEL, (uint)(dx > 0 ? 120 : -120)) }); Thread.Sleep(15); }
|
|
499
|
+
}
|
|
500
|
+
finally { Modifiers(mods, true); }
|
|
501
|
+
return null;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// combos: [[vk, vk, ...], ...] — each combo presses in order and releases in reverse.
|
|
505
|
+
public static object Keys(Dictionary<string, object> r)
|
|
506
|
+
{
|
|
507
|
+
var combos = Args.IntLists(r, "combos");
|
|
508
|
+
int hold = Args.Int(r, "holdMs", 0);
|
|
509
|
+
foreach (var combo in combos)
|
|
510
|
+
{
|
|
511
|
+
var downs = new List<Native.INPUT>();
|
|
512
|
+
foreach (var vk in combo) downs.Add(Key(vk, false));
|
|
513
|
+
Send(downs);
|
|
514
|
+
Thread.Sleep(hold > 0 ? hold : 25);
|
|
515
|
+
var ups = new List<Native.INPUT>();
|
|
516
|
+
for (int i = combo.Count - 1; i >= 0; i--) ups.Add(Key(combo[i], true));
|
|
517
|
+
Send(ups);
|
|
518
|
+
Thread.Sleep(30);
|
|
519
|
+
}
|
|
520
|
+
return null;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
public static object KeyState(Dictionary<string, object> r)
|
|
524
|
+
{
|
|
525
|
+
var vks = Args.Ints(r, "vks");
|
|
526
|
+
Modifiers(vks, Args.Bool(r, "up", false));
|
|
527
|
+
return null;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
public static object Type(string text, int chunkDelay)
|
|
531
|
+
{
|
|
532
|
+
var batch = new List<Native.INPUT>();
|
|
533
|
+
Action flush = delegate { Send(batch); batch.Clear(); if (chunkDelay > 0) Thread.Sleep(chunkDelay); };
|
|
534
|
+
for (int i = 0; i < text.Length; i++)
|
|
535
|
+
{
|
|
536
|
+
char c = text[i];
|
|
537
|
+
if (c == '\r') { if (i + 1 < text.Length && text[i + 1] == '\n') continue; c = '\n'; }
|
|
538
|
+
if (c == '\n') { flush(); batch.Add(Key(0x0D, false)); batch.Add(Key(0x0D, true)); flush(); continue; }
|
|
539
|
+
if (c == '\t') { batch.Add(Key(0x09, false)); batch.Add(Key(0x09, true)); continue; }
|
|
540
|
+
batch.Add(Unicode(c, false)); batch.Add(Unicode(c, true));
|
|
541
|
+
if (batch.Count >= 40 && !char.IsHighSurrogate(c)) flush();
|
|
542
|
+
}
|
|
543
|
+
flush();
|
|
544
|
+
return null;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
public static object Paste(string text, bool restore)
|
|
548
|
+
{
|
|
549
|
+
string previous = null;
|
|
550
|
+
if (restore) { try { previous = Clip.Get(); } catch (Exception) { } }
|
|
551
|
+
Clip.Set(text);
|
|
552
|
+
Thread.Sleep(40);
|
|
553
|
+
Send(new List<Native.INPUT> { Key(0x11, false), Key(0x56, false) });
|
|
554
|
+
Thread.Sleep(30);
|
|
555
|
+
Send(new List<Native.INPUT> { Key(0x56, true), Key(0x11, true) });
|
|
556
|
+
Thread.Sleep(250);
|
|
557
|
+
if (restore && previous != null) { try { Clip.Set(previous); } catch (Exception) { } }
|
|
558
|
+
return null;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
internal static class Clip
|
|
563
|
+
{
|
|
564
|
+
public static string Get()
|
|
565
|
+
{
|
|
566
|
+
for (int i = 0; i < 5; i++)
|
|
567
|
+
{
|
|
568
|
+
try { return Clipboard.ContainsText() ? Clipboard.GetText() : ""; }
|
|
569
|
+
catch (ExternalException) { Thread.Sleep(50); }
|
|
570
|
+
}
|
|
571
|
+
throw new Exception("clipboard is busy");
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
public static void Set(string text)
|
|
575
|
+
{
|
|
576
|
+
if (!string.IsNullOrEmpty(text)) text = text.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n");
|
|
577
|
+
for (int i = 0; i < 5; i++)
|
|
578
|
+
{
|
|
579
|
+
try
|
|
580
|
+
{
|
|
581
|
+
if (string.IsNullOrEmpty(text)) Clipboard.Clear(); else Clipboard.SetText(text);
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
catch (ExternalException) { Thread.Sleep(50); }
|
|
585
|
+
}
|
|
586
|
+
throw new Exception("clipboard is busy");
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
internal static class Windows
|
|
591
|
+
{
|
|
592
|
+
public static string ExePath(uint pid)
|
|
593
|
+
{
|
|
594
|
+
IntPtr h = Native.OpenProcess(0x1000, false, pid);
|
|
595
|
+
if (h == IntPtr.Zero) return "";
|
|
596
|
+
try
|
|
597
|
+
{
|
|
598
|
+
var sb = new StringBuilder(1024); int size = sb.Capacity;
|
|
599
|
+
return Native.QueryFullProcessImageName(h, 0, sb, ref size) ? sb.ToString() : "";
|
|
600
|
+
}
|
|
601
|
+
finally { Native.CloseHandle(h); }
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
public static Dictionary<string, object> Describe(IntPtr hwnd)
|
|
605
|
+
{
|
|
606
|
+
var d = new Dictionary<string, object>();
|
|
607
|
+
if (hwnd == IntPtr.Zero || !Native.IsWindow(hwnd)) return d;
|
|
608
|
+
uint pid; Native.GetWindowThreadProcessId(hwnd, out pid);
|
|
609
|
+
int len = Native.GetWindowTextLength(hwnd);
|
|
610
|
+
var title = new StringBuilder(Math.Max(len + 1, 2)); Native.GetWindowText(hwnd, title, title.Capacity);
|
|
611
|
+
var cls = new StringBuilder(256); Native.GetClassName(hwnd, cls, cls.Capacity);
|
|
612
|
+
Native.RECT rc;
|
|
613
|
+
if (Native.DwmGetWindowAttribute(hwnd, 9, out rc, Marshal.SizeOf(typeof(Native.RECT))) != 0) Native.GetWindowRect(hwnd, out rc);
|
|
614
|
+
string path = ExePath(pid);
|
|
615
|
+
d["hwnd"] = hwnd.ToInt64();
|
|
616
|
+
d["title"] = title.ToString();
|
|
617
|
+
d["className"] = cls.ToString();
|
|
618
|
+
d["pid"] = (long)pid;
|
|
619
|
+
d["exe"] = path.Length > 0 ? Path.GetFileName(path) : "";
|
|
620
|
+
d["path"] = path;
|
|
621
|
+
d["x"] = rc.Left; d["y"] = rc.Top; d["width"] = rc.Right - rc.Left; d["height"] = rc.Bottom - rc.Top;
|
|
622
|
+
d["minimized"] = Native.IsIconic(hwnd);
|
|
623
|
+
d["maximized"] = Native.IsZoomed(hwnd);
|
|
624
|
+
return d;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
public static List<object> List()
|
|
628
|
+
{
|
|
629
|
+
var list = new List<object>();
|
|
630
|
+
IntPtr fg = Native.GetForegroundWindow();
|
|
631
|
+
uint self = (uint)Process.GetCurrentProcess().Id;
|
|
632
|
+
Native.EnumWindowsProc cb = delegate (IntPtr h, IntPtr data)
|
|
633
|
+
{
|
|
634
|
+
if (!Native.IsWindowVisible(h) || Native.GetWindowTextLength(h) == 0) return true;
|
|
635
|
+
if (Native.GetWindow(h, 4) != IntPtr.Zero) return true; // owned popup
|
|
636
|
+
int ex = Native.GetWindowLong(h, -20);
|
|
637
|
+
if ((ex & 0x80) != 0 && (ex & 0x40000) == 0) return true; // tool window
|
|
638
|
+
int cloaked;
|
|
639
|
+
if (Native.DwmGetWindowAttribute(h, 14, out cloaked, 4) == 0 && cloaked != 0) return true;
|
|
640
|
+
uint pid; Native.GetWindowThreadProcessId(h, out pid);
|
|
641
|
+
if (pid == self) return true;
|
|
642
|
+
var d = Describe(h);
|
|
643
|
+
d["foreground"] = h == fg;
|
|
644
|
+
list.Add(d);
|
|
645
|
+
return true;
|
|
646
|
+
};
|
|
647
|
+
Native.EnumWindows(cb, IntPtr.Zero);
|
|
648
|
+
GC.KeepAlive(cb);
|
|
649
|
+
return list;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
public static Dictionary<string, object> At(int x, int y)
|
|
653
|
+
{
|
|
654
|
+
IntPtr h = Native.WindowFromPoint(new Native.POINT(x, y));
|
|
655
|
+
if (h != IntPtr.Zero) h = Native.GetAncestor(h, 2); // GA_ROOT
|
|
656
|
+
uint pid = 0;
|
|
657
|
+
if (h != IntPtr.Zero) Native.GetWindowThreadProcessId(h, out pid);
|
|
658
|
+
if (h == IntPtr.Zero || pid == (uint)Process.GetCurrentProcess().Id)
|
|
659
|
+
{
|
|
660
|
+
// Our own status pill is under the point (it dodges before the
|
|
661
|
+
// click): report the topmost foreign window there instead.
|
|
662
|
+
IntPtr found = IntPtr.Zero;
|
|
663
|
+
uint self = (uint)Process.GetCurrentProcess().Id;
|
|
664
|
+
Native.EnumWindowsProc cb = delegate (IntPtr w, IntPtr data)
|
|
665
|
+
{
|
|
666
|
+
if (!Native.IsWindowVisible(w)) return true;
|
|
667
|
+
uint p; Native.GetWindowThreadProcessId(w, out p);
|
|
668
|
+
if (p == self) return true;
|
|
669
|
+
if ((Native.GetWindowLong(w, -20) & 0x20) != 0) return true; // WS_EX_TRANSPARENT
|
|
670
|
+
int cloaked;
|
|
671
|
+
if (Native.DwmGetWindowAttribute(w, 14, out cloaked, 4) == 0 && cloaked != 0) return true;
|
|
672
|
+
Native.RECT rc; Native.GetWindowRect(w, out rc);
|
|
673
|
+
if (x >= rc.Left && x < rc.Right && y >= rc.Top && y < rc.Bottom) { found = w; return false; }
|
|
674
|
+
return true;
|
|
675
|
+
};
|
|
676
|
+
Native.EnumWindows(cb, IntPtr.Zero);
|
|
677
|
+
GC.KeepAlive(cb);
|
|
678
|
+
h = found;
|
|
679
|
+
}
|
|
680
|
+
return Describe(h);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
public static Dictionary<string, object> Focus(IntPtr hwnd)
|
|
684
|
+
{
|
|
685
|
+
if (!Native.IsWindow(hwnd)) throw new Exception("window no longer exists");
|
|
686
|
+
if (Native.IsIconic(hwnd)) { Native.ShowWindow(hwnd, 9); Thread.Sleep(150); }
|
|
687
|
+
if (Native.GetForegroundWindow() != hwnd)
|
|
688
|
+
{
|
|
689
|
+
IntPtr fg = Native.GetForegroundWindow();
|
|
690
|
+
uint pid;
|
|
691
|
+
uint fgThread = Native.GetWindowThreadProcessId(fg, out pid);
|
|
692
|
+
uint cur = Native.GetCurrentThreadId();
|
|
693
|
+
bool attached = fgThread != 0 && fgThread != cur && Native.AttachThreadInput(cur, fgThread, true);
|
|
694
|
+
try
|
|
695
|
+
{
|
|
696
|
+
Native.BringWindowToTop(hwnd);
|
|
697
|
+
Native.SetForegroundWindow(hwnd);
|
|
698
|
+
}
|
|
699
|
+
finally { if (attached) Native.AttachThreadInput(cur, fgThread, false); }
|
|
700
|
+
Thread.Sleep(60);
|
|
701
|
+
if (Native.GetForegroundWindow() != hwnd)
|
|
702
|
+
{
|
|
703
|
+
Native.SwitchToThisWindow(hwnd, true);
|
|
704
|
+
Thread.Sleep(120);
|
|
705
|
+
}
|
|
706
|
+
if (Native.GetForegroundWindow() != hwnd)
|
|
707
|
+
{
|
|
708
|
+
// Holding Alt lifts the foreground lock; a no-op VK keeps the
|
|
709
|
+
// Alt release from opening the target's menu bar.
|
|
710
|
+
var inputs = new Native.INPUT[3];
|
|
711
|
+
var down = new Native.INPUT(); down.type = Native.INPUT_KEYBOARD; down.u.ki.wVk = 0x12; down.u.ki.dwExtraInfo = Native.Marker;
|
|
712
|
+
Native.SendInput(1, new[] { down }, Marshal.SizeOf(typeof(Native.INPUT)));
|
|
713
|
+
Native.SetForegroundWindow(hwnd);
|
|
714
|
+
var noop = new Native.INPUT(); noop.type = Native.INPUT_KEYBOARD; noop.u.ki.wVk = 0xFF; noop.u.ki.dwExtraInfo = Native.Marker;
|
|
715
|
+
var noopUp = noop; noopUp.u.ki.dwFlags = Native.KEYEVENTF_KEYUP;
|
|
716
|
+
var up = down; up.u.ki.dwFlags = Native.KEYEVENTF_KEYUP;
|
|
717
|
+
inputs[0] = noop; inputs[1] = noopUp; inputs[2] = up;
|
|
718
|
+
Native.SendInput(3, inputs, Marshal.SizeOf(typeof(Native.INPUT)));
|
|
719
|
+
Thread.Sleep(120);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
var d = Describe(hwnd);
|
|
723
|
+
d["focused"] = Native.GetForegroundWindow() == hwnd;
|
|
724
|
+
return d;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
static readonly Dictionary<long, Native.WINDOWPLACEMENT> cards = new Dictionary<long, Native.WINDOWPLACEMENT>();
|
|
728
|
+
|
|
729
|
+
/// Shrink a window into an always-on-top card; Uncard restores it exactly.
|
|
730
|
+
public static object Card(IntPtr hwnd, int x, int y, int w, int h)
|
|
731
|
+
{
|
|
732
|
+
if (!Native.IsWindow(hwnd)) throw new Exception("window no longer exists");
|
|
733
|
+
if (!cards.ContainsKey(hwnd.ToInt64()))
|
|
734
|
+
{
|
|
735
|
+
var wp = new Native.WINDOWPLACEMENT();
|
|
736
|
+
wp.length = Marshal.SizeOf(typeof(Native.WINDOWPLACEMENT));
|
|
737
|
+
Native.GetWindowPlacement(hwnd, ref wp);
|
|
738
|
+
cards[hwnd.ToInt64()] = wp;
|
|
739
|
+
}
|
|
740
|
+
// A minimized window restores to its previous state, which may be maximized: restore until normal.
|
|
741
|
+
for (int i = 0; i < 2 && (Native.IsIconic(hwnd) || Native.IsZoomed(hwnd)); i++) { Native.ShowWindow(hwnd, 9); Thread.Sleep(150); }
|
|
742
|
+
Native.SetWindowPos(hwnd, new IntPtr(-1), x, y, w, h, 0x0010 | 0x0040); // TOPMOST, NOACTIVATE | SHOWWINDOW
|
|
743
|
+
// Apps enforce a minimum size (Electron does): keep the card anchored bottom-right.
|
|
744
|
+
Native.RECT rc;
|
|
745
|
+
if (Native.GetWindowRect(hwnd, out rc) && (rc.Right - rc.Left != w || rc.Bottom - rc.Top != h))
|
|
746
|
+
{
|
|
747
|
+
int nx = x + w - (rc.Right - rc.Left), ny = y + h - (rc.Bottom - rc.Top);
|
|
748
|
+
Native.SetWindowPos(hwnd, new IntPtr(-1), nx, ny, 0, 0, 0x0001 | 0x0010); // NOSIZE | NOACTIVATE
|
|
749
|
+
}
|
|
750
|
+
return Describe(hwnd);
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
public static object Uncard(IntPtr hwnd)
|
|
754
|
+
{
|
|
755
|
+
Native.WINDOWPLACEMENT wp;
|
|
756
|
+
bool known = cards.TryGetValue(hwnd.ToInt64(), out wp);
|
|
757
|
+
cards.Remove(hwnd.ToInt64());
|
|
758
|
+
if (!Native.IsWindow(hwnd)) return null;
|
|
759
|
+
Native.SetWindowPos(hwnd, new IntPtr(-2), 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0010); // NOTOPMOST
|
|
760
|
+
if (known)
|
|
761
|
+
{
|
|
762
|
+
// Restore the normal rectangle first, then the original show state (maximized etc.).
|
|
763
|
+
int show = wp.showCmd;
|
|
764
|
+
wp.showCmd = 1; // SW_SHOWNORMAL
|
|
765
|
+
Native.SetWindowPlacement(hwnd, ref wp);
|
|
766
|
+
if (show == 3) Native.ShowWindow(hwnd, 3);
|
|
767
|
+
else if (show == 2) Native.ShowWindow(hwnd, 6);
|
|
768
|
+
}
|
|
769
|
+
return Describe(hwnd);
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
public static object Command(IntPtr hwnd, string op)
|
|
773
|
+
{
|
|
774
|
+
if (!Native.IsWindow(hwnd)) throw new Exception("window no longer exists");
|
|
775
|
+
switch (op)
|
|
776
|
+
{
|
|
777
|
+
case "minimize": Native.ShowWindow(hwnd, 6); break;
|
|
778
|
+
case "maximize": Native.ShowWindow(hwnd, 3); break;
|
|
779
|
+
case "restore": Native.ShowWindow(hwnd, 9); break;
|
|
780
|
+
case "close": Native.PostMessage(hwnd, 0x0010, IntPtr.Zero, IntPtr.Zero); break;
|
|
781
|
+
default: throw new Exception("unknown window op: " + op);
|
|
782
|
+
}
|
|
783
|
+
Thread.Sleep(150);
|
|
784
|
+
return Describe(hwnd);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
internal static class Apps
|
|
789
|
+
{
|
|
790
|
+
static List<KeyValuePair<string, string>> cache;
|
|
791
|
+
static DateTime cachedAt;
|
|
792
|
+
|
|
793
|
+
static List<KeyValuePair<string, string>> All()
|
|
794
|
+
{
|
|
795
|
+
if (cache != null && (DateTime.UtcNow - cachedAt).TotalSeconds < 120) return cache;
|
|
796
|
+
var list = new List<KeyValuePair<string, string>>();
|
|
797
|
+
Type t = Type.GetTypeFromProgID("Shell.Application");
|
|
798
|
+
dynamic shell = Activator.CreateInstance(t);
|
|
799
|
+
try
|
|
800
|
+
{
|
|
801
|
+
dynamic folder = shell.NameSpace("shell:AppsFolder");
|
|
802
|
+
foreach (dynamic item in folder.Items())
|
|
803
|
+
{
|
|
804
|
+
string name = item.Name; string path = item.Path;
|
|
805
|
+
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(path)) list.Add(new KeyValuePair<string, string>(name, path));
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
finally { Marshal.FinalReleaseComObject(shell); }
|
|
809
|
+
// Desktop shortcuts too: portable apps often only live there.
|
|
810
|
+
foreach (var folder in new[] { Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory) })
|
|
811
|
+
{
|
|
812
|
+
try
|
|
813
|
+
{
|
|
814
|
+
if (folder.Length == 0 || !Directory.Exists(folder)) continue;
|
|
815
|
+
foreach (var f in Directory.GetFiles(folder, "*.lnk"))
|
|
816
|
+
list.Add(new KeyValuePair<string, string>(Path.GetFileNameWithoutExtension(f), "lnk:" + f));
|
|
817
|
+
}
|
|
818
|
+
catch (Exception) { }
|
|
819
|
+
}
|
|
820
|
+
cache = list; cachedAt = DateTime.UtcNow;
|
|
821
|
+
return list;
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
public static List<object> Search(string query, int limit)
|
|
825
|
+
{
|
|
826
|
+
string q = query.Trim().ToLowerInvariant();
|
|
827
|
+
var scored = new List<KeyValuePair<int, KeyValuePair<string, string>>>();
|
|
828
|
+
foreach (var app in All())
|
|
829
|
+
{
|
|
830
|
+
string n = app.Key.ToLowerInvariant();
|
|
831
|
+
int score = n == q ? 0 : n.StartsWith(q) ? 1 : n.Contains(q) ? 2 : (q.Length > 0 && q.Contains(n)) ? 3 : app.Value.ToLowerInvariant().Contains(q) ? 4 : -1;
|
|
832
|
+
if (q.Length == 0) score = 5;
|
|
833
|
+
if (score >= 0) scored.Add(new KeyValuePair<int, KeyValuePair<string, string>>(score * 1000 + Math.Min(n.Length, 999), app));
|
|
834
|
+
}
|
|
835
|
+
scored.Sort(delegate (KeyValuePair<int, KeyValuePair<string, string>> a, KeyValuePair<int, KeyValuePair<string, string>> b) { return a.Key.CompareTo(b.Key); });
|
|
836
|
+
var result = new List<object>();
|
|
837
|
+
for (int i = 0; i < scored.Count && i < limit; i++)
|
|
838
|
+
{
|
|
839
|
+
var d = new Dictionary<string, object>();
|
|
840
|
+
d["name"] = scored[i].Value.Key; d["id"] = scored[i].Value.Value;
|
|
841
|
+
result.Add(d);
|
|
842
|
+
}
|
|
843
|
+
return result;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
public static object Launch(Dictionary<string, object> r)
|
|
847
|
+
{
|
|
848
|
+
string appId = Args.Str(r, "appId", "");
|
|
849
|
+
string target = Args.Str(r, "target", "");
|
|
850
|
+
string args = Args.Str(r, "args", "");
|
|
851
|
+
ProcessStartInfo psi;
|
|
852
|
+
if (appId.StartsWith("lnk:")) psi = new ProcessStartInfo(appId.Substring(4));
|
|
853
|
+
else if (appId.Length > 0) psi = new ProcessStartInfo("explorer.exe", "shell:AppsFolder\\" + appId);
|
|
854
|
+
else { psi = new ProcessStartInfo(target); if (args.Length > 0) psi.Arguments = args; }
|
|
855
|
+
psi.UseShellExecute = true;
|
|
856
|
+
using (var p = Process.Start(psi)) { }
|
|
857
|
+
return null;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
internal static class Uia
|
|
862
|
+
{
|
|
863
|
+
static readonly ControlType[] Interactive = {
|
|
864
|
+
ControlType.Button, ControlType.Edit, ControlType.Hyperlink, ControlType.MenuItem, ControlType.ListItem,
|
|
865
|
+
ControlType.TabItem, ControlType.CheckBox, ControlType.RadioButton, ControlType.ComboBox, ControlType.TreeItem,
|
|
866
|
+
ControlType.DataItem, ControlType.SplitButton, ControlType.Slider, ControlType.Spinner, ControlType.Document,
|
|
867
|
+
ControlType.MenuBar, ControlType.Menu, ControlType.ScrollBar };
|
|
868
|
+
|
|
869
|
+
public static object Elements(Dictionary<string, object> r)
|
|
870
|
+
{
|
|
871
|
+
IntPtr hwnd = Args.Has(r, "hwnd") ? new IntPtr(Convert.ToInt64(r["hwnd"])) : Native.GetForegroundWindow();
|
|
872
|
+
int max = Args.Int(r, "maxNodes", 150);
|
|
873
|
+
bool all = Args.Bool(r, "includeText", false);
|
|
874
|
+
List<object> result = null; Exception error = null;
|
|
875
|
+
var worker = new Thread(delegate ()
|
|
876
|
+
{
|
|
877
|
+
try
|
|
878
|
+
{
|
|
879
|
+
result = Collect(hwnd, max, all);
|
|
880
|
+
if (result.Count < 4) { Thread.Sleep(400); result = Collect(hwnd, max, all); } // Chromium builds its tree lazily
|
|
881
|
+
}
|
|
882
|
+
catch (Exception ex) { error = ex; }
|
|
883
|
+
});
|
|
884
|
+
worker.IsBackground = true;
|
|
885
|
+
worker.SetApartmentState(ApartmentState.MTA);
|
|
886
|
+
worker.Start();
|
|
887
|
+
if (!worker.Join(Args.Int(r, "timeoutMs", 8000))) throw new Exception("UI Automation timed out on this window");
|
|
888
|
+
if (error != null) throw error;
|
|
889
|
+
var d = Windows.Describe(hwnd);
|
|
890
|
+
d["elements"] = result;
|
|
891
|
+
return d;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
static List<object> Collect(IntPtr hwnd, int max, bool includeText)
|
|
895
|
+
{
|
|
896
|
+
var root = AutomationElement.FromHandle(hwnd);
|
|
897
|
+
var types = new List<Condition>();
|
|
898
|
+
foreach (var t in Interactive) types.Add(new PropertyCondition(AutomationElement.ControlTypeProperty, t));
|
|
899
|
+
if (includeText) types.Add(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text));
|
|
900
|
+
var cond = new AndCondition(new PropertyCondition(AutomationElement.IsOffscreenProperty, false), new OrCondition(types.ToArray()));
|
|
901
|
+
var cache = new CacheRequest();
|
|
902
|
+
cache.Add(AutomationElement.NameProperty);
|
|
903
|
+
cache.Add(AutomationElement.ControlTypeProperty);
|
|
904
|
+
cache.Add(AutomationElement.BoundingRectangleProperty);
|
|
905
|
+
cache.Add(AutomationElement.IsEnabledProperty);
|
|
906
|
+
cache.Add(AutomationElement.AutomationIdProperty);
|
|
907
|
+
cache.Add(ValuePattern.ValueProperty);
|
|
908
|
+
cache.Add(TogglePattern.ToggleStateProperty);
|
|
909
|
+
cache.AutomationElementMode = AutomationElementMode.None;
|
|
910
|
+
cache.TreeScope = TreeScope.Element;
|
|
911
|
+
AutomationElementCollection found;
|
|
912
|
+
using (cache.Activate()) { found = root.FindAll(TreeScope.Descendants, cond); }
|
|
913
|
+
var list = new List<object>();
|
|
914
|
+
foreach (AutomationElement el in found)
|
|
915
|
+
{
|
|
916
|
+
if (list.Count >= max) break;
|
|
917
|
+
var rect = (System.Windows.Rect)el.GetCachedPropertyValue(AutomationElement.BoundingRectangleProperty);
|
|
918
|
+
if (rect.IsEmpty || rect.Width < 2 || rect.Height < 2) continue;
|
|
919
|
+
var type = (ControlType)el.GetCachedPropertyValue(AutomationElement.ControlTypeProperty);
|
|
920
|
+
string name = (el.GetCachedPropertyValue(AutomationElement.NameProperty) as string) ?? "";
|
|
921
|
+
var d = new Dictionary<string, object>();
|
|
922
|
+
d["role"] = type.ProgrammaticName.Replace("ControlType.", "");
|
|
923
|
+
d["name"] = name.Length > 120 ? name.Substring(0, 120) + "…" : name;
|
|
924
|
+
object value = el.GetCachedPropertyValue(ValuePattern.ValueProperty, true);
|
|
925
|
+
if (value is string && ((string)value).Length > 0) { var v = (string)value; d["value"] = v.Length > 120 ? v.Substring(0, 120) + "…" : v; }
|
|
926
|
+
object toggle = el.GetCachedPropertyValue(TogglePattern.ToggleStateProperty, true);
|
|
927
|
+
if (toggle is ToggleState) d["checked"] = (ToggleState)toggle == ToggleState.On;
|
|
928
|
+
if (!(bool)el.GetCachedPropertyValue(AutomationElement.IsEnabledProperty)) d["disabled"] = true;
|
|
929
|
+
if (type == ControlType.Document && name.Length == 0 && !d.ContainsKey("value")) continue;
|
|
930
|
+
d["x"] = (int)rect.X; d["y"] = (int)rect.Y; d["width"] = (int)rect.Width; d["height"] = (int)rect.Height;
|
|
931
|
+
list.Add(d);
|
|
932
|
+
}
|
|
933
|
+
return list;
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
// ---------------------------------------------------------------- overlay
|
|
938
|
+
|
|
939
|
+
internal class LayeredForm : Form
|
|
940
|
+
{
|
|
941
|
+
readonly bool clickThrough;
|
|
942
|
+
public Rectangle Physical;
|
|
943
|
+
|
|
944
|
+
public LayeredForm(bool clickThrough)
|
|
945
|
+
{
|
|
946
|
+
this.clickThrough = clickThrough;
|
|
947
|
+
FormBorderStyle = FormBorderStyle.None;
|
|
948
|
+
ShowInTaskbar = false;
|
|
949
|
+
StartPosition = FormStartPosition.Manual;
|
|
950
|
+
AutoScaleMode = AutoScaleMode.None;
|
|
951
|
+
TopMost = true;
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
protected override bool ShowWithoutActivation { get { return true; } }
|
|
955
|
+
|
|
956
|
+
protected override CreateParams CreateParams
|
|
957
|
+
{
|
|
958
|
+
get
|
|
959
|
+
{
|
|
960
|
+
var cp = base.CreateParams;
|
|
961
|
+
cp.ExStyle |= 0x80000 | 0x80 | 0x8000000 | 0x8; // LAYERED | TOOLWINDOW | NOACTIVATE | TOPMOST
|
|
962
|
+
if (clickThrough) cp.ExStyle |= 0x20; // TRANSPARENT
|
|
963
|
+
return cp;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
protected override void OnHandleCreated(EventArgs e)
|
|
968
|
+
{
|
|
969
|
+
base.OnHandleCreated(e);
|
|
970
|
+
// Keep the overlay out of every screenshot, including our own.
|
|
971
|
+
if (Overlay.ExcludeFromCapture) { try { Native.SetWindowDisplayAffinity(Handle, 0x11); } catch (Exception) { } }
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
protected override void WndProc(ref Message m)
|
|
975
|
+
{
|
|
976
|
+
if (m.Msg == 0x21) { m.Result = new IntPtr(3); return; } // WM_MOUSEACTIVATE -> MA_NOACTIVATE
|
|
977
|
+
base.WndProc(ref m);
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
public void SetBitmap(Bitmap bmp, Rectangle bounds, byte alpha)
|
|
981
|
+
{
|
|
982
|
+
Physical = bounds;
|
|
983
|
+
IntPtr screenDc = Native.GetDC(IntPtr.Zero);
|
|
984
|
+
IntPtr memDc = Native.CreateCompatibleDC(screenDc);
|
|
985
|
+
IntPtr hBitmap = IntPtr.Zero, old = IntPtr.Zero;
|
|
986
|
+
try
|
|
987
|
+
{
|
|
988
|
+
hBitmap = bmp.GetHbitmap(Color.FromArgb(0));
|
|
989
|
+
old = Native.SelectObject(memDc, hBitmap);
|
|
990
|
+
var size = new Native.SIZE(bmp.Width, bmp.Height);
|
|
991
|
+
var src = new Native.POINT(0, 0);
|
|
992
|
+
var dst = new Native.POINT(bounds.X, bounds.Y);
|
|
993
|
+
var blend = new Native.BLENDFUNCTION(); blend.BlendOp = 0; blend.SourceConstantAlpha = alpha; blend.AlphaFormat = 1;
|
|
994
|
+
Native.UpdateLayeredWindow(Handle, screenDc, ref dst, ref size, memDc, ref src, 0, ref blend, 2);
|
|
995
|
+
}
|
|
996
|
+
finally
|
|
997
|
+
{
|
|
998
|
+
Native.ReleaseDC(IntPtr.Zero, screenDc);
|
|
999
|
+
if (hBitmap != IntPtr.Zero) { Native.SelectObject(memDc, old); Native.DeleteObject(hBitmap); }
|
|
1000
|
+
Native.DeleteDC(memDc);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
public void SetAlpha(byte alpha)
|
|
1005
|
+
{
|
|
1006
|
+
var blend = new Native.BLENDFUNCTION(); blend.BlendOp = 0; blend.SourceConstantAlpha = alpha; blend.AlphaFormat = 1;
|
|
1007
|
+
Native.UpdateLayeredWindow(Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 0, ref blend, 2);
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
public void KeepOnTop()
|
|
1011
|
+
{
|
|
1012
|
+
Native.SetWindowPos(Handle, new IntPtr(-1), 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0010 | 0x0200); // NOSIZE|NOMOVE|NOACTIVATE|NOOWNERZORDER
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
internal static class Overlay
|
|
1017
|
+
{
|
|
1018
|
+
static readonly Color Accent = Color.FromArgb(217, 119, 87); // #D97757
|
|
1019
|
+
static readonly Color AccentHi = Color.FromArgb(240, 160, 106); // #F0A06A
|
|
1020
|
+
|
|
1021
|
+
public static volatile bool ExcludeFromCapture = true;
|
|
1022
|
+
static Thread thread;
|
|
1023
|
+
static Control invoker;
|
|
1024
|
+
static readonly List<LayeredForm> glows = new List<LayeredForm>();
|
|
1025
|
+
static LayeredForm pill;
|
|
1026
|
+
static Rectangle pillRect;
|
|
1027
|
+
static readonly List<KeyValuePair<Rectangle, string>> buttons = new List<KeyValuePair<Rectangle, string>>();
|
|
1028
|
+
static bool pillAtBottom;
|
|
1029
|
+
static string label = "", status = "", rendered = "";
|
|
1030
|
+
static Image icon;
|
|
1031
|
+
static string iconPath = "";
|
|
1032
|
+
static int idleMs = 1500;
|
|
1033
|
+
static System.Windows.Forms.Timer timer;
|
|
1034
|
+
static DateTime shownAt;
|
|
1035
|
+
static IntPtr hook = IntPtr.Zero;
|
|
1036
|
+
static Native.LowLevelKeyboardProc hookProc;
|
|
1037
|
+
static volatile bool visible;
|
|
1038
|
+
static volatile bool paused;
|
|
1039
|
+
static int lastTyping = int.MinValue / 2;
|
|
1040
|
+
static int lastEmit;
|
|
1041
|
+
static readonly object RectLock = new object();
|
|
1042
|
+
|
|
1043
|
+
static void Ensure()
|
|
1044
|
+
{
|
|
1045
|
+
if (thread != null) return;
|
|
1046
|
+
var ready = new ManualResetEvent(false);
|
|
1047
|
+
thread = new Thread(delegate ()
|
|
1048
|
+
{
|
|
1049
|
+
invoker = new Control();
|
|
1050
|
+
var force = invoker.Handle;
|
|
1051
|
+
timer = new System.Windows.Forms.Timer();
|
|
1052
|
+
timer.Interval = 40;
|
|
1053
|
+
timer.Tick += delegate { Tick(); };
|
|
1054
|
+
ready.Set();
|
|
1055
|
+
Application.Run();
|
|
1056
|
+
});
|
|
1057
|
+
thread.IsBackground = true;
|
|
1058
|
+
thread.SetApartmentState(ApartmentState.STA);
|
|
1059
|
+
thread.Start();
|
|
1060
|
+
ready.WaitOne();
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
static void Ui(MethodInvoker action) { invoker.Invoke(action); }
|
|
1064
|
+
|
|
1065
|
+
public static void Show(Dictionary<string, object> r)
|
|
1066
|
+
{
|
|
1067
|
+
Ensure();
|
|
1068
|
+
Ui(delegate
|
|
1069
|
+
{
|
|
1070
|
+
label = Args.Str(r, "label", "");
|
|
1071
|
+
status = Args.Str(r, "status", "");
|
|
1072
|
+
idleMs = Args.Int(r, "idleMs", 1500);
|
|
1073
|
+
string path = Args.Str(r, "icon", "");
|
|
1074
|
+
if (path != iconPath)
|
|
1075
|
+
{
|
|
1076
|
+
iconPath = path;
|
|
1077
|
+
if (icon != null) { icon.Dispose(); icon = null; }
|
|
1078
|
+
try
|
|
1079
|
+
{
|
|
1080
|
+
if (path.Length > 0 && File.Exists(path))
|
|
1081
|
+
using (var raw = Image.FromFile(path)) icon = new Bitmap(raw);
|
|
1082
|
+
}
|
|
1083
|
+
catch (Exception) { icon = null; }
|
|
1084
|
+
}
|
|
1085
|
+
if (visible) { RenderPill(); return; }
|
|
1086
|
+
pillAtBottom = false;
|
|
1087
|
+
paused = false;
|
|
1088
|
+
foreach (var g in glows) g.Close();
|
|
1089
|
+
glows.Clear();
|
|
1090
|
+
if (Args.Bool(r, "glow", true))
|
|
1091
|
+
{
|
|
1092
|
+
foreach (Dictionary<string, object> d in Screen2.Displays())
|
|
1093
|
+
{
|
|
1094
|
+
var bounds = new Rectangle((int)d["x"], (int)d["y"], (int)d["width"], (int)d["height"]);
|
|
1095
|
+
var form = new LayeredForm(true);
|
|
1096
|
+
form.Show();
|
|
1097
|
+
using (var bmp = DrawGlow(bounds.Width, bounds.Height, (int)d["dpi"] / 96f)) form.SetBitmap(bmp, bounds, 255);
|
|
1098
|
+
glows.Add(form);
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
if (pill == null)
|
|
1102
|
+
{
|
|
1103
|
+
pill = new LayeredForm(false);
|
|
1104
|
+
pill.MouseUp += delegate (object s, MouseEventArgs e)
|
|
1105
|
+
{
|
|
1106
|
+
foreach (var b in buttons)
|
|
1107
|
+
if (b.Key.Contains(e.Location)) { Press(b.Value); break; }
|
|
1108
|
+
};
|
|
1109
|
+
pill.Show();
|
|
1110
|
+
}
|
|
1111
|
+
RenderPill();
|
|
1112
|
+
InstallHook();
|
|
1113
|
+
shownAt = DateTime.UtcNow;
|
|
1114
|
+
visible = true;
|
|
1115
|
+
timer.Start();
|
|
1116
|
+
});
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
public static void Status(string newStatus)
|
|
1120
|
+
{
|
|
1121
|
+
if (thread == null || !visible) return;
|
|
1122
|
+
Ui(delegate { status = newStatus; RenderPill(); });
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
public static void SetPaused(bool value)
|
|
1126
|
+
{
|
|
1127
|
+
if (thread == null) return;
|
|
1128
|
+
Ui(delegate { paused = value; RenderPill(); });
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
public static void Hide()
|
|
1132
|
+
{
|
|
1133
|
+
if (thread == null) return;
|
|
1134
|
+
Ui(delegate
|
|
1135
|
+
{
|
|
1136
|
+
visible = false;
|
|
1137
|
+
paused = false;
|
|
1138
|
+
timer.Stop();
|
|
1139
|
+
foreach (var g in glows) g.Close();
|
|
1140
|
+
glows.Clear();
|
|
1141
|
+
if (pill != null) { pill.Close(); pill = null; }
|
|
1142
|
+
rendered = "";
|
|
1143
|
+
lock (RectLock) pillRect = Rectangle.Empty;
|
|
1144
|
+
RemoveHook();
|
|
1145
|
+
});
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
public static void Shutdown()
|
|
1149
|
+
{
|
|
1150
|
+
if (thread == null) return;
|
|
1151
|
+
try { Hide(); Ui(delegate { Application.ExitThread(); }); } catch (Exception) { }
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
/// Move the status pill out of the way when an injected pointer action
|
|
1155
|
+
/// targets it: the pill is clickable for the user but invisible to the model.
|
|
1156
|
+
public static void Dodge(int x, int y)
|
|
1157
|
+
{
|
|
1158
|
+
Rectangle r;
|
|
1159
|
+
lock (RectLock) r = pillRect;
|
|
1160
|
+
if (r.IsEmpty) return;
|
|
1161
|
+
var inflated = Rectangle.Inflate(r, 12, 12);
|
|
1162
|
+
if (!inflated.Contains(x, y)) return;
|
|
1163
|
+
Ui(delegate { pillAtBottom = !pillAtBottom; RenderPill(); });
|
|
1164
|
+
Thread.Sleep(30);
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
// pause / resume / stop, from Esc or the pill buttons.
|
|
1168
|
+
static void Press(string action)
|
|
1169
|
+
{
|
|
1170
|
+
if (action == "stop") { paused = false; Program.EmitEvent("stop", "button"); }
|
|
1171
|
+
else if (action == "pause") { paused = true; Program.EmitEvent("pause", "user"); }
|
|
1172
|
+
else if (action == "resume") { paused = false; Program.EmitEvent("resume", "user"); }
|
|
1173
|
+
RenderPill();
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
static bool Typing() { return unchecked(Environment.TickCount - lastTyping) < idleMs; }
|
|
1177
|
+
|
|
1178
|
+
static void Tick()
|
|
1179
|
+
{
|
|
1180
|
+
if (!visible) return;
|
|
1181
|
+
double t = (DateTime.UtcNow - shownAt).TotalSeconds;
|
|
1182
|
+
double fadeIn = Math.Min(1.0, t / 0.35);
|
|
1183
|
+
double breathe = paused ? 0.55 : 0.72 + 0.28 * (0.5 + 0.5 * Math.Sin(t * Math.PI * 2 / 2.6));
|
|
1184
|
+
byte alpha = (byte)Math.Max(0, Math.Min(255, 255 * fadeIn * breathe));
|
|
1185
|
+
foreach (var g in glows) g.SetAlpha(alpha);
|
|
1186
|
+
if (Key() != rendered) RenderPill(); // typing started or stopped
|
|
1187
|
+
if ((int)(t * 25) % 50 == 0)
|
|
1188
|
+
{
|
|
1189
|
+
foreach (var g in glows) g.KeepOnTop();
|
|
1190
|
+
if (pill != null) pill.KeepOnTop();
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
static Bitmap DrawGlow(int w, int h, float scale)
|
|
1195
|
+
{
|
|
1196
|
+
var bmp = new Bitmap(w, h, PixelFormat.Format32bppArgb);
|
|
1197
|
+
using (var g = Graphics.FromImage(bmp))
|
|
1198
|
+
{
|
|
1199
|
+
g.Clear(Color.Transparent);
|
|
1200
|
+
int depth = Math.Max(14, (int)(30 * scale));
|
|
1201
|
+
for (int i = 0; i < depth; i++)
|
|
1202
|
+
{
|
|
1203
|
+
double f = 1.0 - (double)i / depth;
|
|
1204
|
+
int a = (int)(185 * Math.Pow(f, 1.9));
|
|
1205
|
+
if (a <= 0) continue;
|
|
1206
|
+
using (var pen = new Pen(Color.FromArgb(a, Accent), 1))
|
|
1207
|
+
g.DrawRectangle(pen, i, i, w - 1 - 2 * i, h - 1 - 2 * i);
|
|
1208
|
+
}
|
|
1209
|
+
int line = Math.Max(2, (int)Math.Round(2.5 * scale));
|
|
1210
|
+
using (var pen = new Pen(Color.FromArgb(235, AccentHi), line))
|
|
1211
|
+
{
|
|
1212
|
+
pen.Alignment = PenAlignment.Inset;
|
|
1213
|
+
g.DrawRectangle(pen, 0, 0, w, h);
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
return bmp;
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
static GraphicsPath Rounded(RectangleF r, float radius)
|
|
1220
|
+
{
|
|
1221
|
+
var p = new GraphicsPath();
|
|
1222
|
+
float d = radius * 2;
|
|
1223
|
+
p.AddArc(r.X, r.Y, d, d, 180, 90);
|
|
1224
|
+
p.AddArc(r.Right - d, r.Y, d, d, 270, 90);
|
|
1225
|
+
p.AddArc(r.Right - d, r.Bottom - d, d, d, 0, 90);
|
|
1226
|
+
p.AddArc(r.X, r.Bottom - d, d, d, 90, 90);
|
|
1227
|
+
p.CloseFigure();
|
|
1228
|
+
return p;
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
// What the pill should say right now; it is re-rendered whenever this changes.
|
|
1232
|
+
static void Content(out string main, out string sub, out string[] actions, out string[] captions)
|
|
1233
|
+
{
|
|
1234
|
+
if (paused)
|
|
1235
|
+
{
|
|
1236
|
+
main = "已暂停"; sub = "按 Esc 继续";
|
|
1237
|
+
actions = new[] { "resume", "stop" }; captions = new[] { "继续", "停止" };
|
|
1238
|
+
return;
|
|
1239
|
+
}
|
|
1240
|
+
main = label;
|
|
1241
|
+
sub = Typing() ? "你正在操作,已暂停" : status.Length > 0 ? status : "Esc 暂停";
|
|
1242
|
+
actions = new[] { "stop" }; captions = new[] { "停止" };
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
static string Key()
|
|
1246
|
+
{
|
|
1247
|
+
string main, sub; string[] actions, captions;
|
|
1248
|
+
Content(out main, out sub, out actions, out captions);
|
|
1249
|
+
return main + "|" + sub + "|" + string.Join(",", actions) + "|" + pillAtBottom;
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
static void RenderPill()
|
|
1253
|
+
{
|
|
1254
|
+
if (pill == null) return;
|
|
1255
|
+
// The pill lives on the primary display (or the first one).
|
|
1256
|
+
Rectangle work = Rectangle.Empty; float scale = 1f;
|
|
1257
|
+
foreach (Dictionary<string, object> d in Screen2.Displays())
|
|
1258
|
+
{
|
|
1259
|
+
if (work.IsEmpty || (bool)d["primary"])
|
|
1260
|
+
{
|
|
1261
|
+
work = new Rectangle((int)d["workX"], (int)d["workY"], (int)d["workWidth"], (int)d["workHeight"]);
|
|
1262
|
+
scale = (int)d["dpi"] / 96f;
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
string main, sub; string[] actions, captions;
|
|
1266
|
+
Content(out main, out sub, out actions, out captions);
|
|
1267
|
+
rendered = Key();
|
|
1268
|
+
if (sub.Length > 48) sub = sub.Substring(0, 47) + "…";
|
|
1269
|
+
|
|
1270
|
+
float px = 1.333f * scale; // points -> physical pixels
|
|
1271
|
+
using (var fMain = new Font("Microsoft YaHei UI", 10f * px, FontStyle.Bold, GraphicsUnit.Pixel))
|
|
1272
|
+
using (var fSub = new Font("Microsoft YaHei UI", 9f * px, FontStyle.Regular, GraphicsUnit.Pixel))
|
|
1273
|
+
using (var fBtn = new Font("Microsoft YaHei UI", 9f * px, FontStyle.Bold, GraphicsUnit.Pixel))
|
|
1274
|
+
using (var measureBmp = new Bitmap(1, 1))
|
|
1275
|
+
using (var mg = Graphics.FromImage(measureBmp))
|
|
1276
|
+
{
|
|
1277
|
+
mg.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
|
|
1278
|
+
var fmt = StringFormat.GenericTypographic;
|
|
1279
|
+
fmt.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
|
|
1280
|
+
SizeF mainSize = mg.MeasureString(main, fMain, 10000, fmt);
|
|
1281
|
+
SizeF subSize = mg.MeasureString(sub, fSub, 10000, fmt);
|
|
1282
|
+
float pad = 14 * scale, gap = 11 * scale, iconSize = 24 * scale;
|
|
1283
|
+
float height = 40 * scale, btnH = 26 * scale, btnGap = 6 * scale;
|
|
1284
|
+
var btnSizes = new SizeF[captions.Length];
|
|
1285
|
+
float btnsW = 0;
|
|
1286
|
+
for (int i = 0; i < captions.Length; i++)
|
|
1287
|
+
{
|
|
1288
|
+
btnSizes[i] = mg.MeasureString(captions[i], fBtn, 10000, fmt);
|
|
1289
|
+
btnsW += btnSizes[i].Width + 22 * scale + (i > 0 ? btnGap : 0);
|
|
1290
|
+
}
|
|
1291
|
+
float width = pad + iconSize + gap + mainSize.Width + gap + 1 * scale + gap + subSize.Width + gap + btnsW + (pad - 7 * scale);
|
|
1292
|
+
int W = (int)Math.Ceiling(width), H = (int)Math.Ceiling(height);
|
|
1293
|
+
int x = work.X + (work.Width - W) / 2;
|
|
1294
|
+
int y = pillAtBottom ? work.Bottom - H - (int)(18 * scale) : work.Y + (int)(14 * scale);
|
|
1295
|
+
|
|
1296
|
+
using (var bmp = new Bitmap(W, H, PixelFormat.Format32bppArgb))
|
|
1297
|
+
using (var g = Graphics.FromImage(bmp))
|
|
1298
|
+
{
|
|
1299
|
+
g.Clear(Color.Transparent);
|
|
1300
|
+
g.SmoothingMode = SmoothingMode.AntiAlias;
|
|
1301
|
+
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
1302
|
+
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
|
|
1303
|
+
var body = new RectangleF(0.5f, 0.5f, W - 1, H - 1);
|
|
1304
|
+
using (var path = Rounded(body, (H - 1) / 2f))
|
|
1305
|
+
{
|
|
1306
|
+
using (var brush = new SolidBrush(Color.FromArgb(242, 31, 30, 29))) g.FillPath(brush, path);
|
|
1307
|
+
using (var pen = new Pen(Color.FromArgb(200, Accent), Math.Max(1f, 1.2f * scale))) g.DrawPath(pen, path);
|
|
1308
|
+
}
|
|
1309
|
+
var iconRect = new RectangleF(pad, (H - iconSize) / 2f, iconSize, iconSize);
|
|
1310
|
+
if (icon != null) g.DrawImage(icon, iconRect);
|
|
1311
|
+
else using (var brush = new SolidBrush(Accent)) g.FillEllipse(brush, RectangleF.Inflate(iconRect, -5 * scale, -5 * scale));
|
|
1312
|
+
float tx = pad + iconSize + gap;
|
|
1313
|
+
using (var b = new SolidBrush(Color.FromArgb(250, 250, 249))) g.DrawString(main, fMain, b, tx, (H - mainSize.Height) / 2f, fmt);
|
|
1314
|
+
tx += mainSize.Width + gap;
|
|
1315
|
+
using (var pen = new Pen(Color.FromArgb(90, 255, 255, 255), Math.Max(1f, scale))) g.DrawLine(pen, tx, H * 0.3f, tx, H * 0.7f);
|
|
1316
|
+
tx += 1 * scale + gap;
|
|
1317
|
+
using (var b = new SolidBrush(Color.FromArgb(175, 172, 168))) g.DrawString(sub, fSub, b, tx, (H - subSize.Height) / 2f, fmt);
|
|
1318
|
+
tx += subSize.Width + gap;
|
|
1319
|
+
buttons.Clear();
|
|
1320
|
+
for (int i = 0; i < captions.Length; i++)
|
|
1321
|
+
{
|
|
1322
|
+
float bw = btnSizes[i].Width + 22 * scale;
|
|
1323
|
+
var btn = new RectangleF(tx, (H - btnH) / 2f, bw, btnH);
|
|
1324
|
+
bool primary = i == 0;
|
|
1325
|
+
using (var path = Rounded(btn, btnH / 2f))
|
|
1326
|
+
using (var brush = new SolidBrush(primary ? Accent : Color.FromArgb(70, 68, 66))) g.FillPath(brush, path);
|
|
1327
|
+
using (var b = new SolidBrush(Color.White)) g.DrawString(captions[i], fBtn, b, btn.X + (bw - btnSizes[i].Width) / 2f, btn.Y + (btnH - btnSizes[i].Height) / 2f, fmt);
|
|
1328
|
+
buttons.Add(new KeyValuePair<Rectangle, string>(Rectangle.Round(btn), actions[i]));
|
|
1329
|
+
tx += bw + btnGap;
|
|
1330
|
+
}
|
|
1331
|
+
pill.SetBitmap(bmp, new Rectangle(x, y, W, H), 255);
|
|
1332
|
+
}
|
|
1333
|
+
lock (RectLock) pillRect = new Rectangle(x, y, W, H);
|
|
1334
|
+
}
|
|
1335
|
+
pill.KeepOnTop();
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
static void InstallHook()
|
|
1339
|
+
{
|
|
1340
|
+
if (hook != IntPtr.Zero) return;
|
|
1341
|
+
hookProc = HookCallback;
|
|
1342
|
+
hook = Native.SetWindowsHookEx(13, hookProc, Native.GetModuleHandle(null), 0);
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
static void RemoveHook()
|
|
1346
|
+
{
|
|
1347
|
+
if (hook == IntPtr.Zero) return;
|
|
1348
|
+
Native.UnhookWindowsHookEx(hook);
|
|
1349
|
+
hook = IntPtr.Zero;
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
|
|
1353
|
+
{
|
|
1354
|
+
if (nCode >= 0 && visible)
|
|
1355
|
+
{
|
|
1356
|
+
int msg = wParam.ToInt32();
|
|
1357
|
+
var kb = (Native.KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(Native.KBDLLHOOKSTRUCT));
|
|
1358
|
+
// Only physical keys count: our own SendInput sets LLKHF_INJECTED (0x10).
|
|
1359
|
+
if ((kb.flags & 0x10) == 0)
|
|
1360
|
+
{
|
|
1361
|
+
bool down = msg == 0x100 || msg == 0x104;
|
|
1362
|
+
if (kb.vkCode == 0x1B)
|
|
1363
|
+
{
|
|
1364
|
+
// Esc toggles pause and is swallowed, so the focused app
|
|
1365
|
+
// (DeepSeek Harness included) never turns it into "stop".
|
|
1366
|
+
if (down) invoker.BeginInvoke((MethodInvoker)delegate { Press(paused ? "resume" : "pause"); });
|
|
1367
|
+
return new IntPtr(1);
|
|
1368
|
+
}
|
|
1369
|
+
if (down)
|
|
1370
|
+
{
|
|
1371
|
+
// The user is typing: the host waits. Mouse movement is
|
|
1372
|
+
// deliberately ignored (too easy to nudge).
|
|
1373
|
+
int now = Environment.TickCount;
|
|
1374
|
+
lastTyping = now;
|
|
1375
|
+
if (unchecked(now - lastEmit) > 250) { lastEmit = now; Program.EmitEvent("user_input", "keyboard"); }
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
return Native.CallNextHookEx(hook, nCode, wParam, lParam);
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
}
|