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 "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
33 extern void Debug_SetKTerminal(const char *File);
36 int VT_Install(char **Arguments);
37 int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data);
38 void VT_int_PutFBData(tVTerm *Term, size_t Offset, size_t Length, const void *Data);
39 void VT_PTYOutput(void *Handle, size_t Length, const void *Data);
40 int VT_PTYResize(void *Handle, const struct ptydims *Dims);
41 int VT_PTYModeset(void *Handle, const struct ptymode *Mode);
46 MODULE_DEFINE(0, VERSION, VTerm, VT_Install, NULL, "PTY", NULL);
47 tVFS_NodeType gVT_RootNodeType = {
48 .TypeName = "VTerm Root",
49 .IOCtl = VT_Root_IOCtl
51 tDevFS_Driver gVT_DrvInfo = {
58 .Type = &gVT_RootNodeType
62 tVTerm gVT_Terminals[NUM_VTS];
63 int giVT_CurrentTerminal = 0;
64 tVTerm *gpVT_CurTerm = &gVT_Terminals[0];
65 // --- Video State ---
66 short giVT_RealWidth = DEFAULT_WIDTH; //!< Screen Width
67 short giVT_RealHeight = DEFAULT_HEIGHT; //!< Screen Height
68 int giVT_Scrollback = DEFAULT_SCROLLBACK;
69 // --- Driver Handles ---
70 char *gsVT_OutputDevice = NULL;
71 char *gsVT_InputDevice = NULL;
72 int giVT_OutputDevHandle = -2;
73 int giVT_InputDevHandle = -2;
77 * \fn int VT_Install(char **Arguments)
78 * \brief Installs the Virtual Terminal Driver
80 int VT_Install(char **Arguments)
89 for(args = Arguments; (arg = *args); args++ )
91 char data[strlen(arg)+1];
95 val = strchr(arg, '=');
98 data[ val - arg ] = '\0';
101 Log_Debug("VTerm", "Argument '%s'='%s'", opt, val);
103 if( strcmp(opt, "Video") == 0 ) {
104 if( !gsVT_OutputDevice )
105 gsVT_OutputDevice = val;
107 else if( strcmp(opt, "Input") == 0 ) {
108 if( !gsVT_InputDevice )
109 gsVT_InputDevice = val;
111 else if( strcmp(opt, "Width") == 0 ) {
112 giVT_RealWidth = atoi( val );
114 else if( strcmp(opt, "Height") == 0 ) {
115 giVT_RealHeight = atoi( val );
117 else if( strcmp(opt, "Scrollback") == 0 ) {
118 giVT_Scrollback = atoi( val );
121 Log_Notice("VTerm", "Unknown option '%s'", opt);
127 if(!gsVT_OutputDevice) gsVT_OutputDevice = (char*)DEFAULT_OUTPUT;
128 else if( Module_EnsureLoaded( gsVT_OutputDevice ) ) gsVT_OutputDevice = (char*)DEFAULT_OUTPUT;
129 if( Module_EnsureLoaded( gsVT_OutputDevice ) ) gsVT_OutputDevice = (char*)FALLBACK_OUTPUT;
130 if( Module_EnsureLoaded( gsVT_OutputDevice ) ) {
131 Log_Error("VTerm", "Fallback video '%s' is not avaliable, giving up", FALLBACK_OUTPUT);
132 return MODULE_ERR_MISC;
135 if(!gsVT_InputDevice) gsVT_InputDevice = (char*)DEFAULT_INPUT;
136 else if( Module_EnsureLoaded( gsVT_InputDevice ) ) gsVT_InputDevice = (char*)DEFAULT_INPUT;
137 if( Module_EnsureLoaded( gsVT_InputDevice ) ) {
138 Log_Error("VTerm", "Fallback input '%s' is not avaliable, input will not be avaliable", DEFAULT_INPUT);
141 // Create device paths
144 tmp = malloc( 9 + strlen(gsVT_OutputDevice) + 1 );
145 strcpy(tmp, "/Devices/");
146 strcpy(&tmp[9], gsVT_OutputDevice);
147 gsVT_OutputDevice = tmp;
149 tmp = malloc( 9 + strlen(gsVT_InputDevice) + 1 );
150 strcpy(tmp, "/Devices/");
151 strcpy(&tmp[9], gsVT_InputDevice);
152 gsVT_InputDevice = tmp;
155 Log_Log("VTerm", "Using '%s' as output", gsVT_OutputDevice);
156 Log_Log("VTerm", "Using '%s' as input", gsVT_InputDevice);
163 Log_Debug("VTerm", "Initialising nodes (and creating buffers)");
164 for( i = 0; i < NUM_VTS; i++ )
166 // gVT_Terminals[i].Flags = VT_FLAG_HIDECSR; //HACK - Stop all those memcpy calls
167 gVT_Terminals[i].CurColour = DEFAULT_COLOUR;
168 gVT_Terminals[i].Mode = PTYBUFFMT_TEXT;
171 VT_int_Resize( &gVT_Terminals[i], giVT_RealWidth, giVT_RealHeight );
172 char name[] = {'v','t','0'+i,'\0'};
173 struct ptydims dims = {
174 .W = giVT_RealWidth / giVT_CharWidth,
175 .H = giVT_RealHeight / giVT_CharHeight,
176 .PW = giVT_RealWidth,
177 .PH = giVT_RealHeight
179 struct ptymode mode = {
180 .OutputMode = PTYBUFFMT_TEXT,
181 .InputMode = PTYIMODE_CANON|PTYIMODE_ECHO
183 gVT_Terminals[i].PTY = PTY_Create(name, &gVT_Terminals[i],
184 VT_PTYOutput, VT_PTYResize, VT_PTYModeset,
189 DevFS_AddDevice( &gVT_DrvInfo );
191 // Set kernel output to VT0
192 Log_Debug("VTerm", "Setting kernel output to VT#0");
193 Debug_SetKTerminal("/Devices/pts/vt0");
195 return MODULE_ERR_OK;
199 * \brief Set the video resolution
200 * \param Width New screen width
201 * \param Height New screen height
203 void VT_SetResolution(int Width, int Height)
205 tVideo_IOCtl_Mode mode = {0};
209 // Create the video mode
211 mode.height = Height;
216 VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_FINDMODE, &mode );
218 if( Width != mode.width || Height != mode.height )
221 "Selected resolution (%ix%i) is not supported by the device, using (%ix%i)",
222 giVT_RealWidth, giVT_RealHeight,
223 mode.width, mode.height
225 giVT_RealWidth = mode.width;
226 giVT_RealHeight = mode.height;
228 VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_GETSETMODE, &tmp );
230 // Resize text terminals if needed
231 // - VT0 check is for the first resolution set
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 int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
259 * \brief Control the VTerm Driver
261 int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
266 case DRV_IOCTL_TYPE: return DRV_TYPE_MISC;
267 case DRV_IOCTL_IDENT: memcpy(Data, "VT\0\0", 4); return 0;
268 case DRV_IOCTL_VERSION: return VERSION;
269 case DRV_IOCTL_LOOKUP: return 0;
271 case 4: // Get Video Driver
272 if(Data) strcpy(Data, gsVT_OutputDevice);
273 return strlen(gsVT_OutputDevice);
275 case 5: // Set Video Driver
276 if(!Data) return -EINVAL;
277 if(Threads_GetUID() != 0) return -EACCES;
281 // TODO: Check if the string used is a heap string
283 free(gsVT_OutputDevice);
285 gsVT_OutputDevice = malloc(len+1);
286 strcpy(gsVT_OutputDevice, Data);
288 VFS_Close(giVT_OutputDevHandle);
289 giVT_OutputDevHandle = -1;
297 void VT_int_PutFBData(tVTerm *Term, size_t Offset, size_t Length, const void *Buffer)
299 size_t maxlen = Term->Width * Term->Height * 4;
301 ENTER("pTerm xOffset xLength pBuffer", Term, Offset, Length, Buffer);
303 if( Offset >= maxlen ) {
308 LOG("maxlen = 0x%x", maxlen);
309 Length = MIN(Length, maxlen - Offset);
311 // If the terminal is currently shown, write directly to the screen
312 if( Term == gpVT_CurTerm )
314 // Center the terminal vertically
315 if( giVT_RealHeight > Term->Height ) {
316 Offset += (giVT_RealHeight - Term->Height) / 2 * Term->Width * 4;
317 LOG("Altered offset 0x%x", Offset);
320 // If the terminal is not native width, center it horizontally
321 if( giVT_RealWidth > Term->Width )
323 // No? :( Well, just center it
326 // TODO: Fix to handle the final line correctly?
327 x = Offset/4; y = x / Term->Width; x %= Term->Width;
328 w = Length/4+x; h = w / Term->Width; w %= Term->Width;
330 LOG("(%i,%i) %ix%i", x, y, w, h);
333 x += (giVT_RealWidth - Term->Width) / 2;
334 dst_ofs = (x + y * giVT_RealWidth) * 4;
337 VFS_WriteAt( giVT_OutputDevHandle,
342 Buffer = (const Uint32*)Buffer + Term->Width;
343 dst_ofs += giVT_RealWidth * 4;
346 // otherwise, just go directly to the screen
349 VFS_WriteAt( giVT_OutputDevHandle, Offset, Length, Buffer );
352 // If not active, write to the backbuffer (allocating if needed)
356 Term->Buffer = malloc( Term->Width * Term->Height * 4 );
357 LOG("Direct to cache");
358 // Copy to the local cache
359 memcpy( (char*)Term->Buffer + Offset, Buffer, Length );
364 void VT_PTYOutput(void *Handle, size_t Length, const void *Data)
366 tVTerm *term = Handle;
370 VT_int_PutString(term, Data, Length);
373 // TODO: How do offset?
374 VT_int_PutFBData(term, 0, Length, Data);
376 case PTYBUFFMT_2DCMD:
377 // TODO: Impliment 2D commands
378 VT_int_Handle2DCmd(term, Length, Data);
380 case PTYBUFFMT_3DCMD:
381 // TODO: Impliment 3D commands
386 int VT_PTYResize(void *Handle, const struct ptydims *Dims)
388 tVTerm *term = Handle;
389 int newW = Dims->W * (term->Mode == PTYBUFFMT_TEXT ? giVT_CharWidth : 1);
390 int newH = Dims->H * (term->Mode == PTYBUFFMT_TEXT ? giVT_CharHeight : 1);
391 if( newW > giVT_RealWidth || newH > giVT_RealHeight )
393 VT_int_Resize(term, newW, newH);
397 int VT_PTYModeset(void *Handle, const struct ptymode *Mode)
399 tVTerm *term = Handle;
400 term->Mode = (Mode->OutputMode & PTYOMODE_BUFFMT);
402 memset(&term->Cmd2D, 0, sizeof(term->Cmd2D));
404 if( term == gpVT_CurTerm ) {
408 VT_SetMode(VIDEO_BUFFMT_TEXT);
411 VT_SetMode(VIDEO_BUFFMT_FRAMEBUFFER);
421 * \fn int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
422 * \brief Call an IO Control on a virtual terminal
424 int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
428 tVTerm *term = Node->ImplPtr;
429 ENTER("pNode iId pData", Node, Id, Data);
431 if(Id >= DRV_IOCTL_LOOKUP) {
432 // Only root can fiddle with graphics modes
433 // TODO: Remove this and replace with user ownership
434 if( Threads_GetUID() != 0 ) return -1;
441 LEAVE('i', DRV_TYPE_TERMINAL);
442 return DRV_TYPE_TERMINAL;
443 case DRV_IOCTL_IDENT:
444 memcpy(Data, "VT\0\0", 4);
447 case DRV_IOCTL_VERSION:
450 case DRV_IOCTL_LOOKUP:
454 // Get/Set the mode (and apply any changes)
455 case TERM_IOCTL_MODETYPE:
458 if( CheckMem(Data, sizeof(int)) == 0 ) {
462 Log_Log("VTerm", "VTerm %i mode set to %i", (int)Node->Inode, *iData);
464 // Update mode if needed
465 if( term->Mode != *iData || term->NewWidth || term->NewHeight)
467 // Adjust for text mode
468 if( *iData == TERM_MODE_TEXT ) {
469 term->NewHeight *= giVT_CharHeight;
470 term->NewWidth *= giVT_CharWidth;
472 // Fill unchanged dimensions
473 if(term->NewHeight == 0) term->NewHeight = term->Height;
474 if(term->NewWidth == 0) term->NewWidth = term->Width;
476 VT_int_ChangeMode(term, *iData, term->NewWidth, term->NewHeight);
477 // Clear unapplied dimensions
482 // Update the screen dimensions
483 if(Node->Inode == giVT_CurrentTerminal)
484 VT_SetTerminal( giVT_CurrentTerminal );
486 LEAVE('i', term->Mode);
489 // Get/set the terminal width
490 case TERM_IOCTL_WIDTH:
492 if( CheckMem(Data, sizeof(int)) == 0 ) {
496 term->NewWidth = *iData;
499 ret = term->NewWidth;
500 else if( term->Mode == TERM_MODE_TEXT )
501 ret = term->TextWidth;
507 // Get/set the terminal height
508 case TERM_IOCTL_HEIGHT:
510 if( CheckMem(Data, sizeof(int)) == 0 ) {
514 term->NewHeight = *iData;
516 if( term->NewHeight )
517 ret = term->NewHeight;
518 else if( term->Mode == TERM_MODE_TEXT )
519 ret = term->TextHeight;
525 case TERM_IOCTL_FORCESHOW:
526 Log_Log("VTerm", "Thread %i forced VTerm %i to be shown",
527 Threads_GetTID(), (int)Node->Inode);
528 VT_SetTerminal( Node->Inode );
532 case TERM_IOCTL_GETSETCURSOR:
535 tVideo_IOCtl_Pos *pos = Data;
536 if( !CheckMem(Data, sizeof(*pos)) ) {
542 if( term->Mode == TERM_MODE_TEXT )
544 if(term->Flags & VT_FLAG_ALTBUF)
545 term->AltWritePos = pos->x + pos->y * term->TextWidth;
547 term->WritePos = pos->x + pos->y * term->TextWidth + term->ViewPos;
548 VT_int_UpdateCursor(term, 0);
552 term->VideoCursorX = pos->x;
553 term->VideoCursorY = pos->y;
554 VT_int_UpdateCursor(term, 1);
557 ret = (term->Flags & VT_FLAG_ALTBUF) ? term->AltWritePos : term->WritePos-term->ViewPos;
561 case TERM_IOCTL_SETCURSORBITMAP: {
562 tVideo_IOCtl_Bitmap *bmp = Data;
565 free( term->VideoCursor );
566 term->VideoCursor = NULL;
571 // Sanity check bitmap
572 if( !CheckMem(bmp, sizeof(tVideo_IOCtl_Bitmap)) ) {
573 Log_Notice("VTerm", "%p in TERM_IOCTL_SETCURSORBITMAP invalid", bmp);
577 if( !CheckMem(bmp->Data, bmp->W*bmp->H*sizeof(Uint32)) ) {
578 Log_Notice("VTerm", "%p in TERM_IOCTL_SETCURSORBITMAP invalid", bmp);
583 // Reallocate if needed
584 if(term->VideoCursor)
586 if(bmp->W * bmp->H != term->VideoCursor->W * term->VideoCursor->H) {
587 free(term->VideoCursor);
588 term->VideoCursor = NULL;
591 if(!term->VideoCursor) {
592 term->VideoCursor = malloc(sizeof(tVideo_IOCtl_Pos) + bmp->W*bmp->H*sizeof(Uint32));
593 if(!term->VideoCursor) {
594 Log_Error("VTerm", "Unable to allocate memory for cursor");
600 memcpy(term->VideoCursor, bmp, sizeof(tVideo_IOCtl_Pos) + bmp->W*bmp->H*sizeof(Uint32));
602 Log_Debug("VTerm", "Set VT%i's cursor to %p %ix%i",
603 (int)term->Node.Inode, bmp, bmp->W, bmp->H);
605 if(gpVT_CurTerm == term)
606 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSORBITMAP, term->VideoCursor);
615 void VT_Terminal_Reference(tVFS_Node *Node)
617 // Append PID to list
620 void VT_Terminal_Close(tVFS_Node *Node)
622 // Remove PID from list
627 * \fn void VT_SetTerminal(int ID)
628 * \brief Set the current terminal
630 void VT_SetTerminal(int ID)
632 // Copy the screen state
633 if( ID != giVT_CurrentTerminal && gpVT_CurTerm->Mode != TERM_MODE_TEXT )
635 if( !gpVT_CurTerm->Buffer )
636 gpVT_CurTerm->Buffer = malloc( gpVT_CurTerm->Width*gpVT_CurTerm->Height*4 );
637 if( gpVT_CurTerm->Width < giVT_RealWidth )
640 Uint32 *dest = gpVT_CurTerm->Buffer;
641 // Slower scanline copy
642 for( int line = 0; line < gpVT_CurTerm->Height; line ++ )
644 VFS_ReadAt(giVT_OutputDevHandle, ofs, gpVT_CurTerm->Width*4, dest);
645 ofs += giVT_RealWidth * 4;
646 dest += gpVT_CurTerm->Width;
651 VFS_ReadAt(giVT_OutputDevHandle,
652 0, gpVT_CurTerm->Height*giVT_RealWidth*4,
656 LOG("Cached screen contents");
659 // Update current terminal ID
660 Log_Log("VTerm", "Changed terminal from %i to %i", giVT_CurrentTerminal, ID);
661 giVT_CurrentTerminal = ID;
662 gpVT_CurTerm = &gVT_Terminals[ID];
664 LOG("Attempting VT_SetMode");
666 if( gpVT_CurTerm->Mode == PTYBUFFMT_TEXT )
668 VT_SetMode( VIDEO_BUFFMT_TEXT );
672 // Update the cursor image
673 if(gpVT_CurTerm->VideoCursor)
674 VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSORBITMAP, gpVT_CurTerm->VideoCursor);
675 VT_SetMode( VIDEO_BUFFMT_FRAMEBUFFER );
680 if(gpVT_CurTerm->Buffer)
682 // TODO: Handle non equal sized
684 giVT_OutputDevHandle,
686 gpVT_CurTerm->Width*gpVT_CurTerm->Height*sizeof(Uint32),
689 LOG("Updated screen contents");
692 VT_int_UpdateCursor(gpVT_CurTerm, 1);
694 VT_int_UpdateScreen(gpVT_CurTerm, 1);