The repository formerly known as dotfiles
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.

fbatt.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Copyright (c) 2018, Curtis McEnroe <programble@gmail.com>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <dirent.h>
  17. #include <err.h>
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <linux/fb.h>
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <sys/ioctl.h>
  25. #include <sys/mman.h>
  26. #include <sysexits.h>
  27. #include <unistd.h>
  28. #include "scheme.h"
  29. static const char *Class = "/sys/class/power_supply";
  30. static const uint32_t Right = 5 * 8 + 1; // fbclock width.
  31. static const uint32_t Width = 8;
  32. static const uint32_t Height = 16;
  33. int main() {
  34. int error;
  35. DIR *dir = opendir(Class);
  36. if (!dir) err(EX_OSFILE, "%s", Class);
  37. FILE *chargeFull = NULL;
  38. FILE *chargeNow = NULL;
  39. const struct dirent *entry;
  40. while (NULL != (errno = 0, entry = readdir(dir))) {
  41. if (entry->d_name[0] == '.') continue;
  42. error = chdir(Class);
  43. if (error) err(EX_OSFILE, "%s", Class);
  44. error = chdir(entry->d_name);
  45. if (error) err(EX_OSFILE, "%s/%s", Class, entry->d_name);
  46. chargeFull = fopen("charge_full", "r");
  47. chargeNow = fopen("charge_now", "r");
  48. if (chargeFull && chargeNow) break;
  49. }
  50. if (!chargeFull || !chargeNow) {
  51. if (errno) err(EX_OSFILE, "%s", Class);
  52. errx(EX_CONFIG, "%s: empty", Class);
  53. }
  54. closedir(dir);
  55. const char *path = getenv("FRAMEBUFFER");
  56. if (!path) path = "/dev/fb0";
  57. int fb = open(path, O_RDWR);
  58. if (fb < 0) err(EX_OSFILE, "%s", path);
  59. struct fb_var_screeninfo info;
  60. error = ioctl(fb, FBIOGET_VSCREENINFO, &info);
  61. if (error) err(EX_IOERR, "%s", path);
  62. size_t size = 4 * info.xres * info.yres;
  63. uint32_t *buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0);
  64. if (buf == MAP_FAILED) err(EX_IOERR, "%s", path);
  65. for (;;) {
  66. int match;
  67. rewind(chargeFull);
  68. fflush(chargeFull);
  69. uint32_t full;
  70. match = fscanf(chargeFull, "%u", &full);
  71. if (match == EOF) err(EX_IOERR, "charge_full");
  72. if (match < 1) errx(EX_DATAERR, "charge_full");
  73. rewind(chargeNow);
  74. fflush(chargeNow);
  75. uint32_t now;
  76. match = fscanf(chargeNow, "%u", &now);
  77. if (match == EOF) err(EX_IOERR, "charge_now");
  78. if (match < 1) errx(EX_DATAERR, "charge_now");
  79. uint32_t percent = 100 * now / full;
  80. uint32_t height = 16 * now / full;
  81. for (int i = 0; i < 60; ++i, sleep(1)) {
  82. uint32_t left = info.xres - Right - Width;
  83. for (uint32_t y = 0; y <= Height; ++y) {
  84. buf[y * info.xres + left - 1] = DarkWhite;
  85. buf[y * info.xres + left + Width] = DarkWhite;
  86. }
  87. for (uint32_t x = left; x < left + Width; ++x) {
  88. buf[Height * info.xres + x] = DarkWhite;
  89. }
  90. for (uint32_t y = 0; y < Height; ++y) {
  91. for (uint32_t x = left; x < left + Width; ++x) {
  92. buf[y * info.xres + x] =
  93. (Height - 1 - y > height) ? DarkBlack
  94. : (percent <= 10) ? DarkRed
  95. : (percent <= 30) ? DarkYellow
  96. : LightBlack;
  97. }
  98. }
  99. }
  100. }
  101. }