slstatus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /* See LICENSE file for copyright and license details. */
  2. #include <dirent.h>
  3. #include <err.h>
  4. #include <fcntl.h>
  5. #include <ifaddrs.h>
  6. #include <limits.h>
  7. #include <linux/wireless.h>
  8. #include <locale.h>
  9. #include <netdb.h>
  10. #include <pwd.h>
  11. #include <signal.h>
  12. #include <stdarg.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/ioctl.h>
  17. #include <sys/stat.h>
  18. #include <sys/statvfs.h>
  19. #include <sys/socket.h>
  20. #include <sys/soundcard.h>
  21. #include <sys/sysinfo.h>
  22. #include <sys/types.h>
  23. #include <sys/utsname.h>
  24. #include <time.h>
  25. #include <unistd.h>
  26. #include <X11/Xlib.h>
  27. #include "arg.h"
  28. #define LEN(x) (sizeof (x) / sizeof *(x))
  29. struct arg {
  30. const char *(*func)();
  31. const char *fmt;
  32. const char *args;
  33. };
  34. static const char *bprintf(const char *fmt, ...);
  35. static const char *battery_perc(const char *bat);
  36. static const char *battery_power(const char *bat);
  37. static const char *battery_state(const char *bat);
  38. static const char *cpu_freq(void);
  39. static const char *cpu_perc(void);
  40. static const char *datetime(const char *fmt);
  41. static const char *disk_free(const char *mnt);
  42. static const char *disk_perc(const char *mnt);
  43. static const char *disk_total(const char *mnt);
  44. static const char *disk_used(const char *mnt);
  45. static const char *entropy(void);
  46. static const char *gid(void);
  47. static const char *hostname(void);
  48. static const char *ip(const char *iface);
  49. static const char *kernel_release(void);
  50. static const char *keyboard_indicators(void);
  51. static const char *load_avg(void);
  52. static const char *num_files(const char *dir);
  53. static const char *ram_free(void);
  54. static const char *ram_perc(void);
  55. static const char *ram_used(void);
  56. static const char *ram_total(void);
  57. static const char *run_command(const char *cmd);
  58. static const char *swap_free(void);
  59. static const char *swap_perc(void);
  60. static const char *swap_used(void);
  61. static const char *swap_total(void);
  62. static const char *temp(const char *file);
  63. static const char *uid(void);
  64. static const char *uptime(void);
  65. static const char *username(void);
  66. static const char *vol_perc(const char *card);
  67. static const char *wifi_perc(const char *iface);
  68. static const char *wifi_essid(const char *iface);
  69. static void sighandler(const int signo);
  70. static void usage(void);
  71. char *argv0;
  72. static unsigned short int delay = 0;
  73. static unsigned short int done;
  74. static Display *dpy;
  75. #include "config.h"
  76. static char buf[MAXLEN];
  77. static const char *
  78. bprintf(const char *fmt, ...)
  79. {
  80. va_list ap;
  81. size_t len;
  82. va_start(ap, fmt);
  83. len = vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
  84. va_end(ap);
  85. if (len >= sizeof(buf))
  86. buf[sizeof(buf)-1] = '\0';
  87. return buf;
  88. }
  89. static const char *
  90. battery_perc(const char *bat)
  91. {
  92. int n, perc;
  93. char path[PATH_MAX];
  94. FILE *fp;
  95. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/capacity");
  96. fp = fopen(path, "r");
  97. if (fp == NULL) {
  98. warn("Failed to open file %s", path);
  99. return UNKNOWN_STR;
  100. }
  101. n = fscanf(fp, "%i", &perc);
  102. fclose(fp);
  103. if (n != 1)
  104. return UNKNOWN_STR;
  105. return bprintf("%d", perc);
  106. }
  107. static const char *
  108. battery_power(const char *bat)
  109. {
  110. char path[PATH_MAX];
  111. FILE *fp;
  112. int n, watts;
  113. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/power_now");
  114. fp = fopen(path, "r");
  115. if (fp == NULL) {
  116. warn("Failed to open file %s", path);
  117. return UNKNOWN_STR;
  118. }
  119. n = fscanf(fp, "%i", &watts);
  120. fclose(fp);
  121. if (n != 1)
  122. return UNKNOWN_STR;
  123. return bprintf("%d", (watts + 500000) / 1000000);
  124. }
  125. static const char *
  126. battery_state(const char *bat)
  127. {
  128. FILE *fp;
  129. struct {
  130. char *state;
  131. char *symbol;
  132. } map[] = {
  133. { "Charging", "+" },
  134. { "Discharging", "-" },
  135. { "Full", "=" },
  136. { "Unknown", "/" },
  137. };
  138. size_t i;
  139. int n;
  140. char path[PATH_MAX], state[12];
  141. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/status");
  142. fp = fopen(path, "r");
  143. if (fp == NULL) {
  144. warn("Failed to open file %s", path);
  145. return UNKNOWN_STR;
  146. }
  147. n = fscanf(fp, "%12s", state);
  148. fclose(fp);
  149. if (n != 1)
  150. return UNKNOWN_STR;
  151. for (i = 0; i < LEN(map); i++) {
  152. if (!strcmp(map[i].state, state)) {
  153. break;
  154. }
  155. }
  156. return (i == LEN(map)) ? "?" : map[i].symbol;
  157. }
  158. static const char *
  159. cpu_freq(void)
  160. {
  161. int n, freq;
  162. FILE *fp;
  163. fp = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r");
  164. if (fp == NULL) {
  165. warn("Failed to open file /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
  166. return UNKNOWN_STR;
  167. }
  168. n = fscanf(fp, "%i", &freq);
  169. fclose(fp);
  170. if (n != 1)
  171. return UNKNOWN_STR;
  172. return bprintf("%d", (freq + 500) / 1000);
  173. }
  174. static const char *
  175. cpu_perc(void)
  176. {
  177. int n, perc;
  178. long double a[4], b[4];
  179. FILE *fp;
  180. fp = fopen("/proc/stat", "r");
  181. if (fp == NULL) {
  182. warn("Failed to open file /proc/stat");
  183. return UNKNOWN_STR;
  184. }
  185. n = fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
  186. fclose(fp);
  187. if (n != 4)
  188. return UNKNOWN_STR;
  189. delay++;
  190. sleep(delay);
  191. fp = fopen("/proc/stat", "r");
  192. if (fp == NULL) {
  193. warn("Failed to open file /proc/stat");
  194. return UNKNOWN_STR;
  195. }
  196. n = fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
  197. fclose(fp);
  198. if (n != 4)
  199. return UNKNOWN_STR;
  200. 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]));
  201. return bprintf("%d", perc);
  202. }
  203. static const char *
  204. datetime(const char *fmt)
  205. {
  206. time_t t;
  207. t = time(NULL);
  208. if (strftime(buf, sizeof(buf), fmt, localtime(&t)) == 0)
  209. return UNKNOWN_STR;
  210. return buf;
  211. }
  212. static const char *
  213. disk_free(const char *mnt)
  214. {
  215. struct statvfs fs;
  216. if (statvfs(mnt, &fs) < 0) {
  217. warn("Failed to get filesystem info");
  218. return UNKNOWN_STR;
  219. }
  220. return bprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
  221. }
  222. static const char *
  223. disk_perc(const char *mnt)
  224. {
  225. int perc;
  226. struct statvfs fs;
  227. if (statvfs(mnt, &fs) < 0) {
  228. warn("Failed to get filesystem info");
  229. return UNKNOWN_STR;
  230. }
  231. perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
  232. return bprintf("%d", perc);
  233. }
  234. static const char *
  235. disk_total(const char *mnt)
  236. {
  237. struct statvfs fs;
  238. if (statvfs(mnt, &fs) < 0) {
  239. warn("Failed to get filesystem info");
  240. return UNKNOWN_STR;
  241. }
  242. return bprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
  243. }
  244. static const char *
  245. disk_used(const char *mnt)
  246. {
  247. struct statvfs fs;
  248. if (statvfs(mnt, &fs) < 0) {
  249. warn("Failed to get filesystem info");
  250. return UNKNOWN_STR;
  251. }
  252. return bprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
  253. }
  254. static const char *
  255. entropy(void)
  256. {
  257. int n, num;
  258. FILE *fp;
  259. fp= fopen("/proc/sys/kernel/random/entropy_avail", "r");
  260. if (fp == NULL) {
  261. warn("Failed to open file /proc/sys/kernel/random/entropy_avail");
  262. return UNKNOWN_STR;
  263. }
  264. n = fscanf(fp, "%d", &num);
  265. fclose(fp);
  266. if (n != 1)
  267. return UNKNOWN_STR;
  268. return bprintf("%d", num);
  269. }
  270. static const char *
  271. gid(void)
  272. {
  273. return bprintf("%d", getgid());
  274. }
  275. static const char *
  276. hostname(void)
  277. {
  278. if (gethostname(buf, sizeof(buf)) == -1) {
  279. warn("hostname");
  280. return UNKNOWN_STR;
  281. }
  282. return buf;
  283. }
  284. static const char *
  285. ip(const char *iface)
  286. {
  287. struct ifaddrs *ifaddr, *ifa;
  288. int s;
  289. char host[NI_MAXHOST];
  290. if (getifaddrs(&ifaddr) == -1) {
  291. warn("Failed to get IP address for interface %s", iface);
  292. return UNKNOWN_STR;
  293. }
  294. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  295. if (ifa->ifa_addr == NULL) {
  296. continue;
  297. }
  298. s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  299. if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) {
  300. if (s != 0) {
  301. warnx("Failed to get IP address for interface %s", iface);
  302. return UNKNOWN_STR;
  303. }
  304. return bprintf("%s", host);
  305. }
  306. }
  307. freeifaddrs(ifaddr);
  308. return UNKNOWN_STR;
  309. }
  310. static const char *
  311. kernel_release(void)
  312. {
  313. struct utsname udata;
  314. if (uname(&udata) < 0) {
  315. return UNKNOWN_STR;
  316. }
  317. return bprintf("%s", udata.release);
  318. }
  319. static const char *
  320. keyboard_indicators(void)
  321. {
  322. Display *dpy = XOpenDisplay(NULL);
  323. XKeyboardState state;
  324. XGetKeyboardControl(dpy, &state);
  325. XCloseDisplay(dpy);
  326. switch (state.led_mask) {
  327. case 1:
  328. return "c";
  329. case 2:
  330. return "n";
  331. case 3:
  332. return "cn";
  333. default:
  334. return "";
  335. }
  336. }
  337. static const char *
  338. load_avg(void)
  339. {
  340. double avgs[3];
  341. if (getloadavg(avgs, 3) < 0) {
  342. warnx("Failed to get the load avg");
  343. return UNKNOWN_STR;
  344. }
  345. return bprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
  346. }
  347. static const char *
  348. num_files(const char *dir)
  349. {
  350. struct dirent *dp;
  351. DIR *fd;
  352. int num = 0;
  353. if ((fd = opendir(dir)) == NULL) {
  354. warn("Failed to get number of files in directory %s", dir);
  355. return UNKNOWN_STR;
  356. }
  357. while ((dp = readdir(fd)) != NULL) {
  358. if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
  359. continue; /* skip self and parent */
  360. num++;
  361. }
  362. closedir(fd);
  363. return bprintf("%d", num);
  364. }
  365. static const char *
  366. ram_free(void)
  367. {
  368. long free;
  369. FILE *fp;
  370. int n;
  371. fp = fopen("/proc/meminfo", "r");
  372. if (fp == NULL) {
  373. warn("Failed to open file /proc/meminfo");
  374. return UNKNOWN_STR;
  375. }
  376. n = fscanf(fp, "MemFree: %ld kB\n", &free);
  377. fclose(fp);
  378. if (n != 1)
  379. return UNKNOWN_STR;
  380. return bprintf("%f", (float)free / 1024 / 1024);
  381. }
  382. static const char *
  383. ram_perc(void)
  384. {
  385. long total, free, buffers, cached;
  386. FILE *fp;
  387. fp = fopen("/proc/meminfo", "r");
  388. if (fp == NULL) {
  389. warn("Failed to open file /proc/meminfo");
  390. return UNKNOWN_STR;
  391. }
  392. if (fscanf(fp, "MemTotal: %ld kB\n", &total) != 1 ||
  393. fscanf(fp, "MemFree: %ld kB\n", &free) != 1 ||
  394. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n",
  395. &buffers, &buffers) != 2 ||
  396. fscanf(fp, "Cached: %ld kB\n", &cached) != 1)
  397. goto scanerr;
  398. fclose(fp);
  399. return bprintf("%d", 100 * ((total - free) - (buffers + cached)) / total);
  400. scanerr:
  401. fclose(fp);
  402. return UNKNOWN_STR;
  403. }
  404. static const char *
  405. ram_total(void)
  406. {
  407. long total;
  408. FILE *fp;
  409. int n;
  410. fp = fopen("/proc/meminfo", "r");
  411. if (fp == NULL) {
  412. warn("Failed to open file /proc/meminfo");
  413. return UNKNOWN_STR;
  414. }
  415. n = fscanf(fp, "MemTotal: %ld kB\n", &total);
  416. fclose(fp);
  417. if (n != 1)
  418. return UNKNOWN_STR;
  419. return bprintf("%f", (float)total / 1024 / 1024);
  420. }
  421. static const char *
  422. ram_used(void)
  423. {
  424. long free, total, buffers, cached;
  425. FILE *fp;
  426. fp = fopen("/proc/meminfo", "r");
  427. if (fp == NULL) {
  428. warn("Failed to open file /proc/meminfo");
  429. return UNKNOWN_STR;
  430. }
  431. if (fscanf(fp, "MemTotal: %ld kB\n", &total) != 1 ||
  432. fscanf(fp, "MemFree: %ld kB\n", &free) != 1 ||
  433. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n",
  434. &buffers, &buffers) != 2 ||
  435. fscanf(fp, "Cached: %ld kB\n", &cached) != 1)
  436. goto scanerr;
  437. fclose(fp);
  438. return bprintf("%f", (float)(total - free - buffers - cached) / 1024 / 1024);
  439. scanerr:
  440. fclose(fp);
  441. return UNKNOWN_STR;
  442. }
  443. static const char *
  444. run_command(const char *cmd)
  445. {
  446. char *p;
  447. FILE *fp;
  448. fp = popen(cmd, "r");
  449. if (fp == NULL) {
  450. warn("Failed to get command output for %s", cmd);
  451. return UNKNOWN_STR;
  452. }
  453. p = fgets(buf, sizeof(buf) - 1, fp);
  454. pclose(fp);
  455. if (!p)
  456. return UNKNOWN_STR;
  457. if ((p = strrchr(buf, '\n')) != NULL)
  458. p[0] = '\0';
  459. return buf[0] ? buf : UNKNOWN_STR;
  460. }
  461. static const char *
  462. swap_free(void)
  463. {
  464. long total, free;
  465. FILE *fp;
  466. size_t bytes_read;
  467. char *match;
  468. fp = fopen("/proc/meminfo", "r");
  469. if (fp == NULL) {
  470. warn("Failed to open file /proc/meminfo");
  471. return UNKNOWN_STR;
  472. }
  473. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  474. warn("swap_free: read error");
  475. fclose(fp);
  476. return UNKNOWN_STR;
  477. }
  478. fclose(fp);
  479. if ((match = strstr(buf, "SwapTotal")) == NULL)
  480. return UNKNOWN_STR;
  481. sscanf(match, "SwapTotal: %ld kB\n", &total);
  482. if ((match = strstr(buf, "SwapFree")) == NULL)
  483. return UNKNOWN_STR;
  484. sscanf(match, "SwapFree: %ld kB\n", &free);
  485. return bprintf("%f", (float)free / 1024 / 1024);
  486. }
  487. static const char *
  488. swap_perc(void)
  489. {
  490. long total, free, cached;
  491. FILE *fp;
  492. size_t bytes_read;
  493. char *match;
  494. fp = fopen("/proc/meminfo", "r");
  495. if (fp == NULL) {
  496. warn("Failed to open file /proc/meminfo");
  497. return UNKNOWN_STR;
  498. }
  499. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  500. warn("swap_perc: read error");
  501. fclose(fp);
  502. return UNKNOWN_STR;
  503. }
  504. fclose(fp);
  505. if ((match = strstr(buf, "SwapTotal")) == NULL)
  506. return UNKNOWN_STR;
  507. sscanf(match, "SwapTotal: %ld kB\n", &total);
  508. if ((match = strstr(buf, "SwapCached")) == NULL)
  509. return UNKNOWN_STR;
  510. sscanf(match, "SwapCached: %ld kB\n", &cached);
  511. if ((match = strstr(buf, "SwapFree")) == NULL)
  512. return UNKNOWN_STR;
  513. sscanf(match, "SwapFree: %ld kB\n", &free);
  514. return bprintf("%d", 100 * (total - free - cached) / total);
  515. }
  516. static const char *
  517. swap_total(void)
  518. {
  519. long total;
  520. FILE *fp;
  521. size_t bytes_read;
  522. char *match;
  523. fp = fopen("/proc/meminfo", "r");
  524. if (fp == NULL) {
  525. warn("Failed to open file /proc/meminfo");
  526. return UNKNOWN_STR;
  527. }
  528. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  529. warn("swap_total: read error");
  530. fclose(fp);
  531. return UNKNOWN_STR;
  532. }
  533. fclose(fp);
  534. if ((match = strstr(buf, "SwapTotal")) == NULL)
  535. return UNKNOWN_STR;
  536. sscanf(match, "SwapTotal: %ld kB\n", &total);
  537. return bprintf("%f", (float)total / 1024 / 1024);
  538. }
  539. static const char *
  540. swap_used(void)
  541. {
  542. long total, free, cached;
  543. FILE *fp;
  544. size_t bytes_read;
  545. char *match;
  546. fp = fopen("/proc/meminfo", "r");
  547. if (fp == NULL) {
  548. warn("Failed to open file /proc/meminfo");
  549. return UNKNOWN_STR;
  550. }
  551. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  552. warn("swap_used: read error");
  553. fclose(fp);
  554. return UNKNOWN_STR;
  555. }
  556. fclose(fp);
  557. if ((match = strstr(buf, "SwapTotal")) == NULL)
  558. return UNKNOWN_STR;
  559. sscanf(match, "SwapTotal: %ld kB\n", &total);
  560. if ((match = strstr(buf, "SwapCached")) == NULL)
  561. return UNKNOWN_STR;
  562. sscanf(match, "SwapCached: %ld kB\n", &cached);
  563. if ((match = strstr(buf, "SwapFree")) == NULL)
  564. return UNKNOWN_STR;
  565. sscanf(match, "SwapFree: %ld kB\n", &free);
  566. return bprintf("%f", (float)(total - free - cached) / 1024 / 1024);
  567. }
  568. static const char *
  569. temp(const char *file)
  570. {
  571. int n, temp;
  572. FILE *fp;
  573. fp = fopen(file, "r");
  574. if (fp == NULL) {
  575. warn("Failed to open file %s", file);
  576. return UNKNOWN_STR;
  577. }
  578. n = fscanf(fp, "%d", &temp);
  579. fclose(fp);
  580. if (n != 1)
  581. return UNKNOWN_STR;
  582. return bprintf("%d", temp / 1000);
  583. }
  584. static const char *
  585. uptime(void)
  586. {
  587. struct sysinfo info;
  588. int h = 0;
  589. int m = 0;
  590. sysinfo(&info);
  591. h = info.uptime / 3600;
  592. m = (info.uptime - h * 3600 ) / 60;
  593. return bprintf("%dh %dm", h, m);
  594. }
  595. static const char *
  596. username(void)
  597. {
  598. struct passwd *pw = getpwuid(geteuid());
  599. if (pw == NULL) {
  600. warn("Failed to get username");
  601. return UNKNOWN_STR;
  602. }
  603. return bprintf("%s", pw->pw_name);
  604. }
  605. static const char *
  606. uid(void)
  607. {
  608. return bprintf("%d", geteuid());
  609. }
  610. static const char *
  611. vol_perc(const char *card)
  612. {
  613. unsigned int i;
  614. int v, afd, devmask;
  615. char *vnames[] = SOUND_DEVICE_NAMES;
  616. afd = open(card, O_RDONLY | O_NONBLOCK);
  617. if (afd == -1) {
  618. warn("Cannot open %s", card);
  619. return UNKNOWN_STR;
  620. }
  621. if (ioctl(afd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
  622. warn("Cannot get volume for %s", card);
  623. close(afd);
  624. return UNKNOWN_STR;
  625. }
  626. for (i = 0; i < LEN(vnames); i++) {
  627. if (devmask & (1 << i) && !strcmp("vol", vnames[i])) {
  628. if (ioctl(afd, MIXER_READ(i), &v) == -1) {
  629. warn("vol_perc: ioctl");
  630. close(afd);
  631. return UNKNOWN_STR;
  632. }
  633. }
  634. }
  635. close(afd);
  636. return bprintf("%d", v & 0xff);
  637. }
  638. static const char *
  639. wifi_perc(const char *iface)
  640. {
  641. int i, perc;
  642. char *p, *datastart;
  643. char path[PATH_MAX];
  644. char status[5];
  645. FILE *fp;
  646. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface, "/operstate");
  647. fp = fopen(path, "r");
  648. if (fp == NULL) {
  649. warn("Failed to open file %s", path);
  650. return UNKNOWN_STR;
  651. }
  652. p = fgets(status, 5, fp);
  653. fclose(fp);
  654. if(!p || strcmp(status, "up\n") != 0) {
  655. return UNKNOWN_STR;
  656. }
  657. fp = fopen("/proc/net/wireless", "r");
  658. if (fp == NULL) {
  659. warn("Failed to open file /proc/net/wireless");
  660. return UNKNOWN_STR;
  661. }
  662. for (i = 0; i < 3; i++) {
  663. if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
  664. break;
  665. }
  666. fclose(fp);
  667. if (i < 2 || !p)
  668. return UNKNOWN_STR;
  669. if ((datastart = strstr(buf, iface)) == NULL)
  670. return UNKNOWN_STR;
  671. datastart = (datastart+(strlen(iface)+1));
  672. sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc);
  673. return bprintf("%d", perc);
  674. }
  675. static const char *
  676. wifi_essid(const char *iface)
  677. {
  678. static char id[IW_ESSID_MAX_SIZE+1];
  679. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  680. struct iwreq wreq;
  681. memset(&wreq, 0, sizeof(struct iwreq));
  682. wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
  683. snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
  684. if (sockfd == -1) {
  685. warn("Failed to get ESSID for interface %s", iface);
  686. return UNKNOWN_STR;
  687. }
  688. wreq.u.essid.pointer = id;
  689. if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
  690. warn("Failed to get ESSID for interface %s", iface);
  691. return UNKNOWN_STR;
  692. }
  693. close(sockfd);
  694. if (strcmp(id, "") == 0)
  695. return UNKNOWN_STR;
  696. else
  697. return id;
  698. }
  699. static void
  700. sighandler(const int signo)
  701. {
  702. if (signo == SIGTERM || signo == SIGINT) {
  703. done = 1;
  704. }
  705. }
  706. static void
  707. usage(void)
  708. {
  709. fprintf(stderr, "usage: %s [-s]\n", argv0);
  710. exit(1);
  711. }
  712. int
  713. main(int argc, char *argv[])
  714. {
  715. struct arg argument;
  716. struct sigaction act;
  717. size_t i, len;
  718. int sflag = 0;
  719. char status_string[MAXLEN];
  720. char *element;
  721. ARGBEGIN {
  722. case 's':
  723. sflag = 1;
  724. break;
  725. default:
  726. usage();
  727. } ARGEND
  728. if (argc) {
  729. usage();
  730. }
  731. memset(&act, 0, sizeof(act));
  732. act.sa_handler = sighandler;
  733. sigaction(SIGINT, &act, 0);
  734. sigaction(SIGTERM, &act, 0);
  735. if (!sflag) {
  736. dpy = XOpenDisplay(NULL);
  737. }
  738. setlocale(LC_ALL, "");
  739. while (!done) {
  740. status_string[0] = '\0';
  741. for (element = status_string, i = len = 0; i < LEN(args);
  742. ++i, element += len) {
  743. argument = args[i];
  744. len = snprintf(element, sizeof(status_string)-1 - len,
  745. argument.fmt,
  746. argument.func(argument.args));
  747. if (len >= sizeof(status_string)) {
  748. status_string[sizeof(status_string)-1] = '\0';
  749. break;
  750. }
  751. }
  752. if (sflag) {
  753. printf("%s\n", status_string);
  754. } else {
  755. XStoreName(dpy, DefaultRootWindow(dpy), status_string);
  756. XSync(dpy, False);
  757. }
  758. if ((UPDATE_INTERVAL - delay) <= 0) {
  759. delay = 0;
  760. continue;
  761. } else {
  762. sleep(UPDATE_INTERVAL - delay);
  763. delay = 0;
  764. }
  765. }
  766. if (!sflag) {
  767. XStoreName(dpy, DefaultRootWindow(dpy), NULL);
  768. XCloseDisplay(dpy);
  769. }
  770. return 0;
  771. }