Usermode/libc++ - Implement map::insert and map::erase
[tpg/acess2.git] / KernelLand / Modules / USB / HID / mouse.c
index be015ad..d706a62 100644 (file)
@@ -8,8 +8,7 @@
 #define DEBUG  0
 #include <acess.h>
 #include "hid_reports.h"
-#include <fs_devfs.h>
-#include <api_drv_joystick.h>
+#include <Input/Mouse/include/mouse.h>
 
 // === CONSTANTS ===
 #define MAX_AXIES      3       // X, Y, Scroll
@@ -23,62 +22,58 @@ struct sHID_Mouse
        tHID_Mouse      *Next;
        tUSB_DataCallback       DataAvail;
 
-       tVFS_Node       Node;
-        int    CollectionDepth;
-       
-       Uint8   FileData[ sizeof(tJoystick_FileHeader) + MAX_AXIES*sizeof(tJoystick_Axis) + MAX_BUTTONS ];
-       tJoystick_FileHeader    *FileHeader;
-       tJoystick_Axis  *Axies;
-       Uint8   *Buttons;
+       tMouse  *Handle;
 
-       Uint16  AxisLimits[MAX_AXIES];
-       
+       // - Report parsing
         int    nMappings;
        struct {
                Uint8   Dest;   // 0x00-0x7F = Buttons, 0x80-0xFE = Axies, 0xFF = Ignore
                Uint8   BitSize;
        } *Mappings;
+       
+       // - Initialisation only data
+        int    CollectionDepth;
 };
 
 // === PROTOTYES ===
+Sint32 _ReadBits(void *Data, int Offset, int Length);
 void   HID_Mouse_DataAvail(tUSBInterface *Dev, int EndPt, int Length, void *Data);
+
 tHID_ReportCallbacks   *HID_Mouse_Report_Collection(tUSBInterface *Dev, tHID_ReportGlobalState *Global, tHID_ReportLocalState *Local, Uint32 Value);
 void   HID_Mouse_Report_EndCollection(tUSBInterface *Dev);
 void   HID_Mouse_Report_Input(tUSBInterface *Dev, tHID_ReportGlobalState *Global, tHID_ReportLocalState *Local, Uint32 Value);
 
 // === GLOBALS ===
-tVFS_NodeType  gHID_Mouse_RootNodeType = {
-       .TypeName = "HID Mouse Root"
-};
-tVFS_NodeType  gHID_Mouse_DevNodeType = {
-       .TypeName = "HID Mouse Dev"
-};
-tDevFS_Driver  gHID_Mouse_DevFS = {
-       .Name = "USBMouse",
-       .RootNode = {.Type = &gHID_Mouse_RootNodeType}
-};
 tHID_ReportCallbacks   gHID_Mouse_ReportCBs = {
        .Collection = HID_Mouse_Report_Collection,
        .EndCollection = HID_Mouse_Report_EndCollection,
        .Input = HID_Mouse_Report_Input,
 };
+tMutex glHID_MouseListLock;
 tHID_Mouse     *gpHID_FirstMouse;
 tHID_Mouse     *gpHID_LastMouse = (tHID_Mouse*)&gpHID_FirstMouse;
 
 // === CODE ===
-// ----------------------------------------------------------------------------
-// VFS Interface
-// ----------------------------------------------------------------------------
-
 // ----------------------------------------------------------------------------
 // Data input / Update
 // ----------------------------------------------------------------------------
