13b692eb |
1 | #include <stdlib.h> |
2 | #include <string.h> |
3 | |
13b692eb |
4 | #include "../common/input.h" |
5 | #include "in_gp2x.h" |
6 | |
7 | #define IN_PREFIX "gp2x:" |
8 | #define IN_GP2X_NBUTTONS 32 |
9 | |
2c600560 |
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; |
14 | |
13b692eb |
15 | extern volatile unsigned short *gp2x_memregs; /* from minimal library rlyeh */ |
16 | |
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 }; |
21 | |
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", |
29 | [BTN_PUSH] = "PUSH" |
30 | }; |
31 | |
32 | |
33 | static void in_gp2x_probe(void) |
34 | { |
2c600560 |
35 | in_register(IN_PREFIX "GP2X pad", IN_DRVID_GP2X, -1, (void *)1, 1); |
13b692eb |
36 | } |
37 | |
38 | static int in_gp2x_get_bind_count(void) |
39 | { |
40 | return IN_GP2X_NBUTTONS; |
41 | } |
42 | |
43 | static int in_gp2x_get_gpio_bits(void) |
44 | { |
45 | #ifndef FAKE_IN_GP2X |
46 | int value; |
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; |
55 | |
56 | return value; |
57 | #else |
58 | extern int current_keys; |
59 | return current_keys; |
60 | #endif |
61 | } |
62 | |
63 | /* returns bitfield of binds of pressed buttons */ |
64 | int in_gp2x_update(void *drv_data, int *binds) |
65 | { |
2c600560 |
66 | int i, keys, ret = 0; |
67 | |
68 | keys = in_gp2x_get_gpio_bits(); |
13b692eb |
69 | |
2c600560 |
70 | if (keys & in_gp2x_combo_keys) |
71 | return in_combos_do(keys, binds, BTN_PUSH, in_gp2x_combo_keys, in_gp2x_combo_acts); |
13b692eb |
72 | |
2c600560 |
73 | for (i = 0; keys; i++) { |
74 | if (keys & 1) |
13b692eb |
75 | ret |= binds[i]; |
2c600560 |
76 | keys >>= 1; |
13b692eb |
77 | } |
78 | |
79 | return ret; |
80 | } |
81 | |
82 | int in_gp2x_update_keycode(void *data, int *is_down) |
83 | { |
84 | static int old_val = 0; |
85 | int val, diff, i; |
86 | |
87 | val = in_gp2x_get_gpio_bits(); |
88 | diff = val ^ old_val; |
89 | if (diff == 0) |
90 | return -1; |
91 | |
92 | /* take one bit only */ |
93 | for (i = 0; i < sizeof(diff)*8; i++) |
94 | if (diff & (1<<i)) |
95 | break; |
96 | |
97 | old_val ^= 1 << i; |
98 | |
99 | if (is_down) |
100 | *is_down = !!(val & (1<<i)); |
101 | return i; |
102 | } |
103 | |
82abf46f |
104 | static const struct { |
105 | short key; |
106 | short pbtn; |
107 | } key_pbtn_map[] = |
108 | { |
109 | { BTN_UP, PBTN_UP }, |
110 | { BTN_DOWN, PBTN_DOWN }, |
111 | { BTN_LEFT, PBTN_LEFT }, |
112 | { BTN_RIGHT, PBTN_RIGHT }, |
113 | { BTN_B, PBTN_MOK }, |
114 | { BTN_X, PBTN_MBACK }, |
115 | { BTN_A, PBTN_MA2 }, |
116 | { BTN_Y, PBTN_MA3 }, |
117 | { BTN_L, PBTN_L }, |
118 | { BTN_R, PBTN_R }, |
119 | { BTN_SELECT, PBTN_MENU }, |
120 | }; |
121 | |
122 | #define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0])) |
123 | |
13b692eb |
124 | static int in_gp2x_menu_translate(int keycode) |
125 | { |
82abf46f |
126 | int i; |
127 | if (keycode < 0) |
128 | { |
129 | /* menu -> kc */ |
130 | keycode = -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; |
13b692eb |
134 | } |
82abf46f |
135 | else |
136 | { |
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; |
140 | } |
141 | |
142 | return 0; |
13b692eb |
143 | } |
144 | |
145 | static int in_gp2x_get_key_code(const char *key_name) |
146 | { |
147 | int i; |
148 | |
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) |
152 | return i; |
153 | } |
154 | |
155 | return -1; |
156 | } |
157 | |
158 | static const char *in_gp2x_get_key_name(int keycode) |
159 | { |
160 | const char *name = NULL; |
161 | if (keycode >= 0 && keycode < IN_GP2X_NBUTTONS) |
162 | name = in_gp2x_keys[keycode]; |
163 | if (name == NULL) |
164 | name = "Unkn"; |
165 | |
166 | return name; |
167 | } |
168 | |
169 | static const struct { |
170 | short code; |
171 | short bit; |
172 | } in_gp2x_def_binds[] = |
173 | { |
174 | /* MXYZ SACB RLDU */ |
175 | { BTN_UP, 0 }, |
176 | { BTN_DOWN, 1 }, |
177 | { BTN_LEFT, 2 }, |
178 | { BTN_RIGHT, 3 }, |
179 | { BTN_X, 4 }, /* B */ |
180 | { BTN_B, 5 }, /* C */ |
181 | { BTN_A, 6 }, /* A */ |
182 | { BTN_START, 7 }, |
82abf46f |
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 */ |
13b692eb |
189 | }; |
190 | |
191 | #define DEF_BIND_COUNT (sizeof(in_gp2x_def_binds) / sizeof(in_gp2x_def_binds[0])) |
192 | |
193 | static void in_gp2x_get_def_binds(int *binds) |
194 | { |
195 | int i; |
196 | |
197 | for (i = 0; i < DEF_BIND_COUNT; i++) |
198 | binds[in_gp2x_def_binds[i].code] = 1 << in_gp2x_def_binds[i].bit; |
199 | } |
200 | |
201 | /* remove binds of missing keys, count remaining ones */ |
202 | static int in_gp2x_clean_binds(void *drv_data, int *binds) |
203 | { |
82abf46f |
204 | int i, count = 0, have_vol = 0, have_menu = 0; |
13b692eb |
205 | |
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; |
82abf46f |
209 | if (binds[i]) { |
13b692eb |
210 | count++; |
82abf46f |
211 | if (binds[i] & ((1 << 29)|(1 << 30))) |
212 | have_vol = 1; |
213 | if (binds[i] & (1 << 23)) |
214 | have_menu = 1; |
215 | } |
13b692eb |
216 | } |
217 | |
82abf46f |
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; |
222 | } |
223 | |
224 | if (!have_menu && binds[BTN_SELECT] == 0) |
225 | binds[BTN_SELECT] = 1 << 23; |
226 | |
2c600560 |
227 | in_combos_find(binds, BTN_PUSH, &in_gp2x_combo_keys, &in_gp2x_combo_acts); |
228 | |
13b692eb |
229 | return count; |
230 | |
231 | } |
232 | |
233 | void in_gp2x_init(void *vdrv) |
234 | { |
235 | in_drv_t *drv = vdrv; |
236 | |
2c600560 |
237 | in_gp2x_combo_keys = in_gp2x_combo_acts = 0; |
238 | |
13b692eb |
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; |
248 | } |
249 | |