screen.c 2.4 KB

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