My slstatus configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

759 lines
16 KiB

  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. /* load avg */
  282. char *
  283. load_avg(const char *null)
  284. {
  285. double avgs[3];
  286. /* try to get load avg */
  287. if (getloadavg(avgs, 3) < 0) {
  288. fprintf(stderr, "Error getting load avg.\n");
  289. return smprintf(unknowntext);
  290. }
  291. /* return it */
  292. return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
  293. }
  294. /* ram free */
  295. char *
  296. ram_free(const char *null)
  297. {
  298. long free;
  299. FILE *fp;
  300. /* open meminfo file */
  301. if (!(fp = fopen("/proc/meminfo", "r"))) {
  302. fprintf(stderr, "Error opening meminfo file.\n");
  303. return smprintf(unknowntext);
  304. }
  305. /* read the values */
  306. fscanf(fp, "MemFree: %ld kB\n", &free);
  307. /* close meminfo file */
  308. fclose(fp);
  309. /* return free ram as string */
  310. return smprintf("%f", (float)free / 1024 / 1024);
  311. }
  312. /* ram percentage */
  313. char *
  314. ram_perc(const char *null)
  315. {
  316. int perc;
  317. long total, free, buffers, cached;
  318. FILE *fp;
  319. /* open meminfo file */
  320. if (!(fp = fopen("/proc/meminfo", "r"))) {
  321. fprintf(stderr, "Error opening meminfo file.\n");
  322. return smprintf(unknowntext);
  323. }
  324. /* read the values */
  325. fscanf(fp, "MemTotal: %ld kB\n", &total);
  326. fscanf(fp, "MemFree: %ld kB\n", &free);
  327. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  328. fscanf(fp, "Cached: %ld kB\n", &cached);
  329. /* close meminfo file */
  330. fclose(fp);
  331. /* calculate percentage */
  332. perc = 100 * ((total - free) - (buffers + cached)) / total;
  333. /* return perc as string */
  334. return smprintf("%d%%", perc);
  335. }
  336. /* ram total */
  337. char *
  338. ram_total(const char *null)
  339. {
  340. long total;
  341. FILE *fp;
  342. /* open meminfo file */
  343. if (!(fp = fopen("/proc/meminfo", "r"))) {
  344. fprintf(stderr, "Error opening meminfo file.\n");
  345. return smprintf(unknowntext);
  346. }
  347. /* read the values */
  348. fscanf(fp, "MemTotal: %ld kB\n", &total);
  349. /* close meminfo file */
  350. fclose(fp);
  351. /* return total ram as string */
  352. return smprintf("%f", (float)total / 1024 / 1024);
  353. }
  354. /* ram used */
  355. char *
  356. ram_used(const char *null)
  357. {
  358. long free, total, buffers, cached, used;
  359. FILE *fp;
  360. /* open meminfo file */
  361. if (!(fp = fopen("/proc/meminfo", "r"))) {
  362. fprintf(stderr, "Error opening meminfo file.\n");
  363. return smprintf(unknowntext);
  364. }
  365. /* read the values */
  366. fscanf(fp, "MemTotal: %ld kB\n", &total);
  367. fscanf(fp, "MemFree: %ld kB\n", &free);
  368. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  369. fscanf(fp, "Cached: %ld kB\n", &cached);
  370. /* close meminfo file */
  371. fclose(fp);
  372. /* calculate used */
  373. used = total - free - buffers - cached;
  374. /* return used ram as string */
  375. return smprintf("%f", (float)used / 1024 / 1024);
  376. }
  377. /* custom shell command */
  378. char *
  379. run_command(const char* command)
  380. {
  381. int good;
  382. FILE *fp;
  383. char buffer[64];
  384. /* try to open the command output */
  385. if (!(fp = popen(command, "r"))) {
  386. fprintf(stderr, "Could not get command output for: %s.\n", command);
  387. return smprintf(unknowntext);
  388. }
  389. /* get command output text, save it to buffer */
  390. fgets(buffer, sizeof(buffer) - 1, fp);
  391. /* close it again */
  392. pclose(fp);
  393. /* add nullchar at the end */
  394. for (int i = 0 ; i != sizeof(buffer); i++) {
  395. if (buffer[i] == '\0') {
  396. good = 1;
  397. break;
  398. }
  399. }
  400. if (good) {
  401. buffer[strlen(buffer) - 1] = '\0';
  402. }
  403. /* return the output */
  404. return smprintf("%s", buffer);
  405. }
  406. /* temperature */
  407. char *
  408. temp(const char *file)
  409. {
  410. int temperature;
  411. FILE *fp;
  412. /* open temperature file */
  413. if (!(fp = fopen(file, "r"))) {
  414. fprintf(stderr, "Could not open temperature file.\n");
  415. return smprintf(unknowntext);
  416. }
  417. /* extract temperature */
  418. fscanf(fp, "%d", &temperature);
  419. /* close temperature file */
  420. fclose(fp);
  421. /* return temperature in degrees */
  422. return smprintf("%d°C", temperature / 1000);
  423. }
  424. /* username */
  425. char *
  426. username(const char *null)
  427. {
  428. register struct passwd *pw;
  429. register uid_t uid;
  430. /* get the values */
  431. uid = geteuid();
  432. pw = getpwuid(uid);
  433. /* if it worked, return */
  434. if (pw) {
  435. return smprintf("%s", pw->pw_name);
  436. } else {
  437. fprintf(stderr, "Could not get username.\n");
  438. return smprintf(unknowntext);
  439. }
  440. return smprintf(unknowntext);
  441. }
  442. /* uid */
  443. char *
  444. uid(const char *null)
  445. {
  446. register uid_t uid;
  447. /* get the values */
  448. uid = geteuid();
  449. /* if it worked, return */
  450. if (uid) {
  451. return smprintf("%d", uid);
  452. } else {
  453. fprintf(stderr, "Could not get uid.\n");
  454. return smprintf(unknowntext);
  455. }
  456. return smprintf(unknowntext);
  457. }
  458. /* alsa volume percentage */
  459. char *
  460. vol_perc(const char *soundcard)
  461. {
  462. int mute = 0;
  463. long vol = 0, max = 0, min = 0;
  464. snd_mixer_t *handle;
  465. snd_mixer_elem_t *pcm_mixer, *mas_mixer;
  466. snd_mixer_selem_id_t *vol_info, *mute_info;
  467. /* open everything */
  468. snd_mixer_open(&handle, 0);
  469. snd_mixer_attach(handle, soundcard);
  470. snd_mixer_selem_register(handle, NULL, NULL);
  471. snd_mixer_load(handle);
  472. /* prepare everything */
  473. snd_mixer_selem_id_malloc(&vol_info);
  474. snd_mixer_selem_id_malloc(&mute_info);
  475. /* check */
  476. if (vol_info == NULL || mute_info == NULL) {
  477. fprintf(stderr, "Could not get alsa volume.\n");
  478. return smprintf(unknowntext);
  479. }
  480. snd_mixer_selem_id_set_name(vol_info, channel);
  481. snd_mixer_selem_id_set_name(mute_info, channel);
  482. pcm_mixer = snd_mixer_find_selem(handle, vol_info);
  483. mas_mixer = snd_mixer_find_selem(handle, mute_info);
  484. /* get the info */
  485. snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max);
  486. snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol);
  487. snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute);
  488. /* clean up */
  489. if (vol_info) {
  490. snd_mixer_selem_id_free(vol_info);
  491. }
  492. if (mute_info) {
  493. snd_mixer_selem_id_free(mute_info);
  494. }
  495. if (handle) {
  496. snd_mixer_close(handle);
  497. }
  498. /* return the string (mute) */
  499. if (!mute) {
  500. return smprintf("mute");
  501. } else {
  502. return smprintf("%d%%", (vol * 100) / max);
  503. }
  504. }
  505. /* wifi percentage */
  506. char *
  507. wifi_perc(const char *wificard)
  508. {
  509. int bufsize = 255;
  510. int strength;
  511. char buf[bufsize];
  512. char *datastart;
  513. char path[64];
  514. char status[5];
  515. char needle[sizeof wificard + 1];
  516. FILE *fp;
  517. /* generate the path name */
  518. memset(path, 0, sizeof path);
  519. strcat(path, "/sys/class/net/");
  520. strcat(path, wificard);
  521. strcat(path, "/operstate");
  522. /* open wifi file */
  523. if(!(fp = fopen(path, "r"))) {
  524. fprintf(stderr, "Error opening wifi operstate file.\n");
  525. return smprintf(unknowntext);
  526. }
  527. /* read the status */
  528. fgets(status, 5, fp);
  529. /* close wifi file */
  530. fclose(fp);
  531. /* check if interface down */
  532. if(strcmp(status, "up\n") != 0) {
  533. return smprintf(unknowntext);
  534. }
  535. /* open wifi file */
  536. if (!(fp = fopen("/proc/net/wireless", "r"))) {
  537. fprintf(stderr, "Error opening wireless file.\n");
  538. return smprintf(unknowntext);
  539. }
  540. /* extract the signal strength */
  541. strcpy(needle, wificard);
  542. strcat(needle, ":");
  543. fgets(buf, bufsize, fp);
  544. fgets(buf, bufsize, fp);
  545. fgets(buf, bufsize, fp);
  546. if ((datastart = strstr(buf, needle)) != NULL) {
  547. datastart = strstr(buf, ":");
  548. sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
  549. }
  550. /* close wifi file */
  551. fclose(fp);
  552. /* return strength in percent */
  553. return smprintf("%d%%", strength);
  554. }
  555. /* wifi essid */
  556. char *
  557. wifi_essid(const char *wificard)
  558. {
  559. char id[IW_ESSID_MAX_SIZE+1];
  560. int sockfd;
  561. struct iwreq wreq;
  562. /* prepare */
  563. memset(&wreq, 0, sizeof(struct iwreq));
  564. wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
  565. /* set the interface */
  566. sprintf(wreq.ifr_name, wificard);
  567. /* check */
  568. if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  569. fprintf(stderr, "Cannot open socket for interface: %s\n", wificard);
  570. return smprintf(unknowntext);
  571. }
  572. wreq.u.essid.pointer = id;
  573. if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
  574. fprintf(stderr, "Get ESSID ioctl failed for interface %s\n", wificard);
  575. return smprintf(unknowntext);
  576. }
  577. /* return the essid */
  578. if (strcmp((char *)wreq.u.essid.pointer, "") == 0) {
  579. return smprintf(unknowntext);
  580. } else {
  581. return smprintf("%s", (char *)wreq.u.essid.pointer);
  582. }
  583. }
  584. /* main function */
  585. int
  586. main(void)
  587. {
  588. char status_string[1024];
  589. struct arg argument;
  590. /* try to open display */
  591. if (!(dpy = XOpenDisplay(0x0))) {
  592. fprintf(stderr, "Cannot open display!\n");
  593. exit(1);
  594. }
  595. /* return status every interval */
  596. for (;;) {
  597. /* clear the string */
  598. memset(status_string, 0, sizeof(status_string));
  599. /* generate status_string */
  600. for (size_t i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
  601. argument = args[i];
  602. char *res = argument.func(argument.args);
  603. char *element = smprintf(argument.format, res);
  604. if (element == NULL) {
  605. element = smprintf(unknowntext);
  606. fprintf(stderr, "Failed to format output.\n");
  607. }
  608. strcat(status_string, element);
  609. free(res);
  610. free(element);
  611. }
  612. /* return the statusbar */
  613. setstatus(status_string);
  614. /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
  615. sleep(update_interval -1);
  616. }
  617. /* close display */
  618. XCloseDisplay(dpy);
  619. /* exit successfully */
  620. return 0;
  621. }