32x: final renderer tweaks; PWM disable kills PWM irqs
[libpicofe.git] / gp2x / in_gp2x.c
CommitLineData
fa8d1331 1#include <stdio.h>
13b692eb 2#include <stdlib.h>
3#include <string.h>
fa8d1331 4#include <sys/types.h>
5#include <sys/stat.h>
6#include <fcntl.h>
7#include <unistd.h>
13b692eb 8
13b692eb 9#include "../common/input.h"
10#include "in_gp2x.h"
d572cbad 11#include "soc.h"
13b692eb 12
13#define IN_PREFIX "gp2x:"
14#define IN_GP2X_NBUTTONS 32
15
2c600560 16/* note: in_gp2x hadles combos (if 2 btns have the same bind,
17 * both must be pressed for action to happen) */
18static int in_gp2x_combo_keys = 0;
19static int in_gp2x_combo_acts = 0;
fa8d1331 20static int gpiodev = -1; /* Wiz only */
21
22static int (*in_gp2x_get_bits)(void);
2c600560 23
13b692eb 24enum { BTN_UP = 0, BTN_LEFT = 2, BTN_DOWN = 4, BTN_RIGHT = 6,
25 BTN_START = 8, BTN_SELECT = 9, BTN_L = 10, BTN_R = 11,
26 BTN_A = 12, BTN_B = 13, BTN_X = 14, BTN_Y = 15,
27 BTN_VOL_UP = 23, BTN_VOL_DOWN = 22, BTN_PUSH = 27 };
28
29static const char * const in_gp2x_prefix = IN_PREFIX;
8af2da72 30static const char *in_gp2x_keys[IN_GP2X_NBUTTONS] = {
13b692eb 31 [0 ... IN_GP2X_NBUTTONS-1] = NULL,
32 [BTN_UP] = "UP", [BTN_LEFT] = "LEFT", [BTN_DOWN] = "DOWN", [BTN_RIGHT] = "RIGHT",
33 [BTN_START] = "START", [BTN_SELECT] = "SELECT", [BTN_L] = "L", [BTN_R] = "R",
34 [BTN_A] = "A", [BTN_B] = "B", [BTN_X] = "X", [BTN_Y] = "Y",
35 [BTN_VOL_DOWN]= "VOL DOWN", [BTN_VOL_UP] = "VOL UP",
36 [BTN_PUSH] = "PUSH"
37};
38
13b692eb 39
fa8d1331 40static int in_gp2x_get_mmsp2_bits(void)
13b692eb 41{
d572cbad 42 extern volatile unsigned short *gp2x_memregs;
13b692eb 43 int value;
44 value = gp2x_memregs[0x1198>>1] & 0xff; // GPIO M
45 if (value == 0xFD) value = 0xFA;
46 if (value == 0xF7) value = 0xEB;
47 if (value == 0xDF) value = 0xAF;
48 if (value == 0x7F) value = 0xBE;
49 value |= gp2x_memregs[0x1184>>1] & 0xFF00; // GPIO C
50 value |= gp2x_memregs[0x1186>>1] << 16; // GPIO D
51 value = ~value & 0x08c0ff55;
52
53 return value;
13b692eb 54}
55
fa8d1331 56static int in_gp2x_get_wiz_bits(void)
57{
5f7b5155 58 int r, value = 0;
59 r = read(gpiodev, &value, 4);
fa8d1331 60 if (value & 0x02)
61 value |= 0x05;
62 if (value & 0x08)
63 value |= 0x14;
64 if (value & 0x20)
65 value |= 0x50;
66 if (value & 0x80)
67 value |= 0x41;
68
69 /* convert to GP2X style */
70 value &= 0x7ff55;
71 if (value & (1 << 16))
72 value |= 1 << BTN_VOL_UP;
73 if (value & (1 << 17))
74 value |= 1 << BTN_VOL_DOWN;
75 if (value & (1 << 18))
76 value |= 1 << BTN_PUSH;
77 value &= ~0x70000;
78
79 return value;
80}
81
0114ccc4 82#ifdef FAKE_IN_GP2X
d2f29611 83volatile unsigned short *gp2x_memregs;
84gp2x_soc_t soc_detect(void) { return -1; }
85
0114ccc4 86static int in_gp2x_get_fake_bits(void)
87{
88 extern int current_keys;
89 return current_keys;
90}
91#endif
92
93static void in_gp2x_probe(void)
94{
95 gp2x_soc_t soc;
96
97 soc = soc_detect();
98 switch (soc)
99 {
100 case SOCID_MMSP2:
101 in_gp2x_get_bits = in_gp2x_get_mmsp2_bits;
102 break;
103 case SOCID_POLLUX:
104 gpiodev = open("/dev/GPIO", O_RDONLY);
105 if (gpiodev < 0) {
106 perror("in_gp2x: couldn't open /dev/GPIO");
107 return;
108 }
109 in_gp2x_get_bits = in_gp2x_get_wiz_bits;
110 break;
111 default:
112#ifdef FAKE_IN_GP2X
113 in_gp2x_get_bits = in_gp2x_get_fake_bits;
114 break;
115#endif
116 return;
117 }
118
8e77275e 119 in_register(IN_PREFIX "GP2X pad", IN_DRVID_GP2X, -1, (void *)1,
120 IN_GP2X_NBUTTONS, 1);
0114ccc4 121}
122
123static void in_gp2x_free(void *drv_data)
124{
125 if (gpiodev >= 0) {
126 close(gpiodev);
127 gpiodev = -1;
128 }
129}
130
131static int in_gp2x_get_bind_count(void)
132{
133 return IN_GP2X_NBUTTONS;
134}
135
8e77275e 136/* ORs result with pressed buttons */
137int in_gp2x_update(void *drv_data, const int *binds, int *result)
13b692eb 138{
8e77275e 139 int type_start = 0;
140 int i, t, keys;
2c600560 141
fa8d1331 142 keys = in_gp2x_get_bits();
13b692eb 143
8e77275e 144 if (keys & in_gp2x_combo_keys) {
145 result[IN_BINDTYPE_EMU] = in_combos_do(keys, binds, BTN_PUSH,
146 in_gp2x_combo_keys, in_gp2x_combo_acts);
147 type_start = IN_BINDTYPE_PLAYER12;
148 }
149
150 for (i = 0; keys; i++, keys >>= 1) {
151 if (!(keys & 1))
152 continue;
13b692eb 153
8e77275e 154 for (t = type_start; t < IN_BINDTYPE_COUNT; t++)
155 result[t] |= binds[IN_BIND_OFFS(i, t)];
13b692eb 156 }
157
8e77275e 158 return 0;
13b692eb 159}
160
161int in_gp2x_update_keycode(void *data, int *is_down)
162{
163 static int old_val = 0;
164 int val, diff, i;
165
fa8d1331 166 val = in_gp2x_get_bits();
13b692eb 167 diff = val ^ old_val;
168 if (diff == 0)
169 return -1;
170
171 /* take one bit only */
172 for (i = 0; i < sizeof(diff)*8; i++)
173 if (diff & (1<<i))
174 break;
175
176 old_val ^= 1 << i;
177
178 if (is_down)
179 *is_down = !!(val & (1<<i));
180 return i;
181}
182
82abf46f 183static const struct {
184 short key;
185 short pbtn;
186} key_pbtn_map[] =
187{
188 { BTN_UP, PBTN_UP },
189 { BTN_DOWN, PBTN_DOWN },
190 { BTN_LEFT, PBTN_LEFT },
191 { BTN_RIGHT, PBTN_RIGHT },
192 { BTN_B, PBTN_MOK },
193 { BTN_X, PBTN_MBACK },
194 { BTN_A, PBTN_MA2 },
195 { BTN_Y, PBTN_MA3 },
196 { BTN_L, PBTN_L },
197 { BTN_R, PBTN_R },
198 { BTN_SELECT, PBTN_MENU },
199};
200
201#define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
202
13b692eb 203static int in_gp2x_menu_translate(int keycode)
204{
82abf46f 205 int i;
206 if (keycode < 0)
207 {
208 /* menu -> kc */
209 keycode = -keycode;
210 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
211 if (key_pbtn_map[i].pbtn == keycode)
212 return key_pbtn_map[i].key;
13b692eb 213 }
82abf46f 214 else
215 {
216 for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
217 if (key_pbtn_map[i].key == keycode)
218 return key_pbtn_map[i].pbtn;
219 }
220
221 return 0;
13b692eb 222}
223
224static int in_gp2x_get_key_code(const char *key_name)
225{
226 int i;
227
228 for (i = 0; i < IN_GP2X_NBUTTONS; i++) {
229 const char *k = in_gp2x_keys[i];
230 if (k != NULL && strcasecmp(k, key_name) == 0)
231 return i;
232 }
233
234 return -1;
235}
236
237static const char *in_gp2x_get_key_name(int keycode)
238{
239 const char *name = NULL;
240 if (keycode >= 0 && keycode < IN_GP2X_NBUTTONS)
241 name = in_gp2x_keys[keycode];
242 if (name == NULL)
243 name = "Unkn";
244
245 return name;
246}
247
248static const struct {
249 short code;
8e77275e 250 char btype;
251 char bit;
13b692eb 252} in_gp2x_def_binds[] =
253{
254 /* MXYZ SACB RLDU */
8e77275e 255 { BTN_UP, IN_BINDTYPE_PLAYER12, 0 },
256 { BTN_DOWN, IN_BINDTYPE_PLAYER12, 1 },
257 { BTN_LEFT, IN_BINDTYPE_PLAYER12, 2 },
258 { BTN_RIGHT, IN_BINDTYPE_PLAYER12, 3 },
259 { BTN_X, IN_BINDTYPE_PLAYER12, 4 }, /* B */
260 { BTN_B, IN_BINDTYPE_PLAYER12, 5 }, /* C */
261 { BTN_A, IN_BINDTYPE_PLAYER12, 6 }, /* A */
262 { BTN_START, IN_BINDTYPE_PLAYER12, 7 },
263 { BTN_SELECT, IN_BINDTYPE_EMU, PEVB_MENU },
b7911801 264// { BTN_Y, IN_BINDTYPE_EMU, PEVB_SWITCH_RND },
8e77275e 265 { BTN_L, IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
266 { BTN_R, IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
267 { BTN_VOL_UP, IN_BINDTYPE_EMU, PEVB_VOL_UP },
268 { BTN_VOL_DOWN, IN_BINDTYPE_EMU, PEVB_VOL_DOWN },
13b692eb 269};
270
271#define DEF_BIND_COUNT (sizeof(in_gp2x_def_binds) / sizeof(in_gp2x_def_binds[0]))
272
273static void in_gp2x_get_def_binds(int *binds)
274{
275 int i;
276
277 for (i = 0; i < DEF_BIND_COUNT; i++)
8e77275e 278 binds[IN_BIND_OFFS(in_gp2x_def_binds[i].code, in_gp2x_def_binds[i].btype)] =
279 1 << in_gp2x_def_binds[i].bit;
13b692eb 280}
281
282/* remove binds of missing keys, count remaining ones */
8e77275e 283static int in_gp2x_clean_binds(void *drv_data, int *binds, int *def_binds)
13b692eb 284{
82abf46f 285 int i, count = 0, have_vol = 0, have_menu = 0;
13b692eb 286
287 for (i = 0; i < IN_GP2X_NBUTTONS; i++) {
8e77275e 288 int t, eb, offs;
289 for (t = 0; t < IN_BINDTYPE_COUNT; t++) {
290 offs = IN_BIND_OFFS(i, t);
291 if (in_gp2x_keys[i] == NULL)
292 binds[offs] = def_binds[offs] = 0;
293 if (binds[offs])
294 count++;
82abf46f 295 }
8e77275e 296 eb = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)];
297 if (eb & (PEV_VOL_DOWN|PEV_VOL_UP))
298 have_vol = 1;
299 if (eb & PEV_MENU)
300 have_menu = 1;
13b692eb 301 }
302
82abf46f 303 /* autobind some important keys, if they are unbound */
304 if (!have_vol && binds[BTN_VOL_UP] == 0 && binds[BTN_VOL_DOWN] == 0) {
8e77275e 305 binds[IN_BIND_OFFS(BTN_VOL_UP, IN_BINDTYPE_EMU)] = PEV_VOL_UP;
306 binds[IN_BIND_OFFS(BTN_VOL_DOWN, IN_BINDTYPE_EMU)] = PEV_VOL_DOWN;
307 count += 2;
82abf46f 308 }
309
8e77275e 310 if (!have_menu) {
311 binds[IN_BIND_OFFS(BTN_SELECT, IN_BINDTYPE_EMU)] = PEV_MENU;
312 count++;
313 }
82abf46f 314
2c600560 315 in_combos_find(binds, BTN_PUSH, &in_gp2x_combo_keys, &in_gp2x_combo_acts);
316
13b692eb 317 return count;
13b692eb 318}
319
320void in_gp2x_init(void *vdrv)
321{
322 in_drv_t *drv = vdrv;
4858c633 323 gp2x_soc_t soc;
13b692eb 324
4858c633 325 soc = soc_detect();
326 if (soc == SOCID_POLLUX)
327 in_gp2x_keys[BTN_START] = "MENU";
328
2c600560 329 in_gp2x_combo_keys = in_gp2x_combo_acts = 0;
330
13b692eb 331 drv->prefix = in_gp2x_prefix;
332 drv->probe = in_gp2x_probe;
fa8d1331 333 drv->free = in_gp2x_free;
13b692eb 334 drv->get_bind_count = in_gp2x_get_bind_count;
335 drv->get_def_binds = in_gp2x_get_def_binds;
336 drv->clean_binds = in_gp2x_clean_binds;
337 drv->menu_translate = in_gp2x_menu_translate;
338 drv->get_key_code = in_gp2x_get_key_code;
339 drv->get_key_name = in_gp2x_get_key_name;
340 drv->update_keycode = in_gp2x_update_keycode;
341}
342