mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-05-31 06:13: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){
|
||||
MMPoint pos = getMousePos();
|
||||
MMSize screenSize = getMainDisplaySize();
|
||||
MMSizeInt32 screenSize = getMainDisplaySize();
|
||||
double velo_x = 0.0, velo_y = 0.0;
|
||||
double distance;
|
||||
|
||||
@ -428,7 +428,7 @@ 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;
|
||||
}
|
||||
|
||||
|
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
|
||||
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)
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user