X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=linux%2Fin_evdev.c;h=8a002960c17f74f80ef75cb8d6f7ca11fa63b420;hb=f6eaae4f09c6abab99692900a31c1df2a06b99af;hp=02fd0e05988a5779a186644b5c8baa6d2623d5c5;hpb=54563d7189d88f8a56717f7e3150064aacb0de86;p=libpicofe.git diff --git a/linux/in_evdev.c b/linux/in_evdev.c index 02fd0e0..8a00296 100644 --- a/linux/in_evdev.c +++ b/linux/in_evdev.c @@ -9,13 +9,12 @@ #include #include -#include "../common/common.h" #include "../common/input.h" #include "in_evdev.h" typedef struct { int fd; - int prev_update; + int *kbits; int abs_lzone; int abs_rzone; int abs_tzone; @@ -24,14 +23,21 @@ typedef struct { int abs_lasty; } in_evdev_t; +#ifndef KEY_CNT +#define KEY_CNT (KEY_MAX + 1) +#endif + #define KEYBITS_BIT(x) (keybits[(x)/sizeof(keybits[0])/8] & \ (1 << ((x) & (sizeof(keybits[0])*8-1)))) #define KEYBITS_BIT_SET(x) (keybits[(x)/sizeof(keybits[0])/8] |= \ (1 << ((x) & (sizeof(keybits[0])*8-1)))) +#define KEYBITS_BIT_CLEAR(x) (keybits[(x)/sizeof(keybits[0])/8] &= \ + ~(1 << ((x) & (sizeof(keybits[0])*8-1)))) + static const char * const in_evdev_prefix = "evdev:"; -static const char * const in_evdev_keys[KEY_MAX + 1] = { +static const char * const in_evdev_keys[KEY_CNT] = { [0 ... KEY_MAX] = NULL, [KEY_RESERVED] = "Reserved", [KEY_ESC] = "Esc", [KEY_1] = "1", [KEY_2] = "2", @@ -120,20 +126,29 @@ static const char * const in_evdev_keys[KEY_MAX + 1] = { static void in_evdev_probe(void) { + long keybits[KEY_CNT / sizeof(long) / 8]; + long absbits[(ABS_MAX+1) / sizeof(long) / 8]; int i; + // the kernel might support and return less keys then we know about, + // so make sure the buffers are clear. + memset(keybits, 0, sizeof(keybits)); + memset(absbits, 0, sizeof(absbits)); + for (i = 0;; i++) { - int keybits[(KEY_MAX+1)/sizeof(int)], absbits[(ABS_MAX+1)/sizeof(int)]; int support = 0, count = 0; in_evdev_t *dev; int u, ret, fd; char name[64]; snprintf(name, sizeof(name), "/dev/input/event%d", i); - fd = open(name, O_RDONLY); - if (fd == -1) + fd = open(name, O_RDONLY|O_NONBLOCK); + if (fd == -1) { + if (errno == EACCES) + continue; /* maybe we can access next one */ break; + } /* check supported events */ ret = ioctl(fd, EVIOCGBIT(0, sizeof(support)), &support); @@ -152,8 +167,9 @@ static void in_evdev_probe(void) } /* check for interesting keys */ - for (u = 0; u < KEY_MAX + 1; u++) { - if (KEYBITS_BIT(u) && u != KEY_POWER && u != KEY_SLEEP) + for (u = 0; u < KEY_CNT; u++) { + if (KEYBITS_BIT(u) && u != KEY_POWER && + u != KEY_SLEEP && u != BTN_TOUCH) count++; } @@ -164,6 +180,16 @@ static void in_evdev_probe(void) if (dev == NULL) goto skip; + ret = ioctl(fd, EVIOCGKEY(sizeof(keybits)), keybits); + if (ret == -1) { + printf("Warning: EVIOCGKEY not supported, will have to track state\n"); + dev->kbits = calloc(KEY_CNT, sizeof(int)); + if (dev->kbits == NULL) { + free(dev); + goto skip; + } + } + /* check for abs too */ if (support & (1 << EV_ABS)) { struct input_absinfo ainfo; @@ -195,7 +221,7 @@ no_abs: ioctl(fd, EVIOCGNAME(sizeof(name)-6), name+6); printf("in_evdev: found \"%s\" with %d events (type %08x)\n", name+6, count, support); - in_register(name, IN_DRVID_EVDEV, dev); + in_register(name, IN_DRVID_EVDEV, fd, dev, KEY_CNT, 0); continue; skip: @@ -214,63 +240,76 @@ static void in_evdev_free(void *drv_data) static int in_evdev_get_bind_count(void) { - return KEY_MAX + 1; + return KEY_CNT; } -/* returns bitfield of binds of pressed buttons */ -int in_evdev_update(void *drv_data, int *binds) +static void or_binds(const int *binds, int key, int *result) +{ + int t; + for (t = 0; t < IN_BINDTYPE_COUNT; t++) + result[t] |= binds[IN_BIND_OFFS(key, t)]; +} + +/* ORs result with binds of pressed buttons + * XXX: should measure performance hit of this func, might need to optimize */ +int in_evdev_update(void *drv_data, const int *binds, int *result) { struct input_event ev[16]; struct input_absinfo ainfo; - int keybits[(KEY_MAX+1)/sizeof(int)]; + int keybits_[KEY_CNT / sizeof(int)]; + int *keybits = keybits_; in_evdev_t *dev = drv_data; - int result = 0, changed = 0; int rd, ret, u; - while (1) { - rd = read(dev->fd, ev, sizeof(ev)); - if (rd < (int)sizeof(ev[0])) { - if (errno != EAGAIN) - perror("in_evdev: read failed"); - break; + if (dev->kbits == NULL) { + ret = ioctl(dev->fd, EVIOCGKEY(sizeof(keybits_)), keybits_); + if (ret == -1) { + perror("in_evdev: ioctl failed"); + return -1; } - - changed = 1; } - - if (!changed) - return dev->prev_update; - - ret = ioctl(dev->fd, EVIOCGKEY(sizeof(keybits)), keybits); - if (ret == -1) { - perror("in_evdev: ioctl failed"); - return 0; + else { + keybits = dev->kbits; + while (1) { + rd = read(dev->fd, ev, sizeof(ev)); + if (rd < (int)sizeof(ev[0])) { + if (errno != EAGAIN) + perror("in_evdev: read failed"); + break; + } + for (u = 0; u < rd / sizeof(ev[0]); u++) { + if (ev[u].type != EV_KEY) + continue; + else if (ev[u].value == 1) + KEYBITS_BIT_SET(ev[u].code); + else if (ev[u].value == 0) + KEYBITS_BIT_CLEAR(ev[u].code); + } + } } - for (u = 0; u < KEY_MAX + 1; u++) { - if (KEYBITS_BIT(u)) { - result |= binds[u]; - } + for (u = 0; u < KEY_CNT; u++) { + if (KEYBITS_BIT(u)) + or_binds(binds, u, result); } /* map X and Y absolute to UDLR */ if (dev->abs_lzone != 0) { ret = ioctl(dev->fd, EVIOCGABS(ABS_X), &ainfo); if (ret != -1) { - if (ainfo.value < dev->abs_lzone) result |= binds[KEY_LEFT]; - if (ainfo.value > dev->abs_rzone) result |= binds[KEY_RIGHT]; + if (ainfo.value < dev->abs_lzone) or_binds(binds, KEY_LEFT, result); + if (ainfo.value > dev->abs_rzone) or_binds(binds, KEY_RIGHT, result); } } if (dev->abs_tzone != 0) { ret = ioctl(dev->fd, EVIOCGABS(ABS_Y), &ainfo); if (ret != -1) { - if (ainfo.value < dev->abs_tzone) result |= binds[KEY_UP]; - if (ainfo.value > dev->abs_bzone) result |= binds[KEY_DOWN]; + if (ainfo.value < dev->abs_tzone) or_binds(binds, KEY_UP, result); + if (ainfo.value > dev->abs_bzone) or_binds(binds, KEY_DOWN, result); } } - dev->prev_update = result; - return result; + return 0; } static void in_evdev_set_blocking(void *drv_data, int y) @@ -279,13 +318,21 @@ static void in_evdev_set_blocking(void *drv_data, int y) long flags; int ret; - dev->prev_update = 0; - flags = (long)fcntl(dev->fd, F_GETFL); if ((int)flags == -1) { perror("in_evdev: F_GETFL fcntl failed"); return; } + + if (flags & O_NONBLOCK) { + /* flush the event queue */ + struct input_event ev; + do { + ret = read(dev->fd, &ev, sizeof(ev)); + } + while (ret == sizeof(ev)); + } + if (y) flags &= ~O_NONBLOCK; else @@ -295,121 +342,122 @@ static void in_evdev_set_blocking(void *drv_data, int y) perror("in_evdev: F_SETFL fcntl failed"); } -int in_evdev_update_keycode(void **data, int dcount, int *which, int *is_down, int timeout_ms) +static int in_evdev_update_keycode(void *data, int *is_down) { - in_evdev_t **devs = (in_evdev_t **)data; - struct timeval tv, *timeout = NULL; - int i, fdmax = -1; - - if (timeout_ms >= 0) { - tv.tv_sec = timeout_ms / 1000; - tv.tv_usec = (timeout_ms % 1000) * 1000; - timeout = &tv; - } + in_evdev_t *dev = data; + struct input_event ev; + int rd; if (is_down != NULL) *is_down = 0; - for (i = 0; i < dcount; i++) - if (devs[i]->fd > fdmax) fdmax = devs[i]->fd; - - while (1) - { - struct input_event ev; - in_evdev_t *dev = NULL; - int ret, rd; - fd_set fdset; - - FD_ZERO(&fdset); - for (i = 0; i < dcount; i++) - FD_SET(devs[i]->fd, &fdset); - - ret = select(fdmax + 1, &fdset, NULL, NULL, timeout); - if (ret == -1) - { - perror("in_evdev: select failed"); - sleep(1); - return 0; - } - - if (ret == 0) - return 0; /* timeout */ - - for (i = 0; i < dcount; i++) - if (FD_ISSET(devs[i]->fd, &fdset)) - *which = i, dev = devs[i]; - - rd = read(dev->fd, &ev, sizeof(ev)); - if (rd < (int) sizeof(ev)) { + rd = read(dev->fd, &ev, sizeof(ev)); + if (rd < (int) sizeof(ev)) { + if (errno != EAGAIN) { perror("in_evdev: error reading"); sleep(1); - return 0; } + return -1; + } - if (ev.type == EV_KEY) { - if (ev.value < 0 || ev.value > 1) - continue; + if (ev.type == EV_KEY) { + if (ev.value < 0 || ev.value > 1) + return -1; + if (is_down != NULL) + *is_down = ev.value; + return ev.code; + } + else if (ev.type == EV_ABS) + { + int down = 0; + if (dev->abs_lzone != 0 && ev.code == ABS_X) { + if (ev.value < dev->abs_lzone) { + down = 1; + dev->abs_lastx = KEY_LEFT; + } + else if (ev.value > dev->abs_rzone) { + down = 1; + dev->abs_lastx = KEY_RIGHT; + } if (is_down != NULL) - *is_down = ev.value; - return ev.code; + *is_down = down; + return dev->abs_lastx; } - else if (ev.type == EV_ABS) - { - int down = 0; - if (dev->abs_lzone != 0 && ev.code == ABS_X) { - if (ev.value < dev->abs_lzone) { - down = 1; - dev->abs_lastx = KEY_LEFT; - } - else if (ev.value > dev->abs_rzone) { - down = 1; - dev->abs_lastx = KEY_RIGHT; - } - if (is_down != NULL) - *is_down = down; - return dev->abs_lastx; + if (dev->abs_tzone != 0 && ev.code == ABS_Y) { + if (ev.value < dev->abs_tzone) { + down = 1; + dev->abs_lasty = KEY_UP; } - if (dev->abs_tzone != 0 && ev.code == ABS_Y) { - if (ev.value < dev->abs_tzone) { - down = 1; - dev->abs_lasty = KEY_UP; - } - else if (ev.value > dev->abs_bzone) { - down = 1; - dev->abs_lasty = KEY_DOWN; - } - if (is_down != NULL) - *is_down = down; - return dev->abs_lasty; + else if (ev.value > dev->abs_bzone) { + down = 1; + dev->abs_lasty = KEY_DOWN; } + if (is_down != NULL) + *is_down = down; + return dev->abs_lasty; } } + + return -1; } +static const struct { + short key; + short pbtn; +} key_pbtn_map[] = +{ + { KEY_UP, PBTN_UP }, + { KEY_DOWN, PBTN_DOWN }, + { KEY_LEFT, PBTN_LEFT }, + { KEY_RIGHT, PBTN_RIGHT }, + { KEY_ENTER, PBTN_MOK }, + { KEY_END, PBTN_MOK }, + { BTN_TRIGGER, PBTN_MOK }, + { KEY_ESC, PBTN_MBACK }, + { KEY_PAGEDOWN, PBTN_MBACK }, + { BTN_THUMB, PBTN_MBACK }, + { KEY_A, PBTN_MA2 }, + { KEY_HOME, PBTN_MA2 }, + { KEY_S, PBTN_MA3 }, + { KEY_PAGEUP, PBTN_MA3 }, + { KEY_BACKSLASH, PBTN_MENU }, + { KEY_LEFTCTRL, PBTN_MENU }, + { KEY_RIGHTSHIFT, PBTN_L }, + { KEY_LEFTBRACE, PBTN_L }, + { KEY_RIGHTCTRL, PBTN_R }, + { KEY_RIGHTBRACE, PBTN_R }, +}; + +#define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0])) + static int in_evdev_menu_translate(int keycode) { - switch (keycode) { - case KEY_UP: return PBTN_UP; - case KEY_DOWN: return PBTN_DOWN; - case KEY_LEFT: return PBTN_LEFT; - case KEY_RIGHT: return PBTN_RIGHT; - case KEY_ENTER: - case BTN_TRIGGER: return PBTN_MOK; - case KEY_ESC: - case BTN_THUMB: return PBTN_MBACK; - case KEY_MENU: return PBTN_MENU; - default: return 0; + int i; + if (keycode < 0) + { + /* menu -> kc */ + keycode = -keycode; + for (i = 0; i < KEY_PBTN_MAP_SIZE; i++) + if (key_pbtn_map[i].pbtn == keycode) + return key_pbtn_map[i].key; + } + else + { + for (i = 0; i < KEY_PBTN_MAP_SIZE; i++) + if (key_pbtn_map[i].key == keycode) + return key_pbtn_map[i].pbtn; } + + return 0; } static int in_evdev_get_key_code(const char *key_name) { int i; - for (i = 0; i < KEY_MAX + 1; i++) { + for (i = 0; i < KEY_CNT; i++) { const char *k = in_evdev_keys[i]; - if (k != NULL && k[0] == key_name[0] && - strcasecmp(k, key_name) == 0) + if (k != NULL && strcasecmp(k, key_name) == 0) return i; } @@ -429,18 +477,26 @@ static const char *in_evdev_get_key_name(int keycode) static const struct { short code; - short bit; + char btype; + char bit; } in_evdev_def_binds[] = { /* MXYZ SACB RLDU */ - { KEY_UP, 0 }, - { KEY_DOWN, 1 }, - { KEY_LEFT, 2 }, - { KEY_RIGHT, 3 }, - { KEY_S, 4 }, /* B */ - { KEY_D, 5 }, /* C */ - { KEY_A, 6 }, /* A */ - { KEY_ENTER, 7 }, + { KEY_UP, IN_BINDTYPE_PLAYER12, 0 }, + { KEY_DOWN, IN_BINDTYPE_PLAYER12, 1 }, + { KEY_LEFT, IN_BINDTYPE_PLAYER12, 2 }, + { KEY_RIGHT, IN_BINDTYPE_PLAYER12, 3 }, + { KEY_S, IN_BINDTYPE_PLAYER12, 4 }, /* B */ + { KEY_PAGEDOWN, IN_BINDTYPE_PLAYER12, 4 }, + { KEY_D, IN_BINDTYPE_PLAYER12, 5 }, /* C */ + { KEY_END, IN_BINDTYPE_PLAYER12, 5 }, + { KEY_A, IN_BINDTYPE_PLAYER12, 6 }, /* A */ + { KEY_HOME, IN_BINDTYPE_PLAYER12, 6 }, + { KEY_ENTER, IN_BINDTYPE_PLAYER12, 7 }, + { KEY_LEFTALT, IN_BINDTYPE_PLAYER12, 7 }, + { KEY_RIGHTSHIFT,IN_BINDTYPE_EMU, PEVB_STATE_SAVE }, + { KEY_RIGHTCTRL, IN_BINDTYPE_EMU, PEVB_STATE_LOAD }, + { KEY_LEFTCTRL, IN_BINDTYPE_EMU, PEVB_MENU }, }; #define DEF_BIND_COUNT (sizeof(in_evdev_def_binds) / sizeof(in_evdev_def_binds[0])) @@ -450,15 +506,16 @@ static void in_evdev_get_def_binds(int *binds) int i; for (i = 0; i < DEF_BIND_COUNT; i++) - binds[in_evdev_def_binds[i].code] = 1 << in_evdev_def_binds[i].bit; + binds[IN_BIND_OFFS(in_evdev_def_binds[i].code, in_evdev_def_binds[i].btype)] = + 1 << in_evdev_def_binds[i].bit; } /* remove binds of missing keys, count remaining ones */ -static int in_evdev_clean_binds(void *drv_data, int *binds) +static int in_evdev_clean_binds(void *drv_data, int *binds, int *def_binds) { - int keybits[(KEY_MAX+1)/sizeof(int)]; + int keybits[KEY_CNT / sizeof(int)]; in_evdev_t *dev = drv_data; - int i, ret, count = 0; + int i, t, ret, offs, count = 0; ret = ioctl(dev->fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits); if (ret == -1) { @@ -475,11 +532,14 @@ static int in_evdev_clean_binds(void *drv_data, int *binds) KEYBITS_BIT_SET(KEY_DOWN); } - for (i = 0; i < KEY_MAX + 1; i++) { - if (!KEYBITS_BIT(i)) - binds[i] = binds[i + KEY_MAX + 1] = 0; - if (binds[i]) - count++; + for (i = 0; i < KEY_CNT; i++) { + for (t = 0; t < IN_BINDTYPE_COUNT; t++) { + offs = IN_BIND_OFFS(i, t); + if (!KEYBITS_BIT(i)) + binds[offs] = def_binds[offs] = 0; + if (binds[offs]) + count++; + } } return count; @@ -496,6 +556,7 @@ void in_evdev_init(void *vdrv) drv->get_def_binds = in_evdev_get_def_binds; drv->clean_binds = in_evdev_clean_binds; drv->set_blocking = in_evdev_set_blocking; + drv->update_keycode = in_evdev_update_keycode; drv->menu_translate = in_evdev_menu_translate; drv->get_key_code = in_evdev_get_key_code; drv->get_key_name = in_evdev_get_key_name;