fbtext.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Framebuffer text: draw 8x8 characters into VESA LFB. */
  2. #include "fbtext.h"
  3. #include "font_8x8.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. /* Layout: header 5px down, rainbow 5px below header text, content 5px below rainbow. */
  10. #define HEADER_TOP 5 /* row 0 text at y 5..14 (only ROW_HEIGHT scanlines drawn) */
  11. #define RAINBOW_TOP 18 /* 5px below header text; line at y 18..19 */
  12. #define CONTENT_TOP 25 /* 5px below rainbow; first content row at y 25..34 */
  13. #define ROW_HEIGHT 10 /* drawn height per line (8x8 glyph + 2 line spacing); avoids clearing rainbow */
  14. static inline unsigned int row_to_y0(unsigned int row)
  15. {
  16. return row == 0 ? HEADER_TOP : (CONTENT_TOP + (row - 1) * ROW_HEIGHT);
  17. }
  18. /* VGA attribute (0-15) to RGB. Standard VGA palette. */
  19. static void attr_to_rgb(unsigned char attr, int fg, unsigned int *r, unsigned int *g, unsigned int *b)
  20. {
  21. static const unsigned int vga_rgb[16][3] = {
  22. { 0x00, 0x00, 0x00 }, /* 0 black */
  23. { 0x00, 0x00, 0xaa }, /* 1 blue */
  24. { 0x00, 0xaa, 0x00 }, /* 2 green */
  25. { 0x00, 0xaa, 0xaa }, /* 3 cyan */
  26. { 0xaa, 0x00, 0x00 }, /* 4 red */
  27. { 0xaa, 0x00, 0xaa }, /* 5 magenta */
  28. { 0xaa, 0x55, 0x00 }, /* 6 brown */
  29. { 0xaa, 0xaa, 0xaa }, /* 7 light gray */
  30. { 0x55, 0x55, 0x55 }, /* 8 dark gray */
  31. { 0x55, 0x55, 0xff }, /* 9 light blue */
  32. { 0x55, 0xff, 0x55 }, /* 10 light green */
  33. { 0x55, 0xff, 0xff }, /* 11 light cyan */
  34. { 0xff, 0x55, 0x55 }, /* 12 light red */
  35. { 0xff, 0x55, 0xff }, /* 13 light magenta */
  36. { 0xff, 0xff, 0x55 }, /* 14 yellow */
  37. { 0xff, 0xff, 0xff }, /* 15 white */
  38. };
  39. unsigned int idx = (unsigned int)(fg ? (attr & 0x0F) : ((attr >> 4) & 0x0F));
  40. *r = vga_rgb[idx][0];
  41. *g = vga_rgb[idx][1];
  42. *b = vga_rgb[idx][2];
  43. }
  44. void fbtext_init(void)
  45. {
  46. fbtext_use_lfb = 1;
  47. fbtext_cols = vga_fb_width() / 8;
  48. {
  49. unsigned int h = vga_fb_height();
  50. fbtext_rows = (h >= CONTENT_TOP + ROW_HEIGHT) ? (1 + (h - CONTENT_TOP) / ROW_HEIGHT) : 25;
  51. }
  52. if (fbtext_cols == 0) fbtext_cols = 80;
  53. if (fbtext_rows == 0) fbtext_rows = 25;
  54. fbtext_cur_col = 0;
  55. fbtext_cur_row = 0;
  56. fbtext_cursor_visible = 0;
  57. }
  58. int fbtext_active(void)
  59. {
  60. return fbtext_use_lfb;
  61. }
  62. unsigned int fbtext_get_cols(void)
  63. {
  64. return fbtext_cols;
  65. }
  66. unsigned int fbtext_get_rows(void)
  67. {
  68. return fbtext_rows;
  69. }
  70. void fbtext_clear(void)
  71. {
  72. vga_clear_rgb(0, 0, 0);
  73. }
  74. static void draw_glyph(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
  75. {
  76. unsigned int x0 = col * 8;
  77. unsigned int y0 = row_to_y0(row);
  78. unsigned int r_fg, g_fg, b_fg, r_bg, g_bg, b_bg;
  79. unsigned int x, y;
  80. const unsigned char *glyph;
  81. attr_to_rgb(attr, 1, &r_fg, &g_fg, &b_fg);
  82. attr_to_rgb(attr, 0, &r_bg, &g_bg, &b_bg);
  83. glyph = &font_8x8[(unsigned int)c * FONT_8X8_ROWS];
  84. for (y = 0; y < ROW_HEIGHT; y++) {
  85. unsigned char row_bits = (y < FONT_8X8_ROWS) ? glyph[y] : 0;
  86. for (x = 0; x < 8; x++) {
  87. unsigned int px = x0 + x;
  88. unsigned int py = y0 + y;
  89. if (row_bits & (0x80u >> x))
  90. vga_put_pixel_rgb(px, py, r_fg, g_fg, b_fg);
  91. else
  92. vga_put_pixel_rgb(px, py, r_bg, g_bg, b_bg);
  93. }
  94. }
  95. }
  96. void fbtext_put_char_at(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
  97. {
  98. if (col >= fbtext_cols || row >= fbtext_rows)
  99. return;
  100. draw_glyph(col, row, c, attr);
  101. }
  102. void fbtext_scroll_up_one_line(void)
  103. {
  104. unsigned int y, i;
  105. unsigned int pitch = vga_fb_pitch();
  106. volatile unsigned char *fb = (volatile unsigned char *)vga_fb_ptr();
  107. unsigned int height = vga_fb_height();
  108. if (!fb)
  109. return;
  110. if (height <= CONTENT_TOP + ROW_HEIGHT)
  111. return;
  112. /* Scroll only the content area (below rainbow); header and rainbow stay fixed. */
  113. for (y = CONTENT_TOP; y < height - ROW_HEIGHT; y++) {
  114. unsigned int src_off = (y + ROW_HEIGHT) * pitch;
  115. unsigned int dst_off = y * pitch;
  116. for (i = 0; i < pitch; i++)
  117. fb[dst_off + i] = fb[src_off + i];
  118. }
  119. for (y = height - ROW_HEIGHT; y < height; y++) {
  120. unsigned int off = y * pitch;
  121. for (i = 0; i < pitch; i++)
  122. fb[off + i] = 0;
  123. }
  124. }
  125. void fbtext_set_cursor(unsigned int col, unsigned int row)
  126. {
  127. if (col >= fbtext_cols || row >= fbtext_rows)
  128. return;
  129. fbtext_cur_col = col;
  130. fbtext_cur_row = row;
  131. }
  132. /* Slim vertical line cursor: 2px wide, matches row height. */
  133. #define CURSOR_W 2
  134. #define CURSOR_H ROW_HEIGHT
  135. void fbtext_show_cursor(void)
  136. {
  137. unsigned int x0 = fbtext_cur_col * 8;
  138. unsigned int y0 = row_to_y0(fbtext_cur_row);
  139. unsigned int x, y;
  140. unsigned int r, g, b;
  141. fbtext_cursor_visible = 1;
  142. attr_to_rgb(0x0F, 1, &r, &g, &b); /* white line */
  143. for (y = 0; y < CURSOR_H; y++)
  144. for (x = 0; x < CURSOR_W; x++)
  145. vga_put_pixel_rgb(x0 + x, y0 + y, r, g, b);
  146. }
  147. /* Redraw only the cursor region (CURSOR_W x CURSOR_H) so the line is restored. */
  148. static void draw_glyph_cursor_region(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
  149. {
  150. unsigned int x0 = col * 8;
  151. unsigned int y0 = row_to_y0(row);
  152. unsigned int r_fg, g_fg, b_fg, r_bg, g_bg, b_bg;
  153. unsigned int x, y;
  154. const unsigned char *glyph;
  155. attr_to_rgb(attr, 1, &r_fg, &g_fg, &b_fg);
  156. attr_to_rgb(attr, 0, &r_bg, &g_bg, &b_bg);
  157. glyph = &font_8x8[(unsigned int)c * FONT_8X8_ROWS];
  158. for (y = 0; y < ROW_HEIGHT && y < CURSOR_H; y++) {
  159. unsigned char row_bits = (y < FONT_8X8_ROWS) ? glyph[y] : 0;
  160. for (x = 0; x < CURSOR_W; x++) {
  161. unsigned int px = x0 + x;
  162. unsigned int py = y0 + y;
  163. if (row_bits & (0x80u >> x))
  164. vga_put_pixel_rgb(px, py, r_fg, g_fg, b_fg);
  165. else
  166. vga_put_pixel_rgb(px, py, r_bg, g_bg, b_bg);
  167. }
  168. }
  169. }
  170. void fbtext_hide_cursor(unsigned char c, unsigned char attr)
  171. {
  172. if (fbtext_cursor_visible) {
  173. fbtext_cursor_visible = 0;
  174. draw_glyph_cursor_region(fbtext_cur_col, fbtext_cur_row, c, attr);
  175. }
  176. }
  177. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R. Integer interpolation. */
  178. #define RAINBOW_SEG 256
  179. #define RAINBOW_FULL (6 * RAINBOW_SEG)
  180. static void hue_to_rgb(unsigned int hue, unsigned int *r, unsigned int *g, unsigned int *b)
  181. {
  182. unsigned int seg = hue / RAINBOW_SEG;
  183. unsigned int f = hue % RAINBOW_SEG;
  184. unsigned int r1, g1, b1, r2, g2, b2;
  185. if (seg >= 6)
  186. seg = 0;
  187. switch (seg) {
  188. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break; /* R → Y */
  189. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break; /* Y → G */
  190. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break; /* G → C */
  191. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break; /* C → B */
  192. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break; /* B → M */
  193. default: r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break; /* M → R */
  194. }
  195. *r = (r1 * (RAINBOW_SEG - f) + r2 * f) / RAINBOW_SEG;
  196. *g = (g1 * (RAINBOW_SEG - f) + g2 * f) / RAINBOW_SEG;
  197. *b = (b1 * (RAINBOW_SEG - f) + b2 * f) / RAINBOW_SEG;
  198. }
  199. #define RAINBOW_LINE_HEIGHT 2 /* 2-pixel tall line for visibility */
  200. void fbtext_draw_rainbow_separator(void)
  201. {
  202. unsigned int w = vga_fb_width();
  203. unsigned int x, y;
  204. unsigned int r, g, b;
  205. if (w == 0)
  206. return;
  207. for (y = 0; y < RAINBOW_LINE_HEIGHT; y++) {
  208. unsigned int py = RAINBOW_TOP + y;
  209. for (x = 0; x < w; x++) {
  210. unsigned int hue = (x * RAINBOW_FULL) / w;
  211. hue_to_rgb(hue, &r, &g, &b);
  212. vga_put_pixel_rgb(x, py, r, g, b);
  213. }
  214. }
  215. }