fbtext.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Framebuffer text: draw 8x16 characters into VESA LFB. */
  2. #include "fbtext.h"
  3. #include "font_8x16.h"
  4. #include "vga.h"
  5. static int fbtext_use_lfb;
  6. static unsigned int fbtext_cols, fbtext_rows;
  7. static unsigned int fbtext_cur_col, fbtext_cur_row;
  8. static int fbtext_cursor_visible;
  9. /* VGA attribute (0-15) to RGB. Standard VGA palette. */
  10. static void attr_to_rgb(unsigned char attr, int fg, unsigned int *r, unsigned int *g, unsigned int *b)
  11. {
  12. static const unsigned int vga_rgb[16][3] = {
  13. { 0x00, 0x00, 0x00 }, /* 0 black */
  14. { 0x00, 0x00, 0xaa }, /* 1 blue */
  15. { 0x00, 0xaa, 0x00 }, /* 2 green */
  16. { 0x00, 0xaa, 0xaa }, /* 3 cyan */
  17. { 0xaa, 0x00, 0x00 }, /* 4 red */
  18. { 0xaa, 0x00, 0xaa }, /* 5 magenta */
  19. { 0xaa, 0x55, 0x00 }, /* 6 brown */
  20. { 0xaa, 0xaa, 0xaa }, /* 7 light gray */
  21. { 0x55, 0x55, 0x55 }, /* 8 dark gray */
  22. { 0x55, 0x55, 0xff }, /* 9 light blue */
  23. { 0x55, 0xff, 0x55 }, /* 10 light green */
  24. { 0x55, 0xff, 0xff }, /* 11 light cyan */
  25. { 0xff, 0x55, 0x55 }, /* 12 light red */
  26. { 0xff, 0x55, 0xff }, /* 13 light magenta */
  27. { 0xff, 0xff, 0x55 }, /* 14 yellow */
  28. { 0xff, 0xff, 0xff }, /* 15 white */
  29. };
  30. unsigned int idx = (unsigned int)(fg ? (attr & 0x0F) : ((attr >> 4) & 0x0F));
  31. *r = vga_rgb[idx][0];
  32. *g = vga_rgb[idx][1];
  33. *b = vga_rgb[idx][2];
  34. }
  35. void fbtext_init(void)
  36. {
  37. if (!vga_has_vesa()) {
  38. fbtext_use_lfb = 0;
  39. fbtext_cols = 80;
  40. fbtext_rows = 25;
  41. return;
  42. }
  43. fbtext_use_lfb = 1;
  44. fbtext_cols = vga_fb_width() / 8;
  45. fbtext_rows = vga_fb_height() / 16;
  46. if (fbtext_cols == 0) fbtext_cols = 80;
  47. if (fbtext_rows == 0) fbtext_rows = 25;
  48. fbtext_cur_col = 0;
  49. fbtext_cur_row = 0;
  50. fbtext_cursor_visible = 0;
  51. }
  52. int fbtext_active(void)
  53. {
  54. return fbtext_use_lfb;
  55. }
  56. unsigned int fbtext_get_cols(void)
  57. {
  58. return fbtext_cols;
  59. }
  60. unsigned int fbtext_get_rows(void)
  61. {
  62. return fbtext_rows;
  63. }
  64. void fbtext_clear(void)
  65. {
  66. if (!fbtext_use_lfb)
  67. return;
  68. vga_clear_rgb(0, 0, 0);
  69. }
  70. static void draw_glyph(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
  71. {
  72. unsigned int x0 = col * 8;
  73. unsigned int y0 = row * 16;
  74. unsigned int r_fg, g_fg, b_fg, r_bg, g_bg, b_bg;
  75. unsigned int x, y;
  76. const unsigned char *glyph;
  77. attr_to_rgb(attr, 1, &r_fg, &g_fg, &b_fg);
  78. attr_to_rgb(attr, 0, &r_bg, &g_bg, &b_bg);
  79. glyph = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  80. for (y = 0; y < 16; y++) {
  81. unsigned char row_bits = glyph[y];
  82. for (x = 0; x < 8; x++) {
  83. unsigned int px = x0 + x;
  84. unsigned int py = y0 + y;
  85. if (row_bits & (0x80u >> x))
  86. vga_put_pixel_rgb(px, py, r_fg, g_fg, b_fg);
  87. else
  88. vga_put_pixel_rgb(px, py, r_bg, g_bg, b_bg);
  89. }
  90. }
  91. }
  92. void fbtext_put_char_at(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
  93. {
  94. if (!fbtext_use_lfb)
  95. return;
  96. if (col >= fbtext_cols || row >= fbtext_rows)
  97. return;
  98. draw_glyph(col, row, c, attr);
  99. }
  100. void fbtext_scroll_up_one_line(void)
  101. {
  102. unsigned int y, i;
  103. unsigned int pitch = vga_fb_pitch();
  104. volatile unsigned char *fb = (volatile unsigned char *)vga_fb_ptr();
  105. unsigned int height = vga_fb_height();
  106. if (!fbtext_use_lfb || !fb)
  107. return;
  108. if (height <= 16)
  109. return;
  110. for (y = 16; y < height - 16; y++) {
  111. unsigned int src_off = (y + 16) * pitch;
  112. unsigned int dst_off = y * pitch;
  113. for (i = 0; i < pitch; i++)
  114. fb[dst_off + i] = fb[src_off + i];
  115. }
  116. for (y = height - 16; y < height; y++) {
  117. unsigned int off = y * pitch;
  118. for (i = 0; i < pitch; i++)
  119. fb[off + i] = 0;
  120. }
  121. }
  122. void fbtext_set_cursor(unsigned int col, unsigned int row)
  123. {
  124. if (col >= fbtext_cols || row >= fbtext_rows)
  125. return;
  126. fbtext_cur_col = col;
  127. fbtext_cur_row = row;
  128. }
  129. void fbtext_show_cursor(void)
  130. {
  131. unsigned int x0 = fbtext_cur_col * 8;
  132. unsigned int y0 = fbtext_cur_row * 16;
  133. unsigned int x, y;
  134. unsigned int r, g, b;
  135. if (!fbtext_use_lfb)
  136. return;
  137. fbtext_cursor_visible = 1;
  138. attr_to_rgb(0x0F, 1, &r, &g, &b); /* white block */
  139. for (y = 0; y < 16; y++)
  140. for (x = 0; x < 8; x++)
  141. vga_put_pixel_rgb(x0 + x, y0 + y, r, g, b);
  142. }
  143. void fbtext_hide_cursor(unsigned char c, unsigned char attr)
  144. {
  145. if (!fbtext_use_lfb)
  146. return;
  147. if (fbtext_cursor_visible) {
  148. fbtext_cursor_visible = 0;
  149. draw_glyph(fbtext_cur_col, fbtext_cur_row, c, attr);
  150. }
  151. }