4 #include "../common/common.h"
5 #include "../common/input.h"
8 #define IN_PREFIX "gp2x:"
9 #define IN_GP2X_NBUTTONS 32
11 /* note: in_gp2x hadles combos (if 2 btns have the same bind,
12 * both must be pressed for action to happen) */
13 static int in_gp2x_combo_keys = 0;
14 static int in_gp2x_combo_acts = 0;
16 extern volatile unsigned short *gp2x_memregs; /* from minimal library rlyeh */
18 enum { BTN_UP = 0, BTN_LEFT = 2, BTN_DOWN = 4, BTN_RIGHT = 6,
19 BTN_START = 8, BTN_SELECT = 9, BTN_L = 10, BTN_R = 11,
20 BTN_A = 12, BTN_B = 13, BTN_X = 14, BTN_Y = 15,
21 BTN_VOL_UP = 23, BTN_VOL_DOWN = 22, BTN_PUSH = 27 };
23 static const char * const in_gp2x_prefix = IN_PREFIX;
24 static const char * const in_gp2x_keys[IN_GP2X_NBUTTONS] = {
25 [0 ... IN_GP2X_NBUTTONS-1] = NULL,
26 [BTN_UP] = "UP", [BTN_LEFT] = "LEFT", [BTN_DOWN] = "DOWN", [BTN_RIGHT] = "RIGHT",
27 [BTN_START] = "START", [BTN_SELECT] = "SELECT", [BTN_L] = "L", [BTN_R] = "R",
28 [BTN_A] = "A", [BTN_B] = "B", [BTN_X] = "X", [BTN_Y] = "Y",
29 [BTN_VOL_DOWN]= "VOL DOWN", [BTN_VOL_UP] = "VOL UP",
34 static void in_gp2x_probe(void)
36 in_register(IN_PREFIX "GP2X pad", IN_DRVID_GP2X, -1, (void *)1, 1);
39 static int in_gp2x_get_bind_count(void)
41 return IN_GP2X_NBUTTONS;
44 static int in_gp2x_get_gpio_bits(void)
48 value = gp2x_memregs[0x1198>>1] & 0xff; // GPIO M
49 if (value == 0xFD) value = 0xFA;
50 if (value == 0xF7) value = 0xEB;
51 if (value == 0xDF) value = 0xAF;
52 if (value == 0x7F) value = 0xBE;
53 value |= gp2x_memregs[0x1184>>1] & 0xFF00; // GPIO C
54 value |= gp2x_memregs[0x1186>>1] << 16; // GPIO D
55 value = ~value & 0x08c0ff55;
59 extern int current_keys;
64 /* returns bitfield of binds of pressed buttons */
65 int in_gp2x_update(void *drv_data, int *binds)
69 keys = in_gp2x_get_gpio_bits();
71 if (keys & in_gp2x_combo_keys)
72 return in_combos_do(keys, binds, BTN_PUSH, in_gp2x_combo_keys, in_gp2x_combo_acts);
74 for (i = 0; keys; i++) {
83 int in_gp2x_update_keycode(void *data, int *is_down)
85 static int old_val = 0;
88 val = in_gp2x_get_gpio_bits();
93 /* take one bit only */
94 for (i = 0; i < sizeof(diff)*8; i++)
101 *is_down = !!(val & (1<<i));
105 static int in_gp2x_menu_translate(int keycode)
108 case BTN_UP: return PBTN_UP;
109 case BTN_LEFT: return PBTN_LEFT;
110 case BTN_DOWN: return PBTN_DOWN;
111 case BTN_RIGHT: return PBTN_RIGHT;
112 case BTN_B: return PBTN_MOK;
113 case BTN_X: return PBTN_MBACK;
114 case BTN_SELECT:return PBTN_MENU;
115 case BTN_L: return PBTN_L;
116 case BTN_R: return PBTN_R;
121 static int in_gp2x_get_key_code(const char *key_name)
125 for (i = 0; i < IN_GP2X_NBUTTONS; i++) {
126 const char *k = in_gp2x_keys[i];
127 if (k != NULL && strcasecmp(k, key_name) == 0)
134 static const char *in_gp2x_get_key_name(int keycode)
136 const char *name = NULL;
137 if (keycode >= 0 && keycode < IN_GP2X_NBUTTONS)
138 name = in_gp2x_keys[keycode];
145 static const struct {
148 } in_gp2x_def_binds[] =
155 { BTN_X, 4 }, /* B */
156 { BTN_B, 5 }, /* C */
157 { BTN_A, 6 }, /* A */
161 #define DEF_BIND_COUNT (sizeof(in_gp2x_def_binds) / sizeof(in_gp2x_def_binds[0]))
163 static void in_gp2x_get_def_binds(int *binds)
167 for (i = 0; i < DEF_BIND_COUNT; i++)
168 binds[in_gp2x_def_binds[i].code] = 1 << in_gp2x_def_binds[i].bit;
171 /* remove binds of missing keys, count remaining ones */
172 static int in_gp2x_clean_binds(void *drv_data, int *binds)
176 for (i = 0; i < IN_GP2X_NBUTTONS; i++) {
177 if (in_gp2x_keys[i] == NULL)
178 binds[i] = binds[i + IN_GP2X_NBUTTONS] = 0;
183 in_combos_find(binds, BTN_PUSH, &in_gp2x_combo_keys, &in_gp2x_combo_acts);
189 void in_gp2x_init(void *vdrv)
191 in_drv_t *drv = vdrv;
193 in_gp2x_combo_keys = in_gp2x_combo_acts = 0;
195 drv->prefix = in_gp2x_prefix;
196 drv->probe = in_gp2x_probe;
197 drv->get_bind_count = in_gp2x_get_bind_count;
198 drv->get_def_binds = in_gp2x_get_def_binds;
199 drv->clean_binds = in_gp2x_clean_binds;
200 drv->menu_translate = in_gp2x_menu_translate;
201 drv->get_key_code = in_gp2x_get_key_code;
202 drv->get_key_name = in_gp2x_get_key_name;
203 drv->update_keycode = in_gp2x_update_keycode;