slstatus.c 9.0 KB

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