screen.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 WM repaints 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. int screen_damage_block(const struct text_screen *s, unsigned int *col0,
  48. unsigned int *row0, unsigned int *cols, unsigned int *rows)
  49. {
  50. if (!s->dirty)
  51. return 0;
  52. *col0 = s->dmg_col0;
  53. *row0 = s->dmg_row0;
  54. *cols = s->dmg_col1 - s->dmg_col0 + 1;
  55. *rows = s->dmg_row1 - s->dmg_row0 + 1;
  56. return 1;
  57. }
  58. void screen_clear_damage(struct text_screen *s)
  59. {
  60. s->dirty = 0;
  61. }
  62. void screen_init(struct text_screen *s)
  63. {
  64. s->cols = SCREEN_COLS;
  65. s->rows = SCREEN_ROWS;
  66. s->cursor = 0;
  67. s->cursor_attr = (SCREEN_ATTR << 8);
  68. s->cursor_visible = 0;
  69. s->scrolled = 0;
  70. screen_clear(s);
  71. }
  72. void screen_clear(struct text_screen *s)
  73. {
  74. unsigned int i, n = s->cols * s->rows;
  75. for (i = 0; i < n; i++)
  76. s->cells[i] = (unsigned short)((SCREEN_ATTR << 8) | ' ');
  77. s->cursor = 0;
  78. screen_damage_all(s);
  79. s->scrolled = 0;
  80. }
  81. static void screen_scroll_up(struct text_screen *s)
  82. {
  83. unsigned int ncols = s->cols;
  84. unsigned int total = s->cols * s->rows;
  85. unsigned int i;
  86. for (i = 0; i < total - ncols; i++)
  87. s->cells[i] = s->cells[i + ncols];
  88. for (; i < total; i++)
  89. s->cells[i] = (unsigned short)((SCREEN_ATTR << 8) | ' ');
  90. screen_damage_all(s);
  91. }
  92. void screen_putchar(struct text_screen *s, char c)
  93. {
  94. unsigned int ncols = s->cols;
  95. unsigned int total = s->cols * s->rows;
  96. netlog_char(c); /* terminal text also goes to the netlog mirror */
  97. if (c == '\n') {
  98. screen_damage_cursor(s); /* the cursor leaves this cell */
  99. s->cursor = (s->cursor / ncols + 1) * ncols;
  100. if (s->cursor >= total) {
  101. screen_scroll_up(s);
  102. s->cursor = total - ncols;
  103. s->scrolled = 1;
  104. }
  105. s->cursor_attr = (SCREEN_ATTR << 8);
  106. screen_damage_cursor(s);
  107. return;
  108. }
  109. if (c == '\b') {
  110. if (s->cursor > 0) {
  111. screen_damage_cursor(s);
  112. s->cursor--;
  113. s->cells[s->cursor] = (unsigned short)((SCREEN_ATTR << 8) | ' ');
  114. screen_damage_cursor(s);
  115. }
  116. return;
  117. }
  118. if (s->cursor < total) {
  119. screen_damage_cursor(s); /* the written cell + the cursor */
  120. s->cells[s->cursor] = (unsigned short)(s->cursor_attr | (unsigned char)c);
  121. s->cursor++;
  122. s->cursor_attr = (SCREEN_ATTR << 8);
  123. screen_damage_cursor(s); /* the cursor moved on */
  124. }
  125. }
  126. void screen_putstr(struct text_screen *s, const char *str)
  127. {
  128. while (*str)
  129. screen_putchar(s, *str++);
  130. }
  131. void screen_puthex(struct text_screen *s, unsigned int n)
  132. {
  133. static const char hex[] = "0123456789ABCDEF";
  134. char buf[9];
  135. unsigned int i = 8;
  136. buf[8] = '\0';
  137. do {
  138. buf[--i] = hex[n & 0xF];
  139. n >>= 4;
  140. } while (i > 0);
  141. screen_putstr(s, buf);
  142. }
  143. unsigned int screen_get_cursor(const struct text_screen *s)
  144. {
  145. return s->cursor;
  146. }
  147. void screen_set_cursor(struct text_screen *s, unsigned int pos)
  148. {
  149. if (pos >= s->cols * s->rows)
  150. return;
  151. screen_damage_cursor(s); /* erase the old cursor cell */
  152. s->cursor = pos;
  153. s->cursor_attr = (SCREEN_ATTR << 8);
  154. screen_damage_cursor(s);
  155. }
  156. void screen_put_at(struct text_screen *s, unsigned int pos, unsigned short value)
  157. {
  158. if (pos < s->cols * s->rows) {
  159. screen_damage_cell(s, pos % s->cols, pos / s->cols);
  160. s->cells[pos] = value;
  161. }
  162. }
  163. int screen_did_scroll(struct text_screen *s)
  164. {
  165. if (!s->scrolled)
  166. return 0;
  167. s->scrolled = 0;
  168. return 1;
  169. }