From b94ee718bacafe66e2d1bb6f2ba07c91e93b1b98 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Mon, 13 Jun 2022 23:46:03 +0100 Subject: [PATCH] Parent window id (macOS). --- cmd/zenity/main.go | 7 ++- go.mod | 2 +- go.sum | 4 +- internal/zenutil/window_darwin.go | 71 ++++--------------------------- 4 files changed, 17 insertions(+), 67 deletions(-) diff --git a/cmd/zenity/main.go b/cmd/zenity/main.go index fa6feaf..a2563c2 100644 --- a/cmd/zenity/main.go +++ b/cmd/zenity/main.go @@ -52,6 +52,7 @@ var ( icon string windowIcon string attach string + modal bool multiple bool defaultCancel bool @@ -211,7 +212,7 @@ func setupFlags() { flag.StringVar(&text, "text", "", "Set the dialog `text`") flag.StringVar(&windowIcon, "window-icon", "", "Set the window `icon` (error, info, question, warning)") flag.StringVar(&attach, "attach", "", "Set the parent `window` to attach to") - flag.Bool("modal", true, "Set the modal hint") + flag.BoolVar(&modal, "modal", runtime.GOOS == "darwin", "Set the modal hint") flag.BoolVar(&multiple, "multiple", false, "Allow multiple items to be selected") flag.BoolVar(&defaultCancel, "default-cancel", false, "Give Cancel button focus by default") @@ -448,6 +449,10 @@ func loadFlags() []zenity.Option { if attach != "" { opts = append(opts, zenity.Attach(zenutil.ParseWindowId(attach))) + } else if modal { + if pid := zenutil.GetParentWindowId(); pid != 0 { + opts = append(opts, zenity.Attach(pid)) + } } // Message options diff --git a/go.mod b/go.mod index 357a5e5..158374e 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844 go.uber.org/goleak v1.1.12 // test golang.org/x/image v0.0.0-20220601225756-64ec528b34cd - golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a + golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d ) require ( diff --git a/go.sum b/go.sum index 1a948ca..a4e7300 100644 --- a/go.sum +++ b/go.sum @@ -42,8 +42,8 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d h1:Zu/JngovGLVi6t2J3nmAf3AoTDwuzw85YZ3b9o4yU7s= +golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/internal/zenutil/window_darwin.go b/internal/zenutil/window_darwin.go index 3bffc43..51c74df 100644 --- a/internal/zenutil/window_darwin.go +++ b/internal/zenutil/window_darwin.go @@ -3,8 +3,8 @@ package zenutil import ( "os" "strconv" - "syscall" - "unsafe" + + "golang.org/x/sys/unix" ) // ParseWindowId is internal. @@ -17,75 +17,20 @@ func ParseWindowId(id string) any { // 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] + kinfo, err := unix.SysctlKinfoProc("kern.proc.pid", pid) + if err != nil { + return 0 + } + ppid := kinfo.Eproc.Ppid switch ppid { case 0: return 0 case 1: return pid default: - pid = ppid + pid = int(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 -}