"""Cross-platform mouse/keyboard input and window focus. Macros use the module-level helpers, which delegate to a platform backend: Windows : SendInput (accepted by DirectX games where pyautogui is ignored) macOS : Quartz CGEvent Linux : uinput (kernel-level virtual mouse, works on Wayland + X11) Mouse coordinates are absolute screen pixels (top-left origin), matching the coordinate system produced by :mod:`macrolib.screen`. """ import time import sys class InputBackend: """Common interface implemented by every platform backend.""" name = "base" def screen_size(self): """Return ``(width, height)`` of the primary screen in pixels.""" raise NotImplementedError def cursor_position(self): """Return ``(x, y)`` of the cursor.""" raise NotImplementedError def move(self, x, y): """Move the cursor to ``(x, y)`` instantly.""" raise NotImplementedError def glide(self, x, y, steps=5, step_delay=0.012): """Move to ``(x, y)`` in several quick steps so the game sees movement.""" cx, cy = self.cursor_position() for i in range(1, steps + 1): ix = int(round(cx + (x - cx) * i / steps)) iy = int(round(cy + (y - cy) * i / steps)) self.move(ix, iy) time.sleep(step_delay) def mouse_down(self, button="left"): """Press the given mouse button and hold it.""" raise NotImplementedError def mouse_up(self, button="left"): """Release the given mouse button.""" raise NotImplementedError def scroll(self, clicks): """Scroll the mouse wheel. Positive clicks scroll up, negative scroll down.""" raise NotImplementedError def calibrate(self): """(Re)establish a known cursor position, if the backend needs one. No-op by default; the Linux uinput backend overrides it. """ def focus_window(self, *title_fragments): """Bring a window whose title/name contains any fragment to the front. Returns True if a matching window was found and activated. """ raise NotImplementedError # --------------------------------------------------------------------------- # Windows: SendInput # --------------------------------------------------------------------------- class _WindowsBackend(InputBackend): name = "sendinput" INPUT_MOUSE = 0 MOUSEEVENTF_MOVE = 0x0001 MOUSEEVENTF_LEFTDOWN = 0x0002 MOUSEEVENTF_LEFTUP = 0x0004 MOUSEEVENTF_WHEEL = 0x0800 MOUSEEVENTF_ABSOLUTE = 0x8000 WHEEL_DELTA = 120 def __init__(self): import ctypes self._ctypes = ctypes self._user32 = ctypes.windll.user32 class _MOUSEINPUT(ctypes.Structure): _fields_ = [ ("dx", ctypes.c_long), ("dy", ctypes.c_long), ("mouseData", ctypes.c_long), ("dwFlags", ctypes.c_ulong), ("time", ctypes.c_ulong), ("dwExtraInfo", ctypes.c_void_p), ] class _INPUT(ctypes.Structure): class _U(ctypes.Union): _fields_ = [("mi", _MOUSEINPUT)] _anonymous_ = ("u",) _fields_ = [("type", ctypes.c_ulong), ("u", _U)] class _POINT(ctypes.Structure): _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)] self._MOUSEINPUT = _MOUSEINPUT self._INPUT = _INPUT self._POINT = _POINT self._user32.SendInput.argtypes = ( ctypes.c_uint, ctypes.POINTER(_INPUT), ctypes.c_int, ) self._user32.SendInput.restype = ctypes.c_uint def _send(self, flags, dx=0, dy=0, data=0): event = self._INPUT() event.type = self.INPUT_MOUSE event.mi = self._MOUSEINPUT(dx, dy, data, flags, 0, None) return self._user32.SendInput( 1, self._ctypes.byref(event), self._ctypes.sizeof(self._INPUT) ) def screen_size(self): return self._user32.GetSystemMetrics(0), self._user32.GetSystemMetrics(1) def cursor_position(self): p = self._POINT() self._user32.GetCursorPos(self._ctypes.byref(p)) return p.x, p.y def move(self, x, y): screen_w, screen_h = self.screen_size() if screen_w > 1 and screen_h > 1: nx = int(x * 65535 / (screen_w - 1)) ny = int(y * 65535 / (screen_h - 1)) else: nx, ny = x, y self._send(self.MOUSEEVENTF_MOVE | self.MOUSEEVENTF_ABSOLUTE, nx, ny) def mouse_down(self, button="left"): self._send(self.MOUSEEVENTF_LEFTDOWN) def mouse_up(self, button="left"): self._send(self.MOUSEEVENTF_LEFTUP) def scroll(self, clicks): delta = int(clicks * self.WHEEL_DELTA) self._send(self.MOUSEEVENTF_WHEEL, data=delta) def focus_window(self, *title_fragments): user32 = self._user32 ctypes = self._ctypes frags = [f.lower() for f in title_fragments] hwnds = [] def enum_callback(hwnd, lparam): length = user32.GetWindowTextLengthW(hwnd) if length > 0: buf = ctypes.create_unicode_buffer(length + 1) user32.GetWindowTextW(hwnd, buf, length + 1) title = buf.value.lower() if any(f in title for f in frags): hwnds.append(hwnd) return True WNDENUMPROC = ctypes.WINFUNCTYPE( ctypes.c_bool, ctypes.c_void_p, ctypes.c_void_p ) try: user32.EnumWindows(WNDENUMPROC(enum_callback), 0) except Exception: return False if not hwnds: return False hwnd = hwnds[0] if user32.IsIconic(hwnd): user32.ShowWindow(hwnd, 9) # SW_RESTORE if user32.GetForegroundWindow() != hwnd: try: fg = user32.GetForegroundWindow() cur_tid = user32.GetWindowThreadProcessId(fg, None) tgt_tid = user32.GetWindowThreadProcessId(hwnd, None) user32.AttachThreadInput(cur_tid, tgt_tid, True) user32.SetForegroundWindow(hwnd) user32.BringWindowToTop(hwnd) user32.AttachThreadInput(cur_tid, tgt_tid, False) except Exception: user32.SetForegroundWindow(hwnd) for _ in range(15): if user32.GetForegroundWindow() == hwnd: break time.sleep(0.03) return user32.GetForegroundWindow() == hwnd # --------------------------------------------------------------------------- # macOS: Quartz CGEvent # --------------------------------------------------------------------------- class _MacBackend(InputBackend): name = "quartz" def __init__(self): import Quartz self._Q = Quartz def screen_size(self): return ( self._Q.CGDisplayPixelsWide(self._Q.CGMainDisplayID()), self._Q.CGDisplayPixelsHigh(self._Q.CGMainDisplayID()), ) def cursor_position(self): loc = self._Q.CGEventGetLocation(self._Q.CGEventCreate(None)) return loc.x, loc.y def _post(self, event_type, x, y): e = self._Q.CGEventCreateMouseEvent( None, event_type, (x, y), self._Q.kCGMouseButtonLeft ) self._Q.CGEventPost(self._Q.kCGHIDEventTap, e) def move(self, x, y): self._post(self._Q.kCGEventMouseMoved, x, y) def mouse_down(self, button="left"): x, y = self.cursor_position() self._post(self._Q.kCGEventLeftMouseDown, x, y) def mouse_up(self, button="left"): x, y = self.cursor_position() self._post(self._Q.kCGEventLeftMouseUp, x, y) def scroll(self, clicks): e = self._Q.CGEventCreateScrollWheelEvent( None, self._Q.kCGScrollEventUnitLine, 1, int(clicks) ) self._Q.CGEventPost(self._Q.kCGHIDEventTap, e) def focus_window(self, *title_fragments): try: import AppKit frags = [f.lower() for f in title_fragments] ws = AppKit.NSWorkspace.sharedWorkspace() for app in ws.runningApplications(): name = (app.localizedName() or "").lower() if any(f in name for f in frags): app.activateWithOptions_( AppKit.NSApplicationActivateIgnoringOtherApps ) return True return False except Exception: return False # --------------------------------------------------------------------------- # Linux: uinput (kernel-level virtual mouse, works on Wayland + X11) # --------------------------------------------------------------------------- class _LinuxUinputBackend(InputBackend): name = "uinput" def __init__(self): from evdev import AbsInfo, UInput, ecodes from . import _linux self._e = ecodes w, h = _linux.wayland_output_size() or (1920, 1080) self._w, self._h = w, h self._dev = UInput( { ecodes.EV_KEY: [ ecodes.BTN_LEFT, ecodes.BTN_RIGHT, ecodes.BTN_MIDDLE, # BTN_TOUCH makes the compositor treat the absolute axes as # an absolute pointer (absolute positioning only works when # this capability is present). ecodes.BTN_TOUCH, ], ecodes.EV_ABS: [ (ecodes.ABS_X, AbsInfo(0, 0, w, 0, 0, 0)), (ecodes.ABS_Y, AbsInfo(0, 0, h, 0, 0, 0)), ], ecodes.EV_REL: [ ecodes.REL_WHEEL, ], }, name="macrolib-virtual-mouse", version=0x3, ) self._x, self._y = w // 2, h // 2 time.sleep(0.3) # let the compositor pick up the new device def screen_size(self): return self._w, self._h def cursor_position(self): return self._x, self._y def calibrate(self): # Absolute axes map 1:1, so there is nothing to recalibrate. pass def move(self, x, y): x, y = int(x), int(y) self._dev.write(self._e.EV_ABS, self._e.ABS_X, x) self._dev.write(self._e.EV_ABS, self._e.ABS_Y, y) self._dev.write(self._e.EV_SYN, self._e.SYN_REPORT, 0) self._x, self._y = x, y def _button_code(self, button): return { "left": self._e.BTN_LEFT, "right": self._e.BTN_RIGHT, "middle": self._e.BTN_MIDDLE, }[button] def mouse_down(self, button="left"): self._dev.write(self._e.EV_KEY, self._button_code(button), 1) self._dev.write(self._e.EV_SYN, self._e.SYN_REPORT, 0) def mouse_up(self, button="left"): self._dev.write(self._e.EV_KEY, self._button_code(button), 0) self._dev.write(self._e.EV_SYN, self._e.SYN_REPORT, 0) def scroll(self, clicks): self._dev.write(self._e.EV_REL, self._e.REL_WHEEL, int(clicks)) self._dev.write(self._e.EV_SYN, self._e.SYN_REPORT, 0) def focus_window(self, *title_fragments): # Wayland does not allow a client to raise another client's window. return False # --------------------------------------------------------------------------- # Linux / generic: pyautogui fallback (X11) # --------------------------------------------------------------------------- class _PyAutoGuiBackend(InputBackend): name = "pyautogui" def __init__(self): import pyautogui pyautogui.FAILSAFE = True pyautogui.PAUSE = 0 self._pyautogui = pyautogui def screen_size(self): w, h = self._pyautogui.size() return w, h def cursor_position(self): return self._pyautogui.position() def move(self, x, y): self._pyautogui.moveTo(x, y) def glide(self, x, y, steps=5, step_delay=0.012): self._pyautogui.moveTo( x, y, duration=step_delay * steps, tween=self._pyautogui.easeOutQuad ) def mouse_down(self, button="left"): self._pyautogui.mouseDown(button=button) def mouse_up(self, button="left"): self._pyautogui.mouseUp(button=button) def scroll(self, clicks): self._pyautogui.scroll(int(clicks)) def focus_window(self, *title_fragments): return False # --------------------------------------------------------------------------- # Selection + module-level API # --------------------------------------------------------------------------- _backend = None def get_backend(): global _backend if _backend is None: if sys.platform == "win32": _backend = _WindowsBackend() elif sys.platform == "darwin": _backend = _MacBackend() else: try: _backend = _LinuxUinputBackend() except Exception: _backend = _PyAutoGuiBackend() return _backend def screen_size(): return get_backend().screen_size() def cursor_position(): return get_backend().cursor_position() def move(x, y): get_backend().move(x, y) def glide(x, y, steps=5, step_delay=0.012): get_backend().glide(x, y, steps=steps, step_delay=step_delay) def mouse_down(button="left"): get_backend().mouse_down(button=button) def mouse_up(button="left"): get_backend().mouse_up(button=button) def scroll(clicks): get_backend().scroll(clicks) def scroll_at(x, y, clicks, delay=0.05): """Move cursor to (x, y) and scroll. Positive clicks scroll up, negative scroll down.""" move(x, y) if delay: time.sleep(delay) scroll(clicks) def calibrate(): get_backend().calibrate() def focus_window(*title_fragments): return get_backend().focus_window(*title_fragments) def backend_name(): return get_backend().name