slstatus.c 18 KB

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