graphic.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/init.h>
  20. #include <kernel/types.h>
  21. #include <kernel/errno.h>
  22. #include <kernel/graphic.h>
  23. #include <kernel/vsprintf.h>
  24. #include <lib/string.h>
  25. #include <stdarg.h>
  26. static char buffer[256];
  27. int printk(__uint32_t fg, __uint32_t bg, char *s, ...) {
  28. va_list args;
  29. int i;
  30. /* I think 256 is enough in kernel */
  31. /* too long, return it. */
  32. if (s[255])
  33. return 0;
  34. va_start(args, s);
  35. /* it maybe overflow, but that's in .data section and we did check. */
  36. i = vsprintf(buffer, s, args);
  37. print(fg, bg, buffer);
  38. memset(buffer, 0, 256);
  39. va_end(args);
  40. return i;
  41. }
  42. static int x = 0, y = 0;
  43. void putc(char c, __uint32_t fg, __uint32_t bg) {
  44. if (c == '\n') {
  45. /* it's '\n', but we use '\r\n' */
  46. x = 0;
  47. y += 16;
  48. return;
  49. }
  50. if (c == '\r') {
  51. x = 0;
  52. return;
  53. }
  54. if (c == ' ') {
  55. x += 8;
  56. return;
  57. }
  58. struct psf_header *psf = (struct psf_header *) &font_start;
  59. /*
  60. * yeah, it's very ugly. but it just get address of this glyph, and check
  61. * 'c' is really in this font.
  62. * */
  63. __uint8_t *glyph = (__uint8_t *) &font_start + psf->header_size +
  64. (c > 0 && c < psf->glyphs_count ? c : 0) * psf->glyph_size;
  65. /* okay, now we put it on the screen */
  66. int i;
  67. int mask;
  68. char d;
  69. for (i = 0; i < 16; i++) {
  70. __uint32_t *addr;
  71. int j;
  72. addr = FB_ADDR + VGA_X * (y + i) + x;
  73. mask = 0x0100;
  74. d = glyph[i];
  75. for (j = 0; j < 8; j++) {
  76. mask >>= 1;
  77. if (d & mask)
  78. addr[j] = fg;
  79. else
  80. addr[j] = bg;
  81. }
  82. }
  83. x += 8;
  84. if (x >= VGA_X) {
  85. x = 0;
  86. y += 16;
  87. /* but y will > Y_LINES */
  88. }
  89. return;
  90. }
  91. void print(__uint32_t fg, __uint32_t bg, char *s) {
  92. for (; *s; s++)
  93. putc(*s, fg, bg);
  94. return;
  95. }