2 * Acess2 Virtual Terminal Driver
8 #include <tpl_drv_video.h>
9 #include <tpl_drv_keyboard.h>
10 #include <tpl_drv_terminal.h>
13 #define USE_CTRL_ALT 0
16 #define VERSION ((0<<8)|(50))
19 #define MAX_INPUT_CHARS32 64
20 #define MAX_INPUT_CHARS8 (MAX_INPUT_CHARS32*4)
21 //#define DEFAULT_OUTPUT "BochsGA"
22 #define DEFAULT_OUTPUT "Vesa"
23 #define DEFAULT_INPUT "PS2Keyboard"
24 #define DEFAULT_WIDTH 640
25 #define DEFAULT_HEIGHT 480
26 #define DEFAULT_SCROLLBACK 2 // 2 Screens of text + current screen
27 #define DEFAULT_COLOUR (VT_COL_BLACK|(0xAAA<<16))
29 #define VT_FLAG_HIDECSR 0x01
30 #define VT_FLAG_HASFB 0x10 //!< Set if the VTerm has requested the Framebuffer
33 VT_INMODE_TEXT8, // UTF-8 Text Mode (VT100 Emulation)
34 VT_INMODE_TEXT32, // UTF-32 Text Mode (Acess Native)
40 int Mode; //!< Current Mode (see ::eTplTerminal_Modes)
41 int Flags; //!< Flags (see VT_FLAG_*)
43 short NewWidth; //!< Un-applied dimensions (Width)
44 short NewHeight; //!< Un-applied dimensions (Height)
45 short Width; //!< Virtual Width
46 short Height; //!< Virtual Height
47 short TextWidth; //!< Text Virtual Width
48 short TextHeight; //!< Text Virtual Height
50 int ViewPos; //!< View Buffer Offset (Text Only)
51 int WritePos; //!< Write Buffer Offset (Text Only)
52 Uint32 CurColour; //!< Current Text Colour
54 tMutex ReadingLock; //!< Lock the VTerm when a process is reading from it
55 tTID ReadingThread; //!< Owner of the lock
56 int InputRead; //!< Input buffer read position
57 int InputWrite; //!< Input buffer write position
58 char InputBuffer[MAX_INPUT_CHARS8];
63 char Name[2]; //!< Name of the terminal
68 extern void Debug_SetKTerminal(char *File);
71 int VT_Install(char **Arguments);
72 void VT_InitOutput(void);
73 void VT_InitInput(void);
74 char *VT_ReadDir(tVFS_Node *Node, int Pos);
75 tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name);
76 int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data);
77 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
78 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
79 int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data);
80 void VT_SetResolution(int Width, int Height);
81 void VT_SetMode(int Mode);
82 void VT_SetTerminal(int ID);
83 void VT_KBCallBack(Uint32 Codepoint);
84 void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count);
85 int VT_int_ParseEscape(tVTerm *Term, char *Buffer);
86 void VT_int_PutChar(tVTerm *Term, Uint32 Ch);
87 void VT_int_ScrollFramebuffer( tVTerm *Term );
88 void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll );
89 void VT_int_ChangeMode(tVTerm *Term, int NewMode, int NewWidth, int NewHeight);
92 const Uint16 caVT100Colours[] = {
93 // Black, Red, Green, Yellow, Blue, Purple, Cyan, Gray
94 // Same again, but bright
95 VT_COL_BLACK, 0x700, 0x070, 0x770, 0x007, 0x707, 0x077, 0xAAA,
96 VT_COL_GREY, 0xF00, 0x0F0, 0xFF0, 0x00F, 0xF0F, 0x0FF, VT_COL_WHITE
100 MODULE_DEFINE(0, VERSION, VTerm, VT_Install, NULL, DEFAULT_OUTPUT, DEFAULT_INPUT, NULL);
101 tDevFS_Driver gVT_DrvInfo = {
104 .Flags = VFS_FFLAG_DIRECTORY,
108 .ReadDir = VT_ReadDir,
109 .FindDir = VT_FindDir,
110 .IOCtl = VT_Root_IOCtl
114 tVTerm gVT_Terminals[NUM_VTS];
115 int giVT_CurrentTerminal = 0;
116 tVTerm *gpVT_CurTerm = &gVT_Terminals[0];
117 // --- Video State ---
118 short giVT_RealWidth = DEFAULT_WIDTH; //!< Screen Width
119 short giVT_RealHeight = DEFAULT_HEIGHT; //!< Screen Height
120 int giVT_Scrollback = DEFAULT_SCROLLBACK;
121 // --- Driver Handles ---
122 char *gsVT_OutputDevice = NULL;
123 char *gsVT_InputDevice = NULL;
124 int giVT_OutputDevHandle = -2;
125 int giVT_InputDevHandle = -2;
126 // --- Key States --- (Used for VT Switching/Magic Combos)
127 int gbVT_CtrlDown = 0;
128 int gbVT_AltDown = 0;
129 int gbVT_SysrqDown = 0;
133 * \fn int VT_Install(char **Arguments)
134 * \brief Installs the Virtual Terminal Driver
136 int VT_Install(char **Arguments)
143 char **args = Arguments;
144 char *arg, *opt, *val;
145 for( ; (arg = *args); args++ )
147 Log_Debug("VTerm", "Argument '%s'", arg);
149 val = arg + strpos(arg, '='); *val++ = '\0';
151 if( strcmp(opt, "Video") == 0 ) {
152 gsVT_OutputDevice = val;
154 else if( strcmp(opt, "Input") == 0 ) {
155 gsVT_InputDevice = val;
157 else if( strcmp(opt, "Width") == 0 ) {
158 giVT_RealWidth = atoi( val );
160 else if( strcmp(opt, "Height") == 0 ) {
161 giVT_RealHeight = atoi( val );
163 else if( strcmp(opt, "Scrollback") == 0 ) {
164 giVT_Scrollback = atoi( val );
169 if(gsVT_OutputDevice) Modules_InitialiseBuiltin( gsVT_OutputDevice );
170 if(gsVT_InputDevice) Modules_InitialiseBuiltin( gsVT_InputDevice );
173 if(!gsVT_OutputDevice) gsVT_OutputDevice = DEFAULT_OUTPUT;
174 if(!gsVT_InputDevice) gsVT_InputDevice = DEFAULT_INPUT;
179 tmp = malloc( 9 + strlen(gsVT_OutputDevice) + 1 );
180 strcpy(tmp, "/Devices/");
181 strcpy(&tmp[9], gsVT_OutputDevice);
182 gsVT_OutputDevice = tmp;
183 tmp = malloc( 9 + strlen(gsVT_InputDevice) + 1 );
184 strcpy(tmp, "/Devices/");
185 strcpy(&tmp[9], gsVT_InputDevice);
186 gsVT_InputDevice = tmp;
189 Log_Log("VTerm", "Using '%s' as output", gsVT_OutputDevice);
190 Log_Log("VTerm", "Using '%s' as input", gsVT_InputDevice);
193 for( i = 0; i < NUM_VTS; i++ )
195 gVT_Terminals[i].Mode = TERM_MODE_TEXT;
196 gVT_Terminals[i].Flags = 0;
197 gVT_Terminals[i].CurColour = DEFAULT_COLOUR;
198 gVT_Terminals[i].WritePos = 0;
199 gVT_Terminals[i].ViewPos = 0;
202 VT_int_ChangeMode( &gVT_Terminals[i],
203 TERM_MODE_TEXT, giVT_RealWidth, giVT_RealHeight );
205 gVT_Terminals[i].Name[0] = '0'+i;
206 gVT_Terminals[i].Name[1] = '\0';
207 gVT_Terminals[i].Node.Inode = i;
208 gVT_Terminals[i].Node.ImplPtr = &gVT_Terminals[i];
209 gVT_Terminals[i].Node.NumACLs = 0; // Only root can open virtual terminals
211 gVT_Terminals[i].Node.Read = VT_Read;
212 gVT_Terminals[i].Node.Write = VT_Write;
213 gVT_Terminals[i].Node.IOCtl = VT_Terminal_IOCtl;
217 DevFS_AddDevice( &gVT_DrvInfo );
222 // Set kernel output to VT0
223 Debug_SetKTerminal("/Devices/VTerm/0");
225 Log_Log("VTerm", "Returning %i", MODULE_ERR_OK);
226 return MODULE_ERR_OK;
230 * \fn void VT_InitOutput()
231 * \brief Initialise Video Output
235 giVT_OutputDevHandle = VFS_Open(gsVT_OutputDevice, VFS_OPENFLAG_WRITE);
236 if(giVT_InputDevHandle == -1) {
237 Log_Warning("VTerm", "Oh F**k, I can't open the video device '%s'", gsVT_OutputDevice);
240 VT_SetResolution( giVT_RealWidth, giVT_RealHeight );
242 VT_SetMode( VIDEO_BUFFMT_TEXT );
246 * \fn void VT_InitInput()
247 * \brief Initialises the input
251 giVT_InputDevHandle = VFS_Open(gsVT_InputDevice, VFS_OPENFLAG_READ);
252 if(giVT_InputDevHandle == -1) return ;
253 VFS_IOCtl(giVT_InputDevHandle, KB_IOCTL_SETCALLBACK, VT_KBCallBack);
257 * \brief Set the video resolution
258 * \param Width New screen width
259 * \param Height New screen height
261 void VT_SetResolution(int Width, int Height)
263 tVideo_IOCtl_Mode mode = {0};
267 // Create the video mode
269 mode.height = Height;
274 VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_FINDMODE, &mode );
276 if( Width != mode.width || Height != mode.height )
279 "Selected resolution (%ix%i is not supported) by the device, using (%ix%i)",
280 giVT_RealWidth, giVT_RealHeight,
281 mode.width, mode.height
284 VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_GETSETMODE, &tmp );
286 // Resize text terminals if needed
287 if( giVT_RealWidth != mode.width || giVT_RealHeight != mode.height )
289 int newBufSize = (giVT_RealWidth/giVT_CharWidth)
290 *(giVT_RealHeight/giVT_CharHeight)
291 *(giVT_Scrollback+1);
293 // Resize the text terminals
294 giVT_RealWidth = mode.width;
295 giVT_RealHeight = mode.height;
296 for( i = 0; i < NUM_VTS; i ++ )
298 if( gVT_Terminals[i].Mode != TERM_MODE_TEXT ) continue;
300 gVT_Terminals[i].Text = realloc(
301 gVT_Terminals[i].Text,
302 newBufSize*sizeof(tVT_Char)
309 * \brief Set video output buffer mode
311 void VT_SetMode(int Mode)
313 VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &Mode );
317 * \fn char *VT_ReadDir(tVFS_Node *Node, int Pos)
318 * \brief Read from the VTerm Directory
320 char *VT_ReadDir(tVFS_Node *Node, int Pos)
322 if(Pos < 0) return NULL;
323 if(Pos >= NUM_VTS) return NULL;
324 return strdup( gVT_Terminals[Pos].Name );
328 * \fn tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
329 * \brief Find an item in the VTerm directory
330 * \param Node Root node
331 * \param Name Name (number) of the terminal
333 tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
337 ENTER("pNode sName", Node, Name);
339 // Open the input and output files if needed
340 if(giVT_OutputDevHandle == -2) VT_InitOutput();
341 if(giVT_InputDevHandle == -2) VT_InitInput();
344 if(Name[0] < '0' || Name[0] > '9' || Name[1] != '\0') {
355 LEAVE('p', &gVT_Terminals[num].Node);
356 return &gVT_Terminals[num].Node;
360 * \fn int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
361 * \brief Control the VTerm Driver
363 int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
368 case DRV_IOCTL_TYPE: return DRV_TYPE_MISC;
369 case DRV_IOCTL_IDENT: memcpy(Data, "VT\0\0", 4); return 0;
370 case DRV_IOCTL_VERSION: return VERSION;
371 case DRV_IOCTL_LOOKUP: return 0;
373 case 4: // Get Video Driver
374 if(Data) strcpy(Data, gsVT_OutputDevice);
375 return strlen(gsVT_OutputDevice);
377 case 5: // Set Video Driver
378 if(!Data) return -EINVAL;
379 if(Threads_GetUID() != 0) return -EACCES;
383 free(gsVT_OutputDevice);
385 gsVT_OutputDevice = malloc(len+1);
386 strcpy(gsVT_OutputDevice, Data);
388 VFS_Close(giVT_OutputDevHandle);
389 giVT_OutputDevHandle = -1;
398 * \fn Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
399 * \brief Read from a virtual terminal
401 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
404 tVTerm *term = &gVT_Terminals[ Node->Inode ];
406 Mutex_Acquire( &term->ReadingLock );
407 term->ReadingThread = Threads_GetTID();
409 // Check current mode
416 //TODO: Sleep instead
417 while(term->InputRead == term->InputWrite) Threads_Sleep();
419 ((char*)Buffer)[pos] = term->InputBuffer[term->InputRead];
422 term->InputRead %= MAX_INPUT_CHARS8;
431 while(term->InputRead == term->InputWrite) Threads_Sleep();
432 ((Uint32*)Buffer)[pos] = ((Uint32*)term->InputBuffer)[term->InputRead];
435 term->InputRead %= MAX_INPUT_CHARS32;
440 term->ReadingThread = -1;
441 Mutex_Release( &term->ReadingLock );
447 * \fn Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
448 * \brief Write to a virtual terminal
450 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
452 tVTerm *term = &gVT_Terminals[ Node->Inode ];
460 VT_int_PutString(term, Buffer, Length);
466 size = term->Width*term->Height*4;
467 if( Offset > size ) {
468 Log_Notice("VTerm", "VT_Write: Offset (0x%llx) > FBSize (0x%x)",
472 if( Offset + Length > size ) {
473 Log_Notice("VTerm", "VT_Write: Offset+Length (0x%llx) > FBSize (0x%x)",
474 Offset+Length, size);
475 Length = size - Offset;
478 // Copy to the local cache
479 memcpy( (void*)((Uint)term->Buffer + (Uint)Offset), Buffer, Length );
481 // Update screen if needed
482 if( Node->Inode == giVT_CurrentTerminal )
484 // Fill entire screen?
485 if( giVT_RealWidth > term->Width || giVT_RealHeight > term->Height )
487 // No? :( Well, just center it
489 x = Offset/4; y = x / term->Width; x %= term->Width;
490 w = Length/4+x; h = w / term->Width; w %= term->Width;
492 x += (giVT_RealWidth - term->Width) / 2;
493 y += (giVT_RealHeight - term->Height) / 2;
496 VFS_WriteAt( giVT_OutputDevHandle,
497 (x + y * giVT_RealWidth)*4,
501 Buffer = (void*)( (Uint)Buffer + term->Width*4 );
507 return VFS_WriteAt( giVT_OutputDevHandle, Offset, Length, Buffer );
510 // Just pass on (for now)
511 // TODO: Handle locally too to ensure no information is lost on
512 // VT Switch (and to isolate terminals from each other)
513 case TERM_MODE_2DACCEL:
514 //case TERM_MODE_3DACCEL:
515 if( Node->Inode == giVT_CurrentTerminal )
517 VFS_Write( giVT_OutputDevHandle, Length, Buffer );
526 * \fn int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
527 * \brief Call an IO Control on a virtual terminal
529 int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
533 tVTerm *term = Node->ImplPtr;
534 ENTER("pNode iId pData", Node, Id, Data);
536 if(Id >= DRV_IOCTL_LOOKUP) {
537 // Only root can fiddle with graphics modes
538 // TODO: Remove this and replace with user ownership
539 if( Threads_GetUID() != 0 ) return -1;
546 LEAVE('i', DRV_TYPE_TERMINAL);
547 return DRV_TYPE_TERMINAL;
548 case DRV_IOCTL_IDENT:
549 memcpy(Data, "VT\0\0", 4);
552 case DRV_IOCTL_VERSION:
555 case DRV_IOCTL_LOOKUP:
559 // Get/Set the mode (and apply any changes)
560 case TERM_IOCTL_MODETYPE:
563 if( CheckMem(Data, sizeof(int)) == 0 ) {
567 Log_Log("VTerm", "VTerm %i mode set to %i", (int)Node->Inode, *iData);
569 // Update mode if needed
570 if( term->Mode != *iData
574 // Adjust for text mode
575 if( *iData == TERM_MODE_TEXT ) {
576 term->NewHeight *= giVT_CharHeight;
577 term->NewWidth *= giVT_CharWidth;
579 // Fill unchanged dimensions
580 if(term->NewHeight == 0) term->NewHeight = term->Height;
581 if(term->NewWidth == 0) term->NewWidth = term->Width;
583 VT_int_ChangeMode(term, *iData, term->NewWidth, term->NewHeight);
584 // Clear unapplied dimensions
589 // Update the screen dimensions
590 if(Node->Inode == giVT_CurrentTerminal)
591 VT_SetTerminal( giVT_CurrentTerminal );
593 LEAVE('i', term->Mode);
596 // Get/set the terminal width
597 case TERM_IOCTL_WIDTH:
599 if( CheckMem(Data, sizeof(int)) == 0 ) {
603 term->NewWidth = *iData;
606 ret = term->NewWidth;
607 else if( term->Mode == TERM_MODE_TEXT )
608 ret = term->TextWidth;
614 // Get/set the terminal height
615 case TERM_IOCTL_HEIGHT:
617 if( CheckMem(Data, sizeof(int)) == 0 ) {
621 term->NewHeight = *iData;
623 if( term->NewHeight )
624 ret = term->NewHeight;
625 else if( term->Mode == TERM_MODE_TEXT )
626 ret = term->TextHeight;
632 case TERM_IOCTL_FORCESHOW:
633 Log_Log("VTerm", "Thread %i forced VTerm %i to be shown",
634 Threads_GetTID(), (int)Node->Inode);
635 VT_SetTerminal( Node->Inode );
644 * \fn void VT_SetTerminal(int ID)
645 * \brief Set the current terminal
647 void VT_SetTerminal(int ID)
649 // Update current terminal ID
650 Log_Log("VTerm", "Changed terminal from %i to %i", giVT_CurrentTerminal, ID);
651 giVT_CurrentTerminal = ID;
652 gpVT_CurTerm = &gVT_Terminals[ID];
655 if( gpVT_CurTerm->Mode == TERM_MODE_TEXT && !(gpVT_CurTerm->Flags & VT_FLAG_HIDECSR) )
657 tVideo_IOCtl_Pos pos;
658 pos.x = (gpVT_CurTerm->WritePos - gpVT_CurTerm->ViewPos) % gpVT_CurTerm->TextWidth;
659 pos.y = (gpVT_CurTerm->WritePos - gpVT_CurTerm->ViewPos) / gpVT_CurTerm->TextWidth;
660 if( pos.x < gpVT_CurTerm->TextHeight )
661 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
664 if( gpVT_CurTerm->Mode == TERM_MODE_TEXT )
665 VT_SetMode( VIDEO_BUFFMT_TEXT );
667 VT_SetMode( VIDEO_BUFFMT_FRAMEBUFFER );
670 VT_int_UpdateScreen( &gVT_Terminals[ ID ], 1 );
674 * \fn void VT_KBCallBack(Uint32 Codepoint)
675 * \brief Called on keyboard interrupt
676 * \param Codepoint Pseudo-UTF32 character
678 * Handles a key press and sends the key code to the user's buffer.
679 * If the code creates a kernel-magic sequence, it is not passed to the
680 * user and is handled in-kernel.
682 void VT_KBCallBack(Uint32 Codepoint)
684 tVTerm *term = gpVT_CurTerm;
686 // How the hell did we get a codepoint of zero?
687 if(Codepoint == 0) return;
690 if( Codepoint & 0x80000000 )
692 Codepoint &= 0x7FFFFFFF;
696 case KEY_RSHIFT: gbVT_CtrlDown = 0; break;
697 case KEY_LSHIFT: gbVT_AltDown = 0; break;
699 case KEY_LALT: gbVT_AltDown &= ~1; break;
700 case KEY_RALT: gbVT_AltDown &= ~2; break;
701 case KEY_LCTRL: gbVT_CtrlDown &= ~1 break;
702 case KEY_RCTRL: gbVT_CtrlDown &= ~2; break;
710 #if !USE_CTRL_ALT // HACK: Use both shifts instead of Ctrl-Alt
711 case KEY_RSHIFT: gbVT_CtrlDown = 1; break;
712 case KEY_LSHIFT: gbVT_AltDown = 1; break;
714 case KEY_LALT: gbVT_AltDown |= 1; break;
715 case KEY_RALT: gbVT_AltDown |= 2; break;
716 case KEY_LCTRL: gbVT_CtrlDown |= 1; break;
717 case KEY_RCTRL: gbVT_CtrlDown |= 2; break;
721 if(!gbVT_AltDown || !gbVT_CtrlDown)
725 case KEY_F1: VT_SetTerminal(0); return;
726 case KEY_F2: VT_SetTerminal(1); return;
727 case KEY_F3: VT_SetTerminal(2); return;
728 case KEY_F4: VT_SetTerminal(3); return;
729 case KEY_F5: VT_SetTerminal(4); return;
730 case KEY_F6: VT_SetTerminal(5); return;
731 case KEY_F7: VT_SetTerminal(6); return;
732 case KEY_F8: VT_SetTerminal(7); return;
733 case KEY_F9: VT_SetTerminal(8); return;
734 case KEY_F10: VT_SetTerminal(9); return;
735 case KEY_F11: VT_SetTerminal(10); return;
736 case KEY_F12: VT_SetTerminal(11); return;
739 if( gpVT_CurTerm->ViewPos > gpVT_CurTerm->Width )
740 gpVT_CurTerm->ViewPos -= gpVT_CurTerm->Width;
742 gpVT_CurTerm->ViewPos = 0;
745 if( gpVT_CurTerm->ViewPos < gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback-1) )
746 gpVT_CurTerm->ViewPos += gpVT_CurTerm->Width;
748 gpVT_CurTerm->ViewPos = gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback-1);
754 if(term->Mode == TERM_MODE_TEXT)
759 // Ignore Modifer Keys
760 if(Codepoint > KEY_MODIFIERS) return;
762 // Get UTF-8/ANSI Encoding
766 buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'D';
770 buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'C';
774 buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'A';
778 buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'B';
783 buf[0] = '\x1B'; buf[1] = '['; buf[2] = '5'; // Some overline also
784 //len = 4; // Commented out until I'm sure
790 // Attempt to encode in UTF-8
792 len = WriteUTF8( buf, Codepoint );
794 Warning("Codepoint (%x) is unrepresentable in UTF-8", Codepoint);
800 // Unprintable / Don't Pass
805 if( MAX_INPUT_CHARS8 - term->InputWrite >= len )
806 memcpy( &term->InputBuffer[term->InputWrite], buf, len );
808 memcpy( &term->InputBuffer[term->InputWrite], buf, MAX_INPUT_CHARS8 - term->InputWrite );
809 memcpy( &term->InputBuffer[0], buf, len - (MAX_INPUT_CHARS8 - term->InputWrite) );
811 // Roll the buffer over
812 term->InputWrite += len;
813 term->InputWrite %= MAX_INPUT_CHARS8;
814 if( (term->InputWrite - term->InputRead + MAX_INPUT_CHARS8)%MAX_INPUT_CHARS8 < len ) {
815 term->InputRead = term->InputWrite + 1;
816 term->InputRead %= MAX_INPUT_CHARS8;
822 // Encode the raw UTF-32 Key
823 ((Uint32*)term->InputBuffer)[ term->InputWrite ] = Codepoint;
825 term->InputWrite %= MAX_INPUT_CHARS32;
826 if(term->InputRead == term->InputWrite) {
828 term->InputRead %= MAX_INPUT_CHARS32;
832 // Wake up the thread waiting on us
833 if( term->ReadingThread >= 0 ) {
834 Threads_WakeTID(term->ReadingThread);
839 * \fn void VT_int_ClearLine(tVTerm *Term, int Num)
840 * \brief Clears a line in a virtual terminal
842 void VT_int_ClearLine(tVTerm *Term, int Num)
845 tVT_Char *cell = &Term->Text[ Num*Term->TextWidth ];
846 if( Num < 0 || Num >= Term->TextHeight * (giVT_Scrollback + 1) ) return ;
847 //ENTER("pTerm iNum", Term, Num);
848 for( i = Term->TextWidth; i--; )
851 cell[ i ].Colour = Term->CurColour;
857 * \fn int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
858 * \brief Parses a VT100 Escape code
860 int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
865 int args[6] = {0,0,0,0};
873 if( '0' <= c && c <= '9' )
876 if(c == ';') c = Buffer[j++];
877 while('0' <= c && c <= '9') {
887 if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
893 if(argc == 1) tmp = args[0];
896 if( Term->WritePos-(tmp-1) % Term->TextWidth == 0 )
897 Term->WritePos -= Term->WritePos % Term->TextWidth;
899 Term->WritePos -= tmp;
904 if(argc == 1) tmp = args[0];
906 if( (Term->WritePos + tmp) % Term->TextWidth == 0 ) {
907 Term->WritePos -= Term->WritePos % Term->TextWidth;
908 Term->WritePos += Term->TextWidth - 1;
910 Term->WritePos += tmp;
920 int i = Term->TextHeight * (giVT_Scrollback + 1);
921 while( i-- ) VT_int_ClearLine(Term, i);
924 VT_int_UpdateScreen(Term, 1);
929 // Set cursor position
931 Term->WritePos = args[0] + args[1]*Term->TextWidth;
932 Log_Debug("VTerm", "args = {%i, %i}", args[0], args[1]);
939 if( 0 <= args[argc] && args[argc] <= 8)
943 case 0: Term->CurColour = DEFAULT_COLOUR; break; // Reset
944 case 1: Term->CurColour |= 0x80000000; break; // Bright
945 case 2: Term->CurColour &= ~0x80000000; break; // Dim
949 else if(30 <= args[argc] && args[argc] <= 37) {
950 Term->CurColour &= 0xF000FFFF;
951 Term->CurColour |= (Uint32)caVT100Colours[ args[argc]-30+(Term->CurColour>>28) ] << 16;
954 else if(40 <= args[argc] && args[argc] <= 47) {
955 Term->CurColour &= 0xFFFF8000;
956 Term->CurColour |= caVT100Colours[ args[argc]-40+((Term->CurColour>>12)&15) ];
961 Log_Warning("VTerm", "Unknown control sequence");
970 //Log_Debug("VTerm", "j = %i, Buffer = '%s'", j, Buffer);
975 * \fn void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
976 * \brief Print a string to the Virtual Terminal
978 void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
984 for( i = 0; i < Count; i++ )
986 // Handle escape sequences
987 if( Buffer[i] == 0x1B )
990 i += VT_int_ParseEscape(Term, (char*)&Buffer[i]) - 1;
994 // Fast check for non UTF-8
995 if( Buffer[i] < 128 ) // Plain ASCII
996 VT_int_PutChar(Term, Buffer[i]);
998 i += ReadUTF8(&Buffer[i], &val) - 1;
999 VT_int_PutChar(Term, val);
1003 VT_int_UpdateScreen( Term, 0 );
1006 if( Term == gpVT_CurTerm && !(Term->Flags & VT_FLAG_HIDECSR) )
1008 tVideo_IOCtl_Pos pos;
1009 pos.x = (Term->WritePos - Term->ViewPos) % Term->TextWidth;
1010 pos.y = (Term->WritePos - Term->ViewPos) / Term->TextWidth;
1011 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
1016 * \fn void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
1017 * \brief Write a single character to a VTerm
1019 void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
1025 case '\0': return; // Ignore NULL byte
1027 VT_int_UpdateScreen( Term, 0 ); // Update the line before newlining
1028 Term->WritePos += Term->TextWidth;
1030 Term->WritePos -= Term->WritePos % Term->TextWidth;
1035 Term->Text[ Term->WritePos ].Ch = '\0';
1036 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
1038 } while(Term->WritePos & 7);
1042 // Backspace is invalid at Offset 0
1043 if(Term->WritePos == 0) break;
1047 if(Term->Text[ Term->WritePos ].Ch != '\0') {
1048 Term->Text[ Term->WritePos ].Ch = 0;
1049 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
1053 i = 7; // Limit it to 8
1055 Term->Text[ Term->WritePos ].Ch = 0;
1056 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
1058 } while(Term->WritePos && i-- && Term->Text[ Term->WritePos ].Ch == '\0');
1059 if(Term->Text[ Term->WritePos ].Ch != '\0')
1064 Term->Text[ Term->WritePos ].Ch = Ch;
1065 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
1071 // - Check if we need to scroll the entire scrollback buffer
1072 if(Term->WritePos >= Term->TextWidth*Term->TextHeight*(giVT_Scrollback+1))
1077 Term->WritePos -= Term->TextWidth;
1079 VT_int_UpdateScreen( Term, 0 );
1081 // Update view position
1082 base = Term->TextWidth*Term->TextHeight*(giVT_Scrollback);
1083 if(Term->ViewPos < base)
1084 Term->ViewPos += Term->Width;
1085 if(Term->ViewPos > base)
1086 Term->ViewPos = base;
1088 // Scroll terminal cache
1089 base = Term->TextWidth*(Term->TextHeight*(giVT_Scrollback+1)-1);
1092 &Term->Text[Term->TextWidth],
1093 base*sizeof(tVT_Char)
1097 for( i = 0; i < Term->TextWidth; i ++ )
1099 Term->Text[ base + i ].Ch = 0;
1100 Term->Text[ base + i ].Colour = Term->CurColour;
1103 VT_int_ScrollFramebuffer( Term );
1104 VT_int_UpdateScreen( Term, 0 );
1106 // Ok, so we only need to scroll the screen
1107 else if(Term->WritePos >= Term->ViewPos + Term->TextWidth*Term->TextHeight)
1109 //Debug("Term->WritePos (%i) >= %i",
1111 // Term->ViewPos + Term->Width*Term->Height
1113 //Debug("Scrolling screen only");
1115 // Update the last line
1116 Term->WritePos -= Term->TextWidth;
1117 VT_int_UpdateScreen( Term, 0 );
1118 Term->WritePos += Term->TextWidth;
1119 VT_int_ClearLine(Term, Term->WritePos / Term->TextWidth);
1122 Term->ViewPos += Term->TextWidth;
1123 //Debug("Term->ViewPos = %i", Term->ViewPos);
1124 VT_int_ScrollFramebuffer( Term );
1125 VT_int_UpdateScreen( Term, 0 );
1132 * \fn void VT_int_ScrollFramebuffer( tVTerm *Term )
1133 * \note Scrolls the framebuffer by 1 text line
1135 void VT_int_ScrollFramebuffer( tVTerm *Term )
1145 // Only update if this is the current terminal
1146 if( Term != gpVT_CurTerm ) return;
1148 // Switch to 2D Command Stream
1149 tmp = VIDEO_BUFFMT_2DSTREAM;
1150 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp);
1152 // BLIT from 0,0 to 0,giVT_CharHeight
1153 buf.Op = VIDEO_2DOP_BLIT;
1154 buf.DstX = 0; buf.DstY = 0;
1155 buf.SrcX = 0; buf.SrcY = giVT_CharHeight;
1156 buf.W = Term->TextWidth * giVT_CharWidth;
1157 buf.H = (Term->TextHeight-1) * giVT_CharHeight;
1158 VFS_WriteAt(giVT_OutputDevHandle, 0, sizeof(buf), &buf);
1160 // Restore old mode (this function is only called during text mode)
1161 tmp = VIDEO_BUFFMT_TEXT;
1162 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp);
1166 * \fn void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
1167 * \brief Updates the video framebuffer
1169 void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
1171 // Only update if this is the current terminal
1172 if( Term != gpVT_CurTerm ) return;
1174 switch( Term->Mode )
1176 case TERM_MODE_TEXT:
1177 // Re copy the entire screen?
1180 giVT_OutputDevHandle,
1182 Term->TextWidth*Term->TextHeight*sizeof(tVT_Char),
1183 &Term->Text[Term->ViewPos]
1186 // Only copy the current line
1188 int pos = Term->WritePos - Term->WritePos % Term->TextWidth;
1190 giVT_OutputDevHandle,
1191 (pos - Term->ViewPos)*sizeof(tVT_Char),
1192 Term->TextWidth*sizeof(tVT_Char),
1199 giVT_OutputDevHandle,
1201 Term->Width*Term->Height*sizeof(Uint32),
1209 * \brief Update the screen mode
1210 * \param Term Terminal to update
1211 * \param NewMode New mode to set
1212 * \param NewWidth New framebuffer width
1213 * \param NewHeight New framebuffer height
1215 void VT_int_ChangeMode(tVTerm *Term, int NewMode, int NewWidth, int NewHeight)
1217 int oldW = Term->Width;
1218 int oldTW = oldW / giVT_CharWidth;
1219 int oldH = Term->Height;
1220 int oldTH = oldH / giVT_CharWidth;
1221 tVT_Char *oldTBuf = Term->Text;
1222 Uint32 *oldFB = Term->Buffer;
1225 // TODO: Increase RealWidth/RealHeight when this happens
1226 if(NewWidth > giVT_RealWidth) NewWidth = giVT_RealWidth;
1227 if(NewHeight > giVT_RealHeight) NewHeight = giVT_RealHeight;
1229 // Calculate new dimensions
1230 Term->TextWidth = NewWidth / giVT_CharWidth;
1231 Term->TextHeight = NewHeight / giVT_CharHeight;
1232 Term->Width = NewWidth;
1233 Term->Height = NewHeight;
1234 Term->Mode = NewMode;
1236 // Allocate new buffers
1238 Term->Text = calloc(
1239 Term->TextWidth * Term->TextHeight * (giVT_Scrollback+1),
1245 if( w > Term->TextWidth ) w = Term->TextWidth;
1247 if( h > Term->TextHeight ) h = Term->TextHeight;
1248 h *= giVT_Scrollback + 1;
1249 for( i = 0; i < h; i ++ )
1252 &Term->Text[i*Term->TextWidth],
1261 Term->Buffer = calloc( Term->Width * Term->Height, sizeof(Uint32) );
1265 if( w > Term->Width ) w = Term->Width;
1267 if( h > Term->Height ) h = Term->Height;
1268 for( i = 0; i < h; i ++ )
1271 &Term->Buffer[i*Term->Width],
1282 case TERM_MODE_TEXT:
1283 Log_Log("VTerm", "Set VT %p to text mode (%ix%i)",
1284 Term, Term->TextWidth, Term->TextHeight);
1287 Log_Log("VTerm", "Set VT %p to framebuffer mode (%ix%i)",
1288 Term, Term->Width, Term->Height);
1290 //case TERM_MODE_2DACCEL:
1291 //case TERM_MODE_3DACCEL:
1299 #define MONOSPACE_FONT 10816
1301 #if MONOSPACE_FONT == 10808 // 8x8
1302 # include "vterm_font_8x8.h"
1303 #elif MONOSPACE_FONT == 10816 // 8x16
1304 # include "vterm_font_8x16.h"
1307 // === PROTOTYPES ===
1308 Uint8 *VT_Font_GetChar(Uint32 Codepoint);
1311 int giVT_CharWidth = FONT_WIDTH;
1312 int giVT_CharHeight = FONT_HEIGHT;
1316 * \fn void VT_Font_Render(Uint32 Codepoint, void *Buffer, int Pitch, Uint32 BGC, Uint32 FGC)
1317 * \brief Render a font character
1319 void VT_Font_Render(Uint32 Codepoint, void *Buffer, int Pitch, Uint32 BGC, Uint32 FGC)
1322 Uint32 *buf = Buffer;
1325 font = VT_Font_GetChar(Codepoint);
1327 for(y = 0; y < FONT_HEIGHT; y ++)
1329 for(x = 0; x < FONT_WIDTH; x ++)
1331 if(*font & (1 << (FONT_WIDTH-x-1)))
1342 * \fn Uint32 VT_Colour12to24(Uint16 Col12)
1345 Uint32 VT_Colour12to24(Uint16 Col12)
1350 ret = (tmp << 0) | (tmp << 4);
1351 tmp = (Col12 & 0xF0) >> 4;
1352 ret |= (tmp << 8) | (tmp << 12);
1353 tmp = (Col12 & 0xF00) >> 8;
1354 ret |= (tmp << 16) | (tmp << 20);
1359 * \fn Uint8 *VT_Font_GetChar(Uint32 Codepoint)
1360 * \brief Gets an index into the font array given a Unicode Codepoint
1361 * \note See http://en.wikipedia.org/wiki/CP437
1363 Uint8 *VT_Font_GetChar(Uint32 Codepoint)
1367 return &VTermFont[Codepoint*FONT_HEIGHT];
1370 case 0xC7: index = 128; break; // Ç
1371 case 0xFC: index = 129; break; // ü
1372 case 0xE9: index = 130; break; // é
1373 case 0xE2: index = 131; break; // â
1374 case 0xE4: index = 132; break; // ä
1375 case 0xE0: index = 133; break; // à
1376 case 0xE5: index = 134; break; // å
1377 case 0xE7: index = 135; break; // ç
1378 case 0xEA: index = 136; break; // ê
1379 case 0xEB: index = 137; break; // ë
1380 case 0xE8: index = 138; break; // è
1381 case 0xEF: index = 139; break; // ï
1382 case 0xEE: index = 140; break; // î
1383 case 0xEC: index = 141; break; // ì
1384 case 0xC4: index = 142; break; // Ä
1385 case 0xC5: index = 143; break; // Å
1388 return &VTermFont[index*FONT_HEIGHT];
1391 EXPORTAS(&giVT_CharWidth, giVT_CharWidth);
1392 EXPORTAS(&giVT_CharHeight, giVT_CharHeight);
1393 EXPORT(VT_Font_Render);
1394 EXPORT(VT_Colour12to24);