2 * Acess2 Kernel - Mouse Mulitplexing Driver
3 * - By John Hodge (thePowersGang)
9 #define VERSION VER2(0,1)
13 #include <Input/Mouse/include/mouse.h>
14 #include "include/mouse_int.h"
17 int Mouse_Install(char **Arguments);
18 void Mouse_Cleanup(void);
20 char *Mouse_Root_ReadDir(tVFS_Node *Node, int Pos);
21 tVFS_Node *Mouse_Root_FindDir(tVFS_Node *Node, const char *Name);
22 int Mouse_Dev_IOCtl(tVFS_Node *Node, int ID, void *Data);
23 size_t Mouse_Dev_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Data);
25 tMouse *Mouse_Register(const char *Name, int NumButtons, int NumAxies);
26 void Mouse_RemoveInstance(tMouse *Handle);
27 void Mouse_HandleEvent(tMouse *Handle, Uint32 ButtonState, int *AxisDeltas);
30 MODULE_DEFINE(0, VERSION, Mouse, Mouse_Install, Mouse_Cleanup, NULL);
31 tVFS_NodeType gMouse_RootNodeType = {
32 .ReadDir = Mouse_Root_ReadDir,
33 .FindDir = Mouse_Root_FindDir
35 tVFS_NodeType gMouse_DevNodeType = {
36 .IOCtl = Mouse_Dev_IOCtl,
37 .Read = Mouse_Dev_Read
39 tDevFS_Driver gMouse_DevInfo = {
41 { .Flags = VFS_FFLAG_DIRECTORY, .Type = &gMouse_RootNodeType, .Size = 1 }
43 tPointer gMouse_Pointer;
47 * \brief Initialise the keyboard driver
49 int Mouse_Install(char **Arguments)
51 /// - Register with DevFS
52 DevFS_AddDevice( &gMouse_DevInfo );
54 gMouse_Pointer.Node.Type = &gMouse_DevNodeType;
55 gMouse_Pointer.Node.ImplPtr = &gMouse_Pointer;
56 gMouse_Pointer.FileHeader = (void*)gMouse_Pointer.FileData;
57 gMouse_Pointer.Axies = (void*)( gMouse_Pointer.FileHeader + 1 );
63 * \brief Pre-unload cleanup function
65 void Mouse_Cleanup(void)
69 // --- VFS Interface ---
70 char *Mouse_Root_ReadDir(tVFS_Node *Node, int Pos)
72 if( Pos != 0 ) return NULL;
73 return strdup("system");
76 tVFS_Node *Mouse_Root_FindDir(tVFS_Node *Node, const char *Name)
78 if( strcmp(Name, "system") != 0 ) return NULL;
79 return &gMouse_Pointer.Node;
82 static const char *csaIOCTL_NAMES[] = {DRV_IOCTLNAMES, DRV_JOY_IOCTLNAMES, NULL};
84 * \brief IOCtl handler for the mouse
86 int Mouse_Dev_IOCtl(tVFS_Node *Node, int ID, void *Data)
88 tJoystick_NumValue *numval = Data;
89 tPointer *ptr = Node->ImplPtr;
92 BASE_IOCTLS(DRV_TYPE_MOUSE, "Mouse", VERSION, csaIOCTL_NAMES);
94 case JOY_IOCTL_GETSETAXISLIMIT:
95 if( !numval || !CheckMem(numval, sizeof(*numval)) )
97 LOG("GetSetAxisLimit %i = %i", numval->Num, numval->Value);
98 if(numval->Num < 0 || numval->Num >= ptr->FileHeader->NAxies)
100 if(numval->Value != -1)
101 ptr->AxisLimits[numval->Num] = numval->Value;
102 return ptr->AxisLimits[numval->Num];
104 case JOY_IOCTL_GETSETAXISPOSITION:
105 if( !numval || !CheckMem(numval, sizeof(*numval)) )
107 if(numval->Num < 0 || numval->Num >= ptr->FileHeader->NAxies)
109 if(numval->Value != -1)
110 ptr->Axies[numval->Num].CursorPos = numval->Value;
111 return ptr->Axies[numval->Num].CursorPos;
117 * \brief Read from a device
119 size_t Mouse_Dev_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Data)
121 tPointer *ptr = Node->ImplPtr;
122 int n_buttons = ptr->FileHeader->NButtons;
123 int n_axies = ptr->FileHeader->NAxies;
125 ENTER("pNode iLength pData", Node, Length, Data);
127 // TODO: Locking (Acquire)
131 sizeof(tJoystick_FileHeader) + n_axies*sizeof(tJoystick_Axis) + n_buttons
135 VFS_MarkAvaliable( Node, 0 );
137 // Check if more than the header is requested
138 if( Length > sizeof(tJoystick_FileHeader) )
140 // Clear axis values and button states
141 for( int i = 0; i < n_axies; i ++ )
142 ptr->Axies[i].CurValue = 0;
143 for( int i = 0; i < n_buttons; i ++ )
146 // Rebuild from device list
147 for( tMouse *dev = ptr->FirstDev; dev; dev = dev->Next )
149 for( int i = 0; i < n_axies; i ++ )
150 ptr->Axies[i].CurValue += dev->LastAxisVal[i];
151 for( int i = 0; i < n_buttons; i ++ )
153 if( dev->ButtonState & (1 << i) )
154 ptr->Buttons[i] = 255;
159 memcpy( Data, ptr->FileData, Length );
161 // TODO: Locking (Release)
167 // --- Device Interface ---
169 * Register an input device
170 * - See Input/Mouse/include/mouse.h
172 tMouse *Mouse_Register(const char *Name, int NumButtons, int NumAxies)
177 // TODO: Multiple pointers?
178 target = &gMouse_Pointer;
180 if( NumButtons > MAX_BUTTONS )
181 NumButtons = MAX_BUTTONS;
182 if( NumAxies > MAX_AXIES )
183 NumAxies = MAX_AXIES;
185 ret = malloc( sizeof(tMouse) + sizeof(ret->LastAxisVal[0])*NumAxies );
187 Log_Error("Mouse", "malloc() failed");
191 ret->Pointer = target;
192 ret->NumAxies = NumAxies;
193 ret->NumButtons = NumButtons;
194 ret->ButtonState = 0;
195 memset(ret->LastAxisVal, 0, sizeof(ret->LastAxisVal[0])*NumAxies );
199 ret->Next = target->FirstDev;
200 target->FirstDev = ret;
201 if( ret->NumAxies > target->FileHeader->NAxies ) {
202 // Clear new axis data
204 target->Axies + target->FileHeader->NAxies,
206 (ret->NumAxies - target->FileHeader->NAxies)/sizeof(tJoystick_Axis)
208 target->FileHeader->NAxies = ret->NumAxies;
209 target->Buttons = (void*)( target->Axies + ret->NumAxies );
211 if( ret->NumButtons > target->FileHeader->NButtons ) {
212 // No need to move as buttons can expand out into space
213 target->FileHeader->NButtons = ret->NumButtons;
221 * Remove a mouse instance
222 * - See Input/Mouse/include/mouse.h
224 void Mouse_RemoveInstance(tMouse *Instance)
229 * Handle a mouse event (movement or button press/release)
230 * - See Input/Mouse/include/mouse.h
232 void Mouse_HandleEvent(tMouse *Handle, Uint32 ButtonState, int *AxisDeltas)
236 ENTER("pHandle xButtonState pAxisDeltas", Handle, ButtonState, AxisDeltas);
238 ptr = Handle->Pointer;
240 // Update device state
241 Handle->ButtonState = ButtonState;
242 memcpy(Handle->LastAxisVal, AxisDeltas, sizeof(*AxisDeltas)*Handle->NumAxies);
244 // Update cursor position
245 // TODO: Acceleration?
246 for( int i = 0; i < Handle->NumAxies; i ++ )
248 ptr->Axies[i].CursorPos = MIN(MAX(0, ptr->Axies[i].CursorPos+AxisDeltas[i]), ptr->AxisLimits[i]);
249 LOG("Axis %i: delta = %i, pos = %i", i, AxisDeltas[i], ptr->Axies[i].CursorPos);
252 VFS_MarkAvaliable( &ptr->Node, 1 );