My slstatus configuration
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

92 líneas
1.9 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. #if defined(__linux__)
  16. const char *
  17. battery_perc(const char *bat)
  18. {
  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. }
  25. #elif defined(__OpenBSD__)
  26. const char *
  27. battery_perc(const char *null)
  28. {
  29. struct apm_power_info apm_info;
  30. int fd;
  31. fd = open("/dev/apm", O_RDONLY);
  32. if (fd < 0) {
  33. fprintf(stderr, "open '/dev/apm': %s\n", strerror(errno));
  34. return NULL;
  35. }
  36. if (ioctl(fd, APM_IOC_GETPOWER, &apm_info) < 0) {
  37. fprintf(stderr, "ioctl 'APM_IOC_GETPOWER': %s\n", strerror(errno));
  38. close(fd);
  39. return NULL;
  40. }
  41. close(fd);
  42. return bprintf("%d", apm_info.battery_life);
  43. }
  44. #endif
  45. #if defined(__linux__)
  46. const char *
  47. battery_power(const char *bat)
  48. {
  49. int watts;
  50. char path[PATH_MAX];
  51. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/power_now");
  52. return (pscanf(path, "%i", &watts) == 1) ?
  53. bprintf("%d", (watts + 500000) / 1000000) : NULL;
  54. }
  55. const char *
  56. battery_state(const char *bat)
  57. {
  58. struct {
  59. char *state;
  60. char *symbol;
  61. } map[] = {
  62. { "Charging", "+" },
  63. { "Discharging", "-" },
  64. { "Full", "=" },
  65. { "Unknown", "/" },
  66. };
  67. size_t i;
  68. char path[PATH_MAX], state[12];
  69. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/status");
  70. if (pscanf(path, "%12s", state) != 1) {
  71. return NULL;
  72. }
  73. for (i = 0; i < LEN(map); i++) {
  74. if (!strcmp(map[i].state, state)) {
  75. break;
  76. }
  77. }
  78. return (i == LEN(map)) ? "?" : map[i].symbol;
  79. }
  80. #endif