Xinqi Bao's Git
505f25d7dab88203c36caf2505fae75e8824e246
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)
54 /* battery percentage */
56 battery_perc(const char *battery
)
59 char batterynowfile
[64] = "";
60 char batteryfullfile
[64] = "";
63 /* generate battery nowfile path */
64 strcat(batterynowfile
, batterypath
);
65 strcat(batterynowfile
, battery
);
66 strcat(batterynowfile
, "/");
67 strcat(batterynowfile
, batterynow
);
69 /* generate battery fullfile path */
70 strcat(batteryfullfile
, batterypath
);
71 strcat(batteryfullfile
, battery
);
72 strcat(batteryfullfile
, "/");
73 strcat(batteryfullfile
, batteryfull
);
75 /* open battery now file */
76 if (!(fp
= fopen(batterynowfile
, "r"))) {
77 fprintf(stderr
, "Error opening battery file.%s",batterynowfile
);
78 return smprintf("n/a");
82 fscanf(fp
, "%i", &now
);
84 /* close battery now file */
87 /* open battery full file */
88 if (!(fp
= fopen(batteryfullfile
, "r"))) {
89 fprintf(stderr
, "Error opening battery file.");
90 return smprintf("n/a");
94 fscanf(fp
, "%i", &full
);
96 /* close battery full file */
99 /* calculate percent */
100 perc
= now
/ (full
/ 100);
102 /* return perc as string */
103 return smprintf("%d%%", perc
);
108 cpu_perc(const char *null
)
111 long double a
[4], b
[4];
115 if (!(fp
= fopen("/proc/stat","r"))) {
116 fprintf(stderr
, "Error opening stat file.");
117 return smprintf("n/a");
121 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &a
[0], &a
[1], &a
[2], &a
[3]);
123 /* close stat file */
126 /* wait a second (for avg values) */
130 if (!(fp
= fopen("/proc/stat","r"))) {
131 fprintf(stderr
, "Error opening stat file.");
132 return smprintf("n/a");
136 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &b
[0], &b
[1], &b
[2], &b
[3]);
138 /* close stat file */
141 /* calculate avg in this second */
142 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]));
144 /* return perc as string */
145 return smprintf("%d%%", perc
);
150 datetime(const char *timeformat
)
154 char *buf
= malloc(bufsize
);
156 fprintf(stderr
, "Failed to get date/time");
157 return smprintf("n/a");
160 /* get time in format */
162 setlocale(LC_TIME
, "");
163 if(!strftime(buf
, bufsize
, timeformat
, localtime(&tm
))) {
164 setlocale(LC_TIME
, "C");
166 fprintf(stderr
, "Strftime failed.\n");
167 return smprintf("n/a");
170 setlocale(LC_TIME
, "C");
172 char *ret
= smprintf("%s", buf
);
179 disk_free(const char *mountpoint
)
183 /* try to open mountpoint */
184 if (statvfs(mountpoint
, &fs
) < 0) {
185 fprintf(stderr
, "Could not get filesystem info.\n");
186 return smprintf("n/a");
190 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_bfree
/ 1024 / 1024 / 1024);
193 /* disk usage percentage */
195 disk_perc(const char *mountpoint
)
200 /* try to open mountpoint */
201 if (statvfs(mountpoint
, &fs
) < 0) {
202 fprintf(stderr
, "Could not get filesystem info.\n");
203 return smprintf("n/a");
206 /* calculate percent */
207 perc
= 100 * (1.0f
- ((float)fs
.f_bfree
/ (float)fs
.f_blocks
));
210 return smprintf("%d%%", perc
);
215 disk_total(const char *mountpoint
)
219 /* try to open mountpoint */
220 if (statvfs(mountpoint
, &fs
) < 0) {
221 fprintf(stderr
, "Could not get filesystem info.\n");
222 return smprintf("n/a");
226 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_blocks
/ 1024 / 1024 / 1024);
231 disk_used(const char *mountpoint
)
235 /* try to open mountpoint */
236 if (statvfs(mountpoint
, &fs
) < 0) {
237 fprintf(stderr
, "Could not get filesystem info.\n");
238 return smprintf("n/a");
242 return smprintf("%f", (float)fs
.f_bsize
* ((float)fs
.f_blocks
- (float)fs
.f_bfree
) / 1024 / 1024 / 1024);
245 /* entropy available */
247 entropy(const char *null
)
252 /* open entropy file */
253 if (!(fp
= fopen("/proc/sys/kernel/random/entropy_avail", "r"))) {
254 fprintf(stderr
, "Could not open entropy file.\n");
255 return smprintf("n/a");
258 /* extract entropy */
259 fscanf(fp
, "%d", &entropy
);
261 /* close entropy file */
265 return smprintf("%d", entropy
);
270 gid(const char *null
)
274 if ((gid
= getgid()) < 0) {
275 fprintf(stderr
, "Could no get gid.");
276 return smprintf("n/a");
278 return smprintf("%d", gid
);
281 return smprintf("n/a");
286 hostname(const char *null
)
288 char hostname
[HOST_NAME_MAX
];
291 /* open hostname file */
292 if (!(fp
= fopen("/proc/sys/kernel/hostname", "r"))) {
293 fprintf(stderr
, "Could not open hostname file.\n");
294 return smprintf("n/a");
297 /* extract hostname */
298 fscanf(fp
, "%s\n", hostname
);
300 /* close hostname file */
304 return smprintf("%s", hostname
);
309 ip(const char *interface
)
311 struct ifaddrs
*ifaddr
, *ifa
;
313 char host
[NI_MAXHOST
];
315 /* check if getting ip address works */
316 if (getifaddrs(&ifaddr
) == -1)
318 fprintf(stderr
, "Error getting IP address.");
319 return smprintf("n/a");
322 /* get the ip address */
323 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
)
325 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
))
334 fprintf(stderr
, "Error getting IP address.");
335 return smprintf("n/a");
337 return smprintf("%s", host
);
341 /* free the address */
344 /* return n/a if nothing works */
345 return smprintf("n/a");
350 ram_free(const char *null
)
355 /* open meminfo file */
356 if (!(fp
= fopen("/proc/meminfo", "r"))) {
357 fprintf(stderr
, "Error opening meminfo file.");
358 return smprintf("n/a");
361 /* read the values */
362 fscanf(fp
, "MemTotal: %*d kB\n");
363 fscanf(fp
, "MemFree: %ld kB\n", &free
);
365 /* close meminfo file */
368 /* return free ram as string */
369 return smprintf("%f", (float)free
/ 1024 / 1024);
374 ram_perc(const char *null
)
377 long total
, free
, buffers
, cached
;
380 /* open meminfo file */
381 if (!(fp
= fopen("/proc/meminfo", "r"))) {
382 fprintf(stderr
, "Error opening meminfo file.");
383 return smprintf("n/a");
386 /* read the values */
387 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
388 fscanf(fp
, "MemFree: %ld kB\n", &free
);
389 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
390 fscanf(fp
, "Cached: %ld kB\n", &cached
);
392 /* close meminfo file */
395 /* calculate percentage */
396 perc
= 100 * ((total
- free
) - (buffers
+ cached
)) / total
;
398 /* return perc as string */
399 return smprintf("%d%%", perc
);
404 ram_total(const char *null
)
409 /* open meminfo file */
410 if (!(fp
= fopen("/proc/meminfo", "r"))) {
411 fprintf(stderr
, "Error opening meminfo file.");
412 return smprintf("n/a");
415 /* read the values */
416 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
418 /* close meminfo file */
421 /* return total ram as string */
422 return smprintf("%f", (float)total
/ 1024 / 1024);
427 ram_used(const char *null
)
429 long free
, total
, buffers
, cached
, used
;
432 /* open meminfo file */
433 if (!(fp
= fopen("/proc/meminfo", "r"))) {
434 fprintf(stderr
, "Error opening meminfo file.");
435 return smprintf("n/a");
438 /* read the values */
439 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
440 fscanf(fp
, "MemFree: %ld kB\n", &free
);
441 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
442 fscanf(fp
, "Cached: %ld kB\n", &cached
);
444 /* close meminfo file */
448 used
= total
- free
- buffers
- cached
;
450 /* return used ram as string */
451 return smprintf("%f", (float)used
/ 1024 / 1024);
454 /* custom shell command */
456 run_command(const char* command
)
462 /* try to open the command output */
463 if (!(fp
= popen(command
, "r"))) {
464 fprintf(stderr
, "Could not get command output for: %s.\n", command
);
465 return smprintf("n/a");
468 /* get command output text, save it to buffer */
469 fgets(buffer
, sizeof(buffer
)-1, fp
);
474 /* add nullchar at the end */
475 for (int i
= 0 ; i
!= sizeof(buffer
) ; i
++) {
476 if (buffer
[i
] == '\0') {
482 buffer
[strlen(buffer
) - 1] = '\0';
485 /* return the output */
486 return smprintf("%s", buffer
);
491 temp(const char *file
)
496 /* open temperature file */
497 if (!(fp
= fopen(file
, "r"))) {
498 fprintf(stderr
, "Could not open temperature file.\n");
499 return smprintf("n/a");
502 /* extract temperature */
503 fscanf(fp
, "%d", &temperature
);
505 /* close temperature file */
508 /* return temperature in degrees */
509 return smprintf("%d°C", temperature
/ 1000);
514 username(const char *null
)
516 register struct passwd
*pw
;
523 /* if it worked, return */
525 return smprintf("%s", pw
->pw_name
);
528 fprintf(stderr
, "Could not get username.\n");
529 return smprintf("n/a");
532 return smprintf("n/a");
537 uid(const char *null
)
544 /* if it worked, return */
546 return smprintf("%d", uid
);
549 fprintf(stderr
, "Could not get uid.\n");
550 return smprintf("n/a");
553 return smprintf("n/a");
557 /* alsa volume percentage */
559 vol_perc(const char *soundcard
)
562 long vol
= 0, max
= 0, min
= 0;
564 snd_mixer_elem_t
*pcm_mixer
, *mas_mixer
;
565 snd_mixer_selem_id_t
*vol_info
, *mute_info
;
567 /* open everything */
568 snd_mixer_open(&handle
, 0);
569 snd_mixer_attach(handle
, soundcard
);
570 snd_mixer_selem_register(handle
, NULL
, NULL
);
571 snd_mixer_load(handle
);
573 /* prepare everything */
574 snd_mixer_selem_id_malloc(&vol_info
);
575 snd_mixer_selem_id_malloc(&mute_info
);
577 if (vol_info
== NULL
|| mute_info
== NULL
) {
578 fprintf(stderr
, "Could not get alsa volume");
579 return smprintf("n/a");
581 snd_mixer_selem_id_set_name(vol_info
, channel
);
582 snd_mixer_selem_id_set_name(mute_info
, channel
);
583 pcm_mixer
= snd_mixer_find_selem(handle
, vol_info
);
584 mas_mixer
= snd_mixer_find_selem(handle
, mute_info
);
587 snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t
*)pcm_mixer
, &min
, &max
);
588 snd_mixer_selem_get_playback_volume((snd_mixer_elem_t
*)pcm_mixer
, SND_MIXER_SCHN_MONO
, &vol
);
589 snd_mixer_selem_get_playback_switch(mas_mixer
, SND_MIXER_SCHN_MONO
, &mute
);
593 snd_mixer_selem_id_free(vol_info
);
596 snd_mixer_selem_id_free(mute_info
);
599 snd_mixer_close(handle
);
602 /* return the string (mute) */
604 return smprintf("mute");
607 return smprintf("%d%%", (vol
* 100) / max
);
611 /* wifi percentage */
613 wifi_perc(const char *wificard
)
621 char needle
[sizeof wificard
+ 1];
624 /* generate the path name */
625 memset(path
, 0, sizeof path
);
626 strcat(path
, "/sys/class/net/");
627 strcat(path
, wificard
);
628 strcat(path
, "/operstate");
631 if(!(fp
= fopen(path
, "r"))) {
632 fprintf(stderr
, "Error opening wifi operstate file.");
633 return smprintf("n/a");
636 /* read the status */
637 fgets(status
, 5, fp
);
639 /* close wifi file */
642 /* check if interface down */
643 if(strcmp(status
, "up\n") != 0){
644 return smprintf("n/a");
648 if (!(fp
= fopen("/proc/net/wireless", "r"))) {
649 fprintf(stderr
, "Error opening wireless file.");
650 return smprintf("n/a");
653 /* extract the signal strength */
654 strcpy(needle
, wificard
);
656 fgets(buf
, bufsize
, fp
);
657 fgets(buf
, bufsize
, fp
);
658 fgets(buf
, bufsize
, fp
);
659 if ((datastart
= strstr(buf
, needle
)) != NULL
) {
660 datastart
= strstr(buf
, ":");
661 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength
);
664 /* close wifi file */
667 /* return strength in percent */
668 return smprintf("%d%%", strength
);
673 wifi_essid(const char *wificard
)
675 char *id
= malloc(IW_ESSID_MAX_SIZE
+1);
677 fprintf(stderr
, "Cannot get ESSID.");
678 return smprintf("n/a");
684 memset(&wreq
, 0, sizeof(struct iwreq
));
685 wreq
.u
.essid
.length
= IW_ESSID_MAX_SIZE
+1;
687 /* set the interface */
688 sprintf(wreq
.ifr_name
, wificard
);
691 if((sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1) {
692 fprintf(stderr
, "Cannot open socket for interface: %s\n", wificard
);
693 return smprintf("n/a");
695 wreq
.u
.essid
.pointer
= id
;
696 if (ioctl(sockfd
,SIOCGIWESSID
, &wreq
) == -1) {
697 fprintf(stderr
, "Get ESSID ioctl failed for interface %s\n", wificard
);
698 return smprintf("n/a");
701 /* return the essid */
702 if (strcmp((char *)wreq
.u
.essid
.pointer
, "") == 0) {
703 return smprintf("n/a");
706 return smprintf("%s", (char *)wreq
.u
.essid
.pointer
);
714 char status_string
[1024];
717 /* try to open display */
718 if (!(dpy
= XOpenDisplay(0x0))) {
719 fprintf(stderr
, "Cannot open display!\n");
723 /* return status every interval */
725 /* clear the string */
726 memset(status_string
, 0, sizeof(status_string
));
728 /* generate status_string */
729 for (size_t i
= 0; i
< sizeof(args
) / sizeof(args
[0]); ++i
) {
731 char *res
= argument
.func(argument
.args
);
732 char *element
= smprintf(argument
.format
, res
);
733 if (element
== NULL
) {
734 element
= smprintf("n/a");
735 fprintf(stderr
, "Failed to format output.");
737 strcat(status_string
, element
);
742 /* return the statusbar */
743 setstatus(status_string
);
745 /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
746 sleep(update_interval
-1);
752 /* exit successfully */