2 * Acess2 Virtual Terminal Driver
8 #include <api_drv_video.h>
9 #include <api_drv_keyboard.h>
10 #include <api_drv_terminal.h>
12 #include <semaphore.h>
15 #define VERSION ((0<<8)|(50))
18 #define MAX_INPUT_CHARS32 64
19 #define MAX_INPUT_CHARS8 (MAX_INPUT_CHARS32*4)
20 //#define DEFAULT_OUTPUT "BochsGA"
21 #define DEFAULT_OUTPUT "Vesa"
22 #define FALLBACK_OUTPUT "x86_VGAText"
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_SCROLLBACK 0
28 #define DEFAULT_COLOUR (VT_COL_BLACK|(0xAAA<<16))
30 #define VT_FLAG_HIDECSR 0x01
31 #define VT_FLAG_ALTBUF 0x02 //!< Alternate screen buffer
32 #define VT_FLAG_RAWIN 0x04 //!< Don't handle ^Z/^C/^V
33 #define VT_FLAG_HASFB 0x10 //!< Set if the VTerm has requested the Framebuffer
36 VT_INMODE_TEXT8, // UTF-8 Text Mode (VT100/xterm Emulation)
37 VT_INMODE_TEXT32, // UTF-32 Text Mode (Acess Native)
43 int Mode; //!< Current Mode (see ::eTplTerminal_Modes)
44 int Flags; //!< Flags (see VT_FLAG_*)
46 short NewWidth; //!< Un-applied dimensions (Width)
47 short NewHeight; //!< Un-applied dimensions (Height)
48 short Width; //!< Virtual Width
49 short Height; //!< Virtual Height
50 short TextWidth; //!< Text Virtual Width
51 short TextHeight; //!< Text Virtual Height
53 Uint32 CurColour; //!< Current Text Colour
55 int ViewPos; //!< View Buffer Offset (Text Only)
56 int WritePos; //!< Write Buffer Offset (Text Only)
59 tVT_Char *AltBuf; //!< Alternate Screen Buffer
60 int AltWritePos; //!< Alternate write position
61 short ScrollTop; //!< Top of scrolling region (smallest)
62 short ScrollHeight; //!< Length of scrolling region
64 tMutex ReadingLock; //!< Lock the VTerm when a process is reading from it
65 tTID ReadingThread; //!< Owner of the lock
66 int InputRead; //!< Input buffer read position
67 int InputWrite; //!< Input buffer write position
68 char InputBuffer[MAX_INPUT_CHARS8];
69 // tSemaphore InputSemaphore;
73 char Name[2]; //!< Name of the terminal
78 extern void Debug_SetKTerminal(const char *File);
81 int VT_Install(char **Arguments);
82 void VT_InitOutput(void);
83 void VT_InitInput(void);
84 char *VT_ReadDir(tVFS_Node *Node, int Pos);
85 tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name);
86 int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data);
87 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
88 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
89 int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data);
90 void VT_SetResolution(int Width, int Height);
91 void VT_SetMode(int Mode);
92 void VT_SetTerminal(int ID);
93 void VT_KBCallBack(Uint32 Codepoint);
94 void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count);
95 void VT_int_ClearLine(tVTerm *Term, int Num);
96 int VT_int_ParseEscape(tVTerm *Term, char *Buffer);
97 void VT_int_PutChar(tVTerm *Term, Uint32 Ch);
98 void VT_int_ScrollText(tVTerm *Term, int Count);
99 void VT_int_ScrollFramebuffer( tVTerm *Term, int Count );
100 void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll );
101 void VT_int_ChangeMode(tVTerm *Term, int NewMode, int NewWidth, int NewHeight);
102 void VT_int_ToggleAltBuffer(tVTerm *Term, int Enabled);
105 const Uint16 caVT100Colours[] = {
106 // Black, Red, Green, Yellow, Blue, Purple, Cyan, Gray
107 // Same again, but bright
108 VT_COL_BLACK, 0x700, 0x070, 0x770, 0x007, 0x707, 0x077, 0xAAA,
109 VT_COL_GREY, 0xF00, 0x0F0, 0xFF0, 0x00F, 0xF0F, 0x0FF, VT_COL_WHITE
113 MODULE_DEFINE(0, VERSION, VTerm, VT_Install, NULL, DEFAULT_INPUT, FALLBACK_OUTPUT, NULL);
114 tDevFS_Driver gVT_DrvInfo = {
117 .Flags = VFS_FFLAG_DIRECTORY,
121 .ReadDir = VT_ReadDir,
122 .FindDir = VT_FindDir,
123 .IOCtl = VT_Root_IOCtl
127 tVTerm gVT_Terminals[NUM_VTS];
128 int giVT_CurrentTerminal = 0;
129 tVTerm *gpVT_CurTerm = &gVT_Terminals[0];
130 // --- Video State ---
131 short giVT_RealWidth = DEFAULT_WIDTH; //!< Screen Width
132 short giVT_RealHeight = DEFAULT_HEIGHT; //!< Screen Height
133 int giVT_Scrollback = DEFAULT_SCROLLBACK;
134 // --- Driver Handles ---
135 char *gsVT_OutputDevice = NULL;
136 char *gsVT_InputDevice = NULL;
137 int giVT_OutputDevHandle = -2;
138 int giVT_InputDevHandle = -2;
139 // --- Key States --- (Used for VT Switching/Magic Combos)
140 int gbVT_CtrlDown = 0;
141 int gbVT_AltDown = 0;
142 int gbVT_SysrqDown = 0;
146 * \fn int VT_Install(char **Arguments)
147 * \brief Installs the Virtual Terminal Driver
149 int VT_Install(char **Arguments)
158 for(args = Arguments; (arg = *args); args++ )
160 char data[strlen(arg)+1];
164 val = strchr(arg, '=');
167 data[ val - arg ] = '\0';
170 Log_Debug("VTerm", "Argument '%s'", arg);
172 if( strcmp(opt, "Video") == 0 ) {
173 if( !gsVT_OutputDevice )
174 gsVT_OutputDevice = strdup(val);
176 else if( strcmp(opt, "Input") == 0 ) {
177 if( !gsVT_InputDevice )
178 gsVT_InputDevice = strdup(val);
180 else if( strcmp(opt, "Width") == 0 ) {
181 giVT_RealWidth = atoi( val );
183 else if( strcmp(opt, "Height") == 0 ) {
184 giVT_RealHeight = atoi( val );
186 else if( strcmp(opt, "Scrollback") == 0 ) {
187 giVT_Scrollback = atoi( val );
193 if(!gsVT_OutputDevice) gsVT_OutputDevice = (char*)DEFAULT_OUTPUT;
194 else if( Module_EnsureLoaded( gsVT_OutputDevice ) ) gsVT_OutputDevice = (char*)DEFAULT_OUTPUT;
195 if( Module_EnsureLoaded( gsVT_OutputDevice ) ) gsVT_OutputDevice = (char*)FALLBACK_OUTPUT;
197 if(!gsVT_InputDevice) gsVT_InputDevice = (char*)DEFAULT_INPUT;
198 else if( Module_EnsureLoaded( gsVT_InputDevice ) ) gsVT_InputDevice = (char*)DEFAULT_INPUT;
200 // Create device paths
203 tmp = malloc( 9 + strlen(gsVT_OutputDevice) + 1 );
204 strcpy(tmp, "/Devices/");
205 strcpy(&tmp[9], gsVT_OutputDevice);
206 gsVT_OutputDevice = tmp;
208 tmp = malloc( 9 + strlen(gsVT_InputDevice) + 1 );
209 strcpy(tmp, "/Devices/");
210 strcpy(&tmp[9], gsVT_InputDevice);
211 gsVT_InputDevice = tmp;
214 Log_Log("VTerm", "Using '%s' as output", gsVT_OutputDevice);
215 Log_Log("VTerm", "Using '%s' as input", gsVT_InputDevice);
218 for( i = 0; i < NUM_VTS; i++ )
220 gVT_Terminals[i].Mode = TERM_MODE_TEXT;
221 gVT_Terminals[i].Flags = 0;
222 gVT_Terminals[i].CurColour = DEFAULT_COLOUR;
223 gVT_Terminals[i].WritePos = 0;
224 gVT_Terminals[i].AltWritePos = 0;
225 gVT_Terminals[i].ViewPos = 0;
226 gVT_Terminals[i].ReadingThread = -1;
227 gVT_Terminals[i].ScrollHeight = 0;
230 VT_int_ChangeMode( &gVT_Terminals[i],
231 TERM_MODE_TEXT, giVT_RealWidth, giVT_RealHeight );
233 gVT_Terminals[i].Name[0] = '0'+i;
234 gVT_Terminals[i].Name[1] = '\0';
235 gVT_Terminals[i].Node.Inode = i;
236 gVT_Terminals[i].Node.ImplPtr = &gVT_Terminals[i];
237 gVT_Terminals[i].Node.NumACLs = 0; // Only root can open virtual terminals
239 gVT_Terminals[i].Node.Read = VT_Read;
240 gVT_Terminals[i].Node.Write = VT_Write;
241 gVT_Terminals[i].Node.IOCtl = VT_Terminal_IOCtl;
242 // Semaphore_Init(&gVT_Terminals[i].InputSemaphore, 0, MAX_INPUT_CHARS8, "VTerm", gVT_Terminals[i].Name);
246 DevFS_AddDevice( &gVT_DrvInfo );
251 // Set kernel output to VT0
252 Debug_SetKTerminal("/Devices/VTerm/0");
254 return MODULE_ERR_OK;
258 * \fn void VT_InitOutput()
259 * \brief Initialise Video Output
263 giVT_OutputDevHandle = VFS_Open(gsVT_OutputDevice, VFS_OPENFLAG_WRITE);
264 if(giVT_OutputDevHandle == -1) {
265 Log_Warning("VTerm", "Oh F**k, I can't open the video device '%s'", gsVT_OutputDevice);
268 VT_SetResolution( giVT_RealWidth, giVT_RealHeight );
270 VT_SetMode( VIDEO_BUFFMT_TEXT );
274 * \fn void VT_InitInput()
275 * \brief Initialises the input
279 giVT_InputDevHandle = VFS_Open(gsVT_InputDevice, VFS_OPENFLAG_READ);
280 if(giVT_InputDevHandle == -1) {
281 Log_Warning("VTerm", "Can't open the input device '%s'", gsVT_InputDevice);
284 VFS_IOCtl(giVT_InputDevHandle, KB_IOCTL_SETCALLBACK, VT_KBCallBack);
288 * \brief Set the video resolution
289 * \param Width New screen width
290 * \param Height New screen height
292 void VT_SetResolution(int Width, int Height)
294 tVideo_IOCtl_Mode mode = {0};
298 // Create the video mode
300 mode.height = Height;
305 VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_FINDMODE, &mode );
307 if( Width != mode.width || Height != mode.height )
310 "Selected resolution (%ix%i is not supported) by the device, using (%ix%i)",
311 giVT_RealWidth, giVT_RealHeight,
312 mode.width, mode.height
315 VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_GETSETMODE, &tmp );
317 // Resize text terminals if needed
318 if( giVT_RealWidth != mode.width || giVT_RealHeight != mode.height )
320 int newBufSize = (giVT_RealWidth/giVT_CharWidth)
321 *(giVT_RealHeight/giVT_CharHeight)
322 *(giVT_Scrollback+1);
324 // Resize the text terminals
325 giVT_RealWidth = mode.width;
326 giVT_RealHeight = mode.height;
327 Log_Debug("VTerm", "Resizing terminals to %ix%i",
328 giVT_RealWidth/giVT_CharWidth, giVT_RealHeight/giVT_CharHeight);
329 for( i = 0; i < NUM_VTS; i ++ )
331 if( gVT_Terminals[i].Mode != TERM_MODE_TEXT ) continue;
333 gVT_Terminals[i].TextWidth = giVT_RealWidth/giVT_CharWidth;
334 gVT_Terminals[i].TextHeight = giVT_RealHeight/giVT_CharHeight;
335 gVT_Terminals[i].ScrollHeight = gVT_Terminals[i].TextHeight;
337 gVT_Terminals[i].Text = realloc(
338 gVT_Terminals[i].Text,
339 newBufSize*sizeof(tVT_Char)
346 * \brief Set video output buffer mode
348 void VT_SetMode(int Mode)
350 VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &Mode );
354 * \fn char *VT_ReadDir(tVFS_Node *Node, int Pos)
355 * \brief Read from the VTerm Directory
357 char *VT_ReadDir(tVFS_Node *Node, int Pos)
359 if(Pos < 0) return NULL;
360 if(Pos >= NUM_VTS) return NULL;
361 return strdup( gVT_Terminals[Pos].Name );
365 * \fn tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
366 * \brief Find an item in the VTerm directory
367 * \param Node Root node
368 * \param Name Name (number) of the terminal
370 tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
374 ENTER("pNode sName", Node, Name);
376 // Open the input and output files if needed
377 if(giVT_OutputDevHandle == -2) VT_InitOutput();
378 if(giVT_InputDevHandle == -2) VT_InitInput();
381 if(Name[0] < '0' || Name[0] > '9' || Name[1] != '\0') {
392 LEAVE('p', &gVT_Terminals[num].Node);
393 return &gVT_Terminals[num].Node;
397 * \fn int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
398 * \brief Control the VTerm Driver
400 int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
405 case DRV_IOCTL_TYPE: return DRV_TYPE_MISC;
406 case DRV_IOCTL_IDENT: memcpy(Data, "VT\0\0", 4); return 0;
407 case DRV_IOCTL_VERSION: return VERSION;
408 case DRV_IOCTL_LOOKUP: return 0;
410 case 4: // Get Video Driver
411 if(Data) strcpy(Data, gsVT_OutputDevice);
412 return strlen(gsVT_OutputDevice);
414 case 5: // Set Video Driver
415 if(!Data) return -EINVAL;
416 if(Threads_GetUID() != 0) return -EACCES;
420 // TODO: Check if the string used is a heap string
422 free(gsVT_OutputDevice);
424 gsVT_OutputDevice = malloc(len+1);
425 strcpy(gsVT_OutputDevice, Data);
427 VFS_Close(giVT_OutputDevHandle);
428 giVT_OutputDevHandle = -1;
437 * \brief Read from a virtual terminal
439 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
443 tVTerm *term = &gVT_Terminals[ Node->Inode ];
444 Uint32 *codepoint_buf = Buffer;
445 Uint32 *codepoint_in;
447 Mutex_Acquire( &term->ReadingLock );
449 // Check current mode
454 VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "VT_Read (UTF-8)");
456 avail = term->InputWrite - term->InputRead;
458 avail += MAX_INPUT_CHARS8;
459 if(avail > Length - pos)
460 avail = Length - pos;
464 ((char*)Buffer)[pos] = term->InputBuffer[term->InputRead];
467 term->InputRead %= MAX_INPUT_CHARS8;
474 VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "VT_Read (UCS-4)");
476 avail = term->InputWrite - term->InputRead;
478 avail += MAX_INPUT_CHARS32;
479 if(avail > Length - pos)
480 avail = Length/4 - pos;
482 codepoint_in = (void*)term->InputBuffer;
483 codepoint_buf = Buffer;
487 codepoint_buf[pos] = codepoint_in[term->InputRead];
490 term->InputRead %= MAX_INPUT_CHARS32;
496 // Mark none avaliable if buffer empty
497 if( term->InputRead == term->InputWrite )
498 VFS_MarkAvaliable(&term->Node, 0);
500 term->ReadingThread = -1;
502 Mutex_Release( &term->ReadingLock );
508 * \fn Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
509 * \brief Write to a virtual terminal
511 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
513 tVTerm *term = &gVT_Terminals[ Node->Inode ];
521 VT_int_PutString(term, Buffer, Length);
527 size = term->Width*term->Height*4;
528 if( Offset > size ) {
529 Log_Notice("VTerm", "VT_Write: Offset (0x%llx) > FBSize (0x%x)",
533 if( Offset + Length > size ) {
534 Log_Notice("VTerm", "VT_Write: Offset+Length (0x%llx) > FBSize (0x%x)",
535 Offset+Length, size);
536 Length = size - Offset;
539 // Copy to the local cache
540 memcpy( (void*)((Uint)term->Buffer + (Uint)Offset), Buffer, Length );
542 // Update screen if needed
543 if( Node->Inode == giVT_CurrentTerminal )
545 // Fill entire screen?
546 if( giVT_RealWidth > term->Width || giVT_RealHeight > term->Height )
548 // No? :( Well, just center it
550 x = Offset/4; y = x / term->Width; x %= term->Width;
551 w = Length/4+x; h = w / term->Width; w %= term->Width;
553 x += (giVT_RealWidth - term->Width) / 2;
554 y += (giVT_RealHeight - term->Height) / 2;
557 VFS_WriteAt( giVT_OutputDevHandle,
558 (x + y * giVT_RealWidth)*4,
562 Buffer = (void*)( (Uint)Buffer + term->Width*4 );
568 return VFS_WriteAt( giVT_OutputDevHandle, Offset, Length, Buffer );
571 // Just pass on (for now)
572 // TODO: Handle locally too to ensure no information is lost on
573 // VT Switch (and to isolate terminals from each other)
574 case TERM_MODE_2DACCEL:
575 //case TERM_MODE_3DACCEL:
576 if( Node->Inode == giVT_CurrentTerminal )
578 VFS_Write( giVT_OutputDevHandle, Length, Buffer );
587 * \fn int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
588 * \brief Call an IO Control on a virtual terminal
590 int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
594 tVTerm *term = Node->ImplPtr;
595 ENTER("pNode iId pData", Node, Id, Data);
597 if(Id >= DRV_IOCTL_LOOKUP) {
598 // Only root can fiddle with graphics modes
599 // TODO: Remove this and replace with user ownership
600 if( Threads_GetUID() != 0 ) return -1;
607 LEAVE('i', DRV_TYPE_TERMINAL);
608 return DRV_TYPE_TERMINAL;
609 case DRV_IOCTL_IDENT:
610 memcpy(Data, "VT\0\0", 4);
613 case DRV_IOCTL_VERSION:
616 case DRV_IOCTL_LOOKUP:
620 // Get/Set the mode (and apply any changes)
621 case TERM_IOCTL_MODETYPE:
624 if( CheckMem(Data, sizeof(int)) == 0 ) {
628 Log_Log("VTerm", "VTerm %i mode set to %i", (int)Node->Inode, *iData);
630 // Update mode if needed
631 if( term->Mode != *iData
635 // Adjust for text mode
636 if( *iData == TERM_MODE_TEXT ) {
637 term->NewHeight *= giVT_CharHeight;
638 term->NewWidth *= giVT_CharWidth;
640 // Fill unchanged dimensions
641 if(term->NewHeight == 0) term->NewHeight = term->Height;
642 if(term->NewWidth == 0) term->NewWidth = term->Width;
644 VT_int_ChangeMode(term, *iData, term->NewWidth, term->NewHeight);
645 // Clear unapplied dimensions
650 // Update the screen dimensions
651 if(Node->Inode == giVT_CurrentTerminal)
652 VT_SetTerminal( giVT_CurrentTerminal );
654 LEAVE('i', term->Mode);
657 // Get/set the terminal width
658 case TERM_IOCTL_WIDTH:
660 if( CheckMem(Data, sizeof(int)) == 0 ) {
664 term->NewWidth = *iData;
667 ret = term->NewWidth;
668 else if( term->Mode == TERM_MODE_TEXT )
669 ret = term->TextWidth;
675 // Get/set the terminal height
676 case TERM_IOCTL_HEIGHT:
678 if( CheckMem(Data, sizeof(int)) == 0 ) {
682 term->NewHeight = *iData;
684 if( term->NewHeight )
685 ret = term->NewHeight;
686 else if( term->Mode == TERM_MODE_TEXT )
687 ret = term->TextHeight;
693 case TERM_IOCTL_FORCESHOW:
694 Log_Log("VTerm", "Thread %i forced VTerm %i to be shown",
695 Threads_GetTID(), (int)Node->Inode);
696 VT_SetTerminal( Node->Inode );
700 case TERM_IOCTL_GETCURSOR:
701 ret = (term->Flags & VT_FLAG_ALTBUF) ? term->AltWritePos : term->WritePos-term->ViewPos;
710 * \fn void VT_SetTerminal(int ID)
711 * \brief Set the current terminal
713 void VT_SetTerminal(int ID)
715 // Update current terminal ID
716 Log_Log("VTerm", "Changed terminal from %i to %i", giVT_CurrentTerminal, ID);
717 giVT_CurrentTerminal = ID;
718 gpVT_CurTerm = &gVT_Terminals[ID];
721 if( gpVT_CurTerm->Mode == TERM_MODE_TEXT && !(gpVT_CurTerm->Flags & VT_FLAG_HIDECSR) )
723 tVideo_IOCtl_Pos pos;
724 int offset = (gpVT_CurTerm->Flags & VT_FLAG_ALTBUF) ? gpVT_CurTerm->AltWritePos : gpVT_CurTerm->WritePos - gpVT_CurTerm->ViewPos;
725 pos.x = offset % gpVT_CurTerm->TextWidth;
726 pos.y = offset / gpVT_CurTerm->TextWidth;
727 if( 0 <= pos.y && pos.y < gpVT_CurTerm->TextHeight )
728 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
731 if( gpVT_CurTerm->Mode == TERM_MODE_TEXT )
732 VT_SetMode( VIDEO_BUFFMT_TEXT );
734 VT_SetMode( VIDEO_BUFFMT_FRAMEBUFFER );
737 VT_int_UpdateScreen( &gVT_Terminals[ ID ], 1 );
741 * \fn void VT_KBCallBack(Uint32 Codepoint)
742 * \brief Called on keyboard interrupt
743 * \param Codepoint Pseudo-UTF32 character
745 * Handles a key press and sends the key code to the user's buffer.
746 * If the code creates a kernel-magic sequence, it is not passed to the
747 * user and is handled in-kernel.
749 void VT_KBCallBack(Uint32 Codepoint)
751 tVTerm *term = gpVT_CurTerm;
753 // How the hell did we get a codepoint of zero?
754 if(Codepoint == 0) return;
757 if( Codepoint & 0x80000000 )
759 Codepoint &= 0x7FFFFFFF;
762 case KEY_LALT: gbVT_AltDown &= ~1; break;
763 case KEY_RALT: gbVT_AltDown &= ~2; break;
764 case KEY_LCTRL: gbVT_CtrlDown &= ~1; break;
765 case KEY_RCTRL: gbVT_CtrlDown &= ~2; break;
772 case KEY_LALT: gbVT_AltDown |= 1; break;
773 case KEY_RALT: gbVT_AltDown |= 2; break;
774 case KEY_LCTRL: gbVT_CtrlDown |= 1; break;
775 case KEY_RCTRL: gbVT_CtrlDown |= 2; break;
778 if(!gbVT_AltDown || !gbVT_CtrlDown)
782 case KEY_F1: VT_SetTerminal(0); return;
783 case KEY_F2: VT_SetTerminal(1); return;
784 case KEY_F3: VT_SetTerminal(2); return;
785 case KEY_F4: VT_SetTerminal(3); return;
786 case KEY_F5: VT_SetTerminal(4); return;
787 case KEY_F6: VT_SetTerminal(5); return;
788 case KEY_F7: VT_SetTerminal(6); return;
789 case KEY_F8: VT_SetTerminal(7); return;
790 case KEY_F9: VT_SetTerminal(8); return;
791 case KEY_F10: VT_SetTerminal(9); return;
792 case KEY_F11: VT_SetTerminal(10); return;
793 case KEY_F12: VT_SetTerminal(11); return;
796 if( gpVT_CurTerm->Flags & VT_FLAG_ALTBUF )
798 if( gpVT_CurTerm->ViewPos > gpVT_CurTerm->Width )
799 gpVT_CurTerm->ViewPos -= gpVT_CurTerm->Width;
801 gpVT_CurTerm->ViewPos = 0;
804 if( gpVT_CurTerm->Flags & VT_FLAG_ALTBUF )
806 if( gpVT_CurTerm->ViewPos < gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback) )
807 gpVT_CurTerm->ViewPos += gpVT_CurTerm->Width;
809 gpVT_CurTerm->ViewPos = gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback);
815 if(term->Mode == TERM_MODE_TEXT)
820 // Ignore Modifer Keys
821 if(Codepoint > KEY_MODIFIERS) return;
823 // Get UTF-8/ANSI Encoding
827 buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'D';
831 buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'C';
835 buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'A';
839 buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'B';
844 //buf[0] = '\x1B'; buf[1] = '['; buf[2] = '5'; // Some overline also
845 //len = 4; // Commented out until I'm sure
852 // Attempt to encode in UTF-8
854 len = WriteUTF8( buf, Codepoint );
856 Warning("Codepoint (%x) is unrepresentable in UTF-8", Codepoint);
862 // Unprintable / Don't Pass
867 // Handle meta characters
868 if( !(term->Flags & VT_FLAG_RAWIN) )
880 if( MAX_INPUT_CHARS8 - term->InputWrite >= len )
881 memcpy( &term->InputBuffer[term->InputWrite], buf, len );
883 memcpy( &term->InputBuffer[term->InputWrite], buf, MAX_INPUT_CHARS8 - term->InputWrite );
884 memcpy( &term->InputBuffer[0], buf, len - (MAX_INPUT_CHARS8 - term->InputWrite) );
886 // Roll the buffer over
887 term->InputWrite += len;
888 term->InputWrite %= MAX_INPUT_CHARS8;
889 if( (term->InputWrite - term->InputRead + MAX_INPUT_CHARS8)%MAX_INPUT_CHARS8 < len ) {
890 term->InputRead = term->InputWrite + 1;
891 term->InputRead %= MAX_INPUT_CHARS8;
896 // Encode the raw UTF-32 Key
897 Uint32 *raw_in = (void*)term->InputBuffer;
898 raw_in[ term->InputWrite ] = Codepoint;
900 term->InputWrite %= MAX_INPUT_CHARS32;
901 if(term->InputRead == term->InputWrite) {
903 term->InputRead %= MAX_INPUT_CHARS32;
907 VFS_MarkAvaliable(&term->Node, 1);
911 * \fn void VT_int_ClearLine(tVTerm *Term, int Num)
912 * \brief Clears a line in a virtual terminal
914 void VT_int_ClearLine(tVTerm *Term, int Num)
919 if( Num < 0 || Num >= Term->TextHeight * (giVT_Scrollback + 1) ) return ;
921 cell = (Term->Flags & VT_FLAG_ALTBUF) ? Term->AltBuf : Term->Text;
922 cell = &cell[ Num*Term->TextWidth ];
924 for( i = Term->TextWidth; i--; )
927 cell[ i ].Colour = Term->CurColour;
932 * \fn int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
933 * \brief Parses a VT100 Escape code
935 int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
940 int args[6] = {0,0,0,0};
941 int bQuestionMark = 0;
953 if( '0' <= c && c <= '9' )
956 if(c == ';') c = Buffer[j++];
957 while('0' <= c && c <= '9') {
967 if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
973 // DEC Private Mode Set
979 VT_int_ToggleAltBuffer(Term, 1);
988 VT_int_ToggleAltBuffer(Term, 0);
993 Log_Warning("VTerm", "Unknown control sequence '\\x1B[?%c'", c);
1007 if(argc == 1) tmp *= args[0];
1008 if( Term->Flags & VT_FLAG_ALTBUF )
1010 if( (Term->AltWritePos + tmp) % Term->TextWidth == 0 ) {
1011 Term->AltWritePos -= Term->AltWritePos % Term->TextWidth;
1012 Term->AltWritePos += Term->TextWidth - 1;
1015 Term->AltWritePos += tmp;
1019 if( (Term->WritePos + tmp) % Term->TextWidth == 0 ) {
1020 Term->WritePos -= Term->WritePos % Term->TextWidth;
1021 Term->WritePos += Term->TextWidth - 1;
1024 Term->WritePos += tmp;
1032 case 0: // Erase below
1034 case 1: // Erase above
1036 case 2: // Erase all
1037 if( Term->Flags & VT_FLAG_ALTBUF )
1039 int i = Term->TextHeight;
1040 while( i-- ) VT_int_ClearLine(Term, i);
1041 Term->AltWritePos = 0;
1042 VT_int_UpdateScreen(Term, 1);
1046 int i = Term->TextHeight * (giVT_Scrollback + 1);
1047 while( i-- ) VT_int_ClearLine(Term, i);
1050 VT_int_UpdateScreen(Term, 1);
1060 case 0: // Erase to right
1061 if( Term->Flags & VT_FLAG_ALTBUF )
1064 max = Term->Width - Term->AltWritePos % Term->Width;
1065 for( i = 0; i < max; i ++ )
1066 Term->AltBuf[Term->AltWritePos+i].Ch = 0;
1071 max = Term->Width - Term->WritePos % Term->Width;
1072 for( i = 0; i < max; i ++ )
1073 Term->Text[Term->WritePos+i].Ch = 0;
1075 VT_int_UpdateScreen(Term, 0);
1077 case 1: // Erase to left
1078 if( Term->Flags & VT_FLAG_ALTBUF )
1080 int i = Term->AltWritePos % Term->Width;
1082 Term->AltBuf[Term->AltWritePos++].Ch = 0;
1086 int i = Term->WritePos % Term->Width;
1088 Term->Text[Term->WritePos++].Ch = 0;
1090 VT_int_UpdateScreen(Term, 0);
1092 case 2: // Erase all
1093 if( Term->Flags & VT_FLAG_ALTBUF )
1095 VT_int_ClearLine(Term, Term->AltWritePos / Term->Width);
1099 VT_int_ClearLine(Term, Term->WritePos / Term->Width);
1101 VT_int_UpdateScreen(Term, 0);
1106 // Set cursor position
1108 if( Term->Flags & VT_FLAG_ALTBUF )
1109 Term->AltWritePos = args[0] + args[1]*Term->TextWidth;
1111 Term->WritePos = args[0] + args[1]*Term->TextWidth;
1112 //Log_Debug("VTerm", "args = {%i, %i}", args[0], args[1]);
1115 // Scroll up `n` lines
1118 // Scroll down `n` lines
1120 if(argc == 1) tmp *= args[0];
1121 if( Term->Flags & VT_FLAG_ALTBUF )
1122 VT_int_ScrollText(Term, tmp);
1125 if(Term->ViewPos/Term->TextWidth + tmp < 0)
1127 if(Term->ViewPos/Term->TextWidth + tmp > Term->TextHeight * (giVT_Scrollback + 1))
1130 Term->ViewPos += Term->TextWidth*tmp;
1139 if( 0 <= args[argc] && args[argc] <= 8)
1143 case 0: Term->CurColour = DEFAULT_COLOUR; break; // Reset
1144 case 1: Term->CurColour |= 0x80000000; break; // Bright
1145 case 2: Term->CurColour &= ~0x80000000; break; // Dim
1148 // Foreground Colour
1149 else if(30 <= args[argc] && args[argc] <= 37) {
1150 Term->CurColour &= 0xF000FFFF;
1151 Term->CurColour |= (Uint32)caVT100Colours[ args[argc]-30+(Term->CurColour>>28) ] << 16;
1153 // Background Colour
1154 else if(40 <= args[argc] && args[argc] <= 47) {
1155 Term->CurColour &= 0xFFFF8000;
1156 Term->CurColour |= caVT100Colours[ args[argc]-40+((Term->CurColour>>12)&15) ];
1161 // Set scrolling region
1163 if( argc != 2 ) break;
1164 Term->ScrollTop = args[0];
1165 Term->ScrollHeight = args[1] - args[0];
1169 Log_Warning("VTerm", "Unknown control sequence '\\x1B[%c'", c);
1179 //Log_Debug("VTerm", "j = %i, Buffer = '%s'", j, Buffer);
1184 * \fn void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
1185 * \brief Print a string to the Virtual Terminal
1187 void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
1193 for( i = 0; i < Count; i++ )
1195 // Handle escape sequences
1196 if( Buffer[i] == 0x1B )
1199 i += VT_int_ParseEscape(Term, (char*)&Buffer[i]) - 1;
1203 // Fast check for non UTF-8
1204 if( Buffer[i] < 128 ) // Plain ASCII
1205 VT_int_PutChar(Term, Buffer[i]);
1207 i += ReadUTF8(&Buffer[i], &val) - 1;
1208 VT_int_PutChar(Term, val);
1212 VT_int_UpdateScreen( Term, 0 );
1215 if( Term == gpVT_CurTerm && !(Term->Flags & VT_FLAG_HIDECSR) )
1217 tVideo_IOCtl_Pos pos;
1218 int offset = (gpVT_CurTerm->Flags & VT_FLAG_ALTBUF) ? gpVT_CurTerm->AltWritePos : gpVT_CurTerm->WritePos - gpVT_CurTerm->ViewPos;
1219 pos.x = offset % gpVT_CurTerm->TextWidth;
1220 pos.y = offset / gpVT_CurTerm->TextWidth;
1221 if( 0 <= pos.y && pos.y < gpVT_CurTerm->TextHeight )
1222 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
1227 * \fn void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
1228 * \brief Write a single character to a VTerm
1230 void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
1236 if(Term->Flags & VT_FLAG_ALTBUF) {
1237 buffer = Term->AltBuf;
1238 write_pos = Term->AltWritePos;
1241 buffer = Term->Text;
1242 write_pos = Term->WritePos;
1247 case '\0': return; // Ignore NULL byte
1249 VT_int_UpdateScreen( Term, 0 ); // Update the line before newlining
1250 write_pos += Term->TextWidth;
1252 write_pos -= write_pos % Term->TextWidth;
1255 case '\t': { int tmp = write_pos / Term->TextWidth;
1256 write_pos %= Term->TextWidth;
1258 buffer[ write_pos ].Ch = '\0';
1259 buffer[ write_pos ].Colour = Term->CurColour;
1261 } while(write_pos & 7);
1262 write_pos += tmp * Term->TextWidth;
1266 // Backspace is invalid at Offset 0
1267 if(write_pos == 0) break;
1271 if(buffer[ write_pos ].Ch != '\0') {
1272 buffer[ write_pos ].Ch = 0;
1273 buffer[ write_pos ].Colour = Term->CurColour;
1277 i = 7; // Limit it to 8
1279 buffer[ write_pos ].Ch = 0;
1280 buffer[ write_pos ].Colour = Term->CurColour;
1282 } while(write_pos && i-- && buffer[ write_pos ].Ch == '\0');
1283 if(buffer[ write_pos ].Ch != '\0')
1288 buffer[ write_pos ].Ch = Ch;
1289 buffer[ write_pos ].Colour = Term->CurColour;
1290 // Update the line before wrapping
1291 if( (write_pos + 1) % Term->TextWidth == 0 )
1292 VT_int_UpdateScreen( Term, 0 );
1297 if(Term->Flags & VT_FLAG_ALTBUF)
1299 Term->AltBuf = buffer;
1300 Term->AltWritePos = write_pos;
1302 if(Term->AltWritePos >= Term->TextWidth*Term->TextHeight)
1304 Term->AltWritePos -= Term->TextWidth;
1305 VT_int_ScrollText(Term, 1);
1311 Term->Text = buffer;
1312 Term->WritePos = write_pos;
1314 // - Check if we need to scroll the entire scrollback buffer
1315 if(Term->WritePos >= Term->TextWidth*Term->TextHeight*(giVT_Scrollback+1))
1319 // Update previous line
1320 Term->WritePos -= Term->TextWidth;
1321 VT_int_UpdateScreen( Term, 0 );
1323 // Update view position
1324 base = Term->TextWidth*Term->TextHeight*(giVT_Scrollback);
1325 if(Term->ViewPos < base)
1326 Term->ViewPos += Term->Width;
1327 if(Term->ViewPos > base)
1328 Term->ViewPos = base;
1330 VT_int_ScrollText(Term, 1);
1332 // Ok, so we only need to scroll the screen
1333 else if(Term->WritePos >= Term->ViewPos + Term->TextWidth*Term->TextHeight)
1335 // Update the last line
1336 Term->WritePos -= Term->TextWidth;
1337 VT_int_UpdateScreen( Term, 0 );
1338 Term->WritePos += Term->TextWidth;
1340 VT_int_ScrollText(Term, 1);
1342 Term->ViewPos += Term->TextWidth;
1349 void VT_int_ScrollText(tVTerm *Term, int Count)
1352 int height, init_write_pos;
1354 int scroll_top, scroll_height;
1356 // Get buffer pointer and attributes
1357 if( Term->Flags & VT_FLAG_ALTBUF )
1360 height = Term->TextHeight;
1361 init_write_pos = Term->AltWritePos;
1362 scroll_top = Term->ScrollTop;
1363 scroll_height = Term->ScrollHeight;
1368 height = Term->TextHeight*(giVT_Scrollback+1);
1369 init_write_pos = Term->WritePos;
1371 scroll_height = height;
1374 // Scroll text downwards
1380 if(Count > scroll_height) Count = scroll_height;
1381 base = Term->TextWidth*(scroll_top + scroll_height - Count);
1382 len = Term->TextWidth*(scroll_height - Count);
1384 // Scroll terminal cache
1386 &buf[Term->TextWidth*scroll_top],
1387 &buf[Term->TextWidth*(scroll_top+Count)],
1388 len*sizeof(tVT_Char)
1391 for( i = 0; i < Term->TextWidth*Count; i ++ )
1393 buf[ base + i ].Ch = 0;
1394 buf[ base + i ].Colour = Term->CurColour;
1398 VT_int_ScrollFramebuffer( Term, Count );
1399 if( Term->Flags & VT_FLAG_ALTBUF )
1400 Term->AltWritePos = base;
1402 Term->WritePos = Term->ViewPos + Term->TextWidth*(Term->TextHeight - Count);
1403 for( i = 0; i < Count; i ++ )
1405 VT_int_UpdateScreen( Term, 0 );
1406 if( Term->Flags & VT_FLAG_ALTBUF )
1407 Term->AltWritePos += Term->TextWidth;
1409 Term->WritePos += Term->TextWidth;
1415 if(Count > scroll_height) Count = scroll_height;
1417 len = Term->TextWidth*(scroll_height - Count);
1419 // Scroll terminal cache
1421 &buf[Term->TextWidth*(scroll_top+Count)],
1422 &buf[Term->TextWidth*scroll_top],
1423 len*sizeof(tVT_Char)
1425 // Clear preceding rows
1426 for( i = 0; i < Term->TextWidth*Count; i ++ )
1429 buf[ i ].Colour = Term->CurColour;
1432 VT_int_ScrollFramebuffer( Term, -Count );
1433 if( Term->Flags & VT_FLAG_ALTBUF )
1434 Term->AltWritePos = Term->TextWidth*scroll_top;
1436 Term->WritePos = Term->ViewPos;
1437 for( i = 0; i < Count; i ++ )
1439 VT_int_UpdateScreen( Term, 0 );
1440 if( Term->Flags & VT_FLAG_ALTBUF )
1441 Term->AltWritePos += Term->TextWidth;
1443 Term->WritePos += Term->TextWidth;
1447 if( Term->Flags & VT_FLAG_ALTBUF )
1448 Term->AltWritePos = init_write_pos;
1450 Term->WritePos = init_write_pos;
1454 * \fn void VT_int_ScrollFramebuffer( tVTerm *Term, int Count )
1455 * \note Scrolls the framebuffer down by \a Count text lines
1457 void VT_int_ScrollFramebuffer( tVTerm *Term, int Count )
1467 // Only update if this is the current terminal
1468 if( Term != gpVT_CurTerm ) return;
1470 if( Count > Term->ScrollHeight ) Count = Term->ScrollHeight;
1471 if( Count < -Term->ScrollHeight ) Count = -Term->ScrollHeight;
1473 // Switch to 2D Command Stream
1474 tmp = VIDEO_BUFFMT_2DSTREAM;
1475 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp);
1477 // BLIT to 0,0 from 0,giVT_CharHeight
1478 buf.Op = VIDEO_2DOP_BLIT;
1479 buf.SrcX = 0; buf.DstX = 0;
1480 // TODO: Don't assume character dimensions
1481 buf.W = Term->TextWidth * giVT_CharWidth;
1484 buf.SrcY = (Term->ScrollTop+Count) * giVT_CharHeight;
1485 buf.DstY = Term->ScrollTop * giVT_CharHeight;
1487 else // Scroll up, move text down
1490 buf.SrcY = Term->ScrollTop * giVT_CharHeight;
1491 buf.DstY = (Term->ScrollTop+Count) * giVT_CharHeight;
1493 buf.H = (Term->ScrollHeight-Count) * giVT_CharHeight;
1494 VFS_WriteAt(giVT_OutputDevHandle, 0, sizeof(buf), &buf);
1496 // Restore old mode (this function is only called during text mode)
1497 tmp = VIDEO_BUFFMT_TEXT;
1498 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp);
1502 * \fn void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
1503 * \brief Updates the video framebuffer
1505 void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
1508 int view_pos, write_pos;
1509 // Only update if this is the current terminal
1510 if( Term != gpVT_CurTerm ) return;
1512 switch( Term->Mode )
1514 case TERM_MODE_TEXT:
1515 view_pos = (Term->Flags & VT_FLAG_ALTBUF) ? 0 : Term->ViewPos;
1516 write_pos = (Term->Flags & VT_FLAG_ALTBUF) ? Term->AltWritePos : Term->WritePos;
1517 buffer = (Term->Flags & VT_FLAG_ALTBUF) ? Term->AltBuf : Term->Text;
1518 // Re copy the entire screen?
1521 giVT_OutputDevHandle,
1523 Term->TextWidth*Term->TextHeight*sizeof(tVT_Char),
1527 // Only copy the current line
1529 int ofs = write_pos - write_pos % Term->TextWidth;
1531 giVT_OutputDevHandle,
1532 (ofs - view_pos)*sizeof(tVT_Char),
1533 Term->TextWidth*sizeof(tVT_Char),
1540 giVT_OutputDevHandle,
1542 Term->Width*Term->Height*sizeof(Uint32),
1550 * \brief Update the screen mode
1551 * \param Term Terminal to update
1552 * \param NewMode New mode to set
1553 * \param NewWidth New framebuffer width
1554 * \param NewHeight New framebuffer height
1556 void VT_int_ChangeMode(tVTerm *Term, int NewMode, int NewWidth, int NewHeight)
1559 // TODO: Increase RealWidth/RealHeight when this happens
1560 if(NewWidth > giVT_RealWidth) NewWidth = giVT_RealWidth;
1561 if(NewHeight > giVT_RealHeight) NewHeight = giVT_RealHeight;
1563 Term->Mode = NewMode;
1565 if(NewWidth != Term->Width || NewHeight != Term->Height)
1567 int oldW = Term->Width;
1568 int oldTW = Term->TextWidth;
1569 int oldH = Term->Height;
1570 int oldTH = Term->TextHeight;
1571 tVT_Char *oldTBuf = Term->Text;
1572 Uint32 *oldFB = Term->Buffer;
1574 // Calculate new dimensions
1575 Term->Width = NewWidth;
1576 Term->Height = NewHeight;
1577 Term->TextWidth = NewWidth / giVT_CharWidth;
1578 Term->TextHeight = NewHeight / giVT_CharHeight;
1579 Term->ScrollHeight = Term->TextHeight - (oldTH - Term->ScrollHeight) - Term->ScrollTop;
1581 // Allocate new buffers
1583 Term->Text = calloc(
1584 Term->TextWidth * Term->TextHeight * (giVT_Scrollback+1),
1589 w = (oldTW > Term->TextWidth) ? Term->TextWidth : oldTW;
1590 h = (oldTH > Term->TextHeight) ? Term->TextHeight : oldTH;
1591 h *= giVT_Scrollback + 1;
1592 for( i = 0; i < h; i ++ )
1595 &Term->Text[i*Term->TextWidth],
1604 Term->AltBuf = realloc(
1606 Term->TextWidth * Term->TextHeight * sizeof(tVT_Char)
1610 Term->Buffer = calloc( Term->Width * Term->Height, sizeof(Uint32) );
1613 w = (oldW > Term->Width) ? Term->Width : oldW;
1614 h = (oldH > Term->Height) ? Term->Height : oldH;
1615 for( i = 0; i < h; i ++ )
1618 &Term->Buffer[i*Term->Width],
1631 case TERM_MODE_TEXT:
1632 Log_Log("VTerm", "Set VT %p to text mode (%ix%i)",
1633 Term, Term->TextWidth, Term->TextHeight);
1636 Log_Log("VTerm", "Set VT %p to framebuffer mode (%ix%i)",
1637 Term, Term->Width, Term->Height);
1639 //case TERM_MODE_2DACCEL:
1640 //case TERM_MODE_3DACCEL:
1646 void VT_int_ToggleAltBuffer(tVTerm *Term, int Enabled)
1649 Term->Flags |= VT_FLAG_ALTBUF;
1651 Term->Flags &= ~VT_FLAG_ALTBUF;
1652 VT_int_UpdateScreen(Term, 1);
1658 #define MONOSPACE_FONT 10816
1660 #if MONOSPACE_FONT == 10808 // 8x8
1661 # include "vterm_font_8x8.h"
1662 #elif MONOSPACE_FONT == 10816 // 8x16
1663 # include "vterm_font_8x16.h"
1666 // === PROTOTYPES ===
1667 Uint8 *VT_Font_GetChar(Uint32 Codepoint);
1670 int giVT_CharWidth = FONT_WIDTH;
1671 int giVT_CharHeight = FONT_HEIGHT;
1675 * \brief Render a font character
1677 void VT_Font_Render(Uint32 Codepoint, void *Buffer, int Depth, int Pitch, Uint32 BGC, Uint32 FGC)
1685 Uint8 *buf = Buffer;
1687 font = VT_Font_GetChar(Codepoint);
1689 for(y = 0; y < FONT_HEIGHT; y ++)
1691 for(x = 0; x < FONT_WIDTH; x ++)
1693 if(*font & (1 << (FONT_WIDTH-x-1)))
1698 buf = (void*)( (tVAddr)buf + Pitch );
1703 else if( Depth <= 16 )
1705 Uint16 *buf = Buffer;
1707 font = VT_Font_GetChar(Codepoint);
1709 for(y = 0; y < FONT_HEIGHT; y ++)
1711 for(x = 0; x < FONT_WIDTH; x ++)
1713 if(*font & (1 << (FONT_WIDTH-x-1)))
1718 buf = (void*)( (tVAddr)buf + Pitch );
1723 // - Special handling to not overwrite the next pixel
1724 //TODO: Endian issues here
1725 else if( Depth == 24 )
1727 Uint8 *buf = Buffer;
1728 Uint8 bg_r = (BGC >> 16) & 0xFF;
1729 Uint8 bg_g = (BGC >> 8) & 0xFF;
1730 Uint8 bg_b = (BGC >> 0) & 0xFF;
1731 Uint8 fg_r = (FGC >> 16) & 0xFF;
1732 Uint8 fg_g = (FGC >> 8) & 0xFF;
1733 Uint8 fg_b = (FGC >> 0) & 0xFF;
1735 font = VT_Font_GetChar(Codepoint);
1737 for(y = 0; y < FONT_HEIGHT; y ++)
1739 for(x = 0; x < FONT_WIDTH; x ++)
1743 if(*font & (1 << (FONT_WIDTH-x-1))) {
1744 r = fg_r; g = fg_g; b = fg_b;
1747 r = bg_r; g = bg_g; b = bg_b;
1753 buf = (void*)( (tVAddr)buf + Pitch );
1757 // 32-bpp colour (nice and easy)
1758 else if( Depth == 32 )
1760 Uint32 *buf = Buffer;
1762 font = VT_Font_GetChar(Codepoint);
1764 for(y = 0; y < FONT_HEIGHT; y ++)
1766 for(x = 0; x < FONT_WIDTH; x ++)
1768 if(*font & (1 << (FONT_WIDTH-x-1)))
1773 buf = (Uint32*)( (tVAddr)buf + Pitch );
1780 * \fn Uint32 VT_Colour12to24(Uint16 Col12)
1781 * \brief Converts a 12-bit colour into 24 bits
1783 Uint32 VT_Colour12to24(Uint16 Col12)
1788 ret = (tmp << 0) | (tmp << 4);
1789 tmp = (Col12 & 0xF0) >> 4;
1790 ret |= (tmp << 8) | (tmp << 12);
1791 tmp = (Col12 & 0xF00) >> 8;
1792 ret |= (tmp << 16) | (tmp << 20);
1796 * \brief Converts a 12-bit colour into 15 bits
1798 Uint16 VT_Colour12to15(Uint16 Col12)
1803 ret = (tmp << 1) | (tmp & 1);
1804 tmp = (Col12 & 0xF0) >> 4;
1805 ret |= ( (tmp << 1) | (tmp & 1) ) << 5;
1806 tmp = (Col12 & 0xF00) >> 8;
1807 ret |= ( (tmp << 1) | (tmp & 1) ) << 10;
1812 * \brief Converts a 12-bit colour into any other depth
1813 * \param Col12 12-bit source colour
1814 * \param Depth Desired bit deptj
1815 * \note Green then blue get the extra avaliable bits (16:5-6-5, 14:4-5-5)
1817 Uint32 VT_Colour12toN(Uint16 Col12, int Depth)
1821 int rSize, gSize, bSize;
1824 if( Depth == 24 ) return VT_Colour12to24(Col12);
1825 if( Depth == 15 ) return VT_Colour12to15(Col12);
1826 // - 32 is a special case, it's usually 24-bit colour with an unused byte
1827 if( Depth == 32 ) return VT_Colour12to24(Col12);
1830 if( Depth < 8 ) return 0;
1831 if( Depth > 32 ) return 0;
1834 g = (Col12 & 0xF0) >> 4;
1835 b = (Col12 & 0xF00) >> 8;
1837 rSize = gSize = bSize = Depth / 3;
1838 if( rSize + gSize + bSize < Depth ) // Depth % 3 == 1
1840 if( rSize + gSize + bSize < Depth ) // Depth % 3 == 2
1844 r <<= rSize - 4; g <<= gSize - 4; b <<= bSize - 4;
1845 // Fill with the lowest bit
1846 if( Col12 & 0x001 ) r |= (1 << (rSize - 4)) - 1;
1847 if( Col12 & 0x010 ) r |= (1 << (gSize - 4)) - 1;
1848 if( Col12 & 0x100 ) r |= (1 << (bSize - 4)) - 1;
1853 ret |= b << (rSize + gSize);
1859 * \fn Uint8 *VT_Font_GetChar(Uint32 Codepoint)
1860 * \brief Gets an index into the font array given a Unicode Codepoint
1861 * \note See http://en.wikipedia.org/wiki/CP437
1863 Uint8 *VT_Font_GetChar(Uint32 Codepoint)
1867 return &VTermFont[Codepoint*FONT_HEIGHT];
1870 case 0xC7: index = 128; break; // Ç
1871 case 0xFC: index = 129; break; // ü
1872 case 0xE9: index = 130; break; // é
1873 case 0xE2: index = 131; break; // â
1874 case 0xE4: index = 132; break; // ä
1875 case 0xE0: index = 133; break; // à
1876 case 0xE5: index = 134; break; // å
1877 case 0xE7: index = 135; break; // ç
1878 case 0xEA: index = 136; break; // ê
1879 case 0xEB: index = 137; break; // ë
1880 case 0xE8: index = 138; break; // è
1881 case 0xEF: index = 139; break; // ï
1882 case 0xEE: index = 140; break; // î
1883 case 0xEC: index = 141; break; // ì
1884 case 0xC4: index = 142; break; // Ä
1885 case 0xC5: index = 143; break; // Å
1888 return &VTermFont[index*FONT_HEIGHT];
1891 EXPORTAS(&giVT_CharWidth, giVT_CharWidth);
1892 EXPORTAS(&giVT_CharHeight, giVT_CharHeight);
1893 EXPORT(VT_Font_Render);
1894 EXPORT(VT_Colour12to24);