Initial commit
This commit is contained in:
commit
804c4b0c12
13
examples/example.go
Normal file
13
examples/example.go
Normal file
@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/egosown/getwindow"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
fmt.Println(getwindow.GetWindow())
|
||||
|
||||
}
|
14
getwindow/getwindow.go
Normal file
14
getwindow/getwindow.go
Normal file
@ -0,0 +1,14 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package getwindow
|
||||
|
||||
import "os/exec"
|
||||
|
||||
func GetWindow() (string, error) {
|
||||
win, err := exec.Command("xdotool getactivewindow getwindowname").Output()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(win), nil
|
||||
}
|
54
getwindow/getwindow_windows.go
Normal file
54
getwindow/getwindow_windows.go
Normal file
@ -0,0 +1,54 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package getwindow
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"golang.org/x/sys/windows"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var (
|
||||
mod = windows.NewLazyDLL("user32.dll")
|
||||
procGetClassNameW = mod.NewProc("GetClassNameW")
|
||||
)
|
||||
|
||||
type (
|
||||
HANDLE uintptr
|
||||
HWND HANDLE
|
||||
)
|
||||
|
||||
func getClassName(hwnd HWND) (name string, err error) {
|
||||
n := make([]uint16, 256)
|
||||
p := &n[0]
|
||||
r0, _, e1 := syscall.Syscall(procGetClassNameW.Addr(), 3, uintptr(hwnd), uintptr(unsafe.Pointer(p)), uintptr(len(n)))
|
||||
if r0 == 0 {
|
||||
if e1 != 0 {
|
||||
err = error(e1)
|
||||
} else {
|
||||
err = syscall.EINVAL
|
||||
}
|
||||
return
|
||||
}
|
||||
name = syscall.UTF16ToString(n)
|
||||
return
|
||||
}
|
||||
|
||||
func getWindow(funcName string) uintptr {
|
||||
proc := mod.NewProc(funcName)
|
||||
hwnd, _, _ := proc.Call()
|
||||
return hwnd
|
||||
}
|
||||
|
||||
func GetWindow() (string, error) {
|
||||
|
||||
hwnd := getWindow("GetForegroundWindow")
|
||||
if hwnd != 0 {
|
||||
cn, _ := getClassName(HWND(hwnd))
|
||||
return cn, nil
|
||||
}
|
||||
return "", errors.New("Could not get window")
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user