diff --git a/robotgo.go b/robotgo.go index 36b538b..1705b21 100644 --- a/robotgo.go +++ b/robotgo.go @@ -1398,13 +1398,8 @@ func GetPID() int32 { return int32(pid) } -// GetBounds get the window bounds -func GetBounds(pid int32, args ...int) (int, int, int, int) { - var hwnd int - if len(args) > 0 { - hwnd = args[0] - } - +// internalGetBounds get the window bounds +func internalGetBounds(pid int32, hwnd int) (int, int, int, int) { bounds := C.get_bounds(C.uintptr(pid), C.uintptr(hwnd)) return int(bounds.X), int(bounds.Y), int(bounds.W), int(bounds.H) } diff --git a/robotgo_mac_win.go b/robotgo_mac_win.go index 81c653d..3acc163 100644 --- a/robotgo_mac_win.go +++ b/robotgo_mac_win.go @@ -12,6 +12,16 @@ package robotgo +// GetBounds get the window bounds +func GetBounds(pid int32, args ...int) (int, int, int, int) { + var hwnd int + if len(args) > 0 { + hwnd = args[0] + } + + return internalGetBounds(pid, hwnd) +} + // ActivePID active the window by PID, // If args[0] > 0 on the Windows platform via a window handle to active func ActivePID(pid int32, args ...int) error { diff --git a/robotgo_unix.go b/robotgo_unix.go index a762446..62ffaf6 100644 --- a/robotgo_unix.go +++ b/robotgo_unix.go @@ -23,6 +23,33 @@ import ( var xu *xgbutil.XUtil +// GetBounds get the window bounds +func GetBounds(pid int32, args ...int) (int, int, int, int) { + var hwnd int + if len(args) > 0 { + hwnd = args[0] + + 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) + if err != nil { + log.Println("GetXidFromPid errors is: ", err) + return 0, 0, 0, 0 + } + + internalGetBounds(int32(xid), hwnd) +} + // ActivePIDC active the window by PID, // If args[0] > 0 on the unix platform via a xid to active func ActivePIDC(pid int32, args ...int) { @@ -73,6 +100,7 @@ func ActivePID(pid int32, args ...int) error { return nil } + // get xid from pid xid, err := GetXidFromPid(xu, pid) if err != nil { return err @@ -103,5 +131,5 @@ func GetXidFromPid(xu *xgbutil.XUtil, pid int32) (xproto.Window, error) { } } - return 0, errors.New("failed to find a window with a matching pid") + return 0, errors.New("failed to find a window with a matching pid.") }