Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
4 #include <alsa/asoundlib.h>
14 #include <sys/types.h>
16 #include <sys/statvfs.h>
17 #include <sys/socket.h>
28 setstatus(const char *str
)
30 /* set WM_NAME via X11 */
31 XStoreName(dpy
, DefaultRootWindow(dpy
), str
);
35 /* smprintf function */
37 smprintf(const char *fmt
, ...)
42 va_start(fmtargs
, fmt
);
43 if (vasprintf(&ret
, fmt
, fmtargs
) < 0)
50 /* battery percentage */
52 battery_perc(const char *battery
)
55 char batterynowfile
[64] = "";
56 char batteryfullfile
[64] = "";
59 /* generate battery nowfile path */
60 strcat(batterynowfile
, batterypath
);
61 strcat(batterynowfile
, battery
);
62 strcat(batterynowfile
, "/");
63 strcat(batterynowfile
, batterynow
);
65 /* generate battery fullfile path */
66 strcat(batteryfullfile
, batterypath
);
67 strcat(batteryfullfile
, battery
);
68 strcat(batteryfullfile
, "/");
69 strcat(batteryfullfile
, batteryfull
);
71 /* open battery now file */
72 if (!(fp
= fopen(batterynowfile
, "r"))) {
73 fprintf(stderr
, "Error opening battery file.%s",batterynowfile
);
74 return smprintf("n/a");
78 fscanf(fp
, "%i", &now
);
80 /* close battery now file */
83 /* open battery full file */
84 if (!(fp
= fopen(batteryfullfile
, "r"))) {
85 fprintf(stderr
, "Error opening battery file.");
86 return smprintf("n/a");
90 fscanf(fp
, "%i", &full
);
92 /* close battery full file */
95 /* calculate percent */
96 perc
= now
/ (full
/ 100);
98 /* return perc as string */
99 return smprintf("%d%%", perc
);
104 cpu_perc(const char *null
)
107 long double a
[4], b
[4];
111 if (!(fp
= fopen("/proc/stat","r"))) {
112 fprintf(stderr
, "Error opening stat file.");
113 return smprintf("n/a");
117 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &a
[0], &a
[1], &a
[2], &a
[3]);
119 /* close stat file */
122 /* wait a second (for avg values) */
126 if (!(fp
= fopen("/proc/stat","r"))) {
127 fprintf(stderr
, "Error opening stat file.");
128 return smprintf("n/a");
132 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &b
[0], &b
[1], &b
[2], &b
[3]);
134 /* close stat file */
137 /* calculate avg in this second */
138 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]));
140 /* return perc as string */
141 return smprintf("%d%%", perc
);
146 datetime(const char *timeformat
)
150 char *buf
= malloc(bufsize
);
152 /* get time in format */
154 setlocale(LC_TIME
, "");
155 if(!strftime(buf
, bufsize
, timeformat
, localtime(&tm
))) {
156 setlocale(LC_TIME
, "C");
157 fprintf(stderr
, "Strftime failed.\n");
158 return smprintf("n/a");
161 setlocale(LC_TIME
, "C");
163 char *ret
= smprintf("%s", buf
);
168 /* disk usage percentage */
170 disk_perc(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");
181 /* calculate percent */
182 perc
= 100 * (1.0f
- ((float)fs
.f_bavail
/ (float)fs
.f_blocks
));
185 return smprintf("%d%%", perc
);
188 /* entropy available */
190 entropy(const char *null
)
195 /* open entropy file */
196 if (!(fp
= fopen("/proc/sys/kernel/random/entropy_avail", "r"))) {
197 fprintf(stderr
, "Could not open entropy file.\n");
198 return smprintf("n/a");
201 /* extract entropy */
202 fscanf(fp
, "%d", &entropy
);
204 /* close entropy file */
208 return smprintf("%d", entropy
);
213 hostname(const char *null
)
218 /* open hostname file */
219 if (!(fp
= fopen("/proc/sys/kernel/hostname", "r"))) {
220 fprintf(stderr
, "Could not open hostname file.\n");
221 return smprintf("n/a");
224 /* extract hostname */
225 fscanf(fp
, "%s", hostname
);
227 /* close hostname file */
231 return smprintf("%s", hostname
);
236 ip(const char *interface
)
238 struct ifaddrs
*ifaddr
, *ifa
;
240 char host
[NI_MAXHOST
];
242 /* check if getting ip address works */
243 if (getifaddrs(&ifaddr
) == -1)
245 fprintf(stderr
, "Error getting IP address.");
246 return smprintf("n/a");
249 /* get the ip address */
250 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
)
252 if (ifa
->ifa_addr
== NULL
)
255 s
= getnameinfo(ifa
->ifa_addr
, sizeof(struct sockaddr_in
), host
, NI_MAXHOST
, NULL
, 0, NI_NUMERICHOST
);
257 if ((strcmp(ifa
->ifa_name
, interface
) == 0) && (ifa
->ifa_addr
->sa_family
== AF_INET
))
261 fprintf(stderr
, "Error getting IP address.");
262 return smprintf("n/a");
264 return smprintf("%s", host
);
268 /* free the address */
271 /* return n/a if nothing works */
272 return smprintf("n/a");
277 ram_perc(const char *null
)
280 long total
, free
, buffers
, cached
;
283 /* open meminfo file */
284 if (!(fp
= fopen("/proc/meminfo", "r"))) {
285 fprintf(stderr
, "Error opening meminfo file.");
286 return smprintf("n/a");
289 /* read the values */
290 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
291 fscanf(fp
, "MemFree: %ld kB\n", &free
);
292 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
293 fscanf(fp
, "Cached: %ld kB\n", &cached
);
295 /* close meminfo file */
298 /* calculate percentage */
299 perc
= 100 * ((total
- free
) - (buffers
+ cached
)) / total
;
301 /* return perc as string */
302 return smprintf("%d%%", perc
);
307 temp(const char *file
)
312 /* open temperature file */
313 if (!(fp
= fopen(file
, "r"))) {
314 fprintf(stderr
, "Could not open temperature file.\n");
315 return smprintf("n/a");
318 /* extract temperature */
319 fscanf(fp
, "%d", &temperature
);
321 /* close temperature file */
324 /* return temperature in degrees */
325 return smprintf("%d°C", temperature
/ 1000);
328 /* alsa volume percentage */
330 vol_perc(const char *soundcard
)
333 long vol
= 0, max
= 0, min
= 0;
335 /* get volume from alsa */
337 snd_mixer_elem_t
*pcm_mixer
, *mas_mixer
;
338 snd_mixer_selem_id_t
*vol_info
, *mute_info
;
339 snd_mixer_open(&handle
, 0);
340 snd_mixer_attach(handle
, soundcard
);
341 snd_mixer_selem_register(handle
, NULL
, NULL
);
342 snd_mixer_load(handle
);
343 snd_mixer_selem_id_malloc(&vol_info
);
344 snd_mixer_selem_id_malloc(&mute_info
);
345 snd_mixer_selem_id_set_name(vol_info
, channel
);
346 snd_mixer_selem_id_set_name(mute_info
, channel
);
347 pcm_mixer
= snd_mixer_find_selem(handle
, vol_info
);
348 mas_mixer
= snd_mixer_find_selem(handle
, mute_info
);
349 snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t
*)pcm_mixer
, &min
, &max
);
350 snd_mixer_selem_get_playback_volume((snd_mixer_elem_t
*)pcm_mixer
, SND_MIXER_SCHN_MONO
, &vol
);
351 snd_mixer_selem_get_playback_switch(mas_mixer
, SND_MIXER_SCHN_MONO
, &mute
);
353 snd_mixer_selem_id_free(vol_info
);
355 snd_mixer_selem_id_free(mute_info
);
357 snd_mixer_close(handle
);
359 /* return the string (mute) */
361 return smprintf("mute");
363 return smprintf("%d%%", (vol
* 100) / max
);
366 /* wifi percentage */
368 wifi_perc(const char *wificard
)
376 char needle
[sizeof wificard
+ 1];
379 /* generate the path name */
380 memset(path
, 0, sizeof path
);
381 strcat(path
, "/sys/class/net/");
382 strcat(path
, wificard
);
383 strcat(path
, "/operstate");
386 if(!(fp
= fopen(path
, "r"))) {
387 fprintf(stderr
, "Error opening wifi operstate file.");
388 return smprintf("n/a");
391 /* read the status */
392 fgets(status
, 5, fp
);
394 /* close wifi file */
397 /* check if interface down */
398 if(strcmp(status
, "up\n") != 0){
399 return smprintf("n/a");
403 if (!(fp
= fopen("/proc/net/wireless", "r"))) {
404 fprintf(stderr
, "Error opening wireless file.");
405 return smprintf("n/a");
408 /* extract the signal strength */
409 strcpy(needle
, wificard
);
411 fgets(buf
, bufsize
, fp
);
412 fgets(buf
, bufsize
, fp
);
413 fgets(buf
, bufsize
, fp
);
414 if ((datastart
= strstr(buf
, needle
)) != NULL
) {
415 datastart
= strstr(buf
, ":");
416 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength
);
419 /* close wifi file */
422 /* return strength in percent */
423 return smprintf("%d%%", strength
);
430 char status_string
[1024];
433 /* try to open display */
434 if (!(dpy
= XOpenDisplay(0x0))) {
435 fprintf(stderr
, "Cannot open display!\n");
439 /* return status every interval */
441 /* clear the string */
442 memset(status_string
, 0, sizeof(status_string
));
444 /* generate status_string */
445 for (size_t i
= 0; i
< sizeof(args
) / sizeof(args
[0]); ++i
) {
447 char *res
= argument
.func(argument
.args
);
448 char *element
= smprintf(argument
.format
, res
);
449 strcat(status_string
, element
);
454 /* return the statusbar */
455 setstatus(status_string
);
457 /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
458 sleep(update_interval
-1);
464 /* exit successfully */