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