My slstatus configuration
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

battery.c 1.9 KiB

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