fbtext.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. unsigned char *fb = (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. /* Mark scrolled rows dirty so the flush copies them. */
  131. vga_fb_mark_dirty_rect(CONTENT_TOP, height - CONTENT_TOP);
  132. }
  133. void fbtext_set_cursor(unsigned int col, unsigned int row)
  134. {
  135. if (col >= fbtext_cols || row >= fbtext_rows)
  136. return;
  137. fbtext_cur_col = col;
  138. fbtext_cur_row = row;
  139. }
  140. /* Slim vertical line cursor: 2px wide, matches row height. */
  141. #define CURSOR_W 2
  142. #define CURSOR_H ROW_HEIGHT
  143. void fbtext_show_cursor(void)
  144. {
  145. unsigned int x0 = fbtext_cur_col * 8;
  146. unsigned int y0 = row_to_y0(fbtext_cur_row);
  147. unsigned int x, y;
  148. unsigned int r, g, b;
  149. fbtext_cursor_visible = 1;
  150. attr_to_rgb(0x0F, 1, &r, &g, &b); /* white line */
  151. for (y = 0; y < CURSOR_H; y++)
  152. for (x = 0; x < CURSOR_W; x++)
  153. vga_put_pixel_rgb(x0 + x, y0 + y, r, g, b);
  154. }
  155. /* Redraw only the cursor region (CURSOR_W x CURSOR_H) so the line is restored. */
  156. static void draw_glyph_cursor_region(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
  157. {
  158. unsigned int r_fg, g_fg, b_fg, r_bg, g_bg, b_bg;
  159. unsigned int x, y;
  160. const unsigned char *glyph = &font_8x8[(unsigned int)c * FONT_8X8_ROWS];
  161. unsigned int x0 = col * 8;
  162. unsigned int y0 = row_to_y0(row);
  163. attr_to_rgb(attr, 1, &r_fg, &g_fg, &b_fg);
  164. attr_to_rgb(attr, 0, &r_bg, &g_bg, &b_bg);
  165. for (y = 0; y < ROW_HEIGHT && y < CURSOR_H; y++) {
  166. unsigned char row_bits = (y < FONT_8X8_ROWS) ? glyph[y] : 0;
  167. for (x = 0; x < CURSOR_W; x++) {
  168. unsigned int px = x0 + x;
  169. unsigned int py = y0 + y;
  170. if (row_bits & (0x80u >> x))
  171. vga_put_pixel_rgb(px, py, r_fg, g_fg, b_fg);
  172. else
  173. vga_put_pixel_rgb(px, py, r_bg, g_bg, b_bg);
  174. }
  175. }
  176. }
  177. void fbtext_hide_cursor(unsigned char c, unsigned char attr)
  178. {
  179. if (fbtext_cursor_visible) {
  180. fbtext_cursor_visible = 0;
  181. draw_glyph_cursor_region(fbtext_cur_col, fbtext_cur_row, c, attr);
  182. }
  183. }
  184. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R. Integer interpolation. */
  185. #define RAINBOW_SEG 256
  186. #define RAINBOW_FULL (6 * RAINBOW_SEG)
  187. static void hue_to_rgb(unsigned int hue, unsigned int *r, unsigned int *g, unsigned int *b)
  188. {
  189. unsigned int seg = hue / RAINBOW_SEG;
  190. unsigned int f = hue % RAINBOW_SEG;
  191. unsigned int r1, g1, b1, r2, g2, b2;
  192. if (seg >= 6)
  193. seg = 0;
  194. switch (seg) {
  195. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break; /* R → Y */
  196. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break; /* Y → G */
  197. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break; /* G → C */
  198. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break; /* C → B */
  199. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break; /* B → M */
  200. default: r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break; /* M → R */
  201. }
  202. *r = (r1 * (RAINBOW_SEG - f) + r2 * f) / RAINBOW_SEG;
  203. *g = (g1 * (RAINBOW_SEG - f) + g2 * f) / RAINBOW_SEG;
  204. *b = (b1 * (RAINBOW_SEG - f) + b2 * f) / RAINBOW_SEG;
  205. }
  206. /* Draw string at pixel (x0, y0) with fg RGB. transparent_bg 1 = only fg pixels (for overlays). */
  207. void fbtext_draw_string_xy(unsigned int x0, unsigned int y0, const char *str,
  208. unsigned int r_fg, unsigned int g_fg, unsigned int b_fg, int transparent_bg)
  209. {
  210. unsigned int i = 0;
  211. while (str[i]) {
  212. 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);
  213. i++;
  214. }
  215. }
  216. #define RAINBOW_LINE_HEIGHT 2 /* 2-pixel tall line for visibility */
  217. void fbtext_draw_rainbow_separator(void)
  218. {
  219. unsigned int w = vga_fb_width();
  220. unsigned int x, y;
  221. unsigned int r, g, b;
  222. if (w == 0)
  223. return;
  224. for (y = 0; y < RAINBOW_LINE_HEIGHT; y++) {
  225. unsigned int py = RAINBOW_TOP + y;
  226. for (x = 0; x < w; x++) {
  227. unsigned int hue = (x * RAINBOW_FULL) / w;
  228. hue_to_rgb(hue, &r, &g, &b);
  229. vga_put_pixel_rgb(x, py, r, g, b);
  230. }
  231. }
  232. }