My slstatus configuration
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

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