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 /* get time in format */
156 setlocale(LC_TIME
, "");
157 if(!strftime(buf
, bufsize
, timeformat
, localtime(&tm
))) {
158 setlocale(LC_TIME
, "C");
159 fprintf(stderr
, "Strftime failed.\n");
160 return smprintf("n/a");
163 setlocale(LC_TIME
, "C");
165 char *ret
= smprintf("%s", buf
);
172 disk_free(const char *mountpoint
)
176 /* try to open mountpoint */
177 if (statvfs(mountpoint
, &fs
) < 0) {
178 fprintf(stderr
, "Could not get filesystem info.\n");
179 return smprintf("n/a");
183 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_bfree
/ 1024 / 1024 / 1024);
186 /* disk usage percentage */
188 disk_perc(const char *mountpoint
)
193 /* try to open mountpoint */
194 if (statvfs(mountpoint
, &fs
) < 0) {
195 fprintf(stderr
, "Could not get filesystem info.\n");
196 return smprintf("n/a");
199 /* calculate percent */
200 perc
= 100 * (1.0f
- ((float)fs
.f_bfree
/ (float)fs
.f_blocks
));
203 return smprintf("%d%%", perc
);
208 disk_total(const char *mountpoint
)
212 /* try to open mountpoint */
213 if (statvfs(mountpoint
, &fs
) < 0) {
214 fprintf(stderr
, "Could not get filesystem info.\n");
215 return smprintf("n/a");
219 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_blocks
/ 1024 / 1024 / 1024);
224 disk_used(const char *mountpoint
)
228 /* try to open mountpoint */
229 if (statvfs(mountpoint
, &fs
) < 0) {
230 fprintf(stderr
, "Could not get filesystem info.\n");
231 return smprintf("n/a");
235 return smprintf("%f", (float)fs
.f_bsize
* ((float)fs
.f_blocks
- (float)fs
.f_bfree
) / 1024 / 1024 / 1024);
238 /* entropy available */
240 entropy(const char *null
)
245 /* open entropy file */
246 if (!(fp
= fopen("/proc/sys/kernel/random/entropy_avail", "r"))) {
247 fprintf(stderr
, "Could not open entropy file.\n");
248 return smprintf("n/a");
251 /* extract entropy */
252 fscanf(fp
, "%d", &entropy
);
254 /* close entropy file */
258 return smprintf("%d", entropy
);
263 gid(const char *null
)
267 if ((gid
= getgid()) < 0) {
268 fprintf(stderr
, "Could no get gid.");
269 return smprintf("n/a");
271 return smprintf("%d", gid
);
274 return smprintf("n/a");
279 hostname(const char *null
)
281 char hostname
[HOST_NAME_MAX
];
284 /* open hostname file */
285 if (!(fp
= fopen("/proc/sys/kernel/hostname", "r"))) {
286 fprintf(stderr
, "Could not open hostname file.\n");
287 return smprintf("n/a");
290 /* extract hostname */
291 fscanf(fp
, "%s\n", hostname
);
293 /* close hostname file */
297 return smprintf("%s", hostname
);
302 ip(const char *interface
)
304 struct ifaddrs
*ifaddr
, *ifa
;
306 char host
[NI_MAXHOST
];
308 /* check if getting ip address works */
309 if (getifaddrs(&ifaddr
) == -1)
311 fprintf(stderr
, "Error getting IP address.");
312 return smprintf("n/a");
315 /* get the ip address */
316 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
)
318 if (ifa
->ifa_addr
== NULL
)
321 s
= getnameinfo(ifa
->ifa_addr
, sizeof(struct sockaddr_in
), host
, NI_MAXHOST
, NULL
, 0, NI_NUMERICHOST
);
323 if ((strcmp(ifa
->ifa_name
, interface
) == 0) && (ifa
->ifa_addr
->sa_family
== AF_INET
))
327 fprintf(stderr
, "Error getting IP address.");
328 return smprintf("n/a");
330 return smprintf("%s", host
);
334 /* free the address */
337 /* return n/a if nothing works */
338 return smprintf("n/a");
343 ram_free(const char *null
)
348 /* open meminfo file */
349 if (!(fp
= fopen("/proc/meminfo", "r"))) {
350 fprintf(stderr
, "Error opening meminfo file.");
351 return smprintf("n/a");
354 /* read the values */
355 fscanf(fp
, "MemTotal: %*d kB\n");
356 fscanf(fp
, "MemFree: %ld kB\n", &free
);
358 /* close meminfo file */
361 /* return free ram as string */
362 return smprintf("%f", (float)free
/ 1024 / 1024);
367 ram_perc(const char *null
)
370 long total
, free
, buffers
, cached
;
373 /* open meminfo file */
374 if (!(fp
= fopen("/proc/meminfo", "r"))) {
375 fprintf(stderr
, "Error opening meminfo file.");
376 return smprintf("n/a");
379 /* read the values */
380 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
381 fscanf(fp
, "MemFree: %ld kB\n", &free
);
382 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
383 fscanf(fp
, "Cached: %ld kB\n", &cached
);
385 /* close meminfo file */
388 /* calculate percentage */
389 perc
= 100 * ((total
- free
) - (buffers
+ cached
)) / total
;
391 /* return perc as string */
392 return smprintf("%d%%", perc
);
397 ram_total(const char *null
)
402 /* open meminfo file */
403 if (!(fp
= fopen("/proc/meminfo", "r"))) {
404 fprintf(stderr
, "Error opening meminfo file.");
405 return smprintf("n/a");
408 /* read the values */
409 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
411 /* close meminfo file */
414 /* return total ram as string */
415 return smprintf("%f", (float)total
/ 1024 / 1024);
420 ram_used(const char *null
)
422 long free
, total
, buffers
, cached
, used
;
425 /* open meminfo file */
426 if (!(fp
= fopen("/proc/meminfo", "r"))) {
427 fprintf(stderr
, "Error opening meminfo file.");
428 return smprintf("n/a");
431 /* read the values */
432 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
433 fscanf(fp
, "MemFree: %ld kB\n", &free
);
434 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
435 fscanf(fp
, "Cached: %ld kB\n", &cached
);
437 /* close meminfo file */
441 used
= total
- free
- buffers
- cached
;
443 /* return used ram as string */
444 return smprintf("%f", (float)used
/ 1024 / 1024);
449 temp(const char *file
)
454 /* open temperature file */
455 if (!(fp
= fopen(file
, "r"))) {
456 fprintf(stderr
, "Could not open temperature file.\n");
457 return smprintf("n/a");
460 /* extract temperature */
461 fscanf(fp
, "%d", &temperature
);
463 /* close temperature file */
466 /* return temperature in degrees */
467 return smprintf("%d°C", temperature
/ 1000);
472 username(const char *null
)
474 register struct passwd
*pw
;
481 /* if it worked, return */
483 return smprintf("%s", pw
->pw_name
);
486 fprintf(stderr
, "Could not get username.\n");
487 return smprintf("n/a");
490 return smprintf("n/a");
495 uid(const char *null
)
502 /* if it worked, return */
504 return smprintf("%d", uid
);
507 fprintf(stderr
, "Could not get uid.\n");
508 return smprintf("n/a");
511 return smprintf("n/a");
515 /* alsa volume percentage */
517 vol_perc(const char *soundcard
)
520 long vol
= 0, max
= 0, min
= 0;
522 /* get volume from alsa */
524 snd_mixer_elem_t
*pcm_mixer
, *mas_mixer
;
525 snd_mixer_selem_id_t
*vol_info
, *mute_info
;
526 snd_mixer_open(&handle
, 0);
527 snd_mixer_attach(handle
, soundcard
);
528 snd_mixer_selem_register(handle
, NULL
, NULL
);
529 snd_mixer_load(handle
);
530 snd_mixer_selem_id_malloc(&vol_info
);
531 snd_mixer_selem_id_malloc(&mute_info
);
532 snd_mixer_selem_id_set_name(vol_info
, channel
);
533 snd_mixer_selem_id_set_name(mute_info
, channel
);
534 pcm_mixer
= snd_mixer_find_selem(handle
, vol_info
);
535 mas_mixer
= snd_mixer_find_selem(handle
, mute_info
);
536 snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t
*)pcm_mixer
, &min
, &max
);
537 snd_mixer_selem_get_playback_volume((snd_mixer_elem_t
*)pcm_mixer
, SND_MIXER_SCHN_MONO
, &vol
);
538 snd_mixer_selem_get_playback_switch(mas_mixer
, SND_MIXER_SCHN_MONO
, &mute
);
540 snd_mixer_selem_id_free(vol_info
);
542 snd_mixer_selem_id_free(mute_info
);
544 snd_mixer_close(handle
);
546 /* return the string (mute) */
548 return smprintf("mute");
550 return smprintf("%d%%", (vol
* 100) / max
);
553 /* wifi percentage */
555 wifi_perc(const char *wificard
)
563 char needle
[sizeof wificard
+ 1];
566 /* generate the path name */
567 memset(path
, 0, sizeof path
);
568 strcat(path
, "/sys/class/net/");
569 strcat(path
, wificard
);
570 strcat(path
, "/operstate");
573 if(!(fp
= fopen(path
, "r"))) {
574 fprintf(stderr
, "Error opening wifi operstate file.");
575 return smprintf("n/a");
578 /* read the status */
579 fgets(status
, 5, fp
);
581 /* close wifi file */
584 /* check if interface down */
585 if(strcmp(status
, "up\n") != 0){
586 return smprintf("n/a");
590 if (!(fp
= fopen("/proc/net/wireless", "r"))) {
591 fprintf(stderr
, "Error opening wireless file.");
592 return smprintf("n/a");
595 /* extract the signal strength */
596 strcpy(needle
, wificard
);
598 fgets(buf
, bufsize
, fp
);
599 fgets(buf
, bufsize
, fp
);
600 fgets(buf
, bufsize
, fp
);
601 if ((datastart
= strstr(buf
, needle
)) != NULL
) {
602 datastart
= strstr(buf
, ":");
603 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength
);
606 /* close wifi file */
609 /* return strength in percent */
610 return smprintf("%d%%", strength
);
617 char status_string
[1024];
620 /* try to open display */
621 if (!(dpy
= XOpenDisplay(0x0))) {
622 fprintf(stderr
, "Cannot open display!\n");
626 /* return status every interval */
628 /* clear the string */
629 memset(status_string
, 0, sizeof(status_string
));
631 /* generate status_string */
632 for (size_t i
= 0; i
< sizeof(args
) / sizeof(args
[0]); ++i
) {
634 char *res
= argument
.func(argument
.args
);
635 char *element
= smprintf(argument
.format
, res
);
636 strcat(status_string
, element
);
641 /* return the statusbar */
642 setstatus(status_string
);
644 /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
645 sleep(update_interval
-1);
651 /* exit successfully */