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.
 
 
 
 

642 rivejä
13 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <alsa/asoundlib.h>
  3. #include <err.h>
  4. #include <fcntl.h>
  5. #include <ifaddrs.h>
  6. #include <limits.h>
  7. #include <linux/wireless.h>
  8. #include <netdb.h>
  9. #include <pwd.h>
  10. #include <signal.h>
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/stat.h>
  17. #include <sys/statvfs.h>
  18. #include <sys/socket.h>
  19. #include <sys/sysinfo.h>
  20. #include <sys/types.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include <X11/Xlib.h>
  24. #undef strlcat
  25. #undef strlcpy
  26. #include "strlcat.h"
  27. #include "strlcpy.h"
  28. #include "concat.h"
  29. char concat[];
  30. struct arg {
  31. char *(*func)();
  32. const char *format;
  33. const char *args;
  34. };
  35. static char *smprintf(const char *, ...);
  36. static char *battery_perc(const char *);
  37. static char *cpu_perc(void);
  38. static char *datetime(const char *);
  39. static char *disk_free(const char *);
  40. static char *disk_perc(const char *);
  41. static char *disk_total(const char *);
  42. static char *disk_used(const char *);
  43. static char *entropy(void);
  44. static char *gid(void);
  45. static char *hostname(void);
  46. static char *ip(const char *);
  47. static char *load_avg(void);
  48. static char *ram_free(void);
  49. static char *ram_perc(void);
  50. static char *ram_used(void);
  51. static char *ram_total(void);
  52. static char *run_command(const char *);
  53. static char *temp(const char *);
  54. static char *uid(void);
  55. static char *uptime(void);
  56. static char *username(void);
  57. static char *vol_perc(const char *);
  58. static char *wifi_perc(const char *);
  59. static char *wifi_essid(const char *);
  60. static void sighandler(const int);
  61. static unsigned short int delay;
  62. static Display *dpy;
  63. static int done = 0;
  64. #include "config.h"
  65. static char *
  66. smprintf(const char *fmt, ...)
  67. {
  68. va_list ap;
  69. char *ret;
  70. int len;
  71. va_start(ap, fmt);
  72. len = vsnprintf(NULL, 0, fmt, ap);
  73. va_end(ap);
  74. ret = malloc(++len);
  75. if (ret == NULL) {
  76. perror("malloc");
  77. exit(1);
  78. }
  79. va_start(ap, fmt);
  80. vsnprintf(ret, len, fmt, ap);
  81. va_end(ap);
  82. return ret;
  83. }
  84. static char *
  85. battery_perc(const char *battery)
  86. {
  87. int now, full, perc;
  88. FILE *fp;
  89. ccat(4, BATTERY_PATH, battery, "/", BATTERY_NOW);
  90. fp = fopen(concat, "r");
  91. if (fp == NULL) {
  92. warn("Error opening battery file: %s", concat);
  93. return smprintf(UNKNOWN_STR);
  94. }
  95. fscanf(fp, "%i", &now);
  96. fclose(fp);
  97. ccat(4, BATTERY_PATH, battery, "/", BATTERY_FULL);
  98. fp = fopen(concat, "r");
  99. if (fp == NULL) {
  100. warn("Error opening battery file: %s", concat);
  101. return smprintf(UNKNOWN_STR);
  102. }
  103. fscanf(fp, "%i", &full);
  104. fclose(fp);
  105. perc = now / (full / 100);
  106. return smprintf("%d%%", perc);
  107. }
  108. static char *
  109. cpu_perc(void)
  110. {
  111. int perc;
  112. long double a[4], b[4];
  113. FILE *fp = fopen("/proc/stat","r");
  114. if (fp == NULL) {
  115. warn("Error opening stat file");
  116. return smprintf(UNKNOWN_STR);
  117. }
  118. fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
  119. fclose(fp);
  120. delay = (UPDATE_INTERVAL - (UPDATE_INTERVAL - 1));
  121. sleep(delay);
  122. fp = fopen("/proc/stat","r");
  123. if (fp == NULL) {
  124. warn("Error opening stat file");
  125. return smprintf(UNKNOWN_STR);
  126. }
  127. fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
  128. fclose(fp);
  129. 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]));
  130. return smprintf("%d%%", perc);
  131. }
  132. static char *
  133. datetime(const char *timeformat)
  134. {
  135. time_t t;
  136. char timestr[80];
  137. t = time(NULL);
  138. if (strftime(timestr, sizeof(timestr), timeformat, localtime(&t)) == 0)
  139. return smprintf(UNKNOWN_STR);
  140. return smprintf("%s", timestr);
  141. }
  142. static char *
  143. disk_free(const char *mountpoint)
  144. {
  145. struct statvfs fs;
  146. if (statvfs(mountpoint, &fs) < 0) {
  147. warn("Could not get filesystem info");
  148. return smprintf(UNKNOWN_STR);
  149. }
  150. return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
  151. }
  152. static char *
  153. disk_perc(const char *mountpoint)
  154. {
  155. int perc = 0;
  156. struct statvfs fs;
  157. if (statvfs(mountpoint, &fs) < 0) {
  158. warn("Could not get filesystem info");
  159. return smprintf(UNKNOWN_STR);
  160. }
  161. perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
  162. return smprintf("%d%%", perc);
  163. }
  164. static char *
  165. disk_total(const char *mountpoint)
  166. {
  167. struct statvfs fs;
  168. if (statvfs(mountpoint, &fs) < 0) {
  169. warn("Could not get filesystem info");
  170. return smprintf(UNKNOWN_STR);
  171. }
  172. return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
  173. }
  174. static char *
  175. disk_used(const char *mountpoint)
  176. {
  177. struct statvfs fs;
  178. if (statvfs(mountpoint, &fs) < 0) {
  179. warn("Could not get filesystem info");
  180. return smprintf(UNKNOWN_STR);
  181. }
  182. return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
  183. }
  184. static char *
  185. entropy(void)
  186. {
  187. int entropy = 0;
  188. FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r");
  189. if (fp == NULL) {
  190. warn("Could not open entropy file");
  191. return smprintf(UNKNOWN_STR);
  192. }
  193. fscanf(fp, "%d", &entropy);
  194. fclose(fp);
  195. return smprintf("%d", entropy);
  196. }
  197. static char *
  198. gid(void)
  199. {
  200. return smprintf("%d", getgid());
  201. }
  202. static char *
  203. hostname(void)
  204. {
  205. char hostname[HOST_NAME_MAX];
  206. FILE *fp = fopen("/proc/sys/kernel/hostname", "r");
  207. if (fp == NULL) {
  208. warn("Could not open hostname file");
  209. return smprintf(UNKNOWN_STR);
  210. }
  211. fgets(hostname, sizeof(hostname), fp);
  212. /* FIXME: needs improvement */
  213. memset(&hostname[strlen(hostname)-1], '\0',
  214. sizeof(hostname) - strlen(hostname));
  215. fclose(fp);
  216. return smprintf("%s", hostname);
  217. }
  218. static char *
  219. ip(const char *interface)
  220. {
  221. struct ifaddrs *ifaddr, *ifa;
  222. int s;
  223. char host[NI_MAXHOST];
  224. if (getifaddrs(&ifaddr) == -1) {
  225. warn("Error getting IP address");
  226. return smprintf(UNKNOWN_STR);
  227. }
  228. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  229. if (ifa->ifa_addr == NULL)
  230. continue;
  231. s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST,
  232. NULL, 0, NI_NUMERICHOST);
  233. if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) {
  234. if (s != 0) {
  235. warnx("Error getting IP address.");
  236. return smprintf(UNKNOWN_STR);
  237. }
  238. return smprintf("%s", host);
  239. }
  240. }
  241. freeifaddrs(ifaddr);
  242. return smprintf(UNKNOWN_STR);
  243. }
  244. static char *
  245. load_avg(void)
  246. {
  247. double avgs[3];
  248. if (getloadavg(avgs, 3) < 0) {
  249. warnx("Error getting load avg.");
  250. return smprintf(UNKNOWN_STR);
  251. }
  252. return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
  253. }
  254. static char *
  255. ram_free(void)
  256. {
  257. long free;
  258. FILE *fp = fopen("/proc/meminfo", "r");
  259. if (fp == NULL) {
  260. warn("Error opening meminfo file");
  261. return smprintf(UNKNOWN_STR);
  262. }
  263. fscanf(fp, "MemFree: %ld kB\n", &free);
  264. fclose(fp);
  265. return smprintf("%f", (float)free / 1024 / 1024);
  266. }
  267. static char *
  268. ram_perc(void)
  269. {
  270. int perc;
  271. long total, free, buffers, cached;
  272. FILE *fp = fopen("/proc/meminfo", "r");
  273. if (fp == NULL) {
  274. warn("Error opening meminfo file");
  275. return smprintf(UNKNOWN_STR);
  276. }
  277. fscanf(fp, "MemTotal: %ld kB\n", &total);
  278. fscanf(fp, "MemFree: %ld kB\n", &free);
  279. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  280. fscanf(fp, "Cached: %ld kB\n", &cached);
  281. fclose(fp);
  282. perc = 100 * ((total - free) - (buffers + cached)) / total;
  283. return smprintf("%d%%", perc);
  284. }
  285. static char *
  286. ram_total(void)
  287. {
  288. long total;
  289. FILE *fp = fopen("/proc/meminfo", "r");
  290. if (fp == NULL) {
  291. warn("Error opening meminfo file");
  292. return smprintf(UNKNOWN_STR);
  293. }
  294. fscanf(fp, "MemTotal: %ld kB\n", &total);
  295. fclose(fp);
  296. return smprintf("%f", (float)total / 1024 / 1024);
  297. }
  298. static char *
  299. ram_used(void)
  300. {
  301. long free, total, buffers, cached, used;
  302. FILE *fp = fopen("/proc/meminfo", "r");
  303. if (fp == NULL) {
  304. warn("Error opening meminfo file");
  305. return smprintf(UNKNOWN_STR);
  306. }
  307. fscanf(fp, "MemTotal: %ld kB\n", &total);
  308. fscanf(fp, "MemFree: %ld kB\n", &free);
  309. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  310. fscanf(fp, "Cached: %ld kB\n", &cached);
  311. fclose(fp);
  312. used = total - free - buffers - cached;
  313. return smprintf("%f", (float)used / 1024 / 1024);
  314. }
  315. static char *
  316. run_command(const char* command)
  317. {
  318. int good;
  319. FILE *fp = popen(command, "r");
  320. char buffer[64] = "";
  321. if (fp == NULL) {
  322. warn("Could not get command output for: %s", command);
  323. return smprintf(UNKNOWN_STR);
  324. }
  325. fgets(buffer, sizeof(buffer)-1, fp);
  326. pclose(fp);
  327. for (int i = 0 ; i != sizeof(buffer); i++) {
  328. if (buffer[i] == '\0') {
  329. good = 1;
  330. break;
  331. }
  332. }
  333. if (good)
  334. buffer[strlen(buffer)-1] = '\0';
  335. return smprintf("%s", buffer);
  336. }
  337. static char *
  338. temp(const char *file)
  339. {
  340. int temperature;
  341. FILE *fp = fopen(file, "r");
  342. if (fp == NULL) {
  343. warn("Could not open temperature file");
  344. return smprintf(UNKNOWN_STR);
  345. }
  346. fscanf(fp, "%d", &temperature);
  347. fclose(fp);
  348. return smprintf("%d°C", temperature / 1000);
  349. }
  350. static char *
  351. uptime(void)
  352. {
  353. struct sysinfo info;
  354. int hours = 0;
  355. int minutes = 0;
  356. sysinfo(&info);
  357. hours = info.uptime / 3600;
  358. minutes = (info.uptime - hours * 3600 ) / 60;
  359. return smprintf("%dh %dm", hours, minutes);
  360. }
  361. static char *
  362. username(void)
  363. {
  364. uid_t uid = geteuid();
  365. struct passwd *pw = getpwuid(uid);
  366. if (pw == NULL) {
  367. warn("Could not get username");
  368. return smprintf(UNKNOWN_STR);
  369. }
  370. return smprintf("%s", pw->pw_name);
  371. }
  372. static char *
  373. uid(void)
  374. {
  375. return smprintf("%d", geteuid());
  376. }
  377. static char *
  378. vol_perc(const char *snd_card)
  379. { /* FIX THIS SHIT! */
  380. long int vol, max, min;
  381. snd_mixer_t *handle;
  382. snd_mixer_elem_t *elem;
  383. snd_mixer_selem_id_t *s_elem;
  384. snd_mixer_open(&handle, 0);
  385. snd_mixer_attach(handle, snd_card);
  386. snd_mixer_selem_register(handle, NULL, NULL);
  387. snd_mixer_load(handle);
  388. snd_mixer_selem_id_malloc(&s_elem);
  389. snd_mixer_selem_id_set_name(s_elem, ALSA_CHANNEL);
  390. elem = snd_mixer_find_selem(handle, s_elem);
  391. if (elem == NULL) {
  392. snd_mixer_selem_id_free(s_elem);
  393. snd_mixer_close(handle);
  394. warn("error: ALSA");
  395. return smprintf(UNKNOWN_STR);
  396. }
  397. snd_mixer_handle_events(handle);
  398. snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
  399. snd_mixer_selem_get_playback_volume(elem, 0, &vol);
  400. snd_mixer_selem_id_free(s_elem);
  401. snd_mixer_close(handle);
  402. return smprintf("%d", ((uint_fast16_t)(vol * 100) / max));
  403. }
  404. static char *
  405. wifi_perc(const char *wificard)
  406. {
  407. int strength;
  408. char buf[255];
  409. char *datastart;
  410. char status[5];
  411. FILE *fp;
  412. ccat(3, "/sys/class/net/", wificard, "/operstate");
  413. fp = fopen(concat, "r");
  414. if (fp == NULL) {
  415. warn("Error opening wifi operstate file");
  416. return smprintf(UNKNOWN_STR);
  417. }
  418. fgets(status, 5, fp);
  419. fclose(fp);
  420. if(strcmp(status, "up\n") != 0)
  421. return smprintf(UNKNOWN_STR);
  422. fp = fopen("/proc/net/wireless", "r");
  423. if (fp == NULL) {
  424. warn("Error opening wireless file");
  425. return smprintf(UNKNOWN_STR);
  426. }
  427. ccat(2, wificard, ":");
  428. fgets(buf, sizeof(buf), fp);
  429. fgets(buf, sizeof(buf), fp);
  430. fgets(buf, sizeof(buf), fp);
  431. datastart = strstr(buf, concat);
  432. if (datastart != NULL) {
  433. datastart = strstr(buf, ":");
  434. sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
  435. }
  436. fclose(fp);
  437. return smprintf("%d%%", strength);
  438. }
  439. static char *
  440. wifi_essid(const char *wificard)
  441. {
  442. char id[IW_ESSID_MAX_SIZE+1];
  443. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  444. struct iwreq wreq;
  445. memset(&wreq, 0, sizeof(struct iwreq));
  446. wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
  447. sprintf(wreq.ifr_name, wificard);
  448. if (sockfd == -1) {
  449. warn("Cannot open socket for interface: %s", wificard);
  450. return smprintf(UNKNOWN_STR);
  451. }
  452. wreq.u.essid.pointer = id;
  453. if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
  454. warn("Get ESSID ioctl failed for interface %s", wificard);
  455. return smprintf(UNKNOWN_STR);
  456. }
  457. close(sockfd);
  458. if (strcmp((char *)wreq.u.essid.pointer, "") == 0)
  459. return smprintf(UNKNOWN_STR);
  460. else
  461. return smprintf("%s", (char *)wreq.u.essid.pointer);
  462. }
  463. static void
  464. sighandler(const int signo)
  465. {
  466. if (signo == SIGTERM || signo == SIGINT) {
  467. done = 1;
  468. }
  469. }
  470. int
  471. main(void)
  472. {
  473. size_t i;
  474. char status_string[4096];
  475. char *res, *element, *status_old;
  476. struct arg argument;
  477. struct sigaction act;
  478. memset(&act, 0, sizeof(act));
  479. act.sa_handler = sighandler;
  480. sigaction(SIGINT, &act, 0);
  481. sigaction(SIGTERM, &act, 0);
  482. dpy = XOpenDisplay(NULL);
  483. XFetchName(dpy, DefaultRootWindow(dpy), &status_old);
  484. while (!done) {
  485. status_string[0] = '\0';
  486. for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
  487. argument = args[i];
  488. if (argument.args == NULL)
  489. res = argument.func();
  490. else
  491. res = argument.func(argument.args);
  492. element = smprintf(argument.format, res);
  493. if (element == NULL) {
  494. element = smprintf(UNKNOWN_STR);
  495. warnx("Failed to format output.");
  496. }
  497. strlcat(status_string, element, sizeof(status_string));
  498. free(res);
  499. free(element);
  500. }
  501. XStoreName(dpy, DefaultRootWindow(dpy), status_string);
  502. XSync(dpy, False);
  503. /*
  504. * subtract delay time spend in function
  505. * calls from the actual global delay time
  506. */
  507. sleep(UPDATE_INTERVAL - delay);
  508. delay = 0;
  509. }
  510. XStoreName(dpy, DefaultRootWindow(dpy), status_old);
  511. XSync(dpy, False);
  512. XCloseDisplay(dpy);
  513. return 0;
  514. }