Skip to content

Commit 85f2702

Browse files
authored
Merge pull request #682 from go-vgo/bitmap-pr
Update: update screengrab to fixed macos 15
2 parents d3e364f + 5b9871b commit 85f2702

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

robotgo.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ package robotgo
3333
#cgo darwin CFLAGS: -x objective-c -Wno-deprecated-declarations
3434
#cgo darwin LDFLAGS: -framework Cocoa -framework OpenGL -framework IOKit
3535
#cgo darwin LDFLAGS: -framework Carbon -framework CoreFoundation
36+
//
37+
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > MAC_OS_VERSION_14_4
38+
#cgo darwin LDFLAGS: -framework ScreenCaptureKit
39+
#endif
3640
3741
#cgo linux CFLAGS: -I/usr/src
3842
#cgo linux LDFLAGS: -L/usr/src -lm -lX11 -lXtst

screen/screengrab_c.h

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <OpenGL/OpenGL.h>
66
#include <OpenGL/gl.h>
77
#include <ApplicationServices/ApplicationServices.h>
8+
#include <ScreenCaptureKit/ScreenCaptureKit.h>
89
#elif defined(USE_X11)
910
#include <X11/Xlib.h>
1011
#include <X11/Xutil.h>
@@ -13,6 +14,49 @@
1314
#include <string.h>
1415
#endif
1516

17+
#if defined(IS_MACOSX) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > MAC_OS_VERSION_14_4
18+
static CGImageRef capture15(CGDirectDisplayID id, CGRect diIntersectDisplayLocal, CGColorSpaceRef colorSpace) {
19+
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
20+
__block CGImageRef image1 = nil;
21+
[SCShareableContent getShareableContentWithCompletionHandler:^(SCShareableContent* content, NSError* error) {
22+
@autoreleasepool {
23+
if (error) {
24+
dispatch_semaphore_signal(semaphore);
25+
return;
26+
}
27+
SCDisplay* target = nil;
28+
for (SCDisplay *display in content.displays) {
29+
if (display.displayID == id) {
30+
target = display;
31+
break;
32+
}
33+
}
34+
if (!target) {
35+
dispatch_semaphore_signal(semaphore);
36+
return;
37+
}
38+
39+
SCContentFilter* filter = [[SCContentFilter alloc] initWithDisplay:target excludingWindows:@[]];
40+
SCStreamConfiguration* config = [[SCStreamConfiguration alloc] init];
41+
config.sourceRect = diIntersectDisplayLocal;
42+
config.width = diIntersectDisplayLocal.size.width;
43+
config.height = diIntersectDisplayLocal.size.height;
44+
[SCScreenshotManager captureImageWithFilter:filter
45+
configuration:config
46+
completionHandler:^(CGImageRef img, NSError* error) {
47+
if (!error) {
48+
image1 = CGImageCreateCopyWithColorSpace(img, colorSpace);
49+
}
50+
dispatch_semaphore_signal(semaphore);
51+
}];
52+
}
53+
}];
54+
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
55+
dispatch_release(semaphore);
56+
return image1;
57+
}
58+
#endif
59+
1660
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id, int8_t isPid) {
1761
#if defined(IS_MACOSX)
1862
MMBitmapRef bitmap = NULL;
@@ -25,9 +69,16 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id,
2569
}
2670

2771
MMPointInt32 o = rect.origin; MMSizeInt32 s = rect.size;
28-
CGImageRef image = CGDisplayCreateImageForRect(displayID, CGRectMake(o.x, o.y, s.w, s.h));
72+
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > MAC_OS_VERSION_14_4
73+
CGColorSpaceRef color = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
74+
CGImageRef image = capture15(displayID, CGRectMake(o.x, o.y, s.w, s.h), color);
75+
CGColorSpaceRelease(color);
76+
#else
77+
// This API is deprecated in macos 15, use ScreenCaptureKit's captureScreenshot
78+
CGImageRef image = CGDisplayCreateImageForRect(displayID, CGRectMake(o.x, o.y, s.w, s.h));
79+
#endif
2980
if (!image) { return NULL; }
30-
81+
3182
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(image));
3283
if (!imageData) { return NULL; }
3384

0 commit comments

Comments
 (0)