Kernel/VTerm - Code cleanup and VT100 bugfixes
[tpg/acess2.git] / KernelLand / Kernel / drv / vterm_vt100.c
1 /*
2  * Acess2 Kernel
3  * - By John Hodge (thePowersGang)
4  *
5  * drv/vterm_vt100.c
6  * - Virtual Terminal - VT100 (Kinda) Emulation
7  */
8 #define DEBUG   0
9 #include "vterm.h"
10
11 #define sTerminal       sVTerm
12 #include "../../../Usermode/Applications/gui_terminal_src/vt100.c"
13
14 void *Display_GetTermState(tTerminal *Term) {
15         return Term->VT100Info;
16 }
17 void Display_SetTermState(tTerminal *Term, void *State) {
18         Term->VT100Info = State;
19 }
20
21 void Display_SendInput(tTerminal *Term, const char *String)
22 {
23         PTY_SendInput(Term->PTY, String, strlen(String));
24 }
25
26 void Display_AddText(tTerminal *Term, size_t Length, const char *UTF8Text)
27 {
28         LOG("'%*C'", Length, UTF8Text);
29         VT_int_PutRawString(Term, (const void*)UTF8Text, Length);
30 }
31 void Display_Newline(tTerminal *Term, bool bCarriageReturn)
32 {
33         LOG("");
34         VT_int_PutChar(Term, '\n');
35 }
36 void Display_SetScrollArea(tTerminal *Term, int Start, int Count)
37 {
38         LOG("(%i,+%i)", Start, Count);
39         Term->ScrollTop = Start;
40         Term->ScrollHeight = Count;
41 }
42 void Display_ScrollDown(tTerminal *Term, int CountDown)
43 {
44         LOG("(%i)", CountDown);
45         VT_int_UpdateScreen(Term, 0);
46         if( Term->Flags & VT_FLAG_ALTBUF )
47                 VT_int_ScrollText(Term, CountDown);
48         else
49         {
50                 if(Term->ViewPos/Term->TextWidth + CountDown < 0)
51                         return ;
52                 if(Term->ViewPos/Term->TextWidth + CountDown  > Term->TextHeight * (giVT_Scrollback + 1))
53                         return ;
54                 
55                 Term->ViewPos += Term->TextWidth * CountDown;
56         }
57 }
58 void Display_SetCursor(tTerminal *Term, int Row, int Col)
59 {
60         LOG("(R%i,C%i)", Row, Col);
61         VT_int_UpdateScreen(Term, 0);
62          int    maxrows = VT_int_GetBufferRows(Term);
63         ASSERTCR( Row, >=, 0, );
64         ASSERTCR( Row, <, maxrows, );
65         ASSERTCR( Col, >=, 0, );
66         ASSERTCR( Col, <, Term->TextWidth, );
67         *VT_int_GetWritePosPtr(Term) = Row*Term->TextWidth + Col;
68 }
69 void Display_MoveCursor(tTerminal *Term, int RelRow, int RelCol)
70 {
71         LOG("(R+%i,C+%i)", RelRow, RelCol);
72         size_t  *wrpos = VT_int_GetWritePosPtr(Term);
73
74         // TODO: Support scrolling if cursor goes offscreen
75         // if( bScrollIfNeeded )
76         //      Display_ScrollDown(extra);
77         // else
78         //      clip
79
80         if( RelCol != 0 )
81         {
82                 // 
83                 if( RelCol < 0 )
84                 {
85                          int    max = *wrpos % Term->TextWidth;
86                         if( RelCol < -max )
87                                 RelCol = -max;
88                 }
89                 else
90                 {
91                         size_t  max = Term->TextWidth - (*wrpos % Term->TextWidth) - 1;
92                         if(RelCol > max)
93                                 RelCol = max;
94                 }
95                 *wrpos += RelCol;
96         }
97         if( RelRow != 0 )
98         {
99                  int    currow = *wrpos / Term->TextWidth;
100                  int    maxrows = VT_int_GetBufferRows(Term);
101                 if( RelRow < 0 )
102                 {
103                         if( RelRow < -currow )
104                                 RelRow = -currow;
105                 }
106                 else
107                 {
108                         if( currow + RelRow > maxrows-1 )
109                                 RelRow = maxrows-1 - currow;
110                 }
111                 *wrpos += RelRow*Term->TextWidth;
112         }
113         LOG("=(R%i,C%i)", *wrpos / Term->TextWidth, *wrpos % Term->TextWidth);
114 }
115 void Display_SaveCursor(tTerminal *Term)
116 {
117         Term->SavedWritePos = *VT_int_GetWritePosPtr(Term);
118         LOG("Saved = %i", Term->SavedWritePos);
119 }
120 void Display_RestoreCursor(tTerminal *Term)
121 {
122         size_t  max = VT_int_GetBufferRows(Term) * Term->TextWidth;
123         size_t  *wrpos = VT_int_GetWritePosPtr(Term);
124         *wrpos = MIN(Term->SavedWritePos, max-1);
125         LOG("Restored %i", *wrpos);
126 }
127 // 0: All, 1: Forward, -1: Reverse
128 void Display_ClearLine(tTerminal *Term, int Dir)
129 {
130         const size_t    wrpos = *VT_int_GetWritePosPtr(Term);
131         const int       row = wrpos / Term->TextWidth;
132         const int       col = wrpos % Term->TextWidth;
133
134         LOG("(Dir=%i)", Dir);
135
136         // Erase all
137         if( Dir == 0 ) {
138                 VT_int_ClearLine(Term, row);
139                 VT_int_UpdateScreen(Term, 0);
140         }
141         // Erase to right
142         else if( Dir == 1 ) {
143                 VT_int_ClearInLine(Term, row, col, Term->TextWidth);
144                 VT_int_UpdateScreen(Term, 0);
145         }
146         // Erase to left
147         else if( Dir == -1 ) {
148                 VT_int_ClearInLine(Term, row, 0, col);
149                 VT_int_UpdateScreen(Term, 0);
150         }
151         else {
152                 // ERROR!
153                 ASSERTC(Dir, >=, -1);
154                 ASSERTC(Dir, <=, 1);
155         }
156 }
157 void Display_ClearLines(tTerminal *Term, int Dir)
158 {
159         LOG("(Dir=%i)", Dir);   
160         size_t  *wrpos = VT_int_GetWritePosPtr(Term);
161         size_t  height = VT_int_GetBufferRows(Term);
162         
163         // All
164         if( Dir == 0 ) {
165                 
166                 if( !(Term->Flags & VT_FLAG_ALTBUF) ) {
167                         Term->ViewPos = 0;
168                 }
169                 int count = height;
170                 while( count -- )
171                         VT_int_ClearLine(Term, count);
172                 *wrpos = 0;
173                 VT_int_UpdateScreen(Term, 1);
174         }
175         // Downwards
176         else if( Dir == 1 ) {
177                 for( int row = *wrpos / Term->TextWidth; row < height; row ++ )
178                         VT_int_ClearLine(Term, row);
179                 VT_int_UpdateScreen(Term, 1);
180         }
181         // Upwards
182         else if( Dir == -1 ) {
183                 for( int row = 0; row < *wrpos / Term->TextWidth; row ++ )
184                         VT_int_ClearLine(Term, row);
185                 VT_int_UpdateScreen(Term, 1);
186         }
187         else {
188                 // ERROR!
189                 ASSERTC(Dir, >=, -1);
190                 ASSERTC(Dir, <=, 1);
191         }
192 }
193 void Display_ResetAttributes(tTerminal *Term)
194 {
195         LOG("");        
196         Term->CurColour = DEFAULT_COLOUR;
197 }
198 void Display_SetForeground(tTerminal *Term, uint32_t RGB)
199 {
200         LOG("(%06x)", RGB);
201         Term->CurColour &= 0x8000FFFF;
202         Term->CurColour |= (Uint32)VT_Colour24to12(RGB) << 16;
203         
204 }
205 void Display_SetBackground(tTerminal *Term, uint32_t RGB)
206 {
207         LOG("(%06x)", RGB);
208         Term->CurColour &= 0xFFFF8000;
209         Term->CurColour |= (Uint32)VT_Colour24to12(RGB) <<  0;
210 }
211 void Display_Flush(tTerminal *Term)
212 {
213         LOG("");
214         VT_int_UpdateScreen(Term, 0);
215 }
216 void Display_ShowAltBuffer(tTerminal *Term, bool AltBufEnabled)
217 {
218         LOG("(%B)", AltBufEnabled);
219         VT_int_ToggleAltBuffer(Term, AltBufEnabled);
220 }
221 void Display_SetTitle(tTerminal *Term, const char *Title)
222 {
223         // ignore
224 }
225

UCC git Repository :: git.ucc.asn.au