forked from lwch/rdesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot_darwin.m
56 lines (53 loc) · 2.25 KB
/
screenshot_darwin.m
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
#import <AppKit/NSCursor.h>
#import <AppKit/NSImage.h>
#import <AppKit/NSColor.h>
void get_cursor_size(int *width, int *height) {
NSCursor *cursor = [NSCursor currentSystemCursor];
NSImage *image = [cursor image];
NSSize size = [image size];
*width = size.width;
*height = size.height;
}
void cursor_copy(unsigned char *pixels, int width, int height) {
NSCursor *cursor = [NSCursor currentSystemCursor];
NSImage *image = [cursor image];
NSSize size = [image size];
CGImageRef CGImage = [image CGImageForProposedRect:nil context:nil hints:nil];
NSBitmapImageRep *bitmap = [[[NSBitmapImageRep alloc] initWithCGImage:CGImage] autorelease];
for (int y = 0; y < height; y++) {
if (y > size.height)
break;
for (int x = 0; x < width; x++) {
if (x > size.width)
break;
NSColor *color = [bitmap colorAtX:x y:y];
pixels[y * width * 4 + x * 4 + 0] = [color redComponent] * 255;
pixels[y * width * 4 + x * 4 + 1] = [color greenComponent] * 255;
pixels[y * width * 4 + x * 4 + 2] = [color blueComponent] * 255;
pixels[y * width * 4 + x * 4 + 3] = [color alphaComponent] * 255;
}
}
}
void screenshot(unsigned char *pixels, int width, int height, bool show_cursor) {
CGImageRef screenshot = nil;
if (show_cursor) {
screenshot = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
} else {
screenshot = CGDisplayCreateImage(kCGDirectMainDisplay);
}
NSBitmapImageRep *bitmap = [[[NSBitmapImageRep alloc] initWithCGImage:screenshot] autorelease];
NSSize size = [bitmap size];
for (int y = 0; y < height; y++) {
if (y > size.height)
break;
for (int x = 0; x < width; x++) {
if (x > size.width)
break;
NSColor *color = [bitmap colorAtX:x y:y];
pixels[y * width * 4 + x * 4 + 0] = [color redComponent] * 255;
pixels[y * width * 4 + x * 4 + 1] = [color greenComponent] * 255;
pixels[y * width * 4 + x * 4 + 2] = [color blueComponent] * 255;
pixels[y * width * 4 + x * 4 + 3] = [color alphaComponent] * 255;
}
}
}