Add get the screen rect support

This commit is contained in:
vcaesar 2021-10-29 11:51:43 -04:00
parent 10ae65cc6c
commit 341f2281d6
5 changed files with 93 additions and 19 deletions

View File

@ -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.

View File

@ -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, "<Bitmap with resolution %lu%lu, \

View File

@ -106,12 +106,23 @@ type Bitmap struct {
BytesPerPixel uint8
}
// MPoint is MPoint struct
type MPoint struct {
// Point is point struct
type Point struct {
X int
Y int
}
// Size is size structure
type Size struct {
W, H int
}
// Rect is rect structure
type Rect struct {
Point
Size
}
// Try handler(err)
func Try(fun func(), handler func(interface{})) {
defer func() {
@ -191,7 +202,7 @@ func RgbToHex(r, g, b uint8) C.uint32_t {
return C.color_rgb_to_hex(C.uint8_t(r), C.uint8_t(g), C.uint8_t(b))
}
// GetPxColor get pixel color return C.MMRGBHex
// GetPxColor get the pixel color return C.MMRGBHex
func GetPxColor(x, y int) C.MMRGBHex {
cx := C.int32_t(x)
cy := C.int32_t(y)
@ -200,7 +211,7 @@ func GetPxColor(x, y int) C.MMRGBHex {
return color
}
// GetPixelColor get pixel color return string
// GetPixelColor get the pixel color return string
func GetPixelColor(x, y int) string {
cx := C.int32_t(x)
cy := C.int32_t(y)
@ -218,7 +229,7 @@ func GetMouseColor() string {
return GetPixelColor(x, y)
}
// ScaleX get primary display horizontal DPI scale factor
// ScaleX get the primary display horizontal DPI scale factor
func ScaleX() int {
return int(C.scale_x())
}
@ -246,6 +257,26 @@ func GetScreenSize() (int, int) {
return int(size.w), int(size.h)
}
// GetScreenRect get the screen rect
func GetScreenRect(displayId ...int) Rect {
display := 0
if len(displayId) > 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),
})

View File

@ -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.

View File

@ -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
}