@andy.ian/dsh-homing 2.1.9 → 2.1.11
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/vendor/vnc.bundle.js +61 -44
- package/lib/vnc-app.js +64 -34
- package/lib/vnc-boot.js +1 -1
- 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/vendor/vnc.bundle.js
CHANGED
|
@@ -14517,19 +14517,7 @@ async function rotateDesktop(mode) {
|
|
|
14517
14517
|
setStatus("\u5207\u6362\u5931\u8D25\uFF1A" + (data.detail || "\u672A\u77E5\u539F\u56E0"), "bad");
|
|
14518
14518
|
return;
|
|
14519
14519
|
}
|
|
14520
|
-
|
|
14521
|
-
if (mode === "portrait") localStorage.setItem("dsh-vnc-portrait", "1");
|
|
14522
|
-
else localStorage.removeItem("dsh-vnc-portrait");
|
|
14523
|
-
} catch {
|
|
14524
|
-
}
|
|
14525
|
-
setStatus(mode === "portrait" ? "\u5DF2\u5207\u7AD6\u5C4F\uFF0C\u91CD\u8FDE\u4E2D\u2026" : "\u5DF2\u5207\u6A2A\u5C4F\uFF0C\u91CD\u8FDE\u4E2D\u2026", "ok");
|
|
14526
|
-
setTimeout(() => {
|
|
14527
|
-
try {
|
|
14528
|
-
cleanup();
|
|
14529
|
-
connect();
|
|
14530
|
-
} catch {
|
|
14531
|
-
}
|
|
14532
|
-
}, 1200);
|
|
14520
|
+
setStatus(mode === "portrait" ? "\u5DF2\u5207\u7AD6\u5C4F\uFF0C\u753B\u9762\u81EA\u52A8\u9002\u914D\u4E2D\u2026" : "\u5DF2\u5207\u6A2A\u5C4F\uFF0C\u753B\u9762\u81EA\u52A8\u9002\u914D\u4E2D\u2026", "ok");
|
|
14533
14521
|
} catch (error) {
|
|
14534
14522
|
setStatus("\u5207\u6362\u5931\u8D25\uFF1A" + (error?.message ?? error), "bad");
|
|
14535
14523
|
}
|
|
@@ -14671,13 +14659,38 @@ function canvasOfGuard() {
|
|
|
14671
14659
|
}, true);
|
|
14672
14660
|
}
|
|
14673
14661
|
}
|
|
14662
|
+
var reconnectTimer = null;
|
|
14663
|
+
var reconnectTries = 0;
|
|
14664
|
+
var userClosed = false;
|
|
14665
|
+
function clearReconnect() {
|
|
14666
|
+
if (reconnectTimer !== null) {
|
|
14667
|
+
clearTimeout(reconnectTimer);
|
|
14668
|
+
reconnectTimer = null;
|
|
14669
|
+
}
|
|
14670
|
+
}
|
|
14671
|
+
function scheduleReconnect() {
|
|
14672
|
+
if (userClosed || reconnectTimer !== null) return;
|
|
14673
|
+
reconnectTries += 1;
|
|
14674
|
+
if (reconnectTries > 6) {
|
|
14675
|
+
setStatus("\u8FDE\u63A5\u4E2D\u65AD\uFF0C\u8BF7\u70B9\u300C\u8FDE\u63A5\u300D\u91CD\u8BD5", "bad");
|
|
14676
|
+
return;
|
|
14677
|
+
}
|
|
14678
|
+
const delay = Math.min(800 * reconnectTries, 4e3);
|
|
14679
|
+
setStatus("\u8FDE\u63A5\u4E2D\u65AD\uFF0C" + (delay / 1e3).toFixed(1) + " \u79D2\u540E\u91CD\u8FDE\u2026", "bad");
|
|
14680
|
+
reconnectTimer = setTimeout(() => {
|
|
14681
|
+
reconnectTimer = null;
|
|
14682
|
+
if (rfb === null && !userClosed) connect();
|
|
14683
|
+
}, delay);
|
|
14684
|
+
}
|
|
14674
14685
|
function cleanup() {
|
|
14686
|
+
clearReconnect();
|
|
14675
14687
|
if (rfb !== null) {
|
|
14688
|
+
const old = rfb;
|
|
14689
|
+
rfb = null;
|
|
14676
14690
|
try {
|
|
14677
|
-
|
|
14691
|
+
old.disconnect();
|
|
14678
14692
|
} catch {
|
|
14679
14693
|
}
|
|
14680
|
-
rfb = null;
|
|
14681
14694
|
}
|
|
14682
14695
|
connected = false;
|
|
14683
14696
|
stopPolling();
|
|
@@ -14687,11 +14700,21 @@ function cleanup() {
|
|
|
14687
14700
|
passwordEl.disabled = false;
|
|
14688
14701
|
}
|
|
14689
14702
|
function connect() {
|
|
14690
|
-
|
|
14703
|
+
userClosed = false;
|
|
14704
|
+
clearReconnect();
|
|
14705
|
+
if (rfb !== null) {
|
|
14706
|
+
const old = rfb;
|
|
14707
|
+
rfb = null;
|
|
14708
|
+
try {
|
|
14709
|
+
old.disconnect();
|
|
14710
|
+
} catch {
|
|
14711
|
+
}
|
|
14712
|
+
}
|
|
14691
14713
|
setStatus("\u8FDE\u63A5\u4E2D\u2026");
|
|
14692
14714
|
connectBtn.disabled = true;
|
|
14715
|
+
let client = null;
|
|
14693
14716
|
try {
|
|
14694
|
-
|
|
14717
|
+
client = new RFB(screenEl, wsUrl(), {
|
|
14695
14718
|
wsProtocols: ["binary"]
|
|
14696
14719
|
});
|
|
14697
14720
|
} catch (error) {
|
|
@@ -14699,12 +14722,15 @@ function connect() {
|
|
|
14699
14722
|
cleanup();
|
|
14700
14723
|
return;
|
|
14701
14724
|
}
|
|
14702
|
-
rfb
|
|
14703
|
-
|
|
14704
|
-
|
|
14705
|
-
|
|
14725
|
+
rfb = client;
|
|
14726
|
+
client.scaleViewport = false;
|
|
14727
|
+
client.clipViewport = false;
|
|
14728
|
+
client.showDotCursor = false;
|
|
14729
|
+
client.focusOnClick = true;
|
|
14706
14730
|
setViewMode("width");
|
|
14707
|
-
|
|
14731
|
+
client.addEventListener("connect", () => {
|
|
14732
|
+
if (rfb !== client) return;
|
|
14733
|
+
reconnectTries = 0;
|
|
14708
14734
|
connected = true;
|
|
14709
14735
|
report("connect-ok", "ok");
|
|
14710
14736
|
disconnectBtn.disabled = false;
|
|
@@ -14719,40 +14745,31 @@ function connect() {
|
|
|
14719
14745
|
});
|
|
14720
14746
|
}
|
|
14721
14747
|
});
|
|
14722
|
-
|
|
14748
|
+
client.addEventListener("disconnect", (event) => {
|
|
14749
|
+
if (rfb !== client) return;
|
|
14723
14750
|
const clean = event.detail?.clean ?? false;
|
|
14724
|
-
report("disconnect", clean ? "clean" : "dirty", { detail: event.detail ?? null, reason:
|
|
14725
|
-
|
|
14751
|
+
report("disconnect", clean ? "clean" : "dirty", { detail: event.detail ?? null, reason: client._lastFailReason ?? null });
|
|
14752
|
+
rfb = null;
|
|
14726
14753
|
cleanup();
|
|
14754
|
+
if (clean || userClosed) {
|
|
14755
|
+
setStatus("\u5DF2\u65AD\u5F00", clean ? "" : "bad");
|
|
14756
|
+
return;
|
|
14757
|
+
}
|
|
14758
|
+
scheduleReconnect();
|
|
14727
14759
|
});
|
|
14728
|
-
|
|
14760
|
+
client.addEventListener("securityfailure", (event) => {
|
|
14761
|
+
if (rfb !== client) return;
|
|
14729
14762
|
setStatus("\u8BA4\u8BC1\u5931\u8D25\uFF1A" + (event.detail?.reason ?? "\u5BC6\u7801\u9519\u8BEF"), "bad");
|
|
14763
|
+
rfb = null;
|
|
14730
14764
|
cleanup();
|
|
14731
14765
|
});
|
|
14732
14766
|
}
|
|
14733
14767
|
connectBtn.addEventListener("click", connect);
|
|
14734
14768
|
disconnectBtn.addEventListener("click", () => {
|
|
14769
|
+
userClosed = true;
|
|
14735
14770
|
cleanup();
|
|
14736
14771
|
setStatus("\u5DF2\u65AD\u5F00");
|
|
14737
14772
|
});
|
|
14738
|
-
window.addEventListener("pagehide", () => {
|
|
14739
|
-
try {
|
|
14740
|
-
if (localStorage.getItem("dsh-vnc-portrait") === "1") {
|
|
14741
|
-
localStorage.removeItem("dsh-vnc-portrait");
|
|
14742
|
-
const body = new Blob([JSON.stringify({ kind: "rotate", mode: "landscape" })], { type: "application/json" });
|
|
14743
|
-
if (navigator.sendBeacon) navigator.sendBeacon("/remote/input", body);
|
|
14744
|
-
else fetch("/remote/input", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ kind: "rotate", mode: "landscape" }), keepalive: true }).catch(() => {
|
|
14745
|
-
});
|
|
14746
|
-
}
|
|
14747
|
-
} catch {
|
|
14748
|
-
}
|
|
14749
|
-
if (rfb !== null) {
|
|
14750
|
-
try {
|
|
14751
|
-
rfb.disconnect();
|
|
14752
|
-
} catch {
|
|
14753
|
-
}
|
|
14754
|
-
}
|
|
14755
|
-
});
|
|
14756
14773
|
setupMobileKeyboard();
|
|
14757
14774
|
setupToolbar();
|
|
14758
14775
|
function autoConnect() {
|
package/lib/vnc-app.js
CHANGED
|
@@ -410,14 +410,9 @@ async function rotateDesktop(mode) {
|
|
|
410
410
|
setStatus('切换失败:' + (data.detail || '未知原因'), 'bad')
|
|
411
411
|
return
|
|
412
412
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
} catch { /* 忽略 */ }
|
|
417
|
-
setStatus(mode === 'portrait' ? '已切竖屏,重连中…' : '已切横屏,重连中…', 'ok')
|
|
418
|
-
setTimeout(() => {
|
|
419
|
-
try { cleanup(); connect() } catch { /* 忽略 */ }
|
|
420
|
-
}, 1200)
|
|
413
|
+
// 不再强制重连:noVNC 会随帧缓冲尺寸变化自动适配;
|
|
414
|
+
// 若 TightVNC 因分辨率变化断开,由下面的自动重连逻辑接管。
|
|
415
|
+
setStatus(mode === 'portrait' ? '已切竖屏,画面自动适配中…' : '已切横屏,画面自动适配中…', 'ok')
|
|
421
416
|
} catch (error) {
|
|
422
417
|
setStatus('切换失败:' + (error?.message ?? error), 'bad')
|
|
423
418
|
}
|
|
@@ -571,10 +566,39 @@ function canvasOfGuard() {
|
|
|
571
566
|
}
|
|
572
567
|
}
|
|
573
568
|
|
|
569
|
+
let reconnectTimer = null
|
|
570
|
+
let reconnectTries = 0
|
|
571
|
+
let userClosed = false
|
|
572
|
+
|
|
573
|
+
function clearReconnect() {
|
|
574
|
+
if (reconnectTimer !== null) {
|
|
575
|
+
clearTimeout(reconnectTimer)
|
|
576
|
+
reconnectTimer = null
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/** 非用户主动断开:退避重连(TightVNC 在分辨率变化时可能断开连接) */
|
|
581
|
+
function scheduleReconnect() {
|
|
582
|
+
if (userClosed || reconnectTimer !== null) return
|
|
583
|
+
reconnectTries += 1
|
|
584
|
+
if (reconnectTries > 6) {
|
|
585
|
+
setStatus('连接中断,请点「连接」重试', 'bad')
|
|
586
|
+
return
|
|
587
|
+
}
|
|
588
|
+
const delay = Math.min(800 * reconnectTries, 4000)
|
|
589
|
+
setStatus('连接中断,' + (delay / 1000).toFixed(1) + ' 秒后重连…', 'bad')
|
|
590
|
+
reconnectTimer = setTimeout(() => {
|
|
591
|
+
reconnectTimer = null
|
|
592
|
+
if (rfb === null && !userClosed) connect()
|
|
593
|
+
}, delay)
|
|
594
|
+
}
|
|
595
|
+
|
|
574
596
|
function cleanup() {
|
|
597
|
+
clearReconnect()
|
|
575
598
|
if (rfb !== null) {
|
|
576
|
-
|
|
599
|
+
const old = rfb
|
|
577
600
|
rfb = null
|
|
601
|
+
try { old.disconnect() } catch { /* 已断开 */ }
|
|
578
602
|
}
|
|
579
603
|
connected = false
|
|
580
604
|
stopPolling()
|
|
@@ -585,12 +609,20 @@ function cleanup() {
|
|
|
585
609
|
}
|
|
586
610
|
|
|
587
611
|
function connect() {
|
|
588
|
-
|
|
612
|
+
userClosed = false
|
|
613
|
+
clearReconnect()
|
|
614
|
+
if (rfb !== null) {
|
|
615
|
+
const old = rfb
|
|
616
|
+
rfb = null
|
|
617
|
+
try { old.disconnect() } catch { /* 已断开 */ }
|
|
618
|
+
}
|
|
589
619
|
setStatus('连接中…')
|
|
590
620
|
connectBtn.disabled = true
|
|
621
|
+
|
|
622
|
+
let client = null
|
|
591
623
|
try {
|
|
592
624
|
// 密码由服务端代认证(lib/vnc-auth-proxy.mjs),浏览器不持有密码
|
|
593
|
-
|
|
625
|
+
client = new RFB(screenEl, wsUrl(), {
|
|
594
626
|
wsProtocols: ['binary'],
|
|
595
627
|
})
|
|
596
628
|
} catch (error) {
|
|
@@ -598,14 +630,16 @@ function connect() {
|
|
|
598
630
|
cleanup()
|
|
599
631
|
return
|
|
600
632
|
}
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
633
|
+
rfb = client
|
|
634
|
+
client.scaleViewport = false
|
|
635
|
+
client.clipViewport = false
|
|
636
|
+
client.showDotCursor = false
|
|
637
|
+
client.focusOnClick = true
|
|
606
638
|
setViewMode('width')
|
|
607
639
|
|
|
608
|
-
|
|
640
|
+
client.addEventListener('connect', () => {
|
|
641
|
+
if (rfb !== client) return
|
|
642
|
+
reconnectTries = 0
|
|
609
643
|
connected = true
|
|
610
644
|
report('connect-ok', 'ok')
|
|
611
645
|
disconnectBtn.disabled = false
|
|
@@ -618,37 +652,33 @@ function connect() {
|
|
|
618
652
|
focusByHwnd(focus).then((found) => { if (!found) setStatus('未找到对应窗口,已显示整屏') })
|
|
619
653
|
}
|
|
620
654
|
})
|
|
621
|
-
|
|
655
|
+
client.addEventListener('disconnect', (event) => {
|
|
656
|
+
if (rfb !== client) return // 旧连接的迟到事件不能影响新连接
|
|
622
657
|
const clean = event.detail?.clean ?? false
|
|
623
|
-
report('disconnect', clean ? 'clean' : 'dirty', { detail: event.detail ?? null, reason:
|
|
624
|
-
|
|
658
|
+
report('disconnect', clean ? 'clean' : 'dirty', { detail: event.detail ?? null, reason: client._lastFailReason ?? null })
|
|
659
|
+
rfb = null
|
|
625
660
|
cleanup()
|
|
661
|
+
if (clean || userClosed) {
|
|
662
|
+
setStatus('已断开', clean ? '' : 'bad')
|
|
663
|
+
return
|
|
664
|
+
}
|
|
665
|
+
scheduleReconnect()
|
|
626
666
|
})
|
|
627
|
-
|
|
667
|
+
client.addEventListener('securityfailure', (event) => {
|
|
668
|
+
if (rfb !== client) return
|
|
628
669
|
setStatus('认证失败:' + (event.detail?.reason ?? '密码错误'), 'bad')
|
|
670
|
+
rfb = null
|
|
629
671
|
cleanup()
|
|
630
672
|
})
|
|
631
673
|
}
|
|
632
674
|
|
|
633
675
|
connectBtn.addEventListener('click', connect)
|
|
634
676
|
disconnectBtn.addEventListener('click', () => {
|
|
677
|
+
userClosed = true
|
|
635
678
|
cleanup()
|
|
636
679
|
setStatus('已断开')
|
|
637
680
|
})
|
|
638
681
|
|
|
639
|
-
window.addEventListener('pagehide', () => {
|
|
640
|
-
// 离开页面时如果竖屏是我们切的,恢复横屏,避免把 PC 留在竖屏
|
|
641
|
-
try {
|
|
642
|
-
if (localStorage.getItem('dsh-vnc-portrait') === '1') {
|
|
643
|
-
localStorage.removeItem('dsh-vnc-portrait')
|
|
644
|
-
const body = new Blob([JSON.stringify({ kind: 'rotate', mode: 'landscape' })], { type: 'application/json' })
|
|
645
|
-
if (navigator.sendBeacon) navigator.sendBeacon('/remote/input', body)
|
|
646
|
-
else fetch('/remote/input', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ kind: 'rotate', mode: 'landscape' }), keepalive: true }).catch(() => {})
|
|
647
|
-
}
|
|
648
|
-
} catch { /* 忽略 */ }
|
|
649
|
-
if (rfb !== null) { try { rfb.disconnect() } catch { /* 忽略 */ } }
|
|
650
|
-
})
|
|
651
|
-
|
|
652
682
|
setupMobileKeyboard()
|
|
653
683
|
setupToolbar()
|
|
654
684
|
|
package/lib/vnc-boot.js
CHANGED
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
|