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))
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
34 int ViewPos, WritePos;
39 Uint32 InputBuffer[MAX_INPUT_CHARS];
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 );
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
67 MODULE_DEFINE(0, 0x0032, VTerm, VT_Install, NULL, NULL);
68 tDevFS_Driver gVT_DrvInfo = {
71 .Flags = VFS_FFLAG_DIRECTORY,
74 .ReadDir = VT_ReadDir,
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;
87 * \fn int VT_Install(char **Arguments)
89 int VT_Install(char **Arguments)
91 char **args = Arguments;
98 for( ; (arg = *args); args++ )
100 if(arg[0] != '-') continue;
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]);
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]);
127 if(!gsVT_OutputDevice) gsVT_OutputDevice = DEFAULT_OUTPUT;
128 if(!gsVT_InputDevice) gsVT_InputDevice = DEFAULT_INPUT;
130 LOG("Using '%s' as output", gsVT_OutputDevice);
131 LOG("Using '%s' as input", gsVT_InputDevice);
134 for( i = 0; i < NUM_VTS; i++ )
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;
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) );
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
151 gVT_Terminals[i].Node.Read = VT_Read;
152 gVT_Terminals[i].Node.Write = VT_Write;
153 gVT_Terminals[i].Node.IOCtl = VT_IOCtl;
157 DevFS_AddDevice( &gVT_DrvInfo );
163 * \fn void VT_InitOutput()
164 * \brief Initialise Video Output
168 giVT_OutputDevHandle = VFS_Open(gsVT_OutputDevice, VFS_OPENFLAG_WRITE);
169 LOG("giVT_OutputDevHandle = %x\n", giVT_OutputDevHandle);
173 * \fn void VT_InitInput()
174 * \brief Initialises the input
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);
185 * \fn char *VT_ReadDir(tVFS_Node *Node, int Pos)
186 * \brief Read from the VTerm Directory
188 char *VT_ReadDir(tVFS_Node *Node, int Pos)
190 if(Pos < 0) return NULL;
191 if(Pos >= NUM_VTS) return NULL;
192 return gVT_Terminals[Pos].Name;
196 * \fn tVFS_Node *VT_FindDir(tVFS_Node *Node, char *Name)
197 * \brief Find an item in the VTerm directory
199 tVFS_Node *VT_FindDir(tVFS_Node *Node, char *Name)
203 //ENTER("pNode sName", Node, Name);
205 // Open the input and output files if needed
206 if(giVT_OutputDevHandle == -2) VT_InitOutput();
207 if(giVT_InputDevHandle == -2) VT_InitInput();
210 if(Name[0] < '0' || Name[0] > '9' || Name[1] != '\0') {
221 //LEAVE('p', &gVT_Terminals[num].Node);
222 return &gVT_Terminals[num].Node;
226 * \fn Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
227 * \brief Read from a virtual terminal
229 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
232 tVTerm *term = &gVT_Terminals[ Node->Inode ];
234 // Check current mode
240 while(term->InputRead == term->InputWrite) Proc_Yield();
241 while(term->InputRead != term->InputWrite)
243 LOG("WriteUTF8(%p, 0x%x)", Buffer+pos, term->InputBuffer[term->InputRead]);
244 pos += WriteUTF8(Buffer+pos, term->InputBuffer[term->InputRead]);
253 while(term->InputRead == term->InputWrite) Proc_Yield();
254 while(term->InputRead != term->InputWrite)
256 ((Uint32*)Buffer)[pos] = term->InputBuffer[term->InputRead];
267 * \fn Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
268 * \brief Write to a virtual terminal
270 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
272 tVTerm *term = &gVT_Terminals[ Node->Inode ];
274 //ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
280 VT_int_PutString(term, Buffer, Length);
283 //VT_int_PutString32(term, Buffer, Length);
291 * \fn int VT_IOCtl(tVFS_Node *Node, int Id, void *Data)
292 * \brief Call an IO Control on a virtual terminal
294 int VT_IOCtl(tVFS_Node *Node, int Id, void *Data)
300 * \fn void VT_KBCallBack(Uint32 Codepoint)
301 * \brief Called on keyboard interrupt
303 void VT_KBCallBack(Uint32 Codepoint)
305 tVTerm *term = &gVT_Terminals[giVT_CurrentTerminal];
307 term->InputBuffer[ term->InputWrite ] = Codepoint;
309 term->InputWrite %= MAX_INPUT_CHARS;
310 if(term->InputRead == term->InputWrite) {
312 term->InputRead %= MAX_INPUT_CHARS;
317 * \fn void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
318 * \brief Print a string to the Virtual Terminal
320 void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
324 for( i = 0; i < Count; i++ )
326 if( Buffer[i] == 0x1B ) // Escape Sequence
328 i += VT_int_ParseEscape(Term, (char*)&Buffer[i]);
332 if( Buffer[i] < 128 ) // Plain ASCII
333 VT_int_PutChar(Term, Buffer[i]);
335 i += ReadUTF8(&Buffer[i], &val);
336 VT_int_PutChar(Term, val);
342 * \fn int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
343 * \brief Parses a VT100 Escape code
345 int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
349 int args[4] = {0,0,0,0};
357 while('0' <= c && c <= '9') {
365 // Get string (what does this do?)
373 if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
380 memset(Term->Text, 0, Term->Width*Term->Height*VT_SCROLLBACK*sizeof(tVT_Char));
390 if( 0 <= args[argc] && args[argc] <= 8)
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
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;
405 else if(40 <= args[argc] && args[argc] <= 47) {
406 Term->CurColour &= 0xFFFF8000;
407 Term->CurColour |= caVT100Colours[ args[argc]-40+((Term->CurColour>>12)&15) ];
423 * \fn void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
424 * \brief Write a single character to a VTerm
426 void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
429 //ENTER("pTerm xCh", Term, Ch);
430 //LOG("Term = {WritePos:%i, ViewPos:%i}\n", Term->WritePos, Term->ViewPos);
435 Term->WritePos += Term->Width;
437 Term->WritePos -= Term->WritePos % Term->Width;
442 Term->Text[ Term->WritePos ].Ch = '\t';
443 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
445 } while(Term->WritePos & 7);
449 // Backspace is invalid at Offset 0
450 if(Term->WritePos == 0) break;
454 if(Term->Text[ Term->WritePos ].Ch != '\t') {
455 Term->Text[ Term->WritePos ].Ch = 0;
456 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
460 i = 7; // Limit it to 8
462 Term->Text[ Term->WritePos ].Ch = 0;
463 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
465 } while(Term->WritePos && i-- && Term->Text[ Term->WritePos ].Ch == '\t');
469 Term->Text[ Term->WritePos ].Ch = Ch;
470 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
475 if(Term->WritePos >= Term->Width*Term->Height*VT_SCROLLBACK)
478 Term->WritePos -= Term->Width;
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;
485 // Scroll terminal cache
486 base = Term->Width*(Term->Height*VT_SCROLLBACK-1);
489 memcpy( Term->Text, &Term->Text[Term->Width], base*sizeof(tVT_Char) );
492 for( i = 0; i < Term->Width; i ++ )
494 Term->Text[ base + i ].Ch = 0;
495 Term->Text[ base + i ].Colour = Term->CurColour;
498 VT_int_UpdateScreen( Term, 1 );
501 VT_int_UpdateScreen( Term, 0 );
507 * \fn void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
508 * \brief Updates the video framebuffer
510 void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
514 giVT_OutputDevHandle,
516 Term->Width*Term->Height*sizeof(tVT_Char),
517 &Term->Text[Term->ViewPos]
520 int pos = Term->WritePos - Term->WritePos % Term->Width;
522 giVT_OutputDevHandle,
523 (pos - Term->ViewPos)*sizeof(tVT_Char),
524 Term->Width*sizeof(tVT_Char),
533 #define MONOSPACE_FONT 10816
535 #if MONOSPACE_FONT == 10808 // 8x8
536 # include "vterm_font_8x8.h"
537 #elif MONOSPACE_FONT == 10816 // 8x16
538 # include "vterm_font_8x16.h"
542 int VT_Font_GetWidth(Uint32 Codepoint)
546 int VT_Font_GetHeight(Uint32 Codepoint)
551 void VT_Font_Render(Uint32 Codepoint, void *Buffer, Uint32 BGC, Uint32 FGC)
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
562 Uint8 *VT_Font_GetChar(Uint32 Codepoint)
566 return &VTermFont[Codepoint*FONT_HEIGHT];
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; // Å
587 return &VTermFont[index*FONT_HEIGHT];