screen.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. void screen_init(struct text_screen *s)
  6. {
  7. s->cols = SCREEN_COLS;
  8. s->rows = SCREEN_ROWS;
  9. s->cursor = 0;
  10. s->cursor_attr = (SCREEN_ATTR << 8);
  11. s->cursor_visible = 0;
  12. s->dirty = 1;
  13. s->scrolled = 0;
  14. screen_clear(s);
  15. }
  16. void screen_clear(struct text_screen *s)
  17. {
  18. unsigned int i, n = s->cols * s->rows;
  19. for (i = 0; i < n; i++)
  20. s->cells[i] = (unsigned short)((SCREEN_ATTR << 8) | ' ');
  21. s->cursor = 0;
  22. s->dirty = 1;
  23. s->scrolled = 0;
  24. }
  25. static void screen_scroll_up(struct text_screen *s)
  26. {
  27. unsigned int ncols = s->cols;
  28. unsigned int total = s->cols * s->rows;
  29. unsigned int i;
  30. for (i = 0; i < total - ncols; i++)
  31. s->cells[i] = s->cells[i + ncols];
  32. for (; i < total; i++)
  33. s->cells[i] = (unsigned short)((SCREEN_ATTR << 8) | ' ');
  34. s->dirty = 1;
  35. }
  36. void screen_putchar(struct text_screen *s, char c)
  37. {
  38. unsigned int ncols = s->cols;
  39. unsigned int total = s->cols * s->rows;
  40. netlog_char(c); /* terminal text also goes to the netlog mirror */
  41. if (c == '\n') {
  42. s->cursor = (s->cursor / ncols + 1) * ncols;
  43. if (s->cursor >= total) {
  44. screen_scroll_up(s);
  45. s->cursor = total - ncols;
  46. s->scrolled = 1;
  47. }
  48. s->cursor_attr = (SCREEN_ATTR << 8);
  49. s->dirty = 1;
  50. return;
  51. }
  52. if (c == '\b') {
  53. if (s->cursor > 0) {
  54. s->cursor--;
  55. s->cells[s->cursor] = (unsigned short)((SCREEN_ATTR << 8) | ' ');
  56. s->dirty = 1;
  57. }
  58. return;
  59. }
  60. if (s->cursor < total) {
  61. s->cells[s->cursor] = (unsigned short)(s->cursor_attr | (unsigned char)c);
  62. s->cursor++;
  63. s->cursor_attr = (SCREEN_ATTR << 8);
  64. s->dirty = 1;
  65. }
  66. }
  67. void screen_putstr(struct text_screen *s, const char *str)
  68. {
  69. while (*str)
  70. screen_putchar(s, *str++);
  71. }
  72. void screen_puthex(struct text_screen *s, unsigned int n)
  73. {
  74. static const char hex[] = "0123456789ABCDEF";
  75. char buf[9];
  76. unsigned int i = 8;
  77. buf[8] = '\0';
  78. do {
  79. buf[--i] = hex[n & 0xF];
  80. n >>= 4;
  81. } while (i > 0);
  82. screen_putstr(s, buf);
  83. }
  84. unsigned int screen_get_cursor(const struct text_screen *s)
  85. {
  86. return s->cursor;
  87. }
  88. void screen_set_cursor(struct text_screen *s, unsigned int pos)
  89. {
  90. if (pos >= s->cols * s->rows)
  91. return;
  92. s->cursor = pos;
  93. s->cursor_attr = (SCREEN_ATTR << 8);
  94. }
  95. void screen_put_at(struct text_screen *s, unsigned int pos, unsigned short value)
  96. {
  97. if (pos < s->cols * s->rows) {
  98. s->cells[pos] = value;
  99. s->dirty = 1;
  100. }
  101. }
  102. int screen_did_scroll(struct text_screen *s)
  103. {
  104. if (!s->scrolled)
  105. return 0;
  106. s->scrolled = 0;
  107. return 1;
  108. }