battery.c 3.8 KB

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