2020-01-07 19:44:26 -05:00
|
|
|
package zenity
|
|
|
|
|
2020-01-12 13:55:10 -05:00
|
|
|
import (
|
2020-01-30 09:14:42 -05:00
|
|
|
"context"
|
2020-01-12 13:55:10 -05:00
|
|
|
"fmt"
|
2021-01-05 10:20:42 -05:00
|
|
|
"os"
|
2021-03-10 09:49:09 -05:00
|
|
|
"reflect"
|
2021-03-26 08:40:24 -04:00
|
|
|
"runtime"
|
|
|
|
"sync/atomic"
|
2020-01-12 13:55:10 -05:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
2020-01-07 19:44:26 -05:00
|
|
|
|
|
|
|
var (
|
|
|
|
comdlg32 = syscall.NewLazyDLL("comdlg32.dll")
|
2021-03-29 14:07:44 -04:00
|
|
|
gdi32 = syscall.NewLazyDLL("gdi32.dll")
|
2020-01-07 19:44:26 -05:00
|
|
|
kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
|
|
|
ole32 = syscall.NewLazyDLL("ole32.dll")
|
|
|
|
shell32 = syscall.NewLazyDLL("shell32.dll")
|
|
|
|
user32 = syscall.NewLazyDLL("user32.dll")
|
2020-01-30 21:18:43 -05:00
|
|
|
wtsapi32 = syscall.NewLazyDLL("wtsapi32.dll")
|
2020-01-07 19:44:26 -05:00
|
|
|
|
2020-01-12 13:55:10 -05:00
|
|
|
commDlgExtendedError = comdlg32.NewProc("CommDlgExtendedError")
|
|
|
|
|
2021-04-08 19:55:49 -04:00
|
|
|
deleteObject = gdi32.NewProc("DeleteObject")
|
|
|
|
getDeviceCaps = gdi32.NewProc("GetDeviceCaps")
|
|
|
|
createFontIndirect = gdi32.NewProc("CreateFontIndirectW")
|
|
|
|
|
2021-03-29 14:07:44 -04:00
|
|
|
getModuleHandle = kernel32.NewProc("GetModuleHandleW")
|
2020-01-07 19:44:26 -05:00
|
|
|
getCurrentThreadId = kernel32.NewProc("GetCurrentThreadId")
|
2021-01-05 10:20:42 -05:00
|
|
|
getConsoleWindow = kernel32.NewProc("GetConsoleWindow")
|
2020-01-07 19:44:26 -05:00
|
|
|
|
|
|
|
coInitializeEx = ole32.NewProc("CoInitializeEx")
|
|
|
|
coUninitialize = ole32.NewProc("CoUninitialize")
|
|
|
|
coCreateInstance = ole32.NewProc("CoCreateInstance")
|
|
|
|
coTaskMemFree = ole32.NewProc("CoTaskMemFree")
|
|
|
|
|
2021-03-26 08:40:24 -04:00
|
|
|
getMessage = user32.NewProc("GetMessageW")
|
|
|
|
sendMessage = user32.NewProc("SendMessageW")
|
2021-04-08 19:55:49 -04:00
|
|
|
isDialogMessage = user32.NewProc("IsDialogMessageW")
|
|
|
|
dispatchMessage = user32.NewProc("DispatchMessageW")
|
|
|
|
translateMessage = user32.NewProc("TranslateMessage")
|
2021-03-26 08:40:24 -04:00
|
|
|
getClassName = user32.NewProc("GetClassNameW")
|
|
|
|
unhookWindowsHookEx = user32.NewProc("UnhookWindowsHookEx")
|
2021-04-08 19:55:49 -04:00
|
|
|
setWindowsHookEx = user32.NewProc("SetWindowsHookExW")
|
2021-03-26 08:40:24 -04:00
|
|
|
callNextHookEx = user32.NewProc("CallNextHookEx")
|
|
|
|
enumWindows = user32.NewProc("EnumWindows")
|
|
|
|
enumChildWindows = user32.NewProc("EnumChildWindows")
|
|
|
|
setWindowText = user32.NewProc("SetWindowTextW")
|
2021-04-05 12:54:46 -04:00
|
|
|
getWindowText = user32.NewProc("GetWindowTextW")
|
|
|
|
getWindowTextLength = user32.NewProc("GetWindowTextLengthW")
|
2021-03-26 08:40:24 -04:00
|
|
|
setForegroundWindow = user32.NewProc("SetForegroundWindow")
|
|
|
|
getWindowThreadProcessId = user32.NewProc("GetWindowThreadProcessId")
|
|
|
|
setThreadDpiAwarenessContext = user32.NewProc("SetThreadDpiAwarenessContext")
|
2021-04-08 19:55:49 -04:00
|
|
|
getDpiForWindow = user32.NewProc("GetDpiForWindow")
|
|
|
|
releaseDC = user32.NewProc("ReleaseDC")
|
|
|
|
getWindowDC = user32.NewProc("GetWindowDC")
|
|
|
|
systemParametersInfo = user32.NewProc("SystemParametersInfoW")
|
2020-01-07 19:44:26 -05:00
|
|
|
)
|
|
|
|
|
2021-04-08 19:55:49 -04:00
|
|
|
func intptr(i int64) uintptr {
|
|
|
|
return uintptr(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func strptr(s string) uintptr {
|
|
|
|
return uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(s)))
|
|
|
|
}
|
|
|
|
|
2021-03-26 08:40:24 -04:00
|
|
|
func setup() context.CancelFunc {
|
2021-01-05 10:20:42 -05:00
|
|
|
var hwnd uintptr
|
|
|
|
enumWindows.Call(syscall.NewCallback(func(wnd, lparam uintptr) uintptr {
|
|
|
|
var pid uintptr
|
|
|
|
getWindowThreadProcessId.Call(wnd, uintptr(unsafe.Pointer(&pid)))
|
|
|
|
if int(pid) == os.Getpid() {
|
|
|
|
hwnd = wnd
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}), 0)
|
|
|
|
if hwnd == 0 {
|
|
|
|
hwnd, _, _ = getConsoleWindow.Call()
|
|
|
|
}
|
|
|
|
if hwnd != 0 {
|
|
|
|
setForegroundWindow.Call(hwnd)
|
|
|
|
}
|
2021-03-26 08:40:24 -04:00
|
|
|
|
|
|
|
var old uintptr
|
|
|
|
runtime.LockOSThread()
|
|
|
|
if setThreadDpiAwarenessContext.Find() == nil {
|
|
|
|
// try:
|
|
|
|
// DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
|
|
|
|
// DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE
|
|
|
|
// DPI_AWARENESS_CONTEXT_SYSTEM_AWARE
|
|
|
|
for i := -4; i <= -2; i++ {
|
|
|
|
restore, _, _ := setThreadDpiAwarenessContext.Call(uintptr(i))
|
|
|
|
if restore != 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
if old != 0 {
|
|
|
|
setThreadDpiAwarenessContext.Call(old)
|
|
|
|
runtime.UnlockOSThread()
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 10:20:42 -05:00
|
|
|
}
|
|
|
|
|
2020-01-12 13:55:10 -05:00
|
|
|
func commDlgError() error {
|
2020-01-29 09:15:21 -05:00
|
|
|
s, _, _ := commDlgExtendedError.Call()
|
|
|
|
if s == 0 {
|
2020-01-12 13:55:10 -05:00
|
|
|
return nil
|
|
|
|
} else {
|
2020-01-29 09:15:21 -05:00
|
|
|
return fmt.Errorf("Common Dialog error: %x", s)
|
2020-01-12 13:55:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-30 09:14:42 -05:00
|
|
|
func hookDialog(ctx context.Context, initDialog func(wnd uintptr)) (unhook context.CancelFunc, err error) {
|
|
|
|
if ctx != nil && ctx.Err() != nil {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
var hook, wnd uintptr
|
2021-03-29 14:07:44 -04:00
|
|
|
callNextHookEx := callNextHookEx.Addr()
|
2020-01-24 08:59:03 -05:00
|
|
|
tid, _, _ := getCurrentThreadId.Call()
|
|
|
|
hook, _, err = setWindowsHookEx.Call(12, // WH_CALLWNDPROCRET
|
2021-03-29 14:07:44 -04:00
|
|
|
syscall.NewCallback(func(code int32, wparam uintptr, lparam *_CWPRETSTRUCT) uintptr {
|
2020-01-24 08:59:03 -05:00
|
|
|
if lparam.Message == 0x0110 { // WM_INITDIALOG
|
2020-01-30 09:14:42 -05:00
|
|
|
name := [8]uint16{}
|
2020-01-24 08:59:03 -05:00
|
|
|
getClassName.Call(lparam.Wnd, uintptr(unsafe.Pointer(&name)), uintptr(len(name)))
|
|
|
|
if syscall.UTF16ToString(name[:]) == "#32770" { // The class for a dialog box
|
2020-01-30 09:14:42 -05:00
|
|
|
var close bool
|
|
|
|
|
|
|
|
if ctx != nil && ctx.Err() != nil {
|
|
|
|
close = true
|
|
|
|
} else {
|
2021-03-26 08:40:24 -04:00
|
|
|
atomic.StoreUintptr(&wnd, lparam.Wnd)
|
2020-01-30 09:14:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if close {
|
|
|
|
sendMessage.Call(lparam.Wnd, 0x0112 /* WM_SYSCOMMAND */, 0xf060 /* SC_CLOSE */, 0)
|
|
|
|
} else if initDialog != nil {
|
|
|
|
initDialog(lparam.Wnd)
|
|
|
|
}
|
2020-01-24 08:59:03 -05:00
|
|
|
}
|
|
|
|
}
|
2021-03-29 14:07:44 -04:00
|
|
|
next, _, _ := syscall.Syscall6(callNextHookEx, 4,
|
|
|
|
hook, uintptr(code), wparam, uintptr(unsafe.Pointer(lparam)),
|
|
|
|
0, 0)
|
2020-01-24 08:59:03 -05:00
|
|
|
return next
|
|
|
|
}), 0, tid)
|
2020-01-30 09:14:42 -05:00
|
|
|
|
|
|
|
if hook == 0 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ctx == nil {
|
|
|
|
return func() { unhookWindowsHookEx.Call(hook) }, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
wait := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-03-26 08:40:24 -04:00
|
|
|
if w := atomic.LoadUintptr(&wnd); w != 0 {
|
2020-01-30 09:14:42 -05:00
|
|
|
sendMessage.Call(w, 0x0112 /* WM_SYSCOMMAND */, 0xf060 /* SC_CLOSE */, 0)
|
|
|
|
}
|
|
|
|
case <-wait:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return func() {
|
|
|
|
unhookWindowsHookEx.Call(hook)
|
|
|
|
close(wait)
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-03 22:25:45 -05:00
|
|
|
func hookDialogTitle(ctx context.Context, title *string) (unhook context.CancelFunc, err error) {
|
|
|
|
var init func(wnd uintptr)
|
|
|
|
if title != nil {
|
|
|
|
init = func(wnd uintptr) {
|
2021-04-05 13:58:32 -04:00
|
|
|
setWindowText.Call(wnd, strptr(*title))
|
2021-03-03 22:25:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return hookDialog(ctx, init)
|
2020-01-24 08:59:03 -05:00
|
|
|
}
|
|
|
|
|
2021-04-08 19:55:49 -04:00
|
|
|
type dpi uintptr
|
|
|
|
|
|
|
|
func getDPI(wnd uintptr) dpi {
|
|
|
|
var res uintptr
|
|
|
|
|
|
|
|
if wnd != 0 && getDpiForWindow.Find() == nil {
|
|
|
|
res, _, _ = getDpiForWindow.Call(wnd)
|
|
|
|
} else if dc, _, _ := getWindowDC.Call(wnd); dc != 0 {
|
|
|
|
res, _, _ = getDeviceCaps.Call(dc, 90) // LOGPIXELSY
|
|
|
|
releaseDC.Call(0, dc)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res == 0 {
|
|
|
|
return 96 // USER_DEFAULT_SCREEN_DPI
|
|
|
|
}
|
|
|
|
return dpi(res)
|
2021-04-05 12:54:46 -04:00
|
|
|
}
|
|
|
|
|
2021-04-08 19:55:49 -04:00
|
|
|
func (d dpi) Scale(dim uintptr) uintptr {
|
|
|
|
if d == 0 {
|
|
|
|
return dim
|
|
|
|
}
|
|
|
|
return dim * uintptr(d) / 96 // USER_DEFAULT_SCREEN_DPI
|
|
|
|
}
|
|
|
|
|
|
|
|
type font struct {
|
|
|
|
handle uintptr
|
|
|
|
logical _LOGFONT
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFont() font {
|
|
|
|
var metrics _NONCLIENTMETRICS
|
|
|
|
metrics.Size = uint32(unsafe.Sizeof(metrics))
|
|
|
|
systemParametersInfo.Call(0x29, // SPI_GETNONCLIENTMETRICS
|
|
|
|
unsafe.Sizeof(metrics), uintptr(unsafe.Pointer(&metrics)), 0)
|
|
|
|
return font{logical: metrics.MessageFont}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *font) ForDPI(dpi dpi) uintptr {
|
|
|
|
if h := -int32(dpi.Scale(12)); f.handle == 0 || f.logical.Height != h {
|
|
|
|
f.Delete()
|
|
|
|
f.logical.Height = h
|
|
|
|
f.handle, _, _ = createFontIndirect.Call(uintptr(unsafe.Pointer(&f.logical)))
|
|
|
|
}
|
|
|
|
return f.handle
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *font) Delete() {
|
|
|
|
if f.handle != 0 {
|
|
|
|
deleteObject.Call(f.handle)
|
|
|
|
f.handle = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues
|
|
|
|
func messageLoop(wnd uintptr) error {
|
|
|
|
getMessage := getMessage.Addr()
|
|
|
|
isDialogMessage := isDialogMessage.Addr()
|
|
|
|
translateMessage := translateMessage.Addr()
|
|
|
|
dispatchMessage := dispatchMessage.Addr()
|
|
|
|
|
|
|
|
for {
|
|
|
|
var msg _MSG
|
|
|
|
ret, _, err := syscall.Syscall6(getMessage, 4, uintptr(unsafe.Pointer(&msg)), 0, 0, 0, 0, 0)
|
|
|
|
if int32(ret) == -1 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ret == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, _, _ = syscall.Syscall(isDialogMessage, 2, wnd, uintptr(unsafe.Pointer(&msg)), 0)
|
|
|
|
if ret == 0 {
|
|
|
|
syscall.Syscall(translateMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0)
|
|
|
|
syscall.Syscall(dispatchMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-cwpretstruct
|
|
|
|
type _CWPRETSTRUCT struct {
|
|
|
|
Result uintptr
|
|
|
|
LParam uintptr
|
|
|
|
WParam uintptr
|
|
|
|
Message uint32
|
|
|
|
Wnd uintptr
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-logfontw
|
|
|
|
type _LOGFONT struct {
|
|
|
|
Height int32
|
|
|
|
Width int32
|
|
|
|
Escapement int32
|
|
|
|
Orientation int32
|
|
|
|
Weight int32
|
|
|
|
Italic byte
|
|
|
|
Underline byte
|
|
|
|
StrikeOut byte
|
|
|
|
CharSet byte
|
|
|
|
OutPrecision byte
|
|
|
|
ClipPrecision byte
|
|
|
|
Quality byte
|
|
|
|
PitchAndFamily byte
|
|
|
|
FaceName [32]uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-nonclientmetricsw
|
|
|
|
type _NONCLIENTMETRICS struct {
|
|
|
|
Size uint32
|
|
|
|
BorderWidth int32
|
|
|
|
ScrollWidth int32
|
|
|
|
ScrollHeight int32
|
|
|
|
CaptionWidth int32
|
|
|
|
CaptionHeight int32
|
|
|
|
CaptionFont _LOGFONT
|
|
|
|
SmCaptionWidth int32
|
|
|
|
SmCaptionHeight int32
|
|
|
|
SmCaptionFont _LOGFONT
|
|
|
|
MenuWidth int32
|
|
|
|
MenuHeight int32
|
|
|
|
MenuFont _LOGFONT
|
|
|
|
StatusFont _LOGFONT
|
|
|
|
MessageFont _LOGFONT
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-msg
|
|
|
|
type _MSG struct {
|
|
|
|
Owner syscall.Handle
|
|
|
|
Message uint32
|
|
|
|
WParam uintptr
|
|
|
|
LParam uintptr
|
|
|
|
Time uint32
|
|
|
|
Pt _POINT
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/windef/ns-windef-point
|
|
|
|
type _POINT struct {
|
|
|
|
x, y int32
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/windef/ns-windef-rect
|
|
|
|
type _RECT struct {
|
|
|
|
left int32
|
|
|
|
top int32
|
|
|
|
right int32
|
|
|
|
bottom int32
|
2021-04-05 13:58:32 -04:00
|
|
|
}
|
|
|
|
|
2021-03-10 09:49:09 -05:00
|
|
|
// https://github.com/wine-mirror/wine/blob/master/include/unknwn.idl
|
|
|
|
|
|
|
|
type _IUnknownVtbl struct {
|
|
|
|
QueryInterface uintptr
|
|
|
|
AddRef uintptr
|
|
|
|
Release uintptr
|
|
|
|
}
|
|
|
|
|
|
|
|
func uuid(s string) uintptr {
|
|
|
|
return (*reflect.StringHeader)(unsafe.Pointer(&s)).Data
|
|
|
|
}
|
|
|
|
|
2020-01-12 13:55:10 -05:00
|
|
|
type _COMObject struct{}
|
|
|
|
|
|
|
|
func (o *_COMObject) Call(trap uintptr, a ...uintptr) (r1, r2 uintptr, lastErr error) {
|
|
|
|
self := uintptr(unsafe.Pointer(o))
|
|
|
|
nargs := uintptr(len(a))
|
|
|
|
switch nargs {
|
|
|
|
case 0:
|
|
|
|
return syscall.Syscall(trap, nargs+1, self, 0, 0)
|
|
|
|
case 1:
|
|
|
|
return syscall.Syscall(trap, nargs+1, self, a[0], 0)
|
|
|
|
case 2:
|
|
|
|
return syscall.Syscall(trap, nargs+1, self, a[0], a[1])
|
|
|
|
default:
|
|
|
|
panic("COM call with too many arguments.")
|
|
|
|
}
|
|
|
|
}
|