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.
 
 
 
 

87 rivejä
1.7 KiB

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