Slight change to VMM defines
[tpg/acess2.git] / Kernel / drv / vterm.c
1 /*
2  * Acess2 Virtual Terminal Driver
3  */
4 #define DEBUG   0
5 #include <acess.h>
6 #include <fs_devfs.h>
7 #include <modules.h>
8 #include <tpl_drv_video.h>
9 #include <tpl_drv_keyboard.h>
10 #include <tpl_drv_terminal.h>
11 #include <errno.h>
12
13 #define USE_CTRL_ALT    0
14
15 // === CONSTANTS ===
16 #define VERSION ((0<<8)|(50))
17
18 #define NUM_VTS 8
19 #define MAX_INPUT_CHARS32       64
20 #define MAX_INPUT_CHARS8        (MAX_INPUT_CHARS32*4)
21 //#define DEFAULT_OUTPUT        "BochsGA"
22 #define DEFAULT_OUTPUT  "Vesa"
23 #define DEFAULT_INPUT   "PS2Keyboard"
24 #define DEFAULT_WIDTH   640
25 #define DEFAULT_HEIGHT  480
26 #define DEFAULT_SCROLLBACK      2       // 2 Screens of text + current screen
27 #define DEFAULT_COLOUR  (VT_COL_BLACK|(0xAAA<<16))
28
29 #define VT_FLAG_HIDECSR 0x01
30 #define VT_FLAG_HASFB   0x10    //!< Set if the VTerm has requested the Framebuffer
31
32 enum eVT_InModes {
33         VT_INMODE_TEXT8,        // UTF-8 Text Mode (VT100 Emulation)
34         VT_INMODE_TEXT32,       // UTF-32 Text Mode (Acess Native)
35         NUM_VT_INMODES
36 };
37
38 // === TYPES ===
39 typedef struct {
40          int    Mode;   //!< Current Mode (see ::eTplTerminal_Modes)
41          int    Flags;  //!< Flags (see VT_FLAG_*)
42         
43         short   NewWidth;       //!< Un-applied dimensions (Width)
44         short   NewHeight;      //!< Un-applied dimensions (Height)
45         short   Width;  //!< Virtual Width
46         short   Height; //!< Virtual Height
47         short   TextWidth;      //!< Text Virtual Width
48         short   TextHeight;     //!< Text Virtual Height
49         
50          int    ViewPos;        //!< View Buffer Offset (Text Only)
51          int    WritePos;       //!< Write Buffer Offset (Text Only)
52         Uint32  CurColour;      //!< Current Text Colour
53         
54         tMutex  ReadingLock;    //!< Lock the VTerm when a process is reading from it
55         tTID    ReadingThread;  //!< Owner of the lock
56          int    InputRead;      //!< Input buffer read position
57          int    InputWrite;     //!< Input buffer write position
58         char    InputBuffer[MAX_INPUT_CHARS8];
59         
60         tVT_Char        *Text;
61         Uint32          *Buffer;
62         
63         char    Name[2];        //!< Name of the terminal
64         tVFS_Node       Node;
65 } tVTerm;
66
67 // === IMPORTS ===
68 extern void     Debug_SetKTerminal(char *File);
69
70 // === PROTOTYPES ===
71  int    VT_Install(char **Arguments);
72 void    VT_InitOutput(void);
73 void    VT_InitInput(void);
74 char    *VT_ReadDir(tVFS_Node *Node, int Pos);
75 tVFS_Node       *VT_FindDir(tVFS_Node *Node, const char *Name);
76  int    VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data);
77 Uint64  VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
78 Uint64  VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
79  int    VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data);
80 void    VT_SetResolution(int Width, int Height);
81 void    VT_SetMode(int Mode);
82 void    VT_SetTerminal(int ID);
83 void    VT_KBCallBack(Uint32 Codepoint);
84 void    VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count);
85  int    VT_int_ParseEscape(tVTerm *Term, char *Buffer);
86 void    VT_int_PutChar(tVTerm *Term, Uint32 Ch);
87 void    VT_int_ScrollFramebuffer( tVTerm *Term );
88 void    VT_int_UpdateScreen( tVTerm *Term, int UpdateAll );
89 void    VT_int_ChangeMode(tVTerm *Term, int NewMode, int NewWidth, int NewHeight);
90
91 // === CONSTANTS ===
92 const Uint16    caVT100Colours[] = {
93                 // Black, Red, Green, Yellow, Blue, Purple, Cyan, Gray
94                 // Same again, but bright
95                 VT_COL_BLACK, 0x700, 0x070, 0x770, 0x007, 0x707, 0x077, 0xAAA,
96                 VT_COL_GREY, 0xF00, 0x0F0, 0xFF0, 0x00F, 0xF0F, 0x0FF, VT_COL_WHITE
97         };
98
99 // === GLOBALS ===
100 MODULE_DEFINE(0, VERSION, VTerm, VT_Install, NULL, DEFAULT_OUTPUT, DEFAULT_INPUT, NULL);
101 tDevFS_Driver   gVT_DrvInfo = {
102         NULL, "VTerm",
103         {
104         .Flags = VFS_FFLAG_DIRECTORY,
105         .Size = NUM_VTS,
106         .Inode = -1,
107         .NumACLs = 0,
108         .ReadDir = VT_ReadDir,
109         .FindDir = VT_FindDir,
110         .IOCtl = VT_Root_IOCtl
111         }
112 };
113 // --- Terminals ---
114 tVTerm  gVT_Terminals[NUM_VTS];
115  int    giVT_CurrentTerminal = 0;
116 tVTerm  *gpVT_CurTerm = &gVT_Terminals[0];
117 // --- Video State ---
118 short   giVT_RealWidth  = DEFAULT_WIDTH;        //!< Screen Width
119 short   giVT_RealHeight = DEFAULT_HEIGHT;       //!< Screen Height
120  int    giVT_Scrollback = DEFAULT_SCROLLBACK;
121 // --- Driver Handles ---
122 char    *gsVT_OutputDevice = NULL;
123 char    *gsVT_InputDevice = NULL;
124  int    giVT_OutputDevHandle = -2;
125  int    giVT_InputDevHandle = -2;
126 // --- Key States --- (Used for VT Switching/Magic Combos)
127  int    gbVT_CtrlDown = 0;
128  int    gbVT_AltDown = 0;
129  int    gbVT_SysrqDown = 0;
130
131 // === CODE ===
132 /**
133  * \fn int VT_Install(char **Arguments)
134  * \brief Installs the Virtual Terminal Driver
135  */
136 int VT_Install(char **Arguments)
137 {
138          int    i;
139         
140         // Scan Arguments
141         if(Arguments)
142         {
143                 char    **args = Arguments;
144                 char    *arg, *opt, *val;
145                 for( ; (arg = *args); args++ )
146                 {
147                         Log_Debug("VTerm", "Argument '%s'", arg);
148                         opt = arg;
149                         val = arg + strpos(arg, '=');   *val++ = '\0';
150                         
151                         if( strcmp(opt, "Video") == 0 ) {
152                                 gsVT_OutputDevice = val;
153                         }
154                         else if( strcmp(opt, "Input") == 0 ) {
155                                 gsVT_InputDevice = val;
156                         }
157                         else if( strcmp(opt, "Width") == 0 ) {
158                                 giVT_RealWidth = atoi( val );
159                         }
160                         else if( strcmp(opt, "Height") == 0 ) {
161                                 giVT_RealHeight = atoi( val );
162                         }
163                         else if( strcmp(opt, "Scrollback") == 0 ) {
164                                 giVT_Scrollback = atoi( val );
165                         }
166                 }
167         }
168         
169         if(gsVT_OutputDevice)   Modules_InitialiseBuiltin( gsVT_OutputDevice );
170         if(gsVT_InputDevice)    Modules_InitialiseBuiltin( gsVT_InputDevice );
171         
172         // Apply Defaults
173         if(!gsVT_OutputDevice)  gsVT_OutputDevice = DEFAULT_OUTPUT;
174         if(!gsVT_InputDevice)   gsVT_InputDevice = DEFAULT_INPUT;
175         
176         // Create paths
177         {
178                 char    *tmp;
179                 tmp = malloc( 9 + strlen(gsVT_OutputDevice) + 1 );
180                 strcpy(tmp, "/Devices/");
181                 strcpy(&tmp[9], gsVT_OutputDevice);
182                 gsVT_OutputDevice = tmp;
183                 tmp = malloc( 9 + strlen(gsVT_InputDevice) + 1 );
184                 strcpy(tmp, "/Devices/");
185                 strcpy(&tmp[9], gsVT_InputDevice);
186                 gsVT_InputDevice = tmp;
187         }
188         
189         Log_Log("VTerm", "Using '%s' as output", gsVT_OutputDevice);
190         Log_Log("VTerm", "Using '%s' as input", gsVT_InputDevice);
191         
192         // Create Nodes
193         for( i = 0; i < NUM_VTS; i++ )
194         {
195                 gVT_Terminals[i].Mode = TERM_MODE_TEXT;
196                 gVT_Terminals[i].Flags = 0;
197                 gVT_Terminals[i].CurColour = DEFAULT_COLOUR;
198                 gVT_Terminals[i].WritePos = 0;
199                 gVT_Terminals[i].ViewPos = 0;
200                 
201                 // Initialise
202                 VT_int_ChangeMode( &gVT_Terminals[i],
203                         TERM_MODE_TEXT, giVT_RealWidth, giVT_RealHeight );
204                 
205                 gVT_Terminals[i].Name[0] = '0'+i;
206                 gVT_Terminals[i].Name[1] = '\0';
207                 gVT_Terminals[i].Node.Inode = i;
208                 gVT_Terminals[i].Node.ImplPtr = &gVT_Terminals[i];
209                 gVT_Terminals[i].Node.NumACLs = 0;      // Only root can open virtual terminals
210                 
211                 gVT_Terminals[i].Node.Read = VT_Read;
212                 gVT_Terminals[i].Node.Write = VT_Write;
213                 gVT_Terminals[i].Node.IOCtl = VT_Terminal_IOCtl;
214         }
215         
216         // Add to DevFS
217         DevFS_AddDevice( &gVT_DrvInfo );
218         
219         VT_InitOutput();
220         VT_InitInput();
221         
222         // Set kernel output to VT0
223         Debug_SetKTerminal("/Devices/VTerm/0");
224         
225         Log_Log("VTerm", "Returning %i", MODULE_ERR_OK);
226         return MODULE_ERR_OK;
227 }
228
229 /**
230  * \fn void VT_InitOutput()
231  * \brief Initialise Video Output
232  */
233 void VT_InitOutput()
234 {
235         giVT_OutputDevHandle = VFS_Open(gsVT_OutputDevice, VFS_OPENFLAG_WRITE);
236         if(giVT_InputDevHandle == -1) {
237                 Log_Warning("VTerm", "Oh F**k, I can't open the video device '%s'", gsVT_OutputDevice);
238                 return ;
239         }
240         VT_SetResolution( giVT_RealWidth, giVT_RealHeight );
241         VT_SetTerminal( 0 );
242         VT_SetMode( VIDEO_BUFFMT_TEXT );
243 }
244
245 /**
246  * \fn void VT_InitInput()
247  * \brief Initialises the input
248  */
249 void VT_InitInput()
250 {
251         giVT_InputDevHandle = VFS_Open(gsVT_InputDevice, VFS_OPENFLAG_READ);
252         if(giVT_InputDevHandle == -1)   return ;
253         VFS_IOCtl(giVT_InputDevHandle, KB_IOCTL_SETCALLBACK, VT_KBCallBack);
254 }
255
256 /**
257  * \brief Set the video resolution
258  * \param Width New screen width
259  * \param Height        New screen height
260  */
261 void VT_SetResolution(int Width, int Height)
262 {
263         tVideo_IOCtl_Mode       mode = {0};
264          int    tmp;
265          int    i;
266         
267         // Create the video mode
268         mode.width = Width;
269         mode.height = Height;
270         mode.bpp = 32;
271         mode.flags = 0;
272         
273         // Set video mode
274         VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_FINDMODE, &mode );
275         tmp = mode.id;
276         if( Width != mode.width || Height != mode.height )
277         {
278                 Log_Warning("VTerm",
279                         "Selected resolution (%ix%i is not supported) by the device, using (%ix%i)",
280                         giVT_RealWidth, giVT_RealHeight,
281                         mode.width, mode.height
282                         );
283         }
284         VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_GETSETMODE, &tmp );
285         
286         // Resize text terminals if needed
287         if( giVT_RealWidth != mode.width || giVT_RealHeight != mode.height )
288         {
289                  int    newBufSize = (giVT_RealWidth/giVT_CharWidth)
290                                         *(giVT_RealHeight/giVT_CharHeight)
291                                         *(giVT_Scrollback+1);
292                 //tVT_Char      *tmp;
293                 // Resize the text terminals
294                 giVT_RealWidth = mode.width;
295                 giVT_RealHeight = mode.height;
296                 for( i = 0; i < NUM_VTS; i ++ )
297                 {
298                         if( gVT_Terminals[i].Mode != TERM_MODE_TEXT )   continue;
299                         
300                         gVT_Terminals[i].Text = realloc(
301                                 gVT_Terminals[i].Text,
302                                 newBufSize*sizeof(tVT_Char)
303                                 );
304                 }
305         }
306 }
307
308 /**
309  * \brief Set video output buffer mode
310  */
311 void VT_SetMode(int Mode)
312 {
313         VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &Mode );
314 }
315
316 /**
317  * \fn char *VT_ReadDir(tVFS_Node *Node, int Pos)
318  * \brief Read from the VTerm Directory
319  */
320 char *VT_ReadDir(tVFS_Node *Node, int Pos)
321 {
322         if(Pos < 0)     return NULL;
323         if(Pos >= NUM_VTS)      return NULL;
324         return strdup( gVT_Terminals[Pos].Name );
325 }
326
327 /**
328  * \fn tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
329  * \brief Find an item in the VTerm directory
330  * \param Node  Root node
331  * \param Name  Name (number) of the terminal
332  */
333 tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
334 {
335          int    num;
336         
337         ENTER("pNode sName", Node, Name);
338         
339         // Open the input and output files if needed
340         if(giVT_OutputDevHandle == -2)  VT_InitOutput();
341         if(giVT_InputDevHandle == -2)   VT_InitInput();
342         
343         // Sanity check name
344         if(Name[0] < '0' || Name[0] > '9' || Name[1] != '\0') {
345                 LEAVE('n');
346                 return NULL;
347         }
348         // Get index
349         num = Name[0] - '0';
350         if(num >= NUM_VTS) {
351                 LEAVE('n');
352                 return NULL;
353         }
354         // Return node
355         LEAVE('p', &gVT_Terminals[num].Node);
356         return &gVT_Terminals[num].Node;
357 }
358
359 /**
360  * \fn int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
361  * \brief Control the VTerm Driver
362  */
363 int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
364 {
365          int    len;
366         switch(Id)
367         {
368         case DRV_IOCTL_TYPE:    return DRV_TYPE_MISC;
369         case DRV_IOCTL_IDENT:   memcpy(Data, "VT\0\0", 4);      return 0;
370         case DRV_IOCTL_VERSION: return VERSION;
371         case DRV_IOCTL_LOOKUP:  return 0;
372         
373         case 4: // Get Video Driver
374                 if(Data)        strcpy(Data, gsVT_OutputDevice);
375                 return strlen(gsVT_OutputDevice);
376         
377         case 5: // Set Video Driver
378                 if(!Data)       return -EINVAL;
379                 if(Threads_GetUID() != 0)       return -EACCES;
380                 
381                 len = strlen(Data);
382                 
383                 free(gsVT_OutputDevice);
384                 
385                 gsVT_OutputDevice = malloc(len+1);
386                 strcpy(gsVT_OutputDevice, Data);
387                 
388                 VFS_Close(giVT_OutputDevHandle);
389                 giVT_OutputDevHandle = -1;
390                 
391                 VT_InitOutput();
392                 return 1;
393         }
394         return 0;
395 }
396
397 /**
398  * \fn Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
399  * \brief Read from a virtual terminal
400  */
401 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
402 {
403          int    pos = 0;
404         tVTerm  *term = &gVT_Terminals[ Node->Inode ];
405         
406         Mutex_Acquire( &term->ReadingLock );
407         term->ReadingThread = Threads_GetTID();
408         
409         // Check current mode
410         switch(term->Mode)
411         {
412         // Text Mode (UTF-8)
413         case TERM_MODE_TEXT:
414                 while(pos < Length)
415                 {
416                         //TODO: Sleep instead
417                         while(term->InputRead == term->InputWrite)      Threads_Sleep();
418                         
419                         ((char*)Buffer)[pos] = term->InputBuffer[term->InputRead];
420                         pos ++;
421                         term->InputRead ++;
422                         term->InputRead %= MAX_INPUT_CHARS8;
423                 }
424                 break;
425         
426         //case TERM_MODE_FB:
427         // Other - UCS-4
428         default:
429                 while(pos < Length)
430                 {
431                         while(term->InputRead == term->InputWrite)      Threads_Sleep();
432                         ((Uint32*)Buffer)[pos] = ((Uint32*)term->InputBuffer)[term->InputRead];
433                         pos ++;
434                         term->InputRead ++;
435                         term->InputRead %= MAX_INPUT_CHARS32;
436                 }
437                 break;
438         }
439         
440         term->ReadingThread = -1;
441         Mutex_Release( &term->ReadingLock );
442         
443         return 0;
444 }
445
446 /**
447  * \fn Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
448  * \brief Write to a virtual terminal
449  */
450 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
451 {
452         tVTerm  *term = &gVT_Terminals[ Node->Inode ];
453          int    size;
454         
455         // Write
456         switch( term->Mode )
457         {
458         // Print Text
459         case TERM_MODE_TEXT:
460                 VT_int_PutString(term, Buffer, Length);
461                 break;
462         // Framebuffer :)
463         case TERM_MODE_FB:
464         
465                 // - Sanity Checking
466                 size = term->Width*term->Height*4;
467                 if( Offset > size ) {
468                         Log_Notice("VTerm", "VT_Write: Offset (0x%llx) > FBSize (0x%x)",
469                                 Offset, size);
470                         return 0;
471                 }
472                 if( Offset + Length > size ) {
473                         Log_Notice("VTerm", "VT_Write: Offset+Length (0x%llx) > FBSize (0x%x)",
474                                 Offset+Length, size);
475                         Length = size - Offset;
476                 }
477                 
478                 // Copy to the local cache
479                 memcpy( (void*)((Uint)term->Buffer + (Uint)Offset), Buffer, Length );
480                 
481                 // Update screen if needed
482                 if( Node->Inode == giVT_CurrentTerminal )
483                 {
484                         // Fill entire screen?
485                         if( giVT_RealWidth > term->Width || giVT_RealHeight > term->Height )
486                         {
487                                 // No? :( Well, just center it
488                                  int    x, y, w, h;
489                                 x = Offset/4;   y = x / term->Width;    x %= term->Width;
490                                 w = Length/4+x; h = w / term->Width;    w %= term->Width;
491                                 // Center
492                                 x += (giVT_RealWidth - term->Width) / 2;
493                                 y += (giVT_RealHeight - term->Height) / 2;
494                                 while(h--)
495                                 {
496                                         VFS_WriteAt( giVT_OutputDevHandle,
497                                                 (x + y * giVT_RealWidth)*4,
498                                                 term->Width * 4,
499                                                 Buffer
500                                                 );
501                                         Buffer = (void*)( (Uint)Buffer + term->Width*4 );
502                                         y ++;
503                                 }
504                                 return 0;
505                         }
506                         else {
507                                 return VFS_WriteAt( giVT_OutputDevHandle, Offset, Length, Buffer );
508                         }
509                 }
510         // Just pass on (for now)
511         // TODO: Handle locally too to ensure no information is lost on
512         //       VT Switch (and to isolate terminals from each other)
513         case TERM_MODE_2DACCEL:
514         //case TERM_MODE_3DACCEL:
515                 if( Node->Inode == giVT_CurrentTerminal )
516                 {
517                         VFS_Write( giVT_OutputDevHandle, Length, Buffer );
518                 }
519                 break;
520         }
521         
522         return 0;
523 }
524
525 /**
526  * \fn int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
527  * \brief Call an IO Control on a virtual terminal
528  */
529 int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
530 {
531          int    *iData = Data;
532          int    ret;
533         tVTerm  *term = Node->ImplPtr;
534         ENTER("pNode iId pData", Node, Id, Data);
535         
536         if(Id >= DRV_IOCTL_LOOKUP) {
537                 // Only root can fiddle with graphics modes
538                 // TODO: Remove this and replace with user ownership
539                 if( Threads_GetUID() != 0 )     return -1;
540         }
541         
542         switch(Id)
543         {
544         // --- Core Defined
545         case DRV_IOCTL_TYPE:
546                 LEAVE('i', DRV_TYPE_TERMINAL);
547                 return DRV_TYPE_TERMINAL;
548         case DRV_IOCTL_IDENT:
549                 memcpy(Data, "VT\0\0", 4);
550                 LEAVE('i', 0);
551                 return 0;
552         case DRV_IOCTL_VERSION:
553                 LEAVE('x', VERSION);
554                 return VERSION;
555         case DRV_IOCTL_LOOKUP:
556                 LEAVE('i', 0);
557                 return 0;
558         
559         // Get/Set the mode (and apply any changes)
560         case TERM_IOCTL_MODETYPE:
561                 if(Data != NULL)
562                 {
563                         if( CheckMem(Data, sizeof(int)) == 0 ) {
564                                 LEAVE('i', -1);
565                                 return -1;
566                         }
567                         Log_Log("VTerm", "VTerm %i mode set to %i", (int)Node->Inode, *iData);
568                         
569                         // Update mode if needed
570                         if( term->Mode != *iData
571                          || term->NewWidth
572                          || term->NewHeight)
573                         {
574                                 // Adjust for text mode
575                                 if( *iData == TERM_MODE_TEXT ) {
576                                         term->NewHeight *= giVT_CharHeight;
577                                         term->NewWidth *= giVT_CharWidth;
578                                 }
579                                 // Fill unchanged dimensions
580                                 if(term->NewHeight == 0)        term->NewHeight = term->Height;
581                                 if(term->NewWidth == 0) term->NewWidth = term->Width;
582                                 // Set new mode
583                                 VT_int_ChangeMode(term, *iData, term->NewWidth, term->NewHeight);
584                                 // Clear unapplied dimensions
585                                 term->NewWidth = 0;
586                                 term->NewHeight = 0;
587                         }
588                         
589                         // Update the screen dimensions
590                         if(Node->Inode == giVT_CurrentTerminal)
591                                 VT_SetTerminal( giVT_CurrentTerminal );
592                 }
593                 LEAVE('i', term->Mode);
594                 return term->Mode;
595         
596         // Get/set the terminal width
597         case TERM_IOCTL_WIDTH:
598                 if(Data != NULL) {
599                         if( CheckMem(Data, sizeof(int)) == 0 ) {
600                                 LEAVE('i', -1);
601                                 return -1;
602                         }
603                         term->NewWidth = *iData;
604                 }
605                 if( term->NewWidth )
606                         ret = term->NewWidth;
607                 else if( term->Mode == TERM_MODE_TEXT )
608                         ret = term->TextWidth;
609                 else
610                         ret = term->Width;
611                 LEAVE('i', ret);
612                 return ret;
613         
614         // Get/set the terminal height
615         case TERM_IOCTL_HEIGHT:
616                 if(Data != NULL) {
617                         if( CheckMem(Data, sizeof(int)) == 0 ) {
618                                 LEAVE('i', -1);
619                                 return -1;
620                         }
621                         term->NewHeight = *iData;
622                 }
623                 if( term->NewHeight )
624                         ret = term->NewHeight;
625                 else if( term->Mode == TERM_MODE_TEXT )
626                         ret = term->TextHeight;
627                 else
628                         ret = term->Height;
629                 LEAVE('i', ret);
630                 return ret;
631         
632         case TERM_IOCTL_FORCESHOW:
633                 Log_Log("VTerm", "Thread %i forced VTerm %i to be shown",
634                         Threads_GetTID(), (int)Node->Inode);
635                 VT_SetTerminal( Node->Inode );
636                 LEAVE('i', 1);
637                 return 1;
638         }
639         LEAVE('i', -1);
640         return -1;
641 }
642
643 /**
644  * \fn void VT_SetTerminal(int ID)
645  * \brief Set the current terminal
646  */
647 void VT_SetTerminal(int ID)
648 {       
649         // Update current terminal ID
650         Log_Log("VTerm", "Changed terminal from %i to %i", giVT_CurrentTerminal, ID);
651         giVT_CurrentTerminal = ID;
652         gpVT_CurTerm = &gVT_Terminals[ID];
653         
654         // Update cursor
655         if( gpVT_CurTerm->Mode == TERM_MODE_TEXT && !(gpVT_CurTerm->Flags & VT_FLAG_HIDECSR) )
656         {
657                 tVideo_IOCtl_Pos        pos;
658                 pos.x = (gpVT_CurTerm->WritePos - gpVT_CurTerm->ViewPos) % gpVT_CurTerm->TextWidth;
659                 pos.y = (gpVT_CurTerm->WritePos - gpVT_CurTerm->ViewPos) / gpVT_CurTerm->TextWidth;
660                 if( pos.x < gpVT_CurTerm->TextHeight )
661                         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
662         }
663         
664         if( gpVT_CurTerm->Mode == TERM_MODE_TEXT )
665                 VT_SetMode( VIDEO_BUFFMT_TEXT );
666         else
667                 VT_SetMode( VIDEO_BUFFMT_FRAMEBUFFER );
668         
669         // Update the screen
670         VT_int_UpdateScreen( &gVT_Terminals[ ID ], 1 );
671 }
672
673 /**
674  * \fn void VT_KBCallBack(Uint32 Codepoint)
675  * \brief Called on keyboard interrupt
676  * \param Codepoint     Pseudo-UTF32 character
677  * 
678  * Handles a key press and sends the key code to the user's buffer.
679  * If the code creates a kernel-magic sequence, it is not passed to the
680  * user and is handled in-kernel.
681  */
682 void VT_KBCallBack(Uint32 Codepoint)
683 {
684         tVTerm  *term = gpVT_CurTerm;
685         
686         // How the hell did we get a codepoint of zero?
687         if(Codepoint == 0)      return;
688         
689         // Key Up
690         if( Codepoint & 0x80000000 )
691         {
692                 Codepoint &= 0x7FFFFFFF;
693                 switch(Codepoint)
694                 {
695                 #if !USE_CTRL_ALT
696                 case KEY_RSHIFT:        gbVT_CtrlDown = 0;      break;
697                 case KEY_LSHIFT:        gbVT_AltDown = 0;       break;
698                 #else
699                 case KEY_LALT:  gbVT_AltDown &= ~1;     break;
700                 case KEY_RALT:  gbVT_AltDown &= ~2;     break;
701                 case KEY_LCTRL: gbVT_CtrlDown &= ~1     break;
702                 case KEY_RCTRL: gbVT_CtrlDown &= ~2;    break;
703                 #endif
704                 }
705                 return;
706         }
707         
708         switch(Codepoint)
709         {
710         #if !USE_CTRL_ALT       // HACK: Use both shifts instead of Ctrl-Alt
711         case KEY_RSHIFT:        gbVT_CtrlDown = 1;      break;
712         case KEY_LSHIFT:        gbVT_AltDown = 1;       break;
713         #else
714         case KEY_LALT:  gbVT_AltDown |= 1;      break;
715         case KEY_RALT:  gbVT_AltDown |= 2;      break;
716         case KEY_LCTRL: gbVT_CtrlDown |= 1;     break;
717         case KEY_RCTRL: gbVT_CtrlDown |= 2;     break;
718         #endif
719         
720         default:
721                 if(!gbVT_AltDown || !gbVT_CtrlDown)
722                         break;
723                 switch(Codepoint)
724                 {
725                 case KEY_F1:    VT_SetTerminal(0);      return;
726                 case KEY_F2:    VT_SetTerminal(1);      return;
727                 case KEY_F3:    VT_SetTerminal(2);      return;
728                 case KEY_F4:    VT_SetTerminal(3);      return;
729                 case KEY_F5:    VT_SetTerminal(4);      return;
730                 case KEY_F6:    VT_SetTerminal(5);      return;
731                 case KEY_F7:    VT_SetTerminal(6);      return;
732                 case KEY_F8:    VT_SetTerminal(7);      return;
733                 case KEY_F9:    VT_SetTerminal(8);      return;
734                 case KEY_F10:   VT_SetTerminal(9);      return;
735                 case KEY_F11:   VT_SetTerminal(10);     return;
736                 case KEY_F12:   VT_SetTerminal(11);     return;
737                 // Scrolling
738                 case KEY_PGUP:
739                         if( gpVT_CurTerm->ViewPos > gpVT_CurTerm->Width )
740                                 gpVT_CurTerm->ViewPos -= gpVT_CurTerm->Width;
741                         else
742                                 gpVT_CurTerm->ViewPos = 0;
743                         return;
744                 case KEY_PGDOWN:
745                         if( gpVT_CurTerm->ViewPos < gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback-1) )
746                                 gpVT_CurTerm->ViewPos += gpVT_CurTerm->Width;
747                         else
748                                 gpVT_CurTerm->ViewPos = gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback-1);
749                         return;
750                 }
751         }
752         
753         // Encode key
754         if(term->Mode == TERM_MODE_TEXT)
755         {
756                 Uint8   buf[6] = {0};
757                  int    len = 0;
758                 
759                 // Ignore Modifer Keys
760                 if(Codepoint > KEY_MODIFIERS)   return;
761                 
762                 // Get UTF-8/ANSI Encoding
763                 switch(Codepoint)
764                 {
765                 case KEY_LEFT:
766                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'D';
767                         len = 3;
768                         break;
769                 case KEY_RIGHT:
770                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'C';
771                         len = 3;
772                         break;
773                 case KEY_UP:
774                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'A';
775                         len = 3;
776                         break;
777                 case KEY_DOWN:
778                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = 'B';
779                         len = 3;
780                         break;
781                 
782                 case KEY_PGUP:
783                         buf[0] = '\x1B';        buf[1] = '[';   buf[2] = '5';   // Some overline also
784                         //len = 4;      // Commented out until I'm sure
785                         break;
786                 case KEY_PGDOWN:
787                         len = 0;
788                         break;
789                 
790                 // Attempt to encode in UTF-8
791                 default:
792                         len = WriteUTF8( buf, Codepoint );
793                         if(len == 0) {
794                                 Warning("Codepoint (%x) is unrepresentable in UTF-8", Codepoint);
795                         }
796                         break;
797                 }
798                 
799                 if(len == 0) {
800                         // Unprintable / Don't Pass
801                         return;
802                 }
803                 
804                 // Write
805                 if( MAX_INPUT_CHARS8 - term->InputWrite >= len )
806                         memcpy( &term->InputBuffer[term->InputWrite], buf, len );
807                 else {
808                         memcpy( &term->InputBuffer[term->InputWrite], buf, MAX_INPUT_CHARS8 - term->InputWrite );
809                         memcpy( &term->InputBuffer[0], buf, len - (MAX_INPUT_CHARS8 - term->InputWrite) );
810                 }
811                 // Roll the buffer over
812                 term->InputWrite += len;
813                 term->InputWrite %= MAX_INPUT_CHARS8;
814                 if( (term->InputWrite - term->InputRead + MAX_INPUT_CHARS8)%MAX_INPUT_CHARS8 < len ) {
815                         term->InputRead = term->InputWrite + 1;
816                         term->InputRead %= MAX_INPUT_CHARS8;
817                 }
818                 
819         }
820         else
821         {
822                 // Encode the raw UTF-32 Key
823                 ((Uint32*)term->InputBuffer)[ term->InputWrite ] = Codepoint;
824                 term->InputWrite ++;
825                 term->InputWrite %= MAX_INPUT_CHARS32;
826                 if(term->InputRead == term->InputWrite) {
827                         term->InputRead ++;
828                         term->InputRead %= MAX_INPUT_CHARS32;
829                 }
830         }
831         
832         // Wake up the thread waiting on us
833         if( term->ReadingThread >= 0 ) {
834                 Threads_WakeTID(term->ReadingThread);
835         }
836 }
837
838 /**
839  * \fn void VT_int_ClearLine(tVTerm *Term, int Num)
840  * \brief Clears a line in a virtual terminal
841  */
842 void VT_int_ClearLine(tVTerm *Term, int Num)
843 {
844          int    i;
845         tVT_Char        *cell = &Term->Text[ Num*Term->TextWidth ];
846         if( Num < 0 || Num >= Term->TextHeight * (giVT_Scrollback + 1) )        return ;
847         //ENTER("pTerm iNum", Term, Num);
848         for( i = Term->TextWidth; i--; )
849         {
850                 cell[ i ].Ch = 0;
851                 cell[ i ].Colour = Term->CurColour;
852         }
853         //LEAVE('-');
854 }
855
856 /**
857  * \fn int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
858  * \brief Parses a VT100 Escape code
859  */
860 int VT_int_ParseEscape(tVTerm *Term, char *Buffer)
861 {
862         char    c;
863          int    argc = 0, j = 1;
864          int    tmp;
865          int    args[6] = {0,0,0,0};
866         
867         switch(Buffer[0])
868         {
869         //Large Code
870         case '[':
871                 // Get Arguments
872                 c = Buffer[j++];
873                 if( '0' <= c && c <= '9' )
874                 {
875                         do {
876                                 if(c == ';')    c = Buffer[j++];
877                                 while('0' <= c && c <= '9') {
878                                         args[argc] *= 10;
879                                         args[argc] += c-'0';
880                                         c = Buffer[j++];
881                                 }
882                                 argc ++;
883                         } while(c == ';');
884                 }
885                 
886                 // Get Command
887                 if(     ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
888                 {
889                         switch(c)
890                         {
891                         // Left
892                         case 'D':
893                                 if(argc == 1)   tmp = args[0];
894                                 else    tmp = 1;
895                                 
896                                 if( Term->WritePos-(tmp-1) % Term->TextWidth == 0 )
897                                         Term->WritePos -= Term->WritePos % Term->TextWidth;
898                                 else
899                                         Term->WritePos -= tmp;
900                                 break;
901                         
902                         // Right
903                         case 'C':
904                                 if(argc == 1)   tmp = args[0];
905                                 else    tmp = 1;
906                                 if( (Term->WritePos + tmp) % Term->TextWidth == 0 ) {
907                                         Term->WritePos -= Term->WritePos % Term->TextWidth;
908                                         Term->WritePos += Term->TextWidth - 1;
909                                 } else
910                                         Term->WritePos += tmp;
911                                 break;
912                         
913                         // Clear By Line
914                         case 'J':
915                                 // Clear Screen
916                                 switch(args[0])
917                                 {
918                                 case 2:
919                                         {
920                                          int    i = Term->TextHeight * (giVT_Scrollback + 1);
921                                         while( i-- )    VT_int_ClearLine(Term, i);
922                                         Term->WritePos = 0;
923                                         Term->ViewPos = 0;
924                                         VT_int_UpdateScreen(Term, 1);
925                                         }
926                                         break;
927                                 }
928                                 break;
929                         // Set cursor position
930                         case 'h':
931                                 Term->WritePos = args[0] + args[1]*Term->TextWidth;
932                                 Log_Debug("VTerm", "args = {%i, %i}", args[0], args[1]);
933                                 break;
934                         // Set Font flags
935                         case 'm':
936                                 for( ; argc--; )
937                                 {
938                                         // Flags
939                                         if( 0 <= args[argc] && args[argc] <= 8)
940                                         {
941                                                 switch(args[argc])
942                                                 {
943                                                 case 0: Term->CurColour = DEFAULT_COLOUR;       break;  // Reset
944                                                 case 1: Term->CurColour |= 0x80000000;  break;  // Bright
945                                                 case 2: Term->CurColour &= ~0x80000000; break;  // Dim
946                                                 }
947                                         }
948                                         // Foreground Colour
949                                         else if(30 <= args[argc] && args[argc] <= 37) {
950                                                 Term->CurColour &= 0xF000FFFF;
951                                                 Term->CurColour |= (Uint32)caVT100Colours[ args[argc]-30+(Term->CurColour>>28) ] << 16;
952                                         }
953                                         // Background Colour
954                                         else if(40 <= args[argc] && args[argc] <= 47) {
955                                                 Term->CurColour &= 0xFFFF8000;
956                                                 Term->CurColour |= caVT100Colours[ args[argc]-40+((Term->CurColour>>12)&15) ];
957                                         }
958                                 }
959                                 break;
960                         default:
961                                 Log_Warning("VTerm", "Unknown control sequence");
962                                 break;
963                         }
964                 }
965                 break;
966                 
967         default:        break;
968         }
969         
970         //Log_Debug("VTerm", "j = %i, Buffer = '%s'", j, Buffer);
971         return j;
972 }
973
974 /**
975  * \fn void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
976  * \brief Print a string to the Virtual Terminal
977  */
978 void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count)
979 {
980         Uint32  val;
981          int    i;
982         
983         // Iterate
984         for( i = 0; i < Count; i++ )
985         {
986                 // Handle escape sequences
987                 if( Buffer[i] == 0x1B )
988                 {
989                         i ++;
990                         i += VT_int_ParseEscape(Term, (char*)&Buffer[i]) - 1;
991                         continue;
992                 }
993                 
994                 // Fast check for non UTF-8
995                 if( Buffer[i] < 128 )   // Plain ASCII
996                         VT_int_PutChar(Term, Buffer[i]);
997                 else {  // UTF-8
998                         i += ReadUTF8(&Buffer[i], &val) - 1;
999                         VT_int_PutChar(Term, val);
1000                 }
1001         }
1002         // Update Screen
1003         VT_int_UpdateScreen( Term, 0 );
1004         
1005         // Update cursor
1006         if( Term == gpVT_CurTerm && !(Term->Flags & VT_FLAG_HIDECSR) )
1007         {
1008                 tVideo_IOCtl_Pos        pos;
1009                 pos.x = (Term->WritePos - Term->ViewPos) % Term->TextWidth;
1010                 pos.y = (Term->WritePos - Term->ViewPos) / Term->TextWidth;
1011                 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
1012         }
1013 }
1014
1015 /**
1016  * \fn void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
1017  * \brief Write a single character to a VTerm
1018  */
1019 void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
1020 {
1021          int    i;
1022                 
1023         switch(Ch)
1024         {
1025         case '\0':      return; // Ignore NULL byte
1026         case '\n':
1027                 VT_int_UpdateScreen( Term, 0 ); // Update the line before newlining
1028                 Term->WritePos += Term->TextWidth;
1029         case '\r':
1030                 Term->WritePos -= Term->WritePos % Term->TextWidth;
1031                 break;
1032         
1033         case '\t':
1034                 do {
1035                         Term->Text[ Term->WritePos ].Ch = '\0';
1036                         Term->Text[ Term->WritePos ].Colour = Term->CurColour;
1037                         Term->WritePos ++;
1038                 } while(Term->WritePos & 7);
1039                 break;
1040         
1041         case '\b':
1042                 // Backspace is invalid at Offset 0
1043                 if(Term->WritePos == 0) break;
1044                 
1045                 Term->WritePos --;
1046                 // Singe Character
1047                 if(Term->Text[ Term->WritePos ].Ch != '\0') {
1048                         Term->Text[ Term->WritePos ].Ch = 0;
1049                         Term->Text[ Term->WritePos ].Colour = Term->CurColour;
1050                         break;
1051                 }
1052                 // Tab
1053                 i = 7;  // Limit it to 8
1054                 do {
1055                         Term->Text[ Term->WritePos ].Ch = 0;
1056                         Term->Text[ Term->WritePos ].Colour = Term->CurColour;
1057                         Term->WritePos --;
1058                 } while(Term->WritePos && i-- && Term->Text[ Term->WritePos ].Ch == '\0');
1059                 if(Term->Text[ Term->WritePos ].Ch != '\0')
1060                         Term->WritePos ++;
1061                 break;
1062         
1063         default:
1064                 Term->Text[ Term->WritePos ].Ch = Ch;
1065                 Term->Text[ Term->WritePos ].Colour = Term->CurColour;
1066                 Term->WritePos ++;
1067                 break;
1068         }
1069         
1070         // Move Screen
1071         // - Check if we need to scroll the entire scrollback buffer
1072         if(Term->WritePos >= Term->TextWidth*Term->TextHeight*(giVT_Scrollback+1))
1073         {
1074                  int    base, i;
1075                 
1076                 // Move back by one
1077                 Term->WritePos -= Term->TextWidth;
1078                 // Update the scren
1079                 VT_int_UpdateScreen( Term, 0 );
1080                 
1081                 // Update view position
1082                 base = Term->TextWidth*Term->TextHeight*(giVT_Scrollback);
1083                 if(Term->ViewPos < base)
1084                         Term->ViewPos += Term->Width;
1085                 if(Term->ViewPos > base)
1086                         Term->ViewPos = base;
1087                 
1088                 // Scroll terminal cache
1089                 base = Term->TextWidth*(Term->TextHeight*(giVT_Scrollback+1)-1);
1090                 memcpy(
1091                         Term->Text,
1092                         &Term->Text[Term->TextWidth],
1093                         base*sizeof(tVT_Char)
1094                         );
1095                 
1096                 // Clear last row
1097                 for( i = 0; i < Term->TextWidth; i ++ )
1098                 {
1099                         Term->Text[ base + i ].Ch = 0;
1100                         Term->Text[ base + i ].Colour = Term->CurColour;
1101                 }
1102                 
1103                 VT_int_ScrollFramebuffer( Term );
1104                 VT_int_UpdateScreen( Term, 0 );
1105         }
1106         // Ok, so we only need to scroll the screen
1107         else if(Term->WritePos >= Term->ViewPos + Term->TextWidth*Term->TextHeight)
1108         {
1109                 //Debug("Term->WritePos (%i) >= %i",
1110                 //      Term->WritePos,
1111                 //      Term->ViewPos + Term->Width*Term->Height
1112                 //      );
1113                 //Debug("Scrolling screen only");
1114                 
1115                 // Update the last line
1116                 Term->WritePos -= Term->TextWidth;
1117                 VT_int_UpdateScreen( Term, 0 );
1118                 Term->WritePos += Term->TextWidth;
1119                 VT_int_ClearLine(Term, Term->WritePos / Term->TextWidth);
1120                 
1121                 // Scroll
1122                 Term->ViewPos += Term->TextWidth;
1123                 //Debug("Term->ViewPos = %i", Term->ViewPos);
1124                 VT_int_ScrollFramebuffer( Term );
1125                 VT_int_UpdateScreen( Term, 0 );
1126         }
1127         
1128         //LEAVE('-');
1129 }
1130
1131 /**
1132  * \fn void VT_int_ScrollFramebuffer( tVTerm *Term )
1133  * \note Scrolls the framebuffer by 1 text line
1134  */
1135 void VT_int_ScrollFramebuffer( tVTerm *Term )
1136 {
1137          int    tmp;
1138         struct {
1139                 Uint8   Op;
1140                 Uint16  DstX, DstY;
1141                 Uint16  SrcX, SrcY;
1142                 Uint16  W, H;
1143         } PACKED        buf;
1144         
1145         // Only update if this is the current terminal
1146         if( Term != gpVT_CurTerm )      return;
1147         
1148         // Switch to 2D Command Stream
1149         tmp = VIDEO_BUFFMT_2DSTREAM;
1150         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp);
1151         
1152         // BLIT from 0,0 to 0,giVT_CharHeight
1153         buf.Op = VIDEO_2DOP_BLIT;
1154         buf.DstX = 0;   buf.DstY = 0;
1155         buf.SrcX = 0;   buf.SrcY = giVT_CharHeight;
1156         buf.W = Term->TextWidth * giVT_CharWidth;
1157         buf.H = (Term->TextHeight-1) * giVT_CharHeight;
1158         VFS_WriteAt(giVT_OutputDevHandle, 0, sizeof(buf), &buf);
1159         
1160         // Restore old mode (this function is only called during text mode)
1161         tmp = VIDEO_BUFFMT_TEXT;
1162         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp);
1163 }
1164
1165 /**
1166  * \fn void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
1167  * \brief Updates the video framebuffer
1168  */
1169 void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
1170 {
1171         // Only update if this is the current terminal
1172         if( Term != gpVT_CurTerm )      return;
1173         
1174         switch( Term->Mode )
1175         {
1176         case TERM_MODE_TEXT:
1177                 // Re copy the entire screen?
1178                 if(UpdateAll) {
1179                         VFS_WriteAt(
1180                                 giVT_OutputDevHandle,
1181                                 0,
1182                                 Term->TextWidth*Term->TextHeight*sizeof(tVT_Char),
1183                                 &Term->Text[Term->ViewPos]
1184                                 );
1185                 }
1186                 // Only copy the current line
1187                 else {
1188                          int    pos = Term->WritePos - Term->WritePos % Term->TextWidth;
1189                         VFS_WriteAt(
1190                                 giVT_OutputDevHandle,
1191                                 (pos - Term->ViewPos)*sizeof(tVT_Char),
1192                                 Term->TextWidth*sizeof(tVT_Char),
1193                                 &Term->Text[pos]
1194                                 );
1195                 }
1196                 break;
1197         case TERM_MODE_FB:
1198                 VFS_WriteAt(
1199                         giVT_OutputDevHandle,
1200                         0,
1201                         Term->Width*Term->Height*sizeof(Uint32),
1202                         Term->Buffer
1203                         );
1204                 break;
1205         }
1206 }
1207
1208 /**
1209  * \brief Update the screen mode
1210  * \param Term  Terminal to update
1211  * \param NewMode       New mode to set
1212  * \param NewWidth      New framebuffer width
1213  * \param NewHeight     New framebuffer height
1214  */
1215 void VT_int_ChangeMode(tVTerm *Term, int NewMode, int NewWidth, int NewHeight)
1216 {
1217          int    oldW = Term->Width;
1218          int    oldTW = oldW / giVT_CharWidth;
1219          int    oldH = Term->Height;
1220          int    oldTH = oldH / giVT_CharWidth;
1221         tVT_Char        *oldTBuf = Term->Text;
1222         Uint32  *oldFB = Term->Buffer;
1223          int    w, h, i;
1224         
1225         // TODO: Increase RealWidth/RealHeight when this happens
1226         if(NewWidth > giVT_RealWidth)   NewWidth = giVT_RealWidth;
1227         if(NewHeight > giVT_RealHeight) NewHeight = giVT_RealHeight;
1228         
1229         // Calculate new dimensions
1230         Term->TextWidth = NewWidth / giVT_CharWidth;
1231         Term->TextHeight = NewHeight / giVT_CharHeight;
1232         Term->Width = NewWidth;
1233         Term->Height = NewHeight;
1234         Term->Mode = NewMode;
1235         
1236         // Allocate new buffers
1237         // - Text
1238         Term->Text = calloc(
1239                 Term->TextWidth * Term->TextHeight * (giVT_Scrollback+1),
1240                 sizeof(tVT_Char)
1241                 );
1242         if(oldTBuf) {
1243                 // Copy old buffer
1244                 w = oldTW;
1245                 if( w > Term->TextWidth )       w = Term->TextWidth;
1246                 h = oldTH;
1247                 if( h > Term->TextHeight )      h = Term->TextHeight;
1248                 h *= giVT_Scrollback + 1;
1249                 for( i = 0; i < h; i ++ )
1250                 {
1251                         memcpy(
1252                                 &Term->Text[i*Term->TextWidth],
1253                                 &oldTBuf[i*oldTW],
1254                                 w*sizeof(tVT_Char)
1255                                 );
1256                                 
1257                 }
1258         }
1259         
1260         // - Framebuffer
1261         Term->Buffer = calloc( Term->Width * Term->Height, sizeof(Uint32) );
1262         if(oldFB) {
1263                 // Copy old buffer
1264                 w = oldW;
1265                 if( w > Term->Width )   w = Term->Width;
1266                 h = oldH;
1267                 if( h > Term->Height )  h = Term->Height;
1268                 for( i = 0; i < h; i ++ )
1269                 {
1270                         memcpy(
1271                                 &Term->Buffer[i*Term->Width],
1272                                 &oldFB[i*oldW],
1273                                 w*sizeof(Uint32)
1274                                 );
1275                 }
1276         }
1277         
1278         
1279         // Debug
1280         switch(NewMode)
1281         {
1282         case TERM_MODE_TEXT:
1283                 Log_Log("VTerm", "Set VT %p to text mode (%ix%i)",
1284                         Term, Term->TextWidth, Term->TextHeight);
1285                 break;
1286         case TERM_MODE_FB:
1287                 Log_Log("VTerm", "Set VT %p to framebuffer mode (%ix%i)",
1288                         Term, Term->Width, Term->Height);
1289                 break;
1290         //case TERM_MODE_2DACCEL:
1291         //case TERM_MODE_3DACCEL:
1292         //      return;
1293         }
1294 }
1295
1296 // ---
1297 // Font Render
1298 // ---
1299 #define MONOSPACE_FONT  10816
1300
1301 #if MONOSPACE_FONT == 10808     // 8x8
1302 # include "vterm_font_8x8.h"
1303 #elif MONOSPACE_FONT == 10816   // 8x16
1304 # include "vterm_font_8x16.h"
1305 #endif
1306
1307 // === PROTOTYPES ===
1308 Uint8   *VT_Font_GetChar(Uint32 Codepoint);
1309
1310 // === GLOBALS ===
1311 int     giVT_CharWidth = FONT_WIDTH;
1312 int     giVT_CharHeight = FONT_HEIGHT;
1313
1314 // === CODE ===
1315 /**
1316  * \fn void VT_Font_Render(Uint32 Codepoint, void *Buffer, int Pitch, Uint32 BGC, Uint32 FGC)
1317  * \brief Render a font character
1318  */
1319 void VT_Font_Render(Uint32 Codepoint, void *Buffer, int Pitch, Uint32 BGC, Uint32 FGC)
1320 {
1321         Uint8   *font;
1322         Uint32  *buf = Buffer;
1323          int    x, y;
1324         
1325         font = VT_Font_GetChar(Codepoint);
1326         
1327         for(y = 0; y < FONT_HEIGHT; y ++)
1328         {
1329                 for(x = 0; x < FONT_WIDTH; x ++)
1330                 {
1331                         if(*font & (1 << (FONT_WIDTH-x-1)))
1332                                 buf[x] = FGC;
1333                         else
1334                                 buf[x] = BGC;
1335                 }
1336                 buf += Pitch;
1337                 font ++;
1338         }
1339 }
1340
1341 /**
1342  * \fn Uint32 VT_Colour12to24(Uint16 Col12)
1343  * \brief Converts a 
1344  */
1345 Uint32 VT_Colour12to24(Uint16 Col12)
1346 {
1347         Uint32  ret;
1348          int    tmp;
1349         tmp = Col12 & 0xF;
1350         ret  = (tmp << 0) | (tmp << 4);
1351         tmp = (Col12 & 0xF0) >> 4;
1352         ret |= (tmp << 8) | (tmp << 12);
1353         tmp = (Col12 & 0xF00) >> 8;
1354         ret |= (tmp << 16) | (tmp << 20);
1355         return ret;
1356 }
1357
1358 /**
1359  * \fn Uint8 *VT_Font_GetChar(Uint32 Codepoint)
1360  * \brief Gets an index into the font array given a Unicode Codepoint
1361  * \note See http://en.wikipedia.org/wiki/CP437
1362  */
1363 Uint8 *VT_Font_GetChar(Uint32 Codepoint)
1364 {
1365          int    index = 0;
1366         if(Codepoint < 128)
1367                 return &VTermFont[Codepoint*FONT_HEIGHT];
1368         switch(Codepoint)
1369         {
1370         case 0xC7:      index = 128;    break;  // Ç
1371         case 0xFC:      index = 129;    break;  // ü
1372         case 0xE9:      index = 130;    break;  // é
1373         case 0xE2:      index = 131;    break;  // â
1374         case 0xE4:      index = 132;    break;  // ä
1375         case 0xE0:      index = 133;    break;  // à
1376         case 0xE5:      index = 134;    break;  // å
1377         case 0xE7:      index = 135;    break;  // ç
1378         case 0xEA:      index = 136;    break;  // ê
1379         case 0xEB:      index = 137;    break;  // ë
1380         case 0xE8:      index = 138;    break;  // è
1381         case 0xEF:      index = 139;    break;  // ï
1382         case 0xEE:      index = 140;    break;  // î
1383         case 0xEC:      index = 141;    break;  // ì
1384         case 0xC4:      index = 142;    break;  // Ä
1385         case 0xC5:      index = 143;    break;  // Å
1386         }
1387         
1388         return &VTermFont[index*FONT_HEIGHT];
1389 }
1390
1391 EXPORTAS(&giVT_CharWidth, giVT_CharWidth);
1392 EXPORTAS(&giVT_CharHeight, giVT_CharHeight);
1393 EXPORT(VT_Font_Render);
1394 EXPORT(VT_Colour12to24);

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