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