@aivin-labs/cli 1.0.11 → 1.0.21
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/README.md +325 -288
- package/bin/cli.mjs +163 -17
- package/bin/lib/localPreview.mjs +437 -0
- package/bin/lib/openFile.mjs +235 -0
- package/bin/lib/previewCache.mjs +71 -0
- package/bin/lib/remote/atomicFile.mjs +32 -0
- package/bin/lib/remote/autoStart.mjs +2 -1
- package/bin/lib/remote/autoUpdate.mjs +12 -0
- package/bin/lib/remote/browserTunnel.mjs +47 -8
- package/bin/lib/remote/callLedger.mjs +94 -14
- package/bin/lib/remote/cdpPolicy.mjs +14 -2
- package/bin/lib/remote/client.mjs +1024 -808
- package/bin/lib/remote/cloudFiles.mjs +53 -0
- package/bin/lib/remote/codingAgents.mjs +2 -2
- package/bin/lib/remote/constants.mjs +261 -234
- package/bin/lib/remote/credentials.mjs +81 -4
- package/bin/lib/remote/daemonLock.mjs +57 -17
- package/bin/lib/remote/dailyBrowser.mjs +8 -2
- package/bin/lib/remote/desktop/castInput.mjs +98 -0
- package/bin/lib/remote/desktop/desktopCast.mjs +334 -0
- package/bin/lib/remote/desktop/desktopCastCaptureSource.mjs +598 -0
- package/bin/lib/remote/desktop/desktopCastSource.mjs +798 -0
- package/bin/lib/remote/desktop/desktopWebRtc.mjs +242 -0
- package/bin/lib/remote/desktop/fileAssociation.mjs +108 -0
- package/bin/lib/remote/desktop/index.mjs +58 -7
- package/bin/lib/remote/desktop/linux.mjs +1 -1
- package/bin/lib/remote/desktop/linuxTraySource.mjs +427 -427
- package/bin/lib/remote/desktop/mac.mjs +2 -2
- package/bin/lib/remote/desktop/macTraySource.mjs +293 -292
- package/bin/lib/remote/desktop/strings.mjs +28 -2
- package/bin/lib/remote/desktop/traySource.mjs +905 -527
- package/bin/lib/remote/desktop/webrtcRuntime.mjs +137 -0
- package/bin/lib/remote/desktop/webrtcWorker.mjs +181 -0
- package/bin/lib/remote/deviceProfile.mjs +1 -0
- package/bin/lib/remote/docText.mjs +85 -0
- package/bin/lib/remote/fileIndexer.mjs +773 -399
- package/bin/lib/remote/folderRules.mjs +143 -0
- package/bin/lib/remote/gitSafe.mjs +31 -0
- package/bin/lib/remote/gui/index.mjs +170 -19
- package/bin/lib/remote/gui/officeHostSource.mjs +635 -393
- package/bin/lib/remote/gui/windowsHost.mjs +12 -3
- package/bin/lib/remote/gui/windowsHostSource.mjs +461 -87
- package/bin/lib/remote/httpTransfer.mjs +64 -3
- package/bin/lib/remote/idempotency.mjs +49 -20
- package/bin/lib/remote/localPolicy.mjs +81 -9
- package/bin/lib/remote/mascot/ask.mjs +207 -42
- package/bin/lib/remote/mascot/askBlocksSource.mjs +286 -0
- package/bin/lib/remote/mascot/askHostSource.mjs +658 -512
- package/bin/lib/remote/mascot/blocks.mjs +201 -0
- package/bin/lib/remote/mascot/bot.mjs +54 -5
- package/bin/lib/remote/mascot/chartLayout.mjs +382 -0
- package/bin/lib/remote/mascot/diagramRender.mjs +154 -0
- package/bin/lib/remote/mascot/emojiSource.mjs +273 -0
- package/bin/lib/remote/mascot/fileChips.mjs +62 -0
- package/bin/lib/remote/mascot/hostSource.mjs +331 -52
- package/bin/lib/remote/mascot/hotkey.mjs +31 -7
- package/bin/lib/remote/mascot/images.mjs +111 -0
- package/bin/lib/remote/mascot/index.mjs +663 -493
- package/bin/lib/remote/mascot/lucideIcons.mjs +32 -0
- package/bin/lib/remote/mascot/mermaidBundle.mjs +28 -0
- package/bin/lib/remote/mascot/mood.mjs +57 -0
- package/bin/lib/remote/mascot/rigSource.mjs +648 -609
- package/bin/lib/remote/mascot/safeUrl.mjs +29 -0
- package/bin/lib/remote/mascot/strings.mjs +456 -375
- package/bin/lib/remote/mascot/ui.mjs +27 -7
- package/bin/lib/remote/multiUser.mjs +7 -2
- package/bin/lib/remote/notify.mjs +4 -1
- package/bin/lib/remote/office/index.mjs +12 -6
- package/bin/lib/remote/osSearch.mjs +107 -0
- package/bin/lib/remote/processes.mjs +5 -2
- package/bin/lib/remote/sandbox.mjs +26 -0
- package/bin/lib/remote/secretPaths.mjs +21 -0
- package/bin/lib/remote/sqliteDb.mjs +51 -0
- package/bin/lib/remote/tokenRotation.mjs +28 -0
- package/bin/lib/remote/toolHandlers.mjs +852 -741
- package/bin/lib/remote/watchBatcher.mjs +56 -0
- package/bin/lib/remoteControl.mjs +26 -26
- package/bin/lib/selfUpdate.mjs +12 -2
- package/bin/lib/update/binarySwap.mjs +15 -1
- package/bin/lib/update/state.mjs +10 -0
- package/docs/AIVIN_MASCOT.md +14 -0
- package/docs/CLI.md +1545 -1484
- package/docs/media/aivin-ecosystem.jpg +0 -0
- package/docs/media/aivin-mascot-live.gif +0 -0
- package/docs/media/aivin-mascot-live.png +0 -0
- package/docs/media/aivin-remote-architecture.svg +102 -0
- package/package.json +75 -71
- package/docs/media/aivin-mascot-states.mp4 +0 -0
|
@@ -0,0 +1,798 @@
|
|
|
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 và phím tắt hệ thống (hotkeys) qua stdin và thực thi qua Win32 API.
|
|
11
|
+
* - Hỗ trợ thay đổi độ phân giải, chất lượng nén JPEG, giới hạn FPS và chọn màn hình theo thời gian thực.
|
|
12
|
+
* - Luồng video (02/11/2026): có `pipe` → mở named pipe gửi khung thô I420 ([rộng, cao, độ dài] int32 LE + dữ liệu) cho Node đưa vào
|
|
13
|
+
* RTCVideoSource (WebRTC tự mã hoá VP8, chỉ gửi phần đổi). JPEG chỉ nén khi còn người xem cần (`jpeg` qua config); không thì gửi dòng
|
|
14
|
+
* `meta` (kích thước, màn hình) để FE quy đổi toạ độ.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { DESKTOP_CAST_CAPTURE_CSHARP } from './desktopCastCaptureSource.mjs';
|
|
18
|
+
|
|
19
|
+
export const DESKTOP_CAST_CSHARP = String.raw`
|
|
20
|
+
using System;
|
|
21
|
+
using System.Collections.Generic;
|
|
22
|
+
using System.Drawing;
|
|
23
|
+
using System.Drawing.Imaging;
|
|
24
|
+
using System.IO;
|
|
25
|
+
using System.IO.Pipes;
|
|
26
|
+
using System.Runtime.InteropServices;
|
|
27
|
+
using System.Text;
|
|
28
|
+
using System.Threading;
|
|
29
|
+
using System.Web.Script.Serialization;
|
|
30
|
+
using System.Windows.Forms;
|
|
31
|
+
|
|
32
|
+
namespace AivinDesktopCast {
|
|
33
|
+
static class Native {
|
|
34
|
+
[DllImport("user32.dll")] public static extern bool SetProcessDPIAware();
|
|
35
|
+
[DllImport("user32.dll")] public static extern bool SetCursorPos(int X, int Y);
|
|
36
|
+
[DllImport("user32.dll")] public static extern void mouse_event(uint dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
|
|
37
|
+
[DllImport("user32.dll")] public static extern void keybd_event(byte bVk, ushort bScan, uint dwFlags, UIntPtr dwExtraInfo);
|
|
38
|
+
[DllImport("user32.dll")] public static extern short VkKeyScan(char ch);
|
|
39
|
+
|
|
40
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
41
|
+
public struct POINT { public int x; public int y; }
|
|
42
|
+
|
|
43
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
44
|
+
public struct CURSORINFO {
|
|
45
|
+
public int cbSize;
|
|
46
|
+
public int flags;
|
|
47
|
+
public IntPtr hCursor;
|
|
48
|
+
public POINT ptScreenPos;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
[DllImport("user32.dll")] public static extern bool GetCursorInfo(out CURSORINFO pci);
|
|
52
|
+
[DllImport("user32.dll")] public static extern bool DrawIcon(IntPtr hDC, int X, int Y, IntPtr hIcon);
|
|
53
|
+
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int memcmp(IntPtr a, IntPtr b, UIntPtr count);
|
|
54
|
+
[DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")] public static extern void CopyMemory(IntPtr dst, IntPtr src, uint count);
|
|
55
|
+
[DllImport("winmm.dll")] public static extern uint timeBeginPeriod(uint ms);
|
|
56
|
+
[DllImport("kernel32.dll")] public static extern bool GetSystemTimes(out long idle, out long kernel, out long user);
|
|
57
|
+
[DllImport("user32.dll")] public static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO info);
|
|
58
|
+
[DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hWnd);
|
|
59
|
+
[DllImport("user32.dll")] public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
|
|
60
|
+
[DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr obj);
|
|
61
|
+
[DllImport("gdi32.dll")] public static extern int GetObject(IntPtr obj, int size, out BITMAP bm);
|
|
62
|
+
[DllImport("gdi32.dll")] public static extern int GetDIBits(IntPtr hdc, IntPtr hbm, uint start, uint lines, [Out] int[] bits, ref BITMAPINFOHEADER info, uint usage);
|
|
63
|
+
|
|
64
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
65
|
+
public struct ICONINFO { public bool fIcon; public int xHotspot; public int yHotspot; public IntPtr hbmMask; public IntPtr hbmColor; }
|
|
66
|
+
|
|
67
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
68
|
+
public struct BITMAP { public int bmType; public int bmWidth; public int bmHeight; public int bmWidthBytes; public ushort bmPlanes; public ushort bmBitsPixel; public IntPtr bmBits; }
|
|
69
|
+
|
|
70
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
71
|
+
public struct BITMAPINFOHEADER {
|
|
72
|
+
public int biSize; public int biWidth; public int biHeight; public ushort biPlanes; public ushort biBitCount;
|
|
73
|
+
public int biCompression; public int biSizeImage; public int biXPelsPerMeter; public int biYPelsPerMeter; public int biClrUsed; public int biClrImportant;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public const int CURSOR_SHOWING = 0x00000001;
|
|
77
|
+
|
|
78
|
+
public const uint MOUSEEVENTF_MOVE = 0x0001;
|
|
79
|
+
public const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
|
|
80
|
+
public const uint MOUSEEVENTF_LEFTUP = 0x0004;
|
|
81
|
+
public const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
|
|
82
|
+
public const uint MOUSEEVENTF_RIGHTUP = 0x0010;
|
|
83
|
+
public const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
|
|
84
|
+
public const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
|
|
85
|
+
public const uint MOUSEEVENTF_WHEEL = 0x0800;
|
|
86
|
+
public const uint MOUSEEVENTF_HWHEEL = 0x01000;
|
|
87
|
+
|
|
88
|
+
public const uint KEYEVENTF_KEYUP = 0x0002;
|
|
89
|
+
public const uint KEYEVENTF_UNICODE = 0x0004;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static class Out {
|
|
93
|
+
static readonly object gate = new object();
|
|
94
|
+
static readonly JavaScriptSerializer json = new JavaScriptSerializer { MaxJsonLength = int.MaxValue };
|
|
95
|
+
|
|
96
|
+
public static void Send(Dictionary<string, object> msg) {
|
|
97
|
+
string line = json.Serialize(msg);
|
|
98
|
+
lock (gate) { Console.Out.WriteLine(line); Console.Out.Flush(); }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public static Dictionary<string, object> Parse(string text) {
|
|
102
|
+
if (string.IsNullOrEmpty(text)) return new Dictionary<string, object>();
|
|
103
|
+
return json.Deserialize<Dictionary<string, object>>(text);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public static Dictionary<string, object> Msg(string t) {
|
|
107
|
+
var d = new Dictionary<string, object>();
|
|
108
|
+
d["t"] = t;
|
|
109
|
+
return d;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Named pipe một chiều gửi khung thô cho tiến trình Node (một khách). Khách rớt → chờ khách mới. */
|
|
114
|
+
static class RawPipe {
|
|
115
|
+
static readonly object gate = new object();
|
|
116
|
+
static NamedPipeServerStream pipe;
|
|
117
|
+
public static volatile bool Connected;
|
|
118
|
+
|
|
119
|
+
public static void Start(string name) {
|
|
120
|
+
var t = new Thread(() => {
|
|
121
|
+
while (CastHost.Running) {
|
|
122
|
+
try {
|
|
123
|
+
var p = new NamedPipeServerStream(name, PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.None, 0, 4 * 1024 * 1024);
|
|
124
|
+
p.WaitForConnection();
|
|
125
|
+
lock (gate) { pipe = p; Connected = true; }
|
|
126
|
+
while (CastHost.Running && p.IsConnected) Thread.Sleep(200);
|
|
127
|
+
} catch {
|
|
128
|
+
Thread.Sleep(300);
|
|
129
|
+
} finally {
|
|
130
|
+
lock (gate) { Connected = false; if (pipe != null) { try { pipe.Dispose(); } catch { } pipe = null; } }
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
t.IsBackground = true;
|
|
135
|
+
t.Start();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
public static void Write(int width, int height, byte[] data) {
|
|
139
|
+
lock (gate) {
|
|
140
|
+
if (pipe == null || !Connected) return;
|
|
141
|
+
try {
|
|
142
|
+
var header = new byte[12];
|
|
143
|
+
BitConverter.GetBytes(width).CopyTo(header, 0);
|
|
144
|
+
BitConverter.GetBytes(height).CopyTo(header, 4);
|
|
145
|
+
BitConverter.GetBytes(data.Length).CopyTo(header, 8);
|
|
146
|
+
pipe.Write(header, 0, 12);
|
|
147
|
+
pipe.Write(data, 0, data.Length);
|
|
148
|
+
pipe.Flush();
|
|
149
|
+
} catch {
|
|
150
|
+
Connected = false;
|
|
151
|
+
try { pipe.Dispose(); } catch { }
|
|
152
|
+
pipe = null;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public static class CastScreen {
|
|
159
|
+
/** BGRX 32 bit → I420 (Y đủ, U/V lấy trung bình khối 2x2); kích thước cắt về số chẵn. */
|
|
160
|
+
public static byte[] ToI420(Bitmap bmp, out int w, out int h) {
|
|
161
|
+
w = bmp.Width & ~1;
|
|
162
|
+
h = bmp.Height & ~1;
|
|
163
|
+
var data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
|
|
164
|
+
try {
|
|
165
|
+
int stride = Math.Abs(data.Stride);
|
|
166
|
+
var raw = new byte[stride * h];
|
|
167
|
+
Marshal.Copy(data.Scan0, raw, 0, raw.Length);
|
|
168
|
+
int ySize = w * h;
|
|
169
|
+
int cw = w / 2;
|
|
170
|
+
var outBuf = new byte[ySize + ySize / 2];
|
|
171
|
+
int uBase = ySize;
|
|
172
|
+
int vBase = ySize + ySize / 4;
|
|
173
|
+
for (int y = 0; y < h; y++) {
|
|
174
|
+
int row = y * stride;
|
|
175
|
+
int yRow = y * w;
|
|
176
|
+
for (int x = 0; x < w; x++) {
|
|
177
|
+
int i = row + x * 4;
|
|
178
|
+
int b = raw[i], g = raw[i + 1], r = raw[i + 2];
|
|
179
|
+
outBuf[yRow + x] = (byte)(((66 * r + 129 * g + 25 * b + 128) >> 8) + 16);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
for (int y = 0; y < h; y += 2) {
|
|
183
|
+
int row0 = y * stride;
|
|
184
|
+
int row1 = row0 + stride;
|
|
185
|
+
int cRow = (y / 2) * cw;
|
|
186
|
+
for (int x = 0; x < w; x += 2) {
|
|
187
|
+
int i0 = row0 + x * 4, i1 = i0 + 4, i2 = row1 + x * 4, i3 = i2 + 4;
|
|
188
|
+
int b = (raw[i0] + raw[i1] + raw[i2] + raw[i3]) >> 2;
|
|
189
|
+
int g = (raw[i0 + 1] + raw[i1 + 1] + raw[i2 + 1] + raw[i3 + 1]) >> 2;
|
|
190
|
+
int r = (raw[i0 + 2] + raw[i1 + 2] + raw[i2 + 2] + raw[i3 + 2]) >> 2;
|
|
191
|
+
int ci = cRow + x / 2;
|
|
192
|
+
outBuf[uBase + ci] = (byte)(((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128);
|
|
193
|
+
outBuf[vBase + ci] = (byte)(((112 * r - 94 * g - 18 * b + 128) >> 8) + 128);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return outBuf;
|
|
197
|
+
} finally {
|
|
198
|
+
bmp.UnlockBits(data);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
public static Rectangle GetBounds(int monitor) {
|
|
203
|
+
if (monitor < 0 || monitor >= Screen.AllScreens.Length) {
|
|
204
|
+
return SystemInformation.VirtualScreen;
|
|
205
|
+
}
|
|
206
|
+
return Screen.AllScreens[monitor].Bounds;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
public static List<Dictionary<string, object>> GetScreens() {
|
|
210
|
+
var list = new List<Dictionary<string, object>>();
|
|
211
|
+
var all = Screen.AllScreens;
|
|
212
|
+
for (int i = 0; i < all.Length; i++) {
|
|
213
|
+
var s = all[i];
|
|
214
|
+
var d = new Dictionary<string, object>();
|
|
215
|
+
d["index"] = i;
|
|
216
|
+
d["name"] = "Màn hình " + (i + 1) + (s.Primary ? " (Chính)" : "");
|
|
217
|
+
d["primary"] = s.Primary;
|
|
218
|
+
d["width"] = s.Bounds.Width;
|
|
219
|
+
d["height"] = s.Bounds.Height;
|
|
220
|
+
d["x"] = s.Bounds.X;
|
|
221
|
+
d["y"] = s.Bounds.Y;
|
|
222
|
+
list.Add(d);
|
|
223
|
+
}
|
|
224
|
+
return list;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Chụp vùng màn hình vào ảnh có sẵn (không kèm con trỏ). */
|
|
228
|
+
public static void CopyScreen(Bitmap dst, Rectangle r) {
|
|
229
|
+
using (var g = Graphics.FromImage(dst)) {
|
|
230
|
+
g.CopyFromScreen(r.Left, r.Top, 0, 0, r.Size);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Bản sao của khung có lồng con trỏ thật của Windows (đường JPEG / người xem chưa tự vẽ con trỏ). */
|
|
235
|
+
public static Bitmap WithCursor(Bitmap src, Rectangle r) {
|
|
236
|
+
var copy = new Bitmap(src.Width, src.Height, PixelFormat.Format32bppRgb);
|
|
237
|
+
using (var g = Graphics.FromImage(copy)) {
|
|
238
|
+
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
|
|
239
|
+
g.DrawImageUnscaled(src, 0, 0);
|
|
240
|
+
try {
|
|
241
|
+
Native.CURSORINFO pci;
|
|
242
|
+
pci.cbSize = Marshal.SizeOf(typeof(Native.CURSORINFO));
|
|
243
|
+
if (Native.GetCursorInfo(out pci) && (pci.flags & Native.CURSOR_SHOWING) != 0) {
|
|
244
|
+
var hot = CursorTracker.Hotspot(pci.hCursor);
|
|
245
|
+
int cursorX = pci.ptScreenPos.x - hot.X - r.Left;
|
|
246
|
+
int cursorY = pci.ptScreenPos.y - hot.Y - r.Top;
|
|
247
|
+
if (cursorX >= -32 && cursorX < r.Width && cursorY >= -32 && cursorY < r.Height) {
|
|
248
|
+
IntPtr hdc = g.GetHdc();
|
|
249
|
+
try {
|
|
250
|
+
Native.DrawIcon(hdc, cursorX, cursorY, pci.hCursor);
|
|
251
|
+
} finally {
|
|
252
|
+
g.ReleaseHdc(hdc);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
} catch {}
|
|
257
|
+
}
|
|
258
|
+
return copy;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
public static Bitmap Scale(Bitmap src, int maxWidth) {
|
|
262
|
+
if (src.Width <= maxWidth) return src;
|
|
263
|
+
int h = (int)Math.Round(src.Height * (double)maxWidth / src.Width);
|
|
264
|
+
var dst = new Bitmap(maxWidth, h, PixelFormat.Format32bppRgb);
|
|
265
|
+
using (var g = Graphics.FromImage(dst)) {
|
|
266
|
+
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
|
|
267
|
+
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
|
|
268
|
+
g.DrawImage(src, 0, 0, maxWidth, h);
|
|
269
|
+
}
|
|
270
|
+
return dst;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
public static byte[] Jpeg(Bitmap bmp, int quality) {
|
|
274
|
+
var encoders = ImageCodecInfo.GetImageEncoders();
|
|
275
|
+
ImageCodecInfo codec = null;
|
|
276
|
+
for (int i = 0; i < encoders.Length; i++) {
|
|
277
|
+
if (encoders[i].MimeType == "image/jpeg") { codec = encoders[i]; break; }
|
|
278
|
+
}
|
|
279
|
+
if (codec == null) {
|
|
280
|
+
using (var ms = new MemoryStream()) { bmp.Save(ms, ImageFormat.Jpeg); return ms.ToArray(); }
|
|
281
|
+
}
|
|
282
|
+
var p = new EncoderParameters(1);
|
|
283
|
+
p.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
|
|
284
|
+
using (var ms = new MemoryStream()) {
|
|
285
|
+
bmp.Save(ms, codec, p);
|
|
286
|
+
return ms.ToArray();
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
public static byte[] Thumb(Bitmap src) {
|
|
291
|
+
var t = new byte[64 * 36];
|
|
292
|
+
using (var small = new Bitmap(64, 36, PixelFormat.Format24bppRgb)) {
|
|
293
|
+
using (var g = Graphics.FromImage(small)) {
|
|
294
|
+
g.DrawImage(src, 0, 0, 64, 36);
|
|
295
|
+
}
|
|
296
|
+
var data = small.LockBits(new Rectangle(0, 0, 64, 36), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
|
|
297
|
+
try {
|
|
298
|
+
int stride = Math.Abs(data.Stride);
|
|
299
|
+
var raw = new byte[stride * 36];
|
|
300
|
+
Marshal.Copy(data.Scan0, raw, 0, raw.Length);
|
|
301
|
+
for (int y = 0; y < 36; y++) {
|
|
302
|
+
int rowOffset = y * stride;
|
|
303
|
+
int outOffset = y * 64;
|
|
304
|
+
for (int x = 0; x < 64; x++) {
|
|
305
|
+
int idx = rowOffset + x * 3;
|
|
306
|
+
int b = raw[idx];
|
|
307
|
+
int gVal = raw[idx + 1];
|
|
308
|
+
int r = raw[idx + 2];
|
|
309
|
+
t[outOffset + x] = (byte)((r * 3 + gVal * 6 + b) / 10);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
} finally {
|
|
313
|
+
small.UnlockBits(data);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return t;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
public static double Diff(byte[] a, byte[] b) {
|
|
320
|
+
if (a == null || b == null) return 255;
|
|
321
|
+
long s = 0;
|
|
322
|
+
for (int i = 0; i < a.Length; i++) s += Math.Abs(a[i] - b[i]);
|
|
323
|
+
return (double)s / a.Length;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** Hai khung giống hệt từng điểm ảnh — ~0,3 ms ở 1080p, rẻ hơn tạo ảnh thu nhỏ. */
|
|
327
|
+
public static bool SameFrame(Bitmap a, Bitmap b) {
|
|
328
|
+
if (a == null || b == null || a.Width != b.Width || a.Height != b.Height) return false;
|
|
329
|
+
var rect = new Rectangle(0, 0, a.Width, a.Height);
|
|
330
|
+
var da = a.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
|
|
331
|
+
try {
|
|
332
|
+
var db = b.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
|
|
333
|
+
try {
|
|
334
|
+
return Native.memcmp(da.Scan0, db.Scan0, (UIntPtr)(ulong)((long)Math.Abs(da.Stride) * a.Height)) == 0;
|
|
335
|
+
} finally {
|
|
336
|
+
b.UnlockBits(db);
|
|
337
|
+
}
|
|
338
|
+
} finally {
|
|
339
|
+
a.UnlockBits(da);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
public static class CastHost {
|
|
345
|
+
const long JPEG_SMALL_CHANGE_INTERVAL_MS = 200;
|
|
346
|
+
const double JPEG_BIG_CHANGE_DIFF = 4.5;
|
|
347
|
+
const int RATIO_SCALE = 65535;
|
|
348
|
+
static volatile bool running = true;
|
|
349
|
+
public static bool Running { get { return running; } }
|
|
350
|
+
static volatile bool jpegEnabled = true;
|
|
351
|
+
static volatile bool refreshRequested = false;
|
|
352
|
+
static bool rawEnabled = false;
|
|
353
|
+
static volatile int currentMonitor = -1;
|
|
354
|
+
static Rectangle currentBounds;
|
|
355
|
+
public static Rectangle CurrentBounds { get { return currentBounds; } }
|
|
356
|
+
static int currentImageWidth = 0;
|
|
357
|
+
static int currentImageHeight = 0;
|
|
358
|
+
static volatile int targetMaxFps = 15;
|
|
359
|
+
static volatile int targetMaxWidth = 1366;
|
|
360
|
+
static volatile int targetQuality = 65;
|
|
361
|
+
|
|
362
|
+
public static void Run(string argsJson) {
|
|
363
|
+
Native.SetProcessDPIAware();
|
|
364
|
+
Native.timeBeginPeriod(1);
|
|
365
|
+
var a = Out.Parse(argsJson);
|
|
366
|
+
currentMonitor = a.ContainsKey("monitor") ? Convert.ToInt32(a["monitor"]) : -1;
|
|
367
|
+
targetMaxFps = a.ContainsKey("max_fps") ? Convert.ToInt32(a["max_fps"]) : 15;
|
|
368
|
+
if (targetMaxFps < 1) targetMaxFps = 1;
|
|
369
|
+
if (targetMaxFps > 30) targetMaxFps = 30;
|
|
370
|
+
targetMaxWidth = a.ContainsKey("max_width") ? Convert.ToInt32(a["max_width"]) : 1366;
|
|
371
|
+
targetQuality = a.ContainsKey("quality") ? Convert.ToInt32(a["quality"]) : 65;
|
|
372
|
+
string pipeName = a.ContainsKey("pipe") ? Convert.ToString(a["pipe"]) : "";
|
|
373
|
+
if (!string.IsNullOrEmpty(pipeName)) { rawEnabled = true; RawPipe.Start(pipeName); }
|
|
374
|
+
|
|
375
|
+
currentBounds = CastScreen.GetBounds(currentMonitor);
|
|
376
|
+
|
|
377
|
+
// Luồng đọc lệnh điều khiển từ xa từ stdin
|
|
378
|
+
var inputThread = new Thread(ReadInputLoop);
|
|
379
|
+
inputThread.IsBackground = true;
|
|
380
|
+
inputThread.Start();
|
|
381
|
+
|
|
382
|
+
var readyMsg = Out.Msg("ready");
|
|
383
|
+
readyMsg["monitor"] = currentMonitor;
|
|
384
|
+
readyMsg["screens"] = CastScreen.GetScreens();
|
|
385
|
+
readyMsg["bounds"] = new Dictionary<string, object> {
|
|
386
|
+
{ "x", currentBounds.X }, { "y", currentBounds.Y },
|
|
387
|
+
{ "width", currentBounds.Width }, { "height", currentBounds.Height }
|
|
388
|
+
};
|
|
389
|
+
Out.Send(readyMsg);
|
|
390
|
+
|
|
391
|
+
var clock = System.Diagnostics.Stopwatch.StartNew();
|
|
392
|
+
var source = new FrameSource();
|
|
393
|
+
var hostStats = new HostStats();
|
|
394
|
+
CursorTracker.Start();
|
|
395
|
+
byte[] lastJpegThumb = null;
|
|
396
|
+
bool changePending = false;
|
|
397
|
+
int lastCursorSeq = -1;
|
|
398
|
+
long lastSendMs = 0;
|
|
399
|
+
|
|
400
|
+
while (running) {
|
|
401
|
+
long frameStartMs = clock.ElapsedMilliseconds;
|
|
402
|
+
long targetFrameInterval = 1000 / Math.Max(1, targetMaxFps);
|
|
403
|
+
try {
|
|
404
|
+
currentBounds = CastScreen.GetBounds(currentMonitor);
|
|
405
|
+
if (source.Capture(currentBounds, (int)targetFrameInterval, frameStartMs)) {
|
|
406
|
+
changePending = true;
|
|
407
|
+
hostStats.OnChange(source.Frame, clock.ElapsedMilliseconds);
|
|
408
|
+
}
|
|
409
|
+
bool cursorInFrame = CursorTracker.DrawInFrame;
|
|
410
|
+
int cursorSeq = CursorTracker.Seq;
|
|
411
|
+
if (cursorInFrame && cursorSeq != lastCursorSeq) changePending = true;
|
|
412
|
+
long nowMs = clock.ElapsedMilliseconds;
|
|
413
|
+
bool forceKeepAlive = (nowMs - lastSendMs) >= 1000;
|
|
414
|
+
bool refresh = refreshRequested;
|
|
415
|
+
if (refresh) refreshRequested = false;
|
|
416
|
+
|
|
417
|
+
bool jpegMode = jpegEnabled || !rawEnabled;
|
|
418
|
+
bool sendChange = changePending;
|
|
419
|
+
if (sendChange && jpegMode && lastJpegThumb != null && (nowMs - lastSendMs) < JPEG_SMALL_CHANGE_INTERVAL_MS) {
|
|
420
|
+
sendChange = CastScreen.Diff(CastScreen.Thumb(source.Frame), lastJpegThumb) > JPEG_BIG_CHANGE_DIFF;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
if (sendChange || forceKeepAlive || refresh) {
|
|
424
|
+
changePending = false;
|
|
425
|
+
lastSendMs = nowMs;
|
|
426
|
+
lastCursorSeq = cursorSeq;
|
|
427
|
+
if (jpegMode) lastJpegThumb = CastScreen.Thumb(source.Frame);
|
|
428
|
+
long sendStartMs = clock.ElapsedMilliseconds;
|
|
429
|
+
SendFrame(source.Frame, cursorInFrame, jpegMode, nowMs, hostStats);
|
|
430
|
+
hostStats.AddLoad(clock.ElapsedMilliseconds - sendStartMs);
|
|
431
|
+
}
|
|
432
|
+
} catch (Exception ex) {
|
|
433
|
+
var warnMsg = Out.Msg("warn");
|
|
434
|
+
warnMsg["error"] = ex.Message;
|
|
435
|
+
Out.Send(warnMsg);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
long sleepMs = targetFrameInterval - (clock.ElapsedMilliseconds - frameStartMs);
|
|
439
|
+
if (sleepMs > 0) Thread.Sleep((int)sleepMs);
|
|
440
|
+
else if (!source.UsingDxgi) Thread.Sleep(1);
|
|
441
|
+
}
|
|
442
|
+
source.Dispose();
|
|
443
|
+
|
|
444
|
+
var stoppedMsg = Out.Msg("stopped");
|
|
445
|
+
Out.Send(stoppedMsg);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
static void SendFrame(Bitmap frame, bool cursorInFrame, bool jpegMode, long nowMs, HostStats hostStats) {
|
|
449
|
+
Bitmap withCursor = null;
|
|
450
|
+
Bitmap scaled = null;
|
|
451
|
+
try {
|
|
452
|
+
Bitmap encodeBmp = frame;
|
|
453
|
+
if (cursorInFrame) {
|
|
454
|
+
withCursor = CastScreen.WithCursor(frame, currentBounds);
|
|
455
|
+
encodeBmp = withCursor;
|
|
456
|
+
}
|
|
457
|
+
if (encodeBmp.Width > targetMaxWidth) {
|
|
458
|
+
scaled = CastScreen.Scale(encodeBmp, targetMaxWidth);
|
|
459
|
+
encodeBmp = scaled;
|
|
460
|
+
}
|
|
461
|
+
currentImageWidth = encodeBmp.Width;
|
|
462
|
+
currentImageHeight = encodeBmp.Height;
|
|
463
|
+
if (rawEnabled && RawPipe.Connected) {
|
|
464
|
+
int rw, rh;
|
|
465
|
+
byte[] i420 = CastScreen.ToI420(encodeBmp, out rw, out rh);
|
|
466
|
+
RawPipe.Write(rw, rh, i420);
|
|
467
|
+
}
|
|
468
|
+
var frameMsg = Out.Msg(jpegMode ? "frame" : "meta");
|
|
469
|
+
if (jpegMode) frameMsg["image"] = Convert.ToBase64String(CastScreen.Jpeg(encodeBmp, targetQuality));
|
|
470
|
+
frameMsg["width"] = currentImageWidth;
|
|
471
|
+
frameMsg["height"] = currentImageHeight;
|
|
472
|
+
frameMsg["screenWidth"] = currentBounds.Width;
|
|
473
|
+
frameMsg["screenHeight"] = currentBounds.Height;
|
|
474
|
+
frameMsg["screenX"] = currentBounds.X;
|
|
475
|
+
frameMsg["screenY"] = currentBounds.Y;
|
|
476
|
+
frameMsg["monitor"] = currentMonitor;
|
|
477
|
+
frameMsg["screens"] = CastScreen.GetScreens();
|
|
478
|
+
frameMsg["ts"] = nowMs;
|
|
479
|
+
frameMsg["stats"] = hostStats.Snapshot(nowMs);
|
|
480
|
+
Out.Send(frameMsg);
|
|
481
|
+
} finally {
|
|
482
|
+
if (scaled != null) scaled.Dispose();
|
|
483
|
+
if (withCursor != null) withCursor.Dispose();
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
static void ReadInputLoop() {
|
|
488
|
+
try {
|
|
489
|
+
string line;
|
|
490
|
+
while (running && (line = Console.In.ReadLine()) != null) {
|
|
491
|
+
line = line.Trim();
|
|
492
|
+
if (string.IsNullOrEmpty(line)) continue;
|
|
493
|
+
if (line == "stop") {
|
|
494
|
+
running = false;
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
var cmd = Out.Parse(line);
|
|
498
|
+
string op = cmd.ContainsKey("op") ? Convert.ToString(cmd["op"]) : "";
|
|
499
|
+
if (op == "stop") {
|
|
500
|
+
running = false;
|
|
501
|
+
break;
|
|
502
|
+
} else if (op == "input") {
|
|
503
|
+
HandleInput(cmd);
|
|
504
|
+
} else if (op == "config") {
|
|
505
|
+
HandleConfig(cmd);
|
|
506
|
+
} else if (op == "refresh") {
|
|
507
|
+
refreshRequested = true;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
} catch {
|
|
511
|
+
running = false;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
static void HandleConfig(Dictionary<string, object> cmd) {
|
|
516
|
+
try {
|
|
517
|
+
if (cmd.ContainsKey("max_fps")) {
|
|
518
|
+
int fps = Convert.ToInt32(cmd["max_fps"]);
|
|
519
|
+
if (fps >= 1 && fps <= 30) targetMaxFps = fps;
|
|
520
|
+
}
|
|
521
|
+
if (cmd.ContainsKey("quality")) {
|
|
522
|
+
int q = Convert.ToInt32(cmd["quality"]);
|
|
523
|
+
if (q >= 10 && q <= 95) targetQuality = q;
|
|
524
|
+
}
|
|
525
|
+
if (cmd.ContainsKey("max_width")) {
|
|
526
|
+
int w = Convert.ToInt32(cmd["max_width"]);
|
|
527
|
+
if (w >= 640 && w <= 2560) {
|
|
528
|
+
// Đổ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ữ.
|
|
529
|
+
if (w != targetMaxWidth) refreshRequested = true;
|
|
530
|
+
targetMaxWidth = w;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
if (cmd.ContainsKey("jpeg") && cmd["jpeg"] is bool) {
|
|
534
|
+
jpegEnabled = (bool)cmd["jpeg"];
|
|
535
|
+
refreshRequested = true;
|
|
536
|
+
}
|
|
537
|
+
if (cmd.ContainsKey("cursor") && cmd["cursor"] is bool) {
|
|
538
|
+
CursorTracker.SetDrawInFrame((bool)cmd["cursor"]);
|
|
539
|
+
refreshRequested = true;
|
|
540
|
+
}
|
|
541
|
+
if (cmd.ContainsKey("monitor")) {
|
|
542
|
+
int m = Convert.ToInt32(cmd["monitor"]);
|
|
543
|
+
if (m >= -1 && m < Screen.AllScreens.Length) {
|
|
544
|
+
currentMonitor = m;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
var ack = Out.Msg("config_ack");
|
|
548
|
+
ack["max_fps"] = targetMaxFps;
|
|
549
|
+
ack["quality"] = targetQuality;
|
|
550
|
+
ack["max_width"] = targetMaxWidth;
|
|
551
|
+
ack["monitor"] = currentMonitor;
|
|
552
|
+
Out.Send(ack);
|
|
553
|
+
} catch {
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
static byte KeyNameToVk(string name) {
|
|
558
|
+
if (string.IsNullOrEmpty(name)) return 0;
|
|
559
|
+
switch (name.ToLower().Trim()) {
|
|
560
|
+
case "win": case "windows": case "lwin": case "meta": case "cmd": return 0x5B;
|
|
561
|
+
case "rwin": return 0x5C;
|
|
562
|
+
case "ctrl": case "control": case "lctrl": return 0x11;
|
|
563
|
+
case "rctrl": return 0xA3;
|
|
564
|
+
case "alt": case "menu": case "lalt": return 0x12;
|
|
565
|
+
case "ralt": return 0xA5;
|
|
566
|
+
case "shift": case "lshift": return 0x10;
|
|
567
|
+
case "rshift": return 0xA1;
|
|
568
|
+
case "tab": return 0x09;
|
|
569
|
+
case "escape": case "esc": return 0x1B;
|
|
570
|
+
case "enter": case "return": return 0x0D;
|
|
571
|
+
case "backspace": return 0x08;
|
|
572
|
+
case "space": return 0x20;
|
|
573
|
+
case "delete": case "del": return 0x2E;
|
|
574
|
+
case "insert": case "ins": return 0x2D;
|
|
575
|
+
case "home": return 0x24;
|
|
576
|
+
case "end": return 0x23;
|
|
577
|
+
case "pageup": return 0x21;
|
|
578
|
+
case "pagedown": return 0x22;
|
|
579
|
+
case "up": case "arrowup": return 0x26;
|
|
580
|
+
case "down": case "arrowdown": return 0x28;
|
|
581
|
+
case "left": case "arrowleft": return 0x25;
|
|
582
|
+
case "right": case "arrowright": return 0x27;
|
|
583
|
+
case "f1": return 0x70;
|
|
584
|
+
case "f2": return 0x71;
|
|
585
|
+
case "f3": return 0x72;
|
|
586
|
+
case "f4": return 0x73;
|
|
587
|
+
case "f5": return 0x74;
|
|
588
|
+
case "f6": return 0x75;
|
|
589
|
+
case "f7": return 0x76;
|
|
590
|
+
case "f8": return 0x77;
|
|
591
|
+
case "f9": return 0x78;
|
|
592
|
+
case "f10": return 0x79;
|
|
593
|
+
case "f11": return 0x7A;
|
|
594
|
+
case "f12": return 0x7B;
|
|
595
|
+
case "printscreen": case "prtsc": return 0x2C;
|
|
596
|
+
case "capslock": return 0x14;
|
|
597
|
+
case "numlock": return 0x90;
|
|
598
|
+
case "scrolllock": return 0x91;
|
|
599
|
+
case "contextmenu": case "apps": return 0x5D;
|
|
600
|
+
default:
|
|
601
|
+
if (name.Length == 1) {
|
|
602
|
+
char ch = char.ToUpperInvariant(name[0]);
|
|
603
|
+
if (ch >= 'A' && ch <= 'Z') return (byte)ch;
|
|
604
|
+
if (ch >= '0' && ch <= '9') return (byte)ch;
|
|
605
|
+
short vks = Native.VkKeyScan(name[0]);
|
|
606
|
+
if (vks != -1) return (byte)(vks & 0xFF);
|
|
607
|
+
}
|
|
608
|
+
return 0;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
static List<byte> ExtractModifiers(Dictionary<string, object> cmd) {
|
|
613
|
+
var list = new List<byte>();
|
|
614
|
+
if (!cmd.ContainsKey("modifiers") || cmd["modifiers"] == null) return list;
|
|
615
|
+
System.Collections.ArrayList raw = cmd["modifiers"] as System.Collections.ArrayList;
|
|
616
|
+
if (raw != null) {
|
|
617
|
+
for (int i = 0; i < raw.Count; i++) {
|
|
618
|
+
byte vk = KeyNameToVk(Convert.ToString(raw[i]));
|
|
619
|
+
if (vk != 0 && !list.Contains(vk)) list.Add(vk);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
return list;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
static void PressModifiers(List<byte> mods) {
|
|
626
|
+
for (int i = 0; i < mods.Count; i++) {
|
|
627
|
+
Native.keybd_event(mods[i], 0, 0, UIntPtr.Zero);
|
|
628
|
+
}
|
|
629
|
+
if (mods.Count > 0) Thread.Sleep(8);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
static void ReleaseModifiers(List<byte> mods) {
|
|
633
|
+
for (int i = mods.Count - 1; i >= 0; i--) {
|
|
634
|
+
Native.keybd_event(mods[i], 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
static void HandleInput(Dictionary<string, object> cmd) {
|
|
639
|
+
try {
|
|
640
|
+
string action = cmd.ContainsKey("action") ? Convert.ToString(cmd["action"]) : "";
|
|
641
|
+
double rx = cmd.ContainsKey("x") ? Convert.ToDouble(cmd["x"]) : 0;
|
|
642
|
+
double ry = cmd.ContainsKey("y") ? Convert.ToDouble(cmd["y"]) : 0;
|
|
643
|
+
var mods = ExtractModifiers(cmd);
|
|
644
|
+
|
|
645
|
+
int targetX = currentBounds.X;
|
|
646
|
+
int targetY = currentBounds.Y;
|
|
647
|
+
if (cmd.ContainsKey("rx") && cmd.ContainsKey("ry")) {
|
|
648
|
+
// 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.
|
|
649
|
+
targetX += (int)Math.Round(Convert.ToDouble(cmd["rx"]) / RATIO_SCALE * (currentBounds.Width - 1));
|
|
650
|
+
targetY += (int)Math.Round(Convert.ToDouble(cmd["ry"]) / RATIO_SCALE * (currentBounds.Height - 1));
|
|
651
|
+
} else if (currentImageWidth > 0 && currentImageHeight > 0) {
|
|
652
|
+
targetX += (int)Math.Round((rx / currentImageWidth) * currentBounds.Width);
|
|
653
|
+
targetY += (int)Math.Round((ry / currentImageHeight) * currentBounds.Height);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
if (action == "move") {
|
|
657
|
+
Native.SetCursorPos(targetX, targetY);
|
|
658
|
+
} else if (action == "click") {
|
|
659
|
+
int button = cmd.ContainsKey("button") ? Convert.ToInt32(cmd["button"]) : 0;
|
|
660
|
+
Native.SetCursorPos(targetX, targetY);
|
|
661
|
+
Thread.Sleep(10);
|
|
662
|
+
PressModifiers(mods);
|
|
663
|
+
try {
|
|
664
|
+
if (button == 2) {
|
|
665
|
+
Native.mouse_event(Native.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
|
|
666
|
+
Thread.Sleep(15);
|
|
667
|
+
Native.mouse_event(Native.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
|
|
668
|
+
} else if (button == 1) {
|
|
669
|
+
Native.mouse_event(Native.MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
|
|
670
|
+
Thread.Sleep(15);
|
|
671
|
+
Native.mouse_event(Native.MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);
|
|
672
|
+
} else {
|
|
673
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
|
|
674
|
+
Thread.Sleep(15);
|
|
675
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
|
|
676
|
+
}
|
|
677
|
+
} finally {
|
|
678
|
+
ReleaseModifiers(mods);
|
|
679
|
+
}
|
|
680
|
+
} else if (action == "dblclick") {
|
|
681
|
+
Native.SetCursorPos(targetX, targetY);
|
|
682
|
+
Thread.Sleep(10);
|
|
683
|
+
PressModifiers(mods);
|
|
684
|
+
try {
|
|
685
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
|
|
686
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
|
|
687
|
+
Thread.Sleep(40);
|
|
688
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
|
|
689
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
|
|
690
|
+
} finally {
|
|
691
|
+
ReleaseModifiers(mods);
|
|
692
|
+
}
|
|
693
|
+
} else if (action == "rightclick") {
|
|
694
|
+
Native.SetCursorPos(targetX, targetY);
|
|
695
|
+
Thread.Sleep(10);
|
|
696
|
+
PressModifiers(mods);
|
|
697
|
+
try {
|
|
698
|
+
Native.mouse_event(Native.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
|
|
699
|
+
Thread.Sleep(15);
|
|
700
|
+
Native.mouse_event(Native.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
|
|
701
|
+
} finally {
|
|
702
|
+
ReleaseModifiers(mods);
|
|
703
|
+
}
|
|
704
|
+
} else if (action == "mousedown") {
|
|
705
|
+
int button = cmd.ContainsKey("button") ? Convert.ToInt32(cmd["button"]) : 0;
|
|
706
|
+
Native.SetCursorPos(targetX, targetY);
|
|
707
|
+
PressModifiers(mods);
|
|
708
|
+
if (button == 2) Native.mouse_event(Native.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
|
|
709
|
+
else if (button == 1) Native.mouse_event(Native.MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
|
|
710
|
+
else Native.mouse_event(Native.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
|
|
711
|
+
} else if (action == "mouseup") {
|
|
712
|
+
int button = cmd.ContainsKey("button") ? Convert.ToInt32(cmd["button"]) : 0;
|
|
713
|
+
Native.SetCursorPos(targetX, targetY);
|
|
714
|
+
try {
|
|
715
|
+
if (button == 2) Native.mouse_event(Native.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
|
|
716
|
+
else if (button == 1) Native.mouse_event(Native.MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);
|
|
717
|
+
else Native.mouse_event(Native.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
|
|
718
|
+
} finally {
|
|
719
|
+
ReleaseModifiers(mods);
|
|
720
|
+
}
|
|
721
|
+
} else if (action == "scroll") {
|
|
722
|
+
int deltaY = cmd.ContainsKey("deltaY") ? Convert.ToInt32(cmd["deltaY"]) : 0;
|
|
723
|
+
int deltaX = cmd.ContainsKey("deltaX") ? Convert.ToInt32(cmd["deltaX"]) : 0;
|
|
724
|
+
Native.SetCursorPos(targetX, targetY);
|
|
725
|
+
PressModifiers(mods);
|
|
726
|
+
try {
|
|
727
|
+
if (deltaY != 0) Native.mouse_event(Native.MOUSEEVENTF_WHEEL, 0, 0, -deltaY, 0);
|
|
728
|
+
if (deltaX != 0) Native.mouse_event(Native.MOUSEEVENTF_HWHEEL, 0, 0, deltaX, 0);
|
|
729
|
+
} finally {
|
|
730
|
+
ReleaseModifiers(mods);
|
|
731
|
+
}
|
|
732
|
+
} else if (action == "type") {
|
|
733
|
+
string text = cmd.ContainsKey("text") ? Convert.ToString(cmd["text"]) : "";
|
|
734
|
+
if (!string.IsNullOrEmpty(text)) {
|
|
735
|
+
for (int i = 0; i < text.Length; i++) {
|
|
736
|
+
char c = text[i];
|
|
737
|
+
Native.keybd_event(0, (ushort)c, Native.KEYEVENTF_UNICODE, UIntPtr.Zero);
|
|
738
|
+
Native.keybd_event(0, (ushort)c, Native.KEYEVENTF_UNICODE | Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
} else if (action == "key") {
|
|
742
|
+
string key = cmd.ContainsKey("key") ? Convert.ToString(cmd["key"]) : "";
|
|
743
|
+
HandleSpecialKey(key);
|
|
744
|
+
} else if (action == "hotkey") {
|
|
745
|
+
System.Collections.ArrayList keys = cmd.ContainsKey("keys") ? cmd["keys"] as System.Collections.ArrayList : null;
|
|
746
|
+
if (keys != null && keys.Count > 0) {
|
|
747
|
+
var vks = new List<byte>();
|
|
748
|
+
for (int i = 0; i < keys.Count; i++) {
|
|
749
|
+
byte vk = KeyNameToVk(Convert.ToString(keys[i]));
|
|
750
|
+
if (vk != 0) vks.Add(vk);
|
|
751
|
+
}
|
|
752
|
+
for (int i = 0; i < vks.Count; i++) {
|
|
753
|
+
Native.keybd_event(vks[i], 0, 0, UIntPtr.Zero);
|
|
754
|
+
Thread.Sleep(10);
|
|
755
|
+
}
|
|
756
|
+
Thread.Sleep(40);
|
|
757
|
+
for (int i = vks.Count - 1; i >= 0; i--) {
|
|
758
|
+
Native.keybd_event(vks[i], 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
|
|
759
|
+
Thread.Sleep(10);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
} catch {
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
static void HandleSpecialKey(string key) {
|
|
768
|
+
byte vk = KeyNameToVk(key);
|
|
769
|
+
if (vk != 0) {
|
|
770
|
+
Native.keybd_event(vk, 0, 0, UIntPtr.Zero);
|
|
771
|
+
Thread.Sleep(15);
|
|
772
|
+
Native.keybd_event(vk, 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
` + DESKTOP_CAST_CAPTURE_CSHARP;
|
|
778
|
+
|
|
779
|
+
export const DESKTOP_CAST_PS1 = String.raw`
|
|
780
|
+
$ErrorActionPreference = 'Stop'
|
|
781
|
+
$dll = $env:AIVIN_CAST_DLL
|
|
782
|
+
$src = $env:AIVIN_CAST_SRC
|
|
783
|
+
$refs = @('System.Drawing', 'System.Windows.Forms', 'System.Web.Extensions', 'System.Core')
|
|
784
|
+
foreach ($r in $refs) { Add-Type -AssemblyName $r }
|
|
785
|
+
if ($dll -and (Test-Path -LiteralPath $dll)) {
|
|
786
|
+
Add-Type -Path $dll
|
|
787
|
+
} else {
|
|
788
|
+
Add-Type -TypeDefinition ([IO.File]::ReadAllText($src)) -ReferencedAssemblies $refs -OutputAssembly $dll -OutputType Library
|
|
789
|
+
Add-Type -Path $dll
|
|
790
|
+
}
|
|
791
|
+
try {
|
|
792
|
+
[AivinDesktopCast.CastHost]::Run($env:AIVIN_CAST_ARGS)
|
|
793
|
+
} catch {
|
|
794
|
+
$m = @{ t = 'error'; error = $_.Exception.Message } | ConvertTo-Json -Compress
|
|
795
|
+
[Console]::Out.WriteLine($m)
|
|
796
|
+
exit 1
|
|
797
|
+
}
|
|
798
|
+
`;
|