slstatus.c 15 KB

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