Xinqi Bao's Git

lol, battery_perc() is even simpler, fuck this shit :D
[slstatus.git] / slstatus.c
1 /* See LICENSE file for copyright and license details. */
2
3 #include <alsa/asoundlib.h>
4 #include <err.h>
5 #include <fcntl.h>
6 #include <ifaddrs.h>
7 #include <limits.h>
8 #include <linux/wireless.h>
9 #include <netdb.h>
10 #include <pwd.h>
11 #include <signal.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/ioctl.h>
17 #include <sys/stat.h>
18 #include <sys/statvfs.h>
19 #include <sys/socket.h>
20 #include <sys/sysinfo.h>
21 #include <sys/types.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <X11/Xlib.h>
25
26 #undef strlcat
27 #undef strlcpy
28
29 #include "strlcat.h"
30 #include "strlcpy.h"
31 #include "concat.h"
32
33 char concat[];
34
35 struct arg {
36 char *(*func)();
37 const char *format;
38 const char *args;
39 };
40
41 static char *smprintf(const char *, ...);
42 static char *battery_perc(const char *);
43 static char *cpu_perc(void);
44 static char *datetime(const char *);
45 static char *disk_free(const char *);
46 static char *disk_perc(const char *);
47 static char *disk_total(const char *);
48 static char *disk_used(const char *);
49 static char *entropy(void);
50 static char *gid(void);
51 static char *hostname(void);
52 static char *ip(const char *);
53 static char *load_avg(void);
54 static char *ram_free(void);
55 static char *ram_perc(void);
56 static char *ram_used(void);
57 static char *ram_total(void);
58 static char *run_command(const char *);
59 static char *temp(const char *);
60 static char *uid(void);
61 static char *uptime(void);
62 static char *username(void);
63 static char *vol_perc(const char *);
64 static char *wifi_perc(const char *);
65 static char *wifi_essid(const char *);
66 static void sighandler(const int);
67
68 static unsigned short int delay;
69 static Display *dpy;
70 static int done = 0;
71
72 #include "config.h"
73
74 static char *
75 smprintf(const char *fmt, ...)
76 {
77 va_list ap;
78 char *ret;
79 int len;
80
81 va_start(ap, fmt);
82 len = vsnprintf(NULL, 0, fmt, ap);
83 va_end(ap);
84
85 ret = malloc(++len);
86 if (ret == NULL) {
87 perror("malloc");
88 exit(1);
89 }
90
91 va_start(ap, fmt);
92 vsnprintf(ret, len, fmt, ap);
93 va_end(ap);
94
95 return ret;
96 }
97
98 static char *
99 battery_perc(const char *battery)
100 {
101 int perc;
102 FILE *fp;
103
104 ccat(3, "/sys/class/power_supply/", battery, "/capacity");
105 fp = fopen(concat, "r");
106 if (fp == NULL) {
107 warn("Error opening battery file: %s", concat);
108 return smprintf(UNKNOWN_STR);
109 }
110 fscanf(fp, "%i", &perc);
111 fclose(fp);
112
113 return smprintf("%d%%", perc);
114 }
115
116 static char *
117 cpu_perc(void)
118 {
119 int perc;
120 long double a[4], b[4];
121 FILE *fp = fopen("/proc/stat","r");
122
123 if (fp == NULL) {
124 warn("Error opening stat file");
125 return smprintf(UNKNOWN_STR);
126 }
127
128 fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
129 fclose(fp);
130
131 delay = (UPDATE_INTERVAL - (UPDATE_INTERVAL - 1));
132 sleep(delay);
133
134 fp = fopen("/proc/stat","r");
135 if (fp == NULL) {
136 warn("Error opening stat file");
137 return smprintf(UNKNOWN_STR);
138 }
139
140 fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
141 fclose(fp);
142 perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
143
144 return smprintf("%d%%", perc);
145 }
146
147 static char *
148 datetime(const char *timeformat)
149 {
150 time_t t;
151 char timestr[80];
152
153 t = time(NULL);
154 if (strftime(timestr, sizeof(timestr), timeformat, localtime(&t)) == 0)
155 return smprintf(UNKNOWN_STR);
156
157 return smprintf("%s", timestr);
158 }
159
160 static char *
161 disk_free(const char *mountpoint)
162 {
163 struct statvfs fs;
164
165 if (statvfs(mountpoint, &fs) < 0) {
166 warn("Could not get filesystem info");
167 return smprintf(UNKNOWN_STR);
168 }
169
170 return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
171 }
172
173 static char *
174 disk_perc(const char *mountpoint)
175 {
176 int perc = 0;
177 struct statvfs fs;
178
179 if (statvfs(mountpoint, &fs) < 0) {
180 warn("Could not get filesystem info");
181 return smprintf(UNKNOWN_STR);
182 }
183
184 perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
185
186 return smprintf("%d%%", perc);
187 }
188
189 static char *
190 disk_total(const char *mountpoint)
191 {
192 struct statvfs fs;
193
194 if (statvfs(mountpoint, &fs) < 0) {
195 warn("Could not get filesystem info");
196 return smprintf(UNKNOWN_STR);
197 }
198
199 return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
200 }
201
202 static char *
203 disk_used(const char *mountpoint)
204 {
205 struct statvfs fs;
206
207 if (statvfs(mountpoint, &fs) < 0) {
208 warn("Could not get filesystem info");
209 return smprintf(UNKNOWN_STR);
210 }
211
212 return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
213 }
214
215 static char *
216 entropy(void)
217 {
218 int entropy = 0;
219 FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r");
220
221 if (fp == NULL) {
222 warn("Could not open entropy file");
223 return smprintf(UNKNOWN_STR);
224 }
225
226 fscanf(fp, "%d", &entropy);
227 fclose(fp);
228
229 return smprintf("%d", entropy);
230 }
231
232 static char *
233 gid(void)
234 {
235 return smprintf("%d", getgid());
236 }
237
238 static char *
239 hostname(void)
240 {
241 char hostname[HOST_NAME_MAX];
242 FILE *fp = fopen("/proc/sys/kernel/hostname", "r");
243
244 if (fp == NULL) {
245 warn("Could not open hostname file");
246 return smprintf(UNKNOWN_STR);
247 }
248
249 fgets(hostname, sizeof(hostname), fp);
250 /* FIXME: needs improvement */
251 memset(&hostname[strlen(hostname)-1], '\0',
252 sizeof(hostname) - strlen(hostname));
253 fclose(fp);
254
255 return smprintf("%s", hostname);
256 }
257
258 static char *
259 ip(const char *interface)
260 {
261 struct ifaddrs *ifaddr, *ifa;
262 int s;
263 char host[NI_MAXHOST];
264
265 if (getifaddrs(&ifaddr) == -1) {
266 warn("Error getting IP address");
267 return smprintf(UNKNOWN_STR);
268 }
269
270 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
271 if (ifa->ifa_addr == NULL)
272 continue;
273
274 s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST,
275 NULL, 0, NI_NUMERICHOST);
276
277 if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) {
278 if (s != 0) {
279 warnx("Error getting IP address.");
280 return smprintf(UNKNOWN_STR);
281 }
282 return smprintf("%s", host);
283 }
284 }
285
286 freeifaddrs(ifaddr);
287
288 return smprintf(UNKNOWN_STR);
289 }
290
291 static char *
292 load_avg(void)
293 {
294 double avgs[3];
295
296 if (getloadavg(avgs, 3) < 0) {
297 warnx("Error getting load avg.");
298 return smprintf(UNKNOWN_STR);
299 }
300
301 return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
302 }
303
304 static char *
305 ram_free(void)
306 {
307 long free;
308 FILE *fp = fopen("/proc/meminfo", "r");
309
310 if (fp == NULL) {
311 warn("Error opening meminfo file");
312 return smprintf(UNKNOWN_STR);
313 }
314
315 fscanf(fp, "MemFree: %ld kB\n", &free);
316 fclose(fp);
317
318 return smprintf("%f", (float)free / 1024 / 1024);
319 }
320
321 static char *
322 ram_perc(void)
323 {
324 int perc;
325 long total, free, buffers, cached;
326 FILE *fp = fopen("/proc/meminfo", "r");
327
328 if (fp == NULL) {
329 warn("Error opening meminfo file");
330 return smprintf(UNKNOWN_STR);
331 }
332
333 fscanf(fp, "MemTotal: %ld kB\n", &total);
334 fscanf(fp, "MemFree: %ld kB\n", &free);
335 fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
336 fscanf(fp, "Cached: %ld kB\n", &cached);
337
338 fclose(fp);
339 perc = 100 * ((total - free) - (buffers + cached)) / total;
340
341 return smprintf("%d%%", perc);
342 }
343
344 static char *
345 ram_total(void)
346 {
347 long total;
348 FILE *fp = fopen("/proc/meminfo", "r");
349
350 if (fp == NULL) {
351 warn("Error opening meminfo file");
352 return smprintf(UNKNOWN_STR);
353 }
354
355 fscanf(fp, "MemTotal: %ld kB\n", &total);
356 fclose(fp);
357
358 return smprintf("%f", (float)total / 1024 / 1024);
359 }
360
361 static char *
362 ram_used(void)
363 {
364 long free, total, buffers, cached, used;
365 FILE *fp = fopen("/proc/meminfo", "r");
366
367 if (fp == NULL) {
368 warn("Error opening meminfo file");
369 return smprintf(UNKNOWN_STR);
370 }
371
372 fscanf(fp, "MemTotal: %ld kB\n", &total);
373 fscanf(fp, "MemFree: %ld kB\n", &free);
374 fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
375 fscanf(fp, "Cached: %ld kB\n", &cached);
376
377 fclose(fp);
378 used = total - free - buffers - cached;
379
380 return smprintf("%f", (float)used / 1024 / 1024);
381 }
382
383 static char *
384 run_command(const char* command)
385 {
386 int good;
387 FILE *fp = popen(command, "r");
388 char buffer[64] = "";
389
390 if (fp == NULL) {
391 warn("Could not get command output for: %s", command);
392 return smprintf(UNKNOWN_STR);
393 }
394
395 fgets(buffer, sizeof(buffer)-1, fp);
396 pclose(fp);
397 for (int i = 0 ; i != sizeof(buffer); i++) {
398 if (buffer[i] == '\0') {
399 good = 1;
400 break;
401 }
402 }
403 if (good)
404 buffer[strlen(buffer)-1] = '\0';
405
406 return smprintf("%s", buffer);
407 }
408
409 static char *
410 temp(const char *file)
411 {
412 int temperature;
413 FILE *fp = fopen(file, "r");
414
415 if (fp == NULL) {
416 warn("Could not open temperature file");
417 return smprintf(UNKNOWN_STR);
418 }
419
420 fscanf(fp, "%d", &temperature);
421 fclose(fp);
422
423 return smprintf("%d°C", temperature / 1000);
424 }
425
426 static char *
427 uptime(void)
428 {
429 struct sysinfo info;
430 int hours = 0;
431 int minutes = 0;
432
433 sysinfo(&info);
434 hours = info.uptime / 3600;
435 minutes = (info.uptime - hours * 3600 ) / 60;
436
437 return smprintf("%dh %dm", hours, minutes);
438 }
439
440 static char *
441 username(void)
442 {
443 uid_t uid = geteuid();
444 struct passwd *pw = getpwuid(uid);
445
446 if (pw == NULL) {
447 warn("Could not get username");
448 return smprintf(UNKNOWN_STR);
449 }
450
451 return smprintf("%s", pw->pw_name);
452 }
453
454 static char *
455 uid(void)
456 {
457 return smprintf("%d", geteuid());
458 }
459
460
461 static char *
462 vol_perc(const char *snd_card)
463 { /* FIX THIS SHIT! */
464 long int vol, max, min;
465 snd_mixer_t *handle;
466 snd_mixer_elem_t *elem;
467 snd_mixer_selem_id_t *s_elem;
468
469 snd_mixer_open(&handle, 0);
470 snd_mixer_attach(handle, snd_card);
471 snd_mixer_selem_register(handle, NULL, NULL);
472 snd_mixer_load(handle);
473 snd_mixer_selem_id_malloc(&s_elem);
474 snd_mixer_selem_id_set_name(s_elem, ALSA_CHANNEL);
475 elem = snd_mixer_find_selem(handle, s_elem);
476
477 if (elem == NULL) {
478 snd_mixer_selem_id_free(s_elem);
479 snd_mixer_close(handle);
480 warn("error: ALSA");
481 return smprintf(UNKNOWN_STR);
482 }
483
484 snd_mixer_handle_events(handle);
485 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
486 snd_mixer_selem_get_playback_volume(elem, 0, &vol);
487
488 snd_mixer_selem_id_free(s_elem);
489 snd_mixer_close(handle);
490
491 return smprintf("%d", ((uint_fast16_t)(vol * 100) / max));
492 }
493
494 static char *
495 wifi_perc(const char *wificard)
496 {
497 int strength;
498 char buf[255];
499 char *datastart;
500 char status[5];
501 FILE *fp;
502
503 ccat(3, "/sys/class/net/", wificard, "/operstate");
504
505 fp = fopen(concat, "r");
506
507 if (fp == NULL) {
508 warn("Error opening wifi operstate file");
509 return smprintf(UNKNOWN_STR);
510 }
511
512 fgets(status, 5, fp);
513 fclose(fp);
514 if(strcmp(status, "up\n") != 0)
515 return smprintf(UNKNOWN_STR);
516
517 fp = fopen("/proc/net/wireless", "r");
518 if (fp == NULL) {
519 warn("Error opening wireless file");
520 return smprintf(UNKNOWN_STR);
521 }
522
523 ccat(2, wificard, ":");
524 fgets(buf, sizeof(buf), fp);
525 fgets(buf, sizeof(buf), fp);
526 fgets(buf, sizeof(buf), fp);
527
528 datastart = strstr(buf, concat);
529 if (datastart != NULL) {
530 datastart = strstr(buf, ":");
531 sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
532 }
533
534 fclose(fp);
535
536 return smprintf("%d%%", strength);
537 }
538
539 static char *
540 wifi_essid(const char *wificard)
541 {
542 char id[IW_ESSID_MAX_SIZE+1];
543 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
544 struct iwreq wreq;
545
546 memset(&wreq, 0, sizeof(struct iwreq));
547 wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
548 sprintf(wreq.ifr_name, wificard);
549 if (sockfd == -1) {
550 warn("Cannot open socket for interface: %s", wificard);
551 return smprintf(UNKNOWN_STR);
552 }
553 wreq.u.essid.pointer = id;
554 if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
555 warn("Get ESSID ioctl failed for interface %s", wificard);
556 return smprintf(UNKNOWN_STR);
557 }
558
559 close(sockfd);
560
561 if (strcmp((char *)wreq.u.essid.pointer, "") == 0)
562 return smprintf(UNKNOWN_STR);
563 else
564 return smprintf("%s", (char *)wreq.u.essid.pointer);
565 }
566
567 static void
568 sighandler(const int signo)
569 {
570 if (signo == SIGTERM || signo == SIGINT) {
571 done = 1;
572 }
573 }
574
575 int
576 main(void)
577 {
578 size_t i;
579 char status_string[4096];
580 char *res, *element, *status_old;
581 struct arg argument;
582 struct sigaction act;
583
584 memset(&act, 0, sizeof(act));
585 act.sa_handler = sighandler;
586 sigaction(SIGINT, &act, 0);
587 sigaction(SIGTERM, &act, 0);
588
589 dpy = XOpenDisplay(NULL);
590
591 XFetchName(dpy, DefaultRootWindow(dpy), &status_old);
592
593 while (!done) {
594 status_string[0] = '\0';
595 for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
596 argument = args[i];
597 if (argument.args == NULL)
598 res = argument.func();
599 else
600 res = argument.func(argument.args);
601 element = smprintf(argument.format, res);
602 if (element == NULL) {
603 element = smprintf(UNKNOWN_STR);
604 warnx("Failed to format output.");
605 }
606 strlcat(status_string, element, sizeof(status_string));
607 free(res);
608 free(element);
609 }
610 XStoreName(dpy, DefaultRootWindow(dpy), status_string);
611 XSync(dpy, False);
612 /*
613 * subtract delay time spend in function
614 * calls from the actual global delay time
615 */
616 sleep(UPDATE_INTERVAL - delay);
617 delay = 0;
618 }
619
620 XStoreName(dpy, DefaultRootWindow(dpy), status_old);
621 XSync(dpy, False);
622
623 XCloseDisplay(dpy);
624
625 return 0;
626 }