slstatus.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 <pwd.h>
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/statvfs.h>
  18. #include <sys/socket.h>
  19. #include <time.h>
  20. #include <unistd.h>
  21. #include <X11/Xlib.h>
  22. /* local headers */
  23. #include "slstatus.h"
  24. #include "config.h"
  25. /* set statusbar */
  26. void
  27. setstatus(const char *str)
  28. {
  29. /* set WM_NAME via X11 */
  30. XStoreName(dpy, DefaultRootWindow(dpy), str);
  31. XSync(dpy, False);
  32. }
  33. /* smprintf function */
  34. char *
  35. smprintf(const char *fmt, ...)
  36. {
  37. va_list fmtargs;
  38. char *ret = NULL;
  39. va_start(fmtargs, fmt);
  40. if (vasprintf(&ret, fmt, fmtargs) < 0)
  41. return NULL;
  42. va_end(fmtargs);
  43. return ret;
  44. }
  45. /* battery percentage */
  46. char *
  47. battery_perc(const char *battery)
  48. {
  49. int now, full, perc;
  50. char batterynowfile[64] = "";
  51. char batteryfullfile[64] = "";
  52. FILE *fp;
  53. /* generate battery nowfile path */
  54. strcat(batterynowfile, batterypath);
  55. strcat(batterynowfile, battery);
  56. strcat(batterynowfile, "/");
  57. strcat(batterynowfile, batterynow);
  58. /* generate battery fullfile path */
  59. strcat(batteryfullfile, batterypath);
  60. strcat(batteryfullfile, battery);
  61. strcat(batteryfullfile, "/");
  62. strcat(batteryfullfile, batteryfull);
  63. /* open battery now file */
  64. if (!(fp = fopen(batterynowfile, "r"))) {
  65. fprintf(stderr, "Error opening battery file.%s",batterynowfile);
  66. return smprintf("n/a");
  67. }
  68. /* read value */
  69. fscanf(fp, "%i", &now);
  70. /* close battery now file */
  71. fclose(fp);
  72. /* open battery full file */
  73. if (!(fp = fopen(batteryfullfile, "r"))) {
  74. fprintf(stderr, "Error opening battery file.");
  75. return smprintf("n/a");
  76. }
  77. /* read value */
  78. fscanf(fp, "%i", &full);
  79. /* close battery full file */
  80. fclose(fp);
  81. /* calculate percent */
  82. perc = now / (full / 100);
  83. /* return perc as string */
  84. return smprintf("%d%%", perc);
  85. }
  86. /* cpu percentage */
  87. char *
  88. cpu_perc(const char *null)
  89. {
  90. int perc;
  91. long double a[4], b[4];
  92. FILE *fp;
  93. /* open stat file */
  94. if (!(fp = fopen("/proc/stat","r"))) {
  95. fprintf(stderr, "Error opening stat file.");
  96. return smprintf("n/a");
  97. }
  98. /* read values */
  99. fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
  100. /* close stat file */
  101. fclose(fp);
  102. /* wait a second (for avg values) */
  103. sleep(1);
  104. /* open stat file */
  105. if (!(fp = fopen("/proc/stat","r"))) {
  106. fprintf(stderr, "Error opening stat file.");
  107. return smprintf("n/a");
  108. }
  109. /* read values */
  110. fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
  111. /* close stat file */
  112. fclose(fp);
  113. /* calculate avg in this second */
  114. 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]));
  115. /* return perc as string */
  116. return smprintf("%d%%", perc);
  117. }
  118. /* date and time */
  119. char *
  120. datetime(const char *timeformat)
  121. {
  122. time_t tm;
  123. size_t bufsize = 64;
  124. char *buf = malloc(bufsize);
  125. /* get time in format */
  126. time(&tm);
  127. setlocale(LC_TIME, "");
  128. if(!strftime(buf, bufsize, timeformat, localtime(&tm))) {
  129. setlocale(LC_TIME, "C");
  130. fprintf(stderr, "Strftime failed.\n");
  131. return smprintf("n/a");
  132. }
  133. setlocale(LC_TIME, "C");
  134. /* return time */
  135. char *ret = smprintf("%s", buf);
  136. free(buf);
  137. return ret;
  138. }
  139. /* disk free */
  140. char *
  141. disk_free(const char *mountpoint)
  142. {
  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. /* return free */
  150. return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
  151. }
  152. /* disk usage percentage */
  153. char *
  154. disk_perc(const char *mountpoint)
  155. {
  156. int perc = 0;
  157. struct statvfs fs;
  158. /* try to open mountpoint */
  159. if (statvfs(mountpoint, &fs) < 0) {
  160. fprintf(stderr, "Could not get filesystem info.\n");
  161. return smprintf("n/a");
  162. }
  163. /* calculate percent */
  164. perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
  165. /* return perc */
  166. return smprintf("%d%%", perc);
  167. }
  168. /* disk total */
  169. char *
  170. disk_total(const char *mountpoint)
  171. {
  172. struct statvfs fs;
  173. /* try to open mountpoint */
  174. if (statvfs(mountpoint, &fs) < 0) {
  175. fprintf(stderr, "Could not get filesystem info.\n");
  176. return smprintf("n/a");
  177. }
  178. /* return total */
  179. return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
  180. }
  181. /* disk used */
  182. char *
  183. disk_used(const char *mountpoint)
  184. {
  185. struct statvfs fs;
  186. /* try to open mountpoint */
  187. if (statvfs(mountpoint, &fs) < 0) {
  188. fprintf(stderr, "Could not get filesystem info.\n");
  189. return smprintf("n/a");
  190. }
  191. /* return used */
  192. return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
  193. }
  194. /* entropy available */
  195. char *
  196. entropy(const char *null)
  197. {
  198. int entropy = 0;
  199. FILE *fp;
  200. /* open entropy file */
  201. if (!(fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"))) {
  202. fprintf(stderr, "Could not open entropy file.\n");
  203. return smprintf("n/a");
  204. }
  205. /* extract entropy */
  206. fscanf(fp, "%d", &entropy);
  207. /* close entropy file */
  208. fclose(fp);
  209. /* return entropy */
  210. return smprintf("%d", entropy);
  211. }
  212. /* gid */
  213. char *
  214. gid(const char *null)
  215. {
  216. gid_t gid;
  217. if ((gid = getgid()) < 0) {
  218. fprintf(stderr, "Could no get gid.");
  219. return smprintf("n/a");
  220. } else {
  221. return smprintf("%d", gid);
  222. }
  223. return smprintf("n/a");
  224. }
  225. /* hostname */
  226. char *
  227. hostname(const char *null)
  228. {
  229. char hostname[HOST_NAME_MAX];
  230. FILE *fp;
  231. /* open hostname file */
  232. if (!(fp = fopen("/proc/sys/kernel/hostname", "r"))) {
  233. fprintf(stderr, "Could not open hostname file.\n");
  234. return smprintf("n/a");
  235. }
  236. /* extract hostname */
  237. fscanf(fp, "%s\n", hostname);
  238. /* close hostname file */
  239. fclose(fp);
  240. /* return entropy */
  241. return smprintf("%s", hostname);
  242. }
  243. /* ip address */
  244. char *
  245. ip(const char *interface)
  246. {
  247. struct ifaddrs *ifaddr, *ifa;
  248. int s;
  249. char host[NI_MAXHOST];
  250. /* check if getting ip address works */
  251. if (getifaddrs(&ifaddr) == -1)
  252. {
  253. fprintf(stderr, "Error getting IP address.");
  254. return smprintf("n/a");
  255. }
  256. /* get the ip address */
  257. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
  258. {
  259. if (ifa->ifa_addr == NULL)
  260. continue;
  261. s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  262. if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET))
  263. {
  264. if (s != 0)
  265. {
  266. fprintf(stderr, "Error getting IP address.");
  267. return smprintf("n/a");
  268. }
  269. return smprintf("%s", host);
  270. }
  271. }
  272. /* free the address */
  273. freeifaddrs(ifaddr);
  274. /* return n/a if nothing works */
  275. return smprintf("n/a");
  276. }
  277. /* ram free */
  278. char *
  279. ram_free(const char *null)
  280. {
  281. long free;
  282. FILE *fp;
  283. /* open meminfo file */
  284. if (!(fp = fopen("/proc/meminfo", "r"))) {
  285. fprintf(stderr, "Error opening meminfo file.");
  286. return smprintf("n/a");
  287. }
  288. /* read the values */
  289. fscanf(fp, "MemTotal: %*d kB\n");
  290. fscanf(fp, "MemFree: %ld kB\n", &free);
  291. /* close meminfo file */
  292. fclose(fp);
  293. /* return free ram as string */
  294. return smprintf("%f", (float)free / 1024 / 1024);
  295. }
  296. /* ram percentage */
  297. char *
  298. ram_perc(const char *null)
  299. {
  300. int perc;
  301. long total, free, buffers, cached;
  302. FILE *fp;
  303. /* open meminfo file */
  304. if (!(fp = fopen("/proc/meminfo", "r"))) {
  305. fprintf(stderr, "Error opening meminfo file.");
  306. return smprintf("n/a");
  307. }
  308. /* read the values */
  309. fscanf(fp, "MemTotal: %ld kB\n", &total);
  310. fscanf(fp, "MemFree: %ld kB\n", &free);
  311. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  312. fscanf(fp, "Cached: %ld kB\n", &cached);
  313. /* close meminfo file */
  314. fclose(fp);
  315. /* calculate percentage */
  316. perc = 100 * ((total - free) - (buffers + cached)) / total;
  317. /* return perc as string */
  318. return smprintf("%d%%", perc);
  319. }
  320. /* ram total */
  321. char *
  322. ram_total(const char *null)
  323. {
  324. long total;
  325. FILE *fp;
  326. /* open meminfo file */
  327. if (!(fp = fopen("/proc/meminfo", "r"))) {
  328. fprintf(stderr, "Error opening meminfo file.");
  329. return smprintf("n/a");
  330. }
  331. /* read the values */
  332. fscanf(fp, "MemTotal: %ld kB\n", &total);
  333. /* close meminfo file */
  334. fclose(fp);
  335. /* return total ram as string */
  336. return smprintf("%f", (float)total / 1024 / 1024);
  337. }
  338. /* ram used */
  339. char *
  340. ram_used(const char *null)
  341. {
  342. long free, total, buffers, cached, used;
  343. FILE *fp;
  344. /* open meminfo file */
  345. if (!(fp = fopen("/proc/meminfo", "r"))) {
  346. fprintf(stderr, "Error opening meminfo file.");
  347. return smprintf("n/a");
  348. }
  349. /* read the values */
  350. fscanf(fp, "MemTotal: %ld kB\n", &total);
  351. fscanf(fp, "MemFree: %ld kB\n", &free);
  352. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  353. fscanf(fp, "Cached: %ld kB\n", &cached);
  354. /* close meminfo file */
  355. fclose(fp);
  356. /* calculate used */
  357. used = total - free - buffers - cached;
  358. /* return used ram as string */
  359. return smprintf("%f", (float)used / 1024 / 1024);
  360. }
  361. /* temperature */
  362. char *
  363. temp(const char *file)
  364. {
  365. int temperature;
  366. FILE *fp;
  367. /* open temperature file */
  368. if (!(fp = fopen(file, "r"))) {
  369. fprintf(stderr, "Could not open temperature file.\n");
  370. return smprintf("n/a");
  371. }
  372. /* extract temperature */
  373. fscanf(fp, "%d", &temperature);
  374. /* close temperature file */
  375. fclose(fp);
  376. /* return temperature in degrees */
  377. return smprintf("%d°C", temperature / 1000);
  378. }
  379. /* username */
  380. char *
  381. username(const char *null)
  382. {
  383. register struct passwd *pw;
  384. register uid_t uid;
  385. /* get the values */
  386. uid = geteuid ();
  387. pw = getpwuid (uid);
  388. /* if it worked, return */
  389. if (pw) {
  390. return smprintf("%s", pw->pw_name);
  391. }
  392. else {
  393. fprintf(stderr, "Could not get username.\n");
  394. return smprintf("n/a");
  395. }
  396. return smprintf("n/a");
  397. }
  398. /* uid */
  399. char *
  400. uid(const char *null)
  401. {
  402. register uid_t uid;
  403. /* get the values */
  404. uid = geteuid ();
  405. /* if it worked, return */
  406. if (uid) {
  407. return smprintf("%d", uid);
  408. }
  409. else {
  410. fprintf(stderr, "Could not get uid.\n");
  411. return smprintf("n/a");
  412. }
  413. return smprintf("n/a");
  414. }
  415. /* alsa volume percentage */
  416. char *
  417. vol_perc(const char *soundcard)
  418. {
  419. int mute = 0;
  420. long vol = 0, max = 0, min = 0;
  421. /* get volume from alsa */
  422. snd_mixer_t *handle;
  423. snd_mixer_elem_t *pcm_mixer, *mas_mixer;
  424. snd_mixer_selem_id_t *vol_info, *mute_info;
  425. snd_mixer_open(&handle, 0);
  426. snd_mixer_attach(handle, soundcard);
  427. snd_mixer_selem_register(handle, NULL, NULL);
  428. snd_mixer_load(handle);
  429. snd_mixer_selem_id_malloc(&vol_info);
  430. snd_mixer_selem_id_malloc(&mute_info);
  431. snd_mixer_selem_id_set_name(vol_info, channel);
  432. snd_mixer_selem_id_set_name(mute_info, channel);
  433. pcm_mixer = snd_mixer_find_selem(handle, vol_info);
  434. mas_mixer = snd_mixer_find_selem(handle, mute_info);
  435. snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max);
  436. snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol);
  437. snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute);
  438. if (vol_info)
  439. snd_mixer_selem_id_free(vol_info);
  440. if (mute_info)
  441. snd_mixer_selem_id_free(mute_info);
  442. if (handle)
  443. snd_mixer_close(handle);
  444. /* return the string (mute) */
  445. if (!mute)
  446. return smprintf("mute");
  447. else
  448. return smprintf("%d%%", (vol * 100) / max);
  449. }
  450. /* wifi percentage */
  451. char *
  452. wifi_perc(const char *wificard)
  453. {
  454. int bufsize = 255;
  455. int strength;
  456. char buf[bufsize];
  457. char *datastart;
  458. char path[64];
  459. char status[5];
  460. char needle[sizeof wificard + 1];
  461. FILE *fp;
  462. /* generate the path name */
  463. memset(path, 0, sizeof path);
  464. strcat(path, "/sys/class/net/");
  465. strcat(path, wificard);
  466. strcat(path, "/operstate");
  467. /* open wifi file */
  468. if(!(fp = fopen(path, "r"))) {
  469. fprintf(stderr, "Error opening wifi operstate file.");
  470. return smprintf("n/a");
  471. }
  472. /* read the status */
  473. fgets(status, 5, fp);
  474. /* close wifi file */
  475. fclose(fp);
  476. /* check if interface down */
  477. if(strcmp(status, "up\n") != 0){
  478. return smprintf("n/a");
  479. }
  480. /* open wifi file */
  481. if (!(fp = fopen("/proc/net/wireless", "r"))) {
  482. fprintf(stderr, "Error opening wireless file.");
  483. return smprintf("n/a");
  484. }
  485. /* extract the signal strength */
  486. strcpy(needle, wificard);
  487. strcat(needle, ":");
  488. fgets(buf, bufsize, fp);
  489. fgets(buf, bufsize, fp);
  490. fgets(buf, bufsize, fp);
  491. if ((datastart = strstr(buf, needle)) != NULL) {
  492. datastart = strstr(buf, ":");
  493. sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
  494. }
  495. /* close wifi file */
  496. fclose(fp);
  497. /* return strength in percent */
  498. return smprintf("%d%%", strength);
  499. }
  500. /* main function */
  501. int
  502. main()
  503. {
  504. char status_string[1024];
  505. struct arg argument;
  506. /* try to open display */
  507. if (!(dpy = XOpenDisplay(0x0))) {
  508. fprintf(stderr, "Cannot open display!\n");
  509. exit(1);
  510. }
  511. /* return status every interval */
  512. for (;;) {
  513. /* clear the string */
  514. memset(status_string, 0, sizeof(status_string));
  515. /* generate status_string */
  516. for (size_t i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
  517. argument = args[i];
  518. char *res = argument.func(argument.args);
  519. char *element = smprintf(argument.format, res);
  520. strcat(status_string, element);
  521. free(res);
  522. free(element);
  523. }
  524. /* return the statusbar */
  525. setstatus(status_string);
  526. /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
  527. sleep(update_interval -1);
  528. }
  529. /* close display */
  530. XCloseDisplay(dpy);
  531. /* exit successfully */
  532. return 0;
  533. }