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(unknowntext
);
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(unknowntext
);
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(unknowntext
);
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(unknowntext
);
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(unknowntext
);
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(unknowntext
);
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(unknowntext
);
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(unknowntext
);
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(unknowntext
);
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(unknowntext
);
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(unknowntext
);
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(unknowntext
);
279 return smprintf("%d", gid
);
282 return smprintf(unknowntext
);
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(unknowntext
);
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(unknowntext
);
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(unknowntext
);
335 return smprintf("%s", host
);
339 /* free the address */
342 return smprintf(unknowntext
);
347 load_avg(const char *null
)
351 /* try to get load avg */
352 if (getloadavg(avgs
, 3) < 0) {
353 fprintf(stderr
, "Error getting load avg.\n");
354 return smprintf(unknowntext
);
358 return smprintf("%.2f %.2f %.2f", avgs
[0], avgs
[1], avgs
[2]);
363 ram_free(const char *null
)
368 /* open meminfo file */
369 if (!(fp
= fopen("/proc/meminfo", "r"))) {
370 fprintf(stderr
, "Error opening meminfo file.\n");
371 return smprintf(unknowntext
);
374 /* read the values */
375 fscanf(fp
, "MemFree: %ld kB\n", &free
);
377 /* close meminfo file */
380 /* return free ram as string */
381 return smprintf("%f", (float)free
/ 1024 / 1024);
386 ram_perc(const char *null
)
389 long total
, free
, buffers
, cached
;
392 /* open meminfo file */
393 if (!(fp
= fopen("/proc/meminfo", "r"))) {
394 fprintf(stderr
, "Error opening meminfo file.\n");
395 return smprintf(unknowntext
);
398 /* read the values */
399 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
400 fscanf(fp
, "MemFree: %ld kB\n", &free
);
401 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
402 fscanf(fp
, "Cached: %ld kB\n", &cached
);
404 /* close meminfo file */
407 /* calculate percentage */
408 perc
= 100 * ((total
- free
) - (buffers
+ cached
)) / total
;
410 /* return perc as string */
411 return smprintf("%d%%", perc
);
416 ram_total(const char *null
)
421 /* open meminfo file */
422 if (!(fp
= fopen("/proc/meminfo", "r"))) {
423 fprintf(stderr
, "Error opening meminfo file.\n");
424 return smprintf(unknowntext
);
427 /* read the values */
428 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
430 /* close meminfo file */
433 /* return total ram as string */
434 return smprintf("%f", (float)total
/ 1024 / 1024);
439 ram_used(const char *null
)
441 long free
, total
, buffers
, cached
, used
;
444 /* open meminfo file */
445 if (!(fp
= fopen("/proc/meminfo", "r"))) {
446 fprintf(stderr
, "Error opening meminfo file.\n");
447 return smprintf(unknowntext
);
450 /* read the values */
451 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
452 fscanf(fp
, "MemFree: %ld kB\n", &free
);
453 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
454 fscanf(fp
, "Cached: %ld kB\n", &cached
);
456 /* close meminfo file */
460 used
= total
- free
- buffers
- cached
;
462 /* return used ram as string */
463 return smprintf("%f", (float)used
/ 1024 / 1024);
466 /* custom shell command */
468 run_command(const char* command
)
474 /* try to open the command output */
475 if (!(fp
= popen(command
, "r"))) {
476 fprintf(stderr
, "Could not get command output for: %s.\n", command
);
477 return smprintf(unknowntext
);
480 /* get command output text, save it to buffer */
481 fgets(buffer
, sizeof(buffer
) - 1, fp
);
486 /* add nullchar at the end */
487 for (int i
= 0 ; i
!= sizeof(buffer
); i
++) {
488 if (buffer
[i
] == '\0') {
494 buffer
[strlen(buffer
) - 1] = '\0';
497 /* return the output */
498 return smprintf("%s", buffer
);
503 temp(const char *file
)
508 /* open temperature file */
509 if (!(fp
= fopen(file
, "r"))) {
510 fprintf(stderr
, "Could not open temperature file.\n");
511 return smprintf(unknowntext
);
514 /* extract temperature */
515 fscanf(fp
, "%d", &temperature
);
517 /* close temperature file */
520 /* return temperature in degrees */
521 return smprintf("%d°C", temperature
/ 1000);
526 username(const char *null
)
528 register struct passwd
*pw
;
535 /* if it worked, return */
537 return smprintf("%s", pw
->pw_name
);
539 fprintf(stderr
, "Could not get username.\n");
540 return smprintf(unknowntext
);
543 return smprintf(unknowntext
);
548 uid(const char *null
)
555 /* if it worked, return */
557 return smprintf("%d", uid
);
559 fprintf(stderr
, "Could not get uid.\n");
560 return smprintf(unknowntext
);
563 return smprintf(unknowntext
);
567 /* alsa volume percentage */
569 vol_perc(const char *soundcard
)
572 long vol
= 0, max
= 0, min
= 0;
574 snd_mixer_elem_t
*pcm_mixer
, *mas_mixer
;
575 snd_mixer_selem_id_t
*vol_info
, *mute_info
;
577 /* open everything */
578 snd_mixer_open(&handle
, 0);
579 snd_mixer_attach(handle
, soundcard
);
580 snd_mixer_selem_register(handle
, NULL
, NULL
);
581 snd_mixer_load(handle
);
583 /* prepare everything */
584 snd_mixer_selem_id_malloc(&vol_info
);
585 snd_mixer_selem_id_malloc(&mute_info
);
587 if (vol_info
== NULL
|| mute_info
== NULL
) {
588 fprintf(stderr
, "Could not get alsa volume.\n");
589 return smprintf(unknowntext
);
591 snd_mixer_selem_id_set_name(vol_info
, channel
);
592 snd_mixer_selem_id_set_name(mute_info
, channel
);
593 pcm_mixer
= snd_mixer_find_selem(handle
, vol_info
);
594 mas_mixer
= snd_mixer_find_selem(handle
, mute_info
);
597 snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t
*)pcm_mixer
, &min
, &max
);
598 snd_mixer_selem_get_playback_volume((snd_mixer_elem_t
*)pcm_mixer
, SND_MIXER_SCHN_MONO
, &vol
);
599 snd_mixer_selem_get_playback_switch(mas_mixer
, SND_MIXER_SCHN_MONO
, &mute
);
603 snd_mixer_selem_id_free(vol_info
);
606 snd_mixer_selem_id_free(mute_info
);
609 snd_mixer_close(handle
);
612 /* return the string (mute) */
614 return smprintf("mute");
616 return smprintf("%d%%", (vol
* 100) / max
);
620 /* wifi percentage */
622 wifi_perc(const char *wificard
)
630 char needle
[sizeof wificard
+ 1];
633 /* generate the path name */
634 memset(path
, 0, sizeof path
);
635 strcat(path
, "/sys/class/net/");
636 strcat(path
, wificard
);
637 strcat(path
, "/operstate");
640 if(!(fp
= fopen(path
, "r"))) {
641 fprintf(stderr
, "Error opening wifi operstate file.\n");
642 return smprintf(unknowntext
);
645 /* read the status */
646 fgets(status
, 5, fp
);
648 /* close wifi file */
651 /* check if interface down */
652 if(strcmp(status
, "up\n") != 0) {
653 return smprintf(unknowntext
);
657 if (!(fp
= fopen("/proc/net/wireless", "r"))) {
658 fprintf(stderr
, "Error opening wireless file.\n");
659 return smprintf(unknowntext
);
662 /* extract the signal strength */
663 strcpy(needle
, wificard
);
665 fgets(buf
, bufsize
, fp
);
666 fgets(buf
, bufsize
, fp
);
667 fgets(buf
, bufsize
, fp
);
668 if ((datastart
= strstr(buf
, needle
)) != NULL
) {
669 datastart
= strstr(buf
, ":");
670 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength
);
673 /* close wifi file */
676 /* return strength in percent */
677 return smprintf("%d%%", strength
);
682 wifi_essid(const char *wificard
)
684 char id
[IW_ESSID_MAX_SIZE
+1];
689 memset(&wreq
, 0, sizeof(struct iwreq
));
690 wreq
.u
.essid
.length
= IW_ESSID_MAX_SIZE
+1;
692 /* set the interface */
693 sprintf(wreq
.ifr_name
, wificard
);
696 if((sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1) {
697 fprintf(stderr
, "Cannot open socket for interface: %s\n", wificard
);
698 return smprintf(unknowntext
);
700 wreq
.u
.essid
.pointer
= id
;
701 if (ioctl(sockfd
,SIOCGIWESSID
, &wreq
) == -1) {
702 fprintf(stderr
, "Get ESSID ioctl failed for interface %s\n", wificard
);
703 return smprintf(unknowntext
);
706 /* return the essid */
707 if (strcmp((char *)wreq
.u
.essid
.pointer
, "") == 0) {
708 return smprintf(unknowntext
);
710 return smprintf("%s", (char *)wreq
.u
.essid
.pointer
);
718 char status_string
[1024];
721 /* try to open display */
722 if (!(dpy
= XOpenDisplay(0x0))) {
723 fprintf(stderr
, "Cannot open display!\n");
727 /* return status every interval */
729 /* clear the string */
730 memset(status_string
, 0, sizeof(status_string
));
732 /* generate status_string */
733 for (size_t i
= 0; i
< sizeof(args
) / sizeof(args
[0]); ++i
) {
735 char *res
= argument
.func(argument
.args
);
736 char *element
= smprintf(argument
.format
, res
);
737 if (element
== NULL
) {
738 element
= smprintf(unknowntext
);
739 fprintf(stderr
, "Failed to format output.\n");
741 strcat(status_string
, element
);
746 /* return the statusbar */
747 setstatus(status_string
);
749 /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
750 sleep(update_interval
-1);
756 /* exit successfully */