@aivin-labs/cli 1.0.42 → 1.0.43
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/bin/lib/remote/client.mjs +29 -16
- package/bin/lib/remote/desktop/castInput.mjs +113 -111
- package/bin/lib/remote/desktop/desktopCast.mjs +417 -337
- package/bin/lib/remote/desktop/desktopCastCaptureSource.mjs +362 -0
- package/bin/lib/remote/desktop/desktopCastSource.mjs +1091 -960
- package/bin/lib/remote/desktop/desktopWebRtc.mjs +365 -276
- package/bin/lib/remote/desktop/webrtcWorker.mjs +223 -191
- package/bin/lib/remote/mascot/index.mjs +25 -9
- package/package.json +1 -1
|
@@ -1,960 +1,1091 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tiến trình phát hình ảnh màn hình trực tiếp và điều khiển từ xa trên Windows (Desktop Scenecast).
|
|
3
|
-
* Tương thích C# 5 / .NET Framework 4.5+ / PowerShell 5.1:
|
|
4
|
-
* - Chụp màn hình bằng Desktop Duplication, lùi Graphics.CopyFromScreen khi không dùng được (desktopCastCaptureSource.mjs).
|
|
5
|
-
* - Con trỏ: gửi vị trí + hình riêng để FE vẽ tức thì; người xem chưa hỗ trợ / đường JPEG thì lồng vào khung hình (GetCursorInfo + DrawIcon).
|
|
6
|
-
* - Hỗ trợ đa màn hình: lấy danh sách màn hình và chuyển đổi màn hình cần chụp theo thời gian thực.
|
|
7
|
-
* - Co ảnh về chiều rộng tối đa (giữ tỉ lệ), nén ảnh sang định dạng JPEG nh
|
|
8
|
-
* - So sánh nguyên khung (memcmp) để chỉ gửi khi hình đổi hoặc giữ nhịp định kỳ (1 FPS): thay đổi nhỏ như con trỏ, gõ chữ cũng phải đi ngay
|
|
9
|
-
* (đo 03/11/2026: ảnh thu nhỏ 64x36 + ngưỡng làm chúng chờ tới nhịp 1 s). Đường JPEG thì đổi nhỏ giới hạn 5 khung/giây, đỡ tải máy chủ.
|
|
10
|
-
* - Nhận lệnh chuột, bàn phím
|
|
11
|
-
* -
|
|
12
|
-
* -
|
|
13
|
-
*
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
using System;
|
|
21
|
-
using System.
|
|
22
|
-
using System.Drawing;
|
|
23
|
-
using System.
|
|
24
|
-
using System.IO;
|
|
25
|
-
using System.
|
|
26
|
-
using System.
|
|
27
|
-
using System.
|
|
28
|
-
using System.Threading;
|
|
29
|
-
using System.Web.Script.Serialization;
|
|
30
|
-
using System.Windows.Automation;
|
|
31
|
-
using System.Windows.Forms;
|
|
32
|
-
|
|
33
|
-
namespace AivinDesktopCast {
|
|
34
|
-
static class Native {
|
|
35
|
-
[DllImport("user32.dll")] public static extern bool SetProcessDPIAware();
|
|
36
|
-
[DllImport("user32.dll")] public static extern bool SetCursorPos(int X, int Y);
|
|
37
|
-
[DllImport("user32.dll")] public static extern void mouse_event(uint dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
|
|
38
|
-
[DllImport("user32.dll")] public static extern void keybd_event(byte bVk, ushort bScan, uint dwFlags, UIntPtr dwExtraInfo);
|
|
39
|
-
[DllImport("user32.dll")] public static extern short VkKeyScan(char ch);
|
|
40
|
-
|
|
41
|
-
// Gõ chữ: keybd_event chỉ mang được 1 byte mã quét (BYTE bScan) → chữ ngoài Latin-1 (ư ơ đ ạ ế…) bị cắt thành ký tự rác.
|
|
42
|
-
// SendInput + KEYEVENTF_UNICODE mang đủ 16 bit mỗi đơn vị UTF-16.
|
|
43
|
-
[StructLayout(LayoutKind.Sequential)] public struct MOUSEINPUT { public int dx; public int dy; public int mouseData; public uint dwFlags; public uint time; public IntPtr dwExtraInfo; }
|
|
44
|
-
[StructLayout(LayoutKind.Sequential)] public struct KEYBDINPUT { public ushort wVk; public ushort wScan; public uint dwFlags; public uint time; public IntPtr dwExtraInfo; }
|
|
45
|
-
[StructLayout(LayoutKind.Explicit)] public struct INPUTUNION { [FieldOffset(0)] public MOUSEINPUT mi; [FieldOffset(0)] public KEYBDINPUT ki; }
|
|
46
|
-
[StructLayout(LayoutKind.Sequential)] public struct INPUT { public uint type; public INPUTUNION u; }
|
|
47
|
-
[DllImport("user32.dll")] public static extern uint SendInput(uint n, INPUT[] inputs, int size);
|
|
48
|
-
public const uint INPUT_KEYBOARD = 1;
|
|
49
|
-
|
|
50
|
-
// Ô nhập chữ đang có tiêu điểm: cửa sổ trước mặt có caret hệ thống (ô Win32, Office, Chromium / Edge đều tạo caret cho trợ năng).
|
|
51
|
-
[StructLayout(LayoutKind.Sequential)]
|
|
52
|
-
public struct RECT { public int left; public int top; public int right; public int bottom; }
|
|
53
|
-
[StructLayout(LayoutKind.Sequential)]
|
|
54
|
-
public struct GUITHREADINFO {
|
|
55
|
-
public int cbSize; public uint flags; public IntPtr hwndActive; public IntPtr hwndFocus; public IntPtr hwndCapture;
|
|
56
|
-
public IntPtr hwndMenuOwner; public IntPtr hwndMoveSize; public IntPtr hwndCaret; public RECT rcCaret;
|
|
57
|
-
}
|
|
58
|
-
[DllImport("user32.dll")] public static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO info);
|
|
59
|
-
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
|
|
60
|
-
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr pid);
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
public
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
[DllImport("user32.dll")] public static extern
|
|
74
|
-
[DllImport("
|
|
75
|
-
[DllImport("
|
|
76
|
-
[DllImport("kernel32.dll"
|
|
77
|
-
[DllImport("
|
|
78
|
-
|
|
79
|
-
[
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
[
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
[
|
|
93
|
-
public
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
public
|
|
99
|
-
|
|
100
|
-
public
|
|
101
|
-
public
|
|
102
|
-
public
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
public
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
public
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
public
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
public
|
|
122
|
-
|
|
123
|
-
public
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
public
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
public static
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
public static
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
var
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
static
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
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
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
static
|
|
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
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
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
|
-
|
|
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
|
-
if (
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
if (
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
if (
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
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
|
-
if (
|
|
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
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
Native.
|
|
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
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
if (
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Tiến trình phát hình ảnh màn hình trực tiếp và điều khiển từ xa trên Windows (Desktop Scenecast).
|
|
3
|
+
* Tương thích C# 5 / .NET Framework 4.5+ / PowerShell 5.1:
|
|
4
|
+
* - Chụp màn hình bằng Desktop Duplication, lùi Graphics.CopyFromScreen khi không dùng được (desktopCastCaptureSource.mjs).
|
|
5
|
+
* - Con trỏ: gửi vị trí + hình riêng để FE vẽ tức thì; người xem chưa hỗ trợ / đường JPEG thì lồng vào khung hình (GetCursorInfo + DrawIcon).
|
|
6
|
+
* - Hỗ trợ đa màn hình: lấy danh sách màn hình và chuyển đổi màn hình cần chụp theo thời gian thực.
|
|
7
|
+
* - Co ảnh về chiều rộng tối đa (giữ tỉ lệ), nén ảnh sang định dạng JPEG nhẹ, tái sử dụng vùng đệm Bitmap/I420 để tránh GC pause.
|
|
8
|
+
* - So sánh nguyên khung (memcmp) để chỉ gửi khi hình đổi hoặc giữ nhịp định kỳ (1 FPS): thay đổi nhỏ như con trỏ, gõ chữ cũng phải đi ngay
|
|
9
|
+
* (đo 03/11/2026: ảnh thu nhỏ 64x36 + ngưỡng làm chúng chờ tới nhịp 1 s). Đường JPEG thì đổi nhỏ giới hạn 5 khung/giây, đỡ tải máy chủ.
|
|
10
|
+
* - Nhận lệnh chuột, bàn phím, phím tắt hệ thống (hotkeys) và đồng bộ Clipboard hai chiều qua stdin / stdout.
|
|
11
|
+
* - Phát hiện vùng màn hình bảo mật Windows (hộp thoại UAC / màn hình khóa) và báo trạng thái về giao diện.
|
|
12
|
+
* - Hỗ trợ thay đổi độ phân giải, chất lượng nén JPEG, giới hạn FPS (tới 60 FPS) và chọn màn hình theo thời gian thực.
|
|
13
|
+
* - Luồng video + âm thanh: có `pipe` → mở named pipe gửi khung thô I420 + âm thanh PCM 16-bit 48 kHz cho tiến trình WebRTC.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { DESKTOP_CAST_CAPTURE_CSHARP } from './desktopCastCaptureSource.mjs';
|
|
17
|
+
|
|
18
|
+
export const DESKTOP_CAST_CSHARP = String.raw`
|
|
19
|
+
using System;
|
|
20
|
+
using System.Collections.Generic;
|
|
21
|
+
using System.Drawing;
|
|
22
|
+
using System.Drawing.Imaging;
|
|
23
|
+
using System.IO;
|
|
24
|
+
using System.IO.Pipes;
|
|
25
|
+
using System.Runtime.InteropServices;
|
|
26
|
+
using System.Text;
|
|
27
|
+
using System.Threading;
|
|
28
|
+
using System.Threading.Tasks;
|
|
29
|
+
using System.Web.Script.Serialization;
|
|
30
|
+
using System.Windows.Automation;
|
|
31
|
+
using System.Windows.Forms;
|
|
32
|
+
|
|
33
|
+
namespace AivinDesktopCast {
|
|
34
|
+
static class Native {
|
|
35
|
+
[DllImport("user32.dll")] public static extern bool SetProcessDPIAware();
|
|
36
|
+
[DllImport("user32.dll")] public static extern bool SetCursorPos(int X, int Y);
|
|
37
|
+
[DllImport("user32.dll")] public static extern void mouse_event(uint dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
|
|
38
|
+
[DllImport("user32.dll")] public static extern void keybd_event(byte bVk, ushort bScan, uint dwFlags, UIntPtr dwExtraInfo);
|
|
39
|
+
[DllImport("user32.dll")] public static extern short VkKeyScan(char ch);
|
|
40
|
+
|
|
41
|
+
// Gõ chữ: keybd_event chỉ mang được 1 byte mã quét (BYTE bScan) → chữ ngoài Latin-1 (ư ơ đ ạ ế…) bị cắt thành ký tự rác.
|
|
42
|
+
// SendInput + KEYEVENTF_UNICODE mang đủ 16 bit mỗi đơn vị UTF-16.
|
|
43
|
+
[StructLayout(LayoutKind.Sequential)] public struct MOUSEINPUT { public int dx; public int dy; public int mouseData; public uint dwFlags; public uint time; public IntPtr dwExtraInfo; }
|
|
44
|
+
[StructLayout(LayoutKind.Sequential)] public struct KEYBDINPUT { public ushort wVk; public ushort wScan; public uint dwFlags; public uint time; public IntPtr dwExtraInfo; }
|
|
45
|
+
[StructLayout(LayoutKind.Explicit)] public struct INPUTUNION { [FieldOffset(0)] public MOUSEINPUT mi; [FieldOffset(0)] public KEYBDINPUT ki; }
|
|
46
|
+
[StructLayout(LayoutKind.Sequential)] public struct INPUT { public uint type; public INPUTUNION u; }
|
|
47
|
+
[DllImport("user32.dll")] public static extern uint SendInput(uint n, INPUT[] inputs, int size);
|
|
48
|
+
public const uint INPUT_KEYBOARD = 1;
|
|
49
|
+
|
|
50
|
+
// Ô nhập chữ đang có tiêu điểm: cửa sổ trước mặt có caret hệ thống (ô Win32, Office, Chromium / Edge đều tạo caret cho trợ năng).
|
|
51
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
52
|
+
public struct RECT { public int left; public int top; public int right; public int bottom; }
|
|
53
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
54
|
+
public struct GUITHREADINFO {
|
|
55
|
+
public int cbSize; public uint flags; public IntPtr hwndActive; public IntPtr hwndFocus; public IntPtr hwndCapture;
|
|
56
|
+
public IntPtr hwndMenuOwner; public IntPtr hwndMoveSize; public IntPtr hwndCaret; public RECT rcCaret;
|
|
57
|
+
}
|
|
58
|
+
[DllImport("user32.dll")] public static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO info);
|
|
59
|
+
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
|
|
60
|
+
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr pid);
|
|
61
|
+
|
|
62
|
+
// Phát hiện vùng màn hình bảo mật (UAC / Lock Screen)
|
|
63
|
+
[DllImport("user32.dll")] public static extern IntPtr OpenInputDesktop(uint dwFlags, bool fInherit, uint dwDesiredAccess);
|
|
64
|
+
[DllImport("user32.dll")] public static extern bool CloseDesktop(IntPtr hDesktop);
|
|
65
|
+
|
|
66
|
+
// Đồng bộ Clipboard
|
|
67
|
+
[DllImport("user32.dll")] public static extern uint GetClipboardSequenceNumber();
|
|
68
|
+
[DllImport("user32.dll")] public static extern bool OpenClipboard(IntPtr hWndNewOwner);
|
|
69
|
+
[DllImport("user32.dll")] public static extern bool CloseClipboard();
|
|
70
|
+
[DllImport("user32.dll")] public static extern bool EmptyClipboard();
|
|
71
|
+
[DllImport("user32.dll")] public static extern bool IsClipboardFormatAvailable(uint format);
|
|
72
|
+
[DllImport("user32.dll")] public static extern IntPtr GetClipboardData(uint uFormat);
|
|
73
|
+
[DllImport("user32.dll")] public static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
|
|
74
|
+
[DllImport("kernel32.dll")] public static extern IntPtr GlobalAlloc(uint uFlags, UIntPtr dwBytes);
|
|
75
|
+
[DllImport("kernel32.dll")] public static extern IntPtr GlobalLock(IntPtr hMem);
|
|
76
|
+
[DllImport("kernel32.dll")] public static extern bool GlobalUnlock(IntPtr hMem);
|
|
77
|
+
[DllImport("kernel32.dll")] public static extern IntPtr GlobalFree(IntPtr hMem);
|
|
78
|
+
|
|
79
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
80
|
+
public struct POINT { public int x; public int y; }
|
|
81
|
+
|
|
82
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
83
|
+
public struct CURSORINFO {
|
|
84
|
+
public int cbSize;
|
|
85
|
+
public int flags;
|
|
86
|
+
public IntPtr hCursor;
|
|
87
|
+
public POINT ptScreenPos;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
[DllImport("user32.dll")] public static extern bool GetCursorInfo(out CURSORINFO pci);
|
|
91
|
+
[DllImport("user32.dll")] public static extern bool DrawIcon(IntPtr hDC, int X, int Y, IntPtr hIcon);
|
|
92
|
+
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int memcmp(IntPtr a, IntPtr b, UIntPtr count);
|
|
93
|
+
[DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")] public static extern void CopyMemory(IntPtr dst, IntPtr src, uint count);
|
|
94
|
+
[DllImport("winmm.dll")] public static extern uint timeBeginPeriod(uint ms);
|
|
95
|
+
[DllImport("kernel32.dll")] public static extern bool GetSystemTimes(out long idle, out long kernel, out long user);
|
|
96
|
+
[DllImport("user32.dll")] public static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO info);
|
|
97
|
+
[DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hWnd);
|
|
98
|
+
[DllImport("user32.dll")] public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
|
|
99
|
+
[DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr obj);
|
|
100
|
+
[DllImport("gdi32.dll")] public static extern int GetObject(IntPtr obj, int size, out BITMAP bm);
|
|
101
|
+
[DllImport("gdi32.dll")] public static extern int GetDIBits(IntPtr hdc, IntPtr hbm, uint start, uint lines, [Out] int[] bits, ref BITMAPINFOHEADER info, uint usage);
|
|
102
|
+
[DllImport("ole32.dll")] public static extern void CoTaskMemFree(IntPtr p);
|
|
103
|
+
|
|
104
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
105
|
+
public struct ICONINFO { public bool fIcon; public int xHotspot; public int yHotspot; public IntPtr hbmMask; public IntPtr hbmColor; }
|
|
106
|
+
|
|
107
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
108
|
+
public struct BITMAP { public int bmType; public int bmWidth; public int bmHeight; public int bmWidthBytes; public ushort bmPlanes; public ushort bmBitsPixel; public IntPtr bmBits; }
|
|
109
|
+
|
|
110
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
111
|
+
public struct BITMAPINFOHEADER {
|
|
112
|
+
public int biSize; public int biWidth; public int biHeight; public ushort biPlanes; public ushort biBitCount;
|
|
113
|
+
public int biCompression; public int biSizeImage; public int biXPelsPerMeter; public int biYPelsPerMeter; public int biClrUsed; public int biClrImportant;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public const int CURSOR_SHOWING = 0x00000001;
|
|
117
|
+
|
|
118
|
+
public const uint MOUSEEVENTF_MOVE = 0x0001;
|
|
119
|
+
public const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
|
|
120
|
+
public const uint MOUSEEVENTF_LEFTUP = 0x0004;
|
|
121
|
+
public const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
|
|
122
|
+
public const uint MOUSEEVENTF_RIGHTUP = 0x0010;
|
|
123
|
+
public const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
|
|
124
|
+
public const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
|
|
125
|
+
public const uint MOUSEEVENTF_WHEEL = 0x0800;
|
|
126
|
+
public const uint MOUSEEVENTF_HWHEEL = 0x01000;
|
|
127
|
+
|
|
128
|
+
public const uint KEYEVENTF_KEYUP = 0x0002;
|
|
129
|
+
public const uint KEYEVENTF_UNICODE = 0x0004;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
static class Out {
|
|
133
|
+
static readonly object gate = new object();
|
|
134
|
+
static readonly JavaScriptSerializer json = new JavaScriptSerializer { MaxJsonLength = int.MaxValue };
|
|
135
|
+
// stdin/stdout là ống (không console): Console.In/Out mặc định theo bảng mã OEM (437…) còn Node đọc/ghi UTF-8 — 06/11/2026 gõ
|
|
136
|
+
// "tôi đang gõ tiếng Việt" từ xa ra "t├┤i ─æang g├╡ ti├ß║┐ng". Mở luồng chuẩn với UTF-8 (không BOM) khai rõ ràng.
|
|
137
|
+
static readonly UTF8Encoding utf8 = new UTF8Encoding(false);
|
|
138
|
+
static readonly StreamWriter stdout = new StreamWriter(Console.OpenStandardOutput(), utf8);
|
|
139
|
+
public static readonly StreamReader Stdin = new StreamReader(Console.OpenStandardInput(), utf8);
|
|
140
|
+
|
|
141
|
+
public static void Send(Dictionary<string, object> msg) {
|
|
142
|
+
string line = json.Serialize(msg);
|
|
143
|
+
lock (gate) { stdout.WriteLine(line); stdout.Flush(); }
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
public static Dictionary<string, object> Parse(string text) {
|
|
147
|
+
if (string.IsNullOrEmpty(text)) return new Dictionary<string, object>();
|
|
148
|
+
return json.Deserialize<Dictionary<string, object>>(text);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
public static Dictionary<string, object> Msg(string t) {
|
|
152
|
+
var d = new Dictionary<string, object>();
|
|
153
|
+
d["t"] = t;
|
|
154
|
+
return d;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Named pipe một chiều gửi khung thô I420 + âm thanh PCM cho tiến trình Node (một khách). Khách rớt → chờ khách mới. */
|
|
159
|
+
static class RawPipe {
|
|
160
|
+
static readonly object gate = new object();
|
|
161
|
+
static NamedPipeServerStream pipe;
|
|
162
|
+
public static volatile bool Connected;
|
|
163
|
+
static int videoBusy = 0;
|
|
164
|
+
static readonly byte[] videoHeader = new byte[12];
|
|
165
|
+
static byte[] audioPacket = new byte[12 + 1920];
|
|
166
|
+
|
|
167
|
+
public static void Start(string name) {
|
|
168
|
+
var t = new Thread(() => {
|
|
169
|
+
while (CastHost.Running) {
|
|
170
|
+
try {
|
|
171
|
+
var p = new NamedPipeServerStream(name, PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.None, 0, 8 * 1024 * 1024);
|
|
172
|
+
p.WaitForConnection();
|
|
173
|
+
lock (gate) { pipe = p; Connected = true; }
|
|
174
|
+
while (CastHost.Running && p.IsConnected) Thread.Sleep(200);
|
|
175
|
+
} catch {
|
|
176
|
+
Thread.Sleep(300);
|
|
177
|
+
} finally {
|
|
178
|
+
lock (gate) { Connected = false; if (pipe != null) { try { pipe.Dispose(); } catch { } pipe = null; } }
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
t.IsBackground = true;
|
|
183
|
+
t.Start();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Ghi khung hình video I420: nếu đường ống đang bận ghi khung trước thì bỏ qua khung mới để không chặn âm thanh. */
|
|
187
|
+
public static void Write(int width, int height, byte[] data) {
|
|
188
|
+
if (data == null || !Connected) return;
|
|
189
|
+
if (Interlocked.CompareExchange(ref videoBusy, 1, 0) != 0) return;
|
|
190
|
+
try {
|
|
191
|
+
bool locked = false;
|
|
192
|
+
try {
|
|
193
|
+
Monitor.TryEnter(gate, 5, ref locked);
|
|
194
|
+
if (!locked || pipe == null || !Connected) return;
|
|
195
|
+
BitConverter.GetBytes(width).CopyTo(videoHeader, 0);
|
|
196
|
+
BitConverter.GetBytes(height).CopyTo(videoHeader, 4);
|
|
197
|
+
BitConverter.GetBytes(data.Length).CopyTo(videoHeader, 8);
|
|
198
|
+
pipe.Write(videoHeader, 0, 12);
|
|
199
|
+
pipe.Write(data, 0, data.Length);
|
|
200
|
+
} catch {
|
|
201
|
+
Connected = false;
|
|
202
|
+
try { if (pipe != null) pipe.Dispose(); } catch { }
|
|
203
|
+
pipe = null;
|
|
204
|
+
} finally {
|
|
205
|
+
if (locked) Monitor.Exit(gate);
|
|
206
|
+
}
|
|
207
|
+
} finally {
|
|
208
|
+
Interlocked.Exchange(ref videoBusy, 0);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Ghi khúc âm thanh 10 ms: gộp header + mẫu vào 1 lần Write nguyên tử để giảm độ trễ và tránh tách gói. */
|
|
213
|
+
public static void WriteAudio(int sampleRate, int channels, byte[] pcm) {
|
|
214
|
+
if (pcm == null || pcm.Length == 0 || channels <= 0 || sampleRate <= 0 || !Connected) return;
|
|
215
|
+
lock (gate) {
|
|
216
|
+
if (pipe == null || !Connected) return;
|
|
217
|
+
try {
|
|
218
|
+
int total = 12 + pcm.Length;
|
|
219
|
+
if (audioPacket.Length < total) audioPacket = new byte[total];
|
|
220
|
+
BitConverter.GetBytes(-channels).CopyTo(audioPacket, 0);
|
|
221
|
+
BitConverter.GetBytes(sampleRate).CopyTo(audioPacket, 4);
|
|
222
|
+
BitConverter.GetBytes(pcm.Length).CopyTo(audioPacket, 8);
|
|
223
|
+
Buffer.BlockCopy(pcm, 0, audioPacket, 12, pcm.Length);
|
|
224
|
+
pipe.Write(audioPacket, 0, total);
|
|
225
|
+
} catch {
|
|
226
|
+
Connected = false;
|
|
227
|
+
try { if (pipe != null) pipe.Dispose(); } catch { }
|
|
228
|
+
pipe = null;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
public static class CastScreen {
|
|
235
|
+
static byte[] i420Raw;
|
|
236
|
+
static byte[] i420Out;
|
|
237
|
+
static Bitmap cursorBuffer;
|
|
238
|
+
static Bitmap scaleBuffer;
|
|
239
|
+
static Bitmap thumbBmp;
|
|
240
|
+
static byte[] thumbRaw;
|
|
241
|
+
|
|
242
|
+
/** Kiểm tra tiến trình có truy cập được vùng màn hình nhận thao tác không (false = đang ở hộp thoại UAC hoặc màn hình khóa). */
|
|
243
|
+
public static bool IsSecureDesktop() {
|
|
244
|
+
IntPtr hDesk = IntPtr.Zero;
|
|
245
|
+
try {
|
|
246
|
+
hDesk = Native.OpenInputDesktop(0, false, 0x0100u);
|
|
247
|
+
return hDesk == IntPtr.Zero;
|
|
248
|
+
} catch {
|
|
249
|
+
return false;
|
|
250
|
+
} finally {
|
|
251
|
+
if (hDesk != IntPtr.Zero) Native.CloseDesktop(hDesk);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** BGRX 32 bit → I420: tái sử dụng mảng đệm tĩnh (tránh GC LOH) và chia luồng song song theo cặp hàng chẵn/lẻ. */
|
|
256
|
+
public static byte[] ToI420(Bitmap bmp, out int w, out int h) {
|
|
257
|
+
w = bmp.Width & ~1;
|
|
258
|
+
h = bmp.Height & ~1;
|
|
259
|
+
int width = w;
|
|
260
|
+
int height = h;
|
|
261
|
+
var data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
|
|
262
|
+
try {
|
|
263
|
+
int stride = Math.Abs(data.Stride);
|
|
264
|
+
int rawLen = stride * height;
|
|
265
|
+
if (i420Raw == null || i420Raw.Length < rawLen) i420Raw = new byte[rawLen];
|
|
266
|
+
byte[] raw = i420Raw;
|
|
267
|
+
Marshal.Copy(data.Scan0, raw, 0, rawLen);
|
|
268
|
+
int ySize = width * height;
|
|
269
|
+
int totalLen = ySize + ySize / 2;
|
|
270
|
+
if (i420Out == null || i420Out.Length != totalLen) i420Out = new byte[totalLen];
|
|
271
|
+
byte[] outBuf = i420Out;
|
|
272
|
+
int cw = width / 2;
|
|
273
|
+
int uBase = ySize;
|
|
274
|
+
int vBase = ySize + ySize / 4;
|
|
275
|
+
int pairs = height / 2;
|
|
276
|
+
|
|
277
|
+
Parallel.For(0, pairs, pair => {
|
|
278
|
+
int y0 = pair * 2;
|
|
279
|
+
int y1 = y0 + 1;
|
|
280
|
+
int row0 = y0 * stride;
|
|
281
|
+
int row1 = row0 + stride;
|
|
282
|
+
int yRow0 = y0 * width;
|
|
283
|
+
int yRow1 = y1 * width;
|
|
284
|
+
int cRow = pair * cw;
|
|
285
|
+
for (int x = 0; x < width; x += 2) {
|
|
286
|
+
int i0 = row0 + x * 4;
|
|
287
|
+
int i1 = i0 + 4;
|
|
288
|
+
int i2 = row1 + x * 4;
|
|
289
|
+
int i3 = i2 + 4;
|
|
290
|
+
|
|
291
|
+
int b0 = raw[i0], g0 = raw[i0 + 1], r0 = raw[i0 + 2];
|
|
292
|
+
int b1 = raw[i1], g1 = raw[i1 + 1], r1 = raw[i1 + 2];
|
|
293
|
+
int b2 = raw[i2], g2 = raw[i2 + 1], r2 = raw[i2 + 2];
|
|
294
|
+
int b3 = raw[i3], g3 = raw[i3 + 1], r3 = raw[i3 + 2];
|
|
295
|
+
|
|
296
|
+
outBuf[yRow0 + x] = (byte)(((66 * r0 + 129 * g0 + 25 * b0 + 128) >> 8) + 16);
|
|
297
|
+
outBuf[yRow0 + x + 1] = (byte)(((66 * r1 + 129 * g1 + 25 * b1 + 128) >> 8) + 16);
|
|
298
|
+
outBuf[yRow1 + x] = (byte)(((66 * r2 + 129 * g2 + 25 * b2 + 128) >> 8) + 16);
|
|
299
|
+
outBuf[yRow1 + x + 1] = (byte)(((66 * r3 + 129 * g3 + 25 * b3 + 128) >> 8) + 16);
|
|
300
|
+
|
|
301
|
+
int bAvg = (b0 + b1 + b2 + b3) >> 2;
|
|
302
|
+
int gAvg = (g0 + g1 + g2 + g3) >> 2;
|
|
303
|
+
int rAvg = (r0 + r1 + r2 + r3) >> 2;
|
|
304
|
+
int ci = cRow + (x >> 1);
|
|
305
|
+
outBuf[uBase + ci] = (byte)(((-38 * rAvg - 74 * gAvg + 112 * bAvg + 128) >> 8) + 128);
|
|
306
|
+
outBuf[vBase + ci] = (byte)(((112 * rAvg - 94 * gAvg - 18 * bAvg + 128) >> 8) + 128);
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
return outBuf;
|
|
310
|
+
} finally {
|
|
311
|
+
bmp.UnlockBits(data);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
public static Rectangle GetBounds(int monitor) {
|
|
316
|
+
if (monitor < 0 || monitor >= Screen.AllScreens.Length) {
|
|
317
|
+
return SystemInformation.VirtualScreen;
|
|
318
|
+
}
|
|
319
|
+
return Screen.AllScreens[monitor].Bounds;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
public static List<Dictionary<string, object>> GetScreens() {
|
|
323
|
+
var list = new List<Dictionary<string, object>>();
|
|
324
|
+
var all = Screen.AllScreens;
|
|
325
|
+
for (int i = 0; i < all.Length; i++) {
|
|
326
|
+
var s = all[i];
|
|
327
|
+
var d = new Dictionary<string, object>();
|
|
328
|
+
d["index"] = i;
|
|
329
|
+
d["name"] = "Màn hình " + (i + 1) + (s.Primary ? " (Chính)" : "");
|
|
330
|
+
d["primary"] = s.Primary;
|
|
331
|
+
d["width"] = s.Bounds.Width;
|
|
332
|
+
d["height"] = s.Bounds.Height;
|
|
333
|
+
d["x"] = s.Bounds.X;
|
|
334
|
+
d["y"] = s.Bounds.Y;
|
|
335
|
+
list.Add(d);
|
|
336
|
+
}
|
|
337
|
+
return list;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** Chụp vùng màn hình vào ảnh có sẵn (không kèm con trỏ). */
|
|
341
|
+
public static void CopyScreen(Bitmap dst, Rectangle r) {
|
|
342
|
+
try {
|
|
343
|
+
using (var g = Graphics.FromImage(dst)) {
|
|
344
|
+
g.CopyFromScreen(r.Left, r.Top, 0, 0, r.Size);
|
|
345
|
+
}
|
|
346
|
+
} catch {
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** Bản sao của khung có lồng con trỏ thật của Windows (tái sử dụng cursorBuffer để tránh cấp phát mới mỗi khung). */
|
|
351
|
+
public static Bitmap WithCursor(Bitmap src, Rectangle r) {
|
|
352
|
+
if (cursorBuffer == null || cursorBuffer.Width != src.Width || cursorBuffer.Height != src.Height) {
|
|
353
|
+
if (cursorBuffer != null) cursorBuffer.Dispose();
|
|
354
|
+
cursorBuffer = new Bitmap(src.Width, src.Height, PixelFormat.Format32bppRgb);
|
|
355
|
+
}
|
|
356
|
+
using (var g = Graphics.FromImage(cursorBuffer)) {
|
|
357
|
+
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
|
|
358
|
+
g.DrawImageUnscaled(src, 0, 0);
|
|
359
|
+
try {
|
|
360
|
+
Native.CURSORINFO pci;
|
|
361
|
+
pci.cbSize = Marshal.SizeOf(typeof(Native.CURSORINFO));
|
|
362
|
+
if (Native.GetCursorInfo(out pci) && (pci.flags & Native.CURSOR_SHOWING) != 0) {
|
|
363
|
+
var hot = CursorTracker.Hotspot(pci.hCursor);
|
|
364
|
+
int cursorX = pci.ptScreenPos.x - hot.X - r.Left;
|
|
365
|
+
int cursorY = pci.ptScreenPos.y - hot.Y - r.Top;
|
|
366
|
+
if (cursorX >= -32 && cursorX < r.Width && cursorY >= -32 && cursorY < r.Height) {
|
|
367
|
+
IntPtr hdc = g.GetHdc();
|
|
368
|
+
try {
|
|
369
|
+
Native.DrawIcon(hdc, cursorX, cursorY, pci.hCursor);
|
|
370
|
+
} finally {
|
|
371
|
+
g.ReleaseHdc(hdc);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
} catch {}
|
|
376
|
+
}
|
|
377
|
+
return cursorBuffer;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
public static Bitmap Scale(Bitmap src, int maxWidth) {
|
|
381
|
+
if (src.Width <= maxWidth) return src;
|
|
382
|
+
int h = (int)Math.Round(src.Height * (double)maxWidth / src.Width);
|
|
383
|
+
if (scaleBuffer == null || scaleBuffer.Width != maxWidth || scaleBuffer.Height != h) {
|
|
384
|
+
if (scaleBuffer != null) scaleBuffer.Dispose();
|
|
385
|
+
scaleBuffer = new Bitmap(maxWidth, h, PixelFormat.Format32bppRgb);
|
|
386
|
+
}
|
|
387
|
+
using (var g = Graphics.FromImage(scaleBuffer)) {
|
|
388
|
+
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
|
|
389
|
+
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
|
|
390
|
+
g.DrawImage(src, 0, 0, maxWidth, h);
|
|
391
|
+
}
|
|
392
|
+
return scaleBuffer;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
public static byte[] Jpeg(Bitmap bmp, int quality) {
|
|
396
|
+
var encoders = ImageCodecInfo.GetImageEncoders();
|
|
397
|
+
ImageCodecInfo codec = null;
|
|
398
|
+
for (int i = 0; i < encoders.Length; i++) {
|
|
399
|
+
if (encoders[i].MimeType == "image/jpeg") { codec = encoders[i]; break; }
|
|
400
|
+
}
|
|
401
|
+
if (codec == null) {
|
|
402
|
+
using (var ms = new MemoryStream()) { bmp.Save(ms, ImageFormat.Jpeg); return ms.ToArray(); }
|
|
403
|
+
}
|
|
404
|
+
var p = new EncoderParameters(1);
|
|
405
|
+
p.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
|
|
406
|
+
using (var ms = new MemoryStream()) {
|
|
407
|
+
bmp.Save(ms, codec, p);
|
|
408
|
+
return ms.ToArray();
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
public static byte[] Thumb(Bitmap src) {
|
|
413
|
+
var t = new byte[64 * 36];
|
|
414
|
+
if (thumbBmp == null) thumbBmp = new Bitmap(64, 36, PixelFormat.Format24bppRgb);
|
|
415
|
+
using (var g = Graphics.FromImage(thumbBmp)) {
|
|
416
|
+
g.DrawImage(src, 0, 0, 64, 36);
|
|
417
|
+
}
|
|
418
|
+
var data = thumbBmp.LockBits(new Rectangle(0, 0, 64, 36), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
|
|
419
|
+
try {
|
|
420
|
+
int stride = Math.Abs(data.Stride);
|
|
421
|
+
int len = stride * 36;
|
|
422
|
+
if (thumbRaw == null || thumbRaw.Length < len) thumbRaw = new byte[len];
|
|
423
|
+
Marshal.Copy(data.Scan0, thumbRaw, 0, len);
|
|
424
|
+
for (int y = 0; y < 36; y++) {
|
|
425
|
+
int rowOffset = y * stride;
|
|
426
|
+
int outOffset = y * 64;
|
|
427
|
+
for (int x = 0; x < 64; x++) {
|
|
428
|
+
int idx = rowOffset + x * 3;
|
|
429
|
+
int b = thumbRaw[idx];
|
|
430
|
+
int gVal = thumbRaw[idx + 1];
|
|
431
|
+
int r = thumbRaw[idx + 2];
|
|
432
|
+
t[outOffset + x] = (byte)((r * 3 + gVal * 6 + b) / 10);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
} finally {
|
|
436
|
+
thumbBmp.UnlockBits(data);
|
|
437
|
+
}
|
|
438
|
+
return t;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
public static double Diff(byte[] a, byte[] b) {
|
|
442
|
+
if (a == null || b == null) return 255;
|
|
443
|
+
long s = 0;
|
|
444
|
+
for (int i = 0; i < a.Length; i++) s += Math.Abs(a[i] - b[i]);
|
|
445
|
+
return (double)s / a.Length;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** Hai khung giống hệt từng điểm ảnh — ~0,3 ms ở 1080p, rẻ hơn tạo ảnh thu nhỏ. */
|
|
449
|
+
public static bool SameFrame(Bitmap a, Bitmap b) {
|
|
450
|
+
if (a == null || b == null || a.Width != b.Width || a.Height != b.Height) return false;
|
|
451
|
+
var rect = new Rectangle(0, 0, a.Width, a.Height);
|
|
452
|
+
var da = a.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
|
|
453
|
+
try {
|
|
454
|
+
var db = b.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
|
|
455
|
+
try {
|
|
456
|
+
return Native.memcmp(da.Scan0, db.Scan0, (UIntPtr)(ulong)((long)Math.Abs(da.Stride) * a.Height)) == 0;
|
|
457
|
+
} finally {
|
|
458
|
+
b.UnlockBits(db);
|
|
459
|
+
}
|
|
460
|
+
} finally {
|
|
461
|
+
a.UnlockBits(da);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
public static class CastHost {
|
|
467
|
+
const long JPEG_SMALL_CHANGE_INTERVAL_MS = 66;
|
|
468
|
+
const double JPEG_BIG_CHANGE_DIFF = 4.5;
|
|
469
|
+
const int RATIO_SCALE = 65535;
|
|
470
|
+
static volatile bool running = true;
|
|
471
|
+
public static bool Running { get { return running; } }
|
|
472
|
+
static volatile bool jpegEnabled = true;
|
|
473
|
+
static volatile bool refreshRequested = false;
|
|
474
|
+
static bool rawEnabled = false;
|
|
475
|
+
static volatile int currentMonitor = -1;
|
|
476
|
+
static Rectangle currentBounds;
|
|
477
|
+
public static Rectangle CurrentBounds { get { return currentBounds; } }
|
|
478
|
+
static int currentImageWidth = 0;
|
|
479
|
+
static int currentImageHeight = 0;
|
|
480
|
+
static volatile int targetMaxFps = 15;
|
|
481
|
+
static volatile int targetMaxWidth = 1366;
|
|
482
|
+
static volatile int targetQuality = 65;
|
|
483
|
+
static bool lastSecureDesktop = false;
|
|
484
|
+
|
|
485
|
+
public static void Run(string argsJson) {
|
|
486
|
+
Native.SetProcessDPIAware();
|
|
487
|
+
Native.timeBeginPeriod(1);
|
|
488
|
+
var a = Out.Parse(argsJson);
|
|
489
|
+
currentMonitor = a.ContainsKey("monitor") ? Convert.ToInt32(a["monitor"]) : -1;
|
|
490
|
+
targetMaxFps = a.ContainsKey("max_fps") ? Convert.ToInt32(a["max_fps"]) : 15;
|
|
491
|
+
if (targetMaxFps < 1) targetMaxFps = 1;
|
|
492
|
+
if (targetMaxFps > 60) targetMaxFps = 60;
|
|
493
|
+
targetMaxWidth = a.ContainsKey("max_width") ? Convert.ToInt32(a["max_width"]) : 1366;
|
|
494
|
+
targetQuality = a.ContainsKey("quality") ? Convert.ToInt32(a["quality"]) : 65;
|
|
495
|
+
string pipeName = a.ContainsKey("pipe") ? Convert.ToString(a["pipe"]) : "";
|
|
496
|
+
if (!string.IsNullOrEmpty(pipeName)) { rawEnabled = true; RawPipe.Start(pipeName); }
|
|
497
|
+
if (a.ContainsKey("audio") && a["audio"] is bool) LoopbackAudio.SetEnabled((bool)a["audio"]);
|
|
498
|
+
if (rawEnabled) LoopbackAudio.Start();
|
|
499
|
+
ClipboardSync.Init();
|
|
500
|
+
|
|
501
|
+
currentBounds = CastScreen.GetBounds(currentMonitor);
|
|
502
|
+
|
|
503
|
+
// Luồng đọc lệnh điều khiển từ xa từ stdin
|
|
504
|
+
var inputThread = new Thread(ReadInputLoop);
|
|
505
|
+
inputThread.IsBackground = true;
|
|
506
|
+
inputThread.Start();
|
|
507
|
+
|
|
508
|
+
var readyMsg = Out.Msg("ready");
|
|
509
|
+
readyMsg["monitor"] = currentMonitor;
|
|
510
|
+
readyMsg["screens"] = CastScreen.GetScreens();
|
|
511
|
+
readyMsg["bounds"] = new Dictionary<string, object> {
|
|
512
|
+
{ "x", currentBounds.X }, { "y", currentBounds.Y },
|
|
513
|
+
{ "width", currentBounds.Width }, { "height", currentBounds.Height }
|
|
514
|
+
};
|
|
515
|
+
Out.Send(readyMsg);
|
|
516
|
+
|
|
517
|
+
var clock = System.Diagnostics.Stopwatch.StartNew();
|
|
518
|
+
var source = new FrameSource();
|
|
519
|
+
var hostStats = new HostStats();
|
|
520
|
+
CursorTracker.Start();
|
|
521
|
+
byte[] lastJpegThumb = null;
|
|
522
|
+
bool changePending = false;
|
|
523
|
+
int lastCursorSeq = -1;
|
|
524
|
+
long lastSendMs = 0;
|
|
525
|
+
long lastAuxCheckMs = 0;
|
|
526
|
+
|
|
527
|
+
while (running) {
|
|
528
|
+
long frameStartMs = clock.ElapsedMilliseconds;
|
|
529
|
+
long targetFrameInterval = 1000 / Math.Max(1, targetMaxFps);
|
|
530
|
+
try {
|
|
531
|
+
if (frameStartMs - lastAuxCheckMs >= 250) {
|
|
532
|
+
lastAuxCheckMs = frameStartMs;
|
|
533
|
+
ClipboardSync.Poll();
|
|
534
|
+
bool sec = CastScreen.IsSecureDesktop();
|
|
535
|
+
if (sec != lastSecureDesktop) {
|
|
536
|
+
lastSecureDesktop = sec;
|
|
537
|
+
var secMsg = Out.Msg("secure_desktop");
|
|
538
|
+
secMsg["active"] = sec;
|
|
539
|
+
Out.Send(secMsg);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
currentBounds = CastScreen.GetBounds(currentMonitor);
|
|
543
|
+
if (source.Capture(currentBounds, (int)targetFrameInterval, frameStartMs)) {
|
|
544
|
+
changePending = true;
|
|
545
|
+
hostStats.OnChange(source.Frame, clock.ElapsedMilliseconds);
|
|
546
|
+
}
|
|
547
|
+
bool cursorInFrame = CursorTracker.DrawInFrame;
|
|
548
|
+
int cursorSeq = CursorTracker.Seq;
|
|
549
|
+
if (cursorInFrame && cursorSeq != lastCursorSeq) changePending = true;
|
|
550
|
+
long nowMs = clock.ElapsedMilliseconds;
|
|
551
|
+
bool forceKeepAlive = (nowMs - lastSendMs) >= 1000;
|
|
552
|
+
bool refresh = refreshRequested;
|
|
553
|
+
if (refresh) refreshRequested = false;
|
|
554
|
+
|
|
555
|
+
bool jpegMode = jpegEnabled || !rawEnabled;
|
|
556
|
+
bool sendChange = changePending;
|
|
557
|
+
if (sendChange && jpegMode && lastJpegThumb != null && (nowMs - lastSendMs) < JPEG_SMALL_CHANGE_INTERVAL_MS) {
|
|
558
|
+
sendChange = CastScreen.Diff(CastScreen.Thumb(source.Frame), lastJpegThumb) > JPEG_BIG_CHANGE_DIFF;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
if (sendChange || forceKeepAlive || refresh) {
|
|
562
|
+
changePending = false;
|
|
563
|
+
lastSendMs = nowMs;
|
|
564
|
+
lastCursorSeq = cursorSeq;
|
|
565
|
+
if (jpegMode) lastJpegThumb = CastScreen.Thumb(source.Frame);
|
|
566
|
+
long sendStartMs = clock.ElapsedMilliseconds;
|
|
567
|
+
SendFrame(source.Frame, cursorInFrame, jpegMode, nowMs, hostStats);
|
|
568
|
+
hostStats.AddLoad(clock.ElapsedMilliseconds - sendStartMs);
|
|
569
|
+
}
|
|
570
|
+
} catch (Exception ex) {
|
|
571
|
+
var warnMsg = Out.Msg("warn");
|
|
572
|
+
warnMsg["error"] = ex.Message;
|
|
573
|
+
Out.Send(warnMsg);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
long sleepMs = targetFrameInterval - (clock.ElapsedMilliseconds - frameStartMs);
|
|
577
|
+
if (sleepMs > 0) Thread.Sleep((int)sleepMs);
|
|
578
|
+
else if (!source.UsingDxgi) Thread.Sleep(1);
|
|
579
|
+
}
|
|
580
|
+
source.Dispose();
|
|
581
|
+
|
|
582
|
+
var stoppedMsg = Out.Msg("stopped");
|
|
583
|
+
Out.Send(stoppedMsg);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
static void SendFrame(Bitmap frame, bool cursorInFrame, bool jpegMode, long nowMs, HostStats hostStats) {
|
|
587
|
+
Bitmap encodeBmp = frame;
|
|
588
|
+
if (cursorInFrame) {
|
|
589
|
+
encodeBmp = CastScreen.WithCursor(frame, currentBounds);
|
|
590
|
+
}
|
|
591
|
+
if (encodeBmp.Width > targetMaxWidth) {
|
|
592
|
+
encodeBmp = CastScreen.Scale(encodeBmp, targetMaxWidth);
|
|
593
|
+
}
|
|
594
|
+
currentImageWidth = encodeBmp.Width;
|
|
595
|
+
currentImageHeight = encodeBmp.Height;
|
|
596
|
+
if (rawEnabled && RawPipe.Connected) {
|
|
597
|
+
int rw, rh;
|
|
598
|
+
byte[] i420 = CastScreen.ToI420(encodeBmp, out rw, out rh);
|
|
599
|
+
RawPipe.Write(rw, rh, i420);
|
|
600
|
+
}
|
|
601
|
+
var frameMsg = Out.Msg(jpegMode ? "frame" : "meta");
|
|
602
|
+
if (jpegMode) frameMsg["image"] = Convert.ToBase64String(CastScreen.Jpeg(encodeBmp, targetQuality));
|
|
603
|
+
frameMsg["width"] = currentImageWidth;
|
|
604
|
+
frameMsg["height"] = currentImageHeight;
|
|
605
|
+
frameMsg["screenWidth"] = currentBounds.Width;
|
|
606
|
+
frameMsg["screenHeight"] = currentBounds.Height;
|
|
607
|
+
frameMsg["screenX"] = currentBounds.X;
|
|
608
|
+
frameMsg["screenY"] = currentBounds.Y;
|
|
609
|
+
frameMsg["monitor"] = currentMonitor;
|
|
610
|
+
frameMsg["screens"] = CastScreen.GetScreens();
|
|
611
|
+
frameMsg["ts"] = nowMs;
|
|
612
|
+
frameMsg["stats"] = hostStats.Snapshot(nowMs);
|
|
613
|
+
if (lastSecureDesktop) frameMsg["secureDesktop"] = true;
|
|
614
|
+
Out.Send(frameMsg);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
static void ReadInputLoop() {
|
|
618
|
+
try {
|
|
619
|
+
string line;
|
|
620
|
+
while (running && (line = Out.Stdin.ReadLine()) != null) {
|
|
621
|
+
line = line.Trim();
|
|
622
|
+
if (string.IsNullOrEmpty(line)) continue;
|
|
623
|
+
if (line == "stop") {
|
|
624
|
+
running = false;
|
|
625
|
+
break;
|
|
626
|
+
}
|
|
627
|
+
var cmd = Out.Parse(line);
|
|
628
|
+
string op = cmd.ContainsKey("op") ? Convert.ToString(cmd["op"]) : "";
|
|
629
|
+
if (op == "stop") {
|
|
630
|
+
running = false;
|
|
631
|
+
break;
|
|
632
|
+
} else if (op == "input") {
|
|
633
|
+
HandleInput(cmd);
|
|
634
|
+
} else if (op == "config") {
|
|
635
|
+
HandleConfig(cmd);
|
|
636
|
+
} else if (op == "refresh") {
|
|
637
|
+
refreshRequested = true;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
} catch {
|
|
641
|
+
running = false;
|
|
642
|
+
} finally {
|
|
643
|
+
// Dừng phiên hoặc daemon chết (stdin đóng) lúc người xem đang kéo: không để nút chuột / phím bổ trợ kẹt xuống trên máy.
|
|
644
|
+
ReleaseHeldInput();
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
static void HandleConfig(Dictionary<string, object> cmd) {
|
|
649
|
+
try {
|
|
650
|
+
if (cmd.ContainsKey("max_fps")) {
|
|
651
|
+
int fps = Convert.ToInt32(cmd["max_fps"]);
|
|
652
|
+
if (fps >= 1 && fps <= 60) targetMaxFps = fps;
|
|
653
|
+
}
|
|
654
|
+
if (cmd.ContainsKey("quality")) {
|
|
655
|
+
int q = Convert.ToInt32(cmd["quality"]);
|
|
656
|
+
if (q >= 10 && q <= 95) targetQuality = q;
|
|
657
|
+
}
|
|
658
|
+
if (cmd.ContainsKey("max_width")) {
|
|
659
|
+
int w = Convert.ToInt32(cmd["max_width"]);
|
|
660
|
+
if (w >= 640 && w <= 2560) {
|
|
661
|
+
// Đổi độ rộng lúc màn hình đứng yên: phải có khung mới ngay, không chờ nhịp giữ.
|
|
662
|
+
if (w != targetMaxWidth) refreshRequested = true;
|
|
663
|
+
targetMaxWidth = w;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
if (cmd.ContainsKey("jpeg") && cmd["jpeg"] is bool) {
|
|
667
|
+
jpegEnabled = (bool)cmd["jpeg"];
|
|
668
|
+
refreshRequested = true;
|
|
669
|
+
}
|
|
670
|
+
if (cmd.ContainsKey("cursor") && cmd["cursor"] is bool) {
|
|
671
|
+
CursorTracker.SetDrawInFrame((bool)cmd["cursor"]);
|
|
672
|
+
refreshRequested = true;
|
|
673
|
+
}
|
|
674
|
+
if (cmd.ContainsKey("audio") && cmd["audio"] is bool) {
|
|
675
|
+
LoopbackAudio.SetEnabled((bool)cmd["audio"]);
|
|
676
|
+
}
|
|
677
|
+
if (cmd.ContainsKey("monitor")) {
|
|
678
|
+
int m = Convert.ToInt32(cmd["monitor"]);
|
|
679
|
+
if (m >= -1 && m < Screen.AllScreens.Length) {
|
|
680
|
+
currentMonitor = m;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
var ack = Out.Msg("config_ack");
|
|
684
|
+
ack["max_fps"] = targetMaxFps;
|
|
685
|
+
ack["quality"] = targetQuality;
|
|
686
|
+
ack["max_width"] = targetMaxWidth;
|
|
687
|
+
ack["monitor"] = currentMonitor;
|
|
688
|
+
Out.Send(ack);
|
|
689
|
+
} catch {
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
static byte KeyNameToVk(string name) {
|
|
694
|
+
if (string.IsNullOrEmpty(name)) return 0;
|
|
695
|
+
switch (name.ToLower().Trim()) {
|
|
696
|
+
case "win": case "windows": case "lwin": case "meta": case "cmd": return 0x5B;
|
|
697
|
+
case "rwin": return 0x5C;
|
|
698
|
+
case "ctrl": case "control": case "lctrl": return 0x11;
|
|
699
|
+
case "rctrl": return 0xA3;
|
|
700
|
+
case "alt": case "menu": case "lalt": return 0x12;
|
|
701
|
+
case "ralt": return 0xA5;
|
|
702
|
+
case "shift": case "lshift": return 0x10;
|
|
703
|
+
case "rshift": return 0xA1;
|
|
704
|
+
case "tab": return 0x09;
|
|
705
|
+
case "escape": case "esc": return 0x1B;
|
|
706
|
+
case "enter": case "return": return 0x0D;
|
|
707
|
+
case "backspace": return 0x08;
|
|
708
|
+
case "space": return 0x20;
|
|
709
|
+
case "delete": case "del": return 0x2E;
|
|
710
|
+
case "insert": case "ins": return 0x2D;
|
|
711
|
+
case "home": return 0x24;
|
|
712
|
+
case "end": return 0x23;
|
|
713
|
+
case "pageup": return 0x21;
|
|
714
|
+
case "pagedown": return 0x22;
|
|
715
|
+
case "up": case "arrowup": return 0x26;
|
|
716
|
+
case "down": case "arrowdown": return 0x28;
|
|
717
|
+
case "left": case "arrowleft": return 0x25;
|
|
718
|
+
case "right": case "arrowright": return 0x27;
|
|
719
|
+
case "f1": return 0x70;
|
|
720
|
+
case "f2": return 0x71;
|
|
721
|
+
case "f3": return 0x72;
|
|
722
|
+
case "f4": return 0x73;
|
|
723
|
+
case "f5": return 0x74;
|
|
724
|
+
case "f6": return 0x75;
|
|
725
|
+
case "f7": return 0x76;
|
|
726
|
+
case "f8": return 0x77;
|
|
727
|
+
case "f9": return 0x78;
|
|
728
|
+
case "f10": return 0x79;
|
|
729
|
+
case "f11": return 0x7A;
|
|
730
|
+
case "f12": return 0x7B;
|
|
731
|
+
case "printscreen": case "prtsc": return 0x2C;
|
|
732
|
+
case "capslock": return 0x14;
|
|
733
|
+
case "numlock": return 0x90;
|
|
734
|
+
case "scrolllock": return 0x91;
|
|
735
|
+
case "contextmenu": case "apps": return 0x5D;
|
|
736
|
+
default:
|
|
737
|
+
if (name.Length == 1) {
|
|
738
|
+
char ch = char.ToUpperInvariant(name[0]);
|
|
739
|
+
if (ch >= 'A' && ch <= 'Z') return (byte)ch;
|
|
740
|
+
if (ch >= '0' && ch <= '9') return (byte)ch;
|
|
741
|
+
short vks = Native.VkKeyScan(name[0]);
|
|
742
|
+
if (vks != -1) return (byte)(vks & 0xFF);
|
|
743
|
+
}
|
|
744
|
+
return 0;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
static List<byte> ExtractModifiers(Dictionary<string, object> cmd) {
|
|
749
|
+
var list = new List<byte>();
|
|
750
|
+
if (!cmd.ContainsKey("modifiers") || cmd["modifiers"] == null) return list;
|
|
751
|
+
System.Collections.ArrayList raw = cmd["modifiers"] as System.Collections.ArrayList;
|
|
752
|
+
if (raw != null) {
|
|
753
|
+
for (int i = 0; i < raw.Count; i++) {
|
|
754
|
+
byte vk = KeyNameToVk(Convert.ToString(raw[i]));
|
|
755
|
+
if (vk != 0 && !list.Contains(vk)) list.Add(vk);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
return list;
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
static void PressModifiers(List<byte> mods) {
|
|
762
|
+
for (int i = 0; i < mods.Count; i++) {
|
|
763
|
+
Native.keybd_event(mods[i], 0, 0, UIntPtr.Zero);
|
|
764
|
+
}
|
|
765
|
+
if (mods.Count > 0) Thread.Sleep(8);
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
static void ReleaseModifiers(List<byte> mods) {
|
|
769
|
+
for (int i = mods.Count - 1; i >= 0; i--) {
|
|
770
|
+
Native.keybd_event(mods[i], 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
static int wheelAccY = 0;
|
|
775
|
+
static int wheelAccX = 0;
|
|
776
|
+
|
|
777
|
+
static void FlushWheelAccumulator() {
|
|
778
|
+
if (Math.Abs(wheelAccY) >= 40) {
|
|
779
|
+
int sign = wheelAccY > 0 ? 1 : -1;
|
|
780
|
+
Native.mouse_event(Native.MOUSEEVENTF_WHEEL, 0, 0, -sign * 120, 0);
|
|
781
|
+
}
|
|
782
|
+
wheelAccY = 0;
|
|
783
|
+
if (Math.Abs(wheelAccX) >= 40) {
|
|
784
|
+
int sign = wheelAccX > 0 ? 1 : -1;
|
|
785
|
+
Native.mouse_event(Native.MOUSEEVENTF_HWHEEL, 0, 0, sign * 120, 0);
|
|
786
|
+
}
|
|
787
|
+
wheelAccX = 0;
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
// Nút + phím bổ trợ đang giữ qua mousedown (chỉ luồng đọc lệnh đụng tới). Gói mouseup không đủ để biết cần nhả gì: người xem có thể thả Ctrl trước khi thả chuột.
|
|
791
|
+
static readonly List<int> heldButtons = new List<int>();
|
|
792
|
+
static readonly List<byte> heldModifiers = new List<byte>();
|
|
793
|
+
|
|
794
|
+
static void MouseButton(int button, bool down) {
|
|
795
|
+
uint flag = button == 2 ? (down ? Native.MOUSEEVENTF_RIGHTDOWN : Native.MOUSEEVENTF_RIGHTUP)
|
|
796
|
+
: button == 1 ? (down ? Native.MOUSEEVENTF_MIDDLEDOWN : Native.MOUSEEVENTF_MIDDLEUP)
|
|
797
|
+
: (down ? Native.MOUSEEVENTF_LEFTDOWN : Native.MOUSEEVENTF_LEFTUP);
|
|
798
|
+
Native.mouse_event(flag, 0, 0, 0, 0);
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
static void ReleaseHeldModifiers() {
|
|
802
|
+
ReleaseModifiers(heldModifiers);
|
|
803
|
+
heldModifiers.Clear();
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
static void ReleaseHeldInput() {
|
|
807
|
+
for (int i = heldButtons.Count - 1; i >= 0; i--) MouseButton(heldButtons[i], false);
|
|
808
|
+
heldButtons.Clear();
|
|
809
|
+
ReleaseHeldModifiers();
|
|
810
|
+
FlushWheelAccumulator();
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
static void HandleInput(Dictionary<string, object> cmd) {
|
|
814
|
+
try {
|
|
815
|
+
string action = cmd.ContainsKey("action") ? Convert.ToString(cmd["action"]) : "";
|
|
816
|
+
if (action == "release_all") {
|
|
817
|
+
ReleaseHeldInput();
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
double rx = cmd.ContainsKey("x") ? Convert.ToDouble(cmd["x"]) : 0;
|
|
821
|
+
double ry = cmd.ContainsKey("y") ? Convert.ToDouble(cmd["y"]) : 0;
|
|
822
|
+
var mods = ExtractModifiers(cmd);
|
|
823
|
+
|
|
824
|
+
int targetX = currentBounds.X;
|
|
825
|
+
int targetY = currentBounds.Y;
|
|
826
|
+
if (cmd.ContainsKey("rx") && cmd.ContainsKey("ry")) {
|
|
827
|
+
// Toạ độ theo tỷ lệ: đúng cả khi độ rộng khung vừa đổi (chế độ Tự động) hoặc video bị hạ độ phân giải.
|
|
828
|
+
targetX += (int)Math.Round(Convert.ToDouble(cmd["rx"]) / RATIO_SCALE * (currentBounds.Width - 1));
|
|
829
|
+
targetY += (int)Math.Round(Convert.ToDouble(cmd["ry"]) / RATIO_SCALE * (currentBounds.Height - 1));
|
|
830
|
+
} else if (currentImageWidth > 0 && currentImageHeight > 0) {
|
|
831
|
+
targetX += (int)Math.Round((rx / currentImageWidth) * currentBounds.Width);
|
|
832
|
+
targetY += (int)Math.Round((ry / currentImageHeight) * currentBounds.Height);
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
if (action == "move") {
|
|
836
|
+
Native.SetCursorPos(targetX, targetY);
|
|
837
|
+
} else if (action == "click") {
|
|
838
|
+
int button = cmd.ContainsKey("button") ? Convert.ToInt32(cmd["button"]) : 0;
|
|
839
|
+
Native.SetCursorPos(targetX, targetY);
|
|
840
|
+
Thread.Sleep(10);
|
|
841
|
+
PressModifiers(mods);
|
|
842
|
+
try {
|
|
843
|
+
if (button == 2) {
|
|
844
|
+
Native.mouse_event(Native.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
|
|
845
|
+
Thread.Sleep(15);
|
|
846
|
+
Native.mouse_event(Native.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
|
|
847
|
+
} else if (button == 1) {
|
|
848
|
+
Native.mouse_event(Native.MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
|
|
849
|
+
Thread.Sleep(15);
|
|
850
|
+
Native.mouse_event(Native.MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);
|
|
851
|
+
} else {
|
|
852
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
|
|
853
|
+
Thread.Sleep(15);
|
|
854
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
|
|
855
|
+
}
|
|
856
|
+
} finally {
|
|
857
|
+
ReleaseModifiers(mods);
|
|
858
|
+
}
|
|
859
|
+
if (button == 0) ProbeInputFocus();
|
|
860
|
+
} else if (action == "dblclick") {
|
|
861
|
+
Native.SetCursorPos(targetX, targetY);
|
|
862
|
+
Thread.Sleep(10);
|
|
863
|
+
PressModifiers(mods);
|
|
864
|
+
try {
|
|
865
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
|
|
866
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
|
|
867
|
+
Thread.Sleep(40);
|
|
868
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
|
|
869
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
|
|
870
|
+
} finally {
|
|
871
|
+
ReleaseModifiers(mods);
|
|
872
|
+
}
|
|
873
|
+
} else if (action == "rightclick") {
|
|
874
|
+
Native.SetCursorPos(targetX, targetY);
|
|
875
|
+
Thread.Sleep(10);
|
|
876
|
+
PressModifiers(mods);
|
|
877
|
+
try {
|
|
878
|
+
Native.mouse_event(Native.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
|
|
879
|
+
Thread.Sleep(15);
|
|
880
|
+
Native.mouse_event(Native.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
|
|
881
|
+
} finally {
|
|
882
|
+
ReleaseModifiers(mods);
|
|
883
|
+
}
|
|
884
|
+
} else if (action == "mousedown") {
|
|
885
|
+
int button = cmd.ContainsKey("button") ? Convert.ToInt32(cmd["button"]) : 0;
|
|
886
|
+
Native.SetCursorPos(targetX, targetY);
|
|
887
|
+
var newMods = new List<byte>();
|
|
888
|
+
for (int i = 0; i < mods.Count; i++) {
|
|
889
|
+
if (!heldModifiers.Contains(mods[i])) newMods.Add(mods[i]);
|
|
890
|
+
}
|
|
891
|
+
PressModifiers(newMods);
|
|
892
|
+
heldModifiers.AddRange(newMods);
|
|
893
|
+
MouseButton(button, true);
|
|
894
|
+
if (!heldButtons.Contains(button)) heldButtons.Add(button);
|
|
895
|
+
} else if (action == "mouseup") {
|
|
896
|
+
int button = cmd.ContainsKey("button") ? Convert.ToInt32(cmd["button"]) : 0;
|
|
897
|
+
// Gói không toạ độ = chỉ nhả tại chỗ (AI đang bấm trên máy): không kéo con trỏ đi.
|
|
898
|
+
if (cmd.ContainsKey("rx") || cmd.ContainsKey("x")) Native.SetCursorPos(targetX, targetY);
|
|
899
|
+
MouseButton(button, false);
|
|
900
|
+
heldButtons.Remove(button);
|
|
901
|
+
if (heldButtons.Count == 0) ReleaseHeldModifiers();
|
|
902
|
+
if (button == 0) ProbeInputFocus();
|
|
903
|
+
} else if (action == "scroll") {
|
|
904
|
+
int deltaY = cmd.ContainsKey("deltaY") ? Convert.ToInt32(cmd["deltaY"]) : 0;
|
|
905
|
+
int deltaX = cmd.ContainsKey("deltaX") ? Convert.ToInt32(cmd["deltaX"]) : 0;
|
|
906
|
+
Native.SetCursorPos(targetX, targetY);
|
|
907
|
+
PressModifiers(mods);
|
|
908
|
+
try {
|
|
909
|
+
if (deltaY != 0) {
|
|
910
|
+
if (Math.Abs(deltaY) >= 120) {
|
|
911
|
+
Native.mouse_event(Native.MOUSEEVENTF_WHEEL, 0, 0, -deltaY, 0);
|
|
912
|
+
wheelAccY = 0;
|
|
913
|
+
} else {
|
|
914
|
+
wheelAccY += deltaY;
|
|
915
|
+
if (Math.Abs(wheelAccY) >= 120) {
|
|
916
|
+
int steps = wheelAccY / 120;
|
|
917
|
+
Native.mouse_event(Native.MOUSEEVENTF_WHEEL, 0, 0, -steps * 120, 0);
|
|
918
|
+
wheelAccY %= 120;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
if (deltaX != 0) {
|
|
923
|
+
if (Math.Abs(deltaX) >= 120) {
|
|
924
|
+
Native.mouse_event(Native.MOUSEEVENTF_HWHEEL, 0, 0, deltaX, 0);
|
|
925
|
+
wheelAccX = 0;
|
|
926
|
+
} else {
|
|
927
|
+
wheelAccX += deltaX;
|
|
928
|
+
if (Math.Abs(wheelAccX) >= 120) {
|
|
929
|
+
int steps = wheelAccX / 120;
|
|
930
|
+
Native.mouse_event(Native.MOUSEEVENTF_HWHEEL, 0, 0, steps * 120, 0);
|
|
931
|
+
wheelAccX %= 120;
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
} finally {
|
|
936
|
+
ReleaseModifiers(mods);
|
|
937
|
+
}
|
|
938
|
+
} else if (action == "type") {
|
|
939
|
+
string text = cmd.ContainsKey("text") ? Convert.ToString(cmd["text"]) : "";
|
|
940
|
+
if (!string.IsNullOrEmpty(text)) TypeUnicode(text);
|
|
941
|
+
} else if (action == "clipboard") {
|
|
942
|
+
string text = cmd.ContainsKey("text") ? Convert.ToString(cmd["text"]) : "";
|
|
943
|
+
if (!string.IsNullOrEmpty(text)) ClipboardSync.SetText(text);
|
|
944
|
+
} else if (action == "paste") {
|
|
945
|
+
string text = cmd.ContainsKey("text") ? Convert.ToString(cmd["text"]) : "";
|
|
946
|
+
if (!string.IsNullOrEmpty(text)) {
|
|
947
|
+
if (ClipboardSync.SetText(text)) {
|
|
948
|
+
Native.keybd_event(0x11, 0, 0, UIntPtr.Zero);
|
|
949
|
+
Thread.Sleep(8);
|
|
950
|
+
Native.keybd_event(0x56, 0, 0, UIntPtr.Zero);
|
|
951
|
+
Thread.Sleep(15);
|
|
952
|
+
Native.keybd_event(0x56, 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
|
|
953
|
+
Native.keybd_event(0x11, 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
|
|
954
|
+
} else {
|
|
955
|
+
TypeUnicode(text);
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
} else if (action == "key") {
|
|
959
|
+
string key = cmd.ContainsKey("key") ? Convert.ToString(cmd["key"]) : "";
|
|
960
|
+
HandleSpecialKey(key);
|
|
961
|
+
} else if (action == "hotkey") {
|
|
962
|
+
System.Collections.ArrayList keys = cmd.ContainsKey("keys") ? cmd["keys"] as System.Collections.ArrayList : null;
|
|
963
|
+
if (keys != null && keys.Count > 0) {
|
|
964
|
+
var vks = new List<byte>();
|
|
965
|
+
for (int i = 0; i < keys.Count; i++) {
|
|
966
|
+
byte vk = KeyNameToVk(Convert.ToString(keys[i]));
|
|
967
|
+
if (vk != 0) vks.Add(vk);
|
|
968
|
+
}
|
|
969
|
+
for (int i = 0; i < vks.Count; i++) {
|
|
970
|
+
Native.keybd_event(vks[i], 0, 0, UIntPtr.Zero);
|
|
971
|
+
Thread.Sleep(10);
|
|
972
|
+
}
|
|
973
|
+
Thread.Sleep(40);
|
|
974
|
+
for (int i = vks.Count - 1; i >= 0; i--) {
|
|
975
|
+
Native.keybd_event(vks[i], 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
|
|
976
|
+
Thread.Sleep(10);
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
} catch {
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
/** Gõ nguyên văn: mỗi đơn vị UTF-16 một cặp nhấn / nhả KEYEVENTF_UNICODE (cặp đại diện của emoji đi liền nhau), gửi theo lô. */
|
|
985
|
+
static void TypeUnicode(string text) {
|
|
986
|
+
const int chunk = 64;
|
|
987
|
+
int size = Marshal.SizeOf(typeof(Native.INPUT));
|
|
988
|
+
int start = 0;
|
|
989
|
+
while (start < text.Length) {
|
|
990
|
+
int end = Math.Min(text.Length, start + chunk);
|
|
991
|
+
// Không cắt đôi một cặp đại diện giữa hai lô.
|
|
992
|
+
if (end < text.Length && char.IsHighSurrogate(text[end - 1])) end--;
|
|
993
|
+
var inputs = new Native.INPUT[(end - start) * 2];
|
|
994
|
+
for (int i = start; i < end; i++) {
|
|
995
|
+
int k = (i - start) * 2;
|
|
996
|
+
inputs[k].type = Native.INPUT_KEYBOARD;
|
|
997
|
+
inputs[k].u.ki = new Native.KEYBDINPUT { wVk = 0, wScan = text[i], dwFlags = Native.KEYEVENTF_UNICODE };
|
|
998
|
+
inputs[k + 1].type = Native.INPUT_KEYBOARD;
|
|
999
|
+
inputs[k + 1].u.ki = new Native.KEYBDINPUT { wVk = 0, wScan = text[i], dwFlags = Native.KEYEVENTF_UNICODE | Native.KEYEVENTF_KEYUP };
|
|
1000
|
+
}
|
|
1001
|
+
Native.SendInput((uint)inputs.Length, inputs, size);
|
|
1002
|
+
start = end;
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
static int focusProbeSeq = 0;
|
|
1007
|
+
|
|
1008
|
+
/**
|
|
1009
|
+
* Sau cú nhấp trái của người xem: chờ ứng dụng nhận tiêu điểm rồi báo ô đang chọn có phải ô nhập chữ (có caret) không —
|
|
1010
|
+
* trình duyệt trên điện thoại dựa vào đó tự mở bàn phím ảo. Kiểm sớm ở 40 ms để mở bàn phím nhanh, nếu chưa thấy mới chờ thêm.
|
|
1011
|
+
*/
|
|
1012
|
+
static void ProbeInputFocus() {
|
|
1013
|
+
int seq = Interlocked.Increment(ref focusProbeSeq);
|
|
1014
|
+
ThreadPool.QueueUserWorkItem(_ => {
|
|
1015
|
+
try {
|
|
1016
|
+
Thread.Sleep(40);
|
|
1017
|
+
if (seq != focusProbeSeq) return;
|
|
1018
|
+
bool editable = HasSystemCaret();
|
|
1019
|
+
if (!editable) {
|
|
1020
|
+
Thread.Sleep(90);
|
|
1021
|
+
if (seq != focusProbeSeq) return;
|
|
1022
|
+
editable = HasSystemCaret() || FocusedIsTextInput();
|
|
1023
|
+
}
|
|
1024
|
+
var msg = new Dictionary<string, object>();
|
|
1025
|
+
msg["t"] = "input_focus";
|
|
1026
|
+
msg["editable"] = editable;
|
|
1027
|
+
Out.Send(msg);
|
|
1028
|
+
} catch {
|
|
1029
|
+
}
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
static bool HasSystemCaret() {
|
|
1034
|
+
var info = new Native.GUITHREADINFO();
|
|
1035
|
+
info.cbSize = Marshal.SizeOf(typeof(Native.GUITHREADINFO));
|
|
1036
|
+
IntPtr fg = Native.GetForegroundWindow();
|
|
1037
|
+
uint tid = fg == IntPtr.Zero ? 0 : Native.GetWindowThreadProcessId(fg, IntPtr.Zero);
|
|
1038
|
+
return tid != 0 && Native.GetGUIThreadInfo(tid, ref info) && info.hwndCaret != IntPtr.Zero;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
/** Chromium / Edge / Electron chỉ tạo caret hệ thống khi bật trợ năng → hỏi UI Automation: ô Edit, hoặc ô có giá trị sửa được. */
|
|
1042
|
+
static bool FocusedIsTextInput() {
|
|
1043
|
+
try {
|
|
1044
|
+
var fe = AutomationElement.FocusedElement;
|
|
1045
|
+
if (fe == null) return false;
|
|
1046
|
+
var ct = fe.Current.ControlType;
|
|
1047
|
+
if (ct == ControlType.Edit) return true;
|
|
1048
|
+
if (ct == ControlType.ComboBox || ct == ControlType.Document) {
|
|
1049
|
+
object vp;
|
|
1050
|
+
if (fe.TryGetCurrentPattern(ValuePattern.Pattern, out vp)) return !((ValuePattern)vp).Current.IsReadOnly;
|
|
1051
|
+
}
|
|
1052
|
+
} catch {
|
|
1053
|
+
}
|
|
1054
|
+
return false;
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
static void HandleSpecialKey(string key) {
|
|
1058
|
+
byte vk = KeyNameToVk(key);
|
|
1059
|
+
if (vk != 0) {
|
|
1060
|
+
Native.keybd_event(vk, 0, 0, UIntPtr.Zero);
|
|
1061
|
+
Thread.Sleep(15);
|
|
1062
|
+
Native.keybd_event(vk, 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
` + DESKTOP_CAST_CAPTURE_CSHARP;
|
|
1068
|
+
|
|
1069
|
+
export const DESKTOP_CAST_PS1 = String.raw`
|
|
1070
|
+
$ErrorActionPreference = 'Stop'
|
|
1071
|
+
$dll = $env:AIVIN_CAST_DLL
|
|
1072
|
+
$src = $env:AIVIN_CAST_SRC
|
|
1073
|
+
$refs = @('UIAutomationClient', 'UIAutomationTypes', 'WindowsBase', 'System.Drawing', 'System.Windows.Forms', 'System.Web.Extensions', 'System.Core')
|
|
1074
|
+
foreach ($r in $refs) { Add-Type -AssemblyName $r }
|
|
1075
|
+
if ($dll -and (Test-Path -LiteralPath $dll)) {
|
|
1076
|
+
Add-Type -Path $dll
|
|
1077
|
+
} else {
|
|
1078
|
+
Add-Type -TypeDefinition ([IO.File]::ReadAllText($src)) -ReferencedAssemblies $refs -OutputAssembly $dll -OutputType Library
|
|
1079
|
+
if ($env:AIVIN_CAST_COMPILE_ONLY -eq '1') { exit 0 }
|
|
1080
|
+
Add-Type -Path $dll
|
|
1081
|
+
}
|
|
1082
|
+
if ($env:AIVIN_CAST_COMPILE_ONLY -eq '1') { exit 0 }
|
|
1083
|
+
try { [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false) } catch {}
|
|
1084
|
+
try {
|
|
1085
|
+
[AivinDesktopCast.CastHost]::Run($env:AIVIN_CAST_ARGS)
|
|
1086
|
+
} catch {
|
|
1087
|
+
$m = @{ t = 'error'; error = $_.Exception.Message } | ConvertTo-Json -Compress
|
|
1088
|
+
[Console]::Out.WriteLine($m)
|
|
1089
|
+
exit 1
|
|
1090
|
+
}
|
|
1091
|
+
`;
|