git.lucas.co / cce-designer
graphic design tool
git clone https://git.lucas.co/cce-designer.git

scratch/find_click_offset.py (2.1K)

 1 import subprocess
 2 import time
 3 import re
 4 import sys
 5 import os
 6 
 7 def run_clearctl(cmd_str):
 8     full_cmd = f"env WAYLAND_DISPLAY=wayland-0 XDG_RUNTIME_DIR=/run/user/1000 /home/lsgalante/.local/bin/clearctl {cmd_str}"
 9     res = subprocess.run(full_cmd, shell=True, capture_output=True, text=True)
10     return res.stdout.strip()
11 
12 def get_window_rect(title):
13     stdout = run_clearctl("windows")
14     for line in stdout.splitlines():
15         if title in line:
16             m = re.search(r'\bx=(\d+)\s+y=(\d+)\s+w=(\d+)\s+h=(\d+)', line)
17             if m:
18                 return int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))
19     return None
20 
21 log_path = "/home/lsgalante/Dropbox/Clear/design-interface.log"
22 
23 def clear_log():
24     if os.path.exists(log_path):
25         with open(log_path, "w") as f:
26             f.write("")
27 
28 def check_log_for_press():
29     if not os.path.exists(log_path):
30         return False
31     with open(log_path, "r", errors="ignore") as f:
32         content = f.read()
33     return "DEBUG" in content
34 
35 title = "Clear Design Interface"
36 rect = get_window_rect(title)
37 if not rect:
38     print(f"Error: Could not find window '{title}'", file=sys.stderr)
39     sys.exit(1)
40 
41 window_x, window_y, window_w, window_h = rect
42 print(f"Window rect: x={window_x}, y={window_y}, w={window_w}, h={window_h}")
43 
44 # Focus the window first
45 run_clearctl(f"focus-window \"{title}\"")
46 time.sleep(0.5)
47 
48 # We will sweep dx from 50 to 200, dy from 0 to 100, in steps of 20
49 found = False
50 for dy in range(0, 150, 10):
51     for dx in range(50, 250, 20):
52         clear_log()
53         target_x = window_x + dx
54         target_y = window_y + dy
55         print(f"Testing client offset dx={dx}, dy={dy} -> screen ({target_x}, {target_y})")
56         
57         run_clearctl(f"pointer-move-to {target_x} {target_y}")
58         time.sleep(0.1)
59         run_clearctl("pointer-click left")
60         time.sleep(0.2)
61         
62         if check_log_for_press():
63             print(f"SUCCESS! Click registered at offset dx={dx}, dy={dy}")
64             found = True
65             break
66     if found:
67         break
68 
69 if not found:
70     print("Failed to register any clicks inside the window.")