slstatus.c 8.8 KB

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