Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
4 #include <alsa/asoundlib.h>
11 #include <sys/types.h>
13 #include <sys/statvfs.h>
24 setstatus(const char *str
)
26 /* set WM_NAME via X11 */
27 XStoreName(dpy
, DefaultRootWindow(dpy
), str
);
31 /* smprintf function */
33 smprintf(const char *fmt
, ...)
38 va_start(fmtargs
, fmt
);
39 if (vasprintf(&ret
, fmt
, fmtargs
) < 0)
46 /* battery percentage */
48 battery_perc(const char *battery
)
51 char batterynowfile
[64] = "";
52 char batteryfullfile
[64] = "";
55 /* generate battery nowfile path */
56 strcat(batterynowfile
, batterypath
);
57 strcat(batterynowfile
, battery
);
58 strcat(batterynowfile
, "/");
59 strcat(batterynowfile
, batterynow
);
61 /* generate battery fullfile path */
62 strcat(batteryfullfile
, batterypath
);
63 strcat(batteryfullfile
, battery
);
64 strcat(batteryfullfile
, "/");
65 strcat(batteryfullfile
, batteryfull
);
67 /* open battery now file */
68 if (!(fp
= fopen(batterynowfile
, "r"))) {
69 fprintf(stderr
, "Error opening battery file.%s",batterynowfile
);
70 return smprintf("n/a");
74 fscanf(fp
, "%i", &now
);
76 /* close battery now file */
79 /* open battery full file */
80 if (!(fp
= fopen(batteryfullfile
, "r"))) {
81 fprintf(stderr
, "Error opening battery file.");
82 return smprintf("n/a");
86 fscanf(fp
, "%i", &full
);
88 /* close battery full file */
91 /* calculate percent */
92 perc
= now
/ (full
/ 100);
94 /* return perc as string */
95 return smprintf("%d%%", perc
);
100 cpu_perc(const char *null
)
103 long double a
[4], b
[4];
107 if (!(fp
= fopen("/proc/stat","r"))) {
108 fprintf(stderr
, "Error opening stat file.");
109 return smprintf("n/a");
113 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &a
[0], &a
[1], &a
[2], &a
[3]);
115 /* close stat file */
118 /* wait a second (for avg values) */
122 if (!(fp
= fopen("/proc/stat","r"))) {
123 fprintf(stderr
, "Error opening stat file.");
124 return smprintf("n/a");
128 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &b
[0], &b
[1], &b
[2], &b
[3]);
130 /* close stat file */
133 /* calculate avg in this second */
134 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]));
136 /* return perc as string */
137 return smprintf("%d%%", perc
);
142 datetime(const char *timeformat
)
146 char *buf
= malloc(bufsize
);
148 /* get time in format */
150 setlocale(LC_TIME
, "");
151 if(!strftime(buf
, bufsize
, timeformat
, localtime(&tm
))) {
152 setlocale(LC_TIME
, "C");
153 fprintf(stderr
, "Strftime failed.\n");
154 return smprintf("n/a");
157 setlocale(LC_TIME
, "C");
159 char *ret
= smprintf("%s", buf
);
164 /* disk usage percentage */
166 disk_perc(const char *mountpoint
)
171 /* try to open mountpoint */
172 if (statvfs(mountpoint
, &fs
) < 0) {
173 fprintf(stderr
, "Could not get filesystem info.\n");
174 return smprintf("n/a");
177 /* calculate percent */
178 perc
= 100 * (1.0f
- ((float)fs
.f_bavail
/ (float)fs
.f_blocks
));
181 return smprintf("%d%%", perc
);
184 /* entropy available */
186 entropy(const char *null
)
191 /* open entropy file */
192 if (!(fp
= fopen("/proc/sys/kernel/random/entropy_avail", "r"))) {
193 fprintf(stderr
, "Could not open entropy file.\n");
194 return smprintf("n/a");
197 /* extract entropy */
198 fscanf(fp
, "%d", &entropy
);
200 /* close entropy file */
204 return smprintf("%d", entropy
);
209 ram_perc(const char *null
)
212 long total
, free
, buffers
, cached
;
215 /* open meminfo file */
216 if (!(fp
= fopen("/proc/meminfo", "r"))) {
217 fprintf(stderr
, "Error opening meminfo file.");
218 return smprintf("n/a");
221 /* read the values */
222 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
223 fscanf(fp
, "MemFree: %ld kB\n", &free
);
224 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
225 fscanf(fp
, "Cached: %ld kB\n", &cached
);
227 /* close meminfo file */
230 /* calculate percentage */
231 perc
= 100 * ((total
- free
) - (buffers
+ cached
)) / total
;
233 /* return perc as string */
234 return smprintf("%d%%", perc
);
239 temp(const char *file
)
244 /* open temperature file */
245 if (!(fp
= fopen(file
, "r"))) {
246 fprintf(stderr
, "Could not open temperature file.\n");
247 return smprintf("n/a");
250 /* extract temperature */
251 fscanf(fp
, "%d", &temperature
);
253 /* close temperature file */
256 /* return temperature in degrees */
257 return smprintf("%d°C", temperature
/ 1000);
261 /* alsa volume percentage */
263 vol_perc(const char *soundcard
)
266 long vol
= 0, max
= 0, min
= 0;
268 /* get volume from alsa */
270 snd_mixer_elem_t
*pcm_mixer
, *mas_mixer
;
271 snd_mixer_selem_id_t
*vol_info
, *mute_info
;
272 snd_mixer_open(&handle
, 0);
273 snd_mixer_attach(handle
, soundcard
);
274 snd_mixer_selem_register(handle
, NULL
, NULL
);
275 snd_mixer_load(handle
);
276 snd_mixer_selem_id_malloc(&vol_info
);
277 snd_mixer_selem_id_malloc(&mute_info
);
278 snd_mixer_selem_id_set_name(vol_info
, channel
);
279 snd_mixer_selem_id_set_name(mute_info
, channel
);
280 pcm_mixer
= snd_mixer_find_selem(handle
, vol_info
);
281 mas_mixer
= snd_mixer_find_selem(handle
, mute_info
);
282 snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t
*)pcm_mixer
, &min
, &max
);
283 snd_mixer_selem_get_playback_volume((snd_mixer_elem_t
*)pcm_mixer
, SND_MIXER_SCHN_MONO
, &vol
);
284 snd_mixer_selem_get_playback_switch(mas_mixer
, SND_MIXER_SCHN_MONO
, &mute
);
286 snd_mixer_selem_id_free(vol_info
);
288 snd_mixer_selem_id_free(mute_info
);
290 snd_mixer_close(handle
);
292 /* return the string (mute) */
294 return smprintf("mute");
296 return smprintf("%d%%", (vol
* 100) / max
);
299 /* wifi percentage */
301 wifi_perc(const char *wificard
)
309 char needle
[sizeof wificard
+ 1];
312 /* generate the path name */
313 memset(path
, 0, sizeof path
);
314 strcat(path
, "/sys/class/net/");
315 strcat(path
, wificard
);
316 strcat(path
, "/operstate");
319 if(!(fp
= fopen(path
, "r"))) {
320 fprintf(stderr
, "Error opening wifi operstate file.");
321 return smprintf("n/a");
324 /* read the status */
325 fgets(status
, 5, fp
);
327 /* close wifi file */
330 /* check if interface down */
331 if(strcmp(status
, "up\n") != 0){
332 return smprintf("n/a");
336 if (!(fp
= fopen("/proc/net/wireless", "r"))) {
337 fprintf(stderr
, "Error opening wireless file.");
338 return smprintf("n/a");
341 /* extract the signal strength */
342 strcpy(needle
, wificard
);
344 fgets(buf
, bufsize
, fp
);
345 fgets(buf
, bufsize
, fp
);
346 fgets(buf
, bufsize
, fp
);
347 if ((datastart
= strstr(buf
, needle
)) != NULL
) {
348 datastart
= strstr(buf
, ":");
349 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength
);
352 /* close wifi file */
355 /* return strength in percent */
356 return smprintf("%d%%", strength
);
363 char status_string
[1024];
366 /* try to open display */
367 if (!(dpy
= XOpenDisplay(0x0))) {
368 fprintf(stderr
, "Cannot open display!\n");
372 /* return status every interval */
374 /* clear the string */
375 memset(status_string
, 0, sizeof(status_string
));
377 /* generate status_string */
378 for (size_t i
= 0; i
< sizeof(args
) / sizeof(args
[0]); ++i
) {
380 char *res
= argument
.func(argument
.args
);
381 char *element
= smprintf(argument
.format
, res
);
382 strcat(status_string
, element
);
387 /* return the statusbar */
388 setstatus(status_string
);
390 /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
391 sleep(update_interval
-1);
397 /* exit successfully */