From 3ae2c079ea44ea7a8fb1a65e7b9ff5d76f3f4c24 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 11 Mar 2012 11:21:12 +0800 Subject: [PATCH] Modules/Mouse - Fixing bugs, removing debug - Mouse multiplexer now works - AxWin3 uses it nicely - TODO: Fix AxWin3 arrow key handling --- KernelLand/Modules/Input/Mouse/main.c | 25 ++- KernelLand/Modules/Input/PS2KbMouse/common.h | 2 +- KernelLand/Modules/Input/PS2KbMouse/main.c | 8 +- .../Modules/Input/PS2KbMouse/ps2mouse.c | 171 +++--------------- KernelLand/Modules/USB/Core/usb_poll.c | 2 +- KernelLand/Modules/USB/HID/main.c | 2 +- KernelLand/Modules/USB/HID/mouse.c | 1 - Usermode/Applications/axwin3_src/WM/input.c | 23 ++- Usermode/Applications/axwin3_src/WM/main.c | 2 +- 9 files changed, 70 insertions(+), 166 deletions(-) diff --git a/KernelLand/Modules/Input/Mouse/main.c b/KernelLand/Modules/Input/Mouse/main.c index 7e9b4aa1..5e6a9a2d 100644 --- a/KernelLand/Modules/Input/Mouse/main.c +++ b/KernelLand/Modules/Input/Mouse/main.c @@ -38,7 +38,7 @@ tVFS_NodeType gMouse_DevNodeType = { }; tDevFS_Driver gMouse_DevInfo = { NULL, "Mouse", - { .Flags = VFS_FFLAG_DIRECTORY, .Type = &gMouse_RootNodeType } + { .Flags = VFS_FFLAG_DIRECTORY, .Type = &gMouse_RootNodeType, .Size = 1 } }; tPointer gMouse_Pointer; @@ -53,6 +53,8 @@ int Mouse_Install(char **Arguments) gMouse_Pointer.Node.Type = &gMouse_DevNodeType; gMouse_Pointer.Node.ImplPtr = &gMouse_Pointer; + gMouse_Pointer.FileHeader = (void*)gMouse_Pointer.FileData; + gMouse_Pointer.Axies = (void*)( gMouse_Pointer.FileHeader + 1 ); return 0; } @@ -92,6 +94,7 @@ int Mouse_Dev_IOCtl(tVFS_Node *Node, int ID, void *Data) case JOY_IOCTL_GETSETAXISLIMIT: if( !numval || !CheckMem(numval, sizeof(*numval)) ) return -1; + LOG("GetSetAxisLimit %i = %i", numval->Num, numval->Value); if(numval->Num < 0 || numval->Num >= ptr->FileHeader->NAxies) return 0; if(numval->Value != -1) @@ -119,6 +122,8 @@ size_t Mouse_Dev_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Data) int n_buttons = ptr->FileHeader->NButtons; int n_axies = ptr->FileHeader->NAxies; + ENTER("pNode iLength pData", Node, Length, Data); + // TODO: Locking (Acquire) Length = MIN( @@ -155,6 +160,7 @@ size_t Mouse_Dev_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Data) // TODO: Locking (Release) + LEAVE('i', Length); return Length; } @@ -192,7 +198,7 @@ tMouse *Mouse_Register(const char *Name, int NumButtons, int NumAxies) // TODO: Locking ret->Next = target->FirstDev; target->FirstDev = ret; - if( ret->NumAxies <= MAX_AXIES && ret->NumAxies > target->FileHeader->NAxies ) { + if( ret->NumAxies > target->FileHeader->NAxies ) { // Clear new axis data memset( target->Axies + target->FileHeader->NAxies, @@ -202,7 +208,7 @@ tMouse *Mouse_Register(const char *Name, int NumButtons, int NumAxies) target->FileHeader->NAxies = ret->NumAxies; target->Buttons = (void*)( target->Axies + ret->NumAxies ); } - if( ret->NumButtons <= MAX_BUTTONS && ret->NumButtons > target->FileHeader->NButtons ) { + if( ret->NumButtons > target->FileHeader->NButtons ) { // No need to move as buttons can expand out into space target->FileHeader->NButtons = ret->NumButtons; } @@ -225,18 +231,25 @@ void Mouse_RemoveInstance(tMouse *Instance) */ void Mouse_HandleEvent(tMouse *Handle, Uint32 ButtonState, int *AxisDeltas) { - tPointer *ptr = Handle->Pointer; + tPointer *ptr; - Handle->ButtonState = ButtonState; + ENTER("pHandle xButtonState pAxisDeltas", Handle, ButtonState, AxisDeltas); + + ptr = Handle->Pointer; + // Update device state + Handle->ButtonState = ButtonState; memcpy(Handle->LastAxisVal, AxisDeltas, sizeof(*AxisDeltas)*Handle->NumAxies); + // Update cursor position // TODO: Acceleration? - for( int i = 0; i < Handle->NumAxies; i ++ ) { ptr->Axies[i].CursorPos = MIN(MAX(0, ptr->Axies[i].CursorPos+AxisDeltas[i]), ptr->AxisLimits[i]); + LOG("Axis %i: delta = %i, pos = %i", i, AxisDeltas[i], ptr->Axies[i].CursorPos); } + VFS_MarkAvaliable( &ptr->Node, 1 ); + LEAVE('-'); } diff --git a/KernelLand/Modules/Input/PS2KbMouse/common.h b/KernelLand/Modules/Input/PS2KbMouse/common.h index 7cbffe9c..b5886a5a 100644 --- a/KernelLand/Modules/Input/PS2KbMouse/common.h +++ b/KernelLand/Modules/Input/PS2KbMouse/common.h @@ -21,6 +21,6 @@ extern void PL050_EnableMouse(void); extern void KB_HandleScancode(Uint8 scancode); extern void PS2Mouse_HandleInterrupt(Uint8 InputByte); -extern void (*gpMouse_EnableFcn)(void); +extern void (*gpPS2Mouse_EnableFcn)(void); #endif diff --git a/KernelLand/Modules/Input/PS2KbMouse/main.c b/KernelLand/Modules/Input/PS2KbMouse/main.c index ae51f81c..ceb422ac 100644 --- a/KernelLand/Modules/Input/PS2KbMouse/main.c +++ b/KernelLand/Modules/Input/PS2KbMouse/main.c @@ -7,7 +7,7 @@ #include #include "common.h" -// === IMPORTS === +// === CONSTANTS === // TODO: Allow runtime/compile-time switching // Maybe PCI will have it? // Integrator-CP @@ -31,17 +31,17 @@ // === GLOBALS === MODULE_DEFINE(0, 0x0100, Input_PS2KbMouse, PS2_Install, NULL, NULL); // Shuts the makefile up MODULE_DEFINE(0, 0x0100, PS2Keyboard, KB_Install, NULL, "Input_PS2KbMouse", "Keyboard", NULL); -MODULE_DEFINE(0, 0x0100, PS2Mouse, PS2Mouse_Install, NULL, "Input_PS2KbMouse", NULL); +MODULE_DEFINE(0, 0x0100, PS2Mouse, PS2Mouse_Install, NULL, "Input_PS2KbMouse", "Mouse", NULL); // === CODE === int PS2_Install(char **Arguments) { #if ARCHDIR_is_x86 || ARCHDIR_is_x86_64 KBC8042_Init(); - gpMouse_EnableFcn = KBC8042_EnableMouse; + gpPS2Mouse_EnableFcn = KBC8042_EnableMouse; #elif ARCHDIR_is_armv7 PL050_Init(KEYBOARD_BASE, KEYBOARD_IRQ, MOUSE_BASE, MOUSE_IRQ); - gpMouse_EnableFcn = PL050_EnableMouse; + gpPS2Mouse_EnableFcn = PL050_EnableMouse; #endif return MODULE_ERR_OK; diff --git a/KernelLand/Modules/Input/PS2KbMouse/ps2mouse.c b/KernelLand/Modules/Input/PS2KbMouse/ps2mouse.c index 69ce2b2e..e7c99490 100644 --- a/KernelLand/Modules/Input/PS2KbMouse/ps2mouse.c +++ b/KernelLand/Modules/Input/PS2KbMouse/ps2mouse.c @@ -5,9 +5,7 @@ #include #include #include -#include -#include -#include +#include #include "common.h" // == CONSTANTS == @@ -18,58 +16,25 @@ // - Internal - int PS2Mouse_Install(char **Arguments); void PS2Mouse_HandleInterrupt(Uint8 InputByte); -// - Filesystem - -size_t PS2Mouse_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer); - int PS2Mouse_IOCtl(tVFS_Node *Node, int ID, void *Data); // == GLOBALS == -void (*gpMouse_EnableFcn)(void); +void (*gpPS2Mouse_EnableFcn)(void); // - Settings - int giMouse_Sensitivity = 1; - int giMouse_MaxX = 640, giMouse_MaxY = 480; -// - File Data -Uint8 gMouse_FileData[sizeof(tJoystick_FileHeader) + NUM_AXIES*sizeof(tJoystick_Axis) + NUM_BUTTONS]; -tJoystick_FileHeader *gMouse_FileHeader = (void *)gMouse_FileData; -tJoystick_Axis *gMouse_Axies; -Uint8 *gMouse_Buttons; -tJoystick_Callback gMouse_Callback; - int gMouse_CallbackArg; - int giMouse_AxisLimits[2]; + int giPS2Mouse_Sensitivity = 1; // - Internal State - int giMouse_Cycle = 0; // IRQ Position -Uint8 gaMouse_Bytes[4] = {0,0,0,0}; -// - Driver definition -tVFS_NodeType gMouse_NodeType = { - .Read = PS2Mouse_Read, - .IOCtl = PS2Mouse_IOCtl -}; -tDevFS_Driver gMouse_DriverStruct = { - NULL, "PS2Mouse", - { - .NumACLs = 1, .ACLs = &gVFS_ACL_EveryoneRX, - .Type = &gMouse_NodeType - } -}; + int giPS2Mouse_Cycle = 0; // IRQ Position +Uint8 gaPS2Mouse_Bytes[4] = {0,0,0,0}; +tMouse *gpPS2Mouse_Handle; // == CODE == int PS2Mouse_Install(char **Arguments) { - - - // Set up variables - gMouse_Axies = (void*)&gMouse_FileData[4]; - gMouse_Buttons = (void*)&gMouse_Axies[NUM_AXIES]; + gpPS2Mouse_Handle = Mouse_Register("PS2Mouse", NUM_AXIES, NUM_BUTTONS); - gMouse_FileHeader->NAxies = 2; gMouse_FileHeader->NButtons = 3; - gMouse_Axies[0].MinValue = -10; gMouse_Axies[0].MaxValue = 10; - gMouse_Axies[1].MinValue = -10; gMouse_Axies[1].MaxValue = 10; - // Initialise Mouse Controller - giMouse_Cycle = 0; // Set Current Cycle position - gpMouse_EnableFcn(); - - DevFS_AddDevice(&gMouse_DriverStruct); - + giPS2Mouse_Cycle = 0; // Set Current Cycle position + gpPS2Mouse_EnableFcn(); + return MODULE_ERR_OK; } @@ -79,22 +44,21 @@ void PS2Mouse_HandleInterrupt(Uint8 InputByte) { Uint8 flags; int d[2], d_accel[2]; - int i; // Gather mouse data - gaMouse_Bytes[giMouse_Cycle] = InputByte; - LOG("gaMouse_Bytes[%i] = 0x%02x", gMouse_Axies[0].MaxValue); + gaPS2Mouse_Bytes[giPS2Mouse_Cycle] = InputByte; // - If bit 3 of the first byte is not set, it's not a valid packet? - if(giMouse_Cycle == 0 && !(gaMouse_Bytes[0] & 0x08) ) - return ; - giMouse_Cycle++; - if(giMouse_Cycle < 3) + if(giPS2Mouse_Cycle == 0 && !(gaPS2Mouse_Bytes[0] & 0x08) ) return ; - giMouse_Cycle = 0; + // 3 bytes per packet + giPS2Mouse_Cycle++; + if(giPS2Mouse_Cycle < 3) + return ; + giPS2Mouse_Cycle = 0; // Actual Processing (once we have all bytes) - flags = gaMouse_Bytes[0]; + flags = gaPS2Mouse_Bytes[0]; LOG("flags = 0x%x", flags); @@ -102,105 +66,18 @@ void PS2Mouse_HandleInterrupt(Uint8 InputByte) if(flags & 0xC0) return; // Calculate dX and dY - d[0] = gaMouse_Bytes[1]; if(flags & 0x10) d[0] = -(256-d[0]); // x - d[1] = gaMouse_Bytes[2]; if(flags & 0x20) d[1] = -(256-d[1]); // y + d[0] = gaPS2Mouse_Bytes[1]; if(flags & 0x10) d[0] = -(256-d[0]); // x + d[1] = gaPS2Mouse_Bytes[2]; if(flags & 0x20) d[1] = -(256-d[1]); // y d[1] = -d[1]; // Y is negated LOG("RAW dx=%i, dy=%i\n", d[0], d[1]); // Apply scaling // TODO: Apply a form of curve to the mouse movement (dx*log(dx), dx^k?) // TODO: Independent sensitivities? // TODO: Disable acceleration via a flag? - d_accel[0] = d[0]*giMouse_Sensitivity; - d_accel[1] = d[1]*giMouse_Sensitivity; + d_accel[0] = d[0]*giPS2Mouse_Sensitivity; + d_accel[1] = d[1]*giPS2Mouse_Sensitivity; - // Set Buttons (Primary) - for( i = 0; i < 3; i ++ ) - { - Uint8 newVal = (flags & (1 << i)) ? 0xFF : 0; - if(newVal != gMouse_Buttons[i]) { - if( gMouse_Callback ) - gMouse_Callback(gMouse_CallbackArg, 0, i, newVal - gMouse_Buttons[i]); - gMouse_Buttons[i] = newVal; - } - } - - // Update X and Y Positions - for( i = 0; i < 2; i ++ ) - { - Sint16 newCursor = 0; - if( giMouse_AxisLimits[i] ) - newCursor = MIN( MAX(0, gMouse_Axies[i].CursorPos + d_accel[i]), giMouse_AxisLimits[i] );; - - if( gMouse_Callback ) - { - if(giMouse_AxisLimits[i] && gMouse_Axies[i].CursorPos != newCursor) - gMouse_Callback(gMouse_CallbackArg, 1, i, newCursor - gMouse_Axies[i].CursorPos); - if(!giMouse_AxisLimits[i] && gMouse_Axies[i].CurValue != d_accel[i]) - gMouse_Callback(gMouse_CallbackArg, 1, i, d_accel[i] - gMouse_Axies[i].CurValue); - } - - gMouse_Axies[i].CurValue = d_accel[i]; - gMouse_Axies[i].CursorPos = newCursor; - } - -// Log("Mouse at %ix%i", gMouse_Axies[0].CursorPos, gMouse_Axies[1].CursorPos); - - VFS_MarkAvaliable(&gMouse_DriverStruct.RootNode, 1); -} - -/* Read mouse state (coordinates) - */ -size_t PS2Mouse_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer) -{ - if(Offset > sizeof(gMouse_FileData)) return 0; - if(Length > sizeof(gMouse_FileData)) Length = sizeof(gMouse_FileData); - if(Offset + Length > sizeof(gMouse_FileData)) Length = sizeof(gMouse_FileData) - Offset; - - memcpy(Buffer, &gMouse_FileData[Offset], Length); - - VFS_MarkAvaliable(Node, 0); - return Length; -} - -static const char *csaIOCtls[] = {DRV_IOCTLNAMES, DRV_JOY_IOCTLNAMES, NULL}; -/* Handle messages to the device - */ -int PS2Mouse_IOCtl(tVFS_Node *Node, int ID, void *Data) -{ - tJoystick_NumValue *info = Data; - - switch(ID) - { - BASE_IOCTLS(DRV_TYPE_JOYSTICK, "PS2Mouse", 0x100, csaIOCtls); - - case JOY_IOCTL_SETCALLBACK: // TODO: Implement - return -1; - - case JOY_IOCTL_SETCALLBACKARG: // TODO: Implement - return -1; - - case JOY_IOCTL_GETSETAXISLIMIT: - if(!info) return 0; - if(info->Num < 0 || info->Num >= 2) return 0; - if(info->Value != -1) - giMouse_AxisLimits[info->Num] = info->Value; - return giMouse_AxisLimits[info->Num]; - - case JOY_IOCTL_GETSETAXISPOSITION: - if(!info) return 0; - if(info->Num < 0 || info->Num >= 2) return 0; - if(info->Value != -1) - gMouse_Axies[info->Num].CursorPos = info->Value; - return gMouse_Axies[info->Num].CursorPos; - - case JOY_IOCTL_GETSETAXISFLAGS: - return -1; - - case JOY_IOCTL_GETSETBUTTONFLAGS: - return -1; - - default: - return 0; - } + // TODO: Scroll wheel? + Mouse_HandleEvent(gpPS2Mouse_Handle, (flags & 7), d_accel); } diff --git a/KernelLand/Modules/USB/Core/usb_poll.c b/KernelLand/Modules/USB/Core/usb_poll.c index 63596653..9cdd6b47 100644 --- a/KernelLand/Modules/USB/Core/usb_poll.c +++ b/KernelLand/Modules/USB/Core/usb_poll.c @@ -5,7 +5,7 @@ * usb_poll.c * - Endpoint polling */ -#define DEBUG 1 +#define DEBUG 0 #include #include "usb.h" #include diff --git a/KernelLand/Modules/USB/HID/main.c b/KernelLand/Modules/USB/HID/main.c index 1b1ae096..6b36809a 100644 --- a/KernelLand/Modules/USB/HID/main.c +++ b/KernelLand/Modules/USB/HID/main.c @@ -5,7 +5,7 @@ * main.c * - Driver Core */ -#define DEBUG 1 +#define DEBUG 0 #define VERSION VER2(0,1) #include #include diff --git a/KernelLand/Modules/USB/HID/mouse.c b/KernelLand/Modules/USB/HID/mouse.c index 2dfbde53..d706a626 100644 --- a/KernelLand/Modules/USB/HID/mouse.c +++ b/KernelLand/Modules/USB/HID/mouse.c @@ -8,7 +8,6 @@ #define DEBUG 0 #include #include "hid_reports.h" -#include #include // === CONSTANTS === diff --git a/Usermode/Applications/axwin3_src/WM/input.c b/Usermode/Applications/axwin3_src/WM/input.c index 9660dd05..9fe73ff7 100644 --- a/Usermode/Applications/axwin3_src/WM/input.c +++ b/Usermode/Applications/axwin3_src/WM/input.c @@ -99,6 +99,8 @@ void Input_HandleSelect(fd_set *set) if(FD_ISSET(giMouseFD, set)) { + const int c_n_axies = 4; + const int c_n_buttons = 5; int i; struct sMouseAxis { int16_t MinValue; @@ -111,17 +113,30 @@ void Input_HandleSelect(fd_set *set) uint16_t NAxies; uint16_t NButtons; } *mouseinfo; - char data[sizeof(*mouseinfo) + sizeof(*axies)*3 + 5]; + char data[sizeof(*mouseinfo) + sizeof(*axies)*c_n_axies + c_n_buttons]; mouseinfo = (void*)data; seek(giMouseFD, 0, SEEK_SET); i = read(giMouseFD, data, sizeof(data)); i -= sizeof(*mouseinfo); - if( i < 0 ) + if( i < 0 ) { + _SysDebug("Mouse data undersized (no header)"); return ; - if( i < sizeof(*axies)*mouseinfo->NAxies + mouseinfo->NButtons ) + } + if( mouseinfo->NAxies > c_n_axies || mouseinfo->NButtons > c_n_buttons ) { + _SysDebug( + "%i axies, %i buttons above prealloc counts (%i, %i)", + mouseinfo->NAxies, mouseinfo->NButtons, c_n_axies, c_n_buttons + ); + return ; + } + if( i < sizeof(*axies)*mouseinfo->NAxies + mouseinfo->NButtons ) { + _SysDebug("Mouse data undersized (body doesn't fit %i < %i)", + i, sizeof(*axies)*mouseinfo->NAxies + mouseinfo->NButtons + ); return ; + } // What? No X/Y? if( mouseinfo->NAxies < 2 ) @@ -133,7 +148,7 @@ void Input_HandleSelect(fd_set *set) // Handle movement Video_SetCursorPos( axies[0].CursorPos, axies[1].CursorPos ); - _SysDebug("Mouse to %i,%i", axies[0].CursorPos, axies[1].CursorPos); +// _SysDebug("Mouse to %i,%i", axies[0].CursorPos, axies[1].CursorPos); WM_Input_MouseMoved( giInput_MouseX, giInput_MouseY, diff --git a/Usermode/Applications/axwin3_src/WM/main.c b/Usermode/Applications/axwin3_src/WM/main.c index b6fcf497..0d150467 100644 --- a/Usermode/Applications/axwin3_src/WM/main.c +++ b/Usermode/Applications/axwin3_src/WM/main.c @@ -49,7 +49,7 @@ int main(int argc, char *argv[]) gsTerminalDevice = "/Devices/VTerm/6"; } if( gsMouseDevice == NULL ) { - gsMouseDevice = "/Devices/PS2Mouse"; + gsMouseDevice = "/Devices/Mouse/system"; } Video_Setup(); -- 2.20.1