Xinqi Bao's Git

revised readme
[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
154 /* get time in format */
155 time(&tm);
156 setlocale(LC_TIME, "");
157 if(!strftime(buf, bufsize, timeformat, localtime(&tm))) {
158 setlocale(LC_TIME, "C");
159 fprintf(stderr, "Strftime failed.\n");
160 return smprintf("n/a");
161 }
162
163 setlocale(LC_TIME, "C");
164 /* return time */
165 char *ret = smprintf("%s", buf);
166 free(buf);
167 return ret;
168 }
169
170 /* disk free */
171 char *
172 disk_free(const char *mountpoint)
173 {
174 struct statvfs fs;
175
176 /* try to open mountpoint */
177 if (statvfs(mountpoint, &fs) < 0) {
178 fprintf(stderr, "Could not get filesystem info.\n");
179 return smprintf("n/a");
180 }
181
182 /* return free */
183 return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
184 }
185
186 /* disk usage percentage */
187 char *
188 disk_perc(const char *mountpoint)
189 {
190 int perc = 0;
191 struct statvfs fs;
192
193 /* try to open mountpoint */
194 if (statvfs(mountpoint, &fs) < 0) {
195 fprintf(stderr, "Could not get filesystem info.\n");
196 return smprintf("n/a");
197 }
198
199 /* calculate percent */
200 perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
201
202 /* return perc */
203 return smprintf("%d%%", perc);
204 }
205
206 /* disk total */
207 char *
208 disk_total(const char *mountpoint)
209 {
210 struct statvfs fs;
211
212 /* try to open mountpoint */
213 if (statvfs(mountpoint, &fs) < 0) {
214 fprintf(stderr, "Could not get filesystem info.\n");
215 return smprintf("n/a");
216 }
217
218 /* return total */
219 return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
220 }
221
222 /* disk used */
223 char *
224 disk_used(const char *mountpoint)
225 {
226 struct statvfs fs;
227
228 /* try to open mountpoint */
229 if (statvfs(mountpoint, &fs) < 0) {
230 fprintf(stderr, "Could not get filesystem info.\n");
231 return smprintf("n/a");
232 }
233
234 /* return used */
235 return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
236 }
237
238 /* entropy available */
239 char *
240 entropy(const char *null)
241 {
242 int entropy = 0;
243 FILE *fp;
244
245 /* open entropy file */
246 if (!(fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"))) {
247 fprintf(stderr, "Could not open entropy file.\n");
248 return smprintf("n/a");
249 }
250
251 /* extract entropy */
252 fscanf(fp, "%d", &entropy);
253
254 /* close entropy file */
255 fclose(fp);
256
257 /* return entropy */
258 return smprintf("%d", entropy);
259 }
260
261 /* gid */
262 char *
263 gid(const char *null)
264 {
265 gid_t gid;
266
267 if ((gid = getgid()) < 0) {
268 fprintf(stderr, "Could no get gid.");
269 return smprintf("n/a");
270 } else {
271 return smprintf("%d", gid);
272 }
273
274 return smprintf("n/a");
275 }
276
277 /* hostname */
278 char *
279 hostname(const char *null)
280 {
281 char hostname[HOST_NAME_MAX];
282 FILE *fp;
283
284 /* open hostname file */
285 if (!(fp = fopen("/proc/sys/kernel/hostname", "r"))) {
286 fprintf(stderr, "Could not open hostname file.\n");
287 return smprintf("n/a");
288 }
289
290 /* extract hostname */
291 fscanf(fp, "%s\n", hostname);
292
293 /* close hostname file */
294 fclose(fp);
295
296 /* return entropy */
297 return smprintf("%s", hostname);
298 }
299
300 /* ip address */
301 char *
302 ip(const char *interface)
303 {
304 struct ifaddrs *ifaddr, *ifa;
305 int s;
306 char host[NI_MAXHOST];
307
308 /* check if getting ip address works */
309 if (getifaddrs(&ifaddr) == -1)
310 {
311 fprintf(stderr, "Error getting IP address.");
312 return smprintf("n/a");
313 }
314
315 /* get the ip address */
316 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
317 {
318 if (ifa->ifa_addr == NULL)
319 continue;
320
321 s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
322
323 if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET))
324 {
325 if (s != 0)
326 {
327 fprintf(stderr, "Error getting IP address.");
328 return smprintf("n/a");
329 }
330 return smprintf("%s", host);
331 }
332 }
333
334 /* free the address */
335 freeifaddrs(ifaddr);
336
337 /* return n/a if nothing works */
338 return smprintf("n/a");
339 }
340
341 /* ram free */
342 char *
343 ram_free(const char *null)
344 {
345 long free;
346 FILE *fp;
347
348 /* open meminfo file */
349 if (!(fp = fopen("/proc/meminfo", "r"))) {
350 fprintf(stderr, "Error opening meminfo file.");
351 return smprintf("n/a");
352 }
353
354 /* read the values */
355 fscanf(fp, "MemTotal: %*d kB\n");
356 fscanf(fp, "MemFree: %ld kB\n", &free);
357
358 /* close meminfo file */
359 fclose(fp);
360
361 /* return free ram as string */
362 return smprintf("%f", (float)free / 1024 / 1024);
363 }
364
365 /* ram percentage */
366 char *
367 ram_perc(const char *null)
368 {
369 int perc;
370 long total, free, buffers, cached;
371 FILE *fp;
372
373 /* open meminfo file */
374 if (!(fp = fopen("/proc/meminfo", "r"))) {
375 fprintf(stderr, "Error opening meminfo file.");
376 return smprintf("n/a");
377 }
378
379 /* read the values */
380 fscanf(fp, "MemTotal: %ld kB\n", &total);
381 fscanf(fp, "MemFree: %ld kB\n", &free);
382 fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
383 fscanf(fp, "Cached: %ld kB\n", &cached);
384
385 /* close meminfo file */
386 fclose(fp);
387
388 /* calculate percentage */
389 perc = 100 * ((total - free) - (buffers + cached)) / total;
390
391 /* return perc as string */
392 return smprintf("%d%%", perc);
393 }
394
395 /* ram total */
396 char *
397 ram_total(const char *null)
398 {
399 long total;
400 FILE *fp;
401
402 /* open meminfo file */
403 if (!(fp = fopen("/proc/meminfo", "r"))) {
404 fprintf(stderr, "Error opening meminfo file.");
405 return smprintf("n/a");
406 }
407
408 /* read the values */
409 fscanf(fp, "MemTotal: %ld kB\n", &total);
410
411 /* close meminfo file */
412 fclose(fp);
413
414 /* return total ram as string */
415 return smprintf("%f", (float)total / 1024 / 1024);
416 }
417
418 /* ram used */
419 char *
420 ram_used(const char *null)
421 {
422 long free, total, buffers, cached, used;
423 FILE *fp;
424
425 /* open meminfo file */
426 if (!(fp = fopen("/proc/meminfo", "r"))) {
427 fprintf(stderr, "Error opening meminfo file.");
428 return smprintf("n/a");
429 }
430
431 /* read the values */
432 fscanf(fp, "MemTotal: %ld kB\n", &total);
433 fscanf(fp, "MemFree: %ld kB\n", &free);
434 fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
435 fscanf(fp, "Cached: %ld kB\n", &cached);
436
437 /* close meminfo file */
438 fclose(fp);
439
440 /* calculate used */
441 used = total - free - buffers - cached;
442
443 /* return used ram as string */
444 return smprintf("%f", (float)used / 1024 / 1024);
445 }
446
447 /* temperature */
448 char *
449 temp(const char *file)
450 {
451 int temperature;
452 FILE *fp;
453
454 /* open temperature file */
455 if (!(fp = fopen(file, "r"))) {
456 fprintf(stderr, "Could not open temperature file.\n");
457 return smprintf("n/a");
458 }
459
460 /* extract temperature */
461 fscanf(fp, "%d", &temperature);
462
463 /* close temperature file */
464 fclose(fp);
465
466 /* return temperature in degrees */
467 return smprintf("%d°C", temperature / 1000);
468 }
469
470 /* username */
471 char *
472 username(const char *null)
473 {
474 register struct passwd *pw;
475 register uid_t uid;
476
477 /* get the values */
478 uid = geteuid ();
479 pw = getpwuid (uid);
480
481 /* if it worked, return */
482 if (pw) {
483 return smprintf("%s", pw->pw_name);
484 }
485 else {
486 fprintf(stderr, "Could not get username.\n");
487 return smprintf("n/a");
488 }
489
490 return smprintf("n/a");
491 }
492
493 /* uid */
494 char *
495 uid(const char *null)
496 {
497 register uid_t uid;
498
499 /* get the values */
500 uid = geteuid ();
501
502 /* if it worked, return */
503 if (uid) {
504 return smprintf("%d", uid);
505 }
506 else {
507 fprintf(stderr, "Could not get uid.\n");
508 return smprintf("n/a");
509 }
510
511 return smprintf("n/a");
512 }
513
514
515 /* alsa volume percentage */
516 char *
517 vol_perc(const char *soundcard)
518 {
519 int mute = 0;
520 long vol = 0, max = 0, min = 0;
521
522 /* get volume from alsa */
523 snd_mixer_t *handle;
524 snd_mixer_elem_t *pcm_mixer, *mas_mixer;
525 snd_mixer_selem_id_t *vol_info, *mute_info;
526 snd_mixer_open(&handle, 0);
527 snd_mixer_attach(handle, soundcard);
528 snd_mixer_selem_register(handle, NULL, NULL);
529 snd_mixer_load(handle);
530 snd_mixer_selem_id_malloc(&vol_info);
531 snd_mixer_selem_id_malloc(&mute_info);
532 snd_mixer_selem_id_set_name(vol_info, channel);
533 snd_mixer_selem_id_set_name(mute_info, channel);
534 pcm_mixer = snd_mixer_find_selem(handle, vol_info);
535 mas_mixer = snd_mixer_find_selem(handle, mute_info);
536 snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max);
537 snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol);
538 snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute);
539 if (vol_info)
540 snd_mixer_selem_id_free(vol_info);
541 if (mute_info)
542 snd_mixer_selem_id_free(mute_info);
543 if (handle)
544 snd_mixer_close(handle);
545
546 /* return the string (mute) */
547 if (!mute)
548 return smprintf("mute");
549 else
550 return smprintf("%d%%", (vol * 100) / max);
551 }
552
553 /* wifi percentage */
554 char *
555 wifi_perc(const char *wificard)
556 {
557 int bufsize = 255;
558 int strength;
559 char buf[bufsize];
560 char *datastart;
561 char path[64];
562 char status[5];
563 char needle[sizeof wificard + 1];
564 FILE *fp;
565
566 /* generate the path name */
567 memset(path, 0, sizeof path);
568 strcat(path, "/sys/class/net/");
569 strcat(path, wificard);
570 strcat(path, "/operstate");
571
572 /* open wifi file */
573 if(!(fp = fopen(path, "r"))) {
574 fprintf(stderr, "Error opening wifi operstate file.");
575 return smprintf("n/a");
576 }
577
578 /* read the status */
579 fgets(status, 5, fp);
580
581 /* close wifi file */
582 fclose(fp);
583
584 /* check if interface down */
585 if(strcmp(status, "up\n") != 0){
586 return smprintf("n/a");
587 }
588
589 /* open wifi file */
590 if (!(fp = fopen("/proc/net/wireless", "r"))) {
591 fprintf(stderr, "Error opening wireless file.");
592 return smprintf("n/a");
593 }
594
595 /* extract the signal strength */
596 strcpy(needle, wificard);
597 strcat(needle, ":");
598 fgets(buf, bufsize, fp);
599 fgets(buf, bufsize, fp);
600 fgets(buf, bufsize, fp);
601 if ((datastart = strstr(buf, needle)) != NULL) {
602 datastart = strstr(buf, ":");
603 sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
604 }
605
606 /* close wifi file */
607 fclose(fp);
608
609 /* return strength in percent */
610 return smprintf("%d%%", strength);
611 }
612
613 /* main function */
614 int
615 main()
616 {
617 char status_string[1024];
618 struct arg argument;
619
620 /* try to open display */
621 if (!(dpy = XOpenDisplay(0x0))) {
622 fprintf(stderr, "Cannot open display!\n");
623 exit(1);
624 }
625
626 /* return status every interval */
627 for (;;) {
628 /* clear the string */
629 memset(status_string, 0, sizeof(status_string));
630
631 /* generate status_string */
632 for (size_t i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
633 argument = args[i];
634 char *res = argument.func(argument.args);
635 char *element = smprintf(argument.format, res);
636 strcat(status_string, element);
637 free(res);
638 free(element);
639 }
640
641 /* return the statusbar */
642 setstatus(status_string);
643
644 /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
645 sleep(update_interval -1);
646 }
647
648 /* close display */
649 XCloseDisplay(dpy);
650
651 /* exit successfully */
652 return 0;
653 }