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

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