graphic.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. struct psf_header *psf = (struct psf_header *) &font_start;
  74. /*
  75. * yeah, it's very ugly. but it just get address of this glyph, and check
  76. * 'c' is really in this font.
  77. * */
  78. __uint8_t *glyph = (__uint8_t *) &font_start + psf->header_size +
  79. (c > 0 && c < psf->glyphs_count ? c : 0) * psf->glyph_size;
  80. /* need next line? */
  81. if (x >= VGA_X) {
  82. x = 0;
  83. y += CHAR_HEIGHT;
  84. }
  85. /* no enough space for this character */
  86. /* VGA_Y % CHAR_HEIGHT != 0, so we cannot use y == VGA_Y */
  87. if (y + CHAR_HEIGHT > VGA_Y)
  88. y = 0;
  89. /* okay, now we put it on the screen */
  90. int i;
  91. int mask;
  92. char d;
  93. for (i = 0; i < 16; i++) {
  94. __uint32_t *addr;
  95. int j;
  96. addr = FB_ADDR + VGA_X * (y + i) + x;
  97. mask = 0x0100;
  98. d = glyph[i];
  99. for (j = 0; j < 8; j++) {
  100. mask >>= 1;
  101. if (d & mask)
  102. addr[j] = fg;
  103. else
  104. addr[j] = bg;
  105. }
  106. }
  107. x += 8;
  108. schk_leave();
  109. return;
  110. }
  111. void print(__uint32_t fg, __uint32_t bg, char *s) {
  112. schk_init();
  113. for (; *s; s++) {
  114. if (*s == '\n')
  115. putc(' ', fg, bg);
  116. putc(*s, fg, bg);
  117. }
  118. schk_leave();
  119. return;
  120. }