9 #include "../common/input.h"
11 #include "plat_gp2x.h"
13 #define IN_PREFIX "gp2x:"
14 #define IN_GP2X_NBUTTONS 32
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;
20 static int gpiodev = -1; /* Wiz only */
22 static int (*in_gp2x_get_bits)(void);
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 };
29 static const char * const in_gp2x_prefix = IN_PREFIX;
30 static const char *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",
40 static int in_gp2x_get_mmsp2_bits(void)
42 extern volatile unsigned short *gp2x_memregs;
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;
56 static int in_gp2x_get_wiz_bits(void)
59 r = read(gpiodev, &value, 4);
69 /* convert to GP2X style */
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;
83 volatile unsigned short *gp2x_memregs;
86 static int in_gp2x_get_fake_bits(void)
88 extern int current_keys;
93 static void in_gp2x_probe(void)
98 in_gp2x_get_bits = in_gp2x_get_mmsp2_bits;
101 gpiodev = open("/dev/GPIO", O_RDONLY);
103 perror("in_gp2x: couldn't open /dev/GPIO");
106 in_gp2x_get_bits = in_gp2x_get_wiz_bits;
108 // we'll use evdev for Caanoo
111 in_gp2x_get_bits = in_gp2x_get_fake_bits;
117 in_register(IN_PREFIX "GP2X pad", IN_DRVID_GP2X, -1, (void *)1,
118 IN_GP2X_NBUTTONS, in_gp2x_keys, 1);
121 static void in_gp2x_free(void *drv_data)
129 static int in_gp2x_get_bind_count(void)
131 return IN_GP2X_NBUTTONS;
134 /* ORs result with pressed buttons */
135 int in_gp2x_update(void *drv_data, const int *binds, int *result)
140 keys = in_gp2x_get_bits();
142 if (keys & in_gp2x_combo_keys) {
143 result[IN_BINDTYPE_EMU] = in_combos_do(keys, binds, BTN_PUSH,
144 in_gp2x_combo_keys, in_gp2x_combo_acts);
145 type_start = IN_BINDTYPE_PLAYER12;
148 for (i = 0; keys; i++, keys >>= 1) {
152 for (t = type_start; t < IN_BINDTYPE_COUNT; t++)
153 result[t] |= binds[IN_BIND_OFFS(i, t)];
159 int in_gp2x_update_keycode(void *data, int *is_down)
161 static int old_val = 0;
164 val = in_gp2x_get_bits();
165 diff = val ^ old_val;
169 /* take one bit only */
170 for (i = 0; i < sizeof(diff)*8; i++)
177 *is_down = !!(val & (1<<i));
181 static const struct {
187 { BTN_DOWN, PBTN_DOWN },
188 { BTN_LEFT, PBTN_LEFT },
189 { BTN_RIGHT, PBTN_RIGHT },
191 { BTN_X, PBTN_MBACK },
196 { BTN_SELECT, PBTN_MENU },
199 #define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
201 static int in_gp2x_menu_translate(void *drv_data, int keycode)
208 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
209 if (key_pbtn_map[i].pbtn == keycode)
210 return key_pbtn_map[i].key;
214 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
215 if (key_pbtn_map[i].key == keycode)
216 return key_pbtn_map[i].pbtn;
222 static const struct {
226 } in_gp2x_def_binds[] =
229 { BTN_UP, IN_BINDTYPE_PLAYER12, 0 },
230 { BTN_DOWN, IN_BINDTYPE_PLAYER12, 1 },
231 { BTN_LEFT, IN_BINDTYPE_PLAYER12, 2 },
232 { BTN_RIGHT, IN_BINDTYPE_PLAYER12, 3 },
233 { BTN_X, IN_BINDTYPE_PLAYER12, 4 }, /* B */
234 { BTN_B, IN_BINDTYPE_PLAYER12, 5 }, /* C */
235 { BTN_A, IN_BINDTYPE_PLAYER12, 6 }, /* A */
236 { BTN_START, IN_BINDTYPE_PLAYER12, 7 },
237 { BTN_SELECT, IN_BINDTYPE_EMU, PEVB_MENU },
238 // { BTN_Y, IN_BINDTYPE_EMU, PEVB_SWITCH_RND },
239 { BTN_L, IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
240 { BTN_R, IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
241 { BTN_VOL_UP, IN_BINDTYPE_EMU, PEVB_VOL_UP },
242 { BTN_VOL_DOWN, IN_BINDTYPE_EMU, PEVB_VOL_DOWN },
245 #define DEF_BIND_COUNT (sizeof(in_gp2x_def_binds) / sizeof(in_gp2x_def_binds[0]))
247 static void in_gp2x_get_def_binds(int *binds)
251 for (i = 0; i < DEF_BIND_COUNT; i++)
252 binds[IN_BIND_OFFS(in_gp2x_def_binds[i].code, in_gp2x_def_binds[i].btype)] =
253 1 << in_gp2x_def_binds[i].bit;
256 /* remove binds of missing keys, count remaining ones */
257 static int in_gp2x_clean_binds(void *drv_data, int *binds, int *def_binds)
259 int i, count = 0, have_vol = 0, have_menu = 0;
261 for (i = 0; i < IN_GP2X_NBUTTONS; i++) {
263 for (t = 0; t < IN_BINDTYPE_COUNT; t++) {
264 offs = IN_BIND_OFFS(i, t);
265 if (in_gp2x_keys[i] == NULL)
266 binds[offs] = def_binds[offs] = 0;
270 eb = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)];
271 if (eb & (PEV_VOL_DOWN|PEV_VOL_UP))
277 /* autobind some important keys, if they are unbound */
278 if (!have_vol && binds[BTN_VOL_UP] == 0 && binds[BTN_VOL_DOWN] == 0) {
279 binds[IN_BIND_OFFS(BTN_VOL_UP, IN_BINDTYPE_EMU)] = PEV_VOL_UP;
280 binds[IN_BIND_OFFS(BTN_VOL_DOWN, IN_BINDTYPE_EMU)] = PEV_VOL_DOWN;
285 binds[IN_BIND_OFFS(BTN_SELECT, IN_BINDTYPE_EMU)] = PEV_MENU;
289 in_combos_find(binds, BTN_PUSH, &in_gp2x_combo_keys, &in_gp2x_combo_acts);
294 void in_gp2x_init(void *vdrv)
296 in_drv_t *drv = vdrv;
298 if (gp2x_dev_id == GP2X_DEV_WIZ)
299 in_gp2x_keys[BTN_START] = "MENU";
301 in_gp2x_combo_keys = in_gp2x_combo_acts = 0;
303 drv->prefix = in_gp2x_prefix;
304 drv->probe = in_gp2x_probe;
305 drv->free = in_gp2x_free;
306 drv->get_bind_count = in_gp2x_get_bind_count;
307 drv->get_def_binds = in_gp2x_get_def_binds;
308 drv->clean_binds = in_gp2x_clean_binds;
309 drv->menu_translate = in_gp2x_menu_translate;
310 drv->update_keycode = in_gp2x_update_keycode;