Xinqi Bao's Git

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