From 186df36c2c12451c8601ad4201102c1aaaba1c5e Mon Sep 17 00:00:00 2001 From: vcaesar Date: Tue, 10 Jul 2018 20:02:18 +0800 Subject: [PATCH] update active pid try to fix #140 --- robotgo.go | 20 ++++++++----- robotgo_mac_win.go | 14 +++++++++ robotgo_unix.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 robotgo_mac_win.go create mode 100644 robotgo_unix.go diff --git a/robotgo.go b/robotgo.go index 48c61e5..aea1f56 100644 --- a/robotgo.go +++ b/robotgo.go @@ -1536,17 +1536,21 @@ func FindIds(name string) ([]int32, error) { return pids, err } -// ActivePID active window by PID, -// If args[0] > 0 on the Windows platform via a window handle to active -func ActivePID(pid int32, args ...int) { - var hwnd int - if len(args) > 0 { - hwnd = args[0] - } - +func internalActive(pid int32, hwnd int) { C.active_PID(C.uintptr(pid), C.uintptr(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) { +// var hwnd int +// if len(args) > 0 { +// hwnd = args[0] +// } + +// C.active_PID(C.uintptr(pid), C.uintptr(hwnd)) +// } + // ActiveName active window by name func ActiveName(name string) error { pids, err := FindIds(name) diff --git a/robotgo_mac_win.go b/robotgo_mac_win.go new file mode 100644 index 0000000..2471c92 --- /dev/null +++ b/robotgo_mac_win.go @@ -0,0 +1,14 @@ +// +build darwin windows + +package robotgo + +// 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) { + var hwnd int + if len(args) > 0 { + hwnd = args[0] + } + + internalActive(pid, hwnd) +} diff --git a/robotgo_unix.go b/robotgo_unix.go new file mode 100644 index 0000000..75077e4 --- /dev/null +++ b/robotgo_unix.go @@ -0,0 +1,74 @@ +// +build !darwin,!windows + +package robotgo + +import ( + "errors" + + "github.com/BurntSushi/xgb/xproto" + "github.com/BurntSushi/xgbutil" + "github.com/BurntSushi/xgbutil/ewmh" +) + +var xu *xgbutil.XUtil + +// ActivePID active the window by PID, +// If args[0] > 0 on the unix platform via a xid to active +func ActivePID(pid int32, args ...int) { + var hwnd int + if len(args) > 0 { + hwnd = args[0] + + internalActive(pid, hwnd) + return + } + + xid, err := getXidFromPid(xu, pid) + if err != nil { + return + } + + internalActive(int32(xid), hwnd) +} + +// ActivePIDXgb makes the window of the PID the active window +func ActivePIDXgb(pid int32) error { + if xu == nil { + var err error + xu, err = xgbutil.NewConn() + if err != nil { + return err + } + } + + xid, err := getXidFromPid(xu, pid) + if err != nil { + return err + } + + err = ewmh.ActiveWindowReq(xu, xid) + if err != nil { + return err + } + + return nil +} + +func getXidFromPid(xu *xgbutil.XUtil, pid int32) (xproto.Window, error) { + windows, err := ewmh.ClientListGet(xu) + if err != nil { + return 0, err + } + + for _, window := range windows { + wmPid, err := ewmh.WmPidGet(xu, window) + if err != nil { + return 0, err + } + if uint(pid) == wmPid { + return window, nil + } + } + + return 0, errors.New("failed to find a window with a matching pid") +}