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

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