a35f913e39ce80acedca772d45c60e2a19263e99
[tpg/acess2.git] / input.c
1 /*
2  * Acess2 GUI (AxWin) Version 3
3  * - By John Hodge (thePowersGang)
4  *
5  * input.c
6  * - Input abstraction
7  */
8 #include <common.h>
9 #include <acess/sys.h>
10
11 // TODO: Move out to a common header
12 typedef struct
13 {
14         int Num;
15         int Value;
16 } tNumValue;
17 #define JOY_IOCTL_GETSETAXISLIMIT       6
18 #define JOY_IOCTL_GETSETAXISPOSITION    7
19
20 // === IMPORTS ===
21 extern void     Video_SetCursorPos(short X, short Y);
22 extern void     WM_Input_MouseMoved(int OldX, int OldY, int NewX, int NewY);
23 extern void     WM_Input_MouseButton(int X, int Y, int Button, int Pressed);
24 const char      *gsMouseDevice;
25 extern int      giTerminalFD;
26 extern int      giScreenWidth;
27 extern int      giScreenHeight;
28
29 // === GLOBALS ===
30  int    giMouseFD;
31  int    giInput_MouseButtonState;
32  int    giInput_MouseX, giInput_MouseY;
33
34 // === CODE ===
35 int Input_Init(void)
36 {
37         tNumValue       num_value;
38
39         // Open mouse for RW
40         giMouseFD = open(gsMouseDevice, 3);
41
42         // Set mouse limits
43         // TODO: Update these if the screen resolution changes
44         num_value.Num = 0;      num_value.Value = giScreenWidth;
45         ioctl(giMouseFD, JOY_IOCTL_GETSETAXISLIMIT, &num_value);
46         num_value.Value = giScreenWidth/2;
47         giInput_MouseX = giScreenWidth/2;
48         ioctl(giMouseFD, JOY_IOCTL_GETSETAXISPOSITION, &num_value);
49
50         num_value.Num = 1;      num_value.Value = giScreenHeight;
51         ioctl(giMouseFD, JOY_IOCTL_GETSETAXISLIMIT, &num_value);
52         num_value.Value = giScreenHeight/2;
53         giInput_MouseY = giScreenHeight/2;
54         ioctl(giMouseFD, JOY_IOCTL_GETSETAXISPOSITION, &num_value);
55
56         return 0;
57 }
58
59 void Input_FillSelect(int *nfds, fd_set *set)
60 {
61         if(*nfds < giTerminalFD)        *nfds = giTerminalFD;
62         if(*nfds < giMouseFD)   *nfds = giMouseFD;
63         FD_SET(giTerminalFD, set);
64         FD_SET(giMouseFD, set);
65 }
66
67 void Input_HandleSelect(fd_set *set)
68 {
69         if(FD_ISSET(giTerminalFD, set))
70         {
71                 uint32_t        codepoint;
72                 if( read(giTerminalFD, &codepoint, sizeof(codepoint)) != sizeof(codepoint) )
73                 {
74                         // oops, error
75                 }
76                 // TODO: pass on to message handler
77                 _SysDebug("Keypress 0x%x", codepoint);
78         }
79
80         if(FD_ISSET(giMouseFD, set))
81         {
82                  int    i;
83                 struct sMouseInfo {
84                         uint16_t        NAxies;
85                         uint16_t        NButtons;
86                         struct sMouseAxis {
87                                  int16_t        MinValue;
88                                  int16_t        MaxValue;
89                                  int16_t        CurValue;
90                                 uint16_t        CursorPos;
91                         }       Axies[2];
92                         uint8_t Buttons[3];
93                 }       mouseinfo;
94         
95                 seek(giMouseFD, 0, SEEK_SET);
96                 if( read(giMouseFD, &mouseinfo, sizeof(mouseinfo)) != sizeof(mouseinfo) )
97                 {
98                         // Not a 3 button mouse, oops
99                         return ;
100                 }
101
102                 // Handle movement
103                 Video_SetCursorPos( mouseinfo.Axies[0].CursorPos, mouseinfo.Axies[1].CursorPos );
104
105                 WM_Input_MouseMoved(
106                         giInput_MouseX, giInput_MouseY,
107                         mouseinfo.Axies[0].CursorPos, mouseinfo.Axies[1].CursorPos
108                         );
109                 giInput_MouseX = mouseinfo.Axies[0].CursorPos;
110                 giInput_MouseY = mouseinfo.Axies[1].CursorPos;
111
112                 for( i = 0; i < mouseinfo.NButtons; i ++ )
113                 {
114                         int bit = 1 << i;
115                         int cur = mouseinfo.Buttons[i] > 128;
116
117                         if( !!(giInput_MouseButtonState & bit) != cur )
118                         {
119                                 WM_Input_MouseButton(giInput_MouseX, giInput_MouseY, i, cur);
120                                 // Flip button state
121                                 giInput_MouseButtonState ^= bit;
122                         }
123                 }
124         }
125 }

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