2 * (C) GraÅžvydas "notaz" Ignotas, 2006-2012
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
9 * See the COPYING file in the top-level directory.
15 #include <sys/types.h>
23 #define IN_GP2X_PREFIX "gp2x:"
24 #define IN_GP2X_NBUTTONS 32
26 /* note: in_gp2x handles combos (if 2 btns have the same bind,
27 * both must be pressed for action to happen) */
28 static int in_gp2x_combo_keys = 0;
29 static int in_gp2x_combo_acts = 0;
30 static int gpiodev = -1; /* Wiz only */
32 static int (*in_gp2x_get_bits)(void);
34 static const char *in_gp2x_keys[IN_GP2X_NBUTTONS] = {
35 [0 ... IN_GP2X_NBUTTONS-1] = NULL,
36 [GP2X_BTN_UP] = "Up", [GP2X_BTN_LEFT] = "Left",
37 [GP2X_BTN_DOWN] = "Down", [GP2X_BTN_RIGHT] = "Right",
38 [GP2X_BTN_START] = "Start", [GP2X_BTN_SELECT] = "Select",
39 [GP2X_BTN_L] = "L", [GP2X_BTN_R] = "R",
40 [GP2X_BTN_A] = "A", [GP2X_BTN_B] = "B",
41 [GP2X_BTN_X] = "X", [GP2X_BTN_Y] = "Y",
42 [GP2X_BTN_VOL_DOWN] = "VOL DOWN",
43 [GP2X_BTN_VOL_UP] = "VOL UP",
44 [GP2X_BTN_PUSH] = "PUSH"
48 static int in_gp2x_get_mmsp2_bits(void)
50 extern volatile unsigned short *gp2x_memregs;
52 value = gp2x_memregs[0x1198>>1] & 0xff; // GPIO M
53 if (value == 0xFD) value = 0xFA;
54 if (value == 0xF7) value = 0xEB;
55 if (value == 0xDF) value = 0xAF;
56 if (value == 0x7F) value = 0xBE;
57 value |= gp2x_memregs[0x1184>>1] & 0xFF00; // GPIO C
58 value |= gp2x_memregs[0x1186>>1] << 16; // GPIO D
59 value = ~value & 0x08c0ff55;
64 static int in_gp2x_get_wiz_bits(void)
67 r = read(gpiodev, &value, 4);
77 /* convert to GP2X style */
79 if (value & (1 << 16))
80 value |= 1 << GP2X_BTN_VOL_UP;
81 if (value & (1 << 17))
82 value |= 1 << GP2X_BTN_VOL_DOWN;
83 if (value & (1 << 18))
84 value |= 1 << GP2X_BTN_PUSH;
91 volatile unsigned short *gp2x_memregs;
94 static int in_gp2x_get_fake_bits(void)
96 extern int current_keys;
101 static void in_gp2x_probe(void)
106 in_gp2x_get_bits = in_gp2x_get_mmsp2_bits;
109 gpiodev = open("/dev/GPIO", O_RDONLY);
111 perror("in_gp2x: couldn't open /dev/GPIO");
114 in_gp2x_get_bits = in_gp2x_get_wiz_bits;
116 // we'll use evdev for Caanoo
119 in_gp2x_get_bits = in_gp2x_get_fake_bits;
125 in_register(IN_GP2X_PREFIX "GP2X pad", -1, NULL,
126 IN_GP2X_NBUTTONS, in_gp2x_keys, 1);
129 static void in_gp2x_free(void *drv_data)
137 static const char * const *
138 in_gp2x_get_key_names(int *count)
140 *count = IN_GP2X_NBUTTONS;
144 /* ORs result with pressed buttons */
145 static int in_gp2x_update(void *drv_data, const int *binds, int *result)
150 keys = in_gp2x_get_bits();
152 if (keys & in_gp2x_combo_keys) {
153 result[IN_BINDTYPE_EMU] = in_combos_do(keys, binds, GP2X_BTN_PUSH,
154 in_gp2x_combo_keys, in_gp2x_combo_acts);
155 type_start = IN_BINDTYPE_PLAYER12;
158 for (i = 0; keys; i++, keys >>= 1) {
162 for (t = type_start; t < IN_BINDTYPE_COUNT; t++)
163 result[t] |= binds[IN_BIND_OFFS(i, t)];
169 int in_gp2x_update_keycode(void *data, int *is_down)
171 static int old_val = 0;
174 val = in_gp2x_get_bits();
175 diff = val ^ old_val;
179 /* take one bit only */
180 for (i = 0; i < sizeof(diff)*8; i++)
187 *is_down = !!(val & (1<<i));
191 static const struct {
196 { GP2X_BTN_UP, PBTN_UP },
197 { GP2X_BTN_DOWN, PBTN_DOWN },
198 { GP2X_BTN_LEFT, PBTN_LEFT },
199 { GP2X_BTN_RIGHT, PBTN_RIGHT },
200 { GP2X_BTN_B, PBTN_MOK },
201 { GP2X_BTN_X, PBTN_MBACK },
202 { GP2X_BTN_A, PBTN_MA2 },
203 { GP2X_BTN_Y, PBTN_MA3 },
204 { GP2X_BTN_L, PBTN_L },
205 { GP2X_BTN_R, PBTN_R },
206 { GP2X_BTN_SELECT, PBTN_MENU },
209 #define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
211 static int in_gp2x_menu_translate(void *drv_data, int keycode, char *charcode)
218 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
219 if (key_pbtn_map[i].pbtn == keycode)
220 return key_pbtn_map[i].key;
224 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
225 if (key_pbtn_map[i].key == keycode)
226 return key_pbtn_map[i].pbtn;
232 #if 0 // TODO: move to pico
233 static const struct {
237 } in_gp2x_defbinds[] =
240 { BTN_UP, IN_BINDTYPE_PLAYER12, 0 },
241 { BTN_DOWN, IN_BINDTYPE_PLAYER12, 1 },
242 { BTN_LEFT, IN_BINDTYPE_PLAYER12, 2 },
243 { BTN_RIGHT, IN_BINDTYPE_PLAYER12, 3 },
244 { BTN_X, IN_BINDTYPE_PLAYER12, 4 }, /* B */
245 { BTN_B, IN_BINDTYPE_PLAYER12, 5 }, /* C */
246 { BTN_A, IN_BINDTYPE_PLAYER12, 6 }, /* A */
247 { BTN_START, IN_BINDTYPE_PLAYER12, 7 },
248 { BTN_SELECT, IN_BINDTYPE_EMU, PEVB_MENU },
249 // { BTN_Y, IN_BINDTYPE_EMU, PEVB_SWITCH_RND },
250 { BTN_L, IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
251 { BTN_R, IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
252 { BTN_VOL_UP, IN_BINDTYPE_EMU, PEVB_VOL_UP },
253 { BTN_VOL_DOWN, IN_BINDTYPE_EMU, PEVB_VOL_DOWN },
258 /* remove binds of missing keys, count remaining ones */
259 static int in_gp2x_clean_binds(void *drv_data, int *binds, int *def_binds)
262 // int eb, have_vol = 0, have_menu = 0;
264 for (i = 0; i < IN_GP2X_NBUTTONS; i++) {
266 for (t = 0; t < IN_BINDTYPE_COUNT; t++) {
267 offs = IN_BIND_OFFS(i, t);
268 if (in_gp2x_keys[i] == NULL)
269 binds[offs] = def_binds[offs] = 0;
274 eb = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)];
275 if (eb & (PEV_VOL_DOWN|PEV_VOL_UP))
282 // TODO: move to pico
284 /* autobind some important keys, if they are unbound */
285 if (!have_vol && binds[GP2X_BTN_VOL_UP] == 0 && binds[GP2X_BTN_VOL_DOWN] == 0) {
286 binds[IN_BIND_OFFS(GP2X_BTN_VOL_UP, IN_BINDTYPE_EMU)] = PEV_VOL_UP;
287 binds[IN_BIND_OFFS(GP2X_BTN_VOL_DOWN, IN_BINDTYPE_EMU)] = PEV_VOL_DOWN;
292 binds[IN_BIND_OFFS(GP2X_BTN_SELECT, IN_BINDTYPE_EMU)] = PEV_MENU;
297 in_combos_find(binds, GP2X_BTN_PUSH, &in_gp2x_combo_keys, &in_gp2x_combo_acts);
302 static const in_drv_t in_gp2x_drv = {
303 .prefix = IN_GP2X_PREFIX,
304 .probe = in_gp2x_probe,
305 .free = in_gp2x_free,
306 .get_key_names = in_gp2x_get_key_names,
307 .clean_binds = in_gp2x_clean_binds,
308 .update = in_gp2x_update,
309 .update_keycode = in_gp2x_update_keycode,
310 .menu_translate = in_gp2x_menu_translate,
313 void in_gp2x_init(const struct in_default_bind *defbinds)
315 if (gp2x_dev_id == GP2X_DEV_WIZ)
316 in_gp2x_keys[GP2X_BTN_START] = "MENU";
318 in_gp2x_combo_keys = in_gp2x_combo_acts = 0;
320 in_register_driver(&in_gp2x_drv, defbinds);