My slstatus configuration
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

202 Zeilen
3.9 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "../util.h"
  5. #if defined(__linux__)
  6. #include <inttypes.h>
  7. #include <limits.h>
  8. #include <unistd.h>
  9. static const char *
  10. pick(const char *bat, const char *f1, const char *f2, char *path,
  11. size_t length)
  12. {
  13. if (esnprintf(path, length, f1, bat) > 0 &&
  14. access(path, R_OK) == 0) {
  15. return f1;
  16. }
  17. if (esnprintf(path, length, f2, bat) > 0 &&
  18. access(path, R_OK) == 0) {
  19. return f2;
  20. }
  21. return NULL;
  22. }
  23. const char *
  24. battery_perc(const char *bat)
  25. {
  26. int perc;
  27. char path[PATH_MAX];
  28. if (esnprintf(path, sizeof(path),
  29. "/sys/class/power_supply/%s/capacity",
  30. bat) < 0) {
  31. return NULL;
  32. }
  33. if (pscanf(path, "%d", &perc) != 1) {
  34. return NULL;
  35. }
  36. return bprintf("%d", perc);
  37. }
  38. const char *
  39. battery_state(const char *bat)
  40. {
  41. static struct {
  42. char *state;
  43. char *symbol;
  44. } map[] = {
  45. { "Charging", "+" },
  46. { "Discharging", "-" },
  47. };
  48. size_t i;
  49. char path[PATH_MAX], state[12];
  50. if (esnprintf(path, sizeof(path),
  51. "/sys/class/power_supply/%s/status",
  52. bat) < 0) {
  53. return NULL;
  54. }
  55. if (pscanf(path, "%12s", state) != 1) {
  56. return NULL;
  57. }
  58. for (i = 0; i < LEN(map); i++) {
  59. if (!strcmp(map[i].state, state)) {
  60. break;
  61. }
  62. }
  63. return (i == LEN(map)) ? "?" : map[i].symbol;
  64. }
  65. const char *
  66. battery_remaining(const char *bat)
  67. {
  68. uintmax_t charge_now, current_now, m, h;
  69. double timeleft;
  70. char path[PATH_MAX], state[12];
  71. if (esnprintf(path, sizeof(path),
  72. "/sys/class/power_supply/%s/status",
  73. bat) < 0) {
  74. return NULL;
  75. }
  76. if (pscanf(path, "%12s", state) != 1) {
  77. return NULL;
  78. }
  79. if (!pick(bat, "/sys/class/power_supply/%s/charge_now",
  80. "/sys/class/power_supply/%s/energy_now",
  81. path, sizeof(path)) ||
  82. pscanf(path, "%" PRIuMAX, &charge_now) < 0) {
  83. return NULL;
  84. }
  85. if (!strcmp(state, "Discharging")) {
  86. if (!pick(bat, "/sys/class/power_supply/%s/current_now",
  87. "/sys/class/power_supply/%s/power_now", path,
  88. sizeof(path)) ||
  89. pscanf(path, "%" PRIuMAX, &current_now) < 0) {
  90. return NULL;
  91. }
  92. if (current_now == 0) {
  93. return NULL;
  94. }
  95. timeleft = (double)charge_now / (double)current_now;
  96. h = timeleft;
  97. m = (timeleft - (double)h) * 60;
  98. return bprintf("%" PRIuMAX "h %" PRIuMAX "m", h, m);
  99. }
  100. return "";
  101. }
  102. #elif defined(__OpenBSD__)
  103. #include <fcntl.h>
  104. #include <machine/apmvar.h>
  105. #include <sys/ioctl.h>
  106. #include <unistd.h>
  107. static int
  108. load_apm_power_info(struct apm_power_info *apm_info)
  109. {
  110. int fd;
  111. fd = open("/dev/apm", O_RDONLY);
  112. if (fd < 0) {
  113. warn("open '/dev/apm':");
  114. return 0;
  115. }
  116. memset(apm_info, 0, sizeof(struct apm_power_info));
  117. if (ioctl(fd, APM_IOC_GETPOWER, apm_info) < 0) {
  118. warn("ioctl 'APM_IOC_GETPOWER':");
  119. close(fd);
  120. return 0;
  121. }
  122. return close(fd), 1;
  123. }
  124. const char *
  125. battery_perc(const char *unused)
  126. {
  127. struct apm_power_info apm_info;
  128. if (load_apm_power_info(&apm_info)) {
  129. return bprintf("%d", apm_info.battery_life);
  130. }
  131. return NULL;
  132. }
  133. const char *
  134. battery_state(const char *unused)
  135. {
  136. struct {
  137. unsigned int state;
  138. char *symbol;
  139. } map[] = {
  140. { APM_AC_ON, "+" },
  141. { APM_AC_OFF, "-" },
  142. };
  143. struct apm_power_info apm_info;
  144. size_t i;
  145. if (load_apm_power_info(&apm_info)) {
  146. for (i = 0; i < LEN(map); i++) {
  147. if (map[i].state == apm_info.ac_state) {
  148. break;
  149. }
  150. }
  151. return (i == LEN(map)) ? "?" : map[i].symbol;
  152. }
  153. return NULL;
  154. }
  155. const char *
  156. battery_remaining(const char *unused)
  157. {
  158. struct apm_power_info apm_info;
  159. if (load_apm_power_info(&apm_info)) {
  160. if (apm_info.ac_state != APM_AC_ON) {
  161. return bprintf("%uh %02um",
  162. apm_info.minutes_left / 60,
  163. apm_info.minutes_left % 60);
  164. } else {
  165. return "";
  166. }
  167. }
  168. return NULL;
  169. }
  170. #endif