Xinqi Bao's Git

fixed disk percent
[slstatus.git] / slstatus.c
1 /* See LICENSE file for copyright and license details. */
2
3 /* global libraries */
4 #include <alsa/asoundlib.h>
5 #include <fcntl.h>
6 #include <locale.h>
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/statvfs.h>
14 #include <time.h>
15 #include <unistd.h>
16 #include <X11/Xlib.h>
17
18 /* local libraries */
19 #include "config.h"
20
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; \
25 } while (0);
26
27 /* functions */
28 int config_check();
29 void setstatus(char *str);
30 char *smprintf(char *fmt, ...);
31 char *get_battery();
32 char *get_cpu_temperature();
33 char *get_cpu_usage();
34 char *get_datetime();
35 char *get_diskusage();
36 char *get_ram_usage();
37 char *get_volume();
38 char *get_wifi_signal();
39
40 /* global variables */
41 static Display *dpy;
42
43 /* check configured paths */
44 int
45 config_check()
46 {
47 struct stat fs;
48
49 /* check all files in the config.h file */
50 CHECK_FILE(batterynowfile, fs);
51 CHECK_FILE(batteryfullfile, fs);
52 CHECK_FILE(tempfile, fs);
53
54 /* check update interval */
55 if (update_interval < 1)
56 return -1;
57
58 /* exit successfully */
59 return 0;
60 }
61
62 /* set statusbar (WM_NAME) */
63 void
64 setstatus(char *str)
65 {
66 XStoreName(dpy, DefaultRootWindow(dpy), str);
67 XSync(dpy, False);
68 }
69
70 /* smprintf function */
71 char *
72 smprintf(char *fmt, ...)
73 {
74 va_list fmtargs;
75 char *ret = NULL;
76 va_start(fmtargs, fmt);
77 if (vasprintf(&ret, fmt, fmtargs) < 0)
78 return NULL;
79 va_end(fmtargs);
80
81 return ret;
82 }
83
84 /* battery percentage */
85 char *
86 get_battery()
87 {
88 int now, full, perc;
89 FILE *fp;
90
91 /* open battery now file */
92 if (!(fp = fopen(batterynowfile, "r"))) {
93 fprintf(stderr, "Error opening battery file.");
94 return smprintf("n/a");
95 }
96
97 /* read value */
98 fscanf(fp, "%i", &now);
99
100 /* close battery now file */
101 fclose(fp);
102
103 /* open battery full file */
104 if (!(fp = fopen(batteryfullfile, "r"))) {
105 fprintf(stderr, "Error opening battery file.");
106 return smprintf("n/a");
107 }
108
109 /* read value */
110 fscanf(fp, "%i", &full);
111
112 /* close battery full file */
113 fclose(fp);
114
115 /* calculate percent */
116 perc = now / (full / 100);
117
118 /* return perc as string */
119 return smprintf("%d%%", perc);
120 }
121
122 /* cpu temperature */
123 char *
124 get_cpu_temperature()
125 {
126 int temperature;
127 FILE *fp;
128
129 /* open temperature file */
130 if (!(fp = fopen(tempfile, "r"))) {
131 fprintf(stderr, "Could not open temperature file.\n");
132 return smprintf("n/a");
133 }
134
135 /* extract temperature */
136 fscanf(fp, "%d", &temperature);
137
138 /* close temperature file */
139 fclose(fp);
140
141 /* return temperature in degrees */
142 return smprintf("%d°C", temperature / 1000);
143 }
144
145 /* cpu percentage */
146 char *
147 get_cpu_usage()
148 {
149 int perc;
150 long double a[4], b[4];
151 FILE *fp;
152
153 /* open stat file */
154 if (!(fp = fopen("/proc/stat","r"))) {
155 fprintf(stderr, "Error opening stat file.");
156 return smprintf("n/a");
157 }
158
159 /* read values */
160 fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
161
162 /* close stat file */
163 fclose(fp);
164
165 /* wait a second (for avg values) */
166 sleep(1);
167
168 /* open stat file */
169 if (!(fp = fopen("/proc/stat","r"))) {
170 fprintf(stderr, "Error opening stat file.");
171 return smprintf("n/a");
172 }
173
174 /* read values */
175 fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
176
177 /* close stat file */
178 fclose(fp);
179
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]));
182
183 /* return perc as string */
184 return smprintf("%d%%", perc);
185 }
186
187 /* date and time */
188 char *
189 get_datetime()
190 {
191 time_t tm;
192 size_t bufsize = 64;
193 char *buf = malloc(bufsize);
194
195 /* get time in format */
196 time(&tm);
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");
202 }
203
204 setlocale(LC_TIME, "C");
205 /* return time */
206 return smprintf("%s", buf);
207 }
208
209 /* disk usage percentage */
210 char *
211 get_diskusage()
212 {
213 int perc = 0;
214 struct statvfs fs;
215
216 /* try to open mountpoint */
217 if (statvfs(mountpath, &fs) < 0) {
218 fprintf(stderr, "Could not get filesystem info.\n");
219 return smprintf("n/a");
220 }
221
222 /* calculate percent */
223 perc = 100 * (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks));
224
225 /* return perc */
226 return smprintf("%d%%", perc);
227 }
228
229 /* ram percentage */
230 char *
231 get_ram_usage()
232 {
233 int perc;
234 long total, free, buffers, cached;
235 FILE *fp;
236
237 /* open meminfo file */
238 if (!(fp = fopen("/proc/meminfo", "r"))) {
239 fprintf(stderr, "Error opening meminfo file.");
240 return smprintf("n/a");
241 }
242
243 /* read the values */
244 fscanf(fp, "MemTotal: %ld kB\n", &total);
245 fscanf(fp, "MemFree: %ld kB\n", &free);
246 fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
247 fscanf(fp, "Cached: %ld kB\n", &cached);
248
249 /* close meminfo file */
250 fclose(fp);
251
252 /* calculate percentage */
253 perc = 100 * ((total - free) - (buffers + cached)) / total;
254
255 /* return perc as string */
256 return smprintf("%d%%", perc);
257 }
258
259 /* alsa volume percentage */
260 char *
261 get_volume()
262 {
263 int mute = 0;
264 long vol = 0, max = 0, min = 0;
265
266 /* get volume from alsa */
267 snd_mixer_t *handle;
268 snd_mixer_elem_t *pcm_mixer, *mas_mixer;
269 snd_mixer_selem_id_t *vol_info, *mute_info;
270 snd_mixer_open(&handle, 0);
271 snd_mixer_attach(handle, soundcard);
272 snd_mixer_selem_register(handle, NULL, NULL);
273 snd_mixer_load(handle);
274 snd_mixer_selem_id_malloc(&vol_info);
275 snd_mixer_selem_id_malloc(&mute_info);
276 snd_mixer_selem_id_set_name(vol_info, channel);
277 snd_mixer_selem_id_set_name(mute_info, channel);
278 pcm_mixer = snd_mixer_find_selem(handle, vol_info);
279 mas_mixer = snd_mixer_find_selem(handle, mute_info);
280 snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max);
281 snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol);
282 snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute);
283 if (vol_info)
284 snd_mixer_selem_id_free(vol_info);
285 if (mute_info)
286 snd_mixer_selem_id_free(mute_info);
287 if (handle)
288 snd_mixer_close(handle);
289
290 /* return the string (mute) */
291 if (!mute)
292 return smprintf("mute");
293 else
294 return smprintf("%d%%", (vol * 100) / max);
295 }
296
297 /* wifi percentage */
298 char *
299 get_wifi_signal()
300 {
301 int bufsize = 255;
302 int strength;
303 char buf[bufsize];
304 char *datastart;
305 char path_start[16] = "/sys/class/net/";
306 char path_end[11] = "/operstate";
307 char path[32];
308 char status[5];
309 char needle[sizeof wificard + 1];
310 FILE *fp;
311
312 /* generate the path name */
313 memset(path, 0, sizeof path);
314 strcat(path, path_start);
315 strcat(path, wificard);
316 strcat(path, path_end);
317
318 /* open wifi file */
319 if(!(fp = fopen(path, "r"))) {
320 fprintf(stderr, "Error opening wifi operstate file.");
321 return smprintf("n/a");
322 }
323
324 /* read the status */
325 fgets(status, 5, fp);
326
327 /* close wifi file */
328 fclose(fp);
329
330 /* check if interface down */
331 if(strcmp(status, "up\n") != 0){
332 return smprintf("n/a");
333 }
334
335 /* open wifi file */
336 if (!(fp = fopen("/proc/net/wireless", "r"))) {
337 fprintf(stderr, "Error opening wireless file.");
338 return smprintf("n/a");
339 }
340
341 /* extract the signal strength */
342 strcpy(needle, wificard);
343 strcat(needle, ":");
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);
350 }
351
352 /* close wifi file */
353 fclose(fp);
354
355 /* return strength in percent */
356 return smprintf("%d%%", strength);
357 }
358
359 /* main function */
360 int
361 main()
362 {
363 char status[1024];
364 char *battery = NULL;
365 char *cpu_temperature = NULL;
366 char *cpu_usage = NULL;
367 char *datetime = NULL;
368 char *diskusage = NULL;
369 char *ram_usage = NULL;
370 char *volume = NULL;
371 char *wifi_signal = NULL;
372
373 /* check config for sanity */
374 if (config_check() < 0) {
375 fprintf(stderr, "Config error, please check paths and interval and recompile!\n");
376 exit(1);
377 }
378
379 /* open display */
380 if (!(dpy = XOpenDisplay(0x0))) {
381 fprintf(stderr, "Cannot open display!\n");
382 exit(1);
383 }
384
385 /* return status every second */
386 for (;;) {
387 /* assign the values */
388 battery = get_battery();
389 cpu_temperature = get_cpu_temperature();
390 cpu_usage = get_cpu_usage();
391 datetime = get_datetime();
392 diskusage = get_diskusage();
393 ram_usage = get_ram_usage();
394 volume = get_volume();
395 wifi_signal = get_wifi_signal();
396
397 /* return the status */
398 sprintf(status, FORMATSTRING, ARGUMENTS);
399 setstatus(status);
400
401 /* free the values */
402 free(battery);
403 free(cpu_temperature);
404 free(cpu_usage);
405 free(datetime);
406 free(diskusage);
407 free(ram_usage);
408 free(volume);
409 free(wifi_signal);
410
411 /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
412 sleep(update_interval -1);
413 }
414
415 /* close display */
416 XCloseDisplay(dpy);
417
418 /* exit successfully */
419 return 0;
420 }