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 pos += WriteUTF8(Buffer+pos, term->InputBuffer[term->InputRead]);
252 while(term->InputRead == term->InputWrite) Proc_Yield();
253 while(term->InputRead != term->InputWrite)
255 ((Uint32*)Buffer)[pos] = term->InputBuffer[term->InputRead];
266 * \fn Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
267 * \brief Write to a virtual terminal
269 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
271 tVTerm *term = &gVT_Terminals[ Node->Inode ];
273 //ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
279 VT_int_PutString(term, Buffer, Length);
282 //VT_int_PutString32(term, Buffer, Length);
290 * \fn int VT_IOCtl(tVFS_Node *Node, int Id, void *Data)
291 * \brief Call an IO Control on a virtual terminal
293 int VT_IOCtl(tVFS_Node *Node, int Id, void *Data)
299 * \fn void VT_KBCallBack(Uint32 Codepoint)
300 * \brief Called on keyboard interrupt
302 void VT_KBCallBack(Uint32 Codepoint)
304 tVTerm *term = &gVT_Terminals[giVT_CurrentTerminal];
306 term->InputBuffer[ term->InputWrite ] = Codepoint;
308 term->InputWrite %= MAX_INPUT_CHARS;
309 if(term->InputRead == term->InputWrite) {
311 term->InputRead %= MAX_INPUT_CHARS;
316 * \fn void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
317 * \brief Print a string to the Virtual Terminal
319 void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
323 for( i = 0; i < Count; i++ )
325 if( Buffer[i] == 0x1B ) // Escape Sequence
327 i += VT_int_ParseEscape(Term, (char*)&Buffer[i]);
331 if( Buffer[i] < 128 ) // Plain ASCII
332 VT_int_PutChar(Term, Buffer[i]);
334 i += ReadUTF8(&Buffer[i], &val);
335 VT_int_PutChar(Term, val);
341 * \fn int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
342 * \brief Parses a VT100 Escape code
344 int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
348 int args[4] = {0,0,0,0};
356 while('0' <= c && c <= '9') {
364 // Get string (what does this do?)
372 if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
379 memset(Term->Text, 0, Term->Width*Term->Height*VT_SCROLLBACK*sizeof(tVT_Char));
389 if( 0 <= args[argc] && args[argc] <= 8)
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
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;
404 else if(40 <= args[argc] && args[argc] <= 47) {
405 Term->CurColour &= 0xFFFF8000;
406 Term->CurColour |= caVT100Colours[ args[argc]-40+((Term->CurColour>>12)&15) ];
422 * \fn void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
423 * \brief Write a single character to a VTerm
425 void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
428 //ENTER("pTerm xCh", Term, Ch);
429 //LOG("Term = {WritePos:%i, ViewPos:%i}\n", Term->WritePos, Term->ViewPos);
434 Term->WritePos += Term->Width;
436 Term->WritePos -= Term->WritePos % Term->Width;
441 Term->Text[ Term->WritePos ].Ch = '\t';
442 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
444 } while(Term->WritePos & 7);
448 // Backspace is invalid at Offset 0
449 if(Term->WritePos == 0) break;
453 if(Term->Text[ Term->WritePos ].Ch != '\t') {
454 Term->Text[ Term->WritePos ].Ch = 0;
455 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
459 i = 7; // Limit it to 8
461 Term->Text[ Term->WritePos ].Ch = 0;
462 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
464 } while(Term->WritePos && i-- && Term->Text[ Term->WritePos ].Ch == '\t');
468 Term->Text[ Term->WritePos ].Ch = Ch;
469 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
474 if(Term->WritePos >= Term->Width*Term->Height*VT_SCROLLBACK)
477 Term->WritePos -= Term->Width;
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;
484 // Scroll terminal cache
485 base = Term->Width*(Term->Height*VT_SCROLLBACK-1);
488 memcpy( Term->Text, &Term->Text[Term->Width], base*sizeof(tVT_Char) );
491 for( i = 0; i < Term->Width; i ++ )
493 Term->Text[ base + i ].Ch = 0;
494 Term->Text[ base + i ].Colour = Term->CurColour;
497 VT_int_UpdateScreen( Term, 1 );
500 VT_int_UpdateScreen( Term, 0 );
506 * \fn void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
507 * \brief Updates the video framebuffer
509 void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
513 giVT_OutputDevHandle,
515 Term->Width*Term->Height*sizeof(tVT_Char),
516 &Term->Text[Term->ViewPos]
519 int pos = Term->WritePos - Term->WritePos % Term->Width;
521 giVT_OutputDevHandle,
522 (pos - Term->ViewPos)*sizeof(tVT_Char),
523 Term->Width*sizeof(tVT_Char),
532 #define MONOSPACE_FONT 10816
534 #if MONOSPACE_FONT == 10808 // 8x8
535 # include "vterm_font_8x8.h"
536 #elif MONOSPACE_FONT == 10816 // 8x16
537 # include "vterm_font_8x16.h"
541 int VT_Font_GetWidth(Uint32 Codepoint)
545 int VT_Font_GetHeight(Uint32 Codepoint)
550 void VT_Font_Render(Uint32 Codepoint, void *Buffer, Uint32 BGC, Uint32 FGC)
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
561 Uint8 *VT_Font_GetChar(Uint32 Codepoint)
565 return &VTermFont[Codepoint*FONT_HEIGHT];
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; // Å
586 return &VTermFont[index*FONT_HEIGHT];