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

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