3 * - By John Hodge (thePowersGang)
6 * - Virtual Terminal - Initialisation and VFS Interface
12 #include <api_drv_keyboard.h>
13 #include <api_drv_video.h>
15 #include <semaphore.h>
18 #define VERSION ((0<<8)|(50))
21 //#define DEFAULT_OUTPUT "BochsGA"
22 #define DEFAULT_OUTPUT "Vesa"
23 #define FALLBACK_OUTPUT "x86_VGAText"
24 #define DEFAULT_INPUT "PS2Keyboard"
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
33 extern void Debug_SetKTerminal(const char *File);
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 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
41 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, const void *Buffer);
42 int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data);
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
54 tVFS_NodeType gVT_TermNodeType = {
58 .IOCtl = VT_Terminal_IOCtl
60 tDevFS_Driver gVT_DrvInfo = {
63 .Flags = VFS_FFLAG_DIRECTORY,
67 .Type = &gVT_RootNodeType
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;
86 * \fn int VT_Install(char **Arguments)
87 * \brief Installs the Virtual Terminal Driver
89 int VT_Install(char **Arguments)
98 for(args = Arguments; (arg = *args); args++ )
100 char data[strlen(arg)+1];
104 val = strchr(arg, '=');
107 data[ val - arg ] = '\0';
110 Log_Debug("VTerm", "Argument '%s'", arg);
112 if( strcmp(opt, "Video") == 0 ) {
113 if( !gsVT_OutputDevice )
114 gsVT_OutputDevice = strdup(val);
116 else if( strcmp(opt, "Input") == 0 ) {
117 if( !gsVT_InputDevice )
118 gsVT_InputDevice = strdup(val);
120 else if( strcmp(opt, "Width") == 0 ) {
121 giVT_RealWidth = atoi( val );
123 else if( strcmp(opt, "Height") == 0 ) {
124 giVT_RealHeight = atoi( val );
126 else if( strcmp(opt, "Scrollback") == 0 ) {
127 giVT_Scrollback = atoi( val );
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;
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);
147 // Create device paths
150 tmp = malloc( 9 + strlen(gsVT_OutputDevice) + 1 );
151 strcpy(tmp, "/Devices/");
152 strcpy(&tmp[9], gsVT_OutputDevice);
153 gsVT_OutputDevice = tmp;
155 tmp = malloc( 9 + strlen(gsVT_InputDevice) + 1 );
156 strcpy(tmp, "/Devices/");
157 strcpy(&tmp[9], gsVT_InputDevice);
158 gsVT_InputDevice = tmp;
161 Log_Log("VTerm", "Using '%s' as output", gsVT_OutputDevice);
162 Log_Log("VTerm", "Using '%s' as input", gsVT_InputDevice);
168 for( i = 0; i < NUM_VTS; i++ )
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;
181 VT_int_ChangeMode( &gVT_Terminals[i],
182 TERM_MODE_TEXT, giVT_RealWidth, giVT_RealHeight );
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
190 gVT_Terminals[i].Node.Type = &gVT_TermNodeType;
191 // Semaphore_Init(&gVT_Terminals[i].InputSemaphore, 0, MAX_INPUT_CHARS8, "VTerm", gVT_Terminals[i].Name);
195 DevFS_AddDevice( &gVT_DrvInfo );
197 // Set kernel output to VT0
198 Debug_SetKTerminal("/Devices/VTerm/0");
200 return MODULE_ERR_OK;
204 * \brief Set the video resolution
205 * \param Width New screen width
206 * \param Height New screen height
208 void VT_SetResolution(int Width, int Height)
210 tVideo_IOCtl_Mode mode = {0};
214 // Create the video mode
216 mode.height = Height;
221 VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_FINDMODE, &mode );
223 if( Width != mode.width || Height != mode.height )
226 "Selected resolution (%ix%i is not supported) by the device, using (%ix%i)",
227 giVT_RealWidth, giVT_RealHeight,
228 mode.width, mode.height
230 giVT_RealWidth = mode.width;
231 giVT_RealHeight = mode.height;
233 VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_GETSETMODE, &tmp );
235 // Resize text terminals if needed
236 if( gVT_Terminals[0].Text && (giVT_RealWidth != mode.width || giVT_RealHeight != mode.height) )
238 int newBufSize = (giVT_RealWidth/giVT_CharWidth)
239 *(giVT_RealHeight/giVT_CharHeight)
240 *(giVT_Scrollback+1);
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 ++ )
247 if( gVT_Terminals[i].Mode != TERM_MODE_TEXT ) continue;
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;
253 gVT_Terminals[i].Text = realloc(
254 gVT_Terminals[i].Text,
255 newBufSize*sizeof(tVT_Char)
262 * \fn char *VT_ReadDir(tVFS_Node *Node, int Pos)
263 * \brief Read from the VTerm Directory
265 char *VT_ReadDir(tVFS_Node *Node, int Pos)
267 if(Pos < 0) return NULL;
268 if(Pos >= NUM_VTS) return NULL;
269 return strdup( gVT_Terminals[Pos].Name );
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
278 tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
282 ENTER("pNode sName", Node, Name);
284 // Open the input and output files if needed
285 if(giVT_OutputDevHandle == -2) VT_InitOutput();
286 if(giVT_InputDevHandle == -2) VT_InitInput();
289 if(Name[0] < '0' || Name[0] > '9' || Name[1] != '\0') {
300 LEAVE('p', &gVT_Terminals[num].Node);
301 return &gVT_Terminals[num].Node;
305 * \fn int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
306 * \brief Control the VTerm Driver
308 int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
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;
318 case 4: // Get Video Driver
319 if(Data) strcpy(Data, gsVT_OutputDevice);
320 return strlen(gsVT_OutputDevice);
322 case 5: // Set Video Driver
323 if(!Data) return -EINVAL;
324 if(Threads_GetUID() != 0) return -EACCES;
328 // TODO: Check if the string used is a heap string
330 free(gsVT_OutputDevice);
332 gsVT_OutputDevice = malloc(len+1);
333 strcpy(gsVT_OutputDevice, Data);
335 VFS_Close(giVT_OutputDevHandle);
336 giVT_OutputDevHandle = -1;
345 * \brief Read from a virtual terminal
347 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
351 tVTerm *term = &gVT_Terminals[ Node->Inode ];
352 Uint32 *codepoint_buf = Buffer;
353 Uint32 *codepoint_in;
355 Mutex_Acquire( &term->ReadingLock );
357 // Check current mode
362 VT_int_UpdateCursor(term, 1);
364 VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "VT_Read (UTF-8)");
366 avail = term->InputWrite - term->InputRead;
368 avail += MAX_INPUT_CHARS8;
369 if(avail > Length - pos)
370 avail = Length - pos;
374 ((char*)Buffer)[pos] = term->InputBuffer[term->InputRead];
377 while(term->InputRead >= MAX_INPUT_CHARS8)
378 term->InputRead -= MAX_INPUT_CHARS8;
385 VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "VT_Read (UCS-4)");
387 avail = term->InputWrite - term->InputRead;
389 avail += MAX_INPUT_CHARS32;
391 if(avail > Length - pos)
392 avail = Length - pos;
394 codepoint_in = (void*)term->InputBuffer;
395 codepoint_buf = Buffer;
399 codepoint_buf[pos] = codepoint_in[term->InputRead];
402 while(term->InputRead >= MAX_INPUT_CHARS32)
403 term->InputRead -= MAX_INPUT_CHARS32;
409 // Mark none avaliable if buffer empty
410 if( term->InputRead == term->InputWrite )
411 VFS_MarkAvaliable(&term->Node, 0);
413 term->ReadingThread = -1;
415 // VT_int_UpdateCursor(term, term->Mode == TERM_MODE_TEXT);
417 Mutex_Release( &term->ReadingLock );
423 * \fn Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, const void *Buffer)
424 * \brief Write to a virtual terminal
426 Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, const void *Buffer)
428 tVTerm *term = &gVT_Terminals[ Node->Inode ];
436 VT_int_PutString(term, Buffer, Length);
442 size = term->Width*term->Height*4;
443 if( Offset > size ) {
444 Log_Notice("VTerm", "VT_Write: Offset (0x%llx) > FBSize (0x%x)",
448 if( Offset + Length > size ) {
449 Log_Notice("VTerm", "VT_Write: Offset+Length (0x%llx) > FBSize (0x%x)",
450 Offset+Length, size);
451 Length = size - Offset;
454 // Update screen if needed
455 if( Node->Inode == giVT_CurrentTerminal )
457 if( giVT_RealHeight > term->Height )
458 Offset += (giVT_RealHeight - term->Height) / 2 * term->Width * 4;
459 // Handle undersized virtual terminals
460 if( giVT_RealWidth > term->Width )
462 // No? :( Well, just center it
465 // TODO: Fix to handle the final line correctly?
466 x = Offset/4; y = x / term->Width; x %= term->Width;
467 w = Length/4+x; h = w / term->Width; w %= term->Width;
470 x += (giVT_RealWidth - term->Width) / 2;
471 dst_ofs = (x + y * giVT_RealWidth) * 4;
474 VFS_WriteAt( giVT_OutputDevHandle,
479 Buffer = (void*)( (Uint)Buffer + term->Width*4 );
480 dst_ofs += giVT_RealWidth * 4;
486 return VFS_WriteAt( giVT_OutputDevHandle, Offset, Length, Buffer );
492 term->Buffer = malloc( term->Width * term->Height * 4 );
493 // Copy to the local cache
494 memcpy( (char*)term->Buffer + (Uint)Offset, Buffer, Length );
497 // Just pass on (for now)
498 // TODO: Handle locally too to ensure no information is lost on
499 // VT Switch (and to isolate terminals from each other)
500 case TERM_MODE_2DACCEL:
501 //case TERM_MODE_3DACCEL:
502 if( Node->Inode == giVT_CurrentTerminal )
504 VFS_Write( giVT_OutputDevHandle, Length, Buffer );
513 * \fn int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
514 * \brief Call an IO Control on a virtual terminal
516 int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
520 tVTerm *term = Node->ImplPtr;
521 ENTER("pNode iId pData", Node, Id, Data);
523 if(Id >= DRV_IOCTL_LOOKUP) {
524 // Only root can fiddle with graphics modes
525 // TODO: Remove this and replace with user ownership
526 if( Threads_GetUID() != 0 ) return -1;
533 LEAVE('i', DRV_TYPE_TERMINAL);
534 return DRV_TYPE_TERMINAL;
535 case DRV_IOCTL_IDENT:
536 memcpy(Data, "VT\0\0", 4);
539 case DRV_IOCTL_VERSION:
542 case DRV_IOCTL_LOOKUP:
546 // Get/Set the mode (and apply any changes)
547 case TERM_IOCTL_MODETYPE:
550 if( CheckMem(Data, sizeof(int)) == 0 ) {
554 Log_Log("VTerm", "VTerm %i mode set to %i", (int)Node->Inode, *iData);
556 // Update mode if needed
557 if( term->Mode != *iData || term->NewWidth || term->NewHeight)
559 // Adjust for text mode
560 if( *iData == TERM_MODE_TEXT ) {
561 term->NewHeight *= giVT_CharHeight;
562 term->NewWidth *= giVT_CharWidth;
564 // Fill unchanged dimensions
565 if(term->NewHeight == 0) term->NewHeight = term->Height;
566 if(term->NewWidth == 0) term->NewWidth = term->Width;
568 VT_int_ChangeMode(term, *iData, term->NewWidth, term->NewHeight);
569 // Clear unapplied dimensions
574 // Update the screen dimensions
575 if(Node->Inode == giVT_CurrentTerminal)
576 VT_SetTerminal( giVT_CurrentTerminal );
578 LEAVE('i', term->Mode);
581 // Get/set the terminal width
582 case TERM_IOCTL_WIDTH:
584 if( CheckMem(Data, sizeof(int)) == 0 ) {
588 term->NewWidth = *iData;
591 ret = term->NewWidth;
592 else if( term->Mode == TERM_MODE_TEXT )
593 ret = term->TextWidth;
599 // Get/set the terminal height
600 case TERM_IOCTL_HEIGHT:
602 if( CheckMem(Data, sizeof(int)) == 0 ) {
606 term->NewHeight = *iData;
608 if( term->NewHeight )
609 ret = term->NewHeight;
610 else if( term->Mode == TERM_MODE_TEXT )
611 ret = term->TextHeight;
617 case TERM_IOCTL_FORCESHOW:
618 Log_Log("VTerm", "Thread %i forced VTerm %i to be shown",
619 Threads_GetTID(), (int)Node->Inode);
620 VT_SetTerminal( Node->Inode );
624 case TERM_IOCTL_GETSETCURSOR:
627 tVideo_IOCtl_Pos *pos = Data;
628 if( !CheckMem(Data, sizeof(*pos)) ) {
634 if( term->Mode == TERM_MODE_TEXT )
636 if(term->Flags & VT_FLAG_ALTBUF)
637 term->AltWritePos = pos->x + pos->y * term->TextWidth;
639 term->WritePos = pos->x + pos->y * term->TextWidth + term->ViewPos;
640 VT_int_UpdateCursor(term, 0);
644 term->VideoCursorX = pos->x;
645 term->VideoCursorY = pos->y;
646 VT_int_UpdateCursor(term, 1);
649 ret = (term->Flags & VT_FLAG_ALTBUF) ? term->AltWritePos : term->WritePos-term->ViewPos;
653 case TERM_IOCTL_SETCURSORBITMAP: {
654 tVideo_IOCtl_Bitmap *bmp = Data;
657 free( term->VideoCursor );
658 term->VideoCursor = NULL;
663 // Sanity check bitmap
664 if( !CheckMem(bmp, sizeof(tVideo_IOCtl_Bitmap)) ) {
665 Log_Notice("VTerm", "%p in TERM_IOCTL_SETCURSORBITMAP invalid", bmp);
669 if( !CheckMem(bmp->Data, bmp->W*bmp->H*sizeof(Uint32)) ) {
670 Log_Notice("VTerm", "%p in TERM_IOCTL_SETCURSORBITMAP invalid", bmp);
675 // Reallocate if needed
676 if(term->VideoCursor)
678 if(bmp->W * bmp->H != term->VideoCursor->W * term->VideoCursor->H) {
679 free(term->VideoCursor);
680 term->VideoCursor = NULL;
683 if(!term->VideoCursor) {
684 term->VideoCursor = malloc(sizeof(tVideo_IOCtl_Pos) + bmp->W*bmp->H*sizeof(Uint32));
685 if(!term->VideoCursor) {
686 Log_Error("VTerm", "Unable to allocate memory for cursor");
692 memcpy(term->VideoCursor, bmp, sizeof(tVideo_IOCtl_Pos) + bmp->W*bmp->H*sizeof(Uint32));
694 Log_Debug("VTerm", "Set VT%i's cursor to %p %ix%i",
695 (int)term->Node.Inode, bmp, bmp->W, bmp->H);
697 if(gpVT_CurTerm == term)
698 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSORBITMAP, term->VideoCursor);
708 * \fn void VT_SetTerminal(int ID)
709 * \brief Set the current terminal
711 void VT_SetTerminal(int ID)
713 // Copy the screen state
714 if( ID != giVT_CurrentTerminal && gpVT_CurTerm->Mode != TERM_MODE_TEXT )
716 if( !gpVT_CurTerm->Buffer )
717 gpVT_CurTerm->Buffer = malloc( gpVT_CurTerm->Width*gpVT_CurTerm->Height*4 );
718 if( gpVT_CurTerm->Width < giVT_RealWidth )
722 Uint32 *dest = gpVT_CurTerm->Buffer;
723 // Slower scanline copy
724 for( line = 0; line < gpVT_CurTerm->Height; line ++ )
726 VFS_ReadAt(giVT_OutputDevHandle, ofs, gpVT_CurTerm->Width*4, dest);
727 ofs += giVT_RealWidth * 4;
728 dest += gpVT_CurTerm->Width;
733 VFS_ReadAt(giVT_OutputDevHandle,
734 0, gpVT_CurTerm->Height*giVT_RealWidth*4,
740 // Update current terminal ID
741 Log_Log("VTerm", "Changed terminal from %i to %i", giVT_CurrentTerminal, ID);
742 giVT_CurrentTerminal = ID;
743 gpVT_CurTerm = &gVT_Terminals[ID];
745 if( gpVT_CurTerm->Mode == TERM_MODE_TEXT )
747 VT_SetMode( VIDEO_BUFFMT_TEXT );
751 // Update the cursor image
752 if(gpVT_CurTerm->VideoCursor)
753 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSORBITMAP, gpVT_CurTerm->VideoCursor);
754 VT_SetMode( VIDEO_BUFFMT_FRAMEBUFFER );
757 if(gpVT_CurTerm->Buffer)
759 // TODO: Handle non equal sized
761 giVT_OutputDevHandle,
763 gpVT_CurTerm->Width*gpVT_CurTerm->Height*sizeof(Uint32),
768 VT_int_UpdateCursor(gpVT_CurTerm, 1);
770 VT_int_UpdateScreen(gpVT_CurTerm, 1);