Merge branch 'master' of git://git.ucc.asn.au/tpg/acess2
[tpg/acess2.git] / KernelLand / Kernel / drv / vterm.c
index 2ae38ee..fc6dea2 100644 (file)
@@ -1,5 +1,9 @@
 /*
- * Acess2 Virtual Terminal Driver
+ * Acess2 Kernel
+ * - By John Hodge (thePowersGang)
+ *
+ * drv/vterm.c
+ * - Virtual Terminal - Initialisation and VFS Interface
  */
 #define DEBUG  0
 #include "vterm.h"
 //#define DEFAULT_OUTPUT       "BochsGA"
 #define DEFAULT_OUTPUT "Vesa"
 #define FALLBACK_OUTPUT        "x86_VGAText"
-#define DEFAULT_INPUT  "PS2Keyboard"
+#define DEFAULT_INPUT  "Keyboard"
 #define        DEFAULT_WIDTH   640
 #define        DEFAULT_HEIGHT  480
-#define DEFAULT_SCROLLBACK     2       // 2 Screens of text + current screen
+#define DEFAULT_SCROLLBACK     4       // 2 Screens of text + current screen
 //#define DEFAULT_SCROLLBACK   0
 
 // === TYPES ===
@@ -30,17 +34,20 @@ extern void Debug_SetKTerminal(const char *File);
 
 // === PROTOTYPES ===
  int   VT_Install(char **Arguments);
-char   *VT_ReadDir(tVFS_Node *Node, int Pos);
+ int   VT_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX]);
 tVFS_Node      *VT_FindDir(tVFS_Node *Node, const 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, const void *Buffer);
+size_t VT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
+size_t VT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer);
  int   VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data);
+void   VT_Terminal_Reference(tVFS_Node *Node);
+void   VT_Terminal_Close(tVFS_Node *Node);
+//void VT_SetTerminal(int Term);
 
 // === CONSTANTS ===
 
 // === GLOBALS ===
