From eb3753ee3c81f6cb47140ca4142a920e074caa33 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 28 Jul 2019 06:42:03 +0200 Subject: [PATCH] (XInput/DirectInput) Show device disconnect messages when unplugging a gamepad --- input/drivers_joypad/dinput_joypad.c | 6 +++++- input/drivers_joypad/xinput_joypad.c | 15 +++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/input/drivers_joypad/dinput_joypad.c b/input/drivers_joypad/dinput_joypad.c index c2dc260fad..a94652c74f 100644 --- a/input/drivers_joypad/dinput_joypad.c +++ b/input/drivers_joypad/dinput_joypad.c @@ -553,6 +553,7 @@ static void dinput_joypad_poll(void) unsigned i; for (i = 0; i < MAX_USERS; i++) { + HRESULT ret; struct dinput_joypad_data *pad = &g_pads[i]; bool polled = g_xinput_pad_indexes[i] < 0; @@ -577,8 +578,11 @@ static void dinput_joypad_poll(void) } } - IDirectInputDevice8_GetDeviceState(pad->joypad, + ret = IDirectInputDevice8_GetDeviceState(pad->joypad, sizeof(DIJOYSTATE2), &pad->joy_state); + + if (ret == DIERR_INPUTLOST || ret == DIERR_NOTACQUIRED) + input_autoconfigure_disconnect(i, g_pads[i].joy_friendly_name); } } diff --git a/input/drivers_joypad/xinput_joypad.c b/input/drivers_joypad/xinput_joypad.c index 79fc97ceca..f4f7c59500 100644 --- a/input/drivers_joypad/xinput_joypad.c +++ b/input/drivers_joypad/xinput_joypad.c @@ -540,10 +540,13 @@ static void xinput_joypad_poll(void) #ifdef HAVE_DINPUT if (g_xinput_states[i].connected) { - if (g_XInputGetStateEx && g_XInputGetStateEx(i, + if (g_XInputGetStateEx(i, &(g_xinput_states[i].xstate)) == ERROR_DEVICE_NOT_CONNECTED) + { g_xinput_states[i].connected = false; + input_autoconfigure_disconnect(i, xinput_joypad_name(i)); + } } #else /* Normally, dinput handles device insertion/removal for us, but @@ -551,7 +554,7 @@ static void xinput_joypad_poll(void) /* Also note that on UWP, the controllers are not available on startup * and are instead 'plugged in' a moment later because Microsoft reasons */ /* TODO: This may be bad for performance? */ - bool new_connected = g_XInputGetStateEx && g_XInputGetStateEx(i, &(g_xinput_states[i].xstate)) != ERROR_DEVICE_NOT_CONNECTED; + bool new_connected = g_XInputGetStateEx(i, &(g_xinput_states[i].xstate)) != ERROR_DEVICE_NOT_CONNECTED; if (new_connected != g_xinput_states[i].connected) { if (new_connected) @@ -561,10 +564,10 @@ static void xinput_joypad_poll(void) xinput_joypad_init(NULL); return; } - else - { - g_xinput_states[i].connected = new_connected; - } + + g_xinput_states[i].connected = new_connected; + if (!g_xinput_states[i].connected) + input_autoconfigure_disconnect(i, xinput_joypad_name(i)); } #endif }