Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
4 #include <alsa/asoundlib.h>
15 #include <sys/types.h>
17 #include <sys/statvfs.h>
18 #include <sys/socket.h>
29 setstatus(const char *str
)
31 /* set WM_NAME via X11 */
32 XStoreName(dpy
, DefaultRootWindow(dpy
), str
);
36 /* smprintf function */
38 smprintf(const char *fmt
, ...)
43 va_start(fmtargs
, fmt
);
44 if (vasprintf(&ret
, fmt
, fmtargs
) < 0)
51 /* battery percentage */
53 battery_perc(const char *battery
)
56 char batterynowfile
[64] = "";
57 char batteryfullfile
[64] = "";
60 /* generate battery nowfile path */
61 strcat(batterynowfile
, batterypath
);
62 strcat(batterynowfile
, battery
);
63 strcat(batterynowfile
, "/");
64 strcat(batterynowfile
, batterynow
);
66 /* generate battery fullfile path */
67 strcat(batteryfullfile
, batterypath
);
68 strcat(batteryfullfile
, battery
);
69 strcat(batteryfullfile
, "/");
70 strcat(batteryfullfile
, batteryfull
);
72 /* open battery now file */
73 if (!(fp
= fopen(batterynowfile
, "r"))) {
74 fprintf(stderr
, "Error opening battery file.%s",batterynowfile
);
75 return smprintf("n/a");
79 fscanf(fp
, "%i", &now
);
81 /* close battery now file */
84 /* open battery full file */
85 if (!(fp
= fopen(batteryfullfile
, "r"))) {
86 fprintf(stderr
, "Error opening battery file.");
87 return smprintf("n/a");
91 fscanf(fp
, "%i", &full
);
93 /* close battery full file */
96 /* calculate percent */
97 perc
= now
/ (full
/ 100);
99 /* return perc as string */
100 return smprintf("%d%%", perc
);
105 cpu_perc(const char *null
)
108 long double a
[4], b
[4];
112 if (!(fp
= fopen("/proc/stat","r"))) {
113 fprintf(stderr
, "Error opening stat file.");
114 return smprintf("n/a");
118 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &a
[0], &a
[1], &a
[2], &a
[3]);
120 /* close stat file */
123 /* wait a second (for avg values) */
127 if (!(fp
= fopen("/proc/stat","r"))) {
128 fprintf(stderr
, "Error opening stat file.");
129 return smprintf("n/a");
133 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &b
[0], &b
[1], &b
[2], &b
[3]);
135 /* close stat file */
138 /* calculate avg in this second */
139 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]));
141 /* return perc as string */
142 return smprintf("%d%%", perc
);
147 datetime(const char *timeformat
)
151 char *buf
= malloc(bufsize
);
153 /* get time in format */
155 setlocale(LC_TIME
, "");
156 if(!strftime(buf
, bufsize
, timeformat
, localtime(&tm
))) {
157 setlocale(LC_TIME
, "C");
158 fprintf(stderr
, "Strftime failed.\n");
159 return smprintf("n/a");
162 setlocale(LC_TIME
, "C");
164 char *ret
= smprintf("%s", buf
);
171 disk_free(const char *mountpoint
)
175 /* try to open mountpoint */
176 if (statvfs(mountpoint
, &fs
) < 0) {
177 fprintf(stderr
, "Could not get filesystem info.\n");
178 return smprintf("n/a");
182 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_bfree
/ 1024 / 1024 / 1024);
185 /* disk usage percentage */
187 disk_perc(const char *mountpoint
)
192 /* try to open mountpoint */
193 if (statvfs(mountpoint
, &fs
) < 0) {
194 fprintf(stderr
, "Could not get filesystem info.\n");
195 return smprintf("n/a");
198 /* calculate percent */
199 perc
= 100 * (1.0f
- ((float)fs
.f_bfree
/ (float)fs
.f_blocks
));
202 return smprintf("%d%%", perc
);
207 disk_total(const char *mountpoint
)
211 /* try to open mountpoint */
212 if (statvfs(mountpoint
, &fs
) < 0) {
213 fprintf(stderr
, "Could not get filesystem info.\n");
214 return smprintf("n/a");
218 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_blocks
/ 1024 / 1024 / 1024);
223 disk_used(const char *mountpoint
)
227 /* try to open mountpoint */
228 if (statvfs(mountpoint
, &fs
) < 0) {
229 fprintf(stderr
, "Could not get filesystem info.\n");
230 return smprintf("n/a");
234 return smprintf("%f", (float)fs
.f_bsize
* ((float)fs
.f_blocks
- (float)fs
.f_bfree
) / 1024 / 1024 / 1024);
237 /* entropy available */
239 entropy(const char *null
)
244 /* open entropy file */
245 if (!(fp
= fopen("/proc/sys/kernel/random/entropy_avail", "r"))) {
246 fprintf(stderr
, "Could not open entropy file.\n");
247 return smprintf("n/a");
250 /* extract entropy */
251 fscanf(fp
, "%d", &entropy
);
253 /* close entropy file */
257 return smprintf("%d", entropy
);
262 hostname(const char *null
)
264 char hostname
[HOST_NAME_MAX
];
267 /* open hostname file */
268 if (!(fp
= fopen("/proc/sys/kernel/hostname", "r"))) {
269 fprintf(stderr
, "Could not open hostname file.\n");
270 return smprintf("n/a");
273 /* extract hostname */
274 fscanf(fp
, "%s\n", hostname
);
276 /* close hostname file */
280 return smprintf("%s", hostname
);
285 ip(const char *interface
)
287 struct ifaddrs
*ifaddr
, *ifa
;
289 char host
[NI_MAXHOST
];
291 /* check if getting ip address works */
292 if (getifaddrs(&ifaddr
) == -1)
294 fprintf(stderr
, "Error getting IP address.");
295 return smprintf("n/a");
298 /* get the ip address */
299 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
)
301 if (ifa
->ifa_addr
== NULL
)
304 s
= getnameinfo(ifa
->ifa_addr
, sizeof(struct sockaddr_in
), host
, NI_MAXHOST
, NULL
, 0, NI_NUMERICHOST
);
306 if ((strcmp(ifa
->ifa_name
, interface
) == 0) && (ifa
->ifa_addr
->sa_family
== AF_INET
))
310 fprintf(stderr
, "Error getting IP address.");
311 return smprintf("n/a");
313 return smprintf("%s", host
);
317 /* free the address */
320 /* return n/a if nothing works */
321 return smprintf("n/a");
326 ram_free(const char *null
)
331 /* open meminfo file */
332 if (!(fp
= fopen("/proc/meminfo", "r"))) {
333 fprintf(stderr
, "Error opening meminfo file.");
334 return smprintf("n/a");
337 /* read the values */
338 fscanf(fp
, "MemTotal: %*d kB\n");
339 fscanf(fp
, "MemFree: %ld kB\n", &free
);
341 /* close meminfo file */
344 /* return free ram as string */
345 return smprintf("%f", (float)free
/ 1024 / 1024);
350 ram_perc(const char *null
)
353 long total
, free
, buffers
, cached
;
356 /* open meminfo file */
357 if (!(fp
= fopen("/proc/meminfo", "r"))) {
358 fprintf(stderr
, "Error opening meminfo file.");
359 return smprintf("n/a");
362 /* read the values */
363 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
364 fscanf(fp
, "MemFree: %ld kB\n", &free
);
365 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
366 fscanf(fp
, "Cached: %ld kB\n", &cached
);
368 /* close meminfo file */
371 /* calculate percentage */
372 perc
= 100 * ((total
- free
) - (buffers
+ cached
)) / total
;
374 /* return perc as string */
375 return smprintf("%d%%", perc
);
380 ram_total(const char *null
)
385 /* open meminfo file */
386 if (!(fp
= fopen("/proc/meminfo", "r"))) {
387 fprintf(stderr
, "Error opening meminfo file.");
388 return smprintf("n/a");
391 /* read the values */
392 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
394 /* close meminfo file */
397 /* return total ram as string */
398 return smprintf("%f", (float)total
/ 1024 / 1024);
403 ram_used(const char *null
)
405 long free
, total
, buffers
, cached
, used
;
408 /* open meminfo file */
409 if (!(fp
= fopen("/proc/meminfo", "r"))) {
410 fprintf(stderr
, "Error opening meminfo file.");
411 return smprintf("n/a");
414 /* read the values */
415 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
416 fscanf(fp
, "MemFree: %ld kB\n", &free
);
417 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
418 fscanf(fp
, "Cached: %ld kB\n", &cached
);
420 /* close meminfo file */
424 used
= total
- free
- buffers
- cached
;
426 /* return used ram as string */
427 return smprintf("%f", (float)used
/ 1024 / 1024);
432 temp(const char *file
)
437 /* open temperature file */
438 if (!(fp
= fopen(file
, "r"))) {
439 fprintf(stderr
, "Could not open temperature file.\n");
440 return smprintf("n/a");
443 /* extract temperature */
444 fscanf(fp
, "%d", &temperature
);
446 /* close temperature file */
449 /* return temperature in degrees */
450 return smprintf("%d°C", temperature
/ 1000);
453 /* alsa volume percentage */
455 vol_perc(const char *soundcard
)
458 long vol
= 0, max
= 0, min
= 0;
460 /* get volume from alsa */
462 snd_mixer_elem_t
*pcm_mixer
, *mas_mixer
;
463 snd_mixer_selem_id_t
*vol_info
, *mute_info
;
464 snd_mixer_open(&handle
, 0);
465 snd_mixer_attach(handle
, soundcard
);
466 snd_mixer_selem_register(handle
, NULL
, NULL
);
467 snd_mixer_load(handle
);
468 snd_mixer_selem_id_malloc(&vol_info
);
469 snd_mixer_selem_id_malloc(&mute_info
);
470 snd_mixer_selem_id_set_name(vol_info
, channel
);
471 snd_mixer_selem_id_set_name(mute_info
, channel
);
472 pcm_mixer
= snd_mixer_find_selem(handle
, vol_info
);
473 mas_mixer
= snd_mixer_find_selem(handle
, mute_info
);
474 snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t
*)pcm_mixer
, &min
, &max
);
475 snd_mixer_selem_get_playback_volume((snd_mixer_elem_t
*)pcm_mixer
, SND_MIXER_SCHN_MONO
, &vol
);
476 snd_mixer_selem_get_playback_switch(mas_mixer
, SND_MIXER_SCHN_MONO
, &mute
);
478 snd_mixer_selem_id_free(vol_info
);
480 snd_mixer_selem_id_free(mute_info
);
482 snd_mixer_close(handle
);
484 /* return the string (mute) */
486 return smprintf("mute");
488 return smprintf("%d%%", (vol
* 100) / max
);
491 /* wifi percentage */
493 wifi_perc(const char *wificard
)
501 char needle
[sizeof wificard
+ 1];
504 /* generate the path name */
505 memset(path
, 0, sizeof path
);
506 strcat(path
, "/sys/class/net/");
507 strcat(path
, wificard
);
508 strcat(path
, "/operstate");
511 if(!(fp
= fopen(path
, "r"))) {
512 fprintf(stderr
, "Error opening wifi operstate file.");
513 return smprintf("n/a");
516 /* read the status */
517 fgets(status
, 5, fp
);
519 /* close wifi file */
522 /* check if interface down */
523 if(strcmp(status
, "up\n") != 0){
524 return smprintf("n/a");
528 if (!(fp
= fopen("/proc/net/wireless", "r"))) {
529 fprintf(stderr
, "Error opening wireless file.");
530 return smprintf("n/a");
533 /* extract the signal strength */
534 strcpy(needle
, wificard
);
536 fgets(buf
, bufsize
, fp
);
537 fgets(buf
, bufsize
, fp
);
538 fgets(buf
, bufsize
, fp
);
539 if ((datastart
= strstr(buf
, needle
)) != NULL
) {
540 datastart
= strstr(buf
, ":");
541 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength
);
544 /* close wifi file */
547 /* return strength in percent */
548 return smprintf("%d%%", strength
);
555 char status_string
[1024];
558 /* try to open display */
559 if (!(dpy
= XOpenDisplay(0x0))) {
560 fprintf(stderr
, "Cannot open display!\n");
564 /* return status every interval */
566 /* clear the string */
567 memset(status_string
, 0, sizeof(status_string
));
569 /* generate status_string */
570 for (size_t i
= 0; i
< sizeof(args
) / sizeof(args
[0]); ++i
) {
572 char *res
= argument
.func(argument
.args
);
573 char *element
= smprintf(argument
.format
, res
);
574 strcat(status_string
, element
);
579 /* return the statusbar */
580 setstatus(status_string
);
582 /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
583 sleep(update_interval
-1);
589 /* exit successfully */