bingocode 1.1.62 → 1.1.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -1
- package/runtime/mac_helper.py +775 -0
- package/runtime/requirements-win.txt +7 -0
- package/runtime/requirements.txt +6 -0
- package/runtime/test_helpers.py +322 -0
- package/runtime/win_helper.py +723 -0
- package/src/manager/CliMenuManager.tsx +0 -1
- package/src/server/ensureSingletonLocalServer.ts +160 -256
|
@@ -0,0 +1,723 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Windows Computer Use helper — same JSON protocol as mac_helper.py.
|
|
3
|
+
|
|
4
|
+
Uses win32gui / win32api / win32process / psutil / pyperclip / screeninfo
|
|
5
|
+
to replicate macOS-specific Quartz/AppKit functionality on Windows.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import argparse
|
|
10
|
+
import base64
|
|
11
|
+
import json
|
|
12
|
+
import os
|
|
13
|
+
import subprocess
|
|
14
|
+
import sys
|
|
15
|
+
import time
|
|
16
|
+
from io import BytesIO
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
import mss
|
|
21
|
+
from PIL import Image
|
|
22
|
+
|
|
23
|
+
os.environ.setdefault("PYTHONDONTWRITEBYTECODE", "1")
|
|
24
|
+
os.environ.setdefault("PYAUTOGUI_HIDE_SUPPORT_PROMPT", "1")
|
|
25
|
+
|
|
26
|
+
import pyautogui # noqa: E402
|
|
27
|
+
|
|
28
|
+
pyautogui.FAILSAFE = False
|
|
29
|
+
pyautogui.PAUSE = 0
|
|
30
|
+
|
|
31
|
+
# ---------------------------------------------------------------------------
|
|
32
|
+
# Key mapping — Windows uses 'win' instead of 'command'
|
|
33
|
+
# ---------------------------------------------------------------------------
|
|
34
|
+
KEY_MAP = {
|
|
35
|
+
"a": "a", "b": "b", "c": "c", "d": "d", "e": "e",
|
|
36
|
+
"f": "f", "g": "g", "h": "h", "i": "i", "j": "j",
|
|
37
|
+
"k": "k", "l": "l", "m": "m", "n": "n", "o": "o",
|
|
38
|
+
"p": "p", "q": "q", "r": "r", "s": "s", "t": "t",
|
|
39
|
+
"u": "u", "v": "v", "w": "w", "x": "x", "y": "y",
|
|
40
|
+
"z": "z",
|
|
41
|
+
"0": "0", "1": "1", "2": "2", "3": "3", "4": "4",
|
|
42
|
+
"5": "5", "6": "6", "7": "7", "8": "8", "9": "9",
|
|
43
|
+
# Modifier keys — map macOS names to Windows equivalents
|
|
44
|
+
"cmd": "win",
|
|
45
|
+
"command": "win",
|
|
46
|
+
"meta": "win",
|
|
47
|
+
"super": "win",
|
|
48
|
+
"ctrl": "ctrl",
|
|
49
|
+
"control": "ctrl",
|
|
50
|
+
"shift": "shift",
|
|
51
|
+
"alt": "alt",
|
|
52
|
+
"option": "alt",
|
|
53
|
+
"opt": "alt",
|
|
54
|
+
"fn": "fn",
|
|
55
|
+
# Navigation / editing
|
|
56
|
+
"escape": "esc",
|
|
57
|
+
"esc": "esc",
|
|
58
|
+
"enter": "enter",
|
|
59
|
+
"return": "enter",
|
|
60
|
+
"tab": "tab",
|
|
61
|
+
"space": "space",
|
|
62
|
+
"backspace": "backspace",
|
|
63
|
+
"delete": "delete",
|
|
64
|
+
"forwarddelete": "delete",
|
|
65
|
+
"up": "up",
|
|
66
|
+
"down": "down",
|
|
67
|
+
"left": "left",
|
|
68
|
+
"right": "right",
|
|
69
|
+
"home": "home",
|
|
70
|
+
"end": "end",
|
|
71
|
+
"pageup": "pageup",
|
|
72
|
+
"pagedown": "pagedown",
|
|
73
|
+
"capslock": "capslock",
|
|
74
|
+
# Function keys
|
|
75
|
+
"f1": "f1", "f2": "f2", "f3": "f3", "f4": "f4",
|
|
76
|
+
"f5": "f5", "f6": "f6", "f7": "f7", "f8": "f8",
|
|
77
|
+
"f9": "f9", "f10": "f10", "f11": "f11", "f12": "f12",
|
|
78
|
+
# Symbols
|
|
79
|
+
"-": "-", "=": "=", "[": "[", "]": "]", "\\": "\\",
|
|
80
|
+
";": ";", "'": "'", ",": ",", ".": ".", "/": "/", "`": "`",
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def normalize_key(name: str) -> str:
|
|
85
|
+
key = name.strip().lower()
|
|
86
|
+
if key not in KEY_MAP:
|
|
87
|
+
raise ValueError(f"Unsupported key: {name}")
|
|
88
|
+
return KEY_MAP[key]
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
# ---------------------------------------------------------------------------
|
|
92
|
+
# JSON output helpers
|
|
93
|
+
# ---------------------------------------------------------------------------
|
|
94
|
+
|
|
95
|
+
def json_output(payload: dict[str, Any]) -> None:
|
|
96
|
+
sys.stdout.write(json.dumps(payload, ensure_ascii=False))
|
|
97
|
+
sys.stdout.write("\n")
|
|
98
|
+
sys.stdout.flush()
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def error_output(message: str, code: str = "runtime_error") -> None:
|
|
102
|
+
json_output({"ok": False, "error": {"code": code, "message": message}})
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def bool_env(name: str, default: bool = False) -> bool:
|
|
106
|
+
value = os.environ.get(name)
|
|
107
|
+
if value is None:
|
|
108
|
+
return default
|
|
109
|
+
return value not in {"0", "false", "False", ""}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
# ---------------------------------------------------------------------------
|
|
113
|
+
# Display / Monitor helpers (via screeninfo + ctypes)
|
|
114
|
+
# ---------------------------------------------------------------------------
|
|
115
|
+
|
|
116
|
+
def get_displays() -> list[dict[str, Any]]:
|
|
117
|
+
"""Enumerate monitors via screeninfo, with DPI scale from ctypes."""
|
|
118
|
+
from screeninfo import get_monitors
|
|
119
|
+
|
|
120
|
+
displays: list[dict[str, Any]] = []
|
|
121
|
+
for idx, m in enumerate(get_monitors()):
|
|
122
|
+
scale_factor = _get_monitor_scale(m)
|
|
123
|
+
name = m.name or f"Display {idx + 1}"
|
|
124
|
+
displays.append({
|
|
125
|
+
"id": idx,
|
|
126
|
+
"displayId": idx,
|
|
127
|
+
"width": m.width,
|
|
128
|
+
"height": m.height,
|
|
129
|
+
"scaleFactor": scale_factor,
|
|
130
|
+
"originX": m.x,
|
|
131
|
+
"originY": m.y,
|
|
132
|
+
"isPrimary": m.is_primary if hasattr(m, "is_primary") else (idx == 0),
|
|
133
|
+
"name": name,
|
|
134
|
+
"label": name,
|
|
135
|
+
})
|
|
136
|
+
return displays
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _get_monitor_scale(monitor: Any) -> float:
|
|
140
|
+
"""Get the DPI scale factor for a monitor. Returns 1.0 on failure."""
|
|
141
|
+
try:
|
|
142
|
+
import ctypes
|
|
143
|
+
# SetProcessDPIAware so we get real pixel values
|
|
144
|
+
ctypes.windll.user32.SetProcessDPIAware()
|
|
145
|
+
# Get DPI for the primary — simplified; per-monitor DPI is complex
|
|
146
|
+
hdc = ctypes.windll.user32.GetDC(0)
|
|
147
|
+
dpi = ctypes.windll.gdi32.GetDeviceCaps(hdc, 88) # LOGPIXELSX
|
|
148
|
+
ctypes.windll.user32.ReleaseDC(0, hdc)
|
|
149
|
+
return dpi / 96.0
|
|
150
|
+
except Exception:
|
|
151
|
+
return 1.0
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def choose_display(display_id: int | None) -> dict[str, Any]:
|
|
155
|
+
displays = get_displays()
|
|
156
|
+
if not displays:
|
|
157
|
+
raise RuntimeError("No active displays found")
|
|
158
|
+
if display_id is None:
|
|
159
|
+
for display in displays:
|
|
160
|
+
if display["isPrimary"]:
|
|
161
|
+
return display
|
|
162
|
+
return displays[0]
|
|
163
|
+
for display in displays:
|
|
164
|
+
if display["displayId"] == display_id or display["id"] == display_id:
|
|
165
|
+
return display
|
|
166
|
+
raise RuntimeError(f"Unknown display: {display_id}")
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
# ---------------------------------------------------------------------------
|
|
170
|
+
# Screen capture (mss — cross-platform, identical to mac_helper)
|
|
171
|
+
# ---------------------------------------------------------------------------
|
|
172
|
+
|
|
173
|
+
def capture_display(display_id: int | None, resize: tuple[int, int] | None = None) -> dict[str, Any]:
|
|
174
|
+
display = choose_display(display_id)
|
|
175
|
+
monitor = {
|
|
176
|
+
"left": display["originX"],
|
|
177
|
+
"top": display["originY"],
|
|
178
|
+
"width": display["width"],
|
|
179
|
+
"height": display["height"],
|
|
180
|
+
}
|
|
181
|
+
with mss.mss() as sct:
|
|
182
|
+
raw = sct.grab(monitor)
|
|
183
|
+
image = Image.frombytes("RGB", raw.size, raw.rgb)
|
|
184
|
+
if resize:
|
|
185
|
+
image = image.resize(resize, Image.Resampling.LANCZOS)
|
|
186
|
+
buffer = BytesIO()
|
|
187
|
+
image.save(buffer, format="JPEG", quality=75, optimize=True)
|
|
188
|
+
base64_data = base64.b64encode(buffer.getvalue()).decode("ascii")
|
|
189
|
+
return {
|
|
190
|
+
"base64": base64_data,
|
|
191
|
+
"width": image.width,
|
|
192
|
+
"height": image.height,
|
|
193
|
+
"displayWidth": display["width"],
|
|
194
|
+
"displayHeight": display["height"],
|
|
195
|
+
"displayId": display["displayId"],
|
|
196
|
+
"originX": display["originX"],
|
|
197
|
+
"originY": display["originY"],
|
|
198
|
+
"display": display,
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def capture_region(region: dict[str, int], resize: tuple[int, int] | None = None) -> dict[str, Any]:
|
|
203
|
+
with mss.mss() as sct:
|
|
204
|
+
raw = sct.grab(region)
|
|
205
|
+
image = Image.frombytes("RGB", raw.size, raw.rgb)
|
|
206
|
+
if resize:
|
|
207
|
+
image = image.resize(resize, Image.Resampling.LANCZOS)
|
|
208
|
+
buffer = BytesIO()
|
|
209
|
+
image.save(buffer, format="JPEG", quality=75, optimize=True)
|
|
210
|
+
base64_data = base64.b64encode(buffer.getvalue()).decode("ascii")
|
|
211
|
+
return {"base64": base64_data, "width": image.width, "height": image.height}
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
# ---------------------------------------------------------------------------
|
|
215
|
+
# Window management (win32gui)
|
|
216
|
+
# ---------------------------------------------------------------------------
|
|
217
|
+
|
|
218
|
+
def list_windows() -> list[dict[str, Any]]:
|
|
219
|
+
"""List visible on-screen windows with their bounds."""
|
|
220
|
+
import win32gui
|
|
221
|
+
|
|
222
|
+
results: list[dict[str, Any]] = []
|
|
223
|
+
|
|
224
|
+
def _enum_cb(hwnd: int, _: Any) -> None:
|
|
225
|
+
if not win32gui.IsWindowVisible(hwnd):
|
|
226
|
+
return
|
|
227
|
+
title = win32gui.GetWindowText(hwnd)
|
|
228
|
+
try:
|
|
229
|
+
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
|
|
230
|
+
except Exception:
|
|
231
|
+
return
|
|
232
|
+
width = right - left
|
|
233
|
+
height = bottom - top
|
|
234
|
+
if width <= 1 or height <= 1:
|
|
235
|
+
return
|
|
236
|
+
# Get the process name as owner
|
|
237
|
+
owner = _get_window_process_name(hwnd)
|
|
238
|
+
results.append({
|
|
239
|
+
"ownerName": owner,
|
|
240
|
+
"title": title,
|
|
241
|
+
"bounds": {"x": left, "y": top, "width": width, "height": height},
|
|
242
|
+
})
|
|
243
|
+
|
|
244
|
+
win32gui.EnumWindows(_enum_cb, None)
|
|
245
|
+
return results
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def _get_window_process_name(hwnd: int) -> str:
|
|
249
|
+
"""Get the exe name of the process owning a window handle."""
|
|
250
|
+
try:
|
|
251
|
+
import win32process
|
|
252
|
+
import psutil
|
|
253
|
+
_, pid = win32process.GetWindowThreadProcessId(hwnd)
|
|
254
|
+
proc = psutil.Process(pid)
|
|
255
|
+
return proc.name()
|
|
256
|
+
except Exception:
|
|
257
|
+
return ""
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
# ---------------------------------------------------------------------------
|
|
261
|
+
# Application management
|
|
262
|
+
# ---------------------------------------------------------------------------
|
|
263
|
+
|
|
264
|
+
def _get_exe_path_for_pid(pid: int) -> str | None:
|
|
265
|
+
try:
|
|
266
|
+
import psutil
|
|
267
|
+
return psutil.Process(pid).exe()
|
|
268
|
+
except Exception:
|
|
269
|
+
return None
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def installed_apps() -> list[dict[str, Any]]:
|
|
273
|
+
"""List installed programs from Windows registry and Start Menu shortcuts."""
|
|
274
|
+
import winreg
|
|
275
|
+
|
|
276
|
+
results: dict[str, dict[str, Any]] = {}
|
|
277
|
+
reg_paths = [
|
|
278
|
+
(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"),
|
|
279
|
+
(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"),
|
|
280
|
+
(winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"),
|
|
281
|
+
]
|
|
282
|
+
|
|
283
|
+
for hive, sub_key in reg_paths:
|
|
284
|
+
try:
|
|
285
|
+
key = winreg.OpenKey(hive, sub_key)
|
|
286
|
+
except OSError:
|
|
287
|
+
continue
|
|
288
|
+
try:
|
|
289
|
+
i = 0
|
|
290
|
+
while True:
|
|
291
|
+
try:
|
|
292
|
+
name = winreg.EnumKey(key, i)
|
|
293
|
+
i += 1
|
|
294
|
+
except OSError:
|
|
295
|
+
break
|
|
296
|
+
try:
|
|
297
|
+
app_key = winreg.OpenKey(key, name)
|
|
298
|
+
except OSError:
|
|
299
|
+
continue
|
|
300
|
+
try:
|
|
301
|
+
display_name = winreg.QueryValueEx(app_key, "DisplayName")[0]
|
|
302
|
+
except OSError:
|
|
303
|
+
winreg.CloseKey(app_key)
|
|
304
|
+
continue
|
|
305
|
+
# Use the registry key name as a stable identifier (like bundleId)
|
|
306
|
+
try:
|
|
307
|
+
install_location = winreg.QueryValueEx(app_key, "InstallLocation")[0]
|
|
308
|
+
except OSError:
|
|
309
|
+
install_location = ""
|
|
310
|
+
try:
|
|
311
|
+
display_icon = winreg.QueryValueEx(app_key, "DisplayIcon")[0]
|
|
312
|
+
except OSError:
|
|
313
|
+
display_icon = ""
|
|
314
|
+
# Use registry key name as bundleId equivalent
|
|
315
|
+
bundle_id = name
|
|
316
|
+
if bundle_id not in results:
|
|
317
|
+
results[bundle_id] = {
|
|
318
|
+
"bundleId": bundle_id,
|
|
319
|
+
"displayName": str(display_name),
|
|
320
|
+
"path": str(install_location or display_icon or ""),
|
|
321
|
+
}
|
|
322
|
+
winreg.CloseKey(app_key)
|
|
323
|
+
finally:
|
|
324
|
+
winreg.CloseKey(key)
|
|
325
|
+
|
|
326
|
+
return sorted(results.values(), key=lambda item: item["displayName"].lower())
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def running_apps() -> list[dict[str, Any]]:
|
|
330
|
+
"""List running GUI applications."""
|
|
331
|
+
import psutil
|
|
332
|
+
|
|
333
|
+
apps: list[dict[str, Any]] = []
|
|
334
|
+
seen: set[str] = set()
|
|
335
|
+
|
|
336
|
+
for proc in psutil.process_iter(["pid", "name", "exe"]):
|
|
337
|
+
try:
|
|
338
|
+
name = proc.info["name"] or ""
|
|
339
|
+
exe_path = proc.info["exe"] or ""
|
|
340
|
+
if not name or name in seen:
|
|
341
|
+
continue
|
|
342
|
+
# Skip system/background processes (no window)
|
|
343
|
+
if not exe_path:
|
|
344
|
+
continue
|
|
345
|
+
seen.add(name)
|
|
346
|
+
# Use exe name (without .exe) as bundleId
|
|
347
|
+
bundle_id = Path(exe_path).stem if exe_path else name
|
|
348
|
+
apps.append({"bundleId": bundle_id, "displayName": name})
|
|
349
|
+
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
|
350
|
+
continue
|
|
351
|
+
|
|
352
|
+
return sorted(apps, key=lambda item: item["displayName"].lower())
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
def app_display_name(bundle_id: str) -> str | None:
|
|
356
|
+
"""Find display name for a given bundleId (exe stem or registry key)."""
|
|
357
|
+
import psutil
|
|
358
|
+
for proc in psutil.process_iter(["name", "exe"]):
|
|
359
|
+
try:
|
|
360
|
+
exe = proc.info["exe"] or ""
|
|
361
|
+
if exe and Path(exe).stem == bundle_id:
|
|
362
|
+
return proc.info["name"]
|
|
363
|
+
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
|
364
|
+
continue
|
|
365
|
+
return None
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def frontmost_app() -> dict[str, str] | None:
|
|
369
|
+
"""Get the currently focused (foreground) application."""
|
|
370
|
+
import win32gui
|
|
371
|
+
import win32process
|
|
372
|
+
import psutil
|
|
373
|
+
|
|
374
|
+
hwnd = win32gui.GetForegroundWindow()
|
|
375
|
+
if not hwnd:
|
|
376
|
+
return None
|
|
377
|
+
try:
|
|
378
|
+
_, pid = win32process.GetWindowThreadProcessId(hwnd)
|
|
379
|
+
proc = psutil.Process(pid)
|
|
380
|
+
exe_path = proc.exe()
|
|
381
|
+
return {
|
|
382
|
+
"bundleId": Path(exe_path).stem,
|
|
383
|
+
"displayName": proc.name(),
|
|
384
|
+
}
|
|
385
|
+
except Exception:
|
|
386
|
+
return None
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
def app_under_point(x: int, y: int) -> dict[str, str] | None:
|
|
390
|
+
"""Find the app whose window is under the given screen coordinate."""
|
|
391
|
+
import win32gui
|
|
392
|
+
import win32process
|
|
393
|
+
import psutil
|
|
394
|
+
|
|
395
|
+
hwnd = win32gui.WindowFromPoint((x, y))
|
|
396
|
+
if not hwnd:
|
|
397
|
+
return frontmost_app()
|
|
398
|
+
# Walk up to the top-level owner
|
|
399
|
+
root = win32gui.GetAncestor(hwnd, 3) # GA_ROOTOWNER = 3
|
|
400
|
+
if root:
|
|
401
|
+
hwnd = root
|
|
402
|
+
try:
|
|
403
|
+
_, pid = win32process.GetWindowThreadProcessId(hwnd)
|
|
404
|
+
proc = psutil.Process(pid)
|
|
405
|
+
exe_path = proc.exe()
|
|
406
|
+
return {
|
|
407
|
+
"bundleId": Path(exe_path).stem,
|
|
408
|
+
"displayName": proc.name(),
|
|
409
|
+
}
|
|
410
|
+
except Exception:
|
|
411
|
+
return frontmost_app()
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def find_window_displays(bundle_ids: list[str]) -> list[dict[str, Any]]:
|
|
415
|
+
"""For each bundleId, find which display(s) its windows are on."""
|
|
416
|
+
if not bundle_ids:
|
|
417
|
+
return []
|
|
418
|
+
|
|
419
|
+
displays = get_displays()
|
|
420
|
+
windows = list_windows()
|
|
421
|
+
|
|
422
|
+
# Build exe-stem -> ownerName mapping
|
|
423
|
+
names_by_bundle: dict[str, str | None] = {}
|
|
424
|
+
for bid in bundle_ids:
|
|
425
|
+
names_by_bundle[bid] = app_display_name(bid)
|
|
426
|
+
|
|
427
|
+
result = []
|
|
428
|
+
for bundle_id in bundle_ids:
|
|
429
|
+
target_name = names_by_bundle.get(bundle_id)
|
|
430
|
+
display_ids: set[int] = set()
|
|
431
|
+
for window in windows:
|
|
432
|
+
owner = window["ownerName"]
|
|
433
|
+
if not owner:
|
|
434
|
+
continue
|
|
435
|
+
# Match by exe name
|
|
436
|
+
owner_stem = Path(owner).stem if owner.endswith(".exe") else owner
|
|
437
|
+
if target_name and owner != target_name and owner_stem != bundle_id:
|
|
438
|
+
continue
|
|
439
|
+
if not target_name and owner_stem != bundle_id and owner != bundle_id:
|
|
440
|
+
continue
|
|
441
|
+
# Check which displays this window overlaps
|
|
442
|
+
wx = window["bounds"]["x"]
|
|
443
|
+
wy = window["bounds"]["y"]
|
|
444
|
+
ww = window["bounds"]["width"]
|
|
445
|
+
wh = window["bounds"]["height"]
|
|
446
|
+
for display in displays:
|
|
447
|
+
dx = display["originX"]
|
|
448
|
+
dy = display["originY"]
|
|
449
|
+
dw = display["width"]
|
|
450
|
+
dh = display["height"]
|
|
451
|
+
# Check rectangle intersection
|
|
452
|
+
if wx < dx + dw and wx + ww > dx and wy < dy + dh and wy + wh > dy:
|
|
453
|
+
display_ids.add(int(display["displayId"]))
|
|
454
|
+
result.append({"bundleId": bundle_id, "displayIds": sorted(display_ids)})
|
|
455
|
+
return result
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def open_app(bundle_id: str) -> None:
|
|
459
|
+
"""Open an application by its bundleId (exe path or program name)."""
|
|
460
|
+
# Try to find the exe path from registry
|
|
461
|
+
import winreg
|
|
462
|
+
exe_path = None
|
|
463
|
+
|
|
464
|
+
reg_paths = [
|
|
465
|
+
(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"),
|
|
466
|
+
(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"),
|
|
467
|
+
(winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"),
|
|
468
|
+
]
|
|
469
|
+
for hive, sub_key in reg_paths:
|
|
470
|
+
try:
|
|
471
|
+
key = winreg.OpenKey(hive, sub_key)
|
|
472
|
+
app_key = winreg.OpenKey(key, bundle_id)
|
|
473
|
+
try:
|
|
474
|
+
exe_path = winreg.QueryValueEx(app_key, "DisplayIcon")[0]
|
|
475
|
+
if exe_path and "," in exe_path:
|
|
476
|
+
exe_path = exe_path.split(",")[0]
|
|
477
|
+
except OSError:
|
|
478
|
+
try:
|
|
479
|
+
exe_path = winreg.QueryValueEx(app_key, "InstallLocation")[0]
|
|
480
|
+
except OSError:
|
|
481
|
+
pass
|
|
482
|
+
winreg.CloseKey(app_key)
|
|
483
|
+
winreg.CloseKey(key)
|
|
484
|
+
if exe_path:
|
|
485
|
+
break
|
|
486
|
+
except OSError:
|
|
487
|
+
continue
|
|
488
|
+
|
|
489
|
+
if exe_path and Path(exe_path).exists():
|
|
490
|
+
os.startfile(exe_path)
|
|
491
|
+
else:
|
|
492
|
+
# Fallback: try to run it directly
|
|
493
|
+
try:
|
|
494
|
+
subprocess.Popen([bundle_id], shell=True)
|
|
495
|
+
except Exception:
|
|
496
|
+
raise RuntimeError(f"App not found for identifier: {bundle_id}")
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
# ---------------------------------------------------------------------------
|
|
500
|
+
# Clipboard (pyperclip — cross-platform)
|
|
501
|
+
# ---------------------------------------------------------------------------
|
|
502
|
+
|
|
503
|
+
def read_clipboard() -> str:
|
|
504
|
+
import pyperclip
|
|
505
|
+
try:
|
|
506
|
+
return pyperclip.paste() or ""
|
|
507
|
+
except Exception:
|
|
508
|
+
return ""
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
def write_clipboard(text: str) -> None:
|
|
512
|
+
import pyperclip
|
|
513
|
+
pyperclip.copy(text)
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
def paste_clipboard() -> None:
|
|
517
|
+
pyautogui.hotkey("ctrl", "v", interval=0.02)
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
# ---------------------------------------------------------------------------
|
|
521
|
+
# Permissions — Windows doesn't have macOS-style TCC
|
|
522
|
+
# ---------------------------------------------------------------------------
|
|
523
|
+
|
|
524
|
+
def check_permissions() -> dict[str, bool | None]:
|
|
525
|
+
"""Windows does not require explicit accessibility/screen-recording
|
|
526
|
+
permissions like macOS TCC. Always report as granted."""
|
|
527
|
+
return {
|
|
528
|
+
"accessibility": True,
|
|
529
|
+
"screenRecording": True,
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
# ---------------------------------------------------------------------------
|
|
534
|
+
# Input actions (pyautogui — identical to mac_helper)
|
|
535
|
+
# ---------------------------------------------------------------------------
|
|
536
|
+
|
|
537
|
+
def click(x: int, y: int, button: str, count: int, modifiers: list[str] | None) -> None:
|
|
538
|
+
pyautogui.moveTo(x, y)
|
|
539
|
+
if modifiers:
|
|
540
|
+
normalized = [normalize_key(m) for m in modifiers]
|
|
541
|
+
for key in normalized:
|
|
542
|
+
pyautogui.keyDown(key)
|
|
543
|
+
try:
|
|
544
|
+
pyautogui.click(x=x, y=y, button=button, clicks=count, interval=0.08)
|
|
545
|
+
finally:
|
|
546
|
+
for key in reversed(normalized):
|
|
547
|
+
pyautogui.keyUp(key)
|
|
548
|
+
else:
|
|
549
|
+
pyautogui.click(x=x, y=y, button=button, clicks=count, interval=0.08)
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
def scroll(x: int, y: int, delta_x: int, delta_y: int) -> None:
|
|
553
|
+
pyautogui.moveTo(x, y)
|
|
554
|
+
if delta_y:
|
|
555
|
+
pyautogui.scroll(int(delta_y), x=x, y=y)
|
|
556
|
+
if delta_x:
|
|
557
|
+
pyautogui.hscroll(int(delta_x), x=x, y=y)
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
def key_action(sequence: str, repeat: int = 1) -> None:
|
|
561
|
+
parts = [normalize_key(part) for part in sequence.split("+") if part.strip()]
|
|
562
|
+
for _ in range(max(1, repeat)):
|
|
563
|
+
if len(parts) == 1:
|
|
564
|
+
pyautogui.press(parts[0])
|
|
565
|
+
else:
|
|
566
|
+
pyautogui.hotkey(*parts, interval=0.02)
|
|
567
|
+
time.sleep(0.01)
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
def hold_keys(keys: list[str], duration_ms: int) -> None:
|
|
571
|
+
normalized = [normalize_key(k) for k in keys]
|
|
572
|
+
for key in normalized:
|
|
573
|
+
pyautogui.keyDown(key)
|
|
574
|
+
try:
|
|
575
|
+
time.sleep(max(duration_ms, 0) / 1000)
|
|
576
|
+
finally:
|
|
577
|
+
for key in reversed(normalized):
|
|
578
|
+
pyautogui.keyUp(key)
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
def type_text(text: str) -> None:
|
|
582
|
+
pyautogui.write(text, interval=0.008)
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
# ---------------------------------------------------------------------------
|
|
586
|
+
# Main dispatcher — exact same command protocol as mac_helper.py
|
|
587
|
+
# ---------------------------------------------------------------------------
|
|
588
|
+
|
|
589
|
+
def main() -> int:
|
|
590
|
+
parser = argparse.ArgumentParser()
|
|
591
|
+
parser.add_argument("command")
|
|
592
|
+
parser.add_argument("--payload", default="{}")
|
|
593
|
+
args = parser.parse_args()
|
|
594
|
+
payload = json.loads(args.payload)
|
|
595
|
+
|
|
596
|
+
try:
|
|
597
|
+
command = args.command
|
|
598
|
+
if command == "check_permissions":
|
|
599
|
+
perms = check_permissions()
|
|
600
|
+
json_output({"ok": True, "result": perms})
|
|
601
|
+
return 0
|
|
602
|
+
if command == "list_displays":
|
|
603
|
+
json_output({"ok": True, "result": get_displays()})
|
|
604
|
+
return 0
|
|
605
|
+
if command == "get_display_size":
|
|
606
|
+
json_output({"ok": True, "result": choose_display(payload.get("displayId"))})
|
|
607
|
+
return 0
|
|
608
|
+
if command == "screenshot":
|
|
609
|
+
resize = None
|
|
610
|
+
if payload.get("targetWidth") and payload.get("targetHeight"):
|
|
611
|
+
resize = (int(payload["targetWidth"]), int(payload["targetHeight"]))
|
|
612
|
+
result = capture_display(payload.get("displayId"), resize)
|
|
613
|
+
json_output({"ok": True, "result": result})
|
|
614
|
+
return 0
|
|
615
|
+
if command == "resolve_prepare_capture":
|
|
616
|
+
resize = None
|
|
617
|
+
if payload.get("targetWidth") and payload.get("targetHeight"):
|
|
618
|
+
resize = (int(payload["targetWidth"]), int(payload["targetHeight"]))
|
|
619
|
+
result = capture_display(payload.get("preferredDisplayId"), resize)
|
|
620
|
+
result["hidden"] = []
|
|
621
|
+
result["resolvedDisplayId"] = result["displayId"]
|
|
622
|
+
json_output({"ok": True, "result": result})
|
|
623
|
+
return 0
|
|
624
|
+
if command == "zoom":
|
|
625
|
+
resize = None
|
|
626
|
+
if payload.get("targetWidth") and payload.get("targetHeight"):
|
|
627
|
+
resize = (int(payload["targetWidth"]), int(payload["targetHeight"]))
|
|
628
|
+
region = {
|
|
629
|
+
"left": int(payload["x"]),
|
|
630
|
+
"top": int(payload["y"]),
|
|
631
|
+
"width": int(payload["width"]),
|
|
632
|
+
"height": int(payload["height"]),
|
|
633
|
+
}
|
|
634
|
+
json_output({"ok": True, "result": capture_region(region, resize)})
|
|
635
|
+
return 0
|
|
636
|
+
if command == "prepare_for_action":
|
|
637
|
+
json_output({"ok": True, "result": []})
|
|
638
|
+
return 0
|
|
639
|
+
if command == "preview_hide_set":
|
|
640
|
+
json_output({"ok": True, "result": []})
|
|
641
|
+
return 0
|
|
642
|
+
if command == "find_window_displays":
|
|
643
|
+
json_output({"ok": True, "result": find_window_displays(list(payload.get("bundleIds") or []))})
|
|
644
|
+
return 0
|
|
645
|
+
if command == "key":
|
|
646
|
+
key_action(str(payload["keySequence"]), int(payload.get("repeat") or 1))
|
|
647
|
+
json_output({"ok": True, "result": True})
|
|
648
|
+
return 0
|
|
649
|
+
if command == "hold_key":
|
|
650
|
+
hold_keys(list(payload.get("keyNames") or []), int(payload.get("durationMs") or 0))
|
|
651
|
+
json_output({"ok": True, "result": True})
|
|
652
|
+
return 0
|
|
653
|
+
if command == "type":
|
|
654
|
+
type_text(str(payload.get("text") or ""))
|
|
655
|
+
json_output({"ok": True, "result": True})
|
|
656
|
+
return 0
|
|
657
|
+
if command == "click":
|
|
658
|
+
click(int(payload["x"]), int(payload["y"]), str(payload.get("button") or "left"), int(payload.get("count") or 1), payload.get("modifiers"))
|
|
659
|
+
json_output({"ok": True, "result": True})
|
|
660
|
+
return 0
|
|
661
|
+
if command == "drag":
|
|
662
|
+
from_point = payload.get("from")
|
|
663
|
+
if from_point:
|
|
664
|
+
pyautogui.moveTo(int(from_point["x"]), int(from_point["y"]))
|
|
665
|
+
pyautogui.dragTo(int(payload["to"]["x"]), int(payload["to"]["y"]), duration=0.2, button="left")
|
|
666
|
+
json_output({"ok": True, "result": True})
|
|
667
|
+
return 0
|
|
668
|
+
if command == "move_mouse":
|
|
669
|
+
pyautogui.moveTo(int(payload["x"]), int(payload["y"]))
|
|
670
|
+
json_output({"ok": True, "result": True})
|
|
671
|
+
return 0
|
|
672
|
+
if command == "scroll":
|
|
673
|
+
scroll(int(payload["x"]), int(payload["y"]), int(payload.get("deltaX") or 0), int(payload.get("deltaY") or 0))
|
|
674
|
+
json_output({"ok": True, "result": True})
|
|
675
|
+
return 0
|
|
676
|
+
if command == "mouse_down":
|
|
677
|
+
pyautogui.mouseDown(button="left")
|
|
678
|
+
json_output({"ok": True, "result": True})
|
|
679
|
+
return 0
|
|
680
|
+
if command == "mouse_up":
|
|
681
|
+
pyautogui.mouseUp(button="left")
|
|
682
|
+
json_output({"ok": True, "result": True})
|
|
683
|
+
return 0
|
|
684
|
+
if command == "cursor_position":
|
|
685
|
+
x, y = pyautogui.position()
|
|
686
|
+
json_output({"ok": True, "result": {"x": int(x), "y": int(y)}})
|
|
687
|
+
return 0
|
|
688
|
+
if command == "frontmost_app":
|
|
689
|
+
json_output({"ok": True, "result": frontmost_app()})
|
|
690
|
+
return 0
|
|
691
|
+
if command == "app_under_point":
|
|
692
|
+
json_output({"ok": True, "result": app_under_point(int(payload["x"]), int(payload["y"]))})
|
|
693
|
+
return 0
|
|
694
|
+
if command == "list_installed_apps":
|
|
695
|
+
json_output({"ok": True, "result": installed_apps()})
|
|
696
|
+
return 0
|
|
697
|
+
if command == "list_running_apps":
|
|
698
|
+
json_output({"ok": True, "result": running_apps()})
|
|
699
|
+
return 0
|
|
700
|
+
if command == "open_app":
|
|
701
|
+
open_app(str(payload["bundleId"]))
|
|
702
|
+
json_output({"ok": True, "result": True})
|
|
703
|
+
return 0
|
|
704
|
+
if command == "read_clipboard":
|
|
705
|
+
json_output({"ok": True, "result": read_clipboard()})
|
|
706
|
+
return 0
|
|
707
|
+
if command == "write_clipboard":
|
|
708
|
+
write_clipboard(str(payload.get("text") or ""))
|
|
709
|
+
json_output({"ok": True, "result": True})
|
|
710
|
+
return 0
|
|
711
|
+
if command == "paste_clipboard":
|
|
712
|
+
paste_clipboard()
|
|
713
|
+
json_output({"ok": True, "result": True})
|
|
714
|
+
return 0
|
|
715
|
+
error_output(f"Unknown command: {command}", code="bad_command")
|
|
716
|
+
return 2
|
|
717
|
+
except Exception as exc:
|
|
718
|
+
error_output(str(exc))
|
|
719
|
+
return 1
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
if __name__ == "__main__":
|
|
723
|
+
raise SystemExit(main())
|