fbtext.c 8.0 KB

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