2 * Acess2 Virtual Terminal Driver
8 #include <api_drv_keyboard.h>
9 #include <api_drv_video.h>
11 #include <semaphore.h>
14 #define VERSION ((0<<8)|(50))
17 //#define DEFAULT_OUTPUT "BochsGA"
18 #define DEFAULT_OUTPUT "Vesa"
19 #define FALLBACK_OUTPUT "x86_VGAText"
20 #define DEFAULT_INPUT "PS2Keyboard"
21 #define DEFAULT_WIDTH 640
22 #define DEFAULT_HEIGHT 480
23 #define DEFAULT_SCROLLBACK 2 // 2 Screens of text + current screen
24 //#define DEFAULT_SCROLLBACK 0
29 extern void Debug_SetKTerminal(const char *File);
32 int VT_Install(char **Arguments);
33 char *VT_ReadDir(tVFS_Node *Node, int Pos);
34 tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name);
35 int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data);
36 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
37 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, const void *Buffer);
38 int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data);
43 MODULE_DEFINE(0, VERSION, VTerm, VT_Install, NULL, NULL);
44 tVFS_NodeType gVT_RootNodeType = {
45 .TypeName = "VTerm Root",
46 .ReadDir = VT_ReadDir,
47 .FindDir = VT_FindDir,
48 .IOCtl = VT_Root_IOCtl
50 tVFS_NodeType gVT_TermNodeType = {
54 .IOCtl = VT_Terminal_IOCtl
56 tDevFS_Driver gVT_DrvInfo = {
59 .Flags = VFS_FFLAG_DIRECTORY,
63 .Type = &gVT_RootNodeType
67 tVTerm gVT_Terminals[NUM_VTS];
68 int giVT_CurrentTerminal = 0;
69 tVTerm *gpVT_CurTerm = &gVT_Terminals[0];
70 // --- Video State ---
71 short giVT_RealWidth = DEFAULT_WIDTH; //!< Screen Width
72 short giVT_RealHeight = DEFAULT_HEIGHT; //!< Screen Height
73 int giVT_Scrollback = DEFAULT_SCROLLBACK;
74 // --- Driver Handles ---
75 char *gsVT_OutputDevice = NULL;
76 char *gsVT_InputDevice = NULL;
77 int giVT_OutputDevHandle = -2;
78 int giVT_InputDevHandle = -2;
82 * \fn int VT_Install(char **Arguments)
83 * \brief Installs the Virtual Terminal Driver
85 int VT_Install(char **Arguments)
94 for(args = Arguments; (arg = *args); args++ )
96 char data[strlen(arg)+1];
100 val = strchr(arg, '=');
103 data[ val - arg ] = '\0';
106 Log_Debug("VTerm", "Argument '%s'", arg);
108 if( strcmp(opt, "Video") == 0 ) {
109 if( !gsVT_OutputDevice )
110 gsVT_OutputDevice = strdup(val);
112 else if( strcmp(opt, "Input") == 0 ) {
113 if( !gsVT_InputDevice )
114 gsVT_InputDevice = strdup(val);
116 else if( strcmp(opt, "Width") == 0 ) {
117 giVT_RealWidth = atoi( val );
119 else if( strcmp(opt, "Height") == 0 ) {
120 giVT_RealHeight = atoi( val );
122 else if( strcmp(opt, "Scrollback") == 0 ) {
123 giVT_Scrollback = atoi( val );
129 if(!gsVT_OutputDevice) gsVT_OutputDevice = (char*)DEFAULT_OUTPUT;
130 else if( Module_EnsureLoaded( gsVT_OutputDevice ) ) gsVT_OutputDevice = (char*)DEFAULT_OUTPUT;
131 if( Module_EnsureLoaded( gsVT_OutputDevice ) ) gsVT_OutputDevice = (char*)FALLBACK_OUTPUT;
132 if( Module_EnsureLoaded( gsVT_OutputDevice ) ) {
133 Log_Error("VTerm", "Fallback video '%s' is not avaliable, giving up", FALLBACK_OUTPUT);
134 return MODULE_ERR_MISC;
137 if(!gsVT_InputDevice) gsVT_InputDevice = (char*)DEFAULT_INPUT;
138 else if( Module_EnsureLoaded( gsVT_InputDevice ) ) gsVT_InputDevice = (char*)DEFAULT_INPUT;
139 if( Module_EnsureLoaded( gsVT_InputDevice ) ) {
140 Log_Error("VTerm", "Fallback input '%s' is not avaliable, input will not be avaliable", DEFAULT_INPUT);
143 // Create device paths
146 tmp = malloc( 9 + strlen(gsVT_OutputDevice) + 1 );
147 strcpy(tmp, "/Devices/");
148 strcpy(&tmp[9], gsVT_OutputDevice);
149 gsVT_OutputDevice = tmp;
151 tmp = malloc( 9 + strlen(gsVT_InputDevice) + 1 );
152 strcpy(tmp, "/Devices/");
153 strcpy(&tmp[9], gsVT_InputDevice);
154 gsVT_InputDevice = tmp;
157 Log_Log("VTerm", "Using '%s' as output", gsVT_OutputDevice);
158 Log_Log("VTerm", "Using '%s' as input", gsVT_InputDevice);
164 for( i = 0; i < NUM_VTS; i++ )
166 gVT_Terminals[i].Mode = TERM_MODE_TEXT;
167 gVT_Terminals[i].Flags = 0;
168 // gVT_Terminals[i].Flags = VT_FLAG_HIDECSR; //HACK - Stop all those memcpy calls
169 gVT_Terminals[i].CurColour = DEFAULT_COLOUR;
170 gVT_Terminals[i].WritePos = 0;
171 gVT_Terminals[i].AltWritePos = 0;
172 gVT_Terminals[i].ViewPos = 0;
173 gVT_Terminals[i].ReadingThread = -1;
174 gVT_Terminals[i].ScrollHeight = 0;
177 VT_int_ChangeMode( &gVT_Terminals[i],
178 TERM_MODE_TEXT, giVT_RealWidth, giVT_RealHeight );
180 gVT_Terminals[i].Name[0] = '0'+i;
181 gVT_Terminals[i].Name[1] = '\0';
182 gVT_Terminals[i].Node.Inode = i;
183 gVT_Terminals[i].Node.ImplPtr = &gVT_Terminals[i];
184 gVT_Terminals[i].Node.NumACLs = 0; // Only root can open virtual terminals
186 gVT_Terminals[i].Node.Type = &gVT_TermNodeType;
187 // Semaphore_Init(&gVT_Terminals[i].InputSemaphore, 0, MAX_INPUT_CHARS8, "VTerm", gVT_Terminals[i].Name);
191 DevFS_AddDevice( &gVT_DrvInfo );
193 // Set kernel output to VT0
194 Debug_SetKTerminal("/Devices/VTerm/0");
196 return MODULE_ERR_OK;
200 * \brief Set the video resolution
201 * \param Width New screen width
202 * \param Height New screen height
204 void VT_SetResolution(int Width, int Height)
206 tVideo_IOCtl_Mode mode = {0};
210 // Create the video mode
212 mode.height = Height;
217 VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_FINDMODE, &mode );
219 if( Width != mode.width || Height != mode.height )
222 "Selected resolution (%ix%i is not supported) by the device, using (%ix%i)",
223 giVT_RealWidth, giVT_RealHeight,
224 mode.width, mode.height
226 giVT_RealWidth = mode.width;
227 giVT_RealHeight = mode.height;
229 VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_GETSETMODE, &tmp );
231 // Resize text terminals if needed
232 if( gVT_Terminals[0].Text && (giVT_RealWidth != mode.width || giVT_RealHeight != mode.height) )
234 int newBufSize = (giVT_RealWidth/giVT_CharWidth)
235 *(giVT_RealHeight/giVT_CharHeight)
236 *(giVT_Scrollback+1);
238 // Resize the text terminals
239 Log_Debug("VTerm", "Resizing terminals to %ix%i",
240 giVT_RealWidth/giVT_CharWidth, giVT_RealHeight/giVT_CharHeight);
241 for( i = 0; i < NUM_VTS; i ++ )
243 if( gVT_Terminals[i].Mode != TERM_MODE_TEXT ) continue;
245 gVT_Terminals[i].TextWidth = giVT_RealWidth/giVT_CharWidth;
246 gVT_Terminals[i].TextHeight = giVT_RealHeight/giVT_CharHeight;
247 gVT_Terminals[i].ScrollHeight = gVT_Terminals[i].TextHeight;
249 gVT_Terminals[i].Text = realloc(
250 gVT_Terminals[i].Text,
251 newBufSize*sizeof(tVT_Char)
258 * \fn char *VT_ReadDir(tVFS_Node *Node, int Pos)
259 * \brief Read from the VTerm Directory
261 char *VT_ReadDir(tVFS_Node *Node, int Pos)
263 if(Pos < 0) return NULL;
264 if(Pos >= NUM_VTS) return NULL;
265 return strdup( gVT_Terminals[Pos].Name );
269 * \fn tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
270 * \brief Find an item in the VTerm directory
271 * \param Node Root node
272 * \param Name Name (number) of the terminal
274 tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
278 ENTER("pNode sName", Node, Name);
280 // Open the input and output files if needed
281 if(giVT_OutputDevHandle == -2) VT_InitOutput();
282 if(giVT_InputDevHandle == -2) VT_InitInput();
285 if(Name[0] < '0' || Name[0] > '9' || Name[1] != '\0') {
296 LEAVE('p', &gVT_Terminals[num].Node);
297 return &gVT_Terminals[num].Node;
301 * \fn int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
302 * \brief Control the VTerm Driver
304 int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
309 case DRV_IOCTL_TYPE: return DRV_TYPE_MISC;
310 case DRV_IOCTL_IDENT: memcpy(Data, "VT\0\0", 4); return 0;
311 case DRV_IOCTL_VERSION: return VERSION;
312 case DRV_IOCTL_LOOKUP: return 0;
314 case 4: // Get Video Driver
315 if(Data) strcpy(Data, gsVT_OutputDevice);
316 return strlen(gsVT_OutputDevice);
318 case 5: // Set Video Driver
319 if(!Data) return -EINVAL;
320 if(Threads_GetUID() != 0) return -EACCES;
324 // TODO: Check if the string used is a heap string
326 free(gsVT_OutputDevice);
328 gsVT_OutputDevice = malloc(len+1);
329 strcpy(gsVT_OutputDevice, Data);
331 VFS_Close(giVT_OutputDevHandle);
332 giVT_OutputDevHandle = -1;
341 * \brief Read from a virtual terminal
343 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
347 tVTerm *term = &gVT_Terminals[ Node->Inode ];
348 Uint32 *codepoint_buf = Buffer;
349 Uint32 *codepoint_in;
351 Mutex_Acquire( &term->ReadingLock );
353 // Check current mode
358 VT_int_UpdateCursor(term, 1);
360 VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "VT_Read (UTF-8)");
362 avail = term->InputWrite - term->InputRead;
364 avail += MAX_INPUT_CHARS8;
365 if(avail > Length - pos)
366 avail = Length - pos;
370 ((char*)Buffer)[pos] = term->InputBuffer[term->InputRead];
373 while(term->InputRead >= MAX_INPUT_CHARS8)
374 term->InputRead -= MAX_INPUT_CHARS8;
381 VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "VT_Read (UCS-4)");
383 avail = term->InputWrite - term->InputRead;
385 avail += MAX_INPUT_CHARS32;
387 if(avail > Length - pos)
388 avail = Length - pos;
390 codepoint_in = (void*)term->InputBuffer;
391 codepoint_buf = Buffer;
395 codepoint_buf[pos] = codepoint_in[term->InputRead];
398 while(term->InputRead >= MAX_INPUT_CHARS32)
399 term->InputRead -= MAX_INPUT_CHARS32;
405 // Mark none avaliable if buffer empty
406 if( term->InputRead == term->InputWrite )
407 VFS_MarkAvaliable(&term->Node, 0);
409 term->ReadingThread = -1;
411 // VT_int_UpdateCursor(term, term->Mode == TERM_MODE_TEXT);
413 Mutex_Release( &term->ReadingLock );
419 * \fn Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, const void *Buffer)
420 * \brief Write to a virtual terminal
422 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, const void *Buffer)
424 tVTerm *term = &gVT_Terminals[ Node->Inode ];
432 VT_int_PutString(term, Buffer, Length);
438 size = term->Width*term->Height*4;
439 if( Offset > size ) {
440 Log_Notice("VTerm", "VT_Write: Offset (0x%llx) > FBSize (0x%x)",
444 if( Offset + Length > size ) {
445 Log_Notice("VTerm", "VT_Write: Offset+Length (0x%llx) > FBSize (0x%x)",
446 Offset+Length, size);
447 Length = size - Offset;
450 // Update screen if needed
451 if( Node->Inode == giVT_CurrentTerminal )
453 if( giVT_RealHeight > term->Height )
454 Offset += (giVT_RealHeight - term->Height) / 2 * term->Width * 4;
455 // Handle undersized virtual terminals
456 if( giVT_RealWidth > term->Width )
458 // No? :( Well, just center it
461 // TODO: Fix to handle the final line correctly?
462 x = Offset/4; y = x / term->Width; x %= term->Width;
463 w = Length/4+x; h = w / term->Width; w %= term->Width;
466 x += (giVT_RealWidth - term->Width) / 2;
467 dst_ofs = (x + y * giVT_RealWidth) * 4;
470 VFS_WriteAt( giVT_OutputDevHandle,
475 Buffer = (void*)( (Uint)Buffer + term->Width*4 );
476 dst_ofs += giVT_RealWidth * 4;
482 return VFS_WriteAt( giVT_OutputDevHandle, Offset, Length, Buffer );
488 term->Buffer = malloc( term->Width * term->Height * 4 );
489 // Copy to the local cache
490 memcpy( (char*)term->Buffer + (Uint)Offset, Buffer, Length );
493 // Just pass on (for now)
494 // TODO: Handle locally too to ensure no information is lost on
495 // VT Switch (and to isolate terminals from each other)
496 case TERM_MODE_2DACCEL:
497 //case TERM_MODE_3DACCEL:
498 if( Node->Inode == giVT_CurrentTerminal )
500 VFS_Write( giVT_OutputDevHandle, Length, Buffer );
509 * \fn int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
510 * \brief Call an IO Control on a virtual terminal
512 int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
516 tVTerm *term = Node->ImplPtr;
517 ENTER("pNode iId pData", Node, Id, Data);
519 if(Id >= DRV_IOCTL_LOOKUP) {
520 // Only root can fiddle with graphics modes
521 // TODO: Remove this and replace with user ownership
522 if( Threads_GetUID() != 0 ) return -1;
529 LEAVE('i', DRV_TYPE_TERMINAL);
530 return DRV_TYPE_TERMINAL;
531 case DRV_IOCTL_IDENT:
532 memcpy(Data, "VT\0\0", 4);
535 case DRV_IOCTL_VERSION:
538 case DRV_IOCTL_LOOKUP:
542 // Get/Set the mode (and apply any changes)
543 case TERM_IOCTL_MODETYPE:
546 if( CheckMem(Data, sizeof(int)) == 0 ) {
550 Log_Log("VTerm", "VTerm %i mode set to %i", (int)Node->Inode, *iData);
552 // Update mode if needed
553 if( term->Mode != *iData || term->NewWidth || term->NewHeight)
555 // Adjust for text mode
556 if( *iData == TERM_MODE_TEXT ) {
557 term->NewHeight *= giVT_CharHeight;
558 term->NewWidth *= giVT_CharWidth;
560 // Fill unchanged dimensions
561 if(term->NewHeight == 0) term->NewHeight = term->Height;
562 if(term->NewWidth == 0) term->NewWidth = term->Width;
564 VT_int_ChangeMode(term, *iData, term->NewWidth, term->NewHeight);
565 // Clear unapplied dimensions
570 // Update the screen dimensions
571 if(Node->Inode == giVT_CurrentTerminal)
572 VT_SetTerminal( giVT_CurrentTerminal );
574 LEAVE('i', term->Mode);
577 // Get/set the terminal width
578 case TERM_IOCTL_WIDTH:
580 if( CheckMem(Data, sizeof(int)) == 0 ) {
584 term->NewWidth = *iData;
587 ret = term->NewWidth;
588 else if( term->Mode == TERM_MODE_TEXT )
589 ret = term->TextWidth;
595 // Get/set the terminal height
596 case TERM_IOCTL_HEIGHT:
598 if( CheckMem(Data, sizeof(int)) == 0 ) {
602 term->NewHeight = *iData;
604 if( term->NewHeight )
605 ret = term->NewHeight;
606 else if( term->Mode == TERM_MODE_TEXT )
607 ret = term->TextHeight;
613 case TERM_IOCTL_FORCESHOW:
614 Log_Log("VTerm", "Thread %i forced VTerm %i to be shown",
615 Threads_GetTID(), (int)Node->Inode);
616 VT_SetTerminal( Node->Inode );
620 case TERM_IOCTL_GETSETCURSOR:
623 tVideo_IOCtl_Pos *pos = Data;
624 if( !CheckMem(Data, sizeof(*pos)) ) {
630 if( term->Mode == TERM_MODE_TEXT )
632 if(term->Flags & VT_FLAG_ALTBUF)
633 term->AltWritePos = pos->x + pos->y * term->TextWidth;
635 term->WritePos = pos->x + pos->y * term->TextWidth + term->ViewPos;
636 VT_int_UpdateCursor(term, 0);
640 term->VideoCursorX = pos->x;
641 term->VideoCursorY = pos->y;
642 VT_int_UpdateCursor(term, 1);
645 ret = (term->Flags & VT_FLAG_ALTBUF) ? term->AltWritePos : term->WritePos-term->ViewPos;
649 case TERM_IOCTL_SETCURSORBITMAP: {
650 tVideo_IOCtl_Bitmap *bmp = Data;
653 free( term->VideoCursor );
654 term->VideoCursor = NULL;
659 // Sanity check bitmap
660 if( !CheckMem(bmp, sizeof(tVideo_IOCtl_Bitmap)) ) {
661 Log_Notice("VTerm", "%p in TERM_IOCTL_SETCURSORBITMAP invalid", bmp);
665 if( !CheckMem(bmp->Data, bmp->W*bmp->H*sizeof(Uint32)) ) {
666 Log_Notice("VTerm", "%p in TERM_IOCTL_SETCURSORBITMAP invalid", bmp);
671 // Reallocate if needed
672 if(term->VideoCursor)
674 if(bmp->W * bmp->H != term->VideoCursor->W * term->VideoCursor->H) {
675 free(term->VideoCursor);
676 term->VideoCursor = NULL;
679 if(!term->VideoCursor) {
680 term->VideoCursor = malloc(sizeof(tVideo_IOCtl_Pos) + bmp->W*bmp->H*sizeof(Uint32));
681 if(!term->VideoCursor) {
682 Log_Error("VTerm", "Unable to allocate memory for cursor");
688 memcpy(term->VideoCursor, bmp, sizeof(tVideo_IOCtl_Pos) + bmp->W*bmp->H*sizeof(Uint32));
690 Log_Debug("VTerm", "Set VT%i's cursor to %p %ix%i",
691 (int)term->Node.Inode, bmp, bmp->W, bmp->H);
693 if(gpVT_CurTerm == term)
694 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSORBITMAP, term->VideoCursor);
704 * \fn void VT_SetTerminal(int ID)
705 * \brief Set the current terminal
707 void VT_SetTerminal(int ID)
709 // Copy the screen state
710 if( ID != giVT_CurrentTerminal && gpVT_CurTerm->Mode != TERM_MODE_TEXT )
712 if( !gpVT_CurTerm->Buffer )
713 gpVT_CurTerm->Buffer = malloc( gpVT_CurTerm->Width*gpVT_CurTerm->Height*4 );
714 if( gpVT_CurTerm->Width < giVT_RealWidth )
718 Uint32 *dest = gpVT_CurTerm->Buffer;
719 // Slower scanline copy
720 for( line = 0; line < gpVT_CurTerm->Height; line ++ )
722 VFS_ReadAt(giVT_OutputDevHandle, ofs, gpVT_CurTerm->Width*4, dest);
723 ofs += giVT_RealWidth * 4;
724 dest += gpVT_CurTerm->Width;
729 VFS_ReadAt(giVT_OutputDevHandle,
730 0, gpVT_CurTerm->Height*giVT_RealWidth*4,
736 // Update current terminal ID
737 Log_Log("VTerm", "Changed terminal from %i to %i", giVT_CurrentTerminal, ID);
738 giVT_CurrentTerminal = ID;
739 gpVT_CurTerm = &gVT_Terminals[ID];
741 if( gpVT_CurTerm->Mode == TERM_MODE_TEXT )
743 VT_SetMode( VIDEO_BUFFMT_TEXT );
747 // Update the cursor image
748 if(gpVT_CurTerm->VideoCursor)
749 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSORBITMAP, gpVT_CurTerm->VideoCursor);
750 VT_SetMode( VIDEO_BUFFMT_FRAMEBUFFER );
753 if(gpVT_CurTerm->Buffer)
755 // TODO: Handle non equal sized
757 giVT_OutputDevHandle,
759 gpVT_CurTerm->Width*gpVT_CurTerm->Height*sizeof(Uint32),
764 VT_int_UpdateCursor(gpVT_CurTerm, 1);
766 VT_int_UpdateScreen(gpVT_CurTerm, 1);