screen.c 4.4 KB

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