Signed-off-by: Rémi Bernon rbernon@codeweavers.com --- dlls/xinput1_3/main.c | 134 +++++++++++++++++++++--------------------- 1 file changed, 67 insertions(+), 67 deletions(-)
diff --git a/dlls/xinput1_3/main.c b/dlls/xinput1_3/main.c index 248f8f796e5..25f3b0f0407 100644 --- a/dlls/xinput1_3/main.c +++ b/dlls/xinput1_3/main.c @@ -240,18 +240,18 @@ static BOOL VerifyGamepad(PHIDP_PREPARSED_DATA preparsed, XINPUT_CAPABILITIES *x return TRUE; }
-static DWORD HID_set_state(struct xinput_controller *device, XINPUT_VIBRATION *state) +static DWORD HID_set_state(struct xinput_controller *controller, XINPUT_VIBRATION *state) { - struct hid_platform_private *private = device->platform_private; + struct hid_platform_private *private = controller->platform_private; char *output_report_buf = private->output_report_buf; ULONG output_report_len = private->caps.OutputReportByteLength;
- if (device->caps.Flags & XINPUT_CAPS_FFB_SUPPORTED) + if (controller->caps.Flags & XINPUT_CAPS_FFB_SUPPORTED) { - device->vibration.wLeftMotorSpeed = state->wLeftMotorSpeed; - device->vibration.wRightMotorSpeed = state->wRightMotorSpeed; + controller->vibration.wLeftMotorSpeed = state->wLeftMotorSpeed; + controller->vibration.wRightMotorSpeed = state->wRightMotorSpeed;
- if (device->enabled) + if (controller->enabled) { memset(output_report_buf, 0, output_report_len); output_report_buf[0] = /* report id */ 0; @@ -395,16 +395,16 @@ static void HID_find_gamepads(void) LeaveCriticalSection(&xinput_crit); }
-static void remove_gamepad(struct xinput_controller *device) +static void remove_gamepad(struct xinput_controller *controller) { - EnterCriticalSection(&device->crit); + EnterCriticalSection(&controller->crit);
- if (device->platform_private) + if (controller->platform_private) { - struct hid_platform_private *private = device->platform_private; + struct hid_platform_private *private = controller->platform_private;
- controller_disable(device); - device->platform_private = NULL; + controller_disable(controller); + controller->platform_private = NULL;
CloseHandle(private->device); free(private->input_report_buf[0]); @@ -414,7 +414,7 @@ static void remove_gamepad(struct xinput_controller *device) free(private); }
- LeaveCriticalSection(&device->crit); + LeaveCriticalSection(&controller->crit); }
static void HID_destroy_gamepads(void) @@ -433,9 +433,9 @@ static BYTE scale_byte(LONG value, const struct axis_info *axis) return (((ULONGLONG)(value - axis->min)) * 0xff) / axis->range; }
-static void HID_update_state(struct xinput_controller *device, XINPUT_STATE *state) +static void HID_update_state(struct xinput_controller *controller, XINPUT_STATE *state) { - struct hid_platform_private *private = device->platform_private; + struct hid_platform_private *private = controller->platform_private; int i; char **report_buf = private->input_report_buf, *tmp; ULONG report_len = private->caps.InputReportByteLength; @@ -445,14 +445,14 @@ static void HID_update_state(struct xinput_controller *device, XINPUT_STATE *sta ULONG button_length, hat_value; LONG value;
- if (!device->enabled) return; + if (!controller->enabled) return;
if (!HidD_GetInputReport(private->device, report_buf[0], report_len)) { if (GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == ERROR_INVALID_HANDLE) { EnterCriticalSection(&xinput_crit); - remove_gamepad(device); + remove_gamepad(controller); LeaveCriticalSection(&xinput_crit); } else ERR("Failed to get input report, HidD_GetInputReport failed with error %u\n", GetLastError()); @@ -461,27 +461,27 @@ static void HID_update_state(struct xinput_controller *device, XINPUT_STATE *sta
if (memcmp(report_buf[0], report_buf[1], report_len) != 0) { - device->state.dwPacketNumber++; + controller->state.dwPacketNumber++; button_length = ARRAY_SIZE(buttons); status = HidP_GetUsages(HidP_Input, HID_USAGE_PAGE_BUTTON, 0, buttons, &button_length, private->preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsages HID_USAGE_PAGE_BUTTON returned %#x\n", status);
- device->state.Gamepad.wButtons = 0; + controller->state.Gamepad.wButtons = 0; for (i = 0; i < button_length; i++) { switch (buttons[i]) { - case 1: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_A; break; - case 2: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_B; break; - case 3: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_X; break; - case 4: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_Y; break; - case 5: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_SHOULDER; break; - case 6: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_SHOULDER; break; - case 7: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_BACK; break; - case 8: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_START; break; - case 9: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_THUMB; break; - case 10: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_THUMB; break; - case 11: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_GUIDE; break; + case 1: controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_A; break; + case 2: controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_B; break; + case 3: controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_X; break; + case 4: controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_Y; break; + case 5: controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_SHOULDER; break; + case 6: controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_SHOULDER; break; + case 7: controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_BACK; break; + case 8: controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_START; break; + case 9: controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_THUMB; break; + case 10: controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_THUMB; break; + case 11: controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_GUIDE; break; } }
@@ -496,81 +496,81 @@ static void HID_update_state(struct xinput_controller *device, XINPUT_STATE *sta case 0: break; case 1: - device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP; + controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP; break; case 2: - device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP | XINPUT_GAMEPAD_DPAD_RIGHT; + controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP | XINPUT_GAMEPAD_DPAD_RIGHT; break; case 3: - device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT; + controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT; break; case 4: - device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT | XINPUT_GAMEPAD_DPAD_DOWN; + controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT | XINPUT_GAMEPAD_DPAD_DOWN; break; case 5: - device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN; + controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN; break; case 6: - device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN | XINPUT_GAMEPAD_DPAD_LEFT; + controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN | XINPUT_GAMEPAD_DPAD_LEFT; break; case 7: - device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT; + controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT; break; case 8: - device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT | XINPUT_GAMEPAD_DPAD_UP; + controller->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT | XINPUT_GAMEPAD_DPAD_UP; break; } }
status = HidP_GetScaledUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_X, &value, private->preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetScaledUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_X returned %#x\n", status); - else device->state.Gamepad.sThumbLX = scale_short(value, &private->lx); + else controller->state.Gamepad.sThumbLX = scale_short(value, &private->lx);
status = HidP_GetScaledUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_Y, &value, private->preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetScaledUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_Y returned %#x\n", status); - else device->state.Gamepad.sThumbLY = -scale_short(value, &private->ly) - 1; + else controller->state.Gamepad.sThumbLY = -scale_short(value, &private->ly) - 1;
status = HidP_GetScaledUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RX, &value, private->preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetScaledUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_RX returned %#x\n", status); - else device->state.Gamepad.sThumbRX = scale_short(value, &private->rx); + else controller->state.Gamepad.sThumbRX = scale_short(value, &private->rx);
status = HidP_GetScaledUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RY, &value, private->preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetScaledUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_RY returned %#x\n", status); - else device->state.Gamepad.sThumbRY = -scale_short(value, &private->ry) - 1; + else controller->state.Gamepad.sThumbRY = -scale_short(value, &private->ry) - 1;
status = HidP_GetScaledUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RZ, &value, private->preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetScaledUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_RZ returned %#x\n", status); - else device->state.Gamepad.bRightTrigger = scale_byte(value, &private->rtrigger); + else controller->state.Gamepad.bRightTrigger = scale_byte(value, &private->rtrigger);
status = HidP_GetScaledUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_Z, &value, private->preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetScaledUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_Z returned %#x\n", status); - else device->state.Gamepad.bLeftTrigger = scale_byte(value, &private->ltrigger); + else controller->state.Gamepad.bLeftTrigger = scale_byte(value, &private->ltrigger); }
tmp = report_buf[0]; report_buf[0] = report_buf[1]; report_buf[1] = tmp; - memcpy(state, &device->state, sizeof(*state)); + memcpy(state, &controller->state, sizeof(*state)); }
-static BOOL verify_and_lock_device(struct xinput_controller *device) +static BOOL verify_and_lock_device(struct xinput_controller *controller) { - if (!device->platform_private) return FALSE; + if (!controller->platform_private) return FALSE;
- EnterCriticalSection(&device->crit); + EnterCriticalSection(&controller->crit);
- if (!device->platform_private) + if (!controller->platform_private) { - LeaveCriticalSection(&device->crit); + LeaveCriticalSection(&controller->crit); return FALSE; }
return TRUE; }
-static void unlock_device(struct xinput_controller *device) +static void unlock_device(struct xinput_controller *controller) { - LeaveCriticalSection(&device->crit); + LeaveCriticalSection(&controller->crit); }
BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) @@ -768,7 +768,7 @@ static BOOL trigger_is_on(const BYTE value)
static DWORD check_for_keystroke(const DWORD index, XINPUT_KEYSTROKE *keystroke) { - struct xinput_controller *device = &controllers[index]; + struct xinput_controller *controller = &controllers[index]; const XINPUT_GAMEPAD *cur; DWORD ret = ERROR_EMPTY; int i; @@ -795,26 +795,26 @@ static DWORD check_for_keystroke(const DWORD index, XINPUT_KEYSTROKE *keystroke) /* note: guide button does not send an event */ };
- if (!verify_and_lock_device(device)) return ERROR_DEVICE_NOT_CONNECTED; + if (!verify_and_lock_device(controller)) return ERROR_DEVICE_NOT_CONNECTED;
- cur = &device->state.Gamepad; + cur = &controller->state.Gamepad;
/*** buttons ***/ for (i = 0; i < ARRAY_SIZE(buttons); ++i) { - if ((cur->wButtons & buttons[i].mask) ^ (device->last_keystroke.wButtons & buttons[i].mask)) + if ((cur->wButtons & buttons[i].mask) ^ (controller->last_keystroke.wButtons & buttons[i].mask)) { keystroke->VirtualKey = buttons[i].vk; keystroke->Unicode = 0; /* unused */ if (cur->wButtons & buttons[i].mask) { keystroke->Flags = XINPUT_KEYSTROKE_KEYDOWN; - device->last_keystroke.wButtons |= buttons[i].mask; + controller->last_keystroke.wButtons |= buttons[i].mask; } else { keystroke->Flags = XINPUT_KEYSTROKE_KEYUP; - device->last_keystroke.wButtons &= ~buttons[i].mask; + controller->last_keystroke.wButtons &= ~buttons[i].mask; } keystroke->UserIndex = index; keystroke->HidCode = 0; @@ -824,45 +824,45 @@ static DWORD check_for_keystroke(const DWORD index, XINPUT_KEYSTROKE *keystroke) }
/*** triggers ***/ - if (trigger_is_on(cur->bLeftTrigger) ^ trigger_is_on(device->last_keystroke.bLeftTrigger)) + if (trigger_is_on(cur->bLeftTrigger) ^ trigger_is_on(controller->last_keystroke.bLeftTrigger)) { keystroke->VirtualKey = VK_PAD_LTRIGGER; keystroke->Unicode = 0; /* unused */ keystroke->Flags = trigger_is_on(cur->bLeftTrigger) ? XINPUT_KEYSTROKE_KEYDOWN : XINPUT_KEYSTROKE_KEYUP; keystroke->UserIndex = index; keystroke->HidCode = 0; - device->last_keystroke.bLeftTrigger = cur->bLeftTrigger; + controller->last_keystroke.bLeftTrigger = cur->bLeftTrigger; ret = ERROR_SUCCESS; goto done; }
- if (trigger_is_on(cur->bRightTrigger) ^ trigger_is_on(device->last_keystroke.bRightTrigger)) + if (trigger_is_on(cur->bRightTrigger) ^ trigger_is_on(controller->last_keystroke.bRightTrigger)) { keystroke->VirtualKey = VK_PAD_RTRIGGER; keystroke->Unicode = 0; /* unused */ keystroke->Flags = trigger_is_on(cur->bRightTrigger) ? XINPUT_KEYSTROKE_KEYDOWN : XINPUT_KEYSTROKE_KEYUP; keystroke->UserIndex = index; keystroke->HidCode = 0; - device->last_keystroke.bRightTrigger = cur->bRightTrigger; + controller->last_keystroke.bRightTrigger = cur->bRightTrigger; ret = ERROR_SUCCESS; goto done; }
/*** joysticks ***/ ret = check_joystick_keystroke(index, keystroke, &cur->sThumbLX, &cur->sThumbLY, - &device->last_keystroke.sThumbLX, - &device->last_keystroke.sThumbLY, VK_PAD_LTHUMB_UP); + &controller->last_keystroke.sThumbLX, + &controller->last_keystroke.sThumbLY, VK_PAD_LTHUMB_UP); if (ret == ERROR_SUCCESS) goto done;
ret = check_joystick_keystroke(index, keystroke, &cur->sThumbRX, &cur->sThumbRY, - &device->last_keystroke.sThumbRX, - &device->last_keystroke.sThumbRY, VK_PAD_RTHUMB_UP); + &controller->last_keystroke.sThumbRX, + &controller->last_keystroke.sThumbRY, VK_PAD_RTHUMB_UP); if (ret == ERROR_SUCCESS) goto done;
done: - unlock_device(device); + unlock_device(controller);
return ret; }
And use device instead of platform_private to check connection.
Signed-off-by: Rémi Bernon rbernon@codeweavers.com --- dlls/xinput1_3/main.c | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-)
diff --git a/dlls/xinput1_3/main.c b/dlls/xinput1_3/main.c index 25f3b0f0407..3892f5a4e9b 100644 --- a/dlls/xinput1_3/main.c +++ b/dlls/xinput1_3/main.c @@ -56,6 +56,8 @@ struct xinput_controller XINPUT_STATE state; XINPUT_GAMEPAD last_keystroke; XINPUT_VIBRATION vibration; + HANDLE device; + WCHAR device_path[MAX_PATH]; BOOL enabled; };
@@ -114,9 +116,6 @@ struct hid_platform_private PHIDP_PREPARSED_DATA preparsed; HIDP_CAPS caps;
- HANDLE device; - WCHAR device_path[MAX_PATH]; - char *input_report_buf[2]; char *output_report_buf;
@@ -127,14 +126,13 @@ static DWORD last_check = 0;
static BOOL find_opened_device(SP_DEVICE_INTERFACE_DETAIL_DATA_W *detail, int *free_slot) { - struct hid_platform_private *private; int i;
*free_slot = XUSER_MAX_COUNT; for (i = XUSER_MAX_COUNT; i > 0; i--) { - if (!(private = controllers[i - 1].platform_private)) *free_slot = i - 1; - else if (!wcscmp(detail->DevicePath, private->device_path)) return TRUE; + if (!controllers[i - 1].device) *free_slot = i - 1; + else if (!wcscmp(detail->DevicePath, controllers[i - 1].device_path)) return TRUE; } return FALSE; } @@ -259,7 +257,7 @@ static DWORD HID_set_state(struct xinput_controller *controller, XINPUT_VIBRATIO output_report_buf[3] = (BYTE)(state->wLeftMotorSpeed / 256); output_report_buf[4] = (BYTE)(state->wRightMotorSpeed / 256);
- if (!HidD_SetOutputReport(private->device, output_report_buf, output_report_len)) + if (!HidD_SetOutputReport(controller->device, output_report_buf, output_report_len)) { WARN("unable to set output report, HidD_SetOutputReport failed with error %u\n", GetLastError()); return GetLastError(); @@ -302,18 +300,18 @@ static BOOL init_controller(struct xinput_controller *controller, PHIDP_PREPARSE TRACE("Found gamepad %s\n", debugstr_w(device_path));
private->preparsed = preparsed; - private->device = device; if (!(private->input_report_buf[0] = calloc(1, private->caps.InputReportByteLength))) goto failed; if (!(private->input_report_buf[1] = calloc(1, private->caps.InputReportByteLength))) goto failed; if (!(private->output_report_buf = calloc(1, private->caps.OutputReportByteLength))) goto failed; - lstrcpynW(private->device_path, device_path, MAX_PATH); + controller->platform_private = private;
memset(&controller->state, 0, sizeof(controller->state)); memset(&controller->vibration, 0, sizeof(controller->vibration)); + lstrcpynW(controller->device_path, device_path, MAX_PATH); controller->enabled = FALSE;
EnterCriticalSection(&controller->crit); - controller->platform_private = private; + controller->device = device; controller_enable(controller); LeaveCriticalSection(&controller->crit); return TRUE; @@ -399,14 +397,15 @@ static void remove_gamepad(struct xinput_controller *controller) { EnterCriticalSection(&controller->crit);
- if (controller->platform_private) + if (controller->device) { struct hid_platform_private *private = controller->platform_private;
controller_disable(controller); + CloseHandle(controller->device); + controller->device = NULL; controller->platform_private = NULL;
- CloseHandle(private->device); free(private->input_report_buf[0]); free(private->input_report_buf[1]); free(private->output_report_buf); @@ -447,7 +446,7 @@ static void HID_update_state(struct xinput_controller *controller, XINPUT_STATE
if (!controller->enabled) return;
- if (!HidD_GetInputReport(private->device, report_buf[0], report_len)) + if (!HidD_GetInputReport(controller->device, report_buf[0], report_len)) { if (GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == ERROR_INVALID_HANDLE) { @@ -555,11 +554,11 @@ static void HID_update_state(struct xinput_controller *controller, XINPUT_STATE
static BOOL verify_and_lock_device(struct xinput_controller *controller) { - if (!controller->platform_private) return FALSE; + if (!controller->device) return FALSE;
EnterCriticalSection(&controller->crit);
- if (!controller->platform_private) + if (!controller->device) { LeaveCriticalSection(&controller->crit); return FALSE; @@ -640,7 +639,7 @@ static DWORD xinput_get_state(DWORD index, XINPUT_STATE *state)
HID_update_state(&controllers[index], state);
- if (!controllers[index].platform_private) + if (!controllers[index].device) { /* update_state may have disconnected the controller */ unlock_device(&controllers[index]); @@ -913,7 +912,7 @@ DWORD WINAPI DECLSPEC_HOTPATCH XInputGetDSoundAudioDeviceGuids(DWORD index, GUID FIXME("(index %u, render guid %p, capture guid %p) Stub!\n", index, render_guid, capture_guid);
if (index >= XUSER_MAX_COUNT) return ERROR_BAD_ARGUMENTS; - if (!controllers[index].platform_private) return ERROR_DEVICE_NOT_CONNECTED; + if (!controllers[index].device) return ERROR_DEVICE_NOT_CONNECTED;
return ERROR_NOT_SUPPORTED; } @@ -925,7 +924,7 @@ DWORD WINAPI DECLSPEC_HOTPATCH XInputGetBatteryInformation(DWORD index, BYTE typ if (!once++) FIXME("(index %u, type %u, battery %p) Stub!\n", index, type, battery);
if (index >= XUSER_MAX_COUNT) return ERROR_BAD_ARGUMENTS; - if (!controllers[index].platform_private) return ERROR_DEVICE_NOT_CONNECTED; + if (!controllers[index].device) return ERROR_DEVICE_NOT_CONNECTED;
return ERROR_NOT_SUPPORTED; }
Instead of scaling to HID physical range, then to XInput range. Also keep and use axis value caps directly.
Signed-off-by: Rémi Bernon rbernon@codeweavers.com --- dlls/xinput1_3/main.c | 141 ++++++++++++++++++------------------------ 1 file changed, 61 insertions(+), 80 deletions(-)
diff --git a/dlls/xinput1_3/main.c b/dlls/xinput1_3/main.c index 3892f5a4e9b..338c818c350 100644 --- a/dlls/xinput1_3/main.c +++ b/dlls/xinput1_3/main.c @@ -104,22 +104,19 @@ static struct xinput_controller controllers[XUSER_MAX_COUNT] = {{ &controller_critsect_debug[3], -1, 0, 0, 0, 0 }}, };
-struct axis_info -{ - LONG min; - LONG range; - USHORT bits; -}; - struct hid_platform_private { PHIDP_PREPARSED_DATA preparsed; HIDP_CAPS caps; + HIDP_VALUE_CAPS lx_caps; + HIDP_VALUE_CAPS ly_caps; + HIDP_VALUE_CAPS lt_caps; + HIDP_VALUE_CAPS rx_caps; + HIDP_VALUE_CAPS ry_caps; + HIDP_VALUE_CAPS rt_caps;
char *input_report_buf[2]; char *output_report_buf; - - struct axis_info lx, ly, ltrigger, rx, ry, rtrigger; };
static DWORD last_check = 0; @@ -137,18 +134,16 @@ static BOOL find_opened_device(SP_DEVICE_INTERFACE_DETAIL_DATA_W *detail, int *f return FALSE; }
-static void MarkUsage(struct hid_platform_private *private, WORD usage, LONG min, LONG max, USHORT bits) +static void check_value_caps(struct hid_platform_private *private, USHORT usage, HIDP_VALUE_CAPS *caps) { - struct axis_info info = {min, max - min, bits}; - switch (usage) { - case HID_USAGE_GENERIC_X: private->lx = info; break; - case HID_USAGE_GENERIC_Y: private->ly = info; break; - case HID_USAGE_GENERIC_Z: private->ltrigger = info; break; - case HID_USAGE_GENERIC_RX: private->rx = info; break; - case HID_USAGE_GENERIC_RY: private->ry = info; break; - case HID_USAGE_GENERIC_RZ: private->rtrigger = info; break; + case HID_USAGE_GENERIC_X: private->lx_caps = *caps; break; + case HID_USAGE_GENERIC_Y: private->ly_caps = *caps; break; + case HID_USAGE_GENERIC_Z: private->lt_caps = *caps; break; + case HID_USAGE_GENERIC_RX: private->rx_caps = *caps; break; + case HID_USAGE_GENERIC_RY: private->ry_caps = *caps; break; + case HID_USAGE_GENERIC_RZ: private->rt_caps = *caps; break; } }
@@ -158,8 +153,7 @@ static BOOL VerifyGamepad(PHIDP_PREPARSED_DATA preparsed, XINPUT_CAPABILITIES *x HIDP_BUTTON_CAPS *button_caps; HIDP_VALUE_CAPS *value_caps; NTSTATUS status; - - int i; + int i, u; int button_count = 0;
/* Count buttons */ @@ -187,43 +181,25 @@ static BOOL VerifyGamepad(PHIDP_PREPARSED_DATA preparsed, XINPUT_CAPABILITIES *x if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetValueCaps returned %#x\n", status); else for (i = 0; i < private->caps.NumberInputValueCaps; i++) { - if (value_caps[i].UsagePage != HID_USAGE_PAGE_GENERIC) - continue; - if (value_caps[i].IsRange) - { - int u; - for (u = value_caps[i].Range.UsageMin; u <=value_caps[i].Range.UsageMax; u++) - MarkUsage(private, u, value_caps[i].LogicalMin, value_caps[i].LogicalMax, value_caps[i].BitSize); - } - else - MarkUsage(private, value_caps[i].NotRange.Usage, value_caps[i].LogicalMin, value_caps[i].LogicalMax, value_caps[i].BitSize); + HIDP_VALUE_CAPS *caps = value_caps + i; + if (caps->UsagePage != HID_USAGE_PAGE_GENERIC) continue; + if (!caps->IsRange) check_value_caps(private, caps->NotRange.Usage, caps); + else for (u = caps->Range.UsageMin; u <=caps->Range.UsageMax; u++) check_value_caps(private, u, value_caps + i); } free(value_caps);
- if (private->ltrigger.bits) - xinput_caps->Gamepad.bLeftTrigger = (1u << (sizeof(xinput_caps->Gamepad.bLeftTrigger) + 1)) - 1; - else - WARN("Missing axis LeftTrigger\n"); - if (private->rtrigger.bits) - xinput_caps->Gamepad.bRightTrigger = (1u << (sizeof(xinput_caps->Gamepad.bRightTrigger) + 1)) - 1; - else - WARN("Missing axis RightTrigger\n"); - if (private->lx.bits) - xinput_caps->Gamepad.sThumbLX = (1u << (sizeof(xinput_caps->Gamepad.sThumbLX) + 1)) - 1; - else - WARN("Missing axis ThumbLX\n"); - if (private->ly.bits) - xinput_caps->Gamepad.sThumbLY = (1u << (sizeof(xinput_caps->Gamepad.sThumbLY) + 1)) - 1; - else - WARN("Missing axis ThumbLY\n"); - if (private->rx.bits) - xinput_caps->Gamepad.sThumbRX = (1u << (sizeof(xinput_caps->Gamepad.sThumbRX) + 1)) - 1; - else - WARN("Missing axis ThumbRX\n"); - if (private->ry.bits) - xinput_caps->Gamepad.sThumbRY = (1u << (sizeof(xinput_caps->Gamepad.sThumbRY) + 1)) - 1; - else - WARN("Missing axis ThumbRY\n"); + if (!private->lt_caps.UsagePage) WARN("Missing axis LeftTrigger\n"); + else xinput_caps->Gamepad.bLeftTrigger = (1u << (sizeof(xinput_caps->Gamepad.bLeftTrigger) + 1)) - 1; + if (!private->rt_caps.UsagePage) WARN("Missing axis RightTrigger\n"); + else xinput_caps->Gamepad.bRightTrigger = (1u << (sizeof(xinput_caps->Gamepad.bRightTrigger) + 1)) - 1; + if (!private->lx_caps.UsagePage) WARN("Missing axis ThumbLX\n"); + else xinput_caps->Gamepad.sThumbLX = (1u << (sizeof(xinput_caps->Gamepad.sThumbLX) + 1)) - 1; + if (!private->ly_caps.UsagePage) WARN("Missing axis ThumbLY\n"); + else xinput_caps->Gamepad.sThumbLY = (1u << (sizeof(xinput_caps->Gamepad.sThumbLY) + 1)) - 1; + if (!private->rx_caps.UsagePage) WARN("Missing axis ThumbRX\n"); + else xinput_caps->Gamepad.sThumbRX = (1u << (sizeof(xinput_caps->Gamepad.sThumbRX) + 1)) - 1; + if (!private->ry_caps.UsagePage) WARN("Missing axis ThumbRY\n"); + else xinput_caps->Gamepad.sThumbRY = (1u << (sizeof(xinput_caps->Gamepad.sThumbRY) + 1)) - 1;
xinput_caps->Type = XINPUT_DEVTYPE_GAMEPAD; xinput_caps->SubType = XINPUT_DEVSUBTYPE_GAMEPAD; @@ -422,14 +398,19 @@ static void HID_destroy_gamepads(void) for (i = 0; i < XUSER_MAX_COUNT; i++) remove_gamepad(&controllers[i]); }
-static SHORT scale_short(LONG value, const struct axis_info *axis) +static LONG sign_extend(ULONG value, const HIDP_VALUE_CAPS *caps) { - return ((((ULONGLONG)(value - axis->min)) * 0xffff) / axis->range) - 32768; + UINT sign = 1 << (caps->BitSize - 1); + if (sign <= 1 || caps->LogicalMin >= 0) return value; + return value - ((value & sign) << 1); }
-static BYTE scale_byte(LONG value, const struct axis_info *axis) +static LONG scale_value(ULONG value, const HIDP_VALUE_CAPS *caps, LONG min, LONG max) { - return (((ULONGLONG)(value - axis->min)) * 0xff) / axis->range; + LONG tmp = sign_extend(value, caps); + if (caps->LogicalMin > caps->LogicalMax) return 0; + if (caps->LogicalMin > tmp || caps->LogicalMax < tmp) return 0; + return min + MulDiv(tmp - caps->LogicalMin, max - min, caps->LogicalMax - caps->LogicalMin); }
static void HID_update_state(struct xinput_controller *controller, XINPUT_STATE *state) @@ -441,8 +422,7 @@ static void HID_update_state(struct xinput_controller *controller, XINPUT_STATE NTSTATUS status;
USAGE buttons[11]; - ULONG button_length, hat_value; - LONG value; + ULONG button_length, value;
if (!controller->enabled) return;
@@ -484,11 +464,12 @@ static void HID_update_state(struct xinput_controller *controller, XINPUT_STATE } }
- status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_HATSWITCH, &hat_value, private->preparsed, report_buf[0], report_len); + status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_HATSWITCH, &value, private->preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_HATSWITCH returned %#x\n", status); else { - switch(hat_value){ + switch (value) + { /* 8 1 2 * 7 0 3 * 6 5 4 */ @@ -521,29 +502,29 @@ static void HID_update_state(struct xinput_controller *controller, XINPUT_STATE } }
- status = HidP_GetScaledUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_X, &value, private->preparsed, report_buf[0], report_len); - if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetScaledUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_X returned %#x\n", status); - else controller->state.Gamepad.sThumbLX = scale_short(value, &private->lx); + status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_X, &value, private->preparsed, report_buf[0], report_len); + if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_X returned %#x\n", status); + else controller->state.Gamepad.sThumbLX = scale_value(value, &private->lx_caps, -32768, 32767);
- status = HidP_GetScaledUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_Y, &value, private->preparsed, report_buf[0], report_len); - if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetScaledUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_Y returned %#x\n", status); - else controller->state.Gamepad.sThumbLY = -scale_short(value, &private->ly) - 1; + status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_Y, &value, private->preparsed, report_buf[0], report_len); + if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_Y returned %#x\n", status); + else controller->state.Gamepad.sThumbLY = -scale_value(value, &private->ly_caps, -32768, 32767) - 1;
- status = HidP_GetScaledUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RX, &value, private->preparsed, report_buf[0], report_len); - if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetScaledUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_RX returned %#x\n", status); - else controller->state.Gamepad.sThumbRX = scale_short(value, &private->rx); + status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RX, &value, private->preparsed, report_buf[0], report_len); + if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_RX returned %#x\n", status); + else controller->state.Gamepad.sThumbRX = scale_value(value, &private->rx_caps, -32768, 32767);
- status = HidP_GetScaledUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RY, &value, private->preparsed, report_buf[0], report_len); - if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetScaledUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_RY returned %#x\n", status); - else controller->state.Gamepad.sThumbRY = -scale_short(value, &private->ry) - 1; + status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RY, &value, private->preparsed, report_buf[0], report_len); + if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_RY returned %#x\n", status); + else controller->state.Gamepad.sThumbRY = -scale_value(value, &private->ry_caps, -32768, 32767) - 1;
- status = HidP_GetScaledUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RZ, &value, private->preparsed, report_buf[0], report_len); - if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetScaledUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_RZ returned %#x\n", status); - else controller->state.Gamepad.bRightTrigger = scale_byte(value, &private->rtrigger); + status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RZ, &value, private->preparsed, report_buf[0], report_len); + if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_RZ returned %#x\n", status); + else controller->state.Gamepad.bRightTrigger = scale_value(value, &private->rt_caps, 0, 255);
- status = HidP_GetScaledUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_Z, &value, private->preparsed, report_buf[0], report_len); - if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetScaledUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_Z returned %#x\n", status); - else controller->state.Gamepad.bLeftTrigger = scale_byte(value, &private->ltrigger); + status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_Z, &value, private->preparsed, report_buf[0], report_len); + if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_Z returned %#x\n", status); + else controller->state.Gamepad.bLeftTrigger = scale_value(value, &private->lt_caps, 0, 255); }
tmp = report_buf[0];
Signed-off-by: Rémi Bernon rbernon@codeweavers.com --- dlls/xinput1_3/main.c | 184 ++++++++++++++++++++---------------------- 1 file changed, 87 insertions(+), 97 deletions(-)
diff --git a/dlls/xinput1_3/main.c b/dlls/xinput1_3/main.c index 338c818c350..1cc554a8aa7 100644 --- a/dlls/xinput1_3/main.c +++ b/dlls/xinput1_3/main.c @@ -52,13 +52,27 @@ struct xinput_controller { CRITICAL_SECTION crit; XINPUT_CAPABILITIES caps; - void *platform_private; /* non-NULL when device is valid; validity may be read without holding crit */ XINPUT_STATE state; XINPUT_GAMEPAD last_keystroke; XINPUT_VIBRATION vibration; HANDLE device; WCHAR device_path[MAX_PATH]; BOOL enabled; + + struct + { + PHIDP_PREPARSED_DATA preparsed; + HIDP_CAPS caps; + HIDP_VALUE_CAPS lx_caps; + HIDP_VALUE_CAPS ly_caps; + HIDP_VALUE_CAPS lt_caps; + HIDP_VALUE_CAPS rx_caps; + HIDP_VALUE_CAPS ry_caps; + HIDP_VALUE_CAPS rt_caps; + + char *input_report_buf[2]; + char *output_report_buf; + } hid; };
/* xinput_crit guards controllers array */ @@ -104,21 +118,6 @@ static struct xinput_controller controllers[XUSER_MAX_COUNT] = {{ &controller_critsect_debug[3], -1, 0, 0, 0, 0 }}, };
-struct hid_platform_private -{ - PHIDP_PREPARSED_DATA preparsed; - HIDP_CAPS caps; - HIDP_VALUE_CAPS lx_caps; - HIDP_VALUE_CAPS ly_caps; - HIDP_VALUE_CAPS lt_caps; - HIDP_VALUE_CAPS rx_caps; - HIDP_VALUE_CAPS ry_caps; - HIDP_VALUE_CAPS rt_caps; - - char *input_report_buf[2]; - char *output_report_buf; -}; - static DWORD last_check = 0;
static BOOL find_opened_device(SP_DEVICE_INTERFACE_DETAIL_DATA_W *detail, int *free_slot) @@ -134,22 +133,22 @@ static BOOL find_opened_device(SP_DEVICE_INTERFACE_DETAIL_DATA_W *detail, int *f return FALSE; }
-static void check_value_caps(struct hid_platform_private *private, USHORT usage, HIDP_VALUE_CAPS *caps) +static void check_value_caps(struct xinput_controller *controller, USHORT usage, HIDP_VALUE_CAPS *caps) { switch (usage) { - case HID_USAGE_GENERIC_X: private->lx_caps = *caps; break; - case HID_USAGE_GENERIC_Y: private->ly_caps = *caps; break; - case HID_USAGE_GENERIC_Z: private->lt_caps = *caps; break; - case HID_USAGE_GENERIC_RX: private->rx_caps = *caps; break; - case HID_USAGE_GENERIC_RY: private->ry_caps = *caps; break; - case HID_USAGE_GENERIC_RZ: private->rt_caps = *caps; break; + case HID_USAGE_GENERIC_X: controller->hid.lx_caps = *caps; break; + case HID_USAGE_GENERIC_Y: controller->hid.ly_caps = *caps; break; + case HID_USAGE_GENERIC_Z: controller->hid.lt_caps = *caps; break; + case HID_USAGE_GENERIC_RX: controller->hid.rx_caps = *caps; break; + case HID_USAGE_GENERIC_RY: controller->hid.ry_caps = *caps; break; + case HID_USAGE_GENERIC_RZ: controller->hid.rt_caps = *caps; break; } }
-static BOOL VerifyGamepad(PHIDP_PREPARSED_DATA preparsed, XINPUT_CAPABILITIES *xinput_caps, - struct hid_platform_private *private) +static BOOL controller_check_caps(struct xinput_controller *controller, PHIDP_PREPARSED_DATA preparsed) { + XINPUT_CAPABILITIES *caps = &controller->caps; HIDP_BUTTON_CAPS *button_caps; HIDP_VALUE_CAPS *value_caps; NTSTATUS status; @@ -157,12 +156,12 @@ static BOOL VerifyGamepad(PHIDP_PREPARSED_DATA preparsed, XINPUT_CAPABILITIES *x int button_count = 0;
/* Count buttons */ - memset(xinput_caps, 0, sizeof(XINPUT_CAPABILITIES)); + memset(caps, 0, sizeof(XINPUT_CAPABILITIES));
- if (!(button_caps = malloc(sizeof(*button_caps) * private->caps.NumberInputButtonCaps))) return FALSE; - status = HidP_GetButtonCaps(HidP_Input, button_caps, &private->caps.NumberInputButtonCaps, preparsed); + if (!(button_caps = malloc(sizeof(*button_caps) * controller->hid.caps.NumberInputButtonCaps))) return FALSE; + status = HidP_GetButtonCaps(HidP_Input, button_caps, &controller->hid.caps.NumberInputButtonCaps, preparsed); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetButtonCaps returned %#x\n", status); - else for (i = 0; i < private->caps.NumberInputButtonCaps; i++) + else for (i = 0; i < controller->hid.caps.NumberInputButtonCaps; i++) { if (button_caps[i].UsagePage != HID_USAGE_PAGE_BUTTON) continue; @@ -174,41 +173,41 @@ static BOOL VerifyGamepad(PHIDP_PREPARSED_DATA preparsed, XINPUT_CAPABILITIES *x free(button_caps); if (button_count < 11) WARN("Too few buttons, continuing anyway\n"); - xinput_caps->Gamepad.wButtons = 0xffff; + caps->Gamepad.wButtons = 0xffff;
- if (!(value_caps = malloc(sizeof(*value_caps) * private->caps.NumberInputValueCaps))) return FALSE; - status = HidP_GetValueCaps(HidP_Input, value_caps, &private->caps.NumberInputValueCaps, preparsed); + if (!(value_caps = malloc(sizeof(*value_caps) * controller->hid.caps.NumberInputValueCaps))) return FALSE; + status = HidP_GetValueCaps(HidP_Input, value_caps, &controller->hid.caps.NumberInputValueCaps, preparsed); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetValueCaps returned %#x\n", status); - else for (i = 0; i < private->caps.NumberInputValueCaps; i++) + else for (i = 0; i < controller->hid.caps.NumberInputValueCaps; i++) { HIDP_VALUE_CAPS *caps = value_caps + i; if (caps->UsagePage != HID_USAGE_PAGE_GENERIC) continue; - if (!caps->IsRange) check_value_caps(private, caps->NotRange.Usage, caps); - else for (u = caps->Range.UsageMin; u <=caps->Range.UsageMax; u++) check_value_caps(private, u, value_caps + i); + if (!caps->IsRange) check_value_caps(controller, caps->NotRange.Usage, caps); + else for (u = caps->Range.UsageMin; u <=caps->Range.UsageMax; u++) check_value_caps(controller, u, value_caps + i); } free(value_caps);
- if (!private->lt_caps.UsagePage) WARN("Missing axis LeftTrigger\n"); - else xinput_caps->Gamepad.bLeftTrigger = (1u << (sizeof(xinput_caps->Gamepad.bLeftTrigger) + 1)) - 1; - if (!private->rt_caps.UsagePage) WARN("Missing axis RightTrigger\n"); - else xinput_caps->Gamepad.bRightTrigger = (1u << (sizeof(xinput_caps->Gamepad.bRightTrigger) + 1)) - 1; - if (!private->lx_caps.UsagePage) WARN("Missing axis ThumbLX\n"); - else xinput_caps->Gamepad.sThumbLX = (1u << (sizeof(xinput_caps->Gamepad.sThumbLX) + 1)) - 1; - if (!private->ly_caps.UsagePage) WARN("Missing axis ThumbLY\n"); - else xinput_caps->Gamepad.sThumbLY = (1u << (sizeof(xinput_caps->Gamepad.sThumbLY) + 1)) - 1; - if (!private->rx_caps.UsagePage) WARN("Missing axis ThumbRX\n"); - else xinput_caps->Gamepad.sThumbRX = (1u << (sizeof(xinput_caps->Gamepad.sThumbRX) + 1)) - 1; - if (!private->ry_caps.UsagePage) WARN("Missing axis ThumbRY\n"); - else xinput_caps->Gamepad.sThumbRY = (1u << (sizeof(xinput_caps->Gamepad.sThumbRY) + 1)) - 1; - - xinput_caps->Type = XINPUT_DEVTYPE_GAMEPAD; - xinput_caps->SubType = XINPUT_DEVSUBTYPE_GAMEPAD; - - if (private->caps.NumberOutputValueCaps > 0) + if (!controller->hid.lt_caps.UsagePage) WARN("Missing axis LeftTrigger\n"); + else caps->Gamepad.bLeftTrigger = (1u << (sizeof(caps->Gamepad.bLeftTrigger) + 1)) - 1; + if (!controller->hid.rt_caps.UsagePage) WARN("Missing axis RightTrigger\n"); + else caps->Gamepad.bRightTrigger = (1u << (sizeof(caps->Gamepad.bRightTrigger) + 1)) - 1; + if (!controller->hid.lx_caps.UsagePage) WARN("Missing axis ThumbLX\n"); + else caps->Gamepad.sThumbLX = (1u << (sizeof(caps->Gamepad.sThumbLX) + 1)) - 1; + if (!controller->hid.ly_caps.UsagePage) WARN("Missing axis ThumbLY\n"); + else caps->Gamepad.sThumbLY = (1u << (sizeof(caps->Gamepad.sThumbLY) + 1)) - 1; + if (!controller->hid.rx_caps.UsagePage) WARN("Missing axis ThumbRX\n"); + else caps->Gamepad.sThumbRX = (1u << (sizeof(caps->Gamepad.sThumbRX) + 1)) - 1; + if (!controller->hid.ry_caps.UsagePage) WARN("Missing axis ThumbRY\n"); + else caps->Gamepad.sThumbRY = (1u << (sizeof(caps->Gamepad.sThumbRY) + 1)) - 1; + + caps->Type = XINPUT_DEVTYPE_GAMEPAD; + caps->SubType = XINPUT_DEVSUBTYPE_GAMEPAD; + + if (controller->hid.caps.NumberOutputValueCaps > 0) { - xinput_caps->Flags |= XINPUT_CAPS_FFB_SUPPORTED; - xinput_caps->Vibration.wLeftMotorSpeed = 255; - xinput_caps->Vibration.wRightMotorSpeed = 255; + caps->Flags |= XINPUT_CAPS_FFB_SUPPORTED; + caps->Vibration.wLeftMotorSpeed = 255; + caps->Vibration.wRightMotorSpeed = 255; }
return TRUE; @@ -216,9 +215,8 @@ static BOOL VerifyGamepad(PHIDP_PREPARSED_DATA preparsed, XINPUT_CAPABILITIES *x
static DWORD HID_set_state(struct xinput_controller *controller, XINPUT_VIBRATION *state) { - struct hid_platform_private *private = controller->platform_private; - char *output_report_buf = private->output_report_buf; - ULONG output_report_len = private->caps.OutputReportByteLength; + char *output_report_buf = controller->hid.output_report_buf; + ULONG output_report_len = controller->hid.caps.OutputReportByteLength;
if (controller->caps.Flags & XINPUT_CAPS_FFB_SUPPORTED) { @@ -267,19 +265,15 @@ static void controller_disable(struct xinput_controller *controller) static BOOL init_controller(struct xinput_controller *controller, PHIDP_PREPARSED_DATA preparsed, HIDP_CAPS *caps, HANDLE device, WCHAR *device_path) { - struct hid_platform_private *private; - - if (!(private = calloc(1, sizeof(struct hid_platform_private)))) return FALSE; - private->caps = *caps; - if (!VerifyGamepad(preparsed, &controller->caps, private)) goto failed; + controller->hid.caps = *caps; + if (!controller_check_caps(controller, preparsed)) goto failed;
TRACE("Found gamepad %s\n", debugstr_w(device_path));
- private->preparsed = preparsed; - if (!(private->input_report_buf[0] = calloc(1, private->caps.InputReportByteLength))) goto failed; - if (!(private->input_report_buf[1] = calloc(1, private->caps.InputReportByteLength))) goto failed; - if (!(private->output_report_buf = calloc(1, private->caps.OutputReportByteLength))) goto failed; - controller->platform_private = private; + controller->hid.preparsed = preparsed; + if (!(controller->hid.input_report_buf[0] = calloc(1, controller->hid.caps.InputReportByteLength))) goto failed; + if (!(controller->hid.input_report_buf[1] = calloc(1, controller->hid.caps.InputReportByteLength))) goto failed; + if (!(controller->hid.output_report_buf = calloc(1, controller->hid.caps.OutputReportByteLength))) goto failed;
memset(&controller->state, 0, sizeof(controller->state)); memset(&controller->vibration, 0, sizeof(controller->vibration)); @@ -293,10 +287,10 @@ static BOOL init_controller(struct xinput_controller *controller, PHIDP_PREPARSE return TRUE;
failed: - free(private->input_report_buf[0]); - free(private->input_report_buf[1]); - free(private->output_report_buf); - free(private); + free(controller->hid.input_report_buf[0]); + free(controller->hid.input_report_buf[1]); + free(controller->hid.output_report_buf); + memset(&controller->hid, 0, sizeof(controller->hid)); return FALSE; }
@@ -375,18 +369,15 @@ static void remove_gamepad(struct xinput_controller *controller)
if (controller->device) { - struct hid_platform_private *private = controller->platform_private; - controller_disable(controller); CloseHandle(controller->device); controller->device = NULL; - controller->platform_private = NULL;
- free(private->input_report_buf[0]); - free(private->input_report_buf[1]); - free(private->output_report_buf); - HidD_FreePreparsedData(private->preparsed); - free(private); + free(controller->hid.input_report_buf[0]); + free(controller->hid.input_report_buf[1]); + free(controller->hid.output_report_buf); + HidD_FreePreparsedData(controller->hid.preparsed); + memset(&controller->hid, 0, sizeof(controller->hid)); }
LeaveCriticalSection(&controller->crit); @@ -415,10 +406,9 @@ static LONG scale_value(ULONG value, const HIDP_VALUE_CAPS *caps, LONG min, LONG
static void HID_update_state(struct xinput_controller *controller, XINPUT_STATE *state) { - struct hid_platform_private *private = controller->platform_private; int i; - char **report_buf = private->input_report_buf, *tmp; - ULONG report_len = private->caps.InputReportByteLength; + char **report_buf = controller->hid.input_report_buf, *tmp; + ULONG report_len = controller->hid.caps.InputReportByteLength; NTSTATUS status;
USAGE buttons[11]; @@ -442,7 +432,7 @@ static void HID_update_state(struct xinput_controller *controller, XINPUT_STATE { controller->state.dwPacketNumber++; button_length = ARRAY_SIZE(buttons); - status = HidP_GetUsages(HidP_Input, HID_USAGE_PAGE_BUTTON, 0, buttons, &button_length, private->preparsed, report_buf[0], report_len); + status = HidP_GetUsages(HidP_Input, HID_USAGE_PAGE_BUTTON, 0, buttons, &button_length, controller->hid.preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsages HID_USAGE_PAGE_BUTTON returned %#x\n", status);
controller->state.Gamepad.wButtons = 0; @@ -464,7 +454,7 @@ static void HID_update_state(struct xinput_controller *controller, XINPUT_STATE } }
- status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_HATSWITCH, &value, private->preparsed, report_buf[0], report_len); + status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_HATSWITCH, &value, controller->hid.preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_HATSWITCH returned %#x\n", status); else { @@ -502,29 +492,29 @@ static void HID_update_state(struct xinput_controller *controller, XINPUT_STATE } }
- status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_X, &value, private->preparsed, report_buf[0], report_len); + status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_X, &value, controller->hid.preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_X returned %#x\n", status); - else controller->state.Gamepad.sThumbLX = scale_value(value, &private->lx_caps, -32768, 32767); + else controller->state.Gamepad.sThumbLX = scale_value(value, &controller->hid.lx_caps, -32768, 32767);
- status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_Y, &value, private->preparsed, report_buf[0], report_len); + status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_Y, &value, controller->hid.preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_Y returned %#x\n", status); - else controller->state.Gamepad.sThumbLY = -scale_value(value, &private->ly_caps, -32768, 32767) - 1; + else controller->state.Gamepad.sThumbLY = -scale_value(value, &controller->hid.ly_caps, -32768, 32767) - 1;
- status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RX, &value, private->preparsed, report_buf[0], report_len); + status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RX, &value, controller->hid.preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_RX returned %#x\n", status); - else controller->state.Gamepad.sThumbRX = scale_value(value, &private->rx_caps, -32768, 32767); + else controller->state.Gamepad.sThumbRX = scale_value(value, &controller->hid.rx_caps, -32768, 32767);
- status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RY, &value, private->preparsed, report_buf[0], report_len); + status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RY, &value, controller->hid.preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_RY returned %#x\n", status); - else controller->state.Gamepad.sThumbRY = -scale_value(value, &private->ry_caps, -32768, 32767) - 1; + else controller->state.Gamepad.sThumbRY = -scale_value(value, &controller->hid.ry_caps, -32768, 32767) - 1;
- status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RZ, &value, private->preparsed, report_buf[0], report_len); + status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RZ, &value, controller->hid.preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_RZ returned %#x\n", status); - else controller->state.Gamepad.bRightTrigger = scale_value(value, &private->rt_caps, 0, 255); + else controller->state.Gamepad.bRightTrigger = scale_value(value, &controller->hid.rt_caps, 0, 255);
- status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_Z, &value, private->preparsed, report_buf[0], report_len); + status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_Z, &value, controller->hid.preparsed, report_buf[0], report_len); if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_Z returned %#x\n", status); - else controller->state.Gamepad.bLeftTrigger = scale_value(value, &private->lt_caps, 0, 255); + else controller->state.Gamepad.bLeftTrigger = scale_value(value, &controller->hid.lt_caps, 0, 255); }
tmp = report_buf[0];
Signed-off-by: Rémi Bernon rbernon@codeweavers.com --- dlls/xinput1_3/main.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)
diff --git a/dlls/xinput1_3/main.c b/dlls/xinput1_3/main.c index 1cc554a8aa7..ae2670ba306 100644 --- a/dlls/xinput1_3/main.c +++ b/dlls/xinput1_3/main.c @@ -262,7 +262,7 @@ static void controller_disable(struct xinput_controller *controller) controller->enabled = FALSE; }
-static BOOL init_controller(struct xinput_controller *controller, PHIDP_PREPARSED_DATA preparsed, +static BOOL controller_init(struct xinput_controller *controller, PHIDP_PREPARSED_DATA preparsed, HIDP_CAPS *caps, HANDLE device, WCHAR *device_path) { controller->hid.caps = *caps; @@ -350,7 +350,7 @@ static void HID_find_gamepads(void) else if (caps.Usage != HID_USAGE_GENERIC_GAMEPAD && caps.Usage != HID_USAGE_GENERIC_JOYSTICK && caps.Usage != HID_USAGE_GENERIC_MULTI_AXIS_CONTROLLER) WARN("ignoring HID device, unsupported usage %04x:%04x\n", caps.UsagePage, caps.Usage); - else if (!init_controller(&controllers[i], preparsed, &caps, device, detail->DevicePath)) + else if (!controller_init(&controllers[i], preparsed, &caps, device, detail->DevicePath)) WARN("ignoring HID device, failed to initialize\n"); else continue; @@ -363,7 +363,7 @@ static void HID_find_gamepads(void) LeaveCriticalSection(&xinput_crit); }
-static void remove_gamepad(struct xinput_controller *controller) +static void controller_destroy(struct xinput_controller *controller) { EnterCriticalSection(&controller->crit);
@@ -386,7 +386,7 @@ static void remove_gamepad(struct xinput_controller *controller) static void HID_destroy_gamepads(void) { int i; - for (i = 0; i < XUSER_MAX_COUNT; i++) remove_gamepad(&controllers[i]); + for (i = 0; i < XUSER_MAX_COUNT; i++) controller_destroy(&controllers[i]); }
static LONG sign_extend(ULONG value, const HIDP_VALUE_CAPS *caps) @@ -421,7 +421,7 @@ static void HID_update_state(struct xinput_controller *controller, XINPUT_STATE if (GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == ERROR_INVALID_HANDLE) { EnterCriticalSection(&xinput_crit); - remove_gamepad(controller); + controller_destroy(controller); LeaveCriticalSection(&xinput_crit); } else ERR("Failed to get input report, HidD_GetInputReport failed with error %u\n", GetLastError()); @@ -523,7 +523,7 @@ static void HID_update_state(struct xinput_controller *controller, XINPUT_STATE memcpy(state, &controller->state, sizeof(*state)); }
-static BOOL verify_and_lock_device(struct xinput_controller *controller) +static BOOL controller_lock(struct xinput_controller *controller) { if (!controller->device) return FALSE;
@@ -538,7 +538,7 @@ static BOOL verify_and_lock_device(struct xinput_controller *controller) return TRUE; }
-static void unlock_device(struct xinput_controller *controller) +static void controller_unlock(struct xinput_controller *controller) { LeaveCriticalSection(&controller->crit); } @@ -572,10 +572,10 @@ void WINAPI DECLSPEC_HOTPATCH XInputEnable(BOOL enable)
for (index = 0; index < XUSER_MAX_COUNT; index++) { - if (!verify_and_lock_device(&controllers[index])) continue; + if (!controller_lock(&controllers[index])) continue; if (enable) controller_enable(&controllers[index]); else controller_disable(&controllers[index]); - unlock_device(&controllers[index]); + controller_unlock(&controllers[index]); } }
@@ -588,11 +588,11 @@ DWORD WINAPI DECLSPEC_HOTPATCH XInputSetState(DWORD index, XINPUT_VIBRATION *vib HID_find_gamepads();
if (index >= XUSER_MAX_COUNT) return ERROR_BAD_ARGUMENTS; - if (!verify_and_lock_device(&controllers[index])) return ERROR_DEVICE_NOT_CONNECTED; + if (!controller_lock(&controllers[index])) return ERROR_DEVICE_NOT_CONNECTED;
ret = HID_set_state(&controllers[index], vibration);
- unlock_device(&controllers[index]); + controller_unlock(&controllers[index]);
return ret; } @@ -606,18 +606,18 @@ static DWORD xinput_get_state(DWORD index, XINPUT_STATE *state) HID_find_gamepads();
if (index >= XUSER_MAX_COUNT) return ERROR_BAD_ARGUMENTS; - if (!verify_and_lock_device(&controllers[index])) return ERROR_DEVICE_NOT_CONNECTED; + if (!controller_lock(&controllers[index])) return ERROR_DEVICE_NOT_CONNECTED;
HID_update_state(&controllers[index], state);
if (!controllers[index].device) { /* update_state may have disconnected the controller */ - unlock_device(&controllers[index]); + controller_unlock(&controllers[index]); return ERROR_DEVICE_NOT_CONNECTED; }
- unlock_device(&controllers[index]); + controller_unlock(&controllers[index]);
return ERROR_SUCCESS; } @@ -765,7 +765,7 @@ static DWORD check_for_keystroke(const DWORD index, XINPUT_KEYSTROKE *keystroke) /* note: guide button does not send an event */ };
- if (!verify_and_lock_device(controller)) return ERROR_DEVICE_NOT_CONNECTED; + if (!controller_lock(controller)) return ERROR_DEVICE_NOT_CONNECTED;
cur = &controller->state.Gamepad;
@@ -832,7 +832,7 @@ static DWORD check_for_keystroke(const DWORD index, XINPUT_KEYSTROKE *keystroke) goto done;
done: - unlock_device(controller); + controller_unlock(controller);
return ret; } @@ -863,17 +863,17 @@ DWORD WINAPI DECLSPEC_HOTPATCH XInputGetCapabilities(DWORD index, DWORD flags, X
if (index >= XUSER_MAX_COUNT) return ERROR_BAD_ARGUMENTS;
- if (!verify_and_lock_device(&controllers[index])) return ERROR_DEVICE_NOT_CONNECTED; + if (!controller_lock(&controllers[index])) return ERROR_DEVICE_NOT_CONNECTED;
if (flags & XINPUT_FLAG_GAMEPAD && controllers[index].caps.SubType != XINPUT_DEVSUBTYPE_GAMEPAD) { - unlock_device(&controllers[index]); + controller_unlock(&controllers[index]); return ERROR_DEVICE_NOT_CONNECTED; }
memcpy(capabilities, &controllers[index].caps, sizeof(*capabilities));
- unlock_device(&controllers[index]); + controller_unlock(&controllers[index]);
return ERROR_SUCCESS; }