diff --git a/backends/_util.py b/backends/_util.py new file mode 100644 index 0000000..609db3f --- /dev/null +++ b/backends/_util.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +"""后端共用小工具。""" +import ctypes + +KEY_ALIASES = {'return': 'enter', 'escape': 'esc', 'del': 'delete', 'ins': 'insert', + 'pgup': 'pageup', 'pgdn': 'pagedown', 'control': 'ctrl', 'win': 'lwin', + 'arrowup': 'up', 'arrowdown': 'down', 'arrowleft': 'left', 'arrowright': 'right'} + + +def parse_combo(key): + """'Alt+F4' -> ['alt', 'f4']; 单个 '+' 视为加号键。""" + k = str(key).strip().lower() + if k == '+' or '+' not in k: + parts = [k] + else: + parts = [p.strip() for p in k.split('+')] + if any(not p for p in parts): + raise ValueError('bad key combo %r' % key) + return [KEY_ALIASES.get(p, p) for p in parts] + + +def win_dpi_aware(user32): + """声明 per-monitor DPI 感知, 让 SetCursorPos/GetSystemMetrics 用物理像素, 和截图一致。""" + try: + ctypes.windll.shcore.SetProcessDpiAwareness(2) + return + except Exception: + pass + try: + user32.SetProcessDPIAware() + except Exception: + pass diff --git a/backends/interception_win.py b/backends/interception_win.py index 48b4a09..a638ad3 100644 --- a/backends/interception_win.py +++ b/backends/interception_win.py @@ -3,11 +3,15 @@ 输入在驱动层转为真实 HID 事件(对应用透明,非 SendInput 虚拟输入)。 需要: install-interception.exe /install 后重启系统,且本 dll 与驱动共存。 + +API 以 oblitum/Interception interception.h 为准: + 设备号 1..10 = 键盘, 11..20 = 鼠标; 没有 open_device 之类的函数。 + interception_send(ctx, device, stroke*, nstroke) 第 4 个参数是 stroke 个数, 不是字节数。 """ import ctypes import time -from ctypes import wintypes +from ._util import parse_combo, win_dpi_aware # ---- Interception 常量 ---- MOUSE_LEFTBUTTON_DOWN = 0x001 @@ -25,9 +29,12 @@ KEY_DOWN = 0x00 KEY_UP = 0x01 KEY_E0 = 0x02 +MAX_KEYBOARD = 10 +MAX_MOUSE = 10 +KEYBOARD_DEVS = range(1, MAX_KEYBOARD + 1) +MOUSE_DEVS = range(MAX_KEYBOARD + 1, MAX_KEYBOARD + MAX_MOUSE + 1) + # ---- Set 1 扫描码 ---- -SCAN_ALPHA = {chr(c): 0x1E + i for i, c in enumerate(range(ord('A'), ord('Z') + 1))} -# 上面的映射只在 A-P 连续,Q-Z 不连续,改为显式表 SCAN = { 'a': 0x1E, 'b': 0x30, 'c': 0x2E, 'd': 0x20, 'e': 0x12, 'f': 0x21, 'g': 0x22, 'h': 0x23, 'i': 0x17, 'j': 0x24, 'k': 0x25, 'l': 0x26, @@ -36,37 +43,59 @@ SCAN = { 'y': 0x15, 'z': 0x2C, '1': 0x02, '2': 0x03, '3': 0x04, '4': 0x05, '5': 0x06, '6': 0x07, '7': 0x08, '8': 0x09, '9': 0x0A, '0': 0x0B, - 'enter': 0x1C, 'return': 0x1C, 'esc': 0x01, 'space': 0x39, 'tab': 0x0F, + 'enter': 0x1C, 'esc': 0x01, 'space': 0x39, 'tab': 0x0F, 'backspace': 0x0E, 'ctrl': 0x1D, 'alt': 0x38, 'lshift': 0x2A, 'rshift': 0x36, 'shift': 0x2A, 'f1': 0x3B, 'f2': 0x3C, 'f3': 0x3D, 'f4': 0x3E, 'f5': 0x3F, 'f6': 0x40, 'f7': 0x41, 'f8': 0x42, 'f9': 0x43, 'f10': 0x44, 'f11': 0x57, 'f12': 0x58, ',': 0x33, '.': 0x34, '/': 0x35, ';': 0x27, "'": 0x28, '[': 0x1A, ']': 0x1B, '-': 0x0C, '=': 0x0D, '`': 0x29, '\\': 0x2B, } -UPPER_SCAN = { # 大写所需符号的扫描码(按住 Shift 时) +# 扩展键: 扫描码同小键盘, 需带 E0 前缀 +SCAN_E0 = { + 'up': 0x48, 'down': 0x50, 'left': 0x4B, 'right': 0x4D, + 'home': 0x47, 'end': 0x4F, 'pageup': 0x49, 'pagedown': 0x51, + 'insert': 0x52, 'delete': 0x53, 'rctrl': 0x1D, 'ralt': 0x38, 'lwin': 0x5B, +} +UPPER_SCAN = { # 需按住 Shift 的符号 '!': 0x02, '@': 0x03, '#': 0x04, '$': 0x05, '%': 0x06, '^': 0x07, '&': 0x08, '*': 0x09, '(': 0x0A, ')': 0x0B, '_': 0x0C, '+': 0x0D, '{': 0x1A, '}': 0x1B, ':': 0x27, '"': 0x28, '<': 0x33, '>': 0x34, '?': 0x35, '~': 0x29, '|': 0x2B, } -MAX_DEVICES = 64 # 键盘下游设备数量通常 <= 16,鼠标 <= 16;放宽到 64 覆盖异常 - class MouseStroke(ctypes.Structure): + # interception.h: unsigned short state, flags; short rolling; int x, y; unsigned int information _fields_ = [("state", ctypes.c_ushort), ("flags", ctypes.c_ushort), - ("rolling", ctypes.c_short), ("x", ctypes.c_ulong), - ("y", ctypes.c_ulong), ("information", ctypes.c_ulong)] + ("rolling", ctypes.c_short), ("x", ctypes.c_int), + ("y", ctypes.c_int), ("information", ctypes.c_uint)] class KeyStroke(ctypes.Structure): _fields_ = [("code", ctypes.c_ushort), ("state", ctypes.c_ushort), - ("information", ctypes.c_ulong)] + ("information", ctypes.c_uint)] + + +def key_strokes(key): + """键名 -> (scancode, e0) ; 未知返回 None。""" + if key in SCAN: + return SCAN[key], False + if key in SCAN_E0: + return SCAN_E0[key], True + return None + + +def to_absolute(v, size): + """像素 -> Interception 绝对坐标 0..65535 (主屏)。""" + if size <= 1: + return 0 + v = min(max(int(v), 0), size - 1) + return int(round(v * 65535.0 / (size - 1))) class InterceptionBackend: - def __init__(self, dll_path=None): + def __init__(self, dll_path=None, keyboard=None, mouse=None): if dll_path is None: dll_path = r'C:\Program Files\Interception\interception.dll' try: @@ -76,69 +105,77 @@ class InterceptionBackend: self.dll = ctypes.CDLL('interception.dll') self._api() self._user32 = ctypes.WinDLL('user32', use_last_error=True) + win_dpi_aware(self._user32) self.ctx = self.dll.interception_create_context() if not self.ctx: raise RuntimeError('interception_create_context failed (驱动未安装/未重启?)') - self._kbd_devs, self._mou_devs = self._discover() + # 只往一个设备发: 广播到所有键盘会让同一个键被按多次 + self.kbd = keyboard or self._first_present(KEYBOARD_DEVS) + self.mou = mouse or self._first_present(MOUSE_DEVS) self._scr_w = self._user32.GetSystemMetrics(0) self._scr_h = self._user32.GetSystemMetrics(1) + def __del__(self): + try: + if getattr(self, 'ctx', None): + self.dll.interception_destroy_context(self.ctx) + self.ctx = None + except Exception: + pass + def _api(self): d = self.dll d.interception_create_context.restype = ctypes.c_void_p d.interception_create_context.argtypes = [] + d.interception_destroy_context.restype = None d.interception_destroy_context.argtypes = [ctypes.c_void_p] - d.interception_open_device.restype = ctypes.c_bool - d.interception_open_device.argtypes = [ctypes.c_void_p, ctypes.c_int] - d.interception_close_device.argtypes = [ctypes.c_void_p, ctypes.c_int] - d.interception_get_hardware_id.restype = ctypes.c_int + d.interception_get_hardware_id.restype = ctypes.c_uint d.interception_get_hardware_id.argtypes = [ctypes.c_void_p, ctypes.c_int, - ctypes.c_char_p, ctypes.c_int] + ctypes.c_void_p, ctypes.c_uint] d.interception_send.restype = ctypes.c_int d.interception_send.argtypes = [ctypes.c_void_p, ctypes.c_int, - ctypes.c_void_p, ctypes.c_size_t] + ctypes.c_void_p, ctypes.c_uint] - def _discover(self): - kbd, mou = [], [] - buf = ctypes.create_string_buffer(256) - for dev in range(1, MAX_DEVICES + 1): - if not self.dll.interception_open_device(self.ctx, dev): - continue - n = self.dll.interception_get_hardware_id(self.ctx, dev, buf, 256) - hid = buf.value.decode('ascii', 'replace').lower() if n > 0 else '' - if 'keyboard' in hid: - kbd.append(dev) - elif 'mouse' in hid: - mou.append(dev) - return kbd, mou + def hardware_id(self, dev): + buf = ctypes.create_string_buffer(512) + n = self.dll.interception_get_hardware_id(self.ctx, dev, buf, ctypes.sizeof(buf)) + if not n or n > ctypes.sizeof(buf): + return '' + # 驱动返回 UTF-16 (REG_MULTI_SZ), 取第一个 + return buf.raw[:n].decode('utf-16-le', 'replace').split('\x00')[0] + + def _first_present(self, devs): + for dev in devs: + if self.hardware_id(dev): + return dev + return devs[0] + + def set_screen(self, w, h): + """以截图尺寸为准(会话 0 / DPI 虚拟化下 GetSystemMetrics 不可信)。""" + self._scr_w, self._scr_h = int(w), int(h) # ---- 底层发送 ---- - def _kbd_stroke(self, code, state): - ks = KeyStroke() - ks.code = ctypes.c_ushort(code) - ks.state = ctypes.c_ushort(state) - for dev in self._kbd_devs or [1]: - try: - self.dll.interception_send(self.ctx, dev, ctypes.byref(ks), ctypes.sizeof(ks)) - except Exception: - pass + def _send(self, dev, stroke): + n = self.dll.interception_send(self.ctx, dev, ctypes.byref(stroke), 1) + if n != 1: + raise RuntimeError('interception_send(dev=%d) returned %d' % (dev, n)) + + def _kbd_stroke(self, code, state, e0=False): + ks = KeyStroke(code=code, state=state | (KEY_E0 if e0 else 0)) + self._send(self.kbd, ks) def _mouse_stroke(self, state=0, flags=0, x=0, y=0): - ms = MouseStroke() - ms.state = ctypes.c_ushort(state) - ms.flags = ctypes.c_ushort(flags) - ms.x = ctypes.c_ulong(int(x)) - ms.y = ctypes.c_ulong(int(y)) - for dev in self._mou_devs or [17]: - try: - self.dll.interception_send(self.ctx, dev, ctypes.byref(ms), ctypes.sizeof(ms)) - except Exception: - pass + self._send(self.mou, MouseStroke(state=state, flags=flags, x=int(x), y=int(y))) + + def _tap(self, code, e0=False, hold=0.04): + self._kbd_stroke(code, KEY_DOWN, e0) + time.sleep(hold) + self._kbd_stroke(code, KEY_UP, e0) # ---- 公开动作 ---- def move(self, x, y): - nx = int(int(x) * 65535 / self._scr_w) if self._scr_w else 0 - ny = int(int(y) * 65535 / self._scr_h) if self._scr_h else 0 + nx = to_absolute(x, self._scr_w) + ny = to_absolute(y, self._scr_h) self._mouse_stroke(state=0, flags=MOUSE_MOVE_ABSOLUTE, x=nx, y=ny) time.sleep(0.03) return {'ok': True, 'backend': 'interception'} @@ -152,44 +189,51 @@ class InterceptionBackend: down, up = MOUSE_RIGHTBUTTON_DOWN, MOUSE_RIGHTBUTTON_UP for _ in range(max(1, int(clicks))): self._mouse_stroke(state=down) - time.sleep(0.04) + time.sleep(0.06) self._mouse_stroke(state=up) - time.sleep(0.05) + time.sleep(0.06) return {'ok': True, 'clicked': [int(x), int(y)], 'backend': 'interception'} - def _scancode(self, key): - k = str(key).lower() - return SCAN.get(k) - def keypress(self, key): - k = str(key) - code = SCAN.get(k.lower()) - if code is None: + try: + parts = parse_combo(key) + except ValueError as e: + return {'ok': False, 'error': str(e)} + strokes = [key_strokes(p) for p in parts] + if None in strokes: return {'ok': False, 'error': 'unknown key %s' % key} - self._kbd_stroke(code, KEY_DOWN) - time.sleep(0.04) - self._kbd_stroke(code, KEY_UP) - time.sleep(0.02) - return {'ok': True, 'key': k, 'backend': 'interception'} + for code, e0 in strokes[:-1]: # 修饰键按下 + self._kbd_stroke(code, KEY_DOWN, e0) + time.sleep(0.03) + self._tap(*strokes[-1]) + for code, e0 in reversed(strokes[:-1]): + time.sleep(0.03) + self._kbd_stroke(code, KEY_UP, e0) + return {'ok': True, 'key': str(key), 'backend': 'interception'} def type_text(self, text): - for ch in str(text): + text = str(text) + plan = [] + for ch in text: low = ch.lower() - if ch.isupper() and low in SCAN: - self._kbd_stroke(SCAN['shift'], KEY_DOWN) - self._kbd_stroke(SCAN[low], KEY_DOWN) - self._kbd_stroke(SCAN[low], KEY_UP) - self._kbd_stroke(SCAN['shift'], KEY_UP) - elif ch.lower() in SCAN: - code = SCAN[low] - self._kbd_stroke(code, KEY_DOWN) - self._kbd_stroke(code, KEY_UP) + if ch == ' ': + plan.append((SCAN['space'], False)) + elif ch == '\n': + plan.append((SCAN['enter'], False)) + elif low in SCAN and len(low) == 1: + plan.append((SCAN[low], ch.isupper())) elif ch in UPPER_SCAN: - self._kbd_stroke(SCAN['shift'], KEY_DOWN) - self._kbd_stroke(UPPER_SCAN[ch], KEY_DOWN) - self._kbd_stroke(UPPER_SCAN[ch], KEY_UP) - self._kbd_stroke(SCAN['shift'], KEY_UP) + plan.append((UPPER_SCAN[ch], True)) else: - return {'ok': False, 'error': 'untypable char %r' % ch} - time.sleep(0.015) - return {'ok': True, 'typed': str(text), 'backend': 'interception'} \ No newline at end of file + # 扫描码只能打 US 布局 ASCII; 中文请用 sendinput 后端(KEYEVENTF_UNICODE) + return {'ok': False, 'error': 'untypable char %r (interception 只支持 ASCII)' % ch} + for code, shift in plan: + if shift: + self._kbd_stroke(SCAN['shift'], KEY_DOWN) + time.sleep(0.02) + self._tap(code, hold=0.03) + if shift: + time.sleep(0.02) + self._kbd_stroke(SCAN['shift'], KEY_UP) + time.sleep(0.03) + return {'ok': True, 'typed': text, 'backend': 'interception'} diff --git a/backends/sendinput_win.py b/backends/sendinput_win.py index 0b3480f..dbae153 100644 --- a/backends/sendinput_win.py +++ b/backends/sendinput_win.py @@ -1,20 +1,26 @@ # -*- coding: utf-8 -*- """SendInput 后端(Windows,ctypes user32)。 当前线上 lc_mcp.py 用的机制;作为 interception 不可用时的兜底。 +注意: SendInput 只能注入到调用进程所在的交互桌面; 从 SSH(session 0)里跑不会作用到游戏窗口。 """ import ctypes import time from ctypes import wintypes +from ._util import parse_combo, win_dpi_aware + INPUT_MOUSE = 0 INPUT_KEYBOARD = 1 +KEYEVENTF_EXTENDEDKEY = 0x0001 KEYEVENTF_KEYUP = 0x0002 KEYEVENTF_UNICODE = 0x0004 +MOUSEEVENTF_MOVE = 0x0001 MOUSEEVENTF_LEFTDOWN = 0x0002 MOUSEEVENTF_LEFTUP = 0x0004 MOUSEEVENTF_RIGHTDOWN = 0x0008 MOUSEEVENTF_RIGHTUP = 0x0010 +MAPVK_VK_TO_VSC = 0 class MOUSEINPUT(ctypes.Structure): @@ -36,65 +42,106 @@ class INPUT(ctypes.Structure): _fields_ = [("type", wintypes.DWORD), ("i", _I)] -KEYMAP = {'enter': 0x0D, 'return': 0x0D, 'esc': 0x1B, 'space': 0x20, - 'tab': 0x09, 'ctrl': 0x11, 'alt': 0x12, 'shift': 0x10, - 'p': 0x50, 'f4': 0x73, 'f5': 0x74, 'f8': 0x77, 'f12': 0x7B, - '1': 0x31, '2': 0x32, '3': 0x33, '4': 0x34, '5': 0x35, - '6': 0x36, '7': 0x37, '8': 0x38, '9': 0x39, '0': 0x30} +KEYMAP = {'enter': 0x0D, 'esc': 0x1B, 'space': 0x20, 'tab': 0x09, 'backspace': 0x08, + 'ctrl': 0x11, 'alt': 0x12, 'shift': 0x10, 'lshift': 0xA0, 'rshift': 0xA1, + 'rctrl': 0xA3, 'ralt': 0xA5, 'lwin': 0x5B, + 'up': 0x26, 'down': 0x28, 'left': 0x25, 'right': 0x27, + 'home': 0x24, 'end': 0x23, 'pageup': 0x21, 'pagedown': 0x22, + 'insert': 0x2D, 'delete': 0x2E} +KEYMAP.update({'f%d' % i: 0x6F + i for i in range(1, 13)}) +EXTENDED = {0x26, 0x28, 0x25, 0x27, 0x24, 0x23, 0x21, 0x22, 0x2D, 0x2E, 0xA3, 0xA5, 0x5B} + + +def utf16_units(text): + """str -> UTF-16 码元列表(BMP 外字符拆成代理对)。""" + data = str(text).encode('utf-16-le') + return [int.from_bytes(data[i:i + 2], 'little') for i in range(0, len(data), 2)] class SendInputBackend: def __init__(self): self._user32 = ctypes.WinDLL('user32', use_last_error=True) + win_dpi_aware(self._user32) + self._user32.VkKeyScanW.restype = ctypes.c_short + self._user32.VkKeyScanW.argtypes = [wintypes.WCHAR] + self._user32.MapVirtualKeyW.restype = wintypes.UINT + self._user32.MapVirtualKeyW.argtypes = [wintypes.UINT, wintypes.UINT] def _send_input(self, *inputs): n = len(inputs) arr = (INPUT * n)(*inputs) - return self._user32.SendInput(n, arr, ctypes.sizeof(INPUT)) + sent = self._user32.SendInput(n, arr, ctypes.sizeof(INPUT)) + if sent != n: + raise ctypes.WinError(ctypes.get_last_error()) + return sent + + def _mouse(self, flags): + m = INPUT(); m.type = INPUT_MOUSE; m.mi.dwFlags = flags + return m def click(self, x, y, button='left', clicks=1): self._user32.SetCursorPos(int(x), int(y)) + # 补一个 0 位移的 MOVE, 让只看原始输入/WM_MOUSEMOVE 的游戏刷新 hover + self._send_input(self._mouse(MOUSEEVENTF_MOVE)) time.sleep(0.03) down_flag = MOUSEEVENTF_LEFTDOWN if button == 'left' else MOUSEEVENTF_RIGHTDOWN up_flag = MOUSEEVENTF_LEFTUP if button == 'left' else MOUSEEVENTF_RIGHTUP for _ in range(max(1, int(clicks))): - down = INPUT(); down.type = INPUT_MOUSE; down.mi.dwFlags = down_flag - up = INPUT(); up.type = INPUT_MOUSE; up.mi.dwFlags = up_flag - self._send_input(down, up) + self._send_input(self._mouse(down_flag)) + time.sleep(0.05) + self._send_input(self._mouse(up_flag)) time.sleep(0.05) return {'ok': True, 'clicked': [int(x), int(y)], 'backend': 'sendinput'} def move(self, x, y): self._user32.SetCursorPos(int(x), int(y)) + self._send_input(self._mouse(MOUSEEVENTF_MOVE)) time.sleep(0.01) return {'ok': True, 'backend': 'sendinput'} + def _vk(self, name): + if name in KEYMAP: + return KEYMAP[name] + if len(name) == 1: + r = self._user32.VkKeyScanW(name) + if r != -1: + return r & 0xFF + return None + + def _key(self, vk, up): + ki = INPUT(); ki.type = INPUT_KEYBOARD + ki.ki.wVk = vk + ki.ki.wScan = self._user32.MapVirtualKeyW(vk, MAPVK_VK_TO_VSC) + ki.ki.dwFlags = (KEYEVENTF_KEYUP if up else 0) | (KEYEVENTF_EXTENDEDKEY if vk in EXTENDED else 0) + self._send_input(ki) + def keypress(self, key): - name = str(key).lower() - vk = KEYMAP.get(name) - if vk is None and len(name) == 1: - vk = ord(name.upper()) if name.isalpha() else ord(name) - if vk is None: + try: + parts = parse_combo(key) + except ValueError as e: + return {'ok': False, 'error': str(e)} + vks = [self._vk(p) for p in parts] + if None in vks: return {'ok': False, 'error': 'unknown key %s' % key} - self._press(vk) + for vk in vks[:-1]: + self._key(vk, False) + time.sleep(0.03) + self._key(vks[-1], False) + time.sleep(0.04) + self._key(vks[-1], True) + for vk in reversed(vks[:-1]): + time.sleep(0.03) + self._key(vk, True) return {'ok': True, 'key': str(key), 'backend': 'sendinput'} - def _press(self, vk): - ki = INPUT(); ki.type = INPUT_KEYBOARD; ki.ki.wVk = vk - self._send_input(ki) - time.sleep(0.04) - ki2 = INPUT(); ki2.type = INPUT_KEYBOARD; ki2.ki.wVk = vk - ki2.ki.dwFlags = KEYEVENTF_KEYUP - self._send_input(ki2) - def type_text(self, text): - for ch in str(text): + for unit in utf16_units(text): ki = INPUT(); ki.type = INPUT_KEYBOARD - ki.ki.wScan = ord(ch); ki.ki.dwFlags = KEYEVENTF_UNICODE + ki.ki.wScan = unit; ki.ki.dwFlags = KEYEVENTF_UNICODE ki2 = INPUT(); ki2.type = INPUT_KEYBOARD - ki2.ki.wScan = ord(ch) + ki2.ki.wScan = unit ki2.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP self._send_input(ki, ki2) time.sleep(0.01) - return {'ok': True, 'typed': str(text), 'backend': 'sendinput'} \ No newline at end of file + return {'ok': True, 'typed': str(text), 'backend': 'sendinput'} diff --git a/backends/uinput_linux.py b/backends/uinput_linux.py index 5040b81..e4c7b38 100644 --- a/backends/uinput_linux.py +++ b/backends/uinput_linux.py @@ -1,94 +1,133 @@ # -*- coding: utf-8 -*- """Linux uinput 物理输入后端(虚拟内核 evdev 设备,需 root 或 uinput 组权限)。 比 xdotool 更接近硬件:内核层面人为物理设备,无 X 层痕迹。 -依赖: python3-uinput 或手工 ioctl。 +依赖: python-uinput (pip install python-uinput)。 + +鼠标用绝对坐标轴(ABS_X/ABS_Y): 相对位移(REL_X/REL_Y)会被 libinput 指针加速放大, 点不准。 """ -import os -import struct +import time + +from ._util import parse_combo try: import uinput HAVE_UINPUT = True except ImportError: + uinput = None HAVE_UINPUT = False +ABS_MAX = 65535 + +# 键名 -> python-uinput 常量名 +_NAMED = { + 'enter': 'ENTER', 'esc': 'ESC', 'space': 'SPACE', 'tab': 'TAB', 'backspace': 'BACKSPACE', + 'ctrl': 'LEFTCTRL', 'rctrl': 'RIGHTCTRL', 'alt': 'LEFTALT', 'ralt': 'RIGHTALT', + 'shift': 'LEFTSHIFT', 'lshift': 'LEFTSHIFT', 'rshift': 'RIGHTSHIFT', 'lwin': 'LEFTMETA', + 'up': 'UP', 'down': 'DOWN', 'left': 'LEFT', 'right': 'RIGHT', + 'home': 'HOME', 'end': 'END', 'pageup': 'PAGEUP', 'pagedown': 'PAGEDOWN', + 'insert': 'INSERT', 'delete': 'DELETE', + ',': 'COMMA', '.': 'DOT', '/': 'SLASH', ';': 'SEMICOLON', "'": 'APOSTROPHE', + '[': 'LEFTBRACE', ']': 'RIGHTBRACE', '-': 'MINUS', '=': 'EQUAL', '`': 'GRAVE', '\\': 'BACKSLASH', +} +_NAMED.update({c: c.upper() for c in 'abcdefghijklmnopqrstuvwxyz0123456789'}) +_NAMED.update({'f%d' % i: 'F%d' % i for i in range(1, 13)}) +# US 布局下需要 Shift 的符号 -> 基础键 +_SHIFTED = {'!': '1', '@': '2', '#': '3', '$': '4', '%': '5', '^': '6', '&': '7', '*': '8', + '(': '9', ')': '0', '_': '-', '+': '=', '{': '[', '}': ']', ':': ';', '"': "'", + '<': ',', '>': '.', '?': '/', '~': '`', '|': '\\'} + + +def _key_event(name): + const = _NAMED.get(name) + return getattr(uinput, 'KEY_' + const) if const else None + + +def _x_screen_size(): + try: + from Xlib import display + s = display.Display().screen() + return s.width_in_pixels, s.height_in_pixels + except Exception: + return 1920, 1080 + class UinputBackend: - def __init__(self): + def __init__(self, screen=None): if not HAVE_UINPUT: - raise RuntimeError('python3-uinput not installed') - self._ctx = None + raise RuntimeError('python-uinput not installed (pip install python-uinput)') + self._scr_w, self._scr_h = screen or _x_screen_size() + events = [getattr(uinput, 'KEY_' + c) for c in sorted(set(_NAMED.values()))] + events += [uinput.BTN_LEFT, uinput.BTN_RIGHT, + uinput.ABS_X + (0, ABS_MAX, 0, 0), uinput.ABS_Y + (0, ABS_MAX, 0, 0)] + self._dev = uinput.Device(events, name='lc-mcp-uinput') + time.sleep(0.5) # 等 udev / 显示服务器识别新设备 - def _ensure(self): - if self._ctx is None: - from uinput import Device, KEY_A, KEY_Z, KEY_ENTER, KEY_ESC, KEY_SPACE, KEY_TAB, KEY_F4, KEY_LEFTCTRL, KEY_LEFTSHIFT, KEY_LEFTALT, KEY_BACKSPACE, REL_X, REL_Y, BTN_LEFT, BTN_RIGHT - events = ( - # 全键盘范围(够用): A-Z + 数字 + 常用 - *[key for key in range(KEY_A, KEY_Z + 1)], - KEY_ENTER, KEY_ESC, KEY_SPACE, KEY_TAB, KEY_F4, - KEY_LEFTCTRL, KEY_LEFTSHIFT, KEY_LEFTALT, KEY_BACKSPACE, - REL_X, REL_Y, BTN_LEFT, BTN_RIGHT, - ) - self._dev = Device(events) - return self._dev + def set_screen(self, w, h): + self._scr_w, self._scr_h = int(w), int(h) - def _key(self, code): - dev = self._ensure() - dev.emit(code, 1) - dev.emit(code, 0) - time.sleep(0.03) + def _abs(self, v, size): + v = min(max(int(v), 0), max(size - 1, 0)) + return int(round(v * ABS_MAX / max(size - 1, 1))) def move(self, x, y): - # uinput 只有相对位移; 需要知道当前位置。先简单相对 0 通过 xdotool 定位, 或 https 轮询 - # 简化: 用 python-xlib 查指针位置 -> 计算 delta - try: - from Xlib import display - d = display.Display() - root = d.screen().root - qp = root.query_pointer() - curx, cury = qp.root_x, qp.root_y - dx, dy = int(x) - curx, int(y) - cury - dev = self._ensure() - dev.emit(REL_X, dx) - dev.emit(REL_Y, dy) - d.sync() - except Exception: - return {'ok': False, 'error': 'uinput move needs python-xlib'} - return {'ok': True} + self._dev.emit(uinput.ABS_X, self._abs(x, self._scr_w), syn=False) + self._dev.emit(uinput.ABS_Y, self._abs(y, self._scr_h)) + time.sleep(0.03) + return {'ok': True, 'backend': 'uinput'} - def click(self, x, y, button='left', times=1): + def click(self, x, y, button='left', clicks=1): self.move(x, y) - dev = self._ensure() - btn = BTN_LEFT if button == 'left' else BTN_RIGHT - for _ in range(times): - dev.emit(btn, 1) + time.sleep(0.03) + btn = uinput.BTN_LEFT if button == 'left' else uinput.BTN_RIGHT + for _ in range(max(1, int(clicks))): + self._dev.emit(btn, 1) time.sleep(0.05) - dev.emit(btn, 0) + self._dev.emit(btn, 0) time.sleep(0.05) - return {'ok': True} + return {'ok': True, 'clicked': [int(x), int(y)], 'backend': 'uinput'} + + def _tap(self, ev, hold=0.03): + self._dev.emit(ev, 1) + time.sleep(hold) + self._dev.emit(ev, 0) def keypress(self, key): - mapping = {'enter': KEY_ENTER, 'esc': KEY_ESC, 'space': KEY_SPACE, - 'tab': KEY_TAB, 'f4': KEY_F4, 'ctrl': KEY_LEFTCTRL, - 'shift': KEY_LEFTSHIFT, 'alt': KEY_LEFTALT, 'backspace': KEY_BACKSPACE} - k = key.lower() - if k in mapping: - self._key(mapping[k]) - return {'ok': True} - if len(k) == 1 and 'a' <= k <= 'z': - from uinput import KEY_A - self._key(KEY_A + (ord(k) - ord('a'))) - return {'ok': True} - return {'ok': False, 'error': 'unsupported key %s' % key} + try: + parts = parse_combo(key) + except ValueError as e: + return {'ok': False, 'error': str(e)} + evs = [_key_event(p) for p in parts] + if None in evs: + return {'ok': False, 'error': 'unsupported key %s' % key} + for ev in evs[:-1]: + self._dev.emit(ev, 1) + time.sleep(0.03) + self._tap(evs[-1], 0.04) + for ev in reversed(evs[:-1]): + time.sleep(0.03) + self._dev.emit(ev, 0) + return {'ok': True, 'key': str(key), 'backend': 'uinput'} def type_text(self, text): - for ch in text: - if 'a' <= ch.lower() <= 'z': - self.keypress(ch.lower()) - elif ch == ' ': - self.keypress('space') + plan = [] + for ch in str(text): + if ch == ' ': + plan.append(('space', False)) + elif ch == '\n': + plan.append(('enter', False)) + elif ch.lower() in _NAMED and len(ch) == 1: + plan.append((ch.lower(), ch.isupper())) + elif ch in _SHIFTED: + plan.append((_SHIFTED[ch], True)) else: - return {'ok': False, 'error': 'unsupported char %r' % ch} - time.sleep(0.05) - return {'ok': True} \ No newline at end of file + return {'ok': False, 'error': 'unsupported char %r (uinput 只支持 US 布局 ASCII)' % ch} + shift = uinput.KEY_LEFTSHIFT + for name, need_shift in plan: + if need_shift: + self._dev.emit(shift, 1) + self._tap(_key_event(name)) + if need_shift: + self._dev.emit(shift, 0) + time.sleep(0.03) + return {'ok': True, 'typed': str(text), 'backend': 'uinput'} diff --git a/backends/xdotool_linux.py b/backends/xdotool_linux.py index b84014c..462be4c 100644 --- a/backends/xdotool_linux.py +++ b/backends/xdotool_linux.py @@ -4,11 +4,24 @@ """ import shutil import subprocess -import time + +from ._util import parse_combo + +# 内部键名 -> X keysym +KEYSYM = {'enter': 'Return', 'esc': 'Escape', 'space': 'space', 'tab': 'Tab', + 'backspace': 'BackSpace', 'alt': 'Alt_L', 'ralt': 'Alt_R', 'ctrl': 'Control_L', + 'rctrl': 'Control_R', 'shift': 'Shift_L', 'lshift': 'Shift_L', 'rshift': 'Shift_R', + 'lwin': 'Super_L', 'up': 'Up', 'down': 'Down', 'left': 'Left', 'right': 'Right', + 'home': 'Home', 'end': 'End', 'pageup': 'Prior', 'pagedown': 'Next', + 'insert': 'Insert', 'delete': 'Delete'} +KEYSYM.update({'f%d' % i: 'F%d' % i for i in range(1, 13)}) def _run(args): - return subprocess.run(args, capture_output=True, text=True, timeout=30) + r = subprocess.run(args, capture_output=True, text=True, timeout=30) + if r.returncode != 0: + raise RuntimeError('%s failed: %s' % (' '.join(args[:2]), (r.stderr or '').strip()[-200:])) + return r class XdotoolBackend: @@ -17,34 +30,26 @@ class XdotoolBackend: if not shutil.which('xdotool'): raise RuntimeError('xdotool not found (apt install xdotool)') - def _screen(self): - try: - r = _run(['xdotool', 'getdisplaygeometry']) - w, h = r.stdout.strip().split() - return int(w), int(h) - except Exception: - return 1920, 1080 - def move(self, x, y): - _run(['xdotool', 'mousemove', str(int(x)), str(int(y))]) - return {'ok': True} + _run(['xdotool', 'mousemove', '--sync', str(int(x)), str(int(y))]) + return {'ok': True, 'backend': 'xdotool'} - def click(self, x, y, button='left', times=1): + def click(self, x, y, button='left', clicks=1): btn = {'left': '1', 'right': '3', 'middle': '2'}.get(button, '1') - _run(['xdotool', 'mousemove', str(int(x)), str(int(y))]) - _run(['xdotool', 'click', '--repeat', str(times), '--delay', '60', btn]) - return {'ok': True} + _run(['xdotool', 'mousemove', '--sync', str(int(x)), str(int(y)), + 'click', '--repeat', str(max(1, int(clicks))), '--delay', '60', btn]) + return {'ok': True, 'clicked': [int(x), int(y)], 'backend': 'xdotool'} def keypress(self, key): - key = key.lower() - # xdotool 键名映射 - mapping = {'enter': 'Return', 'esc': 'Escape', 'space': 'space', - 'tab': 'Tab', 'alt': 'Alt_L', 'f4': 'F4', 'ctrl': 'Control_L', - 'shift': 'Shift_L', 'backspace': 'BackSpace'} - k = mapping.get(key, key) - _run(['xdotool', 'key', k]) - return {'ok': True} + try: + parts = parse_combo(key) + except ValueError as e: + return {'ok': False, 'error': str(e)} + combo = '+'.join(KEYSYM.get(p, p) for p in parts) + _run(['xdotool', 'key', '--', combo]) + return {'ok': True, 'key': str(key), 'backend': 'xdotool'} def type_text(self, text): - _run(['xdotool', 'type', '--delay', '60', text]) - return {'ok': True} \ No newline at end of file + # '--' 防止以 '-' 开头的文本被当成选项 + _run(['xdotool', 'type', '--delay', '60', '--', str(text)]) + return {'ok': True, 'typed': str(text), 'backend': 'xdotool'} diff --git a/lc_mcp.py b/lc_mcp.py index 2d00131..a4098d7 100644 --- a/lc_mcp.py +++ b/lc_mcp.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -"""Limbus Company 语义化操控 MCP server v0.5 (跨平台)。 +"""Limbus Company 语义化操控 MCP server v0.6 (跨平台)。 核心思路: 不让 AI 盲猜坐标。工具层做"文本化 + 语义点击"—— get_state : 截图 + OCR -> 全页文字元素(带坐标) @@ -10,22 +10,32 @@ win : 截图=交互计划任务; 输入=sendinput|interception linux : 截图=X11 PIL/scrot; 输入=xdotool|uinput(物理) mac : 截图=screencapture; 输入=cg -OCR 服务: 跑在远端(默认 http://35.212.193.150:8599/), 客户端机器零安装。 +OCR: 纯本地 (Windows.Media.Ocr / winsdk)。 协议: MCP stdio (newline-delimited JSON-RPC)。 """ +import glob import os +import re +import struct import sys import json import time -from platform import detect_platform, get_input_backend, get_screenshot_provider +from lcplatform import detect_platform, get_input_backend, get_screenshot_provider +VERSION = '0.6.0' BACKEND_NAME = os.environ.get('INPUT_BACKEND', '').strip().lower() OCR_BACKEND = os.environ.get('OCR_BACKEND', 'local').strip().lower() MIN_SCORE = float(os.environ.get('OCR_MIN_SCORE', '0.5')) +KEEP_SHOTS = int(os.environ.get('LCP_KEEP_SHOTS', '30')) +MAX_WAIT = 300.0 +WAIT_INTERVAL = 1.2 BASE = os.path.join(os.path.expanduser('~'), 'lc_mcp') os.makedirs(BASE, exist_ok=True) +NEED_SCREEN = {'screenshot', 'get_state', 'wait_for_text', 'wait_for_text_gone', 'click_text'} +NEED_INPUT = {'click_text', 'click', 'move', 'keypress', 'type_text'} + def log(msg): try: @@ -46,19 +56,59 @@ def ocr_image(img_path): items, err = ocr_local.ocr_image(img_path) if items is None: return None, err + # Windows.Media.Ocr 不给置信度; 只有带 score 的后端才会被过滤 items = [it for it in items if float(it.get('score', 1)) >= MIN_SCORE] return items, None +def png_size(path): + """读 PNG IHDR 拿宽高(纯标准库)。失败返回 None。""" + try: + with open(path, 'rb') as f: + head = f.read(24) + if head[:8] != b'\x89PNG\r\n\x1a\n': + return None + return struct.unpack('>II', head[16:24]) + except Exception: + return None + + +def prune_shots(path, keep=KEEP_SHOTS): + """只保留同目录下最新 keep 张截图,防止轮询把磁盘写满。""" + if keep <= 0: + return + d = os.path.dirname(path) + shots = glob.glob(os.path.join(d, 'shot_*.png')) + glob.glob(os.path.join(d, 'lc_shot_*.png')) + shots.sort(key=lambda p: os.path.getmtime(p) if os.path.exists(p) else 0) + for p in shots[:-keep]: + try: + os.remove(p) + except Exception: + pass + + +_WS = re.compile(r'\s+') + + +def norm(s): + """匹配用归一化: 去全部空白 + 小写。 + Windows OCR 对中文按单字切词, 拼行时会变成 "开 始"。""" + return _WS.sub('', str(s)).lower() + + def calc_click(txt, items): - """在 OCR items 里找包含 txt 的项,返回中心和候选列表。""" - hits = [] + """在 OCR items 里找包含 txt 的项,返回中心列表。完全相等的排前面。""" + key = norm(txt) + if not key: + return [] + exact, partial = [], [] for it in items: - if txt.lower() in it['text'].lower(): - cx = it['x'] + it['w'] // 2 - cy = it['y'] + it['h'] // 2 - hits.append({'text': it['text'], 'x': cx, 'y': cy}) - return hits + t = norm(it['text']) + if key not in t: + continue + hit = {'text': it['text'], 'x': it['x'] + it['w'] // 2, 'y': it['y'] + it['h'] // 2} + (exact if t == key else partial).append(hit) + return exact + partial class MCPServer: @@ -70,94 +120,132 @@ class MCPServer: self.tools = [ {"name": "get_state", "description": "截图并OCR, 返回当前屏幕全部文字元素(含坐标)。AI 决策前先调这个, 不需要自己猜坐标", "inputSchema": {"type": "object", "properties": {}}}, - {"name": "click_text", "description": "语义点击: 给按钮文字(支持子串), 内部OCR定位后点击其中心。例: click_text('开始')", + {"name": "click_text", "description": "语义点击: 给按钮文字(支持子串, 忽略空格; 完全匹配优先), 内部OCR定位后点击其中心。例: click_text('开始')", "inputSchema": {"type": "object", "properties": { "text": {"type": "string"}, "index": {"type": "number", "default": 0}}, "required": ["text"]}}, - {"name": "wait_for_text", "description": "循环截图+OCR, 等到指定文字出现(默认最多30秒)", "inputSchema": {"type": "object", "properties": { + {"name": "wait_for_text", "description": "循环截图+OCR, 等到指定文字出现(默认30秒, 上限300秒)", "inputSchema": {"type": "object", "properties": { "text": {"type": "string"}, "timeout": {"type": "number", "default": 30}}, "required": ["text"]}}, - {"name": "wait_for_text_gone", "description": "循环截图+OCR, 等到指定文字消失(默认最多30秒)", "inputSchema": {"type": "object", "properties": { + {"name": "wait_for_text_gone", "description": "循环截图+OCR, 等到指定文字消失(默认30秒, 上限300秒)", "inputSchema": {"type": "object", "properties": { "text": {"type": "string"}, "timeout": {"type": "number", "default": 30}}, "required": ["text"]}}, {"name": "screenshot", "description": "截图并返回PNG路径(用于模板/人工查看)", "inputSchema": {"type": "object", "properties": {}}}, - {"name": "click", "description": "直接按坐标点击(一般用 click_text 更稳)", "inputSchema": {"type": "object", "properties": { + {"name": "click", "description": "直接按坐标点击(截图像素坐标; 一般用 click_text 更稳)", "inputSchema": {"type": "object", "properties": { "x": {"type": "number"}, "y": {"type": "number"}, "button": {"type": "string", "enum": ["left", "right"], "default": "left"}}, "required": ["x", "y"]}}, {"name": "move", "description": "移动鼠标到(x,y)", "inputSchema": {"type": "object", "properties": { "x": {"type": "number"}, "y": {"type": "number"}}, "required": ["x", "y"]}}, - {"name": "keypress", "description": "按一次键(p/enter/esc/space/tab/alt/f4/单字符等)", "inputSchema": {"type": "object", "properties": { + {"name": "keypress", "description": "按一次键(p/enter/esc/space/tab/alt/f1-f12/单字符等)", "inputSchema": {"type": "object", "properties": { "key": {"type": "string"}}, "required": ["key"]}}, {"name": "type_text", "description": "输入一串文本", "inputSchema": {"type": "object", "properties": { "text": {"type": "string"}}, "required": ["text"]}}, ] + def _grab(self): + """截图 + 清理旧图 + 把真实屏幕像素尺寸告诉输入后端(坐标换算以截图为准)。""" + sh = self.provider.grab() + if sh.get('ok'): + prune_shots(sh['path']) + size = png_size(sh['path']) + if size and hasattr(self.backend, 'set_screen'): + self.backend.set_screen(*size) + return sh + + def _ocr_now(self): + sh = self._grab() + if not sh.get('ok'): + return sh, None + items, err = ocr_image(sh['path']) + if err: + return {'ok': False, 'error': err, 'screen': sh['path']}, None + return sh, items or [] + def call(self, name, args): args = args or {} if name == 'screenshot': - return self.provider.grab() + return self._grab() if name == 'get_state': - sh = self.provider.grab() - if not sh.get('ok'): + sh, items = self._ocr_now() + if items is None: return sh - items, err = ocr_image(sh['path']) - if err: - return {'ok': False, 'error': err} return {'ok': True, 'screen': sh['path'], 'items': items} if name == 'click_text': - sh = self.provider.grab() - if not sh.get('ok'): + txt = str(args.get('text', '')) + if not norm(txt): + return {'ok': False, 'error': 'text must be non-empty'} + sh, items = self._ocr_now() + if items is None: return sh - items, err = ocr_image(sh['path']) - if err: - return {'ok': False, 'error': err} - hits = calc_click(args.get('text', ''), items or []) + hits = calc_click(txt, items) if not hits: - return {'ok': False, 'error': 'text not found: %s' % args.get('text', ''), + return {'ok': False, 'error': 'text not found: %s' % txt, 'screen': sh['path'], 'items': items} - idx = min(int(args.get('index', 0)), len(hits) - 1) + idx = int(args.get('index', 0)) + if not 0 <= idx < len(hits): + return {'ok': False, 'error': 'index %d out of range (%d hits)' % (idx, len(hits)), + 'hits': hits[:10]} h = hits[idx] r = self.backend.click(h['x'], h['y']) r['on'] = h['text'] + if len(hits) > 1: + r['other_hits'] = len(hits) - 1 return r - if name == 'wait_for_text': - return self._wait(args.get('text', ''), float(args.get('timeout', 30)), True) - if name == 'wait_for_text_gone': - return self._wait(args.get('text', ''), float(args.get('timeout', 30)), False) + if name in ('wait_for_text', 'wait_for_text_gone'): + txt = str(args.get('text', '')) + if not norm(txt): + return {'ok': False, 'error': 'text must be non-empty'} + timeout = max(0.0, min(float(args.get('timeout', 30)), MAX_WAIT)) + return self._wait(txt, timeout, name == 'wait_for_text') if name == 'click': - return self.backend.click(int(args.get('x', 0)), int(args.get('y', 0)), args.get('button', 'left'), 1) + button = args.get('button', 'left') + if button not in ('left', 'right'): + return {'ok': False, 'error': 'button must be left/right'} + return self.backend.click(int(args['x']), int(args['y']), button, 1) if name == 'move': - return self.backend.move(int(args.get('x', 0)), int(args.get('y', 0))) + return self.backend.move(int(args['x']), int(args['y'])) if name == 'keypress': - return self.backend.keypress(args.get('key', '')) + return self.backend.keypress(str(args.get('key', ''))) if name == 'type_text': - return self.backend.type_text(args.get('text', '')) + return self.backend.type_text(str(args.get('text', ''))) return {'ok': False, 'error': 'unknown tool %s' % name} def _wait(self, txt, timeout, want_present): deadline = time.time() + timeout - last_items = [] - while time.time() < deadline: - sh = self.provider.grab() - if sh.get('ok'): - items, err = ocr_image(sh['path']) - if not err: - last_items = items or [] - hits = calc_click(txt, last_items) - if bool(hits) == want_present: - return {'ok': True, 'present': bool(hits), - 'hits': hits[:5], 'screen': sh['path']} - time.sleep(1.2) - return {'ok': False, 'error': 'timeout waiting %s (present=%s)' % (txt, want_present), - 'last_items': last_items[:10]} + last_items, last_err = [], None + while True: + sh, items = self._ocr_now() + if items is not None: + last_items = items + hits = calc_click(txt, items) + if bool(hits) == want_present: + return {'ok': True, 'present': bool(hits), + 'hits': hits[:5], 'screen': sh['path']} + else: + last_err = sh.get('error') + if time.time() + WAIT_INTERVAL > deadline: + break + time.sleep(WAIT_INTERVAL) + r = {'ok': False, 'error': 'timeout waiting %s (present=%s)' % (txt, want_present), + 'last_items': last_items[:10]} + if last_err: + r['last_error'] = last_err + return r def read_msg(): + """读一行 JSON-RPC。EOF 返回 None; 空行/坏 JSON 抛 ValueError 由调用方回错误后继续。""" line = sys.stdin.readline() if not line: return None - return json.loads(line.strip()) + line = line.strip() + if not line: + return {} + msg = json.loads(line) + if not isinstance(msg, dict): + raise ValueError('request must be a JSON object') + return msg def send(obj): @@ -165,64 +253,70 @@ def send(obj): sys.stdout.flush() +def tool_result(mid, result, is_error=False): + send({"jsonrpc": "2.0", "id": mid, "result": { + "content": [{"type": "text", "text": json.dumps(result, ensure_ascii=False)}], + "isError": bool(is_error)}}) + + def main(): platform = detect_platform() - log('lc_mcp v0.5 platform=%s backend=%s ocr=%s' % (platform, BACKEND_NAME or 'auto', OCR_BACKEND)) - backend = None + log('lc_mcp v%s platform=%s backend=%s ocr=%s' % (VERSION, platform, BACKEND_NAME or 'auto', OCR_BACKEND)) + provider = backend = None try: provider = get_screenshot_provider(platform) + except Exception as e: + log('screenshot provider init fail: %s' % e) + try: backend = get_input_backend(BACKEND_NAME, platform) except Exception as e: - log('init fail: %s' % e) - provider = None - backend = None - if backend is None: - log('WARN: input backend unavailable, semantic/click tools disabled') + log('input backend init fail: %s' % e) server = MCPServer(platform, provider, backend) while True: try: msg = read_msg() - if msg is None: - break - except Exception as e: - log('read err %s' % e) + except ValueError as e: + log('parse err %s' % e) + send({"jsonrpc": "2.0", "id": None, "error": {"code": -32700, "message": "parse error: %s" % e}}) + continue + if msg is None: break + if not msg: + continue method = msg.get('method') mid = msg.get('id') if method == 'initialize': send({"jsonrpc": "2.0", "id": mid, "result": { "protocolVersion": "2024-11-05", "capabilities": {"tools": {"listChanged": False}}, - "serverInfo": {"name": "lc-mcp", "version": "0.5.0"}}}) - elif method == 'notifications/initialized': - continue + "serverInfo": {"name": "lc-mcp", "version": VERSION}}}) elif method == 'ping': send({"jsonrpc": "2.0", "id": mid, "result": {}}) elif method == 'tools/list': send({"jsonrpc": "2.0", "id": mid, "result": {"tools": server.tools}}) elif method == 'tools/call': - params = msg.get('params', {}) + params = msg.get('params') or {} name = params.get('name') - args = params.get('arguments', {}) + args = params.get('arguments') or {} log('call %s %s' % (name, args)) - if server.backend is None and name not in ('screenshot', 'get_state'): - send({"jsonrpc": "2.0", "id": mid, "result": {"content": [ - {"type": "text", "text": json.dumps({'ok': False, 'error': 'input backend unavailable'})}]}}) + if name in NEED_SCREEN and server.provider is None: + tool_result(mid, {'ok': False, 'error': 'screenshot provider unavailable (see lc_mcp.log)'}, True) + continue + if name in NEED_INPUT and server.backend is None: + tool_result(mid, {'ok': False, 'error': 'input backend unavailable (see lc_mcp.log)'}, True) continue try: result = server.call(name, args) - send({"jsonrpc": "2.0", "id": mid, "result": {"content": [ - {"type": "text", "text": json.dumps(result, ensure_ascii=False)}]}}) + tool_result(mid, result, not result.get('ok', True)) except Exception as e: - log('tool err %s' % e) - send({"jsonrpc": "2.0", "id": mid, "result": {"content": [ - {"type": "text", "text": json.dumps({'ok': False, 'error': str(e)})}]}, "isError": True}) - else: - if mid is not None: - send({"jsonrpc": "2.0", "id": mid, - "error": {"code": -32601, "message": "method not found"}}) + log('tool err %s: %r' % (name, e)) + tool_result(mid, {'ok': False, 'error': '%s: %s' % (type(e).__name__, e)}, True) + elif mid is not None: + send({"jsonrpc": "2.0", "id": mid, + "error": {"code": -32601, "message": "method not found"}}) + # 其余无 id 的通知(notifications/*)按规范忽略 log('lc_mcp exit') if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/platform/__init__.py b/lcplatform/__init__.py similarity index 100% rename from platform/__init__.py rename to lcplatform/__init__.py diff --git a/platform/linux.py b/lcplatform/linux.py similarity index 100% rename from platform/linux.py rename to lcplatform/linux.py diff --git a/platform/mac.py b/lcplatform/mac.py similarity index 100% rename from platform/mac.py rename to lcplatform/mac.py diff --git a/lcplatform/win.py b/lcplatform/win.py new file mode 100644 index 0000000..c69fee5 --- /dev/null +++ b/lcplatform/win.py @@ -0,0 +1,122 @@ +# -*- 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('\\', '/')} diff --git a/platform/win.py b/platform/win.py deleted file mode 100644 index cafac64..0000000 --- a/platform/win.py +++ /dev/null @@ -1,76 +0,0 @@ -# -*- coding: utf-8 -*- -"""Windows 截图提供者:交互计划任务(LogonType Interactive)。 -OpenSSH 服务会话(session 0)没有交互桌面,截图/前端输入必须在交互会话执行。 -""" -import os -import subprocess -import time - -USER = os.environ.get('USERNAME', 'yws-hhh') -BASE = os.path.join(os.path.expanduser('~'), 'lc_mcp') -os.makedirs(BASE, exist_ok=True) -OUT_DIR = r'C:\ProgramData\hduty' -os.makedirs(OUT_DIR, exist_ok=True) -JOB_PS1 = os.path.join(BASE, 'job.ps1') -RUN_VBS = os.path.join(BASE, 'run.vbs') -TASK = 'LcMCP' - -if not os.path.exists(RUN_VBS): - with open(RUN_VBS, 'w', encoding='utf-8') as f: - f.write('CreateObject("WScript.Shell").Run "powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%s"", 0, False\n' % JOB_PS1) - - -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 _run_interactive(ps1_code, expect_file, timeout=15): - with open(JOB_PS1, 'w', encoding='utf-8') as f: - f.write('$ErrorActionPreference="Stop"\n' + ps1_code) - try: - if os.path.exists(expect_file): - os.remove(expect_file) - except Exception: - pass - reg = ("Unregister-ScheduledTask -TaskName %s -Confirm:$false -ErrorAction SilentlyContinue; " - "$A=New-ScheduledTaskAction -Execute 'wscript.exe' -Argument '%s'; " - "$P=New-ScheduledTaskPrincipal -UserId '%s' -LogonType Interactive -RunLevel Highest; " - "Register-ScheduledTask -TaskName %s -Action $A -Principal $P -Force | Out-Null; " - "Start-ScheduledTask -TaskName %s; 'OK'" - % (TASK, RUN_VBS.replace("'", "''"), USER, TASK, TASK)) - rc, out, err = _pshell(reg) - 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') as f: - return {"ok": True, "out": f.read().strip()} - except Exception: - return {"ok": True, "out": ''} - time.sleep(0.4) - return {"ok": False, "error": "timeout waiting %s" % expect_file} - - -class WinScreenshotProvider: - - def grab(self): - name = 'shot_%s.png' % time.strftime('%Y%m%d_%H%M%S') - img_path = os.path.join(OUT_DIR, name) - ps = ("Add-Type -AssemblyName System.Windows.Forms,System.Drawing; " - "$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('%s'); $g.Dispose(); $bmp.Dispose(); " - "$f=Join-Path '%s' 'done.txt'; 'DONE' | Set-Content $f" - % (img_path, OUT_DIR)) - r = _run_interactive(ps, os.path.join(OUT_DIR, 'done.txt'), timeout=20) - if not r.get('ok'): - return {'ok': False, 'error': r.get('error', 'screenshot fail')} - if not os.path.exists(img_path): - return {'ok': False, 'error': 'png not produced'} - return {'ok': True, 'path': img_path.replace('\\', '/')} \ No newline at end of file diff --git a/tests/test_dev.py b/tests/test_dev.py index 5c1713c..6e4a83e 100644 --- a/tests/test_dev.py +++ b/tests/test_dev.py @@ -8,7 +8,7 @@ import sys HERE = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, HERE) -from platform import detect_platform, get_input_backend # noqa: E402 +from lcplatform import detect_platform, get_input_backend # noqa: E402 def test_mock_backend():