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>
21 /* check file macro */
22 #define CHECK_FILE(X,Y) do { \
23 if (stat(X,&Y) < 0) return -1; \
24 if (!S_ISREG(Y.st_mode)) return -1; \
29 void setstatus(char *str
);
30 char *smprintf(char *fmt
, ...);
32 char *get_cpu_temperature();
33 char *get_cpu_usage();
35 char *get_diskusage();
36 char *get_ram_usage();
38 char *get_wifi_signal();
40 /* global variables */
43 /* check configured paths */
49 /* check all files in the config.h file */
50 CHECK_FILE(batterynowfile
, fs
);
51 CHECK_FILE(batteryfullfile
, fs
);
52 CHECK_FILE(tempfile
, fs
);
54 /* check update interval */
55 if (update_interval
< 1)
58 /* exit successfully */
62 /* set statusbar (WM_NAME) */
66 XStoreName(dpy
, DefaultRootWindow(dpy
), str
);
70 /* smprintf function */
72 smprintf(char *fmt
, ...)
76 va_start(fmtargs
, fmt
);
77 if (vasprintf(&ret
, fmt
, fmtargs
) < 0)
84 /* battery percentage */
91 /* open battery now file */
92 if (!(fp
= fopen(batterynowfile
, "r"))) {
93 fprintf(stderr
, "Error opening battery file.");
94 return smprintf("n/a");
98 fscanf(fp
, "%i", &now
);
100 /* close battery now file */
103 /* open battery full file */
104 if (!(fp
= fopen(batteryfullfile
, "r"))) {
105 fprintf(stderr
, "Error opening battery file.");
106 return smprintf("n/a");
110 fscanf(fp
, "%i", &full
);
112 /* close battery full file */
115 /* calculate percent */
116 perc
= now
/ (full
/ 100);
118 /* return perc as string */
119 return smprintf("%d%%", perc
);
122 /* cpu temperature */
124 get_cpu_temperature()
129 /* open temperature file */
130 if (!(fp
= fopen(tempfile
, "r"))) {
131 fprintf(stderr
, "Could not open temperature file.\n");
132 return smprintf("n/a");
135 /* extract temperature */
136 fscanf(fp
, "%d", &temperature
);
138 /* close temperature file */
141 /* return temperature in degrees */
142 return smprintf("%d°C", temperature
/ 1000);
150 long double a
[4], b
[4];
154 if (!(fp
= fopen("/proc/stat","r"))) {
155 fprintf(stderr
, "Error opening stat file.");
156 return smprintf("n/a");
160 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &a
[0], &a
[1], &a
[2], &a
[3]);
162 /* close stat file */
165 /* wait a second (for avg values) */
169 if (!(fp
= fopen("/proc/stat","r"))) {
170 fprintf(stderr
, "Error opening stat file.");
171 return smprintf("n/a");
175 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &b
[0], &b
[1], &b
[2], &b
[3]);
177 /* close stat file */
180 /* calculate avg in this second */
181 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]));
183 /* return perc as string */
184 return smprintf("%d%%", perc
);
193 char *buf
= malloc(bufsize
);
195 /* get time in format */
197 setlocale(LC_TIME
, "");
198 if(!strftime(buf
, bufsize
, timeformat
, localtime(&tm
))) {
199 setlocale(LC_TIME
, "C");
200 fprintf(stderr
, "Strftime failed.\n");
201 return smprintf("n/a");
204 setlocale(LC_TIME
, "C");
206 return smprintf("%s", buf
);
209 /* disk usage percentage */
215 if (statvfs(mountpath
, &fs
) < 0) {
216 fprintf(stderr
, "Could not get filesystem info.\n");
217 return smprintf("n/a");
219 perc
= 1.0f
- ((float)fs
.f_bavail
/(float)fs
.f_blocks
);
220 return smprintf("%2f%%", perc
);
228 long total
, free
, buffers
, cached
;
231 /* open meminfo file */
232 if (!(fp
= fopen("/proc/meminfo", "r"))) {
233 fprintf(stderr
, "Error opening meminfo file.");
234 return smprintf("n/a");
237 /* read the values */
238 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
239 fscanf(fp
, "MemFree: %ld kB\n", &free
);
240 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
241 fscanf(fp
, "Cached: %ld kB\n", &cached
);
243 /* close meminfo file */
246 /* calculate percentage */
247 perc
= 100 * ((total
- free
) - (buffers
+ cached
)) / total
;
249 /* return perc as string */
250 return smprintf("%d%%", perc
);
253 /* alsa volume percentage */
258 long vol
= 0, max
= 0, min
= 0;
260 /* get volume from alsa */
262 snd_mixer_elem_t
*pcm_mixer
, *mas_mixer
;
263 snd_mixer_selem_id_t
*vol_info
, *mute_info
;
264 snd_mixer_open(&handle
, 0);
265 snd_mixer_attach(handle
, soundcard
);
266 snd_mixer_selem_register(handle
, NULL
, NULL
);
267 snd_mixer_load(handle
);
268 snd_mixer_selem_id_malloc(&vol_info
);
269 snd_mixer_selem_id_malloc(&mute_info
);
270 snd_mixer_selem_id_set_name(vol_info
, channel
);
271 snd_mixer_selem_id_set_name(mute_info
, channel
);
272 pcm_mixer
= snd_mixer_find_selem(handle
, vol_info
);
273 mas_mixer
= snd_mixer_find_selem(handle
, mute_info
);
274 snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t
*)pcm_mixer
, &min
, &max
);
275 snd_mixer_selem_get_playback_volume((snd_mixer_elem_t
*)pcm_mixer
, SND_MIXER_SCHN_MONO
, &vol
);
276 snd_mixer_selem_get_playback_switch(mas_mixer
, SND_MIXER_SCHN_MONO
, &mute
);
278 snd_mixer_selem_id_free(vol_info
);
280 snd_mixer_selem_id_free(mute_info
);
282 snd_mixer_close(handle
);
284 /* return the string (mute) */
286 return smprintf("mute");
288 return smprintf("%d%%", (vol
* 100) / max
);
291 /* wifi percentage */
299 char path_start
[16] = "/sys/class/net/";
300 char path_end
[11] = "/operstate";
303 char needle
[sizeof wificard
+ 1];
306 /* generate the path name */
307 memset(path
, 0, sizeof path
);
308 strcat(path
, path_start
);
309 strcat(path
, wificard
);
310 strcat(path
, path_end
);
313 if(!(fp
= fopen(path
, "r"))) {
314 fprintf(stderr
, "Error opening wifi operstate file.");
315 return smprintf("n/a");
318 /* read the status */
319 fgets(status
, 5, fp
);
321 /* close wifi file */
324 /* check if interface down */
325 if(strcmp(status
, "up\n") != 0){
326 return smprintf("n/a");
330 if (!(fp
= fopen("/proc/net/wireless", "r"))) {
331 fprintf(stderr
, "Error opening wireless file.");
332 return smprintf("n/a");
335 /* extract the signal strength */
336 strcpy(needle
, wificard
);
338 fgets(buf
, bufsize
, fp
);
339 fgets(buf
, bufsize
, fp
);
340 fgets(buf
, bufsize
, fp
);
341 if ((datastart
= strstr(buf
, needle
)) != NULL
) {
342 datastart
= strstr(buf
, ":");
343 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength
);
346 /* close wifi file */
349 /* return strength in percent */
350 return smprintf("%d%%", strength
);
358 char *battery
= NULL
;
359 char *cpu_temperature
= NULL
;
360 char *cpu_usage
= NULL
;
361 char *datetime
= NULL
;
362 char *diskusage
= NULL
;
363 char *ram_usage
= NULL
;
365 char *wifi_signal
= NULL
;
367 /* check config for sanity */
368 if (config_check() < 0) {
369 fprintf(stderr
, "Config error, please check paths and interval and recompile!\n");
374 if (!(dpy
= XOpenDisplay(0x0))) {
375 fprintf(stderr
, "Cannot open display!\n");
379 /* return status every second */
381 /* assign the values */
382 battery
= get_battery();
383 cpu_temperature
= get_cpu_temperature();
384 cpu_usage
= get_cpu_usage();
385 datetime
= get_datetime();
386 diskusage
= get_diskusage();
387 ram_usage
= get_ram_usage();
388 volume
= get_volume();
389 wifi_signal
= get_wifi_signal();
391 /* return the status */
392 sprintf(status
, FORMATSTRING
, ARGUMENTS
);
395 /* free the values */
397 free(cpu_temperature
);
405 /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
406 sleep(update_interval
-1);
412 /* exit successfully */