+/**
+ * \brief Read a set amounts of bits from a stream
+ * \param Data Base of data
+ * \param Offset       Bit offset
+ * \param Length       Number of bits to read
+ * \return Sign-extended value
+ */
 Sint32 _ReadBits(void *Data, int Offset, int Length)
 {
         int    dest_ofs = 0;
+        int    rem = Length;
        Uint32  rv = 0;
        Uint8   *bytes = (Uint8*)Data + Offset / 8;
-       
+
+       // Sanity please        
+       if( Length > 32 )       return 0;
+
        // Leading byte
        if( Offset & 7 )
        {
@@ -91,23 +86,22 @@ Sint32 _ReadBits(void *Data, int Offset, int Length)
                rv = (*bytes >> Offset);
                
                dest_ofs = Offset & 7;
-               Length -= Offset & 7;
+               rem = Length - (Offset & 7);
                bytes ++;
        }
 
        // Body bytes
-       while( Length >= 8 )
+       while( rem >= 8 )
        {
                rv |= *bytes << dest_ofs;
                dest_ofs += 8;
-               Length -= 8;
+               rem -= 8;
                bytes ++;
        }
        
-       if( Length )
+       if( rem )
        {
-               rv |= (*bytes & ((1 << Length)-1)) << dest_ofs;
-               
+               rv |= (*bytes & ((1 << rem)-1)) << dest_ofs;
        }
        
        // Do sign extension
@@ -117,15 +111,19 @@ _ext:
        return rv;
 }
 
