|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +package autostart |
| 4 | + |
| 5 | +import ( |
| 6 | + "errors" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "golang.org/x/sys/windows/registry" |
| 12 | +) |
| 13 | + |
| 14 | +const supported = true |
| 15 | + |
| 16 | +// runKeyPath is the per-user startup list. HKCU rather than HKLM on purpose: |
| 17 | +// TinyPlay installs per-user (see windows/TinyPlay.iss, PrivilegesRequired=lowest), |
| 18 | +// so writing a machine-wide entry would both need elevation and start the app |
| 19 | +// for accounts that never installed it. |
| 20 | +const runKeyPath = `Software\Microsoft\Windows\CurrentVersion\Run` |
| 21 | + |
| 22 | +// runValueName is the value under that key. It doubles as the label Windows |
| 23 | +// shows in Task Manager's Startup tab, so it stays the product name. A var, |
| 24 | +// not a const, so the test can use a throwaway name instead of touching the |
| 25 | +// real user's startup list. |
| 26 | +var runValueName = "TinyPlay" |
| 27 | + |
| 28 | +// autostartFlag marks a login-triggered launch so the shell can stay quietly |
| 29 | +// in the tray instead of opening the QR window on every sign-in. See |
| 30 | +// launchedAtLogin in cmd/tvremote/shell_windows.go. |
| 31 | +const autostartFlag = "--autostart" |
| 32 | + |
| 33 | +// startupCommand is what gets written to the Run value: the absolute path to |
| 34 | +// this executable, quoted (install paths contain spaces — "Program Files", |
| 35 | +// and per-user installs land under a folder named after the account), plus |
| 36 | +// the flag that tells the shell it was started by the system. |
| 37 | +func startupCommand() (string, error) { |
| 38 | + exe, err := os.Executable() |
| 39 | + if err != nil { |
| 40 | + return "", err |
| 41 | + } |
| 42 | + if resolved, err := filepath.EvalSymlinks(exe); err == nil { |
| 43 | + exe = resolved |
| 44 | + } |
| 45 | + exe, err = filepath.Abs(exe) |
| 46 | + if err != nil { |
| 47 | + return "", err |
| 48 | + } |
| 49 | + return `"` + exe + `" ` + autostartFlag, nil |
| 50 | +} |
| 51 | + |
| 52 | +// readValue returns the current Run value, or "" when autostart is off. A |
| 53 | +// missing key and a missing value are both simply "off" — the Run key does not |
| 54 | +// exist at all on a fresh account until something registers. |
| 55 | +func readValue() (string, error) { |
| 56 | + k, err := registry.OpenKey(registry.CURRENT_USER, runKeyPath, registry.QUERY_VALUE) |
| 57 | + if err != nil { |
| 58 | + if errors.Is(err, registry.ErrNotExist) { |
| 59 | + return "", nil |
| 60 | + } |
| 61 | + return "", err |
| 62 | + } |
| 63 | + defer k.Close() |
| 64 | + value, _, err := k.GetStringValue(runValueName) |
| 65 | + if err != nil { |
| 66 | + if errors.Is(err, registry.ErrNotExist) { |
| 67 | + return "", nil |
| 68 | + } |
| 69 | + return "", err |
| 70 | + } |
| 71 | + return value, nil |
| 72 | +} |
| 73 | + |
| 74 | +func enabled() (bool, error) { |
| 75 | + value, err := readValue() |
| 76 | + if err != nil { |
| 77 | + return false, err |
| 78 | + } |
| 79 | + return strings.TrimSpace(value) != "", nil |
| 80 | +} |
| 81 | + |
| 82 | +func setEnabled(on bool) error { |
| 83 | + k, _, err := registry.CreateKey(registry.CURRENT_USER, runKeyPath, registry.SET_VALUE) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + defer k.Close() |
| 88 | + if !on { |
| 89 | + if err := k.DeleteValue(runValueName); err != nil && !errors.Is(err, registry.ErrNotExist) { |
| 90 | + return err |
| 91 | + } |
| 92 | + return nil |
| 93 | + } |
| 94 | + command, err := startupCommand() |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + return k.SetStringValue(runValueName, command) |
| 99 | +} |
| 100 | + |
| 101 | +func syncPath() error { |
| 102 | + current, err := readValue() |
| 103 | + if err != nil || strings.TrimSpace(current) == "" { |
| 104 | + return err |
| 105 | + } |
| 106 | + want, err := startupCommand() |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + if current == want { |
| 111 | + return nil |
| 112 | + } |
| 113 | + return setEnabled(true) |
| 114 | +} |
0 commit comments