graphic.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <kernel/schk.h>
  25. #include <lib/string.h>
  26. #include <stdarg.h>
  27. static char buffer[256];
  28. static int syslog_index = 0;
  29. void syslog(const char *s) {
  30. /* we can't use schk in this function, */
  31. /* because this will make a die-loop (schk_init calls this function) */
  32. int i;
  33. for (i = 0; s[i]; i++) {
  34. if (syslog_index + i == 32768)
  35. syslog_index = 0;
  36. *(syslog_index + SYSLOG_BUFFER) = s[i];
  37. syslog_index++;
  38. }
  39. return;
  40. }
  41. int printk(__uint32_t fg, __uint32_t bg, char *s, ...) {
  42. schk_init();
  43. va_list args;
  44. int i;
  45. /* I think 256 is enough in kernel */
  46. /* too long, return it. */
  47. va_start(args, s);
  48. /* it maybe overflow, but that's in .data section and we did check. */
  49. i = vsprintf(buffer, s, args);
  50. print(fg, bg, buffer);
  51. memset(buffer, 0, 256);
  52. va_end(args);
  53. schk_leave();
  54. return i;
  55. }
  56. static int x = 0, y = 0;
  57. void putc(char c, __uint32_t fg, __uint32_t bg) {
  58. schk_init();
  59. if (c == '\n') {
  60. /* it's '\n', but we use '\r\n' */
  61. if (y + CHAR_HEIGHT > VGA_Y)
  62. y = 0;
  63. else
  64. y += 16;
  65. x = 0;
  66. /* and clear next line */
  67. return;
  68. }
  69. if (c == '\r') {
  70. x = 0;
  71. return;
  72. }
  73. if (c == '\t') {
  74. /* align to next tab point */
  75. int spaces = ((x + TAB_SIZE - 1) & (~(TAB_SIZE - 1))) - x;
  76. /* this is not good: maybe uses too many stack */
  77. for (; spaces; spaces--)
  78. putc(' ', fg, bg);
  79. return;
  80. }
  81. struct psf_header *psf = (struct psf_header *) &font_start;
  82. /*
  83. * yeah, it's very ugly. but it just get address of this glyph, and check
  84. * 'c' is really in this font.
  85. * */
  86. __uint8_t *glyph = (__uint8_t *) &font_start + psf->header_size +
  87. (c > 0 && c < psf->glyphs_count ? c : 0) * psf->glyph_size;
  88. /* need next line? */
  89. if (x >= VGA_X) {
  90. x = 0;
  91. y += CHAR_HEIGHT;
  92. }
  93. /* no enough space for this character */
  94. /* VGA_Y % CHAR_HEIGHT != 0, so we cannot use y == VGA_Y */
  95. if (y + CHAR_HEIGHT > VGA_Y)
  96. y = 0;
  97. /* okay, now we put it on the screen */
  98. int i;
  99. int mask;
  100. char d;
  101. for (i = 0; i < 16; i++) {
  102. __uint32_t *addr;
  103. int j;
  104. addr = FB_ADDR + VGA_X * (y + i) + x;
  105. mask = 0x0100;
  106. d = glyph[i];
  107. for (j = 0; j < 8; j++) {
  108. mask >>= 1;
  109. if (d & mask)
  110. addr[j] = fg;
  111. else
  112. addr[j] = bg;
  113. }
  114. }
  115. x += 8;
  116. schk_leave();
  117. return;
  118. }
  119. void print(__uint32_t fg, __uint32_t bg, char *s) {
  120. schk_init();
  121. for (; *s; s++) {
  122. if (*s == '\n')
  123. putc(' ', fg, bg);
  124. putc(*s, fg, bg);
  125. }
  126. schk_leave();
  127. return;
  128. }