+/**
+ * \brief Handle an update from the device
+ */
 void HID_Mouse_DataAvail(tUSBInterface *Dev, int EndPt, int Length, void *Data)
 {
        tHID_Mouse      *info;
         int    ofs;
+       Uint32  button_value = 0;
+        int    axis_values[MAX_AXIES] = {0};
        
        info = USB_GetDeviceDataPtr(Dev);
        if( !info )     return ;
-       
-       
+
        ofs = 0;
        for( int i = 0; i < info->nMappings; i ++ )
        {
@@ -136,6 +134,7 @@ void HID_Mouse_DataAvail(tUSBInterface *Dev, int EndPt, int Length, void *Data)
                        return ;
 
                value = _ReadBits(Data, ofs, info->Mappings[i].BitSize);
+               LOG("%i+%i: value = %i", ofs, info->Mappings[i].BitSize, value);
                ofs += info->Mappings[i].BitSize;
                
                if( dest == 0xFF )      continue ;
@@ -143,37 +142,27 @@ void HID_Mouse_DataAvail(tUSBInterface *Dev, int EndPt, int Length, void *Data)
                if( dest & 0x80 )
                {
                        // Axis
-                       info->Axies[ dest & 0x7F ].CurValue = value;
+                       axis_values[ dest & 0x7F ] = value;
+                       LOG("Axis %i = %i", dest & 0x7F, value);
                }
                else
                {
                        // Button
-                       info->Buttons[ dest & 0x7F ] = (value == 0) ? 0 : 0xFF;
+                       if( value == 0 )
+                               ;
+                       else
+                               button_value |= 1 << (dest & 0x7F);
                }
        }
        
-       // Update axis positions
-       for( int i = 0; i < MAX_AXIES; i ++ )
-       {
-                int    newpos;
-               
-               // TODO: Scaling
-               newpos = info->Axies[i].CursorPos + info->Axies[i].CurValue;
-               
-               if(newpos < 0)  newpos = 0;
-               if(newpos > info->AxisLimits[i])        newpos = info->AxisLimits[i];
-               
-               info->Axies[i].CursorPos = newpos;
-       }
-       Log_Debug("USBMouse", "New Pos (%i,%i,%i)",
-               info->Axies[0].CursorPos, info->Axies[1].CursorPos, info->Axies[2].CursorPos
-               );
+       Mouse_HandleEvent(info->Handle, button_value, axis_values);
 }
 
 // ----------------------------------------------------------------------------
 // Device initialisation
 // ----------------------------------------------------------------------------
 /**
+ * \brief Handle the opening of a collection
  */
 tHID_ReportCallbacks *HID_Mouse_Report_Collection(
        tUSBInterface *Dev,
@@ -189,11 +178,10 @@ tHID_ReportCallbacks *HID_Mouse_Report_Collection(
                // New device!
                info = calloc( sizeof(tHID_Mouse), 1 );
                info->DataAvail = HID_Mouse_DataAvail;
-               info->Node.Type = &gHID_Mouse_DevNodeType;
-               
-               info->FileHeader = (void*)info->FileData;
-               info->Axies = (void*)(info->FileHeader + 1);
-               info->Buttons = (void*)(info->Axies + MAX_AXIES);
+       
+               info->Handle = Mouse_Register("USBMouse", MAX_BUTTONS, MAX_AXIES);
+       
+               LOG("Initialised new mouse at %p", info);
                
                USB_SetDeviceDataPtr(Dev, info);
        }
@@ -205,6 +193,9 @@ tHID_ReportCallbacks *HID_Mouse_Report_Collection(
        return &gHID_Mouse_ReportCBs;
 }
 
+/**
+ * \brief Handle the end of a collection
+ */
 void HID_Mouse_Report_EndCollection(tUSBInterface *Dev)
 {
        tHID_Mouse      *info;
@@ -217,9 +208,11 @@ void HID_Mouse_Report_EndCollection(tUSBInterface *Dev)
 
        if( info->CollectionDepth == 0 )
        {
-               // TODO: Do final cleanup on device
+               // Perform final initialisation steps
+               Mutex_Acquire(&glHID_MouseListLock);
                gpHID_LastMouse->Next = info;
                gpHID_LastMouse = info;
+               Mutex_Release(&glHID_MouseListLock);
        }
        else
        {
@@ -227,6 +220,9 @@ void HID_Mouse_Report_EndCollection(tUSBInterface *Dev)
        }
 }
 
+/**
+ * \brief Add a new input mapping
+ */
 void HID_int_AddInput(tUSBInterface *Dev, Uint32 Usage, Uint8 Size, Uint32 Min, Uint32 Max)
 {
        Uint8   tag;
@@ -238,6 +234,7 @@ void HID_int_AddInput(tUSBInterface *Dev, Uint32 Usage, Uint8 Size, Uint32 Min,
                return ;
        }
 
+       // --- Get the destination for the field ---
        switch(Usage)
        {
        case 0x00010030:        tag = 0x80;     break;  // Generic Desktop - X
@@ -251,20 +248,28 @@ void HID_int_AddInput(tUSBInterface *Dev, Uint32 Usage, Uint8 Size, Uint32 Min,
        default:        tag = 0xFF;     break;
        }
        
+       LOG("Usage = 0x%08x, tag = 0x%2x", Usage, tag);
+
+       // --- Add to list of mappings ---
        info->nMappings ++;
        info->Mappings = realloc(info->Mappings, info->nMappings * sizeof(info->Mappings[0]));
        // TODO: NULL check
-       
        info->Mappings[ info->nMappings - 1].Dest = tag;
        info->Mappings[ info->nMappings - 1].BitSize = Size;
        
-       if( tag != 0xFF && (tag & 0x80) )
-       {
-               info->Axies[ tag & 0x7F ].MinValue = Min;
-               info->Axies[ tag & 0x7F ].MaxValue = Max;
-       }
+       // --- Update Min/Max for Axies ---
+       // TODO: DPI too
+       // TODO: Pass to mouse multiplexer
+//     if( tag != 0xFF && (tag & 0x80) )
+//     {
+//             info->Axies[ tag & 0x7F ].MinValue = Min;
+//             info->Axies[ tag & 0x7F ].MaxValue = Max;
+//     }
 }
 
+/**
+ * \brief Handle an input item in a report
+ */
 void HID_Mouse_Report_Input(
        tUSBInterface *Dev,
        tHID_ReportGlobalState *Global, tHID_ReportLocalState *Local,
@@ -272,10 +277,14 @@ void HID_Mouse_Report_Input(
        )
 {
        Uint32  usage = 0;
+       LOG("Local->Usages.nItems = %i", Local->Usages.nItems);
        for( int i = 0; i < Global->ReportCount; i ++ )
        {
+               // - Update usage
                if( i < Local->Usages.nItems )
                        usage = Local->Usages.Items[i];
+               LOG("%i: usage = %x", i, usage);
+               // - Add to list
                HID_int_AddInput(Dev, usage, Global->ReportSize, Global->LogMin, Global->LogMax);
        }
 }

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