Kernel - Added 'Flags' param to VFS Read/Write/FindDir
[tpg/acess2.git] / KernelLand / Kernel / drv / vterm.c
index 1cc60be..01612dc 100644 (file)
@@ -34,11 +34,11 @@ extern void Debug_SetKTerminal(const char *File);
 
 // === PROTOTYPES ===
  int   VT_Install(char **Arguments);
-char   *VT_ReadDir(tVFS_Node *Node, int Pos);
-tVFS_Node      *VT_FindDir(tVFS_Node *Node, const char *Name);
+ int   VT_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX]);
+tVFS_Node      *VT_FindDir(tVFS_Node *Node, const char *Name, Uint Flags);
  int   VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data);
-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);
+size_t VT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags);
+size_t VT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags);
  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);
@@ -114,11 +114,11 @@ int VT_Install(char **Arguments)
                        
                        if( strcmp(opt, "Video") == 0 ) {
                                if( !gsVT_OutputDevice )
-                                       gsVT_OutputDevice = strdup(val);
+                                       gsVT_OutputDevice = val;
                        }
                        else if( strcmp(opt, "Input") == 0 ) {
                                if( !gsVT_InputDevice )
-                                       gsVT_InputDevice = strdup(val);
+                                       gsVT_InputDevice = val;
                        }
                        else if( strcmp(opt, "Width") == 0 ) {
                                giVT_RealWidth = atoi( val );
@@ -129,6 +129,9 @@ int VT_Install(char **Arguments)
                        else if( strcmp(opt, "Scrollback") == 0 ) {
                                giVT_Scrollback = atoi( val );
                        }
+                       else {
+                               Log_Notice("VTerm", "Unknown option '%s'", opt);
+                       }
                }
        }
        
@@ -196,7 +199,6 @@ int VT_Install(char **Arguments)
 //             Semaphore_Init(&gVT_Terminals[i].InputSemaphore, 0, MAX_INPUT_CHARS8, "VTerm", gVT_Terminals[i].Name);
        }
        
-       Log_Debug("VTerm", "Registering with DevFS");
        // Add to DevFS
        DevFS_AddDevice( &gVT_DrvInfo );
        
@@ -230,7 +232,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
                        );
@@ -269,11 +271,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;
 }
 
 /**
@@ -282,7 +285,7 @@ char *VT_ReadDir(tVFS_Node *Node, int Pos)
  * \param Node Root node
  * \param Name Name (number) of the terminal
  */
-tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name)
+tVFS_Node *VT_FindDir(tVFS_Node *Node, const char *Name, Uint Flags)
 {
         int    num;
        
@@ -351,13 +354,13 @@ int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
 /**
  * \brief Read from a virtual terminal
  */
-size_t VT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
+size_t VT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags)
 {
-        int    pos = 0;
-        int    avail;
+        int    pos, avail, rv;
        tVTerm  *term = &gVT_Terminals[ Node->Inode ];
        Uint32  *codepoint_buf = Buffer;
        Uint32  *codepoint_in;
+       tTime   timeout_zero = 0;
        
        Mutex_Acquire( &term->ReadingLock );
        
@@ -368,14 +371,20 @@ size_t VT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
        case TERM_MODE_TEXT:
                VT_int_UpdateCursor(term, 1);
        
-               VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "VT_Read (UTF-8)");
+               rv = VFS_SelectNode(Node, VFS_SELECT_READ,
+                       (Flags & VFS_IOFLAG_NOBLOCK ? &timeout_zero : NULL), "VT_Read (UTF-8)");
+               if(!rv) {
+                       errno = (Flags & VFS_IOFLAG_NOBLOCK) ? EWOULDBLOCK : EINTR;
+                       return -1;
+               }
                
                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];
@@ -389,18 +398,24 @@ size_t VT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
        //case TERM_MODE_FB:
        // Other - UCS-4
        default:
-               VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "VT_Read (UCS-4)");
+               rv = VFS_SelectNode(Node, VFS_SELECT_READ,
+                       (Flags & VFS_IOFLAG_NOBLOCK ? &timeout_zero : NULL), "VT_Read (UCS-4)");
+               if(!rv) {
+                       errno = (Flags & VFS_IOFLAG_NOBLOCK) ? EWOULDBLOCK : EINTR;
+                       return -1;
+               }
                
                avail = term->InputWrite - term->InputRead;
                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];
@@ -429,7 +444,7 @@ size_t VT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
 /**
  * \brief Write to a virtual terminal
  */
-size_t VT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer)
+size_t VT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags)
 {
        tVTerm  *term = &gVT_Terminals[ Node->Inode ];
         int    size;
@@ -447,8 +462,8 @@ size_t VT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer
                // - Sanity Checking
                size = term->Width*term->Height*4;
                if( Offset > size ) {
-                       Log_Notice("VTerm", "VT_Write: Offset (0x%llx) > FBSize (0x%x)",
-                               Offset, size);
+                       Log_Notice("VTerm", "VT_Write: %i Offset (0x%llx) > FBSize (0x%x)",
+                               (int)Node->Inode, Offset, size);
                        return 0;
                }
                if( Offset + Length > size ) {
@@ -512,7 +527,7 @@ size_t VT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer
                break;
        }
        
-       return 0;
+       return Length;
 }
 
 /**
@@ -733,11 +748,10 @@ void VT_SetTerminal(int ID)
                        gpVT_CurTerm->Buffer = malloc( gpVT_CurTerm->Width*gpVT_CurTerm->Height*4 );
                if( gpVT_CurTerm->Width < giVT_RealWidth )
                {
-                        int    line;
                        Uint    ofs = 0;
                        Uint32  *dest = gpVT_CurTerm->Buffer;
                        // Slower scanline copy
-                       for( line = 0; line < gpVT_CurTerm->Height; line ++ )
+                       for( int line = 0; line < gpVT_CurTerm->Height; line ++ )
                        {
                                VFS_ReadAt(giVT_OutputDevHandle, ofs, gpVT_CurTerm->Width*4, dest);
                                ofs += giVT_RealWidth * 4;
@@ -751,6 +765,7 @@ void VT_SetTerminal(int ID)
                                gpVT_CurTerm->Buffer
                                );
                }
+               LOG("Cached screen contents");
        }
 
        // Update current terminal ID

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