Xinqi Bao's Git

9884aaad128177ecec3cd3e18a35da03d00fd8f7
[slstatus.git] / slstatus.c
1 /* See LICENSE file for copyright and license details. */
2
3 /* global libraries */
4 #include <alsa/asoundlib.h>
5 #include <arpa/inet.h>
6 #include <fcntl.h>
7 #include <ifaddrs.h>
8 #include <limits.h>
9 #include <linux/wireless.h>
10 #include <locale.h>
11 #include <netdb.h>
12 #include <pwd.h>
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/ioctl.h>
18 #include <sys/stat.h>
19 #include <sys/statvfs.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <X11/Xlib.h>
25
26 /* local headers */
27 #include "slstatus.h"
28 #include "config.h"
29
30 /* set statusbar */
31 void
32 setstatus(const char *str)
33 {
34 /* set WM_NAME via X11 */
35 XStoreName(dpy, DefaultRootWindow(dpy), str);
36 XSync(dpy, False);
37 }
38
39 /* smprintf function */
40 char *
41 smprintf(const char *fmt, ...)
42 {
43 va_list fmtargs;
44 char *ret = NULL;
45
46 va_start(fmtargs, fmt);
47 if (vasprintf(&ret, fmt, fmtargs) < 0) {
48 return NULL;
49 }
50 va_end(fmtargs);
51
52 return ret;
53 }
54
55 /* battery percentage */
56 char *
57 battery_perc(const char *battery)
58 {
59 int now, full, perc;
60 char batterynowfile[64] = "";
61 char batteryfullfile[64] = "";
62 FILE *fp;
63
64 /* generate battery nowfile path */
65 strcat(batterynowfile, batterypath);
66 strcat(batterynowfile, battery);
67 strcat(batterynowfile, "/");
68 strcat(batterynowfile, batterynow);
69
70 /* generate battery fullfile path */
71 strcat(batteryfullfile, batterypath);
72 strcat(batteryfullfile, battery);
73 strcat(batteryfullfile, "/");
74 strcat(batteryfullfile, batteryfull);
75
76 /* open battery now file */
77 if (!(fp = fopen(batterynowfile, "r"))) {
78 fprintf(stderr, "Error opening battery file: %s.\n", batterynowfile);
79 return smprintf(unknowntext);
80 }
81
82 /* read value */
83 fscanf(fp, "%i", &now);
84
85 /* close battery now file */
86 fclose(fp);
87
88 /* open battery full file */
89 if (!(fp = fopen(batteryfullfile, "r"))) {
90 fprintf(stderr, "Error opening battery file.\n");
91 return smprintf(unknowntext);
92 }
93
94 /* read value */
95 fscanf(fp, "%i", &full);
96
97 /* close battery full file */
98 fclose(fp);
99
100 /* calculate percent */
101 perc = now / (full / 100);
102
103 /* return perc as string */
104 return smprintf("%d%%", perc);
105 }
106
107 /* cpu percentage */
108 char *
109 cpu_perc(const char *null)
110 {
111 int perc;
112 long double a[4], b[4];
113 FILE *fp;
114
115 /* open stat file */
116 if (!(fp = fopen("/proc/stat","r"))) {
117 fprintf(stderr, "Error opening stat file.\n");
118 return smprintf(unknowntext);
119 }
120
121 /* read values */
122 fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
123
124 /* close stat file */
125 fclose(fp);
126
127 /* wait a second (for avg values) */
128 sleep(1);
129
130 /* open stat file */
131 if (!(fp = fopen("/proc/stat","r"))) {
132 fprintf(stderr, "Error opening stat file.\n");
133 return smprintf(unknowntext);
134 }
135
136 /* read values */
137 fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
138
139 /* close stat file */
140 fclose(fp);
141
142 /* calculate avg in this second */
143 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]));
144
145 /* return perc as string */
146 return smprintf("%d%%", perc);
147 }
148
149 /* date and time */
150 char *
151 datetime(const char *timeformat)
152 {
153 time_t tm;
154 size_t bufsize = 64;
155 char *buf = malloc(bufsize);
156 if (buf == NULL) {
157 fprintf(stderr, "Failed to get date/time.\n");
158 return smprintf(unknowntext);
159 }
160
161 /* get time in format */
162 time(&tm);
163 setlocale(LC_TIME, "");
164 if (!strftime(buf, bufsize, timeformat, localtime(&tm))) {
165 setlocale(LC_TIME, "C");
166 free(buf);
167 fprintf(stderr, "Strftime failed.\n");
168 return smprintf(unknowntext);
169 }
170
171 setlocale(LC_TIME, "C");
172 /* return time */
173 char *ret = smprintf("%s", buf);
174 free(buf);
175 return ret;
176 }
177
178 /* disk free */
179 char *
180 disk_free(const char *mountpoint)
181 {
182 struct statvfs fs;
183
184 /* try to open mountpoint */
185 if (statvfs(mountpoint, &fs) < 0) {
186 fprintf(stderr, "Could not get filesystem info.\n");
187 return smprintf(unknowntext);
188 }
189
190 /* return free */
191 return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
192 }
193
194 /* disk usage percentage */
195 char *
196 disk_perc(const char *mountpoint)
197 {
198 int perc = 0;
199 struct statvfs fs;
200
201 /* try to open mountpoint */
202 if (statvfs(mountpoint, &fs) < 0) {
203 fprintf(stderr, "Could not get filesystem info.\n");
204 return smprintf(unknowntext);
205 }
206
207 /* calculate percent */
208 perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
209
210 /* return perc */
211 return smprintf("%d%%", perc);
212 }
213
214 /* disk total */
215 char *
216 disk_total(const char *mountpoint)
217 {
218 struct statvfs fs;
219
220 /* try to open mountpoint */
221 if (statvfs(mountpoint, &fs) < 0) {
222 fprintf(stderr, "Could not get filesystem info.\n");
223 return smprintf(unknowntext);
224 }
225
226 /* return total */
227 return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
228 }
229
230 /* disk used */
231 char *
232 disk_used(const char *mountpoint)
233 {
234 struct statvfs fs;
235
236 /* try to open mountpoint */
237 if (statvfs(mountpoint, &fs) < 0) {
238 fprintf(stderr, "Could not get filesystem info.\n");
239 return smprintf(unknowntext);
240 }
241
242 /* return used */
243 return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
244 }
245
246 /* entropy available */
247 char *
248 entropy(const char *null)
249 {
250 int entropy = 0;
251 FILE *fp;
252
253 /* open entropy file */
254 if (!(fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"))) {
255 fprintf(stderr, "Could not open entropy file.\n");
256 return smprintf(unknowntext);
257 }
258
259 /* extract entropy */
260 fscanf(fp, "%d", &entropy);
261
262 /* close entropy file */
263 fclose(fp);
264
265 /* return entropy */
266 return smprintf("%d", entropy);
267 }
268
269 /* gid */
270 char *
271 gid(const char *null)
272 {
273 gid_t gid;
274
275 if ((gid = getgid()) < 0) {
276 fprintf(stderr, "Could no get gid.\n");
277 return smprintf(unknowntext);
278 } else {
279 return smprintf("%d", gid);
280 }
281
282 return smprintf(unknowntext);
283 }
284
285 /* hostname */
286 char *
287 hostname(const char *null)
288 {
289 char hostname[HOST_NAME_MAX];
290 FILE *fp;
291
292 /* open hostname file */
293 if (!(fp = fopen("/proc/sys/kernel/hostname", "r"))) {
294 fprintf(stderr, "Could not open hostname file.\n");
295 return smprintf(unknowntext);
296 }
297
298 /* extract hostname */
299 fscanf(fp, "%s\n", hostname);
300
301 /* close hostname file */
302 fclose(fp);
303
304 /* return entropy */
305 return smprintf("%s", hostname);
306 }
307
308 /* ip address */
309 char *
310 ip(const char *interface)
311 {
312 struct ifaddrs *ifaddr, *ifa;
313 int s;
314 char host[NI_MAXHOST];
315
316 /* check if getting ip address works */
317 if (getifaddrs(&ifaddr) == -1) {
318 fprintf(stderr, "Error getting IP address.\n");
319 return smprintf(unknowntext);
320 }
321
322 /* get the ip address */
323 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
324 if (ifa->ifa_addr == NULL) {
325 continue;
326 }
327
328 s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
329
330 if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) {
331 if (s != 0) {
332 fprintf(stderr, "Error getting IP address.\n");
333 return smprintf(unknowntext);
334 }
335 return smprintf("%s", host);
336 }
337 }
338
339 /* free the address */
340 freeifaddrs(ifaddr);
341
342 return smprintf(unknowntext);
343 }
344
345 /* ram free */
346 char *
347 ram_free(const char *null)
348 {
349 long free;
350 FILE *fp;
351
352 /* open meminfo file */
353 if (!(fp = fopen("/proc/meminfo", "r"))) {
354 fprintf(stderr, "Error opening meminfo file.\n");
355 return smprintf(unknowntext);
356 }
357
358 /* read the values */
359 fscanf(fp, "MemFree: %ld kB\n", &free);
360
361 /* close meminfo file */
362 fclose(fp);
363
364 /* return free ram as string */
365 return smprintf("%f", (float)free / 1024 / 1024);
366 }
367
368 /* ram percentage */
369 char *
370 ram_perc(const char *null)
371 {
372 int perc;
373 long total, free, buffers, cached;
374 FILE *fp;
375
376 /* open meminfo file */
377 if (!(fp = fopen("/proc/meminfo", "r"))) {
378 fprintf(stderr, "Error opening meminfo file.\n");
379 return smprintf(unknowntext);
380 }
381
382 /* read the values */
383 fscanf(fp, "MemTotal: %ld kB\n", &total);
384 fscanf(fp, "MemFree: %ld kB\n", &free);
385 fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
386 fscanf(fp, "Cached: %ld kB\n", &cached);
387
388 /* close meminfo file */
389 fclose(fp);
390
391 /* calculate percentage */
392 perc = 100 * ((total - free) - (buffers + cached)) / total;
393
394 /* return perc as string */
395 return smprintf("%d%%", perc);
396 }
397
398 /* ram total */
399 char *
400 ram_total(const char *null)
401 {
402 long total;
403 FILE *fp;
404
405 /* open meminfo file */
406 if (!(fp = fopen("/proc/meminfo", "r"))) {
407 fprintf(stderr, "Error opening meminfo file.\n");
408 return smprintf(unknowntext);
409 }
410
411 /* read the values */
412 fscanf(fp, "MemTotal: %ld kB\n", &total);
413
414 /* close meminfo file */
415 fclose(fp);
416
417 /* return total ram as string */
418 return smprintf("%f", (float)total / 1024 / 1024);
419 }
420
421 /* ram used */
422 char *
423 ram_used(const char *null)
424 {
425 long free, total, buffers, cached, used;
426 FILE *fp;
427
428 /* open meminfo file */
429 if (!(fp = fopen("/proc/meminfo", "r"))) {
430 fprintf(stderr, "Error opening meminfo file.\n");
431 return smprintf(unknowntext);
432 }
433
434 /* read the values */
435 fscanf(fp, "MemTotal: %ld kB\n", &total);
436 fscanf(fp, "MemFree: %ld kB\n", &free);
437 fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
438 fscanf(fp, "Cached: %ld kB\n", &cached);
439
440 /* close meminfo file */
441 fclose(fp);
442
443 /* calculate used */
444 used = total - free - buffers - cached;
445
446 /* return used ram as string */
447 return smprintf("%f", (float)used / 1024 / 1024);
448 }
449
450 /* custom shell command */
451 char *
452 run_command(const char* command)
453 {
454 int good;
455 FILE *fp;
456 char buffer[64];
457
458 /* try to open the command output */
459 if (!(fp = popen(command, "r"))) {
460 fprintf(stderr, "Could not get command output for: %s.\n", command);
461 return smprintf(unknowntext);
462 }
463
464 /* get command output text, save it to buffer */
465 fgets(buffer, sizeof(buffer) - 1, fp);
466
467 /* close it again */
468 pclose(fp);
469
470 /* add nullchar at the end */
471 for (int i = 0 ; i != sizeof(buffer); i++) {
472 if (buffer[i] == '\0') {
473 good = 1;
474 break;
475 }
476 }
477 if (good) {
478 buffer[strlen(buffer) - 1] = '\0';
479 }
480
481 /* return the output */
482 return smprintf("%s", buffer);
483 }
484
485 /* temperature */
486 char *
487 temp(const char *file)
488 {
489 int temperature;
490 FILE *fp;
491
492 /* open temperature file */
493 if (!(fp = fopen(file, "r"))) {
494 fprintf(stderr, "Could not open temperature file.\n");
495 return smprintf(unknowntext);
496 }
497
498 /* extract temperature */
499 fscanf(fp, "%d", &temperature);
500
501 /* close temperature file */
502 fclose(fp);
503
504 /* return temperature in degrees */
505 return smprintf("%d°C", temperature / 1000);
506 }
507
508 /* username */
509 char *
510 username(const char *null)
511 {
512 register struct passwd *pw;
513 register uid_t uid;
514
515 /* get the values */
516 uid = geteuid();
517 pw = getpwuid(uid);
518
519 /* if it worked, return */
520 if (pw) {
521 return smprintf("%s", pw->pw_name);
522 } else {
523 fprintf(stderr, "Could not get username.\n");
524 return smprintf(unknowntext);
525 }
526
527 return smprintf(unknowntext);
528 }
529
530 /* uid */
531 char *
532 uid(const char *null)
533 {
534 register uid_t uid;
535
536 /* get the values */
537 uid = geteuid();
538
539 /* if it worked, return */
540 if (uid) {
541 return smprintf("%d", uid);
542 } else {
543 fprintf(stderr, "Could not get uid.\n");
544 return smprintf(unknowntext);
545 }
546
547 return smprintf(unknowntext);
548 }
549
550
551 /* alsa volume percentage */
552 char *
553 vol_perc(const char *soundcard)
554 {
555 int mute = 0;
556 long vol = 0, max = 0, min = 0;
557 snd_mixer_t *handle;
558 snd_mixer_elem_t *pcm_mixer, *mas_mixer;
559 snd_mixer_selem_id_t *vol_info, *mute_info;
560
561 /* open everything */
562 snd_mixer_open(&handle, 0);
563 snd_mixer_attach(handle, soundcard);
564 snd_mixer_selem_register(handle, NULL, NULL);
565 snd_mixer_load(handle);
566
567 /* prepare everything */
568 snd_mixer_selem_id_malloc(&vol_info);
569 snd_mixer_selem_id_malloc(&mute_info);
570 /* check */
571 if (vol_info == NULL || mute_info == NULL) {
572 fprintf(stderr, "Could not get alsa volume.\n");
573 return smprintf(unknowntext);
574 }
575 snd_mixer_selem_id_set_name(vol_info, channel);
576 snd_mixer_selem_id_set_name(mute_info, channel);
577 pcm_mixer = snd_mixer_find_selem(handle, vol_info);
578 mas_mixer = snd_mixer_find_selem(handle, mute_info);
579
580 /* get the info */
581 snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max);
582 snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol);
583 snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute);
584
585 /* clean up */
586 if (vol_info) {
587 snd_mixer_selem_id_free(vol_info);
588 }
589 if (mute_info) {
590 snd_mixer_selem_id_free(mute_info);
591 }
592 if (handle) {
593 snd_mixer_close(handle);
594 }
595
596 /* return the string (mute) */
597 if (!mute) {
598 return smprintf("mute");
599 } else {
600 return smprintf("%d%%", (vol * 100) / max);
601 }
602 }
603
604 /* wifi percentage */
605 char *
606 wifi_perc(const char *wificard)
607 {
608 int bufsize = 255;
609 int strength;
610 char buf[bufsize];
611 char *datastart;
612 char path[64];
613 char status[5];
614 char needle[sizeof wificard + 1];
615 FILE *fp;
616
617 /* generate the path name */
618 memset(path, 0, sizeof path);
619 strcat(path, "/sys/class/net/");
620 strcat(path, wificard);
621 strcat(path, "/operstate");
622
623 /* open wifi file */
624 if(!(fp = fopen(path, "r"))) {
625 fprintf(stderr, "Error opening wifi operstate file.\n");
626 return smprintf(unknowntext);
627 }
628
629 /* read the status */
630 fgets(status, 5, fp);
631
632 /* close wifi file */
633 fclose(fp);
634
635 /* check if interface down */
636 if(strcmp(status, "up\n") != 0) {
637 return smprintf(unknowntext);
638 }
639
640 /* open wifi file */
641 if (!(fp = fopen("/proc/net/wireless", "r"))) {
642 fprintf(stderr, "Error opening wireless file.\n");
643 return smprintf(unknowntext);
644 }
645
646 /* extract the signal strength */
647 strcpy(needle, wificard);
648 strcat(needle, ":");
649 fgets(buf, bufsize, fp);
650 fgets(buf, bufsize, fp);
651 fgets(buf, bufsize, fp);
652 if ((datastart = strstr(buf, needle)) != NULL) {
653 datastart = strstr(buf, ":");
654 sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
655 }
656
657 /* close wifi file */
658 fclose(fp);
659
660 /* return strength in percent */
661 return smprintf("%d%%", strength);
662 }
663
664 /* wifi essid */
665 char *
666 wifi_essid(const char *wificard)
667 {
668 char id[IW_ESSID_MAX_SIZE+1];
669 int sockfd;
670 struct iwreq wreq;
671
672 /* prepare */
673 memset(&wreq, 0, sizeof(struct iwreq));
674 wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
675
676 /* set the interface */
677 sprintf(wreq.ifr_name, wificard);
678
679 /* check */
680 if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
681 fprintf(stderr, "Cannot open socket for interface: %s\n", wificard);
682 return smprintf(unknowntext);
683 }
684 wreq.u.essid.pointer = id;
685 if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
686 fprintf(stderr, "Get ESSID ioctl failed for interface %s\n", wificard);
687 return smprintf(unknowntext);
688 }
689
690 /* return the essid */
691 if (strcmp((char *)wreq.u.essid.pointer, "") == 0) {
692 return smprintf(unknowntext);
693 } else {
694 return smprintf("%s", (char *)wreq.u.essid.pointer);
695 }
696 }
697
698 /* main function */
699 int
700 main(void)
701 {
702 char status_string[1024];
703 struct arg argument;
704
705 /* try to open display */
706 if (!(dpy = XOpenDisplay(0x0))) {
707 fprintf(stderr, "Cannot open display!\n");
708 exit(1);
709 }
710
711 /* return status every interval */
712 for (;;) {
713 /* clear the string */
714 memset(status_string, 0, sizeof(status_string));
715
716 /* generate status_string */
717 for (size_t i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
718 argument = args[i];
719 char *res = argument.func(argument.args);
720 char *element = smprintf(argument.format, res);
721 if (element == NULL) {
722 element = smprintf(unknowntext);
723 fprintf(stderr, "Failed to format output.\n");
724 }
725 strcat(status_string, element);
726 free(res);
727 free(element);
728 }
729
730 /* return the statusbar */
731 setstatus(status_string);
732
733 /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
734 sleep(update_interval -1);
735 }
736
737 /* close display */
738 XCloseDisplay(dpy);
739
740 /* exit successfully */
741 return 0;
742 }