Kernel - Integrated PTY with VTerm, userland currently broken
[tpg/acess2.git] / KernelLand / Kernel / drv / vterm.c
1 /*
2  * Acess2 Kernel
3  * - By John Hodge (thePowersGang)
4  *
5  * drv/vterm.c
6  * - Virtual Terminal - Initialisation and VFS Interface
7  */
8 #define DEBUG   0
9 #include "vterm.h"
10 #include <fs_devfs.h>
11 #include <modules.h>
12 #include <api_drv_keyboard.h>
13 #include <api_drv_video.h>
14 #include <errno.h>
15 #include <semaphore.h>
16
17 // === CONSTANTS ===
18 #define VERSION ((0<<8)|(50))
19
20 #define NUM_VTS 8
21 //#define DEFAULT_OUTPUT        "BochsGA"
22 #define DEFAULT_OUTPUT  "Vesa"
23 #define FALLBACK_OUTPUT "x86_VGAText"
24 #define DEFAULT_INPUT   "Keyboard"
25 #define DEFAULT_WIDTH   640
26 #define DEFAULT_HEIGHT  480
27 #define DEFAULT_SCROLLBACK      4       // 2 Screens of text + current screen
28 //#define DEFAULT_SCROLLBACK    0
29
30 // === TYPES ===
31
32 // === IMPORTS ===
33 extern void     Debug_SetKTerminal(const char *File);
34
35 // === PROTOTYPES ===
36  int    VT_Install(char **Arguments);
37  int    VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data);
38 void    VT_int_PutFBData(tVTerm *Term, size_t Offset, size_t Length, const void *Data);
39 void    VT_PTYOutput(void *Handle, size_t Length, const void *Data);
40  int    VT_PTYResize(void *Handle, const struct ptydims *Dims); 
41  int    VT_PTYModeset(void *Handle, const struct ptymode *Mode);
42
43 // === CONSTANTS ===
44
45 // === GLOBALS ===
46 MODULE_DEFINE(0, VERSION, VTerm, VT_Install, NULL, "PTY", NULL);
47 tVFS_NodeType   gVT_RootNodeType = {
48         .TypeName = "VTerm Root",
49         .IOCtl = VT_Root_IOCtl
50         };
51 tDevFS_Driver   gVT_DrvInfo = {
52         NULL, "VTerm",
53         {
54         .Flags = 0,
55         .Size = NUM_VTS,
56         .Inode = -1,
57         .NumACLs = 0,
58         .Type = &gVT_RootNodeType
59         }
60 };
61 // --- Terminals ---
62 tVTerm  gVT_Terminals[NUM_VTS];
63  int    giVT_CurrentTerminal = 0;
64 tVTerm  *gpVT_CurTerm = &gVT_Terminals[0];
65 // --- Video State ---
66 short   giVT_RealWidth  = DEFAULT_WIDTH;        //!< Screen Width
67 short   giVT_RealHeight = DEFAULT_HEIGHT;       //!< Screen Height
68  int    giVT_Scrollback = DEFAULT_SCROLLBACK;
69 // --- Driver Handles ---
70 char    *gsVT_OutputDevice = NULL;
71 char    *gsVT_InputDevice = NULL;
72  int    giVT_OutputDevHandle = -2;
73  int    giVT_InputDevHandle = -2;
74
75 // === CODE ===
76 /**
77  * \fn int VT_Install(char **Arguments)
78  * \brief Installs the Virtual Terminal Driver
79  */
80 int VT_Install(char **Arguments)
81 {
82          int    i;
83         
84         // Scan Arguments
85         if(Arguments)
86         {
87                 char    **args;
88                 const char      *arg;
89                 for(args = Arguments; (arg = *args); args++ )
90                 {
91                         char    data[strlen(arg)+1];
92                         char    *opt = data;
93                         char    *val;
94                         
95                         val = strchr(arg, '=');
96                         strcpy(data, arg);
97                         if( val ) {
98                                 data[ val - arg ] = '\0';
99                                 val ++;
100                         }
101                         Log_Debug("VTerm", "Argument '%s'", arg);
102                         
103                         if( strcmp(opt, "Video") == 0 ) {
104                                 if( !gsVT_OutputDevice )
105                                         gsVT_OutputDevice = val;
106                         }
107                         else if( strcmp(opt, "Input") == 0 ) {
108                                 if( !gsVT_InputDevice )
109                                         gsVT_InputDevice = val;
110                         }
111                         else if( strcmp(opt, "Width") == 0 ) {
112                                 giVT_RealWidth = atoi( val );
113                         }
114                         else if( strcmp(opt, "Height") == 0 ) {
115                                 giVT_RealHeight = atoi( val );
116                         }
117                         else if( strcmp(opt, "Scrollback") == 0 ) {
118                                 giVT_Scrollback = atoi( val );
119                         }
120                         else {
121                                 Log_Notice("VTerm", "Unknown option '%s'", opt);
122                         }
123                 }
124         }
125         
126         // Apply Defaults
127         if(!gsVT_OutputDevice)  gsVT_OutputDevice = (char*)DEFAULT_OUTPUT;
128         else if( Module_EnsureLoaded( gsVT_OutputDevice ) )     gsVT_OutputDevice = (char*)DEFAULT_OUTPUT;
129         if( Module_EnsureLoaded( gsVT_OutputDevice ) )  gsVT_OutputDevice = (char*)FALLBACK_OUTPUT;
130         if( Module_EnsureLoaded( gsVT_OutputDevice ) ) {
131                 Log_Error("VTerm", "Fallback video '%s' is not avaliable, giving up", FALLBACK_OUTPUT);
132                 return MODULE_ERR_MISC;
133         }
134         
135         if(!gsVT_InputDevice)   gsVT_InputDevice = (char*)DEFAULT_INPUT;
136         else if( Module_EnsureLoaded( gsVT_InputDevice ) )      gsVT_InputDevice = (char*)DEFAULT_INPUT;
137         if( Module_EnsureLoaded( gsVT_InputDevice ) ) {
138                 Log_Error("VTerm", "Fallback input '%s' is not avaliable, input will not be avaliable", DEFAULT_INPUT);
139         }
140         
141         // Create device paths
142         {
143                 char    *tmp;
144                 tmp = malloc( 9 + strlen(gsVT_OutputDevice) + 1 );
145                 strcpy(tmp, "/Devices/");
146                 strcpy(&tmp[9], gsVT_OutputDevice);
147                 gsVT_OutputDevice = tmp;
148
149                 tmp = malloc( 9 + strlen(gsVT_InputDevice) + 1 );
150                 strcpy(tmp, "/Devices/");
151                 strcpy(&tmp[9], gsVT_InputDevice);
152                 gsVT_InputDevice = tmp;
153         }
154         
155         Log_Log("VTerm", "Using '%s' as output", gsVT_OutputDevice);
156         Log_Log("VTerm", "Using '%s' as input", gsVT_InputDevice);
157         
158         VT_InitOutput();
159         VT_InitInput();
160         
161         
162         // Create Nodes
163         Log_Debug("VTerm", "Initialising nodes (and creating buffers)");
164         for( i = 0; i < NUM_VTS; i++ )
165         {
166                 gVT_Terminals[i].Mode = TERM_MODE_TEXT;
167                 gVT_Terminals[i].Flags = 0;
168 //              gVT_Terminals[i].Flags = VT_FLAG_HIDECSR;       //HACK - Stop all those memcpy calls
169                 gVT_Terminals[i].CurColour = DEFAULT_COLOUR;
170                 gVT_Terminals[i].WritePos = 0;
171                 gVT_Terminals[i].AltWritePos = 0;
172                 gVT_Terminals[i].ViewPos = 0;
173                 gVT_Terminals[i].ScrollHeight = 0;
174                 
175                 // Initialise
176                 VT_int_Resize( &gVT_Terminals[i], giVT_RealWidth, giVT_RealHeight );
177                 gVT_Terminals[i].Mode = PTYBUFFMT_TEXT;
178                 char    name[] = {'v','t','0'+i,'\0'};
179                 gVT_Terminals[i].PTY = PTY_Create(name, &gVT_Terminals[i],
180                         VT_PTYOutput, VT_PTYResize, VT_PTYModeset);
181                 struct ptymode mode = {
182                         .OutputMode = PTYBUFFMT_TEXT,
183                         .InputMode = PTYIMODE_CANON|PTYIMODE_ECHO
184                 };
185                 PTY_SetAttrib(gVT_Terminals[i].PTY, NULL, &mode, 0);
186         }
187         
188         // Add to DevFS
189         DevFS_AddDevice( &gVT_DrvInfo );
190         
191         // Set kernel output to VT0
192         Log_Debug("VTerm", "Setting kernel output to VT#0");
193         Debug_SetKTerminal("/Devices/pts/vt0c");
194         
195         return MODULE_ERR_OK;
196 }
197
198 /**
199  * \brief Set the video resolution
200  * \param Width New screen width
201  * \param Height        New screen height
202  */
203 void VT_SetResolution(int Width, int Height)
204 {
205         tVideo_IOCtl_Mode       mode = {0};
206          int    tmp;
207          int    i;
208         
209         // Create the video mode
210         mode.width = Width;
211         mode.height = Height;
212         mode.bpp = 32;
213         mode.flags = 0;
214         
215         // Set video mode
216         VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_FINDMODE, &mode );
217         tmp = mode.id;
218         if( Width != mode.width || Height != mode.height )
219         {
220                 Log_Warning("VTerm",
221                         "Selected resolution (%ix%i) is not supported by the device, using (%ix%i)",
222                         giVT_RealWidth, giVT_RealHeight,
223                         mode.width, mode.height
224                         );
225                 giVT_RealWidth = mode.width;
226                 giVT_RealHeight = mode.height;
227         }
228         VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_GETSETMODE, &tmp );
229         
230         // Resize text terminals if needed
231         // - VT0 check is for the first resolution set
232         if( gVT_Terminals[0].Text && (giVT_RealWidth != mode.width || giVT_RealHeight != mode.height) )
233         {
234                  int    newBufSize = (giVT_RealWidth/giVT_CharWidth)
235                                         *(giVT_RealHeight/giVT_CharHeight)
236                                         *(giVT_Scrollback+1);
237                 //tVT_Char      *tmp;
238                 // Resize the text terminals
239                 Log_Debug("VTerm", "Resizing terminals to %ix%i",
240                         giVT_RealWidth/giVT_CharWidth, giVT_RealHeight/giVT_CharHeight);
241                 for( i = 0; i < NUM_VTS; i ++ )
242                 {
243                         if( gVT_Terminals[i].Mode != TERM_MODE_TEXT )   continue;
244                         
245                         gVT_Terminals[i].TextWidth = giVT_RealWidth/giVT_CharWidth;
246                         gVT_Terminals[i].TextHeight = giVT_RealHeight/giVT_CharHeight;
247                         gVT_Terminals[i].ScrollHeight = gVT_Terminals[i].TextHeight;
248                         
249                         gVT_Terminals[i].Text = realloc(
250                                 gVT_Terminals[i].Text,
251                                 newBufSize*sizeof(tVT_Char)
252                                 );
253                 }
254         }
255 }
256
257 /**
258  * \fn int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
259  * \brief Control the VTerm Driver
260  */
261 int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
262 {
263          int    len;
264         switch(Id)
265         {
266         case DRV_IOCTL_TYPE:    return DRV_TYPE_MISC;
267         case DRV_IOCTL_IDENT:   memcpy(Data, "VT\0\0", 4);      return 0;
268         case DRV_IOCTL_VERSION: return VERSION;
269         case DRV_IOCTL_LOOKUP:  return 0;
270         
271         case 4: // Get Video Driver
272                 if(Data)        strcpy(Data, gsVT_OutputDevice);
273                 return strlen(gsVT_OutputDevice);
274         
275         case 5: // Set Video Driver
276                 if(!Data)       return -EINVAL;
277                 if(Threads_GetUID() != 0)       return -EACCES;
278                 
279                 len = strlen(Data);
280                 
281                 // TODO: Check if the string used is a heap string
282                 
283                 free(gsVT_OutputDevice);
284                 
285                 gsVT_OutputDevice = malloc(len+1);
286                 strcpy(gsVT_OutputDevice, Data);
287                 
288                 VFS_Close(giVT_OutputDevHandle);
289                 giVT_OutputDevHandle = -1;
290                 
291                 VT_InitOutput();
292                 return 1;
293         }
294         return 0;
295 }
296
297 void VT_int_PutFBData(tVTerm *Term, size_t Offset, size_t Length, const void *Buffer)
298 {
299         size_t  maxlen = Term->Width * Term->Height * 4;
300
301         if( Offset >= maxlen )
302                 return ;
303
304         Length = MIN(Length, maxlen - Offset);
305         
306         // If the terminal is currently shown, write directly to the screen
307         if( Term == gpVT_CurTerm )
308         {
309                 // Center the terminal vertically
310                 if( giVT_RealHeight > Term->Height )
311                         Offset += (giVT_RealHeight - Term->Height) / 2 * Term->Width * 4;
312                 
313                 // If the terminal is not native width, center it horizontally
314                 if( giVT_RealWidth > Term->Width )
315                 {
316                         // No? :( Well, just center it
317                          int    x, y, w, h;
318                         Uint    dst_ofs;
319                         // TODO: Fix to handle the final line correctly?
320                         x = Offset/4;   y = x / Term->Width;    x %= Term->Width;
321                         w = Length/4+x; h = w / Term->Width;    w %= Term->Width;
322                         
323                         // Center
324                         x += (giVT_RealWidth - Term->Width) / 2;
325                         dst_ofs = (x + y * giVT_RealWidth) * 4;
326                         while(h--)
327                         {
328                                 VFS_WriteAt( giVT_OutputDevHandle,
329                                         dst_ofs,
330                                         Term->Width * 4,
331                                         Buffer
332                                         );
333                                 Buffer = (const Uint32*)Buffer + Term->Width;
334                                 dst_ofs += giVT_RealWidth * 4;
335                         }
336                 }
337                 // otherwise, just go directly to the screen
338                 else
339                 {
340                         VFS_WriteAt( giVT_OutputDevHandle, Offset, Length, Buffer );
341                 }
342         }
343         // If not active, write to the backbuffer (allocating if needed)
344         else
345         {
346                 if( !Term->Buffer )
347                         Term->Buffer = malloc( Term->Width * Term->Height * 4 );
348                 // Copy to the local cache
349                 memcpy( (char*)Term->Buffer + Offset, Buffer, Length );
350         }
351 }
352
353 void VT_PTYOutput(void *Handle, size_t Length, const void *Data)
354 {
355         tVTerm  *term = Handle;
356         switch( term->Mode )
357         {
358         case PTYBUFFMT_TEXT:
359                 VT_int_PutString(term, Data, Length);
360                 break;
361         case PTYBUFFMT_FB:
362                 // TODO: How do offset?
363                 VT_int_PutFBData(term, 0, Length, Data);
364                 break;
365         case PTYBUFFMT_2DCMD:
366                 // TODO: Impliment 2D commands
367                 break;
368         case PTYBUFFMT_3DCMD:
369                 // TODO: Impliment 3D commands
370                 break;
371         }
372 }
373
374 int VT_PTYResize(void *Handle, const struct ptydims *Dims)
375 {
376         tVTerm  *term = Handle;
377          int    newW = Dims->W * (term->Mode == PTYBUFFMT_TEXT ? giVT_CharWidth : 1);
378          int    newH = Dims->H * (term->Mode == PTYBUFFMT_TEXT ? giVT_CharHeight : 1);
379         if( newW > giVT_RealWidth || newH > giVT_RealHeight )
380                 return 1;
381         VT_int_Resize(term, newW, newH);
382         return 0;
383 }
384
385 int VT_PTYModeset(void *Handle, const struct ptymode *Mode)
386 {
387         tVTerm  *term = Handle;
388         term->Mode = (Mode->OutputMode & PTYOMODE_BUFFMT);
389         return 0;
390 }
391
392 #if 0
393 /**
394  * \fn int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
395  * \brief Call an IO Control on a virtual terminal
396  */
397 int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
398 {
399          int    *iData = Data;
400          int    ret;
401         tVTerm  *term = Node->ImplPtr;
402         ENTER("pNode iId pData", Node, Id, Data);
403         
404         if(Id >= DRV_IOCTL_LOOKUP) {
405                 // Only root can fiddle with graphics modes
406                 // TODO: Remove this and replace with user ownership
407                 if( Threads_GetUID() != 0 )     return -1;
408         }
409         
410         switch(Id)
411         {
412         // --- Core Defined
413         case DRV_IOCTL_TYPE:
414                 LEAVE('i', DRV_TYPE_TERMINAL);
415                 return DRV_TYPE_TERMINAL;
416         case DRV_IOCTL_IDENT:
417                 memcpy(Data, "VT\0\0", 4);
418                 LEAVE('i', 0);
419                 return 0;
420         case DRV_IOCTL_VERSION:
421                 LEAVE('x', VERSION);
422                 return VERSION;
423         case DRV_IOCTL_LOOKUP:
424                 LEAVE('i', 0);
425                 return 0;
426         
427         // Get/Set the mode (and apply any changes)
428         case TERM_IOCTL_MODETYPE:
429                 if(Data != NULL)
430                 {
431                         if( CheckMem(Data, sizeof(int)) == 0 ) {
432                                 LEAVE('i', -1);
433                                 return -1;
434                         }
435                         Log_Log("VTerm", "VTerm %i mode set to %i", (int)Node->Inode, *iData);
436                         
437                         // Update mode if needed
438                         if( term->Mode != *iData || term->NewWidth || term->NewHeight)
439                         {
440                                 // Adjust for text mode
441                                 if( *iData == TERM_MODE_TEXT ) {
442                                         term->NewHeight *= giVT_CharHeight;
443                                         term->NewWidth *= giVT_CharWidth;
444                                 }
445                                 // Fill unchanged dimensions
446                                 if(term->NewHeight == 0)        term->NewHeight = term->Height;
447                                 if(term->NewWidth == 0) term->NewWidth = term->Width;
448                                 // Set new mode
449                                 VT_int_ChangeMode(term, *iData, term->NewWidth, term->NewHeight);
450                                 // Clear unapplied dimensions
451                                 term->NewWidth = 0;
452                                 term->NewHeight = 0;
453                         }
454                         
455                         // Update the screen dimensions
456                         if(Node->Inode == giVT_CurrentTerminal)
457                                 VT_SetTerminal( giVT_CurrentTerminal );
458                 }
459                 LEAVE('i', term->Mode);
460                 return term->Mode;
461         
462         // Get/set the terminal width
463         case TERM_IOCTL_WIDTH:
464                 if(Data != NULL) {
465                         if( CheckMem(Data, sizeof(int)) == 0 ) {
466                                 LEAVE('i', -1);
467                                 return -1;
468                         }
469                         term->NewWidth = *iData;
470                 }
471                 if( term->NewWidth )
472                         ret = term->NewWidth;
473                 else if( term->Mode == TERM_MODE_TEXT )
474                         ret = term->TextWidth;
475                 else
476                         ret = term->Width;
477                 LEAVE('i', ret);
478                 return ret;
479         
480         // Get/set the terminal height
481         case TERM_IOCTL_HEIGHT:
482                 if(Data != NULL) {
483                         if( CheckMem(Data, sizeof(int)) == 0 ) {
484                                 LEAVE('i', -1);
485                                 return -1;
486                         }
487                         term->NewHeight = *iData;
488                 }
489                 if( term->NewHeight )
490                         ret = term->NewHeight;
491                 else if( term->Mode == TERM_MODE_TEXT )
492                         ret = term->TextHeight;
493                 else
494                         ret = term->Height;
495                 LEAVE('i', ret);
496                 return ret;
497         
498         case TERM_IOCTL_FORCESHOW:
499                 Log_Log("VTerm", "Thread %i forced VTerm %i to be shown",
500                         Threads_GetTID(), (int)Node->Inode);
501                 VT_SetTerminal( Node->Inode );
502                 LEAVE('i', 1);
503                 return 1;
504         
505         case TERM_IOCTL_GETSETCURSOR:
506                 if(Data != NULL)
507                 {
508                         tVideo_IOCtl_Pos        *pos = Data;
509                         if( !CheckMem(Data, sizeof(*pos)) ) {
510                                 errno = -EINVAL;
511                                 LEAVE('i', -1);
512                                 return -1;
513                         }
514                 
515                         if( term->Mode == TERM_MODE_TEXT )
516                         {
517                                 if(term->Flags & VT_FLAG_ALTBUF)
518                                         term->AltWritePos = pos->x + pos->y * term->TextWidth;
519                                 else
520                                         term->WritePos = pos->x + pos->y * term->TextWidth + term->ViewPos;
521                                 VT_int_UpdateCursor(term, 0);
522                         }
523                         else
524                         {
525                                 term->VideoCursorX = pos->x;
526                                 term->VideoCursorY = pos->y;
527                                 VT_int_UpdateCursor(term, 1);
528                         }
529                 }
530                 ret = (term->Flags & VT_FLAG_ALTBUF) ? term->AltWritePos : term->WritePos-term->ViewPos;
531                 LEAVE('i', ret);
532                 return ret;
533
534         case TERM_IOCTL_SETCURSORBITMAP: {
535                 tVideo_IOCtl_Bitmap     *bmp = Data;
536                 if( Data == NULL )
537                 {
538                         free( term->VideoCursor );
539                         term->VideoCursor = NULL;
540                         LEAVE('i', 0);
541                         return 0;
542                 }
543
544                 // Sanity check bitmap
545                 if( !CheckMem(bmp, sizeof(tVideo_IOCtl_Bitmap)) ) {
546                         Log_Notice("VTerm", "%p in TERM_IOCTL_SETCURSORBITMAP invalid", bmp);
547                         errno = -EINVAL;
548                         LEAVE_RET('i', -1);
549                 }
550                 if( !CheckMem(bmp->Data, bmp->W*bmp->H*sizeof(Uint32)) ) {
551                         Log_Notice("VTerm", "%p in TERM_IOCTL_SETCURSORBITMAP invalid", bmp);
552                         errno = -EINVAL;
553                         LEAVE_RET('i', -1);
554                 }
555
556                 // Reallocate if needed
557                 if(term->VideoCursor)
558                 {
559                         if(bmp->W * bmp->H != term->VideoCursor->W * term->VideoCursor->H) {
560                                 free(term->VideoCursor);
561                                 term->VideoCursor = NULL;
562                         }
563                 }
564                 if(!term->VideoCursor) {
565                         term->VideoCursor = malloc(sizeof(tVideo_IOCtl_Pos) + bmp->W*bmp->H*sizeof(Uint32));
566                         if(!term->VideoCursor) {
567                                 Log_Error("VTerm", "Unable to allocate memory for cursor");
568                                 errno = -ENOMEM;
569                                 LEAVE_RET('i', -1);
570                         }
571                 }
572                 
573                 memcpy(term->VideoCursor, bmp, sizeof(tVideo_IOCtl_Pos) + bmp->W*bmp->H*sizeof(Uint32));
574         
575                 Log_Debug("VTerm", "Set VT%i's cursor to %p %ix%i",
576                         (int)term->Node.Inode, bmp, bmp->W, bmp->H);
577
578                 if(gpVT_CurTerm == term)
579                         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSORBITMAP, term->VideoCursor);
580         
581                 LEAVE('i', 0);
582                 return 0; }
583         }
584         LEAVE('i', -1);
585         return -1;
586 }
587
588 void VT_Terminal_Reference(tVFS_Node *Node)
589 {
590         // Append PID to list
591 }
592
593 void VT_Terminal_Close(tVFS_Node *Node)
594 {
595         // Remove PID from list
596 }
597 #endif
598
599 /**
600  * \fn void VT_SetTerminal(int ID)
601  * \brief Set the current terminal
602  */
603 void VT_SetTerminal(int ID)
604 {
605         // Copy the screen state
606         if( ID != giVT_CurrentTerminal && gpVT_CurTerm->Mode != TERM_MODE_TEXT )
607         {
608                 if( !gpVT_CurTerm->Buffer )
609                         gpVT_CurTerm->Buffer = malloc( gpVT_CurTerm->Width*gpVT_CurTerm->Height*4 );
610                 if( gpVT_CurTerm->Width < giVT_RealWidth )
611                 {
612                         Uint    ofs = 0;
613                         Uint32  *dest = gpVT_CurTerm->Buffer;
614                         // Slower scanline copy
615                         for( int line = 0; line < gpVT_CurTerm->Height; line ++ )
616                         {
617                                 VFS_ReadAt(giVT_OutputDevHandle, ofs, gpVT_CurTerm->Width*4, dest);
618                                 ofs += giVT_RealWidth * 4;
619                                 dest += gpVT_CurTerm->Width;
620                         }
621                 }
622                 else
623                 {
624                         VFS_ReadAt(giVT_OutputDevHandle,
625                                 0, gpVT_CurTerm->Height*giVT_RealWidth*4,
626                                 gpVT_CurTerm->Buffer
627                                 );
628                 }
629                 LOG("Cached screen contents");
630         }
631
632         // Update current terminal ID
633         Log_Log("VTerm", "Changed terminal from %i to %i", giVT_CurrentTerminal, ID);
634         giVT_CurrentTerminal = ID;
635         gpVT_CurTerm = &gVT_Terminals[ID];
636         
637         LOG("Attempting VT_SetMode");
638         
639         if( gpVT_CurTerm->Mode == TERM_MODE_TEXT )
640         {
641                 VT_SetMode( VIDEO_BUFFMT_TEXT );
642         }
643         else
644         {
645                 // Update the cursor image
646                 if(gpVT_CurTerm->VideoCursor)
647                         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSORBITMAP, gpVT_CurTerm->VideoCursor);
648                 VT_SetMode( VIDEO_BUFFMT_FRAMEBUFFER );
649         }
650
651         LOG("Mode set");        
652
653         if(gpVT_CurTerm->Buffer)
654         {
655                 // TODO: Handle non equal sized
656                 VFS_WriteAt(
657                         giVT_OutputDevHandle,
658                         0,
659                         gpVT_CurTerm->Width*gpVT_CurTerm->Height*sizeof(Uint32),
660                         gpVT_CurTerm->Buffer
661                         );
662                 LOG("Updated screen contents");
663         }
664         
665         VT_int_UpdateCursor(gpVT_CurTerm, 1);
666         // Update the screen
667         VT_int_UpdateScreen(gpVT_CurTerm, 1);
668         LOG("done");
669 }

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