134 lines
3.3 KiB
Go
134 lines
3.3 KiB
Go
package main
|
|
|
|
// Start with Windows: a per-user entry in the Run list, toggled from the
|
|
// right-click menu. No administrator rights needed.
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
"unsafe"
|
|
)
|
|
|
|
var (
|
|
advapi32 = syscall.NewLazyDLL("advapi32.dll")
|
|
pRegOpenKeyEx = advapi32.NewProc("RegOpenKeyExW")
|
|
pRegSetValueEx = advapi32.NewProc("RegSetValueExW")
|
|
pRegQueryValueEx = advapi32.NewProc("RegQueryValueExW")
|
|
pRegDeleteValue = advapi32.NewProc("RegDeleteValueW")
|
|
pRegCloseKey = advapi32.NewProc("RegCloseKey")
|
|
pGetAsyncKeyState = syscall.NewLazyDLL("user32.dll").NewProc("GetAsyncKeyState")
|
|
)
|
|
|
|
const (
|
|
hkeyCurrentUser = 0x80000001
|
|
keyQuerySet = 0x0003 // KEY_QUERY_VALUE | KEY_SET_VALUE
|
|
regSZ = 1
|
|
vkShift = 0x10
|
|
|
|
menuAutostartID = 31
|
|
runKeyPath = `Software\Microsoft\Windows\CurrentVersion\Run`
|
|
runValueName = "MirrorBar"
|
|
)
|
|
|
|
func openRunKey() (uintptr, bool) {
|
|
var key uintptr
|
|
path := u16(runKeyPath)
|
|
r, _, _ := pRegOpenKeyEx.Call(hkeyCurrentUser, uintptr(unsafe.Pointer(path)), 0, keyQuerySet,
|
|
uintptr(unsafe.Pointer(&key)))
|
|
return key, r == 0
|
|
}
|
|
|
|
// autostartCommand is what Windows runs at sign-in: this program, with the
|
|
// self-closing safety timer turned off.
|
|
func autostartCommand() string {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return `"` + exe + `" --forever`
|
|
}
|
|
|
|
func autostartValue() (string, bool) {
|
|
key, ok := openRunKey()
|
|
if !ok {
|
|
return "", false
|
|
}
|
|
defer pRegCloseKey.Call(key)
|
|
|
|
name := u16(runValueName)
|
|
var typ uint32
|
|
var buf [1024]uint16
|
|
size := uint32(len(buf) * 2)
|
|
r, _, _ := pRegQueryValueEx.Call(key, uintptr(unsafe.Pointer(name)), 0,
|
|
uintptr(unsafe.Pointer(&typ)), uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&size)))
|
|
if r != 0 || typ != regSZ || size > uint32(len(buf)*2) {
|
|
return "", false
|
|
}
|
|
return syscall.UTF16ToString(buf[:size/2]), true
|
|
}
|
|
|
|
func autostartEnabled() bool {
|
|
_, ok := autostartValue()
|
|
return ok
|
|
}
|
|
|
|
func setAutostart(on bool) {
|
|
key, ok := openRunKey()
|
|
if !ok {
|
|
return
|
|
}
|
|
defer pRegCloseKey.Call(key)
|
|
|
|
name := u16(runValueName)
|
|
if !on {
|
|
pRegDeleteValue.Call(key, uintptr(unsafe.Pointer(name)))
|
|
return
|
|
}
|
|
cmd := autostartCommand()
|
|
if cmd == "" {
|
|
return
|
|
}
|
|
data, err := syscall.UTF16FromString(cmd)
|
|
if err != nil {
|
|
return
|
|
}
|
|
pRegSetValueEx.Call(key, uintptr(unsafe.Pointer(name)), 0, regSZ,
|
|
uintptr(unsafe.Pointer(&data[0])), uintptr(len(data)*2))
|
|
}
|
|
|
|
// refreshAutostartPath keeps the sign-in entry pointing at the copy of the
|
|
// program that was run most recently, so a new version in a new folder takes
|
|
// over. It does nothing if start with Windows is off.
|
|
func refreshAutostartPath() {
|
|
cur, ok := autostartValue()
|
|
if !ok {
|
|
return
|
|
}
|
|
if want := autostartCommand(); want != "" && !strings.EqualFold(cur, want) {
|
|
setAutostart(true)
|
|
}
|
|
}
|
|
|
|
// shiftHeld is the escape hatch: hold Shift while the program starts and it
|
|
// exits at once without touching the taskbar.
|
|
func shiftHeld() bool {
|
|
r, _, _ := pGetAsyncKeyState.Call(vkShift)
|
|
return r&0x8000 != 0
|
|
}
|
|
|
|
// waitForTaskbar gives Explorer time to create the real taskbar at sign-in.
|
|
func waitForTaskbar(max time.Duration) uintptr {
|
|
deadline := time.Now().Add(max)
|
|
for {
|
|
if tb := findTaskbar(); tb != 0 {
|
|
return tb
|
|
}
|
|
if time.Now().After(deadline) {
|
|
return 0
|
|
}
|
|
time.Sleep(300 * time.Millisecond)
|
|
}
|
|
}
|