go-toast

Send toast notifications in Windows
Log | Files | Refs | README | LICENSE

commit d9dbb391d85e5beff96924ef4a9ce1d1e0bc345e
parent 96c735044b149648fbe6051b9137b6db2dd3c4ef
Author: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Date:   Tue,  5 Dec 2023 18:47:25 +0800

wintoast: use windows utf16 helpers

Signed-off-by: Jack Mordaunt <jackmordaunt.dev@gmail.com>

Diffstat:
Mwintoast/bind_windows.go | 26+++-----------------------
Mwintoast/impl.go | 5+++--
2 files changed, 6 insertions(+), 25 deletions(-)

diff --git a/wintoast/bind_windows.go b/wintoast/bind_windows.go @@ -10,13 +10,13 @@ import ( "os/exec" "sync" "syscall" - "unicode/utf16" "unsafe" "git.sr.ht/~jackmordaunt/go-toast/internal/winrt/data/xml/dom" "git.sr.ht/~jackmordaunt/go-toast/internal/winrt/ui/notifications" "git.sr.ht/~jackmordaunt/go-toast/tmpl" "github.com/go-ole/go-ole" + "golang.org/x/sys/windows" ) func pushPowershell(xml string) error { @@ -176,30 +176,10 @@ func sliceUserDataFromUnsafe(ptr unsafe.Pointer, count int) []UserData { // Convert the unsafe layout to safe strings. for ii, it := range tmp { out[ii] = UserData{ - Key: utf16PtrToString((*uint16)(it.Key)), - Value: utf16PtrToString((*uint16)(it.Value)), + Key: windows.UTF16PtrToString((*uint16)(it.Key)), + Value: windows.UTF16PtrToString((*uint16)(it.Value)), } } return out } - -// utf16PtrToString builds a string out of a utf16 null terminated byte sequence. -// -// Copied from package syscall. -func utf16PtrToString(p *uint16) string { - if p == nil { - return "" - } - // Find NUL terminator. - end := unsafe.Pointer(p) - n := 0 - for *(*uint16)(end) != 0 { - end = unsafe.Pointer(uintptr(end) + unsafe.Sizeof(*p)) - n++ - } - // Turn *uint16 into []uint16. - s := unsafe.Slice(p, n) - // Decode []uint16 into string. - return string(utf16.Decode(s)) -} diff --git a/wintoast/impl.go b/wintoast/impl.go @@ -26,6 +26,7 @@ import ( "unsafe" "github.com/go-ole/go-ole" + "golang.org/x/sys/windows" ) // Interface GUIDS. These GUIDS are predefined by the Windows Runtime, identifying the various @@ -169,8 +170,8 @@ var ( count uint32, ) (ret uintptr) { callback( - utf16PtrToString((*uint16)(appUserModelId)), - utf16PtrToString((*uint16)(invokedArgs)), + windows.UTF16PtrToString((*uint16)(appUserModelId)), + windows.UTF16PtrToString((*uint16)(invokedArgs)), sliceUserDataFromUnsafe(data, int(count)), ) return