6 #include "../linux/in_evdev.h"
18 #define IN_MAX_DEVS 10
20 static in_dev_t in_devices[IN_MAX_DEVS];
21 static int in_dev_count = 0;
23 static int in_bind_count(int drv_id)
28 count = in_evdev_bind_count();
32 printf("input: failed to get bind count for drv %d\n", drv_id);
37 static int *in_alloc_binds(int drv_id)
41 count = in_bind_count(drv_id);
43 printf("input: failed to get bind count for drv %d\n", drv_id);
47 ret = malloc(count * sizeof(*ret));
51 static void in_free(in_dev_t *dev)
54 switch (dev->drv_id) {
56 in_evdev_free(dev->drv_data);
68 /* to be called by drivers */
69 void in_register(const char *nname, int drv_id, void *drv_data)
71 int i, dupe_count = 0, *binds;
72 char name[256], *name_end, *tmp;
74 strncpy(name, nname, sizeof(name));
75 name[sizeof(name)-12] = 0;
76 name_end = name + strlen(name);
78 for (i = 0; i < in_dev_count; i++)
80 if (in_devices[i].name == NULL)
82 if (strcmp(in_devices[i].name, name) == 0)
84 if (in_devices[i].probed) {
86 sprintf(name_end, " [%d]", dupe_count);
95 /* try to find unused device */
96 for (i = 0; i < IN_MAX_DEVS; i++)
97 if (!in_devices[i].probed) break;
98 if (i >= IN_MAX_DEVS) {
99 printf("input: too many devices, can't add %s\n", name);
102 in_free(&in_devices[i]);
109 binds = in_alloc_binds(drv_id);
115 in_devices[i].name = tmp;
116 in_devices[i].binds = binds;
117 if (i + 1 > in_dev_count)
118 in_dev_count = i + 1;
120 printf("input: new device #%d \"%s\"\n", i, name);
122 in_devices[i].probed = 1;
123 in_devices[i].drv_id = drv_id;
124 in_devices[i].drv_data = drv_data;
130 for (i = 0; i < in_dev_count; i++)
131 in_devices[i].probed = 0;
135 /* get rid of devs without binds and probes */
136 for (i = 0; i < in_dev_count; i++) {
137 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
139 if (i < in_dev_count) {
140 free(in_devices[i].name);
141 memmove(&in_devices[i], &in_devices[i+1],
142 (in_dev_count - i) * sizeof(in_devices[0]));
148 void in_clear_binds(const char *devname)
152 count = in_bind_count(drv_id);
154 printf("input: failed to get bind count for drv %d\n", dev->drv_id);
164 for (i = 0; i < in_dev_count; i++) {
165 if (in_devices[i].probed && in_devices[i].binds != NULL) {
166 switch (in_devices[i].drv_id) {
168 result |= in_evdev_update(in_devices[i].drv_data, in_devices[i].binds);
177 static void **in_collect_drvdata(int drv_id, int *count)
179 static void *data[IN_MAX_DEVS];
182 for (*count = i = 0; i < in_dev_count; i++) {
183 if (in_devices[i].drv_id == drv_id && in_devices[i].probed)
184 data[(*count)++] = in_devices[i].drv_data;
190 void in_set_blocking(int is_blocking)
194 for (i = 0; i < in_dev_count; i++) {
195 if (in_devices[i].probed) {
196 switch (in_devices[i].drv_id) {
198 in_evdev_set_blocking(in_devices[i].drv_data, is_blocking);
206 * update with wait for a press, return keycode
207 * only can use 1 drv here..
209 int in_update_keycode(int *dev_id, int *is_down)
214 int i, id = 0, count = 0;
216 data = in_collect_drvdata(IN_DRVID_EVDEV, &count);
218 /* don't deadlock, fail */
219 printf("input: failed to find devices to read\n");
223 result = in_evdev_update_keycode(data, count, &id, is_down);
225 if (dev_id != NULL) {
226 for (i = id; i < in_dev_count; i++) {
227 if (in_devices[i].drv_data == data[id]) {
234 #error no menu read handlers
241 * same as above, only return bitfield of BTN_*
243 int in_update_menu(void)
245 static int keys_active = 0;
246 int keys_old = keys_active;
250 int code, is_down = 0;
251 code = in_update_keycode(NULL, &is_down);
253 code = in_evdev_menu_translate(code);
255 if (code == 0) continue;
260 keys_active &= ~code;
262 if (keys_old != keys_active)
269 const char *in_get_key_name(int dev_id, int keycode)
271 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
273 switch (in_devices[dev_id].drv_id) {
275 return in_evdev_get_key_name(keycode);
283 memset(in_devices, 0, sizeof(in_devices));
299 ret = in_update_keycode(&dev, &down);
300 printf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
304 ret = in_update_menu();
305 printf("%08x\n", ret);