From 905e7679830ff042040cb2342ee89956fc3e80fb Mon Sep 17 00:00:00 2001 From: pinumbernumber <1337rz@gmail.com> Date: Tue, 27 Aug 2013 12:14:56 +0100 Subject: [PATCH 1/6] Add autoconfig support to xinput and dinput --- input/dinput.c | 53 +++++++++++++++++++++++++++++------- input/winxinput_joypad.c | 58 ++++++++++++++++++++++++---------------- 2 files changed, 79 insertions(+), 32 deletions(-) diff --git a/input/dinput.c b/input/dinput.c index 972772da75..487a179e5a 100644 --- a/input/dinput.c +++ b/input/dinput.c @@ -345,6 +345,14 @@ const input_driver_t input_dinput = { dinput_grab_mouse, }; +// Keep track of which pad indexes are 360 controllers +// not static, will be read in winxinput_joypad.c +// -1 = not xbox pad, otherwise 0..3 +int g_xbox_pad_indexes[MAX_PLAYERS]; + +// TODO: Move the name string to struct dinput_joypad +static char *g_pad_names[MAX_PLAYERS]; + static void dinput_joypad_destroy(void) { for (unsigned i = 0; i < MAX_PLAYERS; i++) @@ -358,6 +366,12 @@ static void dinput_joypad_destroy(void) g_joypad_cnt = 0; memset(g_pads, 0, sizeof(g_pads)); + + for (unsigned i = 0; i < MAX_PLAYERS; i++) + { + free (g_pad_names[i]); + g_pad_names[i] = NULL; + } // Can be blocked by global Dinput context. dinput_destroy_context(); @@ -380,7 +394,9 @@ static BOOL CALLBACK enum_axes_cb(const DIDEVICEOBJECTINSTANCE *inst, void *p) return DIENUM_CONTINUE; } -static const char* const XBOX_PAD_NAMES[] = +// Is there a better way of detecting dual XInput/DInput pads? This is going to get +// outdated, for example when the Xbox One controller becomes available. +static const char* const XINPUT_PAD_NAMES[] = { "Controller (Gamepad for Xbox 360)", "Controller (XBOX 360 For Windows)", @@ -397,7 +413,7 @@ static bool name_is_360_pad(const char* name) { for (unsigned i = 0; ; ++i) { - const char* t = XBOX_PAD_NAMES[i]; + const char* t = XINPUT_PAD_NAMES[i]; if (t == NULL) return false; else if (lstrcmpi(name, t) == 0) @@ -405,10 +421,8 @@ static bool name_is_360_pad(const char* name) } } -// Keep track of which pad indexes are 360 controllers -// not static, will be read in winxinput_joypad.c -// -1 = not xbox pad, otherwise 0..3 -int g_xbox_pad_indexes[MAX_PLAYERS]; +// Forward declaration +static const char *dinput_joypad_name(unsigned pad); static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p) { @@ -425,10 +439,15 @@ static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p) #endif return DIENUM_CONTINUE; + size_t name_len = strlen(inst->tszProductName) + 1; + g_pad_names[g_joypad_cnt] = malloc(name_len); + strncpy(g_pad_names[g_joypad_cnt], inst->tszProductName, name_len); + #ifdef HAVE_WINXINPUT int last_xbox_pad_index = 0; + bool is_360_pad = name_is_360_pad(inst->tszProductName); - if (name_is_360_pad(inst->tszProductName)) + if (is_360_pad) { if (last_xbox_pad_index < 4) g_xbox_pad_indexes[g_joypad_cnt] = last_xbox_pad_index; @@ -442,6 +461,17 @@ static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p) IDirectInputDevice8_EnumObjects(*pad, enum_axes_cb, *pad, DIDFT_ABSAXIS); + +#ifdef HAVE_WINXINPUT + if (!is_360_pad) +#else + if (1) +#endif + { + + input_config_autoconfigure_joypad(g_joypad_cnt, dinput_joypad_name(g_joypad_cnt), dinput_joypad.ident); + } + g_joypad_cnt++; @@ -455,6 +485,8 @@ static bool dinput_joypad_init(void) for (unsigned i = 0; i < MAX_PLAYERS; ++i) g_xbox_pad_indexes[i] = -1; + + memset(&g_pad_names, 0, sizeof(g_pad_names)); RARCH_LOG("Enumerating DInput joypads ...\n"); IDirectInput8_EnumDevices(g_ctx, DI8DEVCLASS_GAMECTRL, @@ -593,10 +625,13 @@ static bool dinput_joypad_query_pad(unsigned pad) return pad < MAX_PLAYERS && g_pads[pad].joypad; } + + static const char *dinput_joypad_name(unsigned pad) { - (void)pad; - // FIXME + if ((pad < MAX_PLAYERS) && (g_pad_names[pad])) + return g_pad_names[pad]; + return NULL; } diff --git a/input/winxinput_joypad.c b/input/winxinput_joypad.c index 00ab50c60b..2f0956bb3f 100644 --- a/input/winxinput_joypad.c +++ b/input/winxinput_joypad.c @@ -108,6 +108,27 @@ static int pad_index_to_xplayer_index(unsigned pad) return g_xbox_pad_indexes[pad]; } +static const char* const XBOX_CONTROLLER_NAMES[4] = +{ + "Xbox 360 Controller (Player 1)", + "Xbox 360 Controller (Player 2)", + "Xbox 360 Controller (Player 3)", + "Xbox 360 Controller (Player 4)" +}; + +const char* winxinput_joypad_name (unsigned pad) +{ + int xplayer = pad_index_to_xplayer_index(pad); + + if (xplayer < 0) + return dinput_joypad.name(pad); + else + // TODO: Different name if disconnected? + return XBOX_CONTROLLER_NAMES[xplayer]; +} + + + static bool winxinput_joypad_init(void) { g_winxinput_dll = NULL; @@ -120,6 +141,9 @@ static bool winxinput_joypad_init(void) // No need to check for existance as we will be checking LoadLibrary's // success anyway. + + // Note: Windows 8 ships with 1.4 but there doesn't + // seem to be any compelling reason to use it. const char* DLL_NAME = "xinput1_3.dll"; g_winxinput_dll = LoadLibrary(DLL_NAME); // Using dylib_* complicates building joyconfig. if (!g_winxinput_dll) @@ -136,7 +160,6 @@ static bool winxinput_joypad_init(void) RARCH_ERR("Failed to load xinput1_3.dll, ensure DirectX and controller drivers are up to date.\n"); return false; // DLL does not exist or is invalid } - } // If we get here then an xinput DLL is correctly loaded. @@ -146,7 +169,7 @@ static bool winxinput_joypad_init(void) if (!g_XInputGetStateEx) { - // no ordinal 100. (old version of x360ce perhaps?) Load the ordinary XInputGetState, + // no ordinal 100. (Old version of x360ce perhaps?) Load the ordinary XInputGetState, // at the cost of losing guide button support. g_winxinput_guide_button_supported = false; g_XInputGetStateEx = (XInputGetStateEx_t) GetProcAddress(g_winxinput_dll, "XInputGetState"); @@ -158,7 +181,7 @@ static bool winxinput_joypad_init(void) RARCH_WARN("XInput: No guide button support.\n"); } - // zero out the states + // Zero out the states for (unsigned i = 0; i < 4; ++i) memset(&g_winxinput_states[i], 0, sizeof(winxinput_joypad_state)); @@ -179,7 +202,15 @@ static bool winxinput_joypad_init(void) // We're going to have to be buddies with dinput if we want to be able // to use XI and non-XI controllers together. - return dinput_joypad.init(); + if (!dinput_joypad.init()) + return false; + + for (unsigned autoconf_pad = 0; autoconf_pad < MAX_PLAYERS; autoconf_pad++) + if (pad_index_to_xplayer_index(autoconf_pad) > -1) + input_config_autoconfigure_joypad(autoconf_pad, winxinput_joypad_name(autoconf_pad), winxinput_joypad.ident); + + return true; + } static bool winxinput_joypad_query_pad(unsigned pad) @@ -320,25 +351,6 @@ static void winxinput_joypad_poll(void) dinput_joypad.poll(); } -static const char* const XBOX_CONTROLLER_NAMES[4] = -{ - "Xbox 360 Controller (Player 1)", - "Xbox 360 Controller (Player 2)", - "Xbox 360 Controller (Player 3)", - "Xbox 360 Controller (Player 4)" -}; - -const char* winxinput_joypad_name (unsigned pad) -{ - int xplayer = pad_index_to_xplayer_index(pad); - - if (xplayer < 0) - return dinput_joypad.name(pad); - else - // TODO: Different name if disconnected? - return XBOX_CONTROLLER_NAMES[xplayer]; -} - const rarch_joypad_driver_t winxinput_joypad = { winxinput_joypad_init, winxinput_joypad_query_pad, From 6bed0cfa659271a80b286ed47db0f1dafb977dc6 Mon Sep 17 00:00:00 2001 From: pinumbernumber <1337rz@gmail.com> Date: Tue, 27 Aug 2013 14:38:20 +0100 Subject: [PATCH 2/6] More small adjustments to joypad drivers --- input/dinput.c | 12 ++++++++---- input/winxinput_joypad.c | 14 ++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/input/dinput.c b/input/dinput.c index 487a179e5a..f71d690ae0 100644 --- a/input/dinput.c +++ b/input/dinput.c @@ -394,8 +394,11 @@ static BOOL CALLBACK enum_axes_cb(const DIDEVICEOBJECTINSTANCE *inst, void *p) return DIENUM_CONTINUE; } -// Is there a better way of detecting dual XInput/DInput pads? This is going to get -// outdated, for example when the Xbox One controller becomes available. +// TODO: Use a better way of detecting dual XInput/DInput pads. This current method +// will not work correctly for third-party controllers or future MS pads (Xbox One?). +// An example of this is provided in the DX SDK, which advises "Enum each PNP device +// using WMI and check each device ID to see if it contains "IG_"". Unfortunately the +// example code is a horrible unsightly mess. static const char* const XINPUT_PAD_NAMES[] = { "Controller (Gamepad for Xbox 360)", @@ -452,6 +455,8 @@ static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p) if (last_xbox_pad_index < 4) g_xbox_pad_indexes[g_joypad_cnt] = last_xbox_pad_index; ++last_xbox_pad_index; + + goto enum_iteration_done; } #endif @@ -472,9 +477,8 @@ static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p) input_config_autoconfigure_joypad(g_joypad_cnt, dinput_joypad_name(g_joypad_cnt), dinput_joypad.ident); } - +enum_iteration_done: g_joypad_cnt++; - return DIENUM_CONTINUE; } diff --git a/input/winxinput_joypad.c b/input/winxinput_joypad.c index 2f0956bb3f..48b8e8dceb 100644 --- a/input/winxinput_joypad.c +++ b/input/winxinput_joypad.c @@ -108,12 +108,14 @@ static int pad_index_to_xplayer_index(unsigned pad) return g_xbox_pad_indexes[pad]; } +// Generic "XInput" instead of "Xbox 360", because there are +// some other non-xbox third party PC controllers. static const char* const XBOX_CONTROLLER_NAMES[4] = { - "Xbox 360 Controller (Player 1)", - "Xbox 360 Controller (Player 2)", - "Xbox 360 Controller (Player 3)", - "Xbox 360 Controller (Player 4)" + "XInput Controller (Player 1)", + "XInput Controller (Player 2)", + "XInput Controller (Player 3)", + "XInput Controller (Player 4)" }; const char* winxinput_joypad_name (unsigned pad) @@ -169,8 +171,8 @@ static bool winxinput_joypad_init(void) if (!g_XInputGetStateEx) { - // no ordinal 100. (Old version of x360ce perhaps?) Load the ordinary XInputGetState, - // at the cost of losing guide button support. + // no ordinal 100. (Presumably a wrapper.) Load the ordinary + // XInputGetState, at the cost of losing guide button support. g_winxinput_guide_button_supported = false; g_XInputGetStateEx = (XInputGetStateEx_t) GetProcAddress(g_winxinput_dll, "XInputGetState"); if (!g_XInputGetStateEx) From 2b6907e0abac51d2960fcfa7213813d8fb4b257f Mon Sep 17 00:00:00 2001 From: pinumbernumber <1337rz@gmail.com> Date: Tue, 27 Aug 2013 19:26:01 +0100 Subject: [PATCH 3/6] fix C++ (MSVC) build --- input/dinput.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/input/dinput.c b/input/dinput.c index f71d690ae0..989279df5c 100644 --- a/input/dinput.c +++ b/input/dinput.c @@ -443,7 +443,7 @@ static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p) return DIENUM_CONTINUE; size_t name_len = strlen(inst->tszProductName) + 1; - g_pad_names[g_joypad_cnt] = malloc(name_len); + g_pad_names[g_joypad_cnt] = (char*)malloc(name_len); strncpy(g_pad_names[g_joypad_cnt], inst->tszProductName, name_len); #ifdef HAVE_WINXINPUT From a2d5d201257c5c37a4067e4c1b26ebabe523ebd6 Mon Sep 17 00:00:00 2001 From: pinumbernumber <1337rz@gmail.com> Date: Wed, 28 Aug 2013 16:08:44 +0100 Subject: [PATCH 4/6] tiny XI tweak/fix --- input/winxinput_joypad.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/input/winxinput_joypad.c b/input/winxinput_joypad.c index 0083b17baa..abf7ec9fb7 100644 --- a/input/winxinput_joypad.c +++ b/input/winxinput_joypad.c @@ -103,7 +103,7 @@ typedef struct static winxinput_joypad_state g_winxinput_states[4]; -static int pad_index_to_xplayer_index(unsigned pad) +static inline int pad_index_to_xplayer_index(unsigned pad) { return g_xbox_pad_indexes[pad]; } @@ -219,7 +219,7 @@ static bool winxinput_joypad_query_pad(unsigned pad) { int xplayer = pad_index_to_xplayer_index(pad); if (xplayer > -1) - return g_winxinput_states[0].connected; + return g_winxinput_states[xplayer].connected; else return dinput_joypad.query_pad(pad); } @@ -264,6 +264,8 @@ static bool winxinput_joypad_button (unsigned port_num, uint16_t joykey) if (!(g_winxinput_states[xplayer].connected)) return false; + + //return false; uint16_t btn_word = g_winxinput_states[xplayer].xstate.Gamepad.wButtons; @@ -348,7 +350,7 @@ static void winxinput_joypad_poll(void) { for (unsigned i = 0; i < 4; ++i) if (g_winxinput_states[i].connected) - if (g_XInputGetStateEx(i, &(g_winxinput_states[i].xstate)) != ERROR_SUCCESS) + if (g_XInputGetStateEx(i, &(g_winxinput_states[i].xstate)) == ERROR_DEVICE_NOT_CONNECTED) g_winxinput_states[i].connected = false; dinput_joypad.poll(); From 37ebdcdec774a730f08062b3c6f9bcb38bf70e97 Mon Sep 17 00:00:00 2001 From: pinumbernumber <1337rz@gmail.com> Date: Wed, 28 Aug 2013 21:16:27 +0100 Subject: [PATCH 5/6] Add correct joypad name reporting to RGUI (for xi and di), also cleanup dinput name variables --- input/dinput.c | 32 ++++++++++++++++---------------- input/winxinput_joypad.c | 5 +++++ 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/input/dinput.c b/input/dinput.c index 9971f5bd93..0f787633a0 100644 --- a/input/dinput.c +++ b/input/dinput.c @@ -49,6 +49,7 @@ struct dinput_joypad { LPDIRECTINPUTDEVICE8 joypad; DIJOYSTATE2 joy_state; + char* joy_name; }; static unsigned g_joypad_cnt; @@ -350,9 +351,6 @@ const input_driver_t input_dinput = { // -1 = not xbox pad, otherwise 0..3 int g_xbox_pad_indexes[MAX_PLAYERS]; -// TODO: Move the name string to struct dinput_joypad -static char *g_pad_names[MAX_PLAYERS]; - static void dinput_joypad_destroy(void) { for (unsigned i = 0; i < MAX_PLAYERS; i++) @@ -362,16 +360,17 @@ static void dinput_joypad_destroy(void) IDirectInputDevice8_Unacquire(g_pads[i].joypad); IDirectInputDevice8_Release(g_pads[i].joypad); } + + if (g_pads[i].joy_name) + { + free(g_pads[i].joy_name); + g_pads[i].joy_name = NULL; + } + } g_joypad_cnt = 0; memset(g_pads, 0, sizeof(g_pads)); - - for (unsigned i = 0; i < MAX_PLAYERS; i++) - { - free (g_pad_names[i]); - g_pad_names[i] = NULL; - } // Can be blocked by global Dinput context. dinput_destroy_context(); @@ -443,8 +442,8 @@ static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p) return DIENUM_CONTINUE; size_t name_len = strlen(inst->tszProductName) + 1; - g_pad_names[g_joypad_cnt] = (char*)malloc(name_len); - strncpy(g_pad_names[g_joypad_cnt], inst->tszProductName, name_len); + g_pads[g_joypad_cnt].joy_name = (char*)malloc(name_len); + strncpy(g_pads[g_joypad_cnt].joy_name, inst->tszProductName, name_len); #ifdef HAVE_WINXINPUT int last_xbox_pad_index = 0; @@ -473,7 +472,7 @@ static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p) if (1) #endif { - + strlcpy(g_settings.input.device_names[g_joypad_cnt], dinput_joypad_name(g_joypad_cnt), sizeof(g_settings.input.device_names[g_joypad_cnt])); input_config_autoconfigure_joypad(g_joypad_cnt, dinput_joypad_name(g_joypad_cnt), dinput_joypad.ident); } @@ -488,9 +487,10 @@ static bool dinput_joypad_init(void) return false; for (unsigned i = 0; i < MAX_PLAYERS; ++i) + { g_xbox_pad_indexes[i] = -1; - - memset(&g_pad_names, 0, sizeof(g_pad_names)); + g_pads[i].joy_name = NULL; + } RARCH_LOG("Enumerating DInput joypads ...\n"); IDirectInput8_EnumDevices(g_ctx, DI8DEVCLASS_GAMECTRL, @@ -633,8 +633,8 @@ static bool dinput_joypad_query_pad(unsigned pad) static const char *dinput_joypad_name(unsigned pad) { - if ((pad < MAX_PLAYERS) && (g_pad_names[pad])) - return g_pad_names[pad]; + if (pad < MAX_PLAYERS) + return g_pads[pad].joy_name; return NULL; } diff --git a/input/winxinput_joypad.c b/input/winxinput_joypad.c index abf7ec9fb7..535e8b62ea 100644 --- a/input/winxinput_joypad.c +++ b/input/winxinput_joypad.c @@ -208,8 +208,13 @@ static bool winxinput_joypad_init(void) return false; for (unsigned autoconf_pad = 0; autoconf_pad < MAX_PLAYERS; autoconf_pad++) + { if (pad_index_to_xplayer_index(autoconf_pad) > -1) + { + strlcpy(g_settings.input.device_names[autoconf_pad], winxinput_joypad_name(autoconf_pad), sizeof(g_settings.input.device_names[autoconf_pad])); input_config_autoconfigure_joypad(autoconf_pad, winxinput_joypad_name(autoconf_pad), winxinput_joypad.ident); + } + } return true; From 0fb0d0e891ec168eca3a9f5d4185ea3cb3ce9818 Mon Sep 17 00:00:00 2001 From: pinumbernumber <1337rz@gmail.com> Date: Thu, 29 Aug 2013 09:27:13 +0100 Subject: [PATCH 6/6] coding style corrections --- input/dinput.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/input/dinput.c b/input/dinput.c index 0f787633a0..b3e15b2e67 100644 --- a/input/dinput.c +++ b/input/dinput.c @@ -361,11 +361,8 @@ static void dinput_joypad_destroy(void) IDirectInputDevice8_Release(g_pads[i].joypad); } - if (g_pads[i].joy_name) - { - free(g_pads[i].joy_name); - g_pads[i].joy_name = NULL; - } + free(g_pads[i].joy_name); + g_pads[i].joy_name = NULL; } @@ -418,7 +415,7 @@ static bool name_is_360_pad(const char* name) const char* t = XINPUT_PAD_NAMES[i]; if (t == NULL) return false; - else if (lstrcmpi(name, t) == 0) + else if (strcasecmp(name, t) == 0) return true; } } @@ -441,9 +438,7 @@ static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p) #endif return DIENUM_CONTINUE; - size_t name_len = strlen(inst->tszProductName) + 1; - g_pads[g_joypad_cnt].joy_name = (char*)malloc(name_len); - strncpy(g_pads[g_joypad_cnt].joy_name, inst->tszProductName, name_len); + g_pads[g_joypad_cnt].joy_name = strdup(inst->tszProductName); #ifdef HAVE_WINXINPUT int last_xbox_pad_index = 0; @@ -468,8 +463,6 @@ static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p) #ifdef HAVE_WINXINPUT if (!is_360_pad) -#else - if (1) #endif { strlcpy(g_settings.input.device_names[g_joypad_cnt], dinput_joypad_name(g_joypad_cnt), sizeof(g_settings.input.device_names[g_joypad_cnt]));