foo
[eightysix] / kern / src / con.c
1 #if 0
2 #include "con.h"
3
4
5
6 #define NCOLS                   80
7 #define NROWS                   25
8
9 /* must be pow2 */
10 #define TEXTBUF_SIZE    256
11
12 static char textbuf[TEXTBUF_SIZE][NCOLS];
13 static int tbuf_first, tbuf_last;
14
15 static int curx, cury, scroll;
16
17 #define CURSTACK_SIZE   16
18 static unsigned int savecur[CURSTACK_SIZE];
19 static int savecur_top;
20
21
22 static void newline(void);
23
24
25 void con_init(void)
26 {
27         con_reset();
28 }
29
30 void con_reset(void)
31 {
32         vga_reset();
33
34         curx = cury = scroll = 0;
35         tbuf_first = tbuf_last = 0;
36         memset(textbuf[0], 0, sizeof textbuf[0]);
37
38         savecur_top = 0;
39 }
40
41 void con_setcur(int x, int y)
42 {
43         curx = x < 0 ? 0 : (x >= NCOLS ? NCOLS - 1 : x);
44         cury = y < 0 ? 0 : (y >= NROWS ? NROWS - 1 : y);
45 }
46
47 void con_getcur(int *x, int *y)
48 {
49         *x = curx;
50         *y = cury;
51 }
52
53 void con_pushcur(void)
54 {
55         if(savecur_top >= CURSTACK_SIZE) return;
56
57         savecur[savecur_top++] = curx | (cury << 16);
58 }
59
60 void con_popcur(void)
61 {
62         if(savecur_top <= 0) return;
63
64         savecur_top--;
65         curx = savecur[savecur_top] & 0xffff;
66         cury = savecur[savecur_top] >> 16;
67 }
68
69 void con_putchar(int c)
70 {
71         switch(c) {
72         case '\t':
73                 curx = (curx + 8) & 0xfffffff8;
74                 if(curx >= NCOLS) {
75                         newline();
76                 }
77                 break;
78
79         case '\r':
80                 curx = 0;
81                 break;
82
83         case '\n':
84                 newline();
85                 break;
86
87         case '\b':
88                 if(curx > 0) {
89                         textbuf[tbuf_last][--curx] = 0;
90                         vga_drawchar(curx, cury, 0);
91                 }
92                 break;
93
94         default:
95                 textbuf[tbuf_last][curx] = c;
96                 vga_drawchar(curx, cury, c);
97                 if(++curx >= NCOLS) {
98                         newline();
99                 }
100                 break;
101         }
102
103         if(cury >= NROWS) {
104                 cury--;
105                 vga_scroll(++scroll);
106                 vga_clearline(NROWS - 1);
107         }
108
109         vid_set_cursor(curx, cury);
110 }
111
112 static void newline(void)
113 {
114         int num;
115
116         curx = 0;
117         cury++;
118
119         num = (tbuf_last + 1) & (TEXTBUF_SIZE - 1);
120
121         if(tbuf_last == tbuf_first) {
122                 tbuf_first = num;
123         }
124         tbuf_last = num;
125 }
126 #endif