From e9b5a9251e1689ec54241d569ff5fa2d4a8b650a Mon Sep 17 00:00:00 2001 From: vcaesar Date: Tue, 13 Sep 2022 15:49:21 -0700 Subject: [PATCH] Update: Unified pid and xid name, Update godoc, fixed typo --- README.md | 2 +- base/pubs.h | 4 ++-- examples/window/main.go | 6 +++--- mouse/mouse_c.h | 2 +- robotgo.go | 47 +++++++++++++++++++++++++++-------------- robotgo_mac.go | 2 +- robotgo_mac_win.go | 8 ++++--- robotgo_win.go | 2 +- robotgo_x11.go | 23 ++++++++++---------- screen/goScreen.h | 2 +- window/goWindow.h | 4 ++-- window/win_sys.h | 4 ++-- window/window.h | 2 +- 13 files changed, 63 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index b09f518..a375d6f 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Golang GCC ``` -#### For Mac OS X: +#### For MacOS: Xcode Command Line Tools (And Privacy setting: [#277](https://github.com/go-vgo/robotgo/issues/277) ) diff --git a/base/pubs.h b/base/pubs.h index 6dfecc7..1ad716b 100644 --- a/base/pubs.h +++ b/base/pubs.h @@ -22,12 +22,12 @@ return TRUE; } - HWND GetHwndByPId(DWORD dwProcessId) { + HWND GetHwndByPid(DWORD dwProcessId) { WNDINFO info = {0}; info.hWnd = NULL; info.dwPid = dwProcessId; EnumWindows(EnumWindowsProc, (LPARAM)&info); - // printf("%d\n", info.hWnd); + return info.hWnd; } #endif \ No newline at end of file diff --git a/examples/window/main.go b/examples/window/main.go index ffa5c87..7e39b5b 100644 --- a/examples/window/main.go +++ b/examples/window/main.go @@ -28,7 +28,7 @@ func alert() { func get() { // get the current process id - pid := robotgo.GetPID() + pid := robotgo.GetPid() fmt.Println("pid----", pid) // get current Window Active @@ -64,7 +64,7 @@ func findIds() { fmt.Println("pids...", fpid) if len(fpid) > 0 { - err = robotgo.ActivePID(fpid[0]) + err = robotgo.ActivePid(fpid[0]) if err != nil { fmt.Println(err) } @@ -87,7 +87,7 @@ func findIds() { } func active() { - robotgo.ActivePID(100) + robotgo.ActivePid(100) // robotgo.Sleep(2) robotgo.ActiveName("code") robotgo.Sleep(1) diff --git a/mouse/mouse_c.h b/mouse/mouse_c.h index 6e08d9a..a53bdf5 100644 --- a/mouse/mouse_c.h +++ b/mouse/mouse_c.h @@ -192,7 +192,7 @@ void clickMouse(MMMouseButton button){ toggleMouse(false, button); } -/* Special function for sending double clicks, needed for Mac OS X. */ +/* Special function for sending double clicks, needed for MacOS. */ void doubleClick(MMMouseButton button){ #if defined(IS_MACOSX) /* Double click for Mac. */ diff --git a/robotgo.go b/robotgo.go index 420c22c..e9d6efd 100644 --- a/robotgo.go +++ b/robotgo.go @@ -9,20 +9,22 @@ // except according to those terms. /* - Package robotgo Go native cross-platform system automation. Please make sure Golang, GCC is installed correctly before installing RobotGo; See Requirements: + https://github.com/go-vgo/robotgo#requirements Installation: With Go module support (Go 1.11+), just import: + import "github.com/go-vgo/robotgo" Otherwise, to install the robotgo package, run the command: + go get -u github.com/go-vgo/robotgo */ package robotgo @@ -236,8 +238,8 @@ func getNumDisplays() int { return int(C.get_num_displays()) } -// GetHwndByPId get the hwnd by pid -func GetHwndByPId(pid int) int { +// GetHWNDByPid get the hwnd by pid +func GetHWNDByPid(pid int) int { return int(C.get_hwnd_by_pid(C.uintptr(pid))) } @@ -513,8 +515,9 @@ func MoveMouse(x, y int) { // Move move the mouse to (x, y) // // Examples: -// robotgo.MouseSleep = 100 // 100 millisecond -// robotgo.Move(10, 10) +// +// robotgo.MouseSleep = 100 // 100 millisecond +// robotgo.Move(10, 10) func Move(x, y int) { // if runtime.GOOS == "windows" { // f := ScaleF() @@ -560,6 +563,7 @@ func Drag(x, y int, args ...string) { // DragSmooth drag the mouse like smooth to (x, y) // // Examples: +// // robotgo.DragSmooth(10, 10) func DragSmooth(x, y int, args ...interface{}) { Toggle("left") @@ -582,6 +586,7 @@ func MoveMouseSmooth(x, y int, args ...interface{}) bool { // robotgo.MoveSmooth(x, y int, low, high float64, mouseDelay int) // // Examples: +// // robotgo.MoveSmooth(10, 10) // robotgo.MoveSmooth(10, 10, 1.0, 2.0) func MoveSmooth(x, y int, args ...interface{}) bool { @@ -648,7 +653,7 @@ func GetMousePos() (int, int) { // Deprecated: use the Click(), // -// MouseClick click the mouse +// # MouseClick click the mouse // // robotgo.MouseClick(button string, double bool) func MouseClick(args ...interface{}) { @@ -660,7 +665,8 @@ func MouseClick(args ...interface{}) { // robotgo.Click(button string, double bool) // // Examples: -// robotgo.Click() // default is left button +// +// robotgo.Click() // default is left button // robotgo.Click("right") // robotgo.Click("wheelLeft") func Click(args ...interface{}) { @@ -691,6 +697,7 @@ func Click(args ...interface{}) { // robotgo.MoveClick(x, y int, button string, double bool) // // Examples: +// // robotgo.MouseSleep = 100 // robotgo.MoveClick(10, 10) func MoveClick(x, y int, args ...interface{}) { @@ -709,10 +716,12 @@ func MovesClick(x, y int, args ...interface{}) { } // Toggle toggle the mouse, support button: -// "left", "center", "right", -// "wheelDown", "wheelUp", "wheelLeft", "wheelRight" +// +// "left", "center", "right", +// "wheelDown", "wheelUp", "wheelLeft", "wheelRight" // // Examples: +// // robotgo.Toggle("left") // default is down // robotgo.Toggle("left", "up") func Toggle(key ...string) error { @@ -749,6 +758,7 @@ func MouseUp(key ...string) error { // robotgo.Scroll(x, y, msDelay int) // // Examples: +// // robotgo.Scroll(10, 10) func Scroll(x, y int, args ...int) { var msDelay = 10 @@ -767,6 +777,7 @@ func Scroll(x, y int, args ...int) { // supported: "up", "down", "left", "right" // // Examples: +// // robotgo.ScrollMouse(10, "down") // robotgo.ScrollMouse(10, "up") func ScrollMouse(x int, direction ...string) { @@ -797,6 +808,7 @@ func ScrollMouse(x int, direction ...string) { // robotgo.ScrollSmooth(toy, num, sleep, tox) // // Examples: +// // robotgo.ScrollSmooth(-10) // robotgo.ScrollSmooth(-10, 6, 200, -10) func ScrollSmooth(to int, args ...int) { @@ -828,6 +840,7 @@ func ScrollSmooth(to int, args ...int) { // ScrollRelative scroll mouse with relative // // Examples: +// // robotgo.ScrollRelative(10, 10) func ScrollRelative(x, y int, args ...int) { mx, my := MoveArgs(x, y) @@ -987,7 +1000,7 @@ func GetHandle() int { // Deprecated: use the GetHandle(), // -// GetBHandle get the window handle, Wno-deprecated +// # GetBHandle get the window handle, Wno-deprecated // // This function will be removed in version v1.0.0 func GetBHandle() int { @@ -1008,6 +1021,7 @@ func cgetTitle(hwnd, isHwnd int32) string { // GetTitle get the window title return string // // Examples: +// // fmt.Println(robotgo.GetTitle()) // // ids, _ := robotgo.FindIds() @@ -1026,8 +1040,8 @@ func GetTitle(args ...int32) string { return internalGetTitle(args[0]) } -// GetPID get the process id return int32 -func GetPID() int32 { +// GetPid get the process id return int32 +func GetPid() int32 { pid := C.get_PID() return int32(pid) } @@ -1054,9 +1068,9 @@ func internalActive(pid int32, hwnd int) { C.active_PID(C.uintptr(pid), C.uintptr(hwnd)) } -// ActivePID active the window by PID, +// 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) { +// func ActivePid(pid int32, args ...int) { // var hwnd int // if len(args) > 0 { // hwnd = args[0] @@ -1068,11 +1082,12 @@ func internalActive(pid int32, hwnd int) { // ActiveName active the window by name // // Examples: -// robotgo.ActiveName("chrome") +// +// robotgo.ActiveName("chrome") func ActiveName(name string) error { pids, err := FindIds(name) if err == nil && len(pids) > 0 { - return ActivePID(pids[0]) + return ActivePid(pids[0]) } return err diff --git a/robotgo_mac.go b/robotgo_mac.go index 0f9ce4a..0d8ac31 100644 --- a/robotgo_mac.go +++ b/robotgo_mac.go @@ -18,7 +18,7 @@ package robotgo */ import "C" -// GetMianId get the main display id +// GetMainId get the main display id func GetMainId() int { return int(C.CGMainDisplayID()) } diff --git a/robotgo_mac_win.go b/robotgo_mac_win.go index 88c1bb9..798ea06 100644 --- a/robotgo_mac_win.go +++ b/robotgo_mac_win.go @@ -44,14 +44,15 @@ func internalGetTitle(pid int32, args ...int32) string { return gtitle } -// ActivePID active the window by PID, +// ActivePid active the window by PID, // // If args[0] > 0 on the Windows platform via a window handle to active // // Examples: +// // ids, _ := robotgo.FindIds() -// robotgo.ActivePID(ids[0]) -func ActivePID(pid int32, args ...int) error { +// robotgo.ActivePid(ids[0]) +func ActivePid(pid int32, args ...int) error { var hwnd int if len(args) > 0 { hwnd = args[0] @@ -71,6 +72,7 @@ func DisplaysNum() int { // If cancel button is not given, only the default button is displayed // // Examples: +// // robotgo.Alert("hi", "window", "ok", "cancel") func Alert(title, msg string, args ...string) bool { return showAlert(title, msg, args...) diff --git a/robotgo_win.go b/robotgo_win.go index e8aced3..2d05078 100644 --- a/robotgo_win.go +++ b/robotgo_win.go @@ -57,7 +57,7 @@ func GetMain() win.HWND { return win.GetActiveWindow() } -// GetMianId get the main display id +// GetMainId get the main display id func GetMainId() int { return int(GetMain()) } diff --git a/robotgo_x11.go b/robotgo_x11.go index cc4dbc5..c6445f8 100644 --- a/robotgo_x11.go +++ b/robotgo_x11.go @@ -34,7 +34,7 @@ func GetBounds(pid int32, args ...int) (int, int, int, int) { return internalGetBounds(pid, hwnd) } - xid, err := GetXId(xu, pid) + xid, err := GetXid(xu, pid) if err != nil { log.Println("Get Xid from Pid errors is: ", err) return 0, 0, 0, 0 @@ -51,7 +51,7 @@ func GetClient(pid int32, args ...int) (int, int, int, int) { return internalGetClient(pid, hwnd) } - xid, err := GetXId(xu, pid) + xid, err := GetXid(xu, pid) if err != nil { log.Println("Get Xid from Pid errors is: ", err) return 0, 0, 0, 0 @@ -68,7 +68,7 @@ func internalGetTitle(pid int32, args ...int32) string { return cgetTitle(pid, hwnd) } - xid, err := GetXId(xu, pid) + xid, err := GetXid(xu, pid) if err != nil { log.Println("Get Xid from Pid errors is: ", err) return "" @@ -77,9 +77,9 @@ func internalGetTitle(pid int32, args ...int32) string { return cgetTitle(int32(xid), hwnd) } -// ActivePIDC active the window by PID, +// 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) error { +func ActivePidC(pid int32, args ...int) error { var hwnd int if len(args) > 0 { hwnd = args[0] @@ -87,7 +87,7 @@ func ActivePIDC(pid int32, args ...int) error { return nil } - xid, err := GetXId(xu, pid) + xid, err := GetXid(xu, pid) if err != nil { log.Println("Get Xid from Pid errors is: ", err) return err @@ -97,11 +97,11 @@ func ActivePIDC(pid int32, args ...int) error { return nil } -// ActivePID active the window by PID, +// ActivePid active the window by Pid, // // If args[0] > 0 on the Windows platform via a window handle to active, // If args[0] > 0 on the unix platform via a xid to active -func ActivePID(pid int32, args ...int) error { +func ActivePid(pid int32, args ...int) error { if xu == nil { var err error xu, err = xgbutil.NewConn() @@ -133,8 +133,8 @@ func ActivePID(pid int32, args ...int) error { return nil } -// GetXId get the xid return window and error -func GetXId(xu *xgbutil.XUtil, pid int32) (xproto.Window, error) { +// GetXid get the xid return window and error +func GetXid(xu *xgbutil.XUtil, pid int32) (xproto.Window, error) { if xu == nil { var err error xu, err = xgbutil.NewConn() @@ -148,7 +148,7 @@ func GetXId(xu *xgbutil.XUtil, pid int32) (xproto.Window, error) { return xid, err } -// GetXidFromPid get the xide from pid +// GetXidFromPid get the xid from pid func GetXidFromPid(xu *xgbutil.XUtil, pid int32) (xproto.Window, error) { windows, err := ewmh.ClientListGet(xu) if err != nil { @@ -214,6 +214,7 @@ func GetMainId() int { // If cancel button is not given, only the default button is displayed // // Examples: +// // robotgo.Alert("hi", "window", "ok", "cancel") func Alert(title, msg string, args ...string) bool { defaultBtn, cancelBtn := alertArgs(args...) diff --git a/screen/goScreen.h b/screen/goScreen.h index b9c0a16..e990d3e 100644 --- a/screen/goScreen.h +++ b/screen/goScreen.h @@ -99,7 +99,7 @@ uint32_t get_num_displays() { uintptr get_hwnd_by_pid(uintptr pid) { #if defined(IS_WINDOWS) - HWND hwnd = GetHwndByPId(pid); + HWND hwnd = GetHwndByPid(pid); return (uintptr)hwnd; #else return 0; diff --git a/window/goWindow.h b/window/goWindow.h index 7ab510e..e73300b 100644 --- a/window/goWindow.h +++ b/window/goWindow.h @@ -24,7 +24,7 @@ void min_window(uintptr pid, bool state, uintptr isHwnd){ // SetState((Window)pid, STATE_MINIMIZE, state); #elif defined(IS_WINDOWS) if (isHwnd == 0) { - HWND hwnd = GetHwndByPId(pid); + HWND hwnd = GetHwndByPid(pid); win_min(hwnd, state); } else { win_min((HWND)pid, state); @@ -41,7 +41,7 @@ void max_window(uintptr pid, bool state, uintptr isHwnd){ // SetState((Window)pid, STATE_MAXIMIZE, state); #elif defined(IS_WINDOWS) if (isHwnd == 0) { - HWND hwnd = GetHwndByPId(pid); + HWND hwnd = GetHwndByPid(pid); win_max(hwnd, state); } else { win_max((HWND)pid, state); diff --git a/window/win_sys.h b/window/win_sys.h index 9a80060..89e643d 100644 --- a/window/win_sys.h +++ b/window/win_sys.h @@ -130,7 +130,7 @@ Bounds get_bounds(uintptr pid, uintptr isHwnd){ #elif defined(IS_WINDOWS) HWND hwnd; if (isHwnd == 0) { - hwnd= GetHwndByPId(pid); + hwnd= GetHwndByPid(pid); } else { hwnd = (HWND)pid; } @@ -195,7 +195,7 @@ Bounds get_client(uintptr pid, uintptr isHwnd) { #elif defined(IS_WINDOWS) HWND hwnd; if (isHwnd == 0) { - hwnd = GetHwndByPId(pid); + hwnd = GetHwndByPid(pid); } else { hwnd = (HWND)pid; } diff --git a/window/window.h b/window/window.h index f25c60c..b4a861f 100644 --- a/window/window.h +++ b/window/window.h @@ -60,7 +60,7 @@ MData set_handle_pid(uintptr pid, uintptr isHwnd){ #elif defined(IS_WINDOWS) // win.HWnd = (HWND)pid; // Handle to a window HWND if (isHwnd == 0) { - win.HWnd = GetHwndByPId(pid); + win.HWnd = GetHwndByPid(pid); } else { win.HWnd = (HWND)pid; }