-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwhyawake.go
More file actions
161 lines (146 loc) · 3.57 KB
/
Copy pathwhyawake.go
File metadata and controls
161 lines (146 loc) · 3.57 KB
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
package main
import (
"fmt"
"syscall"
"github.com/caseymrm/go-clamshell"
"github.com/caseymrm/go-pmset"
"github.com/caseymrm/menuet/v2"
)
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.Regular{
Text: preventionRemaining(),
FontSize: 12,
})
items = append(items,
menuet.Regular{
Text: "Deactivate",
Clicked: cancelSleepPrevention,
},
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.Regular{
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.Regular{
Text: text,
FontSize: 12,
})
items = append(items, processes...)
} else if !preventingSleep() {
items = append(items, menuet.Regular{
Text: "Your Mac can sleep",
})
}
items = append(items, menuet.Separator{})
items = append(items, menuet.Regular{
Text: "Keep this Mac awake",
FontSize: 12,
})
items = append(items, sleepOptions()...)
items = append(items, menuet.Separator{})
items = append(items, menuet.Regular{
Text: leftClickDefaultLabel(),
FontSize: 12,
Children: leftClickDefaultMenu,
})
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 monitorClamshell(channel chan bool) {
for closed := range channel {
if closed && preventingSleep() && cafMinutes == lidMode {
cancelSleepPrevention()
}
}
}
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)
clamshellChannel := make(chan bool, 4)
clamshell.SubscribeClamshellChanges(clamshellChannel)
go monitorClamshell(clamshellChannel)
setMenuState()
app := menuet.App()
app.Name = "Why Awake?"
app.Label = "com.github.caseymrm.whyawake"
app.Children = menuItems
app.AutoUpdate.Version = "v0.10"
app.AutoUpdate.Repo = "caseymrm/whyawake"
refreshLeftClickHandler()
app.RunApplication()
}