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] 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,