#include <acess.h>
#include <fs_devfs.h>
#include <modules.h>
-#include <tpl_drv_video.h>
-#include <tpl_drv_keyboard.h>
-#include <tpl_drv_terminal.h>
+#include <api_drv_video.h>
+#include <api_drv_keyboard.h>
+#include <api_drv_terminal.h>
#include <errno.h>
#include <semaphore.h>
#define MAX_INPUT_CHARS8 (MAX_INPUT_CHARS32*4)
//#define DEFAULT_OUTPUT "BochsGA"
#define DEFAULT_OUTPUT "Vesa"
+#define FALLBACK_OUTPUT "x86_VGAText"
#define DEFAULT_INPUT "PS2Keyboard"
#define DEFAULT_WIDTH 640
#define DEFAULT_HEIGHT 480
#define DEFAULT_SCROLLBACK 2 // 2 Screens of text + current screen
+//#define DEFAULT_SCROLLBACK 0
#define DEFAULT_COLOUR (VT_COL_BLACK|(0xAAA<<16))
#define VT_FLAG_HIDECSR 0x01
#define VT_FLAG_ALTBUF 0x02 //!< Alternate screen buffer
#define VT_FLAG_RAWIN 0x04 //!< Don't handle ^Z/^C/^V
#define VT_FLAG_HASFB 0x10 //!< Set if the VTerm has requested the Framebuffer
+#define VT_FLAG_SHOWCSR 0x20 //!< Always show the text cursor
enum eVT_InModes {
VT_INMODE_TEXT8, // UTF-8 Text Mode (VT100/xterm Emulation)
int AltWritePos; //!< Alternate write position
short ScrollTop; //!< Top of scrolling region (smallest)
short ScrollHeight; //!< Length of scrolling region
+
+ int VideoCursorX;
+ int VideoCursorY;
tMutex ReadingLock; //!< Lock the VTerm when a process is reading from it
tTID ReadingThread; //!< Owner of the lock
// tSemaphore InputSemaphore;
Uint32 *Buffer;
+
+ // TODO: Do I need to keep this about?
+ // When should it be deallocated? on move to text mode, or some other time
+ // Call set again, it's freed, and if NULL it doesn't get reallocated.
+ tVideo_IOCtl_Bitmap *VideoCursor;
char Name[2]; //!< Name of the terminal
tVFS_Node Node;
void VT_int_PutChar(tVTerm *Term, Uint32 Ch);
void VT_int_ScrollText(tVTerm *Term, int Count);
void VT_int_ScrollFramebuffer( tVTerm *Term, int Count );
+void VT_int_UpdateCursor( tVTerm *Term, int bShow );
void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll );
void VT_int_ChangeMode(tVTerm *Term, int NewMode, int NewWidth, int NewHeight);
void VT_int_ToggleAltBuffer(tVTerm *Term, int Enabled);
};
// === GLOBALS ===
-MODULE_DEFINE(0, VERSION, VTerm, VT_Install, NULL, DEFAULT_OUTPUT, DEFAULT_INPUT, NULL);
+MODULE_DEFINE(0, VERSION, VTerm, VT_Install, NULL, DEFAULT_INPUT, NULL);
tDevFS_Driver gVT_DrvInfo = {
NULL, "VTerm",
{
Log_Debug("VTerm", "Argument '%s'", arg);
if( strcmp(opt, "Video") == 0 ) {
- if( !gsVT_OutputDevice && Modules_InitialiseBuiltin( val ) == 0 )
+ if( !gsVT_OutputDevice )
gsVT_OutputDevice = strdup(val);
}
else if( strcmp(opt, "Input") == 0 ) {
- if( !gsVT_InputDevice && Modules_InitialiseBuiltin( val ) == 0 )
+ if( !gsVT_InputDevice )
gsVT_InputDevice = strdup(val);
}
else if( strcmp(opt, "Width") == 0 ) {
}
// Apply Defaults
- if(!gsVT_OutputDevice) gsVT_OutputDevice = strdup(DEFAULT_OUTPUT);
- if(!gsVT_InputDevice) gsVT_InputDevice = strdup(DEFAULT_INPUT);
+ if(!gsVT_OutputDevice) gsVT_OutputDevice = (char*)DEFAULT_OUTPUT;
+ else if( Module_EnsureLoaded( gsVT_OutputDevice ) ) gsVT_OutputDevice = (char*)DEFAULT_OUTPUT;
+ if( Module_EnsureLoaded( gsVT_OutputDevice ) ) gsVT_OutputDevice = (char*)FALLBACK_OUTPUT;
+ if( Module_EnsureLoaded( gsVT_OutputDevice ) ) {
+ Log_Error("VTerm", "Fallback video '%s' is not avaliable, giving up", FALLBACK_OUTPUT);
+ return MODULE_ERR_MISC;
+ }
+
+ if(!gsVT_InputDevice) gsVT_InputDevice = (char*)DEFAULT_INPUT;
+ else if( Module_EnsureLoaded( gsVT_InputDevice ) ) gsVT_InputDevice = (char*)DEFAULT_INPUT;
- // Create paths
+ // Create device paths
{
char *tmp;
tmp = malloc( 9 + strlen(gsVT_OutputDevice) + 1 );
strcpy(tmp, "/Devices/");
strcpy(&tmp[9], gsVT_OutputDevice);
gsVT_OutputDevice = tmp;
+
tmp = malloc( 9 + strlen(gsVT_InputDevice) + 1 );
strcpy(tmp, "/Devices/");
strcpy(&tmp[9], gsVT_InputDevice);
Log_Log("VTerm", "Using '%s' as output", gsVT_OutputDevice);
Log_Log("VTerm", "Using '%s' as input", gsVT_InputDevice);
+ VT_InitOutput();
+ VT_InitInput();
+
// Create Nodes
for( i = 0; i < NUM_VTS; i++ )
{
// Add to DevFS
DevFS_AddDevice( &gVT_DrvInfo );
- VT_InitOutput();
- VT_InitInput();
-
// Set kernel output to VT0
Debug_SetKTerminal("/Devices/VTerm/0");
- Log_Log("VTerm", "Returning %i", MODULE_ERR_OK);
return MODULE_ERR_OK;
}
VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_GETSETMODE, &tmp );
// Resize text terminals if needed
- if( giVT_RealWidth != mode.width || giVT_RealHeight != mode.height )
+ if( gVT_Terminals[0].Text && (giVT_RealWidth != mode.width || giVT_RealHeight != mode.height) )
{
int newBufSize = (giVT_RealWidth/giVT_CharWidth)
*(giVT_RealHeight/giVT_CharHeight)
// Resize the text terminals
giVT_RealWidth = mode.width;
giVT_RealHeight = mode.height;
+ Log_Debug("VTerm", "Resizing terminals to %ix%i",
+ giVT_RealWidth/giVT_CharWidth, giVT_RealHeight/giVT_CharHeight);
for( i = 0; i < NUM_VTS; i ++ )
{
if( gVT_Terminals[i].Mode != TERM_MODE_TEXT ) continue;
gVT_Terminals[i].TextWidth = giVT_RealWidth/giVT_CharWidth;
gVT_Terminals[i].TextHeight = giVT_RealHeight/giVT_CharHeight;
+ gVT_Terminals[i].ScrollHeight = gVT_Terminals[i].TextHeight;
gVT_Terminals[i].Text = realloc(
gVT_Terminals[i].Text,
Mutex_Acquire( &term->ReadingLock );
+ // Update cursor
+ VT_int_UpdateCursor(term, 1);
+
// Check current mode
switch(term->Mode)
{
((char*)Buffer)[pos] = term->InputBuffer[term->InputRead];
pos ++;
term->InputRead ++;
- term->InputRead %= MAX_INPUT_CHARS8;
+ while(term->InputRead > MAX_INPUT_CHARS8)
+ term->InputRead -= MAX_INPUT_CHARS8;
}
break;
codepoint_buf[pos] = codepoint_in[term->InputRead];
pos ++;
term->InputRead ++;
- term->InputRead %= MAX_INPUT_CHARS32;
+ while(term->InputRead > MAX_INPUT_CHARS32)
+ term->InputRead -= MAX_INPUT_CHARS32;
}
pos *= 4;
break;
VFS_MarkAvaliable(&term->Node, 0);
term->ReadingThread = -1;
-
+
+// VT_int_UpdateCursor(term, term->Mode == TERM_MODE_TEXT);
+
Mutex_Release( &term->ReadingLock );
return pos;
Log_Log("VTerm", "VTerm %i mode set to %i", (int)Node->Inode, *iData);
// Update mode if needed
- if( term->Mode != *iData
- || term->NewWidth
- || term->NewHeight)
+ if( term->Mode != *iData || term->NewWidth || term->NewHeight)
{
// Adjust for text mode
if( *iData == TERM_MODE_TEXT ) {
LEAVE('i', 1);
return 1;
- case TERM_IOCTL_GETCURSOR:
+ case TERM_IOCTL_GETSETCURSOR:
+ if(Data != NULL)
+ {
+ tVideo_IOCtl_Pos *pos = Data;
+ if( !CheckMem(Data, sizeof(*pos)) ) {
+ errno = -EINVAL;
+ LEAVE('i', -1);
+ return -1;
+ }
+
+ if( term->Mode == TERM_MODE_TEXT )
+ {
+ if(term->Flags & VT_FLAG_ALTBUF)
+ term->AltWritePos = pos->x + pos->y * term->TextWidth;
+ else
+ term->WritePos = pos->x + pos->y * term->TextWidth + term->ViewPos;
+ VT_int_UpdateCursor(term, 0);
+ }
+ else
+ {
+ term->VideoCursorX = pos->x;
+ term->VideoCursorY = pos->y;
+ VT_int_UpdateCursor(term, 1);
+ }
+ }
ret = (term->Flags & VT_FLAG_ALTBUF) ? term->AltWritePos : term->WritePos-term->ViewPos;
LEAVE('i', ret);
return ret;
+
+ case TERM_IOCTL_SETCURSORBITMAP: {
+ tVideo_IOCtl_Bitmap *bmp = Data;
+ if( Data == NULL )
+ {
+ free( term->VideoCursor );
+ term->VideoCursor = NULL;
+ LEAVE('i', 0);
+ return 0;
+ }
+
+ // Sanity check bitmap
+ if( !CheckMem(bmp, sizeof(tVideo_IOCtl_Bitmap)) ) {
+ Log_Notice("VTerm", "%p in TERM_IOCTL_SETCURSORBITMAP invalid", bmp);
+ errno = -EINVAL;
+ LEAVE_RET('i', -1);
+ }
+ if( !CheckMem(bmp->Data, bmp->W*bmp->H*sizeof(Uint32)) ) {
+ Log_Notice("VTerm", "%p in TERM_IOCTL_SETCURSORBITMAP invalid", bmp);
+ errno = -EINVAL;
+ LEAVE_RET('i', -1);
+ }
+
+ // Reallocate if needed
+ if(term->VideoCursor)
+ {
+ if(bmp->W * bmp->H != term->VideoCursor->W * term->VideoCursor->H) {
+ free(term->VideoCursor);
+ term->VideoCursor = NULL;
+ }
+ }
+ if(!term->VideoCursor) {
+ term->VideoCursor = malloc(sizeof(tVideo_IOCtl_Pos) + bmp->W*bmp->H*sizeof(Uint32));
+ if(!term->VideoCursor) {
+ Log_Error("VTerm", "Unable to allocate memory for cursor");
+ errno = -ENOMEM;
+ LEAVE_RET('i', -1);
+ }
+ }
+
+ memcpy(term->VideoCursor, bmp, sizeof(tVideo_IOCtl_Pos) + bmp->W*bmp->H*sizeof(Uint32));
+
+ Log_Debug("VTerm", "Set VT%i's cursor to %p %ix%i",
+ (int)term->Node.Inode, bmp, bmp->W, bmp->H);
+
+ if(gpVT_CurTerm == term)
+ VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSORBITMAP, term->VideoCursor);
+
+ LEAVE('i', 0);
+ return 0; }
}
LEAVE('i', -1);
return -1;
giVT_CurrentTerminal = ID;
gpVT_CurTerm = &gVT_Terminals[ID];
- // Update cursor
- if( gpVT_CurTerm->Mode == TERM_MODE_TEXT && !(gpVT_CurTerm->Flags & VT_FLAG_HIDECSR) )
- {
- tVideo_IOCtl_Pos pos;
- int offset = (gpVT_CurTerm->Flags & VT_FLAG_ALTBUF) ? gpVT_CurTerm->AltWritePos : gpVT_CurTerm->WritePos - gpVT_CurTerm->ViewPos;
- pos.x = offset % gpVT_CurTerm->TextWidth;
- pos.y = offset / gpVT_CurTerm->TextWidth;
- if( 0 <= pos.y && pos.y < gpVT_CurTerm->TextHeight )
- VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
- }
if( gpVT_CurTerm->Mode == TERM_MODE_TEXT )
+ {
VT_SetMode( VIDEO_BUFFMT_TEXT );
+ }
else
+ {
+ // Update the cursor image
+ if(gpVT_CurTerm->VideoCursor)
+ VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSORBITMAP, gpVT_CurTerm->VideoCursor);
VT_SetMode( VIDEO_BUFFMT_FRAMEBUFFER );
+ }
+
+ VT_int_UpdateCursor(gpVT_CurTerm, 1);
// Update the screen
- VT_int_UpdateScreen( &gVT_Terminals[ ID ], 1 );
+ VT_int_UpdateScreen( gpVT_CurTerm, 1 );
}
/**
case KEY_PGDOWN:
if( gpVT_CurTerm->Flags & VT_FLAG_ALTBUF )
return ;
- if( gpVT_CurTerm->ViewPos < gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback-1) )
+ if( gpVT_CurTerm->ViewPos < gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback) )
gpVT_CurTerm->ViewPos += gpVT_CurTerm->Width;
else
- gpVT_CurTerm->ViewPos = gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback-1);
+ gpVT_CurTerm->ViewPos = gpVT_CurTerm->Width*gpVT_CurTerm->Height*(giVT_Scrollback);
return;
}
}
}
VFS_MarkAvaliable(&term->Node, 1);
-
- // Wake up the thread waiting on us
- //if( term->ReadingThread >= 0 ) {
- // Threads_WakeTID(term->ReadingThread);
- //}
}
/**
}
// Get Command
- if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
+ if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
{
if( bQuestionMark )
{
}
// Update Screen
VT_int_UpdateScreen( Term, 0 );
-
- // Update cursor
- if( Term == gpVT_CurTerm && !(Term->Flags & VT_FLAG_HIDECSR) )
- {
- tVideo_IOCtl_Pos pos;
- int offset = (gpVT_CurTerm->Flags & VT_FLAG_ALTBUF) ? gpVT_CurTerm->AltWritePos : gpVT_CurTerm->WritePos - gpVT_CurTerm->ViewPos;
- pos.x = offset % gpVT_CurTerm->TextWidth;
- pos.y = offset / gpVT_CurTerm->TextWidth;
- if( 0 <= pos.y && pos.y < gpVT_CurTerm->TextHeight )
- VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
- }
}
/**
tVT_Char *buf;
int height, init_write_pos;
int len, i;
-
+ int scroll_top, scroll_height;
+
+ // Get buffer pointer and attributes
if( Term->Flags & VT_FLAG_ALTBUF )
{
buf = Term->AltBuf;
height = Term->TextHeight;
init_write_pos = Term->AltWritePos;
+ scroll_top = Term->ScrollTop;
+ scroll_height = Term->ScrollHeight;
}
else
{
buf = Term->Text;
- height = Term->TextHeight*giVT_Scrollback;
+ height = Term->TextHeight*(giVT_Scrollback+1);
init_write_pos = Term->WritePos;
+ scroll_top = 0;
+ scroll_height = height;
}
-
+
+ // Scroll text downwards
if( Count > 0 )
{
int base;
- if(Count > Term->ScrollHeight) Count = Term->ScrollHeight;
- base = Term->TextWidth*(Term->ScrollTop + Term->ScrollHeight - Count);
- len = Term->TextWidth*(Term->ScrollHeight - Count);
+
+ // Set up
+ if(Count > scroll_height) Count = scroll_height;
+ base = Term->TextWidth*(scroll_top + scroll_height - Count);
+ len = Term->TextWidth*(scroll_height - Count);
// Scroll terminal cache
- memcpy(
- &buf[Term->TextWidth*Term->ScrollTop],
- &buf[Term->TextWidth*(Term->ScrollTop+Count)],
+ memmove(
+ &buf[Term->TextWidth*scroll_top],
+ &buf[Term->TextWidth*(scroll_top+Count)],
len*sizeof(tVT_Char)
);
// Clear last rows
for( i = 0; i < Term->TextWidth*Count; i ++ )
{
- Term->AltBuf[ base + i ].Ch = 0;
- Term->AltBuf[ base + i ].Colour = Term->CurColour;
+ buf[ base + i ].Ch = 0;
+ buf[ base + i ].Colour = Term->CurColour;
}
// Update Screen
VT_int_ScrollFramebuffer( Term, Count );
if( Term->Flags & VT_FLAG_ALTBUF )
- Term->AltWritePos = Term->TextWidth*(Term->ScrollTop + Term->ScrollHeight - Count);
+ Term->AltWritePos = base;
else
- Term->WritePos = Term->ViewPos + Term->TextWidth*(Term->ScrollTop + Term->ScrollHeight - Count);
-// Log_Debug("VTerm", "Term->WritePos = %i/%i = %i", Term->WritePos, Term->TextWidth, Term->WritePos/Term->TextWidth);
+ Term->WritePos = Term->ViewPos + Term->TextWidth*(Term->TextHeight - Count);
for( i = 0; i < Count; i ++ )
{
VT_int_UpdateScreen( Term, 0 );
else
{
Count = -Count;
- if(Count > Term->ScrollHeight) Count = Term->ScrollHeight;
+ if(Count > scroll_height) Count = scroll_height;
- len = Term->TextWidth*(Term->ScrollHeight - Count);
+ len = Term->TextWidth*(scroll_height - Count);
// Scroll terminal cache
- memcpy(
- &buf[Term->TextWidth*(Term->ScrollTop+Count)],
- &buf[Term->TextWidth*Term->ScrollTop],
+ memmove(
+ &buf[Term->TextWidth*(scroll_top+Count)],
+ &buf[Term->TextWidth*scroll_top],
len*sizeof(tVT_Char)
);
// Clear preceding rows
for( i = 0; i < Term->TextWidth*Count; i ++ )
{
- Term->AltBuf[ i ].Ch = 0;
- Term->AltBuf[ i ].Colour = Term->CurColour;
+ buf[ i ].Ch = 0;
+ buf[ i ].Colour = Term->CurColour;
}
VT_int_ScrollFramebuffer( Term, -Count );
if( Term->Flags & VT_FLAG_ALTBUF )
- Term->AltWritePos = Term->TextWidth*Term->ScrollTop;
+ Term->AltWritePos = Term->TextWidth*scroll_top;
else
- Term->WritePos = Term->ViewPos + Term->TextWidth*Term->ScrollTop;
+ Term->WritePos = Term->ViewPos;
for( i = 0; i < Count; i ++ )
{
VT_int_UpdateScreen( Term, 0 );
// BLIT to 0,0 from 0,giVT_CharHeight
buf.Op = VIDEO_2DOP_BLIT;
buf.SrcX = 0; buf.DstX = 0;
+ // TODO: Don't assume character dimensions
buf.W = Term->TextWidth * giVT_CharWidth;
if( Count > 0 )
{
VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp);
}
+void VT_int_UpdateCursor( tVTerm *Term, int bShow )
+{
+ tVideo_IOCtl_Pos csr_pos;
+
+ if( Term != gpVT_CurTerm ) return ;
+
+ if( !bShow )
+ {
+ csr_pos.x = -1;
+ csr_pos.y = -1;
+ }
+ else if( Term->Mode == TERM_MODE_TEXT )
+ {
+ int offset;
+
+// if( !(Term->Flags & VT_FLAG_SHOWCSR)
+// && ( (Term->Flags & VT_FLAG_HIDECSR) || !Term->Node.ReadThreads)
+// )
+ if( !Term->Text || Term->Flags & VT_FLAG_HIDECSR )
+ {
+ csr_pos.x = -1;
+ csr_pos.y = -1;
+ }
+ else
+ {
+ if(Term->Flags & VT_FLAG_ALTBUF)
+ offset = Term->AltWritePos;
+ else
+ offset = Term->WritePos - Term->ViewPos;
+
+ csr_pos.x = offset % Term->TextWidth;
+ csr_pos.y = offset / Term->TextWidth;
+ if( 0 > csr_pos.y || csr_pos.y >= Term->TextHeight )
+ csr_pos.y = -1, csr_pos.x = -1;
+ }
+ }
+ else
+ {
+ csr_pos.x = Term->VideoCursorX;
+ csr_pos.y = Term->VideoCursorY;
+ }
+ VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &csr_pos);
+}
+
/**
* \fn void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
* \brief Updates the video framebuffer
);
break;
}
+
+ VT_int_UpdateCursor(Term, 1);
}
/**
*/
void VT_int_ChangeMode(tVTerm *Term, int NewMode, int NewWidth, int NewHeight)
{
-
// TODO: Increase RealWidth/RealHeight when this happens
if(NewWidth > giVT_RealWidth) NewWidth = giVT_RealWidth;
if(NewHeight > giVT_RealHeight) NewHeight = giVT_RealHeight;
Term->Mode = NewMode;
-
+
if(NewWidth != Term->Width || NewHeight != Term->Height)
{
int oldW = Term->Width;
}
free(oldFB);
}
+
+ // Debug
+ switch(NewMode)
+ {
+ case TERM_MODE_TEXT:
+ Log_Log("VTerm", "Set VT %p to text mode (%ix%i)",
+ Term, Term->TextWidth, Term->TextHeight);
+ break;
+ case TERM_MODE_FB:
+ Log_Log("VTerm", "Set VT %p to framebuffer mode (%ix%i)",
+ Term, Term->Width, Term->Height);
+ break;
+ //case TERM_MODE_2DACCEL:
+ //case TERM_MODE_3DACCEL:
+ // return;
+ }
}
-
- // Debug
- switch(NewMode)
- {
- case TERM_MODE_TEXT:
- Log_Log("VTerm", "Set VT %p to text mode (%ix%i)",
- Term, Term->TextWidth, Term->TextHeight);
- break;
- case TERM_MODE_FB:
- Log_Log("VTerm", "Set VT %p to framebuffer mode (%ix%i)",
- Term, Term->Width, Term->Height);
- break;
- //case TERM_MODE_2DACCEL:
- //case TERM_MODE_3DACCEL:
- // return;
- }
}