fbtext.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. /* 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 (top 10 rows of 8x16 font); 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. if (!vga_has_vesa()) {
  47. fbtext_use_lfb = 0;
  48. fbtext_cols = 80;
  49. fbtext_rows = 25;
  50. return;
  51. }
  52. fbtext_use_lfb = 1;
  53. fbtext_cols = vga_fb_width() / 8;
  54. {
  55. unsigned int h = vga_fb_height();
  56. fbtext_rows = (h >= CONTENT_TOP + ROW_HEIGHT) ? (1 + (h - CONTENT_TOP) / ROW_HEIGHT) : 25;
  57. }
  58. if (fbtext_cols == 0) fbtext_cols = 80;
  59. if (fbtext_rows == 0) fbtext_rows = 25;
  60. fbtext_cur_col = 0;
  61. fbtext_cur_row = 0;
  62. fbtext_cursor_visible = 0;
  63. }
  64. int fbtext_active(void)
  65. {
  66. return fbtext_use_lfb;
  67. }
  68. unsigned int fbtext_get_cols(void)
  69. {
  70. return fbtext_cols;
  71. }
  72. unsigned int fbtext_get_rows(void)
  73. {
  74. return fbtext_rows;
  75. }
  76. void fbtext_clear(void)
  77. {
  78. if (!fbtext_use_lfb)
  79. return;
  80. vga_clear_rgb(0, 0, 0);
  81. }
  82. static void draw_glyph(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
  83. {
  84. unsigned int x0 = col * 8;
  85. unsigned int y0 = row_to_y0(row);
  86. unsigned int r_fg, g_fg, b_fg, r_bg, g_bg, b_bg;
  87. unsigned int x, y;
  88. const unsigned char *glyph;
  89. attr_to_rgb(attr, 1, &r_fg, &g_fg, &b_fg);
  90. attr_to_rgb(attr, 0, &r_bg, &g_bg, &b_bg);
  91. glyph = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  92. for (y = 0; y < ROW_HEIGHT; y++) {
  93. unsigned char row_bits = glyph[y];
  94. for (x = 0; x < 8; x++) {
  95. unsigned int px = x0 + x;
  96. unsigned int py = y0 + y;
  97. if (row_bits & (0x80u >> x))
  98. vga_put_pixel_rgb(px, py, r_fg, g_fg, b_fg);
  99. else
  100. vga_put_pixel_rgb(px, py, r_bg, g_bg, b_bg);
  101. }
  102. }
  103. }
  104. void fbtext_put_char_at(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
  105. {
  106. if (!fbtext_use_lfb)
  107. return;
  108. if (col >= fbtext_cols || row >= fbtext_rows)
  109. return;
  110. draw_glyph(col, row, c, attr);
  111. }
  112. void fbtext_scroll_up_one_line(void)
  113. {
  114. unsigned int y, i;
  115. unsigned int pitch = vga_fb_pitch();
  116. volatile unsigned char *fb = (volatile unsigned char *)vga_fb_ptr();
  117. unsigned int height = vga_fb_height();
  118. if (!fbtext_use_lfb || !fb)
  119. return;
  120. if (height <= CONTENT_TOP + ROW_HEIGHT)
  121. return;
  122. /* Scroll only the content area (below rainbow); header and rainbow stay fixed. */
  123. for (y = CONTENT_TOP; y < height - ROW_HEIGHT; y++) {
  124. unsigned int src_off = (y + ROW_HEIGHT) * pitch;
  125. unsigned int dst_off = y * pitch;
  126. for (i = 0; i < pitch; i++)
  127. fb[dst_off + i] = fb[src_off + i];
  128. }
  129. for (y = height - ROW_HEIGHT; y < height; y++) {
  130. unsigned int off = y * pitch;
  131. for (i = 0; i < pitch; i++)
  132. fb[off + i] = 0;
  133. }
  134. }
  135. void fbtext_set_cursor(unsigned int col, unsigned int row)
  136. {
  137. if (col >= fbtext_cols || row >= fbtext_rows)
  138. return;
  139. fbtext_cur_col = col;
  140. fbtext_cur_row = row;
  141. }
  142. /* Slim vertical line cursor: 2px wide, matches row height. */
  143. #define CURSOR_W 2
  144. #define CURSOR_H ROW_HEIGHT
  145. void fbtext_show_cursor(void)
  146. {
  147. unsigned int x0 = fbtext_cur_col * 8;
  148. unsigned int y0 = row_to_y0(fbtext_cur_row);
  149. unsigned int x, y;
  150. unsigned int r, g, b;
  151. if (!fbtext_use_lfb)
  152. return;
  153. fbtext_cursor_visible = 1;
  154. attr_to_rgb(0x0F, 1, &r, &g, &b); /* white line */
  155. for (y = 0; y < CURSOR_H; y++)
  156. for (x = 0; x < CURSOR_W; x++)
  157. vga_put_pixel_rgb(x0 + x, y0 + y, r, g, b);
  158. }
  159. /* Redraw only the cursor region (CURSOR_W x CURSOR_H) so the line is restored. */
  160. static void draw_glyph_cursor_region(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
  161. {
  162. unsigned int x0 = col * 8;
  163. unsigned int y0 = row_to_y0(row);
  164. unsigned int r_fg, g_fg, b_fg, r_bg, g_bg, b_bg;
  165. unsigned int x, y;
  166. const unsigned char *glyph;
  167. attr_to_rgb(attr, 1, &r_fg, &g_fg, &b_fg);
  168. attr_to_rgb(attr, 0, &r_bg, &g_bg, &b_bg);
  169. glyph = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  170. for (y = 0; y < ROW_HEIGHT && y < CURSOR_H; y++) {
  171. unsigned char row_bits = glyph[y];
  172. for (x = 0; x < CURSOR_W; x++) {
  173. unsigned int px = x0 + x;
  174. unsigned int py = y0 + y;
  175. if (row_bits & (0x80u >> x))
  176. vga_put_pixel_rgb(px, py, r_fg, g_fg, b_fg);
  177. else
  178. vga_put_pixel_rgb(px, py, r_bg, g_bg, b_bg);
  179. }
  180. }
  181. }
  182. void fbtext_hide_cursor(unsigned char c, unsigned char attr)
  183. {
  184. if (!fbtext_use_lfb)
  185. return;
  186. if (fbtext_cursor_visible) {
  187. fbtext_cursor_visible = 0;
  188. draw_glyph_cursor_region(fbtext_cur_col, fbtext_cur_row, c, attr);
  189. }
  190. }
  191. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R. Integer interpolation. */
  192. #define RAINBOW_SEG 256
  193. #define RAINBOW_FULL (6 * RAINBOW_SEG)
  194. static void hue_to_rgb(unsigned int hue, unsigned int *r, unsigned int *g, unsigned int *b)
  195. {
  196. unsigned int seg = hue / RAINBOW_SEG;
  197. unsigned int f = hue % RAINBOW_SEG;
  198. unsigned int r1, g1, b1, r2, g2, b2;
  199. if (seg >= 6)
  200. seg = 0;
  201. switch (seg) {
  202. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break; /* R → Y */
  203. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break; /* Y → G */
  204. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break; /* G → C */
  205. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break; /* C → B */
  206. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break; /* B → M */
  207. default: r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break; /* M → R */
  208. }
  209. *r = (r1 * (RAINBOW_SEG - f) + r2 * f) / RAINBOW_SEG;
  210. *g = (g1 * (RAINBOW_SEG - f) + g2 * f) / RAINBOW_SEG;
  211. *b = (b1 * (RAINBOW_SEG - f) + b2 * f) / RAINBOW_SEG;
  212. }
  213. #define RAINBOW_LINE_HEIGHT 2 /* 2-pixel tall line for visibility */
  214. void fbtext_draw_rainbow_separator(void)
  215. {
  216. unsigned int w = vga_fb_width();
  217. unsigned int x, y;
  218. unsigned int r, g, b;
  219. if (!fbtext_use_lfb || w == 0)
  220. return;
  221. for (y = 0; y < RAINBOW_LINE_HEIGHT; y++) {
  222. unsigned int py = RAINBOW_TOP + y;
  223. for (x = 0; x < w; x++) {
  224. unsigned int hue = (x * RAINBOW_FULL) / w;
  225. hue_to_rgb(hue, &r, &g, &b);
  226. vga_put_pixel_rgb(x, py, r, g, b);
  227. }
  228. }
  229. }