Workaround #17.

This commit is contained in:
Nuno Cruces 2021-08-14 02:50:04 +01:00
parent 2fa0d652db
commit 01c2abbb00
3 changed files with 39 additions and 23 deletions

View file

@ -11,9 +11,11 @@ import (
"image/color" "image/color"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"syscall"
"time" "time"
"github.com/ncruces/zenity" "github.com/ncruces/zenity"
@ -113,11 +115,15 @@ func main() {
if unixeol { if unixeol {
zenutil.LineBreak = "\n" zenutil.LineBreak = "\n"
} }
if zenutil.Timeout > 0 { ctx, cancel := signal.NotifyContext(context.Background(),
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(zenutil.Timeout)*time.Second) syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
opts = append(opts, zenity.Context(ctx))
defer cancel() defer cancel()
if zenutil.Timeout > 0 {
c, cancel := context.WithTimeout(ctx, time.Duration(zenutil.Timeout)*time.Second)
defer cancel()
ctx = c
} }
opts = append(opts, zenity.Context(ctx))
switch { switch {
case errorDlg: case errorDlg:
@ -458,7 +464,7 @@ func errResult(err error) {
if os.IsTimeout(err) { if os.IsTimeout(err) {
os.Exit(5) os.Exit(5)
} }
if err == zenity.ErrCanceled { if err == zenity.ErrCanceled || err == context.Canceled {
os.Exit(1) os.Exit(1)
} }
if err == zenity.ErrExtraButton { if err == zenity.ErrExtraButton {
@ -471,36 +477,24 @@ func errResult(err error) {
os.Stderr.WriteString(zenutil.LineBreak) os.Stderr.WriteString(zenutil.LineBreak)
os.Exit(-1) os.Exit(-1)
} }
os.Exit(0)
} }
func strResult(s string, err error) { func strResult(s string, err error) {
if err != nil {
errResult(err) errResult(err)
}
os.Stdout.WriteString(s) os.Stdout.WriteString(s)
os.Stdout.WriteString(zenutil.LineBreak) os.Stdout.WriteString(zenutil.LineBreak)
os.Exit(0)
} }
func lstResult(l []string, err error) { func lstResult(l []string, err error) {
if err != nil {
errResult(err) errResult(err)
}
if len(l) > 0 {
os.Stdout.WriteString(strings.Join(l, zenutil.Separator)) os.Stdout.WriteString(strings.Join(l, zenutil.Separator))
os.Stdout.WriteString(zenutil.LineBreak) os.Stdout.WriteString(zenutil.LineBreak)
}
os.Exit(0)
} }
func colResult(c color.Color, err error) { func colResult(c color.Color, err error) {
if err != nil {
errResult(err) errResult(err)
}
os.Stdout.WriteString(zenutil.UnparseColor(c)) os.Stdout.WriteString(zenutil.UnparseColor(c))
os.Stdout.WriteString(zenutil.LineBreak) os.Stdout.WriteString(zenutil.LineBreak)
os.Exit(0)
} }
func ingestPath(path string) string { func ingestPath(path string) string {

View file

@ -1,14 +1,17 @@
package zenity package zenity
import ( import (
"math/rand"
"runtime" "runtime"
"syscall" "syscall"
"time"
"unsafe" "unsafe"
"github.com/ncruces/zenity/internal/zenutil" "github.com/ncruces/zenity/internal/zenutil"
) )
var ( var (
rtlGetNtVersionNumbers = ntdll.NewProc("RtlGetNtVersionNumbers")
shellNotifyIcon = shell32.NewProc("Shell_NotifyIconW") shellNotifyIcon = shell32.NewProc("Shell_NotifyIconW")
wtsSendMessage = wtsapi32.NewProc("WTSSendMessageW") wtsSendMessage = wtsapi32.NewProc("WTSSendMessageW")
) )
@ -20,7 +23,7 @@ func notify(text string, opts options) error {
var args _NOTIFYICONDATA var args _NOTIFYICONDATA
args.StructSize = uint32(unsafe.Sizeof(args)) args.StructSize = uint32(unsafe.Sizeof(args))
args.ID = 0x378eb49c // random args.ID = rand.Uint32()
args.Flags = 0x00000010 // NIF_INFO args.Flags = 0x00000010 // NIF_INFO
args.State = 0x00000001 // NIS_HIDDEN args.State = 0x00000001 // NIS_HIDDEN
@ -52,6 +55,24 @@ func notify(text string, opts options) error {
return err return err
} }
var major, minor, build uint32
rtlGetNtVersionNumbers.Call(
uintptr(unsafe.Pointer(&major)),
uintptr(unsafe.Pointer(&minor)),
uintptr(unsafe.Pointer(&build)))
// On Windows 7 (6.1) and lower, wait up to 10 seconds to clean up.
if major < 6 || major == 6 && minor < 2 {
if opts.ctx != nil {
select {
case <-opts.ctx.Done():
case <-time.After(10 * time.Second):
}
} else {
time.Sleep(10 * time.Second)
}
}
shellNotifyIcon.Call(2 /* NIM_DELETE */, uintptr(unsafe.Pointer(&args))) shellNotifyIcon.Call(2 /* NIM_DELETE */, uintptr(unsafe.Pointer(&args)))
return nil return nil
} }

View file

@ -17,6 +17,7 @@ var (
comdlg32 = syscall.NewLazyDLL("comdlg32.dll") comdlg32 = syscall.NewLazyDLL("comdlg32.dll")
gdi32 = syscall.NewLazyDLL("gdi32.dll") gdi32 = syscall.NewLazyDLL("gdi32.dll")
kernel32 = syscall.NewLazyDLL("kernel32.dll") kernel32 = syscall.NewLazyDLL("kernel32.dll")
ntdll = syscall.NewLazyDLL("ntdll.dll")
ole32 = syscall.NewLazyDLL("ole32.dll") ole32 = syscall.NewLazyDLL("ole32.dll")
shell32 = syscall.NewLazyDLL("shell32.dll") shell32 = syscall.NewLazyDLL("shell32.dll")
user32 = syscall.NewLazyDLL("user32.dll") user32 = syscall.NewLazyDLL("user32.dll")