@aivin-labs/cli 1.0.21 → 1.0.24
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/autoUpdate.mjs +153 -141
- package/bin/lib/remote/client.mjs +2 -1
- package/bin/lib/remote/constants.mjs +2 -0
- package/bin/lib/remote/desktop/desktopCast.mjs +3 -0
- package/bin/lib/remote/desktop/desktopCastSource.mjs +90 -8
- package/bin/lib/remote/desktop/desktopWebRtc.mjs +10 -1
- package/bin/lib/remote/desktop/webrtcWorker.mjs +183 -181
- package/bin/lib/remote/deviceProfile.mjs +2 -4
- package/bin/lib/remote/mascot/ask.mjs +956 -933
- package/bin/lib/remote/mascot/fileChips.mjs +2 -2
- package/bin/lib/remote/mascot/strings.mjs +3 -0
- package/docs/CLI.md +1545 -1545
- package/package.json +75 -75
|
@@ -1,141 +1,153 @@
|
|
|
1
|
-
import {
|
|
2
|
-
UpdateStatus, compareVersions, currentCliVersion, installUpdate, installedNewerThanRunning, loadUpdateSettings, parseVersion, recordCheck, tagOf,
|
|
3
|
-
versionList,
|
|
4
|
-
} from '../selfUpdate.mjs';
|
|
5
|
-
|
|
6
|
-
const HOUR = 3_600_000;
|
|
7
|
-
const MINUTE = 60_000;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Tự cập nhật trong `aivin remote start` (29/10/2026).
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* - `
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
let
|
|
34
|
-
let
|
|
35
|
-
let
|
|
36
|
-
let
|
|
37
|
-
let
|
|
38
|
-
let
|
|
39
|
-
let
|
|
40
|
-
let
|
|
41
|
-
let
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
} else if (res.status === UpdateStatus.
|
|
84
|
-
noticeOnce(`
|
|
85
|
-
} else if (res.status === UpdateStatus.
|
|
86
|
-
noticeOnce(`
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
1
|
+
import {
|
|
2
|
+
UpdateStatus, compareVersions, currentCliVersion, installUpdate, installedNewerThanRunning, loadUpdateSettings, parseVersion, recordCheck, tagOf,
|
|
3
|
+
versionList,
|
|
4
|
+
} from '../selfUpdate.mjs';
|
|
5
|
+
|
|
6
|
+
const HOUR = 3_600_000;
|
|
7
|
+
const MINUTE = 60_000;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Tự cập nhật trong `aivin remote start` (29/10/2026). BE thấy trang tải có bản mới thì thúc qua `device:update` (`requestCheck`); ngoài ra mỗi
|
|
11
|
+
* ~1 giờ (05/11/2026, trước là 6 giờ; `latest.txt` chỉ vài byte — lưới an toàn khi BE không thúc được; lần đầu sau 2–5 phút, rải ngẫu nhiên để các máy không cùng dội trang tải
|
|
12
|
+
* sau một lần phát hành) kiểm bản mới; có (và máy thuộc đợt phát hành, xem rollout.json) thì tải + kiểm chữ ký + thay tệp chạy ngay (tiến
|
|
13
|
+
* trình đang chạy không bị ảnh hưởng). Rồi:
|
|
14
|
+
* - chạy dưới trình giữ chạy thật (`supervised`, xem detectSupervisor ở autoStart.mjs): chờ máy yên `idleGraceMs` tính từ lần hoạt động cuối
|
|
15
|
+
* (`lastActivityAt` — lời gọi bắt đầu / kết thúc — hoặc lần cuối thấy còn tiến trình nền / đường hầm trình duyệt) → `onRestart` dừng gọn +
|
|
16
|
+
* thoát mã 75, trình giữ chạy mở lại bằng bản mới. 10 phút: khoảng nghỉ giữa các bước của một nhiệm vụ (lượt Planner, chờ người duyệt trên
|
|
17
|
+
* web) không bị coi là rảnh;
|
|
18
|
+
* - không có trình giữ chạy (cửa sổ chạy tay, script riêng): báo một lần "đóng, chạy lại" — không tự thoát.
|
|
19
|
+
* `setServerHints` (BE báo trong ack của HELLO — chỉ là gợi ý, không chọn được nguồn / tệp):
|
|
20
|
+
* - `min_version`: bản đang chạy cũ hơn → kiểm trong vài phút (rải ngẫu nhiên: BE khởi động lại thì mọi máy cùng nối lại), bỏ qua đợt;
|
|
21
|
+
* - `latest_version`: bản mới nhất BE biết (đặt lúc phát hành) — trang tải báo thấp hơn = trang tải bị chặn / tráo / cache cũ ("đóng băng"
|
|
22
|
+
* máy ở bản có lỗ hổng) → cảnh báo một lần + báo lại trong hồ sơ máy;
|
|
23
|
+
* - `hold_versions`: bản đang bị hoãn (nhiều máy tự lùi khỏi) → không cài, đã tải rồi thì không chuyển sang.
|
|
24
|
+
* Tắt: `aivin update --auto off`, `--no-auto-update`, hoặc AIVIN_AUTO_UPDATE=0 — khi tắt chỉ nhắc, không tải gì.
|
|
25
|
+
*/
|
|
26
|
+
export function createAutoUpdater({
|
|
27
|
+
enabled = true, supervised = false, isIdle = () => true, lastActivityAt = () => 0, onRestart = () => {}, say = () => {}, deviceId,
|
|
28
|
+
checkEveryMs = HOUR, firstCheckMs = 2 * MINUTE + Math.floor(Math.random() * 3 * MINUTE), tickMs = 30_000, idleGraceMs = 10 * MINUTE,
|
|
29
|
+
retryMs = HOUR, urgentGapMs = 10 * MINUTE, urgentJitterMs = 5 * MINUTE,
|
|
30
|
+
install = installUpdate, settings = loadUpdateSettings, installedNewer = installedNewerThanRunning, record = recordCheck, current = currentCliVersion(),
|
|
31
|
+
now = Date.now, random = Math.random,
|
|
32
|
+
} = {}) {
|
|
33
|
+
let nextCheckAt = now() + firstCheckMs;
|
|
34
|
+
let checking = null;
|
|
35
|
+
let ready = null;
|
|
36
|
+
let lastBusyAt = now();
|
|
37
|
+
let restarting = false;
|
|
38
|
+
let minVersion = null;
|
|
39
|
+
let serverLatest = null;
|
|
40
|
+
let holdVersions = [];
|
|
41
|
+
let urgent = false;
|
|
42
|
+
let lastUrgentAt = -Infinity;
|
|
43
|
+
let lastPushAt = -Infinity;
|
|
44
|
+
const notices = new Set();
|
|
45
|
+
const noticeOnce = (key, text) => {
|
|
46
|
+
if (notices.has(key)) return;
|
|
47
|
+
notices.add(key);
|
|
48
|
+
say(text);
|
|
49
|
+
};
|
|
50
|
+
const jitter = (ms) => Math.round(ms * (0.8 + random() * 0.4));
|
|
51
|
+
const restartHint = 'đóng cửa sổ này và chạy lại `aivin remote start` để dùng bản mới';
|
|
52
|
+
|
|
53
|
+
const noteSiteBehind = (siteLatest) => {
|
|
54
|
+
const behind = !!serverLatest && compareVersions(siteLatest, serverLatest) < 0;
|
|
55
|
+
if (behind) {
|
|
56
|
+
noticeOnce(`behind:${siteLatest}:${serverLatest}`, `⚠️ Trang tải báo bản mới nhất là ${siteLatest} nhưng máy chủ Aivin biết đã có ${serverLatest} — trang tải có thể đang bị chặn hoặc lỗi; máy này có thể đang kẹt ở bản cũ.`);
|
|
57
|
+
}
|
|
58
|
+
return behind;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const check = async () => {
|
|
62
|
+
if (!settings().auto) {
|
|
63
|
+
nextCheckAt = now() + checkEveryMs;
|
|
64
|
+
if (minVersion) noticeOnce(`min-off:${minVersion}`, `⚠️ Máy chủ cần aivin ${tagOf(minVersion)} trở lên (máy này ${tagOf(current)}), tự cập nhật đang tắt — chạy \`aivin update\`.`);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
// Đã có bản mới chờ khởi động lại (cài ở lần kiểm trước / cửa sổ khác) → không kiểm / cài lại (02/11/2026: sự kiện UPDATE của máy chủ
|
|
68
|
+
// gọi checkNow cài chồng, đè `.prev`). Ngoại lệ: máy chủ đòi bản tối thiểu cao hơn bản đang chờ.
|
|
69
|
+
const waiting = ready || installedNewer();
|
|
70
|
+
if (waiting && !(minVersion && compareVersions(waiting, minVersion) < 0)) {
|
|
71
|
+
ready = waiting;
|
|
72
|
+
nextCheckAt = now() + jitter(checkEveryMs);
|
|
73
|
+
return { status: UpdateStatus.INSTALLED, latest: waiting, current: tagOf(current), already: true };
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
const res = await install({ deviceId, urgent: urgent && !!minVersion && compareVersions(current, minVersion) < 0, holdVersions });
|
|
77
|
+
nextCheckAt = now() + jitter(checkEveryMs);
|
|
78
|
+
record({ siteLatest: res.latest, siteBehind: noteSiteBehind(res.latest) });
|
|
79
|
+
if (res.status === UpdateStatus.INSTALLED) {
|
|
80
|
+
ready = res.latest;
|
|
81
|
+
notices.add(`ready:${ready}`);
|
|
82
|
+
say(`⬆ Đã tải aivin ${res.latest} (đang chạy ${res.current}) — ${supervised ? 'chuyển sang bản mới khi máy rảnh' : restartHint}.`);
|
|
83
|
+
} else if (res.status === UpdateStatus.NOT_BINARY) {
|
|
84
|
+
noticeOnce(`npm:${res.latest}`, `⬆ Có aivin ${res.latest} (máy này ${res.current}) — cập nhật: npm i -g @aivin-labs/cli@latest`);
|
|
85
|
+
} else if (res.status === UpdateStatus.SKIPPED) {
|
|
86
|
+
noticeOnce(`skip:${res.latest}`, ` (bỏ qua aivin ${res.latest}: máy này đã tự lùi khỏi bản đó vì khởi động hỏng — cài lại: aivin update --to ${res.latest})`);
|
|
87
|
+
} else if (res.status === UpdateStatus.HELD) {
|
|
88
|
+
noticeOnce(`held:${res.latest}`, ` (aivin ${res.latest} đang được máy chủ tạm hoãn — giữ bản ${res.current})`);
|
|
89
|
+
}
|
|
90
|
+
} catch (e) {
|
|
91
|
+
nextCheckAt = now() + retryMs;
|
|
92
|
+
record({ error: e?.message || String(e) });
|
|
93
|
+
noticeOnce(`err:${e?.message}`, ` (chưa cập nhật được: ${e?.message || e} — thử lại sau)`);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const tick = async () => {
|
|
98
|
+
if (!enabled || restarting) return;
|
|
99
|
+
if (!isIdle()) lastBusyAt = now();
|
|
100
|
+
ready = ready || installedNewer();
|
|
101
|
+
if (ready) {
|
|
102
|
+
if (holdVersions.includes(tagOf(ready))) return;
|
|
103
|
+
if (!supervised) return noticeOnce(`ready:${ready}`, `⬆ Đã cài aivin ${ready} — ${restartHint}.`);
|
|
104
|
+
if (!isIdle() || now() - Math.max(lastBusyAt, Number(lastActivityAt()) || 0) < idleGraceMs) return;
|
|
105
|
+
restarting = true;
|
|
106
|
+
return onRestart(ready);
|
|
107
|
+
}
|
|
108
|
+
if (checking || now() < nextCheckAt) return;
|
|
109
|
+
checking = check().finally(() => { checking = null; });
|
|
110
|
+
await checking;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const timer = enabled ? setInterval(() => { void tick(); }, tickMs) : null;
|
|
114
|
+
timer?.unref?.();
|
|
115
|
+
|
|
116
|
+
/** BE yêu cầu bản tối thiểu: máy cũ hơn → kiểm trong `urgentJitterMs` (tối đa một lần / `urgentGapMs`), bỏ qua đợt phát hành. */
|
|
117
|
+
const setMinVersion = (version) => {
|
|
118
|
+
if (!enabled || !parseVersion(version) || compareVersions(current, version) >= 0) return;
|
|
119
|
+
minVersion = version;
|
|
120
|
+
urgent = true;
|
|
121
|
+
if (now() - lastUrgentAt < urgentGapMs) return;
|
|
122
|
+
lastUrgentAt = now();
|
|
123
|
+
nextCheckAt = Math.min(nextCheckAt, now() + Math.floor(random() * urgentJitterMs));
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
tick,
|
|
128
|
+
async checkNow({ urgent: isUrgent = true } = {}) {
|
|
129
|
+
urgent = isUrgent;
|
|
130
|
+
return check();
|
|
131
|
+
},
|
|
132
|
+
setMinVersion,
|
|
133
|
+
/**
|
|
134
|
+
* BE báo có bản mới (`device:update`): dời lượt kiểm kế tiếp lên ngay — đi qua `tick` như mọi lượt (không chạy song song, có bản chờ thì không
|
|
135
|
+
* cài chồng, vẫn theo đợt phát hành). Tối đa một lần / `urgentGapMs`: BE lỗi / bị chiếm không dội được trang tải. false = bỏ qua.
|
|
136
|
+
*/
|
|
137
|
+
requestCheck() {
|
|
138
|
+
if (!enabled || now() - lastPushAt < urgentGapMs) return false;
|
|
139
|
+
lastPushAt = now();
|
|
140
|
+
nextCheckAt = Math.min(nextCheckAt, now());
|
|
141
|
+
return true;
|
|
142
|
+
},
|
|
143
|
+
/** Gợi ý từ ack của `device:hello` (`hello.cli`) — xem đầu tệp. */
|
|
144
|
+
setServerHints(hints) {
|
|
145
|
+
if (!hints || typeof hints !== 'object') return;
|
|
146
|
+
holdVersions = versionList(hints.hold_versions);
|
|
147
|
+
serverLatest = parseVersion(hints.latest_version) ? tagOf(hints.latest_version) : null;
|
|
148
|
+
if (typeof hints.min_version === 'string') setMinVersion(hints.min_version);
|
|
149
|
+
},
|
|
150
|
+
close() { if (timer) clearInterval(timer); },
|
|
151
|
+
state: () => ({ ready, nextCheckAt, minVersion, serverLatest, holdVersions, checking: !!checking }),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
@@ -897,7 +897,7 @@ export async function startRemoteClient(options = {}) {
|
|
|
897
897
|
if (!updater) return;
|
|
898
898
|
if (payload?.min_version) updater.setMinVersion(payload.min_version);
|
|
899
899
|
if (payload?.latest_version) updater.setServerHints(payload);
|
|
900
|
-
|
|
900
|
+
updater.requestCheck();
|
|
901
901
|
}));
|
|
902
902
|
socket.on(DeviceEvent.DESKTOP_CAST_START, guarded(DeviceEvent.DESKTOP_CAST_START, async (payload) => {
|
|
903
903
|
const sessionId = payload?.session_id;
|
|
@@ -932,6 +932,7 @@ export async function startRemoteClient(options = {}) {
|
|
|
932
932
|
onMeta: (meta) => { desktopWebRtc.push(meta); syncCastJpeg(); },
|
|
933
933
|
onCursor: (cursor) => desktopWebRtc.pushCursor(cursor),
|
|
934
934
|
onCursorShape: (shape) => desktopWebRtc.pushCursorShape(shape),
|
|
935
|
+
onInputFocus: (focus) => desktopWebRtc.pushInputFocus?.(focus),
|
|
935
936
|
onCapture: ({ mode, reason }) => say(chalk.gray(` 🖥️ Chụp màn hình: ${mode}${reason ? ` (${reason})` : ''}`)),
|
|
936
937
|
rawPipe: true,
|
|
937
938
|
onReady: (meta) => {
|
|
@@ -140,6 +140,8 @@ export const ServerFeature = Object.freeze({
|
|
|
140
140
|
CHAT_CANCEL: 'chat_cancel',
|
|
141
141
|
/** Mở phiên chat mới cho linh vật (`device:chat_new`). */
|
|
142
142
|
CHAT_NEW: 'chat_new',
|
|
143
|
+
/** Giao thêm việc khi lượt trước chưa xong (`device:chat` + `parallel`) — chạy ở phiên riêng, kết quả tới bằng báo chủ động. */
|
|
144
|
+
CHAT_PARALLEL: 'chat_parallel',
|
|
143
145
|
});
|
|
144
146
|
|
|
145
147
|
/** Nhóm giới hạn song song trên máy — mỗi công cụ thuộc một nhóm. */
|
|
@@ -86,6 +86,7 @@ export function createDesktopCastManager({ dir = CAST_DIR, spawnImpl = spawn } =
|
|
|
86
86
|
onMeta,
|
|
87
87
|
onCursor,
|
|
88
88
|
onCursorShape,
|
|
89
|
+
onInputFocus,
|
|
89
90
|
onCapture,
|
|
90
91
|
rawPipe = false,
|
|
91
92
|
onReady,
|
|
@@ -174,6 +175,8 @@ export function createDesktopCastManager({ dir = CAST_DIR, spawnImpl = spawn } =
|
|
|
174
175
|
onCursor?.({ x: msg.x, y: msg.y, visible: msg.visible, shape: msg.shape });
|
|
175
176
|
} else if (msg.t === 'cursor_shape') {
|
|
176
177
|
onCursorShape?.({ id: msg.id, png: msg.png, hotX: msg.hotX, hotY: msg.hotY });
|
|
178
|
+
} else if (msg.t === 'input_focus') {
|
|
179
|
+
onInputFocus?.({ editable: msg.editable === true });
|
|
177
180
|
} else if (msg.t === 'capture') {
|
|
178
181
|
onCapture?.({ mode: msg.mode, reason: msg.reason });
|
|
179
182
|
} else if (msg.t === 'error') {
|
|
@@ -27,6 +27,7 @@ using System.Runtime.InteropServices;
|
|
|
27
27
|
using System.Text;
|
|
28
28
|
using System.Threading;
|
|
29
29
|
using System.Web.Script.Serialization;
|
|
30
|
+
using System.Windows.Automation;
|
|
30
31
|
using System.Windows.Forms;
|
|
31
32
|
|
|
32
33
|
namespace AivinDesktopCast {
|
|
@@ -37,6 +38,27 @@ namespace AivinDesktopCast {
|
|
|
37
38
|
[DllImport("user32.dll")] public static extern void keybd_event(byte bVk, ushort bScan, uint dwFlags, UIntPtr dwExtraInfo);
|
|
38
39
|
[DllImport("user32.dll")] public static extern short VkKeyScan(char ch);
|
|
39
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
|
+
|
|
40
62
|
[StructLayout(LayoutKind.Sequential)]
|
|
41
63
|
public struct POINT { public int x; public int y; }
|
|
42
64
|
|
|
@@ -677,6 +699,7 @@ namespace AivinDesktopCast {
|
|
|
677
699
|
} finally {
|
|
678
700
|
ReleaseModifiers(mods);
|
|
679
701
|
}
|
|
702
|
+
if (button == 0) ProbeInputFocus();
|
|
680
703
|
} else if (action == "dblclick") {
|
|
681
704
|
Native.SetCursorPos(targetX, targetY);
|
|
682
705
|
Thread.Sleep(10);
|
|
@@ -718,6 +741,7 @@ namespace AivinDesktopCast {
|
|
|
718
741
|
} finally {
|
|
719
742
|
ReleaseModifiers(mods);
|
|
720
743
|
}
|
|
744
|
+
if (button == 0) ProbeInputFocus();
|
|
721
745
|
} else if (action == "scroll") {
|
|
722
746
|
int deltaY = cmd.ContainsKey("deltaY") ? Convert.ToInt32(cmd["deltaY"]) : 0;
|
|
723
747
|
int deltaX = cmd.ContainsKey("deltaX") ? Convert.ToInt32(cmd["deltaX"]) : 0;
|
|
@@ -731,13 +755,7 @@ namespace AivinDesktopCast {
|
|
|
731
755
|
}
|
|
732
756
|
} else if (action == "type") {
|
|
733
757
|
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
|
-
}
|
|
758
|
+
if (!string.IsNullOrEmpty(text)) TypeUnicode(text);
|
|
741
759
|
} else if (action == "key") {
|
|
742
760
|
string key = cmd.ContainsKey("key") ? Convert.ToString(cmd["key"]) : "";
|
|
743
761
|
HandleSpecialKey(key);
|
|
@@ -764,6 +782,70 @@ namespace AivinDesktopCast {
|
|
|
764
782
|
}
|
|
765
783
|
}
|
|
766
784
|
|
|
785
|
+
/** 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ô. */
|
|
786
|
+
static void TypeUnicode(string text) {
|
|
787
|
+
const int chunk = 64;
|
|
788
|
+
int size = Marshal.SizeOf(typeof(Native.INPUT));
|
|
789
|
+
int start = 0;
|
|
790
|
+
while (start < text.Length) {
|
|
791
|
+
int end = Math.Min(text.Length, start + chunk);
|
|
792
|
+
// Không cắt đôi một cặp đại diện giữa hai lô.
|
|
793
|
+
if (end < text.Length && char.IsHighSurrogate(text[end - 1])) end--;
|
|
794
|
+
var inputs = new Native.INPUT[(end - start) * 2];
|
|
795
|
+
for (int i = start; i < end; i++) {
|
|
796
|
+
int k = (i - start) * 2;
|
|
797
|
+
inputs[k].type = Native.INPUT_KEYBOARD;
|
|
798
|
+
inputs[k].u.ki = new Native.KEYBDINPUT { wVk = 0, wScan = text[i], dwFlags = Native.KEYEVENTF_UNICODE };
|
|
799
|
+
inputs[k + 1].type = Native.INPUT_KEYBOARD;
|
|
800
|
+
inputs[k + 1].u.ki = new Native.KEYBDINPUT { wVk = 0, wScan = text[i], dwFlags = Native.KEYEVENTF_UNICODE | Native.KEYEVENTF_KEYUP };
|
|
801
|
+
}
|
|
802
|
+
Native.SendInput((uint)inputs.Length, inputs, size);
|
|
803
|
+
start = end;
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
static int focusProbeSeq = 0;
|
|
808
|
+
|
|
809
|
+
/**
|
|
810
|
+
* 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 —
|
|
811
|
+
* trình duyệt trên điện thoại dựa vào đó tự mở bàn phím ảo. Chạy luồng riêng, không giữ hàng thao tác.
|
|
812
|
+
*/
|
|
813
|
+
static void ProbeInputFocus() {
|
|
814
|
+
int seq = Interlocked.Increment(ref focusProbeSeq);
|
|
815
|
+
ThreadPool.QueueUserWorkItem(_ => {
|
|
816
|
+
try {
|
|
817
|
+
Thread.Sleep(150);
|
|
818
|
+
if (seq != focusProbeSeq) return;
|
|
819
|
+
var info = new Native.GUITHREADINFO();
|
|
820
|
+
info.cbSize = Marshal.SizeOf(typeof(Native.GUITHREADINFO));
|
|
821
|
+
IntPtr fg = Native.GetForegroundWindow();
|
|
822
|
+
uint tid = fg == IntPtr.Zero ? 0 : Native.GetWindowThreadProcessId(fg, IntPtr.Zero);
|
|
823
|
+
bool editable = (tid != 0 && Native.GetGUIThreadInfo(tid, ref info) && info.hwndCaret != IntPtr.Zero) || FocusedIsTextInput();
|
|
824
|
+
var msg = new Dictionary<string, object>();
|
|
825
|
+
msg["t"] = "input_focus";
|
|
826
|
+
msg["editable"] = editable;
|
|
827
|
+
Out.Send(msg);
|
|
828
|
+
} catch {
|
|
829
|
+
}
|
|
830
|
+
});
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
/** 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. */
|
|
834
|
+
static bool FocusedIsTextInput() {
|
|
835
|
+
try {
|
|
836
|
+
var fe = AutomationElement.FocusedElement;
|
|
837
|
+
if (fe == null) return false;
|
|
838
|
+
var ct = fe.Current.ControlType;
|
|
839
|
+
if (ct == ControlType.Edit) return true;
|
|
840
|
+
if (ct == ControlType.ComboBox || ct == ControlType.Document) {
|
|
841
|
+
object vp;
|
|
842
|
+
if (fe.TryGetCurrentPattern(ValuePattern.Pattern, out vp)) return !((ValuePattern)vp).Current.IsReadOnly;
|
|
843
|
+
}
|
|
844
|
+
} catch {
|
|
845
|
+
}
|
|
846
|
+
return false;
|
|
847
|
+
}
|
|
848
|
+
|
|
767
849
|
static void HandleSpecialKey(string key) {
|
|
768
850
|
byte vk = KeyNameToVk(key);
|
|
769
851
|
if (vk != 0) {
|
|
@@ -780,7 +862,7 @@ export const DESKTOP_CAST_PS1 = String.raw`
|
|
|
780
862
|
$ErrorActionPreference = 'Stop'
|
|
781
863
|
$dll = $env:AIVIN_CAST_DLL
|
|
782
864
|
$src = $env:AIVIN_CAST_SRC
|
|
783
|
-
$refs = @('System.Drawing', 'System.Windows.Forms', 'System.Web.Extensions', 'System.Core')
|
|
865
|
+
$refs = @('UIAutomationClient', 'UIAutomationTypes', 'WindowsBase', 'System.Drawing', 'System.Windows.Forms', 'System.Web.Extensions', 'System.Core')
|
|
784
866
|
foreach ($r in $refs) { Add-Type -AssemblyName $r }
|
|
785
867
|
if ($dll -and (Test-Path -LiteralPath $dll)) {
|
|
786
868
|
Add-Type -Path $dll
|
|
@@ -208,6 +208,15 @@ export function createDesktopWebRtc({ emitSignal = () => {}, emitStatus = () =>
|
|
|
208
208
|
for (const peer of peers.values()) if (peer.localCursor) sendShape(peer, shape);
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
+
/** Ô đang có tiêu điểm trên máy có phải ô nhập chữ không (sau cú nhấp của người xem) — trình duyệt điện thoại tự mở / đóng bàn phím ảo. */
|
|
212
|
+
function pushInputFocus(focus) {
|
|
213
|
+
const packet = JSON.stringify({ t: 'input_focus', editable: focus?.editable === true });
|
|
214
|
+
for (const peer of peers.values()) {
|
|
215
|
+
if (peer.inputChannel?.readyState !== 'open') continue;
|
|
216
|
+
try { peer.inputChannel.send(packet); } catch { /* kênh vừa đóng */ }
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
211
220
|
/** Mọi người xem đều có video và tự vẽ con trỏ → bộ quay thôi lồng con trỏ vào khung. */
|
|
212
221
|
function localCursor() {
|
|
213
222
|
return peers.size > 0 && [...peers.values()].every(p => p.video && p.open && p.localCursor);
|
|
@@ -238,5 +247,5 @@ export function createDesktopWebRtc({ emitSignal = () => {}, emitStatus = () =>
|
|
|
238
247
|
}
|
|
239
248
|
|
|
240
249
|
function closeAll() { for (const id of [...peers.keys()]) close(id); cursorShapes.clear(); }
|
|
241
|
-
return { start, signal, push, pushRaw, pushCursor, pushCursorShape, localCursor, needsJpeg, hasVideo, anyOpen, needsFallback, close, closeAll };
|
|
250
|
+
return { start, signal, push, pushRaw, pushCursor, pushCursorShape, pushInputFocus, localCursor, needsJpeg, hasVideo, anyOpen, needsFallback, close, closeAll };
|
|
242
251
|
}
|