Kernel/vterm - Removed an unneeded divide
[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                         while(term->InputRead > MAX_INPUT_CHARS8)
472                                 term->InputRead -= MAX_INPUT_CHARS8;
473                 }
474                 break;
475         
476         //case TERM_MODE_FB:
477         // Other - UCS-4
478         default:
479                 VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "VT_Read (UCS-4)");
480                 
481                 avail = term->InputWrite - term->InputRead;
482                 if(avail < 0)
483                         avail += MAX_INPUT_CHARS32;
484                 if(avail > Length - pos)
485                         avail = Length/4 - pos;
486                 
487                 codepoint_in = (void*)term->InputBuffer;
488                 codepoint_buf = Buffer;
489                 
490                 while( avail -- )
491                 {
492                         codepoint_buf[pos] = codepoint_in[term->InputRead];
493                         pos ++;
494                         term->InputRead ++;
495                         while(term->InputRead > MAX_INPUT_CHARS32)
496                                 term->InputRead -= MAX_INPUT_CHARS32;
497                 }
498                 pos *= 4;
499                 break;
500         }
501         
502         // Mark none avaliable if buffer empty
503         if( term->InputRead == term->InputWrite )
504                 VFS_MarkAvaliable(&term->Node, 0);
505         
506         term->ReadingThread = -1;
507         
508         Mutex_Release( &term->ReadingLock );
509         
510         return pos;
511 }
512
513 /**
514  * \fn Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
515  * \brief Write to a virtual terminal
516  */
517 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
518 {
519         tVTerm  *term = &gVT_Terminals[ Node->Inode ];
520          int    size;
521         
522         // Write
523         switch( term->Mode )
524         {
525         // Print Text
526         case TERM_MODE_TEXT:
527                 VT_int_PutString(term, Buffer, Length);
528                 break;
529         // Framebuffer :)
530         case TERM_MODE_FB:
531         
532                 // - Sanity Checking
533                 size = term->Width*term->Height*4;
534                 if( Offset > size ) {
535                         Log_Notice("VTerm", "VT_Write: Offset (0x%llx) > FBSize (0x%x)",
536                                 Offset, size);
537                         return 0;
538                 }
539                 if( Offset + Length > size ) {
540                         Log_Notice("VTerm", "VT_Write: Offset+Length (0x%llx) > FBSize (0x%x)",
541                                 Offset+Length, size);
542                         Length = size - Offset;
543                 }
544                 
545                 // Copy to the local cache
546                 memcpy( (void*)((Uint)term->Buffer + (Uint)Offset), Buffer, Length );
547                 
548                 // Update screen if needed
549                 if( Node->Inode == giVT_CurrentTerminal )
550                 {
551                         // Fill entire screen?
552                         if( giVT_RealWidth > term->Width || giVT_RealHeight > term->Height )
553                         {
554                                 // No? :( Well, just center it
555                                  int    x, y, w, h;
556                                 x = Offset/4;   y = x / term->Width;    x %= term->Width;
557                                 w = Length/4+x; h = w / term->Width;    w %= term->Width;
558                                 // Center
559                                 x += (giVT_RealWidth - term->Width) / 2;
560                                 y += (giVT_RealHeight - term->Height) / 2;
561                                 while(h--)
562                                 {
563                                         VFS_WriteAt( giVT_OutputDevHandle,
564                                                 (x + y * giVT_RealWidth)*4,
565                                                 term->Width * 4,
566                                                 Buffer
567                                                 );
568                                         Buffer = (void*)( (Uint)Buffer + term->Width*4 );
569                                         y ++;
570                                 }
571                                 return 0;
572                         }
573                         else {
574                                 return VFS_WriteAt( giVT_OutputDevHandle, Offset, Length, Buffer );
575                         }
576                 }
577         // Just pass on (for now)
578         // TODO: Handle locally too to ensure no information is lost on
579         //       VT Switch (and to isolate terminals from each other)
580         case TERM_MODE_2DACCEL:
581         //case TERM_MODE_3DACCEL:
582                 if( Node->Inode == giVT_CurrentTerminal )
583                 {
584                         VFS_Write( giVT_OutputDevHandle, Length, Buffer );
585                 }
586                 break;
587         }
588         
589         return 0;
590 }
591
592 /**
593  * \fn int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
594  * \brief Call an IO Control on a virtual terminal
595  */
596 int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
597 {
598          int    *iData = Data;
599          int    ret;
600         tVTerm  *term = Node->ImplPtr;
601         ENTER("pNode iId pData", Node, Id, Data);
602         
603         if(Id >= DRV_IOCTL_LOOKUP) {
604                 // Only root can fiddle with graphics modes
605                 // TODO: Remove this and replace with user ownership
606                 if( Threads_GetUID() != 0 )     return -1;
607         }
608         
609         switch(Id)
610         {
611         // --- Core Defined
612         case DRV_IOCTL_TYPE:
613                 LEAVE('i', DRV_TYPE_TERMINAL);
614                 return DRV_TYPE_TERMINAL;
615         case DRV_IOCTL_IDENT:
616                 memcpy(Data, "VT\0\0", 4);
617                 LEAVE('i', 0);
618                 return 0;
619         case DRV_IOCTL_VERSION:
620                 LEAVE('x', VERSION);
621                 return VERSION;
622         case DRV_IOCTL_LOOKUP:
623                 LEAVE('i', 0);
624                 return 0;
625         
626         // Get/Set the mode (and apply any changes)
627         case TERM_IOCTL_MODETYPE:
628                 if(Data != NULL)
629                 {
630                         if( CheckMem(Data, sizeof(int)) == 0 ) {
631                                 LEAVE('i', -1);
632                                 return -1;
633                         }
634                         Log_Log("VTerm", "VTerm %i mode set to %i", (int)Node->Inode, *iData);
635                         
636                         // Update mode if needed
637                         if( term->Mode != *iData
638                          || term->NewWidth
639                          || term->NewHeight)
640                         {
641                                 // Adjust for text mode
642                                 if( *iData == TERM_MODE_TEXT ) {
643                                         term->NewHeight *= giVT_CharHeight;
644                                         term->NewWidth *= giVT_CharWidth;
645                                 }
646                                 // Fill unchanged dimensions
647                                 if(term->NewHeight == 0)        term->NewHeight = term->Height;
648                                 if(term->NewWidth == 0) term->NewWidth = term->Width;
649                                 // Set new mode
650                                 VT_int_ChangeMode(term, *iData, term->NewWidth, term->NewHeight);
651                                 // Clear unapplied dimensions
652                                 term->NewWidth = 0;
653                                 term->NewHeight = 0;
654                         }
655                         
656                         // Update the screen dimensions
657                         if(Node->Inode == giVT_CurrentTerminal)
658                                 VT_SetTerminal( giVT_CurrentTerminal );
659                 }
660                 LEAVE('i', term->Mode);
661                 return term->Mode;
662         
663         // Get/set the terminal width
664         case TERM_IOCTL_WIDTH:
665                 if(Data != NULL) {
666                         if( CheckMem(Data, sizeof(int)) == 0 ) {
667                                 LEAVE('i', -1);
668                                 return -1;
669                         }
670                         term->NewWidth = *iData;
671                 }
672                 if( term->NewWidth )
673                         ret = term->NewWidth;
674                 else if( term->Mode == TERM_MODE_TEXT )
675                         ret = term->TextWidth;
676                 else
677                         ret = term->Width;
678                 LEAVE('i', ret);
679                 return ret;
680         
681         // Get/set the terminal height
682         case TERM_IOCTL_HEIGHT:
683                 if(Data != NULL) {
684                         if( CheckMem(Data, sizeof(int)) == 0 ) {
685                                 LEAVE('i', -1);
686                                 return -1;
687                         }
688                         term->NewHeight = *iData;
689                 }
690                 if( term->NewHeight )
691                         ret = term->NewHeight;
692                 else if( term->Mode == TERM_MODE_TEXT )
693                         ret = term->TextHeight;
694                 else
695                         ret = term->Height;
696                 LEAVE('i', ret);
697                 return ret;
698         
699         case TERM_IOCTL_FORCESHOW:
700                 Log_Log("VTerm", "Thread %i forced VTerm %i to be shown",
701                         Threads_GetTID(), (int)Node->Inode);
702                 VT_SetTerminal( Node->Inode );
703                 LEAVE('i', 1);
704                 return 1;
705         
706         case TERM_IOCTL_GETCURSOR:
707                 ret = (term->Flags & VT_FLAG_ALTBUF) ? term->AltWritePos : term->WritePos-term->ViewPos;
708                 LEAVE('i', ret);
709                 return ret;
710         }
711         LEAVE('i', -1);
712         return -1;
713 }
714
715 /**
716  * \fn void VT_SetTerminal(int ID)
717  * \brief Set the current terminal
718  */
719 void VT_SetTerminal(int ID)
720 {       
721         // Update current terminal ID
722         Log_Log("VTerm", "Changed terminal from %i to %i", giVT_CurrentTerminal, ID);
723         giVT_CurrentTerminal = ID;
724         gpVT_CurTerm = &gVT_Terminals[ID];
725         
726         // Update cursor
727         if( gpVT_CurTerm->Text && gpVT_CurTerm->Mode == TERM_MODE_TEXT && !(gpVT_CurTerm->Flags & VT_FLAG_HIDECSR) )
728         {
729                 tVideo_IOCtl_Pos        pos;
730                  int    offset = (gpVT_CurTerm->Flags & VT_FLAG_ALTBUF) ? gpVT_CurTerm->AltWritePos : gpVT_CurTerm->WritePos - gpVT_CurTerm->ViewPos;
731                 pos.x = offset % gpVT_CurTerm->TextWidth;
732                 pos.y = offset / gpVT_CurTerm->TextWidth;
733                 if( 0 <= pos.y && pos.y < gpVT_CurTerm->TextHeight )
734                         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
735         }
736         
737         if( gpVT_CurTerm->Mode == TERM_MODE_TEXT )
738                 VT_SetMode( VIDEO_BUFFMT_TEXT );
739         else
740                 VT_SetMode( VIDEO_BUFFMT_FRAMEBUFFER );
741         
742         // Update the screen
743         VT_int_UpdateScreen( &gVT_Terminals[ ID ], 1 );
744 }
745
746 /**
747  * \fn void VT_KBCallBack(Uint32 Codepoint)
748  * \brief Called on keyboard interrupt
749  * \param Codepoint     Pseudo-UTF32 character
750  * 
751  * Handles a key press and sends the key code to the user's buffer.
752  * If the code creates a kernel-magic sequence, it is not passed to the
753  * user and is handled in-kernel.
754  */
755 void VT_KBCallBack(Uint32 Codepoint)
756 {
757         tVTerm  *term = gpVT_CurTerm;
758         
759         // How the hell did we get a codepoint of zero?
760         if(Codepoint == 0)      return;
761         
762         // Key Up
763         if( Codepoint & 0x80000000 )
764         {
765                 Codepoint &= 0x7FFFFFFF;
766                 switch(Codepoint)
767                 {
768                 case KEY_LALT:  gbVT_AltDown &= ~1;     break;
769                 case KEY_RALT:  gbVT_AltDown &= ~2;     break;
770                 case KEY_LCTRL: gbVT_CtrlDown &= ~1;    break;
771                 case KEY_RCTRL: gbVT_CtrlDown &= ~2;    break;
772                 }
773                 return;
774         }
775         
776         switch(Codepoint)
777         {
778         case KEY_LALT:  gbVT_AltDown |= 1;      break;
779         case KEY_RALT:  gbVT_AltDown |= 2;      break;
780         case KEY_LCTRL: gbVT_CtrlDown |= 1;     break;
781         case KEY_RCTRL: gbVT_CtrlDown |= 2;     break;
782         
783         default:
784                 if(!gbVT_AltDown || !gbVT_CtrlDown)
785                         break;
786                 switch(Codepoint)
787                 {
788                 case KEY_F1:    VT_SetTerminal(0);      return;
789                 case KEY_F2:    VT_SetTerminal(1);      return;
790                 case KEY_F3:    VT_SetTerminal(2);      return;
791                 case KEY_F4:    VT_SetTerminal(3);      return;
792                 case KEY_F5:    VT_SetTerminal(4);      return;
793                 case KEY_F6:    VT_SetTerminal(5);      return;
794                 case KEY_F7:    VT_SetTerminal(6);      return;
795                 case KEY_F8:    VT_SetTerminal(7);      return;
796                 case KEY_F9:    VT_SetTerminal(8);      return;
797                 case KEY_F10:   VT_SetTerminal(9);      return;
798                 case KEY_F11:   VT_SetTerminal(10);     return;
799                 case KEY_F12:   VT_SetTerminal(11);     return;
800                 // Scrolling
801                 case KEY_PGUP:
802                         if( gpVT_CurTerm->Flags & VT_FLAG_ALTBUF )
803                                 return ;
804                         if( gpVT_CurTerm->ViewPos > gpVT_CurTerm->Width )
805                                 gpVT_CurTerm->ViewPos -= gpVT_CurTerm->Width;
806                         else
807                                 gpVT_CurTerm->ViewPos = 0;
808                         return;
809                 case KEY_PGDOWN:
810                         if( gpVT_CurTerm->Flags & VT_FLAG_ALTBUF )
811                                 return ;
812                         if( gpVT_CurTerm->ViewPos < gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback) )
813                                 gpVT_CurTerm->ViewPos += gpVT_CurTerm->Width;
814                         else
815                                 gpVT_CurTerm->ViewPos = gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback);
816                         return;
817                 }
818         }
819         
820         // Encode key
821         if(term->Mode == TERM_MODE_TEXT)
822         {
823                 Uint8   buf[6] = {0};
824                  int    len = 0;
825                 
826                 // Ignore Modifer Keys
827                 if(Codepoint > KEY_MODIFIERS)   return;
828                 
829                 // Get UTF-8/ANSI Encoding
830                 switch(Codepoint)
831                 {
832                 case KEY_LEFT:
833                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'D';
834                         len = 3;
835                         break;
836                 case KEY_RIGHT:
837                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'C';
838                         len = 3;
839                         break;
840                 case KEY_UP:
841                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'A';
842                         len = 3;
843                         break;
844                 case KEY_DOWN:
845                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'B';
846                         len = 3;
847                         break;
848                 
849                 case KEY_PGUP:
850                         //buf[0] = '\x1B';      buf[1] = '[';   buf[2] = '5';   // Some overline also
851                         //len = 4;      // Commented out until I'm sure
852                         len = 0;
853                         break;
854                 case KEY_PGDOWN:
855                         len = 0;
856                         break;
857                 
858                 // Attempt to encode in UTF-8
859                 default:
860                         len = WriteUTF8( buf, Codepoint );
861                         if(len == 0) {
862                                 Warning("Codepoint (%x) is unrepresentable in UTF-8", Codepoint);
863                         }
864                         break;
865                 }
866                 
867                 if(len == 0) {
868                         // Unprintable / Don't Pass
869                         return;
870                 }
871
872 #if 0
873                 // Handle meta characters
874                 if( !(term->Flags & VT_FLAG_RAWIN) )
875                 {
876                         switch(buf[0])
877                         {
878                         case '\3':      // ^C
879                                 
880                                 break;
881                         }
882                 }
883 #endif
884                 
885                 // Write
886                 if( MAX_INPUT_CHARS8 - term->InputWrite >= len )
887                         memcpy( &term->InputBuffer[term->InputWrite], buf, len );
888                 else {
889                         memcpy( &term->InputBuffer[term->InputWrite], buf, MAX_INPUT_CHARS8 - term->InputWrite );
890                         memcpy( &term->InputBuffer[0], buf, len - (MAX_INPUT_CHARS8 - term->InputWrite) );
891                 }
892                 // Roll the buffer over
893                 term->InputWrite += len;
894                 term->InputWrite %= MAX_INPUT_CHARS8;
895                 if( (term->InputWrite - term->InputRead + MAX_INPUT_CHARS8)%MAX_INPUT_CHARS8 < len ) {
896                         term->InputRead = term->InputWrite + 1;
897                         term->InputRead %= MAX_INPUT_CHARS8;
898                 }
899         }
900         else
901         {
902                 // Encode the raw UTF-32 Key
903                 Uint32  *raw_in = (void*)term->InputBuffer;
904                 raw_in[ term->InputWrite ] = Codepoint;
905                 term->InputWrite ++;
906                 term->InputWrite %= MAX_INPUT_CHARS32;
907                 if(term->InputRead == term->InputWrite) {
908                         term->InputRead ++;
909                         term->InputRead %= MAX_INPUT_CHARS32;
910                 }
911         }
912         
913         VFS_MarkAvaliable(&term->Node, 1);
914 }
915
916 /**
917  * \fn void VT_int_ClearLine(tVTerm *Term, int Num)
918  * \brief Clears a line in a virtual terminal
919  */
920 void VT_int_ClearLine(tVTerm *Term, int Num)
921 {
922          int    i;
923         tVT_Char        *cell;
924         
925         if( Num < 0 || Num >= Term->TextHeight * (giVT_Scrollback + 1) )        return ;
926         
927         cell = (Term->Flags & VT_FLAG_ALTBUF) ? Term->AltBuf : Term->Text;
928         cell = &cell[ Num*Term->TextWidth ];
929         
930         for( i = Term->TextWidth; i--; )
931         {
932                 cell[ i ].Ch = 0;
933                 cell[ i ].Colour = Term->CurColour;
934         }
935 }
936
937 /**
938  * \fn int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
939  * \brief Parses a VT100 Escape code
940  */
941 int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
942 {
943         char    c;
944          int    argc = 0, j = 1;
945          int    tmp;
946          int    args[6] = {0,0,0,0};
947          int    bQuestionMark = 0;
948         
949         switch(Buffer[0])
950         {
951         //Large Code
952         case '[':
953                 // Get Arguments
954                 c = Buffer[j++];
955                 if(c == '?') {
956                         bQuestionMark = 1;
957                         c = Buffer[j++];
958                 }
959                 if( '0' <= c && c <= '9' )
960                 {
961                         do {
962                                 if(c == ';')    c = Buffer[j++];
963                                 while('0' <= c && c <= '9') {
964                                         args[argc] *= 10;
965                                         args[argc] += c-'0';
966                                         c = Buffer[j++];
967                                 }
968                                 argc ++;
969                         } while(c == ';');
970                 }
971                 
972                 // Get Command
973                 if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
974                 {
975                         if( bQuestionMark )
976                         {
977                                 switch(c)
978                                 {
979                                 // DEC Private Mode Set
980                                 case 'h':
981                                         if(argc != 1)   break;
982                                         switch(args[0])
983                                         {
984                                         case 1047:
985                                                 VT_int_ToggleAltBuffer(Term, 1);
986                                                 break;
987                                         }
988                                         break;
989                                 case 'l':
990                                         if(argc != 1)   break;
991                                         switch(args[0])
992                                         {
993                                         case 1047:
994                                                 VT_int_ToggleAltBuffer(Term, 0);
995                                                 break;
996                                         }
997                                         break;
998                                 default:
999                                         Log_Warning("VTerm", "Unknown control sequence '\\x1B[?%c'", c);
1000                                         break;
1001                                 }
1002                         }
1003                         else
1004                         {
1005                                 tmp = 1;
1006                                 switch(c)
1007                                 {
1008                                 // Left
1009                                 case 'D':
1010                                         tmp = -1;
1011                                 // Right
1012                                 case 'C':
1013                                         if(argc == 1)   tmp *= args[0];
1014                                         if( Term->Flags & VT_FLAG_ALTBUF )
1015                                         {
1016                                                 if( (Term->AltWritePos + tmp) % Term->TextWidth == 0 ) {
1017                                                         Term->AltWritePos -= Term->AltWritePos % Term->TextWidth;
1018                                                         Term->AltWritePos += Term->TextWidth - 1;
1019                                                 }
1020                                                 else
1021                                                         Term->AltWritePos += tmp;
1022                                         }
1023                                         else
1024                                         {
1025                                                 if( (Term->WritePos + tmp) % Term->TextWidth == 0 ) {
1026                                                         Term->WritePos -= Term->WritePos % Term->TextWidth;
1027                                                         Term->WritePos += Term->TextWidth - 1;
1028                                                 }
1029                                                 else
1030                                                         Term->WritePos += tmp;
1031                                         }
1032                                         break;
1033                                 
1034                                 // Erase
1035                                 case 'J':
1036                                         switch(args[0])
1037                                         {
1038                                         case 0: // Erase below
1039                                                 break;
1040                                         case 1: // Erase above
1041                                                 break;
1042                                         case 2: // Erase all
1043                                                 if( Term->Flags & VT_FLAG_ALTBUF )
1044                                                 {
1045                                                          int    i = Term->TextHeight;
1046                                                         while( i-- )    VT_int_ClearLine(Term, i);
1047                                                         Term->AltWritePos = 0;
1048                                                         VT_int_UpdateScreen(Term, 1);
1049                                                 }
1050                                                 else
1051                                                 {
1052                                                          int    i = Term->TextHeight * (giVT_Scrollback + 1);
1053                                                         while( i-- )    VT_int_ClearLine(Term, i);
1054                                                         Term->WritePos = 0;
1055                                                         Term->ViewPos = 0;
1056                                                         VT_int_UpdateScreen(Term, 1);
1057                                                 }
1058                                                 break;
1059                                         }
1060                                         break;
1061                                 
1062                                 // Erase in line
1063                                 case 'K':
1064                                         switch(args[0])
1065                                         {
1066                                         case 0: // Erase to right
1067                                                 if( Term->Flags & VT_FLAG_ALTBUF )
1068                                                 {
1069                                                          int    i, max;
1070                                                         max = Term->Width - Term->AltWritePos % Term->Width;
1071                                                         for( i = 0; i < max; i ++ )
1072                                                                 Term->AltBuf[Term->AltWritePos+i].Ch = 0;
1073                                                 }
1074                                                 else
1075                                                 {
1076                                                          int    i, max;
1077                                                         max = Term->Width - Term->WritePos % Term->Width;
1078                                                         for( i = 0; i < max; i ++ )
1079                                                                 Term->Text[Term->WritePos+i].Ch = 0;
1080                                                 }
1081                                                 VT_int_UpdateScreen(Term, 0);
1082                                                 break;
1083                                         case 1: // Erase to left
1084                                                 if( Term->Flags & VT_FLAG_ALTBUF )
1085                                                 {
1086                                                          int    i = Term->AltWritePos % Term->Width;
1087                                                         while( i -- )
1088                                                                 Term->AltBuf[Term->AltWritePos++].Ch = 0;
1089                                                 }
1090                                                 else
1091                                                 {
1092                                                          int    i = Term->WritePos % Term->Width;
1093                                                         while( i -- )
1094                                                                 Term->Text[Term->WritePos++].Ch = 0;
1095                                                 }
1096                                                 VT_int_UpdateScreen(Term, 0);
1097                                                 break;
1098                                         case 2: // Erase all
1099                                                 if( Term->Flags & VT_FLAG_ALTBUF )
1100                                                 {
1101                                                         VT_int_ClearLine(Term, Term->AltWritePos / Term->Width);
1102                                                 }
1103                                                 else
1104                                                 {
1105                                                         VT_int_ClearLine(Term, Term->WritePos / Term->Width);
1106                                                 }
1107                                                 VT_int_UpdateScreen(Term, 0);
1108                                                 break;
1109                                         }
1110                                         break;
1111                                 
1112                                 // Set cursor position
1113                                 case 'H':
1114                                         if( Term->Flags & VT_FLAG_ALTBUF )
1115                                                 Term->AltWritePos = args[0] + args[1]*Term->TextWidth;
1116                                         else
1117                                                 Term->WritePos = args[0] + args[1]*Term->TextWidth;
1118                                         //Log_Debug("VTerm", "args = {%i, %i}", args[0], args[1]);
1119                                         break;
1120                                 
1121                                 // Scroll up `n` lines
1122                                 case 'S':
1123                                         tmp = -1;
1124                                 // Scroll down `n` lines
1125                                 case 'T':
1126                                         if(argc == 1)   tmp *= args[0];
1127                                         if( Term->Flags & VT_FLAG_ALTBUF )
1128                                                 VT_int_ScrollText(Term, tmp);
1129                                         else
1130                                         {
1131                                                 if(Term->ViewPos/Term->TextWidth + tmp < 0)
1132                                                         break;
1133                                                 if(Term->ViewPos/Term->TextWidth + tmp  > Term->TextHeight * (giVT_Scrollback + 1))
1134                                                         break;
1135                                                 
1136                                                 Term->ViewPos += Term->TextWidth*tmp;
1137                                         }
1138                                         break;
1139                                 
1140                                 // Set Font flags
1141                                 case 'm':
1142                                         for( ; argc--; )
1143                                         {
1144                                                 // Flags
1145                                                 if( 0 <= args[argc] && args[argc] <= 8)
1146                                                 {
1147                                                         switch(args[argc])
1148                                                         {
1149                                                         case 0: Term->CurColour = DEFAULT_COLOUR;       break;  // Reset
1150                                                         case 1: Term->CurColour |= 0x80000000;  break;  // Bright
1151                                                         case 2: Term->CurColour &= ~0x80000000; break;  // Dim
1152                                                         }
1153                                                 }
1154                                                 // Foreground Colour
1155                                                 else if(30 <= args[argc] && args[argc] <= 37) {
1156                                                         Term->CurColour &= 0xF000FFFF;
1157                                                         Term->CurColour |= (Uint32)caVT100Colours[ args[argc]-30+(Term->CurColour>>28) ] << 16;
1158                                                 }
1159                                                 // Background Colour
1160                                                 else if(40 <= args[argc] && args[argc] <= 47) {
1161                                                         Term->CurColour &= 0xFFFF8000;
1162                                                         Term->CurColour |= caVT100Colours[ args[argc]-40+((Term->CurColour>>12)&15) ];
1163                                                 }
1164                                         }
1165                                         break;
1166                                 
1167                                 // Set scrolling region
1168                                 case 'r':
1169                                         if( argc != 2 ) break;
1170                                         Term->ScrollTop = args[0];
1171                                         Term->ScrollHeight = args[1] - args[0];
1172                                         break;
1173                                 
1174                                 default:
1175                                         Log_Warning("VTerm", "Unknown control sequence '\\x1B[%c'", c);
1176                                         break;
1177                                 }
1178                         }
1179                 }
1180                 break;
1181                 
1182         default:        break;
1183         }
1184         
1185         //Log_Debug("VTerm", "j = %i, Buffer = '%s'", j, Buffer);
1186         return j;
1187 }
1188
1189 /**
1190  * \fn void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
1191  * \brief Print a string to the Virtual Terminal
1192  */
1193 void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
1194 {
1195         Uint32  val;
1196          int    i;
1197         
1198         // Iterate
1199         for( i = 0; i < Count; i++ )
1200         {
1201                 // Handle escape sequences
1202                 if( Buffer[i] == 0x1B )
1203                 {
1204                         i ++;
1205                         i += VT_int_ParseEscape(Term, (char*)&Buffer[i]) - 1;
1206                         continue;
1207                 }
1208                 
1209                 // Fast check for non UTF-8
1210                 if( Buffer[i] < 128 )   // Plain ASCII
1211                         VT_int_PutChar(Term, Buffer[i]);
1212                 else {  // UTF-8
1213                         i += ReadUTF8(&Buffer[i], &val) - 1;
1214                         VT_int_PutChar(Term, val);
1215                 }
1216         }
1217         // Update Screen
1218         VT_int_UpdateScreen( Term, 0 );
1219         
1220         // Update cursor
1221         if( Term == gpVT_CurTerm && !(Term->Flags & VT_FLAG_HIDECSR) )
1222         {
1223                 tVideo_IOCtl_Pos        pos;
1224                  int    offset = (gpVT_CurTerm->Flags & VT_FLAG_ALTBUF) ? gpVT_CurTerm->AltWritePos : gpVT_CurTerm->WritePos - gpVT_CurTerm->ViewPos;
1225                 pos.x = offset % gpVT_CurTerm->TextWidth;
1226                 pos.y = offset / gpVT_CurTerm->TextWidth;
1227                 if( 0 <= pos.y && pos.y < gpVT_CurTerm->TextHeight )
1228                         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
1229         }
1230 }
1231
1232 /**
1233  * \fn void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
1234  * \brief Write a single character to a VTerm
1235  */
1236 void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
1237 {
1238          int    i;
1239         tVT_Char        *buffer;
1240          int    write_pos;
1241         
1242         if(Term->Flags & VT_FLAG_ALTBUF) {
1243                 buffer = Term->AltBuf;
1244                 write_pos = Term->AltWritePos;
1245         }
1246         else {
1247                 buffer = Term->Text;
1248                 write_pos = Term->WritePos;
1249         }
1250         
1251         switch(Ch)
1252         {
1253         case '\0':      return; // Ignore NULL byte
1254         case '\n':
1255                 VT_int_UpdateScreen( Term, 0 ); // Update the line before newlining
1256                 write_pos += Term->TextWidth;
1257         case '\r':
1258                 write_pos -= write_pos % Term->TextWidth;
1259                 break;
1260         
1261         case '\t': { int tmp = write_pos / Term->TextWidth;
1262                 write_pos %= Term->TextWidth;
1263                 do {
1264                         buffer[ write_pos ].Ch = '\0';
1265                         buffer[ write_pos ].Colour = Term->CurColour;
1266                         write_pos ++;
1267                 } while(write_pos & 7);
1268                 write_pos += tmp * Term->TextWidth;
1269                 break; }
1270         
1271         case '\b':
1272                 // Backspace is invalid at Offset 0
1273                 if(write_pos == 0)      break;
1274                 
1275                 write_pos --;
1276                 // Singe Character
1277                 if(buffer[ write_pos ].Ch != '\0') {
1278                         buffer[ write_pos ].Ch = 0;
1279                         buffer[ write_pos ].Colour = Term->CurColour;
1280                         break;
1281                 }
1282                 // Tab
1283                 i = 7;  // Limit it to 8
1284                 do {
1285                         buffer[ write_pos ].Ch = 0;
1286                         buffer[ write_pos ].Colour = Term->CurColour;
1287                         write_pos --;
1288                 } while(write_pos && i-- && buffer[ write_pos ].Ch == '\0');
1289                 if(buffer[ write_pos ].Ch != '\0')
1290                         write_pos ++;
1291                 break;
1292         
1293         default:
1294                 buffer[ write_pos ].Ch = Ch;
1295                 buffer[ write_pos ].Colour = Term->CurColour;
1296                 // Update the line before wrapping
1297                 if( (write_pos + 1) % Term->TextWidth == 0 )
1298                         VT_int_UpdateScreen( Term, 0 );
1299                 write_pos ++;
1300                 break;
1301         }
1302         
1303         if(Term->Flags & VT_FLAG_ALTBUF)
1304         {
1305                 Term->AltBuf = buffer;
1306                 Term->AltWritePos = write_pos;
1307                 
1308                 if(Term->AltWritePos >= Term->TextWidth*Term->TextHeight)
1309                 {
1310                         Term->AltWritePos -= Term->TextWidth;
1311                         VT_int_ScrollText(Term, 1);
1312                 }
1313                 
1314         }
1315         else
1316         {
1317                 Term->Text = buffer;
1318                 Term->WritePos = write_pos;
1319                 // Move Screen
1320                 // - Check if we need to scroll the entire scrollback buffer
1321                 if(Term->WritePos >= Term->TextWidth*Term->TextHeight*(giVT_Scrollback+1))
1322                 {
1323                          int    base;
1324                         
1325                         // Update previous line
1326                         Term->WritePos -= Term->TextWidth;
1327                         VT_int_UpdateScreen( Term, 0 );
1328                         
1329                         // Update view position
1330                         base = Term->TextWidth*Term->TextHeight*(giVT_Scrollback);
1331                         if(Term->ViewPos < base)
1332                                 Term->ViewPos += Term->Width;
1333                         if(Term->ViewPos > base)
1334                                 Term->ViewPos = base;
1335                         
1336                         VT_int_ScrollText(Term, 1);
1337                 }
1338                 // Ok, so we only need to scroll the screen
1339                 else if(Term->WritePos >= Term->ViewPos + Term->TextWidth*Term->TextHeight)
1340                 {
1341                         // Update the last line
1342                         Term->WritePos -= Term->TextWidth;
1343                         VT_int_UpdateScreen( Term, 0 );
1344                         Term->WritePos += Term->TextWidth;
1345                         
1346                         VT_int_ScrollText(Term, 1);
1347                         
1348                         Term->ViewPos += Term->TextWidth;
1349                 }
1350         }
1351         
1352         //LEAVE('-');
1353 }
1354
1355 void VT_int_ScrollText(tVTerm *Term, int Count)
1356 {
1357         tVT_Char        *buf;
1358          int    height, init_write_pos;
1359          int    len, i;
1360          int    scroll_top, scroll_height;
1361
1362         // Get buffer pointer and attributes    
1363         if( Term->Flags & VT_FLAG_ALTBUF )
1364         {
1365                 buf = Term->AltBuf;
1366                 height = Term->TextHeight;
1367                 init_write_pos = Term->AltWritePos;
1368                 scroll_top = Term->ScrollTop;
1369                 scroll_height = Term->ScrollHeight;
1370         }
1371         else
1372         {
1373                 buf = Term->Text;
1374                 height = Term->TextHeight*(giVT_Scrollback+1);
1375                 init_write_pos = Term->WritePos;
1376                 scroll_top = 0;
1377                 scroll_height = height;
1378         }
1379
1380         // Scroll text downwards        
1381         if( Count > 0 )
1382         {
1383                  int    base;
1384         
1385                 // Set up
1386                 if(Count > scroll_height)       Count = scroll_height;
1387                 base = Term->TextWidth*(scroll_top + scroll_height - Count);
1388                 len = Term->TextWidth*(scroll_height - Count);
1389                 
1390                 // Scroll terminal cache
1391                 memmove(
1392                         &buf[Term->TextWidth*scroll_top],
1393                         &buf[Term->TextWidth*(scroll_top+Count)],
1394                         len*sizeof(tVT_Char)
1395                         );
1396                 // Clear last rows
1397                 for( i = 0; i < Term->TextWidth*Count; i ++ )
1398                 {
1399                         buf[ base + i ].Ch = 0;
1400                         buf[ base + i ].Colour = Term->CurColour;
1401                 }
1402                 
1403                 // Update Screen
1404                 VT_int_ScrollFramebuffer( Term, Count );
1405                 if( Term->Flags & VT_FLAG_ALTBUF )
1406                         Term->AltWritePos = base;
1407                 else
1408                         Term->WritePos = Term->ViewPos + Term->TextWidth*(Term->TextHeight - Count);
1409                 for( i = 0; i < Count; i ++ )
1410                 {
1411                         VT_int_UpdateScreen( Term, 0 );
1412                         if( Term->Flags & VT_FLAG_ALTBUF )
1413                                 Term->AltWritePos += Term->TextWidth;
1414                         else
1415                                 Term->WritePos += Term->TextWidth;
1416                 }
1417         }
1418         else
1419         {
1420                 Count = -Count;
1421                 if(Count > scroll_height)       Count = scroll_height;
1422                 
1423                 len = Term->TextWidth*(scroll_height - Count);
1424                 
1425                 // Scroll terminal cache
1426                 memmove(
1427                         &buf[Term->TextWidth*(scroll_top+Count)],
1428                         &buf[Term->TextWidth*scroll_top],
1429                         len*sizeof(tVT_Char)
1430                         );
1431                 // Clear preceding rows
1432                 for( i = 0; i < Term->TextWidth*Count; i ++ )
1433                 {
1434                         buf[ i ].Ch = 0;
1435                         buf[ i ].Colour = Term->CurColour;
1436                 }
1437                 
1438                 VT_int_ScrollFramebuffer( Term, -Count );
1439                 if( Term->Flags & VT_FLAG_ALTBUF )
1440                         Term->AltWritePos = Term->TextWidth*scroll_top;
1441                 else
1442                         Term->WritePos = Term->ViewPos;
1443                 for( i = 0; i < Count; i ++ )
1444                 {
1445                         VT_int_UpdateScreen( Term, 0 );
1446                         if( Term->Flags & VT_FLAG_ALTBUF )
1447                                 Term->AltWritePos += Term->TextWidth;
1448                         else
1449                                 Term->WritePos += Term->TextWidth;
1450                 }
1451         }
1452         
1453         if( Term->Flags & VT_FLAG_ALTBUF )
1454                 Term->AltWritePos = init_write_pos;
1455         else
1456                 Term->WritePos = init_write_pos;
1457 }
1458
1459 /**
1460  * \fn void VT_int_ScrollFramebuffer( tVTerm *Term, int Count )
1461  * \note Scrolls the framebuffer down by \a Count text lines
1462  */
1463 void VT_int_ScrollFramebuffer( tVTerm *Term, int Count )
1464 {
1465          int    tmp;
1466         struct {
1467                 Uint8   Op;
1468                 Uint16  DstX, DstY;
1469                 Uint16  SrcX, SrcY;
1470                 Uint16  W, H;
1471         } PACKED        buf;
1472         
1473         // Only update if this is the current terminal
1474         if( Term != gpVT_CurTerm )      return;
1475         
1476         if( Count > Term->ScrollHeight )        Count = Term->ScrollHeight;
1477         if( Count < -Term->ScrollHeight )       Count = -Term->ScrollHeight;
1478         
1479         // Switch to 2D Command Stream
1480         tmp = VIDEO_BUFFMT_2DSTREAM;
1481         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp);
1482         
1483         // BLIT to 0,0 from 0,giVT_CharHeight
1484         buf.Op = VIDEO_2DOP_BLIT;
1485         buf.SrcX = 0;   buf.DstX = 0;
1486         // TODO: Don't assume character dimensions
1487         buf.W = Term->TextWidth * giVT_CharWidth;
1488         if( Count > 0 )
1489         {
1490                 buf.SrcY = (Term->ScrollTop+Count) * giVT_CharHeight;
1491                 buf.DstY = Term->ScrollTop * giVT_CharHeight;
1492         }
1493         else    // Scroll up, move text down
1494         {
1495                 Count = -Count;
1496                 buf.SrcY = Term->ScrollTop * giVT_CharHeight;
1497                 buf.DstY = (Term->ScrollTop+Count) * giVT_CharHeight;
1498         }
1499         buf.H = (Term->ScrollHeight-Count) * giVT_CharHeight;
1500         VFS_WriteAt(giVT_OutputDevHandle, 0, sizeof(buf), &buf);
1501         
1502         // Restore old mode (this function is only called during text mode)
1503         tmp = VIDEO_BUFFMT_TEXT;
1504         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp);
1505 }
1506
1507 /**
1508  * \fn void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
1509  * \brief Updates the video framebuffer
1510  */
1511 void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
1512 {
1513         tVT_Char        *buffer;
1514          int    view_pos, write_pos;
1515         // Only update if this is the current terminal
1516         if( Term != gpVT_CurTerm )      return;
1517         
1518         switch( Term->Mode )
1519         {
1520         case TERM_MODE_TEXT:
1521                 view_pos = (Term->Flags & VT_FLAG_ALTBUF) ? 0 : Term->ViewPos;
1522                 write_pos = (Term->Flags & VT_FLAG_ALTBUF) ? Term->AltWritePos : Term->WritePos;
1523                 buffer = (Term->Flags & VT_FLAG_ALTBUF) ? Term->AltBuf : Term->Text;
1524                 // Re copy the entire screen?
1525                 if(UpdateAll) {
1526                         VFS_WriteAt(
1527                                 giVT_OutputDevHandle,
1528                                 0,
1529                                 Term->TextWidth*Term->TextHeight*sizeof(tVT_Char),
1530                                 &buffer[view_pos]
1531                                 );
1532                 }
1533                 // Only copy the current line
1534                 else {
1535                          int    ofs = write_pos - write_pos % Term->TextWidth;
1536                         VFS_WriteAt(
1537                                 giVT_OutputDevHandle,
1538                                 (ofs - view_pos)*sizeof(tVT_Char),
1539                                 Term->TextWidth*sizeof(tVT_Char),
1540                                 &buffer[ofs]
1541                                 );
1542                 }
1543                 break;
1544         case TERM_MODE_FB:
1545                 VFS_WriteAt(
1546                         giVT_OutputDevHandle,
1547                         0,
1548                         Term->Width*Term->Height*sizeof(Uint32),
1549                         Term->Buffer
1550                         );
1551                 break;
1552         }
1553 }
1554
1555 /**
1556  * \brief Update the screen mode
1557  * \param Term  Terminal to update
1558  * \param NewMode       New mode to set
1559  * \param NewWidth      New framebuffer width
1560  * \param NewHeight     New framebuffer height
1561  */
1562 void VT_int_ChangeMode(tVTerm *Term, int NewMode, int NewWidth, int NewHeight)
1563 {
1564         // TODO: Increase RealWidth/RealHeight when this happens
1565         if(NewWidth > giVT_RealWidth)   NewWidth = giVT_RealWidth;
1566         if(NewHeight > giVT_RealHeight) NewHeight = giVT_RealHeight;
1567         
1568         Term->Mode = NewMode;
1569
1570         if(NewWidth != Term->Width || NewHeight != Term->Height)
1571         {
1572                  int    oldW = Term->Width;
1573                  int    oldTW = Term->TextWidth;
1574                  int    oldH = Term->Height;
1575                  int    oldTH = Term->TextHeight;
1576                 tVT_Char        *oldTBuf = Term->Text;
1577                 Uint32  *oldFB = Term->Buffer;
1578                  int    w, h, i;
1579                 // Calculate new dimensions
1580                 Term->Width = NewWidth;
1581                 Term->Height = NewHeight;
1582                 Term->TextWidth = NewWidth / giVT_CharWidth;
1583                 Term->TextHeight = NewHeight / giVT_CharHeight;
1584                 Term->ScrollHeight = Term->TextHeight - (oldTH - Term->ScrollHeight) - Term->ScrollTop;
1585         
1586                 // Allocate new buffers
1587                 // - Text
1588                 Term->Text = calloc(
1589                         Term->TextWidth * Term->TextHeight * (giVT_Scrollback+1),
1590                         sizeof(tVT_Char)
1591                         );
1592                 if(oldTBuf) {
1593                         // Copy old buffer
1594                         w = (oldTW > Term->TextWidth) ? Term->TextWidth : oldTW;
1595                         h = (oldTH > Term->TextHeight) ? Term->TextHeight : oldTH;
1596                         h *= giVT_Scrollback + 1;
1597                         for( i = 0; i < h; i ++ )
1598                         {
1599                                 memcpy(
1600                                         &Term->Text[i*Term->TextWidth],
1601                                         &oldTBuf[i*oldTW],
1602                                         w*sizeof(tVT_Char)
1603                                         );      
1604                         }
1605                         free(oldTBuf);
1606                 }
1607                 
1608                 // - Alternate Text
1609                 Term->AltBuf = realloc(
1610                         Term->AltBuf,
1611                         Term->TextWidth * Term->TextHeight * sizeof(tVT_Char)
1612                         );
1613                 
1614                 // - Framebuffer
1615                 Term->Buffer = calloc( Term->Width * Term->Height, sizeof(Uint32) );
1616                 if(oldFB) {
1617                         // Copy old buffer
1618                         w = (oldW > Term->Width) ? Term->Width : oldW;
1619                         h = (oldH > Term->Height) ? Term->Height : oldH;
1620                         for( i = 0; i < h; i ++ )
1621                         {
1622                                 memcpy(
1623                                         &Term->Buffer[i*Term->Width],
1624                                         &oldFB[i*oldW],
1625                                         w*sizeof(Uint32)
1626                                         );
1627                         }
1628                         free(oldFB);
1629                 }
1630                 
1631                 // Debug
1632                 switch(NewMode)
1633                 {
1634                 case TERM_MODE_TEXT:
1635                         Log_Log("VTerm", "Set VT %p to text mode (%ix%i)",
1636                                 Term, Term->TextWidth, Term->TextHeight);
1637                         break;
1638                 case TERM_MODE_FB:
1639                         Log_Log("VTerm", "Set VT %p to framebuffer mode (%ix%i)",
1640                                 Term, Term->Width, Term->Height);
1641                         break;
1642                 //case TERM_MODE_2DACCEL:
1643                 //case TERM_MODE_3DACCEL:
1644                 //      return;
1645                 }
1646         }
1647         
1648 }
1649
1650
1651 void VT_int_ToggleAltBuffer(tVTerm *Term, int Enabled)
1652 {       
1653         if(Enabled)
1654                 Term->Flags |= VT_FLAG_ALTBUF;
1655         else
1656                 Term->Flags &= ~VT_FLAG_ALTBUF;
1657         VT_int_UpdateScreen(Term, 1);
1658 }
1659
1660 // ---
1661 // Font Render
1662 // ---
1663 #define MONOSPACE_FONT  10816
1664
1665 #if MONOSPACE_FONT == 10808     // 8x8
1666 # include "vterm_font_8x8.h"
1667 #elif MONOSPACE_FONT == 10816   // 8x16
1668 # include "vterm_font_8x16.h"
1669 #endif
1670
1671 // === PROTOTYPES ===
1672 Uint8   *VT_Font_GetChar(Uint32 Codepoint);
1673
1674 // === GLOBALS ===
1675 int     giVT_CharWidth = FONT_WIDTH;
1676 int     giVT_CharHeight = FONT_HEIGHT;
1677
1678 // === CODE ===
1679 /**
1680  * \brief Render a font character
1681  */
1682 void VT_Font_Render(Uint32 Codepoint, void *Buffer, int Depth, int Pitch, Uint32 BGC, Uint32 FGC)
1683 {
1684         Uint8   *font;
1685          int    x, y;
1686         
1687         // 8-bpp and below
1688         if( Depth <= 8 )
1689         {
1690                 Uint8   *buf = Buffer;
1691                 
1692                 font = VT_Font_GetChar(Codepoint);
1693                 
1694                 for(y = 0; y < FONT_HEIGHT; y ++)
1695                 {
1696                         for(x = 0; x < FONT_WIDTH; x ++)
1697                         {
1698                                 if(*font & (1 << (FONT_WIDTH-x-1)))
1699                                         buf[x] = FGC;
1700                                 else
1701                                         buf[x] = BGC;
1702                         }
1703                         buf = (void*)( (tVAddr)buf + Pitch );
1704                         font ++;
1705                 }
1706         }
1707         // 16-bpp and below
1708         else if( Depth <= 16 )
1709         {
1710                 Uint16  *buf = Buffer;
1711                 
1712                 font = VT_Font_GetChar(Codepoint);
1713                 
1714                 for(y = 0; y < FONT_HEIGHT; y ++)
1715                 {
1716                         for(x = 0; x < FONT_WIDTH; x ++)
1717                         {
1718                                 if(*font & (1 << (FONT_WIDTH-x-1)))
1719                                         buf[x] = FGC;
1720                                 else
1721                                         buf[x] = BGC;
1722                         }
1723                         buf = (void*)( (tVAddr)buf + Pitch );
1724                         font ++;
1725                 }
1726         }
1727         // 24-bpp colour
1728         // - Special handling to not overwrite the next pixel
1729         //TODO: Endian issues here
1730         else if( Depth == 24 )
1731         {
1732                 Uint8   *buf = Buffer;
1733                 Uint8   bg_r = (BGC >> 16) & 0xFF;
1734                 Uint8   bg_g = (BGC >>  8) & 0xFF;
1735                 Uint8   bg_b = (BGC >>  0) & 0xFF;
1736                 Uint8   fg_r = (FGC >> 16) & 0xFF;
1737                 Uint8   fg_g = (FGC >>  8) & 0xFF;
1738                 Uint8   fg_b = (FGC >>  0) & 0xFF;
1739                 
1740                 font = VT_Font_GetChar(Codepoint);
1741                 
1742                 for(y = 0; y < FONT_HEIGHT; y ++)
1743                 {
1744                         for(x = 0; x < FONT_WIDTH; x ++)
1745                         {
1746                                 Uint8   r, g, b;
1747                                 
1748                                 if(*font & (1 << (FONT_WIDTH-x-1))) {
1749                                         r = fg_r;       g = fg_g;       b = fg_b;
1750                                 }
1751                                 else {
1752                                         r = bg_r;       g = bg_g;       b = bg_b;
1753                                 }
1754                                 buf[x*3+0] = b;
1755                                 buf[x*3+1] = g;
1756                                 buf[x*3+2] = r;
1757                         }
1758                         buf = (void*)( (tVAddr)buf + Pitch );
1759                         font ++;
1760                 }
1761         }
1762         // 32-bpp colour (nice and easy)
1763         else if( Depth == 32 )
1764         {
1765                 Uint32  *buf = Buffer;
1766                 
1767                 font = VT_Font_GetChar(Codepoint);
1768                 
1769                 for(y = 0; y < FONT_HEIGHT; y ++)
1770                 {
1771                         for(x = 0; x < FONT_WIDTH; x ++)
1772                         {
1773                                 if(*font & (1 << (FONT_WIDTH-x-1)))
1774                                         buf[x] = FGC;
1775                                 else
1776                                         buf[x] = BGC;
1777                         }
1778                         buf = (Uint32*)( (tVAddr)buf + Pitch );
1779                         font ++;
1780                 }
1781         }
1782 }
1783
1784 /**
1785  * \fn Uint32 VT_Colour12to24(Uint16 Col12)
1786  * \brief Converts a 12-bit colour into 24 bits
1787  */
1788 Uint32 VT_Colour12to24(Uint16 Col12)
1789 {
1790         Uint32  ret;
1791          int    tmp;
1792         tmp = Col12 & 0xF;
1793         ret  = (tmp << 0) | (tmp << 4);
1794         tmp = (Col12 & 0xF0) >> 4;
1795         ret |= (tmp << 8) | (tmp << 12);
1796         tmp = (Col12 & 0xF00) >> 8;
1797         ret |= (tmp << 16) | (tmp << 20);
1798         return ret;
1799 }
1800 /**
1801  * \brief Converts a 12-bit colour into 15 bits
1802  */
1803 Uint16 VT_Colour12to15(Uint16 Col12)
1804 {
1805         Uint32  ret;
1806          int    tmp;
1807         tmp = Col12 & 0xF;
1808         ret  = (tmp << 1) | (tmp & 1);
1809         tmp = (Col12 & 0xF0) >> 4;
1810         ret |= ( (tmp << 1) | (tmp & 1) ) << 5;
1811         tmp = (Col12 & 0xF00) >> 8;
1812         ret |= ( (tmp << 1) | (tmp & 1) ) << 10;
1813         return ret;
1814 }
1815
1816 /**
1817  * \brief Converts a 12-bit colour into any other depth
1818  * \param Col12 12-bit source colour
1819  * \param Depth Desired bit deptj
1820  * \note Green then blue get the extra avaliable bits (16:5-6-5, 14:4-5-5)
1821  */
1822 Uint32 VT_Colour12toN(Uint16 Col12, int Depth)
1823 {
1824         Uint32  ret;
1825         Uint32  r, g, b;
1826          int    rSize, gSize, bSize;
1827         
1828         // Fast returns
1829         if( Depth == 24 )       return VT_Colour12to24(Col12);
1830         if( Depth == 15 )       return VT_Colour12to15(Col12);
1831         // - 32 is a special case, it's usually 24-bit colour with an unused byte
1832         if( Depth == 32 )       return VT_Colour12to24(Col12);
1833         
1834         // Bounds checks
1835         if( Depth < 8 ) return 0;
1836         if( Depth > 32 )        return 0;
1837         
1838         r = Col12 & 0xF;
1839         g = (Col12 & 0xF0) >> 4;
1840         b = (Col12 & 0xF00) >> 8;
1841         
1842         rSize = gSize = bSize = Depth / 3;
1843         if( rSize + gSize + bSize < Depth )     // Depth % 3 == 1
1844                 gSize ++;
1845         if( rSize + gSize + bSize < Depth )     // Depth % 3 == 2
1846                 bSize ++;
1847         
1848         // Expand
1849         r <<= rSize - 4;        g <<= gSize - 4;        b <<= bSize - 4;
1850         // Fill with the lowest bit
1851         if( Col12 & 0x001 )     r |= (1 << (rSize - 4)) - 1;
1852         if( Col12 & 0x010 )     r |= (1 << (gSize - 4)) - 1;
1853         if( Col12 & 0x100 )     r |= (1 << (bSize - 4)) - 1;
1854         
1855         // Create output
1856         ret  = r;
1857         ret |= g << rSize;
1858         ret |= b << (rSize + gSize);
1859         
1860         return ret;
1861 }
1862
1863 /**
1864  * \fn Uint8 *VT_Font_GetChar(Uint32 Codepoint)
1865  * \brief Gets an index into the font array given a Unicode Codepoint
1866  * \note See http://en.wikipedia.org/wiki/CP437
1867  */
1868 Uint8 *VT_Font_GetChar(Uint32 Codepoint)
1869 {
1870          int    index = 0;
1871         if(Codepoint < 128)
1872                 return &VTermFont[Codepoint*FONT_HEIGHT];
1873         switch(Codepoint)
1874         {
1875         case 0xC7:      index = 128;    break;  // Ç
1876         case 0xFC:      index = 129;    break;  // ü
1877         case 0xE9:      index = 130;    break;  // é
1878         case 0xE2:      index = 131;    break;  // â
1879         case 0xE4:      index = 132;    break;  // ä
1880         case 0xE0:      index = 133;    break;  // à
1881         case 0xE5:      index = 134;    break;  // å
1882         case 0xE7:      index = 135;    break;  // ç
1883         case 0xEA:      index = 136;    break;  // ê
1884         case 0xEB:      index = 137;    break;  // ë
1885         case 0xE8:      index = 138;    break;  // è
1886         case 0xEF:      index = 139;    break;  // ï
1887         case 0xEE:      index = 140;    break;  // î
1888         case 0xEC:      index = 141;    break;  // ì
1889         case 0xC4:      index = 142;    break;  // Ä
1890         case 0xC5:      index = 143;    break;  // Å
1891         }
1892         
1893         return &VTermFont[index*FONT_HEIGHT];
1894 }
1895
1896 EXPORTAS(&giVT_CharWidth, giVT_CharWidth);
1897 EXPORTAS(&giVT_CharHeight, giVT_CharHeight);
1898 EXPORT(VT_Font_Render);
1899 EXPORT(VT_Colour12to24);

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