key combos for new input code
[libpicofe.git] / gp2x / in_gp2x.c
CommitLineData
13b692eb 1#include <stdlib.h>
2#include <string.h>
3
4#include "../common/common.h"
5#include "../common/input.h"
6#include "in_gp2x.h"
7
8#define IN_PREFIX "gp2x:"
9#define IN_GP2X_NBUTTONS 32
10
2c600560 11/* note: in_gp2x hadles combos (if 2 btns have the same bind,
12 * both must be pressed for action to happen) */
13static int in_gp2x_combo_keys = 0;
14static int in_gp2x_combo_acts = 0;
15
13b692eb 16extern volatile unsigned short *gp2x_memregs; /* from minimal library rlyeh */
17
18enum { BTN_UP = 0, BTN_LEFT = 2, BTN_DOWN = 4, BTN_RIGHT = 6,
19 BTN_START = 8, BTN_SELECT = 9, BTN_L = 10, BTN_R = 11,
20 BTN_A = 12, BTN_B = 13, BTN_X = 14, BTN_Y = 15,
21 BTN_VOL_UP = 23, BTN_VOL_DOWN = 22, BTN_PUSH = 27 };
22
23static const char * const in_gp2x_prefix = IN_PREFIX;
24static const char * const in_gp2x_keys[IN_GP2X_NBUTTONS] = {
25 [0 ... IN_GP2X_NBUTTONS-1] = NULL,
26 [BTN_UP] = "UP", [BTN_LEFT] = "LEFT", [BTN_DOWN] = "DOWN", [BTN_RIGHT] = "RIGHT",
27 [BTN_START] = "START", [BTN_SELECT] = "SELECT", [BTN_L] = "L", [BTN_R] = "R",
28 [BTN_A] = "A", [BTN_B] = "B", [BTN_X] = "X", [BTN_Y] = "Y",
29 [BTN_VOL_DOWN]= "VOL DOWN", [BTN_VOL_UP] = "VOL UP",
30 [BTN_PUSH] = "PUSH"
31};
32
33
34static void in_gp2x_probe(void)
35{
2c600560 36 in_register(IN_PREFIX "GP2X pad", IN_DRVID_GP2X, -1, (void *)1, 1);
13b692eb 37}
38
39static int in_gp2x_get_bind_count(void)
40{
41 return IN_GP2X_NBUTTONS;
42}
43
44static int in_gp2x_get_gpio_bits(void)
45{
46#ifndef FAKE_IN_GP2X
47 int value;
48 value = gp2x_memregs[0x1198>>1] & 0xff; // GPIO M
49 if (value == 0xFD) value = 0xFA;
50 if (value == 0xF7) value = 0xEB;
51 if (value == 0xDF) value = 0xAF;
52 if (value == 0x7F) value = 0xBE;
53 value |= gp2x_memregs[0x1184>>1] & 0xFF00; // GPIO C
54 value |= gp2x_memregs[0x1186>>1] << 16; // GPIO D
55 value = ~value & 0x08c0ff55;
56
57 return value;
58#else
59 extern int current_keys;
60 return current_keys;
61#endif
62}
63
64/* returns bitfield of binds of pressed buttons */
65int in_gp2x_update(void *drv_data, int *binds)
66{
2c600560 67 int i, keys, ret = 0;
68
69 keys = in_gp2x_get_gpio_bits();
13b692eb 70
2c600560 71 if (keys & in_gp2x_combo_keys)
72 return in_combos_do(keys, binds, BTN_PUSH, in_gp2x_combo_keys, in_gp2x_combo_acts);
13b692eb 73
2c600560 74 for (i = 0; keys; i++) {
75 if (keys & 1)
13b692eb 76 ret |= binds[i];
2c600560 77 keys >>= 1;
13b692eb 78 }
79
80 return ret;
81}
82
83int in_gp2x_update_keycode(void *data, int *is_down)
84{
85 static int old_val = 0;
86 int val, diff, i;
87
88 val = in_gp2x_get_gpio_bits();
89 diff = val ^ old_val;
90 if (diff == 0)
91 return -1;
92
93 /* take one bit only */
94 for (i = 0; i < sizeof(diff)*8; i++)
95 if (diff & (1<<i))
96 break;
97
98 old_val ^= 1 << i;
99
100 if (is_down)
101 *is_down = !!(val & (1<<i));
102 return i;
103}
104
105static int in_gp2x_menu_translate(int keycode)
106{
107 switch (keycode) {
108 case BTN_UP: return PBTN_UP;
109 case BTN_LEFT: return PBTN_LEFT;
110 case BTN_DOWN: return PBTN_DOWN;
111 case BTN_RIGHT: return PBTN_RIGHT;
112 case BTN_B: return PBTN_MOK;
113 case BTN_X: return PBTN_MBACK;
049a6b3e 114 case BTN_SELECT:return PBTN_MENU;
115 case BTN_L: return PBTN_L;
116 case BTN_R: return PBTN_R;
13b692eb 117 default: return 0;
118 }
119}
120
121static int in_gp2x_get_key_code(const char *key_name)
122{
123 int i;
124
125 for (i = 0; i < IN_GP2X_NBUTTONS; i++) {
126 const char *k = in_gp2x_keys[i];
127 if (k != NULL && strcasecmp(k, key_name) == 0)
128 return i;
129 }
130
131 return -1;
132}
133
134static const char *in_gp2x_get_key_name(int keycode)
135{
136 const char *name = NULL;
137 if (keycode >= 0 && keycode < IN_GP2X_NBUTTONS)
138 name = in_gp2x_keys[keycode];
139 if (name == NULL)
140 name = "Unkn";
141
142 return name;
143}
144
145static const struct {
146 short code;
147 short bit;
148} in_gp2x_def_binds[] =
149{
150 /* MXYZ SACB RLDU */
151 { BTN_UP, 0 },
152 { BTN_DOWN, 1 },
153 { BTN_LEFT, 2 },
154 { BTN_RIGHT, 3 },
155 { BTN_X, 4 }, /* B */
156 { BTN_B, 5 }, /* C */
157 { BTN_A, 6 }, /* A */
158 { BTN_START, 7 },
159};
160
161#define DEF_BIND_COUNT (sizeof(in_gp2x_def_binds) / sizeof(in_gp2x_def_binds[0]))
162
163static void in_gp2x_get_def_binds(int *binds)
164{
165 int i;
166
167 for (i = 0; i < DEF_BIND_COUNT; i++)
168 binds[in_gp2x_def_binds[i].code] = 1 << in_gp2x_def_binds[i].bit;
169}
170
171/* remove binds of missing keys, count remaining ones */
172static int in_gp2x_clean_binds(void *drv_data, int *binds)
173{
174 int i, count = 0;
175
176 for (i = 0; i < IN_GP2X_NBUTTONS; i++) {
177 if (in_gp2x_keys[i] == NULL)
178 binds[i] = binds[i + IN_GP2X_NBUTTONS] = 0;
179 if (binds[i])
180 count++;
181 }
182
2c600560 183 in_combos_find(binds, BTN_PUSH, &in_gp2x_combo_keys, &in_gp2x_combo_acts);
184
13b692eb 185 return count;
186
187}
188
189void in_gp2x_init(void *vdrv)
190{
191 in_drv_t *drv = vdrv;
192
2c600560 193 in_gp2x_combo_keys = in_gp2x_combo_acts = 0;
194
13b692eb 195 drv->prefix = in_gp2x_prefix;
196 drv->probe = in_gp2x_probe;
197 drv->get_bind_count = in_gp2x_get_bind_count;
198 drv->get_def_binds = in_gp2x_get_def_binds;
199 drv->clean_binds = in_gp2x_clean_binds;
200 drv->menu_translate = in_gp2x_menu_translate;
201 drv->get_key_code = in_gp2x_get_key_code;
202 drv->get_key_name = in_gp2x_get_key_name;
203 drv->update_keycode = in_gp2x_update_keycode;
204}
205