My slstatus configuration
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

battery.c 1.7 KiB

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