dwmstatus.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 *tzchile = "America/Santiago";
  18. static Display *dpy;
  19. char *
  20. smprintf(char *fmt, ...)
  21. {
  22. va_list fmtargs;
  23. char *ret;
  24. int len;
  25. va_start(fmtargs, fmt);
  26. len = vsnprintf(NULL, 0, fmt, fmtargs);
  27. va_end(fmtargs);
  28. ret = malloc(++len);
  29. if (ret == NULL) {
  30. perror("malloc");
  31. exit(1);
  32. }
  33. va_start(fmtargs, fmt);
  34. vsnprintf(ret, len, fmt, fmtargs);
  35. va_end(fmtargs);
  36. return ret;
  37. }
  38. void
  39. settz(char *tzname)
  40. {
  41. setenv("TZ", tzname, 1);
  42. }
  43. char *
  44. mktimes(char *fmt, char *tzname)
  45. {
  46. char buf[129];
  47. time_t tim;
  48. struct tm *timtm;
  49. settz(tzname);
  50. tim = time(NULL);
  51. timtm = localtime(&tim);
  52. if (timtm == NULL)
  53. return smprintf("");
  54. if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) {
  55. fprintf(stderr, "strftime == 0\n");
  56. return smprintf("");
  57. }
  58. return smprintf("%s", buf);
  59. }
  60. void
  61. setstatus(char *str)
  62. {
  63. XStoreName(dpy, DefaultRootWindow(dpy), str);
  64. XSync(dpy, False);
  65. }
  66. char *
  67. readfile(char *base, char *file)
  68. {
  69. char *path, line[513];
  70. FILE *fd;
  71. memset(line, 0, sizeof(line));
  72. path = smprintf("%s/%s", base, file);
  73. fd = fopen(path, "r");
  74. free(path);
  75. if (fd == NULL)
  76. return NULL;
  77. if (fgets(line, sizeof(line)-1, fd) == NULL)
  78. return NULL;
  79. fclose(fd);
  80. return smprintf("%s", line);
  81. }
  82. char *
  83. getbattery(char *base)
  84. {
  85. char *co, status;
  86. int descap, remcap;
  87. descap = -1;
  88. remcap = -1;
  89. co = readfile(base, "present");
  90. if (co == NULL)
  91. return smprintf("");
  92. if (co[0] != '1') {
  93. free(co);
  94. return smprintf("not present");
  95. }
  96. free(co);
  97. co = readfile(base, "charge_full_design");
  98. if (co == NULL) {
  99. co = readfile(base, "energy_full_design");
  100. if (co == NULL)
  101. return smprintf("");
  102. }
  103. sscanf(co, "%d", &descap);
  104. free(co);
  105. co = readfile(base, "charge_now");
  106. if (co == NULL) {
  107. co = readfile(base, "energy_now");
  108. if (co == NULL)
  109. return smprintf("");
  110. }
  111. sscanf(co, "%d", &remcap);
  112. free(co);
  113. co = readfile(base, "status");
  114. if (!strncmp(co, "Discharging", 11)) {
  115. status = '-';
  116. } else if(!strncmp(co, "Charging", 8)) {
  117. status = '+';
  118. } else {
  119. status = '?';
  120. }
  121. if (remcap < 0 || descap < 0)
  122. return smprintf("invalid");
  123. return smprintf("%.0f%%%c", ((float)remcap / (float)descap) * 100, status);
  124. }
  125. int
  126. main(void)
  127. {
  128. char *status;
  129. char *bat;
  130. char *tmcl;
  131. if (!(dpy = XOpenDisplay(NULL))) {
  132. fprintf(stderr, "dwmstatus: cannot open display.\n");
  133. return 1;
  134. }
  135. for (;;sleep(60)) {
  136. bat = getbattery("/sys/class/power_supply/BAT0");
  137. tmcl = mktimes("%H:%M"/*" | %a %d %b %Y"*/, tzchile);
  138. status = smprintf("B:%s | %s | pomelo",
  139. bat, tmcl);
  140. setstatus(status);
  141. free(bat);
  142. free(tmcl);
  143. free(status);
  144. }
  145. XCloseDisplay(dpy);
  146. return 0;
  147. }