| 1 | #include <stdlib.h> |
| 2 | #include <string.h> |
| 3 | |
| 4 | #include "../common/common.h" |
| 5 | #include "../common/input.h" |
| 6 | #include "in_gp2x.h" |
| 7 | |
| 8 | #define IN_PREFIX "gp2x:" |
| 9 | #define IN_GP2X_NBUTTONS 32 |
| 10 | |
| 11 | extern volatile unsigned short *gp2x_memregs; /* from minimal library rlyeh */ |
| 12 | |
| 13 | enum { BTN_UP = 0, BTN_LEFT = 2, BTN_DOWN = 4, BTN_RIGHT = 6, |
| 14 | BTN_START = 8, BTN_SELECT = 9, BTN_L = 10, BTN_R = 11, |
| 15 | BTN_A = 12, BTN_B = 13, BTN_X = 14, BTN_Y = 15, |
| 16 | BTN_VOL_UP = 23, BTN_VOL_DOWN = 22, BTN_PUSH = 27 }; |
| 17 | |
| 18 | static const char * const in_gp2x_prefix = IN_PREFIX; |
| 19 | static const char * const in_gp2x_keys[IN_GP2X_NBUTTONS] = { |
| 20 | [0 ... IN_GP2X_NBUTTONS-1] = NULL, |
| 21 | [BTN_UP] = "UP", [BTN_LEFT] = "LEFT", [BTN_DOWN] = "DOWN", [BTN_RIGHT] = "RIGHT", |
| 22 | [BTN_START] = "START", [BTN_SELECT] = "SELECT", [BTN_L] = "L", [BTN_R] = "R", |
| 23 | [BTN_A] = "A", [BTN_B] = "B", [BTN_X] = "X", [BTN_Y] = "Y", |
| 24 | [BTN_VOL_DOWN]= "VOL DOWN", [BTN_VOL_UP] = "VOL UP", |
| 25 | [BTN_PUSH] = "PUSH" |
| 26 | }; |
| 27 | |
| 28 | |
| 29 | static void in_gp2x_probe(void) |
| 30 | { |
| 31 | in_register(IN_PREFIX "GP2X pad", IN_DRVID_GP2X, -1, (void *)1); |
| 32 | } |
| 33 | |
| 34 | static int in_gp2x_get_bind_count(void) |
| 35 | { |
| 36 | return IN_GP2X_NBUTTONS; |
| 37 | } |
| 38 | |
| 39 | static int in_gp2x_get_gpio_bits(void) |
| 40 | { |
| 41 | #ifndef FAKE_IN_GP2X |
| 42 | int value; |
| 43 | value = gp2x_memregs[0x1198>>1] & 0xff; // GPIO M |
| 44 | if (value == 0xFD) value = 0xFA; |
| 45 | if (value == 0xF7) value = 0xEB; |
| 46 | if (value == 0xDF) value = 0xAF; |
| 47 | if (value == 0x7F) value = 0xBE; |
| 48 | value |= gp2x_memregs[0x1184>>1] & 0xFF00; // GPIO C |
| 49 | value |= gp2x_memregs[0x1186>>1] << 16; // GPIO D |
| 50 | value = ~value & 0x08c0ff55; |
| 51 | |
| 52 | return value; |
| 53 | #else |
| 54 | extern int current_keys; |
| 55 | return current_keys; |
| 56 | #endif |
| 57 | } |
| 58 | |
| 59 | /* returns bitfield of binds of pressed buttons */ |
| 60 | int in_gp2x_update(void *drv_data, int *binds) |
| 61 | { |
| 62 | int i, value, ret = 0; |
| 63 | |
| 64 | value = in_gp2x_get_gpio_bits(); |
| 65 | |
| 66 | for (i = 0; value; i++) { |
| 67 | if (value & 1) |
| 68 | ret |= binds[i]; |
| 69 | value >>= 1; |
| 70 | } |
| 71 | |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | int in_gp2x_update_keycode(void *data, int *is_down) |
| 76 | { |
| 77 | static int old_val = 0; |
| 78 | int val, diff, i; |
| 79 | |
| 80 | val = in_gp2x_get_gpio_bits(); |
| 81 | diff = val ^ old_val; |
| 82 | if (diff == 0) |
| 83 | return -1; |
| 84 | |
| 85 | /* take one bit only */ |
| 86 | for (i = 0; i < sizeof(diff)*8; i++) |
| 87 | if (diff & (1<<i)) |
| 88 | break; |
| 89 | |
| 90 | old_val ^= 1 << i; |
| 91 | |
| 92 | if (is_down) |
| 93 | *is_down = !!(val & (1<<i)); |
| 94 | return i; |
| 95 | } |
| 96 | |
| 97 | static int in_gp2x_menu_translate(int keycode) |
| 98 | { |
| 99 | switch (keycode) { |
| 100 | case BTN_UP: return PBTN_UP; |
| 101 | case BTN_LEFT: return PBTN_LEFT; |
| 102 | case BTN_DOWN: return PBTN_DOWN; |
| 103 | case BTN_RIGHT: return PBTN_RIGHT; |
| 104 | case BTN_B: return PBTN_MOK; |
| 105 | case BTN_X: return PBTN_MBACK; |
| 106 | case BTN_SELECT:return PBTN_MENU; |
| 107 | case BTN_L: return PBTN_L; |
| 108 | case BTN_R: return PBTN_R; |
| 109 | default: return 0; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | static int in_gp2x_get_key_code(const char *key_name) |
| 114 | { |
| 115 | int i; |
| 116 | |
| 117 | for (i = 0; i < IN_GP2X_NBUTTONS; i++) { |
| 118 | const char *k = in_gp2x_keys[i]; |
| 119 | if (k != NULL && strcasecmp(k, key_name) == 0) |
| 120 | return i; |
| 121 | } |
| 122 | |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | static const char *in_gp2x_get_key_name(int keycode) |
| 127 | { |
| 128 | const char *name = NULL; |
| 129 | if (keycode >= 0 && keycode < IN_GP2X_NBUTTONS) |
| 130 | name = in_gp2x_keys[keycode]; |
| 131 | if (name == NULL) |
| 132 | name = "Unkn"; |
| 133 | |
| 134 | return name; |
| 135 | } |
| 136 | |
| 137 | static const struct { |
| 138 | short code; |
| 139 | short bit; |
| 140 | } in_gp2x_def_binds[] = |
| 141 | { |
| 142 | /* MXYZ SACB RLDU */ |
| 143 | { BTN_UP, 0 }, |
| 144 | { BTN_DOWN, 1 }, |
| 145 | { BTN_LEFT, 2 }, |
| 146 | { BTN_RIGHT, 3 }, |
| 147 | { BTN_X, 4 }, /* B */ |
| 148 | { BTN_B, 5 }, /* C */ |
| 149 | { BTN_A, 6 }, /* A */ |
| 150 | { BTN_START, 7 }, |
| 151 | }; |
| 152 | |
| 153 | #define DEF_BIND_COUNT (sizeof(in_gp2x_def_binds) / sizeof(in_gp2x_def_binds[0])) |
| 154 | |
| 155 | static void in_gp2x_get_def_binds(int *binds) |
| 156 | { |
| 157 | int i; |
| 158 | |
| 159 | for (i = 0; i < DEF_BIND_COUNT; i++) |
| 160 | binds[in_gp2x_def_binds[i].code] = 1 << in_gp2x_def_binds[i].bit; |
| 161 | } |
| 162 | |
| 163 | /* remove binds of missing keys, count remaining ones */ |
| 164 | static int in_gp2x_clean_binds(void *drv_data, int *binds) |
| 165 | { |
| 166 | int i, count = 0; |
| 167 | |
| 168 | for (i = 0; i < IN_GP2X_NBUTTONS; i++) { |
| 169 | if (in_gp2x_keys[i] == NULL) |
| 170 | binds[i] = binds[i + IN_GP2X_NBUTTONS] = 0; |
| 171 | if (binds[i]) |
| 172 | count++; |
| 173 | } |
| 174 | |
| 175 | return count; |
| 176 | |
| 177 | } |
| 178 | |
| 179 | void in_gp2x_init(void *vdrv) |
| 180 | { |
| 181 | in_drv_t *drv = vdrv; |
| 182 | |
| 183 | drv->prefix = in_gp2x_prefix; |
| 184 | drv->probe = in_gp2x_probe; |
| 185 | drv->get_bind_count = in_gp2x_get_bind_count; |
| 186 | drv->get_def_binds = in_gp2x_get_def_binds; |
| 187 | drv->clean_binds = in_gp2x_clean_binds; |
| 188 | drv->menu_translate = in_gp2x_menu_translate; |
| 189 | drv->get_key_code = in_gp2x_get_key_code; |
| 190 | drv->get_key_name = in_gp2x_get_key_name; |
| 191 | drv->update_keycode = in_gp2x_update_keycode; |
| 192 | } |
| 193 | |