fbtext.c 9.9 KB

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