slstatus.c 8.7 KB

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