mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-05-31 06:13:55 +00:00
update active pid try to fix #140
This commit is contained in:
parent
86d35b65b1
commit
186df36c2c
20
robotgo.go
20
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)
|
||||
|
14
robotgo_mac_win.go
Normal file
14
robotgo_mac_win.go
Normal file
@ -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)
|
||||
}
|
74
robotgo_unix.go
Normal file
74
robotgo_unix.go
Normal file
@ -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")
|
||||
}
|
Loading…
Reference in New Issue
Block a user