screen.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Text screen: cell-buffer text output (no framebuffer access). */
  2. #include "screen.h"
  3. #include "netlog.h"
  4. #define SCREEN_ATTR 0x0A /* light green on black */
  5. /* ---- Damage tracking ----
  6. *
  7. * The boot console tracks only the cell block that changed, so every mutation of the
  8. * cell buffer -- or of the cursor, which is drawn as an overlay -- has to mark
  9. * it: screen_damage_cell() grows the block around a cell, screen_damage_all()
  10. * covers the whole grid (a scroll or a clear) and screen_damage_cursor() marks
  11. * the cell the cursor sits in.
  12. */
  13. static void screen_damage_cell(struct text_screen *s, unsigned int col, unsigned int row)
  14. {
  15. if (!s->dirty) {
  16. s->dirty = 1;
  17. s->dmg_col0 = s->dmg_col1 = col;
  18. s->dmg_row0 = s->dmg_row1 = row;
  19. return;
  20. }
  21. if (col < s->dmg_col0)
  22. s->dmg_col0 = col;
  23. if (col > s->dmg_col1)
  24. s->dmg_col1 = col;
  25. if (row < s->dmg_row0)
  26. s->dmg_row0 = row;
  27. if (row > s->dmg_row1)
  28. s->dmg_row1 = row;
  29. }
  30. static void screen_damage_all(struct text_screen *s)
  31. {
  32. s->dirty = 1;
  33. s->dmg_col0 = 0;
  34. s->dmg_col1 = s->cols - 1;
  35. s->dmg_row0 = 0;
  36. s->dmg_row1 = s->rows - 1;
  37. }
  38. void screen_damage_cursor(struct text_screen *s)
  39. {
  40. unsigned int pos = s->cursor;
  41. if (pos >= s->cols * s->rows) {
  42. screen_damage_all(s);
  43. return;
  44. }
  45. screen_damage_cell(s, pos % s->cols, pos / s->cols);
  46. }
  47. void screen_init(struct text_screen *s)
  48. {
  49. s->cols = SCREEN_COLS;
  50. s->rows = SCREEN_ROWS;
  51. s->cursor = 0;
  52. s->cursor_attr = (SCREEN_ATTR << 8);
  53. s->cursor_visible = 0;
  54. screen_clear(s);
  55. }
  56. void screen_clear(struct text_screen *s)
  57. {
  58. unsigned int i, n = s->cols * s->rows;
  59. for (i = 0; i < n; i++)
  60. s->cells[i] = (unsigned short)((SCREEN_ATTR << 8) | ' ');
  61. s->cursor = 0;
  62. screen_damage_all(s);
  63. }
  64. static void screen_scroll_up(struct text_screen *s)
  65. {
  66. unsigned int ncols = s->cols;
  67. unsigned int total = s->cols * s->rows;
  68. unsigned int i;
  69. for (i = 0; i < total - ncols; i++)
  70. s->cells[i] = s->cells[i + ncols];
  71. for (; i < total; i++)
  72. s->cells[i] = (unsigned short)((SCREEN_ATTR << 8) | ' ');
  73. screen_damage_all(s);
  74. }
  75. void screen_putchar(struct text_screen *s, char c)
  76. {
  77. unsigned int ncols = s->cols;
  78. unsigned int total = s->cols * s->rows;
  79. netlog_char(c); /* terminal text also goes to the netlog mirror */
  80. if (c == '\n') {
  81. screen_damage_cursor(s); /* the cursor leaves this cell */
  82. s->cursor = (s->cursor / ncols + 1) * ncols;
  83. if (s->cursor >= total) {
  84. screen_scroll_up(s);
  85. s->cursor = total - ncols;
  86. }
  87. s->cursor_attr = (SCREEN_ATTR << 8);
  88. screen_damage_cursor(s);
  89. return;
  90. }
  91. if (c == '\b') {
  92. if (s->cursor > 0) {
  93. screen_damage_cursor(s);
  94. s->cursor--;
  95. s->cells[s->cursor] = (unsigned short)((SCREEN_ATTR << 8) | ' ');
  96. screen_damage_cursor(s);
  97. }
  98. return;
  99. }
  100. if (s->cursor < total) {
  101. screen_damage_cursor(s); /* the written cell + the cursor */
  102. s->cells[s->cursor] = (unsigned short)(s->cursor_attr | (unsigned char)c);
  103. s->cursor++;
  104. s->cursor_attr = (SCREEN_ATTR << 8);
  105. screen_damage_cursor(s); /* the cursor moved on */
  106. }
  107. }
  108. void screen_putstr(struct text_screen *s, const char *str)
  109. {
  110. while (*str)
  111. screen_putchar(s, *str++);
  112. }
  113. void screen_puthex(struct text_screen *s, unsigned int n)
  114. {
  115. static const char hex[] = "0123456789ABCDEF";
  116. char buf[9];
  117. unsigned int i = 8;
  118. buf[8] = '\0';
  119. do {
  120. buf[--i] = hex[n & 0xF];
  121. n >>= 4;
  122. } while (i > 0);
  123. screen_putstr(s, buf);
  124. }
  125. void screen_put_at(struct text_screen *s, unsigned int pos, unsigned short value)
  126. {
  127. if (pos < s->cols * s->rows) {
  128. screen_damage_cell(s, pos % s->cols, pos / s->cols);
  129. s->cells[pos] = value;
  130. }
  131. }