Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build/
node_modules/
prebuilds/
/.idea
/.idea
/.vscode
8 changes: 4 additions & 4 deletions src/robotjs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,8 @@ NAN_METHOD(getPixelColor)
MMBitmapRef bitmap;
MMRGBHex color;

size_t x = Nan::To<int32_t>(info[0]).FromJust();
size_t y = Nan::To<int32_t>(info[1]).FromJust();
int32_t x = Nan::To<int32_t>(info[0]).FromJust();
int32_t y = Nan::To<int32_t>(info[1]).FromJust();

if (!pointVisibleOnMainDisplay(MMPointMake(x, y)))
{
Expand Down Expand Up @@ -797,8 +797,8 @@ NAN_METHOD(setXDisplayName)

NAN_METHOD(captureScreen)
{
size_t x;
size_t y;
int32_t x;
int32_t y;
size_t w;
size_t h;

Expand Down
36 changes: 36 additions & 0 deletions src/screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,42 @@
#include "xdisplay.h"
#endif

#if defined(IS_MACOSX)
uint32_t getIDOfDisplayWithRect(MMRect rect)
{
CGError cgErr;
CGDisplayCount displayCount;
CGDisplayCount maxDisplays = 1;
CGDirectDisplayID onlineDisplays[1];
CGRect cgRect = CGRectMake(rect.origin.x,
rect.origin.y,
rect.size.width,
rect.size.height);

// If CGGetOnlineDisplayList() is not called, CGGetDisplaysWithRect() fails
CGGetOnlineDisplayList(maxDisplays, onlineDisplays, &displayCount);
cgErr = CGGetDisplaysWithRect(cgRect, maxDisplays, onlineDisplays, &displayCount);

if (cgErr != kCGErrorSuccess)
{
fprintf(stderr, "CGGetDisplaysWithRect: error %d.\n", cgErr);
exit(1);
}
if (displayCount == 0)
{
fprintf(stderr,
"No display with rect with origin (%d, %d), width %d and height %d.\n",
(int) cgRect.origin.x,
(int) cgRect.origin.y,
(int) cgRect.size.width,
(int) cgRect.size.height);
exit(1);
}

return onlineDisplays[0];
};
#endif

MMSignedSize getMainDisplaySize(void)
{
#if defined(IS_MACOSX)
Expand Down
4 changes: 4 additions & 0 deletions src/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ extern "C"
{
#endif

#if defined(IS_MACOSX)
uint32_t getIDOfDisplayWithRect(MMRect rect);
#endif

/* Returns the size of the main display. */
MMSignedSize getMainDisplaySize(void);

Expand Down
8 changes: 5 additions & 3 deletions src/screengrab.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdlib.h> /* malloc() */

#if defined(IS_MACOSX)
#include "screen.h"
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include <ApplicationServices/ApplicationServices.h>
Expand All @@ -23,11 +24,12 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
uint8_t *buffer = NULL;
size_t bufferSize = 0;

CGDirectDisplayID displayID = CGMainDisplayID();
CGDirectDisplayID displayID = getIDOfDisplayWithRect(rect);
CGRect bounds = CGDisplayBounds(displayID);

CGImageRef image = CGDisplayCreateImageForRect(displayID,
CGRectMake(rect.origin.x,
rect.origin.y,
CGRectMake(rect.origin.x - (int32_t) bounds.origin.x,
rect.origin.y - (int32_t) bounds.origin.y,
rect.size.width,
rect.size.height));

Expand Down
6 changes: 3 additions & 3 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct _MMSignedSize {
typedef struct _MMSignedSize MMSignedSize;

struct _MMRect {
MMPoint origin;
MMSignedPoint origin;
MMSize size;
};

Expand Down Expand Up @@ -76,10 +76,10 @@ H_INLINE MMSignedSize MMSignedSizeMake(int32_t width, int32_t height)
return size;
}

H_INLINE MMRect MMRectMake(size_t x, size_t y, size_t width, size_t height)
H_INLINE MMRect MMRectMake(int32_t x, int32_t y, size_t width, size_t height)
{
MMRect rect;
rect.origin = MMPointMake(x, y);
rect.origin = MMSignedPointMake(x, y);
rect.size = MMSizeMake(width, height);
return rect;
}
Expand Down