跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

screenshot_darwin.go

112 行 · 3.5 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4//go:build darwin && !no_vz
5
6package vz
7
8/*
9#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c -fno-objc-arc
10#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework AppKit -framework CoreGraphics -framework ImageIO
11#include <stdlib.h>
12#include <string.h>
13#include <dispatch/dispatch.h>
14#include <AppKit/AppKit.h>
15#include <CoreGraphics/CoreGraphics.h>
16#include <ImageIO/ImageIO.h>
17
18static void freeCString(char *s) { free(s); }
19
20// captureWindowImageBytes captures the frontmost non-panel on-screen window of
21// the current process, encodes it using the given ImageIO UTI, and returns a
22// malloc'd buffer. The caller must free() the returned pointer.
23static void *captureWindowImageBytes(int *outLen, const char *uti) {
24 __block void *result = NULL;
25 __block int resultLen = 0;
26
27 NSString *utiStr = [NSString stringWithUTF8String:uti];
28
29 // dispatch_sync to the main queue is required for window server access.
30 // CaptureScreenshot is always called from a Go goroutine, which is never
31 // the main thread, so no deadlock is possible in normal Lima operation.
32 // The isMainThread guard is a safety net for unexpected call paths.
33 void (^captureBlock)(void) = ^{
34 NSWindow *target = nil;
35 for (NSWindow *w in [NSApplication sharedApplication].windows) {
36 if ([w isKindOfClass:[NSPanel class]]) continue;
37 if (!w.isVisible) continue;
38 target = w;
39 break;
40 }
41 if (!target) return;
42
43 CGWindowID wid = (CGWindowID)[target windowNumber];
44 CGImageRef img = CGWindowListCreateImage(
45 CGRectNull,
46 kCGWindowListOptionIncludingWindow,
47 wid,
48 kCGWindowImageBoundsIgnoreFraming | kCGWindowImageShouldBeOpaque
49 );
50 if (!img) return;
51
52 NSMutableData *data = [NSMutableData data];
53 CGImageDestinationRef dst = CGImageDestinationCreateWithData(
54 (__bridge CFMutableDataRef)data,
55 (__bridge CFStringRef)utiStr,
56 1, NULL
57 );
58 if (!dst) {
59 CGImageRelease(img);
60 return;
61 }
62 CGImageDestinationAddImage(dst, img, NULL);
63 bool ok = CGImageDestinationFinalize(dst);
64 CFRelease(dst);
65 CGImageRelease(img);
66 if (!ok) return;
67
68 resultLen = (int)data.length;
69 result = malloc(resultLen);
70 memcpy(result, data.bytes, resultLen);
71 };
72
73 if ([NSThread isMainThread]) {
74 captureBlock();
75 } else {
76 dispatch_sync(dispatch_get_main_queue(), captureBlock);
77 }
78
79 *outLen = resultLen;
80 return result;
81}
82*/
83import "C"
84
85import (
86 "errors"
87 "fmt"
88
89 "github.com/lima-vm/lima/v2/pkg/driver"
90)
91
92// CaptureScreenshot captures the VM display window.
93// format is "png" or "bmp"; anything else defaults to PNG.
94// Implements driver.Screenshotter. Requires the GUI app bundle to be running.
95func (l *LimaVzDriver) CaptureScreenshot(format string) ([]byte, error) {
96 if !l.canRunGUI() {
97 return nil, fmt.Errorf("%w (set video.display to \"default\" or \"vz\" to enable screenshots)", driver.ErrNoDisplay)
98 }
99 uti := "public.png"
100 if format == "bmp" {
101 uti = "com.microsoft.bmp"
102 }
103 cuti := C.CString(uti)
104 defer C.freeCString(cuti)
105 var outLen C.int
106 ptr := C.captureWindowImageBytes(&outLen, cuti)
107 if ptr == nil || outLen == 0 {
108 return nil, errors.New("screenshot capture returned no data; GUI window may not be visible")
109 }
110 defer C.free(ptr)
111 return C.GoBytes(ptr, outLen), nil
112}