-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhyawake.go
142 lines (129 loc) · 3.08 KB
/
whyawake.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
package main
import (
"fmt"
"syscall"
"github.com/caseymrm/go-pmset"
"github.com/caseymrm/menuet"
)
var sleepKeywords = map[string]bool{
"PreventUserIdleDisplaySleep": true,
"NoDisplaySleepAssertion": true,
}
var otherChangeKeywords = map[string]bool{
"PreventUserIdleSystemSleep": true,
}
var canSleepTitle = "💤"
var cantSleepTitle = "😳"
var caffeinatedTitle = "🤪"
func canSleep() bool {
asserts := pmset.GetAssertions()
for key, val := range asserts {
if val == 1 && sleepKeywords[key] {
return false
}
}
return true
}
func menuItems() []menuet.MenuItem {
items := make([]menuet.MenuItem, 0)
if preventingSleep() {
items = append(items, menuet.MenuItem{
Text: preventionRemaining(),
FontSize: 12,
})
items = append(items, menuet.MenuItem{
Text: "Deactivate",
Clicked: cancelSleepPrevention,
}, menuet.MenuItem{
Type: menuet.Separator,
})
}
processes := make([]menuet.MenuItem, 0)
pidAsserts := pmset.GetPIDAssertions()
for key := range sleepKeywords {
pids := pidAsserts[key]
for _, pid := range pids {
if pid.PID == cafPID {
continue
}
processes = append(processes, menuet.MenuItem{
Text: pid.Name,
Clicked: func() {
killProcess(pid.PID)
},
})
}
}
if len(processes) > 0 {
text := "1 process keeping your Mac awake"
if len(processes) > 1 {
text = fmt.Sprintf("%d processes keeping your Mac awake", len(processes))
}
items = append(items, menuet.MenuItem{
Text: text,
FontSize: 12,
})
items = append(items, processes...)
} else if !preventingSleep() {
items = append(items, menuet.MenuItem{
Text: "Your Mac can sleep",
})
}
items = append(items, menuet.MenuItem{
Type: menuet.Separator,
})
items = append(items, menuet.MenuItem{
Text: "Keep this Mac awake",
FontSize: 12,
})
items = append(items, sleepOptions()...)
return items
}
func setMenuState() {
image := "Red Eye.pdf"
if canSleep() {
image = "Eye.pdf"
}
if preventingSleep() {
image = "Awake Eye.pdf"
}
menuet.App().SetMenuState(&menuet.MenuState{
Image: image,
})
menuet.App().MenuChanged()
}
func monitorAssertionChanges(channel chan pmset.AssertionChange) {
for change := range channel {
if sleepKeywords[change.Type] || otherChangeKeywords[change.Type] {
setMenuState()
}
}
}
func killProcess(pid int) {
response := menuet.App().Alert(menuet.Alert{
MessageText: "Kill process?",
InformativeText: fmt.Sprintf("PID %d", pid),
Buttons: []string{"Kill", "Force Kill", "Cancel"},
})
switch response.Button {
case 0:
fmt.Printf("Killing pid %d\n", pid)
syscall.Kill(pid, syscall.SIGTERM)
case 1:
fmt.Printf("Killing -9 pid %d\n", pid)
syscall.Kill(pid, syscall.SIGKILL)
}
}
func main() {
assertionsChannel := make(chan pmset.AssertionChange)
pmset.SubscribeAssertionChanges(assertionsChannel)
go monitorAssertionChanges(assertionsChannel)
setMenuState()
app := menuet.App()
app.Name = "Why Awake?"
app.Label = "com.github.caseymrm.whyawake"
app.Children = menuItems
app.AutoUpdate.Version = "v0.5"
app.AutoUpdate.Repo = "caseymrm/whyawake"
app.RunApplication()
}