Blame


1 c440ada7 2024-01-04 benni /*
2 c440ada7 2024-01-04 benni * Copyright (c) 2024 Benjamin Stürz <benni@stuerz.xyz>
3 c440ada7 2024-01-04 benni *
4 c440ada7 2024-01-04 benni * Permission to use, copy, modify, and distribute this software for any
5 c440ada7 2024-01-04 benni * purpose with or without fee is hereby granted, provided that the above
6 c440ada7 2024-01-04 benni * copyright notice and this permission notice appear in all copies.
7 c440ada7 2024-01-04 benni *
8 c440ada7 2024-01-04 benni * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c440ada7 2024-01-04 benni * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c440ada7 2024-01-04 benni * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c440ada7 2024-01-04 benni * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c440ada7 2024-01-04 benni * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c440ada7 2024-01-04 benni * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c440ada7 2024-01-04 benni * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 c440ada7 2024-01-04 benni */
16 8f16cb3e 2024-01-04 benni #include <sys/types.h>
17 8f16cb3e 2024-01-04 benni #include <machine/apmvar.h>
18 8f16cb3e 2024-01-04 benni #include <sys/signal.h>
19 8f16cb3e 2024-01-04 benni #include <sys/sched.h>
20 8f16cb3e 2024-01-04 benni #include <sys/sysctl.h>
21 8f16cb3e 2024-01-04 benni #include <sys/ioctl.h>
22 8f16cb3e 2024-01-04 benni #include <sys/sensors.h>
23 c440ada7 2024-01-04 benni
24 8f16cb3e 2024-01-04 benni #include <stdbool.h>
25 8f16cb3e 2024-01-04 benni #include <ncurses.h>
26 8f16cb3e 2024-01-04 benni #include <locale.h>
27 8f16cb3e 2024-01-04 benni #include <limits.h>
28 8f16cb3e 2024-01-04 benni #include <unistd.h>
29 8f16cb3e 2024-01-04 benni #include <string.h>
30 8f16cb3e 2024-01-04 benni #include <stdint.h>
31 8f16cb3e 2024-01-04 benni #include <stdlib.h>
32 8f16cb3e 2024-01-04 benni #include <stdio.h>
33 8f16cb3e 2024-01-04 benni #include <fcntl.h>
34 8f16cb3e 2024-01-04 benni #include <time.h>
35 8f16cb3e 2024-01-04 benni #include <err.h>
36 8f16cb3e 2024-01-04 benni
37 8f16cb3e 2024-01-04 benni #define MAXSENSORS 16
38 8f16cb3e 2024-01-04 benni #define LOGLEN 512
39 8f16cb3e 2024-01-04 benni #define MIN(a, b) ((a) < (b) ? (a) : (b))
40 8f16cb3e 2024-01-04 benni
41 8f16cb3e 2024-01-04 benni static int power_sensors[MAXSENSORS];
42 8f16cb3e 2024-01-04 benni static int cputemp_sensor = -1;
43 8f16cb3e 2024-01-04 benni static int num_power_sensors = 0;
44 8f16cb3e 2024-01-04 benni static int entries = 0;
45 8f16cb3e 2024-01-04 benni
46 8f16cb3e 2024-01-04 benni enum {
47 8f16cb3e 2024-01-04 benni G_CPUUSAGE,
48 8f16cb3e 2024-01-04 benni G_CPUSPEED,
49 8f16cb3e 2024-01-04 benni G_CPUTEMP,
50 8f16cb3e 2024-01-04 benni G_BATTERY,
51 8f16cb3e 2024-01-04 benni G_POWER,
52 8f16cb3e 2024-01-04 benni NGRAPH,
53 8f16cb3e 2024-01-04 benni };
54 8f16cb3e 2024-01-04 benni
55 8f16cb3e 2024-01-04 benni struct graph {
56 8f16cb3e 2024-01-04 benni WINDOW *win;
57 8f16cb3e 2024-01-04 benni int log[LOGLEN];
58 8f16cb3e 2024-01-04 benni bool hide;
59 8f16cb3e 2024-01-04 benni };
60 8f16cb3e 2024-01-04 benni
61 8f16cb3e 2024-01-04 benni struct display {
62 8f16cb3e 2024-01-04 benni struct graph g[NGRAPH];
63 8f16cb3e 2024-01-04 benni
64 8f16cb3e 2024-01-04 benni struct cpu_usage *cpus;
65 8f16cb3e 2024-01-04 benni int fd_apm, ncpu;
66 8f16cb3e 2024-01-04 benni int ngraph;
67 8f16cb3e 2024-01-04 benni int width;
68 8f16cb3e 2024-01-04 benni int delay;
69 8f16cb3e 2024-01-04 benni bool running;
70 8f16cb3e 2024-01-04 benni };
71 8f16cb3e 2024-01-04 benni
72 8f16cb3e 2024-01-04 benni struct cpu_usage {
73 8f16cb3e 2024-01-04 benni struct cpustats new;
74 8f16cb3e 2024-01-04 benni struct cpustats old;
75 8f16cb3e 2024-01-04 benni struct cpustats diffs;
76 8f16cb3e 2024-01-04 benni };
77 8f16cb3e 2024-01-04 benni
78 8f16cb3e 2024-01-04 benni static void
79 8f16cb3e 2024-01-04 benni find_sensors (void)
80 8f16cb3e 2024-01-04 benni {
81 8f16cb3e 2024-01-04 benni for (int i = 0; i < MAXSENSORS; ++i) {
82 8f16cb3e 2024-01-04 benni const int mib[3] = { CTL_HW, HW_SENSORS, i };
83 8f16cb3e 2024-01-04 benni struct sensordev dev;
84 8f16cb3e 2024-01-04 benni size_t sdlen = sizeof dev;
85 8f16cb3e 2024-01-04 benni
86 8f16cb3e 2024-01-04 benni if (sysctl (mib, 3, &dev, &sdlen, NULL, 0) == -1)
87 8f16cb3e 2024-01-04 benni continue;
88 8f16cb3e 2024-01-04 benni
89 8f16cb3e 2024-01-04 benni if (dev.maxnumt[SENSOR_WATTS] > 0) {
90 8f16cb3e 2024-01-04 benni power_sensors[num_power_sensors++] = i;
91 8f16cb3e 2024-01-04 benni }
92 8f16cb3e 2024-01-04 benni
93 8f16cb3e 2024-01-04 benni if (dev.maxnumt[SENSOR_TEMP] > 0
94 8f16cb3e 2024-01-04 benni && memcmp (dev.xname, "cpu", 3) == 0
95 8f16cb3e 2024-01-04 benni && cputemp_sensor == -1) {
96 8f16cb3e 2024-01-04 benni cputemp_sensor = i;
97 8f16cb3e 2024-01-04 benni }
98 8f16cb3e 2024-01-04 benni }
99 8f16cb3e 2024-01-04 benni }
100 8f16cb3e 2024-01-04 benni
101 8f16cb3e 2024-01-04 benni static int
102 8f16cb3e 2024-01-04 benni num_cpu (void)
103 8f16cb3e 2024-01-04 benni {
104 8f16cb3e 2024-01-04 benni const int mib[] = { CTL_HW, HW_NCPU };
105 8f16cb3e 2024-01-04 benni int ncpu = 0;
106 8f16cb3e 2024-01-04 benni size_t size = sizeof ncpu;
107 8f16cb3e 2024-01-04 benni
108 8f16cb3e 2024-01-04 benni if (sysctl (mib, 2, &ncpu, &size, NULL, 0) < 0)
109 8f16cb3e 2024-01-04 benni return 1;
110 8f16cb3e 2024-01-04 benni
111 8f16cb3e 2024-01-04 benni return ncpu;
112 8f16cb3e 2024-01-04 benni }
113 8f16cb3e 2024-01-04 benni
114 8f16cb3e 2024-01-04 benni static int64_t
115 8f16cb3e 2024-01-04 benni percentages (struct cpustats *out, struct cpu_usage *cpu)
116 8f16cb3e 2024-01-04 benni {
117 8f16cb3e 2024-01-04 benni int64_t total_change = 0;
118 8f16cb3e 2024-01-04 benni
119 8f16cb3e 2024-01-04 benni for (int i = 0; i < 6; ++i) {
120 8f16cb3e 2024-01-04 benni int64_t change = cpu->new.cs_time[i] - cpu->old.cs_time[i];
121 8f16cb3e 2024-01-04 benni if (change < 0)
122 8f16cb3e 2024-01-04 benni change = INT64_MAX - cpu->old.cs_time[i] - cpu->new.cs_time[i];
123 8f16cb3e 2024-01-04 benni
124 8f16cb3e 2024-01-04 benni cpu->diffs.cs_time[i] = change;
125 8f16cb3e 2024-01-04 benni total_change += change;
126 8f16cb3e 2024-01-04 benni cpu->old.cs_time[i] = cpu->new.cs_time[i];
127 8f16cb3e 2024-01-04 benni }
128 8f16cb3e 2024-01-04 benni
129 8f16cb3e 2024-01-04 benni if (total_change == 0)
130 8f16cb3e 2024-01-04 benni total_change = 1;
131 8f16cb3e 2024-01-04 benni
132 8f16cb3e 2024-01-04 benni for (int i = 0; i < 6; ++i) {
133 8f16cb3e 2024-01-04 benni out->cs_time[i] = (cpu->diffs.cs_time[i] * 1000 + total_change / 2) / total_change;
134 8f16cb3e 2024-01-04 benni }
135 8f16cb3e 2024-01-04 benni
136 8f16cb3e 2024-01-04 benni return total_change;
137 8f16cb3e 2024-01-04 benni }
138 8f16cb3e 2024-01-04 benni
139 8f16cb3e 2024-01-04 benni static int
140 8f16cb3e 2024-01-04 benni cpuspeed (void)
141 8f16cb3e 2024-01-04 benni {
142 8f16cb3e 2024-01-04 benni const int mib[2] = { CTL_HW, HW_CPUSPEED };
143 8f16cb3e 2024-01-04 benni int speed;
144 8f16cb3e 2024-01-04 benni size_t len = sizeof speed;
145 8f16cb3e 2024-01-04 benni
146 8f16cb3e 2024-01-04 benni if (sysctl (mib, 2, &speed, &len, NULL, 0) == -1)
147 8f16cb3e 2024-01-04 benni return -1;
148 8f16cb3e 2024-01-04 benni
149 8f16cb3e 2024-01-04 benni return speed;
150 8f16cb3e 2024-01-04 benni }
151 8f16cb3e 2024-01-04 benni
152 8f16cb3e 2024-01-04 benni static int
153 8f16cb3e 2024-01-04 benni cputemp (void)
154 8f16cb3e 2024-01-04 benni {
155 8f16cb3e 2024-01-04 benni const int mib[5] = { CTL_HW, HW_SENSORS, cputemp_sensor, SENSOR_TEMP, 0 };
156 8f16cb3e 2024-01-04 benni struct sensor sensor;
157 8f16cb3e 2024-01-04 benni size_t len = sizeof sensor;
158 8f16cb3e 2024-01-04 benni int ret;
159 8f16cb3e 2024-01-04 benni
160 8f16cb3e 2024-01-04 benni ret = sysctl (mib, 5, &sensor, &len, NULL, 0);
161 8f16cb3e 2024-01-04 benni
162 8f16cb3e 2024-01-04 benni return (ret == -1 || sensor.status & SENSOR_FINVALID) ? -1 : sensor.value / 10000000;
163 8f16cb3e 2024-01-04 benni }
164 8f16cb3e 2024-01-04 benni
165 8f16cb3e 2024-01-04 benni static int
166 8f16cb3e 2024-01-04 benni cpu (int ncpu, struct cpu_usage *cpus)
167 8f16cb3e 2024-01-04 benni {
168 8f16cb3e 2024-01-04 benni uint64_t sum = 0;
169 8f16cb3e 2024-01-04 benni
170 8f16cb3e 2024-01-04 benni for (int i = 0; i < ncpu; ++i) {
171 8f16cb3e 2024-01-04 benni const int mib[] = { CTL_KERN, KERN_CPUSTATS, i };
172 8f16cb3e 2024-01-04 benni struct cpustats tmp;
173 8f16cb3e 2024-01-04 benni size_t size = sizeof tmp;
174 8f16cb3e 2024-01-04 benni
175 8f16cb3e 2024-01-04 benni if (sysctl (mib, 3, &cpus[i].new, &size, NULL, 0) < 0)
176 8f16cb3e 2024-01-04 benni continue;
177 8f16cb3e 2024-01-04 benni
178 8f16cb3e 2024-01-04 benni percentages (&tmp, &cpus[i]);
179 8f16cb3e 2024-01-04 benni sum += 1000 - tmp.cs_time[5];
180 8f16cb3e 2024-01-04 benni }
181 8f16cb3e 2024-01-04 benni
182 8f16cb3e 2024-01-04 benni return sum / ncpu / 10;
183 8f16cb3e 2024-01-04 benni }
184 8f16cb3e 2024-01-04 benni
185 8f16cb3e 2024-01-04 benni static int
186 8f16cb3e 2024-01-04 benni bat (int fd)
187 8f16cb3e 2024-01-04 benni {
188 8f16cb3e 2024-01-04 benni struct apm_power_info info;
189 8f16cb3e 2024-01-04 benni
190 8f16cb3e 2024-01-04 benni if (fd < 0 || ioctl (fd, APM_IOC_GETPOWER, &info) != 0)
191 8f16cb3e 2024-01-04 benni return 0;
192 8f16cb3e 2024-01-04 benni
193 8f16cb3e 2024-01-04 benni return info.battery_life;
194 8f16cb3e 2024-01-04 benni }
195 8f16cb3e 2024-01-04 benni
196 8f16cb3e 2024-01-04 benni static int
197 8f16cb3e 2024-01-04 benni power (void)
198 8f16cb3e 2024-01-04 benni {
199 8f16cb3e 2024-01-04 benni int64_t total = 0;
200 8f16cb3e 2024-01-04 benni
201 8f16cb3e 2024-01-04 benni for (int i = 0; i < num_power_sensors; ++i) {
202 8f16cb3e 2024-01-04 benni const int mib[5] = { CTL_HW, HW_SENSORS, power_sensors[i], SENSOR_WATTS, 0 };
203 8f16cb3e 2024-01-04 benni struct sensor sensor;
204 8f16cb3e 2024-01-04 benni size_t slen = sizeof sensor;
205 8f16cb3e 2024-01-04 benni
206 8f16cb3e 2024-01-04 benni if (sysctl (mib, 5, &sensor, &slen, NULL, 0) == -1)
207 8f16cb3e 2024-01-04 benni continue;
208 8f16cb3e 2024-01-04 benni
209 8f16cb3e 2024-01-04 benni if (sensor.flags & SENSOR_FINVALID)
210 8f16cb3e 2024-01-04 benni continue;
211 8f16cb3e 2024-01-04 benni
212 8f16cb3e 2024-01-04 benni total += sensor.value;
213 8f16cb3e 2024-01-04 benni }
214 8f16cb3e 2024-01-04 benni
215 8f16cb3e 2024-01-04 benni return total / 1000;
216 8f16cb3e 2024-01-04 benni }
217 8f16cb3e 2024-01-04 benni
218 8f16cb3e 2024-01-04 benni static void
219 8f16cb3e 2024-01-04 benni create_win (struct display *dpy)
220 8f16cb3e 2024-01-04 benni {
221 8f16cb3e 2024-01-04 benni int j = 0, w, h, wh;
222 8f16cb3e 2024-01-04 benni
223 8f16cb3e 2024-01-04 benni dpy->ngraph = 0;
224 8f16cb3e 2024-01-04 benni for (int i = 0; i < NGRAPH; ++i) {
225 8f16cb3e 2024-01-04 benni if (!dpy->g[i].hide)
226 8f16cb3e 2024-01-04 benni ++dpy->ngraph;
227 8f16cb3e 2024-01-04 benni }
228 8f16cb3e 2024-01-04 benni
229 8f16cb3e 2024-01-04 benni getmaxyx (stdscr, h, w);
230 8f16cb3e 2024-01-04 benni dpy->width = MIN (w - 2, LOGLEN);
231 8f16cb3e 2024-01-04 benni --h;
232 8f16cb3e 2024-01-04 benni wh = h / dpy->ngraph;
233 8f16cb3e 2024-01-04 benni
234 8f16cb3e 2024-01-04 benni if (entries > dpy->width) {
235 8f16cb3e 2024-01-04 benni const int diff = entries - dpy->width;
236 8f16cb3e 2024-01-04 benni for (int i = 0; i < NGRAPH; ++i) {
237 8f16cb3e 2024-01-04 benni struct graph *g = &dpy->g[i];
238 8f16cb3e 2024-01-04 benni memmove (g->log, g->log + diff, dpy->width * sizeof (g->log[0]));
239 8f16cb3e 2024-01-04 benni }
240 8f16cb3e 2024-01-04 benni entries = dpy->width;
241 8f16cb3e 2024-01-04 benni }
242 8f16cb3e 2024-01-04 benni
243 8f16cb3e 2024-01-04 benni for (int i = 0; i < NGRAPH; ++i) {
244 8f16cb3e 2024-01-04 benni struct graph *g = &dpy->g[i];
245 8f16cb3e 2024-01-04 benni if (g->win != NULL)
246 8f16cb3e 2024-01-04 benni delwin (g->win);
247 8f16cb3e 2024-01-04 benni g->win = g->hide ? NULL : newwin (wh, w, 1 + wh * j++, 0);
248 8f16cb3e 2024-01-04 benni }
249 8f16cb3e 2024-01-04 benni }
250 8f16cb3e 2024-01-04 benni
251 8f16cb3e 2024-01-04 benni static void
252 8f16cb3e 2024-01-04 benni draw_graph (struct graph *g, const char *fmt, int min, int max)
253 8f16cb3e 2024-01-04 benni {
254 8f16cb3e 2024-01-04 benni int w, h;
255 8f16cb3e 2024-01-04 benni
256 8f16cb3e 2024-01-04 benni if (g->hide || g->win == NULL)
257 8f16cb3e 2024-01-04 benni return;
258 8f16cb3e 2024-01-04 benni
259 8f16cb3e 2024-01-04 benni getmaxyx (g->win, h, w);
260 8f16cb3e 2024-01-04 benni w -= 2;
261 8f16cb3e 2024-01-04 benni h -= 3;
262 8f16cb3e 2024-01-04 benni
263 8f16cb3e 2024-01-04 benni wclear (g->win);
264 8f16cb3e 2024-01-04 benni
265 8f16cb3e 2024-01-04 benni for (int i = 0; i < entries; ++i) {
266 8f16cb3e 2024-01-04 benni int v = g->log[i];
267 8f16cb3e 2024-01-04 benni if (v < min)
268 8f16cb3e 2024-01-04 benni min = v;
269 8f16cb3e 2024-01-04 benni if (v > max)
270 8f16cb3e 2024-01-04 benni max = v;
271 8f16cb3e 2024-01-04 benni }
272 8f16cb3e 2024-01-04 benni
273 8f16cb3e 2024-01-04 benni if (min < max) {
274 8f16cb3e 2024-01-04 benni for (int i = 0; i < entries; ++i) {
275 8f16cb3e 2024-01-04 benni int v = g->log[i];
276 8f16cb3e 2024-01-04 benni int y = (int)((float)(v - min) / (max - min) * h);
277 8f16cb3e 2024-01-04 benni mvwaddch (g->win, h - y + 1, i + 1, 'X');
278 8f16cb3e 2024-01-04 benni }
279 8f16cb3e 2024-01-04 benni }
280 8f16cb3e 2024-01-04 benni
281 8f16cb3e 2024-01-04 benni box (g->win, 0, 0);
282 8f16cb3e 2024-01-04 benni
283 8f16cb3e 2024-01-04 benni mvwprintw (g->win, 0, 3, fmt, g->log[entries - 1]);
284 8f16cb3e 2024-01-04 benni wprintw (g->win, " (min: %d/max: %d)", min, max);
285 8f16cb3e 2024-01-04 benni
286 8f16cb3e 2024-01-04 benni }
287 8f16cb3e 2024-01-04 benni
288 8f16cb3e 2024-01-04 benni static void
289 8f16cb3e 2024-01-04 benni draw (struct display *dpy)
290 8f16cb3e 2024-01-04 benni {
291 8f16cb3e 2024-01-04 benni clear ();
292 8f16cb3e 2024-01-04 benni
293 8f16cb3e 2024-01-04 benni draw_graph (&dpy->g[G_CPUUSAGE], "CPU: %d%%", 0, 100);
294 8f16cb3e 2024-01-04 benni draw_graph (&dpy->g[G_CPUTEMP], "CPU.: %d C", 0, 100);
295 8f16cb3e 2024-01-04 benni draw_graph (&dpy->g[G_CPUSPEED], "CPU: %d MHz", INT_MAX, INT_MIN);
296 8f16cb3e 2024-01-04 benni draw_graph (&dpy->g[G_BATTERY], "BAT: %d%%", 0, 100);
297 8f16cb3e 2024-01-04 benni draw_graph (&dpy->g[G_POWER], "PWR: %dmW", INT_MAX, INT_MIN);
298 8f16cb3e 2024-01-04 benni
299 8f16cb3e 2024-01-04 benni mvprintw (0, 0, "Panels: ");
300 8f16cb3e 2024-01-04 benni for (int i = 0; i < NGRAPH; ++i) {
301 8f16cb3e 2024-01-04 benni addch (dpy->g[i].hide ? ' ' : ('1' + i));
302 8f16cb3e 2024-01-04 benni }
303 8f16cb3e 2024-01-04 benni
304 8f16cb3e 2024-01-04 benni printw (" %c delay=%d", dpy->running ? 'R' : 'S', dpy->delay);
305 8f16cb3e 2024-01-04 benni
306 8f16cb3e 2024-01-04 benni refresh ();
307 8f16cb3e 2024-01-04 benni for (int i = 0; i < NGRAPH; ++i)
308 8f16cb3e 2024-01-04 benni wnoutrefresh (dpy->g[i].win);
309 8f16cb3e 2024-01-04 benni doupdate ();
310 8f16cb3e 2024-01-04 benni move (0, 0);
311 8f16cb3e 2024-01-04 benni
312 8f16cb3e 2024-01-04 benni }
313 8f16cb3e 2024-01-04 benni
314 8f16cb3e 2024-01-04 benni static void
315 8f16cb3e 2024-01-04 benni update_graph (int w, struct graph *g, int value)
316 8f16cb3e 2024-01-04 benni {
317 8f16cb3e 2024-01-04 benni if (entries == w) {
318 8f16cb3e 2024-01-04 benni memmove (g->log, g->log + 1, (LOGLEN - 1) * sizeof (g->log[0]));
319 8f16cb3e 2024-01-04 benni g->log[entries - 1] = value;
320 8f16cb3e 2024-01-04 benni } else {
321 8f16cb3e 2024-01-04 benni g->log[entries] = value;
322 8f16cb3e 2024-01-04 benni }
323 8f16cb3e 2024-01-04 benni fprintf (stderr, "entries=%d, w=%d, value=%d\n", entries, w, value);
324 8f16cb3e 2024-01-04 benni }
325 8f16cb3e 2024-01-04 benni
326 8f16cb3e 2024-01-04 benni static void
327 8f16cb3e 2024-01-04 benni update (struct display *dpy)
328 8f16cb3e 2024-01-04 benni {
329 8f16cb3e 2024-01-04 benni int w = dpy->width;
330 8f16cb3e 2024-01-04 benni
331 8f16cb3e 2024-01-04 benni update_graph (w, &dpy->g[G_CPUUSAGE], cpu (dpy->ncpu, dpy->cpus));
332 8f16cb3e 2024-01-04 benni update_graph (w, &dpy->g[G_CPUTEMP], cputemp ());
333 8f16cb3e 2024-01-04 benni update_graph (w, &dpy->g[G_CPUSPEED], cpuspeed ());
334 8f16cb3e 2024-01-04 benni update_graph (w, &dpy->g[G_BATTERY], bat (dpy->fd_apm));
335 8f16cb3e 2024-01-04 benni update_graph (w, &dpy->g[G_POWER], power ());
336 8f16cb3e 2024-01-04 benni
337 8f16cb3e 2024-01-04 benni entries++;
338 8f16cb3e 2024-01-04 benni if (entries > w)
339 8f16cb3e 2024-01-04 benni entries = w;
340 8f16cb3e 2024-01-04 benni }
341 8f16cb3e 2024-01-04 benni
342 8f16cb3e 2024-01-04 benni int
343 8f16cb3e 2024-01-04 benni main (int argc, char *argv[])
344 8f16cb3e 2024-01-04 benni {
345 8f16cb3e 2024-01-04 benni struct display dpy;
346 8f16cb3e 2024-01-04 benni
347 c440ada7 2024-01-04 benni // pledge(2) doesn't work, because apmtop(1) needs sysctl(2).
348 c440ada7 2024-01-04 benni unveil ("/usr/share/terminfo", "r");
349 c440ada7 2024-01-04 benni unveil (NULL, NULL);
350 c440ada7 2024-01-04 benni
351 8f16cb3e 2024-01-04 benni memset (&dpy, 0, sizeof (dpy));
352 8f16cb3e 2024-01-04 benni dpy.ncpu = num_cpu ();
353 8f16cb3e 2024-01-04 benni dpy.cpus = calloc (dpy.ncpu, sizeof (struct cpu_usage));
354 8f16cb3e 2024-01-04 benni dpy.fd_apm = open ("/dev/apm", O_RDONLY);
355 8f16cb3e 2024-01-04 benni dpy.delay = 10;
356 8f16cb3e 2024-01-04 benni dpy.running = true;
357 8f16cb3e 2024-01-04 benni find_sensors ();
358 8f16cb3e 2024-01-04 benni
359 8f16cb3e 2024-01-04 benni setlocale (LC_ALL, "");
360 8f16cb3e 2024-01-04 benni initscr ();
361 8f16cb3e 2024-01-04 benni cbreak ();
362 8f16cb3e 2024-01-04 benni noecho ();
363 8f16cb3e 2024-01-04 benni intrflush (stdscr, FALSE);
364 8f16cb3e 2024-01-04 benni keypad (stdscr, TRUE);
365 8f16cb3e 2024-01-04 benni halfdelay (dpy.delay);
366 8f16cb3e 2024-01-04 benni
367 8f16cb3e 2024-01-04 benni entries = 0;
368 8f16cb3e 2024-01-04 benni create_win (&dpy);
369 8f16cb3e 2024-01-04 benni update (&dpy);
370 8f16cb3e 2024-01-04 benni draw (&dpy);
371 8f16cb3e 2024-01-04 benni
372 8f16cb3e 2024-01-04 benni while (1) {
373 8f16cb3e 2024-01-04 benni struct graph *g;
374 8f16cb3e 2024-01-04 benni int ch;
375 8f16cb3e 2024-01-04 benni
376 8f16cb3e 2024-01-04 benni ch = getch ();
377 8f16cb3e 2024-01-04 benni
378 8f16cb3e 2024-01-04 benni switch (ch) {
379 8f16cb3e 2024-01-04 benni case 'q':
380 8f16cb3e 2024-01-04 benni goto done;
381 8f16cb3e 2024-01-04 benni case KEY_RESIZE:
382 8f16cb3e 2024-01-04 benni create_win (&dpy);
383 8f16cb3e 2024-01-04 benni break;
384 8f16cb3e 2024-01-04 benni case '1':
385 8f16cb3e 2024-01-04 benni case '2':
386 8f16cb3e 2024-01-04 benni case '3':
387 8f16cb3e 2024-01-04 benni case '4':
388 8f16cb3e 2024-01-04 benni case '5':
389 8f16cb3e 2024-01-04 benni g = &dpy.g[ch - '1'];
390 8f16cb3e 2024-01-04 benni if (dpy.ngraph == 1 && !g->hide)
391 8f16cb3e 2024-01-04 benni break;
392 8f16cb3e 2024-01-04 benni g->hide = !g->hide;
393 8f16cb3e 2024-01-04 benni create_win (&dpy);
394 8f16cb3e 2024-01-04 benni break;
395 8f16cb3e 2024-01-04 benni case '+':
396 8f16cb3e 2024-01-04 benni ++dpy.delay;
397 8f16cb3e 2024-01-04 benni halfdelay (dpy.delay);
398 8f16cb3e 2024-01-04 benni break;
399 8f16cb3e 2024-01-04 benni case '-':
400 8f16cb3e 2024-01-04 benni if (dpy.delay > 1)
401 8f16cb3e 2024-01-04 benni --dpy.delay;
402 8f16cb3e 2024-01-04 benni halfdelay (dpy.delay);
403 8f16cb3e 2024-01-04 benni break;
404 8f16cb3e 2024-01-04 benni case ' ':
405 8f16cb3e 2024-01-04 benni dpy.running = !dpy.running;
406 8f16cb3e 2024-01-04 benni break;
407 8f16cb3e 2024-01-04 benni case ERR:
408 8f16cb3e 2024-01-04 benni if (dpy.running)
409 8f16cb3e 2024-01-04 benni update (&dpy);
410 8f16cb3e 2024-01-04 benni break;
411 8f16cb3e 2024-01-04 benni }
412 8f16cb3e 2024-01-04 benni
413 8f16cb3e 2024-01-04 benni draw (&dpy);
414 8f16cb3e 2024-01-04 benni }
415 8f16cb3e 2024-01-04 benni
416 8f16cb3e 2024-01-04 benni done:
417 8f16cb3e 2024-01-04 benni endwin ();
418 8f16cb3e 2024-01-04 benni return 0;
419 8f16cb3e 2024-01-04 benni }