99#define INSERT 3
1010#define CAPSLOCK 4
1111
12+ //terminal modes:
13+ #define TERMINAL 0
14+ #define EDITOR 1
15+ #define INTERPRETER 2
16+
1217void delay (int );
1318void print (int , int , char * );
1419void printChar (int , int , char , int );
@@ -26,7 +31,11 @@ void clearScreen(int);
2631void scrollUp ();
2732void cursorBackwards ();
2833void cursorForwards ();
34+
2935void keyPressed (unsigned char );
36+ void terminal_keyPressed (unsigned char , char );
37+ void editor_keyPressed (unsigned char , char );
38+ void interpreter_keyPressed (unsigned char , char );
3039void pitCall ();
3140void rtcCall ();
3241void printStatus (unsigned char );
@@ -47,6 +56,10 @@ int commandLength;
4756char counter ;
4857char prevCommand [20 ];
4958
59+ int terminalMode ;
60+
61+ char fileBuffer [height * width ];
62+
5063#include "driver.c"
5164#include "util.c"
5265#include "IDT.c"
@@ -70,6 +83,7 @@ void main(){
7083 }
7184 modifier [INSERT ] = 1 ;
7285 modifier [CAPSLOCK ] = 0 ;
86+ terminalMode = TERMINAL ;
7387 prompt = '$' ;
7488 promptColor = 0x02 ;
7589 clearScreen (0x0F );
@@ -98,6 +112,128 @@ void main(){
98112//down: E0 50 E0 D0
99113void keyPressed (unsigned char code ){
100114 char c = scancodeToAscii (code , modifier [SHIFT ], modifier [CTRL ], modifier [CAPSLOCK ]);
115+ if (terminalMode == TERMINAL ){
116+ terminal_keyPressed (code , c );
117+ } else if (terminalMode == EDITOR ){
118+ editor_keyPressed (code , c );
119+ } else if (terminalMode == INTERPRETER ){
120+ interpreter_keyPressed (code , c );
121+ }
122+ }
123+
124+ void editor_keyPressed (unsigned char code , char c ){
125+ char printable = 1 ;
126+ if (code == 0x0E ){ // backspace
127+ if (ttx == 0 && tty == 0 ) return ;
128+ cursorBackwards ();
129+ int i = 0 ;
130+ while (getChar (ttx + i , tty ) != 0 ){
131+ printChar (ttx + i ,tty ,getChar (ttx + i + 1 ,tty ),0x0F );
132+ i ++ ;
133+ }
134+ printable = 0 ;
135+ } else if (code == 0x4B ){ // left arrow
136+ ttx -- ;
137+ if (ttx == -1 ){
138+ ttx = 0 ;
139+ tty -- ;
140+ if (tty == -1 ) tty = 0 ;
141+ }
142+ setCursor (ttx , tty );
143+ printable = 0 ;
144+ } else if (code == 0x4D ){ // right arrow
145+ ttx ++ ;
146+ if (ttx == width ){
147+ ttx = width - 1 ;
148+ tty ++ ;
149+ if (tty == height ) tty = height - 1 ;
150+ }
151+ setCursor (ttx , tty );
152+ printable = 0 ;
153+ } else if (code == 0x48 ){ // up arrow
154+ tty -- ;
155+ if (tty == -1 ) tty = 0 ;
156+ setCursor (ttx , tty );
157+ printable = 0 ;
158+ } else if (code == 0x50 ){ // down arrow
159+ tty ++ ;
160+ if (tty == height ) tty = height - 1 ;
161+ setCursor (ttx , tty );
162+ printable = 0 ;
163+ } else if (code == 0xCB ){ // left arrow release
164+ printable = 0 ;
165+ } else if (code == 0xCD ){ // right arrow release
166+ printable = 0 ;
167+ } else if (code == 0xC8 ){ // up arrow release
168+ printable = 0 ;
169+ } else if (code == 0xCB ){ // down arrow release
170+ printable = 0 ;
171+ } else if (code == 0x1D ){ // control on
172+ modifier [CTRL ] = 1 ;
173+ printable = 0 ;
174+ } else if (code == 0x9D ){ //control off
175+ modifier [CTRL ] = 0 ;
176+ printable = 0 ;
177+ } else if (code == 0x36 || code == 0x2A ){ //shift on
178+ modifier [SHIFT ] = 1 ;
179+ printable = 0 ;
180+ } else if (code == 0xB6 || code == 0xAA ){ //shift off
181+ modifier [SHIFT ] = 0 ;
182+ printable = 0 ;
183+ } else if (code == 0x38 ){ // alt on
184+ modifier [ALT ] = 1 ;
185+ printable = 0 ;
186+ } else if (code == 0xB8 ){ // alt off
187+ modifier [ALT ] = 0 ;
188+ printable = 0 ;
189+ } else if (code == 0x52 ){ // insert
190+ modifier [INSERT ]++ ;
191+ modifier [INSERT ] %= 2 ;
192+ printable = 0 ;
193+ } else if (code == 0x3A ){ // capslock
194+ modifier [CAPSLOCK ]++ ;
195+ modifier [CAPSLOCK ] %= 2 ;
196+ printable = 0 ;
197+ }
198+ printStatus (code );
199+
200+
201+ if (modifier [CTRL ] && c == 's' ){
202+ int x = 0 ;
203+ int y = 0 ;
204+ for (y = 0 ; y < height ; y ++ ){
205+ for (x = 0 ; x < width ; x ++ ){
206+ fileBuffer [y * width + x ] = getChar (x ,y );
207+ }
208+ }
209+ printable = 0 ;
210+ clearScreen (0x00 );
211+ terminalMode = TERMINAL ;
212+ ttx = 0 ; tty = 0 ;
213+ setCursor (ttx ,tty );
214+ printPrompt ();
215+ printStatus (0x00 );
216+ return ;
217+ }
218+
219+ if (c == 0 ) return ;
220+ if (modifier [INSERT ] && printable == 1 ){
221+ int i = 0 ;
222+ while (getChar (ttx + i , tty ) != 0 ){
223+ i ++ ;
224+ }
225+ while (i > 0 ){
226+ printChar (ttx + i ,tty ,getChar (ttx + i - 1 ,tty ),0x0F );
227+ i -- ;
228+ }
229+ }
230+ ttprintChar (c );
231+
232+
233+ }
234+ void interpreter_keyPressed (unsigned char code , char c ){}
235+
236+ void terminal_keyPressed (unsigned char code , char c ){
101237 if (code == 0x0E ){ // backspace
102238 if (getChar (ttx - 1 ,tty ) == prompt ) return ;
103239 cursorBackwards ();
@@ -164,8 +300,10 @@ void keyPressed(unsigned char code){
164300 commandLength = strLen (command );
165301 memCopy (command ,prevCommand ,commandLength );
166302 sh_handler (command );
167- ttprintChar (c );
168- printPrompt ();
303+ if (terminalMode == TERMINAL ){
304+ ttprintChar (c );
305+ printPrompt ();
306+ }
169307 }
170308}
171309void printPrompt (){
@@ -193,37 +331,55 @@ void printStatus(unsigned char code){
193331 printChar (i ,absolute_height ,' ' ,0x0F );
194332 }
195333 int statusX = 0 ;
196- print (statusX , absolute_height , "KeyCode:" );
197- statusX += 8 ;
198334 char str [10 ];
335+ print (statusX , absolute_height , "Code:" );
336+ statusX += 5 ;
199337 charToString (code ,str );
200338 if (code != -1 ) print (statusX , absolute_height , str );
201339 statusX += 4 ;
202- print (statusX , absolute_height , " Insert:" );
203- statusX += 8 ;
340+
341+ print (statusX , absolute_height , " Ins:" );
342+ statusX += 5 ;
204343 printChar (statusX , absolute_height , modifier [INSERT ]+ '0' , 0x0F );
205344 statusX += 1 ;
345+
206346 print (statusX , absolute_height , " Shift:" );
207347 statusX += 7 ;
208348 printChar (statusX , absolute_height , modifier [SHIFT ]+ '0' , 0x0F );
209349 statusX += 1 ;
350+
210351 print (statusX , absolute_height , " Caps:" );
211352 statusX += 6 ;
212353 printChar (statusX , absolute_height , modifier [CAPSLOCK ]+ '0' , 0x0F );
213354 statusX += 1 ;
355+
214356 print (statusX , absolute_height , " Ctrl:" );
215357 statusX += 6 ;
216358 printChar (statusX , absolute_height , modifier [CTRL ]+ '0' , 0x0F );
217359 statusX += 1 ;
360+
218361 print (statusX , absolute_height , " Alt:" );
219362 statusX += 5 ;
220363 printChar (statusX , absolute_height , modifier [ALT ]+ '0' , 0x0F );
221364 statusX += 1 ;
222- print (statusX , absolute_height , " Last Entry:" );
223- statusX += 12 ;
365+
366+ print (statusX , absolute_height , " Last:" );
367+ statusX += 6 ;
224368 intToString (commandLength ,str );
225369 print (statusX , absolute_height , str );
226- statusX += strLen (str - 1 );
370+ statusX += strLen (str )- 1 ;
371+
372+ print (statusX , absolute_height , " Mode:" );
373+ statusX += 6 ;
374+ printChar (statusX , absolute_height , terminalMode + '0' , 0x0F );
375+ statusX += 1 ;
376+
377+ print (statusX , absolute_height , " ttx:" );
378+ statusX += 5 ;
379+ intToString (ttx , str );
380+ print (statusX , absolute_height , str );
381+ statusX += strLen (str )- 1 ;
382+
227383 print (statusX , absolute_height , " Time:" );
228384 statusX += 6 ;
229385 intToString (ticks /PITfreq ,str );
@@ -299,6 +455,7 @@ void cursorBackwards(){
299455 setCursor (ttx ,tty );
300456 }
301457}
458+
302459void scrollUp (){
303460 int j ;
304461 for (j = 0 ; j < height - 1 ; j ++ ){
@@ -335,6 +492,8 @@ void setCursor(int x, int y){
335492 writeByteToPort (REG_SCREEN_DATA , high );
336493 writeByteToPort (REG_SCREEN_CTRL , 15 );
337494 writeByteToPort (REG_SCREEN_DATA , low );
495+ ttx = x ;
496+ tty = y ;
338497}
339498int offset (int x , int y ){
340499 return 2 * (y * width + x );
0 commit comments