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

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