4 #include "../common/input.h"
7 #define IN_PREFIX "gp2x:"
8 #define IN_GP2X_NBUTTONS 32
10 /* note: in_gp2x hadles combos (if 2 btns have the same bind,
11 * both must be pressed for action to happen) */
12 static int in_gp2x_combo_keys = 0;
13 static int in_gp2x_combo_acts = 0;
15 extern volatile unsigned short *gp2x_memregs; /* from minimal library rlyeh */
17 enum { BTN_UP = 0, BTN_LEFT = 2, BTN_DOWN = 4, BTN_RIGHT = 6,
18 BTN_START = 8, BTN_SELECT = 9, BTN_L = 10, BTN_R = 11,
19 BTN_A = 12, BTN_B = 13, BTN_X = 14, BTN_Y = 15,
20 BTN_VOL_UP = 23, BTN_VOL_DOWN = 22, BTN_PUSH = 27 };
22 static const char * const in_gp2x_prefix = IN_PREFIX;
23 static const char * const in_gp2x_keys[IN_GP2X_NBUTTONS] = {
24 [0 ... IN_GP2X_NBUTTONS-1] = NULL,
25 [BTN_UP] = "UP", [BTN_LEFT] = "LEFT", [BTN_DOWN] = "DOWN", [BTN_RIGHT] = "RIGHT",
26 [BTN_START] = "START", [BTN_SELECT] = "SELECT", [BTN_L] = "L", [BTN_R] = "R",
27 [BTN_A] = "A", [BTN_B] = "B", [BTN_X] = "X", [BTN_Y] = "Y",
28 [BTN_VOL_DOWN]= "VOL DOWN", [BTN_VOL_UP] = "VOL UP",
33 static void in_gp2x_probe(void)
35 in_register(IN_PREFIX "GP2X pad", IN_DRVID_GP2X, -1, (void *)1, 1);
38 static int in_gp2x_get_bind_count(void)
40 return IN_GP2X_NBUTTONS;
43 static int in_gp2x_get_gpio_bits(void)
47 value = gp2x_memregs[0x1198>>1] & 0xff; // GPIO M
48 if (value == 0xFD) value = 0xFA;
49 if (value == 0xF7) value = 0xEB;
50 if (value == 0xDF) value = 0xAF;
51 if (value == 0x7F) value = 0xBE;
52 value |= gp2x_memregs[0x1184>>1] & 0xFF00; // GPIO C
53 value |= gp2x_memregs[0x1186>>1] << 16; // GPIO D
54 value = ~value & 0x08c0ff55;
58 extern int current_keys;
63 /* returns bitfield of binds of pressed buttons */
64 int in_gp2x_update(void *drv_data, int *binds)
68 keys = in_gp2x_get_gpio_bits();
70 if (keys & in_gp2x_combo_keys)
71 return in_combos_do(keys, binds, BTN_PUSH, in_gp2x_combo_keys, in_gp2x_combo_acts);
73 for (i = 0; keys; i++) {
82 int in_gp2x_update_keycode(void *data, int *is_down)
84 static int old_val = 0;
87 val = in_gp2x_get_gpio_bits();
92 /* take one bit only */
93 for (i = 0; i < sizeof(diff)*8; i++)
100 *is_down = !!(val & (1<<i));
104 static const struct {
110 { BTN_DOWN, PBTN_DOWN },
111 { BTN_LEFT, PBTN_LEFT },
112 { BTN_RIGHT, PBTN_RIGHT },
114 { BTN_X, PBTN_MBACK },
119 { BTN_SELECT, PBTN_MENU },
122 #define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
124 static int in_gp2x_menu_translate(int keycode)
131 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
132 if (key_pbtn_map[i].pbtn == keycode)
133 return key_pbtn_map[i].key;
137 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
138 if (key_pbtn_map[i].key == keycode)
139 return key_pbtn_map[i].pbtn;
145 static int in_gp2x_get_key_code(const char *key_name)
149 for (i = 0; i < IN_GP2X_NBUTTONS; i++) {
150 const char *k = in_gp2x_keys[i];
151 if (k != NULL && strcasecmp(k, key_name) == 0)
158 static const char *in_gp2x_get_key_name(int keycode)
160 const char *name = NULL;
161 if (keycode >= 0 && keycode < IN_GP2X_NBUTTONS)
162 name = in_gp2x_keys[keycode];
169 static const struct {
172 } in_gp2x_def_binds[] =
179 { BTN_X, 4 }, /* B */
180 { BTN_B, 5 }, /* C */
181 { BTN_A, 6 }, /* A */
183 { BTN_SELECT, 23 }, /* menu */
184 { BTN_Y, 26 }, /* switch rend */
185 { BTN_L, 27 }, /* save state */
186 { BTN_R, 28 }, /* load state */
187 { BTN_VOL_UP, 29 }, /* vol up */
188 { BTN_VOL_DOWN, 30 }, /* vol down */
191 #define DEF_BIND_COUNT (sizeof(in_gp2x_def_binds) / sizeof(in_gp2x_def_binds[0]))
193 static void in_gp2x_get_def_binds(int *binds)
197 for (i = 0; i < DEF_BIND_COUNT; i++)
198 binds[in_gp2x_def_binds[i].code] = 1 << in_gp2x_def_binds[i].bit;
201 /* remove binds of missing keys, count remaining ones */
202 static int in_gp2x_clean_binds(void *drv_data, int *binds)
204 int i, count = 0, have_vol = 0, have_menu = 0;
206 for (i = 0; i < IN_GP2X_NBUTTONS; i++) {
207 if (in_gp2x_keys[i] == NULL)
208 binds[i] = binds[i + IN_GP2X_NBUTTONS] = 0;
211 if (binds[i] & ((1 << 29)|(1 << 30)))
213 if (binds[i] & (1 << 23))
218 /* autobind some important keys, if they are unbound */
219 if (!have_vol && binds[BTN_VOL_UP] == 0 && binds[BTN_VOL_DOWN] == 0) {
220 binds[BTN_VOL_UP] = 1 << 29;
221 binds[BTN_VOL_DOWN] = 1 << 30;
224 if (!have_menu && binds[BTN_SELECT] == 0)
225 binds[BTN_SELECT] = 1 << 23;
227 in_combos_find(binds, BTN_PUSH, &in_gp2x_combo_keys, &in_gp2x_combo_acts);
233 void in_gp2x_init(void *vdrv)
235 in_drv_t *drv = vdrv;
237 in_gp2x_combo_keys = in_gp2x_combo_acts = 0;
239 drv->prefix = in_gp2x_prefix;
240 drv->probe = in_gp2x_probe;
241 drv->get_bind_count = in_gp2x_get_bind_count;
242 drv->get_def_binds = in_gp2x_get_def_binds;
243 drv->clean_binds = in_gp2x_clean_binds;
244 drv->menu_translate = in_gp2x_menu_translate;
245 drv->get_key_code = in_gp2x_get_key_code;
246 drv->get_key_name = in_gp2x_get_key_name;
247 drv->update_keycode = in_gp2x_update_keycode;