Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
4 #include <alsa/asoundlib.h>
16 #include <sys/types.h>
18 #include <sys/statvfs.h>
19 #include <sys/socket.h>
30 setstatus(const char *str
)
32 /* set WM_NAME via X11 */
33 XStoreName(dpy
, DefaultRootWindow(dpy
), str
);
37 /* smprintf function */
39 smprintf(const char *fmt
, ...)
44 va_start(fmtargs
, fmt
);
45 if (vasprintf(&ret
, fmt
, fmtargs
) < 0)
52 /* battery percentage */
54 battery_perc(const char *battery
)
57 char batterynowfile
[64] = "";
58 char batteryfullfile
[64] = "";
61 /* generate battery nowfile path */
62 strcat(batterynowfile
, batterypath
);
63 strcat(batterynowfile
, battery
);
64 strcat(batterynowfile
, "/");
65 strcat(batterynowfile
, batterynow
);
67 /* generate battery fullfile path */
68 strcat(batteryfullfile
, batterypath
);
69 strcat(batteryfullfile
, battery
);
70 strcat(batteryfullfile
, "/");
71 strcat(batteryfullfile
, batteryfull
);
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");
80 fscanf(fp
, "%i", &now
);
82 /* close battery now file */
85 /* open battery full file */
86 if (!(fp
= fopen(batteryfullfile
, "r"))) {
87 fprintf(stderr
, "Error opening battery file.");
88 return smprintf("n/a");
92 fscanf(fp
, "%i", &full
);
94 /* close battery full file */
97 /* calculate percent */
98 perc
= now
/ (full
/ 100);
100 /* return perc as string */
101 return smprintf("%d%%", perc
);
106 cpu_perc(const char *null
)
109 long double a
[4], b
[4];
113 if (!(fp
= fopen("/proc/stat","r"))) {
114 fprintf(stderr
, "Error opening stat file.");
115 return smprintf("n/a");
119 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &a
[0], &a
[1], &a
[2], &a
[3]);
121 /* close stat file */
124 /* wait a second (for avg values) */
128 if (!(fp
= fopen("/proc/stat","r"))) {
129 fprintf(stderr
, "Error opening stat file.");
130 return smprintf("n/a");
134 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &b
[0], &b
[1], &b
[2], &b
[3]);
136 /* close stat file */
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]));
142 /* return perc as string */
143 return smprintf("%d%%", perc
);
148 datetime(const char *timeformat
)
152 char *buf
= malloc(bufsize
);
154 fprintf(stderr
, "Failed to get date/time");
155 return smprintf("n/a");
158 /* get time in format */
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");
167 setlocale(LC_TIME
, "C");
169 char *ret
= smprintf("%s", buf
);
176 disk_free(const char *mountpoint
)
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");
187 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_bfree
/ 1024 / 1024 / 1024);
190 /* disk usage percentage */
192 disk_perc(const char *mountpoint
)
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");
203 /* calculate percent */
204 perc
= 100 * (1.0f
- ((float)fs
.f_bfree
/ (float)fs
.f_blocks
));
207 return smprintf("%d%%", perc
);
212 disk_total(const char *mountpoint
)
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");
223 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_blocks
/ 1024 / 1024 / 1024);
228 disk_used(const char *mountpoint
)
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");
239 return smprintf("%f", (float)fs
.f_bsize
* ((float)fs
.f_blocks
- (float)fs
.f_bfree
) / 1024 / 1024 / 1024);
242 /* entropy available */
244 entropy(const char *null
)
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");
255 /* extract entropy */
256 fscanf(fp
, "%d", &entropy
);
258 /* close entropy file */
262 return smprintf("%d", entropy
);
267 gid(const char *null
)
271 if ((gid
= getgid()) < 0) {
272 fprintf(stderr
, "Could no get gid.");
273 return smprintf("n/a");
275 return smprintf("%d", gid
);
278 return smprintf("n/a");
283 hostname(const char *null
)
285 char hostname
[HOST_NAME_MAX
];
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");
294 /* extract hostname */
295 fscanf(fp
, "%s\n", hostname
);
297 /* close hostname file */
301 return smprintf("%s", hostname
);
306 ip(const char *interface
)
308 struct ifaddrs
*ifaddr
, *ifa
;
310 char host
[NI_MAXHOST
];
312 /* check if getting ip address works */
313 if (getifaddrs(&ifaddr
) == -1)
315 fprintf(stderr
, "Error getting IP address.");
316 return smprintf("n/a");
319 /* get the ip address */
320 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
)
322 if (ifa
->ifa_addr
== NULL
)
325 s
= getnameinfo(ifa
->ifa_addr
, sizeof(struct sockaddr_in
), host
, NI_MAXHOST
, NULL
, 0, NI_NUMERICHOST
);
327 if ((strcmp(ifa
->ifa_name
, interface
) == 0) && (ifa
->ifa_addr
->sa_family
== AF_INET
))
331 fprintf(stderr
, "Error getting IP address.");
332 return smprintf("n/a");
334 return smprintf("%s", host
);
338 /* free the address */
341 /* return n/a if nothing works */
342 return smprintf("n/a");
347 ram_free(const char *null
)
352 /* open meminfo file */
353 if (!(fp
= fopen("/proc/meminfo", "r"))) {
354 fprintf(stderr
, "Error opening meminfo file.");
355 return smprintf("n/a");
358 /* read the values */
359 fscanf(fp
, "MemTotal: %*d kB\n");
360 fscanf(fp
, "MemFree: %ld kB\n", &free
);
362 /* close meminfo file */
365 /* return free ram as string */
366 return smprintf("%f", (float)free
/ 1024 / 1024);
371 ram_perc(const char *null
)
374 long total
, free
, buffers
, cached
;
377 /* open meminfo file */
378 if (!(fp
= fopen("/proc/meminfo", "r"))) {
379 fprintf(stderr
, "Error opening meminfo file.");
380 return smprintf("n/a");
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
);
389 /* close meminfo file */
392 /* calculate percentage */
393 perc
= 100 * ((total
- free
) - (buffers
+ cached
)) / total
;
395 /* return perc as string */
396 return smprintf("%d%%", perc
);
401 ram_total(const char *null
)
406 /* open meminfo file */
407 if (!(fp
= fopen("/proc/meminfo", "r"))) {
408 fprintf(stderr
, "Error opening meminfo file.");
409 return smprintf("n/a");
412 /* read the values */
413 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
415 /* close meminfo file */
418 /* return total ram as string */
419 return smprintf("%f", (float)total
/ 1024 / 1024);
424 ram_used(const char *null
)
426 long free
, total
, buffers
, cached
, used
;
429 /* open meminfo file */
430 if (!(fp
= fopen("/proc/meminfo", "r"))) {
431 fprintf(stderr
, "Error opening meminfo file.");
432 return smprintf("n/a");
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
);
441 /* close meminfo file */
445 used
= total
- free
- buffers
- cached
;
447 /* return used ram as string */
448 return smprintf("%f", (float)used
/ 1024 / 1024);
451 /* custom shell command */
453 run_command(const char* command
)
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");
464 /* get command output text, save it to buffer */
465 fgets(buffer
, sizeof(buffer
)-1, fp
);
470 /* add nullchar at the end */
471 buffer
[strlen(buffer
) - 1] = '\0';
473 /* return the output */
474 return smprintf("%s", buffer
);
479 temp(const char *file
)
484 /* open temperature file */
485 if (!(fp
= fopen(file
, "r"))) {
486 fprintf(stderr
, "Could not open temperature file.\n");
487 return smprintf("n/a");
490 /* extract temperature */
491 fscanf(fp
, "%d", &temperature
);
493 /* close temperature file */
496 /* return temperature in degrees */
497 return smprintf("%d°C", temperature
/ 1000);
502 username(const char *null
)
504 register struct passwd
*pw
;
511 /* if it worked, return */
513 return smprintf("%s", pw
->pw_name
);
516 fprintf(stderr
, "Could not get username.\n");
517 return smprintf("n/a");
520 return smprintf("n/a");
525 uid(const char *null
)
532 /* if it worked, return */
534 return smprintf("%d", uid
);
537 fprintf(stderr
, "Could not get uid.\n");
538 return smprintf("n/a");
541 return smprintf("n/a");
545 /* alsa volume percentage */
547 vol_perc(const char *soundcard
)
550 long vol
= 0, max
= 0, min
= 0;
552 snd_mixer_elem_t
*pcm_mixer
, *mas_mixer
;
553 snd_mixer_selem_id_t
*vol_info
, *mute_info
;
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
);
561 /* prepare everything */
562 snd_mixer_selem_id_malloc(&vol_info
);
563 snd_mixer_selem_id_malloc(&mute_info
);
565 if (vol_info
== NULL
|| mute_info
== NULL
) {
566 fprintf(stderr
, "Could not get alsa volume");
567 return smprintf("n/a");
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
);
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
);
581 snd_mixer_selem_id_free(vol_info
);
584 snd_mixer_selem_id_free(mute_info
);
587 snd_mixer_close(handle
);
590 /* return the string (mute) */
592 return smprintf("mute");
595 return smprintf("%d%%", (vol
* 100) / max
);
599 /* wifi percentage */
601 wifi_perc(const char *wificard
)
609 char needle
[sizeof wificard
+ 1];
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");
619 if(!(fp
= fopen(path
, "r"))) {
620 fprintf(stderr
, "Error opening wifi operstate file.");
621 return smprintf("n/a");
624 /* read the status */
625 fgets(status
, 5, fp
);
627 /* close wifi file */
630 /* check if interface down */
631 if(strcmp(status
, "up\n") != 0){
632 return smprintf("n/a");
636 if (!(fp
= fopen("/proc/net/wireless", "r"))) {
637 fprintf(stderr
, "Error opening wireless file.");
638 return smprintf("n/a");
641 /* extract the signal strength */
642 strcpy(needle
, wificard
);
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
);
652 /* close wifi file */
655 /* return strength in percent */
656 return smprintf("%d%%", strength
);
663 char status_string
[1024];
666 /* try to open display */
667 if (!(dpy
= XOpenDisplay(0x0))) {
668 fprintf(stderr
, "Cannot open display!\n");
672 /* return status every interval */
674 /* clear the string */
675 memset(status_string
, 0, sizeof(status_string
));
677 /* generate status_string */
678 for (size_t i
= 0; i
< sizeof(args
) / sizeof(args
[0]); ++i
) {
680 char *res
= argument
.func(argument
.args
);
681 char *element
= smprintf(argument
.format
, res
);
682 strcat(status_string
, element
);
687 /* return the statusbar */
688 setstatus(status_string
);
690 /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
691 sleep(update_interval
-1);
697 /* exit successfully */