diff --git a/input/dinput.c b/input/dinput.c index 0d690044c4..b3e15b2e67 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; @@ -345,6 +346,11 @@ 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]; + static void dinput_joypad_destroy(void) { for (unsigned i = 0; i < MAX_PLAYERS; i++) @@ -354,6 +360,10 @@ static void dinput_joypad_destroy(void) IDirectInputDevice8_Unacquire(g_pads[i].joypad); IDirectInputDevice8_Release(g_pads[i].joypad); } + + free(g_pads[i].joy_name); + g_pads[i].joy_name = NULL; + } g_joypad_cnt = 0; @@ -380,7 +390,12 @@ static BOOL CALLBACK enum_axes_cb(const DIDEVICEOBJECTINSTANCE *inst, void *p) return DIENUM_CONTINUE; } -static const char* const XBOX_PAD_NAMES[] = +// 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)", "Controller (XBOX 360 For Windows)", @@ -397,18 +412,16 @@ 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) + else if (strcasecmp(name, t) == 0) return true; } } -// 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,14 +438,19 @@ static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p) #endif return DIENUM_CONTINUE; + g_pads[g_joypad_cnt].joy_name = strdup(inst->tszProductName); + #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; ++last_xbox_pad_index; + + goto enum_iteration_done; } #endif @@ -442,9 +460,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) +#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); + } +enum_iteration_done: g_joypad_cnt++; - return DIENUM_CONTINUE; } @@ -454,7 +480,10 @@ static bool dinput_joypad_init(void) return false; for (unsigned i = 0; i < MAX_PLAYERS; ++i) + { g_xbox_pad_indexes[i] = -1; + g_pads[i].joy_name = NULL; + } RARCH_LOG("Enumerating DInput joypads ...\n"); IDirectInput8_EnumDevices(g_ctx, DI8DEVCLASS_GAMECTRL, @@ -593,10 +622,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) + return g_pads[pad].joy_name; + return NULL; } diff --git a/input/winxinput_joypad.c b/input/winxinput_joypad.c index 86446c10ed..535e8b62ea 100644 --- a/input/winxinput_joypad.c +++ b/input/winxinput_joypad.c @@ -103,11 +103,34 @@ 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]; } +// 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] = +{ + "XInput Controller (Player 1)", + "XInput Controller (Player 2)", + "XInput Controller (Player 3)", + "XInput 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 +143,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 +162,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,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) @@ -158,7 +183,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,14 +204,27 @@ 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) + { + 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; + } 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); } @@ -231,6 +269,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; @@ -315,31 +355,12 @@ 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(); } -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,