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 int 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 int Mouse_Cleanup(void)
70 // --- VFS Interface ---
71 char *Mouse_Root_ReadDir(tVFS_Node *Node, int Pos)
73 if( Pos != 0 ) return NULL;
74 return strdup("system");
77 tVFS_Node *Mouse_Root_FindDir(tVFS_Node *Node, const char *Name)
79 if( strcmp(Name, "system") != 0 ) return NULL;
80 return &gMouse_Pointer.Node;
83 static const char *csaIOCTL_NAMES[] = {DRV_IOCTLNAMES, DRV_JOY_IOCTLNAMES, NULL};
85 * \brief IOCtl handler for the mouse
87 int Mouse_Dev_IOCtl(tVFS_Node *Node, int ID, void *Data)
89 tJoystick_NumValue *numval = Data;
90 tPointer *ptr = Node->ImplPtr;
93 BASE_IOCTLS(DRV_TYPE_MOUSE, "Mouse", VERSION, csaIOCTL_NAMES);
95 case JOY_IOCTL_GETSETAXISLIMIT:
96 if( !numval || !CheckMem(numval, sizeof(*numval)) )
98 LOG("GetSetAxisLimit %i = %i", numval->Num, numval->Value);
99 if(numval->Num < 0 || numval->Num >= ptr->FileHeader->NAxies)
101 if(numval->Value != -1)
102 ptr->AxisLimits[numval->Num] = numval->Value;
103 return ptr->AxisLimits[numval->Num];
105 case JOY_IOCTL_GETSETAXISPOSITION:
106 if( !numval || !CheckMem(numval, sizeof(*numval)) )
108 if(numval->Num < 0 || numval->Num >= ptr->FileHeader->NAxies)
110 if(numval->Value != -1)
111 ptr->Axies[numval->Num].CursorPos = numval->Value;
112 return ptr->Axies[numval->Num].CursorPos;
118 * \brief Read from a device
120 size_t Mouse_Dev_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Data)
122 tPointer *ptr = Node->ImplPtr;
123 int n_buttons = ptr->FileHeader->NButtons;
124 int n_axies = ptr->FileHeader->NAxies;
126 ENTER("pNode iLength pData", Node, Length, Data);
128 // TODO: Locking (Acquire)
132 sizeof(tJoystick_FileHeader) + n_axies*sizeof(tJoystick_Axis) + n_buttons
136 VFS_MarkAvaliable( Node, 0 );
138 // Check if more than the header is requested
139 if( Length > sizeof(tJoystick_FileHeader) )
141 // Clear axis values and button states
142 for( int i = 0; i < n_axies; i ++ )
143 ptr->Axies[i].CurValue = 0;
144 for( int i = 0; i < n_buttons; i ++ )
147 // Rebuild from device list
148 for( tMouse *dev = ptr->FirstDev; dev; dev = dev->Next )
150 for( int i = 0; i < n_axies; i ++ )
151 ptr->Axies[i].CurValue += dev->LastAxisVal[i];
152 for( int i = 0; i < n_buttons; i ++ )
154 if( dev->ButtonState & (1 << i) )
155 ptr->Buttons[i] = 255;
160 memcpy( Data, ptr->FileData, Length );
162 // TODO: Locking (Release)
168 // --- Device Interface ---
170 * Register an input device
171 * - See Input/Mouse/include/mouse.h
173 tMouse *Mouse_Register(const char *Name, int NumButtons, int NumAxies)
178 // TODO: Multiple pointers?
179 target = &gMouse_Pointer;
181 if( NumButtons > MAX_BUTTONS )
182 NumButtons = MAX_BUTTONS;
183 if( NumAxies > MAX_AXIES )
184 NumAxies = MAX_AXIES;
186 ret = malloc( sizeof(tMouse) + sizeof(ret->LastAxisVal[0])*NumAxies );
188 Log_Error("Mouse", "malloc() failed");
192 ret->Pointer = target;
193 ret->NumAxies = NumAxies;
194 ret->NumButtons = NumButtons;
195 ret->ButtonState = 0;
196 memset(ret->LastAxisVal, 0, sizeof(ret->LastAxisVal[0])*NumAxies );
200 ret->Next = target->FirstDev;
201 target->FirstDev = ret;
202 if( ret->NumAxies > target->FileHeader->NAxies ) {
203 // Clear new axis data
205 target->Axies + target->FileHeader->NAxies,
207 (ret->NumAxies - target->FileHeader->NAxies)/sizeof(tJoystick_Axis)
209 target->FileHeader->NAxies = ret->NumAxies;
210 target->Buttons = (void*)( target->Axies + ret->NumAxies );
212 if( ret->NumButtons > target->FileHeader->NButtons ) {
213 // No need to move as buttons can expand out into space
214 target->FileHeader->NButtons = ret->NumButtons;
222 * Remove a mouse instance
223 * - See Input/Mouse/include/mouse.h
225 void Mouse_RemoveInstance(tMouse *Instance)
230 * Handle a mouse event (movement or button press/release)
231 * - See Input/Mouse/include/mouse.h
233 void Mouse_HandleEvent(tMouse *Handle, Uint32 ButtonState, int *AxisDeltas)
237 ENTER("pHandle xButtonState pAxisDeltas", Handle, ButtonState, AxisDeltas);
239 ptr = Handle->Pointer;
241 // Update device state
242 Handle->ButtonState = ButtonState;
243 memcpy(Handle->LastAxisVal, AxisDeltas, sizeof(*AxisDeltas)*Handle->NumAxies);
245 // Update cursor position
246 // TODO: Acceleration?
247 for( int i = 0; i < Handle->NumAxies; i ++ )
249 ptr->Axies[i].CursorPos = MIN(MAX(0, ptr->Axies[i].CursorPos+AxisDeltas[i]), ptr->AxisLimits[i]);
250 LOG("Axis %i: delta = %i, pos = %i", i, AxisDeltas[i], ptr->Axies[i].CursorPos);
253 VFS_MarkAvaliable( &ptr->Node, 1 );