/*
* 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_keyboard.h>
+#include <tpl_drv_terminal.h>
+#include <errno.h>
+
+#define USE_CTRL_ALT 0
// === CONSTANTS ===
-#define NUM_VTS 4
-#define MAX_INPUT_CHARS 64
-#define VT_SCROLLBACK 4 // 4 Screens of text
-#define DEFAULT_OUTPUT "/Devices/VGA"
-#define DEFAULT_INPUT "/Devices/PS2Keyboard"
+#define VERSION ((0<<8)|(50))
+
+#define NUM_VTS 8
+#define MAX_INPUT_CHARS32 64
+#define MAX_INPUT_CHARS8 (MAX_INPUT_CHARS32*4)
+#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|(VT_COL_WHITE<<16))
-
-enum eVT_Modes {
- VT_MODE_TEXT8, // UTF-8 Text Mode (VT100 Emulation)
- VT_MODE_TEXT32, // UTF-32 Text Mode (Acess Native)
- VT_MODE_8BPP, // 256 Colour Mode
- VT_MODE_16BPP, // 16 bit Colour Mode
- VT_MODE_24BPP, // 24 bit Colour Mode
- VT_MODE_32BPP, // 32 bit Colour Mode
- NUM_VT_MODES
+#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)
+ VT_INMODE_TEXT32, // UTF-32 Text Mode (Acess Native)
+ NUM_VT_INMODES
};
// === TYPES ===
typedef struct {
- int Mode;
- int Width, Height;
- int ViewPos, WritePos;
- Uint32 CurColour;
- char Name[2];
- int InputRead;
- int InputWrite;
- Uint32 InputBuffer[MAX_INPUT_CHARS];
+ int Mode; //!< Current Mode (see ::eTplTerminal_Modes)
+ int Flags; //!< Flags (see VT_FLAG_*)
+ short Width; //!< Virtual Width
+ short Height; //!< Virtual Height
+
+ int ViewPos; //!< View Buffer Offset (Text Only)
+ int WritePos; //!< Write Buffer Offset (Text Only)
+ Uint32 CurColour; //!< Current Text Colour
+
+ int InputRead; //!< Input buffer read position
+ int InputWrite; //!< Input buffer write position
+ char InputBuffer[MAX_INPUT_CHARS8];
union {
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_IOCtl(tVFS_Node *Node, int Id, void *Data);
+ 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[] = {
- VT_COL_BLACK, 0, 0, 0, 0, 0, 0, VT_COL_LTGREY,
- VT_COL_GREY, 0, 0, 0, 0, 0, 0, VT_COL_WHITE
+ VT_COL_BLACK, 0x700, 0x070, 0x770, 0x007, 0x707, 0x077, 0xAAA,
+ VT_COL_GREY, 0xF00, 0x0F0, 0xFF0, 0x00F, 0xF0F, 0x0FF, VT_COL_WHITE
};
// === GLOBALS ===
-MODULE_DEFINE(0, 0x0032, VTerm, VT_Install, NULL, NULL);
+MODULE_DEFINE(0, VERSION, VTerm, VT_Install, NULL, DEFAULT_OUTPUT, DEFAULT_INPUT, NULL);
tDevFS_Driver gVT_DrvInfo = {
NULL, "VTerm",
{
.Flags = VFS_FFLAG_DIRECTORY,
+ .Size = NUM_VTS,
.Inode = -1,
.NumACLs = 0,
.ReadDir = VT_ReadDir,
- .FindDir = VT_FindDir
+ .FindDir = VT_FindDir,
+ .IOCtl = VT_Root_IOCtl
}
};
+// --- 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;
int giVT_OutputDevHandle = -2;
int giVT_InputDevHandle = -2;
- int giVT_CurrentTerminal = 0;
+// --- Key States --- (Used for VT Switching/Magic Combos)
+ int gbVT_CtrlDown = 0;
+ int gbVT_AltDown = 0;
+ int gbVT_SysrqDown = 0;
// === CODE ===
/**
* \fn int VT_Install(char **Arguments)
+ * \brief Installs the Virtual Terminal Driver
*/
int VT_Install(char **Arguments)
{
strcpy(gsVT_InputDevice, args[1]);
args ++;
break;
-
}
}
}
// Apply Defaults
- if(!gsVT_OutputDevice) gsVT_OutputDevice = DEFAULT_OUTPUT;
- if(!gsVT_InputDevice) gsVT_InputDevice = DEFAULT_INPUT;
+ 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].Mode = VT_MODE_TEXT8;
+ gVT_Terminals[i].Mode = TERM_MODE_TEXT;
+ gVT_Terminals[i].Flags = 0;
gVT_Terminals[i].Width = DEFAULT_WIDTH;
gVT_Terminals[i].Height = DEFAULT_HEIGHT;
gVT_Terminals[i].CurColour = DEFAULT_COLOUR;
gVT_Terminals[i].WritePos = 0;
gVT_Terminals[i].ViewPos = 0;
- gVT_Terminals[i].Buffer = malloc( DEFAULT_WIDTH*DEFAULT_HEIGHT*sizeof(tVT_Char) );
- memset( gVT_Terminals[i].Buffer, 0, DEFAULT_WIDTH*DEFAULT_HEIGHT*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';
gVT_Terminals[i].Node.Inode = i;
+ gVT_Terminals[i].Node.ImplPtr = &gVT_Terminals[i];
gVT_Terminals[i].Node.NumACLs = 0; // Only root can open virtual terminals
gVT_Terminals[i].Node.Read = VT_Read;
gVT_Terminals[i].Node.Write = VT_Write;
- gVT_Terminals[i].Node.IOCtl = VT_IOCtl;
+ gVT_Terminals[i].Node.IOCtl = VT_Terminal_IOCtl;
}
// 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);
}
{
if(Pos < 0) return NULL;
if(Pos >= NUM_VTS) return NULL;
- return gVT_Terminals[Pos].Name;
+ return strdup( gVT_Terminals[Pos].Name );
}
/**
{
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;
}
+/**
+ * \fn int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
+ * \brief Control the VTerm Driver
+ */
+int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
+{
+ int len;
+ switch(Id)
+ {
+ case DRV_IOCTL_TYPE: return DRV_TYPE_MISC;
+ 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 4: // Get Video Driver
+ if(Data) strcpy(Data, gsVT_OutputDevice);
+ return strlen(gsVT_OutputDevice);
+
+ case 5: // Set Video Driver
+ if(!Data) return -EINVAL;
+ if(Threads_GetUID() != 0) return -EACCES;
+
+ len = strlen(Data);
+
+ free(gsVT_OutputDevice);
+
+ gsVT_OutputDevice = malloc(len+1);
+ strcpy(gsVT_OutputDevice, Data);
+
+ VFS_Close(giVT_OutputDevHandle);
+ giVT_OutputDevHandle = -1;
+
+ VT_InitOutput();
+ return 1;
+ }
+ return 0;
+}
+
/**
* \fn Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
* \brief Read from a virtual terminal
// Check current mode
switch(term->Mode)
{
- case VT_MODE_TEXT8:
+ case TERM_MODE_TEXT:
while(pos < Length)
{
- while(term->InputRead == term->InputWrite) Proc_Yield();
- while(term->InputRead != term->InputWrite)
- {
- pos += WriteUTF8(Buffer+pos, term->InputBuffer[term->InputRead]);
- term->InputRead ++;
- }
+ while(term->InputRead == term->InputWrite) Threads_Yield();
+
+ ((char*)Buffer)[pos] = term->InputBuffer[term->InputRead];
+ pos ++;
+ term->InputRead ++;
+ term->InputRead %= MAX_INPUT_CHARS8;
}
break;
- case VT_MODE_TEXT32:
+ //case TERM_MODE_FB:
+ default:
while(pos < Length)
{
- while(term->InputRead == term->InputWrite) Proc_Yield();
- while(term->InputRead != term->InputWrite)
- {
- ((Uint32*)Buffer)[pos] = term->InputBuffer[term->InputRead];
- pos ++;
- term->InputRead ++;
- }
+ while(term->InputRead == term->InputWrite) Threads_Yield();
+ ((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 VT_MODE_TEXT8:
+ case TERM_MODE_TEXT:
VT_int_PutString(term, Buffer, Length);
break;
- case VT_MODE_TEXT32:
- //VT_int_PutString32(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;
}
/**
- * \fn int VT_IOCtl(tVFS_Node *Node, int Id, void *Data)
+ * \fn int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
* \brief Call an IO Control on a virtual terminal
*/
-int VT_IOCtl(tVFS_Node *Node, int Id, void *Data)
+int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
{
- 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:
+ 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)
+ {
+ 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) 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) term->Height = *iData;
+ Log("VT_Terminal_IOCtl - RETURN term->Height = %i", term->Height);
+ LEAVE('i', term->Height);
+ return term->Height;
+
+ case TERM_IOCTL_FORCESHOW:
+ VT_SetTerminal( Node->Inode );
+ LEAVE('i', 1);
+ return 1;
+ }
+ 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 );
+}
+
+/**
+ * \fn void VT_SetTerminal(int ID)
+ * \brief Set the current terminal
+ */
+void VT_SetTerminal(int ID)
+{
+ // Update current terminal ID
+ Log_Log("VTerm", "Changed terminal from %i to %i", giVT_CurrentTerminal, ID);
+ giVT_CurrentTerminal = ID;
+ gpVT_CurTerm = &gVT_Terminals[ID];
+
+ // 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);
+ }
+
+ 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)
{
tVTerm *term = &gVT_Terminals[giVT_CurrentTerminal];
- term->InputBuffer[ term->InputWrite ] = Codepoint;
- term->InputWrite ++;
- term->InputWrite %= MAX_INPUT_CHARS;
- if(term->InputRead == term->InputWrite) {
- term->InputRead ++;
- term->InputRead %= MAX_INPUT_CHARS;
+ // How the hell did we get a Codepoint of zero?
+ if(Codepoint == 0) return;
+
+ // Key Up
+ if( Codepoint & 0x80000000 )
+ {
+ 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;
+ break;
+ case KEY_LCTRL:
+ 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;
+ break;
+ case KEY_LCTRL:
+ 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: 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;
+ }
+ }
+
+ // Encode key
+ if(term->Mode == TERM_MODE_TEXT)
+ {
+ Uint8 buf[6] = {0};
+ int len = 0;
+
+ // Ignore Modifer Keys
+ if(Codepoint > KEY_MODIFIERS) return;
+
+ // Get UTF-8/ANSI Encoding
+ switch(Codepoint)
+ {
+ case KEY_LEFT:
+ buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'D';
+ len = 3;
+ break;
+ case KEY_RIGHT:
+ 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 );
+ if(len == 0) {
+ Warning("Codepoint (%x) is unrepresentable in UTF-8", Codepoint);
+ }
+ break;
+ }
+
+ if(len == 0) {
+ // Unprintable / Don't Pass
+ return;
+ }
+
+ // Write
+ if( MAX_INPUT_CHARS8 - term->InputWrite >= len )
+ memcpy( &term->InputBuffer[term->InputWrite], buf, len );
+ else {
+ memcpy( &term->InputBuffer[term->InputWrite], buf, MAX_INPUT_CHARS8 - term->InputWrite );
+ memcpy( &term->InputBuffer[0], buf, len - (MAX_INPUT_CHARS8 - term->InputWrite) );
+ }
+ // Roll the buffer over
+ term->InputWrite += len;
+ term->InputWrite %= MAX_INPUT_CHARS8;
+ if( (term->InputWrite - term->InputRead + MAX_INPUT_CHARS8)%MAX_INPUT_CHARS8 < len ) {
+ term->InputRead = term->InputWrite + 1;
+ term->InputRead %= MAX_INPUT_CHARS8;
+ }
+
+ }
+ else
+ {
+ // Encode the raw UTF-32 Key
+ ((Uint32*)term->InputBuffer)[ term->InputWrite ] = Codepoint;
+ term->InputWrite ++;
+ term->InputWrite %= MAX_INPUT_CHARS32;
+ if(term->InputRead == term->InputWrite) {
+ term->InputRead ++;
+ term->InputRead %= MAX_INPUT_CHARS32;
+ }
}
}
{
if( Buffer[i] == 0x1B ) // Escape Sequence
{
+ i ++;
i += VT_int_ParseEscape(Term, (char*)&Buffer[i]);
continue;
}
VT_int_PutChar(Term, val);
}
}
+
+ // Update cursor
+ if( Term == gpVT_CurTerm && !(Term->Flags & VT_FLAG_HIDECSR) )
+ {
+ tVideo_IOCtl_Pos pos;
+ pos.x = Term->WritePos % Term->Width;
+ pos.y = Term->WritePos / Term->Width;
+ VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &pos);
+ }
+}
+
+/**
+ * \fn void VT_int_ClearLine(tVTerm *Term, int Num)
+ * \brief Clears a line in a virtual terminal
+ */
+void VT_int_ClearLine(tVTerm *Term, int Num)
+{
+ int i;
+ //ENTER("pTerm iNum", Term, Num);
+ for( i = Term->Width; i--; )
+ {
+ Term->Text[ Num*Term->Width + i ].Ch = 0;
+ Term->Text[ Num*Term->Width + i ].Colour = Term->CurColour;
+ }
+ //LEAVE('-');
}
/**
{
char c;
int argc = 0, j = 1;
+ int tmp;
int args[4] = {0,0,0,0};
switch(Buffer[0]) {
args[argc] += c-'0';
c = Buffer[++j];
}
- argc ++;
+ if( j != 1 ) argc ++;
} while(c == ';');
// Get string (what does this do?)
// Get Command
if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
{
- switch(c) {
- //Clear By Line
+ switch(c)
+ {
+ // Left
+ case 'D':
+ if(argc == 1) tmp = args[0];
+ else tmp = 1;
+
+ if( Term->WritePos-(tmp-1) % Term->Width == 0 )
+ Term->WritePos -= Term->WritePos % Term->Width;
+ else
+ Term->WritePos -= tmp;
+ break;
+
+ // Right
+ case 'C':
+ if(argc == 1) tmp = args[0];
+ else tmp = 1;
+ if( (Term->WritePos + tmp) % Term->Width == 0 ) {
+ Term->WritePos -= Term->WritePos % Term->Width;
+ Term->WritePos += Term->Width - 1;
+ } else
+ Term->WritePos += tmp;
+ break;
+
+ // Clear By Line
case 'J':
// Clear Screen
- if(args[0] == 2) {
- memset(Term->Text, 0, Term->Width*Term->Height*VT_SCROLLBACK*sizeof(tVT_Char));
+ switch(args[0])
+ {
+ case 2:
+ {
+ int i = Term->Height * VT_SCROLLBACK;
+ while( i-- ) VT_int_ClearLine(Term, i);
Term->WritePos = 0;
Term->ViewPos = 0;
+ VT_int_UpdateScreen(Term, 1);
+ }
+ break;
}
break;
// Set Font flags
{
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)
{
+ case '\0': return; // Ignore NULL byte
case '\n':
Term->WritePos += Term->Width;
case '\r':
case '\t':
do {
- Term->Text[ Term->WritePos ].Ch = '\t';
+ Term->Text[ Term->WritePos ].Ch = '\0';
Term->Text[ Term->WritePos ].Colour = Term->CurColour;
Term->WritePos ++;
} while(Term->WritePos & 7);
Term->WritePos --;
// Singe Character
- if(Term->Text[ Term->WritePos ].Ch != '\t') {
+ if(Term->Text[ Term->WritePos ].Ch != '\0') {
Term->Text[ Term->WritePos ].Ch = 0;
Term->Text[ Term->WritePos ].Colour = Term->CurColour;
break;
Term->Text[ Term->WritePos ].Ch = 0;
Term->Text[ Term->WritePos ].Colour = Term->CurColour;
Term->WritePos --;
- } while(Term->WritePos && i-- && Term->Text[ Term->WritePos ].Ch == '\t');
+ } while(Term->WritePos && i-- && Term->Text[ Term->WritePos ].Ch == '\0');
+ if(Term->Text[ Term->WritePos ].Ch != '\0')
+ Term->WritePos ++;
break;
default:
Term->WritePos ++;
break;
}
+
+ // Move Screen
if(Term->WritePos >= Term->Width*Term->Height*VT_SCROLLBACK)
{
int base, i;
base = Term->Width*(Term->Height*VT_SCROLLBACK-1);
// Scroll Back
- memcpy( Term->Text, &Term->Text[Term->Width], base*sizeof(tVT_Char) );
+ memcpy(
+ Term->Text,
+ &Term->Text[Term->Width],
+ (Term->Width*Term->Height*VT_SCROLLBACK-Term->Width)*sizeof(tVT_Char)
+ );
// Clear last row
for( i = 0; i < Term->Width; i ++ )
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
// ---
# include "vterm_font_8x16.h"
#endif
+// === PROTOTYPES ===
+Uint8 *VT_Font_GetChar(Uint32 Codepoint);
-int VT_Font_GetWidth(Uint32 Codepoint)
-{
- return FONT_WIDTH;
-}
-int VT_Font_GetHeight(Uint32 Codepoint)
+// === GLOBALS ===
+int giVT_CharWidth = FONT_WIDTH;
+int giVT_CharHeight = FONT_HEIGHT;
+
+// === CODE ===
+/**
+ * \fn void VT_Font_Render(Uint32 Codepoint, void *Buffer, int Pitch, Uint32 BGC, Uint32 FGC)
+ * \brief Render a font character
+ */
+void VT_Font_Render(Uint32 Codepoint, void *Buffer, int Pitch, Uint32 BGC, Uint32 FGC)
{
- return FONT_HEIGHT;
+ 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-1)))
+ buf[x] = FGC;
+ else
+ buf[x] = BGC;
+ }
+ buf += Pitch;
+ font ++;
+ }
}
-void VT_Font_Render(Uint32 Codepoint, void *Buffer, Uint32 BGC, Uint32 FGC)
+/**
+ * \fn Uint32 VT_Colour12to24(Uint16 Col12)
+ * \brief Converts a
+ */
+Uint32 VT_Colour12to24(Uint16 Col12)
{
-// Uint8 *font;
-
+ Uint32 ret;
+ int tmp;
+ tmp = Col12 & 0xF;
+ ret = (tmp << 0) | (tmp << 4);
+ tmp = (Col12 & 0xF0) >> 4;
+ ret |= (tmp << 8) | (tmp << 12);
+ tmp = (Col12 & 0xF00) >> 8;
+ ret |= (tmp << 16) | (tmp << 20);
+ return ret;
}
/**
return &VTermFont[index*FONT_HEIGHT];
}
+
+EXPORTAS(&giVT_CharWidth, giVT_CharWidth);
+EXPORTAS(&giVT_CharHeight, giVT_CharHeight);
+EXPORT(VT_Font_Render);
+EXPORT(VT_Colour12to24);