Xinqi Bao's Git
404ead6a03b77364e3805901c36516fa6ec29f5b
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
);
169 /* disk usage percentage */
171 disk_perc(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");
182 /* calculate percent */
183 perc
= 100 * (1.0f
- ((float)fs
.f_bavail
/ (float)fs
.f_blocks
));
186 return smprintf("%d%%", perc
);
189 /* entropy available */
191 entropy(const char *null
)
196 /* open entropy file */
197 if (!(fp
= fopen("/proc/sys/kernel/random/entropy_avail", "r"))) {
198 fprintf(stderr
, "Could not open entropy file.\n");
199 return smprintf("n/a");
202 /* extract entropy */
203 fscanf(fp
, "%d", &entropy
);
205 /* close entropy file */
209 return smprintf("%d", entropy
);
214 hostname(const char *null
)
216 char hostname
[HOST_NAME_MAX
];
219 /* open hostname file */
220 if (!(fp
= fopen("/proc/sys/kernel/hostname", "r"))) {
221 fprintf(stderr
, "Could not open hostname file.\n");
222 return smprintf("n/a");
225 /* extract hostname */
226 fscanf(fp
, "%s\n", hostname
);
228 /* close hostname file */
232 return smprintf("%s", hostname
);
237 ip(const char *interface
)
239 struct ifaddrs
*ifaddr
, *ifa
;
241 char host
[NI_MAXHOST
];
243 /* check if getting ip address works */
244 if (getifaddrs(&ifaddr
) == -1)
246 fprintf(stderr
, "Error getting IP address.");
247 return smprintf("n/a");
250 /* get the ip address */
251 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
)
253 if (ifa
->ifa_addr
== NULL
)
256 s
= getnameinfo(ifa
->ifa_addr
, sizeof(struct sockaddr_in
), host
, NI_MAXHOST
, NULL
, 0, NI_NUMERICHOST
);
258 if ((strcmp(ifa
->ifa_name
, interface
) == 0) && (ifa
->ifa_addr
->sa_family
== AF_INET
))
262 fprintf(stderr
, "Error getting IP address.");
263 return smprintf("n/a");
265 return smprintf("%s", host
);
269 /* free the address */
272 /* return n/a if nothing works */
273 return smprintf("n/a");
278 ram_perc(const char *null
)
281 long total
, free
, buffers
, cached
;
284 /* open meminfo file */
285 if (!(fp
= fopen("/proc/meminfo", "r"))) {
286 fprintf(stderr
, "Error opening meminfo file.");
287 return smprintf("n/a");
290 /* read the values */
291 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
292 fscanf(fp
, "MemFree: %ld kB\n", &free
);
293 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
294 fscanf(fp
, "Cached: %ld kB\n", &cached
);
296 /* close meminfo file */
299 /* calculate percentage */
300 perc
= 100 * ((total
- free
) - (buffers
+ cached
)) / total
;
302 /* return perc as string */
303 return smprintf("%d%%", perc
);
308 temp(const char *file
)
313 /* open temperature file */
314 if (!(fp
= fopen(file
, "r"))) {
315 fprintf(stderr
, "Could not open temperature file.\n");
316 return smprintf("n/a");
319 /* extract temperature */
320 fscanf(fp
, "%d", &temperature
);
322 /* close temperature file */
325 /* return temperature in degrees */
326 return smprintf("%d°C", temperature
/ 1000);
329 /* alsa volume percentage */
331 vol_perc(const char *soundcard
)
334 long vol
= 0, max
= 0, min
= 0;
336 /* get volume from alsa */
338 snd_mixer_elem_t
*pcm_mixer
, *mas_mixer
;
339 snd_mixer_selem_id_t
*vol_info
, *mute_info
;
340 snd_mixer_open(&handle
, 0);
341 snd_mixer_attach(handle
, soundcard
);
342 snd_mixer_selem_register(handle
, NULL
, NULL
);
343 snd_mixer_load(handle
);
344 snd_mixer_selem_id_malloc(&vol_info
);
345 snd_mixer_selem_id_malloc(&mute_info
);
346 snd_mixer_selem_id_set_name(vol_info
, channel
);
347 snd_mixer_selem_id_set_name(mute_info
, channel
);
348 pcm_mixer
= snd_mixer_find_selem(handle
, vol_info
);
349 mas_mixer
= snd_mixer_find_selem(handle
, mute_info
);
350 snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t
*)pcm_mixer
, &min
, &max
);
351 snd_mixer_selem_get_playback_volume((snd_mixer_elem_t
*)pcm_mixer
, SND_MIXER_SCHN_MONO
, &vol
);
352 snd_mixer_selem_get_playback_switch(mas_mixer
, SND_MIXER_SCHN_MONO
, &mute
);
354 snd_mixer_selem_id_free(vol_info
);
356 snd_mixer_selem_id_free(mute_info
);
358 snd_mixer_close(handle
);
360 /* return the string (mute) */
362 return smprintf("mute");
364 return smprintf("%d%%", (vol
* 100) / max
);
367 /* wifi percentage */
369 wifi_perc(const char *wificard
)
377 char needle
[sizeof wificard
+ 1];
380 /* generate the path name */
381 memset(path
, 0, sizeof path
);
382 strcat(path
, "/sys/class/net/");
383 strcat(path
, wificard
);
384 strcat(path
, "/operstate");
387 if(!(fp
= fopen(path
, "r"))) {
388 fprintf(stderr
, "Error opening wifi operstate file.");
389 return smprintf("n/a");
392 /* read the status */
393 fgets(status
, 5, fp
);
395 /* close wifi file */
398 /* check if interface down */
399 if(strcmp(status
, "up\n") != 0){
400 return smprintf("n/a");
404 if (!(fp
= fopen("/proc/net/wireless", "r"))) {
405 fprintf(stderr
, "Error opening wireless file.");
406 return smprintf("n/a");
409 /* extract the signal strength */
410 strcpy(needle
, wificard
);
412 fgets(buf
, bufsize
, fp
);
413 fgets(buf
, bufsize
, fp
);
414 fgets(buf
, bufsize
, fp
);
415 if ((datastart
= strstr(buf
, needle
)) != NULL
) {
416 datastart
= strstr(buf
, ":");
417 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength
);
420 /* close wifi file */
423 /* return strength in percent */
424 return smprintf("%d%%", strength
);
431 char status_string
[1024];
434 /* try to open display */
435 if (!(dpy
= XOpenDisplay(0x0))) {
436 fprintf(stderr
, "Cannot open display!\n");
440 /* return status every interval */
442 /* clear the string */
443 memset(status_string
, 0, sizeof(status_string
));
445 /* generate status_string */
446 for (size_t i
= 0; i
< sizeof(args
) / sizeof(args
[0]); ++i
) {
448 char *res
= argument
.func(argument
.args
);
449 char *element
= smprintf(argument
.format
, res
);
450 strcat(status_string
, element
);
455 /* return the statusbar */
456 setstatus(status_string
);
458 /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
459 sleep(update_interval
-1);
465 /* exit successfully */