Kernel/vterm - Changed VTerm to be less x86 centric
[tpg/acess2.git] / Kernel / drv / vterm.c
1 /*
2  * Acess2 Virtual Terminal Driver
3  */
4 #define DEBUG   0
5 #include <acess.h>
6 #include <fs_devfs.h>
7 #include <modules.h>
8 #include <api_drv_video.h>
9 #include <api_drv_keyboard.h>
10 #include <api_drv_terminal.h>
11 #include <errno.h>
12 #include <semaphore.h>
13
14 // === CONSTANTS ===
15 #define VERSION ((0<<8)|(50))
16
17 #define NUM_VTS 8
18 #define MAX_INPUT_CHARS32       64
19 #define MAX_INPUT_CHARS8        (MAX_INPUT_CHARS32*4)
20 //#define DEFAULT_OUTPUT        "BochsGA"
21 #define DEFAULT_OUTPUT  "Vesa"
22 #define FALLBACK_OUTPUT "x86_VGAText"
23 #define DEFAULT_INPUT   "PS2Keyboard"
24 #define DEFAULT_WIDTH   640
25 #define DEFAULT_HEIGHT  480
26 #define DEFAULT_SCROLLBACK      2       // 2 Screens of text + current screen
27 //#define DEFAULT_SCROLLBACK    0
28 #define DEFAULT_COLOUR  (VT_COL_BLACK|(0xAAA<<16))
29
30 #define VT_FLAG_HIDECSR 0x01
31 #define VT_FLAG_ALTBUF  0x02    //!< Alternate screen buffer
32 #define VT_FLAG_RAWIN   0x04    //!< Don't handle ^Z/^C/^V
33 #define VT_FLAG_HASFB   0x10    //!< Set if the VTerm has requested the Framebuffer
34
35 enum eVT_InModes {
36         VT_INMODE_TEXT8,        // UTF-8 Text Mode (VT100/xterm Emulation)
37         VT_INMODE_TEXT32,       // UTF-32 Text Mode (Acess Native)
38         NUM_VT_INMODES
39 };
40
41 // === TYPES ===
42 typedef struct {
43          int    Mode;   //!< Current Mode (see ::eTplTerminal_Modes)
44          int    Flags;  //!< Flags (see VT_FLAG_*)
45         
46         short   NewWidth;       //!< Un-applied dimensions (Width)
47         short   NewHeight;      //!< Un-applied dimensions (Height)
48         short   Width;  //!< Virtual Width
49         short   Height; //!< Virtual Height
50         short   TextWidth;      //!< Text Virtual Width
51         short   TextHeight;     //!< Text Virtual Height
52         
53         Uint32  CurColour;      //!< Current Text Colour
54         
55          int    ViewPos;        //!< View Buffer Offset (Text Only)
56          int    WritePos;       //!< Write Buffer Offset (Text Only)
57         tVT_Char        *Text;
58         
59         tVT_Char        *AltBuf;        //!< Alternate Screen Buffer
60          int    AltWritePos;    //!< Alternate write position
61         short   ScrollTop;      //!< Top of scrolling region (smallest)
62         short   ScrollHeight;   //!< Length of scrolling region
63         
64         tMutex  ReadingLock;    //!< Lock the VTerm when a process is reading from it
65         tTID    ReadingThread;  //!< Owner of the lock
66          int    InputRead;      //!< Input buffer read position
67          int    InputWrite;     //!< Input buffer write position
68         char    InputBuffer[MAX_INPUT_CHARS8];
69 //      tSemaphore      InputSemaphore;
70         
71         Uint32          *Buffer;
72         
73         char    Name[2];        //!< Name of the terminal
74         tVFS_Node       Node;
75 } tVTerm;
76
77 // === IMPORTS ===
78 extern void     Debug_SetKTerminal(const char *File);
79
80 // === PROTOTYPES ===
81  int    VT_Install(char **Arguments);
82 void    VT_InitOutput(void);
83 void    VT_InitInput(void);
84 char    *VT_ReadDir(tVFS_Node *Node, int Pos);
85 tVFS_Node       *VT_FindDir(tVFS_Node *Node, const char *Name);
86  int    VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data);
87 Uint64  VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
88 Uint64  VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
89  int    VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data);
90 void    VT_SetResolution(int Width, int Height);
91 void    VT_SetMode(int Mode);
92 void    VT_SetTerminal(int ID);
93 void    VT_KBCallBack(Uint32 Codepoint);
94 void    VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count);
95 void    VT_int_ClearLine(tVTerm *Term, int Num);
96  int    VT_int_ParseEscape(tVTerm *Term, char *Buffer);
97 void    VT_int_PutChar(tVTerm *Term, Uint32 Ch);
98 void    VT_int_ScrollText(tVTerm *Term, int Count);
99 void    VT_int_ScrollFramebuffer( tVTerm *Term, int Count );
100 void    VT_int_UpdateScreen( tVTerm *Term, int UpdateAll );
101 void    VT_int_ChangeMode(tVTerm *Term, int NewMode, int NewWidth, int NewHeight);
102 void    VT_int_ToggleAltBuffer(tVTerm *Term, int Enabled);
103
104 // === CONSTANTS ===
105 const Uint16    caVT100Colours[] = {
106                 // Black, Red, Green, Yellow, Blue, Purple, Cyan, Gray
107                 // Same again, but bright
108                 VT_COL_BLACK, 0x700, 0x070, 0x770, 0x007, 0x707, 0x077, 0xAAA,
109                 VT_COL_GREY, 0xF00, 0x0F0, 0xFF0, 0x00F, 0xF0F, 0x0FF, VT_COL_WHITE
110         };
111
112 // === GLOBALS ===
113 MODULE_DEFINE(0, VERSION, VTerm, VT_Install, NULL, DEFAULT_INPUT, NULL);
114 tDevFS_Driver   gVT_DrvInfo = {
115         NULL, "VTerm",
116         {
117         .Flags = VFS_FFLAG_DIRECTORY,
118         .Size = NUM_VTS,
119         .Inode = -1,
120         .NumACLs = 0,
121         .ReadDir = VT_ReadDir,
122         .FindDir = VT_FindDir,
123         .IOCtl = VT_Root_IOCtl
124         }
125 };
126 // --- Terminals ---
127 tVTerm  gVT_Terminals[NUM_VTS];
128  int    giVT_CurrentTerminal = 0;
129 tVTerm  *gpVT_CurTerm = &gVT_Terminals[0];
130 // --- Video State ---
131 short   giVT_RealWidth  = DEFAULT_WIDTH;        //!< Screen Width
132 short   giVT_RealHeight = DEFAULT_HEIGHT;       //!< Screen Height
133  int    giVT_Scrollback = DEFAULT_SCROLLBACK;
134 // --- Driver Handles ---
135 char    *gsVT_OutputDevice = NULL;
136 char    *gsVT_InputDevice = NULL;
137  int    giVT_OutputDevHandle = -2;
138  int    giVT_InputDevHandle = -2;
139 // --- Key States --- (Used for VT Switching/Magic Combos)
140  int    gbVT_CtrlDown = 0;
141  int    gbVT_AltDown = 0;
142  int    gbVT_SysrqDown = 0;
143
144 // === CODE ===
145 /**
146  * \fn int VT_Install(char **Arguments)
147  * \brief Installs the Virtual Terminal Driver
148  */
149 int VT_Install(char **Arguments)
150 {
151          int    i;
152         
153         // Scan Arguments
154         if(Arguments)
155         {
156                 char    **args;
157                 const char      *arg;
158                 for(args = Arguments; (arg = *args); args++ )
159                 {
160                         char    data[strlen(arg)+1];
161                         char    *opt = data;
162                         char    *val;
163                         
164                         val = strchr(arg, '=');
165                         strcpy(data, arg);
166                         if( val ) {
167                                 data[ val - arg ] = '\0';
168                                 val ++;
169                         }
170                         Log_Debug("VTerm", "Argument '%s'", arg);
171                         
172                         if( strcmp(opt, "Video") == 0 ) {
173                                 if( !gsVT_OutputDevice )
174                                         gsVT_OutputDevice = strdup(val);
175                         }
176                         else if( strcmp(opt, "Input") == 0 ) {
177                                 if( !gsVT_InputDevice )
178                                         gsVT_InputDevice = strdup(val);
179                         }
180                         else if( strcmp(opt, "Width") == 0 ) {
181                                 giVT_RealWidth = atoi( val );
182                         }
183                         else if( strcmp(opt, "Height") == 0 ) {
184                                 giVT_RealHeight = atoi( val );
185                         }
186                         else if( strcmp(opt, "Scrollback") == 0 ) {
187                                 giVT_Scrollback = atoi( val );
188                         }
189                 }
190         }
191         
192         // Apply Defaults
193         if(!gsVT_OutputDevice)  gsVT_OutputDevice = (char*)DEFAULT_OUTPUT;
194         else if( Module_EnsureLoaded( gsVT_OutputDevice ) )     gsVT_OutputDevice = (char*)DEFAULT_OUTPUT;
195         if( Module_EnsureLoaded( gsVT_OutputDevice ) )  gsVT_OutputDevice = (char*)FALLBACK_OUTPUT;
196         if( Module_EnsureLoaded( gsVT_OutputDevice ) ) {
197                 Log_Error("VTerm", "Fallback video '%s' is not avaliable, giving up", FALLBACK_OUTPUT);
198                 return MODULE_ERR_MISC;
199         }
200         
201         if(!gsVT_InputDevice)   gsVT_InputDevice = (char*)DEFAULT_INPUT;
202         else if( Module_EnsureLoaded( gsVT_InputDevice ) )      gsVT_InputDevice = (char*)DEFAULT_INPUT;
203         
204         // Create device paths
205         {
206                 char    *tmp;
207                 tmp = malloc( 9 + strlen(gsVT_OutputDevice) + 1 );
208                 strcpy(tmp, "/Devices/");
209                 strcpy(&tmp[9], gsVT_OutputDevice);
210                 gsVT_OutputDevice = tmp;
211
212                 tmp = malloc( 9 + strlen(gsVT_InputDevice) + 1 );
213                 strcpy(tmp, "/Devices/");
214                 strcpy(&tmp[9], gsVT_InputDevice);
215                 gsVT_InputDevice = tmp;
216         }
217         
218         Log_Log("VTerm", "Using '%s' as output", gsVT_OutputDevice);
219         Log_Log("VTerm", "Using '%s' as input", gsVT_InputDevice);
220         
221         VT_InitOutput();
222         VT_InitInput();
223         
224         // Create Nodes
225         for( i = 0; i < NUM_VTS; i++ )
226         {
227                 gVT_Terminals[i].Mode = TERM_MODE_TEXT;
228                 gVT_Terminals[i].Flags = 0;
229                 gVT_Terminals[i].CurColour = DEFAULT_COLOUR;
230                 gVT_Terminals[i].WritePos = 0;
231                 gVT_Terminals[i].AltWritePos = 0;
232                 gVT_Terminals[i].ViewPos = 0;
233                 gVT_Terminals[i].ReadingThread = -1;
234                 gVT_Terminals[i].ScrollHeight = 0;
235                 
236                 // Initialise
237                 VT_int_ChangeMode( &gVT_Terminals[i],
238                         TERM_MODE_TEXT, giVT_RealWidth, giVT_RealHeight );
239                 
240                 gVT_Terminals[i].Name[0] = '0'+i;
241                 gVT_Terminals[i].Name[1] = '\0';
242                 gVT_Terminals[i].Node.Inode = i;
243                 gVT_Terminals[i].Node.ImplPtr = &gVT_Terminals[i];
244                 gVT_Terminals[i].Node.NumACLs = 0;      // Only root can open virtual terminals
245                 
246                 gVT_Terminals[i].Node.Read = VT_Read;
247                 gVT_Terminals[i].Node.Write = VT_Write;
248                 gVT_Terminals[i].Node.IOCtl = VT_Terminal_IOCtl;
249 //              Semaphore_Init(&gVT_Terminals[i].InputSemaphore, 0, MAX_INPUT_CHARS8, "VTerm", gVT_Terminals[i].Name);
250         }
251         
252         // Add to DevFS
253         DevFS_AddDevice( &gVT_DrvInfo );
254         
255         // Set kernel output to VT0
256         Debug_SetKTerminal("/Devices/VTerm/0");
257         
258         return MODULE_ERR_OK;
259 }
260
261 /**
262  * \fn void VT_InitOutput()
263  * \brief Initialise Video Output
264  */
265 void VT_InitOutput()
266 {
267         giVT_OutputDevHandle = VFS_Open(gsVT_OutputDevice, VFS_OPENFLAG_WRITE);
268         if(giVT_OutputDevHandle == -1) {
269                 Log_Warning("VTerm", "Oh F**k, I can't open the video device '%s'", gsVT_OutputDevice);
270                 return ;
271         }
272         VT_SetResolution( giVT_RealWidth, giVT_RealHeight );
273         VT_SetTerminal( 0 );
274         VT_SetMode( VIDEO_BUFFMT_TEXT );
275 }
276
277 /**
278  * \fn void VT_InitInput()
279  * \brief Initialises the input
280  */
281 void VT_InitInput()
282 {
283         giVT_InputDevHandle = VFS_Open(gsVT_InputDevice, VFS_OPENFLAG_READ);
284         if(giVT_InputDevHandle == -1) {
285                 Log_Warning("VTerm", "Can't open the input device '%s'", gsVT_InputDevice);
286                 return ;
287         }
288         VFS_IOCtl(giVT_InputDevHandle, KB_IOCTL_SETCALLBACK, VT_KBCallBack);
289 }
290
291 /**
292  * \brief Set the video resolution
293  * \param Width New screen width
294  * \param Height        New screen height
295  */
296 void VT_SetResolution(int Width, int Height)
297 {
298         tVideo_IOCtl_Mode       mode = {0};
299          int    tmp;
300          int    i;
301         
302         // Create the video mode
303         mode.width = Width;
304         mode.height = Height;
305         mode.bpp = 32;
306         mode.flags = 0;
307         
308         // Set video mode
309         VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_FINDMODE, &mode );
310         tmp = mode.id;
311         if( Width != mode.width || Height != mode.height )
312         {
313                 Log_Warning("VTerm",
314                         "Selected resolution (%ix%i is not supported) by the device, using (%ix%i)",
315                         giVT_RealWidth, giVT_RealHeight,
316                         mode.width, mode.height
317                         );
318         }
319         VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_GETSETMODE, &tmp );
320         
321         // Resize text terminals if needed
322         if( gVT_Terminals[0].Text && (giVT_RealWidth != mode.width || giVT_RealHeight != mode.height) )
323         {
324                  int    newBufSize = (giVT_RealWidth/giVT_CharWidth)
325                                         *(giVT_RealHeight/giVT_CharHeight)
326                                         *(giVT_Scrollback+1);
327                 //tVT_Char      *tmp;
328                 // Resize the text terminals
329                 giVT_RealWidth = mode.width;
330                 giVT_RealHeight = mode.height;
331                 Log_Debug("VTerm", "Resizing terminals to %ix%i",
332                         giVT_RealWidth/giVT_CharWidth, giVT_RealHeight/giVT_CharHeight);
333                 for( i = 0; i < NUM_VTS; i ++ )
334                 {
335                         if( gVT_Terminals[i].Mode != TERM_MODE_TEXT )   continue;
336                         
337                         gVT_Terminals[i].TextWidth = giVT_RealWidth/giVT_CharWidth;
338                         gVT_Terminals[i].TextHeight = giVT_RealHeight/giVT_CharHeight;
339                         gVT_Terminals[i].ScrollHeight = gVT_Terminals[i].TextHeight;
340                         
341                         gVT_Terminals[i].Text = realloc(
342                                 gVT_Terminals[i].Text,
343                                 newBufSize*sizeof(tVT_Char)
344                                 );
345                 }
346         }
347 }
348
349 /**
350  * \brief Set video output buffer mode
351  */
352 void VT_SetMode(int Mode)
353 {
354         VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &Mode );
355 }
356
357 /**
358  * \fn char *VT_ReadDir(tVFS_Node *Node, int Pos)
359  * \brief Read from the VTerm Directory
360  */
361 char *VT_ReadDir(tVFS_Node *Node, int Pos)
362 {
363         if(Pos < 0)     return NULL;
364         if(Pos >= NUM_VTS)      return NULL;
365         return strdup( gVT_Terminals[Pos].Name );
366 }
367
368 /**
369  * \fn tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
370  * \brief Find an item in the VTerm directory
371  * \param Node  Root node
372  * \param Name  Name (number) of the terminal
373  */
374 tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
375 {
376          int    num;
377         
378         ENTER("pNode sName", Node, Name);
379         
380         // Open the input and output files if needed
381         if(giVT_OutputDevHandle == -2)  VT_InitOutput();
382         if(giVT_InputDevHandle == -2)   VT_InitInput();
383         
384         // Sanity check name
385         if(Name[0] < '0' || Name[0] > '9' || Name[1] != '\0') {
386                 LEAVE('n');
387                 return NULL;
388         }
389         // Get index
390         num = Name[0] - '0';
391         if(num >= NUM_VTS) {
392                 LEAVE('n');
393                 return NULL;
394         }
395         // Return node
396         LEAVE('p', &gVT_Terminals[num].Node);
397         return &gVT_Terminals[num].Node;
398 }
399
400 /**
401  * \fn int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
402  * \brief Control the VTerm Driver
403  */
404 int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
405 {
406          int    len;
407         switch(Id)
408         {
409         case DRV_IOCTL_TYPE:    return DRV_TYPE_MISC;
410         case DRV_IOCTL_IDENT:   memcpy(Data, "VT\0\0", 4);      return 0;
411         case DRV_IOCTL_VERSION: return VERSION;
412         case DRV_IOCTL_LOOKUP:  return 0;
413         
414         case 4: // Get Video Driver
415                 if(Data)        strcpy(Data, gsVT_OutputDevice);
416                 return strlen(gsVT_OutputDevice);
417         
418         case 5: // Set Video Driver
419                 if(!Data)       return -EINVAL;
420                 if(Threads_GetUID() != 0)       return -EACCES;
421                 
422                 len = strlen(Data);
423                 
424                 // TODO: Check if the string used is a heap string
425                 
426                 free(gsVT_OutputDevice);
427                 
428                 gsVT_OutputDevice = malloc(len+1);
429                 strcpy(gsVT_OutputDevice, Data);
430                 
431                 VFS_Close(giVT_OutputDevHandle);
432                 giVT_OutputDevHandle = -1;
433                 
434                 VT_InitOutput();
435                 return 1;
436         }
437         return 0;
438 }
439
440 /**
441  * \brief Read from a virtual terminal
442  */
443 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
444 {
445          int    pos = 0;
446          int    avail;
447         tVTerm  *term = &gVT_Terminals[ Node->Inode ];
448         Uint32  *codepoint_buf = Buffer;
449         Uint32  *codepoint_in;
450         
451         Mutex_Acquire( &term->ReadingLock );
452         
453         // Check current mode
454         switch(term->Mode)
455         {
456         // Text Mode (UTF-8)
457         case TERM_MODE_TEXT:
458                 VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "VT_Read (UTF-8)");
459                 
460                 avail = term->InputWrite - term->InputRead;
461                 if(avail < 0)
462                         avail += MAX_INPUT_CHARS8;
463                 if(avail > Length - pos)
464                         avail = Length - pos;
465                 
466                 while( avail -- )
467                 {
468                         ((char*)Buffer)[pos] = term->InputBuffer[term->InputRead];
469                         pos ++;
470                         term->InputRead ++;
471                         term->InputRead %= MAX_INPUT_CHARS8;
472                 }
473                 break;
474         
475         //case TERM_MODE_FB:
476         // Other - UCS-4
477         default:
478                 VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "VT_Read (UCS-4)");
479                 
480                 avail = term->InputWrite - term->InputRead;
481                 if(avail < 0)
482                         avail += MAX_INPUT_CHARS32;
483                 if(avail > Length - pos)
484                         avail = Length/4 - pos;
485                 
486                 codepoint_in = (void*)term->InputBuffer;
487                 codepoint_buf = Buffer;
488                 
489                 while( avail -- )
490                 {
491                         codepoint_buf[pos] = codepoint_in[term->InputRead];
492                         pos ++;
493                         term->InputRead ++;
494                         term->InputRead %= MAX_INPUT_CHARS32;
495                 }
496                 pos *= 4;
497                 break;
498         }
499         
500         // Mark none avaliable if buffer empty
501         if( term->InputRead == term->InputWrite )
502                 VFS_MarkAvaliable(&term->Node, 0);
503         
504         term->ReadingThread = -1;
505         
506         Mutex_Release( &term->ReadingLock );
507         
508         return pos;
509 }
510
511 /**
512  * \fn Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
513  * \brief Write to a virtual terminal
514  */
515 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
516 {
517         tVTerm  *term = &gVT_Terminals[ Node->Inode ];
518          int    size;
519         
520         // Write
521         switch( term->Mode )
522         {
523         // Print Text
524         case TERM_MODE_TEXT:
525                 VT_int_PutString(term, Buffer, Length);
526                 break;
527         // Framebuffer :)
528         case TERM_MODE_FB:
529         
530                 // - Sanity Checking
531                 size = term->Width*term->Height*4;
532                 if( Offset > size ) {
533                         Log_Notice("VTerm", "VT_Write: Offset (0x%llx) > FBSize (0x%x)",
534                                 Offset, size);
535                         return 0;
536                 }
537                 if( Offset + Length > size ) {
538                         Log_Notice("VTerm", "VT_Write: Offset+Length (0x%llx) > FBSize (0x%x)",
539                                 Offset+Length, size);
540                         Length = size - Offset;
541                 }
542                 
543                 // Copy to the local cache
544                 memcpy( (void*)((Uint)term->Buffer + (Uint)Offset), Buffer, Length );
545                 
546                 // Update screen if needed
547                 if( Node->Inode == giVT_CurrentTerminal )
548                 {
549                         // Fill entire screen?
550                         if( giVT_RealWidth > term->Width || giVT_RealHeight > term->Height )
551                         {
552                                 // No? :( Well, just center it
553                                  int    x, y, w, h;
554                                 x = Offset/4;   y = x / term->Width;    x %= term->Width;
555                                 w = Length/4+x; h = w / term->Width;    w %= term->Width;
556                                 // Center
557                                 x += (giVT_RealWidth - term->Width) / 2;
558                                 y += (giVT_RealHeight - term->Height) / 2;
559                                 while(h--)
560                                 {
561                                         VFS_WriteAt( giVT_OutputDevHandle,
562                                                 (x + y * giVT_RealWidth)*4,
563                                                 term->Width * 4,
564                                                 Buffer
565                                                 );
566                                         Buffer = (void*)( (Uint)Buffer + term->Width*4 );
567                                         y ++;
568                                 }
569                                 return 0;
570                         }
571                         else {
572                                 return VFS_WriteAt( giVT_OutputDevHandle, Offset, Length, Buffer );
573                         }
574                 }
575         // Just pass on (for now)
576         // TODO: Handle locally too to ensure no information is lost on
577         //       VT Switch (and to isolate terminals from each other)
578         case TERM_MODE_2DACCEL:
579         //case TERM_MODE_3DACCEL:
580                 if( Node->Inode == giVT_CurrentTerminal )
581                 {
582                         VFS_Write( giVT_OutputDevHandle, Length, Buffer );
583                 }
584                 break;
585         }
586         
587         return 0;
588 }
589
590 /**
591  * \fn int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
592  * \brief Call an IO Control on a virtual terminal
593  */
594 int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
595 {
596          int    *iData = Data;
597          int    ret;
598         tVTerm  *term = Node->ImplPtr;
599         ENTER("pNode iId pData", Node, Id, Data);
600         
601         if(Id >= DRV_IOCTL_LOOKUP) {
602                 // Only root can fiddle with graphics modes
603                 // TODO: Remove this and replace with user ownership
604                 if( Threads_GetUID() != 0 )     return -1;
605         }
606         
607         switch(Id)
608         {
609         // --- Core Defined
610         case DRV_IOCTL_TYPE:
611                 LEAVE('i', DRV_TYPE_TERMINAL);
612                 return DRV_TYPE_TERMINAL;
613         case DRV_IOCTL_IDENT:
614                 memcpy(Data, "VT\0\0", 4);
615                 LEAVE('i', 0);
616                 return 0;
617         case DRV_IOCTL_VERSION:
618                 LEAVE('x', VERSION);
619                 return VERSION;
620         case DRV_IOCTL_LOOKUP:
621                 LEAVE('i', 0);
622                 return 0;
623         
624         // Get/Set the mode (and apply any changes)
625         case TERM_IOCTL_MODETYPE:
626                 if(Data != NULL)
627                 {
628                         if( CheckMem(Data, sizeof(int)) == 0 ) {
629                                 LEAVE('i', -1);
630                                 return -1;
631                         }
632                         Log_Log("VTerm", "VTerm %i mode set to %i", (int)Node->Inode, *iData);
633                         
634                         // Update mode if needed
635                         if( term->Mode != *iData
636                          || term->NewWidth
637                          || term->NewHeight)
638                         {
639                                 // Adjust for text mode
640                                 if( *iData == TERM_MODE_TEXT ) {
641                                         term->NewHeight *= giVT_CharHeight;
642                                         term->NewWidth *= giVT_CharWidth;
643                                 }
644                                 // Fill unchanged dimensions
645                                 if(term->NewHeight == 0)        term->NewHeight = term->Height;
646                                 if(term->NewWidth == 0) term->NewWidth = term->Width;
647                                 // Set new mode
648                                 VT_int_ChangeMode(term, *iData, term->NewWidth, term->NewHeight);
649                                 // Clear unapplied dimensions
650                                 term->NewWidth = 0;
651                                 term->NewHeight = 0;
652                         }
653                         
654                         // Update the screen dimensions
655                         if(Node->Inode == giVT_CurrentTerminal)
656                                 VT_SetTerminal( giVT_CurrentTerminal );
657                 }
658                 LEAVE('i', term->Mode);
659                 return term->Mode;
660         
661         // Get/set the terminal width
662         case TERM_IOCTL_WIDTH:
663                 if(Data != NULL) {
664                         if( CheckMem(Data, sizeof(int)) == 0 ) {
665                                 LEAVE('i', -1);
666                                 return -1;
667                         }
668                         term->NewWidth = *iData;
669                 }
670                 if( term->NewWidth )
671                         ret = term->NewWidth;
672                 else if( term->Mode == TERM_MODE_TEXT )
673                         ret = term->TextWidth;
674                 else
675                         ret = term->Width;
676                 LEAVE('i', ret);
677                 return ret;
678         
679         // Get/set the terminal height
680         case TERM_IOCTL_HEIGHT:
681                 if(Data != NULL) {
682                         if( CheckMem(Data, sizeof(int)) == 0 ) {
683                                 LEAVE('i', -1);
684                                 return -1;
685                         }
686                         term->NewHeight = *iData;
687                 }
688                 if( term->NewHeight )
689                         ret = term->NewHeight;
690                 else if( term->Mode == TERM_MODE_TEXT )
691                         ret = term->TextHeight;
692                 else
693                         ret = term->Height;
694                 LEAVE('i', ret);
695                 return ret;
696         
697         case TERM_IOCTL_FORCESHOW:
698                 Log_Log("VTerm", "Thread %i forced VTerm %i to be shown",
699                         Threads_GetTID(), (int)Node->Inode);
700                 VT_SetTerminal( Node->Inode );
701                 LEAVE('i', 1);
702                 return 1;
703         
704         case TERM_IOCTL_GETCURSOR:
705                 ret = (term->Flags & VT_FLAG_ALTBUF) ? term->AltWritePos : term->WritePos-term->ViewPos;
706                 LEAVE('i', ret);
707                 return ret;
708         }
709         LEAVE('i', -1);
710         return -1;
711 }
712
713 /**
714  * \fn void VT_SetTerminal(int ID)
715  * \brief Set the current terminal
716  */
717 void VT_SetTerminal(int ID)
718 {       
719         // Update current terminal ID
720         Log_Log("VTerm", "Changed terminal from %i to %i", giVT_CurrentTerminal, ID);
721         giVT_CurrentTerminal = ID;
722         gpVT_CurTerm = &gVT_Terminals[ID];
723         
724         // Update cursor
725         if( gpVT_CurTerm->Text && gpVT_CurTerm->Mode == TERM_MODE_TEXT && !(gpVT_CurTerm->Flags & VT_FLAG_HIDECSR) )
726         {
727                 tVideo_IOCtl_Pos        pos;
728                  int    offset = (gpVT_CurTerm->Flags & VT_FLAG_ALTBUF) ? gpVT_CurTerm->AltWritePos : gpVT_CurTerm->WritePos - gpVT_CurTerm->ViewPos;
729                 pos.x = offset % gpVT_CurTerm->TextWidth;
730                 pos.y = offset / gpVT_CurTerm->TextWidth;
731                 if( 0 <= pos.y && pos.y < gpVT_CurTerm->TextHeight )
732                         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
733         }
734         
735         if( gpVT_CurTerm->Mode == TERM_MODE_TEXT )
736                 VT_SetMode( VIDEO_BUFFMT_TEXT );
737         else
738                 VT_SetMode( VIDEO_BUFFMT_FRAMEBUFFER );
739         
740         // Update the screen
741         VT_int_UpdateScreen( &gVT_Terminals[ ID ], 1 );
742 }
743
744 /**
745  * \fn void VT_KBCallBack(Uint32 Codepoint)
746  * \brief Called on keyboard interrupt
747  * \param Codepoint     Pseudo-UTF32 character
748  * 
749  * Handles a key press and sends the key code to the user's buffer.
750  * If the code creates a kernel-magic sequence, it is not passed to the
751  * user and is handled in-kernel.
752  */
753 void VT_KBCallBack(Uint32 Codepoint)
754 {
755         tVTerm  *term = gpVT_CurTerm;
756         
757         // How the hell did we get a codepoint of zero?
758         if(Codepoint == 0)      return;
759         
760         // Key Up
761         if( Codepoint & 0x80000000 )
762         {
763                 Codepoint &= 0x7FFFFFFF;
764                 switch(Codepoint)
765                 {
766                 case KEY_LALT:  gbVT_AltDown &= ~1;     break;
767                 case KEY_RALT:  gbVT_AltDown &= ~2;     break;
768                 case KEY_LCTRL: gbVT_CtrlDown &= ~1;    break;
769                 case KEY_RCTRL: gbVT_CtrlDown &= ~2;    break;
770                 }
771                 return;
772         }
773         
774         switch(Codepoint)
775         {
776         case KEY_LALT:  gbVT_AltDown |= 1;      break;
777         case KEY_RALT:  gbVT_AltDown |= 2;      break;
778         case KEY_LCTRL: gbVT_CtrlDown |= 1;     break;
779         case KEY_RCTRL: gbVT_CtrlDown |= 2;     break;
780         
781         default:
782                 if(!gbVT_AltDown || !gbVT_CtrlDown)
783                         break;
784                 switch(Codepoint)
785                 {
786                 case KEY_F1:    VT_SetTerminal(0);      return;
787                 case KEY_F2:    VT_SetTerminal(1);      return;
788                 case KEY_F3:    VT_SetTerminal(2);      return;
789                 case KEY_F4:    VT_SetTerminal(3);      return;
790                 case KEY_F5:    VT_SetTerminal(4);      return;
791                 case KEY_F6:    VT_SetTerminal(5);      return;
792                 case KEY_F7:    VT_SetTerminal(6);      return;
793                 case KEY_F8:    VT_SetTerminal(7);      return;
794                 case KEY_F9:    VT_SetTerminal(8);      return;
795                 case KEY_F10:   VT_SetTerminal(9);      return;
796                 case KEY_F11:   VT_SetTerminal(10);     return;
797                 case KEY_F12:   VT_SetTerminal(11);     return;
798                 // Scrolling
799                 case KEY_PGUP:
800                         if( gpVT_CurTerm->Flags & VT_FLAG_ALTBUF )
801                                 return ;
802                         if( gpVT_CurTerm->ViewPos > gpVT_CurTerm->Width )
803                                 gpVT_CurTerm->ViewPos -= gpVT_CurTerm->Width;
804                         else
805                                 gpVT_CurTerm->ViewPos = 0;
806                         return;
807                 case KEY_PGDOWN:
808                         if( gpVT_CurTerm->Flags & VT_FLAG_ALTBUF )
809                                 return ;
810                         if( gpVT_CurTerm->ViewPos < gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback) )
811                                 gpVT_CurTerm->ViewPos += gpVT_CurTerm->Width;
812                         else
813                                 gpVT_CurTerm->ViewPos = gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback);
814                         return;
815                 }
816         }
817         
818         // Encode key
819         if(term->Mode == TERM_MODE_TEXT)
820         {
821                 Uint8   buf[6] = {0};
822                  int    len = 0;
823                 
824                 // Ignore Modifer Keys
825                 if(Codepoint > KEY_MODIFIERS)   return;
826                 
827                 // Get UTF-8/ANSI Encoding
828                 switch(Codepoint)
829                 {
830                 case KEY_LEFT:
831                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'D';
832                         len = 3;
833                         break;
834                 case KEY_RIGHT:
835                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'C';
836                         len = 3;
837                         break;
838                 case KEY_UP:
839                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'A';
840                         len = 3;
841                         break;
842                 case KEY_DOWN:
843                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'B';
844                         len = 3;
845                         break;
846                 
847                 case KEY_PGUP:
848                         //buf[0] = '\x1B';      buf[1] = '[';   buf[2] = '5';   // Some overline also
849                         //len = 4;      // Commented out until I'm sure
850                         len = 0;
851                         break;
852                 case KEY_PGDOWN:
853                         len = 0;
854                         break;
855                 
856                 // Attempt to encode in UTF-8
857                 default:
858                         len = WriteUTF8( buf, Codepoint );
859                         if(len == 0) {
860                                 Warning("Codepoint (%x) is unrepresentable in UTF-8", Codepoint);
861                         }
862                         break;
863                 }
864                 
865                 if(len == 0) {
866                         // Unprintable / Don't Pass
867                         return;
868                 }
869
870 #if 0
871                 // Handle meta characters
872                 if( !(term->Flags & VT_FLAG_RAWIN) )
873                 {
874                         switch(buf[0])
875                         {
876                         case '\3':      // ^C
877                                 
878                                 break;
879                         }
880                 }
881 #endif
882                 
883                 // Write
884                 if( MAX_INPUT_CHARS8 - term->InputWrite >= len )
885                         memcpy( &term->InputBuffer[term->InputWrite], buf, len );
886                 else {
887                         memcpy( &term->InputBuffer[term->InputWrite], buf, MAX_INPUT_CHARS8 - term->InputWrite );
888                         memcpy( &term->InputBuffer[0], buf, len - (MAX_INPUT_CHARS8 - term->InputWrite) );
889                 }
890                 // Roll the buffer over
891                 term->InputWrite += len;
892                 term->InputWrite %= MAX_INPUT_CHARS8;
893                 if( (term->InputWrite - term->InputRead + MAX_INPUT_CHARS8)%MAX_INPUT_CHARS8 < len ) {
894                         term->InputRead = term->InputWrite + 1;
895                         term->InputRead %= MAX_INPUT_CHARS8;
896                 }
897         }
898         else
899         {
900                 // Encode the raw UTF-32 Key
901                 Uint32  *raw_in = (void*)term->InputBuffer;
902                 raw_in[ term->InputWrite ] = Codepoint;
903                 term->InputWrite ++;
904                 term->InputWrite %= MAX_INPUT_CHARS32;
905                 if(term->InputRead == term->InputWrite) {
906                         term->InputRead ++;
907                         term->InputRead %= MAX_INPUT_CHARS32;
908                 }
909         }
910         
911         VFS_MarkAvaliable(&term->Node, 1);
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         // Get buffer pointer and attributes    
1361         if( Term->Flags & VT_FLAG_ALTBUF )
1362         {
1363                 buf = Term->AltBuf;
1364                 height = Term->TextHeight;
1365                 init_write_pos = Term->AltWritePos;
1366                 scroll_top = Term->ScrollTop;
1367                 scroll_height = Term->ScrollHeight;
1368         }
1369         else
1370         {
1371                 buf = Term->Text;
1372                 height = Term->TextHeight*(giVT_Scrollback+1);
1373                 init_write_pos = Term->WritePos;
1374                 scroll_top = 0;
1375                 scroll_height = height;
1376         }
1377
1378         // Scroll text downwards        
1379         if( Count > 0 )
1380         {
1381                  int    base;
1382         
1383                 // Set up
1384                 if(Count > scroll_height)       Count = scroll_height;
1385                 base = Term->TextWidth*(scroll_top + scroll_height - Count);
1386                 len = Term->TextWidth*(scroll_height - Count);
1387                 
1388                 // Scroll terminal cache
1389                 memmove(
1390                         &buf[Term->TextWidth*scroll_top],
1391                         &buf[Term->TextWidth*(scroll_top+Count)],
1392                         len*sizeof(tVT_Char)
1393                         );
1394                 // Clear last rows
1395                 for( i = 0; i < Term->TextWidth*Count; i ++ )
1396                 {
1397                         buf[ base + i ].Ch = 0;
1398                         buf[ base + i ].Colour = Term->CurColour;
1399                 }
1400                 
1401                 // Update Screen
1402                 VT_int_ScrollFramebuffer( Term, Count );
1403                 if( Term->Flags & VT_FLAG_ALTBUF )
1404                         Term->AltWritePos = base;
1405                 else
1406                         Term->WritePos = Term->ViewPos + Term->TextWidth*(Term->TextHeight - Count);
1407                 for( i = 0; i < Count; i ++ )
1408                 {
1409                         VT_int_UpdateScreen( Term, 0 );
1410                         if( Term->Flags & VT_FLAG_ALTBUF )
1411                                 Term->AltWritePos += Term->TextWidth;
1412                         else
1413                                 Term->WritePos += Term->TextWidth;
1414                 }
1415         }
1416         else
1417         {
1418                 Count = -Count;
1419                 if(Count > scroll_height)       Count = scroll_height;
1420                 
1421                 len = Term->TextWidth*(scroll_height - Count);
1422                 
1423                 // Scroll terminal cache
1424                 memmove(
1425                         &buf[Term->TextWidth*(scroll_top+Count)],
1426                         &buf[Term->TextWidth*scroll_top],
1427                         len*sizeof(tVT_Char)
1428                         );
1429                 // Clear preceding rows
1430                 for( i = 0; i < Term->TextWidth*Count; i ++ )
1431                 {
1432                         buf[ i ].Ch = 0;
1433                         buf[ i ].Colour = Term->CurColour;
1434                 }
1435                 
1436                 VT_int_ScrollFramebuffer( Term, -Count );
1437                 if( Term->Flags & VT_FLAG_ALTBUF )
1438                         Term->AltWritePos = Term->TextWidth*scroll_top;
1439                 else
1440                         Term->WritePos = Term->ViewPos;
1441                 for( i = 0; i < Count; i ++ )
1442                 {
1443                         VT_int_UpdateScreen( Term, 0 );
1444                         if( Term->Flags & VT_FLAG_ALTBUF )
1445                                 Term->AltWritePos += Term->TextWidth;
1446                         else
1447                                 Term->WritePos += Term->TextWidth;
1448                 }
1449         }
1450         
1451         if( Term->Flags & VT_FLAG_ALTBUF )
1452                 Term->AltWritePos = init_write_pos;
1453         else
1454                 Term->WritePos = init_write_pos;
1455 }
1456
1457 /**
1458  * \fn void VT_int_ScrollFramebuffer( tVTerm *Term, int Count )
1459  * \note Scrolls the framebuffer down by \a Count text lines
1460  */
1461 void VT_int_ScrollFramebuffer( tVTerm *Term, int Count )
1462 {
1463          int    tmp;
1464         struct {
1465                 Uint8   Op;
1466                 Uint16  DstX, DstY;
1467                 Uint16  SrcX, SrcY;
1468                 Uint16  W, H;
1469         } PACKED        buf;
1470         
1471         // Only update if this is the current terminal
1472         if( Term != gpVT_CurTerm )      return;
1473         
1474         if( Count > Term->ScrollHeight )        Count = Term->ScrollHeight;
1475         if( Count < -Term->ScrollHeight )       Count = -Term->ScrollHeight;
1476         
1477         // Switch to 2D Command Stream
1478         tmp = VIDEO_BUFFMT_2DSTREAM;
1479         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp);
1480         
1481         // BLIT to 0,0 from 0,giVT_CharHeight
1482         buf.Op = VIDEO_2DOP_BLIT;
1483         buf.SrcX = 0;   buf.DstX = 0;
1484         // TODO: Don't assume character dimensions
1485         buf.W = Term->TextWidth * giVT_CharWidth;
1486         if( Count > 0 )
1487         {
1488                 buf.SrcY = (Term->ScrollTop+Count) * giVT_CharHeight;
1489                 buf.DstY = Term->ScrollTop * giVT_CharHeight;
1490         }
1491         else    // Scroll up, move text down
1492         {
1493                 Count = -Count;
1494                 buf.SrcY = Term->ScrollTop * giVT_CharHeight;
1495                 buf.DstY = (Term->ScrollTop+Count) * giVT_CharHeight;
1496         }
1497         buf.H = (Term->ScrollHeight-Count) * giVT_CharHeight;
1498         VFS_WriteAt(giVT_OutputDevHandle, 0, sizeof(buf), &buf);
1499         
1500         // Restore old mode (this function is only called during text mode)
1501         tmp = VIDEO_BUFFMT_TEXT;
1502         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp);
1503 }
1504
1505 /**
1506  * \fn void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
1507  * \brief Updates the video framebuffer
1508  */
1509 void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
1510 {
1511         tVT_Char        *buffer;
1512          int    view_pos, write_pos;
1513         // Only update if this is the current terminal
1514         if( Term != gpVT_CurTerm )      return;
1515         
1516         switch( Term->Mode )
1517         {
1518         case TERM_MODE_TEXT:
1519                 view_pos = (Term->Flags & VT_FLAG_ALTBUF) ? 0 : Term->ViewPos;
1520                 write_pos = (Term->Flags & VT_FLAG_ALTBUF) ? Term->AltWritePos : Term->WritePos;
1521                 buffer = (Term->Flags & VT_FLAG_ALTBUF) ? Term->AltBuf : Term->Text;
1522                 // Re copy the entire screen?
1523                 if(UpdateAll) {
1524                         VFS_WriteAt(
1525                                 giVT_OutputDevHandle,
1526                                 0,
1527                                 Term->TextWidth*Term->TextHeight*sizeof(tVT_Char),
1528                                 &buffer[view_pos]
1529                                 );
1530                 }
1531                 // Only copy the current line
1532                 else {
1533                          int    ofs = write_pos - write_pos % Term->TextWidth;
1534                         VFS_WriteAt(
1535                                 giVT_OutputDevHandle,
1536                                 (ofs - view_pos)*sizeof(tVT_Char),
1537                                 Term->TextWidth*sizeof(tVT_Char),
1538                                 &buffer[ofs]
1539                                 );
1540                 }
1541                 break;
1542         case TERM_MODE_FB:
1543                 VFS_WriteAt(
1544                         giVT_OutputDevHandle,
1545                         0,
1546                         Term->Width*Term->Height*sizeof(Uint32),
1547                         Term->Buffer
1548                         );
1549                 break;
1550         }
1551 }
1552
1553 /**
1554  * \brief Update the screen mode
1555  * \param Term  Terminal to update
1556  * \param NewMode       New mode to set
1557  * \param NewWidth      New framebuffer width
1558  * \param NewHeight     New framebuffer height
1559  */
1560 void VT_int_ChangeMode(tVTerm *Term, int NewMode, int NewWidth, int NewHeight)
1561 {
1562         // TODO: Increase RealWidth/RealHeight when this happens
1563         if(NewWidth > giVT_RealWidth)   NewWidth = giVT_RealWidth;
1564         if(NewHeight > giVT_RealHeight) NewHeight = giVT_RealHeight;
1565         
1566         Term->Mode = NewMode;
1567
1568         if(NewWidth != Term->Width || NewHeight != Term->Height)
1569         {
1570                  int    oldW = Term->Width;
1571                  int    oldTW = Term->TextWidth;
1572                  int    oldH = Term->Height;
1573                  int    oldTH = Term->TextHeight;
1574                 tVT_Char        *oldTBuf = Term->Text;
1575                 Uint32  *oldFB = Term->Buffer;
1576                  int    w, h, i;
1577                 // Calculate new dimensions
1578                 Term->Width = NewWidth;
1579                 Term->Height = NewHeight;
1580                 Term->TextWidth = NewWidth / giVT_CharWidth;
1581                 Term->TextHeight = NewHeight / giVT_CharHeight;
1582                 Term->ScrollHeight = Term->TextHeight - (oldTH - Term->ScrollHeight) - Term->ScrollTop;
1583         
1584                 // Allocate new buffers
1585                 // - Text
1586                 Term->Text = calloc(
1587                         Term->TextWidth * Term->TextHeight * (giVT_Scrollback+1),
1588                         sizeof(tVT_Char)
1589                         );
1590                 if(oldTBuf) {
1591                         // Copy old buffer
1592                         w = (oldTW > Term->TextWidth) ? Term->TextWidth : oldTW;
1593                         h = (oldTH > Term->TextHeight) ? Term->TextHeight : oldTH;
1594                         h *= giVT_Scrollback + 1;
1595                         for( i = 0; i < h; i ++ )
1596                         {
1597                                 memcpy(
1598                                         &Term->Text[i*Term->TextWidth],
1599                                         &oldTBuf[i*oldTW],
1600                                         w*sizeof(tVT_Char)
1601                                         );      
1602                         }
1603                         free(oldTBuf);
1604                 }
1605                 
1606                 // - Alternate Text
1607                 Term->AltBuf = realloc(
1608                         Term->AltBuf,
1609                         Term->TextWidth * Term->TextHeight * sizeof(tVT_Char)
1610                         );
1611                 
1612                 // - Framebuffer
1613                 Term->Buffer = calloc( Term->Width * Term->Height, sizeof(Uint32) );
1614                 if(oldFB) {
1615                         // Copy old buffer
1616                         w = (oldW > Term->Width) ? Term->Width : oldW;
1617                         h = (oldH > Term->Height) ? Term->Height : oldH;
1618                         for( i = 0; i < h; i ++ )
1619                         {
1620                                 memcpy(
1621                                         &Term->Buffer[i*Term->Width],
1622                                         &oldFB[i*oldW],
1623                                         w*sizeof(Uint32)
1624                                         );
1625                         }
1626                         free(oldFB);
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
1648
1649 void VT_int_ToggleAltBuffer(tVTerm *Term, int Enabled)
1650 {       
1651         if(Enabled)
1652                 Term->Flags |= VT_FLAG_ALTBUF;
1653         else
1654                 Term->Flags &= ~VT_FLAG_ALTBUF;
1655         VT_int_UpdateScreen(Term, 1);
1656 }
1657
1658 // ---
1659 // Font Render
1660 // ---
1661 #define MONOSPACE_FONT  10816
1662
1663 #if MONOSPACE_FONT == 10808     // 8x8
1664 # include "vterm_font_8x8.h"
1665 #elif MONOSPACE_FONT == 10816   // 8x16
1666 # include "vterm_font_8x16.h"
1667 #endif
1668
1669 // === PROTOTYPES ===
1670 Uint8   *VT_Font_GetChar(Uint32 Codepoint);
1671
1672 // === GLOBALS ===
1673 int     giVT_CharWidth = FONT_WIDTH;
1674 int     giVT_CharHeight = FONT_HEIGHT;
1675
1676 // === CODE ===
1677 /**
1678  * \brief Render a font character
1679  */
1680 void VT_Font_Render(Uint32 Codepoint, void *Buffer, int Depth, int Pitch, Uint32 BGC, Uint32 FGC)
1681 {
1682         Uint8   *font;
1683          int    x, y;
1684         
1685         // 8-bpp and below
1686         if( Depth <= 8 )
1687         {
1688                 Uint8   *buf = Buffer;
1689                 
1690                 font = VT_Font_GetChar(Codepoint);
1691                 
1692                 for(y = 0; y < FONT_HEIGHT; y ++)
1693                 {
1694                         for(x = 0; x < FONT_WIDTH; x ++)
1695                         {
1696                                 if(*font & (1 << (FONT_WIDTH-x-1)))
1697                                         buf[x] = FGC;
1698                                 else
1699                                         buf[x] = BGC;
1700                         }
1701                         buf = (void*)( (tVAddr)buf + Pitch );
1702                         font ++;
1703                 }
1704         }
1705         // 16-bpp and below
1706         else if( Depth <= 16 )
1707         {
1708                 Uint16  *buf = Buffer;
1709                 
1710                 font = VT_Font_GetChar(Codepoint);
1711                 
1712                 for(y = 0; y < FONT_HEIGHT; y ++)
1713                 {
1714                         for(x = 0; x < FONT_WIDTH; x ++)
1715                         {
1716                                 if(*font & (1 << (FONT_WIDTH-x-1)))
1717                                         buf[x] = FGC;
1718                                 else
1719                                         buf[x] = BGC;
1720                         }
1721                         buf = (void*)( (tVAddr)buf + Pitch );
1722                         font ++;
1723                 }
1724         }
1725         // 24-bpp colour
1726         // - Special handling to not overwrite the next pixel
1727         //TODO: Endian issues here
1728         else if( Depth == 24 )
1729         {
1730                 Uint8   *buf = Buffer;
1731                 Uint8   bg_r = (BGC >> 16) & 0xFF;
1732                 Uint8   bg_g = (BGC >>  8) & 0xFF;
1733                 Uint8   bg_b = (BGC >>  0) & 0xFF;
1734                 Uint8   fg_r = (FGC >> 16) & 0xFF;
1735                 Uint8   fg_g = (FGC >>  8) & 0xFF;
1736                 Uint8   fg_b = (FGC >>  0) & 0xFF;
1737                 
1738                 font = VT_Font_GetChar(Codepoint);
1739                 
1740                 for(y = 0; y < FONT_HEIGHT; y ++)
1741                 {
1742                         for(x = 0; x < FONT_WIDTH; x ++)
1743                         {
1744                                 Uint8   r, g, b;
1745                                 
1746                                 if(*font & (1 << (FONT_WIDTH-x-1))) {
1747                                         r = fg_r;       g = fg_g;       b = fg_b;
1748                                 }
1749                                 else {
1750                                         r = bg_r;       g = bg_g;       b = bg_b;
1751                                 }
1752                                 buf[x*3+0] = b;
1753                                 buf[x*3+1] = g;
1754                                 buf[x*3+2] = r;
1755                         }
1756                         buf = (void*)( (tVAddr)buf + Pitch );
1757                         font ++;
1758                 }
1759         }
1760         // 32-bpp colour (nice and easy)
1761         else if( Depth == 32 )
1762         {
1763                 Uint32  *buf = Buffer;
1764                 
1765                 font = VT_Font_GetChar(Codepoint);
1766                 
1767                 for(y = 0; y < FONT_HEIGHT; y ++)
1768                 {
1769                         for(x = 0; x < FONT_WIDTH; x ++)
1770                         {
1771                                 if(*font & (1 << (FONT_WIDTH-x-1)))
1772                                         buf[x] = FGC;
1773                                 else
1774                                         buf[x] = BGC;
1775                         }
1776                         buf = (Uint32*)( (tVAddr)buf + Pitch );
1777                         font ++;
1778                 }
1779         }
1780 }
1781
1782 /**
1783  * \fn Uint32 VT_Colour12to24(Uint16 Col12)
1784  * \brief Converts a 12-bit colour into 24 bits
1785  */
1786 Uint32 VT_Colour12to24(Uint16 Col12)
1787 {
1788         Uint32  ret;
1789          int    tmp;
1790         tmp = Col12 & 0xF;
1791         ret  = (tmp << 0) | (tmp << 4);
1792         tmp = (Col12 & 0xF0) >> 4;
1793         ret |= (tmp << 8) | (tmp << 12);
1794         tmp = (Col12 & 0xF00) >> 8;
1795         ret |= (tmp << 16) | (tmp << 20);
1796         return ret;
1797 }
1798 /**
1799  * \brief Converts a 12-bit colour into 15 bits
1800  */
1801 Uint16 VT_Colour12to15(Uint16 Col12)
1802 {
1803         Uint32  ret;
1804          int    tmp;
1805         tmp = Col12 & 0xF;
1806         ret  = (tmp << 1) | (tmp & 1);
1807         tmp = (Col12 & 0xF0) >> 4;
1808         ret |= ( (tmp << 1) | (tmp & 1) ) << 5;
1809         tmp = (Col12 & 0xF00) >> 8;
1810         ret |= ( (tmp << 1) | (tmp & 1) ) << 10;
1811         return ret;
1812 }
1813
1814 /**
1815  * \brief Converts a 12-bit colour into any other depth
1816  * \param Col12 12-bit source colour
1817  * \param Depth Desired bit deptj
1818  * \note Green then blue get the extra avaliable bits (16:5-6-5, 14:4-5-5)
1819  */
1820 Uint32 VT_Colour12toN(Uint16 Col12, int Depth)
1821 {
1822         Uint32  ret;
1823         Uint32  r, g, b;
1824          int    rSize, gSize, bSize;
1825         
1826         // Fast returns
1827         if( Depth == 24 )       return VT_Colour12to24(Col12);
1828         if( Depth == 15 )       return VT_Colour12to15(Col12);
1829         // - 32 is a special case, it's usually 24-bit colour with an unused byte
1830         if( Depth == 32 )       return VT_Colour12to24(Col12);
1831         
1832         // Bounds checks
1833         if( Depth < 8 ) return 0;
1834         if( Depth > 32 )        return 0;
1835         
1836         r = Col12 & 0xF;
1837         g = (Col12 & 0xF0) >> 4;
1838         b = (Col12 & 0xF00) >> 8;
1839         
1840         rSize = gSize = bSize = Depth / 3;
1841         if( rSize + gSize + bSize < Depth )     // Depth % 3 == 1
1842                 gSize ++;
1843         if( rSize + gSize + bSize < Depth )     // Depth % 3 == 2
1844                 bSize ++;
1845         
1846         // Expand
1847         r <<= rSize - 4;        g <<= gSize - 4;        b <<= bSize - 4;
1848         // Fill with the lowest bit
1849         if( Col12 & 0x001 )     r |= (1 << (rSize - 4)) - 1;
1850         if( Col12 & 0x010 )     r |= (1 << (gSize - 4)) - 1;
1851         if( Col12 & 0x100 )     r |= (1 << (bSize - 4)) - 1;
1852         
1853         // Create output
1854         ret  = r;
1855         ret |= g << rSize;
1856         ret |= b << (rSize + gSize);
1857         
1858         return ret;
1859 }
1860
1861 /**
1862  * \fn Uint8 *VT_Font_GetChar(Uint32 Codepoint)
1863  * \brief Gets an index into the font array given a Unicode Codepoint
1864  * \note See http://en.wikipedia.org/wiki/CP437
1865  */
1866 Uint8 *VT_Font_GetChar(Uint32 Codepoint)
1867 {
1868          int    index = 0;
1869         if(Codepoint < 128)
1870                 return &VTermFont[Codepoint*FONT_HEIGHT];
1871         switch(Codepoint)
1872         {
1873         case 0xC7:      index = 128;    break;  // Ç
1874         case 0xFC:      index = 129;    break;  // ü
1875         case 0xE9:      index = 130;    break;  // é
1876         case 0xE2:      index = 131;    break;  // â
1877         case 0xE4:      index = 132;    break;  // ä
1878         case 0xE0:      index = 133;    break;  // à
1879         case 0xE5:      index = 134;    break;  // å
1880         case 0xE7:      index = 135;    break;  // ç
1881         case 0xEA:      index = 136;    break;  // ê
1882         case 0xEB:      index = 137;    break;  // ë
1883         case 0xE8:      index = 138;    break;  // è
1884         case 0xEF:      index = 139;    break;  // ï
1885         case 0xEE:      index = 140;    break;  // î
1886         case 0xEC:      index = 141;    break;  // ì
1887         case 0xC4:      index = 142;    break;  // Ä
1888         case 0xC5:      index = 143;    break;  // Å
1889         }
1890         
1891         return &VTermFont[index*FONT_HEIGHT];
1892 }
1893
1894 EXPORTAS(&giVT_CharWidth, giVT_CharWidth);
1895 EXPORTAS(&giVT_CharHeight, giVT_CharHeight);
1896 EXPORT(VT_Font_Render);
1897 EXPORT(VT_Colour12to24);

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