2 * Acess2 Virtual Terminal Driver
7 #include <tpl_drv_video.h>
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))
32 int ViewPos, WritePos;
36 Uint8 InputBuffer[MAX_INPUT_BYTES];
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 );
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
63 MODULE_DEFINE(0, 0x0032, VTerm, VT_Install, NULL, NULL);
64 tDevFS_Driver gVT_DrvInfo = {
67 .Flags = VFS_FFLAG_DIRECTORY,
70 .ReadDir = VT_ReadDir,
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;
83 * \fn int VT_Install(char **Arguments)
85 int VT_Install(char **Arguments)
87 char **args = Arguments;
94 for( ; (arg = *args); args++ )
96 if(arg[0] != '-') continue;
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]);
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]);
123 if(!gsVT_OutputDevice) gsVT_OutputDevice = DEFAULT_OUTPUT;
124 if(!gsVT_InputDevice) gsVT_InputDevice = DEFAULT_INPUT;
126 LOG("Using '%s' as output", gsVT_OutputDevice);
127 LOG("Using '%s' as input", gsVT_InputDevice);
130 for( i = 0; i < NUM_VTS; i++ )
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;
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) );
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
147 gVT_Terminals[i].Node.Read = VT_Read;
148 gVT_Terminals[i].Node.Write = VT_Write;
149 gVT_Terminals[i].Node.IOCtl = VT_IOCtl;
153 DevFS_AddDevice( &gVT_DrvInfo );
159 * \fn void VT_InitOutput()
160 * \brief Initialise Video Output
164 giVT_OutputDevHandle = VFS_Open(gsVT_OutputDevice, VFS_OPENFLAG_WRITE);
165 LOG("giVT_OutputDevHandle = %x\n", giVT_OutputDevHandle);
169 * \fn void VT_InitInput()
170 * \brief Initialises the input
174 giVT_InputDevHandle = VFS_Open(gsVT_InputDevice, VFS_OPENFLAG_READ);
175 LOG("giVT_InputDevHandle = %x\n", giVT_InputDevHandle);
179 * \fn char *VT_ReadDir(tVFS_Node *Node, int Pos)
180 * \brief Read from the VTerm Directory
182 char *VT_ReadDir(tVFS_Node *Node, int Pos)
184 if(Pos < 0) return NULL;
185 if(Pos >= NUM_VTS) return NULL;
186 return gVT_Terminals[Pos].Name;
190 * \fn tVFS_Node *VT_FindDir(tVFS_Node *Node, char *Name)
191 * \brief Find an item in the VTerm directory
193 tVFS_Node *VT_FindDir(tVFS_Node *Node, char *Name)
197 //ENTER("pNode sName", Node, Name);
199 // Open the input and output files if needed
200 if(giVT_OutputDevHandle == -2) VT_InitOutput();
201 if(giVT_InputDevHandle == -2) VT_InitInput();
204 if(Name[0] < '0' || Name[0] > '9' || Name[1] != '\0') {
215 //LEAVE('p', &gVT_Terminals[num].Node);
216 return &gVT_Terminals[num].Node;
220 * \fn Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
221 * \brief Read from a virtual terminal
223 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
225 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
231 * \fn Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
232 * \brief Write to a virtual terminal
234 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
236 tVTerm *term = &gVT_Terminals[ Node->Inode ];
238 //ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
244 VT_int_PutString(term, Buffer, Length);
252 * \fn int VT_IOCtl(tVFS_Node *Node, int Id, void *Data)
253 * \brief Call an IO Control on a virtual terminal
255 int VT_IOCtl(tVFS_Node *Node, int Id, void *Data)
261 * \fn void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
262 * \brief Print a string to the Virtual Terminal
264 void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
268 for( i = 0; i < Count; i++ )
270 if( Buffer[i] == 0x1B ) // Escape Sequence
272 i += VT_int_ParseEscape(Term, (char*)&Buffer[i]);
276 if( Buffer[i] < 128 ) // Plain ASCII
277 VT_int_PutChar(Term, Buffer[i]);
279 i += ReadUTF8(&Buffer[i], &val);
280 VT_int_PutChar(Term, val);
286 * \fn int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
287 * \brief Parses a VT100 Escape code
289 int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
293 int args[4] = {0,0,0,0};
301 while('0' <= c && c <= '9') {
309 // Get string (what does this do?)
317 if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
324 memset(Term->Text, 0, Term->Width*Term->Height*VT_SCROLLBACK*sizeof(tVT_Char));
334 if( 0 <= args[argc] && args[argc] <= 8)
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
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;
349 else if(40 <= args[argc] && args[argc] <= 47) {
350 Term->CurColour &= 0xFFFF8000;
351 Term->CurColour |= caVT100Colours[ args[argc]-40+((Term->CurColour>>12)&15) ];
367 * \fn void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
368 * \brief Write a single character to a VTerm
370 void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
373 //ENTER("pTerm xCh", Term, Ch);
374 //LOG("Term = {WritePos:%i, ViewPos:%i}\n", Term->WritePos, Term->ViewPos);
379 Term->WritePos += Term->Width;
381 Term->WritePos -= Term->WritePos % Term->Width;
386 Term->Text[ Term->WritePos ].Ch = '\t';
387 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
389 } while(Term->WritePos & 7);
393 // Backspace is invalid at Offset 0
394 if(Term->WritePos == 0) break;
398 if(Term->Text[ Term->WritePos ].Ch != '\t') {
399 Term->Text[ Term->WritePos ].Ch = 0;
400 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
404 i = 7; // Limit it to 8
406 Term->Text[ Term->WritePos ].Ch = 0;
407 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
409 } while(Term->WritePos && i-- && Term->Text[ Term->WritePos ].Ch == '\t');
413 Term->Text[ Term->WritePos ].Ch = Ch;
414 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
419 if(Term->WritePos >= Term->Width*Term->Height*VT_SCROLLBACK)
422 Term->WritePos -= Term->Width;
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;
429 // Scroll terminal cache
430 base = Term->Width*(Term->Height*VT_SCROLLBACK-1);
433 memcpy( Term->Text, &Term->Text[Term->Width], base*sizeof(tVT_Char) );
436 for( i = 0; i < Term->Width; i ++ )
438 Term->Text[ base + i ].Ch = 0;
439 Term->Text[ base + i ].Colour = Term->CurColour;
442 VT_int_UpdateScreen( Term, 1 );
445 VT_int_UpdateScreen( Term, 0 );
451 * \fn void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
452 * \brief Updates the video framebuffer
454 void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
458 giVT_OutputDevHandle,
460 Term->Width*Term->Height*sizeof(tVT_Char),
461 &Term->Text[Term->ViewPos]
465 giVT_OutputDevHandle,
466 Term->ViewPos*sizeof(tVT_Char),
467 Term->Width*sizeof(tVT_Char),
468 &Term->Text[Term->ViewPos]
476 #define MONOSPACE_FONT 10816
478 #if MONOSPACE_FONT == 10808 // 8x8
479 # include "vterm_font_8x8.h"
480 #elif MONOSPACE_FONT == 10816 // 8x16
481 # include "vterm_font_8x16.h"
485 int VT_Font_GetWidth(Uint32 Codepoint)
489 int VT_Font_GetHeight(Uint32 Codepoint)
494 void VT_Font_Render(Uint32 Codepoint, void *Buffer, Uint32 BGC, Uint32 FGC)
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
505 Uint8 *VT_Font_GetChar(Uint32 Codepoint)
509 return &VTermFont[Codepoint*FONT_HEIGHT];
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; // Å
530 return &VTermFont[index*FONT_HEIGHT];