Modules - Implementing mouse multiplexer
[tpg/acess2.git] / KernelLand / Modules / Input / Mouse / main.c
1 /*
2  * Acess2 Kernel - Mouse Mulitplexing Driver
3  * - By John Hodge (thePowersGang)
4  *
5  * main.c
6  * - Mouse mulitplexing
7  */
8 #define DEBUG   0
9 #define VERSION VER2(0,1)
10 #include <acess.h>
11 #include <modules.h>
12 #include <fs_devfs.h>
13 #include <Input/Mouse/include/mouse.h>
14 #include "include/mouse_int.h"
15
16 // === PROTOTYPES ===
17  int    Mouse_Install(char **Arguments);
18 void    Mouse_Cleanup(void);
19 // - "User" side
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);
24 // - Device Side
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);
28
29 // === GLOBALS ===
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
34 };
35 tVFS_NodeType   gMouse_DevNodeType = {
36         .IOCtl = Mouse_Dev_IOCtl,
37         .Read = Mouse_Dev_Read
38 };
39 tDevFS_Driver   gMouse_DevInfo = {
40         NULL, "Mouse",
41         { .Flags = VFS_FFLAG_DIRECTORY, .Type = &gMouse_RootNodeType }
42 };
43 tPointer        gMouse_Pointer;
44
45 // === CODE ===
46 /**
47  * \brief Initialise the keyboard driver
48  */
49 int Mouse_Install(char **Arguments)
50 {
51         /// - Register with DevFS
52         DevFS_AddDevice( &gMouse_DevInfo );
53
54         gMouse_Pointer.Node.Type = &gMouse_DevNodeType;
55         gMouse_Pointer.Node.ImplPtr = &gMouse_Pointer;
56
57         return 0;
58 }
59
60 /**
61  * \brief Pre-unload cleanup function
62  */
63 void Mouse_Cleanup(void)
64 {
65 }
66
67 // --- VFS Interface ---
68 char *Mouse_Root_ReadDir(tVFS_Node *Node, int Pos)
69 {
70         if( Pos != 0 )  return NULL;
71         return strdup("system");
72 }
73
74 tVFS_Node *Mouse_Root_FindDir(tVFS_Node *Node, const char *Name)
75 {
76         if( strcmp(Name, "system") != 0 )       return NULL;
77         return &gMouse_Pointer.Node;
78 }
79
80 static const char *csaIOCTL_NAMES[] = {DRV_IOCTLNAMES, DRV_JOY_IOCTLNAMES, NULL};
81 /**
82  * \brief IOCtl handler for the mouse
83  */
84 int Mouse_Dev_IOCtl(tVFS_Node *Node, int ID, void *Data)
85 {
86         tJoystick_NumValue      *numval = Data;
87         tPointer        *ptr = Node->ImplPtr;
88         switch(ID)
89         {
90         BASE_IOCTLS(DRV_TYPE_MOUSE, "Mouse", VERSION, csaIOCTL_NAMES);
91
92         case JOY_IOCTL_GETSETAXISLIMIT:
93                 if( !numval || !CheckMem(numval, sizeof(*numval)) )
94                         return -1;
95                 if(numval->Num < 0 || numval->Num >= ptr->FileHeader->NAxies)
96                         return 0;
97                 if(numval->Value != -1)
98                         ptr->AxisLimits[numval->Num] = numval->Value;
99                 return ptr->AxisLimits[numval->Num];
100
101         case JOY_IOCTL_GETSETAXISPOSITION:
102                 if( !numval || !CheckMem(numval, sizeof(*numval)) )
103                         return -1;
104                 if(numval->Num < 0 || numval->Num >= ptr->FileHeader->NAxies)
105                         return 0;
106                 if(numval->Value != -1)
107                         ptr->Axies[numval->Num].CursorPos = numval->Value;
108                 return ptr->Axies[numval->Num].CursorPos;
109         }
110         return -1;
111 }
112
113 /**
114  * \brief Read from a device
115  */
116 size_t Mouse_Dev_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Data)
117 {
118         tPointer *ptr = Node->ImplPtr;
119          int    n_buttons = ptr->FileHeader->NButtons;
120          int    n_axies = ptr->FileHeader->NAxies;
121
122         // TODO: Locking (Acquire)
123
124         Length = MIN(
125                 Length, 
126                 sizeof(tJoystick_FileHeader) + n_axies*sizeof(tJoystick_Axis) + n_buttons
127                 );
128
129         // Mark as checked
130         VFS_MarkAvaliable( Node, 0 );
131
132         // Check if more than the header is requested
133         if( Length > sizeof(tJoystick_FileHeader) )
134         {
135                 // Clear axis values and button states
136                 for( int i = 0; i < n_axies; i ++ )
137                         ptr->Axies[i].CurValue = 0;
138                 for( int i = 0; i < n_buttons; i ++ )
139                         ptr->Buttons[i] = 0;
140
141                 // Rebuild from device list
142                 for( tMouse *dev = ptr->FirstDev; dev; dev = dev->Next )
143                 {
144                         for( int i = 0; i < n_axies; i ++ )
145                                 ptr->Axies[i].CurValue += dev->LastAxisVal[i];
146                         for( int i = 0; i < n_buttons; i ++ )
147                         {
148                                 if( dev->ButtonState & (1 << i) )
149                                         ptr->Buttons[i] = 255;
150                         }
151                 }
152         }
153
154         memcpy( Data, ptr->FileData, Length );
155
156         // TODO: Locking (Release)
157
158         return Length;
159 }
160
161 // --- Device Interface ---
162 /*
163  * Register an input device
164  * - See Input/Mouse/include/mouse.h
165  */
166 tMouse *Mouse_Register(const char *Name, int NumButtons, int NumAxies)
167 {
168         tPointer *target;
169         tMouse   *ret;
170
171         // TODO: Multiple pointers?
172         target = &gMouse_Pointer;
173
174         if( NumButtons > MAX_BUTTONS )
175                 NumButtons = MAX_BUTTONS;
176         if( NumAxies > MAX_AXIES )
177                 NumAxies = MAX_AXIES;
178
179         ret = malloc( sizeof(tMouse) + sizeof(ret->LastAxisVal[0])*NumAxies );
180         if(!ret) {
181                 Log_Error("Mouse", "malloc() failed");
182                 return NULL;
183         }
184         ret->Next = NULL;
185         ret->Pointer = target;
186         ret->NumAxies = NumAxies;
187         ret->NumButtons = NumButtons;
188         ret->ButtonState = 0;
189         memset(ret->LastAxisVal, 0, sizeof(ret->LastAxisVal[0])*NumAxies );
190
191         // Add
192         // TODO: Locking
193         ret->Next = target->FirstDev;
194         target->FirstDev = ret;
195         if( ret->NumAxies <= MAX_AXIES && ret->NumAxies > target->FileHeader->NAxies ) {
196                 // Clear new axis data
197                 memset(
198                         target->Axies + target->FileHeader->NAxies,
199                         0,
200                         (ret->NumAxies - target->FileHeader->NAxies)/sizeof(tJoystick_Axis)
201                         );
202                 target->FileHeader->NAxies = ret->NumAxies;
203                 target->Buttons = (void*)( target->Axies + ret->NumAxies );
204         }
205         if( ret->NumButtons <= MAX_BUTTONS && ret->NumButtons > target->FileHeader->NButtons ) {
206                 // No need to move as buttons can expand out into space
207                 target->FileHeader->NButtons = ret->NumButtons;
208         }
209         // TODO: Locking
210
211         return ret;
212 }
213
214 /*
215  * Remove a mouse instance
216  * - See Input/Mouse/include/mouse.h
217  */
218 void Mouse_RemoveInstance(tMouse *Instance)
219 {
220 }
221
222 /*
223  * Handle a mouse event (movement or button press/release)
224  * - See Input/Mouse/include/mouse.h
225  */
226 void Mouse_HandleEvent(tMouse *Handle, Uint32 ButtonState, int *AxisDeltas)
227 {
228         tPointer *ptr = Handle->Pointer;
229         
230         Handle->ButtonState = ButtonState;
231
232         memcpy(Handle->LastAxisVal, AxisDeltas, sizeof(*AxisDeltas)*Handle->NumAxies);
233
234         // TODO: Acceleration?
235         
236         for( int i = 0; i < Handle->NumAxies; i ++ )
237         {
238                 ptr->Axies[i].CursorPos = MIN(MAX(0, ptr->Axies[i].CursorPos+AxisDeltas[i]), ptr->AxisLimits[i]);
239         }
240         VFS_MarkAvaliable( &ptr->Node, 1 );
241 }
242

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