(Dinput joypad) Some cleanups

This commit is contained in:
twinaphex 2020-06-11 04:46:15 +02:00
parent b363ed7d79
commit cfc511cc6b
1 changed files with 40 additions and 30 deletions

View File

@ -254,31 +254,48 @@ static bool guid_is_xinput_device(const GUID* product_guid)
for (i = 0; i < num_raw_devs; i++) for (i = 0; i < num_raw_devs; i++)
{ {
RID_DEVICE_INFO rdi; RID_DEVICE_INFO rdi;
char *devName = NULL; char *dev_name = NULL;
UINT rdiSize = sizeof(rdi); UINT rdi_size = sizeof(rdi);
UINT nameSize = 0; UINT name_size = 0;
rdi.cbSize = sizeof (rdi); rdi.cbSize = rdi_size;
if ((raw_devs[i].dwType == RIM_TYPEHID) && /*
(GetRawInputDeviceInfoA(raw_devs[i].hDevice, * Step 1 -
RIDI_DEVICEINFO, &rdi, &rdiSize) != ((UINT)-1)) && * Check if device type is HID
(MAKELONG(rdi.hid.dwVendorId, rdi.hid.dwProductId) * Step 2 -
== ((LONG)product_guid->Data1)) && * Query size of name
(GetRawInputDeviceInfoA(raw_devs[i].hDevice, RIDI_DEVICENAME, NULL, &nameSize) != ((UINT)-1)) && * Step 3 -
((devName = (char*)malloc(nameSize)) != NULL) && * Allocate string holding ID of device
(GetRawInputDeviceInfoA(raw_devs[i].hDevice, RIDI_DEVICENAME, devName, &nameSize) != ((UINT)-1)) && * Step 4 -
(strstr(devName, "IG_") != NULL) ) * query ID of device
* Step 5 -
* Check if the device ID contains "IG_".
* If it does, then it's an XInput device
* This information can not be found from DirectInput
*/
if (
(raw_devs[i].dwType == RIM_TYPEHID) /* 1 */
&& (GetRawInputDeviceInfoA(raw_devs[i].hDevice,
RIDI_DEVICEINFO, &rdi, &rdi_size) != ((UINT)-1))
&& (MAKELONG(rdi.hid.dwVendorId, rdi.hid.dwProductId)
== ((LONG)product_guid->Data1))
&& (GetRawInputDeviceInfoA(raw_devs[i].hDevice,
RIDI_DEVICENAME, NULL, &name_size) != ((UINT)-1)) /* 2 */
&& ((dev_name = (char*)malloc(name_size)) != NULL) /* 3 */
&& (GetRawInputDeviceInfoA(raw_devs[i].hDevice,
RIDI_DEVICENAME, dev_name, &name_size) != ((UINT)-1)) /* 4 */
&& (strstr(dev_name, "IG_")) /* 5 */
)
{ {
free(devName); free(dev_name);
free(raw_devs); free(raw_devs);
raw_devs = NULL; raw_devs = NULL;
return true; return true;
} }
if (devName) { if (dev_name)
free(devName); free(dev_name);
}
} }
free(raw_devs); free(raw_devs);
@ -370,6 +387,7 @@ static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p)
} }
#endif #endif
/* Set data format to simple joystick */
IDirectInputDevice8_SetDataFormat(*pad, &c_dfDIJoystick2); IDirectInputDevice8_SetDataFormat(*pad, &c_dfDIJoystick2);
IDirectInputDevice8_SetCooperativeLevel(*pad, IDirectInputDevice8_SetCooperativeLevel(*pad,
(HWND)video_driver_window_get(), (HWND)video_driver_window_get(),
@ -561,21 +579,13 @@ static void dinput_joypad_poll(void)
memset(&pad->joy_state, 0, sizeof(pad->joy_state)); memset(&pad->joy_state, 0, sizeof(pad->joy_state));
/* If this fails, something *really* bad must have happened. */
if (FAILED(IDirectInputDevice8_Poll(pad->joypad))) if (FAILED(IDirectInputDevice8_Poll(pad->joypad)))
{ if (
if (FAILED(IDirectInputDevice8_Acquire(pad->joypad))) FAILED(IDirectInputDevice8_Acquire(pad->joypad))
{ || FAILED(IDirectInputDevice8_Poll(pad->joypad))
memset(&pad->joy_state, 0, sizeof(DIJOYSTATE2)); )
continue; continue;
}
/* If this fails, something *really* bad must have happened. */
if (FAILED(IDirectInputDevice8_Poll(pad->joypad)))
{
memset(&pad->joy_state, 0, sizeof(DIJOYSTATE2));
continue;
}
}
ret = IDirectInputDevice8_GetDeviceState(pad->joypad, ret = IDirectInputDevice8_GetDeviceState(pad->joypad,
sizeof(DIJOYSTATE2), &pad->joy_state); sizeof(DIJOYSTATE2), &pad->joy_state);