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.
 
 
 
 

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