graphic.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <stdarg.h>
  25. int printk(__uint32_t fg, __uint32_t bg, char *s, ...) {
  26. va_list args;
  27. int i;
  28. /* I think 256 is enough in kernel */
  29. /* too long, return it. */
  30. if (s[255])
  31. return 0;
  32. static char buffer[256] = {0};
  33. va_start(args, s);
  34. /* it maybe overflow, but that's in .data section and we did check. */
  35. i = vsprintf(buffer, s, args);
  36. print(fg, bg, buffer);
  37. va_end(args);
  38. return i;
  39. }
  40. static int x = 0, y = 0;
  41. void putc(char c, __uint32_t fg, __uint32_t bg) {
  42. if (c == '\n') {
  43. /* it's '\n', but we use '\r\n' */
  44. x = 0;
  45. y += 16;
  46. return;
  47. }
  48. if (c == '\r') {
  49. x = 0;
  50. return;
  51. }
  52. if (c == ' ') {
  53. x += 8;
  54. return;
  55. }
  56. struct psf_header *psf = (struct psf_header *) &font_start;
  57. /*
  58. * yeah, it's very ugly. but it just get address of this glyph, and check
  59. * 'c' is really in this font.
  60. * */
  61. __uint8_t *glyph = (__uint8_t *) &font_start + psf->header_size +
  62. (c > 0 && c < psf->glyphs_count ? c : 0) * psf->glyph_size;
  63. /* okay, now we put it on the screen */
  64. int i;
  65. int mask;
  66. char d;
  67. for (i = 0; i < 16; i++) {
  68. __uint32_t *addr;
  69. int j;
  70. addr = FB_ADDR + VGA_X * (y + i) + x;
  71. mask = 0x0100;
  72. d = glyph[i];
  73. for (j = 0; j < 8; j++) {
  74. mask >>= 1;
  75. if (d & mask)
  76. addr[j] = fg;
  77. else
  78. addr[j] = bg;
  79. }
  80. }
  81. x += 8;
  82. if (x >= VGA_X) {
  83. x = 0;
  84. y += 16;
  85. /* but y will > Y_LINES */
  86. }
  87. return;
  88. }
  89. void print(__uint32_t fg, __uint32_t bg, char *s) {
  90. for (; *s; s++)
  91. putc(*s, fg, bg);
  92. return;
  93. }