fa8d1331 |
1 | #include <stdio.h> |
13b692eb |
2 | #include <stdlib.h> |
3 | #include <string.h> |
fa8d1331 |
4 | #include <sys/types.h> |
5 | #include <sys/stat.h> |
6 | #include <fcntl.h> |
7 | #include <unistd.h> |
13b692eb |
8 | |
13b692eb |
9 | #include "../common/input.h" |
10 | #include "in_gp2x.h" |
d572cbad |
11 | #include "soc.h" |
13b692eb |
12 | |
13 | #define IN_PREFIX "gp2x:" |
14 | #define IN_GP2X_NBUTTONS 32 |
15 | |
2c600560 |
16 | /* note: in_gp2x hadles combos (if 2 btns have the same bind, |
17 | * both must be pressed for action to happen) */ |
18 | static int in_gp2x_combo_keys = 0; |
19 | static int in_gp2x_combo_acts = 0; |
fa8d1331 |
20 | static int gpiodev = -1; /* Wiz only */ |
21 | |
22 | static int (*in_gp2x_get_bits)(void); |
2c600560 |
23 | |
13b692eb |
24 | enum { BTN_UP = 0, BTN_LEFT = 2, BTN_DOWN = 4, BTN_RIGHT = 6, |
25 | BTN_START = 8, BTN_SELECT = 9, BTN_L = 10, BTN_R = 11, |
26 | BTN_A = 12, BTN_B = 13, BTN_X = 14, BTN_Y = 15, |
27 | BTN_VOL_UP = 23, BTN_VOL_DOWN = 22, BTN_PUSH = 27 }; |
28 | |
29 | static const char * const in_gp2x_prefix = IN_PREFIX; |
30 | static const char * const in_gp2x_keys[IN_GP2X_NBUTTONS] = { |
31 | [0 ... IN_GP2X_NBUTTONS-1] = NULL, |
32 | [BTN_UP] = "UP", [BTN_LEFT] = "LEFT", [BTN_DOWN] = "DOWN", [BTN_RIGHT] = "RIGHT", |
33 | [BTN_START] = "START", [BTN_SELECT] = "SELECT", [BTN_L] = "L", [BTN_R] = "R", |
34 | [BTN_A] = "A", [BTN_B] = "B", [BTN_X] = "X", [BTN_Y] = "Y", |
35 | [BTN_VOL_DOWN]= "VOL DOWN", [BTN_VOL_UP] = "VOL UP", |
36 | [BTN_PUSH] = "PUSH" |
37 | }; |
38 | |
13b692eb |
39 | |
fa8d1331 |
40 | static int in_gp2x_get_mmsp2_bits(void) |
13b692eb |
41 | { |
d572cbad |
42 | extern volatile unsigned short *gp2x_memregs; |
13b692eb |
43 | int value; |
44 | value = gp2x_memregs[0x1198>>1] & 0xff; // GPIO M |
45 | if (value == 0xFD) value = 0xFA; |
46 | if (value == 0xF7) value = 0xEB; |
47 | if (value == 0xDF) value = 0xAF; |
48 | if (value == 0x7F) value = 0xBE; |
49 | value |= gp2x_memregs[0x1184>>1] & 0xFF00; // GPIO C |
50 | value |= gp2x_memregs[0x1186>>1] << 16; // GPIO D |
51 | value = ~value & 0x08c0ff55; |
52 | |
53 | return value; |
13b692eb |
54 | } |
55 | |
fa8d1331 |
56 | static int in_gp2x_get_wiz_bits(void) |
57 | { |
58 | int value = 0; |
59 | read(gpiodev, &value, 4); |
60 | if (value & 0x02) |
61 | value |= 0x05; |
62 | if (value & 0x08) |
63 | value |= 0x14; |
64 | if (value & 0x20) |
65 | value |= 0x50; |
66 | if (value & 0x80) |
67 | value |= 0x41; |
68 | |
69 | /* convert to GP2X style */ |
70 | value &= 0x7ff55; |
71 | if (value & (1 << 16)) |
72 | value |= 1 << BTN_VOL_UP; |
73 | if (value & (1 << 17)) |
74 | value |= 1 << BTN_VOL_DOWN; |
75 | if (value & (1 << 18)) |
76 | value |= 1 << BTN_PUSH; |
77 | value &= ~0x70000; |
78 | |
79 | return value; |
80 | } |
81 | |
0114ccc4 |
82 | #ifdef FAKE_IN_GP2X |
83 | static int in_gp2x_get_fake_bits(void) |
84 | { |
85 | extern int current_keys; |
86 | return current_keys; |
87 | } |
88 | #endif |
89 | |
90 | static void in_gp2x_probe(void) |
91 | { |
92 | gp2x_soc_t soc; |
93 | |
94 | soc = soc_detect(); |
95 | switch (soc) |
96 | { |
97 | case SOCID_MMSP2: |
98 | in_gp2x_get_bits = in_gp2x_get_mmsp2_bits; |
99 | break; |
100 | case SOCID_POLLUX: |
101 | gpiodev = open("/dev/GPIO", O_RDONLY); |
102 | if (gpiodev < 0) { |
103 | perror("in_gp2x: couldn't open /dev/GPIO"); |
104 | return; |
105 | } |
106 | in_gp2x_get_bits = in_gp2x_get_wiz_bits; |
107 | break; |
108 | default: |
109 | #ifdef FAKE_IN_GP2X |
110 | in_gp2x_get_bits = in_gp2x_get_fake_bits; |
111 | break; |
112 | #endif |
113 | return; |
114 | } |
115 | |
116 | in_register(IN_PREFIX "GP2X pad", IN_DRVID_GP2X, -1, (void *)1, 1); |
117 | } |
118 | |
119 | static void in_gp2x_free(void *drv_data) |
120 | { |
121 | if (gpiodev >= 0) { |
122 | close(gpiodev); |
123 | gpiodev = -1; |
124 | } |
125 | } |
126 | |
127 | static int in_gp2x_get_bind_count(void) |
128 | { |
129 | return IN_GP2X_NBUTTONS; |
130 | } |
131 | |
13b692eb |
132 | /* returns bitfield of binds of pressed buttons */ |
133 | int in_gp2x_update(void *drv_data, int *binds) |
134 | { |
2c600560 |
135 | int i, keys, ret = 0; |
136 | |
fa8d1331 |
137 | keys = in_gp2x_get_bits(); |
13b692eb |
138 | |
2c600560 |
139 | if (keys & in_gp2x_combo_keys) |
140 | return in_combos_do(keys, binds, BTN_PUSH, in_gp2x_combo_keys, in_gp2x_combo_acts); |
13b692eb |
141 | |
2c600560 |
142 | for (i = 0; keys; i++) { |
143 | if (keys & 1) |
13b692eb |
144 | ret |= binds[i]; |
2c600560 |
145 | keys >>= 1; |
13b692eb |
146 | } |
147 | |
148 | return ret; |
149 | } |
150 | |
151 | int in_gp2x_update_keycode(void *data, int *is_down) |
152 | { |
153 | static int old_val = 0; |
154 | int val, diff, i; |
155 | |
fa8d1331 |
156 | val = in_gp2x_get_bits(); |
13b692eb |
157 | diff = val ^ old_val; |
158 | if (diff == 0) |
159 | return -1; |
160 | |
161 | /* take one bit only */ |
162 | for (i = 0; i < sizeof(diff)*8; i++) |
163 | if (diff & (1<<i)) |
164 | break; |
165 | |
166 | old_val ^= 1 << i; |
167 | |
168 | if (is_down) |
169 | *is_down = !!(val & (1<<i)); |
170 | return i; |
171 | } |
172 | |
82abf46f |
173 | static const struct { |
174 | short key; |
175 | short pbtn; |
176 | } key_pbtn_map[] = |
177 | { |
178 | { BTN_UP, PBTN_UP }, |
179 | { BTN_DOWN, PBTN_DOWN }, |
180 | { BTN_LEFT, PBTN_LEFT }, |
181 | { BTN_RIGHT, PBTN_RIGHT }, |
182 | { BTN_B, PBTN_MOK }, |
183 | { BTN_X, PBTN_MBACK }, |
184 | { BTN_A, PBTN_MA2 }, |
185 | { BTN_Y, PBTN_MA3 }, |
186 | { BTN_L, PBTN_L }, |
187 | { BTN_R, PBTN_R }, |
188 | { BTN_SELECT, PBTN_MENU }, |
189 | }; |
190 | |
191 | #define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0])) |
192 | |
13b692eb |
193 | static int in_gp2x_menu_translate(int keycode) |
194 | { |
82abf46f |
195 | int i; |
196 | if (keycode < 0) |
197 | { |
198 | /* menu -> kc */ |
199 | keycode = -keycode; |
200 | for (i = 0; i < KEY_PBTN_MAP_SIZE; i++) |
201 | if (key_pbtn_map[i].pbtn == keycode) |
202 | return key_pbtn_map[i].key; |
13b692eb |
203 | } |
82abf46f |
204 | else |
205 | { |
206 | for (i = 0; i < KEY_PBTN_MAP_SIZE; i++) |
207 | if (key_pbtn_map[i].key == keycode) |
208 | return key_pbtn_map[i].pbtn; |
209 | } |
210 | |
211 | return 0; |
13b692eb |
212 | } |
213 | |
214 | static int in_gp2x_get_key_code(const char *key_name) |
215 | { |
216 | int i; |
217 | |
218 | for (i = 0; i < IN_GP2X_NBUTTONS; i++) { |
219 | const char *k = in_gp2x_keys[i]; |
220 | if (k != NULL && strcasecmp(k, key_name) == 0) |
221 | return i; |
222 | } |
223 | |
224 | return -1; |
225 | } |
226 | |
227 | static const char *in_gp2x_get_key_name(int keycode) |
228 | { |
229 | const char *name = NULL; |
230 | if (keycode >= 0 && keycode < IN_GP2X_NBUTTONS) |
231 | name = in_gp2x_keys[keycode]; |
232 | if (name == NULL) |
233 | name = "Unkn"; |
234 | |
235 | return name; |
236 | } |
237 | |
238 | static const struct { |
239 | short code; |
240 | short bit; |
241 | } in_gp2x_def_binds[] = |
242 | { |
243 | /* MXYZ SACB RLDU */ |
244 | { BTN_UP, 0 }, |
245 | { BTN_DOWN, 1 }, |
246 | { BTN_LEFT, 2 }, |
247 | { BTN_RIGHT, 3 }, |
248 | { BTN_X, 4 }, /* B */ |
249 | { BTN_B, 5 }, /* C */ |
250 | { BTN_A, 6 }, /* A */ |
251 | { BTN_START, 7 }, |
82abf46f |
252 | { BTN_SELECT, 23 }, /* menu */ |
253 | { BTN_Y, 26 }, /* switch rend */ |
254 | { BTN_L, 27 }, /* save state */ |
255 | { BTN_R, 28 }, /* load state */ |
256 | { BTN_VOL_UP, 29 }, /* vol up */ |
257 | { BTN_VOL_DOWN, 30 }, /* vol down */ |
13b692eb |
258 | }; |
259 | |
260 | #define DEF_BIND_COUNT (sizeof(in_gp2x_def_binds) / sizeof(in_gp2x_def_binds[0])) |
261 | |
262 | static void in_gp2x_get_def_binds(int *binds) |
263 | { |
264 | int i; |
265 | |
266 | for (i = 0; i < DEF_BIND_COUNT; i++) |
267 | binds[in_gp2x_def_binds[i].code] = 1 << in_gp2x_def_binds[i].bit; |
268 | } |
269 | |
270 | /* remove binds of missing keys, count remaining ones */ |
271 | static int in_gp2x_clean_binds(void *drv_data, int *binds) |
272 | { |
82abf46f |
273 | int i, count = 0, have_vol = 0, have_menu = 0; |
13b692eb |
274 | |
275 | for (i = 0; i < IN_GP2X_NBUTTONS; i++) { |
276 | if (in_gp2x_keys[i] == NULL) |
277 | binds[i] = binds[i + IN_GP2X_NBUTTONS] = 0; |
82abf46f |
278 | if (binds[i]) { |
13b692eb |
279 | count++; |
82abf46f |
280 | if (binds[i] & ((1 << 29)|(1 << 30))) |
281 | have_vol = 1; |
282 | if (binds[i] & (1 << 23)) |
283 | have_menu = 1; |
284 | } |
13b692eb |
285 | } |
286 | |
82abf46f |
287 | /* autobind some important keys, if they are unbound */ |
288 | if (!have_vol && binds[BTN_VOL_UP] == 0 && binds[BTN_VOL_DOWN] == 0) { |
289 | binds[BTN_VOL_UP] = 1 << 29; |
290 | binds[BTN_VOL_DOWN] = 1 << 30; |
291 | } |
292 | |
293 | if (!have_menu && binds[BTN_SELECT] == 0) |
294 | binds[BTN_SELECT] = 1 << 23; |
295 | |
2c600560 |
296 | in_combos_find(binds, BTN_PUSH, &in_gp2x_combo_keys, &in_gp2x_combo_acts); |
297 | |
13b692eb |
298 | return count; |
299 | |
300 | } |
301 | |
302 | void in_gp2x_init(void *vdrv) |
303 | { |
304 | in_drv_t *drv = vdrv; |
305 | |
2c600560 |
306 | in_gp2x_combo_keys = in_gp2x_combo_acts = 0; |
307 | |
13b692eb |
308 | drv->prefix = in_gp2x_prefix; |
309 | drv->probe = in_gp2x_probe; |
fa8d1331 |
310 | drv->free = in_gp2x_free; |
13b692eb |
311 | drv->get_bind_count = in_gp2x_get_bind_count; |
312 | drv->get_def_binds = in_gp2x_get_def_binds; |
313 | drv->clean_binds = in_gp2x_clean_binds; |
314 | drv->menu_translate = in_gp2x_menu_translate; |
315 | drv->get_key_code = in_gp2x_get_key_code; |
316 | drv->get_key_name = in_gp2x_get_key_name; |
317 | drv->update_keycode = in_gp2x_update_keycode; |
318 | } |
319 | |