add GetClient bounds support

This commit is contained in:
vcaesar 2022-01-26 09:41:06 -08:00
parent 18243a2192
commit 76abeff3ff
5 changed files with 42 additions and 15 deletions

View File

@ -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) 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 // Is64Bit determine whether the sys is 64bit
func Is64Bit() bool { func Is64Bit() bool {
b := C.Is64Bit() b := C.Is64Bit()

View File

@ -23,6 +23,16 @@ func GetBounds(pid int32, args ...int) (int, int, int, int) {
return internalGetBounds(pid, hwnd) 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 // internalGetTitle get the window title
func internalGetTitle(pid int32, args ...int32) string { func internalGetTitle(pid int32, args ...int32) string {
var isHwnd int32 var isHwnd int32

View File

@ -44,6 +44,23 @@ func GetBounds(pid int32, args ...int) (int, int, int, int) {
return internalGetBounds(int32(xid), hwnd) 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 // internalGetTitle get the window title
func internalGetTitle(pid int32, args ...int32) string { func internalGetTitle(pid int32, args ...int32) string {
var hwnd int32 var hwnd int32

View File

@ -88,22 +88,20 @@ Bounds get_bounds(uintptr pid, uintptr isHwnd){
AXUIElementRef AxID = AXUIElementCreateApplication(pid); AXUIElementRef AxID = AXUIElementCreateApplication(pid);
// Determine the current point of the window // Determine the current point of the window
if (AXUIElementCopyAttributeValue( if (AXUIElementCopyAttributeValue(AxID, kAXPositionAttribute, (CFTypeRef*) &axp)
AxID, kAXPositionAttribute, (CFTypeRef*) &axp)
!= kAXErrorSuccess || axp == NULL){ != kAXErrorSuccess || axp == NULL){
goto exit; goto exit;
} }
// Determine the current size of the window // Determine the current size of the window
if (AXUIElementCopyAttributeValue( if (AXUIElementCopyAttributeValue(AxID, kAXSizeAttribute, (CFTypeRef*) &axs)
AxID, kAXSizeAttribute, (CFTypeRef*) &axs)
!= kAXErrorSuccess || axs == NULL){ != kAXErrorSuccess || axs == NULL){
goto exit; goto exit;
} }
CGPoint p; CGSize s; CGPoint p; CGSize s;
// Attempt to convert both values into atomic types // Attempt to convert both values into atomic types
if (AXValueGetValue(axp, kAXValueCGPointType, &p) && if (AXValueGetValue(axp, kAXValueCGPointType, &p) &&
AXValueGetValue(axs, kAXValueCGSizeType, &s)){ AXValueGetValue(axs, kAXValueCGSizeType, &s)){
bounds.X = p.x; bounds.X = p.x;
bounds.Y = p.y; bounds.Y = p.y;
@ -174,8 +172,7 @@ Bounds get_client(uintptr pid, uintptr isHwnd) {
int32_t x = 0, y = 0; int32_t x = 0, y = 0;
// Check if the window is the root // Check if the window is the root
XQueryTree(rDisplay, win.XWin, XQueryTree(rDisplay, win.XWin, &root, &parent, &children, &count);
&root, &parent, &children, &count);
if (children) { XFree(children); } if (children) { XFree(children); }
// Retrieve window attributes // Retrieve window attributes
@ -183,9 +180,8 @@ Bounds get_client(uintptr pid, uintptr isHwnd) {
XGetWindowAttributes(rDisplay, win.XWin, &attr); XGetWindowAttributes(rDisplay, win.XWin, &attr);
// Coordinates must be translated // Coordinates must be translated
if (parent != attr.root){ if (parent != attr.root) {
XTranslateCoordinates(rDisplay, win.XWin, attr.root, attr.x, XTranslateCoordinates(rDisplay, win.XWin, attr.root, attr.x, attr.y, &x, &y, &parent);
attr.y, &x, &y, &parent);
} }
// Coordinates can be left alone // Coordinates can be left alone
else { else {
@ -209,7 +205,6 @@ Bounds get_client(uintptr pid, uintptr isHwnd) {
hwnd = (HWND)pid; hwnd = (HWND)pid;
} }
RECT rect = { 0 }; RECT rect = { 0 };
GetClientRect(hwnd, &rect); GetClientRect(hwnd, &rect);

View File

@ -532,13 +532,12 @@ char* named(void *result) {
char* get_title_by_hand(MData m_data){ char* get_title_by_hand(MData m_data){
// Check if the window is valid // Check if the window is valid
if (!IsValid()) {return "IsValid failed.";} if (!IsValid()) { return "IsValid failed."; }
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
CFStringRef data = NULL; CFStringRef data = NULL;
// Determine the current title of the window // Determine the current title of the window
if (AXUIElementCopyAttributeValue(m_data.AxID, if (AXUIElementCopyAttributeValue(m_data.AxID, kAXTitleAttribute, (CFTypeRef*) &data) ==
kAXTitleAttribute, (CFTypeRef*) &data) == kAXErrorSuccess && data != NULL) { kAXErrorSuccess && data != NULL) {
char conv[512]; char conv[512];
// Convert result to a C-String // Convert result to a C-String
CFStringGetCString(data, conv, 512, kCFStringEncodingUTF8); CFStringGetCString(data, conv, 512, kCFStringEncodingUTF8);