PS2Mouse - Reworked driver to conform with Joystick spec
authorJohn Hodge <[email protected]>
Wed, 6 Jul 2011 15:08:46 +0000 (23:08 +0800)
committerJohn Hodge <[email protected]>
Wed, 6 Jul 2011 15:08:46 +0000 (23:08 +0800)
- Also slightly updated spec to confirm how Axis callbacks happen

Kernel/include/tpl_drv_joystick.h
Modules/Input/PS2KbMouse/ps2mouse.c

index 06cdb23..2f4249b 100644 (file)
  */\r
 enum eTplJoystick_IOCtl {\r
        /**\r
-        * ioctl(..., tJoystickCallback *Callback)\r
+        * ioctl(..., tJoystick_Callback *Callback)\r
         * \brief Sets the callback\r
         * \note Can be called from kernel mode only\r
         *\r
         * Sets the callback that is called when a event occurs (button or axis\r
         * change). This function pointer must be in kernel mode (although,\r
         * kernel->user or kernel->ring3driver abstraction functions can be used)\r
+        * \r
+        * Axis events depend on the axis limit, if non-zero, the callback fires\r
+        * if the cursor position changes. Otherwise it fires when the axis value\r
+        * (cursor accelleration) changes.\r
         */\r
        JOY_IOCTL_SETCALLBACK = 4,\r
 \r
index 7ccf461..f67c8b1 100644 (file)
@@ -36,6 +36,9 @@ Uint8 gMouse_FileData[sizeof(tJoystick_FileHeader) + NUM_AXIES*sizeof(tJoystick_
 tJoystick_FileHeader   *gMouse_FileHeader = (void *)gMouse_FileData;\r
 tJoystick_Axis *gMouse_Axies;\r
 Uint8  *gMouse_Buttons;\r
+tJoystick_Callback     gMouse_Callback;\r
+ int   gMouse_CallbackArg;\r
+ int   giMouse_AxisLimits[2];\r
 // - Internal State\r
  int   giMouse_Cycle = 0;      // IRQ Position\r
 Uint8  gaMouse_Bytes[4] = {0,0,0,0};\r
@@ -70,9 +73,8 @@ int PS2Mouse_Install(char **Arguments)
 void PS2Mouse_IRQ(int Num)\r
 {\r
        Uint8   flags;\r
-        int    dx, dy;\r
-        int    dx_accel, dy_accel;\r
-\r
+        int    d[2], d_accel[2];\r
+        int    i;\r
        \r
        // Gather mouse data\r
        gaMouse_Bytes[giMouse_Cycle] = inb(0x60);\r
@@ -95,25 +97,52 @@ void PS2Mouse_IRQ(int Num)
        if(flags & 0xC0)        return;\r
                \r
        // Calculate dX and dY\r
-       dx = gaMouse_Bytes[1];  if(flags & 0x10) dx = -(256-dx);\r
-       dy = gaMouse_Bytes[2];  if(flags & 0x20) dy = -(256-dy);\r
-       dy = -dy;       // Y is negated\r
-       LOG("RAW dx=%i, dy=%i\n", dx, dy);\r
+       d[0] = gaMouse_Bytes[1];        if(flags & 0x10) d[0] = -(256-d[0]);    // x\r
+       d[1] = gaMouse_Bytes[2];        if(flags & 0x20) d[1] = -(256-d[1]);    // y\r
+       d[1] = -d[1];   // Y is negated\r
+       LOG("RAW dx=%i, dy=%i\n", d[0], d[1]);\r
        // Apply scaling\r
        // TODO: Apply a form of curve to the mouse movement (dx*log(dx), dx^k?)\r
        // TODO: Independent sensitivities?\r
        // TODO: Disable acceleration via a flag?\r
-       dx_accel = dx*giMouse_Sensitivity;\r
-       dy_accel = dy*giMouse_Sensitivity;\r
+       d_accel[0] = d[0]*giMouse_Sensitivity;\r
+       d_accel[1] = d[1]*giMouse_Sensitivity;\r
        \r
        // Set Buttons (Primary)\r
-       gMouse_Buttons[0] = (flags & 1) ? 0xFF : 0;\r
-       gMouse_Buttons[1] = (flags & 2) ? 0xFF : 0;\r
-       gMouse_Buttons[2] = (flags & 4) ? 0xFF : 0;\r
+       for( i = 0; i < 3; i ++ )\r
+       {\r
+               Uint8   newVal = (flags & (1 << i)) ? 0xFF : 0;\r
+               if(newVal != gMouse_Buttons[i]) {\r
+                       if( gMouse_Callback )\r
+                               gMouse_Callback(gMouse_CallbackArg, 0, i, newVal - gMouse_Buttons[i]);\r
+                       gMouse_Buttons[i] = newVal;\r
+               }\r
+       }\r
        \r
        // Update X and Y Positions\r
-       gMouse_Axies[0].CurValue = MIN( MAX(gMouse_Axies[0].MinValue, gMouse_Axies[0].CurValue + dx_accel), gMouse_Axies[0].MaxValue );\r
-       gMouse_Axies[1].CurValue = MIN( MAX(gMouse_Axies[1].MinValue, gMouse_Axies[1].CurValue + dy_accel), gMouse_Axies[1].MaxValue );\r
+       for( i = 0; i < 2; i ++ )\r
+       {\r
+               Sint16  newCursor = 0;\r
+               if( giMouse_AxisLimits[i] )\r
+                       newCursor = MIN( MAX(0, gMouse_Axies[i].CursorPos + d_accel[i]), giMouse_AxisLimits[i] );;\r
+               \r
+               if( gMouse_Callback )\r
+               {\r
+                       if(giMouse_AxisLimits[i] && gMouse_Axies[i].CursorPos != newCursor)\r
+                               gMouse_Callback(gMouse_CallbackArg, 1, i, newCursor - gMouse_Axies[i].CursorPos);\r
+                       if(!giMouse_AxisLimits[i] && gMouse_Axies[i].CurValue != d_accel[i])\r
+                               gMouse_Callback(gMouse_CallbackArg, 1, i, d_accel[i] - gMouse_Axies[i].CurValue);\r
+               }\r
+               \r
+               gMouse_Axies[i].CurValue = d_accel[i];\r
+               gMouse_Axies[i].CursorPos = newCursor;\r
+       }\r
+       \r
+//     Log_Debug("PS2Mouse", "gMouse_Buttons = {0x%x,0x%x,0x%x}, gMouse_Axies={%i,%i}", \r
+//             gMouse_Buttons[0], gMouse_Buttons[1], gMouse_Buttons[2],\r
+//             gMouse_Axies[0].CursorPos, gMouse_Axies[1].CursorPos);\r
+       \r
+       VFS_MarkAvaliable(&gMouse_DriverStruct.RootNode, 1);\r
 }\r
 \r
 /* Read mouse state (coordinates)\r
@@ -125,7 +154,8 @@ Uint64 PS2Mouse_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer
        if(Offset + Length > sizeof(gMouse_FileData))   Length = sizeof(gMouse_FileData) - Offset;\r
 \r
        memcpy(Buffer, &gMouse_FileData[Offset], Length);\r
-               \r
+       \r
+       VFS_MarkAvaliable(Node, 0);\r
        return Length;\r
 }\r
 \r
@@ -150,15 +180,15 @@ int PS2Mouse_IOCtl(tVFS_Node *Node, int ID, void *Data)
                if(!info)       return 0;\r
                if(info->Num < 0 || info->Num >= 2)     return 0;\r
                if(info->Value != -1)\r
-                       gMouse_Axies[info->Num].MaxValue = info->Value;\r
-               return gMouse_Axies[info->Num].MaxValue;\r
+                       giMouse_AxisLimits[info->Num] = info->Value;\r
+               return giMouse_AxisLimits[info->Num];\r
        \r
        case JOY_IOCTL_GETSETAXISPOSITION:\r
                if(!info)       return 0;\r
                if(info->Num < 0 || info->Num >= 2)     return 0;\r
                if(info->Value != -1)\r
-                       gMouse_Axies[info->Num].CurValue = info->Value;\r
-               return gMouse_Axies[info->Num].CurValue;\r
+                       gMouse_Axies[info->Num].CursorPos = info->Value;\r
+               return gMouse_Axies[info->Num].CursorPos;\r
 \r
        case JOY_IOCTL_GETSETAXISFLAGS:\r
                return -1;\r

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