From b4fffb3d06efcc0a4532fab9c111393103e9d366 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Mon, 13 Aug 2018 10:40:31 -0400 Subject: [PATCH] optimize robot unix getxid func --- robotgo_unix.go | 37 +++++++++++++++++-------------------- window/win_sys.h | 10 +++++----- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/robotgo_unix.go b/robotgo_unix.go index 62ffaf6..774ff23 100644 --- a/robotgo_unix.go +++ b/robotgo_unix.go @@ -32,16 +32,7 @@ func GetBounds(pid int32, args ...int) (int, int, int, int) { return internalGetBounds(pid, hwnd) } - if xu == nil { - var err error - xu, err = xgbutil.NewConn() - if err != nil { - log.Println("xgbutil.NewConn errors is: ", err) - return 0, 0, 0, 0 - } - } - - xid, err := GetXidFromPid(xu, pid) + xid, err := GetXId(xu, pid) if err != nil { log.Println("GetXidFromPid errors is: ", err) return 0, 0, 0, 0 @@ -61,16 +52,7 @@ func ActivePIDC(pid int32, args ...int) { return } - if xu == nil { - var err error - xu, err = xgbutil.NewConn() - if err != nil { - log.Println("xgbutil.NewConn errors is: ", err) - return - } - } - - xid, err := GetXidFromPid(xu, pid) + xid, err := GetXId(xu, pid) if err != nil { log.Println("GetXidFromPid errors is: ", err) return @@ -114,6 +96,21 @@ func ActivePID(pid int32, args ...int) error { return nil } +// GetXId get the xid +func GetXId(xu *xgbutil.XUtil, pid int32) (xproto.Window, error) { + if xu == nil { + var err error + xu, err = xgbutil.NewConn() + if err != nil { + // log.Println("xgbutil.NewConn errors is: ", err) + return 0, err + } + } + + xid, err := GetXidFromPid(xu, pid) + return xid, err +} + // GetXidFromPid get the xide from pid func GetXidFromPid(xu *xgbutil.XUtil, pid int32) (xproto.Window, error) { windows, err := ewmh.ClientListGet(xu) diff --git a/window/win_sys.h b/window/win_sys.h index cf1aa5d..de4b020 100644 --- a/window/win_sys.h +++ b/window/win_sys.h @@ -15,14 +15,14 @@ Bounds get_bounds(uintptr pid, uintptr isHwnd){ // Determine the current point of the window if (AXUIElementCopyAttributeValue(AxID, - kAXPositionAttribute, (CFTypeRef*) &axp) + kAXPositionAttribute, (CFTypeRef*) &axp) != kAXErrorSuccess || axp == NULL){ goto exit; } // Determine the current size of the window if (AXUIElementCopyAttributeValue(AxID, - kAXSizeAttribute, (CFTypeRef*) &axs) + kAXSizeAttribute, (CFTypeRef*) &axs) != kAXErrorSuccess || axs == NULL){ goto exit; } @@ -36,9 +36,9 @@ Bounds get_bounds(uintptr pid, uintptr isHwnd){ bounds.W = s.width; bounds.H = s.height; } - - exit: - if (axp != NULL) { CFRelease(axp); } + + exit: + if (axp != NULL) { CFRelease(axp); } if (axs != NULL) { CFRelease(axs); } return bounds;