-MODULE_DEFINE(0, VERSION, VTerm, VT_Install, NULL, DEFAULT_INPUT, NULL);
+MODULE_DEFINE(0, VERSION, VTerm, VT_Install, NULL, NULL);
 tVFS_NodeType  gVT_RootNodeType = {
        .TypeName = "VTerm Root",
        .ReadDir = VT_ReadDir,
@@ -136,6 +143,9 @@ int VT_Install(char **Arguments)
        
        if(!gsVT_InputDevice)   gsVT_InputDevice = (char*)DEFAULT_INPUT;
        else if( Module_EnsureLoaded( gsVT_InputDevice ) )      gsVT_InputDevice = (char*)DEFAULT_INPUT;
+       if( Module_EnsureLoaded( gsVT_InputDevice ) ) {
+               Log_Error("VTerm", "Fallback input '%s' is not avaliable, input will not be avaliable", DEFAULT_INPUT);
+       }
        
        // Create device paths
        {
@@ -157,7 +167,9 @@ int VT_Install(char **Arguments)
        VT_InitOutput();
        VT_InitInput();
        
+       
        // Create Nodes
+       Log_Debug("VTerm", "Initialising nodes (and creating buffers)");
        for( i = 0; i < NUM_VTS; i++ )
        {
                gVT_Terminals[i].Mode = TERM_MODE_TEXT;
@@ -188,6 +200,7 @@ int VT_Install(char **Arguments)
        DevFS_AddDevice( &gVT_DrvInfo );
        
        // Set kernel output to VT0
+       Log_Debug("VTerm", "Setting kernel output to VT#0");
        Debug_SetKTerminal("/Devices/VTerm/0");
        
        return MODULE_ERR_OK;
@@ -216,7 +229,7 @@ void VT_SetResolution(int Width, int Height)
        if( Width != mode.width || Height != mode.height )
        {
                Log_Warning("VTerm",
-                       "Selected resolution (%ix%i is not supported) by the device, using (%ix%i)",
+                       "Selected resolution (%ix%i) is not supported by the device, using (%ix%i)",
                        giVT_RealWidth, giVT_RealHeight,
                        mode.width, mode.height
                        );
@@ -255,11 +268,12 @@ void VT_SetResolution(int Width, int Height)
  * \fn char *VT_ReadDir(tVFS_Node *Node, int Pos)
  * \brief Read from the VTerm Directory
  */
-char *VT_ReadDir(tVFS_Node *Node, int Pos)
+int VT_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
 {
-       if(Pos < 0)     return NULL;
-       if(Pos >= NUM_VTS)      return NULL;
-       return strdup( gVT_Terminals[Pos].Name );
+       if(Pos < 0)     return -EINVAL;
+       if(Pos >= NUM_VTS)      return -EINVAL;
+       strncpy(Dest, gVT_Terminals[Pos].Name, FILENAME_MAX);
+       return 0;
 }
 
 /**
@@ -337,10 +351,9 @@ int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
 /**
  * \brief Read from a virtual terminal
  */
-Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
+size_t VT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
 {
-        int    pos = 0;
-        int    avail;
+        int    pos, avail;
        tVTerm  *term = &gVT_Terminals[ Node->Inode ];
        Uint32  *codepoint_buf = Buffer;
        Uint32  *codepoint_in;
@@ -359,9 +372,10 @@ Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
                avail = term->InputWrite - term->InputRead;
                if(avail < 0)
                        avail += MAX_INPUT_CHARS8;
-               if(avail > Length - pos)
-                       avail = Length - pos;
+               if(avail > Length)
+                       avail = Length;
                
+               pos = 0;
                while( avail -- )
                {
                        ((char*)Buffer)[pos] = term->InputBuffer[term->InputRead];
@@ -381,12 +395,13 @@ Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
                if(avail < 0)
                        avail += MAX_INPUT_CHARS32;
                Length /= 4;
-               if(avail > Length - pos)
-                       avail = Length - pos;
+               if(avail > Length)
+                       avail = Length;
                
                codepoint_in = (void*)term->InputBuffer;
                codepoint_buf = Buffer;
                
+               pos = 0;
                while( avail -- )
                {
                        codepoint_buf[pos] = codepoint_in[term->InputRead];
@@ -413,10 +428,9 @@ Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
 }
 
 /**
- * \fn Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, const void *Buffer)
  * \brief Write to a virtual terminal
  */
-Uint64 VT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, const void *Buffer)
+size_t VT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer)
 {
        tVTerm  *term = &gVT_Terminals[ Node->Inode ];
         int    size;
@@ -697,6 +711,16 @@ int VT_Terminal_IOCtl(tVFS_Node *Node, int Id, void *Data)
        return -1;
 }
 
+void VT_Terminal_Reference(tVFS_Node *Node)
+{
+       // Append PID to list
+}
+
+void VT_Terminal_Close(tVFS_Node *Node)
+{
+       // Remove PID from list
+}
+
 /**
  * \fn void VT_SetTerminal(int ID)
  * \brief Set the current terminal
@@ -735,6 +759,8 @@ void VT_SetTerminal(int ID)
        giVT_CurrentTerminal = ID;
        gpVT_CurTerm = &gVT_Terminals[ID];
        
+       LOG("Attempting VT_SetMode");
+       
        if( gpVT_CurTerm->Mode == TERM_MODE_TEXT )
        {
                VT_SetMode( VIDEO_BUFFMT_TEXT );
@@ -746,7 +772,9 @@ void VT_SetTerminal(int ID)
                        VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSORBITMAP, gpVT_CurTerm->VideoCursor);
                VT_SetMode( VIDEO_BUFFMT_FRAMEBUFFER );
        }
-       
+
+       LOG("Mode set");        
+
        if(gpVT_CurTerm->Buffer)
        {
                // TODO: Handle non equal sized
@@ -756,9 +784,11 @@ void VT_SetTerminal(int ID)
                        gpVT_CurTerm->Width*gpVT_CurTerm->Height*sizeof(Uint32),
                        gpVT_CurTerm->Buffer
                        );
+               LOG("Updated screen contents");
        }
        
        VT_int_UpdateCursor(gpVT_CurTerm, 1);
        // Update the screen
        VT_int_UpdateScreen(gpVT_CurTerm, 1);
+       LOG("done");
 }

UCC git Repository :: git.ucc.asn.au