|
9 | 9 | cua-driver starts again after ending it for idling, where a named one stays ended. A call the daemon can't take, as |
10 | 10 | while it restarts, fails as unavailable; input whose reply is lost fails with its outcome unknown, since it may have |
11 | 11 | arrived. It needs ``agentenv-framework-protocol[computer]`` and ``cua-driver`` where it runs. On a Hyprland desktop |
12 | | -(Wayland) it runs cua-driver's Wayland backend, sends keys and text to the focused window in the foreground, and is |
13 | | -ready, and takes input, only while Hyprland has cua-driver's plugin loaded, asking Hyprland through hyprland.py.""" |
| 12 | +(Wayland) it runs cua-driver's Wayland backend at its own pin, sends text to the desktop and keys to the focused |
| 13 | +window in the foreground, Super and a key a menu holds the keyboard from going to the desktop, and is ready, and takes |
| 14 | +input, only while Hyprland has cua-driver's plugin loaded, asking Hyprland through hyprland.py.""" |
14 | 15 | from __future__ import annotations |
15 | 16 |
|
16 | 17 | import asyncio |
|
77 | 78 | SOCKET = "/run/agentenv/cua-driver.sock" |
78 | 79 | #: What cua-driver's own MCP server (``cua-driver mcp``) says at connect and lists at the pinned version on each OS: its |
79 | 80 | #: instructions, its tools, which the running daemon must list the same, and its skill resources. |
80 | | -MANIFEST = Path(__file__).with_name("mcp-macos.json" if MACOS else "mcp-windows.json" if WINDOWS else "mcp.json") |
| 81 | +MANIFEST = Path(__file__).with_name("mcp-macos.json" if MACOS else "mcp-windows.json" if WINDOWS else |
| 82 | + "mcp-hyprland.json" if HYPRLAND else "mcp.json") |
81 | 83 | #: Where the release's zip is unpacked on Windows, beside this file. |
82 | 84 | WINDOWS_EXECUTABLE = Path(__file__).with_name("cua-driver") / "cua-driver.exe" |
83 | 85 | #: CuaDriver.app's executable, which macOS runs the daemon as. |
84 | 86 | APP_EXECUTABLE = Path("/Applications/CuaDriver.app/Contents/MacOS/cua-driver") |
85 | | -#: The sha256 of the cua-driver executable each arch's pinned wheel installs, on macOS CuaDriver.app's, and on Windows |
86 | | -#: the release zip's. |
| 87 | +#: The sha256 of the cua-driver executable each arch's pinned wheel installs, on macOS CuaDriver.app's, on Windows the |
| 88 | +#: release zip's, and on Hyprland its own pin's wheel's, 0.44, which delivers background input there. |
87 | 89 | EXECUTABLES = { |
88 | 90 | "amd64": "bf57b2f3778a0a7daa9388e241f20ce948b32a7a2f4dd102e497f34e54137934", |
89 | 91 | "arm64": "26e9eee8165e9ce7b614b5e3d68e5d963231385a82b65236406718b5ce1ff936", |
90 | 92 | "macos/arm64": "b4d974a9c1c92a700cd7be7ede987aea4a1e6835e37039ce3d628b71bd5f0ca8", |
91 | 93 | "windows/amd64": "ffa58d88779ee219304b0c307f4cfa970fe822f830c6ecf9254eda5c29e22bf2", |
| 94 | + "hyprland/amd64": "f4259d5135d23d4d03968e074d144a10ece6305ca28ef48edfb3ef5c9e724228", |
92 | 95 | } |
93 | 96 | #: How the daemon delivers the pointer's actions: in the foreground on macOS, where it doesn't by default. |
94 | 97 | FOREGROUND = {"delivery_mode": "foreground"} if MACOS else {} |
@@ -140,6 +143,7 @@ def __init__(self) -> None: |
140 | 143 | self._skill: tuple[str, str, dict[str, str]] | None = None |
141 | 144 | self._texts: dict[str, str] | None = None |
142 | 145 | self._keymap: _Keymap | None = None |
| 146 | + self._reset_bind: dict | None = None |
143 | 147 |
|
144 | 148 | def forwarded_tools(self) -> list[ForwardedTool]: |
145 | 149 | return [ForwardedTool(EnvironmentTool(name=t["name"], description=t["description"], inputSchema=t["inputSchema"]), |
@@ -209,13 +213,53 @@ async def scroll_at(self, x, y, direction, amount, modifiers) -> None: |
209 | 213 | async def type_in(self, text) -> None: |
210 | 214 | if not MACOS and not WINDOWS and not HYPRLAND and any(ord(ch) >= LATIN_1 for ch in text): |
211 | 215 | await asyncio.to_thread(self._hold, text) |
212 | | - await self._send("type_text", text=text, **(await hyprland.focused() if HYPRLAND else {})) |
| 216 | + await self._send("type_text", text=text, **(hyprland.DESKTOP if HYPRLAND else {})) |
213 | 217 |
|
214 | 218 | @press |
215 | 219 | async def press_keys(self, keys) -> None: |
216 | 220 | *held, key = keys |
217 | | - await self._send("press_key", modifiers=_modifiers(held), key=MODIFIERS.get(key) or NAMED.get(key, key), |
218 | | - **(await hyprland.focused() if HYPRLAND else {})) |
| 221 | + modifiers, name = _modifiers(held), MODIFIERS.get(key) or NAMED.get(key, key) |
| 222 | + if HYPRLAND: |
| 223 | + await self._press_on_hyprland(modifiers, name) |
| 224 | + else: |
| 225 | + await self._send("press_key", modifiers=modifiers, key=name) |
| 226 | + |
| 227 | + async def _press_on_hyprland(self, modifiers: list[str], key: str) -> None: |
| 228 | + """A key on Hyprland: to the focused window as a person presses it on a US keyboard, where it edits text as a |
| 229 | + person's does, unless it's Super or held with it, which is Hyprland's own, or nothing is focused, or a layer |
| 230 | + such as a menu holds the keyboard and refuses it there; then to the desktop, punctuation by its keysym name.""" |
| 231 | + if key != "super" and "super" not in modifiers and (window := await hyprland.focused()): |
| 232 | + try: |
| 233 | + return await self._to_window({**hyprland.on_us_keyboard(modifiers, key), **window}) |
| 234 | + except DeliveryFailed as e: |
| 235 | + if hyprland.GRABBED not in e.message: |
| 236 | + raise |
| 237 | + await self._send("press_key", modifiers=modifiers, key=hyprland.PUNCTUATION.get(key, key), |
| 238 | + **hyprland.DESKTOP) |
| 239 | + |
| 240 | + async def _to_window(self, pressed: dict) -> None: |
| 241 | + """A key aimed at a window, the US layout's keymap put back first when cua-driver refuses it for the seat's |
| 242 | + keymap, which an input method changes after desktop text, by a chord bound to nothing but this driver's bind, |
| 243 | + which Hyprland keeps from every window.""" |
| 244 | + try: |
| 245 | + return await self._send("press_key", **pressed) |
| 246 | + except DeliveryFailed as e: |
| 247 | + if hyprland.UNLAID not in e.message: |
| 248 | + raise |
| 249 | + if (bind := await hyprland.reset_bind(self._reset_bind)) is None: |
| 250 | + raise DeliveryFailed(e.message, hint=f"Hyprland has {hyprland.RESET_BIND}, which puts the US keymap back, " |
| 251 | + "bound to something this driver didn't bind, or it couldn't bind " |
| 252 | + "it, so the driver didn't press it") from e |
| 253 | + self._reset_bind = bind |
| 254 | + await self._send("press_key", **hyprland.RESET, **hyprland.DESKTOP) |
| 255 | + deadline = time.monotonic() + hyprland.RESET_S |
| 256 | + while True: |
| 257 | + try: |
| 258 | + return await self._send("press_key", **pressed) |
| 259 | + except DeliveryFailed as e: |
| 260 | + if hyprland.UNLAID not in e.message or time.monotonic() > deadline: |
| 261 | + raise |
| 262 | + await asyncio.sleep(hyprland.RESET_POLL_S) |
219 | 263 |
|
220 | 264 | async def _forward(self, tool: str, acts: bool, /, **arguments: Any) -> CallToolResult: |
221 | 265 | """A call to one of cua-driver's own tools in its caller's session, answered as cua-driver answers it; one that |
@@ -275,7 +319,8 @@ async def _pinned_checks(self) -> list[ReadyCheck]: |
275 | 319 | return [ReadyCheck("cua-driver", False, "its daemon isn't answering yet")] |
276 | 320 | arch = self.platform.arch or "" |
277 | 321 | digest = await asyncio.to_thread(_executable_sha256) |
278 | | - expected = EXECUTABLES.get(f"macos/{arch}" if MACOS else f"windows/{arch}" if WINDOWS else arch) |
| 322 | + expected = EXECUTABLES.get(f"macos/{arch}" if MACOS else f"windows/{arch}" if WINDOWS else |
| 323 | + f"hyprland/{arch}" if HYPRLAND else arch) |
279 | 324 | running = {t["name"]: t["input_schema"] for t in listed} |
280 | 325 | pinned = {t["name"]: t["inputSchema"] for t in _manifest()["tools"]} |
281 | 326 | differ = sorted(name for name in running.keys() | pinned.keys() if running.get(name) != pinned.get(name)) |
|
0 commit comments