From 76abeff3ff4eac67800938d120b949a3dd49c63c Mon Sep 17 00:00:00 2001 From: vcaesar Date: Wed, 26 Jan 2022 09:41:06 -0800 Subject: [PATCH] add GetClient bounds support --- robotgo.go | 6 ++++++ robotgo_mac_win.go | 10 ++++++++++ robotgo_x11.go | 17 +++++++++++++++++ window/win_sys.h | 17 ++++++----------- window/window.h | 7 +++---- 5 files changed, 42 insertions(+), 15 deletions(-) diff --git a/robotgo.go b/robotgo.go index 9ac4b87..e256143 100644 --- a/robotgo.go +++ b/robotgo.go @@ -1401,6 +1401,12 @@ func internalGetBounds(pid int32, hwnd int) (int, int, int, int) { return int(bounds.X), int(bounds.Y), int(bounds.W), int(bounds.H) } +// internalGetClient get the window client bounds +func internalGetClient(pid int32, hwnd int) (int, int, int, int) { + bounds := C.get_client(C.uintptr(pid), C.uintptr(hwnd)) + return int(bounds.X), int(bounds.Y), int(bounds.W), int(bounds.H) +} + // Is64Bit determine whether the sys is 64bit func Is64Bit() bool { b := C.Is64Bit() diff --git a/robotgo_mac_win.go b/robotgo_mac_win.go index f782723..88c1bb9 100644 --- a/robotgo_mac_win.go +++ b/robotgo_mac_win.go @@ -23,6 +23,16 @@ func GetBounds(pid int32, args ...int) (int, int, int, int) { return internalGetBounds(pid, hwnd) } +// GetClient get the window client bounds +func GetClient(pid int32, args ...int) (int, int, int, int) { + var hwnd int + if len(args) > 0 { + hwnd = args[0] + } + + return internalGetClient(pid, hwnd) +} + // internalGetTitle get the window title func internalGetTitle(pid int32, args ...int32) string { var isHwnd int32 diff --git a/robotgo_x11.go b/robotgo_x11.go index cd807b9..1bd0018 100644 --- a/robotgo_x11.go +++ b/robotgo_x11.go @@ -44,6 +44,23 @@ func GetBounds(pid int32, args ...int) (int, int, int, int) { return internalGetBounds(int32(xid), hwnd) } +// GetClient get the window client bounds +func GetClient(pid int32, args ...int) (int, int, int, int) { + var hwnd int + if len(args) > 0 { + hwnd = args[0] + return internalGetClient(pid, hwnd) + } + + xid, err := GetXId(xu, pid) + if err != nil { + log.Println("Get Xid from Pid errors is: ", err) + return 0, 0, 0, 0 + } + + return internalGetClient(int32(xid), hwnd) +} + // internalGetTitle get the window title func internalGetTitle(pid int32, args ...int32) string { var hwnd int32 diff --git a/window/win_sys.h b/window/win_sys.h index 1d64d62..92ea3a5 100644 --- a/window/win_sys.h +++ b/window/win_sys.h @@ -88,22 +88,20 @@ Bounds get_bounds(uintptr pid, uintptr isHwnd){ AXUIElementRef AxID = AXUIElementCreateApplication(pid); // Determine the current point of the window - if (AXUIElementCopyAttributeValue( - AxID, kAXPositionAttribute, (CFTypeRef*) &axp) + if (AXUIElementCopyAttributeValue(AxID, kAXPositionAttribute, (CFTypeRef*) &axp) != kAXErrorSuccess || axp == NULL){ goto exit; } // Determine the current size of the window - if (AXUIElementCopyAttributeValue( - AxID, kAXSizeAttribute, (CFTypeRef*) &axs) + if (AXUIElementCopyAttributeValue(AxID, kAXSizeAttribute, (CFTypeRef*) &axs) != kAXErrorSuccess || axs == NULL){ goto exit; } CGPoint p; CGSize s; // Attempt to convert both values into atomic types - if (AXValueGetValue(axp, kAXValueCGPointType, &p) && + if (AXValueGetValue(axp, kAXValueCGPointType, &p) && AXValueGetValue(axs, kAXValueCGSizeType, &s)){ bounds.X = p.x; bounds.Y = p.y; @@ -174,8 +172,7 @@ Bounds get_client(uintptr pid, uintptr isHwnd) { int32_t x = 0, y = 0; // Check if the window is the root - XQueryTree(rDisplay, win.XWin, - &root, &parent, &children, &count); + XQueryTree(rDisplay, win.XWin, &root, &parent, &children, &count); if (children) { XFree(children); } // Retrieve window attributes @@ -183,9 +180,8 @@ Bounds get_client(uintptr pid, uintptr isHwnd) { XGetWindowAttributes(rDisplay, win.XWin, &attr); // Coordinates must be translated - if (parent != attr.root){ - XTranslateCoordinates(rDisplay, win.XWin, attr.root, attr.x, - attr.y, &x, &y, &parent); + if (parent != attr.root) { + XTranslateCoordinates(rDisplay, win.XWin, attr.root, attr.x, attr.y, &x, &y, &parent); } // Coordinates can be left alone else { @@ -209,7 +205,6 @@ Bounds get_client(uintptr pid, uintptr isHwnd) { hwnd = (HWND)pid; } - RECT rect = { 0 }; GetClientRect(hwnd, &rect); diff --git a/window/window.h b/window/window.h index a8cc058..6b79974 100644 --- a/window/window.h +++ b/window/window.h @@ -532,13 +532,12 @@ char* named(void *result) { char* get_title_by_hand(MData m_data){ // Check if the window is valid - if (!IsValid()) {return "IsValid failed.";} + if (!IsValid()) { return "IsValid failed."; } #if defined(IS_MACOSX) CFStringRef data = NULL; - // Determine the current title of the window - if (AXUIElementCopyAttributeValue(m_data.AxID, - kAXTitleAttribute, (CFTypeRef*) &data) == kAXErrorSuccess && data != NULL) { + if (AXUIElementCopyAttributeValue(m_data.AxID, kAXTitleAttribute, (CFTypeRef*) &data) == + kAXErrorSuccess && data != NULL) { char conv[512]; // Convert result to a C-String CFStringGetCString(data, conv, 512, kCFStringEncodingUTF8);