Added VFS_IOCtl, Implemented input 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                                 pos += WriteUTF8(Buffer+pos, term->InputBuffer[term->InputRead]);
244                                 term->InputRead ++;
245                         }
246                 }
247                 break;
248         
249         case VT_MODE_TEXT32:
250                 while(pos < Length)
251                 {
252                         while(term->InputRead == term->InputWrite)      Proc_Yield();
253                         while(term->InputRead != term->InputWrite)
254                         {
255                                 ((Uint32*)Buffer)[pos] = term->InputBuffer[term->InputRead];
256                                 pos ++;
257                                 term->InputRead ++;
258                         }
259                 }
260                 break;
261         }
262         return 0;
263 }
264
265 /**
266  * \fn Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
267  * \brief Write to a virtual terminal
268  */
269 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
270 {
271         tVTerm  *term = &gVT_Terminals[ Node->Inode ];
272         
273         //ENTER("pNode XOffset XLength pBuffer",  Node, Offset, Length, Buffer);
274         
275         // Write
276         switch( term->Mode )
277         {
278         case VT_MODE_TEXT8:
279                 VT_int_PutString(term, Buffer, Length);
280                 break;
281         case VT_MODE_TEXT32:
282                 //VT_int_PutString32(term, Buffer, Length);
283                 break;
284         }
285         //LEAVE('i', 0);
286         return 0;
287 }
288
289 /**
290  * \fn int VT_IOCtl(tVFS_Node *Node, int Id, void *Data)
291  * \brief Call an IO Control on a virtual terminal
292  */
293 int VT_IOCtl(tVFS_Node *Node, int Id, void *Data)
294 {
295         return 0;
296 }
297
298 /**
299  * \fn void VT_KBCallBack(Uint32 Codepoint)
300  * \brief Called on keyboard interrupt
301  */
302 void VT_KBCallBack(Uint32 Codepoint)
303 {
304         tVTerm  *term = &gVT_Terminals[giVT_CurrentTerminal];
305         
306         term->InputBuffer[ term->InputWrite ] = Codepoint;
307         term->InputWrite ++;
308         term->InputWrite %= MAX_INPUT_CHARS;
309         if(term->InputRead == term->InputWrite) {
310                 term->InputRead ++;
311                 term->InputRead %= MAX_INPUT_CHARS;
312         }
313 }
314
315 /**
316  * \fn void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
317  * \brief Print a string to the Virtual Terminal
318  */
319 void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
320 {
321         Uint32  val;
322          int    i;
323         for( i = 0; i < Count; i++ )
324         {
325                 if( Buffer[i] == 0x1B ) // Escape Sequence
326                 {
327                         i += VT_int_ParseEscape(Term, (char*)&Buffer[i]);
328                         continue;
329                 }
330                 
331                 if( Buffer[i] < 128 )   // Plain ASCII
332                         VT_int_PutChar(Term, Buffer[i]);
333                 else {  // UTF-8
334                         i += ReadUTF8(&Buffer[i], &val);
335                         VT_int_PutChar(Term, val);
336                 }
337         }
338 }
339
340 /**
341  * \fn int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
342  * \brief Parses a VT100 Escape code
343  */
344 int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
345 {
346         char    c;
347          int    argc = 0, j = 1;
348          int    args[4] = {0,0,0,0};
349         
350         switch(Buffer[0]) {
351         //Large Code
352         case '[':
353                 // Get Arguments
354                 c = Buffer[1];
355                 do {
356                         while('0' <= c && c <= '9') {
357                                 args[argc] *= 10;
358                                 args[argc] += c-'0';
359                                 c = Buffer[++j];
360                         }
361                         argc ++;
362                 } while(c == ';');
363                 
364                 // Get string (what does this do?)
365                 if(c == '"') {
366                         c = Buffer[++j];
367                         while(c != '"')
368                                 c = Buffer[++j];
369                 }
370                 
371                 // Get Command
372                 if(     ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
373                 {
374                         switch(c) {
375                         //Clear By Line
376                         case 'J':
377                                 // Clear Screen
378                                 if(args[0] == 2) {
379                                         memset(Term->Text, 0, Term->Width*Term->Height*VT_SCROLLBACK*sizeof(tVT_Char));
380                                         Term->WritePos = 0;
381                                         Term->ViewPos = 0;
382                                 }
383                                 break;
384                         // Set Font flags
385                         case 'm':
386                                 for( ; argc--; )
387                                 {
388                                         // Flags
389                                         if( 0 <= args[argc] && args[argc] <= 8)
390                                         {
391                                                 switch(args[argc])
392                                                 {
393                                                 case 0: Term->CurColour = DEFAULT_COLOUR;       break;  // Reset
394                                                 case 1: Term->CurColour |= 0x80000000;  break;  // Bright
395                                                 case 2: Term->CurColour &= ~0x80000000; break;  // Dim
396                                                 }
397                                         }
398                                         // Foreground Colour
399                                         else if(30 <= args[argc] && args[argc] <= 37) {
400                                                 Term->CurColour &= 0xF000FFFF;
401                                                 Term->CurColour |= (Uint32)caVT100Colours[ args[argc]-30+(Term->CurColour>>28) ] << 16;
402                                         }
403                                         // Background Colour
404                                         else if(40 <= args[argc] && args[argc] <= 47) {
405                                                 Term->CurColour &= 0xFFFF8000;
406                                                 Term->CurColour |= caVT100Colours[ args[argc]-40+((Term->CurColour>>12)&15) ];
407                                         }
408                                 }
409                                 break;
410                         }
411                 }
412                 break;
413                 
414         default:
415                 break;
416         }
417         
418         return j + 1;
419 }
420
421 /**
422  * \fn void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
423  * \brief Write a single character to a VTerm
424  */
425 void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
426 {
427          int    i;
428         //ENTER("pTerm xCh", Term, Ch);
429         //LOG("Term = {WritePos:%i, ViewPos:%i}\n", Term->WritePos, Term->ViewPos);
430         
431         switch(Ch)
432         {
433         case '\n':
434                 Term->WritePos += Term->Width;
435         case '\r':
436                 Term->WritePos -= Term->WritePos % Term->Width;
437                 break;
438         
439         case '\t':
440                 do {
441                         Term->Text[ Term->WritePos ].Ch = '\t';
442                         Term->Text[ Term->WritePos ].Colour = Term->CurColour;
443                         Term->WritePos ++;
444                 } while(Term->WritePos & 7);
445                 break;
446         
447         case '\b':
448                 // Backspace is invalid at Offset 0
449                 if(Term->WritePos == 0) break;
450                 
451                 Term->WritePos --;
452                 // Singe Character
453                 if(Term->Text[ Term->WritePos ].Ch != '\t') {
454                         Term->Text[ Term->WritePos ].Ch = 0;
455                         Term->Text[ Term->WritePos ].Colour = Term->CurColour;
456                         break;
457                 }
458                 // Tab
459                 i = 7;  // Limit it to 8
460                 do {
461                         Term->Text[ Term->WritePos ].Ch = 0;
462                         Term->Text[ Term->WritePos ].Colour = Term->CurColour;
463                         Term->WritePos --;
464                 } while(Term->WritePos && i-- && Term->Text[ Term->WritePos ].Ch == '\t');
465                 break;
466         
467         default:
468                 Term->Text[ Term->WritePos ].Ch = Ch;
469                 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
470                 Term->WritePos ++;
471                 break;
472         }
473         
474         if(Term->WritePos >= Term->Width*Term->Height*VT_SCROLLBACK)
475         {
476                  int    base, i;
477                 Term->WritePos -= Term->Width;
478                 
479                 // Update view position
480                 base = Term->Width*Term->Height*(VT_SCROLLBACK-1);
481                 if(Term->ViewPos < base)        Term->ViewPos += Term->Width;
482                 if(Term->ViewPos > base)        Term->ViewPos = base;
483                 
484                 // Scroll terminal cache
485                 base = Term->Width*(Term->Height*VT_SCROLLBACK-1);
486                 
487                 // Scroll Back
488                 memcpy( Term->Text, &Term->Text[Term->Width], base*sizeof(tVT_Char) );
489                 
490                 // Clear last row
491                 for( i = 0; i < Term->Width; i ++ )
492                 {
493                         Term->Text[ base + i ].Ch = 0;
494                         Term->Text[ base + i ].Colour = Term->CurColour;
495                 }
496                 
497                 VT_int_UpdateScreen( Term, 1 );
498         }
499         else
500                 VT_int_UpdateScreen( Term, 0 );
501         
502         //LEAVE('-');
503 }
504
505 /**
506  * \fn void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
507  * \brief Updates the video framebuffer
508  */
509 void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
510 {
511         if(UpdateAll) {
512                 VFS_WriteAt(
513                         giVT_OutputDevHandle,
514                         0,
515                         Term->Width*Term->Height*sizeof(tVT_Char),
516                         &Term->Text[Term->ViewPos]
517                         );
518         } else {
519                  int    pos = Term->WritePos - Term->WritePos % Term->Width;
520                 VFS_WriteAt(
521                         giVT_OutputDevHandle,
522                         (pos - Term->ViewPos)*sizeof(tVT_Char),
523                         Term->Width*sizeof(tVT_Char),
524                         &Term->Text[pos]
525                         );
526         }
527 }
528
529 // ---
530 // Font Render
531 // ---
532 #define MONOSPACE_FONT  10816
533
534 #if MONOSPACE_FONT == 10808     // 8x8
535 # include "vterm_font_8x8.h"
536 #elif MONOSPACE_FONT == 10816   // 8x16
537 # include "vterm_font_8x16.h"
538 #endif
539
540
541 int VT_Font_GetWidth(Uint32 Codepoint)
542 {
543         return FONT_WIDTH;
544 }
545 int VT_Font_GetHeight(Uint32 Codepoint)
546 {
547         return FONT_HEIGHT;
548 }
549
550 void VT_Font_Render(Uint32 Codepoint, void *Buffer, Uint32 BGC, Uint32 FGC)
551 {
552 //      Uint8   *font;
553         
554 }
555
556 /**
557  * \fn Uint8 *VT_Font_GetChar(Uint32 Codepoint)
558  * \brief Gets an index into the font array given a Unicode Codepoint
559  * \note See http://en.wikipedia.org/wiki/CP437
560  */
561 Uint8 *VT_Font_GetChar(Uint32 Codepoint)
562 {
563          int    index = 0;
564         if(Codepoint < 128)
565                 return &VTermFont[Codepoint*FONT_HEIGHT];
566         switch(Codepoint)
567         {
568         case 0xC7:      index = 128;    break;  // Ç
569         case 0xFC:      index = 129;    break;  // ü
570         case 0xE9:      index = 130;    break;  // é
571         case 0xE2:      index = 131;    break;  // â
572         case 0xE4:      index = 132;    break;  // ä
573         case 0xE0:      index = 133;    break;  // à
574         case 0xE5:      index = 134;    break;  // å
575         case 0xE7:      index = 135;    break;  // ç
576         case 0xEA:      index = 136;    break;  // ê
577         case 0xEB:      index = 137;    break;  // ë
578         case 0xE8:      index = 138;    break;  // è
579         case 0xEF:      index = 139;    break;  // ï
580         case 0xEE:      index = 140;    break;  // î
581         case 0xEC:      index = 141;    break;  // ì
582         case 0xC4:      index = 142;    break;  // Ä
583         case 0xC5:      index = 143;    break;  // Å
584         }
585         
586         return &VTermFont[index*FONT_HEIGHT];
587 }

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