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

163 行
3.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. const char *
  42. battery_remaining(const char *bat)
  43. {
  44. int charge_now, current_now, m, h;
  45. float timeleft;
  46. char path[PATH_MAX], state[12];
  47. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/",
  48. bat, "/status");
  49. if (pscanf(path, "%12s", state) != 1) {
  50. return NULL;
  51. }
  52. if (!strcmp(state, "Discharging")) {
  53. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/",
  54. bat, "/charge_now");
  55. if (pscanf(path, "%i", &charge_now) != 1) {
  56. return NULL;
  57. }
  58. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/",
  59. bat, "/current_now");
  60. if (pscanf(path, "%i", &current_now) != 1) {
  61. return NULL;
  62. }
  63. timeleft = (float)charge_now / (float)current_now;
  64. h = timeleft;
  65. m = (timeleft - (float)h) * 60;
  66. return bprintf("%dh %dm", h, m);
  67. }
  68. return "";
  69. }
  70. #elif defined(__OpenBSD__)
  71. #include <fcntl.h>
  72. #include <machine/apmvar.h>
  73. #include <sys/ioctl.h>
  74. #include <unistd.h>
  75. static int
  76. load_apm_power_info(struct apm_power_info *apm_info)
  77. {
  78. int fd;
  79. fd = open("/dev/apm", O_RDONLY);
  80. if (fd < 0) {
  81. warn("open '/dev/apm':");
  82. return 0;
  83. }
  84. memset(apm_info, 0, sizeof(struct apm_power_info));
  85. if (ioctl(fd, APM_IOC_GETPOWER, apm_info) < 0) {
  86. warn("ioctl 'APM_IOC_GETPOWER':");
  87. close(fd);
  88. return 0;
  89. }
  90. return close(fd), 1;
  91. }
  92. const char *
  93. battery_perc(const char *unused)
  94. {
  95. struct apm_power_info apm_info;
  96. if (load_apm_power_info(&apm_info)) {
  97. return bprintf("%d", apm_info.battery_life);
  98. }
  99. return NULL;
  100. }
  101. const char *
  102. battery_state(const char *unused)
  103. {
  104. struct apm_power_info apm_info;
  105. size_t i;
  106. struct {
  107. unsigned int state;
  108. char *symbol;
  109. } map[] = {
  110. { APM_AC_ON, "+" },
  111. { APM_AC_OFF, "-" },
  112. };
  113. if (load_apm_power_info(&apm_info)) {
  114. for (i = 0; i < LEN(map); i++) {
  115. if (map[i].state == apm_info.ac_state) {
  116. break;
  117. }
  118. }
  119. return (i == LEN(map)) ? "?" : map[i].symbol;
  120. }
  121. return NULL;
  122. }
  123. const char *
  124. battery_remaining(const char *unused)
  125. {
  126. struct apm_power_info apm_info;
  127. if (load_apm_power_info(&apm_info)) {
  128. if (apm_info.ac_state != APM_AC_ON) {
  129. return bprintf("%uh %02um", apm_info.minutes_left / 60,
  130. apm_info.minutes_left % 60);
  131. } else {
  132. return "";
  133. }
  134. }
  135. return NULL;
  136. }
  137. #endif