| 1 | /* |
| 2 | * (C) GraÅžvydas "notaz" Ignotas, 2006-2012 |
| 3 | * |
| 4 | * This work is licensed under the terms of any of these licenses |
| 5 | * (at your option): |
| 6 | * - GNU GPL, version 2 or later. |
| 7 | * - GNU LGPL, version 2.1 or later. |
| 8 | * - MAME license. |
| 9 | * See the COPYING file in the top-level directory. |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <sys/types.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include "../input.h" |
| 21 | #include "soc.h" |
| 22 | #include "plat_gp2x.h" |
| 23 | #include "in_gp2x.h" |
| 24 | |
| 25 | #define IN_GP2X_PREFIX "gp2x:" |
| 26 | #define IN_GP2X_NBUTTONS 32 |
| 27 | |
| 28 | /* note: in_gp2x handles combos (if 2 btns have the same bind, |
| 29 | * both must be pressed for action to happen) */ |
| 30 | static int in_gp2x_combo_keys = 0; |
| 31 | static int in_gp2x_combo_acts = 0; |
| 32 | static int gpiodev = -1; /* Wiz only */ |
| 33 | |
| 34 | static int (*in_gp2x_get_bits)(void); |
| 35 | |
| 36 | static const char *in_gp2x_keys[IN_GP2X_NBUTTONS] = { |
| 37 | [0 ... IN_GP2X_NBUTTONS-1] = NULL, |
| 38 | [GP2X_BTN_UP] = "Up", [GP2X_BTN_LEFT] = "Left", |
| 39 | [GP2X_BTN_DOWN] = "Down", [GP2X_BTN_RIGHT] = "Right", |
| 40 | [GP2X_BTN_START] = "Start", [GP2X_BTN_SELECT] = "Select", |
| 41 | [GP2X_BTN_L] = "L", [GP2X_BTN_R] = "R", |
| 42 | [GP2X_BTN_A] = "A", [GP2X_BTN_B] = "B", |
| 43 | [GP2X_BTN_X] = "X", [GP2X_BTN_Y] = "Y", |
| 44 | [GP2X_BTN_VOL_DOWN] = "VOL DOWN", |
| 45 | [GP2X_BTN_VOL_UP] = "VOL UP", |
| 46 | [GP2X_BTN_PUSH] = "PUSH" |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | static int in_gp2x_get_mmsp2_bits(void) |
| 51 | { |
| 52 | int value; |
| 53 | value = memregs[0x1198>>1] & 0xff; // GPIO M |
| 54 | if (value == 0xFD) value = 0xFA; |
| 55 | if (value == 0xF7) value = 0xEB; |
| 56 | if (value == 0xDF) value = 0xAF; |
| 57 | if (value == 0x7F) value = 0xBE; |
| 58 | value |= memregs[0x1184>>1] & 0xFF00; // GPIO C |
| 59 | value |= memregs[0x1186>>1] << 16; // GPIO D |
| 60 | value = ~value & 0x08c0ff55; |
| 61 | |
| 62 | return value; |
| 63 | } |
| 64 | |
| 65 | static int in_gp2x_get_wiz_bits(void) |
| 66 | { |
| 67 | int r, value = 0; |
| 68 | r = read(gpiodev, &value, 4); |
| 69 | if (value & 0x02) |
| 70 | value |= 0x05; |
| 71 | if (value & 0x08) |
| 72 | value |= 0x14; |
| 73 | if (value & 0x20) |
| 74 | value |= 0x50; |
| 75 | if (value & 0x80) |
| 76 | value |= 0x41; |
| 77 | |
| 78 | /* convert to GP2X style */ |
| 79 | value &= 0x7ff55; |
| 80 | if (value & (1 << 16)) |
| 81 | value |= 1 << GP2X_BTN_VOL_UP; |
| 82 | if (value & (1 << 17)) |
| 83 | value |= 1 << GP2X_BTN_VOL_DOWN; |
| 84 | if (value & (1 << 18)) |
| 85 | value |= 1 << GP2X_BTN_PUSH; |
| 86 | value &= ~0x70000; |
| 87 | |
| 88 | return value; |
| 89 | } |
| 90 | |
| 91 | static void in_gp2x_probe(const in_drv_t *drv) |
| 92 | { |
| 93 | switch (gp2x_dev_id) |
| 94 | { |
| 95 | case GP2X_DEV_GP2X: |
| 96 | in_gp2x_get_bits = in_gp2x_get_mmsp2_bits; |
| 97 | break; |
| 98 | case GP2X_DEV_WIZ: |
| 99 | gpiodev = open("/dev/GPIO", O_RDONLY); |
| 100 | if (gpiodev < 0) { |
| 101 | perror("in_gp2x: couldn't open /dev/GPIO"); |
| 102 | return; |
| 103 | } |
| 104 | in_gp2x_get_bits = in_gp2x_get_wiz_bits; |
| 105 | break; |
| 106 | // we'll use evdev for Caanoo |
| 107 | default: |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | in_register(IN_GP2X_PREFIX "GP2X pad", -1, NULL, |
| 112 | IN_GP2X_NBUTTONS, in_gp2x_keys, 1); |
| 113 | } |
| 114 | |
| 115 | static void in_gp2x_free(void *drv_data) |
| 116 | { |
| 117 | if (gpiodev >= 0) { |
| 118 | close(gpiodev); |
| 119 | gpiodev = -1; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static const char * const * |
| 124 | in_gp2x_get_key_names(const in_drv_t *drv, int *count) |
| 125 | { |
| 126 | *count = IN_GP2X_NBUTTONS; |
| 127 | return in_gp2x_keys; |
| 128 | } |
| 129 | |
| 130 | /* ORs result with pressed buttons */ |
| 131 | static int in_gp2x_update(void *drv_data, const int *binds, int *result) |
| 132 | { |
| 133 | int type_start = 0; |
| 134 | int i, t, keys; |
| 135 | |
| 136 | keys = in_gp2x_get_bits(); |
| 137 | |
| 138 | if (keys & in_gp2x_combo_keys) { |
| 139 | result[IN_BINDTYPE_EMU] = in_combos_do(keys, binds, GP2X_BTN_PUSH, |
| 140 | in_gp2x_combo_keys, in_gp2x_combo_acts); |
| 141 | type_start = IN_BINDTYPE_PLAYER12; |
| 142 | } |
| 143 | |
| 144 | for (i = 0; keys; i++, keys >>= 1) { |
| 145 | if (!(keys & 1)) |
| 146 | continue; |
| 147 | |
| 148 | for (t = type_start; t < IN_BINDTYPE_COUNT; t++) |
| 149 | result[t] |= binds[IN_BIND_OFFS(i, t)]; |
| 150 | } |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | int in_gp2x_update_keycode(void *data, int *is_down) |
| 156 | { |
| 157 | static int old_val = 0; |
| 158 | int val, diff, i; |
| 159 | |
| 160 | val = in_gp2x_get_bits(); |
| 161 | diff = val ^ old_val; |
| 162 | if (diff == 0) |
| 163 | return -1; |
| 164 | |
| 165 | /* take one bit only */ |
| 166 | for (i = 0; i < sizeof(diff)*8; i++) |
| 167 | if (diff & (1<<i)) |
| 168 | break; |
| 169 | |
| 170 | old_val ^= 1 << i; |
| 171 | |
| 172 | if (is_down) |
| 173 | *is_down = !!(val & (1<<i)); |
| 174 | return i; |
| 175 | } |
| 176 | |
| 177 | static const struct { |
| 178 | short key; |
| 179 | short pbtn; |
| 180 | } key_pbtn_map[] = |
| 181 | { |
| 182 | { GP2X_BTN_UP, PBTN_UP }, |
| 183 | { GP2X_BTN_DOWN, PBTN_DOWN }, |
| 184 | { GP2X_BTN_LEFT, PBTN_LEFT }, |
| 185 | { GP2X_BTN_RIGHT, PBTN_RIGHT }, |
| 186 | { GP2X_BTN_B, PBTN_MOK }, |
| 187 | { GP2X_BTN_X, PBTN_MBACK }, |
| 188 | { GP2X_BTN_A, PBTN_MA2 }, |
| 189 | { GP2X_BTN_Y, PBTN_MA3 }, |
| 190 | { GP2X_BTN_L, PBTN_L }, |
| 191 | { GP2X_BTN_R, PBTN_R }, |
| 192 | { GP2X_BTN_SELECT, PBTN_MENU }, |
| 193 | }; |
| 194 | |
| 195 | #define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0])) |
| 196 | |
| 197 | static int in_gp2x_menu_translate(void *drv_data, int keycode, char *charcode) |
| 198 | { |
| 199 | int i; |
| 200 | if (keycode < 0) |
| 201 | { |
| 202 | /* menu -> kc */ |
| 203 | keycode = -keycode; |
| 204 | for (i = 0; i < KEY_PBTN_MAP_SIZE; i++) |
| 205 | if (key_pbtn_map[i].pbtn == keycode) |
| 206 | return key_pbtn_map[i].key; |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | for (i = 0; i < KEY_PBTN_MAP_SIZE; i++) |
| 211 | if (key_pbtn_map[i].key == keycode) |
| 212 | return key_pbtn_map[i].pbtn; |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | #if 0 // TODO: move to pico |
| 219 | static const struct { |
| 220 | short code; |
| 221 | char btype; |
| 222 | char bit; |
| 223 | } in_gp2x_defbinds[] = |
| 224 | { |
| 225 | /* MXYZ SACB RLDU */ |
| 226 | { BTN_UP, IN_BINDTYPE_PLAYER12, 0 }, |
| 227 | { BTN_DOWN, IN_BINDTYPE_PLAYER12, 1 }, |
| 228 | { BTN_LEFT, IN_BINDTYPE_PLAYER12, 2 }, |
| 229 | { BTN_RIGHT, IN_BINDTYPE_PLAYER12, 3 }, |
| 230 | { BTN_X, IN_BINDTYPE_PLAYER12, 4 }, /* B */ |
| 231 | { BTN_B, IN_BINDTYPE_PLAYER12, 5 }, /* C */ |
| 232 | { BTN_A, IN_BINDTYPE_PLAYER12, 6 }, /* A */ |
| 233 | { BTN_START, IN_BINDTYPE_PLAYER12, 7 }, |
| 234 | { BTN_SELECT, IN_BINDTYPE_EMU, PEVB_MENU }, |
| 235 | // { BTN_Y, IN_BINDTYPE_EMU, PEVB_SWITCH_RND }, |
| 236 | { BTN_L, IN_BINDTYPE_EMU, PEVB_STATE_SAVE }, |
| 237 | { BTN_R, IN_BINDTYPE_EMU, PEVB_STATE_LOAD }, |
| 238 | { BTN_VOL_UP, IN_BINDTYPE_EMU, PEVB_VOL_UP }, |
| 239 | { BTN_VOL_DOWN, IN_BINDTYPE_EMU, PEVB_VOL_DOWN }, |
| 240 | { 0, 0, 0 }, |
| 241 | }; |
| 242 | #endif |
| 243 | |
| 244 | /* remove binds of missing keys, count remaining ones */ |
| 245 | static int in_gp2x_clean_binds(void *drv_data, int *binds, int *def_binds) |
| 246 | { |
| 247 | int i, count = 0; |
| 248 | // int eb, have_vol = 0, have_menu = 0; |
| 249 | |
| 250 | for (i = 0; i < IN_GP2X_NBUTTONS; i++) { |
| 251 | int t, offs; |
| 252 | for (t = 0; t < IN_BINDTYPE_COUNT; t++) { |
| 253 | offs = IN_BIND_OFFS(i, t); |
| 254 | if (in_gp2x_keys[i] == NULL) |
| 255 | binds[offs] = def_binds[offs] = 0; |
| 256 | if (binds[offs]) |
| 257 | count++; |
| 258 | } |
| 259 | #if 0 |
| 260 | eb = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)]; |
| 261 | if (eb & (PEV_VOL_DOWN|PEV_VOL_UP)) |
| 262 | have_vol = 1; |
| 263 | if (eb & PEV_MENU) |
| 264 | have_menu = 1; |
| 265 | #endif |
| 266 | } |
| 267 | |
| 268 | // TODO: move to pico |
| 269 | #if 0 |
| 270 | /* autobind some important keys, if they are unbound */ |
| 271 | if (!have_vol && binds[GP2X_BTN_VOL_UP] == 0 && binds[GP2X_BTN_VOL_DOWN] == 0) { |
| 272 | binds[IN_BIND_OFFS(GP2X_BTN_VOL_UP, IN_BINDTYPE_EMU)] = PEV_VOL_UP; |
| 273 | binds[IN_BIND_OFFS(GP2X_BTN_VOL_DOWN, IN_BINDTYPE_EMU)] = PEV_VOL_DOWN; |
| 274 | count += 2; |
| 275 | } |
| 276 | |
| 277 | if (!have_menu) { |
| 278 | binds[IN_BIND_OFFS(GP2X_BTN_SELECT, IN_BINDTYPE_EMU)] = PEV_MENU; |
| 279 | count++; |
| 280 | } |
| 281 | #endif |
| 282 | |
| 283 | in_combos_find(binds, GP2X_BTN_PUSH, &in_gp2x_combo_keys, &in_gp2x_combo_acts); |
| 284 | |
| 285 | return count; |
| 286 | } |
| 287 | |
| 288 | static const in_drv_t in_gp2x_drv = { |
| 289 | .prefix = IN_GP2X_PREFIX, |
| 290 | .probe = in_gp2x_probe, |
| 291 | .free = in_gp2x_free, |
| 292 | .get_key_names = in_gp2x_get_key_names, |
| 293 | .clean_binds = in_gp2x_clean_binds, |
| 294 | .update = in_gp2x_update, |
| 295 | .update_keycode = in_gp2x_update_keycode, |
| 296 | .menu_translate = in_gp2x_menu_translate, |
| 297 | }; |
| 298 | |
| 299 | void in_gp2x_init(const struct in_default_bind *defbinds) |
| 300 | { |
| 301 | if (gp2x_dev_id == GP2X_DEV_WIZ) |
| 302 | in_gp2x_keys[GP2X_BTN_START] = "MENU"; |
| 303 | |
| 304 | in_gp2x_combo_keys = in_gp2x_combo_acts = 0; |
| 305 | |
| 306 | in_register_driver(&in_gp2x_drv, defbinds, NULL); |
| 307 | } |
| 308 | |