fbtext.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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), clipped to [cx0,cx1) x [cy0,cy1).
  19. height 8 = glyph only; 10 = glyph + 2 rows bg (terminal row).
  20. skip_bg: 1 = only draw foreground (transparent background for overlays).
  21. Writes directly into the shadow buffer (no per-pixel function call); the
  22. caller is responsible for marking the affected rows dirty. */
  23. static void draw_glyph_at_clip(unsigned int x0, unsigned int y0, unsigned char c,
  24. unsigned int r_fg, unsigned int g_fg, unsigned int b_fg,
  25. unsigned int r_bg, unsigned int g_bg, unsigned int b_bg,
  26. unsigned int height, int skip_bg,
  27. unsigned int cx0, unsigned int cy0, unsigned int cx1, unsigned int cy1)
  28. {
  29. unsigned int x, y;
  30. const unsigned char *glyph = &font_8x8[(unsigned int)c * FONT_8X8_ROWS];
  31. unsigned char *fb = (unsigned char *)vga_fb_ptr();
  32. unsigned int pitch = vga_fb_pitch();
  33. unsigned int bpp = vga_fb_bpp();
  34. unsigned int fg32 = (r_fg << 16) | (g_fg << 8) | b_fg;
  35. unsigned int bg32 = (r_bg << 16) | (g_bg << 8) | b_bg;
  36. unsigned short fg16 = (unsigned short)(((r_fg >> 3) << 11) | ((g_fg >> 2) << 5) | (b_fg >> 3));
  37. unsigned short bg16 = (unsigned short)(((r_bg >> 3) << 11) | ((g_bg >> 2) << 5) | (b_bg >> 3));
  38. for (y = 0; y < height; y++) {
  39. unsigned char row_bits = (y < FONT_8X8_ROWS) ? glyph[y] : 0;
  40. unsigned int py = y0 + y;
  41. unsigned int xa, xb;
  42. if (py < cy0 || py >= cy1)
  43. continue;
  44. /* Columns of this row inside the clip (a cell that merely
  45. intersects the clip is only painted where it is visible). */
  46. xa = (x0 > cx0) ? x0 : cx0;
  47. xb = (x0 + 8 < cx1) ? x0 + 8 : cx1;
  48. if (xa >= xb)
  49. continue;
  50. if (bpp == 16) {
  51. unsigned short *p = (unsigned short *)(fb + (unsigned long)py * pitch + xa * 2);
  52. for (x = xa; x < xb; x++) {
  53. if (row_bits & (0x80u >> (x - x0)))
  54. *p = fg16;
  55. else if (!skip_bg)
  56. *p = bg16;
  57. p++;
  58. }
  59. } else if (bpp == 24) {
  60. unsigned char *p = fb + (unsigned long)py * pitch + xa * 3;
  61. for (x = xa; x < xb; x++) {
  62. if (row_bits & (0x80u >> (x - x0))) {
  63. p[0] = (unsigned char)b_fg; p[1] = (unsigned char)g_fg; p[2] = (unsigned char)r_fg;
  64. } else if (!skip_bg) {
  65. p[0] = (unsigned char)b_bg; p[1] = (unsigned char)g_bg; p[2] = (unsigned char)r_bg;
  66. }
  67. p += 3;
  68. }
  69. } else {
  70. unsigned int *p = (unsigned int *)(fb + (unsigned long)py * pitch + xa * 4);
  71. for (x = xa; x < xb; x++) {
  72. if (row_bits & (0x80u >> (x - x0)))
  73. *p = fg32;
  74. else if (!skip_bg)
  75. *p = bg32;
  76. p++;
  77. }
  78. }
  79. }
  80. }
  81. static void draw_glyph_at(unsigned int x0, unsigned int y0, unsigned char c,
  82. unsigned int r_fg, unsigned int g_fg, unsigned int b_fg,
  83. unsigned int r_bg, unsigned int g_bg, unsigned int b_bg,
  84. unsigned int height, int skip_bg)
  85. {
  86. draw_glyph_at_clip(x0, y0, c, r_fg, g_fg, b_fg, r_bg, g_bg, b_bg,
  87. height, skip_bg, 0, 0, 0xFFFFFFFFu, 0xFFFFFFFFu);
  88. }
  89. /* VGA attribute (0-15) to RGB. Standard VGA palette. */
  90. static void attr_to_rgb(unsigned char attr, int fg, unsigned int *r, unsigned int *g, unsigned int *b)
  91. {
  92. static const unsigned int vga_rgb[16][3] = {
  93. { 0x00, 0x00, 0x00 }, /* 0 black */
  94. { 0x00, 0x00, 0xaa }, /* 1 blue */
  95. { 0x00, 0xaa, 0x00 }, /* 2 green */
  96. { 0x00, 0xaa, 0xaa }, /* 3 cyan */
  97. { 0xaa, 0x00, 0x00 }, /* 4 red */
  98. { 0xaa, 0x00, 0xaa }, /* 5 magenta */
  99. { 0xaa, 0x55, 0x00 }, /* 6 brown */
  100. { 0xaa, 0xaa, 0xaa }, /* 7 light gray */
  101. { 0x55, 0x55, 0x55 }, /* 8 dark gray */
  102. { 0x55, 0x55, 0xff }, /* 9 light blue */
  103. { 0x55, 0xff, 0x55 }, /* 10 light green */
  104. { 0x55, 0xff, 0xff }, /* 11 light cyan */
  105. { 0xff, 0x55, 0x55 }, /* 12 light red */
  106. { 0xff, 0x55, 0xff }, /* 13 light magenta */
  107. { 0xff, 0xff, 0x55 }, /* 14 yellow */
  108. { 0xff, 0xff, 0xff }, /* 15 white */
  109. };
  110. unsigned int idx = (unsigned int)(fg ? (attr & 0x0F) : ((attr >> 4) & 0x0F));
  111. *r = vga_rgb[idx][0];
  112. *g = vga_rgb[idx][1];
  113. *b = vga_rgb[idx][2];
  114. }
  115. void fbtext_init(void)
  116. {
  117. fbtext_use_lfb = 1;
  118. fbtext_cols = vga_fb_width() / 8;
  119. {
  120. unsigned int h = vga_fb_height();
  121. fbtext_rows = (h >= CONTENT_TOP + ROW_HEIGHT) ? (1 + (h - CONTENT_TOP) / ROW_HEIGHT) : 25;
  122. }
  123. if (fbtext_cols == 0) fbtext_cols = 80;
  124. if (fbtext_rows == 0) fbtext_rows = 25;
  125. fbtext_cur_col = 0;
  126. fbtext_cur_row = 0;
  127. fbtext_cursor_visible = 0;
  128. }
  129. int fbtext_active(void)
  130. {
  131. return fbtext_use_lfb;
  132. }
  133. unsigned int fbtext_get_cols(void)
  134. {
  135. return fbtext_cols;
  136. }
  137. unsigned int fbtext_get_rows(void)
  138. {
  139. return fbtext_rows;
  140. }
  141. void fbtext_clear(void)
  142. {
  143. vga_clear_rgb(0, 0, 0);
  144. }
  145. static void draw_glyph(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
  146. {
  147. unsigned int r_fg, g_fg, b_fg, r_bg, g_bg, b_bg;
  148. attr_to_rgb(attr, 1, &r_fg, &g_fg, &b_fg);
  149. attr_to_rgb(attr, 0, &r_bg, &g_bg, &b_bg);
  150. 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);
  151. }
  152. void fbtext_put_char_at(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
  153. {
  154. if (col >= fbtext_cols || row >= fbtext_rows)
  155. return;
  156. draw_glyph(col, row, c, attr);
  157. }
  158. void fbtext_draw_cells(const unsigned short *cells, unsigned int cols, unsigned int rows,
  159. unsigned int x0, unsigned int y0)
  160. {
  161. unsigned int row, col;
  162. for (row = 0; row < rows; row++) {
  163. for (col = 0; col < cols; col++) {
  164. unsigned short cell = cells[row * cols + col];
  165. unsigned char c = (unsigned char)(cell & 0xFF);
  166. unsigned char attr = (unsigned char)(cell >> 8);
  167. unsigned int rf, gf, bf, rb, gb, bb;
  168. attr_to_rgb(attr, 1, &rf, &gf, &bf);
  169. attr_to_rgb(attr, 0, &rb, &gb, &bb);
  170. draw_glyph_at(x0 + col * 8, y0 + row * ROW_HEIGHT, c,
  171. rf, gf, bf, rb, gb, bb, ROW_HEIGHT, 0);
  172. }
  173. }
  174. vga_fb_mark_dirty_rect_xy(y0, rows * ROW_HEIGHT, x0, x0 + cols * 8);
  175. }
  176. void fbtext_draw_cells_clip(const unsigned short *cells, unsigned int cols, unsigned int rows,
  177. unsigned int x0, unsigned int y0,
  178. unsigned int cx, unsigned int cy, unsigned int cw, unsigned int ch)
  179. {
  180. int col0 = ((int)cx - (int)x0) / 8;
  181. int col1 = ((int)(cx + cw) - (int)x0 + 7) / 8;
  182. int row0 = ((int)cy - (int)y0) / ROW_HEIGHT;
  183. int row1 = ((int)(cy + ch) - (int)y0 + (ROW_HEIGHT - 1)) / ROW_HEIGHT;
  184. unsigned int row, col;
  185. if (col0 < 0) col0 = 0;
  186. if (row0 < 0) row0 = 0;
  187. if (col1 > (int)cols) col1 = (int)cols;
  188. if (row1 > (int)rows) row1 = (int)rows;
  189. if (col1 <= col0 || row1 <= row0)
  190. return;
  191. for (row = (unsigned int)row0; row < (unsigned int)row1; row++) {
  192. for (col = (unsigned int)col0; col < (unsigned int)col1; col++) {
  193. unsigned short cell = cells[row * cols + col];
  194. unsigned char c = (unsigned char)(cell & 0xFF);
  195. unsigned char attr = (unsigned char)(cell >> 8);
  196. unsigned int rf, gf, bf, rb, gb, bb;
  197. attr_to_rgb(attr, 1, &rf, &gf, &bf);
  198. attr_to_rgb(attr, 0, &rb, &gb, &bb);
  199. draw_glyph_at_clip(x0 + col * 8, y0 + row * ROW_HEIGHT, c,
  200. rf, gf, bf, rb, gb, bb, ROW_HEIGHT, 0,
  201. cx, cy, cx + cw, cy + ch);
  202. }
  203. }
  204. vga_fb_mark_dirty_rect_xy(y0 + (unsigned int)row0 * ROW_HEIGHT,
  205. (unsigned int)(row1 - row0) * ROW_HEIGHT,
  206. x0 + (unsigned int)col0 * 8, x0 + (unsigned int)col1 * 8);
  207. }
  208. void fbtext_scroll_up_one_line(void)
  209. {
  210. unsigned int y, i;
  211. unsigned int pitch = vga_fb_pitch();
  212. unsigned char *fb = (unsigned char *)vga_fb_ptr();
  213. unsigned int height = vga_fb_height();
  214. if (!fb)
  215. return;
  216. if (height <= CONTENT_TOP + ROW_HEIGHT)
  217. return;
  218. /* Scroll only the content area (below rainbow); header and rainbow stay fixed. */
  219. for (y = CONTENT_TOP; y < height - ROW_HEIGHT; y++) {
  220. unsigned int src_off = (y + ROW_HEIGHT) * pitch;
  221. unsigned int dst_off = y * pitch;
  222. for (i = 0; i < pitch; i++)
  223. fb[dst_off + i] = fb[src_off + i];
  224. }
  225. for (y = height - ROW_HEIGHT; y < height; y++) {
  226. unsigned int off = y * pitch;
  227. for (i = 0; i < pitch; i++)
  228. fb[off + i] = 0;
  229. }
  230. /* Mark scrolled rows dirty so the flush copies them. */
  231. vga_fb_mark_dirty_rect(CONTENT_TOP, height - CONTENT_TOP);
  232. }
  233. void fbtext_set_cursor(unsigned int col, unsigned int row)
  234. {
  235. if (col >= fbtext_cols || row >= fbtext_rows)
  236. return;
  237. fbtext_cur_col = col;
  238. fbtext_cur_row = row;
  239. }
  240. /* Slim vertical line cursor: 2px wide, matches row height. */
  241. #define CURSOR_W 2
  242. #define CURSOR_H ROW_HEIGHT
  243. void fbtext_show_cursor(void)
  244. {
  245. unsigned int x0 = fbtext_cur_col * 8;
  246. unsigned int y0 = row_to_y0(fbtext_cur_row);
  247. unsigned int x, y;
  248. unsigned int r, g, b;
  249. fbtext_cursor_visible = 1;
  250. attr_to_rgb(0x0F, 1, &r, &g, &b); /* white line */
  251. for (y = 0; y < CURSOR_H; y++)
  252. for (x = 0; x < CURSOR_W; x++)
  253. vga_put_pixel_rgb(x0 + x, y0 + y, r, g, b);
  254. }
  255. /* Redraw only the cursor region (CURSOR_W x CURSOR_H) so the line is restored. */
  256. static void draw_glyph_cursor_region(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
  257. {
  258. unsigned int r_fg, g_fg, b_fg, r_bg, g_bg, b_bg;
  259. unsigned int x, y;
  260. const unsigned char *glyph = &font_8x8[(unsigned int)c * FONT_8X8_ROWS];
  261. unsigned int x0 = col * 8;
  262. unsigned int y0 = row_to_y0(row);
  263. attr_to_rgb(attr, 1, &r_fg, &g_fg, &b_fg);
  264. attr_to_rgb(attr, 0, &r_bg, &g_bg, &b_bg);
  265. for (y = 0; y < ROW_HEIGHT && y < CURSOR_H; y++) {
  266. unsigned char row_bits = (y < FONT_8X8_ROWS) ? glyph[y] : 0;
  267. for (x = 0; x < CURSOR_W; x++) {
  268. unsigned int px = x0 + x;
  269. unsigned int py = y0 + y;
  270. if (row_bits & (0x80u >> x))
  271. vga_put_pixel_rgb(px, py, r_fg, g_fg, b_fg);
  272. else
  273. vga_put_pixel_rgb(px, py, r_bg, g_bg, b_bg);
  274. }
  275. }
  276. }
  277. void fbtext_hide_cursor(unsigned char c, unsigned char attr)
  278. {
  279. if (fbtext_cursor_visible) {
  280. fbtext_cursor_visible = 0;
  281. draw_glyph_cursor_region(fbtext_cur_col, fbtext_cur_row, c, attr);
  282. }
  283. }
  284. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R. Integer interpolation. */
  285. #define RAINBOW_SEG 256
  286. #define RAINBOW_FULL (6 * RAINBOW_SEG)
  287. static void hue_to_rgb(unsigned int hue, unsigned int *r, unsigned int *g, unsigned int *b)
  288. {
  289. unsigned int seg = hue / RAINBOW_SEG;
  290. unsigned int f = hue % RAINBOW_SEG;
  291. unsigned int r1, g1, b1, r2, g2, b2;
  292. if (seg >= 6)
  293. seg = 0;
  294. switch (seg) {
  295. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break; /* R → Y */
  296. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break; /* Y → G */
  297. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break; /* G → C */
  298. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break; /* C → B */
  299. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break; /* B → M */
  300. default: r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break; /* M → R */
  301. }
  302. *r = (r1 * (RAINBOW_SEG - f) + r2 * f) / RAINBOW_SEG;
  303. *g = (g1 * (RAINBOW_SEG - f) + g2 * f) / RAINBOW_SEG;
  304. *b = (b1 * (RAINBOW_SEG - f) + b2 * f) / RAINBOW_SEG;
  305. }
  306. /* Draw string at pixel (x0, y0) with fg RGB. transparent_bg 1 = only fg pixels (for overlays). */
  307. void fbtext_draw_string_xy(unsigned int x0, unsigned int y0, const char *str,
  308. unsigned int r_fg, unsigned int g_fg, unsigned int b_fg, int transparent_bg)
  309. {
  310. unsigned int i = 0;
  311. while (str[i]) {
  312. 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);
  313. i++;
  314. }
  315. vga_fb_mark_dirty_rect_xy(y0, FONT_8X8_ROWS, x0, x0 + i * 8);
  316. }
  317. /* Like fbtext_draw_string_xy, but per-pixel clipped to (cx, cy, cw, ch): only
  318. * foreground pixels inside the clip rect are written. */
  319. void fbtext_draw_string_xy_clip(unsigned int x0, unsigned int y0, const char *str,
  320. unsigned int r_fg, unsigned int g_fg, unsigned int b_fg,
  321. unsigned int cx, unsigned int cy, unsigned int cw, unsigned int ch)
  322. {
  323. unsigned int i;
  324. for (i = 0; str[i]; i++) {
  325. unsigned int gx = x0 + i * 8;
  326. const unsigned char *glyph;
  327. unsigned int x, y;
  328. if (gx + 8 <= cx || gx >= cx + cw ||
  329. y0 + FONT_8X8_ROWS <= cy || y0 >= cy + ch)
  330. continue;
  331. glyph = &font_8x8[(unsigned int)str[i] * FONT_8X8_ROWS];
  332. for (y = 0; y < FONT_8X8_ROWS; y++) {
  333. unsigned char bits = glyph[y];
  334. unsigned int py = y0 + y;
  335. unsigned int xa, xb;
  336. if (!bits || py < cy || py >= cy + ch)
  337. continue;
  338. xa = (gx > cx) ? gx : cx;
  339. xb = (gx + 8 < cx + cw) ? gx + 8 : cx + cw;
  340. for (x = xa; x < xb; x++)
  341. if (bits & (0x80u >> (x - gx)))
  342. vga_put_pixel_rgb(x, py, r_fg, g_fg, b_fg);
  343. }
  344. }
  345. vga_fb_mark_dirty_rect_xy(cy, ch, cx, cx + cw);
  346. }
  347. #define RAINBOW_LINE_HEIGHT 2 /* 2-pixel tall line for visibility */
  348. void fbtext_draw_rainbow_separator(void)
  349. {
  350. unsigned int w = vga_fb_width();
  351. unsigned int x, y;
  352. unsigned int r, g, b;
  353. if (w == 0)
  354. return;
  355. for (y = 0; y < RAINBOW_LINE_HEIGHT; y++) {
  356. unsigned int py = RAINBOW_TOP + y;
  357. for (x = 0; x < w; x++) {
  358. unsigned int hue = (x * RAINBOW_FULL) / w;
  359. hue_to_rgb(hue, &r, &g, &b);
  360. vga_put_pixel_rgb(x, py, r, g, b);
  361. }
  362. }
  363. }