Merge pull request #245 from go-vgo/dev

Support for multiple screens
This commit is contained in:
vz 2019-12-01 11:18:51 -04:00 committed by GitHub
commit 6417b546fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 148 additions and 85 deletions

View File

@ -5,6 +5,7 @@
#include "os.h"
#include "inline_keywords.h" /* For H_INLINE */
#include <stddef.h>
#include <stdint.h>
/* Some generic, cross-platform types. */
@ -15,6 +16,13 @@ struct _MMPoint {
typedef struct _MMPoint MMPoint;
struct _MMPointInt32 {
int32_t x;
int32_t y;
};
typedef struct _MMPointInt32 MMPointInt32;
struct _MMSize {
size_t width;
size_t height;
@ -22,6 +30,14 @@ struct _MMSize {
typedef struct _MMSize MMSize;
struct _MMSizeInt32 {
int32_t w;
int32_t h;
};
typedef struct _MMSizeInt32 MMSizeInt32;
struct _MMRect {
MMPoint origin;
MMSize size;
@ -29,6 +45,13 @@ struct _MMRect {
typedef struct _MMRect MMRect;
struct _MMRectInt32 {
MMPointInt32 origin;
MMSizeInt32 size;
};
typedef struct _MMRectInt32 MMRectInt32;
H_INLINE MMPoint MMPointMake(size_t x, size_t y)
{
MMPoint point;
@ -37,6 +60,14 @@ H_INLINE MMPoint MMPointMake(size_t x, size_t y)
return point;
}
H_INLINE MMPointInt32 MMPointInt32Make(int32_t x, int32_t y)
{
MMPointInt32 point;
point.x = x;
point.y = y;
return point;
}
H_INLINE MMSize MMSizeMake(size_t width, size_t height)
{
MMSize size;
@ -45,6 +76,14 @@ H_INLINE MMSize MMSizeMake(size_t width, size_t height)
return size;
}
H_INLINE MMSizeInt32 MMSizeInt32Make(int32_t w, int32_t h)
{
MMSizeInt32 size;
size.w = w;
size.h = h;
return size;
}
H_INLINE MMRect MMRectMake(size_t x, size_t y, size_t width, size_t height)
{
MMRect rect;
@ -53,6 +92,15 @@ H_INLINE MMRect MMRectMake(size_t x, size_t y, size_t width, size_t height)
return rect;
}
H_INLINE MMRectInt32 MMRectInt32Make(int32_t x, int32_t y, int32_t w, int32_t h)
{
MMRectInt32 rect;
rect.origin = MMPointInt32Make(x, y);
rect.size = MMSizeInt32Make(w, h);
return rect;
}
//
#define MMPointZero MMPointMake(0, 0)
#if defined(IS_MACOSX)
@ -60,9 +108,13 @@ H_INLINE MMRect MMRectMake(size_t x, size_t y, size_t width, size_t height)
#define CGPointFromMMPoint(p) CGPointMake((CGFloat)(p).x, (CGFloat)(p).y)
#define MMPointFromCGPoint(p) MMPointMake((size_t)(p).x, (size_t)(p).y)
#define CGPointFromMMPointInt32(p) CGPointMake((CGFloat)(p).x, (CGFloat)(p).y)
#define MMPointInt32FromCGPoint(p) MMPointInt32Make((int32_t)(p).x, (int32_t)(p).y)
#elif defined(IS_WINDOWS)
#define MMPointFromPOINT(p) MMPointMake((size_t)p.x, (size_t)p.y)
#define MMPointInt32FromPOINT(p) MMPointInt32Make((int32_t)p.x, (int32_t)p.y)
#endif

View File

@ -16,7 +16,7 @@ int mouseDelay = 10;
// int keyboardDelay = 10;
// int CheckMouseButton(const char * const b,
// int CheckMouseButton(const char * const b,
// MMMouseButton * const button){
// if (!button) return -1;
@ -35,24 +35,24 @@ int mouseDelay = 10;
// return 0;
// }
int move_mouse(size_t x, size_t y){
MMPoint point;
int move_mouse(int32_t x, int32_t y){
MMPointInt32 point;
// int x = 103;
// int y = 104;
point = MMPointMake(x, y);
point = MMPointInt32Make(x, y);
moveMouse(point);
return 0;
}
int drag_mouse(size_t x, size_t y, MMMouseButton button){
int drag_mouse(int32_t x, int32_t y, MMMouseButton button){
// const size_t x = 10;
// const size_t y = 20;
// MMMouseButton button = LEFT_BUTTON;
MMPoint point;
point = MMPointMake(x, y);
MMPointInt32 point;
point = MMPointInt32Make(x, y);
dragMouse(point, button);
microsleep(mouseDelay);
@ -70,8 +70,8 @@ bool move_mouse_smooth(size_t x, size_t y, double lowSpeed,
return cbool;
}
MMPoint get_mouse_pos(){
MMPoint pos = getMousePos();
MMPointInt32 get_mouse_pos(){
MMPointInt32 pos = getMousePos();
// Return object with .x and .y.
// printf("%zu\n%zu\n", pos.x, pos.y );
@ -105,7 +105,7 @@ int mouse_toggle(char* d, MMMouseButton button){
toggleMouse(down, button);
microsleep(mouseDelay);
return 0;
}

View File

@ -65,24 +65,24 @@ typedef int MMMouseWheelDirection;
/* Immediately moves the mouse to the given point on-screen.
* It is up to the caller to ensure that this point is within the
* screen boundaries. */
void moveMouse(MMPoint point);
void moveMouse(MMPointInt32 point);
/* Like moveMouse, moves the mouse to the given point on-screen, but marks
* the event as the mouse being dragged on platforms where it is supported.
* It is up to the caller to ensure that this point is within the screen
* boundaries. */
void dragMouse(MMPoint point, const MMMouseButton button);
void dragMouse(MMPointInt32 point, const MMMouseButton button);
/* Smoothly moves the mouse from the current position to the given point.
* deadbeef_srand() should be called before using this function.
*
* Returns false if unsuccessful (i.e. a point was hit that is outside of the
* screen boundaries), or true if successful. */
bool smoothlyMoveMouse(MMPoint endPoint, double lowSpeed, double highSpeed);
bool smoothlyMoveMouse(MMPoint endPoint, double lowSpeed, double highSpeed);
// bool smoothlyMoveMouse(MMPoint point);
/* Returns the coordinates of the mouse on the current screen. */
MMPoint getMousePos(void);
MMPointInt32 getMousePos(void);
/* Holds down or releases the mouse with the given button in the current
* position. */

View File

@ -66,7 +66,7 @@
* @param event The mouse move event (by ref).
* @param point The new mouse x and y.
*/
void calculateDeltas(CGEventRef *event, MMPoint point){
void calculateDeltas(CGEventRef *event, MMPointInt32 point){
/**
* The next few lines are a workaround for games not detecting mouse moves.
* See this issue for more information:
@ -91,10 +91,10 @@ void calculateDeltas(CGEventRef *event, MMPoint point){
* Move the mouse to a specific point.
* @param point The coordinates to move the mouse to (x, y).
*/
void moveMouse(MMPoint point){
void moveMouse(MMPointInt32 point){
#if defined(IS_MACOSX)
CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
CGPointFromMMPoint(point),
CGPointFromMMPointInt32(point),
kCGMouseButtonLeft);
calculateDeltas(&move, point);
@ -108,7 +108,7 @@ void moveMouse(MMPoint point){
XSync(display, false);
#elif defined(IS_WINDOWS)
// Mouse motion is now done using SendInput with MOUSEINPUT.
// Mouse motion is now done using SendInput with MOUSEINPUT.
// We use Absolute mouse positioning
#define MOUSE_COORD_TO_ABS(coord, width_or_height) ( \
((65536 * coord) / width_or_height) + (coord < 0 ? -1 : 1))
@ -129,7 +129,7 @@ void moveMouse(MMPoint point){
#endif
}
void dragMouse(MMPoint point, const MMMouseButton button){
void dragMouse(MMPointInt32 point, const MMMouseButton button){
#if defined(IS_MACOSX)
const CGEventType dragType = MMMouseDragToCGEventType(button);
CGEventRef drag = CGEventCreateMouseEvent(NULL, dragType,
@ -144,13 +144,13 @@ void dragMouse(MMPoint point, const MMMouseButton button){
#endif
}
MMPoint getMousePos(){
MMPointInt32 getMousePos(){
#if defined(IS_MACOSX)
CGEventRef event = CGEventCreate(NULL);
CGPoint point = CGEventGetLocation(event);
CFRelease(event);
return MMPointFromCGPoint(point);
return MMPointInt32FromCGPoint(point);
#elif defined(USE_X11)
int x, y; /* This is all we care about. Seriously. */
Window garb1, garb2; /* Why you can't specify NULL as a parameter */
@ -161,12 +161,12 @@ MMPoint getMousePos(){
XQueryPointer(display, XDefaultRootWindow(display), &garb1, &garb2,
&x, &y, &garb_x, &garb_y, &more_garbage);
return MMPointMake(x, y);
return MMPointInt32Make(x, y);
#elif defined(IS_WINDOWS)
POINT point;
GetCursorPos(&point);
return MMPointFromPOINT(point);
return MMPointInt32FromPOINT(point);
#endif
}
@ -181,7 +181,7 @@ void toggleMouse(bool down, MMMouseButton button){
const CGEventType mouseType = MMMouseToCGEventType(down, button);
CGEventRef event = CGEventCreateMouseEvent(NULL,
mouseType, currentPos, (CGMouseButton)button);
CGEventPost(kCGSessionEventTap, event);
CFRelease(event);
#elif defined(USE_X11)
@ -192,7 +192,7 @@ void toggleMouse(bool down, MMMouseButton button){
// mouse_event(MMMouseToMEventF(down, button), 0, 0, 0, 0);
INPUT mouseInput;
mouseInput.type = INPUT_MOUSE;
mouseInput.mi.dx = 0;
mouseInput.mi.dy = 0;
@ -272,7 +272,7 @@ void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection){
/* Make scroll magnitude negative if we're scrolling down. */
cleanScrollMagnitude = cleanScrollMagnitude * scrollDirection;
event = CGEventCreateScrollWheelEvent(NULL,
event = CGEventCreateScrollWheelEvent(NULL,
kCGScrollEventUnitLine, wheel, cleanScrollMagnitude, 0);
CGEventPost(kCGHIDEventTap, event);
@ -403,15 +403,15 @@ static double crude_hypot(double x, double y){
}
bool smoothlyMoveMouse(MMPoint endPoint, double lowSpeed, double highSpeed){
MMPoint pos = getMousePos();
MMSize screenSize = getMainDisplaySize();
MMPointInt32 pos = getMousePos();
MMSizeInt32 screenSize = getMainDisplaySize();
double velo_x = 0.0, velo_y = 0.0;
double distance;
while ((distance =
while ((distance =
crude_hypot((double)pos.x - endPoint.x, (double)pos.y - endPoint.y)
) > 1.0) {
double gravity = DEADBEEF_UNIFORM(5.0, 500.0);
// double gravity = DEADBEEF_UNIFORM(lowSpeed, highSpeed);
double veloDistance;
@ -428,11 +428,11 @@ bool smoothlyMoveMouse(MMPoint endPoint, double lowSpeed, double highSpeed){
/* Make sure we are in the screen boundaries!
* (Strange things will happen if we are not.) */
if (pos.x >= screenSize.width || pos.y >= screenSize.height) {
if (pos.x >= screenSize.w || pos.y >= screenSize.h) {
return false;
}
moveMouse(pos);
moveMouse(MMPointInt32Make((int32_t)pos.x, (int32_t)pos.y));
/* Wait 1 - 3 milliseconds. */
microsleep(DEADBEEF_UNIFORM(lowSpeed, highSpeed));

View File

@ -184,8 +184,8 @@ func RgbToHex(r, g, b uint8) C.uint32_t {
// GetPxColor get pixel color return C.MMRGBHex
func GetPxColor(x, y int) C.MMRGBHex {
cx := C.size_t(x)
cy := C.size_t(y)
cx := C.int32_t(x)
cy := C.int32_t(y)
color := C.get_px_color(cx, cy)
return color
@ -193,8 +193,8 @@ func GetPxColor(x, y int) C.MMRGBHex {
// GetPixelColor get pixel color return string
func GetPixelColor(x, y int) string {
cx := C.size_t(x)
cy := C.size_t(y)
cx := C.int32_t(x)
cy := C.int32_t(y)
color := C.get_pixel_color(cx, cy)
gcolor := C.GoString(color)
@ -223,7 +223,7 @@ func ScaleY() int {
func GetScreenSize() (int, int) {
size := C.get_screen_size()
// fmt.Println("...", size, size.width)
return int(size.width), int(size.height)
return int(size.w), int(size.h)
}
// Scale get the screen scale
@ -286,21 +286,21 @@ func GetXDisplayName() string {
//
// robotgo.CaptureScreen(x, y, w, h int)
func CaptureScreen(args ...int) C.MMBitmapRef {
var x, y, w, h C.size_t
var x, y, w, h C.int32_t
if len(args) > 3 {
x = C.size_t(args[0])
y = C.size_t(args[1])
w = C.size_t(args[2])
h = C.size_t(args[3])
x = C.int32_t(args[0])
y = C.int32_t(args[1])
w = C.int32_t(args[2])
h = C.int32_t(args[3])
} else {
x = 0
y = 0
// Get screen size.
var displaySize C.MMSize
var displaySize C.MMSizeInt32
displaySize = C.getMainDisplaySize()
w = displaySize.width
h = displaySize.height
w = displaySize.w
h = displaySize.h
}
bit := C.capture_screen(x, y, w, h)
@ -359,8 +359,8 @@ func MoveMouse(x, y int) {
// Move move the mouse
func Move(x, y int) {
cx := C.size_t(x)
cy := C.size_t(y)
cx := C.int32_t(x)
cy := C.int32_t(y)
C.move_mouse(cx, cy)
}
@ -373,8 +373,8 @@ func DragMouse(x, y int, args ...string) {
func Drag(x, y int, args ...string) {
var button C.MMMouseButton = C.LEFT_BUTTON
cx := C.size_t(x)
cy := C.size_t(y)
cx := C.int32_t(x)
cy := C.int32_t(y)
if len(args) > 0 {
button = CheckMouse(args[0])

View File

@ -14,6 +14,7 @@ package robotgo
import (
"fmt"
"log"
"runtime"
"testing"
@ -60,3 +61,8 @@ func TestScrollMouse(t *testing.T) {
Scroll(210, 210)
}
func TestGetScreenSize(t *testing.T) {
x, y := GetScreenSize()
log.Println("GetScreenSize: ", x, y)
}

View File

@ -44,15 +44,15 @@ uint32_t color_rgb_to_hex(uint8_t r, uint8_t g, uint8_t b){
return RGB_TO_HEX(r, g, b);
}
MMRGBHex get_px_color(size_t x, size_t y){
MMRGBHex get_px_color(int32_t x, int32_t y){
MMBitmapRef bitmap;
MMRGBHex color;
if (!pointVisibleOnMainDisplay(MMPointMake(x, y))){
if (!pointVisibleOnMainDisplay(MMPointInt32Make(x, y))){
return color;
}
bitmap = copyMMBitmapFromDisplayInRect(MMRectMake(x, y, 1, 1));
bitmap = copyMMBitmapFromDisplayInRect(MMRectInt32Make(x, y, 1, 1));
// bitmap = MMRectMake(x, y, 1, 1);
color = MMRGBHexAtPoint(bitmap, 0, 0);
@ -61,16 +61,16 @@ MMRGBHex get_px_color(size_t x, size_t y){
return color;
}
char* get_pixel_color(size_t x, size_t y){
char* get_pixel_color(int32_t x, int32_t y){
MMRGBHex color = get_px_color(x, y);
char* s = pad_hex(color);
return s;
}
MMSize get_screen_size(){
MMSizeInt32 get_screen_size(){
// Get display size.
MMSize displaySize = getMainDisplaySize();
MMSizeInt32 displaySize = getMainDisplaySize();
return displaySize;
}
@ -96,7 +96,7 @@ char* get_XDisplay_name(){
}
// capture_screen capture screen
MMBitmapRef capture_screen(size_t x, size_t y, size_t w, size_t h){
MMBitmapRef capture_screen(int32_t x, int32_t y, int32_t w, int32_t h){
// if (){
// x = 0;
// y = 0;
@ -105,7 +105,7 @@ MMBitmapRef capture_screen(size_t x, size_t y, size_t w, size_t h){
// w = displaySize.width;
// h = displaySize.height;
// }
MMBitmapRef bitmap = copyMMBitmapFromDisplayInRect(MMRectMake(x, y, w, h));
MMBitmapRef bitmap = copyMMBitmapFromDisplayInRect(MMRectInt32Make(x, y, w, h));
// printf("%s\n", bitmap);
return bitmap;
}

View File

@ -16,11 +16,11 @@ extern "C"
#endif
/* Returns the size of the main display. */
MMSize getMainDisplaySize(void);
MMSizeInt32 getMainDisplaySize(void);
/* Convenience function that returns whether the given point is in the bounds
* of the main screen. */
bool pointVisibleOnMainDisplay(MMPoint point);
bool pointVisibleOnMainDisplay(MMPointInt32 point);
#ifdef __cplusplus
}

View File

@ -8,24 +8,29 @@
// #include "../base/xdisplay_c.h"
#endif
MMSize getMainDisplaySize(void){
MMSizeInt32 getMainDisplaySize(void){
#if defined(IS_MACOSX)
CGDirectDisplayID displayID = CGMainDisplayID();
return MMSizeMake(CGDisplayPixelsWide(displayID),
CGDisplayPixelsHigh(displayID));
return MMSizeInt32Make((int32_t)CGDisplayPixelsWide(displayID),
(int32_t)CGDisplayPixelsHigh(displayID));
#elif defined(USE_X11)
Display *display = XGetMainDisplay();
const int screen = DefaultScreen(display);
return MMSizeMake((size_t)DisplayWidth(display, screen),
(size_t)DisplayHeight(display, screen));
return MMSizeInt32Make((int32_t)DisplayWidth(display, screen),
(int32_t)DisplayHeight(display, screen));
#elif defined(IS_WINDOWS)
return MMSizeMake((size_t)GetSystemMetrics(SM_CXSCREEN),
(size_t)GetSystemMetrics(SM_CYSCREEN));
if (GetSystemMetrics(SM_CMONITORS) == 1) {
return MMSizeInt32Make((int32_t)GetSystemMetrics(SM_CXSCREEN),
(int32_t)GetSystemMetrics(SM_CYSCREEN));
} else {
return MMSizeInt32Make((int32_t)GetSystemMetrics(SM_CXVIRTUALSCREEN),
(int32_t)GetSystemMetrics(SM_CYVIRTUALSCREEN));
}
#endif
}
bool pointVisibleOnMainDisplay(MMPoint point){
MMSize displaySize = getMainDisplaySize();
return point.x < displaySize.width && point.y < displaySize.height;
bool pointVisibleOnMainDisplay(MMPointInt32 point){
MMSizeInt32 displaySize = getMainDisplaySize();
return point.x < displaySize.w && point.y < displaySize.h;
}

View File

@ -12,7 +12,7 @@ extern "C"
/* Returns a raw bitmap of screengrab of the display (to be destroyed()'d by
* caller), or NULL on error. */
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect);
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect);
#ifdef __cplusplus
}

View File

@ -17,7 +17,7 @@
#include <string.h>
#endif
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect){
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect){
#if defined(IS_MACOSX)
MMBitmapRef bitmap = NULL;
@ -29,8 +29,8 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect){
CGImageRef image = CGDisplayCreateImageForRect(displayID,
CGRectMake(rect.origin.x,
rect.origin.y,
rect.size.width,
rect.size.height));
rect.size.w,
rect.size.h));
if (!image) { return NULL; }
@ -63,15 +63,15 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect){
XDefaultRootWindow(display),
(int)rect.origin.x,
(int)rect.origin.y,
(unsigned int)rect.size.width,
(unsigned int)rect.size.height,
(unsigned int)rect.size.w,
(unsigned int)rect.size.h,
AllPlanes, ZPixmap);
XCloseDisplay(display);
if (image == NULL) return NULL;
bitmap = createMMBitmap((uint8_t *)image->data,
rect.size.width,
rect.size.height,
rect.size.w,
rect.size.h,
(size_t)image->bytes_per_line,
(uint8_t)image->bits_per_pixel,
(uint8_t)image->bits_per_pixel / 8);
@ -89,12 +89,12 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect){
/* Initialize bitmap info. */
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth = (long)rect.size.width;
bi.bmiHeader.biHeight = -(long)rect.size.height; /* Non-cartesian, please */
bi.bmiHeader.biWidth = (long)rect.size.w;
bi.bmiHeader.biHeight = -(long)rect.size.h; /* Non-cartesian, please */
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = (DWORD)(4 * rect.size.width * rect.size.height);
bi.bmiHeader.biSizeImage = (DWORD)(4 * rect.size.w * rect.size.h);
bi.bmiHeader.biXPelsPerMeter = 0;
bi.bmiHeader.biYPelsPerMeter = 0;
bi.bmiHeader.biClrUsed = 0;
@ -112,8 +112,8 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect){
!BitBlt(screenMem,
(int)0,
(int)0,
(int)rect.size.width,
(int)rect.size.height,
(int)rect.size.w,
(int)rect.size.h,
screen,
rect.origin.x,
rect.origin.y,
@ -128,9 +128,9 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect){
}
bitmap = createMMBitmap(NULL,
rect.size.width,
rect.size.height,
4 * rect.size.width,
rect.size.w,
rect.size.h,
4 * rect.size.w,
(uint8_t)bi.bmiHeader.biBitCount,
4);