Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
4 #include <alsa/asoundlib.h>
9 #include <linux/wireless.h>
17 #include <sys/ioctl.h>
19 #include <sys/statvfs.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
32 setstatus(const char *str
)
34 /* set WM_NAME via X11 */
35 XStoreName(dpy
, DefaultRootWindow(dpy
), str
);
39 /* smprintf function */
41 smprintf(const char *fmt
, ...)
46 va_start(fmtargs
, fmt
);
47 if (vasprintf(&ret
, fmt
, fmtargs
) < 0) {
55 /* battery percentage */
57 battery_perc(const char *battery
)
60 char batterynowfile
[64] = "";
61 char batteryfullfile
[64] = "";
64 /* generate battery nowfile path */
65 strcat(batterynowfile
, batterypath
);
66 strcat(batterynowfile
, battery
);
67 strcat(batterynowfile
, "/");
68 strcat(batterynowfile
, batterynow
);
70 /* generate battery fullfile path */
71 strcat(batteryfullfile
, batterypath
);
72 strcat(batteryfullfile
, battery
);
73 strcat(batteryfullfile
, "/");
74 strcat(batteryfullfile
, batteryfull
);
76 /* open battery now file */
77 if (!(fp
= fopen(batterynowfile
, "r"))) {
78 fprintf(stderr
, "Error opening battery file: %s.\n", batterynowfile
);
79 return smprintf("n/a");
83 fscanf(fp
, "%i", &now
);
85 /* close battery now file */
88 /* open battery full file */
89 if (!(fp
= fopen(batteryfullfile
, "r"))) {
90 fprintf(stderr
, "Error opening battery file.\n");
91 return smprintf("n/a");
95 fscanf(fp
, "%i", &full
);
97 /* close battery full file */
100 /* calculate percent */
101 perc
= now
/ (full
/ 100);
103 /* return perc as string */
104 return smprintf("%d%%", perc
);
109 cpu_perc(const char *null
)
112 long double a
[4], b
[4];
116 if (!(fp
= fopen("/proc/stat","r"))) {
117 fprintf(stderr
, "Error opening stat file.\n");
118 return smprintf("n/a");
122 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &a
[0], &a
[1], &a
[2], &a
[3]);
124 /* close stat file */
127 /* wait a second (for avg values) */
131 if (!(fp
= fopen("/proc/stat","r"))) {
132 fprintf(stderr
, "Error opening stat file.\n");
133 return smprintf("n/a");
137 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &b
[0], &b
[1], &b
[2], &b
[3]);
139 /* close stat file */
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]));
145 /* return perc as string */
146 return smprintf("%d%%", perc
);
151 datetime(const char *timeformat
)
155 char *buf
= malloc(bufsize
);
157 fprintf(stderr
, "Failed to get date/time.\n");
158 return smprintf("n/a");
161 /* get time in format */
163 setlocale(LC_TIME
, "");
164 if (!strftime(buf
, bufsize
, timeformat
, localtime(&tm
))) {
165 setlocale(LC_TIME
, "C");
167 fprintf(stderr
, "Strftime failed.\n");
168 return smprintf("n/a");
171 setlocale(LC_TIME
, "C");
173 char *ret
= smprintf("%s", buf
);
180 disk_free(const char *mountpoint
)
184 /* try to open mountpoint */
185 if (statvfs(mountpoint
, &fs
) < 0) {
186 fprintf(stderr
, "Could not get filesystem info.\n");
187 return smprintf("n/a");
191 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_bfree
/ 1024 / 1024 / 1024);
194 /* disk usage percentage */
196 disk_perc(const char *mountpoint
)
201 /* try to open mountpoint */
202 if (statvfs(mountpoint
, &fs
) < 0) {
203 fprintf(stderr
, "Could not get filesystem info.\n");
204 return smprintf("n/a");
207 /* calculate percent */
208 perc
= 100 * (1.0f
- ((float)fs
.f_bfree
/ (float)fs
.f_blocks
));
211 return smprintf("%d%%", perc
);
216 disk_total(const char *mountpoint
)
220 /* try to open mountpoint */
221 if (statvfs(mountpoint
, &fs
) < 0) {
222 fprintf(stderr
, "Could not get filesystem info.\n");
223 return smprintf("n/a");
227 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_blocks
/ 1024 / 1024 / 1024);
232 disk_used(const char *mountpoint
)
236 /* try to open mountpoint */
237 if (statvfs(mountpoint
, &fs
) < 0) {
238 fprintf(stderr
, "Could not get filesystem info.\n");
239 return smprintf("n/a");
243 return smprintf("%f", (float)fs
.f_bsize
* ((float)fs
.f_blocks
- (float)fs
.f_bfree
) / 1024 / 1024 / 1024);
246 /* entropy available */
248 entropy(const char *null
)
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("n/a");
259 /* extract entropy */
260 fscanf(fp
, "%d", &entropy
);
262 /* close entropy file */
266 return smprintf("%d", entropy
);
271 gid(const char *null
)
275 if ((gid
= getgid()) < 0) {
276 fprintf(stderr
, "Could no get gid.\n");
277 return smprintf("n/a");
279 return smprintf("%d", gid
);
282 return smprintf("n/a");
287 hostname(const char *null
)
289 char hostname
[HOST_NAME_MAX
];
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("n/a");
298 /* extract hostname */
299 fscanf(fp
, "%s\n", hostname
);
301 /* close hostname file */
305 return smprintf("%s", hostname
);
310 ip(const char *interface
)
312 struct ifaddrs
*ifaddr
, *ifa
;
314 char host
[NI_MAXHOST
];
316 /* check if getting ip address works */
317 if (getifaddrs(&ifaddr
) == -1) {
318 fprintf(stderr
, "Error getting IP address.\n");
319 return smprintf("n/a");
322 /* get the ip address */
323 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
324 if (ifa
->ifa_addr
== NULL
) {
328 s
= getnameinfo(ifa
->ifa_addr
, sizeof(struct sockaddr_in
), host
, NI_MAXHOST
, NULL
, 0, NI_NUMERICHOST
);
330 if ((strcmp(ifa
->ifa_name
, interface
) == 0) && (ifa
->ifa_addr
->sa_family
== AF_INET
)) {
332 fprintf(stderr
, "Error getting IP address.\n");
333 return smprintf("n/a");
335 return smprintf("%s", host
);
339 /* free the address */
342 /* return n/a if nothing works */
343 return smprintf("n/a");
348 ram_free(const char *null
)
353 /* open meminfo file */
354 if (!(fp
= fopen("/proc/meminfo", "r"))) {
355 fprintf(stderr
, "Error opening meminfo file.\n");
356 return smprintf("n/a");
359 /* read the values */
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.\n");
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.\n");
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.\n");
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
)
459 /* try to open the command output */
460 if (!(fp
= popen(command
, "r"))) {
461 fprintf(stderr
, "Could not get command output for: %s.\n", command
);
462 return smprintf("n/a");
465 /* get command output text, save it to buffer */
466 fgets(buffer
, sizeof(buffer
) - 1, fp
);
471 /* add nullchar at the end */
472 for (int i
= 0 ; i
!= sizeof(buffer
); i
++) {
473 if (buffer
[i
] == '\0') {
479 buffer
[strlen(buffer
) - 1] = '\0';
482 /* return the output */
483 return smprintf("%s", buffer
);
488 temp(const char *file
)
493 /* open temperature file */
494 if (!(fp
= fopen(file
, "r"))) {
495 fprintf(stderr
, "Could not open temperature file.\n");
496 return smprintf("n/a");
499 /* extract temperature */
500 fscanf(fp
, "%d", &temperature
);
502 /* close temperature file */
505 /* return temperature in degrees */
506 return smprintf("%d°C", temperature
/ 1000);
511 username(const char *null
)
513 register struct passwd
*pw
;
520 /* if it worked, return */
522 return smprintf("%s", pw
->pw_name
);
524 fprintf(stderr
, "Could not get username.\n");
525 return smprintf("n/a");
528 return smprintf("n/a");
533 uid(const char *null
)
540 /* if it worked, return */
542 return smprintf("%d", uid
);
544 fprintf(stderr
, "Could not get uid.\n");
545 return smprintf("n/a");
548 return smprintf("n/a");
552 /* alsa volume percentage */
554 vol_perc(const char *soundcard
)
557 long vol
= 0, max
= 0, min
= 0;
559 snd_mixer_elem_t
*pcm_mixer
, *mas_mixer
;
560 snd_mixer_selem_id_t
*vol_info
, *mute_info
;
562 /* open everything */
563 snd_mixer_open(&handle
, 0);
564 snd_mixer_attach(handle
, soundcard
);
565 snd_mixer_selem_register(handle
, NULL
, NULL
);
566 snd_mixer_load(handle
);
568 /* prepare everything */
569 snd_mixer_selem_id_malloc(&vol_info
);
570 snd_mixer_selem_id_malloc(&mute_info
);
572 if (vol_info
== NULL
|| mute_info
== NULL
) {
573 fprintf(stderr
, "Could not get alsa volume.\n");
574 return smprintf("n/a");
576 snd_mixer_selem_id_set_name(vol_info
, channel
);
577 snd_mixer_selem_id_set_name(mute_info
, channel
);
578 pcm_mixer
= snd_mixer_find_selem(handle
, vol_info
);
579 mas_mixer
= snd_mixer_find_selem(handle
, mute_info
);
582 snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t
*)pcm_mixer
, &min
, &max
);
583 snd_mixer_selem_get_playback_volume((snd_mixer_elem_t
*)pcm_mixer
, SND_MIXER_SCHN_MONO
, &vol
);
584 snd_mixer_selem_get_playback_switch(mas_mixer
, SND_MIXER_SCHN_MONO
, &mute
);
588 snd_mixer_selem_id_free(vol_info
);
591 snd_mixer_selem_id_free(mute_info
);
594 snd_mixer_close(handle
);
597 /* return the string (mute) */
599 return smprintf("mute");
601 return smprintf("%d%%", (vol
* 100) / max
);
605 /* wifi percentage */
607 wifi_perc(const char *wificard
)
615 char needle
[sizeof wificard
+ 1];
618 /* generate the path name */
619 memset(path
, 0, sizeof path
);
620 strcat(path
, "/sys/class/net/");
621 strcat(path
, wificard
);
622 strcat(path
, "/operstate");
625 if(!(fp
= fopen(path
, "r"))) {
626 fprintf(stderr
, "Error opening wifi operstate file.\n");
627 return smprintf("n/a");
630 /* read the status */
631 fgets(status
, 5, fp
);
633 /* close wifi file */
636 /* check if interface down */
637 if(strcmp(status
, "up\n") != 0) {
638 return smprintf("n/a");
642 if (!(fp
= fopen("/proc/net/wireless", "r"))) {
643 fprintf(stderr
, "Error opening wireless file.\n");
644 return smprintf("n/a");
647 /* extract the signal strength */
648 strcpy(needle
, wificard
);
650 fgets(buf
, bufsize
, fp
);
651 fgets(buf
, bufsize
, fp
);
652 fgets(buf
, bufsize
, fp
);
653 if ((datastart
= strstr(buf
, needle
)) != NULL
) {
654 datastart
= strstr(buf
, ":");
655 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength
);
658 /* close wifi file */
661 /* return strength in percent */
662 return smprintf("%d%%", strength
);
667 wifi_essid(const char *wificard
)
669 char id
[IW_ESSID_MAX_SIZE
+1];
674 memset(&wreq
, 0, sizeof(struct iwreq
));
675 wreq
.u
.essid
.length
= IW_ESSID_MAX_SIZE
+1;
677 /* set the interface */
678 sprintf(wreq
.ifr_name
, wificard
);
681 if((sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1) {
682 fprintf(stderr
, "Cannot open socket for interface: %s\n", wificard
);
683 return smprintf("n/a");
685 wreq
.u
.essid
.pointer
= id
;
686 if (ioctl(sockfd
,SIOCGIWESSID
, &wreq
) == -1) {
687 fprintf(stderr
, "Get ESSID ioctl failed for interface %s\n", wificard
);
688 return smprintf("n/a");
691 /* return the essid */
692 if (strcmp((char *)wreq
.u
.essid
.pointer
, "") == 0) {
693 return smprintf("n/a");
695 return smprintf("%s", (char *)wreq
.u
.essid
.pointer
);
703 char status_string
[1024];
706 /* try to open display */
707 if (!(dpy
= XOpenDisplay(0x0))) {
708 fprintf(stderr
, "Cannot open display!\n");
712 /* return status every interval */
714 /* clear the string */
715 memset(status_string
, 0, sizeof(status_string
));
717 /* generate status_string */
718 for (size_t i
= 0; i
< sizeof(args
) / sizeof(args
[0]); ++i
) {
720 char *res
= argument
.func(argument
.args
);
721 char *element
= smprintf(argument
.format
, res
);
722 if (element
== NULL
) {
723 element
= smprintf("n/a");
724 fprintf(stderr
, "Failed to format output.\n");
726 strcat(status_string
, element
);
731 /* return the statusbar */
732 setstatus(status_string
);
734 /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
735 sleep(update_interval
-1);
741 /* exit successfully */