windows-computer-use-mcp 0.3.0__py3-none-any.whl

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.
@@ -0,0 +1,3 @@
1
+ """Antigravity Computer Use toolkit for Windows."""
2
+
3
+ __version__ = "0.3.0"
@@ -0,0 +1,350 @@
1
+ """Targeted window capture (PrintWindow PW_RENDERFULLCONTENT) & monitor capture with overlays."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import base64
6
+ import ctypes
7
+ from ctypes import wintypes
8
+ import io
9
+ from pathlib import Path
10
+ import time
11
+ from typing import Any
12
+
13
+ import mss
14
+ from PIL import Image, ImageDraw, ImageFont
15
+
16
+ from .vdisplay import pick_agent_monitor
17
+ from .win32_core import Rect, gdi32, get_window_info, user32
18
+
19
+ PW_CLIENTONLY = 0x00000001
20
+ PW_RENDERFULLCONTENT = 0x00000002
21
+ BI_RGB = 0
22
+ DIB_RGB_COLORS = 0
23
+ SRCCOPY = 0x00CC0020
24
+
25
+
26
+ class BITMAPINFOHEADER(ctypes.Structure):
27
+ _fields_ = [
28
+ ("biSize", wintypes.DWORD),
29
+ ("biWidth", wintypes.LONG),
30
+ ("biHeight", wintypes.LONG),
31
+ ("biPlanes", wintypes.WORD),
32
+ ("biBitCount", wintypes.WORD),
33
+ ("biCompression", wintypes.DWORD),
34
+ ("biSizeImage", wintypes.DWORD),
35
+ ("biXPelsPerMeter", wintypes.LONG),
36
+ ("biYPelsPerMeter", wintypes.LONG),
37
+ ("biClrUsed", wintypes.DWORD),
38
+ ("biClrImportant", wintypes.DWORD),
39
+ ]
40
+
41
+
42
+ class BITMAPINFO(ctypes.Structure):
43
+ _fields_ = [
44
+ ("bmiHeader", BITMAPINFOHEADER),
45
+ ("bmiColors", wintypes.DWORD * 3),
46
+ ]
47
+
48
+
49
+ user32.PrintWindow.argtypes = [wintypes.HWND, wintypes.HDC, wintypes.UINT]
50
+ user32.PrintWindow.restype = wintypes.BOOL
51
+ user32.GetWindowDC.argtypes = [wintypes.HWND]
52
+ user32.GetWindowDC.restype = wintypes.HDC
53
+ user32.GetDC.argtypes = [wintypes.HWND]
54
+ user32.GetDC.restype = wintypes.HDC
55
+ user32.ReleaseDC.argtypes = [wintypes.HWND, wintypes.HDC]
56
+ user32.ReleaseDC.restype = ctypes.c_int
57
+
58
+ gdi32.CreateCompatibleDC.argtypes = [wintypes.HDC]
59
+ gdi32.CreateCompatibleDC.restype = wintypes.HDC
60
+ gdi32.CreateDIBSection.argtypes = [
61
+ wintypes.HDC,
62
+ ctypes.POINTER(BITMAPINFO),
63
+ wintypes.UINT,
64
+ ctypes.POINTER(ctypes.c_void_p),
65
+ wintypes.HANDLE,
66
+ wintypes.DWORD,
67
+ ]
68
+ gdi32.CreateDIBSection.restype = wintypes.HBITMAP
69
+ gdi32.SelectObject.argtypes = [wintypes.HDC, wintypes.HGDIOBJ]
70
+ gdi32.SelectObject.restype = wintypes.HGDIOBJ
71
+ gdi32.DeleteObject.argtypes = [wintypes.HGDIOBJ]
72
+ gdi32.DeleteObject.restype = wintypes.BOOL
73
+ gdi32.DeleteDC.argtypes = [wintypes.HDC]
74
+ gdi32.DeleteDC.restype = wintypes.BOOL
75
+ gdi32.BitBlt.argtypes = [
76
+ wintypes.HDC,
77
+ ctypes.c_int,
78
+ ctypes.c_int,
79
+ ctypes.c_int,
80
+ ctypes.c_int,
81
+ wintypes.HDC,
82
+ ctypes.c_int,
83
+ ctypes.c_int,
84
+ wintypes.DWORD,
85
+ ]
86
+ gdi32.BitBlt.restype = wintypes.BOOL
87
+
88
+
89
+ def _capture_hwnd_printwindow(hwnd: int, width: int, height: int, client_only: bool) -> Image.Image | None:
90
+ if width <= 0 or height <= 0:
91
+ return None
92
+
93
+ hdc_src = user32.GetDC(hwnd) if client_only else user32.GetWindowDC(hwnd)
94
+ if not hdc_src:
95
+ hdc_src = user32.GetDC(0)
96
+ hdc_mem = gdi32.CreateCompatibleDC(hdc_src)
97
+ if not hdc_mem:
98
+ user32.ReleaseDC(hwnd, hdc_src)
99
+ return None
100
+
101
+ bmi = BITMAPINFO()
102
+ bmi.bmiHeader.biSize = ctypes.sizeof(BITMAPINFOHEADER)
103
+ bmi.bmiHeader.biWidth = width
104
+ bmi.bmiHeader.biHeight = -height # top-down DIB
105
+ bmi.bmiHeader.biPlanes = 1
106
+ bmi.bmiHeader.biBitCount = 32
107
+ bmi.bmiHeader.biCompression = BI_RGB
108
+
109
+ bits_ptr = ctypes.c_void_p()
110
+ hbitmap = gdi32.CreateDIBSection(
111
+ hdc_mem,
112
+ ctypes.byref(bmi),
113
+ DIB_RGB_COLORS,
114
+ ctypes.byref(bits_ptr),
115
+ None,
116
+ 0,
117
+ )
118
+ if not hbitmap:
119
+ gdi32.DeleteDC(hdc_mem)
120
+ user32.ReleaseDC(hwnd, hdc_src)
121
+ return None
122
+
123
+ old_obj = gdi32.SelectObject(hdc_mem, hbitmap)
124
+ try:
125
+ flags = PW_RENDERFULLCONTENT | (PW_CLIENTONLY if client_only else 0)
126
+ ok = bool(user32.PrintWindow(hwnd, hdc_mem, flags))
127
+ if not ok:
128
+ # Retry without PW_RENDERFULLCONTENT for older GDI windows
129
+ ok = bool(user32.PrintWindow(hwnd, hdc_mem, PW_CLIENTONLY if client_only else 0))
130
+ if not ok:
131
+ # Final GDI BitBlt attempt
132
+ gdi32.BitBlt(hdc_mem, 0, 0, width, height, hdc_src, 0, 0, SRCCOPY)
133
+
134
+ buf_size = width * height * 4
135
+ raw = ctypes.string_at(bits_ptr.value, buf_size)
136
+ img = Image.frombuffer("RGBA", (width, height), raw, "raw", "BGRA", 0, 1).convert("RGB")
137
+ return img
138
+ finally:
139
+ gdi32.SelectObject(hdc_mem, old_obj)
140
+ gdi32.DeleteObject(hbitmap)
141
+ gdi32.DeleteDC(hdc_mem)
142
+ user32.ReleaseDC(hwnd, hdc_src)
143
+
144
+
145
+ def _is_effectively_blank(img: Image.Image) -> bool:
146
+ extrema = img.getextrema()
147
+ # extrema is ((minR, maxR), (minG, maxG), (minB, maxB))
148
+ return all(lo == hi for lo, hi in extrema)
149
+
150
+
151
+ def _capture_rect_mss(rect: Rect) -> Image.Image:
152
+ with mss.mss() as sct:
153
+ region = {
154
+ "left": rect.left,
155
+ "top": rect.top,
156
+ "width": max(1, rect.width),
157
+ "height": max(1, rect.height),
158
+ }
159
+ shot = sct.grab(region)
160
+ return Image.frombytes("RGB", shot.size, shot.rgb)
161
+
162
+
163
+ def _draw_grid(img: Image.Image, step: int = 100) -> Image.Image:
164
+ overlay = img.copy()
165
+ draw = ImageDraw.Draw(overlay, "RGBA")
166
+ w, h = overlay.size
167
+ font = ImageFont.load_default()
168
+ line_color = (255, 80, 80, 110)
169
+ text_bg = (0, 0, 0, 170)
170
+ text_fg = (255, 255, 0, 255)
171
+
172
+ for x in range(step, w, step):
173
+ draw.line([(x, 0), (x, h)], fill=line_color, width=1)
174
+ label = str(x)
175
+ draw.rectangle([x + 2, 2, x + 28, 15], fill=text_bg)
176
+ draw.text((x + 4, 3), label, fill=text_fg, font=font)
177
+
178
+ for y in range(step, h, step):
179
+ draw.line([(0, y), (w, y)], fill=line_color, width=1)
180
+ label = str(y)
181
+ draw.rectangle([2, y + 2, 32, y + 15], fill=text_bg)
182
+ draw.text((4, y + 3), label, fill=text_fg, font=font)
183
+
184
+ return overlay
185
+
186
+
187
+ def _draw_annotations(
188
+ img: Image.Image,
189
+ annotations: list[dict[str, Any]],
190
+ origin_x: int,
191
+ origin_y: int,
192
+ ) -> Image.Image:
193
+ """Draw Set-of-Mark numbered bounding boxes onto the image using screen rects."""
194
+ out = img.copy()
195
+ draw = ImageDraw.Draw(out, "RGBA")
196
+ font = ImageFont.load_default()
197
+ w, h = out.size
198
+
199
+ palette = [
200
+ (230, 57, 70),
201
+ (29, 53, 87),
202
+ (42, 157, 143),
203
+ (233, 196, 106),
204
+ (244, 162, 97),
205
+ (131, 56, 236),
206
+ ]
207
+
208
+ for idx, item in enumerate(annotations):
209
+ rect = item.get("screen_rect")
210
+ if not rect:
211
+ continue
212
+ x1 = max(0, min(w - 1, int(rect["left"]) - origin_x))
213
+ y1 = max(0, min(h - 1, int(rect["top"]) - origin_y))
214
+ x2 = max(0, min(w - 1, int(rect["right"]) - origin_x))
215
+ y2 = max(0, min(h - 1, int(rect["bottom"]) - origin_y))
216
+ if x2 <= x1 or y2 <= y1:
217
+ continue
218
+
219
+ color = palette[idx % len(palette)]
220
+ draw.rectangle([x1, y1, x2, y2], outline=(*color, 235), width=2)
221
+
222
+ tag = str(item.get("id", f"e{idx + 1}"))
223
+ tb = draw.textbbox((0, 0), tag, font=font)
224
+ tw = tb[2] - tb[0] + 6
225
+ th = tb[3] - tb[1] + 4
226
+ bx1 = x1
227
+ by1 = max(0, y1 - th)
228
+ draw.rectangle([bx1, by1, bx1 + tw, by1 + th], fill=(*color, 230))
229
+ draw.text((bx1 + 3, by1 + 2), tag, fill=(255, 255, 255, 255), font=font)
230
+
231
+ return out
232
+
233
+
234
+ def _encode_image(
235
+ img: Image.Image,
236
+ max_width: int = 1600,
237
+ save_path: str | None = None,
238
+ ) -> tuple[str, int, int, float, float, str | None]:
239
+ orig_w, orig_h = img.size
240
+ out_img = img
241
+ scale_x = 1.0
242
+ scale_y = 1.0
243
+
244
+ if max_width > 0 and orig_w > max_width:
245
+ ratio = max_width / float(orig_w)
246
+ new_w = max_width
247
+ new_h = max(1, int(round(orig_h * ratio)))
248
+ out_img = img.resize((new_w, new_h), Image.Resampling.LANCZOS)
249
+ scale_x = orig_w / float(new_w)
250
+ scale_y = orig_h / float(new_h)
251
+
252
+ saved: str | None = None
253
+ if save_path:
254
+ p = Path(save_path)
255
+ p.parent.mkdir(parents=True, exist_ok=True)
256
+ out_img.save(p, format="PNG")
257
+ saved = str(p)
258
+
259
+ buf = io.BytesIO()
260
+ out_img.save(buf, format="PNG", optimize=True)
261
+ b64 = base64.b64encode(buf.getvalue()).decode("ascii")
262
+ return b64, out_img.width, out_img.height, round(scale_x, 4), round(scale_y, 4), saved
263
+
264
+
265
+ def capture_window(
266
+ hwnd: int,
267
+ client_only: bool = False,
268
+ include_grid: bool = False,
269
+ grid_step: int = 100,
270
+ annotations: list[dict[str, Any]] | None = None,
271
+ max_width: int = 1600,
272
+ save_path: str | None = None,
273
+ ) -> dict[str, Any]:
274
+ """Capture a specific window via PrintWindow (works occluded & on virtual displays)."""
275
+ info = get_window_info(hwnd)
276
+ if info is None:
277
+ raise ValueError(f"Invalid window handle: {hwnd}")
278
+ if info.is_minimized:
279
+ raise RuntimeError(
280
+ f"Window {hwnd} ({info.title!r}) is minimized. Move or restore it before capturing."
281
+ )
282
+
283
+ target_rect = info.client_rect if client_only else info.window_rect
284
+ method = "printwindow_fullcontent"
285
+ img = _capture_hwnd_printwindow(hwnd, target_rect.width, target_rect.height, client_only=client_only)
286
+
287
+ if img is None or _is_effectively_blank(img):
288
+ # Fallback to screen region capture (e.g. DRM/special surface)
289
+ img = _capture_rect_mss(target_rect)
290
+ method = "mss_rect_fallback"
291
+
292
+ if annotations:
293
+ img = _draw_annotations(img, annotations, target_rect.left, target_rect.top)
294
+ if include_grid:
295
+ img = _draw_grid(img, step=max(25, grid_step))
296
+
297
+ b64, out_w, out_h, scale_x, scale_y, saved = _encode_image(img, max_width=max_width, save_path=save_path)
298
+
299
+ return {
300
+ "image_base64": b64,
301
+ "mime_type": "image/png",
302
+ "method": method,
303
+ "client_only": client_only,
304
+ "hwnd": info.hwnd,
305
+ "title": info.title,
306
+ "process_name": info.process_name,
307
+ "monitor_index": info.monitor_index,
308
+ "origin_screen_x": target_rect.left,
309
+ "origin_screen_y": target_rect.top,
310
+ "original_width": target_rect.width,
311
+ "original_height": target_rect.height,
312
+ "image_width": out_w,
313
+ "image_height": out_h,
314
+ "scale_x": scale_x,
315
+ "scale_y": scale_y,
316
+ "saved_path": saved,
317
+ "timestamp": time.time(),
318
+ }
319
+
320
+
321
+ def capture_monitor(
322
+ monitor_index: int | None = None,
323
+ include_grid: bool = False,
324
+ grid_step: int = 100,
325
+ max_width: int = 1600,
326
+ save_path: str | None = None,
327
+ ) -> dict[str, Any]:
328
+ """Capture a whole physical or virtual monitor."""
329
+ mon = pick_agent_monitor(monitor_index)
330
+ img = _capture_rect_mss(mon.rect)
331
+ if include_grid:
332
+ img = _draw_grid(img, step=max(50, grid_step))
333
+
334
+ b64, out_w, out_h, scale_x, scale_y, saved = _encode_image(img, max_width=max_width, save_path=save_path)
335
+ return {
336
+ "image_base64": b64,
337
+ "mime_type": "image/png",
338
+ "method": "mss_monitor",
339
+ "monitor": mon.to_dict(),
340
+ "origin_screen_x": mon.rect.left,
341
+ "origin_screen_y": mon.rect.top,
342
+ "original_width": mon.rect.width,
343
+ "original_height": mon.rect.height,
344
+ "image_width": out_w,
345
+ "image_height": out_h,
346
+ "scale_x": scale_x,
347
+ "scale_y": scale_y,
348
+ "saved_path": saved,
349
+ "timestamp": time.time(),
350
+ }
@@ -0,0 +1,90 @@
1
+ """Command-line interface for Antigravity Computer Use."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import json
7
+ import sys
8
+
9
+ from . import capture, input_ctrl, uia_forms, vdisplay, win32_core
10
+
11
+
12
+ def main(argv: list[str] | None = None) -> int:
13
+ parser = argparse.ArgumentParser(prog="agy-computer-use", description="Windows Computer Use CLI for Antigravity")
14
+ sub = parser.add_subparsers(dest="cmd", required=True)
15
+
16
+ sub.add_parser("display-status", help="Show monitors and Virtual Display Driver status")
17
+
18
+ p_win = sub.add_parser("windows", help="List visible windows")
19
+ p_win.add_argument("--title", default=None)
20
+ p_win.add_argument("--process", default=None)
21
+ p_win.add_argument("--monitor", type=int, default=None)
22
+
23
+ p_cap = sub.add_parser("capture", help="Capture a window to PNG")
24
+ p_cap.add_argument("--hwnd", type=int, default=None)
25
+ p_cap.add_argument("--title", default=None)
26
+ p_cap.add_argument("--out", required=True)
27
+ p_cap.add_argument("--client-only", action="store_true")
28
+ p_cap.add_argument("--grid", action="store_true")
29
+ p_cap.add_argument("--annotate", action="store_true")
30
+
31
+ p_uia = sub.add_parser("inspect", help="Inspect UI Automation tree of a window")
32
+ p_uia.add_argument("--hwnd", type=int, default=None)
33
+ p_uia.add_argument("--title", default=None)
34
+ p_uia.add_argument("--all", action="store_true", help="Include non-interactive named elements")
35
+
36
+ p_mv = sub.add_parser("move", help="Move a window to the virtual or target monitor")
37
+ p_mv.add_argument("--hwnd", type=int, default=None)
38
+ p_mv.add_argument("--title", default=None)
39
+ p_mv.add_argument("--monitor", type=int, default=None)
40
+ p_mv.add_argument("--maximize", action="store_true")
41
+
42
+ args = parser.parse_args(argv)
43
+
44
+ if args.cmd == "display-status":
45
+ print(json.dumps(vdisplay.get_display_status(), indent=2, ensure_ascii=False))
46
+ return 0
47
+
48
+ if args.cmd == "windows":
49
+ wins = win32_core.list_windows(
50
+ title_contains=args.title,
51
+ process_contains=args.process,
52
+ monitor_index=args.monitor,
53
+ )
54
+ print(json.dumps([w.to_dict() for w in wins], indent=2, ensure_ascii=False))
55
+ return 0
56
+
57
+ if args.cmd == "capture":
58
+ w = win32_core.resolve_window(hwnd=args.hwnd, title_contains=args.title)
59
+ annotations = None
60
+ if args.annotate:
61
+ uia_res = uia_forms.inspect_ui(w.hwnd)
62
+ annotations = uia_res["elements"]
63
+ res = capture.capture_window(
64
+ w.hwnd,
65
+ client_only=args.client_only,
66
+ include_grid=args.grid,
67
+ annotations=annotations,
68
+ save_path=args.out,
69
+ )
70
+ res.pop("image_base64", None)
71
+ print(json.dumps(res, indent=2, ensure_ascii=False))
72
+ return 0
73
+
74
+ if args.cmd == "inspect":
75
+ w = win32_core.resolve_window(hwnd=args.hwnd, title_contains=args.title)
76
+ res = uia_forms.inspect_ui(w.hwnd, interactive_only=not args.all)
77
+ print(json.dumps(res, indent=2, ensure_ascii=False))
78
+ return 0
79
+
80
+ if args.cmd == "move":
81
+ w = win32_core.resolve_window(hwnd=args.hwnd, title_contains=args.title)
82
+ res = vdisplay.move_window_to_monitor(w.hwnd, monitor_index=args.monitor, maximize=args.maximize)
83
+ print(json.dumps(res, indent=2, ensure_ascii=False))
84
+ return 0
85
+
86
+ return 1
87
+
88
+
89
+ if __name__ == "__main__":
90
+ sys.exit(main())