-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
311 lines (276 loc) · 6.55 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/caseymrm/menuet"
"howett.net/plist"
)
const (
appVersion = "0.2.4"
boltIconOutline = "bolt.png"
boltIconFilled = "bolt-filled.png"
ALWAYS BatteryState = iota
NEVER
BATTERY_ONLY
POWER_ONLY
)
var (
hardwareUUID string //lint:ignore U1000 false positives
plistPath string
lowPowerMode = ""
inChan = make(chan BatteryState)
currentState BatteryState //lint:ignore U1000 false positives
currentIcon string
)
type BatteryState uint8
func (b BatteryState) String() string {
switch b {
case ALWAYS:
return "alwaysState"
case NEVER:
return "neverState"
case BATTERY_ONLY:
return "batteryOnlyState"
case POWER_ONLY:
return "powerOnlyState"
default:
return "Invalid state"
}
}
func getBatteryStates() []BatteryState {
return []BatteryState{ALWAYS, NEVER, BATTERY_ONLY, POWER_ONLY}
}
func getStateFromCondition(ac bool, battery bool) BatteryState {
states := map[[2]bool]BatteryState{
{true, true}: ALWAYS,
{false, false}: NEVER,
{false, true}: BATTERY_ONLY,
{true, false}: POWER_ONLY,
}
return BatteryState(states[[2]bool{ac, battery}])
}
func getHardwareUUID() (string, error) {
cmd := exec.Command("system_profiler", "SPHardwareDataType")
output, err := cmd.Output()
if err != nil {
return "", err
}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.Contains(line, "Hardware UUID:") {
uuid := strings.TrimSpace(strings.Split(line, ":")[1])
return uuid, nil
}
}
return "", errors.New("hardware UUID not found")
}
func setLowPowerMode(str string) error {
cmd := exec.Command(
"/usr/bin/osascript",
"-e",
fmt.Sprintf(
"do shell script \"%s\" with prompt \"Galvani is trying to update battery preferences\" with administrator privileges",
str,
),
)
err := cmd.Run()
return err
}
func getState() (BatteryState, error) {
cmd := exec.Command("defaults", "read", plistPath)
out, err := cmd.Output()
if err != nil {
return NEVER, err
}
var config map[string]interface{}
_, err = plist.Unmarshal(out, &config)
if err != nil {
return NEVER, err
}
// extract the LowPowerMode values for Battery and AC
batteryLowPowerModeStr := config["Battery Power"].(map[string]interface{})["LowPowerMode"].(string)
batteryLowPowerMode, err := strconv.ParseBool(batteryLowPowerModeStr)
if err != nil {
return NEVER, err
}
acLowPowerModeStr := config["AC Power"].(map[string]interface{})["LowPowerMode"].(string)
acLowPowerMode, err := strconv.ParseBool(acLowPowerModeStr)
if err != nil {
return NEVER, err
}
// Get the state for the current condition
state := getStateFromCondition(acLowPowerMode, batteryLowPowerMode)
return state, nil
}
func pollLowPowerState() {
// Set initial state
currentState, err := getState()
if err != nil {
log.Println(err)
os.Exit(1)
}
err = setMenu(currentState)
if err != nil {
log.Println(err)
os.Exit(1)
}
// Polling loop
tick := time.Tick(15 * time.Second)
for range tick {
state, err := getState()
if err != nil {
log.Println(err)
continue
}
// Only update if state has changed
if state != currentState {
inChan <- state
log.Printf("Updated state from %s to %s\n", currentState, state)
currentState = state
continue
}
icon, err := getIcon()
if err != nil {
log.Println(err)
continue
}
if icon != currentIcon {
setIcon(icon)
}
}
}
func getIcon() (string, error) {
cmd := exec.Command("pmset", "-g")
output, err := cmd.Output()
if err != nil {
log.Println(err)
return "", err
}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.Contains(line, "lowpowermode") {
fields := strings.Fields(line)
if fields[1] == "1" {
lowPowerMode = "ON"
return boltIconFilled, nil
}
lowPowerMode = "OFF"
return boltIconOutline, nil
}
}
return "", errors.New("low power mode not found")
}
func setMenuStatesFalse() {
for _, key := range getBatteryStates() {
menuet.Defaults().SetBoolean(key.String(), false)
}
}
func menuItems() []menuet.MenuItem {
alwaysState := menuet.Defaults().Boolean(ALWAYS.String())
neverState := menuet.Defaults().Boolean(NEVER.String())
batteryOnlyState := menuet.Defaults().Boolean(BATTERY_ONLY.String())
powerOnlyState := menuet.Defaults().Boolean(POWER_ONLY.String())
items := []menuet.MenuItem{}
items = append(items, menuet.MenuItem{
Text: fmt.Sprintf("Galvani (v%s)", appVersion),
FontSize: 12,
})
items = append(items, menuet.MenuItem{
Text: fmt.Sprintf("Low Power Mode %s", lowPowerMode),
FontSize: 12,
})
items = append(items, menuet.MenuItem{
Text: "💡 Always",
Clicked: func() {
err := setLowPowerMode("sudo pmset -a lowpowermode 1")
if err == nil {
inChan <- ALWAYS
}
},
State: alwaysState,
})
items = append(items, menuet.MenuItem{
Text: "🛑 Never",
Clicked: func() {
err := setLowPowerMode("sudo pmset -a lowpowermode 0")
if err == nil {
inChan <- NEVER
}
},
State: neverState,
})
items = append(items, menuet.MenuItem{
Text: "🔋 Only on Battery",
Clicked: func() {
err := setLowPowerMode("sudo pmset -c lowpowermode 0; sudo pmset -b lowpowermode 1")
if err == nil {
inChan <- BATTERY_ONLY
}
},
State: batteryOnlyState,
})
items = append(items, menuet.MenuItem{
Text: "🔌 Only on Power",
Clicked: func() {
err := setLowPowerMode("sudo pmset -c lowpowermode 1; sudo pmset -b lowpowermode 0")
if err == nil {
inChan <- POWER_ONLY
}
},
State: powerOnlyState,
})
return items
}
func setIcon(icon string) {
menuet.App().SetMenuState(&menuet.MenuState{
Image: icon,
})
menuet.App().MenuChanged()
currentIcon = icon
}
func setMenu(state BatteryState) error {
setMenuStatesFalse()
menuet.Defaults().SetBoolean(state.String(), true)
icon, err := getIcon()
if err != nil {
return err
}
setIcon(icon)
currentState = state
return nil
}
func menu() {
for state := range inChan {
err := setMenu(state)
if err != nil {
log.Println(err)
}
}
}
func main() {
go menu()
hardwareUUID, err := getHardwareUUID()
log.Printf("Hardware UUID is %s\n", hardwareUUID)
plistPath = fmt.Sprintf(
"/Library/Preferences/com.apple.PowerManagement.%s.plist",
hardwareUUID,
)
if err != nil {
log.Println(err)
os.Exit(1)
}
go pollLowPowerState()
app := menuet.App()
app.Name = "Galvani"
app.Label = "com.github.theden.galvani"
app.Children = menuItems
app.AutoUpdate.Version = appVersion
app.AutoUpdate.Repo = "TheDen/galvani"
app.RunApplication()
}