Kernel - Commenting changes only
[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_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX]);
38 tVFS_Node       *VT_FindDir(tVFS_Node *Node, const char *Name);
39  int    VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data);
40 size_t  VT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
41 size_t  VT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer);
42  int    VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data);
43 void    VT_Terminal_Reference(tVFS_Node *Node);
44 void    VT_Terminal_Close(tVFS_Node *Node);
45 //void  VT_SetTerminal(int Term);
46
47 // === CONSTANTS ===
48
49 // === GLOBALS ===
50 MODULE_DEFINE(0, VERSION, VTerm, VT_Install, NULL, NULL);
51 tVFS_NodeType   gVT_RootNodeType = {
52         .TypeName = "VTerm Root",
53         .ReadDir = VT_ReadDir,
54         .FindDir = VT_FindDir,
55         .IOCtl = VT_Root_IOCtl
56         };
57 tVFS_NodeType   gVT_TermNodeType = {
58         .TypeName = "VTerm",
59         .Read = VT_Read,
60         .Write = VT_Write,
61         .IOCtl = VT_Terminal_IOCtl
62         };
63 tDevFS_Driver   gVT_DrvInfo = {
64         NULL, "VTerm",
65         {
66         .Flags = VFS_FFLAG_DIRECTORY,
67         .Size = NUM_VTS,
68         .Inode = -1,
69         .NumACLs = 0,
70         .Type = &gVT_RootNodeType
71         }
72 };
73 // --- Terminals ---
74 tVTerm  gVT_Terminals[NUM_VTS];
75  int    giVT_CurrentTerminal = 0;
76 tVTerm  *gpVT_CurTerm = &gVT_Terminals[0];
77 // --- Video State ---
78 short   giVT_RealWidth  = DEFAULT_WIDTH;        //!< Screen Width
79 short   giVT_RealHeight = DEFAULT_HEIGHT;       //!< Screen Height
80  int    giVT_Scrollback = DEFAULT_SCROLLBACK;
81 // --- Driver Handles ---
82 char    *gsVT_OutputDevice = NULL;
83 char    *gsVT_InputDevice = NULL;
84  int    giVT_OutputDevHandle = -2;
85  int    giVT_InputDevHandle = -2;
86
87 // === CODE ===
88 /**
89  * \fn int VT_Install(char **Arguments)
90  * \brief Installs the Virtual Terminal Driver
91  */
92 int VT_Install(char **Arguments)
93 {
94          int    i;
95         
96         // Scan Arguments
97         if(Arguments)
98         {
99                 char    **args;
100                 const char      *arg;
101                 for(args = Arguments; (arg = *args); args++ )
102                 {
103                         char    data[strlen(arg)+1];
104                         char    *opt = data;
105                         char    *val;
106                         
107                         val = strchr(arg, '=');
108                         strcpy(data, arg);
109                         if( val ) {
110                                 data[ val - arg ] = '\0';
111                                 val ++;
112                         }
113                         Log_Debug("VTerm", "Argument '%s'", arg);
114                         
115                         if( strcmp(opt, "Video") == 0 ) {
116                                 if( !gsVT_OutputDevice )
117                                         gsVT_OutputDevice = val;
118                         }
119                         else if( strcmp(opt, "Input") == 0 ) {
120                                 if( !gsVT_InputDevice )
121                                         gsVT_InputDevice = val;
122                         }
123                         else if( strcmp(opt, "Width") == 0 ) {
124                                 giVT_RealWidth = atoi( val );
125                         }
126                         else if( strcmp(opt, "Height") == 0 ) {
127                                 giVT_RealHeight = atoi( val );
128                         }
129                         else if( strcmp(opt, "Scrollback") == 0 ) {
130                                 giVT_Scrollback = atoi( val );
131                         }
132                 }
133         }
134         
135         // Apply Defaults
136         if(!gsVT_OutputDevice)  gsVT_OutputDevice = (char*)DEFAULT_OUTPUT;
137         else if( Module_EnsureLoaded( gsVT_OutputDevice ) )     gsVT_OutputDevice = (char*)DEFAULT_OUTPUT;
138         if( Module_EnsureLoaded( gsVT_OutputDevice ) )  gsVT_OutputDevice = (char*)FALLBACK_OUTPUT;
139         if( Module_EnsureLoaded( gsVT_OutputDevice ) ) {
140                 Log_Error("VTerm", "Fallback video '%s' is not avaliable, giving up", FALLBACK_OUTPUT);
141                 return MODULE_ERR_MISC;
142         }
143         
144         if(!gsVT_InputDevice)   gsVT_InputDevice = (char*)DEFAULT_INPUT;
145         else if( Module_EnsureLoaded( gsVT_InputDevice ) )      gsVT_InputDevice = (char*)DEFAULT_INPUT;
146         if( Module_EnsureLoaded( gsVT_InputDevice ) ) {
147                 Log_Error("VTerm", "Fallback input '%s' is not avaliable, input will not be avaliable", DEFAULT_INPUT);
148         }
149         
150         // Create device paths
151         {
152                 char    *tmp;
153                 tmp = malloc( 9 + strlen(gsVT_OutputDevice) + 1 );
154                 strcpy(tmp, "/Devices/");
155                 strcpy(&tmp[9], gsVT_OutputDevice);
156                 gsVT_OutputDevice = tmp;
157
158                 tmp = malloc( 9 + strlen(gsVT_InputDevice) + 1 );
159                 strcpy(tmp, "/Devices/");
160                 strcpy(&tmp[9], gsVT_InputDevice);
161                 gsVT_InputDevice = tmp;
162         }
163         
164         Log_Log("VTerm", "Using '%s' as output", gsVT_OutputDevice);
165         Log_Log("VTerm", "Using '%s' as input", gsVT_InputDevice);
166         
167         VT_InitOutput();
168         VT_InitInput();
169         
170         
171         // Create Nodes
172         Log_Debug("VTerm", "Initialising nodes (and creating buffers)");
173         for( i = 0; i < NUM_VTS; i++ )
174         {
175                 gVT_Terminals[i].Mode = TERM_MODE_TEXT;
176                 gVT_Terminals[i].Flags = 0;
177 //              gVT_Terminals[i].Flags = VT_FLAG_HIDECSR;       //HACK - Stop all those memcpy calls
178                 gVT_Terminals[i].CurColour = DEFAULT_COLOUR;
179                 gVT_Terminals[i].WritePos = 0;
180                 gVT_Terminals[i].AltWritePos = 0;
181                 gVT_Terminals[i].ViewPos = 0;
182                 gVT_Terminals[i].ReadingThread = -1;
183                 gVT_Terminals[i].ScrollHeight = 0;
184                 
185                 // Initialise
186                 VT_int_ChangeMode( &gVT_Terminals[i],
187                         TERM_MODE_TEXT, giVT_RealWidth, giVT_RealHeight );
188                 
189                 gVT_Terminals[i].Name[0] = '0'+i;
190                 gVT_Terminals[i].Name[1] = '\0';
191                 gVT_Terminals[i].Node.Inode = i;
192                 gVT_Terminals[i].Node.ImplPtr = &gVT_Terminals[i];
193                 gVT_Terminals[i].Node.NumACLs = 0;      // Only root can open virtual terminals
194         
195                 gVT_Terminals[i].Node.Type = &gVT_TermNodeType; 
196 //              Semaphore_Init(&gVT_Terminals[i].InputSemaphore, 0, MAX_INPUT_CHARS8, "VTerm", gVT_Terminals[i].Name);
197         }
198         
199         // Add to DevFS
200         DevFS_AddDevice( &gVT_DrvInfo );
201         
202         // Set kernel output to VT0
203         Log_Debug("VTerm", "Setting kernel output to VT#0");
204         Debug_SetKTerminal("/Devices/VTerm/0");
205         
206         return MODULE_ERR_OK;
207 }
208
209 /**
210  * \brief Set the video resolution
211  * \param Width New screen width
212  * \param Height        New screen height
213  */
214 void VT_SetResolution(int Width, int Height)
215 {
216         tVideo_IOCtl_Mode       mode = {0};
217          int    tmp;
218          int    i;
219         
220         // Create the video mode
221         mode.width = Width;
222         mode.height = Height;
223         mode.bpp = 32;
224         mode.flags = 0;
225         
226         // Set video mode
227         VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_FINDMODE, &mode );
228         tmp = mode.id;
229         if( Width != mode.width || Height != mode.height )
230         {
231                 Log_Warning("VTerm",
232                         "Selected resolution (%ix%i) is not supported by the device, using (%ix%i)",
233                         giVT_RealWidth, giVT_RealHeight,
234                         mode.width, mode.height
235                         );
236                 giVT_RealWidth = mode.width;
237                 giVT_RealHeight = mode.height;
238         }
239         VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_GETSETMODE, &tmp );
240         
241         // Resize text terminals if needed
242         if( gVT_Terminals[0].Text && (giVT_RealWidth != mode.width || giVT_RealHeight != mode.height) )
243         {
244                  int    newBufSize = (giVT_RealWidth/giVT_CharWidth)
245                                         *(giVT_RealHeight/giVT_CharHeight)
246                                         *(giVT_Scrollback+1);
247                 //tVT_Char      *tmp;
248                 // Resize the text terminals
249                 Log_Debug("VTerm", "Resizing terminals to %ix%i",
250                         giVT_RealWidth/giVT_CharWidth, giVT_RealHeight/giVT_CharHeight);
251                 for( i = 0; i < NUM_VTS; i ++ )
252                 {
253                         if( gVT_Terminals[i].Mode != TERM_MODE_TEXT )   continue;
254                         
255                         gVT_Terminals[i].TextWidth = giVT_RealWidth/giVT_CharWidth;
256                         gVT_Terminals[i].TextHeight = giVT_RealHeight/giVT_CharHeight;
257                         gVT_Terminals[i].ScrollHeight = gVT_Terminals[i].TextHeight;
258                         
259                         gVT_Terminals[i].Text = realloc(
260                                 gVT_Terminals[i].Text,
261                                 newBufSize*sizeof(tVT_Char)
262                                 );
263                 }
264         }
265 }
266
267 /**
268  * \fn char *VT_ReadDir(tVFS_Node *Node, int Pos)
269  * \brief Read from the VTerm Directory
270  */
271 int VT_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
272 {
273         if(Pos < 0)     return -EINVAL;
274         if(Pos >= NUM_VTS)      return -EINVAL;
275         strncpy(Dest, gVT_Terminals[Pos].Name, FILENAME_MAX);
276         return 0;
277 }
278
279 /**
280  * \fn tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
281  * \brief Find an item in the VTerm directory
282  * \param Node  Root node
283  * \param Name  Name (number) of the terminal
284  */
285 tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
286 {
287          int    num;
288         
289         ENTER("pNode sName", Node, Name);
290         
291         // Open the input and output files if needed
292         if(giVT_OutputDevHandle == -2)  VT_InitOutput();
293         if(giVT_InputDevHandle == -2)   VT_InitInput();
294         
295         // Sanity check name
296         if(Name[0] < '0' || Name[0] > '9' || Name[1] != '\0') {
297                 LEAVE('n');
298                 return NULL;
299         }
300         // Get index
301         num = Name[0] - '0';
302         if(num >= NUM_VTS) {
303                 LEAVE('n');
304                 return NULL;
305         }
306         // Return node
307         LEAVE('p', &gVT_Terminals[num].Node);
308         return &gVT_Terminals[num].Node;
309 }
310
311 /**
312  * \fn int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
313  * \brief Control the VTerm Driver
314  */
315 int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
316 {
317          int    len;
318         switch(Id)
319         {
320         case DRV_IOCTL_TYPE:    return DRV_TYPE_MISC;
321         case DRV_IOCTL_IDENT:   memcpy(Data, "VT\0\0", 4);      return 0;
322         case DRV_IOCTL_VERSION: return VERSION;
323         case DRV_IOCTL_LOOKUP:  return 0;
324         
325         case 4: // Get Video Driver
326                 if(Data)        strcpy(Data, gsVT_OutputDevice);
327                 return strlen(gsVT_OutputDevice);
328         
329         case 5: // Set Video Driver
330                 if(!Data)       return -EINVAL;
331                 if(Threads_GetUID() != 0)       return -EACCES;
332                 
333                 len = strlen(Data);
334                 
335                 // TODO: Check if the string used is a heap string
336                 
337                 free(gsVT_OutputDevice);
338                 
339                 gsVT_OutputDevice = malloc(len+1);
340                 strcpy(gsVT_OutputDevice, Data);
341                 
342                 VFS_Close(giVT_OutputDevHandle);
343                 giVT_OutputDevHandle = -1;
344                 
345                 VT_InitOutput();
346                 return 1;
347         }
348         return 0;
349 }
350
351 /**
352  * \brief Read from a virtual terminal
353  */
354 size_t VT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
355 {
356          int    pos, avail;
357         tVTerm  *term = &gVT_Terminals[ Node->Inode ];
358         Uint32  *codepoint_buf = Buffer;
359         Uint32  *codepoint_in;
360         
361         Mutex_Acquire( &term->ReadingLock );
362         
363         // Check current mode
364         switch(term->Mode)
365         {
366         // Text Mode (UTF-8)
367         case TERM_MODE_TEXT:
368                 VT_int_UpdateCursor(term, 1);
369         
370                 VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "VT_Read (UTF-8)");
371                 
372                 avail = term->InputWrite - term->InputRead;
373                 if(avail < 0)
374                         avail += MAX_INPUT_CHARS8;
375                 if(avail > Length)
376                         avail = Length;
377                 
378                 pos = 0;
379                 while( avail -- )
380                 {
381                         ((char*)Buffer)[pos] = term->InputBuffer[term->InputRead];
382                         pos ++;
383                         term->InputRead ++;
384                         while(term->InputRead >= MAX_INPUT_CHARS8)
385                                 term->InputRead -= MAX_INPUT_CHARS8;
386                 }
387                 break;
388         
389         //case TERM_MODE_FB:
390         // Other - UCS-4
391         default:
392                 VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "VT_Read (UCS-4)");
393                 
394                 avail = term->InputWrite - term->InputRead;
395                 if(avail < 0)
396                         avail += MAX_INPUT_CHARS32;
397                 Length /= 4;
398                 if(avail > Length)
399                         avail = Length;
400                 
401                 codepoint_in = (void*)term->InputBuffer;
402                 codepoint_buf = Buffer;
403                 
404                 pos = 0;
405                 while( avail -- )
406                 {
407                         codepoint_buf[pos] = codepoint_in[term->InputRead];
408                         pos ++;
409                         term->InputRead ++;
410                         while(term->InputRead >= MAX_INPUT_CHARS32)
411                                 term->InputRead -= MAX_INPUT_CHARS32;
412                 }
413                 pos *= 4;
414                 break;
415         }
416         
417         // Mark none avaliable if buffer empty
418         if( term->InputRead == term->InputWrite )
419                 VFS_MarkAvaliable(&term->Node, 0);
420         
421         term->ReadingThread = -1;
422
423 //      VT_int_UpdateCursor(term, term->Mode == TERM_MODE_TEXT);
424
425         Mutex_Release( &term->ReadingLock );
426         
427         return pos;
428 }
429
430 /**
431  * \brief Write to a virtual terminal
432  */
433 size_t VT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer)
434 {
435         tVTerm  *term = &gVT_Terminals[ Node->Inode ];
436          int    size;
437         
438         // Write
439         switch( term->Mode )
440         {
441         // Print Text
442         case TERM_MODE_TEXT:
443                 VT_int_PutString(term, Buffer, Length);
444                 break;
445         
446         // Framebuffer :)
447         case TERM_MODE_FB:
448                 // - Sanity Checking
449                 size = term->Width*term->Height*4;
450                 if( Offset > size ) {
451                         Log_Notice("VTerm", "VT_Write: Offset (0x%llx) > FBSize (0x%x)",
452                                 Offset, size);
453                         return 0;
454                 }
455                 if( Offset + Length > size ) {
456                         Log_Notice("VTerm", "VT_Write: Offset+Length (0x%llx) > FBSize (0x%x)",
457                                 Offset+Length, size);
458                         Length = size - Offset;
459                 }
460                 
461                 // Update screen if needed
462                 if( Node->Inode == giVT_CurrentTerminal )
463                 {
464                         if( giVT_RealHeight > term->Height )
465                                 Offset += (giVT_RealHeight - term->Height) / 2 * term->Width * 4;
466                         // Handle undersized virtual terminals
467                         if( giVT_RealWidth > term->Width )
468                         {
469                                 // No? :( Well, just center it
470                                  int    x, y, w, h;
471                                 Uint    dst_ofs;
472                                 // TODO: Fix to handle the final line correctly?
473                                 x = Offset/4;   y = x / term->Width;    x %= term->Width;
474                                 w = Length/4+x; h = w / term->Width;    w %= term->Width;
475                                 
476                                 // Center
477                                 x += (giVT_RealWidth - term->Width) / 2;
478                                 dst_ofs = (x + y * giVT_RealWidth) * 4;
479                                 while(h--)
480                                 {
481                                         VFS_WriteAt( giVT_OutputDevHandle,
482                                                 dst_ofs,
483                                                 term->Width * 4,
484                                                 Buffer
485                                                 );
486                                         Buffer = (void*)( (Uint)Buffer + term->Width*4 );
487                                         dst_ofs += giVT_RealWidth * 4;
488                                 }
489                                 return 0;
490                         }
491                         else
492                         {
493                                 return VFS_WriteAt( giVT_OutputDevHandle, Offset, Length, Buffer );
494                         }
495                 }
496                 else
497                 {
498                         if( !term->Buffer )
499                                 term->Buffer = malloc( term->Width * term->Height * 4 );
500                         // Copy to the local cache
501                         memcpy( (char*)term->Buffer + (Uint)Offset, Buffer, Length );
502                 }
503                 break;
504         // Just pass on (for now)
505         // TODO: Handle locally too to ensure no information is lost on
506         //       VT Switch (and to isolate terminals from each other)
507         case TERM_MODE_2DACCEL:
508         //case TERM_MODE_3DACCEL:
509                 if( Node->Inode == giVT_CurrentTerminal )
510                 {
511                         VFS_Write( giVT_OutputDevHandle, Length, Buffer );
512                 }
513                 break;
514         }
515         
516         return 0;
517 }
518
519 /**
520  * \fn int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
521  * \brief Call an IO Control on a virtual terminal
522  */
523 int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
524 {
525          int    *iData = Data;
526          int    ret;
527         tVTerm  *term = Node->ImplPtr;
528         ENTER("pNode iId pData", Node, Id, Data);
529         
530         if(Id >= DRV_IOCTL_LOOKUP) {
531                 // Only root can fiddle with graphics modes
532                 // TODO: Remove this and replace with user ownership
533                 if( Threads_GetUID() != 0 )     return -1;
534         }
535         
536         switch(Id)
537         {
538         // --- Core Defined
539         case DRV_IOCTL_TYPE:
540                 LEAVE('i', DRV_TYPE_TERMINAL);
541                 return DRV_TYPE_TERMINAL;
542         case DRV_IOCTL_IDENT:
543                 memcpy(Data, "VT\0\0", 4);
544                 LEAVE('i', 0);
545                 return 0;
546         case DRV_IOCTL_VERSION:
547                 LEAVE('x', VERSION);
548                 return VERSION;
549         case DRV_IOCTL_LOOKUP:
550                 LEAVE('i', 0);
551                 return 0;
552         
553         // Get/Set the mode (and apply any changes)
554         case TERM_IOCTL_MODETYPE:
555                 if(Data != NULL)
556                 {
557                         if( CheckMem(Data, sizeof(int)) == 0 ) {
558                                 LEAVE('i', -1);
559                                 return -1;
560                         }
561                         Log_Log("VTerm", "VTerm %i mode set to %i", (int)Node->Inode, *iData);
562                         
563                         // Update mode if needed
564                         if( term->Mode != *iData || term->NewWidth || term->NewHeight)
565                         {
566                                 // Adjust for text mode
567                                 if( *iData == TERM_MODE_TEXT ) {
568                                         term->NewHeight *= giVT_CharHeight;
569                                         term->NewWidth *= giVT_CharWidth;
570                                 }
571                                 // Fill unchanged dimensions
572                                 if(term->NewHeight == 0)        term->NewHeight = term->Height;
573                                 if(term->NewWidth == 0) term->NewWidth = term->Width;
574                                 // Set new mode
575                                 VT_int_ChangeMode(term, *iData, term->NewWidth, term->NewHeight);
576                                 // Clear unapplied dimensions
577                                 term->NewWidth = 0;
578                                 term->NewHeight = 0;
579                         }
580                         
581                         // Update the screen dimensions
582                         if(Node->Inode == giVT_CurrentTerminal)
583                                 VT_SetTerminal( giVT_CurrentTerminal );
584                 }
585                 LEAVE('i', term->Mode);
586                 return term->Mode;
587         
588         // Get/set the terminal width
589         case TERM_IOCTL_WIDTH:
590                 if(Data != NULL) {
591                         if( CheckMem(Data, sizeof(int)) == 0 ) {
592                                 LEAVE('i', -1);
593                                 return -1;
594                         }
595                         term->NewWidth = *iData;
596                 }
597                 if( term->NewWidth )
598                         ret = term->NewWidth;
599                 else if( term->Mode == TERM_MODE_TEXT )
600                         ret = term->TextWidth;
601                 else
602                         ret = term->Width;
603                 LEAVE('i', ret);
604                 return ret;
605         
606         // Get/set the terminal height
607         case TERM_IOCTL_HEIGHT:
608                 if(Data != NULL) {
609                         if( CheckMem(Data, sizeof(int)) == 0 ) {
610                                 LEAVE('i', -1);
611                                 return -1;
612                         }
613                         term->NewHeight = *iData;
614                 }
615                 if( term->NewHeight )
616                         ret = term->NewHeight;
617                 else if( term->Mode == TERM_MODE_TEXT )
618                         ret = term->TextHeight;
619                 else
620                         ret = term->Height;
621                 LEAVE('i', ret);
622                 return ret;
623         
624         case TERM_IOCTL_FORCESHOW:
625                 Log_Log("VTerm", "Thread %i forced VTerm %i to be shown",
626                         Threads_GetTID(), (int)Node->Inode);
627                 VT_SetTerminal( Node->Inode );
628                 LEAVE('i', 1);
629                 return 1;
630         
631         case TERM_IOCTL_GETSETCURSOR:
632                 if(Data != NULL)
633                 {
634                         tVideo_IOCtl_Pos        *pos = Data;
635                         if( !CheckMem(Data, sizeof(*pos)) ) {
636                                 errno = -EINVAL;
637                                 LEAVE('i', -1);
638                                 return -1;
639                         }
640                 
641                         if( term->Mode == TERM_MODE_TEXT )
642                         {
643                                 if(term->Flags & VT_FLAG_ALTBUF)
644                                         term->AltWritePos = pos->x + pos->y * term->TextWidth;
645                                 else
646                                         term->WritePos = pos->x + pos->y * term->TextWidth + term->ViewPos;
647                                 VT_int_UpdateCursor(term, 0);
648                         }
649                         else
650                         {
651                                 term->VideoCursorX = pos->x;
652                                 term->VideoCursorY = pos->y;
653                                 VT_int_UpdateCursor(term, 1);
654                         }
655                 }
656                 ret = (term->Flags & VT_FLAG_ALTBUF) ? term->AltWritePos : term->WritePos-term->ViewPos;
657                 LEAVE('i', ret);
658                 return ret;
659
660         case TERM_IOCTL_SETCURSORBITMAP: {
661                 tVideo_IOCtl_Bitmap     *bmp = Data;
662                 if( Data == NULL )
663                 {
664                         free( term->VideoCursor );
665                         term->VideoCursor = NULL;
666                         LEAVE('i', 0);
667                         return 0;
668                 }
669
670                 // Sanity check bitmap
671                 if( !CheckMem(bmp, sizeof(tVideo_IOCtl_Bitmap)) ) {
672                         Log_Notice("VTerm", "%p in TERM_IOCTL_SETCURSORBITMAP invalid", bmp);
673                         errno = -EINVAL;
674                         LEAVE_RET('i', -1);
675                 }
676                 if( !CheckMem(bmp->Data, bmp->W*bmp->H*sizeof(Uint32)) ) {
677                         Log_Notice("VTerm", "%p in TERM_IOCTL_SETCURSORBITMAP invalid", bmp);
678                         errno = -EINVAL;
679                         LEAVE_RET('i', -1);
680                 }
681
682                 // Reallocate if needed
683                 if(term->VideoCursor)
684                 {
685                         if(bmp->W * bmp->H != term->VideoCursor->W * term->VideoCursor->H) {
686                                 free(term->VideoCursor);
687                                 term->VideoCursor = NULL;
688                         }
689                 }
690                 if(!term->VideoCursor) {
691                         term->VideoCursor = malloc(sizeof(tVideo_IOCtl_Pos) + bmp->W*bmp->H*sizeof(Uint32));
692                         if(!term->VideoCursor) {
693                                 Log_Error("VTerm", "Unable to allocate memory for cursor");
694                                 errno = -ENOMEM;
695                                 LEAVE_RET('i', -1);
696                         }
697                 }
698                 
699                 memcpy(term->VideoCursor, bmp, sizeof(tVideo_IOCtl_Pos) + bmp->W*bmp->H*sizeof(Uint32));
700         
701                 Log_Debug("VTerm", "Set VT%i's cursor to %p %ix%i",
702                         (int)term->Node.Inode, bmp, bmp->W, bmp->H);
703
704                 if(gpVT_CurTerm == term)
705                         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSORBITMAP, term->VideoCursor);
706         
707                 LEAVE('i', 0);
708                 return 0; }
709         }
710         LEAVE('i', -1);
711         return -1;
712 }
713
714 void VT_Terminal_Reference(tVFS_Node *Node)
715 {
716         // Append PID to list
717 }
718
719 void VT_Terminal_Close(tVFS_Node *Node)
720 {
721         // Remove PID from list
722 }
723
724 /**
725  * \fn void VT_SetTerminal(int ID)
726  * \brief Set the current terminal
727  */
728 void VT_SetTerminal(int ID)
729 {
730         // Copy the screen state
731         if( ID != giVT_CurrentTerminal && gpVT_CurTerm->Mode != TERM_MODE_TEXT )
732         {
733                 if( !gpVT_CurTerm->Buffer )
734                         gpVT_CurTerm->Buffer = malloc( gpVT_CurTerm->Width*gpVT_CurTerm->Height*4 );
735                 if( gpVT_CurTerm->Width < giVT_RealWidth )
736                 {
737                          int    line;
738                         Uint    ofs = 0;
739                         Uint32  *dest = gpVT_CurTerm->Buffer;
740                         // Slower scanline copy
741                         for( line = 0; line < gpVT_CurTerm->Height; line ++ )
742                         {
743                                 VFS_ReadAt(giVT_OutputDevHandle, ofs, gpVT_CurTerm->Width*4, dest);
744                                 ofs += giVT_RealWidth * 4;
745                                 dest += gpVT_CurTerm->Width;
746                         }
747                 }
748                 else
749                 {
750                         VFS_ReadAt(giVT_OutputDevHandle,
751                                 0, gpVT_CurTerm->Height*giVT_RealWidth*4,
752                                 gpVT_CurTerm->Buffer
753                                 );
754                 }
755         }
756
757         // Update current terminal ID
758         Log_Log("VTerm", "Changed terminal from %i to %i", giVT_CurrentTerminal, ID);
759         giVT_CurrentTerminal = ID;
760         gpVT_CurTerm = &gVT_Terminals[ID];
761         
762         LOG("Attempting VT_SetMode");
763         
764         if( gpVT_CurTerm->Mode == TERM_MODE_TEXT )
765         {
766                 VT_SetMode( VIDEO_BUFFMT_TEXT );
767         }
768         else
769         {
770                 // Update the cursor image
771                 if(gpVT_CurTerm->VideoCursor)
772                         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSORBITMAP, gpVT_CurTerm->VideoCursor);
773                 VT_SetMode( VIDEO_BUFFMT_FRAMEBUFFER );
774         }
775
776         LOG("Mode set");        
777
778         if(gpVT_CurTerm->Buffer)
779         {
780                 // TODO: Handle non equal sized
781                 VFS_WriteAt(
782                         giVT_OutputDevHandle,
783                         0,
784                         gpVT_CurTerm->Width*gpVT_CurTerm->Height*sizeof(Uint32),
785                         gpVT_CurTerm->Buffer
786                         );
787                 LOG("Updated screen contents");
788         }
789         
790         VT_int_UpdateCursor(gpVT_CurTerm, 1);
791         // Update the screen
792         VT_int_UpdateScreen(gpVT_CurTerm, 1);
793         LOG("done");
794 }

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