My slstatus configuration
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

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