Parent window id (macOS).

This commit is contained in:
Nuno Cruces 2022-06-09 13:35:02 +01:00
parent 3adf86dd39
commit b3fe9d02e9
6 changed files with 147 additions and 23 deletions

View file

@ -1,16 +1,7 @@
package zenutil package zenutil
import "strconv"
// These are internal. // These are internal.
var ( var (
Separator = "\x00" Separator = "\x00"
LineBreak = "\n" LineBreak = "\n"
) )
func ParseWindowId(id string) any {
if pid, err := strconv.ParseUint(id, 0, 64); err == nil {
return int(pid)
}
return id
}

View file

@ -2,15 +2,8 @@
package zenutil package zenutil
import "strconv"
// These are internal. // These are internal.
var ( var (
Separator = "\x1e" Separator = "\x1e"
LineBreak = "\n" LineBreak = "\n"
) )
func ParseWindowId(id string) int {
wid, _ := strconv.ParseUint(id, 0, 64)
return int(wid)
}

View file

@ -1,14 +1,7 @@
package zenutil package zenutil
import "strconv"
// These are internal. // These are internal.
var ( var (
Separator string Separator string
LineBreak = "\r\n" LineBreak = "\r\n"
) )
func ParseWindowId(id string) uintptr {
hwnd, _ := strconv.ParseUint(id, 0, 64)
return uintptr(hwnd)
}

View file

@ -0,0 +1,91 @@
package zenutil
import (
"os"
"strconv"
"syscall"
"unsafe"
)
// ParseWindowId is internal.
func ParseWindowId(id string) any {
if pid, err := strconv.ParseUint(id, 0, 64); err == nil {
return int(pid)
}
return id
}
// GetParentWindowId is internal.
func GetParentWindowId() int {
buf, err := sysctlKernProcAll()
if err != nil {
return 0
}
type kinfo_proc struct {
_ [40]byte
Pid int32
_ [516]byte
PPid int32
_ [84]byte
}
const size = int(unsafe.Sizeof(kinfo_proc{}))
ppids := map[int]int{}
for i := 0; i+size < len(buf); i += size {
p := (*kinfo_proc)(unsafe.Pointer(&buf[i]))
ppids[int(p.Pid)] = int(p.PPid)
}
pid := os.Getppid()
for {
ppid := ppids[pid]
switch ppid {
case 0:
return 0
case 1:
return pid
default:
pid = ppid
}
}
}
func sysctlKernProcAll() ([]byte, error) {
const (
CTRL_KERN = 1
KERN_PROC = 14
KERN_PROC_ALL = 0
)
mib := [4]int32{CTRL_KERN, KERN_PROC, KERN_PROC_ALL, 0}
size := uintptr(0)
_, _, errno := syscall.Syscall6(
syscall.SYS___SYSCTL,
uintptr(unsafe.Pointer(&mib[0])),
uintptr(len(mib)),
0,
uintptr(unsafe.Pointer(&size)),
0,
0)
if errno != 0 {
return nil, errno
}
bs := make([]byte, size)
_, _, errno = syscall.Syscall6(
syscall.SYS___SYSCTL,
uintptr(unsafe.Pointer(&mib[0])),
uintptr(len(mib)),
uintptr(unsafe.Pointer(&bs[0])),
uintptr(unsafe.Pointer(&size)),
0,
0)
if errno != 0 {
return nil, errno
}
return bs[0:size], nil
}

View file

@ -0,0 +1,42 @@
//go:build !windows && !darwin
package zenutil
import (
"bytes"
"fmt"
"io"
"os/exec"
"strconv"
)
// ParseWindowId is internal.
func ParseWindowId(id string) int {
wid, _ := strconv.ParseUint(id, 0, 64)
return int(wid)
}
// GetParentWindowId is internal.
func GetParentWindowId() int {
buf, err := exec.Command("ps", "-xo", "pid=,ppid=").CombinedOutput()
if err != nil {
return 0
}
ppids := map[int]int{}
reader := bytes.NewReader(buf)
for {
var pid, ppid int
_, err := fmt.Fscan(reader, &pid, &ppid)
if err == io.EOF {
break
}
if err != nil {
return 0
}
ppids[pid] = ppid
}
// Find the relevant pid and window id.
return 0
}

View file

@ -0,0 +1,14 @@
package zenutil
import "strconv"
// ParseWindowId is internal.
func ParseWindowId(id string) uintptr {
hwnd, _ := strconv.ParseUint(id, 0, 64)
return uintptr(hwnd)
}
// GetParentWindowId is internal.
func GetParentWindowId() int {
return 0
}