From 494a190c5bd241e4b9257ab81b37eb486fae9bcd Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 3 Apr 2015 01:19:51 +0200 Subject: [PATCH] (drivers_joypad) Cleanups --- input/drivers_joypad/android_joypad.c | 2 +- input/drivers_joypad/linuxraw_joypad.c | 12 ++- input/drivers_joypad/parport_joypad.c | 17 ++-- input/drivers_joypad/ps3_joypad.c | 8 +- input/drivers_joypad/sdl_joypad.c | 106 ++++++++++++++----------- input/drivers_joypad/udev_joypad.c | 2 - input/drivers_joypad/xdk_joypad.c | 19 +++-- input/drivers_joypad/xinput_joypad.c | 16 ++-- 8 files changed, 98 insertions(+), 84 deletions(-) diff --git a/input/drivers_joypad/android_joypad.c b/input/drivers_joypad/android_joypad.c index c1a9b3b9ac..cf9a280238 100644 --- a/input/drivers_joypad/android_joypad.c +++ b/input/drivers_joypad/android_joypad.c @@ -25,7 +25,7 @@ static const char *android_joypad_name(unsigned pad) static bool android_joypad_init(void) { unsigned autoconf_pad; - settings_t *settings = config_get_ptr(); + settings_t *settings = config_get_ptr(); autoconfig_params_t params = {{0}}; for (autoconf_pad = 0; autoconf_pad < MAX_USERS; autoconf_pad++) diff --git a/input/drivers_joypad/linuxraw_joypad.c b/input/drivers_joypad/linuxraw_joypad.c index ad13219133..84f9270a7f 100644 --- a/input/drivers_joypad/linuxraw_joypad.c +++ b/input/drivers_joypad/linuxraw_joypad.c @@ -15,9 +15,7 @@ */ #include "../input_autodetect.h" -#include "../input_common.h" #include "../../general.h" -#include "../../runloop.h" #include #include #include @@ -46,7 +44,7 @@ static int g_notify; static int g_epoll; static bool g_hotplug; -static void poll_pad(struct linuxraw_joypad *pad) +static void linuxraw_poll_pad(struct linuxraw_joypad *pad) { struct js_event event; @@ -122,7 +120,7 @@ static bool linuxraw_joypad_init_pad(const char *path, struct linuxraw_joypad *p static void handle_plugged_pad(void) { int i, rc; - size_t event_size = sizeof(struct inotify_event) + NAME_MAX + 1; + size_t event_size = sizeof(struct inotify_event) + NAME_MAX + 1; uint8_t *event_buf = (uint8_t*)calloc(1, event_size); if (!event_buf) @@ -211,7 +209,7 @@ retry: for (i = 0; i < ret; i++) { if (events[i].data.ptr) - poll_pad((struct linuxraw_joypad*)events[i].data.ptr); + linuxraw_poll_pad((struct linuxraw_joypad*)events[i].data.ptr); else handle_plugged_pad(); } @@ -228,7 +226,7 @@ static bool linuxraw_joypad_init(void) unsigned i; settings_t *settings = config_get_ptr(); - g_epoll = epoll_create(MAX_USERS + 1); + g_epoll = epoll_create(MAX_USERS + 1); if (g_epoll < 0) return false; @@ -255,7 +253,7 @@ static bool linuxraw_joypad_init(void) /* TODO - implement VID/PID? */ input_config_autoconfigure_joypad(¶ms); - poll_pad(pad); + linuxraw_poll_pad(pad); } else input_config_autoconfigure_joypad(¶ms); diff --git a/input/drivers_joypad/parport_joypad.c b/input/drivers_joypad/parport_joypad.c index 07a80825b2..97414009e1 100644 --- a/input/drivers_joypad/parport_joypad.c +++ b/input/drivers_joypad/parport_joypad.c @@ -14,7 +14,6 @@ */ #include "../input_autodetect.h" -#include "../input_common.h" #include "../../general.h" #include #include @@ -109,11 +108,11 @@ static void parport_poll_pad(struct parport_joypad *pad) static bool parport_joypad_init_pad(const char *path, struct parport_joypad *pad) { int i; - int datadir = 1; /* read */ char data; struct ppdev_frob_struct frob; - bool set_control = false; - int mode = IEEE1284_MODE_BYTE; + int datadir = 1; /* read */ + bool set_control = false; + int mode = IEEE1284_MODE_BYTE; settings_t *settings = config_get_ptr(); if (pad->fd >= 0) @@ -122,9 +121,9 @@ static bool parport_joypad_init_pad(const char *path, struct parport_joypad *pad if (access(path, R_OK | W_OK) < 0) return false; - pad->fd = open(path, O_RDWR | O_NONBLOCK); - + pad->fd = open(path, O_RDWR | O_NONBLOCK); *pad->ident = '\0'; + if (pad->fd >= 0) { RARCH_LOG("[Joypad]: Found parallel port: %s\n", path); @@ -208,8 +207,8 @@ static void parport_joypad_poll(void) static void destroy_pad(struct parport_joypad *pad) { - char data; - data = pad->saved_data; + char data = pad->saved_data; + if (ioctl(pad->fd, PPWDATA, &data) < 0) RARCH_ERR("[Joypad]: Failed to restore original data register on %s\n", pad->ident); @@ -231,7 +230,7 @@ static bool parport_joypad_init(void) bool found_disabled_button; char buf[PARPORT_NUM_BUTTONS * 3 + 1]; char pin[3 + 1]; - settings_t *settings = config_get_ptr(); + settings_t *settings = config_get_ptr(); autoconfig_params_t params = {{0}}; memset(buf, 0, PARPORT_NUM_BUTTONS * 3 + 1); diff --git a/input/drivers_joypad/ps3_joypad.c b/input/drivers_joypad/ps3_joypad.c index c9e7950842..b48c62b0be 100644 --- a/input/drivers_joypad/ps3_joypad.c +++ b/input/drivers_joypad/ps3_joypad.c @@ -35,7 +35,7 @@ static INLINE int16_t convert_u8_to_s16(uint8_t val) static const char *ps3_joypad_name(unsigned pad) { settings_t *settings = config_get_ptr(); - return settings->input.device_names[pad]; + return settings ? settings->input.device_names[pad] : NULL; } static bool ps3_joypad_init(void) @@ -77,8 +77,10 @@ static uint64_t ps3_joypad_get_buttons(unsigned port_num) static int16_t ps3_joypad_axis(unsigned port_num, uint32_t joyaxis) { - int val = 0, axis = -1; - bool is_neg = false, is_pos = false; + int val = 0; + int axis = -1; + bool is_neg = false; + bool is_pos = false; if (joyaxis == AXIS_NONE || port_num >= MAX_PADS) return 0; diff --git a/input/drivers_joypad/sdl_joypad.c b/input/drivers_joypad/sdl_joypad.c index 0253b66c75..e7498d4a4a 100644 --- a/input/drivers_joypad/sdl_joypad.c +++ b/input/drivers_joypad/sdl_joypad.c @@ -15,7 +15,6 @@ */ #include "../input_autodetect.h" -#include "../input_common.h" #include "SDL.h" #include "../../general.h" @@ -25,7 +24,7 @@ typedef struct _sdl_joypad #ifdef HAVE_SDL2 SDL_GameController *controller; SDL_Haptic *haptic; - int rumble_effect; // -1 = not initialized, -2 = error/unsupported + int rumble_effect; /* -1 = not initialized, -2 = error/unsupported */ #endif unsigned num_axes; unsigned num_buttons; @@ -46,7 +45,7 @@ static sdl_joypad_t sdl_pads[MAX_USERS]; static bool g_has_haptic; #endif -static const char* pad_name(unsigned id) +static const char* sdl_pad_name(unsigned id) { #ifdef HAVE_SDL2 if (sdl_pads[id].controller) @@ -57,7 +56,7 @@ static const char* pad_name(unsigned id) #endif } -static uint8_t pad_get_button(sdl_joypad_t *pad, unsigned button) +static uint8_t sdl_pad_get_button(sdl_joypad_t *pad, unsigned button) { #ifdef HAVE_SDL2 /* TODO: see if a LUT like winxinput_joypad.c's button_index_to_bitmap_code is needed. */ @@ -67,16 +66,16 @@ static uint8_t pad_get_button(sdl_joypad_t *pad, unsigned button) return SDL_JoystickGetButton(pad->joypad, button); } -static uint8_t pad_get_hat(sdl_joypad_t *pad, unsigned hat) +static uint8_t sdl_pad_get_hat(sdl_joypad_t *pad, unsigned hat) { #ifdef HAVE_SDL2 if (pad->controller) - return pad_get_button(pad, hat); + return sdl_pad_get_button(pad, hat); #endif return SDL_JoystickGetHat(pad->joypad, hat); } -static int16_t pad_get_axis(sdl_joypad_t *pad, unsigned axis) +static int16_t sdl_pad_get_axis(sdl_joypad_t *pad, unsigned axis) { #ifdef HAVE_SDL2 /* TODO: see if a rarch <-> sdl translation is needed. */ @@ -86,13 +85,13 @@ static int16_t pad_get_axis(sdl_joypad_t *pad, unsigned axis) return SDL_JoystickGetAxis(pad->joypad, axis); } -static void pad_connect(unsigned id) +static void sdl_pad_connect(unsigned id) { - sdl_joypad_t *pad = (sdl_joypad_t*)&sdl_pads[id]; - bool success = false; - int32_t product = 0; - int32_t vendor = 0; - settings_t *settings = config_get_ptr(); + sdl_joypad_t *pad = (sdl_joypad_t*)&sdl_pads[id]; + bool success = false; + int32_t product = 0; + int32_t vendor = 0; + settings_t *settings = config_get_ptr(); autoconfig_params_t params = {{0}}; #ifdef HAVE_SDL2 @@ -125,21 +124,21 @@ static void pad_connect(unsigned id) return; } - strlcpy(settings->input.device_names[id], pad_name(id), sizeof(settings->input.device_names[id])); + strlcpy(settings->input.device_names[id], sdl_pad_name(id), sizeof(settings->input.device_names[id])); #ifdef HAVE_SDL2 - guid = SDL_JoystickGetGUID(pad->joypad); - guid_ptr = (uint16_t*)guid.data; + guid = SDL_JoystickGetGUID(pad->joypad); + guid_ptr = (uint16_t*)guid.data; #ifdef __linux - vendor = guid_ptr[2]; - product = guid_ptr[4]; + vendor = guid_ptr[2]; + product = guid_ptr[4]; #elif _WIN32 - vendor = guid_ptr[0]; - product = guid_ptr[1]; + vendor = guid_ptr[0]; + product = guid_ptr[1]; #endif #endif params.idx = id; - strlcpy(params.name, pad_name(id), sizeof(params.name)); + strlcpy(params.name, sdl_pad_name(id), sizeof(params.name)); params.vid = vendor; params.pid = product; strlcpy(params.driver, sdl_joypad.ident, sizeof(params.driver)); @@ -147,7 +146,7 @@ static void pad_connect(unsigned id) input_config_autoconfigure_joypad(¶ms); RARCH_LOG("[SDL]: Joypad #%u (%04x:%04x) connected: %s.\n", id, vendor, - product, pad_name(id)); + product, sdl_pad_name(id)); #ifdef HAVE_SDL2 @@ -193,7 +192,7 @@ static void pad_connect(unsigned id) #endif } -static void pad_disconnect(unsigned id) +static void sdl_pad_disconnect(unsigned id) { settings_t *settings = config_get_ptr(); #ifdef HAVE_SDL2 @@ -222,7 +221,7 @@ static void sdl_joypad_destroy(void) { unsigned i; for (i = 0; i < MAX_USERS; i++) - pad_disconnect(i); + sdl_pad_disconnect(i); SDL_QuitSubSystem(g_subsystem); memset(sdl_pads, 0, sizeof(sdl_pads)); @@ -230,7 +229,7 @@ static void sdl_joypad_destroy(void) static bool sdl_joypad_init(void) { - unsigned i; + unsigned i, num_sticks; if (SDL_WasInit(0) == 0) { @@ -251,12 +250,12 @@ static bool sdl_joypad_init(void) memset(sdl_pads, 0, sizeof(sdl_pads)); - unsigned num_sticks = SDL_NumJoysticks(); + num_sticks = SDL_NumJoysticks(); if (num_sticks > MAX_USERS) num_sticks = MAX_USERS; for (i = 0; i < num_sticks; i++) - pad_connect(i); + sdl_pad_connect(i); #ifndef HAVE_SDL2 /* quit if no joypad is detected. */ @@ -280,21 +279,24 @@ error: static bool sdl_joypad_button(unsigned port, uint16_t joykey) { + sdl_joypad_t *pad = NULL; if (joykey == NO_BTN) return false; - sdl_joypad_t *pad = (sdl_joypad_t*)&sdl_pads[port]; + pad = (sdl_joypad_t*)&sdl_pads[port]; if (!pad->joypad) return false; - // Check hat. + /* Check hat. */ if (GET_HAT_DIR(joykey)) { + uint8_t dir; uint16_t hat = GET_HAT(joykey); if (hat >= pad->num_hats) return false; - Uint8 dir = pad_get_hat(pad, hat); + dir = sdl_pad_get_hat(pad, hat); + switch (GET_HAT_DIR(joykey)) { case HAT_UP_MASK: @@ -311,8 +313,8 @@ static bool sdl_joypad_button(unsigned port, uint16_t joykey) return false; } - // Check the button - if (joykey < pad->num_buttons && pad_get_button(pad, joykey)) + /* Check the button */ + if (joykey < pad->num_buttons && sdl_pad_get_button(pad, joykey)) return true; return false; @@ -330,16 +332,16 @@ static int16_t sdl_joypad_axis(unsigned port, uint32_t joyaxis) int16_t val = 0; if (AXIS_NEG_GET(joyaxis) < pad->num_axes) { - val = pad_get_axis(pad, AXIS_NEG_GET(joyaxis)); + val = sdl_pad_get_axis(pad, AXIS_NEG_GET(joyaxis)); if (val > 0) val = 0; - else if (val < -0x7fff) // -0x8000 can cause trouble if we later abs() it. + else if (val < -0x7fff) /* -0x8000 can cause trouble if we later abs() it. */ val = -0x7fff; } else if (AXIS_POS_GET(joyaxis) < pad->num_axes) { - val = pad_get_axis(pad, AXIS_POS_GET(joyaxis)); + val = sdl_pad_get_axis(pad, AXIS_POS_GET(joyaxis)); if (val < 0) val = 0; @@ -354,15 +356,18 @@ static void sdl_joypad_poll(void) SDL_Event event; SDL_PumpEvents(); - while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED) > 0) + + while (SDL_PeepEvents(&event, 1, + SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED) > 0) { - if (event.type == SDL_JOYDEVICEADDED) + switch (event.type) { - pad_connect(event.jdevice.which); - } - else if (event.type == SDL_JOYDEVICEREMOVED) - { - pad_disconnect(event.jdevice.which); + case SDL_JOYDEVICEADDED: + sdl_pad_connect(event.jdevice.which); + break; + case SDL_JOYDEVICEREMOVED: + sdl_pad_disconnect(event.jdevice.which); + break; } } #else @@ -381,15 +386,20 @@ static bool sdl_joypad_set_rumble(unsigned pad, enum retro_rumble_effect effect, if (!joypad->joypad || !joypad->haptic) return false; - efx.type = SDL_HAPTIC_LEFTRIGHT; - efx.leftright.type = SDL_HAPTIC_LEFTRIGHT; + efx.type = SDL_HAPTIC_LEFTRIGHT; + efx.leftright.type = SDL_HAPTIC_LEFTRIGHT; efx.leftright.length = 5000; switch (effect) { - case RETRO_RUMBLE_STRONG: efx.leftright.large_magnitude = strength; break; - case RETRO_RUMBLE_WEAK: efx.leftright.small_magnitude = strength; break; - default: return false; + case RETRO_RUMBLE_STRONG: + efx.leftright.large_magnitude = strength; + break; + case RETRO_RUMBLE_WEAK: + efx.leftright.small_magnitude = strength; + break; + default: + return false; } if (joypad->rumble_effect == -1) @@ -430,7 +440,7 @@ static const char *sdl_joypad_name(unsigned pad) if (pad >= MAX_USERS) return NULL; - return pad_name(pad); + return sdl_pad_name(pad); } rarch_joypad_driver_t sdl_joypad = { diff --git a/input/drivers_joypad/udev_joypad.c b/input/drivers_joypad/udev_joypad.c index bbdebe51dd..b369d27cfa 100644 --- a/input/drivers_joypad/udev_joypad.c +++ b/input/drivers_joypad/udev_joypad.c @@ -14,9 +14,7 @@ */ #include "../input_autodetect.h" -#include "../input_common.h" #include "../../general.h" -#include "../../runloop.h" #include #include #include diff --git a/input/drivers_joypad/xdk_joypad.c b/input/drivers_joypad/xdk_joypad.c index 13665a89eb..84aa643967 100644 --- a/input/drivers_joypad/xdk_joypad.c +++ b/input/drivers_joypad/xdk_joypad.c @@ -36,7 +36,7 @@ static const char* const XBOX_CONTROLLER_NAMES[4] = static const char *xdk_joypad_name(unsigned pad) { settings_t *settings = config_get_ptr(); - return settings->input.device_names[pad]; + return settings ? settings->input.device_names[pad] : NULL; } static bool xdk_joypad_init(void) @@ -95,8 +95,11 @@ static uint64_t xdk_joypad_get_buttons(unsigned port_num) static int16_t xdk_joypad_axis(unsigned port_num, uint32_t joyaxis) { - int val = 0, axis = -1; - bool is_neg = false, is_pos = false; + int val = 0; + int axis = -1; + bool is_neg = false; + bool is_pos = false; + if (joyaxis == AXIS_NONE || port_num >= MAX_PADS) return 0; @@ -139,12 +142,14 @@ static int16_t xdk_joypad_axis(unsigned port_num, uint32_t joyaxis) static void xdk_joypad_poll(void) { unsigned port; - uint64_t *state_p1 = NULL; - uint64_t *lifecycle_state = NULL; - global_t *global = global_get_ptr(); -#if defined(_XBOX1) +#ifdef _XBOX1 unsigned int dwInsertions, dwRemovals; +#endif + uint64_t *state_p1 = NULL; + uint64_t *lifecycle_state = NULL; + global_t *global = global_get_ptr(); +#if defined(_XBOX1) XGetDeviceChanges(XDEVICE_TYPE_GAMEPAD, reinterpret_cast(&dwInsertions), reinterpret_cast(&dwRemovals)); diff --git a/input/drivers_joypad/xinput_joypad.c b/input/drivers_joypad/xinput_joypad.c index b1505b8c6b..226d652b77 100644 --- a/input/drivers_joypad/xinput_joypad.c +++ b/input/drivers_joypad/xinput_joypad.c @@ -13,13 +13,14 @@ * If not, see . */ -// Support 360 controllers on Windows. -// Said controllers do show under DInput but they have limitations in this mode; -// The triggers are combined rather than seperate and it is not possible to use -// the guide button. - -// Some wrappers for other controllers also simulate xinput (as it is easier to implement) -// so this may be useful for those also. +/* Support 360 controllers on Windows. + * Said controllers do show under DInput but they have limitations in this mode; + * The triggers are combined rather than seperate and it is not possible to use + * the guide button. + * + * Some wrappers for other controllers also simulate xinput (as it is easier to implement) + * so this may be useful for those also. + **/ #include "../input_autodetect.h" #include "../input_common.h" @@ -274,6 +275,7 @@ static bool winxinput_joypad_query_pad(unsigned pad) static void winxinput_joypad_destroy(void) { unsigned i; + for (i = 0; i < 4; ++i) memset(&g_winxinput_states[i], 0, sizeof(winxinput_joypad_state));