Usermode/libc - Fix strchr and strrchr behavior
[tpg/acess2.git] / KernelLand / Modules / Input / PS2KbMouse / ps2mouse.c
1 /*\r
2  * Acess2 Mouse Driver\r
3  */\r
4 #define DEBUG   0\r
5 #include <acess.h>\r
6 #include <modules.h>\r
7 #include <vfs.h>\r
8 #include <Input/Mouse/include/mouse.h>\r
9 #include "common.h"\r
10 \r
11 // == CONSTANTS ==\r
12 #define NUM_AXIES       2       // X+Y\r
13 #define NUM_BUTTONS     5       // Left, Right, Scroll Click, Scroll Up, Scroll Down\r
14 \r
15 // == PROTOTYPES ==\r
16 // - Internal -\r
17  int    PS2Mouse_Install(char **Arguments);\r
18 void    PS2Mouse_HandleInterrupt(Uint8 InputByte);\r
19 \r
20 // == GLOBALS ==\r
21 void    (*gpPS2Mouse_EnableFcn)(void);\r
22 // - Settings\r
23  int    giPS2Mouse_Sensitivity = 1;\r
24 // - Internal State\r
25  int    giPS2Mouse_Cycle = 0;   // IRQ Position\r
26 Uint8   gaPS2Mouse_Bytes[4] = {0,0,0,0};\r
27 tMouse  *gpPS2Mouse_Handle;\r
28 \r
29 // == CODE ==\r
30 int PS2Mouse_Install(char **Arguments)\r
31 {\r
32         gpPS2Mouse_Handle = Mouse_Register("PS2Mouse", NUM_AXIES, NUM_BUTTONS);\r
33 \r
34         // Initialise Mouse Controller\r
35         giPS2Mouse_Cycle = 0;   // Set Current Cycle position\r
36         gpPS2Mouse_EnableFcn();\r
37 \r
38         return MODULE_ERR_OK;\r
39 }\r
40 \r
41 /* Handle Mouse Interrupt\r
42  */\r
43 void PS2Mouse_HandleInterrupt(Uint8 InputByte)\r
44 {\r
45         Uint8   flags;\r
46          int    d[2], d_accel[2];\r
47         \r
48         // Gather mouse data\r
49         gaPS2Mouse_Bytes[giPS2Mouse_Cycle] = InputByte;\r
50         // - If bit 3 of the first byte is not set, it's not a valid packet?\r
51         if(giPS2Mouse_Cycle == 0 && !(gaPS2Mouse_Bytes[0] & 0x08) )\r
52                 return ;\r
53 \r
54         // 3 bytes per packet\r
55         giPS2Mouse_Cycle++;\r
56         if(giPS2Mouse_Cycle < 3)\r
57                 return ;\r
58         giPS2Mouse_Cycle = 0;\r
59 \r
60         // Actual Processing (once we have all bytes)   \r
61         flags = gaPS2Mouse_Bytes[0];\r
62 \r
63         LOG("flags = 0x%x", flags);\r
64         \r
65         // Check for X/Y Overflow\r
66         if(flags & 0xC0)        return;\r
67                 \r
68         // Calculate dX and dY\r
69         d[0] = gaPS2Mouse_Bytes[1];     if(flags & 0x10) d[0] = -(256-d[0]);    // x\r
70         d[1] = gaPS2Mouse_Bytes[2];     if(flags & 0x20) d[1] = -(256-d[1]);    // y\r
71         d[1] = -d[1];   // Y is negated\r
72         LOG("RAW dx=%i, dy=%i\n", d[0], d[1]);\r
73         // Apply scaling\r
74         // TODO: Apply a form of curve to the mouse movement (dx*log(dx), dx^k?)\r
75         // TODO: Independent sensitivities?\r
76         // TODO: Disable acceleration via a flag?\r
77         d_accel[0] = d[0]*giPS2Mouse_Sensitivity;\r
78         d_accel[1] = d[1]*giPS2Mouse_Sensitivity;\r
79         \r
80         // TODO: Scroll wheel?  \r
81         Mouse_HandleEvent(gpPS2Mouse_Handle, (flags & 7), d_accel);\r
82 }\r
83 \r

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