Kernel/VTerm - Disabled cursor unless the terminal has a read blocking
[tpg/acess2.git] / Kernel / drv / vterm.c
1 /*
2  * Acess2 Virtual Terminal Driver
3  */
4 #define DEBUG   0
5 #include <acess.h>
6 #include <fs_devfs.h>
7 #include <modules.h>
8 #include <api_drv_video.h>
9 #include <api_drv_keyboard.h>
10 #include <api_drv_terminal.h>
11 #include <errno.h>
12 #include <semaphore.h>
13
14 // === CONSTANTS ===
15 #define VERSION ((0<<8)|(50))
16
17 #define NUM_VTS 8
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))
29
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
34
35 enum eVT_InModes {
36         VT_INMODE_TEXT8,        // UTF-8 Text Mode (VT100/xterm Emulation)
37         VT_INMODE_TEXT32,       // UTF-32 Text Mode (Acess Native)
38         NUM_VT_INMODES
39 };
40
41 // === TYPES ===
42 typedef struct {
43          int    Mode;   //!< Current Mode (see ::eTplTerminal_Modes)
44          int    Flags;  //!< Flags (see VT_FLAG_*)
45         
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
52         
53         Uint32  CurColour;      //!< Current Text Colour
54         
55          int    ViewPos;        //!< View Buffer Offset (Text Only)
56          int    WritePos;       //!< Write Buffer Offset (Text Only)
57         tVT_Char        *Text;
58         
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
63         
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;
70         
71         Uint32          *Buffer;
72         
73         char    Name[2];        //!< Name of the terminal
74         tVFS_Node       Node;
75 } tVTerm;
76
77 // === IMPORTS ===
78 extern void     Debug_SetKTerminal(const char *File);
79
80 // === PROTOTYPES ===
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);
103
104 // === CONSTANTS ===
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
110         };
111
112 // === GLOBALS ===
113 MODULE_DEFINE(0, VERSION, VTerm, VT_Install, NULL, DEFAULT_INPUT, NULL);
114 tDevFS_Driver   gVT_DrvInfo = {
115         NULL, "VTerm",
116         {
117         .Flags = VFS_FFLAG_DIRECTORY,
118         .Size = NUM_VTS,
119         .Inode = -1,
120         .NumACLs = 0,
121         .ReadDir = VT_ReadDir,
122         .FindDir = VT_FindDir,
123         .IOCtl = VT_Root_IOCtl
124         }
125 };
126 // --- Terminals ---
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;
143
144 // === CODE ===
145 /**
146  * \fn int VT_Install(char **Arguments)
147  * \brief Installs the Virtual Terminal Driver
148  */
149 int VT_Install(char **Arguments)
150 {
151          int    i;
152         
153         // Scan Arguments
154         if(Arguments)
155         {
156                 char    **args;
157                 const char      *arg;
158                 for(args = Arguments; (arg = *args); args++ )
159                 {
160                         char    data[strlen(arg)+1];
161                         char    *opt = data;
162                         char    *val;
163                         
164                         val = strchr(arg, '=');
165                         strcpy(data, arg);
166                         if( val ) {
167                                 data[ val - arg ] = '\0';
168                                 val ++;
169                         }
170                         Log_Debug("VTerm", "Argument '%s'", arg);
171                         
172                         if( strcmp(opt, "Video") == 0 ) {
173                                 if( !gsVT_OutputDevice )
174                                         gsVT_OutputDevice = strdup(val);
175                         }
176                         else if( strcmp(opt, "Input") == 0 ) {
177                                 if( !gsVT_InputDevice )
178                                         gsVT_InputDevice = strdup(val);
179                         }
180                         else if( strcmp(opt, "Width") == 0 ) {
181                                 giVT_RealWidth = atoi( val );
182                         }
183                         else if( strcmp(opt, "Height") == 0 ) {
184                                 giVT_RealHeight = atoi( val );
185                         }
186                         else if( strcmp(opt, "Scrollback") == 0 ) {
187                                 giVT_Scrollback = atoi( val );
188                         }
189                 }
190         }
191         
192         // Apply Defaults
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;
196         if( Module_EnsureLoaded( gsVT_OutputDevice ) ) {
197                 Log_Error("VTerm", "Fallback video '%s' is not avaliable, giving up", FALLBACK_OUTPUT);
198                 return MODULE_ERR_MISC;
199         }
200         
201         if(!gsVT_InputDevice)   gsVT_InputDevice = (char*)DEFAULT_INPUT;
202         else if( Module_EnsureLoaded( gsVT_InputDevice ) )      gsVT_InputDevice = (char*)DEFAULT_INPUT;
203         
204         // Create device paths
205         {
206                 char    *tmp;
207                 tmp = malloc( 9 + strlen(gsVT_OutputDevice) + 1 );
208                 strcpy(tmp, "/Devices/");
209                 strcpy(&tmp[9], gsVT_OutputDevice);
210                 gsVT_OutputDevice = tmp;
211
212                 tmp = malloc( 9 + strlen(gsVT_InputDevice) + 1 );
213                 strcpy(tmp, "/Devices/");
214                 strcpy(&tmp[9], gsVT_InputDevice);
215                 gsVT_InputDevice = tmp;
216         }
217         
218         Log_Log("VTerm", "Using '%s' as output", gsVT_OutputDevice);
219         Log_Log("VTerm", "Using '%s' as input", gsVT_InputDevice);
220         
221         VT_InitOutput();
222         VT_InitInput();
223         
224         // Create Nodes
225         for( i = 0; i < NUM_VTS; i++ )
226         {
227                 gVT_Terminals[i].Mode = TERM_MODE_TEXT;
228                 gVT_Terminals[i].Flags = 0;
229                 gVT_Terminals[i].CurColour = DEFAULT_COLOUR;
230                 gVT_Terminals[i].WritePos = 0;
231                 gVT_Terminals[i].AltWritePos = 0;
232                 gVT_Terminals[i].ViewPos = 0;
233                 gVT_Terminals[i].ReadingThread = -1;
234                 gVT_Terminals[i].ScrollHeight = 0;
235                 
236                 // Initialise
237                 VT_int_ChangeMode( &gVT_Terminals[i],
238                         TERM_MODE_TEXT, giVT_RealWidth, giVT_RealHeight );
239                 
240                 gVT_Terminals[i].Name[0] = '0'+i;
241                 gVT_Terminals[i].Name[1] = '\0';
242                 gVT_Terminals[i].Node.Inode = i;
243                 gVT_Terminals[i].Node.ImplPtr = &gVT_Terminals[i];
244                 gVT_Terminals[i].Node.NumACLs = 0;      // Only root can open virtual terminals
245                 
246                 gVT_Terminals[i].Node.Read = VT_Read;
247                 gVT_Terminals[i].Node.Write = VT_Write;
248                 gVT_Terminals[i].Node.IOCtl = VT_Terminal_IOCtl;
249 //              Semaphore_Init(&gVT_Terminals[i].InputSemaphore, 0, MAX_INPUT_CHARS8, "VTerm", gVT_Terminals[i].Name);
250         }
251         
252         // Add to DevFS
253         DevFS_AddDevice( &gVT_DrvInfo );
254         
255         // Set kernel output to VT0
256         Debug_SetKTerminal("/Devices/VTerm/0");
257         
258         return MODULE_ERR_OK;
259 }
260
261 /**
262  * \fn void VT_InitOutput()
263  * \brief Initialise Video Output
264  */
265 void VT_InitOutput()
266 {
267         giVT_OutputDevHandle = VFS_Open(gsVT_OutputDevice, VFS_OPENFLAG_WRITE);
268         if(giVT_OutputDevHandle == -1) {
269                 Log_Warning("VTerm", "Oh F**k, I can't open the video device '%s'", gsVT_OutputDevice);
270                 return ;
271         }
272         VT_SetResolution( giVT_RealWidth, giVT_RealHeight );
273         VT_SetTerminal( 0 );
274         VT_SetMode( VIDEO_BUFFMT_TEXT );
275 }
276
277 /**
278  * \fn void VT_InitInput()
279  * \brief Initialises the input
280  */
281 void VT_InitInput()
282 {
283         giVT_InputDevHandle = VFS_Open(gsVT_InputDevice, VFS_OPENFLAG_READ);
284         if(giVT_InputDevHandle == -1) {
285                 Log_Warning("VTerm", "Can't open the input device '%s'", gsVT_InputDevice);
286                 return ;
287         }
288         VFS_IOCtl(giVT_InputDevHandle, KB_IOCTL_SETCALLBACK, VT_KBCallBack);
289 }
290
291 /**
292  * \brief Set the video resolution
293  * \param Width New screen width
294  * \param Height        New screen height
295  */
296 void VT_SetResolution(int Width, int Height)
297 {
298         tVideo_IOCtl_Mode       mode = {0};
299          int    tmp;
300          int    i;
301         
302         // Create the video mode
303         mode.width = Width;
304         mode.height = Height;
305         mode.bpp = 32;
306         mode.flags = 0;
307         
308         // Set video mode
309         VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_FINDMODE, &mode );
310         tmp = mode.id;
311         if( Width != mode.width || Height != mode.height )
312         {
313                 Log_Warning("VTerm",
314                         "Selected resolution (%ix%i is not supported) by the device, using (%ix%i)",
315                         giVT_RealWidth, giVT_RealHeight,
316                         mode.width, mode.height
317                         );
318         }
319         VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_GETSETMODE, &tmp );
320         
321         // Resize text terminals if needed
322         if( gVT_Terminals[0].Text && (giVT_RealWidth != mode.width || giVT_RealHeight != mode.height) )
323         {
324                  int    newBufSize = (giVT_RealWidth/giVT_CharWidth)
325                                         *(giVT_RealHeight/giVT_CharHeight)
326                                         *(giVT_Scrollback+1);
327                 //tVT_Char      *tmp;
328                 // Resize the text terminals
329                 giVT_RealWidth = mode.width;
330                 giVT_RealHeight = mode.height;
331                 Log_Debug("VTerm", "Resizing terminals to %ix%i",
332                         giVT_RealWidth/giVT_CharWidth, giVT_RealHeight/giVT_CharHeight);
333                 for( i = 0; i < NUM_VTS; i ++ )
334                 {
335                         if( gVT_Terminals[i].Mode != TERM_MODE_TEXT )   continue;
336                         
337                         gVT_Terminals[i].TextWidth = giVT_RealWidth/giVT_CharWidth;
338                         gVT_Terminals[i].TextHeight = giVT_RealHeight/giVT_CharHeight;
339                         gVT_Terminals[i].ScrollHeight = gVT_Terminals[i].TextHeight;
340                         
341                         gVT_Terminals[i].Text = realloc(
342                                 gVT_Terminals[i].Text,
343                                 newBufSize*sizeof(tVT_Char)
344                                 );
345                 }
346         }
347 }
348
349 /**
350  * \brief Set video output buffer mode
351  */
352 void VT_SetMode(int Mode)
353 {
354         VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &Mode );
355 }
356
357 /**
358  * \fn char *VT_ReadDir(tVFS_Node *Node, int Pos)
359  * \brief Read from the VTerm Directory
360  */
361 char *VT_ReadDir(tVFS_Node *Node, int Pos)
362 {
363         if(Pos < 0)     return NULL;
364         if(Pos >= NUM_VTS)      return NULL;
365         return strdup( gVT_Terminals[Pos].Name );
366 }
367
368 /**
369  * \fn tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
370  * \brief Find an item in the VTerm directory
371  * \param Node  Root node
372  * \param Name  Name (number) of the terminal
373  */
374 tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
375 {
376          int    num;
377         
378         ENTER("pNode sName", Node, Name);
379         
380         // Open the input and output files if needed
381         if(giVT_OutputDevHandle == -2)  VT_InitOutput();
382         if(giVT_InputDevHandle == -2)   VT_InitInput();
383         
384         // Sanity check name
385         if(Name[0] < '0' || Name[0] > '9' || Name[1] != '\0') {
386                 LEAVE('n');
387                 return NULL;
388         }
389         // Get index
390         num = Name[0] - '0';
391         if(num >= NUM_VTS) {
392                 LEAVE('n');
393                 return NULL;
394         }
395         // Return node
396         LEAVE('p', &gVT_Terminals[num].Node);
397         return &gVT_Terminals[num].Node;
398 }
399
400 /**
401  * \fn int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
402  * \brief Control the VTerm Driver
403  */
404 int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
405 {
406          int    len;
407         switch(Id)
408         {
409         case DRV_IOCTL_TYPE:    return DRV_TYPE_MISC;
410         case DRV_IOCTL_IDENT:   memcpy(Data, "VT\0\0", 4);      return 0;
411         case DRV_IOCTL_VERSION: return VERSION;
412         case DRV_IOCTL_LOOKUP:  return 0;
413         
414         case 4: // Get Video Driver
415                 if(Data)        strcpy(Data, gsVT_OutputDevice);
416                 return strlen(gsVT_OutputDevice);
417         
418         case 5: // Set Video Driver
419                 if(!Data)       return -EINVAL;
420                 if(Threads_GetUID() != 0)       return -EACCES;
421                 
422                 len = strlen(Data);
423                 
424                 // TODO: Check if the string used is a heap string
425                 
426                 free(gsVT_OutputDevice);
427                 
428                 gsVT_OutputDevice = malloc(len+1);
429                 strcpy(gsVT_OutputDevice, Data);
430                 
431                 VFS_Close(giVT_OutputDevHandle);
432                 giVT_OutputDevHandle = -1;
433                 
434                 VT_InitOutput();
435                 return 1;
436         }
437         return 0;
438 }
439
440 /**
441  * \brief Read from a virtual terminal
442  */
443 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
444 {
445          int    pos = 0;
446          int    avail;
447         tVTerm  *term = &gVT_Terminals[ Node->Inode ];
448         Uint32  *codepoint_buf = Buffer;
449         Uint32  *codepoint_in;
450         
451         Mutex_Acquire( &term->ReadingLock );
452         
453         // Update cursor
454         if( term == gpVT_CurTerm && !(term->Flags & VT_FLAG_HIDECSR) )
455         {
456                 tVideo_IOCtl_Pos        csr_pos;
457                  int    offset;
458                 
459                 if(term->Flags & VT_FLAG_ALTBUF)
460                         offset = term->AltWritePos;
461                 else
462                         offset = term->WritePos - term->ViewPos;
463                 
464                 csr_pos.x = offset % term->TextWidth;
465                 csr_pos.y = offset / term->TextWidth;
466                 if( 0 <= csr_pos.y && csr_pos.y < term->TextHeight )
467                         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &csr_pos);
468         }
469         
470         // Check current mode
471         switch(term->Mode)
472         {
473         // Text Mode (UTF-8)
474         case TERM_MODE_TEXT:
475                 VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "VT_Read (UTF-8)");
476                 
477                 avail = term->InputWrite - term->InputRead;
478                 if(avail < 0)
479                         avail += MAX_INPUT_CHARS8;
480                 if(avail > Length - pos)
481                         avail = Length - pos;
482                 
483                 while( avail -- )
484                 {
485                         ((char*)Buffer)[pos] = term->InputBuffer[term->InputRead];
486                         pos ++;
487                         term->InputRead ++;
488                         while(term->InputRead > MAX_INPUT_CHARS8)
489                                 term->InputRead -= MAX_INPUT_CHARS8;
490                 }
491                 break;
492         
493         //case TERM_MODE_FB:
494         // Other - UCS-4
495         default:
496                 VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "VT_Read (UCS-4)");
497                 
498                 avail = term->InputWrite - term->InputRead;
499                 if(avail < 0)
500                         avail += MAX_INPUT_CHARS32;
501                 if(avail > Length - pos)
502                         avail = Length/4 - pos;
503                 
504                 codepoint_in = (void*)term->InputBuffer;
505                 codepoint_buf = Buffer;
506                 
507                 while( avail -- )
508                 {
509                         codepoint_buf[pos] = codepoint_in[term->InputRead];
510                         pos ++;
511                         term->InputRead ++;
512                         while(term->InputRead > MAX_INPUT_CHARS32)
513                                 term->InputRead -= MAX_INPUT_CHARS32;
514                 }
515                 pos *= 4;
516                 break;
517         }
518         
519         // Mark none avaliable if buffer empty
520         if( term->InputRead == term->InputWrite )
521                 VFS_MarkAvaliable(&term->Node, 0);
522         
523         term->ReadingThread = -1;
524
525         if( term == gpVT_CurTerm && !(term->Flags & VT_FLAG_HIDECSR) )
526         {
527                 tVideo_IOCtl_Pos        csr_pos;
528                 csr_pos.x = -1; csr_pos.y = -1;
529                 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &csr_pos);
530         }
531         
532         Mutex_Release( &term->ReadingLock );
533         
534         return pos;
535 }
536
537 /**
538  * \fn Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
539  * \brief Write to a virtual terminal
540  */
541 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
542 {
543         tVTerm  *term = &gVT_Terminals[ Node->Inode ];
544          int    size;
545         
546         // Write
547         switch( term->Mode )
548         {
549         // Print Text
550         case TERM_MODE_TEXT:
551                 VT_int_PutString(term, Buffer, Length);
552                 break;
553         // Framebuffer :)
554         case TERM_MODE_FB:
555         
556                 // - Sanity Checking
557                 size = term->Width*term->Height*4;
558                 if( Offset > size ) {
559                         Log_Notice("VTerm", "VT_Write: Offset (0x%llx) > FBSize (0x%x)",
560                                 Offset, size);
561                         return 0;
562                 }
563                 if( Offset + Length > size ) {
564                         Log_Notice("VTerm", "VT_Write: Offset+Length (0x%llx) > FBSize (0x%x)",
565                                 Offset+Length, size);
566                         Length = size - Offset;
567                 }
568                 
569                 // Copy to the local cache
570                 memcpy( (void*)((Uint)term->Buffer + (Uint)Offset), Buffer, Length );
571                 
572                 // Update screen if needed
573                 if( Node->Inode == giVT_CurrentTerminal )
574                 {
575                         // Fill entire screen?
576                         if( giVT_RealWidth > term->Width || giVT_RealHeight > term->Height )
577                         {
578                                 // No? :( Well, just center it
579                                  int    x, y, w, h;
580                                 x = Offset/4;   y = x / term->Width;    x %= term->Width;
581                                 w = Length/4+x; h = w / term->Width;    w %= term->Width;
582                                 // Center
583                                 x += (giVT_RealWidth - term->Width) / 2;
584                                 y += (giVT_RealHeight - term->Height) / 2;
585                                 while(h--)
586                                 {
587                                         VFS_WriteAt( giVT_OutputDevHandle,
588                                                 (x + y * giVT_RealWidth)*4,
589                                                 term->Width * 4,
590                                                 Buffer
591                                                 );
592                                         Buffer = (void*)( (Uint)Buffer + term->Width*4 );
593                                         y ++;
594                                 }
595                                 return 0;
596                         }
597                         else {
598                                 return VFS_WriteAt( giVT_OutputDevHandle, Offset, Length, Buffer );
599                         }
600                 }
601         // Just pass on (for now)
602         // TODO: Handle locally too to ensure no information is lost on
603         //       VT Switch (and to isolate terminals from each other)
604         case TERM_MODE_2DACCEL:
605         //case TERM_MODE_3DACCEL:
606                 if( Node->Inode == giVT_CurrentTerminal )
607                 {
608                         VFS_Write( giVT_OutputDevHandle, Length, Buffer );
609                 }
610                 break;
611         }
612         
613         return 0;
614 }
615
616 /**
617  * \fn int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
618  * \brief Call an IO Control on a virtual terminal
619  */
620 int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
621 {
622          int    *iData = Data;
623          int    ret;
624         tVTerm  *term = Node->ImplPtr;
625         ENTER("pNode iId pData", Node, Id, Data);
626         
627         if(Id >= DRV_IOCTL_LOOKUP) {
628                 // Only root can fiddle with graphics modes
629                 // TODO: Remove this and replace with user ownership
630                 if( Threads_GetUID() != 0 )     return -1;
631         }
632         
633         switch(Id)
634         {
635         // --- Core Defined
636         case DRV_IOCTL_TYPE:
637                 LEAVE('i', DRV_TYPE_TERMINAL);
638                 return DRV_TYPE_TERMINAL;
639         case DRV_IOCTL_IDENT:
640                 memcpy(Data, "VT\0\0", 4);
641                 LEAVE('i', 0);
642                 return 0;
643         case DRV_IOCTL_VERSION:
644                 LEAVE('x', VERSION);
645                 return VERSION;
646         case DRV_IOCTL_LOOKUP:
647                 LEAVE('i', 0);
648                 return 0;
649         
650         // Get/Set the mode (and apply any changes)
651         case TERM_IOCTL_MODETYPE:
652                 if(Data != NULL)
653                 {
654                         if( CheckMem(Data, sizeof(int)) == 0 ) {
655                                 LEAVE('i', -1);
656                                 return -1;
657                         }
658                         Log_Log("VTerm", "VTerm %i mode set to %i", (int)Node->Inode, *iData);
659                         
660                         // Update mode if needed
661                         if( term->Mode != *iData
662                          || term->NewWidth
663                          || term->NewHeight)
664                         {
665                                 // Adjust for text mode
666                                 if( *iData == TERM_MODE_TEXT ) {
667                                         term->NewHeight *= giVT_CharHeight;
668                                         term->NewWidth *= giVT_CharWidth;
669                                 }
670                                 // Fill unchanged dimensions
671                                 if(term->NewHeight == 0)        term->NewHeight = term->Height;
672                                 if(term->NewWidth == 0) term->NewWidth = term->Width;
673                                 // Set new mode
674                                 VT_int_ChangeMode(term, *iData, term->NewWidth, term->NewHeight);
675                                 // Clear unapplied dimensions
676                                 term->NewWidth = 0;
677                                 term->NewHeight = 0;
678                         }
679                         
680                         // Update the screen dimensions
681                         if(Node->Inode == giVT_CurrentTerminal)
682                                 VT_SetTerminal( giVT_CurrentTerminal );
683                 }
684                 LEAVE('i', term->Mode);
685                 return term->Mode;
686         
687         // Get/set the terminal width
688         case TERM_IOCTL_WIDTH:
689                 if(Data != NULL) {
690                         if( CheckMem(Data, sizeof(int)) == 0 ) {
691                                 LEAVE('i', -1);
692                                 return -1;
693                         }
694                         term->NewWidth = *iData;
695                 }
696                 if( term->NewWidth )
697                         ret = term->NewWidth;
698                 else if( term->Mode == TERM_MODE_TEXT )
699                         ret = term->TextWidth;
700                 else
701                         ret = term->Width;
702                 LEAVE('i', ret);
703                 return ret;
704         
705         // Get/set the terminal height
706         case TERM_IOCTL_HEIGHT:
707                 if(Data != NULL) {
708                         if( CheckMem(Data, sizeof(int)) == 0 ) {
709                                 LEAVE('i', -1);
710                                 return -1;
711                         }
712                         term->NewHeight = *iData;
713                 }
714                 if( term->NewHeight )
715                         ret = term->NewHeight;
716                 else if( term->Mode == TERM_MODE_TEXT )
717                         ret = term->TextHeight;
718                 else
719                         ret = term->Height;
720                 LEAVE('i', ret);
721                 return ret;
722         
723         case TERM_IOCTL_FORCESHOW:
724                 Log_Log("VTerm", "Thread %i forced VTerm %i to be shown",
725                         Threads_GetTID(), (int)Node->Inode);
726                 VT_SetTerminal( Node->Inode );
727                 LEAVE('i', 1);
728                 return 1;
729         
730         case TERM_IOCTL_GETCURSOR:
731                 ret = (term->Flags & VT_FLAG_ALTBUF) ? term->AltWritePos : term->WritePos-term->ViewPos;
732                 LEAVE('i', ret);
733                 return ret;
734         }
735         LEAVE('i', -1);
736         return -1;
737 }
738
739 /**
740  * \fn void VT_SetTerminal(int ID)
741  * \brief Set the current terminal
742  */
743 void VT_SetTerminal(int ID)
744 {       
745         // Update current terminal ID
746         Log_Log("VTerm", "Changed terminal from %i to %i", giVT_CurrentTerminal, ID);
747         giVT_CurrentTerminal = ID;
748         gpVT_CurTerm = &gVT_Terminals[ID];
749         
750         // Update cursor
751         // TODO: Check if the reading lock is held
752         if( gpVT_CurTerm->Text && gpVT_CurTerm->Mode == TERM_MODE_TEXT
753          && !(gpVT_CurTerm->Flags & VT_FLAG_HIDECSR)
754          && Mutex_IsLocked( &gpVT_CurTerm->ReadingLock )
755          )
756         {
757                 tVideo_IOCtl_Pos        pos;
758                  int    offset = (gpVT_CurTerm->Flags & VT_FLAG_ALTBUF) ? gpVT_CurTerm->AltWritePos : gpVT_CurTerm->WritePos - gpVT_CurTerm->ViewPos;
759                 pos.x = offset % gpVT_CurTerm->TextWidth;
760                 pos.y = offset / gpVT_CurTerm->TextWidth;
761                 if( 0 <= pos.y && pos.y < gpVT_CurTerm->TextHeight )
762                         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
763         }
764         
765         if( gpVT_CurTerm->Mode == TERM_MODE_TEXT )
766                 VT_SetMode( VIDEO_BUFFMT_TEXT );
767         else
768                 VT_SetMode( VIDEO_BUFFMT_FRAMEBUFFER );
769         
770         // Update the screen
771         VT_int_UpdateScreen( &gVT_Terminals[ ID ], 1 );
772 }
773
774 /**
775  * \fn void VT_KBCallBack(Uint32 Codepoint)
776  * \brief Called on keyboard interrupt
777  * \param Codepoint     Pseudo-UTF32 character
778  * 
779  * Handles a key press and sends the key code to the user's buffer.
780  * If the code creates a kernel-magic sequence, it is not passed to the
781  * user and is handled in-kernel.
782  */
783 void VT_KBCallBack(Uint32 Codepoint)
784 {
785         tVTerm  *term = gpVT_CurTerm;
786         
787         // How the hell did we get a codepoint of zero?
788         if(Codepoint == 0)      return;
789         
790         // Key Up
791         if( Codepoint & 0x80000000 )
792         {
793                 Codepoint &= 0x7FFFFFFF;
794                 switch(Codepoint)
795                 {
796                 case KEY_LALT:  gbVT_AltDown &= ~1;     break;
797                 case KEY_RALT:  gbVT_AltDown &= ~2;     break;
798                 case KEY_LCTRL: gbVT_CtrlDown &= ~1;    break;
799                 case KEY_RCTRL: gbVT_CtrlDown &= ~2;    break;
800                 }
801                 return;
802         }
803         
804         switch(Codepoint)
805         {
806         case KEY_LALT:  gbVT_AltDown |= 1;      break;
807         case KEY_RALT:  gbVT_AltDown |= 2;      break;
808         case KEY_LCTRL: gbVT_CtrlDown |= 1;     break;
809         case KEY_RCTRL: gbVT_CtrlDown |= 2;     break;
810         
811         default:
812                 if(!gbVT_AltDown || !gbVT_CtrlDown)
813                         break;
814                 switch(Codepoint)
815                 {
816                 case KEY_F1:    VT_SetTerminal(0);      return;
817                 case KEY_F2:    VT_SetTerminal(1);      return;
818                 case KEY_F3:    VT_SetTerminal(2);      return;
819                 case KEY_F4:    VT_SetTerminal(3);      return;
820                 case KEY_F5:    VT_SetTerminal(4);      return;
821                 case KEY_F6:    VT_SetTerminal(5);      return;
822                 case KEY_F7:    VT_SetTerminal(6);      return;
823                 case KEY_F8:    VT_SetTerminal(7);      return;
824                 case KEY_F9:    VT_SetTerminal(8);      return;
825                 case KEY_F10:   VT_SetTerminal(9);      return;
826                 case KEY_F11:   VT_SetTerminal(10);     return;
827                 case KEY_F12:   VT_SetTerminal(11);     return;
828                 // Scrolling
829                 case KEY_PGUP:
830                         if( gpVT_CurTerm->Flags & VT_FLAG_ALTBUF )
831                                 return ;
832                         if( gpVT_CurTerm->ViewPos > gpVT_CurTerm->Width )
833                                 gpVT_CurTerm->ViewPos -= gpVT_CurTerm->Width;
834                         else
835                                 gpVT_CurTerm->ViewPos = 0;
836                         return;
837                 case KEY_PGDOWN:
838                         if( gpVT_CurTerm->Flags & VT_FLAG_ALTBUF )
839                                 return ;
840                         if( gpVT_CurTerm->ViewPos < gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback) )
841                                 gpVT_CurTerm->ViewPos += gpVT_CurTerm->Width;
842                         else
843                                 gpVT_CurTerm->ViewPos = gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback);
844                         return;
845                 }
846         }
847         
848         // Encode key
849         if(term->Mode == TERM_MODE_TEXT)
850         {
851                 Uint8   buf[6] = {0};
852                  int    len = 0;
853                 
854                 // Ignore Modifer Keys
855                 if(Codepoint > KEY_MODIFIERS)   return;
856                 
857                 // Get UTF-8/ANSI Encoding
858                 switch(Codepoint)
859                 {
860                 case KEY_LEFT:
861                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'D';
862                         len = 3;
863                         break;
864                 case KEY_RIGHT:
865                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'C';
866                         len = 3;
867                         break;
868                 case KEY_UP:
869                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'A';
870                         len = 3;
871                         break;
872                 case KEY_DOWN:
873                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'B';
874                         len = 3;
875                         break;
876                 
877                 case KEY_PGUP:
878                         //buf[0] = '\x1B';      buf[1] = '[';   buf[2] = '5';   // Some overline also
879                         //len = 4;      // Commented out until I'm sure
880                         len = 0;
881                         break;
882                 case KEY_PGDOWN:
883                         len = 0;
884                         break;
885                 
886                 // Attempt to encode in UTF-8
887                 default:
888                         len = WriteUTF8( buf, Codepoint );
889                         if(len == 0) {
890                                 Warning("Codepoint (%x) is unrepresentable in UTF-8", Codepoint);
891                         }
892                         break;
893                 }
894                 
895                 if(len == 0) {
896                         // Unprintable / Don't Pass
897                         return;
898                 }
899
900 #if 0
901                 // Handle meta characters
902                 if( !(term->Flags & VT_FLAG_RAWIN) )
903                 {
904                         switch(buf[0])
905                         {
906                         case '\3':      // ^C
907                                 
908                                 break;
909                         }
910                 }
911 #endif
912                 
913                 // Write
914                 if( MAX_INPUT_CHARS8 - term->InputWrite >= len )
915                         memcpy( &term->InputBuffer[term->InputWrite], buf, len );
916                 else {
917                         memcpy( &term->InputBuffer[term->InputWrite], buf, MAX_INPUT_CHARS8 - term->InputWrite );
918                         memcpy( &term->InputBuffer[0], buf, len - (MAX_INPUT_CHARS8 - term->InputWrite) );
919                 }
920                 // Roll the buffer over
921                 term->InputWrite += len;
922                 term->InputWrite %= MAX_INPUT_CHARS8;
923                 if( (term->InputWrite - term->InputRead + MAX_INPUT_CHARS8)%MAX_INPUT_CHARS8 < len ) {
924                         term->InputRead = term->InputWrite + 1;
925                         term->InputRead %= MAX_INPUT_CHARS8;
926                 }
927         }
928         else
929         {
930                 // Encode the raw UTF-32 Key
931                 Uint32  *raw_in = (void*)term->InputBuffer;
932                 raw_in[ term->InputWrite ] = Codepoint;
933                 term->InputWrite ++;
934                 term->InputWrite %= MAX_INPUT_CHARS32;
935                 if(term->InputRead == term->InputWrite) {
936                         term->InputRead ++;
937                         term->InputRead %= MAX_INPUT_CHARS32;
938                 }
939         }
940         
941         VFS_MarkAvaliable(&term->Node, 1);
942 }
943
944 /**
945  * \fn void VT_int_ClearLine(tVTerm *Term, int Num)
946  * \brief Clears a line in a virtual terminal
947  */
948 void VT_int_ClearLine(tVTerm *Term, int Num)
949 {
950          int    i;
951         tVT_Char        *cell;
952         
953         if( Num < 0 || Num >= Term->TextHeight * (giVT_Scrollback + 1) )        return ;
954         
955         cell = (Term->Flags & VT_FLAG_ALTBUF) ? Term->AltBuf : Term->Text;
956         cell = &cell[ Num*Term->TextWidth ];
957         
958         for( i = Term->TextWidth; i--; )
959         {
960                 cell[ i ].Ch = 0;
961                 cell[ i ].Colour = Term->CurColour;
962         }
963 }
964
965 /**
966  * \fn int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
967  * \brief Parses a VT100 Escape code
968  */
969 int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
970 {
971         char    c;
972          int    argc = 0, j = 1;
973          int    tmp;
974          int    args[6] = {0,0,0,0};
975          int    bQuestionMark = 0;
976         
977         switch(Buffer[0])
978         {
979         //Large Code
980         case '[':
981                 // Get Arguments
982                 c = Buffer[j++];
983                 if(c == '?') {
984                         bQuestionMark = 1;
985                         c = Buffer[j++];
986                 }
987                 if( '0' <= c && c <= '9' )
988                 {
989                         do {
990                                 if(c == ';')    c = Buffer[j++];
991                                 while('0' <= c && c <= '9') {
992                                         args[argc] *= 10;
993                                         args[argc] += c-'0';
994                                         c = Buffer[j++];
995                                 }
996                                 argc ++;
997                         } while(c == ';');
998                 }
999                 
1000                 // Get Command
1001                 if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
1002                 {
1003                         if( bQuestionMark )
1004                         {
1005                                 switch(c)
1006                                 {
1007                                 // DEC Private Mode Set
1008                                 case 'h':
1009                                         if(argc != 1)   break;
1010                                         switch(args[0])
1011                                         {
1012                                         case 1047:
1013                                                 VT_int_ToggleAltBuffer(Term, 1);
1014                                                 break;
1015                                         }
1016                                         break;
1017                                 case 'l':
1018                                         if(argc != 1)   break;
1019                                         switch(args[0])
1020                                         {
1021                                         case 1047:
1022                                                 VT_int_ToggleAltBuffer(Term, 0);
1023                                                 break;
1024                                         }
1025                                         break;
1026                                 default:
1027                                         Log_Warning("VTerm", "Unknown control sequence '\\x1B[?%c'", c);
1028                                         break;
1029                                 }
1030                         }
1031                         else
1032                         {
1033                                 tmp = 1;
1034                                 switch(c)
1035                                 {
1036                                 // Left
1037                                 case 'D':
1038                                         tmp = -1;
1039                                 // Right
1040                                 case 'C':
1041                                         if(argc == 1)   tmp *= args[0];
1042                                         if( Term->Flags & VT_FLAG_ALTBUF )
1043                                         {
1044                                                 if( (Term->AltWritePos + tmp) % Term->TextWidth == 0 ) {
1045                                                         Term->AltWritePos -= Term->AltWritePos % Term->TextWidth;
1046                                                         Term->AltWritePos += Term->TextWidth - 1;
1047                                                 }
1048                                                 else
1049                                                         Term->AltWritePos += tmp;
1050                                         }
1051                                         else
1052                                         {
1053                                                 if( (Term->WritePos + tmp) % Term->TextWidth == 0 ) {
1054                                                         Term->WritePos -= Term->WritePos % Term->TextWidth;
1055                                                         Term->WritePos += Term->TextWidth - 1;
1056                                                 }
1057                                                 else
1058                                                         Term->WritePos += tmp;
1059                                         }
1060                                         break;
1061                                 
1062                                 // Erase
1063                                 case 'J':
1064                                         switch(args[0])
1065                                         {
1066                                         case 0: // Erase below
1067                                                 break;
1068                                         case 1: // Erase above
1069                                                 break;
1070                                         case 2: // Erase all
1071                                                 if( Term->Flags & VT_FLAG_ALTBUF )
1072                                                 {
1073                                                          int    i = Term->TextHeight;
1074                                                         while( i-- )    VT_int_ClearLine(Term, i);
1075                                                         Term->AltWritePos = 0;
1076                                                         VT_int_UpdateScreen(Term, 1);
1077                                                 }
1078                                                 else
1079                                                 {
1080                                                          int    i = Term->TextHeight * (giVT_Scrollback + 1);
1081                                                         while( i-- )    VT_int_ClearLine(Term, i);
1082                                                         Term->WritePos = 0;
1083                                                         Term->ViewPos = 0;
1084                                                         VT_int_UpdateScreen(Term, 1);
1085                                                 }
1086                                                 break;
1087                                         }
1088                                         break;
1089                                 
1090                                 // Erase in line
1091                                 case 'K':
1092                                         switch(args[0])
1093                                         {
1094                                         case 0: // Erase to right
1095                                                 if( Term->Flags & VT_FLAG_ALTBUF )
1096                                                 {
1097                                                          int    i, max;
1098                                                         max = Term->Width - Term->AltWritePos % Term->Width;
1099                                                         for( i = 0; i < max; i ++ )
1100                                                                 Term->AltBuf[Term->AltWritePos+i].Ch = 0;
1101                                                 }
1102                                                 else
1103                                                 {
1104                                                          int    i, max;
1105                                                         max = Term->Width - Term->WritePos % Term->Width;
1106                                                         for( i = 0; i < max; i ++ )
1107                                                                 Term->Text[Term->WritePos+i].Ch = 0;
1108                                                 }
1109                                                 VT_int_UpdateScreen(Term, 0);
1110                                                 break;
1111                                         case 1: // Erase to left
1112                                                 if( Term->Flags & VT_FLAG_ALTBUF )
1113                                                 {
1114                                                          int    i = Term->AltWritePos % Term->Width;
1115                                                         while( i -- )
1116                                                                 Term->AltBuf[Term->AltWritePos++].Ch = 0;
1117                                                 }
1118                                                 else
1119                                                 {
1120                                                          int    i = Term->WritePos % Term->Width;
1121                                                         while( i -- )
1122                                                                 Term->Text[Term->WritePos++].Ch = 0;
1123                                                 }
1124                                                 VT_int_UpdateScreen(Term, 0);
1125                                                 break;
1126                                         case 2: // Erase all
1127                                                 if( Term->Flags & VT_FLAG_ALTBUF )
1128                                                 {
1129                                                         VT_int_ClearLine(Term, Term->AltWritePos / Term->Width);
1130                                                 }
1131                                                 else
1132                                                 {
1133                                                         VT_int_ClearLine(Term, Term->WritePos / Term->Width);
1134                                                 }
1135                                                 VT_int_UpdateScreen(Term, 0);
1136                                                 break;
1137                                         }
1138                                         break;
1139                                 
1140                                 // Set cursor position
1141                                 case 'H':
1142                                         if( Term->Flags & VT_FLAG_ALTBUF )
1143                                                 Term->AltWritePos = args[0] + args[1]*Term->TextWidth;
1144                                         else
1145                                                 Term->WritePos = args[0] + args[1]*Term->TextWidth;
1146                                         //Log_Debug("VTerm", "args = {%i, %i}", args[0], args[1]);
1147                                         break;
1148                                 
1149                                 // Scroll up `n` lines
1150                                 case 'S':
1151                                         tmp = -1;
1152                                 // Scroll down `n` lines
1153                                 case 'T':
1154                                         if(argc == 1)   tmp *= args[0];
1155                                         if( Term->Flags & VT_FLAG_ALTBUF )
1156                                                 VT_int_ScrollText(Term, tmp);
1157                                         else
1158                                         {
1159                                                 if(Term->ViewPos/Term->TextWidth + tmp < 0)
1160                                                         break;
1161                                                 if(Term->ViewPos/Term->TextWidth + tmp  > Term->TextHeight * (giVT_Scrollback + 1))
1162                                                         break;
1163                                                 
1164                                                 Term->ViewPos += Term->TextWidth*tmp;
1165                                         }
1166                                         break;
1167                                 
1168                                 // Set Font flags
1169                                 case 'm':
1170                                         for( ; argc--; )
1171                                         {
1172                                                 // Flags
1173                                                 if( 0 <= args[argc] && args[argc] <= 8)
1174                                                 {
1175                                                         switch(args[argc])
1176                                                         {
1177                                                         case 0: Term->CurColour = DEFAULT_COLOUR;       break;  // Reset
1178                                                         case 1: Term->CurColour |= 0x80000000;  break;  // Bright
1179                                                         case 2: Term->CurColour &= ~0x80000000; break;  // Dim
1180                                                         }
1181                                                 }
1182                                                 // Foreground Colour
1183                                                 else if(30 <= args[argc] && args[argc] <= 37) {
1184                                                         Term->CurColour &= 0xF000FFFF;
1185                                                         Term->CurColour |= (Uint32)caVT100Colours[ args[argc]-30+(Term->CurColour>>28) ] << 16;
1186                                                 }
1187                                                 // Background Colour
1188                                                 else if(40 <= args[argc] && args[argc] <= 47) {
1189                                                         Term->CurColour &= 0xFFFF8000;
1190                                                         Term->CurColour |= caVT100Colours[ args[argc]-40+((Term->CurColour>>12)&15) ];
1191                                                 }
1192                                         }
1193                                         break;
1194                                 
1195                                 // Set scrolling region
1196                                 case 'r':
1197                                         if( argc != 2 ) break;
1198                                         Term->ScrollTop = args[0];
1199                                         Term->ScrollHeight = args[1] - args[0];
1200                                         break;
1201                                 
1202                                 default:
1203                                         Log_Warning("VTerm", "Unknown control sequence '\\x1B[%c'", c);
1204                                         break;
1205                                 }
1206                         }
1207                 }
1208                 break;
1209                 
1210         default:        break;
1211         }
1212         
1213         //Log_Debug("VTerm", "j = %i, Buffer = '%s'", j, Buffer);
1214         return j;
1215 }
1216
1217 /**
1218  * \fn void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
1219  * \brief Print a string to the Virtual Terminal
1220  */
1221 void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
1222 {
1223         Uint32  val;
1224          int    i;
1225         
1226         // Iterate
1227         for( i = 0; i < Count; i++ )
1228         {
1229                 // Handle escape sequences
1230                 if( Buffer[i] == 0x1B )
1231                 {
1232                         i ++;
1233                         i += VT_int_ParseEscape(Term, (char*)&Buffer[i]) - 1;
1234                         continue;
1235                 }
1236                 
1237                 // Fast check for non UTF-8
1238                 if( Buffer[i] < 128 )   // Plain ASCII
1239                         VT_int_PutChar(Term, Buffer[i]);
1240                 else {  // UTF-8
1241                         i += ReadUTF8(&Buffer[i], &val) - 1;
1242                         VT_int_PutChar(Term, val);
1243                 }
1244         }
1245         // Update Screen
1246         VT_int_UpdateScreen( Term, 0 );
1247 }
1248
1249 /**
1250  * \fn void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
1251  * \brief Write a single character to a VTerm
1252  */
1253 void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
1254 {
1255          int    i;
1256         tVT_Char        *buffer;
1257          int    write_pos;
1258         
1259         if(Term->Flags & VT_FLAG_ALTBUF) {
1260                 buffer = Term->AltBuf;
1261                 write_pos = Term->AltWritePos;
1262         }
1263         else {
1264                 buffer = Term->Text;
1265                 write_pos = Term->WritePos;
1266         }
1267         
1268         switch(Ch)
1269         {
1270         case '\0':      return; // Ignore NULL byte
1271         case '\n':
1272                 VT_int_UpdateScreen( Term, 0 ); // Update the line before newlining
1273                 write_pos += Term->TextWidth;
1274         case '\r':
1275                 write_pos -= write_pos % Term->TextWidth;
1276                 break;
1277         
1278         case '\t': { int tmp = write_pos / Term->TextWidth;
1279                 write_pos %= Term->TextWidth;
1280                 do {
1281                         buffer[ write_pos ].Ch = '\0';
1282                         buffer[ write_pos ].Colour = Term->CurColour;
1283                         write_pos ++;
1284                 } while(write_pos & 7);
1285                 write_pos += tmp * Term->TextWidth;
1286                 break; }
1287         
1288         case '\b':
1289                 // Backspace is invalid at Offset 0
1290                 if(write_pos == 0)      break;
1291                 
1292                 write_pos --;
1293                 // Singe Character
1294                 if(buffer[ write_pos ].Ch != '\0') {
1295                         buffer[ write_pos ].Ch = 0;
1296                         buffer[ write_pos ].Colour = Term->CurColour;
1297                         break;
1298                 }
1299                 // Tab
1300                 i = 7;  // Limit it to 8
1301                 do {
1302                         buffer[ write_pos ].Ch = 0;
1303                         buffer[ write_pos ].Colour = Term->CurColour;
1304                         write_pos --;
1305                 } while(write_pos && i-- && buffer[ write_pos ].Ch == '\0');
1306                 if(buffer[ write_pos ].Ch != '\0')
1307                         write_pos ++;
1308                 break;
1309         
1310         default:
1311                 buffer[ write_pos ].Ch = Ch;
1312                 buffer[ write_pos ].Colour = Term->CurColour;
1313                 // Update the line before wrapping
1314                 if( (write_pos + 1) % Term->TextWidth == 0 )
1315                         VT_int_UpdateScreen( Term, 0 );
1316                 write_pos ++;
1317                 break;
1318         }
1319         
1320         if(Term->Flags & VT_FLAG_ALTBUF)
1321         {
1322                 Term->AltBuf = buffer;
1323                 Term->AltWritePos = write_pos;
1324                 
1325                 if(Term->AltWritePos >= Term->TextWidth*Term->TextHeight)
1326                 {
1327                         Term->AltWritePos -= Term->TextWidth;
1328                         VT_int_ScrollText(Term, 1);
1329                 }
1330                 
1331         }
1332         else
1333         {
1334                 Term->Text = buffer;
1335                 Term->WritePos = write_pos;
1336                 // Move Screen
1337                 // - Check if we need to scroll the entire scrollback buffer
1338                 if(Term->WritePos >= Term->TextWidth*Term->TextHeight*(giVT_Scrollback+1))
1339                 {
1340                          int    base;
1341                         
1342                         // Update previous line
1343                         Term->WritePos -= Term->TextWidth;
1344                         VT_int_UpdateScreen( Term, 0 );
1345                         
1346                         // Update view position
1347                         base = Term->TextWidth*Term->TextHeight*(giVT_Scrollback);
1348                         if(Term->ViewPos < base)
1349                                 Term->ViewPos += Term->Width;
1350                         if(Term->ViewPos > base)
1351                                 Term->ViewPos = base;
1352                         
1353                         VT_int_ScrollText(Term, 1);
1354                 }
1355                 // Ok, so we only need to scroll the screen
1356                 else if(Term->WritePos >= Term->ViewPos + Term->TextWidth*Term->TextHeight)
1357                 {
1358                         // Update the last line
1359                         Term->WritePos -= Term->TextWidth;
1360                         VT_int_UpdateScreen( Term, 0 );
1361                         Term->WritePos += Term->TextWidth;
1362                         
1363                         VT_int_ScrollText(Term, 1);
1364                         
1365                         Term->ViewPos += Term->TextWidth;
1366                 }
1367         }
1368         
1369         //LEAVE('-');
1370 }
1371
1372 void VT_int_ScrollText(tVTerm *Term, int Count)
1373 {
1374         tVT_Char        *buf;
1375          int    height, init_write_pos;
1376          int    len, i;
1377          int    scroll_top, scroll_height;
1378
1379         // Get buffer pointer and attributes    
1380         if( Term->Flags & VT_FLAG_ALTBUF )
1381         {
1382                 buf = Term->AltBuf;
1383                 height = Term->TextHeight;
1384                 init_write_pos = Term->AltWritePos;
1385                 scroll_top = Term->ScrollTop;
1386                 scroll_height = Term->ScrollHeight;
1387         }
1388         else
1389         {
1390                 buf = Term->Text;
1391                 height = Term->TextHeight*(giVT_Scrollback+1);
1392                 init_write_pos = Term->WritePos;
1393                 scroll_top = 0;
1394                 scroll_height = height;
1395         }
1396
1397         // Scroll text downwards        
1398         if( Count > 0 )
1399         {
1400                  int    base;
1401         
1402                 // Set up
1403                 if(Count > scroll_height)       Count = scroll_height;
1404                 base = Term->TextWidth*(scroll_top + scroll_height - Count);
1405                 len = Term->TextWidth*(scroll_height - Count);
1406                 
1407                 // Scroll terminal cache
1408                 memmove(
1409                         &buf[Term->TextWidth*scroll_top],
1410                         &buf[Term->TextWidth*(scroll_top+Count)],
1411                         len*sizeof(tVT_Char)
1412                         );
1413                 // Clear last rows
1414                 for( i = 0; i < Term->TextWidth*Count; i ++ )
1415                 {
1416                         buf[ base + i ].Ch = 0;
1417                         buf[ base + i ].Colour = Term->CurColour;
1418                 }
1419                 
1420                 // Update Screen
1421                 VT_int_ScrollFramebuffer( Term, Count );
1422                 if( Term->Flags & VT_FLAG_ALTBUF )
1423                         Term->AltWritePos = base;
1424                 else
1425                         Term->WritePos = Term->ViewPos + Term->TextWidth*(Term->TextHeight - Count);
1426                 for( i = 0; i < Count; i ++ )
1427                 {
1428                         VT_int_UpdateScreen( Term, 0 );
1429                         if( Term->Flags & VT_FLAG_ALTBUF )
1430                                 Term->AltWritePos += Term->TextWidth;
1431                         else
1432                                 Term->WritePos += Term->TextWidth;
1433                 }
1434         }
1435         else
1436         {
1437                 Count = -Count;
1438                 if(Count > scroll_height)       Count = scroll_height;
1439                 
1440                 len = Term->TextWidth*(scroll_height - Count);
1441                 
1442                 // Scroll terminal cache
1443                 memmove(
1444                         &buf[Term->TextWidth*(scroll_top+Count)],
1445                         &buf[Term->TextWidth*scroll_top],
1446                         len*sizeof(tVT_Char)
1447                         );
1448                 // Clear preceding rows
1449                 for( i = 0; i < Term->TextWidth*Count; i ++ )
1450                 {
1451                         buf[ i ].Ch = 0;
1452                         buf[ i ].Colour = Term->CurColour;
1453                 }
1454                 
1455                 VT_int_ScrollFramebuffer( Term, -Count );
1456                 if( Term->Flags & VT_FLAG_ALTBUF )
1457                         Term->AltWritePos = Term->TextWidth*scroll_top;
1458                 else
1459                         Term->WritePos = Term->ViewPos;
1460                 for( i = 0; i < Count; i ++ )
1461                 {
1462                         VT_int_UpdateScreen( Term, 0 );
1463                         if( Term->Flags & VT_FLAG_ALTBUF )
1464                                 Term->AltWritePos += Term->TextWidth;
1465                         else
1466                                 Term->WritePos += Term->TextWidth;
1467                 }
1468         }
1469         
1470         if( Term->Flags & VT_FLAG_ALTBUF )
1471                 Term->AltWritePos = init_write_pos;
1472         else
1473                 Term->WritePos = init_write_pos;
1474 }
1475
1476 /**
1477  * \fn void VT_int_ScrollFramebuffer( tVTerm *Term, int Count )
1478  * \note Scrolls the framebuffer down by \a Count text lines
1479  */
1480 void VT_int_ScrollFramebuffer( tVTerm *Term, int Count )
1481 {
1482          int    tmp;
1483         struct {
1484                 Uint8   Op;
1485                 Uint16  DstX, DstY;
1486                 Uint16  SrcX, SrcY;
1487                 Uint16  W, H;
1488         } PACKED        buf;
1489         
1490         // Only update if this is the current terminal
1491         if( Term != gpVT_CurTerm )      return;
1492         
1493         if( Count > Term->ScrollHeight )        Count = Term->ScrollHeight;
1494         if( Count < -Term->ScrollHeight )       Count = -Term->ScrollHeight;
1495         
1496         // Switch to 2D Command Stream
1497         tmp = VIDEO_BUFFMT_2DSTREAM;
1498         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp);
1499         
1500         // BLIT to 0,0 from 0,giVT_CharHeight
1501         buf.Op = VIDEO_2DOP_BLIT;
1502         buf.SrcX = 0;   buf.DstX = 0;
1503         // TODO: Don't assume character dimensions
1504         buf.W = Term->TextWidth * giVT_CharWidth;
1505         if( Count > 0 )
1506         {
1507                 buf.SrcY = (Term->ScrollTop+Count) * giVT_CharHeight;
1508                 buf.DstY = Term->ScrollTop * giVT_CharHeight;
1509         }
1510         else    // Scroll up, move text down
1511         {
1512                 Count = -Count;
1513                 buf.SrcY = Term->ScrollTop * giVT_CharHeight;
1514                 buf.DstY = (Term->ScrollTop+Count) * giVT_CharHeight;
1515         }
1516         buf.H = (Term->ScrollHeight-Count) * giVT_CharHeight;
1517         VFS_WriteAt(giVT_OutputDevHandle, 0, sizeof(buf), &buf);
1518         
1519         // Restore old mode (this function is only called during text mode)
1520         tmp = VIDEO_BUFFMT_TEXT;
1521         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp);
1522 }
1523
1524 /**
1525  * \fn void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
1526  * \brief Updates the video framebuffer
1527  */
1528 void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
1529 {
1530         tVT_Char        *buffer;
1531          int    view_pos, write_pos;
1532         // Only update if this is the current terminal
1533         if( Term != gpVT_CurTerm )      return;
1534         
1535         switch( Term->Mode )
1536         {
1537         case TERM_MODE_TEXT:
1538                 view_pos = (Term->Flags & VT_FLAG_ALTBUF) ? 0 : Term->ViewPos;
1539                 write_pos = (Term->Flags & VT_FLAG_ALTBUF) ? Term->AltWritePos : Term->WritePos;
1540                 buffer = (Term->Flags & VT_FLAG_ALTBUF) ? Term->AltBuf : Term->Text;
1541                 // Re copy the entire screen?
1542                 if(UpdateAll) {
1543                         VFS_WriteAt(
1544                                 giVT_OutputDevHandle,
1545                                 0,
1546                                 Term->TextWidth*Term->TextHeight*sizeof(tVT_Char),
1547                                 &buffer[view_pos]
1548                                 );
1549                 }
1550                 // Only copy the current line
1551                 else {
1552                          int    ofs = write_pos - write_pos % Term->TextWidth;
1553                         VFS_WriteAt(
1554                                 giVT_OutputDevHandle,
1555                                 (ofs - view_pos)*sizeof(tVT_Char),
1556                                 Term->TextWidth*sizeof(tVT_Char),
1557                                 &buffer[ofs]
1558                                 );
1559                 }
1560                 break;
1561         case TERM_MODE_FB:
1562                 VFS_WriteAt(
1563                         giVT_OutputDevHandle,
1564                         0,
1565                         Term->Width*Term->Height*sizeof(Uint32),
1566                         Term->Buffer
1567                         );
1568                 break;
1569         }
1570 }
1571
1572 /**
1573  * \brief Update the screen mode
1574  * \param Term  Terminal to update
1575  * \param NewMode       New mode to set
1576  * \param NewWidth      New framebuffer width
1577  * \param NewHeight     New framebuffer height
1578  */
1579 void VT_int_ChangeMode(tVTerm *Term, int NewMode, int NewWidth, int NewHeight)
1580 {
1581         // TODO: Increase RealWidth/RealHeight when this happens
1582         if(NewWidth > giVT_RealWidth)   NewWidth = giVT_RealWidth;
1583         if(NewHeight > giVT_RealHeight) NewHeight = giVT_RealHeight;
1584         
1585         Term->Mode = NewMode;
1586
1587         if(NewWidth != Term->Width || NewHeight != Term->Height)
1588         {
1589                  int    oldW = Term->Width;
1590                  int    oldTW = Term->TextWidth;
1591                  int    oldH = Term->Height;
1592                  int    oldTH = Term->TextHeight;
1593                 tVT_Char        *oldTBuf = Term->Text;
1594                 Uint32  *oldFB = Term->Buffer;
1595                  int    w, h, i;
1596                 // Calculate new dimensions
1597                 Term->Width = NewWidth;
1598                 Term->Height = NewHeight;
1599                 Term->TextWidth = NewWidth / giVT_CharWidth;
1600                 Term->TextHeight = NewHeight / giVT_CharHeight;
1601                 Term->ScrollHeight = Term->TextHeight - (oldTH - Term->ScrollHeight) - Term->ScrollTop;
1602         
1603                 // Allocate new buffers
1604                 // - Text
1605                 Term->Text = calloc(
1606                         Term->TextWidth * Term->TextHeight * (giVT_Scrollback+1),
1607                         sizeof(tVT_Char)
1608                         );
1609                 if(oldTBuf) {
1610                         // Copy old buffer
1611                         w = (oldTW > Term->TextWidth) ? Term->TextWidth : oldTW;
1612                         h = (oldTH > Term->TextHeight) ? Term->TextHeight : oldTH;
1613                         h *= giVT_Scrollback + 1;
1614                         for( i = 0; i < h; i ++ )
1615                         {
1616                                 memcpy(
1617                                         &Term->Text[i*Term->TextWidth],
1618                                         &oldTBuf[i*oldTW],
1619                                         w*sizeof(tVT_Char)
1620                                         );      
1621                         }
1622                         free(oldTBuf);
1623                 }
1624                 
1625                 // - Alternate Text
1626                 Term->AltBuf = realloc(
1627                         Term->AltBuf,
1628                         Term->TextWidth * Term->TextHeight * sizeof(tVT_Char)
1629                         );
1630                 
1631                 // - Framebuffer
1632                 Term->Buffer = calloc( Term->Width * Term->Height, sizeof(Uint32) );
1633                 if(oldFB) {
1634                         // Copy old buffer
1635                         w = (oldW > Term->Width) ? Term->Width : oldW;
1636                         h = (oldH > Term->Height) ? Term->Height : oldH;
1637                         for( i = 0; i < h; i ++ )
1638                         {
1639                                 memcpy(
1640                                         &Term->Buffer[i*Term->Width],
1641                                         &oldFB[i*oldW],
1642                                         w*sizeof(Uint32)
1643                                         );
1644                         }
1645                         free(oldFB);
1646                 }
1647                 
1648                 // Debug
1649                 switch(NewMode)
1650                 {
1651                 case TERM_MODE_TEXT:
1652                         Log_Log("VTerm", "Set VT %p to text mode (%ix%i)",
1653                                 Term, Term->TextWidth, Term->TextHeight);
1654                         break;
1655                 case TERM_MODE_FB:
1656                         Log_Log("VTerm", "Set VT %p to framebuffer mode (%ix%i)",
1657                                 Term, Term->Width, Term->Height);
1658                         break;
1659                 //case TERM_MODE_2DACCEL:
1660                 //case TERM_MODE_3DACCEL:
1661                 //      return;
1662                 }
1663         }
1664         
1665 }
1666
1667
1668 void VT_int_ToggleAltBuffer(tVTerm *Term, int Enabled)
1669 {       
1670         if(Enabled)
1671                 Term->Flags |= VT_FLAG_ALTBUF;
1672         else
1673                 Term->Flags &= ~VT_FLAG_ALTBUF;
1674         VT_int_UpdateScreen(Term, 1);
1675 }
1676
1677 // ---
1678 // Font Render
1679 // ---
1680 #define MONOSPACE_FONT  10816
1681
1682 #if MONOSPACE_FONT == 10808     // 8x8
1683 # include "vterm_font_8x8.h"
1684 #elif MONOSPACE_FONT == 10816   // 8x16
1685 # include "vterm_font_8x16.h"
1686 #endif
1687
1688 // === PROTOTYPES ===
1689 Uint8   *VT_Font_GetChar(Uint32 Codepoint);
1690
1691 // === GLOBALS ===
1692 int     giVT_CharWidth = FONT_WIDTH;
1693 int     giVT_CharHeight = FONT_HEIGHT;
1694
1695 // === CODE ===
1696 /**
1697  * \brief Render a font character
1698  */
1699 void VT_Font_Render(Uint32 Codepoint, void *Buffer, int Depth, int Pitch, Uint32 BGC, Uint32 FGC)
1700 {
1701         Uint8   *font;
1702          int    x, y;
1703         
1704         // 8-bpp and below
1705         if( Depth <= 8 )
1706         {
1707                 Uint8   *buf = Buffer;
1708                 
1709                 font = VT_Font_GetChar(Codepoint);
1710                 
1711                 for(y = 0; y < FONT_HEIGHT; y ++)
1712                 {
1713                         for(x = 0; x < FONT_WIDTH; x ++)
1714                         {
1715                                 if(*font & (1 << (FONT_WIDTH-x-1)))
1716                                         buf[x] = FGC;
1717                                 else
1718                                         buf[x] = BGC;
1719                         }
1720                         buf = (void*)( (tVAddr)buf + Pitch );
1721                         font ++;
1722                 }
1723         }
1724         // 16-bpp and below
1725         else if( Depth <= 16 )
1726         {
1727                 Uint16  *buf = Buffer;
1728                 
1729                 font = VT_Font_GetChar(Codepoint);
1730                 
1731                 for(y = 0; y < FONT_HEIGHT; y ++)
1732                 {
1733                         for(x = 0; x < FONT_WIDTH; x ++)
1734                         {
1735                                 if(*font & (1 << (FONT_WIDTH-x-1)))
1736                                         buf[x] = FGC;
1737                                 else
1738                                         buf[x] = BGC;
1739                         }
1740                         buf = (void*)( (tVAddr)buf + Pitch );
1741                         font ++;
1742                 }
1743         }
1744         // 24-bpp colour
1745         // - Special handling to not overwrite the next pixel
1746         //TODO: Endian issues here
1747         else if( Depth == 24 )
1748         {
1749                 Uint8   *buf = Buffer;
1750                 Uint8   bg_r = (BGC >> 16) & 0xFF;
1751                 Uint8   bg_g = (BGC >>  8) & 0xFF;
1752                 Uint8   bg_b = (BGC >>  0) & 0xFF;
1753                 Uint8   fg_r = (FGC >> 16) & 0xFF;
1754                 Uint8   fg_g = (FGC >>  8) & 0xFF;
1755                 Uint8   fg_b = (FGC >>  0) & 0xFF;
1756                 
1757                 font = VT_Font_GetChar(Codepoint);
1758                 
1759                 for(y = 0; y < FONT_HEIGHT; y ++)
1760                 {
1761                         for(x = 0; x < FONT_WIDTH; x ++)
1762                         {
1763                                 Uint8   r, g, b;
1764                                 
1765                                 if(*font & (1 << (FONT_WIDTH-x-1))) {
1766                                         r = fg_r;       g = fg_g;       b = fg_b;
1767                                 }
1768                                 else {
1769                                         r = bg_r;       g = bg_g;       b = bg_b;
1770                                 }
1771                                 buf[x*3+0] = b;
1772                                 buf[x*3+1] = g;
1773                                 buf[x*3+2] = r;
1774                         }
1775                         buf = (void*)( (tVAddr)buf + Pitch );
1776                         font ++;
1777                 }
1778         }
1779         // 32-bpp colour (nice and easy)
1780         else if( Depth == 32 )
1781         {
1782                 Uint32  *buf = Buffer;
1783                 
1784                 font = VT_Font_GetChar(Codepoint);
1785                 
1786                 for(y = 0; y < FONT_HEIGHT; y ++)
1787                 {
1788                         for(x = 0; x < FONT_WIDTH; x ++)
1789                         {
1790                                 if(*font & (1 << (FONT_WIDTH-x-1)))
1791                                         buf[x] = FGC;
1792                                 else
1793                                         buf[x] = BGC;
1794                         }
1795                         buf = (Uint32*)( (tVAddr)buf + Pitch );
1796                         font ++;
1797                 }
1798         }
1799 }
1800
1801 /**
1802  * \fn Uint32 VT_Colour12to24(Uint16 Col12)
1803  * \brief Converts a 12-bit colour into 24 bits
1804  */
1805 Uint32 VT_Colour12to24(Uint16 Col12)
1806 {
1807         Uint32  ret;
1808          int    tmp;
1809         tmp = Col12 & 0xF;
1810         ret  = (tmp << 0) | (tmp << 4);
1811         tmp = (Col12 & 0xF0) >> 4;
1812         ret |= (tmp << 8) | (tmp << 12);
1813         tmp = (Col12 & 0xF00) >> 8;
1814         ret |= (tmp << 16) | (tmp << 20);
1815         return ret;
1816 }
1817 /**
1818  * \brief Converts a 12-bit colour into 15 bits
1819  */
1820 Uint16 VT_Colour12to15(Uint16 Col12)
1821 {
1822         Uint32  ret;
1823          int    tmp;
1824         tmp = Col12 & 0xF;
1825         ret  = (tmp << 1) | (tmp & 1);
1826         tmp = (Col12 & 0xF0) >> 4;
1827         ret |= ( (tmp << 1) | (tmp & 1) ) << 5;
1828         tmp = (Col12 & 0xF00) >> 8;
1829         ret |= ( (tmp << 1) | (tmp & 1) ) << 10;
1830         return ret;
1831 }
1832
1833 /**
1834  * \brief Converts a 12-bit colour into any other depth
1835  * \param Col12 12-bit source colour
1836  * \param Depth Desired bit deptj
1837  * \note Green then blue get the extra avaliable bits (16:5-6-5, 14:4-5-5)
1838  */
1839 Uint32 VT_Colour12toN(Uint16 Col12, int Depth)
1840 {
1841         Uint32  ret;
1842         Uint32  r, g, b;
1843          int    rSize, gSize, bSize;
1844         
1845         // Fast returns
1846         if( Depth == 24 )       return VT_Colour12to24(Col12);
1847         if( Depth == 15 )       return VT_Colour12to15(Col12);
1848         // - 32 is a special case, it's usually 24-bit colour with an unused byte
1849         if( Depth == 32 )       return VT_Colour12to24(Col12);
1850         
1851         // Bounds checks
1852         if( Depth < 8 ) return 0;
1853         if( Depth > 32 )        return 0;
1854         
1855         r = Col12 & 0xF;
1856         g = (Col12 & 0xF0) >> 4;
1857         b = (Col12 & 0xF00) >> 8;
1858         
1859         rSize = gSize = bSize = Depth / 3;
1860         if( rSize + gSize + bSize < Depth )     // Depth % 3 == 1
1861                 gSize ++;
1862         if( rSize + gSize + bSize < Depth )     // Depth % 3 == 2
1863                 bSize ++;
1864         
1865         // Expand
1866         r <<= rSize - 4;        g <<= gSize - 4;        b <<= bSize - 4;
1867         // Fill with the lowest bit
1868         if( Col12 & 0x001 )     r |= (1 << (rSize - 4)) - 1;
1869         if( Col12 & 0x010 )     r |= (1 << (gSize - 4)) - 1;
1870         if( Col12 & 0x100 )     r |= (1 << (bSize - 4)) - 1;
1871         
1872         // Create output
1873         ret  = r;
1874         ret |= g << rSize;
1875         ret |= b << (rSize + gSize);
1876         
1877         return ret;
1878 }
1879
1880 /**
1881  * \fn Uint8 *VT_Font_GetChar(Uint32 Codepoint)
1882  * \brief Gets an index into the font array given a Unicode Codepoint
1883  * \note See http://en.wikipedia.org/wiki/CP437
1884  */
1885 Uint8 *VT_Font_GetChar(Uint32 Codepoint)
1886 {
1887          int    index = 0;
1888         if(Codepoint < 128)
1889                 return &VTermFont[Codepoint*FONT_HEIGHT];
1890         switch(Codepoint)
1891         {
1892         case 0xC7:      index = 128;    break;  // Ç
1893         case 0xFC:      index = 129;    break;  // ü
1894         case 0xE9:      index = 130;    break;  // é
1895         case 0xE2:      index = 131;    break;  // â
1896         case 0xE4:      index = 132;    break;  // ä
1897         case 0xE0:      index = 133;    break;  // à
1898         case 0xE5:      index = 134;    break;  // å
1899         case 0xE7:      index = 135;    break;  // ç
1900         case 0xEA:      index = 136;    break;  // ê
1901         case 0xEB:      index = 137;    break;  // ë
1902         case 0xE8:      index = 138;    break;  // è
1903         case 0xEF:      index = 139;    break;  // ï
1904         case 0xEE:      index = 140;    break;  // î
1905         case 0xEC:      index = 141;    break;  // ì
1906         case 0xC4:      index = 142;    break;  // Ä
1907         case 0xC5:      index = 143;    break;  // Å
1908         }
1909         
1910         return &VTermFont[index*FONT_HEIGHT];
1911 }
1912
1913 EXPORTAS(&giVT_CharWidth, giVT_CharWidth);
1914 EXPORTAS(&giVT_CharHeight, giVT_CharHeight);
1915 EXPORT(VT_Font_Render);
1916 EXPORT(VT_Colour12to24);

UCC git Repository :: git.ucc.asn.au