My slstatus configuration
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

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