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.

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