@andy.ian/dsh-homing 2.1.9 → 2.1.10
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/window-agent-daemon.mjs +163 -88
- package/index.js +4 -0
- package/lib/orientation.mjs +15 -41
- package/lib/window-agent.ps1 +7 -3
- package/lib/window-remote.mjs +8 -3
- package/package.json +1 -1
- package/lib/rotate-screen.ps1 +0 -63
|
@@ -32,6 +32,21 @@ const IDLE_SECONDS = Math.max(10, Number(process.env.DSH_AGENT_IDLE_SECONDS) ||
|
|
|
32
32
|
const LOG_FILE = path.join(agentDir(), 'window-agent.log')
|
|
33
33
|
|
|
34
34
|
const writeLogLine = createLogWriter(LOG_FILE)
|
|
35
|
+
const ORIENTATION_FILE = path.join(agentDir(), 'orientation.json')
|
|
36
|
+
|
|
37
|
+
function readOrientationState() {
|
|
38
|
+
try {
|
|
39
|
+
return JSON.parse(fs.readFileSync(ORIENTATION_FILE, 'utf8'))
|
|
40
|
+
} catch {
|
|
41
|
+
return null
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function writeOrientationState(mode) {
|
|
46
|
+
try {
|
|
47
|
+
fs.writeFileSync(ORIENTATION_FILE, JSON.stringify({ mode, ts: Date.now() }) + '\n', 'utf8')
|
|
48
|
+
} catch { /* 忽略 */ }
|
|
49
|
+
}
|
|
35
50
|
|
|
36
51
|
function log(message) {
|
|
37
52
|
writeLogLine(`${new Date().toISOString()} [node-daemon] ${message}\n`)
|
|
@@ -40,70 +55,100 @@ function log(message) {
|
|
|
40
55
|
const isWindows = process.platform === 'win32'
|
|
41
56
|
|
|
42
57
|
// ---------------- PowerShell 工作进程(仅 Windows) ----------------
|
|
58
|
+
//
|
|
59
|
+
// 两套独立工作进程:
|
|
60
|
+
// - ops:窗口列表/截图/点击/键盘等,请求串行
|
|
61
|
+
// - rotate:只做转屏。独立进程,绝不被窗口操作的排队/卡死拖住
|
|
62
|
+
// 任何一个超时都会被杀掉,下次请求重建。
|
|
43
63
|
|
|
44
|
-
|
|
45
|
-
let
|
|
46
|
-
let
|
|
47
|
-
let
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
windowsHide: true,
|
|
55
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
56
|
-
// stdio 工作进程只被守护进程使用,不需要 token(避免二次鉴权)
|
|
57
|
-
env: { ...process.env, DSH_AGENT_TOKEN: '' },
|
|
58
|
-
})
|
|
59
|
-
child.stderr.on('data', (chunk) => { workerStderr = (workerStderr + String(chunk)).slice(-2000) })
|
|
60
|
-
workerReader = readline.createInterface({ input: child.stdout })
|
|
61
|
-
workerReader.on('line', (line) => {
|
|
62
|
-
const text = line.trim()
|
|
63
|
-
if (!text.startsWith('{')) return
|
|
64
|
-
let message = null
|
|
65
|
-
try { message = JSON.parse(text) } catch { return }
|
|
66
|
-
const resolve = workerPending.get(message.id)
|
|
67
|
-
if (resolve === undefined) return
|
|
68
|
-
workerPending.delete(message.id)
|
|
69
|
-
resolve(message)
|
|
70
|
-
})
|
|
71
|
-
const failPending = () => {
|
|
72
|
-
for (const [, resolve] of workerPending) resolve({ ok: false, error: '窗口工作进程已退出' })
|
|
73
|
-
workerPending.clear()
|
|
74
|
-
worker = null
|
|
64
|
+
function createWorker(label) {
|
|
65
|
+
let child = null
|
|
66
|
+
let reader = null
|
|
67
|
+
let stderr = ''
|
|
68
|
+
let seq = 0
|
|
69
|
+
const pending = new Map()
|
|
70
|
+
|
|
71
|
+
function failPending(message) {
|
|
72
|
+
for (const [, resolve] of pending) resolve({ ok: false, error: message })
|
|
73
|
+
pending.clear()
|
|
75
74
|
}
|
|
76
|
-
child.on('exit', () => { log('worker exit'); failPending() })
|
|
77
|
-
child.on('error', () => { log('worker error'); failPending() })
|
|
78
|
-
worker = child
|
|
79
|
-
return child
|
|
80
|
-
}
|
|
81
75
|
|
|
82
|
-
function
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
76
|
+
function ensure() {
|
|
77
|
+
if (!isWindows) throw new Error('窗口操作仅支持 Windows')
|
|
78
|
+
if (child !== null && child.exitCode === null) return child
|
|
79
|
+
const spawned = spawn(powershellExe(), ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', WORKER], {
|
|
80
|
+
windowsHide: true,
|
|
81
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
82
|
+
env: { ...process.env, DSH_AGENT_TOKEN: '' },
|
|
83
|
+
})
|
|
84
|
+
stderr = ''
|
|
85
|
+
spawned.stderr.on('data', (chunk) => { stderr = (stderr + String(chunk)).slice(-2000) })
|
|
86
|
+
reader = readline.createInterface({ input: spawned.stdout })
|
|
87
|
+
reader.on('line', (line) => {
|
|
88
|
+
const text = line.trim()
|
|
89
|
+
if (!text.startsWith('{')) return
|
|
90
|
+
let message = null
|
|
91
|
+
try { message = JSON.parse(text) } catch { return }
|
|
92
|
+
const resolve = pending.get(message.id)
|
|
93
|
+
if (resolve === undefined) return
|
|
94
|
+
pending.delete(message.id)
|
|
97
95
|
resolve(message)
|
|
98
96
|
})
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
97
|
+
spawned.on('exit', () => { log(label + ' worker exit'); failPending('窗口工作进程已退出') })
|
|
98
|
+
spawned.on('error', () => { log(label + ' worker error'); failPending('窗口工作进程启动失败') })
|
|
99
|
+
child = spawned
|
|
100
|
+
return spawned
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function kill() {
|
|
104
|
+
try { child?.kill() } catch { /* 忽略 */ }
|
|
105
|
+
child = null
|
|
106
|
+
failPending('窗口工作进程已重启')
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function call(request, timeoutMs = 20_000) {
|
|
110
|
+
const spawned = ensure()
|
|
111
|
+
const id = String(++seq)
|
|
112
|
+
return new Promise((resolve) => {
|
|
113
|
+
const timer = setTimeout(() => {
|
|
114
|
+
pending.delete(id)
|
|
115
|
+
log(label + ' worker timeout, restarting' + (stderr ? ': ' + stderr.replace(/\s+/gu, ' ').slice(-300) : ''))
|
|
116
|
+
try { spawned.kill() } catch { /* 忽略 */ }
|
|
117
|
+
child = null
|
|
118
|
+
resolve({ ok: false, error: 'timeout' })
|
|
119
|
+
}, timeoutMs)
|
|
120
|
+
pending.set(id, (message) => {
|
|
121
|
+
clearTimeout(timer)
|
|
122
|
+
resolve(message)
|
|
123
|
+
})
|
|
124
|
+
try {
|
|
125
|
+
spawned.stdin.write(JSON.stringify({ id, ...request }) + '\n')
|
|
126
|
+
} catch (error) {
|
|
127
|
+
clearTimeout(timer)
|
|
128
|
+
pending.delete(id)
|
|
129
|
+
resolve({ ok: false, error: String(error?.message ?? error) })
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return { call, kill }
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const opsWorker = createWorker('ops')
|
|
138
|
+
const rotateWorker = createWorker('rotate')
|
|
139
|
+
|
|
140
|
+
function workerCall(request, timeoutMs = 20_000) {
|
|
141
|
+
return opsWorker.call(request, timeoutMs)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** 转屏:独立进程;失败就杀掉重建再试一次,避免一次卡死影响用户操作 */
|
|
145
|
+
async function rotateWorkerCall(mode) {
|
|
146
|
+
const request = { cmd: 'rotate', mode }
|
|
147
|
+
const first = await rotateWorker.call(request, 15_000)
|
|
148
|
+
if (first.ok === true && first.result === 0) return first
|
|
149
|
+
log('rotate failed (' + (first.error ?? 'result=' + first.result) + '), retry with fresh worker')
|
|
150
|
+
rotateWorker.kill()
|
|
151
|
+
return rotateWorker.call(request, 15_000)
|
|
107
152
|
}
|
|
108
153
|
|
|
109
154
|
// ---------------- 转屏(跨平台分派) ----------------
|
|
@@ -136,9 +181,9 @@ async function linuxScreenSize() {
|
|
|
136
181
|
async function rotatePlatform(mode) {
|
|
137
182
|
const portrait = mode === 'portrait'
|
|
138
183
|
if (isWindows) {
|
|
139
|
-
const result = await
|
|
184
|
+
const result = await rotateWorkerCall(portrait ? 'portrait' : 'landscape')
|
|
140
185
|
return {
|
|
141
|
-
ok: result.ok === true,
|
|
186
|
+
ok: result.ok === true && result.result === 0,
|
|
142
187
|
result: result.result ?? null,
|
|
143
188
|
screen: { width: Number(result.screenWidth) || 0, height: Number(result.screenHeight) || 0 },
|
|
144
189
|
detail: `windows result=${result.result ?? '?'}`,
|
|
@@ -168,11 +213,31 @@ async function rotatePlatform(mode) {
|
|
|
168
213
|
|
|
169
214
|
let queue = Promise.resolve()
|
|
170
215
|
|
|
216
|
+
/** 转屏回横屏(空闲/DSH 退出时自动调用) */
|
|
217
|
+
async function restoreOrientation(reason) {
|
|
218
|
+
const state = readOrientationState()
|
|
219
|
+
const rotatedByUs = state?.mode === 'portrait'
|
|
220
|
+
if (!rotatedByUs) {
|
|
221
|
+
log('restore skip (' + reason + '): orientation not set by us')
|
|
222
|
+
return { ok: true, restored: false }
|
|
223
|
+
}
|
|
224
|
+
const result = await rotatePlatform('landscape')
|
|
225
|
+
if (result.ok === true) {
|
|
226
|
+
writeOrientationState('landscape')
|
|
227
|
+
log('restore orientation (' + reason + '): landscape ok')
|
|
228
|
+
} else {
|
|
229
|
+
log('restore orientation (' + reason + ') failed: ' + (result.detail ?? 'unknown'))
|
|
230
|
+
}
|
|
231
|
+
return { ok: result.ok === true, restored: result.ok === true }
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const FAST_COMMANDS = new Set(['ping', 'version', 'shutdown', 'rotate', 'restore'])
|
|
235
|
+
|
|
171
236
|
async function handle(request) {
|
|
172
237
|
const cmd = String(request.cmd ?? '')
|
|
173
238
|
if (cmd === 'ping') return { ok: true, pong: true }
|
|
174
239
|
if (cmd === 'version') {
|
|
175
|
-
return { ok: true, version: '
|
|
240
|
+
return { ok: true, version: '3', platform: process.platform, pid: process.pid, pipe: NAME }
|
|
176
241
|
}
|
|
177
242
|
if (cmd === 'shutdown') {
|
|
178
243
|
setTimeout(() => process.exit(0), 50)
|
|
@@ -180,12 +245,17 @@ async function handle(request) {
|
|
|
180
245
|
}
|
|
181
246
|
if (cmd === 'rotate') {
|
|
182
247
|
const mode = request.mode === 'landscape' ? 'landscape' : 'portrait'
|
|
183
|
-
|
|
248
|
+
const result = await rotatePlatform(mode)
|
|
249
|
+
if (result.ok === true) writeOrientationState(mode)
|
|
250
|
+
return result
|
|
251
|
+
}
|
|
252
|
+
if (cmd === 'restore') {
|
|
253
|
+
return restoreOrientation('explicit')
|
|
184
254
|
}
|
|
185
255
|
// 其余窗口操作:Windows 交给工作进程
|
|
186
256
|
if (!isWindows) return { ok: false, error: '窗口操作仅支持 Windows' }
|
|
187
257
|
const { id: _ignoredId, token: _ignoredToken, ...payload } = request
|
|
188
|
-
return workerCall(payload,
|
|
258
|
+
return workerCall(payload, 15_000)
|
|
189
259
|
}
|
|
190
260
|
|
|
191
261
|
const server = net.createServer((socket) => {
|
|
@@ -209,15 +279,22 @@ const server = net.createServer((socket) => {
|
|
|
209
279
|
continue
|
|
210
280
|
}
|
|
211
281
|
const id = request.id
|
|
212
|
-
|
|
282
|
+
touchInactivity()
|
|
283
|
+
const runRequest = async () => {
|
|
213
284
|
let result
|
|
214
285
|
try {
|
|
215
286
|
result = await handle(request)
|
|
216
287
|
} catch (error) {
|
|
217
288
|
result = { ok: false, error: String(error?.message ?? error) }
|
|
218
289
|
}
|
|
219
|
-
try { socket.write(JSON.stringify({
|
|
220
|
-
}
|
|
290
|
+
try { socket.write(JSON.stringify({ ...result, id }) + '\n') } catch { /* 已断开 */ }
|
|
291
|
+
}
|
|
292
|
+
if (FAST_COMMANDS.has(String(request.cmd ?? ''))) {
|
|
293
|
+
// 转屏/恢复/探活:不进入窗口操作串行队列
|
|
294
|
+
void runRequest()
|
|
295
|
+
} else {
|
|
296
|
+
queue = queue.then(runRequest)
|
|
297
|
+
}
|
|
221
298
|
}
|
|
222
299
|
})
|
|
223
300
|
socket.on('error', () => { /* 忽略 */ })
|
|
@@ -238,37 +315,35 @@ function start() {
|
|
|
238
315
|
})
|
|
239
316
|
}
|
|
240
317
|
|
|
241
|
-
//
|
|
242
|
-
|
|
243
|
-
let
|
|
318
|
+
// 空闲恢复:超过 IDLE_SECONDS 没有任何请求(VNC 不再使用 / DSH 已退出)时,
|
|
319
|
+
// 如果竖屏是本守护进程切的,自动恢复横屏,然后退出。
|
|
320
|
+
let inactivityTimer = null
|
|
244
321
|
|
|
245
|
-
function
|
|
246
|
-
if (
|
|
247
|
-
|
|
248
|
-
|
|
322
|
+
function touchInactivity() {
|
|
323
|
+
if (inactivityTimer !== null) clearTimeout(inactivityTimer)
|
|
324
|
+
inactivityTimer = setTimeout(async () => {
|
|
325
|
+
inactivityTimer = null
|
|
326
|
+
try {
|
|
327
|
+
await restoreOrientation('idle ' + IDLE_SECONDS + 's')
|
|
328
|
+
} catch (error) {
|
|
329
|
+
log('restore on idle failed: ' + String(error?.message ?? error))
|
|
330
|
+
}
|
|
249
331
|
log('idle exit')
|
|
250
|
-
try {
|
|
332
|
+
try { opsWorker.kill() } catch { /* 忽略 */ }
|
|
333
|
+
try { rotateWorker.kill() } catch { /* 忽略 */ }
|
|
251
334
|
try { server.close() } catch { /* 忽略 */ }
|
|
252
335
|
process.exit(0)
|
|
253
336
|
}, IDLE_SECONDS * 1000)
|
|
254
|
-
|
|
337
|
+
inactivityTimer.unref?.()
|
|
255
338
|
}
|
|
256
339
|
|
|
257
|
-
server.on('connection', (
|
|
258
|
-
clients += 1
|
|
259
|
-
if (idleTimer !== null) {
|
|
260
|
-
clearTimeout(idleTimer)
|
|
261
|
-
idleTimer = null
|
|
262
|
-
}
|
|
263
|
-
socket.on('close', () => {
|
|
264
|
-
clients -= 1
|
|
265
|
-
armIdle()
|
|
266
|
-
})
|
|
267
|
-
})
|
|
340
|
+
server.on('connection', () => { /* 连接本身不重置空闲计时,只有请求算活动 */ })
|
|
268
341
|
|
|
269
342
|
process.on('exit', () => {
|
|
270
|
-
try {
|
|
343
|
+
try { opsWorker.kill() } catch { /* 忽略 */ }
|
|
344
|
+
try { rotateWorker.kill() } catch { /* 忽略 */ }
|
|
271
345
|
removeStaleSocket(SOCKET)
|
|
272
346
|
})
|
|
273
347
|
|
|
274
348
|
start()
|
|
349
|
+
touchInactivity()
|
package/index.js
CHANGED
|
@@ -4617,6 +4617,10 @@ export function apply(ctx, rawConfig) {
|
|
|
4617
4617
|
}
|
|
4618
4618
|
|
|
4619
4619
|
return () => {
|
|
4620
|
+
// DSH 退出:主动让转屏守护进程恢复横屏(硬退出则由守护进程空闲计时兜底)
|
|
4621
|
+
Promise.resolve()
|
|
4622
|
+
.then(() => windowRemote.restoreOrientation(6_000))
|
|
4623
|
+
.catch(() => {})
|
|
4620
4624
|
if (syncTimer) clearInterval(syncTimer);
|
|
4621
4625
|
if (reloadTimer) clearTimeout(reloadTimer);
|
|
4622
4626
|
try { watcher?.close(); } catch { /* ignore */ }
|
package/lib/orientation.mjs
CHANGED
|
@@ -14,11 +14,8 @@ import fs from 'node:fs'
|
|
|
14
14
|
import os from 'node:os'
|
|
15
15
|
import path from 'node:path'
|
|
16
16
|
import { promisify } from 'node:util'
|
|
17
|
-
import { fileURLToPath } from 'node:url'
|
|
18
|
-
import { powershellExe } from './exec-path.mjs'
|
|
19
17
|
|
|
20
18
|
const run = promisify(execFile)
|
|
21
|
-
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
|
22
19
|
|
|
23
20
|
// 记录"竖屏是不是我们切过去的",用于崩溃/断开后自动恢复横屏
|
|
24
21
|
const MARKER = path.join(os.tmpdir(), 'dsh-homing-orientation.json')
|
|
@@ -82,45 +79,22 @@ async function rotateMac(mode) {
|
|
|
82
79
|
return { ok: true, platform: 'darwin', detail: `${idMatch[1]} -> ${rotation}` }
|
|
83
80
|
}
|
|
84
81
|
|
|
85
|
-
/** 兜底:不经过窗口代理守护进程,直接起一个一次性 PowerShell 转屏 */
|
|
86
|
-
async function rotateWindowsOneShot(mode) {
|
|
87
|
-
const script = path.join(HERE, 'rotate-screen.ps1')
|
|
88
|
-
try {
|
|
89
|
-
const { stdout } = await run(powershellExe(), ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', script, '-Mode', mode], {
|
|
90
|
-
timeout: 15_000,
|
|
91
|
-
windowsHide: true,
|
|
92
|
-
})
|
|
93
|
-
const parsed = JSON.parse(String(stdout).trim().split('\n').pop() || '{}')
|
|
94
|
-
return {
|
|
95
|
-
ok: parsed.ok === true,
|
|
96
|
-
platform: 'win32',
|
|
97
|
-
detail: `one-shot result=${parsed.result ?? '?'} screen=${parsed.width ?? 0}x${parsed.height ?? 0}`,
|
|
98
|
-
screen: { width: Number(parsed.width) || 0, height: Number(parsed.height) || 0 },
|
|
99
|
-
}
|
|
100
|
-
} catch (error) {
|
|
101
|
-
return { ok: false, platform: 'win32', detail: String(error?.stderr ?? error?.message ?? error).slice(0, 200) }
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
82
|
async function rotateWindows(mode) {
|
|
106
|
-
//
|
|
107
|
-
//
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
} catch { /* 走兜底 */ }
|
|
123
|
-
return rotateWindowsOneShot(mode)
|
|
83
|
+
// 转屏统一交给 Node 转屏守护进程(窗口代理守护进程)执行:
|
|
84
|
+
// 守护进程内有独立的转屏工作进程,不会被窗口列表/截图等请求阻塞;
|
|
85
|
+
// 它还会在长时间无活动或 DSH 退出后自动恢复横屏。
|
|
86
|
+
const { rotateScreen } = await import('./window-remote.mjs')
|
|
87
|
+
const result = await rotateScreen(mode, 20_000)
|
|
88
|
+
const screen = result.screen ?? {
|
|
89
|
+
width: Number(result.screenWidth) || 0,
|
|
90
|
+
height: Number(result.screenHeight) || 0,
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
ok: result.ok === true && result.result === 0,
|
|
94
|
+
platform: 'win32',
|
|
95
|
+
detail: `agent result=${result.result ?? '?'} screen=${screen.width}x${screen.height}`,
|
|
96
|
+
screen,
|
|
97
|
+
}
|
|
124
98
|
}
|
|
125
99
|
|
|
126
100
|
/** 设置屏幕方向:portrait(竖屏)/ landscape(横屏) */
|
package/lib/window-agent.ps1
CHANGED
|
@@ -31,6 +31,12 @@ public class W32 {
|
|
|
31
31
|
[DllImport("user32.dll")] public static extern bool EnumWindows(EnumProc cb, IntPtr p);
|
|
32
32
|
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr h);
|
|
33
33
|
[DllImport("user32.dll", CharSet=CharSet.Unicode)] public static extern int GetWindowTextW(IntPtr h, StringBuilder s, int n);
|
|
34
|
+
[DllImport("user32.dll", CharSet=CharSet.Unicode)] public static extern IntPtr SendMessageTimeoutW(IntPtr h, uint msg, IntPtr w, StringBuilder l, uint flags, uint timeout, out IntPtr result);
|
|
35
|
+
public static string SafeWindowText(IntPtr h, int n) {
|
|
36
|
+
StringBuilder sb = new StringBuilder(n); IntPtr res;
|
|
37
|
+
SendMessageTimeoutW(h, 0x000D, (IntPtr)n, sb, 0x0002, 100, out res);
|
|
38
|
+
return sb.ToString();
|
|
39
|
+
}
|
|
34
40
|
[DllImport("user32.dll", CharSet=CharSet.Unicode)] public static extern int GetClassNameW(IntPtr h, StringBuilder s, int n);
|
|
35
41
|
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr h, out RECT r);
|
|
36
42
|
[DllImport("user32.dll")] public static extern bool GetClientRect(IntPtr h, out RECT r);
|
|
@@ -80,9 +86,7 @@ function Get-Windows {
|
|
|
80
86
|
param($h, $p)
|
|
81
87
|
try {
|
|
82
88
|
if ([W32]::IsWindowVisible($h)) {
|
|
83
|
-
$
|
|
84
|
-
[void][W32]::GetWindowTextW($h, $sb, 512)
|
|
85
|
-
$title = $sb.ToString().Trim()
|
|
89
|
+
$title = [W32]::SafeWindowText($h, 512).Trim()
|
|
86
90
|
if ($title.Length -gt 0) {
|
|
87
91
|
$r = New-Object W32+RECT
|
|
88
92
|
[void][W32]::GetWindowRect($h, [ref]$r)
|
package/lib/window-remote.mjs
CHANGED
|
@@ -31,6 +31,7 @@ const TOKEN = loadOrCreateToken()
|
|
|
31
31
|
|
|
32
32
|
let socket = null
|
|
33
33
|
let rl = null
|
|
34
|
+
let socketGeneration = 0
|
|
34
35
|
let seq = 0
|
|
35
36
|
let pending = new Map()
|
|
36
37
|
let chain = Promise.resolve()
|
|
@@ -48,6 +49,8 @@ function fail(error) {
|
|
|
48
49
|
|
|
49
50
|
function attach(stream) {
|
|
50
51
|
socket = stream
|
|
52
|
+
socketGeneration += 1
|
|
53
|
+
const generation = socketGeneration
|
|
51
54
|
rl = readline.createInterface({ input: stream })
|
|
52
55
|
rl.on('line', (line) => {
|
|
53
56
|
const text = line.trim()
|
|
@@ -59,8 +62,9 @@ function attach(stream) {
|
|
|
59
62
|
pending.delete(message.id)
|
|
60
63
|
resolve(message)
|
|
61
64
|
})
|
|
62
|
-
|
|
63
|
-
stream.on('
|
|
65
|
+
// 只处理"当前连接"的关闭;旧连接(超时被销毁后)不能清掉新连接的 pending
|
|
66
|
+
stream.on('close', () => { if (generation === socketGeneration) fail(new Error('窗口代理连接已断开')) })
|
|
67
|
+
stream.on('error', () => { if (generation === socketGeneration) fail(new Error('窗口代理连接错误')) })
|
|
64
68
|
}
|
|
65
69
|
|
|
66
70
|
function connectOnce(timeoutMs = 400) {
|
|
@@ -184,7 +188,7 @@ let listInFlight = null
|
|
|
184
188
|
export const listWindows = () => {
|
|
185
189
|
if (listInFlight !== null) return listInFlight
|
|
186
190
|
listInFlight = (async () => {
|
|
187
|
-
const result = await call({ cmd: 'list' },
|
|
191
|
+
const result = await call({ cmd: 'list' }, 8_000)
|
|
188
192
|
return {
|
|
189
193
|
ok: result.ok === true,
|
|
190
194
|
windows: Array.isArray(result.windows) ? result.windows : [],
|
|
@@ -212,6 +216,7 @@ export const focusWindow = (hwnd) => call({ cmd: 'focus', hwnd: String(hwnd) })
|
|
|
212
216
|
export const closeWindow = (hwnd = '') => call({ cmd: 'close', hwnd: String(hwnd) })
|
|
213
217
|
export const killWindow = (hwnd) => call({ cmd: 'kill', hwnd: String(hwnd) })
|
|
214
218
|
export const rotateScreen = (mode, timeoutMs = 20_000) => call({ cmd: 'rotate', mode: String(mode) }, timeoutMs)
|
|
219
|
+
export const restoreOrientation = (timeoutMs = 10_000) => call({ cmd: 'restore' }, timeoutMs)
|
|
215
220
|
export const cursorPos = () => call({ cmd: 'cursor' })
|
|
216
221
|
export const openPath = (filePath) => call({ cmd: 'open', path: String(filePath) }, 20_000)
|
|
217
222
|
|
package/package.json
CHANGED
package/lib/rotate-screen.ps1
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
param(
|
|
2
|
-
[ValidateSet('portrait', 'landscape')]
|
|
3
|
-
[string]$Mode = 'portrait'
|
|
4
|
-
)
|
|
5
|
-
|
|
6
|
-
# 一次性转屏:不依赖窗口代理守护进程/常驻工作进程。
|
|
7
|
-
# 用法:powershell -NoProfile -NonInteractive -File rotate-screen.ps1 -Mode portrait
|
|
8
|
-
# 输出:单行 JSON { ok, result, width, height }
|
|
9
|
-
|
|
10
|
-
$ErrorActionPreference = 'Stop'
|
|
11
|
-
$angle = if ($Mode -eq 'portrait') { 270 } else { 0 }
|
|
12
|
-
|
|
13
|
-
if (-not ('RotHelper' -as [type])) {
|
|
14
|
-
Add-Type @"
|
|
15
|
-
using System; using System.Runtime.InteropServices;
|
|
16
|
-
public static class RotHelper {
|
|
17
|
-
const int EC=-1; const uint O=0x80,W=0x80000,H=0x100000,REG=1,TEST=2,AT=1,PR=4;
|
|
18
|
-
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] public struct DD { public int cb;
|
|
19
|
-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)] public string dn;
|
|
20
|
-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)] public string ds;
|
|
21
|
-
public uint sf;
|
|
22
|
-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)] public string di;
|
|
23
|
-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)] public string dk; }
|
|
24
|
-
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] public struct DM {
|
|
25
|
-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)] public string n;
|
|
26
|
-
public ushort a,b,c,d; public uint f; public int px,py; public uint o,fo;
|
|
27
|
-
public short cc,dd,yy,tt,co;
|
|
28
|
-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)] public string fn;
|
|
29
|
-
public ushort lp; public uint bpp,w,h,fl,fr,icm,icmi,mt,dt,r1,r2,pw,ph; }
|
|
30
|
-
[DllImport("user32.dll", CharSet=CharSet.Unicode)] static extern bool EnumDisplayDevices(string d, uint i, ref DD dd, uint f);
|
|
31
|
-
[DllImport("user32.dll", CharSet=CharSet.Unicode)] static extern bool EnumDisplaySettings(string d, int m, ref DM dm);
|
|
32
|
-
[DllImport("user32.dll", CharSet=CharSet.Unicode)] static extern int ChangeDisplaySettingsEx(string d, ref DM dm, IntPtr h, uint f, IntPtr l);
|
|
33
|
-
public static string Primary() { for (uint i=0;i<64;i++){ DD dd=new DD(); dd.cb=Marshal.SizeOf(typeof(DD));
|
|
34
|
-
if(!EnumDisplayDevices(null,i,ref dd,0)) break; if((dd.sf&AT)==0) continue; if((dd.sf&PR)!=0) return dd.dn; } return null; }
|
|
35
|
-
public static int Rotate(string dev,int angle){ DM m=new DM(); m.c=(ushort)Marshal.SizeOf(typeof(DM));
|
|
36
|
-
if(!EnumDisplaySettings(dev,EC,ref m)) return -100; int oo=(int)m.o, no=angle/90;
|
|
37
|
-
bool op=oo==1||oo==3, np=no==1||no==3; if(op!=np){uint t=m.w;m.w=m.h;m.h=t;}
|
|
38
|
-
m.o=(uint)no; m.f=O|W|H;
|
|
39
|
-
int t1=ChangeDisplaySettingsEx(dev,ref m,IntPtr.Zero,TEST,IntPtr.Zero); if(t1!=0) return t1*100;
|
|
40
|
-
return ChangeDisplaySettingsEx(dev,ref m,IntPtr.Zero,REG,IntPtr.Zero); }
|
|
41
|
-
}
|
|
42
|
-
"@
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
$dev = [RotHelper]::Primary()
|
|
46
|
-
if (-not $dev) {
|
|
47
|
-
'{"ok":false,"result":-101,"width":0,"height":0}'
|
|
48
|
-
exit 0
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
$result = [RotHelper]::Rotate($dev, $angle)
|
|
52
|
-
Start-Sleep -Milliseconds 300
|
|
53
|
-
|
|
54
|
-
$width = 0
|
|
55
|
-
$height = 0
|
|
56
|
-
try {
|
|
57
|
-
Add-Type -AssemblyName System.Windows.Forms | Out-Null
|
|
58
|
-
$bounds = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
|
|
59
|
-
$width = $bounds.Width
|
|
60
|
-
$height = $bounds.Height
|
|
61
|
-
} catch { /* 忽略 */ }
|
|
62
|
-
|
|
63
|
-
@{ ok = ($result -eq 0); result = $result; width = $width; height = $height } | ConvertTo-Json -Compress
|