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

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