-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
245 lines (213 loc) · 5.62 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
_ "embed"
"fmt"
"io"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"github.com/nohajc/asahi-reboot-switcher/asahibless"
"github.com/nohajc/asahi-reboot-switcher/dialog"
"github.com/nohajc/asahi-reboot-switcher/util"
"github.com/nohajc/systray"
)
func setupAutostart(homeDir string) {
autostartDir := filepath.Join(homeDir, ".config", "autostart")
autostartFile := filepath.Join(autostartDir, "asahi-reboot-switcher.desktop")
// Check if the autostart file already exists
if _, err := os.Stat(autostartFile); os.IsNotExist(err) {
// Create the autostart directory if it doesn't exist
confirmed := dialog.
Question("Restart in macOS tray icon will be set to autostart on login.").
Title("Confirm autostart").
Run()
if !confirmed {
return
}
if err := os.MkdirAll(autostartDir, 0755); err != nil {
fmt.Fprintln(os.Stderr, "Failed to create autostart directory:", err)
return
}
// Open the source file for reading
srcFile, err := os.Open("/usr/share/applications/asahi-reboot-switcher.desktop")
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to open source file:", err)
return
}
defer srcFile.Close()
// Create the destination file for writing
dstFile, err := os.Create(autostartFile)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to create destination file:", err)
return
}
defer dstFile.Close()
// Copy the contents from the source file to the destination file
if _, err := io.Copy(dstFile, srcFile); err != nil {
fmt.Fprintln(os.Stderr, "Failed to copy file contents:", err)
return
}
fmt.Println("Autostart file copied successfully.")
}
}
func main() {
currUser, err := user.Current()
if err != nil {
log.Fatal(err)
}
_ = util.RequireCommand("pkexec")
if len(os.Args) > 1 {
callAsahiBless(os.Args[1:])
return
}
if currUser.Uid == "0" {
fmt.Fprintln(os.Stderr, "Should not run as root, exiting...")
os.Exit(1)
}
setupAutostart(currUser.HomeDir)
sctx := &SystrayContext{}
err = sctx.loadVolumes()
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
systray.Run(sctx.onReady, func() {})
}
func requestReboot() error {
if os.Getenv("XDG_CURRENT_DESKTOP") == "KDE" {
cmd := exec.Command("qdbus", "org.kde.ksmserver", "/KSMServer", "logout", "1", "1", "3")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
if os.Getenv("XDG_CURRENT_DESKTOP") == "GNOME" {
cmd := exec.Command("gnome-session-quit", "--reboot")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// fallback
cmd := exec.Command("pkexec", "reboot")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
var allowedBlessPaths = []string{"/usr/local/bin", "/usr/bin"}
var asahiBlessCmd = util.RequireCommand("asahi-bless", allowedBlessPaths...)
func callAsahiBless(args []string) {
{
cmd := exec.Command(asahiBlessCmd, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to set boot volume:", err)
}
}
}
//go:embed asahi-reboot-switcher.png
var appIcon []byte
type SystrayContext struct {
volumes []asahibless.Volume
volMenuItemGroup *systray.MenuItemRadioGroup
activeVolIdx int
}
func (sc *SystrayContext) loadVolumes() error {
volumes, err := asahibless.ListVolumes()
sc.volumes = volumes
if sc.volMenuItemGroup == nil {
return nil
}
// fmt.Printf("Volumes: %#v\n", volumes)
for _, v := range volumes {
i := v.Idx - 1
if v.Active {
sc.volMenuItemGroup.Check(i)
break
}
}
return err
}
func (sc *SystrayContext) onReady() {
systray.SetTemplateIcon(appIcon, appIcon)
// systray.SetTitle("Asahi Reboot Switcher")
systray.SetTooltip("Restart in macOS (tray icon)")
mReboot := systray.AddMenuItem("Restart in macOS...", "")
systray.AddSeparator()
mLabel := systray.AddMenuItem("Default startup disk:", "")
mLabel.Disable()
volMenuItemGroup := systray.AddMenuItemRadioGroup()
activeIdx := 0
for i, v := range sc.volumes {
if v.Active {
activeIdx = i
}
_ = volMenuItemGroup.AddItem(v.ShortName(), "")
}
volMenuItemGroup.Check(activeIdx)
go func() {
for volIdx := range volMenuItemGroup.ClickedCh {
if volIdx != sc.activeVolIdx {
confirmed := dialog.
Question("Change default startup disk to %s?", sc.volumes[volIdx].ShortName()).
Title("Confirm startup disk change").
OKButton("Change").
Run()
if confirmed {
asahibless.SetBoot(volIdx + 1)
sc.activeVolIdx = volIdx
}
}
sc.loadVolumes()
}
}()
sc.volMenuItemGroup = volMenuItemGroup
sc.activeVolIdx = activeIdx
systray.AddSeparator()
mQuitOrig := systray.AddMenuItem("Quit", "Quit application")
for {
select {
case <-mReboot.ClickedCh:
sc.rebootToMacOS()
case <-mQuitOrig.ClickedCh:
confirmed := dialog.
Question("Quit Restart in macOS tray icon?").
Title("Confirm quitting").
OKButton("Quit").
Run()
if confirmed {
fmt.Println("Quit")
systray.Quit()
}
}
}
}
func (sc *SystrayContext) isMacOSActive() bool {
for _, v := range sc.volumes {
if v.Active && strings.Contains(v.ShortName(), "Macintosh") {
return true
}
}
return false
}
func (sc *SystrayContext) rebootToMacOS() {
if !sc.isMacOSActive() { // TODO: also check if override not already set - isMacOSNext()
fmt.Println("macOS is not active, setting next boot override...")
err := asahibless.SetBootMacOS(true)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
} else {
fmt.Println("macOS is already active, rebooting...")
}
// time.Sleep(1 * time.Second)
{
err := requestReboot()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to reboot to macOS:", err)
}
}
}