refactoring for Wiz port; random cleanups
[libpicofe.git] / gp2x / in_gp2x.c
1 #include <stdlib.h>
2 #include <string.h>
3
4 #include "../common/input.h"
5 #include "in_gp2x.h"
6 #include "soc.h"
7
8 #define IN_PREFIX "gp2x:"
9 #define IN_GP2X_NBUTTONS 32
10
11 /* note: in_gp2x hadles combos (if 2 btns have the same bind,
12  * both must be pressed for action to happen) */
13 static int in_gp2x_combo_keys = 0;
14 static int in_gp2x_combo_acts = 0;
15
16 enum  { BTN_UP = 0,      BTN_LEFT = 2,      BTN_DOWN = 4,  BTN_RIGHT = 6,
17         BTN_START = 8,   BTN_SELECT = 9,    BTN_L = 10,    BTN_R = 11,
18         BTN_A = 12,      BTN_B = 13,        BTN_X = 14,    BTN_Y = 15,
19         BTN_VOL_UP = 23, BTN_VOL_DOWN = 22, BTN_PUSH = 27 };
20
21 static const char * const in_gp2x_prefix = IN_PREFIX;
22 static const char * const in_gp2x_keys[IN_GP2X_NBUTTONS] = {
23         [0 ... IN_GP2X_NBUTTONS-1] = NULL,
24         [BTN_UP]    = "UP",    [BTN_LEFT]   = "LEFT",   [BTN_DOWN] = "DOWN", [BTN_RIGHT] = "RIGHT",
25         [BTN_START] = "START", [BTN_SELECT] = "SELECT", [BTN_L]    = "L",    [BTN_R]     = "R",
26         [BTN_A]     = "A",     [BTN_B]      = "B",      [BTN_X]    = "X",    [BTN_Y]     = "Y",
27         [BTN_VOL_DOWN]= "VOL DOWN",                     [BTN_VOL_UP] = "VOL UP",
28         [BTN_PUSH] = "PUSH"
29 };
30
31
32 static void in_gp2x_probe(void)
33 {
34         in_register(IN_PREFIX "GP2X pad", IN_DRVID_GP2X, -1, (void *)1, 1);
35 }
36
37 static int in_gp2x_get_bind_count(void)
38 {
39         return IN_GP2X_NBUTTONS;
40 }
41
42 static int in_gp2x_get_gpio_bits(void)
43 {
44 #ifndef FAKE_IN_GP2X
45         extern volatile unsigned short *gp2x_memregs;
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 {
66         int i, keys, ret = 0;
67
68         keys = in_gp2x_get_gpio_bits();
69
70         if (keys & in_gp2x_combo_keys)
71                 return in_combos_do(keys, binds, BTN_PUSH, in_gp2x_combo_keys, in_gp2x_combo_acts);
72
73         for (i = 0; keys; i++) {
74                 if (keys & 1)
75                         ret |= binds[i];
76                 keys >>= 1;
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
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
124 static int in_gp2x_menu_translate(int keycode)
125 {
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;
134         }
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;
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 },
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 */
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 {
204         int i, count = 0, have_vol = 0, have_menu = 0;
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;
209                 if (binds[i]) {
210                         count++;
211                         if (binds[i] & ((1 << 29)|(1 << 30)))
212                                 have_vol = 1;
213                         if (binds[i] & (1 << 23))
214                                 have_menu = 1;
215                 }
216         }
217
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
227         in_combos_find(binds, BTN_PUSH, &in_gp2x_combo_keys, &in_gp2x_combo_acts);
228
229         return count;
230
231 }
232
233 void in_gp2x_init(void *vdrv)
234 {
235         in_drv_t *drv = vdrv;
236
237         in_gp2x_combo_keys = in_gp2x_combo_acts = 0;
238
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