Xinqi Bao's Git

55e638f37a14cba4637a1c60d9e20ec098863185
[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 <stdarg.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/ioctl.h>
16 #include <sys/stat.h>
17 #include <sys/statvfs.h>
18 #include <sys/socket.h>
19 #include <sys/sysinfo.h>
20 #include <sys/types.h>
21 #include <time.h>
22 #include <unistd.h>
23 #include <X11/Xlib.h>
24
25 #undef strlcat
26 #undef strlcpy
27
28 #include "strlcat.h"
29 #include "strlcpy.h"
30 #include "concat.h"
31
32 char concat[];
33
34 struct arg {
35 char *(*func)();
36 const char *format;
37 const char *args;
38 };
39
40 static char *smprintf(const char *, ...);
41 static char *battery_perc(const char *);
42 static char *cpu_perc(void);
43 static char *datetime(const char *);
44 static char *disk_free(const char *);
45 static char *disk_perc(const char *);
46 static char *disk_total(const char *);
47 static char *disk_used(const char *);
48 static char *entropy(void);
49 static char *gid(void);
50 static char *hostname(void);
51 static char *ip(const char *);
52 static char *load_avg(void);
53 static char *ram_free(void);
54 static char *ram_perc(void);
55 static char *ram_used(void);
56 static char *ram_total(void);
57 static char *run_command(const char *);
58 static char *temp(const char *);
59 static char *uid(void);
60 static char *uptime(void);
61 static char *username(void);
62 static char *vol_perc(const char *);
63 static char *wifi_perc(const char *);
64 static char *wifi_essid(const char *);
65
66 static Display *dpy;
67
68 #include "config.h"
69
70 static char *
71 smprintf(const char *fmt, ...)
72 {
73 va_list ap;
74 char *ret;
75 int len;
76
77 va_start(ap, fmt);
78 len = vsnprintf(NULL, 0, fmt, ap);
79 va_end(ap);
80
81 ret = malloc(++len);
82 if (ret == NULL) {
83 perror("malloc");
84 exit(1);
85 }
86
87 va_start(ap, fmt);
88 vsnprintf(ret, len, fmt, ap);
89 va_end(ap);
90
91 return ret;
92 }
93
94 static char *
95 battery_perc(const char *battery)
96 {
97 int now, full, perc;
98 FILE *fp;
99
100 ccat(4, BATTERY_PATH, battery, "/", BATTERY_NOW);
101
102 fp = fopen(concat, "r");
103 if (fp == NULL) {
104 warn("Error opening battery file: %s", concat);
105 return smprintf(UNKNOWN_STR);
106 }
107
108 fscanf(fp, "%i", &now);
109 fclose(fp);
110
111 ccat(4, BATTERY_PATH, battery, "/", BATTERY_FULL);
112
113 fp = fopen(concat, "r");
114 if (fp == NULL) {
115 warn("Error opening battery file: %s", concat);
116 return smprintf(UNKNOWN_STR);
117 }
118
119 fscanf(fp, "%i", &full);
120 fclose(fp);
121
122 perc = now / (full / 100);
123
124 return smprintf("%d%%", perc);
125 }
126
127 static char *
128 cpu_perc(void)
129 {
130 int perc;
131 long double a[4], b[4];
132 FILE *fp = fopen("/proc/stat","r");
133
134 if (fp == NULL) {
135 warn("Error opening stat file");
136 return smprintf(UNKNOWN_STR);
137 }
138
139 fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
140 fclose(fp);
141
142 sleep(1);
143
144 fp = fopen("/proc/stat","r");
145 if (fp == NULL) {
146 warn("Error opening stat file");
147 return smprintf(UNKNOWN_STR);
148 }
149
150 fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
151 fclose(fp);
152 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]));
153 return smprintf("%d%%", perc);
154 }
155
156 static char *
157 datetime(const char *timeformat)
158 {
159 time_t t;
160 char timestr[80];
161
162 t = time(NULL);
163 if (strftime(timestr, sizeof(timestr), timeformat, localtime(&t)) == 0)
164 return smprintf(UNKNOWN_STR);
165
166 return smprintf("%s", timestr);
167 }
168
169 static char *
170 disk_free(const char *mountpoint)
171 {
172 struct statvfs fs;
173
174 if (statvfs(mountpoint, &fs) < 0) {
175 warn("Could not get filesystem info");
176 return smprintf(UNKNOWN_STR);
177 }
178 return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
179 }
180
181 static char *
182 disk_perc(const char *mountpoint)
183 {
184 int perc = 0;
185 struct statvfs fs;
186
187 if (statvfs(mountpoint, &fs) < 0) {
188 warn("Could not get filesystem info");
189 return smprintf(UNKNOWN_STR);
190 }
191
192 perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
193 return smprintf("%d%%", perc);
194 }
195
196 static char *
197 disk_total(const char *mountpoint)
198 {
199 struct statvfs fs;
200
201 if (statvfs(mountpoint, &fs) < 0) {
202 warn("Could not get filesystem info");
203 return smprintf(UNKNOWN_STR);
204 }
205
206 return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
207 }
208
209 static char *
210 disk_used(const char *mountpoint)
211 {
212 struct statvfs fs;
213
214 if (statvfs(mountpoint, &fs) < 0) {
215 warn("Could not get filesystem info");
216 return smprintf(UNKNOWN_STR);
217 }
218
219 return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
220 }
221
222 static char *
223 entropy(void)
224 {
225 int entropy = 0;
226 FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r");
227
228 if (fp == NULL) {
229 warn("Could not open entropy file");
230 return smprintf(UNKNOWN_STR);
231 }
232
233 fscanf(fp, "%d", &entropy);
234 fclose(fp);
235 return smprintf("%d", entropy);
236 }
237
238 static char *
239 gid(void)
240 {
241 return smprintf("%d", getgid());
242 }
243
244 static char *
245 hostname(void)
246 {
247 char hostname[HOST_NAME_MAX];
248 FILE *fp = fopen("/proc/sys/kernel/hostname", "r");
249
250 if (fp == NULL) {
251 warn("Could not open hostname file");
252 return smprintf(UNKNOWN_STR);
253 }
254
255 fgets(hostname, sizeof(hostname), fp);
256 /* FIXME: needs improvement */
257 memset(&hostname[strlen(hostname)-1], '\0',
258 sizeof(hostname) - strlen(hostname));
259 fclose(fp);
260 return smprintf("%s", hostname);
261 }
262
263 static char *
264 ip(const char *interface)
265 {
266 struct ifaddrs *ifaddr, *ifa;
267 int s;
268 char host[NI_MAXHOST];
269
270 if (getifaddrs(&ifaddr) == -1) {
271 warn("Error getting IP address");
272 return smprintf(UNKNOWN_STR);
273 }
274
275 /* get the ip address */
276 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
277 if (ifa->ifa_addr == NULL)
278 continue;
279
280 s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST,
281 NULL, 0, NI_NUMERICHOST);
282
283 if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) {
284 if (s != 0) {
285 warnx("Error getting IP address.");
286 return smprintf(UNKNOWN_STR);
287 }
288 return smprintf("%s", host);
289 }
290 }
291
292 /* free the address */
293 freeifaddrs(ifaddr);
294
295 return smprintf(UNKNOWN_STR);
296 }
297
298 static char *
299 load_avg(void)
300 {
301 double avgs[3];
302
303 if (getloadavg(avgs, 3) < 0) {
304 warnx("Error getting load avg.");
305 return smprintf(UNKNOWN_STR);
306 }
307
308 return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
309 }
310
311 static char *
312 ram_free(void)
313 {
314 long free;
315 FILE *fp = fopen("/proc/meminfo", "r");
316
317 if (fp == NULL) {
318 warn("Error opening meminfo file");
319 return smprintf(UNKNOWN_STR);
320 }
321
322 fscanf(fp, "MemFree: %ld kB\n", &free);
323 fclose(fp);
324 return smprintf("%f", (float)free / 1024 / 1024);
325 }
326
327 static char *
328 ram_perc(void)
329 {
330 int perc;
331 long total, free, buffers, cached;
332 FILE *fp = fopen("/proc/meminfo", "r");
333
334 if (fp == NULL) {
335 warn("Error opening meminfo file");
336 return smprintf(UNKNOWN_STR);
337 }
338
339 fscanf(fp, "MemTotal: %ld kB\n", &total);
340 fscanf(fp, "MemFree: %ld kB\n", &free);
341 fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
342 fscanf(fp, "Cached: %ld kB\n", &cached);
343
344 fclose(fp);
345 perc = 100 * ((total - free) - (buffers + cached)) / total;
346 return smprintf("%d%%", perc);
347 }
348
349 static char *
350 ram_total(void)
351 {
352 long total;
353 FILE *fp = fopen("/proc/meminfo", "r");
354
355 if (fp == NULL) {
356 warn("Error opening meminfo file");
357 return smprintf(UNKNOWN_STR);
358 }
359
360 fscanf(fp, "MemTotal: %ld kB\n", &total);
361 fclose(fp);
362 return smprintf("%f", (float)total / 1024 / 1024);
363 }
364
365 static char *
366 ram_used(void)
367 {
368 long free, total, buffers, cached, used;
369 FILE *fp = fopen("/proc/meminfo", "r");
370
371 if (fp == NULL) {
372 warn("Error opening meminfo file");
373 return smprintf(UNKNOWN_STR);
374 }
375
376 fscanf(fp, "MemTotal: %ld kB\n", &total);
377 fscanf(fp, "MemFree: %ld kB\n", &free);
378 fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
379 fscanf(fp, "Cached: %ld kB\n", &cached);
380
381 fclose(fp);
382 used = total - free - buffers - cached;
383 return smprintf("%f", (float)used / 1024 / 1024);
384 }
385
386 static char *
387 run_command(const char* command)
388 {
389 int good;
390 FILE *fp = popen(command, "r");
391 char buffer[64];
392
393 if (fp == NULL) {
394 warn("Could not get command output for: %s", command);
395 return smprintf(UNKNOWN_STR);
396 }
397
398 fgets(buffer, sizeof(buffer)-1, fp);
399 pclose(fp);
400 for (int i = 0 ; i != sizeof(buffer); i++) {
401 if (buffer[i] == '\0') {
402 good = 1;
403 break;
404 }
405 }
406 if (good)
407 buffer[strlen(buffer)-1] = '\0';
408 return smprintf("%s", buffer);
409 }
410
411 static char *
412 temp(const char *file)
413 {
414 int temperature;
415 FILE *fp = fopen(file, "r");
416
417 if (fp == NULL) {
418 warn("Could not open temperature file");
419 return smprintf(UNKNOWN_STR);
420 }
421
422 fscanf(fp, "%d", &temperature);
423 fclose(fp);
424 return smprintf("%d°C", temperature / 1000);
425 }
426
427 static char *
428 uptime(void)
429 {
430 struct sysinfo info;
431 int hours = 0;
432 int minutes = 0;
433
434 sysinfo(&info);
435 hours = info.uptime / 3600;
436 minutes = (info.uptime - hours * 3600 ) / 60;
437
438 return smprintf("%dh %dm", hours, minutes);
439 }
440
441 static char *
442 username(void)
443 {
444 uid_t uid = geteuid();
445 struct passwd *pw = getpwuid(uid);
446
447 if (pw == NULL)
448 return smprintf("%s", pw->pw_name);
449
450 warn("Could not get username");
451 return smprintf(UNKNOWN_STR);
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 { /* thanks to botika for this function */
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, "default");
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, snd_card);
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 perror("alsa error");
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", (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 return smprintf("%d%%", strength);
536 }
537
538 static char *
539 wifi_essid(const char *wificard)
540 {
541 char id[IW_ESSID_MAX_SIZE+1];
542 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
543 struct iwreq wreq;
544
545 memset(&wreq, 0, sizeof(struct iwreq));
546 wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
547 sprintf(wreq.ifr_name, wificard);
548 if(sockfd == -1) {
549 warn("Cannot open socket for interface: %s", wificard);
550 return smprintf(UNKNOWN_STR);
551 }
552 wreq.u.essid.pointer = id;
553 if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
554 warn("Get ESSID ioctl failed for interface %s", wificard);
555 return smprintf(UNKNOWN_STR);
556 }
557
558 if (strcmp((char *)wreq.u.essid.pointer, "") == 0)
559 return smprintf(UNKNOWN_STR);
560 else
561 return smprintf("%s", (char *)wreq.u.essid.pointer);
562 }
563
564 int
565 main(void)
566 {
567 size_t i;
568 char status_string[4096];
569 char *res, *element;
570 struct arg argument;
571
572 dpy = XOpenDisplay(NULL);
573
574 for (;;) {
575 memset(status_string, 0, sizeof(status_string));
576 for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
577 argument = args[i];
578 if (argument.args == NULL)
579 res = argument.func();
580 else
581 res = argument.func(argument.args);
582 element = smprintf(argument.format, res);
583 if (element == NULL) {
584 element = smprintf(UNKNOWN_STR);
585 warnx("Failed to format output.");
586 }
587 strlcat(status_string, element, sizeof(status_string));
588 free(res);
589 free(element);
590 }
591 XStoreName(dpy, DefaultRootWindow(dpy), status_string);
592 XSync(dpy, False);
593 }
594 XCloseDisplay(dpy);
595
596 return 0;
597 }