battery.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. const char *
  9. battery_perc(const char *bat)
  10. {
  11. int perc;
  12. char path[PATH_MAX];
  13. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/",
  14. bat, "/capacity");
  15. return (pscanf(path, "%i", &perc) == 1) ?
  16. bprintf("%d", perc) : NULL;
  17. }
  18. const char *
  19. battery_power(const char *bat)
  20. {
  21. int watts;
  22. char path[PATH_MAX];
  23. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/",
  24. bat, "/power_now");
  25. return (pscanf(path, "%i", &watts) == 1) ?
  26. bprintf("%d", (watts + 500000) / 1000000) : NULL;
  27. }
  28. const char *
  29. battery_state(const char *bat)
  30. {
  31. struct {
  32. char *state;
  33. char *symbol;
  34. } map[] = {
  35. { "Charging", "+" },
  36. { "Discharging", "-" },
  37. { "Full", "=" },
  38. { "Unknown", "/" },
  39. };
  40. size_t i;
  41. char path[PATH_MAX], state[12];
  42. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/",
  43. bat, "/status");
  44. if (pscanf(path, "%12s", state) != 1) {
  45. return NULL;
  46. }
  47. for (i = 0; i < LEN(map); i++) {
  48. if (!strcmp(map[i].state, state)) {
  49. break;
  50. }
  51. }
  52. return (i == LEN(map)) ? "?" : map[i].symbol;
  53. }
  54. #elif defined(__OpenBSD__)
  55. #include <fcntl.h>
  56. #include <machine/apmvar.h>
  57. #include <sys/ioctl.h>
  58. #include <unistd.h>
  59. const char *
  60. battery_perc(const char *null)
  61. {
  62. struct apm_power_info apm_info;
  63. int fd;
  64. fd = open("/dev/apm", O_RDONLY);
  65. if (fd < 0) {
  66. fprintf(stderr, "open '/dev/apm': %s\n", strerror(errno));
  67. return NULL;
  68. }
  69. if (ioctl(fd, APM_IOC_GETPOWER, &apm_info) < 0) {
  70. fprintf(stderr, "ioctl 'APM_IOC_GETPOWER': %s\n",
  71. strerror(errno));
  72. close(fd);
  73. return NULL;
  74. }
  75. close(fd);
  76. return bprintf("%d", apm_info.battery_life);
  77. }
  78. const char *
  79. battery_state(const char *bat)
  80. {
  81. int fd;
  82. size_t i;
  83. struct apm_power_info apm_info;
  84. struct {
  85. unsigned int state;
  86. char *symbol;
  87. } map[] = {
  88. { APM_AC_ON, "+" },
  89. { APM_AC_OFF, "-" },
  90. { APM_AC_UNKNOWN, "/" },
  91. };
  92. fd = open("/dev/apm", O_RDONLY);
  93. if (fd < 0) {
  94. fprintf(stderr, "open '/dev/apm': %s\n", strerror(errno));
  95. return NULL;
  96. }
  97. if (ioctl(fd, APM_IOC_GETPOWER, &apm_info) < 0) {
  98. fprintf(stderr, "ioctl 'APM_IOC_GETPOWER': %s\n",
  99. strerror(errno));
  100. close(fd);
  101. return NULL;
  102. }
  103. close(fd);
  104. for (i = 0; i < LEN(map); i++) {
  105. if (map[i].state == apm_info.ac_state) {
  106. break;
  107. }
  108. }
  109. return (i == LEN(map)) ? "?" : map[i].symbol;
  110. }
  111. #endif