From df394a94b84a6679a8b4de5477d19b46061d9a86 Mon Sep 17 00:00:00 2001 From: "saki.shen" Date: Tue, 19 Mar 2024 16:16:39 +0800 Subject: [PATCH 1/4] =?UTF-8?q?[EDIT]=20By=20Saki,=20=E5=8E=BB=E9=99=A4=20?= =?UTF-8?q?robotgo.go=20=E7=9A=84=20window/goWindow.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robotgo.go | 1 - 1 file changed, 1 deletion(-) diff --git a/robotgo.go b/robotgo.go index b445a8f..2b0646a 100644 --- a/robotgo.go +++ b/robotgo.go @@ -41,7 +41,6 @@ package robotgo // #include "screen/goScreen.h" #include "mouse/mouse_c.h" -#include "window/goWindow.h" */ import "C" From de95edb1a8566ba3c1e2ed96c531c165e4683f58 Mon Sep 17 00:00:00 2001 From: "saki.shen" Date: Tue, 19 Mar 2024 16:41:19 +0800 Subject: [PATCH 2/4] =?UTF-8?q?[EDIT]=20By=20Saki,=20=E5=8E=BB=E9=99=A4=20?= =?UTF-8?q?=5FAXUIElementGetWindow=20=E7=9A=84=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robotgo.go | 1 + window/window.h | 29 +++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/robotgo.go b/robotgo.go index 2b0646a..b445a8f 100644 --- a/robotgo.go +++ b/robotgo.go @@ -41,6 +41,7 @@ package robotgo // #include "screen/goScreen.h" #include "mouse/mouse_c.h" +#include "window/goWindow.h" */ import "C" diff --git a/window/window.h b/window/window.h index b206aa8..aafb6d5 100644 --- a/window/window.h +++ b/window/window.h @@ -366,12 +366,12 @@ MData get_active(void) { AXUIElementRef focused = AXUIElementCreateApplication(pid); if (focused == NULL) { return result; } // Verify - AXUIElementRef element; + /*AXUIElementRef element; CGWindowID win = 0; // Retrieve the currently focused window if (AXUIElementCopyAttributeValue(focused, kAXFocusedWindowAttribute, (CFTypeRef*) &element) == kAXErrorSuccess && element) { - + // Use undocumented API to get WID if (_AXUIElementGetWindow(element, &win) == kAXErrorSuccess && win) { // Manually set internals @@ -383,7 +383,32 @@ MData get_active(void) { } else { result.CgID = win; result.AxID = element; + }*/ + + AXUIElementRef focused = NULL; + AXUIElementRef windowElement = NULL; + CGWindowID win = 0; + + // Retrieve the currently focused window + if (AXUIElementCopyAttributeValue(focused, kAXFocusedWindowAttribute, (CFTypeRef*) &windowElement) == kAXErrorSuccess && windowElement) { + // Use AXUIElementCopyAttributeValue to get parent window + CFTypeRef cfWindow = NULL; + if (AXUIElementCopyAttributeValue(windowElement, kAXParentAttribute, &cfWindow) == kAXErrorSuccess && cfWindow != NULL) { + AXUIElementRef parentWindow = (AXUIElementRef)cfWindow; + if (AXUIElementCopyAttributeValue(parentWindow, kAXWindowAttribute, (CFTypeRef*)&win) == kAXErrorSuccess && win) { + // Manually set internals + result.CgID = win; + result.AxID = windowElement; + } else { + CFRelease(parentWindow); + } + } + CFRelease(windowElement); + } else { + result.CgID = 0; + result.AxID = NULL; } + CFRelease(focused); return result; From 31ddf1a95968bdd20f539f4fe47b8cb2eb3ffd06 Mon Sep 17 00:00:00 2001 From: "saki.shen" Date: Tue, 19 Mar 2024 16:46:34 +0800 Subject: [PATCH 3/4] [EDIT] By Saki, fixed bug of previous definition --- window/window.h | 1 - 1 file changed, 1 deletion(-) diff --git a/window/window.h b/window/window.h index aafb6d5..69ae0a8 100644 --- a/window/window.h +++ b/window/window.h @@ -385,7 +385,6 @@ MData get_active(void) { result.AxID = element; }*/ - AXUIElementRef focused = NULL; AXUIElementRef windowElement = NULL; CGWindowID win = 0; From 9aa4059a31d57d31338b45aba1251468975807f4 Mon Sep 17 00:00:00 2001 From: "saki.shen" Date: Tue, 19 Mar 2024 17:01:07 +0800 Subject: [PATCH 4/4] [EDIT] By Saki, replace _AXUIElementGetWindow with AXUIElementCopyAttributeValue --- window/pub.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/window/pub.h b/window/pub.h index af1158b..b8a941a 100644 --- a/window/pub.h +++ b/window/pub.h @@ -42,7 +42,7 @@ typedef struct _Bounds Bounds; static Boolean(*gAXIsProcessTrustedWithOptions) (CFDictionaryRef); static CFStringRef* gkAXTrustedCheckOptionPrompt; - AXError _AXUIElementGetWindow(AXUIElementRef, CGWindowID* out); + //AXError _AXUIElementGetWindow(AXUIElementRef, CGWindowID* out); static AXUIElementRef GetUIElement(CGWindowID win){ intptr pid = 0; // double_t pid = 0; @@ -90,13 +90,25 @@ typedef struct _Bounds Bounds; AXUIElementRef element = (AXUIElementRef) CFArrayGetValueAtIndex(windows, i); CGWindowID temp = 0; // Use undocumented API to get WindowID - _AXUIElementGetWindow(element, &temp); + /*_AXUIElementGetWindow(element, &temp); if (temp == win) { // Retain element CFRetain(element); result = element; break; + }*/ + CFTypeRef cfWindow = NULL; + if (AXUIElementCopyAttributeValue(element, kAXWindowAttribute, &cfWindow) == kAXErrorSuccess && cfWindow != NULL) { + temp = *(CGWindowID*)CFDataGetBytePtr(cfWindow); + CFRelease(cfWindow); + + if (temp == win) { + // Retain element + CFRetain(element); + result = element; + break; + } } }