slstatus.c 10 KB

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