From c19e28f62660cdaed26698234cff9c084517b34c Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Mon, 7 Oct 2013 19:34:04 +0200 Subject: [PATCH] Let the platform code deliver the key mappings --- in_sdl.c | 62 +++++++++++++++----------------------------- in_sdl.h | 5 +--- input.c | 9 ++++--- input.h | 25 +++++++++++++++--- linux/in_evdev.c | 67 +++++++++++++++--------------------------------- linux/in_evdev.h | 4 +-- 6 files changed, 69 insertions(+), 103 deletions(-) diff --git a/in_sdl.c b/in_sdl.c index 45a0276..af8d636 100644 --- a/in_sdl.c +++ b/in_sdl.c @@ -20,6 +20,7 @@ typedef unsigned long keybits_t; #define KEYBITS_WORD_BITS (sizeof(keybits_t) * 8) struct in_sdl_state { + const in_drv_t *drv; SDL_Joystick *joy; int joy_id; int axis_keydown[2]; @@ -162,7 +163,7 @@ static const char * const in_sdl_keys[SDLK_LAST] = { [SDLK_COMPOSE] = "compose", }; -static void in_sdl_probe(void) +static void in_sdl_probe(const in_drv_t *drv) { struct in_sdl_state *state; SDL_Joystick *joy; @@ -175,6 +176,7 @@ static void in_sdl_probe(void) return; } + state->drv = drv; in_register(IN_SDL_PREFIX "keys", -1, state, SDLK_LAST, in_sdl_keys, 0); @@ -194,6 +196,7 @@ static void in_sdl_probe(void) } state->joy = joy; state->joy_id = i; + state->drv = drv; snprintf(name, sizeof(name), IN_SDL_PREFIX "%s", SDL_JoystickName(i)); in_register(name, -1, state, SDLK_LAST, in_sdl_keys, 0); @@ -406,52 +409,22 @@ static int in_sdl_update_keycode(void *drv_data, int *is_down) return ret_kc; } -struct menu_keymap { - short key; - short pbtn; -}; - -static const struct menu_keymap key_pbtn_map[] = -{ - { SDLK_UP, PBTN_UP }, - { SDLK_DOWN, PBTN_DOWN }, - { SDLK_LEFT, PBTN_LEFT }, - { SDLK_RIGHT, PBTN_RIGHT }, - /* XXX: maybe better set this from it's plat code somehow */ - { SDLK_RETURN, PBTN_MOK }, - { SDLK_ESCAPE, PBTN_MBACK }, - { SDLK_SEMICOLON, PBTN_MA2 }, - { SDLK_QUOTE, PBTN_MA3 }, - { SDLK_BACKSLASH, PBTN_MENU }, - { SDLK_LEFTBRACKET, PBTN_L }, - { SDLK_RIGHTBRACKET, PBTN_R }, -}; -#define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0])) - -static const struct menu_keymap joybtn_pbtn_map[] = -{ - { SDLK_UP, PBTN_UP }, - { SDLK_DOWN, PBTN_DOWN }, - { SDLK_LEFT, PBTN_LEFT }, - { SDLK_RIGHT, PBTN_RIGHT }, - /* joystick */ - { SDLK_WORLD_0, PBTN_MOK }, - { SDLK_WORLD_1, PBTN_MBACK }, - { SDLK_WORLD_2, PBTN_MA2 }, - { SDLK_WORLD_3, PBTN_MA3 }, -}; -#define JOYBTN_PBTN_MAP_SIZE (sizeof(joybtn_pbtn_map) / sizeof(joybtn_pbtn_map[0])) - static int in_sdl_menu_translate(void *drv_data, int keycode, char *charcode) { struct in_sdl_state *state = drv_data; + const struct in_pdata *pdata = state->drv->pdata; const struct menu_keymap *map; int map_len; int ret = 0; int i; - map = state->joy ? joybtn_pbtn_map : key_pbtn_map; - map_len = state->joy ? JOYBTN_PBTN_MAP_SIZE : KEY_PBTN_MAP_SIZE; + if (state->joy) { + map = pdata->joy_map; + map_len = pdata->jmap_size; + } else { + map = pdata->key_map; + map_len = pdata->kmap_size; + } if (keycode < 0) { @@ -491,9 +464,14 @@ static const in_drv_t in_sdl_drv = { .menu_translate = in_sdl_menu_translate, }; -void in_sdl_init(const struct in_default_bind *defbinds, - void (*handler)(void *event)) +int in_sdl_init(const struct in_pdata *pdata, void (*handler)(void *event)) { - in_register_driver(&in_sdl_drv, defbinds); + if (!pdata) { + fprintf(stderr, "in_sdl: Missing input platform data\n"); + return -1; + } + + in_register_driver(&in_sdl_drv, pdata->defbinds, pdata); ext_event_handler = handler; + return 0; } diff --git a/in_sdl.h b/in_sdl.h index 2f82312..a185564 100644 --- a/in_sdl.h +++ b/in_sdl.h @@ -1,4 +1 @@ -struct in_default_bind; - -void in_sdl_init(const struct in_default_bind *defbinds, - void (*handler)(void *event)); +int in_sdl_init(const struct in_pdata *pdata, void (*handler)(void *event)); diff --git a/input.c b/input.c index 8d2d552..dc8bedd 100644 --- a/input.c +++ b/input.c @@ -247,7 +247,7 @@ void in_probe(void) for (i = 0; i < in_driver_count; i++) { in_probe_dev_id = i; - in_drivers[i].probe(); + in_drivers[i].probe(&DRV(i)); } /* get rid of devs without binds and probes */ @@ -932,7 +932,8 @@ static const char *in_def_get_key_name(int keycode) { return NULL; } if (d.f == NULL) d.f = in_def_##f /* to be called by drivers */ -int in_register_driver(const in_drv_t *drv, const struct in_default_bind *defbinds) +int in_register_driver(const in_drv_t *drv, + const struct in_default_bind *defbinds, const void *pdata) { int count_new = in_driver_count + 1; in_drv_t *new_drivers; @@ -954,7 +955,9 @@ int in_register_driver(const in_drv_t *drv, const struct in_default_bind *defbin CHECK_ADD_STUB(new_drivers[in_driver_count], menu_translate); CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_code); CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_name); - if (defbinds != NULL) + if (pdata) + new_drivers[in_driver_count].pdata = pdata; + if (defbinds) new_drivers[in_driver_count].defbinds = defbinds; in_drivers = new_drivers; in_driver_count = count_new; diff --git a/input.h b/input.h index e87e6ea..44ab06b 100644 --- a/input.h +++ b/input.h @@ -76,9 +76,11 @@ enum { #define IN_BIND_OFFS(key, btype) \ ((key) * IN_BINDTYPE_COUNT + (btype)) -typedef struct { +typedef struct InputDriver in_drv_t; + +struct InputDriver { const char *prefix; - void (*probe)(void); + void (*probe)(const in_drv_t *drv); void (*free)(void *drv_data); const char * const * (*get_key_names)(int *count); @@ -94,7 +96,8 @@ typedef struct { const char * (*get_key_name)(int keycode); const struct in_default_bind *defbinds; -} in_drv_t; + const void *pdata; +}; struct in_default_bind { unsigned short code; @@ -102,8 +105,22 @@ struct in_default_bind { unsigned char bit; }; +struct menu_keymap { + short key; + short pbtn; +}; + +struct in_pdata { + const struct in_default_bind *defbinds; + const struct menu_keymap *key_map; + size_t kmap_size; + const struct menu_keymap *joy_map; + size_t jmap_size; +}; + /* to be called by drivers */ -int in_register_driver(const in_drv_t *drv, const struct in_default_bind *defbinds); +int in_register_driver(const in_drv_t *drv, + const struct in_default_bind *defbinds, const void *pdata); void in_register(const char *nname, int drv_fd_hnd, void *drv_data, int key_count, const char * const *key_names, int combos); void in_combos_find(const int *binds, int last_key, int *combo_keys, int *combo_acts); diff --git a/linux/in_evdev.c b/linux/in_evdev.c index bd6340c..08e3596 100644 --- a/linux/in_evdev.c +++ b/linux/in_evdev.c @@ -41,6 +41,8 @@ typedef struct { int abs_mult[MAX_ABS_DEVS]; /* 16.16 multiplier to IN_ABS_RANGE */ int abs_adj[MAX_ABS_DEVS]; /* adjust for centering */ unsigned int abs_to_digital:1; + + const in_drv_t *drv; } in_evdev_t; #ifndef KEY_CNT @@ -147,7 +149,7 @@ static const char * const in_evdev_keys[KEY_CNT] = { }; -static void in_evdev_probe(void) +static void in_evdev_probe(const in_drv_t *drv) { long keybits[KEY_CNT / sizeof(long) / 8]; long absbits[(ABS_MAX+1) / sizeof(long) / 8]; @@ -206,6 +208,8 @@ static void in_evdev_probe(void) if (dev == NULL) goto skip; + dev->drv = drv; + ret = ioctl(fd, EVIOCGKEY(sizeof(keybits)), keybits); if (ret == -1) { printf("Warning: EVIOCGKEY not supported, will have to track state\n"); @@ -522,47 +526,10 @@ out: return ret_kc; } -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 }, - /* XXX: maybe better set this from it's plat code somehow */ - /* Pandora */ - { KEY_END, PBTN_MOK }, - { KEY_PAGEDOWN, PBTN_MBACK }, - { KEY_HOME, PBTN_MA2 }, - { KEY_PAGEUP, PBTN_MA3 }, - { KEY_LEFTCTRL, PBTN_MENU }, - { KEY_RIGHTSHIFT, PBTN_L }, - { KEY_RIGHTCTRL, PBTN_R }, - /* Caanoo */ - { BTN_THUMB2, PBTN_MOK }, - { BTN_THUMB, PBTN_MBACK }, - { BTN_TRIGGER, PBTN_MA2 }, - { BTN_TOP, PBTN_MA3 }, - { BTN_BASE, PBTN_MENU }, - { BTN_TOP2, PBTN_L }, - { BTN_PINKIE, PBTN_R }, - /* "normal" keyboards */ - { KEY_ENTER, PBTN_MOK }, - { KEY_ESC, PBTN_MBACK }, - { KEY_SEMICOLON, PBTN_MA2 }, - { KEY_APOSTROPHE, PBTN_MA3 }, - { KEY_BACKSLASH, PBTN_MENU }, - { KEY_LEFTBRACE, PBTN_L }, - { KEY_RIGHTBRACE, PBTN_R }, -}; - -#define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0])) - static int in_evdev_menu_translate(void *drv_data, int keycode, char *charcode) { in_evdev_t *dev = drv_data; + const struct in_pdata *pdata = dev->drv->pdata; int ret = 0; int i; @@ -570,9 +537,9 @@ static int in_evdev_menu_translate(void *drv_data, int keycode, char *charcode) { /* menu -> kc */ keycode = -keycode; - for (i = 0; i < KEY_PBTN_MAP_SIZE; i++) - if (key_pbtn_map[i].pbtn == keycode) { - int k = key_pbtn_map[i].key; + for (i = 0; i < pdata->kmap_size; i++) + if (pdata->key_map[i].pbtn == keycode) { + int k = pdata->key_map[i].key; /* should really check EVIOCGBIT, but this is enough for now */ if (dev->kc_first <= k && k <= dev->kc_last) return k; @@ -580,9 +547,9 @@ static int in_evdev_menu_translate(void *drv_data, int keycode, char *charcode) } else { - for (i = 0; i < KEY_PBTN_MAP_SIZE; i++) { - if (key_pbtn_map[i].key == keycode) { - ret = key_pbtn_map[i].pbtn; + for (i = 0; i < pdata->kmap_size; i++) { + if (pdata->key_map[i].key == keycode) { + ret = pdata->key_map[i].pbtn; break; } } @@ -649,8 +616,14 @@ static const in_drv_t in_evdev_drv = { .menu_translate = in_evdev_menu_translate, }; -void in_evdev_init(const struct in_default_bind *defbinds) +int in_evdev_init(const struct in_pdata *pdata) { - in_register_driver(&in_evdev_drv, defbinds); + if (!pdata) { + fprintf(stderr, "in_sdl: Missing input platform data\n"); + return -1; + } + + in_register_driver(&in_evdev_drv, pdata->defbinds, pdata); + return 0; } diff --git a/linux/in_evdev.h b/linux/in_evdev.h index c5aef9b..e0eb95c 100644 --- a/linux/in_evdev.h +++ b/linux/in_evdev.h @@ -1,5 +1,3 @@ - -struct in_default_bind; extern int in_evdev_allow_abs_only; -void in_evdev_init(const struct in_default_bind *defbinds); +int in_evdev_init(const struct in_pdata *pdata); -- 2.39.2