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

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