From b61ae369f7f20a84eb6ca2e272b56b518811551c Mon Sep 17 00:00:00 2001 From: twinaphex Date: Mon, 12 Jan 2015 02:52:52 +0100 Subject: [PATCH] (input_keymaps.c) Namespace changes and documentation --- apple/OSX/settings.m | 2 +- apple/iOS/menu.m | 2 +- input/apple_input.c | 6 +-- input/dinput.c | 11 ++++-- input/input_common.c | 2 +- input/input_common.h | 1 - input/input_keymaps.c | 63 ++++++++++++++++++++++++------- input/input_keymaps.h | 43 +++++++++++++++++++-- input/keyboard_event_apple.c | 5 ++- input/keyboard_event_win32.c | 22 +++++++---- input/keyboard_event_x11.c | 24 ++++++++---- input/keyboard_event_xkb.c | 3 +- input/linuxraw_input.c | 9 ++--- input/rwebinput_input.c | 60 +++++++++++++++++------------ input/sdl_input.c | 73 ++++++++++++++++++++++-------------- input/udev_input.c | 14 +++---- input/x11_input.c | 42 ++++++++++++++------- settings.c | 3 +- 18 files changed, 258 insertions(+), 127 deletions(-) diff --git a/apple/OSX/settings.m b/apple/OSX/settings.m index 62865589fb..5dc2884ba2 100644 --- a/apple/OSX/settings.m +++ b/apple/OSX/settings.m @@ -72,7 +72,7 @@ static void* const associated_name_tag = (void*)&associated_name_tag; int32_t idx = self.setting->index ? self.setting->index - 1 : 0; if ((value = apple_input_find_any_key())) - BINDFOR(*[self setting]).key = input_translate_keysym_to_rk(value); + BINDFOR(*[self setting]).key = input_keymaps_translate_keysym_to_rk(value); else if ((value = apple_input_find_any_button(idx)) >= 0) BINDFOR(*[self setting]).joykey = value; else if ((value = apple_input_find_any_axis(idx))) diff --git a/apple/iOS/menu.m b/apple/iOS/menu.m index 004bd71d73..1a6b5e7f0e 100644 --- a/apple/iOS/menu.m +++ b/apple/iOS/menu.m @@ -558,7 +558,7 @@ static void RunActionSheet(const char* title, const struct string_list* items, U idx = self.setting->index - 1; if ((value = apple_input_find_any_key())) - BINDFOR(*self.setting).key = input_translate_keysym_to_rk(value); + BINDFOR(*self.setting).key = input_keymaps_translate_keysym_to_rk(value); else if ((value = apple_input_find_any_button(idx)) >= 0) BINDFOR(*self.setting).joykey = value; else if ((value = apple_input_find_any_axis(idx))) diff --git a/input/apple_input.c b/input/apple_input.c index e5bb98edb1..39304352fe 100644 --- a/input/apple_input.c +++ b/input/apple_input.c @@ -217,7 +217,7 @@ static int16_t apple_input_is_pressed(apple_input_data_t *apple, unsigned port_n if (id < RARCH_BIND_LIST_END) { const struct retro_keybind *bind = &binds[id]; - unsigned bit = input_translate_rk_to_keysym(bind->key); + unsigned bit = input_keymaps_translate_rk_to_keysym(bind->key); return bind->valid && apple->key_state[bit]; } return 0; @@ -231,7 +231,7 @@ static void *apple_input_init(void) if (!apple) return NULL; - input_init_keyboard_lut(rarch_key_map_apple_hid); + input_keymaps_init_keyboard_lut(rarch_key_map_apple_hid); apple->joypad = input_joypad_init_driver(g_settings.input.joypad_driver); @@ -329,7 +329,7 @@ static int16_t apple_input_state(void *data, idx, id, binds[port]); case RETRO_DEVICE_KEYBOARD: { - unsigned bit = input_translate_rk_to_keysym((enum retro_key)id); + unsigned bit = input_keymaps_translate_rk_to_keysym((enum retro_key)id); return (id < RETROK_LAST) && apple->key_state[bit]; } case RETRO_DEVICE_MOUSE: diff --git a/input/dinput.c b/input/dinput.c index a7b972e2e9..082d8385d4 100644 --- a/input/dinput.c +++ b/input/dinput.c @@ -159,7 +159,7 @@ static void *dinput_init(void) IDirectInputDevice8_Acquire(di->mouse); } - input_init_keyboard_lut(rarch_key_map_dinput); + input_keymaps_init_keyboard_lut(rarch_key_map_dinput); di->joypad = input_joypad_init_driver(g_settings.input.joypad_driver); return di; @@ -220,10 +220,11 @@ static void dinput_poll(void *data) static bool dinput_keyboard_pressed(struct dinput_input *di, unsigned key) { unsigned sym; + if (key >= RETROK_LAST) return false; - sym = input_translate_rk_to_keysym((enum retro_key)key); + sym = input_keymaps_translate_rk_to_keysym((enum retro_key)key); return di->state[sym] & 0x80; } @@ -258,7 +259,7 @@ static int16_t dinput_pressed_analog(struct dinput_input *di, if (dinput_keyboard_pressed(di, bind_minus->key)) pressed_minus = -0x7fff; if (dinput_keyboard_pressed(di, bind_plus->key)) - pressed_plus = 0x7fff; + pressed_plus = 0x7fff; return pressed_plus + pressed_minus; } @@ -370,8 +371,8 @@ static int16_t dinput_input_state(void *data, const struct retro_keybind **binds, unsigned port, unsigned device, unsigned idx, unsigned id) { - struct dinput_input *di = (struct dinput_input*)data; int16_t ret; + struct dinput_input *di = (struct dinput_input*)data; switch (device) { @@ -474,6 +475,7 @@ struct pointer_status *dinput_find_pointer(struct dinput_input *di, break; check_pos = check_pos->next; } + return check_pos; } @@ -646,6 +648,7 @@ bool g_xinput_block_pads; static void dinput_joypad_destroy(void) { unsigned i; + for (i = 0; i < MAX_USERS; i++) { if (g_pads[i].joypad) diff --git a/input/input_common.c b/input/input_common.c index 798df1dcc1..b7e795c9c4 100644 --- a/input/input_common.c +++ b/input/input_common.c @@ -378,7 +378,7 @@ void input_get_bind_string(char *buf, const struct retro_keybind *bind, input_get_bind_string_joyaxis(buf, "Auto: ", auto_bind, size); #ifndef RARCH_CONSOLE - input_translate_rk_to_str(bind->key, key, sizeof(key)); + input_keymaps_translate_rk_to_str(bind->key, key, sizeof(key)); if (!strcmp(key, "nul")) *key = '\0'; diff --git a/input/input_common.h b/input/input_common.h index 7ca96518e3..f119d266af 100644 --- a/input/input_common.h +++ b/input/input_common.h @@ -54,7 +54,6 @@ bool input_translate_coord_viewport(int mouse_x, int mouse_y, void input_get_bind_string(char *buf, const struct retro_keybind *bind, const struct retro_keybind *auto_bind, size_t size); -void input_translate_rk_to_str(enum retro_key key, char *buf, size_t size); enum retro_key input_translate_str_to_rk(const char *str); diff --git a/input/input_keymaps.c b/input/input_keymaps.c index 273e415bc2..3f9801c72c 100644 --- a/input/input_keymaps.c +++ b/input/input_keymaps.c @@ -999,16 +999,33 @@ const struct rarch_key_map rarch_key_map_apple_hid[] = { static enum retro_key rarch_keysym_lut[RETROK_LAST]; -void input_init_keyboard_lut(const struct rarch_key_map *map) +/** + * input_keymaps_init_keyboard_lut: + * @map : Keyboard map. + * + * Initializes and sets the keyboard layout to a keyboard map (@map). + **/ +void input_keymaps_init_keyboard_lut(const struct rarch_key_map *map) { memset(rarch_keysym_lut, 0, sizeof(rarch_keysym_lut)); + for (; map->rk != RETROK_UNKNOWN; map++) rarch_keysym_lut[map->rk] = (enum retro_key)map->sym; } -enum retro_key input_translate_keysym_to_rk(unsigned sym) +/** + * input_keymaps_translate_keysym_to_rk: + * @sym : Key symbol. + * + * Translates a key symbol from the keyboard layout table + * to an associated retro key identifier. + * + * Returns: Retro key identifier. + **/ +enum retro_key input_keymaps_translate_keysym_to_rk(unsigned sym) { unsigned i; + for (i = 0; i < ARRAY_SIZE(rarch_keysym_lut); i++) { if (rarch_keysym_lut[i] == sym) @@ -1018,8 +1035,19 @@ enum retro_key input_translate_keysym_to_rk(unsigned sym) return RETROK_UNKNOWN; } -void input_translate_rk_to_str(enum retro_key key, char *buf, size_t size) +/** + * input_keymaps_translate_rk_to_str: + * @key : Retro key identifier. + * @buf : Buffer. + * @size : Size of @buf. + * + * Translates a retro key identifier to a human-readable + * identifier string. + **/ +void input_keymaps_translate_rk_to_str(enum retro_key key, char *buf, size_t size) { + unsigned i; + rarch_assert(size >= 2); *buf = '\0'; @@ -1027,22 +1055,29 @@ void input_translate_rk_to_str(enum retro_key key, char *buf, size_t size) { buf[0] = (key - RETROK_a) + 'a'; buf[1] = '\0'; + return; } - else + + for (i = 0; input_config_key_map[i].str; i++) { - unsigned i; - for (i = 0; input_config_key_map[i].str; i++) - { - if (input_config_key_map[i].key == key) - { - strlcpy(buf, input_config_key_map[i].str, size); - break; - } - } + if (input_config_key_map[i].key != key) + continue; + + strlcpy(buf, input_config_key_map[i].str, size); + break; } } -unsigned input_translate_rk_to_keysym(enum retro_key key) +/** + * input_keymaps_translate_rk_to_keysym: + * @key : Retro key identifier + * + * Translates a retro key identifier to a key symbol + * from the keyboard layout table. + * + * Returns: key symbol from the keyboard layout table. + **/ +unsigned input_keymaps_translate_rk_to_keysym(enum retro_key key) { return rarch_keysym_lut[key]; } diff --git a/input/input_keymaps.h b/input/input_keymaps.h index a54e967612..ee88f597f6 100644 --- a/input/input_keymaps.h +++ b/input/input_keymaps.h @@ -46,9 +46,46 @@ extern const struct rarch_key_map rarch_key_map_rwebinput[]; extern const struct rarch_key_map rarch_key_map_linux[]; extern const struct rarch_key_map rarch_key_map_apple_hid[]; -void input_init_keyboard_lut(const struct rarch_key_map *map); -enum retro_key input_translate_keysym_to_rk(unsigned sym); -unsigned input_translate_rk_to_keysym(enum retro_key key); +/** + * input_keymaps_init_keyboard_lut: + * @map : Keyboard map. + * + * Initializes and sets the keyboard layout to a keyboard map (@map). + **/ +void input_keymaps_init_keyboard_lut(const struct rarch_key_map *map); + +/** + * input_keymaps_translate_keysym_to_rk: + * @sym : Key symbol. + * + * Translates a key symbol from the keyboard layout table + * to an associated retro key identifier. + * + * Returns: Retro key identifier. + **/ +enum retro_key input_keymaps_translate_keysym_to_rk(unsigned sym); + +/** + * input_keymaps_translate_rk_to_keysym: + * @key : Retro key identifier + * + * Translates a retro key identifier to a key symbol + * from the keyboard layout table. + * + * Returns: key symbol from the keyboard layout table. + **/ +unsigned input_keymaps_translate_rk_to_keysym(enum retro_key key); + +/** + * input_keymaps_translate_rk_to_str: + * @key : Retro key identifier. + * @buf : Buffer. + * @size : Size of @buf. + * + * Translates a retro key identifier to a human-readable + * identifier string. + **/ +void input_keymaps_translate_rk_to_str(enum retro_key key, char *buf, size_t size); #ifdef __cplusplus } diff --git a/input/keyboard_event_apple.c b/input/keyboard_event_apple.c index bfd75f7399..5beaa66a9f 100644 --- a/input/keyboard_event_apple.c +++ b/input/keyboard_event_apple.c @@ -72,7 +72,8 @@ static bool handle_small_keyboard(unsigned* code, bool down) if (!map_initialized) { - for (int i = 0; mapping_def[i].orig; i ++) + int i; + for (i = 0; mapping_def[i].orig; i ++) mapping[mapping_def[i].orig] = mapping_def[i].mod; map_initialized = true; } @@ -157,5 +158,5 @@ void apple_input_keyboard_event(bool down, apple->key_state[code] = down; input_keyboard_event(down, - input_translate_keysym_to_rk(code), character, (enum retro_mod)mod); + input_keymaps_translate_keysym_to_rk(code), character, (enum retro_mod)mod); } diff --git a/input/keyboard_event_win32.c b/input/keyboard_event_win32.c index 7701710016..d8dfd72a99 100644 --- a/input/keyboard_event_win32.c +++ b/input/keyboard_event_win32.c @@ -24,15 +24,21 @@ LRESULT win32_handle_keyboard_event(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { unsigned scancode = (lparam >> 16) & 0xff; - unsigned keycode = input_translate_keysym_to_rk(scancode); + unsigned keycode = input_keymaps_translate_keysym_to_rk(scancode); uint16_t mod = 0; - mod |= (GetKeyState(VK_SHIFT) & 0x80) ? RETROKMOD_SHIFT : 0; - mod |= (GetKeyState(VK_CONTROL) & 0x80) ? RETROKMOD_CTRL : 0; - mod |= (GetKeyState(VK_MENU) & 0x80) ? RETROKMOD_ALT : 0; - mod |= (GetKeyState(VK_CAPITAL) & 0x81) ? RETROKMOD_CAPSLOCK : 0; - mod |= (GetKeyState(VK_SCROLL) & 0x81) ? RETROKMOD_SCROLLOCK : 0; - mod |= ((GetKeyState(VK_LWIN) | GetKeyState(VK_RWIN)) & 0x80) ? - RETROKMOD_META : 0; + + if (GetKeyState(VK_SHIFT) & 0x80) + mod |= RETROKMOD_SHIFT; + if (GetKeyState(VK_CONTROL) & 0x80) + mod |= RETROKMOD_CTRL; + if (GetKeyState(VK_MENU) & 0x80) + mod |= RETROKMOD_ALT; + if (GetKeyState(VK_CAPITAL) & 0x81) + mod |= RETROKMOD_CAPSLOCK; + if (GetKeyState(VK_SCROLL) & 0x81) + mod |= RETROKMOD_SCROLLOCK; + if ((GetKeyState(VK_LWIN) | GetKeyState(VK_RWIN)) & 0x80) + mod |= RETROKMOD_META; switch (message) { diff --git a/input/keyboard_event_x11.c b/input/keyboard_event_x11.c index 3d497d2c66..836fc0965a 100644 --- a/input/keyboard_event_x11.c +++ b/input/keyboard_event_x11.c @@ -77,11 +77,13 @@ static size_t conv_utf8_utf32(uint32_t *out, void x11_handle_key_event(XEvent *event, XIC ic, bool filter) { int i; + unsigned state; + uint16_t mod = 0; char keybuf[32] = {0}; uint32_t chars[32] = {0}; bool down = event->type == KeyPress; - unsigned key = input_translate_keysym_to_rk(XLookupKeysym(&event->xkey, 0)); + unsigned key = input_keymaps_translate_keysym_to_rk(XLookupKeysym(&event->xkey, 0)); int num = 0; if (down && !filter) @@ -107,15 +109,21 @@ void x11_handle_key_event(XEvent *event, XIC ic, bool filter) #endif } - unsigned state = event->xkey.state; - uint16_t mod = 0; - mod |= (state & ShiftMask) ? RETROKMOD_SHIFT : 0; - mod |= (state & LockMask) ? RETROKMOD_CAPSLOCK : 0; - mod |= (state & ControlMask) ? RETROKMOD_CTRL : 0; - mod |= (state & Mod1Mask) ? RETROKMOD_ALT : 0; - mod |= (state & Mod4Mask) ? RETROKMOD_META : 0; + state = event->xkey.state; + + if (state & ShiftMask) + mod |= RETROKMOD_SHIFT; + if (state & LockMask) + mod |= RETROKMOD_CAPSLOCK; + if (state & ControlMask) + mod |= RETROKMOD_CTRL; + if (state & Mod1Mask) + mod |= RETROKMOD_ALT; + if (state & Mod4Mask) + mod |= RETROKMOD_META; input_keyboard_event(down, key, chars[0], mod); + for (i = 1; i < num; i++) input_keyboard_event(down, RETROK_UNKNOWN, chars[i], mod); } diff --git a/input/keyboard_event_xkb.c b/input/keyboard_event_xkb.c index c46cc0b0c8..60436ea407 100644 --- a/input/keyboard_event_xkb.c +++ b/input/keyboard_event_xkb.c @@ -60,8 +60,9 @@ void handle_xkb( (enum xkb_state_component)((XKB_STATE_MODS_EFFECTIVE) > 0)) ? *map_bit : 0; } - input_keyboard_event(value, input_translate_keysym_to_rk(code), + input_keyboard_event(value, input_keymaps_translate_keysym_to_rk(code), num_syms ? xkb_keysym_to_utf32(syms[0]) : 0, mod); + for (i = 1; i < num_syms; i++) input_keyboard_event(value, RETROK_UNKNOWN, xkb_keysym_to_utf32(syms[i]), mod); } diff --git a/input/linuxraw_input.c b/input/linuxraw_input.c index 1855f07764..a1064aa493 100644 --- a/input/linuxraw_input.c +++ b/input/linuxraw_input.c @@ -115,7 +115,7 @@ static void *linuxraw_input_init(void) atexit(linuxraw_resetKbmd); linuxraw->joypad = input_joypad_init_driver(g_settings.input.joypad_driver); - input_init_keyboard_lut(rarch_key_map_linux); + input_keymaps_init_keyboard_lut(rarch_key_map_linux); /* We need to disable use of stdin command interface if * stdin is supposed to be used for input. */ @@ -126,7 +126,7 @@ static void *linuxraw_input_init(void) static bool linuxraw_key_pressed(linuxraw_input_t *linuxraw, int key) { - unsigned sym = input_translate_rk_to_keysym((enum retro_key)key); + unsigned sym = input_keymaps_translate_rk_to_keysym((enum retro_key)key); return linuxraw->state[sym]; } @@ -149,6 +149,7 @@ static int16_t linuxraw_analog_pressed(linuxraw_input_t *linuxraw, int16_t pressed_minus = 0, pressed_plus = 0; unsigned id_minus = 0; unsigned id_plus = 0; + input_conv_analog_id_to_bind_id(idx, id, &id_minus, &id_plus); if (linuxraw_is_pressed(linuxraw, binds, id_minus)) @@ -162,8 +163,6 @@ static int16_t linuxraw_analog_pressed(linuxraw_input_t *linuxraw, static bool linuxraw_bind_button_pressed(void *data, int key) { linuxraw_input_t *linuxraw = (linuxraw_input_t*)data; - if (!linuxraw) - return false; return linuxraw_is_pressed(linuxraw, g_settings.input.binds[0], key) || input_joypad_pressed(linuxraw->joypad, 0, g_settings.input.binds[0], key); } @@ -172,8 +171,8 @@ static int16_t linuxraw_input_state(void *data, const struct retro_keybind **binds, unsigned port, unsigned device, unsigned idx, unsigned id) { - linuxraw_input_t *linuxraw = (linuxraw_input_t*)data; int16_t ret; + linuxraw_input_t *linuxraw = (linuxraw_input_t*)data; switch (device) { diff --git a/input/rwebinput_input.c b/input/rwebinput_input.c index d4a0bea1e0..605506e746 100644 --- a/input/rwebinput_input.c +++ b/input/rwebinput_input.c @@ -46,18 +46,22 @@ static void *rwebinput_input_init(void) return NULL; } - input_init_keyboard_lut(rarch_key_map_rwebinput); + input_keymaps_init_keyboard_lut(rarch_key_map_rwebinput); return rwebinput; } static bool rwebinput_key_pressed(rwebinput_input_t *rwebinput, int key) { + unsigned sym; + bool ret; + if (key >= RETROK_LAST) return false; - unsigned sym = input_translate_rk_to_keysym((enum retro_key)key); - bool ret = rwebinput->state.keys[sym >> 3] & (1 << (sym & 7)); + sym = input_keymaps_translate_rk_to_keysym((enum retro_key)key); + ret = rwebinput->state.keys[sym >> 3] & (1 << (sym & 7)); + return ret; } @@ -68,8 +72,8 @@ static bool rwebinput_is_pressed(rwebinput_input_t *rwebinput, const struct retr const struct retro_keybind *bind = &binds[id]; return bind->valid && rwebinput_key_pressed(rwebinput, binds[id].key); } - else - return false; + + return false; } static bool rwebinput_bind_button_pressed(void *data, int key) @@ -90,20 +94,25 @@ static int16_t rwebinput_mouse_state(rwebinput_input_t *rwebinput, unsigned id) return rwebinput->state.mouse_l; case RETRO_DEVICE_ID_MOUSE_RIGHT: return rwebinput->state.mouse_r; - default: - return 0; } + + return 0; } static int16_t rwebinput_analog_pressed(rwebinput_input_t *rwebinput, const struct retro_keybind *binds, unsigned idx, unsigned id) { + int16_t pressed_minus = 0, pressed_plus = 0; unsigned id_minus = 0; unsigned id_plus = 0; + input_conv_analog_id_to_bind_id(idx, id, &id_minus, &id_plus); - int16_t pressed_minus = rwebinput_is_pressed(rwebinput, binds, id_minus) ? -0x7fff : 0; - int16_t pressed_plus = rwebinput_is_pressed(rwebinput, binds, id_plus) ? 0x7fff : 0; + if (rwebinput_is_pressed(rwebinput, binds, id_minus)) + pressed_minus = -0x7fff; + if (rwebinput_is_pressed(rwebinput, binds, id_plus)) + pressed_plus = 0x7fff; + return pressed_plus + pressed_minus; } @@ -125,10 +134,9 @@ static int16_t rwebinput_input_state(void *data, const struct retro_keybind **bi case RETRO_DEVICE_MOUSE: return rwebinput_mouse_state(rwebinput, id); - - default: - return 0; } + + return 0; } static void rwebinput_input_free(void *data) @@ -146,23 +154,25 @@ static void rwebinput_input_free(void *data) static void rwebinput_input_poll(void *data) { + unsigned i; rwebinput_input_t *rwebinput = (rwebinput_input_t*)data; + rwebinput_state_t *state = RWebInputPoll(rwebinput->context); - rwebinput_state_t *state = RWebInputPoll(rwebinput->context); - - // get new keys - for (unsigned i = 0; i < 32; i++) + /* Get new keys. */ + for (i = 0; i < 32; i++) { - if (state->keys[i] != rwebinput->state.keys[i]) + unsigned k; + uint8_t diff; + + if (state->keys[i] == rwebinput->state.keys[i]) + continue; + + diff = state->keys[i] ^ rwebinput->state.keys[i]; + + for (k = 0; diff; diff >>= 1, k++) { - uint8_t diff = state->keys[i] ^ rwebinput->state.keys[i]; - for (unsigned k = 0; diff; diff >>= 1, k++) - { - if (diff & 1) - { - input_keyboard_event((state->keys[i] & (1 << k)) != 0, input_translate_keysym_to_rk(i * 8 + k), 0, 0); - } - } + if (diff & 1) + input_keyboard_event((state->keys[i] & (1 << k)) != 0, input_keymaps_translate_keysym_to_rk(i * 8 + k), 0, 0); } } diff --git a/input/sdl_input.c b/input/sdl_input.c index 8a6db243e0..264ac6b261 100644 --- a/input/sdl_input.c +++ b/input/sdl_input.c @@ -40,7 +40,7 @@ typedef struct sdl_input static void *sdl_input_init(void) { - input_init_keyboard_lut(rarch_key_map_sdl); + input_keymaps_init_keyboard_lut(rarch_key_map_sdl); sdl_input_t *sdl = (sdl_input_t*)calloc(1, sizeof(*sdl)); if (!sdl) return NULL; @@ -53,13 +53,15 @@ static void *sdl_input_init(void) static bool sdl_key_pressed(int key) { + int num_keys; + const uint8_t *keymap; + unsigned sym; + if (key >= RETROK_LAST) return false; - unsigned sym = input_translate_rk_to_keysym((enum retro_key)key); + sym = input_keymaps_translate_rk_to_keysym((enum retro_key)key); - int num_keys; - const uint8_t *keymap; #if HAVE_SDL2 sym = SDL_GetScancodeFromKey(sym); keymap = SDL_GetKeyboardState(&num_keys); @@ -83,12 +85,17 @@ static bool sdl_is_pressed(sdl_input_t *sdl, unsigned port_num, const struct ret static int16_t sdl_analog_pressed(sdl_input_t *sdl, const struct retro_keybind *binds, unsigned idx, unsigned id) { + int16_t pressed_minus = 0, pressed_plus = 0; unsigned id_minus = 0; unsigned id_plus = 0; + input_conv_analog_id_to_bind_id(idx, id, &id_minus, &id_plus); - int16_t pressed_minus = sdl_key_pressed(binds[id_minus].key) ? -0x7fff : 0; - int16_t pressed_plus = sdl_key_pressed(binds[id_plus].key) ? 0x7fff : 0; + if (sdl_key_pressed(binds[id_minus].key)) + pressed_minus = -0x7fff; + if (sdl_key_pressed(binds[id_plus].key)) + pressed_plus = 0x7fff; + return pressed_plus + pressed_minus; } @@ -141,19 +148,21 @@ static int16_t sdl_mouse_device_state(sdl_input_t *sdl, unsigned id) return sdl->mouse_y; case RETRO_DEVICE_ID_MOUSE_MIDDLE: return sdl->mouse_m; - default: - return 0; } + + return 0; } static int16_t sdl_pointer_device_state(sdl_input_t *sdl, unsigned idx, unsigned id, bool screen) { + bool valid, inside; + int16_t res_x = 0, res_y = 0, res_screen_x = 0, res_screen_y = 0; + if (idx != 0) return 0; - int16_t res_x = 0, res_y = 0, res_screen_x = 0, res_screen_y = 0; - bool valid = input_translate_coord_viewport(sdl->mouse_abs_x, sdl->mouse_abs_y, + valid = input_translate_coord_viewport(sdl->mouse_abs_x, sdl->mouse_abs_y, &res_x, &res_y, &res_screen_x, &res_screen_y); if (!valid) @@ -165,7 +174,7 @@ static int16_t sdl_pointer_device_state(sdl_input_t *sdl, res_y = res_screen_y; } - bool inside = (res_x >= -0x7fff) && (res_y >= -0x7fff); + inside = (res_x >= -0x7fff) && (res_y >= -0x7fff); if (!inside) return 0; @@ -178,9 +187,9 @@ static int16_t sdl_pointer_device_state(sdl_input_t *sdl, return res_y; case RETRO_DEVICE_ID_POINTER_PRESSED: return sdl->mouse_l; - default: - return 0; } + + return 0; } static int16_t sdl_lightgun_device_state(sdl_input_t *sdl, unsigned id) @@ -201,15 +210,16 @@ static int16_t sdl_lightgun_device_state(sdl_input_t *sdl, unsigned id) return sdl->mouse_m && sdl->mouse_r; case RETRO_DEVICE_ID_LIGHTGUN_PAUSE: return sdl->mouse_m && sdl->mouse_l; - default: - return 0; } + + return 0; } static int16_t sdl_input_state(void *data_, const struct retro_keybind **binds, unsigned port, unsigned device, unsigned idx, unsigned id) { sdl_input_t *data = (sdl_input_t*)data_; + switch (device) { case RETRO_DEVICE_JOYPAD: @@ -232,10 +242,12 @@ static int16_t sdl_input_state(void *data_, const struct retro_keybind **binds, static void sdl_input_free(void *data) { + sdl_input_t *sdl = (sdl_input_t*)data; + if (!data) return; - // Flush out all pending events. + /* Flush out all pending events. */ #ifdef HAVE_SDL2 SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT); #else @@ -243,8 +255,6 @@ static void sdl_input_free(void *data) while (SDL_PollEvent(&event)); #endif - sdl_input_t *sdl = (sdl_input_t*)data; - if (sdl->joypad) sdl->joypad->destroy(); @@ -254,17 +264,17 @@ static void sdl_input_free(void *data) #ifdef HAVE_SDL2 static void sdl_grab_mouse(void *data, bool state) { + struct temp{ + SDL_Window *w; + }; sdl_input_t *sdl = (sdl_input_t*)data; - if (driver.video == &video_sdl2) - { - /* first member of sdl2_video_t is the window */ - struct temp{ - SDL_Window *w; - }; - SDL_SetWindowGrab(((struct temp*)driver.video_data)->w, - state ? SDL_TRUE : SDL_FALSE); - } + if (driver.video != &video_sdl2) + return; + + /* First member of sdl2_video_t is the window */ + SDL_SetWindowGrab(((struct temp*)driver.video_data)->w, + state ? SDL_TRUE : SDL_FALSE); } #endif @@ -272,6 +282,8 @@ static bool sdl_set_rumble(void *data, unsigned port, enum retro_rumble_effect effect, uint16_t strength) { sdl_input_t *sdl = (sdl_input_t*)data; + if (!sdl) + return false; return input_joypad_set_rumble(sdl->joypad, port, effect, strength); } @@ -284,7 +296,9 @@ static const rarch_joypad_driver_t *sdl_get_joypad_driver(void *data) static void sdl_poll_mouse(sdl_input_t *sdl) { Uint8 btn = SDL_GetRelativeMouseState(&sdl->mouse_x, &sdl->mouse_y); + SDL_GetMouseState(&sdl->mouse_abs_x, &sdl->mouse_abs_y); + sdl->mouse_l = SDL_BUTTON(SDL_BUTTON_LEFT) & btn ? 1 : 0; sdl->mouse_r = SDL_BUTTON(SDL_BUTTON_RIGHT) & btn ? 1 : 0; sdl->mouse_m = SDL_BUTTON(SDL_BUTTON_MIDDLE) & btn ? 1 : 0; @@ -296,9 +310,10 @@ static void sdl_poll_mouse(sdl_input_t *sdl) static void sdl_input_poll(void *data) { - SDL_PumpEvents(); sdl_input_t *sdl = (sdl_input_t*)data; + SDL_PumpEvents(); + if (sdl->joypad) sdl->joypad->poll(); sdl_poll_mouse(sdl); @@ -313,7 +328,7 @@ static void sdl_input_poll(void *data) if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) { uint16_t mod = 0; - unsigned code = input_translate_keysym_to_rk(event.key.keysym.sym); + unsigned code = input_keymaps_translate_keysym_to_rk(event.key.keysym.sym); if (event.key.keysym.mod & KMOD_SHIFT) mod |= RETROKMOD_SHIFT; diff --git a/input/udev_input.c b/input/udev_input.c index 5322203cf8..59c5b98802 100644 --- a/input/udev_input.c +++ b/input/udev_input.c @@ -486,7 +486,7 @@ static bool udev_input_is_pressed(udev_input_t *udev, const struct retro_keybind if (id < RARCH_BIND_LIST_END) { const struct retro_keybind *bind = &binds[id]; - unsigned bit = input_translate_rk_to_keysym(binds[id].key); + unsigned bit = input_keymaps_translate_rk_to_keysym(binds[id].key); return bind->valid && BIT_GET(udev->key_state, bit); } return false; @@ -512,8 +512,8 @@ static int16_t udev_analog_pressed(udev_input_t *udev, static int16_t udev_input_state(void *data, const struct retro_keybind **binds, unsigned port, unsigned device, unsigned idx, unsigned id) { - udev_input_t *udev = (udev_input_t*)data; int16_t ret; + udev_input_t *udev = (udev_input_t*)data; switch (device) { @@ -529,7 +529,7 @@ static int16_t udev_input_state(void *data, const struct retro_keybind **binds, case RETRO_DEVICE_KEYBOARD: { - unsigned bit = input_translate_rk_to_keysym((enum retro_key)id); + unsigned bit = input_keymaps_translate_rk_to_keysym((enum retro_key)id); return id < RETROK_LAST && BIT_GET(udev->key_state, bit); } case RETRO_DEVICE_MOUSE: @@ -603,19 +603,19 @@ static bool open_devices(udev_input_t *udev, const char *type, device_handle_cb udev_enumerate_add_match_property(enumerate, type, "1"); udev_enumerate_scan_devices(enumerate); devs = udev_enumerate_get_list_entry(enumerate); + for (item = devs; item; item = udev_list_entry_get_next(item)) { - const char *name = udev_list_entry_get_name(item); + const char *name = udev_list_entry_get_name(item); /* Get the filename of the /sys entry for the device * and create a udev_device object (dev) representing it. */ struct udev_device *dev = udev_device_new_from_syspath(udev->udev, name); const char *devnode = udev_device_get_devnode(dev); - int fd = devnode ? open(devnode, O_RDONLY | O_NONBLOCK) : -1; - if (devnode) { + int fd = open(devnode, O_RDONLY | O_NONBLOCK); RARCH_LOG("[udev] Adding device %s as type %s.\n", devnode, type); if (!add_device(udev, devnode, cb)) RARCH_ERR("[udev] Failed to open device: %s (%s).\n", devnode, strerror(errno)); @@ -802,7 +802,7 @@ static void *udev_input_init(void) RARCH_WARN("[udev]: Couldn't open any keyboard, mouse or touchpad. Are permissions set correctly for /dev/input/event*?\n"); udev->joypad = input_joypad_init_driver(g_settings.input.joypad_driver); - input_init_keyboard_lut(rarch_key_map_linux); + input_keymaps_init_keyboard_lut(rarch_key_map_linux); disable_terminal_input(); return udev; diff --git a/input/x11_input.c b/input/x11_input.c index d0deeb0486..2a5854de5f 100644 --- a/input/x11_input.c +++ b/input/x11_input.c @@ -60,19 +60,23 @@ static void *x_input_init(void) x11->win = (Window)driver.video_window; x11->joypad = input_joypad_init_driver(g_settings.input.joypad_driver); - input_init_keyboard_lut(rarch_key_map_x11); + input_keymaps_init_keyboard_lut(rarch_key_map_x11); return x11; } static bool x_key_pressed(x11_input_t *x11, int key) { + unsigned sym; + int keycode; + bool ret; + if (key >= RETROK_LAST) return false; - unsigned sym = input_translate_rk_to_keysym((enum retro_key)key); - int keycode = XKeysymToKeycode(x11->display, sym); - bool ret = x11->state[keycode >> 3] & (1 << (keycode & 7)); + sym = input_keymaps_translate_rk_to_keysym((enum retro_key)key); + keycode = XKeysymToKeycode(x11->display, sym); + ret = x11->state[keycode >> 3] & (1 << (keycode & 7)); return ret; } @@ -90,18 +94,25 @@ static bool x_is_pressed(x11_input_t *x11, static int16_t x_pressed_analog(x11_input_t *x11, const struct retro_keybind *binds, unsigned idx, unsigned id) { + int16_t pressed_minus = 0, pressed_plus = 0; unsigned id_minus = 0; unsigned id_plus = 0; + input_conv_analog_id_to_bind_id(idx, id, &id_minus, &id_plus); - int16_t pressed_minus = x_is_pressed(x11, binds, id_minus) ? -0x7fff : 0; - int16_t pressed_plus = x_is_pressed(x11, binds, id_plus) ? 0x7fff : 0; + if (x_is_pressed(x11, binds, id_minus)) + pressed_minus = -0x7fff; + if (x_is_pressed(x11, binds, id_plus)) + pressed_plus = 0x7fff; + return pressed_plus + pressed_minus; } static bool x_bind_button_pressed(void *data, int key) { x11_input_t *x11 = (x11_input_t*)data; + if (!x11) + return false; return x_is_pressed(x11, g_settings.input.binds[0], key) || input_joypad_pressed(x11->joypad, 0, g_settings.input.binds[0], key); } @@ -132,11 +143,13 @@ static int16_t x_mouse_state(x11_input_t *x11, unsigned id) static int16_t x_pointer_state(x11_input_t *x11, unsigned idx, unsigned id, bool screen) { + int16_t res_x = 0, res_y = 0, res_screen_x = 0, res_screen_y = 0; + bool valid, inside; + if (idx != 0) return 0; - int16_t res_x = 0, res_y = 0, res_screen_x = 0, res_screen_y = 0; - bool valid = input_translate_coord_viewport(x11->mouse_x, x11->mouse_y, + valid = input_translate_coord_viewport(x11->mouse_x, x11->mouse_y, &res_x, &res_y, &res_screen_x, &res_screen_y); if (!valid) @@ -148,7 +161,7 @@ static int16_t x_pointer_state(x11_input_t *x11, res_y = res_screen_y; } - bool inside = (res_x >= -0x7fff) && (res_y >= -0x7fff); + inside = (res_x >= -0x7fff) && (res_y >= -0x7fff); if (!inside) return 0; @@ -184,17 +197,17 @@ static int16_t x_lightgun_state(x11_input_t *x11, unsigned id) return x11->mouse_m && x11->mouse_r; case RETRO_DEVICE_ID_LIGHTGUN_PAUSE: return x11->mouse_m && x11->mouse_l; - default: - return 0; } + + return 0; } static int16_t x_input_state(void *data, const struct retro_keybind **binds, unsigned port, unsigned device, unsigned idx, unsigned id) { - x11_input_t *x11 = (x11_input_t*)data; int16_t ret; + x11_input_t *x11 = (x11_input_t*)data; switch (device) { @@ -304,13 +317,16 @@ static void x_input_poll(void *data) static void x_grab_mouse(void *data, bool state) { x11_input_t *x11 = (x11_input_t*)data; - x11->grab_mouse = state; + if (x11) + x11->grab_mouse = state; } static bool x_set_rumble(void *data, unsigned port, enum retro_rumble_effect effect, uint16_t strength) { x11_input_t *x11 = (x11_input_t*)data; + if (!x11) + return false; return input_joypad_set_rumble(x11->joypad, port, effect, strength); } diff --git a/settings.c b/settings.c index bcc806ae1d..ac0248f6ea 100644 --- a/settings.c +++ b/settings.c @@ -21,6 +21,7 @@ #include "config.def.h" #include #include "input/input_common.h" +#include "input/input_keymaps.h" #include "settings.h" #ifdef HAVE_CONFIG_H @@ -1535,7 +1536,7 @@ static void save_keybind_key(config_file_t *conf, const char *prefix, char key[64], btn[64]; snprintf(key, sizeof(key), "%s_%s", prefix, base); - input_translate_rk_to_str(bind->key, btn, sizeof(btn)); + input_keymaps_translate_rk_to_str(bind->key, btn, sizeof(btn)); config_set_string(conf, key, btn); }