slstatus.c 17 KB

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