# -*- coding: utf-8 -*- """Linux X11 输入后端:xdotool(X 事件注入,xdotool 需已安装)。 物理化升级路径: uinput 后端(虚拟内核 evdev 设备)。 """ import shutil import subprocess import time def _run(args): return subprocess.run(args, capture_output=True, text=True, timeout=30) class XdotoolBackend: def __init__(self): 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} def click(self, x, y, button='left', times=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} 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} def type_text(self, text): _run(['xdotool', 'type', '--delay', '60', text]) return {'ok': True}