From 341f2281d6e753f06fb83a34d31637a340d8796f Mon Sep 17 00:00:00 2001 From: vcaesar Date: Fri, 29 Oct 2021 11:51:43 -0400 Subject: [PATCH] Add get the screen rect support --- README.md | 1 + bitmap/goBitmap.h | 2 +- robotgo.go | 51 +++++++++++++++++++++++++++++++++--------- screen/goScreen.h | 2 +- screen/screen_c.h | 56 +++++++++++++++++++++++++++++++++++++++++------ 5 files changed, 93 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 3d7f5da..d9b7171 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ RobotGo supports Mac, Windows, and Linux(X11); and robotgo supports arm64 and x8 - [Chinese Docs](https://github.com/go-vgo/robotgo/blob/master/docs/doc_zh.md) ## Binding: +[ADB](https://github.com/vcaesar/adb), packaging android adb API. [Robotn](https://github.com/vcaesar/robotn), binding JavaScript and other, support more language. diff --git a/bitmap/goBitmap.h b/bitmap/goBitmap.h index 68f16b6..429fa39 100644 --- a/bitmap/goBitmap.h +++ b/bitmap/goBitmap.h @@ -141,7 +141,7 @@ char *tostring_bitmap(MMBitmapRef bitmap){ return buf; } -// out with size 200 is enough +// char out size 200 is enough bool bitmap_str(MMBitmapRef bitmap, char *out){ if (!bitmap_ready(bitmap)) { return false; } sprintf(out, " 0 { + display = displayId[0] + } + + rect := C.getScreenRect(C.int32_t(display)) + return Rect{ + Point{ + X: int(rect.origin.x), + Y: int(rect.origin.y), + }, + Size{ + W: int(rect.size.w), + H: int(rect.size.h), + }, + } +} + // Scale get the screen scale func Scale() int { dpi := map[int]int{ @@ -322,7 +353,7 @@ func CaptureScreen(args ...int) C.MMBitmapRef { x = 0 y = 0 - // Get screen size. + // Get the main screen size. displaySize := C.getMainDisplaySize() w = displaySize.w h = displaySize.h @@ -1098,7 +1129,7 @@ func FreeMMPointArr(pointArray C.MMPointArrayRef) { } // FindEveryBitmap find the every bitmap -func FindEveryBitmap(bit C.MMBitmapRef, args ...interface{}) (posArr []MPoint) { +func FindEveryBitmap(bit C.MMBitmapRef, args ...interface{}) (posArr []Point) { var ( sbit C.MMBitmapRef tolerance C.float = 0.01 @@ -1142,7 +1173,7 @@ func FindEveryBitmap(bit C.MMBitmapRef, args ...interface{}) (posArr []MPoint) { cArray := pos.array gSlice := (*[(1 << 28) - 1]C.MMPoint)(unsafe.Pointer(cArray))[:cSize:cSize] for i := 0; i < len(gSlice); i++ { - posArr = append(posArr, MPoint{ + posArr = append(posArr, Point{ X: int(gSlice[i].x), Y: int(gSlice[i].y), }) @@ -1353,7 +1384,7 @@ func FindColorCS(color CHex, x, y, w, h int, args ...float64) (int, int) { } // FindEveryColor find every color -func FindEveryColor(color CHex, args ...interface{}) (posArr []MPoint) { +func FindEveryColor(color CHex, args ...interface{}) (posArr []Point) { var ( bitmap C.MMBitmapRef tolerance C.float = 0.01 @@ -1397,7 +1428,7 @@ func FindEveryColor(color CHex, args ...interface{}) (posArr []MPoint) { cArray := pos.array gSlice := (*[(1 << 28) - 1]C.MMPoint)(unsafe.Pointer(cArray))[:cSize:cSize] for i := 0; i < len(gSlice); i++ { - posArr = append(posArr, MPoint{ + posArr = append(posArr, Point{ X: int(gSlice[i].x), Y: int(gSlice[i].y), }) diff --git a/screen/goScreen.h b/screen/goScreen.h index 648f73f..b2d6471 100644 --- a/screen/goScreen.h +++ b/screen/goScreen.h @@ -97,7 +97,7 @@ char* get_XDisplay_name(){ // capture_screen capture screen MMBitmapRef capture_screen(int32_t x, int32_t y, int32_t w, int32_t h){ - // if (){ + // if () { // x = 0; // y = 0; // // Get screen size. diff --git a/screen/screen_c.h b/screen/screen_c.h index 9f9f2cd..ec4fb41 100644 --- a/screen/screen_c.h +++ b/screen/screen_c.h @@ -19,15 +19,57 @@ MMSizeInt32 getMainDisplaySize(void){ Display *display = XGetMainDisplay(); const int screen = DefaultScreen(display); - return MMSizeInt32Make((int32_t)DisplayWidth(display, screen), - (int32_t)DisplayHeight(display, screen)); + return MMSizeInt32Make( + (int32_t)DisplayWidth(display, screen), + (int32_t)DisplayHeight(display, screen)); #elif defined(IS_WINDOWS) 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)); + 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 +} + +MMRectInt32 getScreenRect(int32_t display_id) { +#if defined(IS_MACOSX) + CGDirectDisplayID displayID = (CGDirectDisplayID) display_id; + if (display_id == 0) { + displayID = CGMainDisplayID(); + } + CGRect displayRect = CGDisplayBounds(displayID); + + CGPoint point = displayRect.origin; + CGSize size = displayRect.size; + return MMRectInt32Make( + (int32_t)point.x, (int32_t)point.y, + (int32_t)size.width, (int32_t)size.height); +#elif defined(USE_X11) + Display *display = XGetMainDisplay(); + const int screen = DefaultScreen(display); + + return MMRectInt32Make( + (int32_t)0, + (int32_t)0, + (int32_t)DisplayWidth(display, screen), + (int32_t)DisplayHeight(display, screen)); +#elif defined(IS_WINDOWS) + if (GetSystemMetrics(SM_CMONITORS) == 1) { + return MMRectInt32Make( + (int32_t)GetSystemMetrics(SM_XSCREEN), + (int32_t)GetSystemMetrics(SM_YSCREEN), + (int32_t)GetSystemMetrics(SM_CXSCREEN), + (int32_t)GetSystemMetrics(SM_CYSCREEN)); + } else { + return MMRectInt32Make( + (int32_t)GetSystemMetrics(SM_XVIRTUALSCREEN), + (int32_t)GetSystemMetrics(SM_YVIRTUALSCREEN), + (int32_t)GetSystemMetrics(SM_CXVIRTUALSCREEN), + (int32_t)GetSystemMetrics(SM_CYVIRTUALSCREEN)); } #endif }