battery.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* See LICENSE file for copyright and license details. */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "../util.h"
  5. #if defined(__linux__)
  6. #include <limits.h>
  7. #include <stdint.h>
  8. #include <unistd.h>
  9. static const char *
  10. pick(const char *bat, const char *f1, const char *f2, char *path,
  11. size_t length)
  12. {
  13. if (esnprintf(path, length, f1, bat) > 0 &&
  14. access(path, R_OK) == 0) {
  15. return f1;
  16. }
  17. if (esnprintf(path, length, f2, bat) > 0 &&
  18. access(path, R_OK) == 0) {
  19. return f2;
  20. }
  21. return NULL;
  22. }
  23. const char *
  24. battery_perc(const char *bat)
  25. {
  26. int perc;
  27. char path[PATH_MAX];
  28. if (esnprintf(path, sizeof(path),
  29. "/sys/class/power_supply/%s/capacity", bat) < 0) {
  30. return NULL;
  31. }
  32. if (pscanf(path, "%d", &perc) != 1) {
  33. return NULL;
  34. }
  35. return bprintf("%d", perc);
  36. }
  37. const char *
  38. battery_state(const char *bat)
  39. {
  40. static struct {
  41. char *state;
  42. char *symbol;
  43. } map[] = {
  44. { "Charging", "+" },
  45. { "Discharging", "-" },
  46. { "Full", "o" },
  47. };
  48. size_t i;
  49. char path[PATH_MAX], state[12];
  50. if (esnprintf(path, sizeof(path),
  51. "/sys/class/power_supply/%s/status", bat) < 0) {
  52. return NULL;
  53. }
  54. if (pscanf(path, "%12s", state) != 1) {
  55. return NULL;
  56. }
  57. for (i = 0; i < LEN(map); i++) {
  58. if (!strcmp(map[i].state, state)) {
  59. break;
  60. }
  61. }
  62. return (i == LEN(map)) ? "?" : map[i].symbol;
  63. }
  64. const char *
  65. battery_remaining(const char *bat)
  66. {
  67. uintmax_t charge_now, current_now, m, h;
  68. double timeleft;
  69. char path[PATH_MAX], state[12];
  70. if (esnprintf(path, sizeof(path),
  71. "/sys/class/power_supply/%s/status", bat) < 0) {
  72. return NULL;
  73. }
  74. if (pscanf(path, "%12s", state) != 1) {
  75. return NULL;
  76. }
  77. if (!pick(bat, "/sys/class/power_supply/%s/charge_now",
  78. "/sys/class/power_supply/%s/energy_now", path,
  79. sizeof(path)) ||
  80. pscanf(path, "%ju", &charge_now) < 0) {
  81. return NULL;
  82. }
  83. if (!strcmp(state, "Discharging")) {
  84. if (!pick(bat, "/sys/class/power_supply/%s/current_now",
  85. "/sys/class/power_supply/%s/power_now", path,
  86. sizeof(path)) ||
  87. pscanf(path, "%ju", &current_now) < 0) {
  88. return NULL;
  89. }
  90. if (current_now == 0) {
  91. return NULL;
  92. }
  93. timeleft = (double)charge_now / (double)current_now;
  94. h = timeleft;
  95. m = (timeleft - (double)h) * 60;
  96. return bprintf("%juh %jum", 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. #elif defined(__FreeBSD__)
  169. #include <sys/sysctl.h>
  170. const char *
  171. battery_perc(const char *unused)
  172. {
  173. int cap;
  174. size_t len;
  175. len = sizeof(cap);
  176. if (sysctlbyname("hw.acpi.battery.life", &cap, &len, NULL, 0) == -1
  177. || !len)
  178. return NULL;
  179. return bprintf("%d", cap);
  180. }
  181. const char *
  182. battery_state(const char *unused)
  183. {
  184. int state;
  185. size_t len;
  186. len = sizeof(state);
  187. if (sysctlbyname("hw.acpi.battery.state", &state, &len, NULL, 0) == -1
  188. || !len)
  189. return NULL;
  190. switch(state) {
  191. case 0:
  192. case 2:
  193. return "+";
  194. case 1:
  195. return "-";
  196. default:
  197. return "?";
  198. }
  199. }
  200. const char *
  201. battery_remaining(const char *unused)
  202. {
  203. int rem;
  204. size_t len;
  205. len = sizeof(rem);
  206. if (sysctlbyname("hw.acpi.battery.time", &rem, &len, NULL, 0) == -1
  207. || !len
  208. || rem == -1)
  209. return NULL;
  210. return bprintf("%uh %02um", rem / 60, rem % 60);
  211. }
  212. #endif