DWM Status Bar
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.

229 lines
4.0 KiB

7 years ago
  1. /*
  2. * Copy me if you can.
  3. * by 20h
  4. */
  5. #define _BSD_SOURCE
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdarg.h>
  10. #include <string.h>
  11. #include <strings.h>
  12. #include <sys/time.h>
  13. #include <time.h>
  14. #include <sys/types.h>
  15. #include <sys/wait.h>
  16. #include <X11/Xlib.h>
  17. char *tzargentina = "America/Buenos_Aires";
  18. char *tzutc = "UTC";
  19. char *tzberlin = "Europe/Berlin";
  20. static Display *dpy;
  21. char *
  22. smprintf(char *fmt, ...)
  23. {
  24. va_list fmtargs;
  25. char *ret;
  26. int len;
  27. va_start(fmtargs, fmt);
  28. len = vsnprintf(NULL, 0, fmt, fmtargs);
  29. va_end(fmtargs);
  30. ret = malloc(++len);
  31. if (ret == NULL) {
  32. perror("malloc");
  33. exit(1);
  34. }
  35. va_start(fmtargs, fmt);
  36. vsnprintf(ret, len, fmt, fmtargs);
  37. va_end(fmtargs);
  38. return ret;
  39. }
  40. void
  41. settz(char *tzname)
  42. {
  43. setenv("TZ", tzname, 1);
  44. }
  45. char *
  46. mktimes(char *fmt, char *tzname)
  47. {
  48. char buf[129];
  49. time_t tim;
  50. struct tm *timtm;
  51. settz(tzname);
  52. tim = time(NULL);
  53. timtm = localtime(&tim);
  54. if (timtm == NULL)
  55. return smprintf("");
  56. if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) {
  57. fprintf(stderr, "strftime == 0\n");
  58. return smprintf("");
  59. }
  60. return smprintf("%s", buf);
  61. }
  62. void
  63. setstatus(char *str)
  64. {
  65. XStoreName(dpy, DefaultRootWindow(dpy), str);
  66. XSync(dpy, False);
  67. }
  68. char *
  69. loadavg(void)
  70. {
  71. double avgs[3];
  72. if (getloadavg(avgs, 3) < 0)
  73. return smprintf("");
  74. return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
  75. }
  76. char *
  77. readfile(char *base, char *file)
  78. {
  79. char *path, line[513];
  80. FILE *fd;
  81. memset(line, 0, sizeof(line));
  82. path = smprintf("%s/%s", base, file);
  83. fd = fopen(path, "r");
  84. free(path);
  85. if (fd == NULL)
  86. return NULL;
  87. if (fgets(line, sizeof(line)-1, fd) == NULL)
  88. return NULL;
  89. fclose(fd);
  90. return smprintf("%s", line);
  91. }
  92. char *
  93. getbattery(char *base)
  94. {
  95. char *co, status;
  96. int descap, remcap;
  97. descap = -1;
  98. remcap = -1;
  99. co = readfile(base, "present");
  100. if (co == NULL)
  101. return smprintf("");
  102. if (co[0] != '1') {
  103. free(co);
  104. return smprintf("not present");
  105. }
  106. free(co);
  107. co = readfile(base, "charge_full_design");
  108. if (co == NULL) {
  109. co = readfile(base, "energy_full_design");
  110. if (co == NULL)
  111. return smprintf("");
  112. }
  113. sscanf(co, "%d", &descap);
  114. free(co);
  115. co = readfile(base, "charge_now");
  116. if (co == NULL) {
  117. co = readfile(base, "energy_now");
  118. if (co == NULL)
  119. return smprintf("");
  120. }
  121. sscanf(co, "%d", &remcap);
  122. free(co);
  123. co = readfile(base, "status");
  124. if (!strncmp(co, "Discharging", 11)) {
  125. status = '-';
  126. } else if(!strncmp(co, "Charging", 8)) {
  127. status = '+';
  128. } else {
  129. status = '?';
  130. }
  131. if (remcap < 0 || descap < 0)
  132. return smprintf("invalid");
  133. return smprintf("%.0f%%%c", ((float)remcap / (float)descap) * 100, status);
  134. }
  135. char *
  136. gettemperature(char *base, char *sensor)
  137. {
  138. char *co;
  139. co = readfile(base, sensor);
  140. if (co == NULL)
  141. return smprintf("");
  142. return smprintf("%02.0f°C", atof(co) / 1000);
  143. }
  144. int
  145. main(void)
  146. {
  147. char *status;
  148. char *avgs;
  149. char *bat;
  150. char *bat1;
  151. char *tmar;
  152. char *tmutc;
  153. char *tmbln;
  154. char *t0, *t1, *t2, *t3;
  155. if (!(dpy = XOpenDisplay(NULL))) {
  156. fprintf(stderr, "dwmstatus: cannot open display.\n");
  157. return 1;
  158. }
  159. for (;;sleep(60)) {
  160. avgs = loadavg();
  161. bat = getbattery("/sys/class/power_supply/BAT0");
  162. bat1 = getbattery("/sys/class/power_supply/BAT1");
  163. tmar = mktimes("%H:%M", tzargentina);
  164. tmutc = mktimes("%H:%M", tzutc);
  165. tmbln = mktimes("KW %W %a %d %b %H:%M %Z %Y", tzberlin);
  166. t0 = gettemperature("/sys/devices/virtual/hwmon/hwmon1", "temp1_input");
  167. t1 = gettemperature("/sys/devices/platform/coretemp.0/hwmon/hwmon3", "temp1_input");
  168. t2 = gettemperature("/sys/devices/platform/coretemp.0/hwmon/hwmon3", "temp2_input");
  169. t3 = gettemperature("/sys/devices/platform/coretemp.0/hwmon/hwmon3", "temp3_input");
  170. status = smprintf("T:%s|%s|%s|%s L:%s B:%s|%s A:%s U:%s %s",
  171. t0, t1, t2, t3, avgs, bat, bat1, tmar, tmutc,
  172. tmbln);
  173. setstatus(status);
  174. free(t0);
  175. free(t1);
  176. free(t2);
  177. free(t3);
  178. free(avgs);
  179. free(bat);
  180. free(bat1);
  181. free(tmar);
  182. free(tmutc);
  183. free(tmbln);
  184. free(status);
  185. }
  186. XCloseDisplay(dpy);
  187. return 0;
  188. }