mirror of
https://github.com/RaySollium99/libpicofe.git
synced 2025-09-03 06:17:44 -04:00
Let the platform code deliver the key mappings
This commit is contained in:
parent
17def48f1f
commit
c19e28f626
6 changed files with 69 additions and 103 deletions
62
in_sdl.c
62
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;
|
||||
}
|
||||
|
|
5
in_sdl.h
5
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));
|
||||
|
|
9
input.c
9
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;
|
||||
|
|
25
input.h
25
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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue