@aivin-labs/cli 1.0.47 → 1.0.51

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.
Files changed (34) hide show
  1. package/README.md +1 -1
  2. package/bin/cli.mjs +1 -1
  3. package/bin/lib/auth.mjs +1 -1
  4. package/bin/lib/context.mjs +3 -3
  5. package/bin/lib/openFile.mjs +3 -2
  6. package/bin/lib/remote/client.mjs +1418 -1221
  7. package/bin/lib/remote/confirmBarrier.mjs +45 -0
  8. package/bin/lib/remote/constants.mjs +3 -1
  9. package/bin/lib/remote/desktop/aivinIco.mjs +696 -0
  10. package/bin/lib/remote/desktop/castAuth.mjs +23 -9
  11. package/bin/lib/remote/desktop/castConsent.mjs +37 -6
  12. package/bin/lib/remote/desktop/castFileDrop.mjs +24 -0
  13. package/bin/lib/remote/desktop/castInput.mjs +63 -19
  14. package/bin/lib/remote/desktop/castTouchViewers.mjs +26 -0
  15. package/bin/lib/remote/desktop/desktopCast.mjs +25 -2
  16. package/bin/lib/remote/desktop/desktopCastSource.mjs +298 -101
  17. package/bin/lib/remote/desktop/index.mjs +5 -3
  18. package/bin/lib/remote/desktop/traySource.mjs +1385 -1343
  19. package/bin/lib/remote/desktop/webrtcWorker.mjs +3 -0
  20. package/bin/lib/remote/deviceProfile.mjs +2 -0
  21. package/bin/lib/remote/gui/index.mjs +485 -480
  22. package/bin/lib/remote/humanInput.mjs +117 -0
  23. package/bin/lib/remote/idempotency.mjs +19 -2
  24. package/bin/lib/remote/keepAwake.mjs +15 -9
  25. package/bin/lib/remote/localConfirm.mjs +26 -7
  26. package/bin/lib/remote/mascot/ask.mjs +23 -4
  27. package/bin/lib/remote/mascot/askHostSource.mjs +10 -4
  28. package/bin/lib/remote/processes.mjs +231 -230
  29. package/bin/lib/remote/scheduler.mjs +164 -124
  30. package/bin/lib/remote/toolHandlers.mjs +3 -2
  31. package/bin/lib/remote/winTree.mjs +29 -0
  32. package/bin/lib/servers.mjs +31 -1
  33. package/docs/CLI.md +2 -2
  34. package/package.json +1 -1
