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.
 
 
 
 

113 lines
2.2 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. 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;
  69. size_t i;
  70. struct apm_power_info apm_info;
  71. struct {
  72. unsigned int state;
  73. char *symbol;
  74. } map[] = {
  75. { APM_AC_ON, "+" },
  76. { APM_AC_OFF, "-" },
  77. };
  78. fd = open("/dev/apm", O_RDONLY);
  79. if (fd < 0) {
  80. fprintf(stderr, "open '/dev/apm': %s\n", strerror(errno));
  81. return NULL;
  82. }
  83. if (ioctl(fd, APM_IOC_GETPOWER, &apm_info) < 0) {
  84. fprintf(stderr, "ioctl 'APM_IOC_GETPOWER': %s\n",
  85. strerror(errno));
  86. close(fd);
  87. return NULL;
  88. }
  89. close(fd);
  90. for (i = 0; i < LEN(map); i++) {
  91. if (map[i].state == apm_info.ac_state) {
  92. break;
  93. }
  94. }
  95. return (i == LEN(map)) ? "?" : map[i].symbol;
  96. }
  97. #endif