@@ -0,0 +1,105 @@
1+#!/usr/bin/env python3
2+"""Focus one KWin window, then press keys and click in it through a short-lived uinput device.
3+
4+Usage: key.py <window-uuid> <step> [...]
5+A step is a combo or key name (alt+o, ctrl+v, enter, space, down), click:X,Y or move:X,Y in
6+global coordinates, or text to type, in lowercase letters, digits and spaces.
7+It stops if the window loses the focus, so nothing lands in another window.
8+"""
9+import subprocess
10+import sys
11+import tempfile
12+import time
13+
14+from evdev import UInput, ecodes as e
15+
16+NAMES = {"alt": "LEFTALT", "ctrl": "LEFTCTRL", "shift": "LEFTSHIFT", "super": "LEFTMETA", "esc": "ESC"}
17+KEYS = {"enter", "space", "tab", "esc", "up", "down", "left", "right", "backspace"}
18+SCRIPT = 'print("%s " + workspace.cursorPos.x + " " + workspace.cursorPos.y + " " + (workspace.activeWindow ? workspace.activeWindow.internalId : ""));'
19+
20+
21+def qdbus(path, method, *args):
22+ return subprocess.run(["qdbus6", "org.kde.KWin", path, method, *args], check=True, capture_output=True, text=True).stdout.strip()
23+
24+
25+def state():
26+ """The pointer's position and the active window, from a KWin script that prints them."""
27+ token = f"KDECU{time.time_ns()}"
28+ since = int(time.time()) - 1
29+ with tempfile.NamedTemporaryFile("w", suffix=".js") as script:
30+ script.write(SCRIPT % token)
31+ script.flush()
32+ number = qdbus("/Scripting", "org.kde.kwin.Scripting.loadScript", script.name, "kdecu")
33+ qdbus(f"/Scripting/Script{number}", "org.kde.kwin.Script.run")
34+ qdbus("/Scripting", "org.kde.kwin.Scripting.unloadScript", "kdecu")
35+ for _ in range(40):
36+ log = subprocess.run(["journalctl", "--user", "--since", f"@{since}", "-o", "cat", "--grep", token], capture_output=True, text=True).stdout
37+ if token in log:
38+ _, x, y, *window = log.strip().splitlines()[-1].split()
39+ return int(x), int(y), window[0] if window else ""
40+ time.sleep(0.05)
41+ raise SystemExit("key.py: KWin did not answer")
42+
43+
44+def code(name):
45+ return getattr(e, "KEY_" + NAMES.get(name, name).upper())
46+
47+
48+def press(device, codes):
49+ for key in codes:
50+ device.write(e.EV_KEY, key, 1)
51+ device.syn()
52+ time.sleep(0.03)
53+ for key in reversed(codes):
54+ device.write(e.EV_KEY, key, 0)
55+ device.syn()
56+ time.sleep(0.03)
57+
58+
59+def travel(device, axis, distance):
60+ for _ in range(abs(distance) // 2):
61+ device.write(e.EV_REL, axis, 2 if distance > 0 else -2)
62+ device.syn()
63+ time.sleep(0.008) # At this pace one unit is one pixel. Faster, the acceleration overshoots.
64+
65+
66+def move(device, x, y):
67+ """The pointer is relative, so it moves, looks where it landed and corrects."""
68+ for _ in range(6):
69+ here_x, here_y, _window = state()
70+ if abs(here_x - x) <= 3 and abs(here_y - y) <= 3:
71+ return
72+ travel(device, e.REL_X, x - here_x)
73+ travel(device, e.REL_Y, y - here_y)
74+ time.sleep(0.05)
75+ raise SystemExit(f"key.py: the pointer stopped at {state()[:2]}, not {x},{y}")
76+
77+
78+def main():
79+ window, steps = sys.argv[1], sys.argv[2:]
80+ capabilities = [v for k, v in vars(e).items() if k.startswith("KEY_") and isinstance(v, int) and v < 249]
81+ events = {e.EV_KEY: capabilities + [e.BTN_LEFT], e.EV_REL: [e.REL_X, e.REL_Y]}
82+ with UInput(events, name="kde-computer-use") as device:
83+ time.sleep(0.6) # The compositor has to see the new device first.
84+ subprocess.run(
85+ ["busctl", "--user", "call", "org.kde.KWin", "/WindowsRunner", "org.kde.krunner1", "Run", "ss", f"0_{window}", ""],
86+ check=True,
87+ )
88+ time.sleep(0.2)
89+ for step in steps:
90+ if state()[2] != window:
91+ raise SystemExit(f"key.py: {window} lost the focus, stopped before {step}")
92+ if step.startswith(("move:", "click:")):
93+ action, place = step.split(":")
94+ move(device, *map(int, place.split(",")))
95+ if action == "click":
96+ press(device, [e.BTN_LEFT])
97+ elif "+" in step or step in KEYS:
98+ press(device, [code(part) for part in step.split("+")])
99+ else:
100+ for char in step:
101+ press(device, [e.KEY_SPACE if char == " " else code(char)])
102+ time.sleep(0.05)
103+
104+
105+main()