@@ -35,9 +35,28 @@ using System.Windows.Forms;
35
35
  namespace AivinDesktopCast {
36
36
  static class Native {
37
37
  [DllImport("user32.dll")] public static extern bool SetProcessDPIAware();
38
+ [DllImport("user32.dll")] public static extern bool SetProcessDpiAwarenessContext(IntPtr value);
39
+ [DllImport("shcore.dll")] public static extern int SetProcessDpiAwareness(int value);
38
40
  [DllImport("user32.dll")] public static extern bool SetCursorPos(int X, int Y);
39
- [DllImport("user32.dll")] public static extern void mouse_event(uint dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
40
- [DllImport("user32.dll")] public static extern void keybd_event(byte bVk, ushort bScan, uint dwFlags, UIntPtr dwExtraInfo);
41
+ [DllImport("user32.dll", EntryPoint = "mouse_event")] static extern void mouse_event_native(uint dwFlags, int dx, int dy, int dwData, UIntPtr dwExtraInfo);
42
+ [DllImport("user32.dll", EntryPoint = "keybd_event")] static extern void keybd_event_native(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
43
+
44
+ // Dấu của phiên (humanInput.mjs): mọi thao tác tiến trình này bơm vào mang tag bí mật trong dwExtraInfo — hộp xác nhận của Aivin nhận ra
45
+ // thao tác của người đang điều khiển từ xa (đã được chủ máy đồng ý), phân biệt với SendInput / UI Automation của AI. Tham số dwExtraInfo
46
+ // của nơi gọi bị bỏ qua, luôn dùng tag.
47
+ public static UIntPtr InputTag = UIntPtr.Zero;
48
+ public static void mouse_event(uint dwFlags, int dx, int dy, int dwData, int dwExtraInfo) { mouse_event_native(dwFlags, dx, dy, dwData, InputTag); }
49
+ public static void keybd_event(byte bVk, ushort bScan, uint dwFlags, UIntPtr dwExtraInfo) { keybd_event_native(bVk, (byte)bScan, dwFlags, InputTag); }
50
+
51
+ /**
52
+ * Per-monitor v2 (Win10 1703+) → per-monitor (8.1) → system aware. Chỉ system aware thì máy nhiều màn hình khác tỷ lệ phóng (100 % + 150 %)
53
+ * bị Windows ảo hoá toạ độ ở màn hình phụ: ảnh chụp / con trỏ / click lệch nhau.
54
+ */
55
+ public static void UsePerMonitorDpi() {
56
+ try { if (SetProcessDpiAwarenessContext(new IntPtr(-4))) return; } catch { }
57
+ try { if (SetProcessDpiAwareness(2) == 0) return; } catch { }
58
+ try { SetProcessDPIAware(); } catch { }
59
+ }
41
60
  [DllImport("user32.dll")] public static extern short VkKeyScan(char ch);
42
61
  [DllImport("user32.dll")] public static extern uint MapVirtualKey(uint code, uint mapType);
43
62
 
@@ -159,6 +178,207 @@ namespace AivinDesktopCast {
159
178
  }
160
179
  }
161
180
 
181
+ /** Đưa nội dung vào clipboard của máy rồi bấm Ctrl + V. Không ghi được clipboard thì không bấm: bấm sẽ dán nội dung cũ vào chỗ người dùng đang đứng. */
182
+ public static class CastPaste {
183
+ public static bool Run(Func<System.Windows.Forms.DataObject> build, bool notifyOnFail) {
184
+ bool ok = false;
185
+ var sta = new Thread(() => {
186
+ try {
187
+ System.Windows.Forms.Clipboard.SetDataObject(build(), true, 10, 100);
188
+ ok = true;
189
+ } catch { }
190
+ });
191
+ sta.SetApartmentState(ApartmentState.STA);
192
+ sta.Start();
193
+ bool done = sta.Join(3000) && ok;
194
+ if (done) PressPaste();
195
+ else if (notifyOnFail) Out.Send(Out.Msg("paste_failed"));
196
+ return done;
197
+ }
198
+
199
+ static void PressPaste() {
200
+ Thread.Sleep(60);
201
+ Native.keybd_event(0x11, 0, 0, UIntPtr.Zero);
202
+ Thread.Sleep(20);
203
+ Native.keybd_event(0x56, 0, 0, UIntPtr.Zero);
204
+ Thread.Sleep(30);
205
+ Native.keybd_event(0x56, 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
206
+ Thread.Sleep(15);
207
+ Native.keybd_event(0x11, 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
208
+ }
209
+
210
+ /** Chỉ dạng ảnh, không kèm dạng tệp: có dạng tệp thì nhiều ứng dụng coi là gửi tệp đính kèm thay vì chèn ảnh. Dạng PNG giữ trong suốt; dạng bitmap nền trắng vì ứng dụng cũ vẽ vùng trong suốt thành đen. */
211
+ public static System.Windows.Forms.DataObject ImageData(byte[] bytes) {
212
+ var data = new System.Windows.Forms.DataObject();
213
+ using (var input = new MemoryStream(bytes))
214
+ using (var src = Image.FromStream(input)) {
215
+ var flat = new Bitmap(src.Width, src.Height, PixelFormat.Format32bppArgb);
216
+ using (var g = Graphics.FromImage(flat)) {
217
+ g.Clear(Color.White);
218
+ g.DrawImage(src, 0, 0, src.Width, src.Height);
219
+ }
220
+ data.SetImage(flat);
221
+ var png = new MemoryStream();
222
+ src.Save(png, ImageFormat.Png);
223
+ png.Position = 0;
224
+ data.SetData("PNG", false, png);
225
+ }
226
+ return data;
227
+ }
228
+
229
+ public static System.Windows.Forms.DataObject FileData(string filePath) {
230
+ var data = new System.Windows.Forms.DataObject();
231
+ var list = new System.Collections.Specialized.StringCollection();
232
+ list.Add(filePath);
233
+ data.SetFileDropList(list);
234
+ string ext = Path.GetExtension(filePath).ToLowerInvariant();
235
+ if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".bmp" || ext == ".gif" || ext == ".webp") {
236
+ try {
237
+ using (var src = Image.FromFile(filePath)) {
238
+ var bmp = new Bitmap(src.Width, src.Height, PixelFormat.Format32bppArgb);
239
+ using (var g = Graphics.FromImage(bmp)) g.DrawImage(src, 0, 0, src.Width, src.Height);
240
+ data.SetImage(bmp);
241
+ }
242
+ } catch { }
243
+ }
244
+ return data;
245
+ }
246
+
247
+ public static System.Windows.Forms.DataObject HtmlData(string html, string plain) {
248
+ var data = new System.Windows.Forms.DataObject();
249
+ data.SetData(System.Windows.Forms.DataFormats.Html, false, new MemoryStream(BuildCfHtml(html)));
250
+ if (!string.IsNullOrEmpty(plain)) data.SetData(System.Windows.Forms.DataFormats.UnicodeText, plain);
251
+ return data;
252
+ }
253
+
254
+ /** Định dạng CF_HTML của Windows: phần đầu ghi vị trí theo BYTE của bản UTF-8, lệch một byte là ứng dụng đọc sai. */
255
+ public static byte[] BuildCfHtml(string html) {
256
+ const string pre = "<!DOCTYPE html><html><body><!--StartFragment-->";
257
+ const string post = "<!--EndFragment--></body></html>";
258
+ const string layout = "Version:0.9\r\nStartHTML:{0:D10}\r\nEndHTML:{1:D10}\r\nStartFragment:{2:D10}\r\nEndFragment:{3:D10}\r\n";
259
+ int headerLength = string.Format(layout, 0, 0, 0, 0).Length;
260
+ int fragmentStart = headerLength + pre.Length;
261
+ int fragmentEnd = fragmentStart + Encoding.UTF8.GetByteCount(html);
262
+ int end = fragmentEnd + post.Length;
263
+ return Encoding.UTF8.GetBytes(string.Format(layout, headerLength, end, fragmentStart, fragmentEnd) + pre + html + post);
264
+ }
265
+ }
266
+
267
+ /**
268
+ * Thả tệp thật vào vị trí trên màn hình: mở một thao tác kéo tệp của Windows rồi đưa chuột tới đích, nên ứng dụng nhận như người kéo thật
269
+ * (vùng tải lên sáng lên, nhận nhiều tệp một lần). Thao tác kéo phải bắt đầu từ một cửa sổ của chính mình: nút chuột được nhấn trên ô gần như
270
+ * trong suốt này, không chạm vào thứ gì của người dùng.
271
+ */
272
+ public static class CastDrop {
273
+ const int SOURCE_SIZE = 16;
274
+ const int SOURCE_OFFSET = 36;
275
+ const int MOVE_STEPS = 10;
276
+ const int MOVE_STEP_MS = 25;
277
+ const int HOVER_TICKS = 8;
278
+ const int HOVER_TICK_MS = 40;
279
+ const int WINDOW_WAIT_MS = 2000;
280
+ const int START_WAIT_MS = 1000;
281
+ const int FINISH_WAIT_MS = 8000;
282
+
283
+ sealed class SourceWindow : Form {
284
+ readonly DataObject data;
285
+ public volatile bool Started;
286
+ public DragDropEffects Result = DragDropEffects.None;
287
+
288
+ public SourceWindow(DataObject payload, int centerX, int centerY) {
289
+ data = payload;
290
+ FormBorderStyle = FormBorderStyle.None;
291
+ ShowInTaskbar = false;
292
+ TopMost = true;
293
+ StartPosition = FormStartPosition.Manual;
294
+ Bounds = new Rectangle(centerX - SOURCE_SIZE / 2, centerY - SOURCE_SIZE / 2, SOURCE_SIZE, SOURCE_SIZE);
295
+ Opacity = 0.02;
296
+ BackColor = Color.Black;
297
+ }
298
+
299
+ protected override bool ShowWithoutActivation { get { return true; } }
300
+
301
+ protected override CreateParams CreateParams {
302
+ get {
303
+ var p = base.CreateParams;
304
+ p.ExStyle |= 0x08000000 | 0x00000080; // Không lấy tiêu điểm, không hiện trong Alt + Tab.
305
+ return p;
306
+ }
307
+ }
308
+
309
+ protected override void OnMouseDown(MouseEventArgs e) {
310
+ base.OnMouseDown(e);
311
+ Started = true;
312
+ try { Result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Move); } catch { }
313
+ Close();
314
+ }
315
+ }
316
+
317
+ static int Away(int target, int min, int max) {
318
+ int start = target - SOURCE_OFFSET;
319
+ return start - SOURCE_SIZE < min ? Math.Min(target + SOURCE_OFFSET, max - SOURCE_SIZE) : start;
320
+ }
321
+
322
+ /** Di chuột từng bước rồi dừng một lúc ở đích: trang web và ứng dụng cần vài sự kiện rê mới bật vùng thả. Luôn nhả nút, kể cả khi lỗi. */
323
+ static void Drive(SourceWindow window, int fromX, int fromY, int toX, int toY) {
324
+ Native.SetCursorPos(fromX, fromY);
325
+ Thread.Sleep(80);
326
+ Native.mouse_event(Native.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
327
+ try {
328
+ var waited = System.Diagnostics.Stopwatch.StartNew();
329
+ while (!window.Started && waited.ElapsedMilliseconds < START_WAIT_MS && CastHost.Running) Thread.Sleep(10);
330
+ if (!window.Started) return;
331
+ Thread.Sleep(80);
332
+ for (int i = 1; i <= MOVE_STEPS && CastHost.Running; i++) {
333
+ Native.SetCursorPos(fromX + (toX - fromX) * i / MOVE_STEPS, fromY + (toY - fromY) * i / MOVE_STEPS);
334
+ Thread.Sleep(MOVE_STEP_MS);
335
+ }
336
+ for (int i = 0; i < HOVER_TICKS && CastHost.Running; i++) {
337
+ Native.SetCursorPos(toX + (i % 2 == 0 ? 1 : 0), toY);
338
+ Thread.Sleep(HOVER_TICK_MS);
339
+ }
340
+ Native.SetCursorPos(toX, toY);
341
+ Thread.Sleep(60);
342
+ } finally {
343
+ Native.mouse_event(Native.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
344
+ }
345
+ }
346
+
347
+ /** Trả về đúng khi ứng dụng ở đích nhận thả. */
348
+ public static bool Run(IList<string> paths, int targetX, int targetY) {
349
+ var list = new System.Collections.Specialized.StringCollection();
350
+ foreach (var path in paths) list.Add(path);
351
+ var data = new DataObject();
352
+ data.SetFileDropList(list);
353
+
354
+ var area = System.Windows.Forms.Screen.FromPoint(new Point(targetX, targetY)).Bounds;
355
+ int fromX = Away(targetX, area.Left, area.Right);
356
+ int fromY = Away(targetY, area.Top, area.Bottom);
357
+
358
+ SourceWindow window = null;
359
+ var shown = new ManualResetEvent(false);
360
+ var host = new Thread(() => {
361
+ try {
362
+ window = new SourceWindow(data, fromX, fromY);
363
+ window.Shown += (sender, e) => shown.Set();
364
+ Application.Run(window);
365
+ } catch { shown.Set(); }
366
+ });
367
+ host.IsBackground = true;
368
+ host.SetApartmentState(ApartmentState.STA);
369
+ host.Start();
370
+ if (!shown.WaitOne(WINDOW_WAIT_MS) || window == null) return false;
371
+
372
+ Drive(window, fromX, fromY, targetX, targetY);
373
+ if (!window.Started) {
374
+ try { window.BeginInvoke(new Action(window.Close)); } catch { }
375
+ return false;
376
+ }
377
+ host.Join(FINISH_WAIT_MS);
378
+ return window.Result != DragDropEffects.None;
379
+ }
380
+ }
381
+
162
382
  /** 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. */
163
383
  static class RawPipe {
164
384
  static readonly object gate = new object();
@@ -541,9 +761,11 @@ namespace AivinDesktopCast {
541
761
  static long lastScreensMs = -SCREENS_REFRESH_MS;
542
762
 
543
763
  public static void Run(string argsJson) {
544
- Native.SetProcessDPIAware();
764
+ Native.UsePerMonitorDpi();
545
765
  Native.timeBeginPeriod(1);
546
766
  var a = Out.Parse(argsJson);
767
+ long inputTag;
768
+ if (a.ContainsKey("input_tag") && long.TryParse(Convert.ToString(a["input_tag"]), out inputTag) && inputTag > 0 && inputTag <= 0xFFFFFFFFL) Native.InputTag = new UIntPtr((ulong)inputTag);
547
769
  currentMonitor = a.ContainsKey("monitor") ? Convert.ToInt32(a["monitor"]) : -1;
548
770
  targetMaxFps = a.ContainsKey("max_fps") ? Convert.ToInt32(a["max_fps"]) : 15;
549
771
  if (targetMaxFps < 1) targetMaxFps = 1;
@@ -562,6 +784,9 @@ namespace AivinDesktopCast {
562
784
  var inputThread = new Thread(ReadInputLoop);
563
785
  inputThread.IsBackground = true;
564
786
  inputThread.Start();
787
+ inputExecutor = new Thread(ExecuteInputLoop);
788
+ inputExecutor.IsBackground = true;
789
+ inputExecutor.Start();
565
790
 
566
791
  var readyMsg = Out.Msg("ready");
567
792
  readyMsg["monitor"] = currentMonitor;
@@ -648,6 +873,8 @@ namespace AivinDesktopCast {
648
873
  else if (!source.UsingDxgi) Thread.Sleep(1);
649
874
  }
650
875
  source.Dispose();
876
+ StopInput();
877
+ try { if (inputExecutor != null) inputExecutor.Join(2000); } catch { }
651
878
 
652
879
  var stoppedMsg = Out.Msg("stopped");
653
880
  Out.Send(stoppedMsg);
@@ -688,28 +915,50 @@ namespace AivinDesktopCast {
688
915
  Out.Send(frameMsg);
689
916
  }
690
917
 
918
+ /**
919
+ * Đọc lệnh từ stdin trên một luồng, thực thi trên luồng khác (ExecuteInputLoop). Trước đây đọc + thực thi chung một luồng: lệnh "stop"
920
+ * nằm sau các thao tác còn xếp hàng (một lần kéo thả, gõ dài…) nên chúng vẫn chạy hết sau khi chủ máy bấm ngắt. Nay đọc thấy "stop" (hoặc
921
+ * stdin đóng — daemon chết) là dừng ngay, bỏ phần còn xếp hàng.
922
+ */
923
+ static readonly Queue<Dictionary<string, object>> inputQueue = new Queue<Dictionary<string, object>>();
924
+ static Thread inputExecutor;
925
+
926
+ static void StopInput() {
927
+ running = false;
928
+ lock (inputQueue) { inputQueue.Clear(); Monitor.PulseAll(inputQueue); }
929
+ }
930
+
691
931
  static void ReadInputLoop() {
692
932
  try {
693
933
  string line;
694
934
  while (running && (line = Out.Stdin.ReadLine()) != null) {
695
935
  line = line.Trim();
696
936
  if (string.IsNullOrEmpty(line)) continue;
697
- if (line == "stop") {
698
- running = false;
699
- break;
700
- }
937
+ if (line == "stop") break;
701
938
  var cmd = Out.Parse(line);
702
939
  string op = cmd.ContainsKey("op") ? Convert.ToString(cmd["op"]) : "";
703
- if (op == "stop") {
704
- running = false;
705
- break;
706
- } else if (op == "input") {
707
- HandleInput(cmd);
708
- } else if (op == "config") {
709
- HandleConfig(cmd);
710
- } else if (op == "refresh") {
711
- refreshRequested = true;
940
+ if (op == "stop") break;
941
+ lock (inputQueue) { inputQueue.Enqueue(cmd); Monitor.Pulse(inputQueue); }
942
+ }
943
+ } catch {
944
+ } finally {
945
+ StopInput();
946
+ }
947
+ }
948
+
949
+ static void ExecuteInputLoop() {
950
+ try {
951
+ while (true) {
952
+ Dictionary<string, object> cmd;
953
+ lock (inputQueue) {
954
+ while (running && inputQueue.Count == 0) Monitor.Wait(inputQueue, 500);
955
+ if (!running) break;
956
+ cmd = inputQueue.Dequeue();
712
957
  }
958
+ string op = cmd.ContainsKey("op") ? Convert.ToString(cmd["op"]) : "";
959
+ if (op == "input") HandleInput(cmd);
960
+ else if (op == "config") HandleConfig(cmd);
961
+ else if (op == "refresh") refreshRequested = true;
713
962
  }
714
963
  } catch {
715
964
  running = false;
@@ -1054,95 +1303,33 @@ namespace AivinDesktopCast {
1054
1303
  } else if (action == "paste_image") {
1055
1304
  string b64 = cmd.ContainsKey("data") ? Convert.ToString(cmd["data"]) : "";
1056
1305
  if (!string.IsNullOrEmpty(b64)) {
1057
- if (cmd.ContainsKey("click") && Convert.ToBoolean(cmd["click"])) {
1058
- Native.SetCursorPos(targetX, targetY);
1059
- Thread.Sleep(20);
1060
- Native.mouse_event(Native.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
1061
- Thread.Sleep(15);
1062
- Native.mouse_event(Native.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
1063
- Thread.Sleep(35);
1064
- }
1065
- var sta = new Thread(() => {
1066
- try {
1067
- byte[] bytes = Convert.FromBase64String(b64);
1068
- using (var ms = new System.IO.MemoryStream(bytes)) {
1069
- using (var src = System.Drawing.Image.FromStream(ms)) {
1070
- var dataObj = new System.Windows.Forms.DataObject();
1071
- var bmp = new System.Drawing.Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
1072
- using (var g = System.Drawing.Graphics.FromImage(bmp)) {
1073
- g.DrawImage(src, 0, 0, src.Width, src.Height);
1074
- }
1075
- dataObj.SetImage(bmp);
1076
- try {
1077
- string tempFile = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "aivin_paste_" + DateTime.Now.Ticks + ".png");
1078
- bmp.Save(tempFile, System.Drawing.Imaging.ImageFormat.Png);
1079
- var sc = new System.Collections.Specialized.StringCollection();
1080
- sc.Add(tempFile);
1081
- dataObj.SetFileDropList(sc);
1082
- } catch { }
1083
- System.Windows.Forms.Clipboard.SetDataObject(dataObj, true);
1084
- }
1085
- }
1086
- } catch { }
1087
- });
1088
- sta.SetApartmentState(ApartmentState.STA);
1089
- sta.Start();
1090
- if (sta.Join(3000)) {
1091
- Thread.Sleep(60);
1092
- Native.keybd_event(0x11, 0, 0, UIntPtr.Zero);
1093
- Thread.Sleep(20);
1094
- Native.keybd_event(0x56, 0, 0, UIntPtr.Zero);
1095
- Thread.Sleep(30);
1096
- Native.keybd_event(0x56, 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
1097
- Thread.Sleep(15);
1098
- Native.keybd_event(0x11, 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
1099
- }
1306
+ ClickIfAsked(cmd, targetX, targetY);
1307
+ CastPaste.Run(() => CastPaste.ImageData(Convert.FromBase64String(b64)), true);
1100
1308
  }
1309
+ } else if (action == "paste_html") {
1310
+ string html = cmd.ContainsKey("html") ? Convert.ToString(cmd["html"]) : "";
1311
+ string plain = cmd.ContainsKey("text") ? Convert.ToString(cmd["text"]) : "";
1312
+ if (!string.IsNullOrEmpty(html)) CastPaste.Run(() => CastPaste.HtmlData(html, plain), true);
1101
1313
  } else if (action == "paste_file") {
1102
1314
  string filePath = cmd.ContainsKey("path") ? Convert.ToString(cmd["path"]) : "";
1315
+ bool asImage = cmd.ContainsKey("as_image") && Convert.ToBoolean(cmd["as_image"]);
1103
1316
  if (!string.IsNullOrEmpty(filePath) && System.IO.File.Exists(filePath)) {
1104
- if (cmd.ContainsKey("click") && Convert.ToBoolean(cmd["click"])) {
1105
- Native.SetCursorPos(targetX, targetY);
1106
- Thread.Sleep(20);
1107
- Native.mouse_event(Native.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
1108
- Thread.Sleep(15);
1109
- Native.mouse_event(Native.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
1110
- Thread.Sleep(35);
1111
- }
1112
- var sta = new Thread(() => {
1113
- try {
1114
- var dataObj = new System.Windows.Forms.DataObject();
1115
- var sc = new System.Collections.Specialized.StringCollection();
1116
- sc.Add(filePath);
1117
- dataObj.SetFileDropList(sc);
1118
- string ext = System.IO.Path.GetExtension(filePath).ToLowerInvariant();
1119
- if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".bmp" || ext == ".gif" || ext == ".webp") {
1120
- try {
1121
- using (var src = System.Drawing.Image.FromFile(filePath)) {
1122
- var bmp = new System.Drawing.Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
1123
- using (var g = System.Drawing.Graphics.FromImage(bmp)) {
1124
- g.DrawImage(src, 0, 0, src.Width, src.Height);
1125
- }
1126
- dataObj.SetImage(bmp);
1127
- }
1128
- } catch { }
1129
- }
1130
- System.Windows.Forms.Clipboard.SetDataObject(dataObj, true);
1131
- } catch { }
1132
- });
1133
- sta.SetApartmentState(ApartmentState.STA);
1134
- sta.Start();
1135
- if (sta.Join(3000)) {
1136
- Thread.Sleep(60);
1137
- Native.keybd_event(0x11, 0, 0, UIntPtr.Zero);
1138
- Thread.Sleep(20);
1139
- Native.keybd_event(0x56, 0, 0, UIntPtr.Zero);
1140
- Thread.Sleep(30);
1141
- Native.keybd_event(0x56, 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
1142
- Thread.Sleep(15);
1143
- Native.keybd_event(0x11, 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
1317
+ ClickIfAsked(cmd, targetX, targetY);
1318
+ // Ảnh từ clipboard của người xem: dán như ảnh, tệp lưu tạm không còn cần nữa. Không đọc được thì dán như tệp.
1319
+ if (asImage && CastPaste.Run(() => CastPaste.ImageData(System.IO.File.ReadAllBytes(filePath)), false)) {
1320
+ try { System.IO.File.Delete(filePath); } catch { }
1321
+ } else {
1322
+ CastPaste.Run(() => CastPaste.FileData(filePath), true);
1144
1323
  }
1145
1324
  }
1325
+ } else if (action == "drop_files") {
1326
+ var dropPaths = new List<string>();
1327
+ var rawPaths = cmd.ContainsKey("paths") ? cmd["paths"] as System.Collections.ArrayList : null;
1328
+ if (rawPaths != null) foreach (var item in rawPaths) {
1329
+ string one = Convert.ToString(item);
1330
+ if (System.IO.File.Exists(one)) dropPaths.Add(one);
1331
+ }
1332
+ if (dropPaths.Count > 0 && !CastDrop.Run(dropPaths, targetX, targetY)) Out.Send(Out.Msg("drop_refused"));
1146
1333
  } else if (action == "key") {
1147
1334
  string key = cmd.ContainsKey("key") ? Convert.ToString(cmd["key"]) : "";
1148
1335
  HandleSpecialKey(key);
@@ -1186,9 +1373,9 @@ namespace AivinDesktopCast {
1186
1373
  for (int i = start; i < end; i++) {
1187
1374
  int k = (i - start) * 2;
1188
1375
  inputs[k].type = Native.INPUT_KEYBOARD;
1189
- inputs[k].u.ki = new Native.KEYBDINPUT { wVk = 0, wScan = text[i], dwFlags = Native.KEYEVENTF_UNICODE };
1376
+ inputs[k].u.ki = new Native.KEYBDINPUT { wVk = 0, wScan = text[i], dwFlags = Native.KEYEVENTF_UNICODE, dwExtraInfo = (IntPtr)(long)Native.InputTag.ToUInt64() };
1190
1377
  inputs[k + 1].type = Native.INPUT_KEYBOARD;
1191
- inputs[k + 1].u.ki = new Native.KEYBDINPUT { wVk = 0, wScan = text[i], dwFlags = Native.KEYEVENTF_UNICODE | Native.KEYEVENTF_KEYUP };
1378
+ inputs[k + 1].u.ki = new Native.KEYBDINPUT { wVk = 0, wScan = text[i], dwFlags = Native.KEYEVENTF_UNICODE | Native.KEYEVENTF_KEYUP, dwExtraInfo = (IntPtr)(long)Native.InputTag.ToUInt64() };
1192
1379
  }
1193
1380
  Native.SendInput((uint)inputs.Length, inputs, size);
1194
1381
  start = end;
@@ -1246,6 +1433,16 @@ namespace AivinDesktopCast {
1246
1433
  return false;
1247
1434
  }
1248
1435
 
1436
+ static void ClickIfAsked(Dictionary<string, object> cmd, int x, int y) {
1437
+ if (!cmd.ContainsKey("click") || !Convert.ToBoolean(cmd["click"])) return;
1438
+ Native.SetCursorPos(x, y);
1439
+ Thread.Sleep(20);
1440
+ Native.mouse_event(Native.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
1441
+ Thread.Sleep(15);
1442
+ Native.mouse_event(Native.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
1443
+ Thread.Sleep(35);
1444
+ }
1445
+
1249
1446
  static void HandleSpecialKey(string key) {
1250
1447
  byte vk = KeyNameToVk(key);
1251
1448
  if (vk != 0) {
@@ -6,6 +6,7 @@ import { execFileSync, spawn } from 'child_process';
6
6
  import { REMOTE_CONFIG_DIR } from '../constants.mjs';
7
7
  import { lockHolder } from '../daemonLock.mjs';
8
8
  import { AIVIN_LOGO_PNG_BASE64 } from '../brand.mjs';
9
+ import { AIVIN_ICO_BASE64 } from './aivinIco.mjs';
9
10
  import { TRAY_CS } from './traySource.mjs';
10
11
  import { trayStrings } from './strings.mjs';
11
12
  import { associationStamp, ensureFileAssociations, ensureUninstallEntry, registerFileAssociations, registerUninstallEntry, unregisterFileAssociations, unregisterUninstallEntry } from './fileAssociation.mjs';
@@ -205,8 +206,9 @@ function cleanupOldLaunchers(exe) {
205
206
  */
206
207
  export function ensureLauncher({ exe = launcherExePath(), home = REMOTE_CONFIG_DIR, dir = DESKTOP_DIR, force = false } = {}) {
207
208
  const source = launcherSource(home);
208
- const png = Buffer.from(AIVIN_LOGO_PNG_BASE64, 'base64');
209
- const hash = createHash('sha256').update(source).update(png).digest('hex');
209
+ // .ico nhiều cỡ (16…256): một ảnh 64×64 duy nhất bị Windows co giãn → nhoè trên taskbar / Start (09/11/2026).
210
+ const icoBytes = Buffer.from(AIVIN_ICO_BASE64, 'base64');
211
+ const hash = createHash('sha256').update(source).update(icoBytes).digest('hex');
210
212
  fs.mkdirSync(dir, { recursive: true });
211
213
  const hashFile = path.join(dir, path.basename(SOURCE_HASH_FILE));
212
214
  let current = null;
@@ -218,7 +220,7 @@ export function ensureLauncher({ exe = launcherExePath(), home = REMOTE_CONFIG_D
218
220
  const src = path.join(dir, 'tray.cs');
219
221
  const ico = path.join(dir, 'aivin.ico');
220
222
  fs.writeFileSync(src, source);
221
- fs.writeFileSync(ico, pngToIco(png));
223
+ fs.writeFileSync(ico, icoBytes);
222
224
  const out = `${exe}.new`;
223
225
  fs.rmSync(out, { force: true });
224
226
  runSetup({ Src: src, Ico: ico, Out: out }, dir);