slstatus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. XKeyboardState state;
  323. XGetKeyboardControl(dpy, &state);
  324. switch (state.led_mask) {
  325. case 1:
  326. return "c";
  327. case 2:
  328. return "n";
  329. case 3:
  330. return "cn";
  331. default:
  332. return "";
  333. }
  334. }
  335. static const char *
  336. load_avg(void)
  337. {
  338. double avgs[3];
  339. if (getloadavg(avgs, 3) < 0) {
  340. warnx("Failed to get the load avg");
  341. return UNKNOWN_STR;
  342. }
  343. return bprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
  344. }
  345. static const char *
  346. num_files(const char *dir)
  347. {
  348. struct dirent *dp;
  349. DIR *fd;
  350. int num = 0;
  351. if ((fd = opendir(dir)) == NULL) {
  352. warn("Failed to get number of files in directory %s", dir);
  353. return UNKNOWN_STR;
  354. }
  355. while ((dp = readdir(fd)) != NULL) {
  356. if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
  357. continue; /* skip self and parent */
  358. num++;
  359. }
  360. closedir(fd);
  361. return bprintf("%d", num);
  362. }
  363. static const char *
  364. ram_free(void)
  365. {
  366. long free;
  367. FILE *fp;
  368. int n;
  369. fp = fopen("/proc/meminfo", "r");
  370. if (fp == NULL) {
  371. warn("Failed to open file /proc/meminfo");
  372. return UNKNOWN_STR;
  373. }
  374. n = fscanf(fp, "MemFree: %ld kB\n", &free);
  375. fclose(fp);
  376. if (n != 1)
  377. return UNKNOWN_STR;
  378. return bprintf("%f", (float)free / 1024 / 1024);
  379. }
  380. static const char *
  381. ram_perc(void)
  382. {
  383. long total, free, buffers, cached;
  384. FILE *fp;
  385. fp = fopen("/proc/meminfo", "r");
  386. if (fp == NULL) {
  387. warn("Failed to open file /proc/meminfo");
  388. return UNKNOWN_STR;
  389. }
  390. if (fscanf(fp, "MemTotal: %ld kB\n", &total) != 1 ||
  391. fscanf(fp, "MemFree: %ld kB\n", &free) != 1 ||
  392. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n",
  393. &buffers, &buffers) != 2 ||
  394. fscanf(fp, "Cached: %ld kB\n", &cached) != 1)
  395. goto scanerr;
  396. fclose(fp);
  397. return bprintf("%d", 100 * ((total - free) - (buffers + cached)) / total);
  398. scanerr:
  399. fclose(fp);
  400. return UNKNOWN_STR;
  401. }
  402. static const char *
  403. ram_total(void)
  404. {
  405. long total;
  406. FILE *fp;
  407. int n;
  408. fp = fopen("/proc/meminfo", "r");
  409. if (fp == NULL) {
  410. warn("Failed to open file /proc/meminfo");
  411. return UNKNOWN_STR;
  412. }
  413. n = fscanf(fp, "MemTotal: %ld kB\n", &total);
  414. fclose(fp);
  415. if (n != 1)
  416. return UNKNOWN_STR;
  417. return bprintf("%f", (float)total / 1024 / 1024);
  418. }
  419. static const char *
  420. ram_used(void)
  421. {
  422. long free, total, buffers, cached;
  423. FILE *fp;
  424. fp = fopen("/proc/meminfo", "r");
  425. if (fp == NULL) {
  426. warn("Failed to open file /proc/meminfo");
  427. return UNKNOWN_STR;
  428. }
  429. if (fscanf(fp, "MemTotal: %ld kB\n", &total) != 1 ||
  430. fscanf(fp, "MemFree: %ld kB\n", &free) != 1 ||
  431. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n",
  432. &buffers, &buffers) != 2 ||
  433. fscanf(fp, "Cached: %ld kB\n", &cached) != 1)
  434. goto scanerr;
  435. fclose(fp);
  436. return bprintf("%f", (float)(total - free - buffers - cached) / 1024 / 1024);
  437. scanerr:
  438. fclose(fp);
  439. return UNKNOWN_STR;
  440. }
  441. static const char *
  442. run_command(const char *cmd)
  443. {
  444. char *p;
  445. FILE *fp;
  446. fp = popen(cmd, "r");
  447. if (fp == NULL) {
  448. warn("Failed to get command output for %s", cmd);
  449. return UNKNOWN_STR;
  450. }
  451. p = fgets(buf, sizeof(buf) - 1, fp);
  452. pclose(fp);
  453. if (!p)
  454. return UNKNOWN_STR;
  455. if ((p = strrchr(buf, '\n')) != NULL)
  456. p[0] = '\0';
  457. return buf[0] ? buf : UNKNOWN_STR;
  458. }
  459. static const char *
  460. swap_free(void)
  461. {
  462. long total, free;
  463. FILE *fp;
  464. size_t bytes_read;
  465. char *match;
  466. fp = fopen("/proc/meminfo", "r");
  467. if (fp == NULL) {
  468. warn("Failed to open file /proc/meminfo");
  469. return UNKNOWN_STR;
  470. }
  471. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  472. warn("swap_free: read error");
  473. fclose(fp);
  474. return UNKNOWN_STR;
  475. }
  476. fclose(fp);
  477. if ((match = strstr(buf, "SwapTotal")) == NULL)
  478. return UNKNOWN_STR;
  479. sscanf(match, "SwapTotal: %ld kB\n", &total);
  480. if ((match = strstr(buf, "SwapFree")) == NULL)
  481. return UNKNOWN_STR;
  482. sscanf(match, "SwapFree: %ld kB\n", &free);
  483. return bprintf("%f", (float)free / 1024 / 1024);
  484. }
  485. static const char *
  486. swap_perc(void)
  487. {
  488. long total, free, cached;
  489. FILE *fp;
  490. size_t bytes_read;
  491. char *match;
  492. fp = fopen("/proc/meminfo", "r");
  493. if (fp == NULL) {
  494. warn("Failed to open file /proc/meminfo");
  495. return UNKNOWN_STR;
  496. }
  497. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  498. warn("swap_perc: read error");
  499. fclose(fp);
  500. return UNKNOWN_STR;
  501. }
  502. fclose(fp);
  503. if ((match = strstr(buf, "SwapTotal")) == NULL)
  504. return UNKNOWN_STR;
  505. sscanf(match, "SwapTotal: %ld kB\n", &total);
  506. if ((match = strstr(buf, "SwapCached")) == NULL)
  507. return UNKNOWN_STR;
  508. sscanf(match, "SwapCached: %ld kB\n", &cached);
  509. if ((match = strstr(buf, "SwapFree")) == NULL)
  510. return UNKNOWN_STR;
  511. sscanf(match, "SwapFree: %ld kB\n", &free);
  512. return bprintf("%d", 100 * (total - free - cached) / total);
  513. }
  514. static const char *
  515. swap_total(void)
  516. {
  517. long total;
  518. FILE *fp;
  519. size_t bytes_read;
  520. char *match;
  521. fp = fopen("/proc/meminfo", "r");
  522. if (fp == NULL) {
  523. warn("Failed to open file /proc/meminfo");
  524. return UNKNOWN_STR;
  525. }
  526. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  527. warn("swap_total: read error");
  528. fclose(fp);
  529. return UNKNOWN_STR;
  530. }
  531. fclose(fp);
  532. if ((match = strstr(buf, "SwapTotal")) == NULL)
  533. return UNKNOWN_STR;
  534. sscanf(match, "SwapTotal: %ld kB\n", &total);
  535. return bprintf("%f", (float)total / 1024 / 1024);
  536. }
  537. static const char *
  538. swap_used(void)
  539. {
  540. long total, free, cached;
  541. FILE *fp;
  542. size_t bytes_read;
  543. char *match;
  544. fp = fopen("/proc/meminfo", "r");
  545. if (fp == NULL) {
  546. warn("Failed to open file /proc/meminfo");
  547. return UNKNOWN_STR;
  548. }
  549. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  550. warn("swap_used: read error");
  551. fclose(fp);
  552. return UNKNOWN_STR;
  553. }
  554. fclose(fp);
  555. if ((match = strstr(buf, "SwapTotal")) == NULL)
  556. return UNKNOWN_STR;
  557. sscanf(match, "SwapTotal: %ld kB\n", &total);
  558. if ((match = strstr(buf, "SwapCached")) == NULL)
  559. return UNKNOWN_STR;
  560. sscanf(match, "SwapCached: %ld kB\n", &cached);
  561. if ((match = strstr(buf, "SwapFree")) == NULL)
  562. return UNKNOWN_STR;
  563. sscanf(match, "SwapFree: %ld kB\n", &free);
  564. return bprintf("%f", (float)(total - free - cached) / 1024 / 1024);
  565. }
  566. static const char *
  567. temp(const char *file)
  568. {
  569. int n, temp;
  570. FILE *fp;
  571. fp = fopen(file, "r");
  572. if (fp == NULL) {
  573. warn("Failed to open file %s", file);
  574. return UNKNOWN_STR;
  575. }
  576. n = fscanf(fp, "%d", &temp);
  577. fclose(fp);
  578. if (n != 1)
  579. return UNKNOWN_STR;
  580. return bprintf("%d", temp / 1000);
  581. }
  582. static const char *
  583. uptime(void)
  584. {
  585. struct sysinfo info;
  586. int h = 0;
  587. int m = 0;
  588. sysinfo(&info);
  589. h = info.uptime / 3600;
  590. m = (info.uptime - h * 3600 ) / 60;
  591. return bprintf("%dh %dm", h, m);
  592. }
  593. static const char *
  594. username(void)
  595. {
  596. struct passwd *pw = getpwuid(geteuid());
  597. if (pw == NULL) {
  598. warn("Failed to get username");
  599. return UNKNOWN_STR;
  600. }
  601. return bprintf("%s", pw->pw_name);
  602. }
  603. static const char *
  604. uid(void)
  605. {
  606. return bprintf("%d", geteuid());
  607. }
  608. static const char *
  609. vol_perc(const char *card)
  610. {
  611. unsigned int i;
  612. int v, afd, devmask;
  613. char *vnames[] = SOUND_DEVICE_NAMES;
  614. afd = open(card, O_RDONLY | O_NONBLOCK);
  615. if (afd == -1) {
  616. warn("Cannot open %s", card);
  617. return UNKNOWN_STR;
  618. }
  619. if (ioctl(afd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
  620. warn("Cannot get volume for %s", card);
  621. close(afd);
  622. return UNKNOWN_STR;
  623. }
  624. for (i = 0; i < LEN(vnames); i++) {
  625. if (devmask & (1 << i) && !strcmp("vol", vnames[i])) {
  626. if (ioctl(afd, MIXER_READ(i), &v) == -1) {
  627. warn("vol_perc: ioctl");
  628. close(afd);
  629. return UNKNOWN_STR;
  630. }
  631. }
  632. }
  633. close(afd);
  634. return bprintf("%d", v & 0xff);
  635. }
  636. static const char *
  637. wifi_perc(const char *iface)
  638. {
  639. int i, perc;
  640. char *p, *datastart;
  641. char path[PATH_MAX];
  642. char status[5];
  643. FILE *fp;
  644. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface, "/operstate");
  645. fp = fopen(path, "r");
  646. if (fp == NULL) {
  647. warn("Failed to open file %s", path);
  648. return UNKNOWN_STR;
  649. }
  650. p = fgets(status, 5, fp);
  651. fclose(fp);
  652. if(!p || strcmp(status, "up\n") != 0) {
  653. return UNKNOWN_STR;
  654. }
  655. fp = fopen("/proc/net/wireless", "r");
  656. if (fp == NULL) {
  657. warn("Failed to open file /proc/net/wireless");
  658. return UNKNOWN_STR;
  659. }
  660. for (i = 0; i < 3; i++) {
  661. if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
  662. break;
  663. }
  664. fclose(fp);
  665. if (i < 2 || !p)
  666. return UNKNOWN_STR;
  667. if ((datastart = strstr(buf, iface)) == NULL)
  668. return UNKNOWN_STR;
  669. datastart = (datastart+(strlen(iface)+1));
  670. sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc);
  671. return bprintf("%d", perc);
  672. }
  673. static const char *
  674. wifi_essid(const char *iface)
  675. {
  676. static char id[IW_ESSID_MAX_SIZE+1];
  677. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  678. struct iwreq wreq;
  679. memset(&wreq, 0, sizeof(struct iwreq));
  680. wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
  681. snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
  682. if (sockfd == -1) {
  683. warn("Failed to get ESSID for interface %s", iface);
  684. return UNKNOWN_STR;
  685. }
  686. wreq.u.essid.pointer = id;
  687. if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
  688. warn("Failed to get ESSID for interface %s", iface);
  689. return UNKNOWN_STR;
  690. }
  691. close(sockfd);
  692. if (strcmp(id, "") == 0)
  693. return UNKNOWN_STR;
  694. else
  695. return id;
  696. }
  697. static void
  698. sighandler(const int signo)
  699. {
  700. if (signo == SIGTERM || signo == SIGINT) {
  701. done = 1;
  702. }
  703. }
  704. static void
  705. usage(void)
  706. {
  707. fprintf(stderr, "usage: %s [-s]\n", argv0);
  708. exit(1);
  709. }
  710. int
  711. main(int argc, char *argv[])
  712. {
  713. struct arg argument;
  714. struct sigaction act;
  715. size_t i, len;
  716. int sflag = 0;
  717. char status_string[MAXLEN];
  718. char *element;
  719. ARGBEGIN {
  720. case 's':
  721. sflag = 1;
  722. break;
  723. default:
  724. usage();
  725. } ARGEND
  726. if (argc) {
  727. usage();
  728. }
  729. memset(&act, 0, sizeof(act));
  730. act.sa_handler = sighandler;
  731. sigaction(SIGINT, &act, 0);
  732. sigaction(SIGTERM, &act, 0);
  733. if (!sflag) {
  734. dpy = XOpenDisplay(NULL);
  735. if (!dpy) {
  736. fprintf(stderr, "slstatus: cannot open display");
  737. exit(1);
  738. }
  739. }
  740. setlocale(LC_ALL, "");
  741. while (!done) {
  742. status_string[0] = '\0';
  743. for (element = status_string, i = len = 0; i < LEN(args);
  744. ++i, element += len) {
  745. argument = args[i];
  746. len = snprintf(element, sizeof(status_string)-1 - len,
  747. argument.fmt,
  748. argument.func(argument.args));
  749. if (len >= sizeof(status_string)) {
  750. status_string[sizeof(status_string)-1] = '\0';
  751. break;
  752. }
  753. }
  754. if (sflag) {
  755. printf("%s\n", status_string);
  756. } else {
  757. XStoreName(dpy, DefaultRootWindow(dpy), status_string);
  758. XSync(dpy, False);
  759. }
  760. if ((UPDATE_INTERVAL - delay) <= 0) {
  761. delay = 0;
  762. continue;
  763. } else {
  764. sleep(UPDATE_INTERVAL - delay);
  765. delay = 0;
  766. }
  767. }
  768. if (!sflag) {
  769. XStoreName(dpy, DefaultRootWindow(dpy), NULL);
  770. XCloseDisplay(dpy);
  771. }
  772. return 0;
  773. }