My slstatus configuration
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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