Xinqi Bao's Git

Merge pull request #9 from sahne/master
[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 struct statvfs fs;
214 float perc = 0;
215 if (statvfs(mountpath, &fs) < 0) {
216 fprintf(stderr, "Could not get filesystem info.\n");
217 return smprintf("n/a");
218 }
219 perc = 1.0f - ((float)fs.f_bavail/(float)fs.f_blocks);
220 return smprintf("%2f%%", perc);
221 }
222
223 /* ram percentage */
224 char *
225 get_ram_usage()
226 {
227 int perc;
228 long total, free, buffers, cached;
229 FILE *fp;
230
231 /* open meminfo file */
232 if (!(fp = fopen("/proc/meminfo", "r"))) {
233 fprintf(stderr, "Error opening meminfo file.");
234 return smprintf("n/a");
235 }
236
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);
242
243 /* close meminfo file */
244 fclose(fp);
245
246 /* calculate percentage */
247 perc = 100 * ((total - free) - (buffers + cached)) / total;
248
249 /* return perc as string */
250 return smprintf("%d%%", perc);
251 }
252
253 /* alsa volume percentage */
254 char *
255 get_volume()
256 {
257 int mute = 0;
258 long vol = 0, max = 0, min = 0;
259
260 /* get volume from alsa */
261 snd_mixer_t *handle;
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);
277 if (vol_info)
278 snd_mixer_selem_id_free(vol_info);
279 if (mute_info)
280 snd_mixer_selem_id_free(mute_info);
281 if (handle)
282 snd_mixer_close(handle);
283
284 /* return the string (mute) */
285 if (!mute)
286 return smprintf("mute");
287 else
288 return smprintf("%d%%", (vol * 100) / max);
289 }
290
291 /* wifi percentage */
292 char *
293 get_wifi_signal()
294 {
295 int bufsize = 255;
296 int strength;
297 char buf[bufsize];
298 char *datastart;
299 char path_start[16] = "/sys/class/net/";
300 char path_end[11] = "/operstate";
301 char path[32];
302 char status[5];
303 char needle[sizeof wificard + 1];
304 FILE *fp;
305
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);
311
312 /* open wifi file */
313 if(!(fp = fopen(path, "r"))) {
314 fprintf(stderr, "Error opening wifi operstate file.");
315 return smprintf("n/a");
316 }
317
318 /* read the status */
319 fgets(status, 5, fp);
320
321 /* close wifi file */
322 fclose(fp);
323
324 /* check if interface down */
325 if(strcmp(status, "up\n") != 0){
326 return smprintf("n/a");
327 }
328
329 /* open wifi file */
330 if (!(fp = fopen("/proc/net/wireless", "r"))) {
331 fprintf(stderr, "Error opening wireless file.");
332 return smprintf("n/a");
333 }
334
335 /* extract the signal strength */
336 strcpy(needle, wificard);
337 strcat(needle, ":");
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);
344 }
345
346 /* close wifi file */
347 fclose(fp);
348
349 /* return strength in percent */
350 return smprintf("%d%%", strength);
351 }
352
353 /* main function */
354 int
355 main()
356 {
357 char status[1024];
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;
364 char *volume = NULL;
365 char *wifi_signal = NULL;
366
367 /* check config for sanity */
368 if (config_check() < 0) {
369 fprintf(stderr, "Config error, please check paths and interval and recompile!\n");
370 exit(1);
371 }
372
373 /* open display */
374 if (!(dpy = XOpenDisplay(0x0))) {
375 fprintf(stderr, "Cannot open display!\n");
376 exit(1);
377 }
378
379 /* return status every second */
380 for (;;) {
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();
390
391 /* return the status */
392 sprintf(status, FORMATSTRING, ARGUMENTS);
393 setstatus(status);
394
395 /* free the values */
396 free(battery);
397 free(cpu_temperature);
398 free(cpu_usage);
399 free(datetime);
400 free(diskusage);
401 free(ram_usage);
402 free(volume);
403 free(wifi_signal);
404
405 /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
406 sleep(update_interval -1);
407 }
408
409 /* close display */
410 XCloseDisplay(dpy);
411
412 /* exit successfully */
413 return 0;
414 }