minor filename fixes
[drnoksnes] / input.c
1
2 #include "menu.h"
3 #include "giz_sdk.h"
4 #if defined(__GP2X__)
5 #include "usbjoy.h"
6 static struct usbjoy *joys[4];
7 static char joyCount = 0;
8 static int buttonMap[4][32];
9 #endif
10
11 struct INPUT Inp;
12 static int repeatCounter = 0;
13 int InputInit()
14 {
15   memset(&Inp,0,sizeof(Inp));
16 #if defined(__GP2X__)
17 int i;
18         for (i=1; i<5; i++)
19         {
20                 struct usbjoy *joy = joy_open(i);
21                 if(joy != NULL)
22                 {
23                         joys[joyCount] = joy;
24                         memset(buttonMap[joyCount],0,sizeof(buttonMap[joyCount]));
25                         buttonMap[joyCount][0] = (1<<INP_BUTTON_A);
26                         buttonMap[joyCount][1] = (1<<INP_BUTTON_B);
27                         buttonMap[joyCount][2] = (1<<INP_BUTTON_X);
28                         buttonMap[joyCount][3] = (1<<INP_BUTTON_Y);
29                         buttonMap[joyCount][4] = (1<<INP_BUTTON_L);
30                         buttonMap[joyCount][5] = (1<<INP_BUTTON_R);
31                         if (joy->numbuttons<10)
32                         {
33                                 buttonMap[joyCount][6] = (1<<INP_BUTTON_SELECT);
34                                 buttonMap[joyCount][7] = (1<<INP_BUTTON_START);
35                         }
36                         else
37                         {
38                                 buttonMap[joyCount][6] = (1<<INP_BUTTON_L);
39                                 buttonMap[joyCount][7] = (1<<INP_BUTTON_R);
40                                 buttonMap[joyCount][8] = (1<<INP_BUTTON_SELECT);
41                                 buttonMap[joyCount][9] = (1<<INP_BUTTON_START);
42                         }
43                         joyCount++;
44                 }
45         }
46 #endif
47   return 0;
48 }
49
50 #if defined(__GP2X__)
51 int InputClose()
52 {
53 int i;
54         for (i=0; i<joyCount; i++)
55         {
56                 joy_close(joys[i]);
57         }
58 }
59
60 int joy_getButton(int joyNumber)
61 {
62 unsigned int key=0;
63         if (joyNumber<joyCount)
64         {
65                 int i;
66                 joy_update(joys[joyNumber]);
67                 if(joy_getaxe(JOYUP, joys[joyNumber])) key|=    (1<<INP_BUTTON_UP);
68                 if(joy_getaxe(JOYDOWN, joys[joyNumber])) key|=   (1<<INP_BUTTON_DOWN);
69                 if(joy_getaxe(JOYLEFT, joys[joyNumber])) key|=   (1<<INP_BUTTON_LEFT);
70                 if(joy_getaxe(JOYRIGHT, joys[joyNumber])) key|= (1<<INP_BUTTON_RIGHT);
71                 for (i = 0; i < joy_buttons(joys[joyNumber]); i++)
72                 {
73                         if (joy_getbutton(i, joys[joyNumber]))
74                                 key|= buttonMap[joyNumber][i];
75                 }
76         }
77         return key;
78 }
79
80 char joy_Count()
81 {
82         return joyCount;
83 }
84 #endif
85
86 int InputUpdate(int EnableDiagnals)
87 {
88   int i=0;
89   unsigned int key=0;
90   // Get input
91 #if defined(__GIZ__)
92   key=gp_getButton(EnableDiagnals);
93   key&= (1<<INP_BUTTON_UP)|
94                         (1<<INP_BUTTON_LEFT)|
95                         (1<<INP_BUTTON_DOWN)|
96                         (1<<INP_BUTTON_RIGHT)|
97                         (1<<INP_BUTTON_HOME)|
98                         (1<<INP_BUTTON_VOL)|
99                         (1<<INP_BUTTON_L)|
100                         (1<<INP_BUTTON_R)|
101                         (1<<INP_BUTTON_REWIND)|
102                         (1<<INP_BUTTON_FORWARD)|
103                         (1<<INP_BUTTON_PLAY)|
104                         (1<<INP_BUTTON_STOP)|
105                         (1<<INP_BUTTON_BRIGHT);
106 #endif
107
108 #if defined(__GP2X__)
109   key=gp_getButton(EnableDiagnals);
110   key&= (1<<INP_BUTTON_UP)|
111                         (1<<INP_BUTTON_LEFT)|
112                         (1<<INP_BUTTON_DOWN)|
113                         (1<<INP_BUTTON_RIGHT)|
114                         (1<<INP_BUTTON_START)|
115                         (1<<INP_BUTTON_SELECT)|
116                         (1<<INP_BUTTON_L)|
117                         (1<<INP_BUTTON_R)|
118                         (1<<INP_BUTTON_A)|
119                         (1<<INP_BUTTON_B)|
120                         (1<<INP_BUTTON_X)|
121                         (1<<INP_BUTTON_Y)|
122                         (1<<INP_BUTTON_VOL_UP)|
123                         (1<<INP_BUTTON_VOL_DOWN)|
124                         (1<<INP_BUTTON_STICK_PUSH);
125   key |= joy_getButton(0);
126 #endif
127   // Find out how long key was pressed for
128   for (i=0;i<32;i++)
129   {
130     int held=Inp.held[i];
131
132     if (key&(1<<i)) held++; else held=0;
133
134     //if (held>=0x80) held&=0xbf; // Keep looping around
135
136     Inp.held[i]=held;
137   }
138
139   // Work out some key repeat values:
140   for (i=0;i<32;i++)
141   {
142     char rep=0;
143     int held=Inp.held[i];
144
145     if (held==1) 
146         {
147                 // Key has just been pressed again, so set repeat by default
148                 rep=1;
149         }
150         else
151         {
152                 // Now make sure key has been held for a period of time
153                 // before auto toggling the repeat flag
154                 if (held>=0x20)
155                 {
156                         repeatCounter++;
157                         if(repeatCounter>15)
158                         {
159                                 rep=1;
160                                 repeatCounter=0;
161                         }
162                 }
163         }
164
165     Inp.repeat[i]=rep;
166   }
167
168   return 0;
169 }