battery.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* See LICENSE file for copyright and license details. */
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "../util.h"
  6. #if defined(__linux__)
  7. #include <limits.h>
  8. #include <unistd.h>
  9. #define CHARGE_NOW "/sys/class/power_supply/%s/charge_now"
  10. #define ENERGY_NOW "/sys/class/power_supply/%s/energy_now"
  11. #define CURRENT_NOW "/sys/class/power_supply/%s/current_now"
  12. #define POWER_NOW "/sys/class/power_supply/%s/power_now"
  13. static const char *
  14. pick(const char *bat, const char *f1, const char *f2, char *path, size_t length)
  15. {
  16. if (esnprintf(path, length, f1, bat) > 0 && access(path, R_OK) == 0) {
  17. return f1;
  18. }
  19. if (esnprintf(path, length, f2, bat) > 0 && access(path, R_OK) == 0) {
  20. return f2;
  21. }
  22. return NULL;
  23. }
  24. const char *
  25. battery_perc(const char *bat)
  26. {
  27. int perc;
  28. char path[PATH_MAX];
  29. if (esnprintf(path, sizeof(path),
  30. "/sys/class/power_supply/%s/capacity",
  31. bat) < 0) {
  32. return NULL;
  33. }
  34. if (pscanf(path, "%d", &perc) != 1) {
  35. return NULL;
  36. }
  37. return bprintf("%d", perc);
  38. }
  39. const char *
  40. battery_state(const char *bat)
  41. {
  42. static struct {
  43. char *state;
  44. char *symbol;
  45. } map[] = {
  46. { "Charging", "+" },
  47. { "Discharging", "-" },
  48. };
  49. size_t i;
  50. char path[PATH_MAX], state[12];
  51. if (esnprintf(path, sizeof(path),
  52. "/sys/class/power_supply/%s/status",
  53. bat) < 0) {
  54. return NULL;
  55. }
  56. if (pscanf(path, "%12s", state) != 1) {
  57. return NULL;
  58. }
  59. for (i = 0; i < LEN(map); i++) {
  60. if (!strcmp(map[i].state, state)) {
  61. break;
  62. }
  63. }
  64. return (i == LEN(map)) ? "?" : map[i].symbol;
  65. }
  66. const char *
  67. battery_remaining(const char *bat)
  68. {
  69. int charge_now, current_now, m, h;
  70. float timeleft;
  71. char path[PATH_MAX], state[12];
  72. if (esnprintf(path, sizeof(path),
  73. "/sys/class/power_supply/%s/status",
  74. bat) < 0) {
  75. return NULL;
  76. }
  77. if (pscanf(path, "%12s", state) != 1) {
  78. return NULL;
  79. }
  80. if (pick(bat, CHARGE_NOW, ENERGY_NOW, path, sizeof (path)) == NULL ||
  81. pscanf(path, "%d", &charge_now) < 0) {
  82. return NULL;
  83. }
  84. if (!strcmp(state, "Discharging")) {
  85. if (pick(bat, CURRENT_NOW, POWER_NOW, path, sizeof (path)) == NULL ||
  86. pscanf(path, "%d", &current_now) < 0) {
  87. return NULL;
  88. }
  89. timeleft = (float)charge_now / (float)current_now;
  90. h = timeleft;
  91. m = (timeleft - (float)h) * 60;
  92. return bprintf("%dh %dm", h, m);
  93. }
  94. return "";
  95. }
  96. #elif defined(__OpenBSD__)
  97. #include <fcntl.h>
  98. #include <machine/apmvar.h>
  99. #include <sys/ioctl.h>
  100. #include <unistd.h>
  101. static int
  102. load_apm_power_info(struct apm_power_info *apm_info)
  103. {
  104. int fd;
  105. fd = open("/dev/apm", O_RDONLY);
  106. if (fd < 0) {
  107. warn("open '/dev/apm':");
  108. return 0;
  109. }
  110. memset(apm_info, 0, sizeof(struct apm_power_info));
  111. if (ioctl(fd, APM_IOC_GETPOWER, apm_info) < 0) {
  112. warn("ioctl 'APM_IOC_GETPOWER':");
  113. close(fd);
  114. return 0;
  115. }
  116. return close(fd), 1;
  117. }
  118. const char *
  119. battery_perc(const char *unused)
  120. {
  121. struct apm_power_info apm_info;
  122. if (load_apm_power_info(&apm_info)) {
  123. return bprintf("%d", apm_info.battery_life);
  124. }
  125. return NULL;
  126. }
  127. const char *
  128. battery_state(const char *unused)
  129. {
  130. struct {
  131. unsigned int state;
  132. char *symbol;
  133. } map[] = {
  134. { APM_AC_ON, "+" },
  135. { APM_AC_OFF, "-" },
  136. };
  137. struct apm_power_info apm_info;
  138. size_t i;
  139. if (load_apm_power_info(&apm_info)) {
  140. for (i = 0; i < LEN(map); i++) {
  141. if (map[i].state == apm_info.ac_state) {
  142. break;
  143. }
  144. }
  145. return (i == LEN(map)) ? "?" : map[i].symbol;
  146. }
  147. return NULL;
  148. }
  149. const char *
  150. battery_remaining(const char *unused)
  151. {
  152. struct apm_power_info apm_info;
  153. if (load_apm_power_info(&apm_info)) {
  154. if (apm_info.ac_state != APM_AC_ON) {
  155. return bprintf("%uh %02um", apm_info.minutes_left / 60,
  156. apm_info.minutes_left % 60);
  157. } else {
  158. return "";
  159. }
  160. }
  161. return NULL;
  162. }
  163. #endif