ee2a3bdf |
1 | #include <stdio.h> |
217d08bc |
2 | #include <stdlib.h> |
3 | #include <string.h> |
ee2a3bdf |
4 | #include <sys/types.h> |
5 | #include <sys/stat.h> |
6 | #include <fcntl.h> |
7 | #include <unistd.h> |
217d08bc |
8 | |
217d08bc |
9 | #include "../common/input.h" |
10 | #include "in_gp2x.h" |
42171343 |
11 | #include "soc.h" |
217d08bc |
12 | |
13 | #define IN_PREFIX "gp2x:" |
14 | #define IN_GP2X_NBUTTONS 32 |
15 | |
9025b931 |
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; |
ee2a3bdf |
20 | static int gpiodev = -1; /* Wiz only */ |
21 | |
22 | static int (*in_gp2x_get_bits)(void); |
9025b931 |
23 | |
217d08bc |
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; |
3328d53b |
30 | static const char *in_gp2x_keys[IN_GP2X_NBUTTONS] = { |
217d08bc |
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 | |
217d08bc |
39 | |
ee2a3bdf |
40 | static int in_gp2x_get_mmsp2_bits(void) |
217d08bc |
41 | { |
42171343 |
42 | extern volatile unsigned short *gp2x_memregs; |
217d08bc |
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; |
217d08bc |
54 | } |
55 | |
ee2a3bdf |
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 | |
c18c15ae |
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 | |
093b8a42 |
116 | in_register(IN_PREFIX "GP2X pad", IN_DRVID_GP2X, -1, (void *)1, |
117 | IN_GP2X_NBUTTONS, 1); |
c18c15ae |
118 | } |
119 | |
120 | static void in_gp2x_free(void *drv_data) |
121 | { |
122 | if (gpiodev >= 0) { |
123 | close(gpiodev); |
124 | gpiodev = -1; |
125 | } |
126 | } |
127 | |
128 | static int in_gp2x_get_bind_count(void) |
129 | { |
130 | return IN_GP2X_NBUTTONS; |
131 | } |
132 | |
093b8a42 |
133 | /* ORs result with pressed buttons */ |
134 | int in_gp2x_update(void *drv_data, const int *binds, int *result) |
217d08bc |
135 | { |
093b8a42 |
136 | int type_start = 0; |
137 | int i, t, keys; |
9025b931 |
138 | |
ee2a3bdf |
139 | keys = in_gp2x_get_bits(); |
217d08bc |
140 | |
093b8a42 |
141 | if (keys & in_gp2x_combo_keys) { |
142 | result[IN_BINDTYPE_EMU] = in_combos_do(keys, binds, BTN_PUSH, |
143 | in_gp2x_combo_keys, in_gp2x_combo_acts); |
144 | type_start = IN_BINDTYPE_PLAYER12; |
145 | } |
146 | |
147 | for (i = 0; keys; i++, keys >>= 1) { |
148 | if (!(keys & 1)) |
149 | continue; |
217d08bc |
150 | |
093b8a42 |
151 | for (t = type_start; t < IN_BINDTYPE_COUNT; t++) |
152 | result[t] |= binds[IN_BIND_OFFS(i, t)]; |
217d08bc |
153 | } |
154 | |
093b8a42 |
155 | return 0; |
217d08bc |
156 | } |
157 | |
158 | int in_gp2x_update_keycode(void *data, int *is_down) |
159 | { |
160 | static int old_val = 0; |
161 | int val, diff, i; |
162 | |
ee2a3bdf |
163 | val = in_gp2x_get_bits(); |
217d08bc |
164 | diff = val ^ old_val; |
165 | if (diff == 0) |
166 | return -1; |
167 | |
168 | /* take one bit only */ |
169 | for (i = 0; i < sizeof(diff)*8; i++) |
170 | if (diff & (1<<i)) |
171 | break; |
172 | |
173 | old_val ^= 1 << i; |
174 | |
175 | if (is_down) |
176 | *is_down = !!(val & (1<<i)); |
177 | return i; |
178 | } |
179 | |
991473ad |
180 | static const struct { |
181 | short key; |
182 | short pbtn; |
183 | } key_pbtn_map[] = |
184 | { |
185 | { BTN_UP, PBTN_UP }, |
186 | { BTN_DOWN, PBTN_DOWN }, |
187 | { BTN_LEFT, PBTN_LEFT }, |
188 | { BTN_RIGHT, PBTN_RIGHT }, |
189 | { BTN_B, PBTN_MOK }, |
190 | { BTN_X, PBTN_MBACK }, |
191 | { BTN_A, PBTN_MA2 }, |
192 | { BTN_Y, PBTN_MA3 }, |
193 | { BTN_L, PBTN_L }, |
194 | { BTN_R, PBTN_R }, |
195 | { BTN_SELECT, PBTN_MENU }, |
196 | }; |
197 | |
198 | #define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0])) |
199 | |
217d08bc |
200 | static int in_gp2x_menu_translate(int keycode) |
201 | { |
991473ad |
202 | int i; |
203 | if (keycode < 0) |
204 | { |
205 | /* menu -> kc */ |
206 | keycode = -keycode; |
207 | for (i = 0; i < KEY_PBTN_MAP_SIZE; i++) |
208 | if (key_pbtn_map[i].pbtn == keycode) |
209 | return key_pbtn_map[i].key; |
217d08bc |
210 | } |
991473ad |
211 | else |
212 | { |
213 | for (i = 0; i < KEY_PBTN_MAP_SIZE; i++) |
214 | if (key_pbtn_map[i].key == keycode) |
215 | return key_pbtn_map[i].pbtn; |
216 | } |
217 | |
218 | return 0; |
217d08bc |
219 | } |
220 | |
221 | static int in_gp2x_get_key_code(const char *key_name) |
222 | { |
223 | int i; |
224 | |
225 | for (i = 0; i < IN_GP2X_NBUTTONS; i++) { |
226 | const char *k = in_gp2x_keys[i]; |
227 | if (k != NULL && strcasecmp(k, key_name) == 0) |
228 | return i; |
229 | } |
230 | |
231 | return -1; |
232 | } |
233 | |
234 | static const char *in_gp2x_get_key_name(int keycode) |
235 | { |
236 | const char *name = NULL; |
237 | if (keycode >= 0 && keycode < IN_GP2X_NBUTTONS) |
238 | name = in_gp2x_keys[keycode]; |
239 | if (name == NULL) |
240 | name = "Unkn"; |
241 | |
242 | return name; |
243 | } |
244 | |
245 | static const struct { |
246 | short code; |
093b8a42 |
247 | char btype; |
248 | char bit; |
217d08bc |
249 | } in_gp2x_def_binds[] = |
250 | { |
251 | /* MXYZ SACB RLDU */ |
093b8a42 |
252 | { BTN_UP, IN_BINDTYPE_PLAYER12, 0 }, |
253 | { BTN_DOWN, IN_BINDTYPE_PLAYER12, 1 }, |
254 | { BTN_LEFT, IN_BINDTYPE_PLAYER12, 2 }, |
255 | { BTN_RIGHT, IN_BINDTYPE_PLAYER12, 3 }, |
256 | { BTN_X, IN_BINDTYPE_PLAYER12, 4 }, /* B */ |
257 | { BTN_B, IN_BINDTYPE_PLAYER12, 5 }, /* C */ |
258 | { BTN_A, IN_BINDTYPE_PLAYER12, 6 }, /* A */ |
259 | { BTN_START, IN_BINDTYPE_PLAYER12, 7 }, |
260 | { BTN_SELECT, IN_BINDTYPE_EMU, PEVB_MENU }, |
cc41eb4f |
261 | // { BTN_Y, IN_BINDTYPE_EMU, PEVB_SWITCH_RND }, |
093b8a42 |
262 | { BTN_L, IN_BINDTYPE_EMU, PEVB_STATE_SAVE }, |
263 | { BTN_R, IN_BINDTYPE_EMU, PEVB_STATE_LOAD }, |
264 | { BTN_VOL_UP, IN_BINDTYPE_EMU, PEVB_VOL_UP }, |
265 | { BTN_VOL_DOWN, IN_BINDTYPE_EMU, PEVB_VOL_DOWN }, |
217d08bc |
266 | }; |
267 | |
268 | #define DEF_BIND_COUNT (sizeof(in_gp2x_def_binds) / sizeof(in_gp2x_def_binds[0])) |
269 | |
270 | static void in_gp2x_get_def_binds(int *binds) |
271 | { |
272 | int i; |
273 | |
274 | for (i = 0; i < DEF_BIND_COUNT; i++) |
093b8a42 |
275 | binds[IN_BIND_OFFS(in_gp2x_def_binds[i].code, in_gp2x_def_binds[i].btype)] = |
276 | 1 << in_gp2x_def_binds[i].bit; |
217d08bc |
277 | } |
278 | |
279 | /* remove binds of missing keys, count remaining ones */ |
093b8a42 |
280 | static int in_gp2x_clean_binds(void *drv_data, int *binds, int *def_binds) |
217d08bc |
281 | { |
991473ad |
282 | int i, count = 0, have_vol = 0, have_menu = 0; |
217d08bc |
283 | |
284 | for (i = 0; i < IN_GP2X_NBUTTONS; i++) { |
093b8a42 |
285 | int t, eb, offs; |
286 | for (t = 0; t < IN_BINDTYPE_COUNT; t++) { |
287 | offs = IN_BIND_OFFS(i, t); |
288 | if (in_gp2x_keys[i] == NULL) |
289 | binds[offs] = def_binds[offs] = 0; |
290 | if (binds[offs]) |
291 | count++; |
991473ad |
292 | } |
093b8a42 |
293 | eb = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)]; |
294 | if (eb & (PEV_VOL_DOWN|PEV_VOL_UP)) |
295 | have_vol = 1; |
296 | if (eb & PEV_MENU) |
297 | have_menu = 1; |
217d08bc |
298 | } |
299 | |
991473ad |
300 | /* autobind some important keys, if they are unbound */ |
301 | if (!have_vol && binds[BTN_VOL_UP] == 0 && binds[BTN_VOL_DOWN] == 0) { |
093b8a42 |
302 | binds[IN_BIND_OFFS(BTN_VOL_UP, IN_BINDTYPE_EMU)] = PEV_VOL_UP; |
303 | binds[IN_BIND_OFFS(BTN_VOL_DOWN, IN_BINDTYPE_EMU)] = PEV_VOL_DOWN; |
304 | count += 2; |
991473ad |
305 | } |
306 | |
093b8a42 |
307 | if (!have_menu) { |
308 | binds[IN_BIND_OFFS(BTN_SELECT, IN_BINDTYPE_EMU)] = PEV_MENU; |
309 | count++; |
310 | } |
991473ad |
311 | |
9025b931 |
312 | in_combos_find(binds, BTN_PUSH, &in_gp2x_combo_keys, &in_gp2x_combo_acts); |
313 | |
217d08bc |
314 | return count; |
217d08bc |
315 | } |
316 | |
317 | void in_gp2x_init(void *vdrv) |
318 | { |
319 | in_drv_t *drv = vdrv; |
21ecaf23 |
320 | gp2x_soc_t soc; |
217d08bc |
321 | |
21ecaf23 |
322 | soc = soc_detect(); |
323 | if (soc == SOCID_POLLUX) |
324 | in_gp2x_keys[BTN_START] = "MENU"; |
325 | |
9025b931 |
326 | in_gp2x_combo_keys = in_gp2x_combo_acts = 0; |
327 | |
217d08bc |
328 | drv->prefix = in_gp2x_prefix; |
329 | drv->probe = in_gp2x_probe; |
ee2a3bdf |
330 | drv->free = in_gp2x_free; |
217d08bc |
331 | drv->get_bind_count = in_gp2x_get_bind_count; |
332 | drv->get_def_binds = in_gp2x_get_def_binds; |
333 | drv->clean_binds = in_gp2x_clean_binds; |
334 | drv->menu_translate = in_gp2x_menu_translate; |
335 | drv->get_key_code = in_gp2x_get_key_code; |
336 | drv->get_key_name = in_gp2x_get_key_name; |
337 | drv->update_keycode = in_gp2x_update_keycode; |
338 | } |
339 | |