slstatus.c 11 KB

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