My slstatus configuration
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

131 líneas
2.4 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. const char *
  42. battery_remaining(const char *bat)
  43. {
  44. /* TODO: Implement */
  45. return NULL;
  46. }
  47. #elif defined(__OpenBSD__)
  48. #include <fcntl.h>
  49. #include <machine/apmvar.h>
  50. #include <sys/ioctl.h>
  51. #include <unistd.h>
  52. static int
  53. load_apm_power_info(struct apm_power_info *apm_info)
  54. {
  55. int fd;
  56. fd = open("/dev/apm", O_RDONLY);
  57. if (fd < 0) {
  58. warn("open '/dev/apm':");
  59. return 0;
  60. }
  61. memset(apm_info, 0, sizeof(struct apm_power_info));
  62. if (ioctl(fd, APM_IOC_GETPOWER, apm_info) < 0) {
  63. warn("ioctl 'APM_IOC_GETPOWER':");
  64. close(fd);
  65. return 0;
  66. }
  67. return close(fd), 1;
  68. }
  69. const char *
  70. battery_perc(const char *unused)
  71. {
  72. struct apm_power_info apm_info;
  73. if (load_apm_power_info(&apm_info)) {
  74. return bprintf("%d", apm_info.battery_life);
  75. }
  76. return NULL;
  77. }
  78. const char *
  79. battery_state(const char *unused)
  80. {
  81. struct apm_power_info apm_info;
  82. size_t i;
  83. struct {
  84. unsigned int state;
  85. char *symbol;
  86. } map[] = {
  87. { APM_AC_ON, "+" },
  88. { APM_AC_OFF, "-" },
  89. };
  90. if (load_apm_power_info(&apm_info)) {
  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. return NULL;
  99. }
  100. const char *
  101. battery_remaining(const char *unused)
  102. {
  103. struct apm_power_info apm_info;
  104. if (load_apm_power_info(&apm_info)) {
  105. return bprintf("%u:%02u", apm_info.minutes_left / 60,
  106. apm_info.minutes_left % 60);
  107. }
  108. return NULL;
  109. }
  110. #endif