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.

fbclock.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <assert.h>
  17. #include <err.h>
  18. #include <fcntl.h>
  19. #include <linux/fb.h>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/mman.h>
  25. #include <sysexits.h>
  26. #include <time.h>
  27. #include <unistd.h>
  28. #include <zlib.h>
  29. #include "scheme.h"
  30. static const uint32_t PSF2Magic = 0x864AB572;
  31. struct PSF2Header {
  32. uint32_t magic;
  33. uint32_t version;
  34. uint32_t headerSize;
  35. uint32_t flags;
  36. uint32_t glyphCount;
  37. uint32_t glyphSize;
  38. uint32_t glyphHeight;
  39. uint32_t glyphWidth;
  40. };
  41. int main() {
  42. size_t len;
  43. const char *fontPath = getenv("FONT");
  44. if (!fontPath) {
  45. fontPath = "/usr/share/kbd/consolefonts/Lat2-Terminus16.psfu.gz";
  46. }
  47. gzFile font = gzopen(fontPath, "r");
  48. if (!font) err(EX_NOINPUT, "%s", fontPath);
  49. struct PSF2Header header;
  50. len = gzfread(&header, sizeof(header), 1, font);
  51. if (!len && gzeof(font)) errx(EX_DATAERR, "%s: missing header", fontPath);
  52. if (!len) errx(EX_IOERR, "%s", gzerror(font, NULL));
  53. if (header.magic != PSF2Magic) {
  54. errx(
  55. EX_DATAERR, "%s: invalid header magic %08X",
  56. fontPath, header.magic
  57. );
  58. }
  59. if (header.headerSize != sizeof(struct PSF2Header)) {
  60. errx(
  61. EX_DATAERR, "%s: weird header size %d",
  62. fontPath, header.headerSize
  63. );
  64. }
  65. uint8_t glyphs[128][header.glyphSize];
  66. len = gzfread(glyphs, header.glyphSize, 128, font);
  67. if (!len && gzeof(font)) errx(EX_DATAERR, "%s: missing glyphs", fontPath);
  68. if (!len) errx(EX_IOERR, "%s", gzerror(font, NULL));
  69. gzclose(font);
  70. const char *fbPath = getenv("FRAMEBUFFER");
  71. if (!fbPath) fbPath = "/dev/fb0";
  72. int fb = open(fbPath, O_RDWR);
  73. if (fb < 0) err(EX_OSFILE, "%s", fbPath);
  74. struct fb_var_screeninfo info;
  75. int error = ioctl(fb, FBIOGET_VSCREENINFO, &info);
  76. if (error) err(EX_IOERR, "%s", fbPath);
  77. size_t size = 4 * info.xres * info.yres;
  78. uint32_t *buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0);
  79. if (buf == MAP_FAILED) err(EX_IOERR, "%s", fbPath);
  80. for (;;) {
  81. time_t t = time(NULL);
  82. if (t < 0) err(EX_OSERR, "time");
  83. const struct tm *local = localtime(&t);
  84. if (!local) err(EX_OSERR, "localtime");
  85. char str[64];
  86. len = strftime(str, sizeof(str), "%H:%M", local);
  87. assert(len);
  88. for (int i = 0; i < (60 - local->tm_sec); ++i, sleep(1)) {
  89. uint32_t left = info.xres - header.glyphWidth * len;
  90. uint32_t bottom = header.glyphHeight;
  91. for (uint32_t y = 0; y < bottom; ++y) {
  92. buf[y * info.xres + left - 1] = DarkWhite;
  93. }
  94. for (uint32_t x = left - 1; x < info.xres; ++x) {
  95. buf[bottom * info.xres + x] = DarkWhite;
  96. }
  97. for (const char *s = str; *s; ++s) {
  98. const uint8_t *glyph = glyphs[(unsigned)*s];
  99. uint32_t stride = header.glyphSize / header.glyphHeight;
  100. for (uint32_t y = 0; y < header.glyphHeight; ++y) {
  101. for (uint32_t x = 0; x < header.glyphWidth; ++x) {
  102. uint8_t bits = glyph[y * stride + x / 8];
  103. uint8_t bit = bits >> (7 - x % 8) & 1;
  104. buf[y * info.xres + left + x] = bit
  105. ? DarkWhite
  106. : DarkBlack;
  107. }
  108. }
  109. left += header.glyphWidth;
  110. }
  111. }
  112. }
  113. }