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