battery.c 3.9 KB

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