2 * Acess2 Virtual Terminal Driver
7 #include <tpl_drv_video.h>
8 #include <tpl_drv_keyboard.h>
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))
20 #define VT_FLAG_HIDECSR 0x01
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
37 int ViewPos, WritePos;
42 Uint32 InputBuffer[MAX_INPUT_CHARS];
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 );
64 const Uint16 caVT100Colours[] = {
65 VT_COL_BLACK, 0x700, 0x070, 0x770, 0x007, 0x707, 0x077, VT_COL_LTGREY,
66 VT_COL_GREY, 0xF00, 0x0F0, 0xFF0, 0x00F, 0xF0F, 0x0FF, VT_COL_WHITE
70 MODULE_DEFINE(0, 0x0032, VTerm, VT_Install, NULL, NULL);
71 tDevFS_Driver gVT_DrvInfo = {
74 .Flags = VFS_FFLAG_DIRECTORY,
77 .ReadDir = VT_ReadDir,
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;
90 * \fn int VT_Install(char **Arguments)
91 * \brief Installs the Virtual Terminal Driver
93 int VT_Install(char **Arguments)
95 char **args = Arguments;
102 for( ; (arg = *args); args++ )
104 if(arg[0] != '-') continue;
110 if(args[1] == NULL) break;
111 if(gsVT_OutputDevice) free(gsVT_OutputDevice);
112 gsVT_OutputDevice = malloc(strlen(args[1])+1);
113 strcpy(gsVT_OutputDevice, args[1]);
119 if(args[1] == NULL) break;
120 if(gsVT_InputDevice) free(gsVT_InputDevice);
121 gsVT_InputDevice = malloc(strlen(args[1])+1);
122 strcpy(gsVT_InputDevice, args[1]);
131 if(!gsVT_OutputDevice) gsVT_OutputDevice = DEFAULT_OUTPUT;
132 if(!gsVT_InputDevice) gsVT_InputDevice = DEFAULT_INPUT;
134 LOG("Using '%s' as output", gsVT_OutputDevice);
135 LOG("Using '%s' as input", gsVT_InputDevice);
138 for( i = 0; i < NUM_VTS; i++ )
140 gVT_Terminals[i].Mode = VT_MODE_TEXT8;
141 gVT_Terminals[i].Flags = 0;
142 gVT_Terminals[i].Width = DEFAULT_WIDTH;
143 gVT_Terminals[i].Height = DEFAULT_HEIGHT;
144 gVT_Terminals[i].CurColour = DEFAULT_COLOUR;
145 gVT_Terminals[i].WritePos = 0;
146 gVT_Terminals[i].ViewPos = 0;
148 gVT_Terminals[i].Buffer = malloc( DEFAULT_WIDTH*DEFAULT_HEIGHT*sizeof(tVT_Char) );
149 memset( gVT_Terminals[i].Buffer, 0, DEFAULT_WIDTH*DEFAULT_HEIGHT*sizeof(tVT_Char) );
151 gVT_Terminals[i].Name[0] = '0'+i;
152 gVT_Terminals[i].Name[1] = '\0';
153 gVT_Terminals[i].Node.Inode = i;
154 gVT_Terminals[i].Node.NumACLs = 0; // Only root can open virtual terminals
156 gVT_Terminals[i].Node.Read = VT_Read;
157 gVT_Terminals[i].Node.Write = VT_Write;
158 gVT_Terminals[i].Node.IOCtl = VT_IOCtl;
162 DevFS_AddDevice( &gVT_DrvInfo );
168 * \fn void VT_InitOutput()
169 * \brief Initialise Video Output
173 giVT_OutputDevHandle = VFS_Open(gsVT_OutputDevice, VFS_OPENFLAG_WRITE);
174 LOG("giVT_OutputDevHandle = %x\n", giVT_OutputDevHandle);
178 * \fn void VT_InitInput()
179 * \brief Initialises the input
183 giVT_InputDevHandle = VFS_Open(gsVT_InputDevice, VFS_OPENFLAG_READ);
184 LOG("giVT_InputDevHandle = %x\n", giVT_InputDevHandle);
185 if(giVT_InputDevHandle == -1) return ;
186 VFS_IOCtl(giVT_InputDevHandle, KB_IOCTL_SETCALLBACK, VT_KBCallBack);
190 * \fn char *VT_ReadDir(tVFS_Node *Node, int Pos)
191 * \brief Read from the VTerm Directory
193 char *VT_ReadDir(tVFS_Node *Node, int Pos)
195 if(Pos < 0) return NULL;
196 if(Pos >= NUM_VTS) return NULL;
197 return gVT_Terminals[Pos].Name;
201 * \fn tVFS_Node *VT_FindDir(tVFS_Node *Node, char *Name)
202 * \brief Find an item in the VTerm directory
204 tVFS_Node *VT_FindDir(tVFS_Node *Node, char *Name)
208 //ENTER("pNode sName", Node, Name);
210 // Open the input and output files if needed
211 if(giVT_OutputDevHandle == -2) VT_InitOutput();
212 if(giVT_InputDevHandle == -2) VT_InitInput();
215 if(Name[0] < '0' || Name[0] > '9' || Name[1] != '\0') {
226 //LEAVE('p', &gVT_Terminals[num].Node);
227 return &gVT_Terminals[num].Node;
231 * \fn Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
232 * \brief Read from a virtual terminal
234 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
237 tVTerm *term = &gVT_Terminals[ Node->Inode ];
239 // Check current mode
245 while(term->InputRead == term->InputWrite) Proc_Yield();
246 while(term->InputRead != term->InputWrite)
248 LOG("WriteUTF8(%p, 0x%x)", Buffer+pos, term->InputBuffer[term->InputRead]);
249 pos += WriteUTF8(Buffer+pos, term->InputBuffer[term->InputRead]);
251 term->InputRead %= MAX_INPUT_CHARS;
259 while(term->InputRead == term->InputWrite) Proc_Yield();
260 while(term->InputRead != term->InputWrite)
262 ((Uint32*)Buffer)[pos] = term->InputBuffer[term->InputRead];
265 term->InputRead %= MAX_INPUT_CHARS;
274 * \fn Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
275 * \brief Write to a virtual terminal
277 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
279 tVTerm *term = &gVT_Terminals[ Node->Inode ];
281 //ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
287 VT_int_PutString(term, Buffer, Length);
290 //VT_int_PutString32(term, Buffer, Length);
299 * \fn int VT_IOCtl(tVFS_Node *Node, int Id, void *Data)
300 * \brief Call an IO Control on a virtual terminal
302 int VT_IOCtl(tVFS_Node *Node, int Id, void *Data)
308 * \fn void VT_KBCallBack(Uint32 Codepoint)
309 * \brief Called on keyboard interrupt
311 void VT_KBCallBack(Uint32 Codepoint)
313 tVTerm *term = &gVT_Terminals[giVT_CurrentTerminal];
315 term->InputBuffer[ term->InputWrite ] = Codepoint;
317 term->InputWrite %= MAX_INPUT_CHARS;
318 if(term->InputRead == term->InputWrite) {
320 term->InputRead %= MAX_INPUT_CHARS;
325 * \fn void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
326 * \brief Print a string to the Virtual Terminal
328 void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
332 for( i = 0; i < Count; i++ )
334 if( Buffer[i] == 0x1B ) // Escape Sequence
337 i += VT_int_ParseEscape(Term, (char*)&Buffer[i]);
341 if( Buffer[i] < 128 ) // Plain ASCII
342 VT_int_PutChar(Term, Buffer[i]);
344 i += ReadUTF8(&Buffer[i], &val);
345 VT_int_PutChar(Term, val);
350 if( !(Term->Flags & VT_FLAG_HIDECSR) )
352 tVideo_IOCtl_Pos pos;
353 pos.x = Term->WritePos % Term->Width;
354 pos.y = Term->WritePos / Term->Width;
355 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
360 * \fn int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
361 * \brief Parses a VT100 Escape code
363 int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
367 int args[4] = {0,0,0,0};
375 while('0' <= c && c <= '9') {
383 // Get string (what does this do?)
391 if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
398 memset(Term->Text, 0, Term->Width*Term->Height*VT_SCROLLBACK*sizeof(tVT_Char));
401 VT_int_UpdateScreen(Term, 1);
409 if( 0 <= args[argc] && args[argc] <= 8)
413 case 0: Term->CurColour = DEFAULT_COLOUR; break; // Reset
414 case 1: Term->CurColour |= 0x80000000; break; // Bright
415 case 2: Term->CurColour &= ~0x80000000; break; // Dim
419 else if(30 <= args[argc] && args[argc] <= 37) {
420 Term->CurColour &= 0xF000FFFF;
421 Term->CurColour |= (Uint32)caVT100Colours[ args[argc]-30+(Term->CurColour>>28) ] << 16;
424 else if(40 <= args[argc] && args[argc] <= 47) {
425 Term->CurColour &= 0xFFFF8000;
426 Term->CurColour |= caVT100Colours[ args[argc]-40+((Term->CurColour>>12)&15) ];
442 * \fn void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
443 * \brief Write a single character to a VTerm
445 void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
448 //ENTER("pTerm xCh", Term, Ch);
449 //LOG("Term = {WritePos:%i, ViewPos:%i}\n", Term->WritePos, Term->ViewPos);
453 case 0: return; // Ignore NULL byte
455 Term->WritePos += Term->Width;
457 Term->WritePos -= Term->WritePos % Term->Width;
462 Term->Text[ Term->WritePos ].Ch = '\t';
463 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
465 } while(Term->WritePos & 7);
469 // Backspace is invalid at Offset 0
470 if(Term->WritePos == 0) break;
474 if(Term->Text[ Term->WritePos ].Ch != '\t') {
475 Term->Text[ Term->WritePos ].Ch = 0;
476 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
480 i = 7; // Limit it to 8
482 Term->Text[ Term->WritePos ].Ch = 0;
483 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
485 } while(Term->WritePos && i-- && Term->Text[ Term->WritePos ].Ch == '\t');
489 Term->Text[ Term->WritePos ].Ch = Ch;
490 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
497 if(Term->WritePos >= Term->Width*Term->Height*VT_SCROLLBACK)
500 Term->WritePos -= Term->Width;
502 // Update view position
503 base = Term->Width*Term->Height*(VT_SCROLLBACK-1);
504 if(Term->ViewPos < base) Term->ViewPos += Term->Width;
505 if(Term->ViewPos > base) Term->ViewPos = base;
507 // Scroll terminal cache
508 base = Term->Width*(Term->Height*VT_SCROLLBACK-1);
511 memcpy( Term->Text, &Term->Text[Term->Width], base*sizeof(tVT_Char) );
514 for( i = 0; i < Term->Width; i ++ )
516 Term->Text[ base + i ].Ch = 0;
517 Term->Text[ base + i ].Colour = Term->CurColour;
520 VT_int_UpdateScreen( Term, 1 );
522 else if(Term->WritePos > Term->Width*Term->Height+Term->ViewPos)
524 Term->ViewPos += Term->Width;
525 VT_int_UpdateScreen( Term, 1 );
528 VT_int_UpdateScreen( Term, 0 );
534 * \fn void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
535 * \brief Updates the video framebuffer
537 void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
541 giVT_OutputDevHandle,
543 Term->Width*Term->Height*sizeof(tVT_Char),
544 &Term->Text[Term->ViewPos]
547 int pos = Term->WritePos - Term->WritePos % Term->Width;
549 giVT_OutputDevHandle,
550 (pos - Term->ViewPos)*sizeof(tVT_Char),
551 Term->Width*sizeof(tVT_Char),
560 #define MONOSPACE_FONT 10816
562 #if MONOSPACE_FONT == 10808 // 8x8
563 # include "vterm_font_8x8.h"
564 #elif MONOSPACE_FONT == 10816 // 8x16
565 # include "vterm_font_8x16.h"
569 int VT_Font_GetWidth(Uint32 Codepoint)
573 int VT_Font_GetHeight(Uint32 Codepoint)
578 void VT_Font_Render(Uint32 Codepoint, void *Buffer, Uint32 BGC, Uint32 FGC)
585 * \fn Uint8 *VT_Font_GetChar(Uint32 Codepoint)
586 * \brief Gets an index into the font array given a Unicode Codepoint
587 * \note See http://en.wikipedia.org/wiki/CP437
589 Uint8 *VT_Font_GetChar(Uint32 Codepoint)
593 return &VTermFont[Codepoint*FONT_HEIGHT];
596 case 0xC7: index = 128; break; // Ç
597 case 0xFC: index = 129; break; // ü
598 case 0xE9: index = 130; break; // é
599 case 0xE2: index = 131; break; // â
600 case 0xE4: index = 132; break; // ä
601 case 0xE0: index = 133; break; // à
602 case 0xE5: index = 134; break; // å
603 case 0xE7: index = 135; break; // ç
604 case 0xEA: index = 136; break; // ê
605 case 0xEB: index = 137; break; // ë
606 case 0xE8: index = 138; break; // è
607 case 0xEF: index = 139; break; // ï
608 case 0xEE: index = 140; break; // î
609 case 0xEC: index = 141; break; // ì
610 case 0xC4: index = 142; break; // Ä
611 case 0xC5: index = 143; break; // Å
614 return &VTermFont[index*FONT_HEIGHT];