merge some pollux code from PCSX
[libpicofe.git] / gp2x / in_gp2x.c
CommitLineData
f89d8471 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
fa8d1331 12#include <stdio.h>
13b692eb 13#include <stdlib.h>
14#include <string.h>
fa8d1331 15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <unistd.h>
13b692eb 19
a86e9a3e 20#include "../input.h"
13b692eb 21#include "in_gp2x.h"
22
e99d8048 23#define IN_GP2X_PREFIX "gp2x:"
13b692eb 24#define IN_GP2X_NBUTTONS 32
25
f89d8471 26/* note: in_gp2x handles combos (if 2 btns have the same bind,
2c600560 27 * both must be pressed for action to happen) */
28static int in_gp2x_combo_keys = 0;
29static int in_gp2x_combo_acts = 0;
fa8d1331 30static int gpiodev = -1; /* Wiz only */
31
32static int (*in_gp2x_get_bits)(void);
2c600560 33
8af2da72 34static const char *in_gp2x_keys[IN_GP2X_NBUTTONS] = {
13b692eb 35 [0 ... IN_GP2X_NBUTTONS-1] = NULL,
e99d8048 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"
13b692eb 45};
46
13b692eb 47
fa8d1331 48static int in_gp2x_get_mmsp2_bits(void)
13b692eb 49{
d572cbad 50 extern volatile unsigned short *gp2x_memregs;
13b692eb 51 int value;
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;
60
61 return value;
13b692eb 62}
63
fa8d1331 64static int in_gp2x_get_wiz_bits(void)
65{
5f7b5155 66 int r, value = 0;
67 r = read(gpiodev, &value, 4);
fa8d1331 68 if (value & 0x02)
69 value |= 0x05;
70 if (value & 0x08)
71 value |= 0x14;
72 if (value & 0x20)
73 value |= 0x50;
74 if (value & 0x80)
75 value |= 0x41;
76
77 /* convert to GP2X style */
78 value &= 0x7ff55;
79 if (value & (1 << 16))
e99d8048 80 value |= 1 << GP2X_BTN_VOL_UP;
fa8d1331 81 if (value & (1 << 17))
e99d8048 82 value |= 1 << GP2X_BTN_VOL_DOWN;
fa8d1331 83 if (value & (1 << 18))
e99d8048 84 value |= 1 << GP2X_BTN_PUSH;
fa8d1331 85 value &= ~0x70000;
86
87 return value;
88}
89
0114ccc4 90#ifdef FAKE_IN_GP2X
d2f29611 91volatile unsigned short *gp2x_memregs;
e9b29264 92int gp2x_dev_id = -1;
d2f29611 93
0114ccc4 94static int in_gp2x_get_fake_bits(void)
95{
96 extern int current_keys;
97 return current_keys;
98}
99#endif
100
101static void in_gp2x_probe(void)
102{
b6072c17 103 switch (gp2x_dev_id)
0114ccc4 104 {
b6072c17 105 case GP2X_DEV_GP2X:
0114ccc4 106 in_gp2x_get_bits = in_gp2x_get_mmsp2_bits;
107 break;
b6072c17 108 case GP2X_DEV_WIZ:
0114ccc4 109 gpiodev = open("/dev/GPIO", O_RDONLY);
110 if (gpiodev < 0) {
111 perror("in_gp2x: couldn't open /dev/GPIO");
112 return;
113 }
114 in_gp2x_get_bits = in_gp2x_get_wiz_bits;
115 break;
e9b29264 116 // we'll use evdev for Caanoo
0114ccc4 117 default:
118#ifdef FAKE_IN_GP2X
119 in_gp2x_get_bits = in_gp2x_get_fake_bits;
120 break;
121#endif
122 return;
123 }
124
e99d8048 125 in_register(IN_GP2X_PREFIX "GP2X pad", -1, NULL,
e9b29264 126 IN_GP2X_NBUTTONS, in_gp2x_keys, 1);
0114ccc4 127}
128
129static void in_gp2x_free(void *drv_data)
130{
131 if (gpiodev >= 0) {
132 close(gpiodev);
133 gpiodev = -1;
134 }
135}
136
e99d8048 137static const char * const *
138in_gp2x_get_key_names(int *count)
0114ccc4 139{
e99d8048 140 *count = IN_GP2X_NBUTTONS;
141 return in_gp2x_keys;
0114ccc4 142}
143
8e77275e 144/* ORs result with pressed buttons */
e99d8048 145static int in_gp2x_update(void *drv_data, const int *binds, int *result)
13b692eb 146{
8e77275e 147 int type_start = 0;
148 int i, t, keys;
2c600560 149
fa8d1331 150 keys = in_gp2x_get_bits();
13b692eb 151
8e77275e 152 if (keys & in_gp2x_combo_keys) {
e99d8048 153 result[IN_BINDTYPE_EMU] = in_combos_do(keys, binds, GP2X_BTN_PUSH,
8e77275e 154 in_gp2x_combo_keys, in_gp2x_combo_acts);
155 type_start = IN_BINDTYPE_PLAYER12;
156 }
157
158 for (i = 0; keys; i++, keys >>= 1) {
159 if (!(keys & 1))
160 continue;
13b692eb 161
8e77275e 162 for (t = type_start; t < IN_BINDTYPE_COUNT; t++)
163 result[t] |= binds[IN_BIND_OFFS(i, t)];
13b692eb 164 }
165
8e77275e 166 return 0;
13b692eb 167}
168
169int in_gp2x_update_keycode(void *data, int *is_down)
170{
171 static int old_val = 0;
172 int val, diff, i;
173
fa8d1331 174 val = in_gp2x_get_bits();
13b692eb 175 diff = val ^ old_val;
176 if (diff == 0)
177 return -1;
178
179 /* take one bit only */
180 for (i = 0; i < sizeof(diff)*8; i++)
181 if (diff & (1<<i))
182 break;
183
184 old_val ^= 1 << i;
185
186 if (is_down)
187 *is_down = !!(val & (1<<i));
188 return i;
189}
190
82abf46f 191static const struct {
192 short key;
193 short pbtn;
194} key_pbtn_map[] =
195{
e99d8048 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 },
82abf46f 207};
208
209#define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
210
afdbb7c8 211static int in_gp2x_menu_translate(void *drv_data, int keycode, char *charcode)
13b692eb 212{
82abf46f 213 int i;
214 if (keycode < 0)
215 {
216 /* menu -> kc */
217 keycode = -keycode;
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;
13b692eb 221 }
82abf46f 222 else
223 {
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;
227 }
228
229 return 0;
13b692eb 230}
231
e99d8048 232#if 0 // TODO: move to pico
13b692eb 233static const struct {
234 short code;
8e77275e 235 char btype;
236 char bit;
e99d8048 237} in_gp2x_defbinds[] =
13b692eb 238{
239 /* MXYZ SACB RLDU */
8e77275e 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 },
b7911801 249// { BTN_Y, IN_BINDTYPE_EMU, PEVB_SWITCH_RND },
8e77275e 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 },
e99d8048 254 { 0, 0, 0 },
13b692eb 255};
e99d8048 256#endif
13b692eb 257
258/* remove binds of missing keys, count remaining ones */
8e77275e 259static int in_gp2x_clean_binds(void *drv_data, int *binds, int *def_binds)
13b692eb 260{
e99d8048 261 int i, count = 0;
262// int eb, have_vol = 0, have_menu = 0;
13b692eb 263
264 for (i = 0; i < IN_GP2X_NBUTTONS; i++) {
e99d8048 265 int t, offs;
8e77275e 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;
270 if (binds[offs])
271 count++;
82abf46f 272 }
e99d8048 273#if 0
8e77275e 274 eb = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)];
275 if (eb & (PEV_VOL_DOWN|PEV_VOL_UP))
276 have_vol = 1;
277 if (eb & PEV_MENU)
278 have_menu = 1;
e99d8048 279#endif
13b692eb 280 }
281
e99d8048 282 // TODO: move to pico
283#if 0
82abf46f 284 /* autobind some important keys, if they are unbound */
e99d8048 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;
8e77275e 288 count += 2;
82abf46f 289 }
290
8e77275e 291 if (!have_menu) {
e99d8048 292 binds[IN_BIND_OFFS(GP2X_BTN_SELECT, IN_BINDTYPE_EMU)] = PEV_MENU;
8e77275e 293 count++;
294 }
e99d8048 295#endif
82abf46f 296
e99d8048 297 in_combos_find(binds, GP2X_BTN_PUSH, &in_gp2x_combo_keys, &in_gp2x_combo_acts);
2c600560 298
13b692eb 299 return count;
13b692eb 300}
301
e99d8048 302static 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,
311};
13b692eb 312
e99d8048 313void in_gp2x_init(const struct in_default_bind *defbinds)
314{
b6072c17 315 if (gp2x_dev_id == GP2X_DEV_WIZ)
e99d8048 316 in_gp2x_keys[GP2X_BTN_START] = "MENU";
4858c633 317
2c600560 318 in_gp2x_combo_keys = in_gp2x_combo_acts = 0;
319
e99d8048 320 in_register_driver(&in_gp2x_drv, defbinds);
13b692eb 321}
322