My slstatus configuration
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

battery.c 2.2 KiB

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