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

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