mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-06-01 14:43:55 +00:00
add screen and bitmap multiple screens support
This commit is contained in:
parent
0a4ae24aec
commit
29ae3d4fdf
@ -404,7 +404,7 @@ static double crude_hypot(double x, double y){
|
|||||||
|
|
||||||
bool smoothlyMoveMouse(MMPoint endPoint, double lowSpeed, double highSpeed){
|
bool smoothlyMoveMouse(MMPoint endPoint, double lowSpeed, double highSpeed){
|
||||||
MMPoint pos = getMousePos();
|
MMPoint pos = getMousePos();
|
||||||
MMSize screenSize = getMainDisplaySize();
|
MMSizeInt32 screenSize = getMainDisplaySize();
|
||||||
double velo_x = 0.0, velo_y = 0.0;
|
double velo_x = 0.0, velo_y = 0.0;
|
||||||
double distance;
|
double distance;
|
||||||
|
|
||||||
@ -428,7 +428,7 @@ bool smoothlyMoveMouse(MMPoint endPoint, double lowSpeed, double highSpeed){
|
|||||||
|
|
||||||
/* Make sure we are in the screen boundaries!
|
/* Make sure we are in the screen boundaries!
|
||||||
* (Strange things will happen if we are not.) */
|
* (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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
26
robotgo.go
26
robotgo.go
@ -184,8 +184,8 @@ func RgbToHex(r, g, b uint8) C.uint32_t {
|
|||||||
|
|
||||||
// GetPxColor get pixel color return C.MMRGBHex
|
// GetPxColor get pixel color return C.MMRGBHex
|
||||||
func GetPxColor(x, y int) C.MMRGBHex {
|
func GetPxColor(x, y int) C.MMRGBHex {
|
||||||
cx := C.size_t(x)
|
cx := C.int32_t(x)
|
||||||
cy := C.size_t(y)
|
cy := C.int32_t(y)
|
||||||
|
|
||||||
color := C.get_px_color(cx, cy)
|
color := C.get_px_color(cx, cy)
|
||||||
return color
|
return color
|
||||||
@ -193,8 +193,8 @@ func GetPxColor(x, y int) C.MMRGBHex {
|
|||||||
|
|
||||||
// GetPixelColor get pixel color return string
|
// GetPixelColor get pixel color return string
|
||||||
func GetPixelColor(x, y int) string {
|
func GetPixelColor(x, y int) string {
|
||||||
cx := C.size_t(x)
|
cx := C.int32_t(x)
|
||||||
cy := C.size_t(y)
|
cy := C.int32_t(y)
|
||||||
|
|
||||||
color := C.get_pixel_color(cx, cy)
|
color := C.get_pixel_color(cx, cy)
|
||||||
gcolor := C.GoString(color)
|
gcolor := C.GoString(color)
|
||||||
@ -223,7 +223,7 @@ func ScaleY() int {
|
|||||||
func GetScreenSize() (int, int) {
|
func GetScreenSize() (int, int) {
|
||||||
size := C.get_screen_size()
|
size := C.get_screen_size()
|
||||||
// fmt.Println("...", size, size.width)
|
// fmt.Println("...", size, size.width)
|
||||||
return int(size.width), int(size.height)
|
return int(size.w), int(size.h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scale get the screen scale
|
// Scale get the screen scale
|
||||||
@ -286,21 +286,21 @@ func GetXDisplayName() string {
|
|||||||
//
|
//
|
||||||
// robotgo.CaptureScreen(x, y, w, h int)
|
// robotgo.CaptureScreen(x, y, w, h int)
|
||||||
func CaptureScreen(args ...int) C.MMBitmapRef {
|
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 {
|
if len(args) > 3 {
|
||||||
x = C.size_t(args[0])
|
x = C.int32_t(args[0])
|
||||||
y = C.size_t(args[1])
|
y = C.int32_t(args[1])
|
||||||
w = C.size_t(args[2])
|
w = C.int32_t(args[2])
|
||||||
h = C.size_t(args[3])
|
h = C.int32_t(args[3])
|
||||||
} else {
|
} else {
|
||||||
x = 0
|
x = 0
|
||||||
y = 0
|
y = 0
|
||||||
// Get screen size.
|
// Get screen size.
|
||||||
var displaySize C.MMSize
|
var displaySize C.MMSizeInt32
|
||||||
displaySize = C.getMainDisplaySize()
|
displaySize = C.getMainDisplaySize()
|
||||||
w = displaySize.width
|
w = displaySize.w
|
||||||
h = displaySize.height
|
h = displaySize.h
|
||||||
}
|
}
|
||||||
|
|
||||||
bit := C.capture_screen(x, y, w, h)
|
bit := C.capture_screen(x, y, w, h)
|
||||||
|
@ -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);
|
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;
|
MMBitmapRef bitmap;
|
||||||
MMRGBHex color;
|
MMRGBHex color;
|
||||||
|
|
||||||
if (!pointVisibleOnMainDisplay(MMPointMake(x, y))){
|
if (!pointVisibleOnMainDisplay(MMPointInt32Make(x, y))){
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
bitmap = copyMMBitmapFromDisplayInRect(MMRectMake(x, y, 1, 1));
|
bitmap = copyMMBitmapFromDisplayInRect(MMRectInt32Make(x, y, 1, 1));
|
||||||
// bitmap = MMRectMake(x, y, 1, 1);
|
// bitmap = MMRectMake(x, y, 1, 1);
|
||||||
|
|
||||||
color = MMRGBHexAtPoint(bitmap, 0, 0);
|
color = MMRGBHexAtPoint(bitmap, 0, 0);
|
||||||
@ -61,16 +61,16 @@ MMRGBHex get_px_color(size_t x, size_t y){
|
|||||||
return color;
|
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);
|
MMRGBHex color = get_px_color(x, y);
|
||||||
|
|
||||||
char* s = pad_hex(color);
|
char* s = pad_hex(color);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
MMSize get_screen_size(){
|
MMSizeInt32 get_screen_size(){
|
||||||
// Get display size.
|
// Get display size.
|
||||||
MMSize displaySize = getMainDisplaySize();
|
MMSizeInt32 displaySize = getMainDisplaySize();
|
||||||
return displaySize;
|
return displaySize;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ char* get_XDisplay_name(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// capture_screen capture screen
|
// 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 (){
|
// if (){
|
||||||
// x = 0;
|
// x = 0;
|
||||||
// y = 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;
|
// w = displaySize.width;
|
||||||
// h = displaySize.height;
|
// h = displaySize.height;
|
||||||
// }
|
// }
|
||||||
MMBitmapRef bitmap = copyMMBitmapFromDisplayInRect(MMRectMake(x, y, w, h));
|
MMBitmapRef bitmap = copyMMBitmapFromDisplayInRect(MMRectInt32Make(x, y, w, h));
|
||||||
// printf("%s\n", bitmap);
|
// printf("%s\n", bitmap);
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
@ -16,11 +16,11 @@ extern "C"
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Returns the size of the main display. */
|
/* 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
|
/* Convenience function that returns whether the given point is in the bounds
|
||||||
* of the main screen. */
|
* of the main screen. */
|
||||||
bool pointVisibleOnMainDisplay(MMPoint point);
|
bool pointVisibleOnMainDisplay(MMPointInt32 point);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
@ -8,24 +8,29 @@
|
|||||||
// #include "../base/xdisplay_c.h"
|
// #include "../base/xdisplay_c.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
MMSize getMainDisplaySize(void){
|
MMSizeInt32 getMainDisplaySize(void){
|
||||||
#if defined(IS_MACOSX)
|
#if defined(IS_MACOSX)
|
||||||
CGDirectDisplayID displayID = CGMainDisplayID();
|
CGDirectDisplayID displayID = CGMainDisplayID();
|
||||||
return MMSizeMake(CGDisplayPixelsWide(displayID),
|
return MMSizeInt32Make((int32_t)CGDisplayPixelsWide(displayID),
|
||||||
CGDisplayPixelsHigh(displayID));
|
(int32_t)CGDisplayPixelsHigh(displayID));
|
||||||
#elif defined(USE_X11)
|
#elif defined(USE_X11)
|
||||||
Display *display = XGetMainDisplay();
|
Display *display = XGetMainDisplay();
|
||||||
const int screen = DefaultScreen(display);
|
const int screen = DefaultScreen(display);
|
||||||
|
|
||||||
return MMSizeMake((size_t)DisplayWidth(display, screen),
|
return MMSizeInt32Make((int32_t)DisplayWidth(display, screen),
|
||||||
(size_t)DisplayHeight(display, screen));
|
(int32_t)DisplayHeight(display, screen));
|
||||||
#elif defined(IS_WINDOWS)
|
#elif defined(IS_WINDOWS)
|
||||||
return MMSizeMake((size_t)GetSystemMetrics(SM_CXSCREEN),
|
if (GetSystemMetrics(SM_CMONITORS) == 1) {
|
||||||
(size_t)GetSystemMetrics(SM_CYSCREEN));
|
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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool pointVisibleOnMainDisplay(MMPoint point){
|
bool pointVisibleOnMainDisplay(MMPointInt32 point){
|
||||||
MMSize displaySize = getMainDisplaySize();
|
MMSizeInt32 displaySize = getMainDisplaySize();
|
||||||
return point.x < displaySize.width && point.y < displaySize.height;
|
return point.x < displaySize.w && point.y < displaySize.h;
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ extern "C"
|
|||||||
|
|
||||||
/* Returns a raw bitmap of screengrab of the display (to be destroyed()'d by
|
/* Returns a raw bitmap of screengrab of the display (to be destroyed()'d by
|
||||||
* caller), or NULL on error. */
|
* caller), or NULL on error. */
|
||||||
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect);
|
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect){
|
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect){
|
||||||
#if defined(IS_MACOSX)
|
#if defined(IS_MACOSX)
|
||||||
|
|
||||||
MMBitmapRef bitmap = NULL;
|
MMBitmapRef bitmap = NULL;
|
||||||
@ -29,8 +29,8 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect){
|
|||||||
CGImageRef image = CGDisplayCreateImageForRect(displayID,
|
CGImageRef image = CGDisplayCreateImageForRect(displayID,
|
||||||
CGRectMake(rect.origin.x,
|
CGRectMake(rect.origin.x,
|
||||||
rect.origin.y,
|
rect.origin.y,
|
||||||
rect.size.width,
|
rect.size.w,
|
||||||
rect.size.height));
|
rect.size.h));
|
||||||
|
|
||||||
if (!image) { return NULL; }
|
if (!image) { return NULL; }
|
||||||
|
|
||||||
@ -63,15 +63,15 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect){
|
|||||||
XDefaultRootWindow(display),
|
XDefaultRootWindow(display),
|
||||||
(int)rect.origin.x,
|
(int)rect.origin.x,
|
||||||
(int)rect.origin.y,
|
(int)rect.origin.y,
|
||||||
(unsigned int)rect.size.width,
|
(unsigned int)rect.size.w,
|
||||||
(unsigned int)rect.size.height,
|
(unsigned int)rect.size.h,
|
||||||
AllPlanes, ZPixmap);
|
AllPlanes, ZPixmap);
|
||||||
XCloseDisplay(display);
|
XCloseDisplay(display);
|
||||||
if (image == NULL) return NULL;
|
if (image == NULL) return NULL;
|
||||||
|
|
||||||
bitmap = createMMBitmap((uint8_t *)image->data,
|
bitmap = createMMBitmap((uint8_t *)image->data,
|
||||||
rect.size.width,
|
rect.size.w,
|
||||||
rect.size.height,
|
rect.size.h,
|
||||||
(size_t)image->bytes_per_line,
|
(size_t)image->bytes_per_line,
|
||||||
(uint8_t)image->bits_per_pixel,
|
(uint8_t)image->bits_per_pixel,
|
||||||
(uint8_t)image->bits_per_pixel / 8);
|
(uint8_t)image->bits_per_pixel / 8);
|
||||||
@ -89,12 +89,12 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect){
|
|||||||
|
|
||||||
/* Initialize bitmap info. */
|
/* Initialize bitmap info. */
|
||||||
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
|
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
|
||||||
bi.bmiHeader.biWidth = (long)rect.size.width;
|
bi.bmiHeader.biWidth = (long)rect.size.w;
|
||||||
bi.bmiHeader.biHeight = -(long)rect.size.height; /* Non-cartesian, please */
|
bi.bmiHeader.biHeight = -(long)rect.size.h; /* Non-cartesian, please */
|
||||||
bi.bmiHeader.biPlanes = 1;
|
bi.bmiHeader.biPlanes = 1;
|
||||||
bi.bmiHeader.biBitCount = 32;
|
bi.bmiHeader.biBitCount = 32;
|
||||||
bi.bmiHeader.biCompression = BI_RGB;
|
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.biXPelsPerMeter = 0;
|
||||||
bi.bmiHeader.biYPelsPerMeter = 0;
|
bi.bmiHeader.biYPelsPerMeter = 0;
|
||||||
bi.bmiHeader.biClrUsed = 0;
|
bi.bmiHeader.biClrUsed = 0;
|
||||||
@ -112,8 +112,8 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect){
|
|||||||
!BitBlt(screenMem,
|
!BitBlt(screenMem,
|
||||||
(int)0,
|
(int)0,
|
||||||
(int)0,
|
(int)0,
|
||||||
(int)rect.size.width,
|
(int)rect.size.w,
|
||||||
(int)rect.size.height,
|
(int)rect.size.h,
|
||||||
screen,
|
screen,
|
||||||
rect.origin.x,
|
rect.origin.x,
|
||||||
rect.origin.y,
|
rect.origin.y,
|
||||||
@ -128,9 +128,9 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect){
|
|||||||
}
|
}
|
||||||
|
|
||||||
bitmap = createMMBitmap(NULL,
|
bitmap = createMMBitmap(NULL,
|
||||||
rect.size.width,
|
rect.size.w,
|
||||||
rect.size.height,
|
rect.size.h,
|
||||||
4 * rect.size.width,
|
4 * rect.size.w,
|
||||||
(uint8_t)bi.bmiHeader.biBitCount,
|
(uint8_t)bi.bmiHeader.biBitCount,
|
||||||
4);
|
4);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user