Files
lc-mcp/lcplatform/win.py
T

123 lines
5.1 KiB
Python

# -*- coding: utf-8 -*-
"""Windows 截图提供者:交互计划任务(LogonType Interactive)。
OpenSSH 服务会话(session 0)没有交互桌面,截图/前端输入必须在交互会话执行。
任务只注册一次(RunLevel Limited, 截图不需要管理员), 之后每次只改 job.ps1 + Start。
"""
import os
import subprocess
import time
USER = os.environ.get('USERNAME', 'yws-hhh')
BASE = os.path.join(os.path.expanduser('~'), 'lc_mcp')
# 默认放用户目录(只有本人可写); ProgramData 子目录对其他本地用户可写
OUT_DIR = os.environ.get('LCP_SHOT_DIR') or os.path.join(BASE, 'shots')
JOB_PS1 = os.path.join(BASE, 'job.ps1')
RUN_VBS = os.path.join(BASE, 'run.vbs')
TASK = 'LcMCP'
def _ps_quote(s):
"""PowerShell 单引号字面量。"""
return "'" + str(s).replace("'", "''") + "'"
def _vbs_content(job_ps1):
# VBS 字符串里的双引号要写成 ""
cmd = 'powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%s"' % job_ps1
return 'CreateObject("WScript.Shell").Run "%s", 0, False\n' % cmd.replace('"', '""')
def _pshell(code):
r = subprocess.run(['powershell.exe', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command', code],
capture_output=True, text=True, encoding='utf-8', errors='replace', timeout=60)
return r.returncode, (r.stdout or ''), (r.stderr or '')
def _register_task():
"""注册(或覆盖)交互计划任务。进程生命周期内只做一次。"""
reg = ("$A=New-ScheduledTaskAction -Execute 'wscript.exe' -Argument %s; "
"$P=New-ScheduledTaskPrincipal -UserId %s -LogonType Interactive -RunLevel Limited; "
"$S=New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries "
"-ExecutionTimeLimit (New-TimeSpan -Minutes 2) -MultipleInstances Parallel; "
"Register-ScheduledTask -TaskName %s -Action $A -Principal $P -Settings $S -Force | Out-Null; 'OK'"
% (_ps_quote('"%s"' % RUN_VBS), _ps_quote(USER), _ps_quote(TASK)))
rc, out, err = _pshell(reg)
if 'OK' not in out:
raise RuntimeError('register task fail: %s %s' % (out[-150:], err[-150:]))
def _run_interactive(ps1_code, expect_file, timeout=15):
# 带 BOM: Windows PowerShell 5.1 把无 BOM 的 .ps1 当 ANSI 读, 中文路径会乱码
with open(JOB_PS1, 'w', encoding='utf-8-sig') as f:
f.write('$ErrorActionPreference="Stop"\n' + ps1_code)
try:
if os.path.exists(expect_file):
os.remove(expect_file)
except Exception:
pass
rc, out, err = _pshell("Start-ScheduledTask -TaskName %s; 'OK'" % _ps_quote(TASK))
if 'OK' not in out:
return {"ok": False, "error": "task start fail: %s %s" % (out[-150:], err[-150:])}
deadline = time.time() + timeout
while time.time() < deadline:
if os.path.exists(expect_file):
try:
with open(expect_file, 'r', encoding='utf-8-sig') as f:
out = f.read().strip()
except Exception:
out = ''
try:
os.remove(expect_file)
except Exception:
pass
return {"ok": True, "out": out}
time.sleep(0.2)
return {"ok": False, "error": "timeout waiting %s" % expect_file}
# 截图前声明 DPI 感知, 否则缩放 != 100% 时 Bounds 是逻辑尺寸, 截出来是裁切的
_SHOT_PS = r"""
Add-Type -AssemblyName System.Windows.Forms,System.Drawing
Add-Type -Namespace LcMcp -Name Dpi -MemberDefinition '[DllImport("user32.dll")] public static extern bool SetProcessDPIAware();'
[void][LcMcp.Dpi]::SetProcessDPIAware()
$img = %(img)s
$done = %(done)s
try {
$b=[System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$bmp=New-Object System.Drawing.Bitmap $b.Width,$b.Height
$g=[System.Drawing.Graphics]::FromImage($bmp)
$g.CopyFromScreen($b.X,$b.Y,0,0,$b.Size)
$bmp.Save($img, [System.Drawing.Imaging.ImageFormat]::Png)
$g.Dispose(); $bmp.Dispose()
'DONE' | Set-Content -Encoding UTF8 $done
} catch {
('ERR ' + $_.Exception.Message) | Set-Content -Encoding UTF8 $done
}
"""
class WinScreenshotProvider:
def __init__(self):
os.makedirs(BASE, exist_ok=True)
os.makedirs(OUT_DIR, exist_ok=True)
# WSH 只认 ANSI / UTF-16(BOM), UTF-8 下中文路径会坏
with open(RUN_VBS, 'w', encoding='utf-16') as f:
f.write(_vbs_content(JOB_PS1))
_register_task()
def grab(self):
stamp = time.strftime('%Y%m%d_%H%M%S') + '_%03d' % (int(time.time() * 1000) % 1000)
img_path = os.path.join(OUT_DIR, 'shot_%s.png' % stamp)
done = os.path.join(OUT_DIR, 'done_%s.txt' % stamp)
ps = _SHOT_PS % {'img': _ps_quote(img_path), 'done': _ps_quote(done)}
r = _run_interactive(ps, done, timeout=20)
if not r.get('ok'):
return {'ok': False, 'error': r.get('error', 'screenshot fail')}
if r.get('out', '').startswith('ERR'):
return {'ok': False, 'error': r['out']}
if not os.path.exists(img_path):
return {'ok': False, 'error': 'png not produced'}
return {'ok': True, 'path': img_path.replace('\\', '/')}