/*
* Acess2 Virtual Terminal Driver
*/
-#include <common.h>
+#define DEBUG 0
+#include <acess.h>
#include <fs_devfs.h>
#include <modules.h>
#include <tpl_drv_video.h>
#include <tpl_drv_terminal.h>
#include <errno.h>
+#define USE_CTRL_ALT 0
+
// === CONSTANTS ===
#define VERSION ((0<<8)|(50))
-#define NUM_VTS 4
+#define NUM_VTS 8
#define MAX_INPUT_CHARS32 64
#define MAX_INPUT_CHARS8 (MAX_INPUT_CHARS32*4)
-#define VT_SCROLLBACK 1 // 2 Screens of text
-#define DEFAULT_OUTPUT "VGA"
-//#define DEFAULT_OUTPUT "/Devices/BochsGA"
+#define VT_SCROLLBACK 2 // 2 Screens of text
+//#define DEFAULT_OUTPUT "VGA"
+//#define DEFAULT_OUTPUT "BochsGA"
+#define DEFAULT_OUTPUT "Vesa"
#define DEFAULT_INPUT "PS2Keyboard"
#define DEFAULT_WIDTH 80
#define DEFAULT_HEIGHT 25
#define DEFAULT_COLOUR (VT_COL_BLACK|(0xAAA<<16))
#define VT_FLAG_HIDECSR 0x01
+#define VT_FLAG_HASFB 0x10 //!< Set if the VTerm has requested the Framebuffer
enum eVT_InModes {
VT_INMODE_TEXT8, // UTF-8 Text Mode (VT100 Emulation)
int Flags; //!< Flags (see VT_FLAG_*)
short Width; //!< Virtual Width
short Height; //!< Virtual Height
- short RealWidth; //!< Real Width
- short RealHeight; //!< Real Height
int ViewPos; //!< View Buffer Offset (Text Only)
int WritePos; //!< Write Buffer Offset (Text Only)
Uint32 CurColour; //!< Current Text Colour
- char Name[2]; //!< Name of the terminal
int InputRead; //!< Input buffer read position
int InputWrite; //!< Input buffer write position
tVT_Char *Text;
Uint32 *Buffer;
};
+ char Name[2]; //!< Name of the terminal
tVFS_Node Node;
} tVTerm;
+// === IMPORTS ===
+extern void Debug_SetKTerminal(char *File);
+
// === PROTOTYPES ===
int VT_Install(char **Arguments);
+void VT_InitOutput(void);
+void VT_InitInput(void);
char *VT_ReadDir(tVFS_Node *Node, int Pos);
tVFS_Node *VT_FindDir(tVFS_Node *Node, char *Name);
int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data);
Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data);
+void VT_SetResolution(int IsTextMode, int Width, int Height);
void VT_SetTerminal(int ID);
void VT_KBCallBack(Uint32 Codepoint);
void VT_int_PutString(tVTerm *Term, Uint8 *Buffer, Uint Count);
int VT_int_ParseEscape(tVTerm *Term, char *Buffer);
void VT_int_PutChar(tVTerm *Term, Uint32 Ch);
void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll );
+void VT_int_ChangeMode(tVTerm *Term, int NewMode);
// === CONSTANTS ===
const Uint16 caVT100Colours[] = {
// --- Terminals ---
tVTerm gVT_Terminals[NUM_VTS];
int giVT_CurrentTerminal = 0;
+tVTerm *gpVT_CurTerm = &gVT_Terminals[0];
+// --- Video State ---
+short giVT_RealWidth; //!< Real Width
+short giVT_RealHeight; //!< Real Height
// --- Driver Handles ---
char *gsVT_OutputDevice = NULL;
char *gsVT_InputDevice = NULL;
strcpy(gsVT_InputDevice, args[1]);
args ++;
break;
-
}
}
}
if(!gsVT_OutputDevice) gsVT_OutputDevice = "/Devices/"DEFAULT_OUTPUT;
if(!gsVT_InputDevice) gsVT_InputDevice = "/Devices/"DEFAULT_INPUT;
- LOG("Using '%s' as output", gsVT_OutputDevice);
- LOG("Using '%s' as input", gsVT_InputDevice);
+ Log_Log("VTerm", "Using '%s' as output", gsVT_OutputDevice);
+ Log_Log("VTerm", "Using '%s' as input", gsVT_InputDevice);
// Create Nodes
for( i = 0; i < NUM_VTS; i++ )
gVT_Terminals[i].WritePos = 0;
gVT_Terminals[i].ViewPos = 0;
- gVT_Terminals[i].Buffer = malloc( DEFAULT_WIDTH*DEFAULT_HEIGHT*VT_SCROLLBACK*sizeof(tVT_Char) );
- memset( gVT_Terminals[i].Buffer, 0, DEFAULT_WIDTH*DEFAULT_HEIGHT*VT_SCROLLBACK*sizeof(tVT_Char) );
+ gVT_Terminals[i].Buffer = calloc( DEFAULT_WIDTH*DEFAULT_HEIGHT*VT_SCROLLBACK, sizeof(tVT_Char) );
gVT_Terminals[i].Name[0] = '0'+i;
gVT_Terminals[i].Name[1] = '\0';
// Add to DevFS
DevFS_AddDevice( &gVT_DrvInfo );
- return 0;
+ 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;
}
/**
void VT_InitOutput()
{
giVT_OutputDevHandle = VFS_Open(gsVT_OutputDevice, VFS_OPENFLAG_WRITE);
- LOG("giVT_OutputDevHandle = %x\n", giVT_OutputDevHandle);
+ if(giVT_InputDevHandle == -1) {
+ Log_Warning("VTerm", "Oh F**k, I can't open the video device '%s'", gsVT_OutputDevice);
+ }
VT_SetTerminal( 0 );
+ VT_SetResolution(1, 640, 480);
}
/**
void VT_InitInput()
{
giVT_InputDevHandle = VFS_Open(gsVT_InputDevice, VFS_OPENFLAG_READ);
- LOG("giVT_InputDevHandle = %x\n", giVT_InputDevHandle);
if(giVT_InputDevHandle == -1) return ;
VFS_IOCtl(giVT_InputDevHandle, KB_IOCTL_SETCALLBACK, VT_KBCallBack);
}
{
int num;
- //ENTER("pNode sName", Node, Name);
+ ENTER("pNode sName", Node, Name);
// Open the input and output files if needed
if(giVT_OutputDevHandle == -2) VT_InitOutput();
return NULL;
}
// Return node
- //LEAVE('p', &gVT_Terminals[num].Node);
+ LEAVE('p', &gVT_Terminals[num].Node);
return &gVT_Terminals[num].Node;
}
}
break;
- case TERM_MODE_FB:
- case TERM_MODE_OPENGL:
+ //case TERM_MODE_FB:
+ default:
while(pos < Length)
{
while(term->InputRead == term->InputWrite) Threads_Yield();
- while(term->InputRead != term->InputWrite)
- {
- ((Uint32*)Buffer)[pos] = ((Uint32*)term->InputBuffer)[term->InputRead];
- pos ++;
- term->InputRead ++;
- term->InputRead %= MAX_INPUT_CHARS32;
- }
+ ((Uint32*)Buffer)[pos] = ((Uint32*)term->InputBuffer)[term->InputRead];
+ pos ++;
+ term->InputRead ++;
+ term->InputRead %= MAX_INPUT_CHARS32;
}
break;
}
{
tVTerm *term = &gVT_Terminals[ Node->Inode ];
- //ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
-
// Write
switch( term->Mode )
{
case TERM_MODE_TEXT:
VT_int_PutString(term, Buffer, Length);
break;
+ case TERM_MODE_FB:
+ if( giVT_RealWidth > term->Width || giVT_RealHeight > term->Height )
+ {
+ #if 0
+ int x, y, h;
+ x = Offset/4; y = x / term->Width; x %= term->Width;
+ w = Length/4+x; h = w / term->Width; w %= term->Width;
+ while(h--)
+ {
+ VFS_WriteAt( giVT_OutputDevHandle,
+ (x+y*term->RealWidth)*4,
+ term->Width * 4,
+ Buffer
+ );
+ Buffer = (void*)( (Uint)Buffer + term->Width*term->Height*4 );
+ }
+ #endif
+ return 0;
+ }
+ else {
+ return VFS_WriteAt( giVT_OutputDevHandle, Offset, Length, Buffer );
+ }
}
- //LEAVE('i', 0);
return 0;
}
{
int *iData = Data;
tVTerm *term = Node->ImplPtr;
+ ENTER("pNode iId pData", Node, Id, Data);
+
+ if(Id >= DRV_IOCTL_LOOKUP) {
+ if( Threads_GetUID() != 0 ) return -1;
+ }
+
switch(Id)
{
- case DRV_IOCTL_TYPE: return DRV_TYPE_TERMINAL;
- case DRV_IOCTL_IDENT: memcpy(Data, "VT\0\0", 4); return 0;
- case DRV_IOCTL_VERSION: return VERSION;
- case DRV_IOCTL_LOOKUP: return 0;
+ case DRV_IOCTL_TYPE:
+ LEAVE('i', DRV_TYPE_TERMINAL);
+ return DRV_TYPE_TERMINAL;
+ case DRV_IOCTL_IDENT:
+ memcpy(Data, "VT\0\0", 4);
+ LEAVE('i', 0);
+ return 0;
+ case DRV_IOCTL_VERSION:
+ LEAVE('x', VERSION);
+ return VERSION;
+ case DRV_IOCTL_LOOKUP:
+ LEAVE('i', 0);
+ return 0;
// Get/Set the mode (and apply any changes)
case TERM_IOCTL_MODETYPE:
- if(Data == NULL) return term->Mode;
- term->Mode = *iData;
- // Update the screen dimensions
- if(giVT_CurrentTerminal == Node->Inode)
- VT_SetTerminal( giVT_CurrentTerminal );
- break;
+ if(Data != NULL)
+ {
+ if(term->Mode != *iData)
+ VT_int_ChangeMode(term, *iData);
+
+ // Update the screen dimensions
+ if(giVT_CurrentTerminal == Node->Inode)
+ VT_SetTerminal( giVT_CurrentTerminal );
+ }
+ LEAVE('i', term->Mode);
+ return term->Mode;
// Get/set the terminal width
case TERM_IOCTL_WIDTH:
- if(Data == NULL) return term->Width;
- term->Width = *iData;
- break;
+ if(Data != NULL) term->Width = *iData;
+ Log("VT_Terminal_IOCtl - RETURN term->Width = %i", term->Width);
+ LEAVE('i', term->Width);
+ return term->Width;
// Get/set the terminal height
case TERM_IOCTL_HEIGHT:
- if(Data == NULL) return term->Height;
- term->Height = *iData;
- break;
+ if(Data != NULL) term->Height = *iData;
+ Log("VT_Terminal_IOCtl - RETURN term->Height = %i", term->Height);
+ LEAVE('i', term->Height);
+ return term->Height;
- default:
- return -1;
+ case TERM_IOCTL_FORCESHOW:
+ VT_SetTerminal( Node->Inode );
+ LEAVE('i', 1);
+ return 1;
}
- return 0;
+ LEAVE('i', -1);
+ return -1;
+}
+
+void VT_SetResolution(int IsTextMode, int Width, int Height)
+{
+ tVideo_IOCtl_Mode mode = {0};
+ int tmp;
+
+ // Create the video mode
+ mode.width = Width;
+ mode.height = Height;
+ mode.bpp = 32;
+ mode.flags = 0;
+
+ // Set video mode
+ VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_FINDMODE, &mode );
+ tmp = mode.id;
+ giVT_RealWidth = mode.width;
+ giVT_RealHeight = mode.height;
+ VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_GETSETMODE, &tmp );
+
+ tmp = IsTextMode ? VIDEO_BUFFMT_TEXT : VIDEO_BUFFMT_FRAMEBUFFER;
+ VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp );
}
/**
* \brief Set the current terminal
*/
void VT_SetTerminal(int ID)
-{
- tVideo_IOCtl_Mode mode = {0};
- int modeNum;
+{
+ // Update current terminal ID
+ Log_Log("VTerm", "Changed terminal from %i to %i", giVT_CurrentTerminal, ID);
+ giVT_CurrentTerminal = ID;
+ gpVT_CurTerm = &gVT_Terminals[ID];
- // Create the video mode
- mode.width = gVT_Terminals[ ID ].Width;
- mode.height = gVT_Terminals[ ID ].Height;
- // - Text Mode
- if(gVT_Terminals[ ID ].Mode == TERM_MODE_TEXT) {
- mode.bpp = 12;
- mode.flags = VIDEO_FLAG_TEXT;
- }
- // - Framebuffer or 3D
- else {
- mode.bpp = 32;
- mode.flags = 0;
+ // Update cursor
+ if( !(gpVT_CurTerm->Flags & VT_FLAG_HIDECSR) )
+ {
+ tVideo_IOCtl_Pos pos;
+ pos.x = gpVT_CurTerm->WritePos % gpVT_CurTerm->Width;
+ pos.y = gpVT_CurTerm->WritePos / gpVT_CurTerm->Width;
+ VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
}
- // Set video mode
- VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_FINDMODE, &mode );
- modeNum = mode.id;
- gVT_Terminals[ ID ].RealWidth = mode.width;
- gVT_Terminals[ ID ].RealHeight = mode.height;
- VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_SETMODE, &modeNum );
+ if( gpVT_CurTerm->Mode == TERM_MODE_TEXT )
+ VT_SetResolution( 1, gpVT_CurTerm->Width*giVT_CharWidth, gpVT_CurTerm->Height*giVT_CharHeight );
+ else
+ VT_SetResolution( 0, gpVT_CurTerm->Width, gpVT_CurTerm->Height );
+
+ // Update the screen
+ VT_int_UpdateScreen( &gVT_Terminals[ ID ], 1 );
}
/**
* \fn void VT_KBCallBack(Uint32 Codepoint)
* \brief Called on keyboard interrupt
+ * \param Codepoint Pseudo-UTF32 character
+ *
+ * Handles a key press and sends the key code to the user's buffer.
+ * If the code creates a kernel-magic sequence, it is not passed to the
+ * user and is handled in-kernel.
*/
void VT_KBCallBack(Uint32 Codepoint)
{
Codepoint &= 0x7FFFFFFF;
switch(Codepoint)
{
+ #if !USE_CTRL_ALT
+ case KEY_RSHIFT: gbVT_CtrlDown = 0; break;
+ case KEY_LSHIFT: gbVT_AltDown = 0; break;
+ #else
case KEY_LALT:
case KEY_RALT:
gbVT_AltDown = 0;
case KEY_RCTRL:
gbVT_CtrlDown = 0;
break;
+ #endif
}
return;
}
switch(Codepoint)
{
+ #if !USE_CTRL_ALT
+ case KEY_RSHIFT: gbVT_CtrlDown = 1; break;
+ case KEY_LSHIFT: gbVT_AltDown = 1; break;
+ #else
case KEY_LALT:
case KEY_RALT:
gbVT_AltDown = 1;
case KEY_RCTRL:
gbVT_CtrlDown = 1;
break;
+ #endif
default:
+ #if USE_CTRL_ALT
if(!gbVT_AltDown || !gbVT_CtrlDown)
break;
+ #endif
switch(Codepoint)
{
- case KEY_F1:
- giVT_CurrentTerminal = 0;
- break;
- case KEY_F2:
- giVT_CurrentTerminal = 0;
- break;
- case KEY_F3:
- giVT_CurrentTerminal = 0;
- break;
- case KEY_F4:
- giVT_CurrentTerminal = 0;
- break;
+ case KEY_F1: VT_SetTerminal(0); return;
+ case KEY_F2: VT_SetTerminal(1); return;
+ case KEY_F3: VT_SetTerminal(2); return;
+ case KEY_F4: VT_SetTerminal(3); return;
+ case KEY_F5: VT_SetTerminal(4); return;
+ case KEY_F6: VT_SetTerminal(5); return;
+ case KEY_F7: VT_SetTerminal(6); return;
+ case KEY_F8: VT_SetTerminal(7); return;
+ case KEY_F9: VT_SetTerminal(8); return;
+ case KEY_F10: VT_SetTerminal(9); return;
+ case KEY_F11: VT_SetTerminal(10); return;
+ case KEY_F12: VT_SetTerminal(11); return;
+ case KEY_PGUP:
+ if( gpVT_CurTerm->ViewPos > gpVT_CurTerm->Width )
+ gpVT_CurTerm->ViewPos -= gpVT_CurTerm->Width;
+ else
+ gpVT_CurTerm->ViewPos = 0;
+ return;
+ case KEY_PGDOWN:
+ if( gpVT_CurTerm->ViewPos < gpVT_CurTerm->Width*(gpVT_CurTerm->Height*(VT_SCROLLBACK-1)) )
+ gpVT_CurTerm->ViewPos += gpVT_CurTerm->Width;
+ else
+ gpVT_CurTerm->ViewPos = gpVT_CurTerm->Width*(gpVT_CurTerm->Height*(VT_SCROLLBACK-1));
+ return;
}
- return;
}
// Encode key
if(term->Mode == TERM_MODE_TEXT)
{
Uint8 buf[6] = {0};
- int len;
+ int len = 0;
// Ignore Modifer Keys
if(Codepoint > KEY_MODIFIERS) return;
switch(Codepoint)
{
case KEY_LEFT:
- buf[0] = '\x1B'; buf[1] = '[';
- buf[2] = 'D'; len = 3;
+ buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'D';
+ len = 3;
break;
case KEY_RIGHT:
- buf[0] = '\x1B'; buf[1] = '[';
- buf[2] = 'C'; len = 3;
+ buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'C';
+ len = 3;
+ break;
+ case KEY_UP:
+ buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'A';
+ len = 3;
+ break;
+ case KEY_DOWN:
+ buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'B';
+ len = 3;
+ break;
+
+ case KEY_PGUP:
+ buf[0] = '\x1B'; buf[1] = '['; buf[2] = '5'; // Some overline also
+ //len = 4; // Commented out until I'm sure
+ break;
+ case KEY_PGDOWN:
+ len = 0;
break;
+
+ // Attempt to encode in UTF-8
default:
len = WriteUTF8( buf, Codepoint );
- //Log("Codepoint = 0x%x", Codepoint);
+ if(len == 0) {
+ Warning("Codepoint (%x) is unrepresentable in UTF-8", Codepoint);
+ }
break;
}
- //Log("len = %i, buf = %s", len, buf);
+ if(len == 0) {
+ // Unprintable / Don't Pass
+ return;
+ }
// Write
if( MAX_INPUT_CHARS8 - term->InputWrite >= len )
}
// Update cursor
- if( !(Term->Flags & VT_FLAG_HIDECSR) )
+ if( Term == gpVT_CurTerm && !(Term->Flags & VT_FLAG_HIDECSR) )
{
tVideo_IOCtl_Pos pos;
pos.x = Term->WritePos % Term->Width;
Term->WritePos -= Term->WritePos % Term->Width;
else
Term->WritePos -= tmp;
- Log("Left by %i", tmp);
break;
// Right
{
int i;
//ENTER("pTerm xCh", Term, Ch);
- //LOG("Term = {WritePos:%i, ViewPos:%i}\n", Term->WritePos, Term->ViewPos);
+ //LOG("Term = {WritePos:%i, ViewPos:%i}", Term->WritePos, Term->ViewPos);
switch(Ch)
{
memcpy(
Term->Text,
&Term->Text[Term->Width],
- Term->Width*(Term->Height-1)*VT_SCROLLBACK*sizeof(tVT_Char)
+ (Term->Width*Term->Height*VT_SCROLLBACK-Term->Width)*sizeof(tVT_Char)
);
// Clear last row
Term->Text[ base + i ].Colour = Term->CurColour;
}
+ //LOG("Scrolled buffer");
VT_int_UpdateScreen( Term, 1 );
}
else if(Term->WritePos > Term->Width*Term->Height+Term->ViewPos)
{
Term->ViewPos += Term->Width;
+ //LOG("Scrolled screen");
VT_int_UpdateScreen( Term, 1 );
}
else
*/
void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
{
- if(UpdateAll) {
+ // Only update if this is the current terminal
+ if( Term != &gVT_Terminals[giVT_CurrentTerminal] ) return;
+
+ if( Term->Mode == TERM_MODE_TEXT )
+ {
+ if(UpdateAll) {
+ //LOG("UpdateAll = 1");
+ //LOG("VFS_WriteAt(0x%x, 0, %i*sizeof(tVT_Char), &Term->Text[%i])",
+ // giVT_OutputDevHandle, Term->Width*Term->Height, Term->ViewPos);
+ VFS_WriteAt(
+ giVT_OutputDevHandle,
+ 0,
+ Term->Width*Term->Height*sizeof(tVT_Char),
+ &Term->Text[Term->ViewPos]
+ );
+ } else {
+ int pos = Term->WritePos - Term->WritePos % Term->Width;
+ //LOG("UpdateAll = 0");
+ //LOG("VFS_WriteAt(0x%x, %i*sizeof(tVT_Char), %i*sizeof(tVT_Char), &Term->Text[%i])",
+ // giVT_OutputDevHandle, (pos - Term->ViewPos), Term->Width, pos);
+ VFS_WriteAt(
+ giVT_OutputDevHandle,
+ (pos - Term->ViewPos)*sizeof(tVT_Char),
+ Term->Width*sizeof(tVT_Char),
+ &Term->Text[pos]
+ );
+ }
+ }
+ else
+ {
VFS_WriteAt(
giVT_OutputDevHandle,
0,
- Term->Width*Term->Height*sizeof(tVT_Char),
- &Term->Text[Term->ViewPos]
- );
- } else {
- int pos = Term->WritePos - Term->WritePos % Term->Width;
- VFS_WriteAt(
- giVT_OutputDevHandle,
- (pos - Term->ViewPos)*sizeof(tVT_Char),
- Term->Width*sizeof(tVT_Char),
- &Term->Text[pos]
+ Term->Width*Term->Height*sizeof(Uint32),
+ &Term->Buffer
);
}
}
+/**
+ * \fn void VT_int_ChangeMode(tVTerm *Term, int NewMode)
+ * \brief Change the mode of a VTerm
+ */
+void VT_int_ChangeMode(tVTerm *Term, int NewMode)
+{
+ switch(NewMode)
+ {
+ case TERM_MODE_TEXT:
+ free(Term->Buffer);
+ Term->Text = calloc( Term->Width*Term->Height*VT_SCROLLBACK, sizeof(tVT_Char) );
+ break;
+ case TERM_MODE_FB:
+ free(Term->Text);
+ Term->Buffer = calloc( Term->Width*Term->Height, sizeof(Uint32) );
+ break;
+ //case TERM_MODE_OPENGL:
+ // return;
+ }
+
+ Term->Mode = NewMode;
+}
+
// ---
// Font Render
// ---
Uint8 *font;
Uint32 *buf = Buffer;
int x, y;
+
font = VT_Font_GetChar(Codepoint);
for(y = 0; y < FONT_HEIGHT; y ++)
{
for(x = 0; x < FONT_WIDTH; x ++)
{
- if(*font & (1 << (FONT_WIDTH-x)))
+ if(*font & (1 << (FONT_WIDTH-x-1)))
buf[x] = FGC;
else
buf[x] = BGC;