-
Notifications
You must be signed in to change notification settings - Fork 177
/
darwin.go
251 lines (219 loc) · 7.75 KB
/
darwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//go:build cgo && darwin
package screenshot
/*
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > MAC_OS_VERSION_14_4
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework CoreGraphics -framework CoreFoundation -framework ScreenCaptureKit
#include <ScreenCaptureKit/ScreenCaptureKit.h>
#else
#cgo LDFLAGS: -framework CoreGraphics -framework CoreFoundation
#endif
#include <CoreGraphics/CoreGraphics.h>
static CGImageRef capture(CGDirectDisplayID id, CGRect diIntersectDisplayLocal, CGColorSpaceRef colorSpace) {
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > MAC_OS_VERSION_14_4
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block CGImageRef result = nil;
[SCShareableContent getShareableContentWithCompletionHandler:^(SCShareableContent* content, NSError* error) {
@autoreleasepool {
if (error) {
dispatch_semaphore_signal(semaphore);
return;
}
SCDisplay* target = nil;
for (SCDisplay *display in content.displays) {
if (display.displayID == id) {
target = display;
break;
}
}
if (!target) {
dispatch_semaphore_signal(semaphore);
return;
}
SCContentFilter* filter = [[SCContentFilter alloc] initWithDisplay:target excludingWindows:@[]];
SCStreamConfiguration* config = [[SCStreamConfiguration alloc] init];
config.sourceRect = diIntersectDisplayLocal;
config.width = diIntersectDisplayLocal.size.width;
config.height = diIntersectDisplayLocal.size.height;
[SCScreenshotManager captureImageWithFilter:filter
configuration:config
completionHandler:^(CGImageRef img, NSError* error) {
if (!error) {
result = CGImageCreateCopyWithColorSpace(img, colorSpace);
}
dispatch_semaphore_signal(semaphore);
}];
}
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_release(semaphore);
return result;
#else
CGImageRef img = CGDisplayCreateImageForRect(id, diIntersectDisplayLocal);
if (!img) {
return nil;
}
CGImageRef copy = CGImageCreateCopyWithColorSpace(img, colorSpace);
CGImageRelease(img);
if (!copy) {
return nil;
}
return copy;
#endif
}
*/
import "C"
import (
"errors"
"image"
"unsafe"
)
func Capture(x, y, width, height int) (*image.RGBA, error) {
if width <= 0 || height <= 0 {
return nil, errors.New("width or height should be > 0")
}
rect := image.Rect(0, 0, width, height)
img, err := createImage(rect)
if err != nil {
return nil, err
}
// cg: CoreGraphics coordinate (origin: lower-left corner of primary display, x-axis: rightward, y-axis: upward)
// win: Windows coordinate (origin: upper-left corner of primary display, x-axis: rightward, y-axis: downward)
// di: Display local coordinate (origin: upper-left corner of the display, x-axis: rightward, y-axis: downward)
cgMainDisplayBounds := getCoreGraphicsCoordinateOfDisplay(C.CGMainDisplayID())
winBottomLeft := C.CGPointMake(C.CGFloat(x), C.CGFloat(y+height))
cgBottomLeft := getCoreGraphicsCoordinateFromWindowsCoordinate(winBottomLeft, cgMainDisplayBounds)
cgCaptureBounds := C.CGRectMake(cgBottomLeft.x, cgBottomLeft.y, C.CGFloat(width), C.CGFloat(height))
ids := activeDisplayList()
ctx := createBitmapContext(width, height, (*C.uint32_t)(unsafe.Pointer(&img.Pix[0])), img.Stride)
if ctx == 0 {
return nil, errors.New("cannot create bitmap context")
}
colorSpace := createColorspace()
if colorSpace == 0 {
return nil, errors.New("cannot create colorspace")
}
defer C.CGColorSpaceRelease(colorSpace)
for _, id := range ids {
cgBounds := getCoreGraphicsCoordinateOfDisplay(id)
cgIntersect := C.CGRectIntersection(cgBounds, cgCaptureBounds)
if C.CGRectIsNull(cgIntersect) {
continue
}
if cgIntersect.size.width <= 0 || cgIntersect.size.height <= 0 {
continue
}
// CGDisplayCreateImageForRect potentially fail in case width/height is odd number.
if int(cgIntersect.size.width)%2 != 0 {
cgIntersect.size.width = C.CGFloat(int(cgIntersect.size.width) + 1)
}
if int(cgIntersect.size.height)%2 != 0 {
cgIntersect.size.height = C.CGFloat(int(cgIntersect.size.height) + 1)
}
diIntersectDisplayLocal := C.CGRectMake(cgIntersect.origin.x-cgBounds.origin.x,
cgBounds.origin.y+cgBounds.size.height-(cgIntersect.origin.y+cgIntersect.size.height),
cgIntersect.size.width, cgIntersect.size.height)
image := C.capture(id, diIntersectDisplayLocal, colorSpace)
if unsafe.Pointer(image) == nil {
return nil, errors.New("cannot capture display")
}
defer C.CGImageRelease(image)
cgDrawRect := C.CGRectMake(cgIntersect.origin.x-cgCaptureBounds.origin.x, cgIntersect.origin.y-cgCaptureBounds.origin.y,
cgIntersect.size.width, cgIntersect.size.height)
C.CGContextDrawImage(ctx, cgDrawRect, image)
}
i := 0
for iy := 0; iy < height; iy++ {
j := i
for ix := 0; ix < width; ix++ {
// ARGB => RGBA, and set A to 255
img.Pix[j], img.Pix[j+1], img.Pix[j+2], img.Pix[j+3] = img.Pix[j+1], img.Pix[j+2], img.Pix[j+3], 255
j += 4
}
i += img.Stride
}
return img, nil
}
func NumActiveDisplays() int {
var count C.uint32_t = 0
if C.CGGetActiveDisplayList(0, nil, &count) == C.kCGErrorSuccess {
return int(count)
} else {
return 0
}
}
func GetDisplayBounds(displayIndex int) image.Rectangle {
id := getDisplayId(displayIndex)
main := C.CGMainDisplayID()
var rect image.Rectangle
bounds := getCoreGraphicsCoordinateOfDisplay(id)
rect.Min.X = int(bounds.origin.x)
if main == id {
rect.Min.Y = 0
} else {
mainBounds := getCoreGraphicsCoordinateOfDisplay(main)
mainHeight := mainBounds.size.height
rect.Min.Y = int(mainHeight - (bounds.origin.y + bounds.size.height))
}
rect.Max.X = rect.Min.X + int(bounds.size.width)
rect.Max.Y = rect.Min.Y + int(bounds.size.height)
return rect
}
func getDisplayId(displayIndex int) C.CGDirectDisplayID {
main := C.CGMainDisplayID()
if displayIndex == 0 {
return main
} else {
n := NumActiveDisplays()
ids := make([]C.CGDirectDisplayID, n)
if C.CGGetActiveDisplayList(C.uint32_t(n), (*C.CGDirectDisplayID)(unsafe.Pointer(&ids[0])), nil) != C.kCGErrorSuccess {
return 0
}
index := 0
for i := 0; i < n; i++ {
if ids[i] == main {
continue
}
index++
if index == displayIndex {
return ids[i]
}
}
}
return 0
}
func getCoreGraphicsCoordinateOfDisplay(id C.CGDirectDisplayID) C.CGRect {
main := C.CGDisplayBounds(C.CGMainDisplayID())
r := C.CGDisplayBounds(id)
return C.CGRectMake(r.origin.x, -r.origin.y-r.size.height+main.size.height,
r.size.width, r.size.height)
}
func getCoreGraphicsCoordinateFromWindowsCoordinate(p C.CGPoint, mainDisplayBounds C.CGRect) C.CGPoint {
return C.CGPointMake(p.x, mainDisplayBounds.size.height-p.y)
}
func createBitmapContext(width int, height int, data *C.uint32_t, bytesPerRow int) C.CGContextRef {
colorSpace := createColorspace()
if colorSpace == 0 {
return 0
}
defer C.CGColorSpaceRelease(colorSpace)
return C.CGBitmapContextCreate(unsafe.Pointer(data),
C.size_t(width),
C.size_t(height),
8, // bits per component
C.size_t(bytesPerRow),
colorSpace,
C.kCGImageAlphaNoneSkipFirst)
}
func createColorspace() C.CGColorSpaceRef {
return C.CGColorSpaceCreateWithName(C.kCGColorSpaceSRGB)
}
func activeDisplayList() []C.CGDirectDisplayID {
count := C.uint32_t(NumActiveDisplays())
ret := make([]C.CGDirectDisplayID, count)
if count > 0 && C.CGGetActiveDisplayList(count, (*C.CGDirectDisplayID)(unsafe.Pointer(&ret[0])), nil) == C.kCGErrorSuccess {
return ret
} else {
return make([]C.CGDirectDisplayID, 0)
}
}