My slstatus configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

88 lines
1.8 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #if defined(__linux__)
  6. #include <limits.h>
  7. #include <string.h>
  8. #elif defined(__OpenBSD__)
  9. #include <sys/ioctl.h>
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <machine/apmvar.h>
  13. #endif
  14. #include "../util.h"
  15. const char *
  16. battery_perc(const char *bat)
  17. {
  18. #if defined(__linux__)
  19. int perc;
  20. char path[PATH_MAX];
  21. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/capacity");
  22. return (pscanf(path, "%i", &perc) == 1) ?
  23. bprintf("%d", perc) : NULL;
  24. #elif defined(__OpenBSD__)
  25. struct apm_power_info apm_info;
  26. int fd;
  27. fd = open("/dev/apm", O_RDONLY);
  28. if (fd < 0) {
  29. fprintf(stderr, "open '/dev/apm': %s\n", strerror(errno));
  30. return NULL;
  31. }
  32. if (ioctl(fd, APM_IOC_GETPOWER, &apm_info) < 0) {
  33. fprintf(stderr, "ioctl 'APM_IOC_GETPOWER': %s\n", strerror(errno));
  34. close(fd);
  35. return NULL;
  36. }
  37. close(fd);
  38. return bprintf("%d", apm_info.battery_life);
  39. #endif
  40. }
  41. #if defined(__linux__)
  42. const char *
  43. battery_power(const char *bat)
  44. {
  45. int watts;
  46. char path[PATH_MAX];
  47. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/power_now");
  48. return (pscanf(path, "%i", &watts) == 1) ?
  49. bprintf("%d", (watts + 500000) / 1000000) : NULL;
  50. }
  51. const char *
  52. battery_state(const char *bat)
  53. {
  54. struct {
  55. char *state;
  56. char *symbol;
  57. } map[] = {
  58. { "Charging", "+" },
  59. { "Discharging", "-" },
  60. { "Full", "=" },
  61. { "Unknown", "/" },
  62. };
  63. size_t i;
  64. char path[PATH_MAX], state[12];
  65. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/status");
  66. if (pscanf(path, "%12s", state) != 1) {
  67. return NULL;
  68. }
  69. for (i = 0; i < LEN(map); i++) {
  70. if (!strcmp(map[i].state, state)) {
  71. break;
  72. }
  73. }
  74. return (i == LEN(map)) ? "?" : map[i].symbol;
  75. }
  76. #endif