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

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