-
Notifications
You must be signed in to change notification settings - Fork 2
/
notafan.go
170 lines (158 loc) · 3.49 KB
/
notafan.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
package main
import (
"fmt"
"time"
"github.com/caseymrm/go-pmset"
smc "github.com/caseymrm/go-smc"
"github.com/caseymrm/menuet"
)
func setMenu() {
celsius := menuet.Defaults().Boolean("celsius")
text := fmt.Sprintf("%.01f°C", lastTemp)
if !celsius {
text = fmt.Sprintf("%.01f°F", lastTemp*1.8+32)
}
average := 0
for _, speed := range lastSpeeds {
average += speed
}
averageText := ""
if len(lastSpeeds) > 0 {
average = average / len(lastSpeeds)
averageText = fmt.Sprintf(" %d", average)
}
if lastCPULimit != 100 {
text += fmt.Sprintf(" %d%%", lastCPULimit)
}
menuet.App().SetMenuState(&menuet.MenuState{
Title: text + averageText,
})
menuet.App().MenuChanged()
}
func menuItems() []menuet.MenuItem {
celsius := menuet.Defaults().Boolean("celsius")
temperatureText := fmt.Sprintf("%.01f°C", lastTemp)
if !celsius {
temperatureText = fmt.Sprintf("%.01f°F", lastTemp*1.8+32)
}
throttleText := "Not throttled"
if lastCPULimit != 100 {
throttleText = fmt.Sprintf("Throttled to %d%%", lastCPULimit)
}
items := []menuet.MenuItem{
{
Text: "CPU",
FontSize: 9,
},
{
Text: temperatureText,
},
{
Text: throttleText,
},
{
Type: menuet.Separator,
},
{
Text: "Fan speeds",
FontSize: 9,
},
}
for _, speed := range lastSpeeds {
items = append(items, menuet.MenuItem{
Text: fmt.Sprintf("%d RPM", speed),
})
}
if len(lastSpeeds) == 0 {
items = append(items, menuet.MenuItem{
Text: fmt.Sprintf("No fans!"),
})
}
items = append(items, menuet.MenuItem{
Type: menuet.Separator,
})
items = append(items, menuet.MenuItem{
Text: "Units",
Children: func() []menuet.MenuItem {
return []menuet.MenuItem{
{
Text: "Fahrenheit",
Clicked: func() {
menuet.Defaults().SetBoolean("celsius", false)
setMenu()
},
State: !celsius,
},
{
Text: "Celsius",
Clicked: func() {
menuet.Defaults().SetBoolean("celsius", true)
setMenu()
},
State: celsius,
},
}
},
})
return items
}
var lastTemp float64
var lastSpeeds []int
func readTempAndFanSpeeds() (float64, []int) {
smc.OpenSMC()
temp := smc.ReadTemperature()
speeds := smc.ReadFanSpeeds()
smc.CloseSMC()
return temp, speeds
}
func watchCPU() {
lastTemp, lastSpeeds = readTempAndFanSpeeds()
ticker := time.NewTicker(3 * time.Second)
for ; true; <-ticker.C {
lastTemp, lastSpeeds = readTempAndFanSpeeds()
setMenu()
}
}
var lastCPULimit int
func cpuSpeedLimit() int {
thermal := pmset.GetThermalConditions()
return thermal["CPU_Speed_Limit"]
}
func monitorThermalChanges(channel chan bool) {
lastCPULimit = cpuSpeedLimit()
lastNotification := time.Now()
for range channel {
newLimit := cpuSpeedLimit()
if newLimit == lastCPULimit {
continue
}
if lastNotification.Add(time.Second).After(time.Now()) {
continue
}
if newLimit == 100 {
menuet.App().Notification(menuet.Notification{
Title: "CPU no longer throttled",
})
} else {
menuet.App().Notification(menuet.Notification{
Title: fmt.Sprintf("CPU being throttled to %d%%", newLimit),
})
}
lastCPULimit = newLimit
lastNotification = time.Now()
setMenu()
}
}
func main() {
thermalChannel := make(chan bool)
pmset.SubscribeThermalChanges(thermalChannel)
go monitorThermalChanges(thermalChannel)
go watchCPU()
app := menuet.App()
app.Name = "Not a Fan"
app.Label = "com.github.caseymrm.notafan"
app.Children = menuItems
app.AutoUpdate.Version = "v0.1"
app.AutoUpdate.Repo = "caseymrm/notafan"
app.RunApplication()
}