My slstatus configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

204 line
3.9 KiB

  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. double 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. if (current_now == 0) {
  94. return NULL;
  95. }
  96. timeleft = (double)charge_now / (double)current_now;
  97. h = timeleft;
  98. m = (timeleft - (double)h) * 60;
  99. return bprintf("%dh %dm", h, m);
  100. }
  101. return "";
  102. }
  103. #elif defined(__OpenBSD__)
  104. #include <fcntl.h>
  105. #include <machine/apmvar.h>
  106. #include <sys/ioctl.h>
  107. #include <unistd.h>
  108. static int
  109. load_apm_power_info(struct apm_power_info *apm_info)
  110. {
  111. int fd;
  112. fd = open("/dev/apm", O_RDONLY);
  113. if (fd < 0) {
  114. warn("open '/dev/apm':");
  115. return 0;
  116. }
  117. memset(apm_info, 0, sizeof(struct apm_power_info));
  118. if (ioctl(fd, APM_IOC_GETPOWER, apm_info) < 0) {
  119. warn("ioctl 'APM_IOC_GETPOWER':");
  120. close(fd);
  121. return 0;
  122. }
  123. return close(fd), 1;
  124. }
  125. const char *
  126. battery_perc(const char *unused)
  127. {
  128. struct apm_power_info apm_info;
  129. if (load_apm_power_info(&apm_info)) {
  130. return bprintf("%d", apm_info.battery_life);
  131. }
  132. return NULL;
  133. }
  134. const char *
  135. battery_state(const char *unused)
  136. {
  137. struct {
  138. unsigned int state;
  139. char *symbol;
  140. } map[] = {
  141. { APM_AC_ON, "+" },
  142. { APM_AC_OFF, "-" },
  143. };
  144. struct apm_power_info apm_info;
  145. size_t i;
  146. if (load_apm_power_info(&apm_info)) {
  147. for (i = 0; i < LEN(map); i++) {
  148. if (map[i].state == apm_info.ac_state) {
  149. break;
  150. }
  151. }
  152. return (i == LEN(map)) ? "?" : map[i].symbol;
  153. }
  154. return NULL;
  155. }
  156. const char *
  157. battery_remaining(const char *unused)
  158. {
  159. struct apm_power_info apm_info;
  160. if (load_apm_power_info(&apm_info)) {
  161. if (apm_info.ac_state != APM_AC_ON) {
  162. return bprintf("%uh %02um",
  163. apm_info.minutes_left / 60,
  164. apm_info.minutes_left % 60);
  165. } else {
  166. return "";
  167. }
  168. }
  169. return NULL;
  170. }
  171. #endif