Fixed bug in libc.so/brk(), fixed support for VT100 escape codes
[tpg/acess2.git] / Kernel / drv / vterm.c
1 /*
2  * Acess2 Virtual Terminal Driver
3  */
4 #include <common.h>
5 #include <fs_devfs.h>
6 #include <modules.h>
7 #include <tpl_drv_video.h>
8 #include <tpl_drv_keyboard.h>
9
10 // === CONSTANTS ===
11 #define NUM_VTS 4
12 #define MAX_INPUT_CHARS 64
13 #define VT_SCROLLBACK   4       // 4 Screens of text
14 #define DEFAULT_OUTPUT  "/Devices/VGA"
15 #define DEFAULT_INPUT   "/Devices/PS2Keyboard"
16 #define DEFAULT_WIDTH   80
17 #define DEFAULT_HEIGHT  25
18 #define DEFAULT_COLOUR  (VT_COL_BLACK|(VT_COL_WHITE<<16))
19
20 #define VT_FLAG_HIDECSR 0x01
21
22 enum eVT_Modes {
23         VT_MODE_TEXT8,  // UTF-8 Text Mode (VT100 Emulation)
24         VT_MODE_TEXT32, // UTF-32 Text Mode (Acess Native)
25         VT_MODE_8BPP,   // 256 Colour Mode
26         VT_MODE_16BPP,  // 16 bit Colour Mode
27         VT_MODE_24BPP,  // 24 bit Colour Mode
28         VT_MODE_32BPP,  // 32 bit Colour Mode
29         NUM_VT_MODES
30 };
31
32 // === TYPES ===
33 typedef struct {
34          int    Mode;
35          int    Flags;
36          int    Width, Height;
37          int    ViewPos, WritePos;
38         Uint32  CurColour;
39         char    Name[2];
40          int    InputRead;
41          int    InputWrite;
42         Uint32  InputBuffer[MAX_INPUT_CHARS];
43         union {
44                 tVT_Char        *Text;
45                 Uint32          *Buffer;
46         };
47         tVFS_Node       Node;
48 } tVTerm;
49
50 // === PROTOTYPES ===
51  int    VT_Install(char **Arguments);
52 char    *VT_ReadDir(tVFS_Node *Node, int Pos);
53 tVFS_Node       *VT_FindDir(tVFS_Node *Node, char *Name);
54 Uint64  VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
55 Uint64  VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
56  int    VT_IOCtl(tVFS_Node *Node, int Id, void *Data);
57 void    VT_KBCallBack(Uint32 Codepoint);
58 void    VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count);
59  int    VT_int_ParseEscape(tVTerm *Term, char *Buffer);
60 void    VT_int_PutChar(tVTerm *Term, Uint32 Ch);
61 void    VT_int_UpdateScreen( tVTerm *Term, int UpdateAll );
62
63 // === CONSTANTS ===
64 const Uint16    caVT100Colours[] = {
65                 VT_COL_BLACK, 0, 0, 0, 0, 0, 0, VT_COL_LTGREY,
66                 VT_COL_GREY, 0, 0, 0, 0, 0, 0, VT_COL_WHITE
67         };
68
69 // === GLOBALS ===
70 MODULE_DEFINE(0, 0x0032, VTerm, VT_Install, NULL, NULL);
71 tDevFS_Driver   gVT_DrvInfo = {
72         NULL, "VTerm",
73         {
74         .Flags = VFS_FFLAG_DIRECTORY,
75         .Inode = -1,
76         .NumACLs = 0,
77         .ReadDir = VT_ReadDir,
78         .FindDir = VT_FindDir
79         }
80 };
81 tVTerm  gVT_Terminals[NUM_VTS];
82 char    *gsVT_OutputDevice = NULL;
83 char    *gsVT_InputDevice = NULL;
84  int    giVT_OutputDevHandle = -2;
85  int    giVT_InputDevHandle = -2;
86  int    giVT_CurrentTerminal = 0;
87
88 // === CODE ===
89 /**
90  * \fn int VT_Install(char **Arguments)
91  */
92 int VT_Install(char **Arguments)
93 {
94         char    **args = Arguments;
95         char    *arg;
96          int    i;
97         
98         // Scan Arguments
99         if(Arguments)
100         {
101                 for( ; (arg = *args); args++ )
102                 {
103                         if(arg[0] != '-')       continue;
104                         
105                         switch(arg[1])
106                         {
107                         // Set output device
108                         case 'o':
109                                 if(args[1] ==  NULL)    break;
110                                 if(gsVT_OutputDevice)   free(gsVT_OutputDevice);
111                                 gsVT_OutputDevice = malloc(strlen(args[1])+1);
112                                 strcpy(gsVT_OutputDevice, args[1]);
113                                 args ++;
114                                 break;
115                         
116                         // Set input device
117                         case 'i':
118                                 if(args[1] == NULL)     break;
119                                 if(gsVT_InputDevice)    free(gsVT_InputDevice);
120                                 gsVT_InputDevice = malloc(strlen(args[1])+1);
121                                 strcpy(gsVT_InputDevice, args[1]);
122                                 args ++;
123                                 break;
124                         
125                         }
126                 }
127         }
128         
129         // Apply Defaults
130         if(!gsVT_OutputDevice)  gsVT_OutputDevice = DEFAULT_OUTPUT;
131         if(!gsVT_InputDevice)   gsVT_InputDevice = DEFAULT_INPUT;
132         
133         LOG("Using '%s' as output", gsVT_OutputDevice);
134         LOG("Using '%s' as input", gsVT_InputDevice);
135         
136         // Create Nodes
137         for( i = 0; i < NUM_VTS; i++ )
138         {
139                 gVT_Terminals[i].Mode = VT_MODE_TEXT8;
140                 gVT_Terminals[i].Flags = 0;
141                 gVT_Terminals[i].Width = DEFAULT_WIDTH;
142                 gVT_Terminals[i].Height = DEFAULT_HEIGHT;
143                 gVT_Terminals[i].CurColour = DEFAULT_COLOUR;
144                 gVT_Terminals[i].WritePos = 0;
145                 gVT_Terminals[i].ViewPos = 0;
146                 
147                 gVT_Terminals[i].Buffer = malloc( DEFAULT_WIDTH*DEFAULT_HEIGHT*sizeof(tVT_Char) );
148                 memset( gVT_Terminals[i].Buffer, 0, DEFAULT_WIDTH*DEFAULT_HEIGHT*sizeof(tVT_Char) );
149                 
150                 gVT_Terminals[i].Name[0] = '0'+i;
151                 gVT_Terminals[i].Name[1] = '\0';
152                 gVT_Terminals[i].Node.Inode = i;
153                 gVT_Terminals[i].Node.NumACLs = 0;      // Only root can open virtual terminals
154                 
155                 gVT_Terminals[i].Node.Read = VT_Read;
156                 gVT_Terminals[i].Node.Write = VT_Write;
157                 gVT_Terminals[i].Node.IOCtl = VT_IOCtl;
158         }
159         
160         // Add to DevFS
161         DevFS_AddDevice( &gVT_DrvInfo );
162         
163         return 0;
164 }
165
166 /**
167  * \fn void VT_InitOutput()
168  * \brief Initialise Video Output
169  */
170 void VT_InitOutput()
171 {
172         giVT_OutputDevHandle = VFS_Open(gsVT_OutputDevice, VFS_OPENFLAG_WRITE);
173         LOG("giVT_OutputDevHandle = %x\n", giVT_OutputDevHandle);
174 }
175
176 /**
177  * \fn void VT_InitInput()
178  * \brief Initialises the input
179  */
180 void VT_InitInput()
181 {
182         giVT_InputDevHandle = VFS_Open(gsVT_InputDevice, VFS_OPENFLAG_READ);
183         LOG("giVT_InputDevHandle = %x\n", giVT_InputDevHandle);
184         if(giVT_InputDevHandle == -1)   return ;
185         VFS_IOCtl(giVT_InputDevHandle, KB_IOCTL_SETCALLBACK, VT_KBCallBack);
186 }
187
188 /**
189  * \fn char *VT_ReadDir(tVFS_Node *Node, int Pos)
190  * \brief Read from the VTerm Directory
191  */
192 char *VT_ReadDir(tVFS_Node *Node, int Pos)
193 {
194         if(Pos < 0)     return NULL;
195         if(Pos >= NUM_VTS)      return NULL;
196         return gVT_Terminals[Pos].Name;
197 }
198
199 /**
200  * \fn tVFS_Node *VT_FindDir(tVFS_Node *Node, char *Name)
201  * \brief Find an item in the VTerm directory
202  */
203 tVFS_Node *VT_FindDir(tVFS_Node *Node, char *Name)
204 {
205          int    num;
206         
207         //ENTER("pNode sName", Node, Name);
208         
209         // Open the input and output files if needed
210         if(giVT_OutputDevHandle == -2)  VT_InitOutput();
211         if(giVT_InputDevHandle == -2)   VT_InitInput();
212         
213         // Sanity check name
214         if(Name[0] < '0' || Name[0] > '9' || Name[1] != '\0') {
215                 LEAVE('n');
216                 return NULL;
217         }
218         // Get index
219         num = Name[0] - '0';
220         if(num >= NUM_VTS) {
221                 LEAVE('n');
222                 return NULL;
223         }
224         // Return node
225         //LEAVE('p', &gVT_Terminals[num].Node);
226         return &gVT_Terminals[num].Node;
227 }
228
229 /**
230  * \fn Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
231  * \brief Read from a virtual terminal
232  */
233 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
234 {
235          int    pos = 0;
236         tVTerm  *term = &gVT_Terminals[ Node->Inode ];
237         
238         // Check current mode
239         switch(term->Mode)
240         {
241         case VT_MODE_TEXT8:
242                 while(pos < Length)
243                 {
244                         while(term->InputRead == term->InputWrite)      Proc_Yield();
245                         while(term->InputRead != term->InputWrite)
246                         {
247                                 LOG("WriteUTF8(%p, 0x%x)", Buffer+pos, term->InputBuffer[term->InputRead]);
248                                 pos += WriteUTF8(Buffer+pos, term->InputBuffer[term->InputRead]);
249                                 term->InputRead ++;
250                                 term->InputRead %= MAX_INPUT_CHARS;
251                         }
252                 }
253                 break;
254         
255         case VT_MODE_TEXT32:
256                 while(pos < Length)
257                 {
258                         while(term->InputRead == term->InputWrite)      Proc_Yield();
259                         while(term->InputRead != term->InputWrite)
260                         {
261                                 ((Uint32*)Buffer)[pos] = term->InputBuffer[term->InputRead];
262                                 pos ++;
263                                 term->InputRead ++;
264                                 term->InputRead %= MAX_INPUT_CHARS;
265                         }
266                 }
267                 break;
268         }
269         return 0;
270 }
271
272 /**
273  * \fn Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
274  * \brief Write to a virtual terminal
275  */
276 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
277 {
278         tVTerm  *term = &gVT_Terminals[ Node->Inode ];
279         
280         //ENTER("pNode XOffset XLength pBuffer",  Node, Offset, Length, Buffer);
281         
282         // Write
283         switch( term->Mode )
284         {
285         case VT_MODE_TEXT8:
286                 VT_int_PutString(term, Buffer, Length);
287                 break;
288         case VT_MODE_TEXT32:
289                 //VT_int_PutString32(term, Buffer, Length);
290                 break;
291         }
292         
293         //LEAVE('i', 0);
294         return 0;
295 }
296
297 /**
298  * \fn int VT_IOCtl(tVFS_Node *Node, int Id, void *Data)
299  * \brief Call an IO Control on a virtual terminal
300  */
301 int VT_IOCtl(tVFS_Node *Node, int Id, void *Data)
302 {
303         return 0;
304 }
305
306 /**
307  * \fn void VT_KBCallBack(Uint32 Codepoint)
308  * \brief Called on keyboard interrupt
309  */
310 void VT_KBCallBack(Uint32 Codepoint)
311 {
312         tVTerm  *term = &gVT_Terminals[giVT_CurrentTerminal];
313         
314         term->InputBuffer[ term->InputWrite ] = Codepoint;
315         term->InputWrite ++;
316         term->InputWrite %= MAX_INPUT_CHARS;
317         if(term->InputRead == term->InputWrite) {
318                 term->InputRead ++;
319                 term->InputRead %= MAX_INPUT_CHARS;
320         }
321 }
322
323 /**
324  * \fn void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
325  * \brief Print a string to the Virtual Terminal
326  */
327 void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
328 {
329         Uint32  val;
330          int    i;
331         for( i = 0; i < Count; i++ )
332         {
333                 if( Buffer[i] == 0x1B ) // Escape Sequence
334                 {
335                         i ++;
336                         i += VT_int_ParseEscape(Term, (char*)&Buffer[i]);
337                         continue;
338                 }
339                 
340                 if( Buffer[i] < 128 )   // Plain ASCII
341                         VT_int_PutChar(Term, Buffer[i]);
342                 else {  // UTF-8
343                         i += ReadUTF8(&Buffer[i], &val);
344                         VT_int_PutChar(Term, val);
345                 }
346         }
347         
348         // Update cursor
349         if( !(Term->Flags & VT_FLAG_HIDECSR) )
350         {
351                 tVideo_IOCtl_Pos        pos;
352                 pos.x = Term->WritePos % Term->Width;
353                 pos.y = Term->WritePos / Term->Width;
354                 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
355         }
356 }
357
358 /**
359  * \fn int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
360  * \brief Parses a VT100 Escape code
361  */
362 int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
363 {
364         char    c;
365          int    argc = 0, j = 1;
366          int    args[4] = {0,0,0,0};
367         
368         switch(Buffer[0]) {
369         //Large Code
370         case '[':
371                 // Get Arguments
372                 c = Buffer[1];
373                 do {
374                         while('0' <= c && c <= '9') {
375                                 args[argc] *= 10;
376                                 args[argc] += c-'0';
377                                 c = Buffer[++j];
378                         }
379                         argc ++;
380                 } while(c == ';');
381                 
382                 // Get string (what does this do?)
383                 if(c == '"') {
384                         c = Buffer[++j];
385                         while(c != '"')
386                                 c = Buffer[++j];
387                 }
388                 
389                 // Get Command
390                 if(     ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
391                 {
392                         switch(c) {
393                         //Clear By Line
394                         case 'J':
395                                 // Clear Screen
396                                 if(args[0] == 2) {
397                                         memset(Term->Text, 0, Term->Width*Term->Height*VT_SCROLLBACK*sizeof(tVT_Char));
398                                         Term->WritePos = 0;
399                                         Term->ViewPos = 0;
400                                 }
401                                 break;
402                         // Set Font flags
403                         case 'm':
404                                 for( ; argc--; )
405                                 {
406                                         // Flags
407                                         if( 0 <= args[argc] && args[argc] <= 8)
408                                         {
409                                                 switch(args[argc])
410                                                 {
411                                                 case 0: Term->CurColour = DEFAULT_COLOUR;       break;  // Reset
412                                                 case 1: Term->CurColour |= 0x80000000;  break;  // Bright
413                                                 case 2: Term->CurColour &= ~0x80000000; break;  // Dim
414                                                 }
415                                         }
416                                         // Foreground Colour
417                                         else if(30 <= args[argc] && args[argc] <= 37) {
418                                                 Term->CurColour &= 0xF000FFFF;
419                                                 Term->CurColour |= (Uint32)caVT100Colours[ args[argc]-30+(Term->CurColour>>28) ] << 16;
420                                         }
421                                         // Background Colour
422                                         else if(40 <= args[argc] && args[argc] <= 47) {
423                                                 Term->CurColour &= 0xFFFF8000;
424                                                 Term->CurColour |= caVT100Colours[ args[argc]-40+((Term->CurColour>>12)&15) ];
425                                         }
426                                 }
427                                 break;
428                         }
429                 }
430                 break;
431                 
432         default:
433                 break;
434         }
435         
436         return j + 1;
437 }
438
439 /**
440  * \fn void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
441  * \brief Write a single character to a VTerm
442  */
443 void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
444 {
445          int    i;
446         //ENTER("pTerm xCh", Term, Ch);
447         //LOG("Term = {WritePos:%i, ViewPos:%i}\n", Term->WritePos, Term->ViewPos);
448         
449         switch(Ch)
450         {
451         case 0: return; // Ignore NULL byte
452         case '\n':
453                 Term->WritePos += Term->Width;
454         case '\r':
455                 Term->WritePos -= Term->WritePos % Term->Width;
456                 break;
457         
458         case '\t':
459                 do {
460                         Term->Text[ Term->WritePos ].Ch = '\t';
461                         Term->Text[ Term->WritePos ].Colour = Term->CurColour;
462                         Term->WritePos ++;
463                 } while(Term->WritePos & 7);
464                 break;
465         
466         case '\b':
467                 // Backspace is invalid at Offset 0
468                 if(Term->WritePos == 0) break;
469                 
470                 Term->WritePos --;
471                 // Singe Character
472                 if(Term->Text[ Term->WritePos ].Ch != '\t') {
473                         Term->Text[ Term->WritePos ].Ch = 0;
474                         Term->Text[ Term->WritePos ].Colour = Term->CurColour;
475                         break;
476                 }
477                 // Tab
478                 i = 7;  // Limit it to 8
479                 do {
480                         Term->Text[ Term->WritePos ].Ch = 0;
481                         Term->Text[ Term->WritePos ].Colour = Term->CurColour;
482                         Term->WritePos --;
483                 } while(Term->WritePos && i-- && Term->Text[ Term->WritePos ].Ch == '\t');
484                 break;
485         
486         default:
487                 Term->Text[ Term->WritePos ].Ch = Ch;
488                 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
489                 Term->WritePos ++;
490                 break;
491         }
492         
493         if(Term->WritePos >= Term->Width*Term->Height*VT_SCROLLBACK)
494         {
495                  int    base, i;
496                 Term->WritePos -= Term->Width;
497                 
498                 // Update view position
499                 base = Term->Width*Term->Height*(VT_SCROLLBACK-1);
500                 if(Term->ViewPos < base)        Term->ViewPos += Term->Width;
501                 if(Term->ViewPos > base)        Term->ViewPos = base;
502                 
503                 // Scroll terminal cache
504                 base = Term->Width*(Term->Height*VT_SCROLLBACK-1);
505                 
506                 // Scroll Back
507                 memcpy( Term->Text, &Term->Text[Term->Width], base*sizeof(tVT_Char) );
508                 
509                 // Clear last row
510                 for( i = 0; i < Term->Width; i ++ )
511                 {
512                         Term->Text[ base + i ].Ch = 0;
513                         Term->Text[ base + i ].Colour = Term->CurColour;
514                 }
515                 
516                 VT_int_UpdateScreen( Term, 1 );
517         }
518         else
519                 VT_int_UpdateScreen( Term, 0 );
520         
521         //LEAVE('-');
522 }
523
524 /**
525  * \fn void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
526  * \brief Updates the video framebuffer
527  */
528 void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
529 {
530         if(UpdateAll) {
531                 VFS_WriteAt(
532                         giVT_OutputDevHandle,
533                         0,
534                         Term->Width*Term->Height*sizeof(tVT_Char),
535                         &Term->Text[Term->ViewPos]
536                         );
537         } else {
538                  int    pos = Term->WritePos - Term->WritePos % Term->Width;
539                 VFS_WriteAt(
540                         giVT_OutputDevHandle,
541                         (pos - Term->ViewPos)*sizeof(tVT_Char),
542                         Term->Width*sizeof(tVT_Char),
543                         &Term->Text[pos]
544                         );
545         }
546 }
547
548 // ---
549 // Font Render
550 // ---
551 #define MONOSPACE_FONT  10816
552
553 #if MONOSPACE_FONT == 10808     // 8x8
554 # include "vterm_font_8x8.h"
555 #elif MONOSPACE_FONT == 10816   // 8x16
556 # include "vterm_font_8x16.h"
557 #endif
558
559
560 int VT_Font_GetWidth(Uint32 Codepoint)
561 {
562         return FONT_WIDTH;
563 }
564 int VT_Font_GetHeight(Uint32 Codepoint)
565 {
566         return FONT_HEIGHT;
567 }
568
569 void VT_Font_Render(Uint32 Codepoint, void *Buffer, Uint32 BGC, Uint32 FGC)
570 {
571 //      Uint8   *font;
572         
573 }
574
575 /**
576  * \fn Uint8 *VT_Font_GetChar(Uint32 Codepoint)
577  * \brief Gets an index into the font array given a Unicode Codepoint
578  * \note See http://en.wikipedia.org/wiki/CP437
579  */
580 Uint8 *VT_Font_GetChar(Uint32 Codepoint)
581 {
582          int    index = 0;
583         if(Codepoint < 128)
584                 return &VTermFont[Codepoint*FONT_HEIGHT];
585         switch(Codepoint)
586         {
587         case 0xC7:      index = 128;    break;  // Ç
588         case 0xFC:      index = 129;    break;  // ü
589         case 0xE9:      index = 130;    break;  // é
590         case 0xE2:      index = 131;    break;  // â
591         case 0xE4:      index = 132;    break;  // ä
592         case 0xE0:      index = 133;    break;  // à
593         case 0xE5:      index = 134;    break;  // å
594         case 0xE7:      index = 135;    break;  // ç
595         case 0xEA:      index = 136;    break;  // ê
596         case 0xEB:      index = 137;    break;  // ë
597         case 0xE8:      index = 138;    break;  // è
598         case 0xEF:      index = 139;    break;  // ï
599         case 0xEE:      index = 140;    break;  // î
600         case 0xEC:      index = 141;    break;  // ì
601         case 0xC4:      index = 142;    break;  // Ä
602         case 0xC5:      index = 143;    break;  // Å
603         }
604         
605         return &VTermFont[index*FONT_HEIGHT];
606 }

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