2021-08-06 17:08:55 +02:00
|
|
|
/*
|
|
|
|
* The Wine project - Xinput Joystick Library
|
|
|
|
* Copyright 2008 Andrew Fenn
|
|
|
|
* Copyright 2018 Aric Stewart
|
|
|
|
* Copyright 2021 Rémi Bernon for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winerror.h"
|
|
|
|
#include "winuser.h"
|
|
|
|
#include "winreg.h"
|
|
|
|
#include "wingdi.h"
|
|
|
|
#include "winnls.h"
|
|
|
|
#include "winternl.h"
|
|
|
|
|
2021-10-08 13:02:26 +02:00
|
|
|
#include "dbt.h"
|
2021-08-06 17:08:55 +02:00
|
|
|
#include "setupapi.h"
|
|
|
|
#include "devpkey.h"
|
|
|
|
#include "hidusage.h"
|
|
|
|
#include "ddk/hidsdi.h"
|
|
|
|
#include "initguid.h"
|
|
|
|
#include "devguid.h"
|
|
|
|
#include "xinput.h"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
2021-09-03 07:57:39 +02:00
|
|
|
DEFINE_GUID(GUID_DEVINTERFACE_WINEXINPUT,0x6c53d5fd,0x6480,0x440f,0xb6,0x18,0x47,0x67,0x50,0xc5,0xe1,0xa6);
|
|
|
|
|
2021-08-06 17:08:55 +02:00
|
|
|
/* Not defined in the headers, used only by XInputGetStateEx */
|
|
|
|
#define XINPUT_GAMEPAD_GUIDE 0x0400
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(xinput);
|
|
|
|
|
2021-08-06 17:08:56 +02:00
|
|
|
struct xinput_controller
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
|
|
|
CRITICAL_SECTION crit;
|
|
|
|
XINPUT_CAPABILITIES caps;
|
|
|
|
XINPUT_STATE state;
|
|
|
|
XINPUT_GAMEPAD last_keystroke;
|
|
|
|
XINPUT_VIBRATION vibration;
|
2021-08-09 11:25:33 +02:00
|
|
|
HANDLE device;
|
|
|
|
WCHAR device_path[MAX_PATH];
|
2021-08-06 17:08:59 +02:00
|
|
|
BOOL enabled;
|
2021-08-09 11:25:35 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
HANDLE read_event;
|
|
|
|
OVERLAPPED read_ovl;
|
|
|
|
|
|
|
|
char *input_report_buf;
|
2021-08-09 11:25:35 +02:00
|
|
|
char *output_report_buf;
|
2021-09-24 11:51:55 +02:00
|
|
|
char *feature_report_buf;
|
|
|
|
|
|
|
|
BYTE haptics_report;
|
2022-02-15 09:03:37 +01:00
|
|
|
BYTE haptics_none_ordinal;
|
|
|
|
BYTE haptics_stop_ordinal;
|
2022-02-14 17:26:20 +01:00
|
|
|
BYTE haptics_rumble_ordinal;
|
|
|
|
BYTE haptics_buzz_ordinal;
|
2021-08-09 11:25:35 +02:00
|
|
|
} hid;
|
2021-08-06 17:08:56 +02:00
|
|
|
};
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-06 17:08:56 +02:00
|
|
|
static struct xinput_controller controllers[XUSER_MAX_COUNT];
|
2021-08-06 17:08:55 +02:00
|
|
|
static CRITICAL_SECTION_DEBUG controller_critsect_debug[XUSER_MAX_COUNT] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
0, 0, &controllers[0].crit,
|
|
|
|
{ &controller_critsect_debug[0].ProcessLocksList, &controller_critsect_debug[0].ProcessLocksList },
|
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": controllers[0].crit") }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0, 0, &controllers[1].crit,
|
|
|
|
{ &controller_critsect_debug[1].ProcessLocksList, &controller_critsect_debug[1].ProcessLocksList },
|
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": controllers[1].crit") }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0, 0, &controllers[2].crit,
|
|
|
|
{ &controller_critsect_debug[2].ProcessLocksList, &controller_critsect_debug[2].ProcessLocksList },
|
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": controllers[2].crit") }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0, 0, &controllers[3].crit,
|
|
|
|
{ &controller_critsect_debug[3].ProcessLocksList, &controller_critsect_debug[3].ProcessLocksList },
|
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": controllers[3].crit") }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-08-06 17:08:56 +02:00
|
|
|
static struct xinput_controller controllers[XUSER_MAX_COUNT] =
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
|
|
|
{{ &controller_critsect_debug[0], -1, 0, 0, 0, 0 }},
|
|
|
|
{{ &controller_critsect_debug[1], -1, 0, 0, 0, 0 }},
|
|
|
|
{{ &controller_critsect_debug[2], -1, 0, 0, 0, 0 }},
|
|
|
|
{{ &controller_critsect_debug[3], -1, 0, 0, 0, 0 }},
|
|
|
|
};
|
|
|
|
|
2021-10-08 13:02:26 +02:00
|
|
|
static HMODULE xinput_instance;
|
2021-10-08 13:02:25 +02:00
|
|
|
static HANDLE start_event;
|
2021-08-10 13:27:22 +02:00
|
|
|
static HANDLE stop_event;
|
|
|
|
static HANDLE done_event;
|
2021-08-10 13:27:24 +02:00
|
|
|
static HANDLE update_event;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2022-02-15 09:07:25 +01:00
|
|
|
static BOOL find_opened_device(const WCHAR *device_path, int *free_slot)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
*free_slot = XUSER_MAX_COUNT;
|
|
|
|
for (i = XUSER_MAX_COUNT; i > 0; i--)
|
|
|
|
{
|
2021-08-09 11:25:33 +02:00
|
|
|
if (!controllers[i - 1].device) *free_slot = i - 1;
|
2022-02-15 09:07:25 +01:00
|
|
|
else if (!wcsicmp(device_path, controllers[i - 1].device_path)) return TRUE;
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-08-09 11:25:35 +02:00
|
|
|
static void check_value_caps(struct xinput_controller *controller, USHORT usage, HIDP_VALUE_CAPS *caps)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
|
|
|
switch (usage)
|
|
|
|
{
|
2021-08-09 11:25:35 +02:00
|
|
|
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;
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 11:51:55 +02:00
|
|
|
static BOOL controller_check_caps(struct xinput_controller *controller, HANDLE device, PHIDP_PREPARSED_DATA preparsed)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
2021-09-24 11:51:55 +02:00
|
|
|
ULONG collections_count = 0, report_len = controller->hid.caps.FeatureReportByteLength;
|
|
|
|
char *report_buf = controller->hid.feature_report_buf;
|
2021-08-09 11:25:35 +02:00
|
|
|
XINPUT_CAPABILITIES *caps = &controller->caps;
|
2021-09-24 11:51:55 +02:00
|
|
|
HIDP_VALUE_CAPS *value_caps, waveform_cap;
|
|
|
|
int i, u, waveform_list, button_count = 0;
|
|
|
|
HIDP_LINK_COLLECTION_NODE *collections;
|
2021-08-06 17:08:55 +02:00
|
|
|
HIDP_BUTTON_CAPS *button_caps;
|
2021-09-24 11:51:55 +02:00
|
|
|
USHORT caps_count = 0;
|
2021-08-06 17:08:55 +02:00
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
/* Count buttons */
|
2021-08-09 11:25:35 +02:00
|
|
|
memset(caps, 0, sizeof(XINPUT_CAPABILITIES));
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-09 11:25:35 +02:00
|
|
|
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);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetButtonCaps returned %#lx\n", status);
|
2021-08-09 11:25:35 +02:00
|
|
|
else for (i = 0; i < controller->hid.caps.NumberInputButtonCaps; i++)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
|
|
|
if (button_caps[i].UsagePage != HID_USAGE_PAGE_BUTTON)
|
|
|
|
continue;
|
|
|
|
if (button_caps[i].IsRange)
|
|
|
|
button_count = max(button_count, button_caps[i].Range.UsageMax);
|
|
|
|
else
|
|
|
|
button_count = max(button_count, button_caps[i].NotRange.Usage);
|
|
|
|
}
|
|
|
|
free(button_caps);
|
|
|
|
if (button_count < 11)
|
|
|
|
WARN("Too few buttons, continuing anyway\n");
|
2021-08-09 11:25:35 +02:00
|
|
|
caps->Gamepad.wButtons = 0xffff;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-09 11:25:35 +02:00
|
|
|
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);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetValueCaps returned %#lx\n", status);
|
2021-08-09 11:25:35 +02:00
|
|
|
else for (i = 0; i < controller->hid.caps.NumberInputValueCaps; i++)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
2021-08-09 11:25:34 +02:00
|
|
|
HIDP_VALUE_CAPS *caps = value_caps + i;
|
|
|
|
if (caps->UsagePage != HID_USAGE_PAGE_GENERIC) continue;
|
2021-08-09 11:25:35 +02:00
|
|
|
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);
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
|
|
|
free(value_caps);
|
|
|
|
|
2021-08-09 11:25:35 +02:00
|
|
|
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;
|
|
|
|
|
2021-09-24 11:51:55 +02:00
|
|
|
collections_count = controller->hid.caps.NumberLinkCollectionNodes;
|
|
|
|
if (!(collections = malloc(sizeof(*collections) * controller->hid.caps.NumberLinkCollectionNodes))) return FALSE;
|
|
|
|
status = HidP_GetLinkCollectionNodes(collections, &collections_count, preparsed);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetLinkCollectionNodes returned %#lx\n", status);
|
2021-09-24 11:51:55 +02:00
|
|
|
else for (i = 0; i < collections_count; ++i)
|
|
|
|
{
|
|
|
|
if (collections[i].LinkUsagePage != HID_USAGE_PAGE_HAPTICS) continue;
|
|
|
|
if (collections[i].LinkUsage == HID_USAGE_HAPTICS_WAVEFORM_LIST) break;
|
|
|
|
}
|
|
|
|
free(collections);
|
|
|
|
if (status != HIDP_STATUS_SUCCESS || i == collections_count)
|
|
|
|
{
|
|
|
|
WARN("could not find haptics waveform list collection\n");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
waveform_list = i;
|
|
|
|
|
|
|
|
caps_count = 1;
|
|
|
|
status = HidP_GetSpecificValueCaps(HidP_Feature, HID_USAGE_PAGE_ORDINAL, waveform_list, 3, &waveform_cap, &caps_count, preparsed);
|
|
|
|
if (status != HIDP_STATUS_SUCCESS || !caps_count)
|
|
|
|
{
|
2022-01-28 10:46:45 +01:00
|
|
|
WARN("could not find haptics waveform list report id, status %#lx\n", status);
|
2021-09-24 11:51:55 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = HidP_InitializeReportForID(HidP_Feature, waveform_cap.ReportID, preparsed, report_buf, report_len);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_InitializeReportForID returned %#lx\n", status);
|
2021-09-24 11:51:55 +02:00
|
|
|
if (!HidD_GetFeature(device, report_buf, report_len))
|
|
|
|
{
|
2022-01-28 10:46:45 +01:00
|
|
|
WARN("failed to get waveform list report, error %lu\n", GetLastError());
|
2021-09-24 11:51:55 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2022-02-15 09:03:37 +01:00
|
|
|
controller->hid.haptics_none_ordinal = 1; /* implicit None waveform ordinal, from the HID spec */
|
|
|
|
controller->hid.haptics_stop_ordinal = 2; /* implicit Stop waveform ordinal, from the HID spec */
|
2022-02-14 17:26:20 +01:00
|
|
|
controller->hid.haptics_buzz_ordinal = 0;
|
|
|
|
controller->hid.haptics_rumble_ordinal = 0;
|
2021-09-24 11:51:55 +02:00
|
|
|
for (i = 3; status == HIDP_STATUS_SUCCESS; ++i)
|
|
|
|
{
|
|
|
|
ULONG waveform = 0;
|
|
|
|
status = HidP_GetUsageValue(HidP_Feature, HID_USAGE_PAGE_ORDINAL, waveform_list,
|
|
|
|
i, &waveform, preparsed, report_buf, report_len);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue returned %#lx\n", status);
|
2022-02-14 17:26:20 +01:00
|
|
|
else if (waveform == HID_USAGE_HAPTICS_WAVEFORM_BUZZ) controller->hid.haptics_buzz_ordinal = i;
|
|
|
|
else if (waveform == HID_USAGE_HAPTICS_WAVEFORM_RUMBLE) controller->hid.haptics_rumble_ordinal = i;
|
2021-09-24 11:51:55 +02:00
|
|
|
}
|
|
|
|
|
2022-02-14 17:26:20 +01:00
|
|
|
if (!controller->hid.haptics_buzz_ordinal) WARN("haptics buzz not supported\n");
|
|
|
|
if (!controller->hid.haptics_rumble_ordinal) WARN("haptics rumble not supported\n");
|
|
|
|
if (!controller->hid.haptics_rumble_ordinal && !controller->hid.haptics_buzz_ordinal) return TRUE;
|
2021-09-24 11:51:55 +02:00
|
|
|
|
|
|
|
caps_count = 1;
|
|
|
|
status = HidP_GetSpecificValueCaps(HidP_Output, HID_USAGE_PAGE_HAPTICS, 0, HID_USAGE_HAPTICS_MANUAL_TRIGGER,
|
|
|
|
&waveform_cap, &caps_count, preparsed);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetSpecificValueCaps MANUAL_TRIGGER returned %#lx\n", status);
|
2021-09-24 11:51:55 +02:00
|
|
|
else if (!caps_count) WARN("haptics manual trigger not supported\n");
|
|
|
|
else
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
2021-08-09 11:25:35 +02:00
|
|
|
caps->Flags |= XINPUT_CAPS_FFB_SUPPORTED;
|
|
|
|
caps->Vibration.wLeftMotorSpeed = 255;
|
|
|
|
caps->Vibration.wRightMotorSpeed = 255;
|
2021-09-24 11:51:55 +02:00
|
|
|
controller->hid.haptics_report = waveform_cap.ReportID;
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-08-09 11:25:32 +02:00
|
|
|
static DWORD HID_set_state(struct xinput_controller *controller, XINPUT_VIBRATION *state)
|
2021-08-06 17:08:58 +02:00
|
|
|
{
|
2021-09-24 11:51:55 +02:00
|
|
|
ULONG report_len = controller->hid.caps.OutputReportByteLength;
|
|
|
|
PHIDP_PREPARSED_DATA preparsed = controller->hid.preparsed;
|
|
|
|
char *report_buf = controller->hid.output_report_buf;
|
|
|
|
BYTE report_id = controller->hid.haptics_report;
|
2022-02-16 09:26:55 +01:00
|
|
|
BOOL update_rumble, update_buzz;
|
2021-09-24 11:51:55 +02:00
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
if (!(controller->caps.Flags & XINPUT_CAPS_FFB_SUPPORTED)) return ERROR_SUCCESS;
|
|
|
|
|
2022-02-16 09:26:55 +01:00
|
|
|
update_rumble = (controller->vibration.wLeftMotorSpeed != state->wLeftMotorSpeed);
|
2021-09-24 11:51:55 +02:00
|
|
|
controller->vibration.wLeftMotorSpeed = state->wLeftMotorSpeed;
|
2022-02-16 09:26:55 +01:00
|
|
|
update_buzz = (controller->vibration.wRightMotorSpeed != state->wRightMotorSpeed);
|
2021-09-24 11:51:55 +02:00
|
|
|
controller->vibration.wRightMotorSpeed = state->wRightMotorSpeed;
|
|
|
|
|
|
|
|
if (!controller->enabled) return ERROR_SUCCESS;
|
|
|
|
|
2022-02-15 09:03:37 +01:00
|
|
|
if (!state->wLeftMotorSpeed && !state->wRightMotorSpeed)
|
|
|
|
{
|
|
|
|
status = HidP_InitializeReportForID(HidP_Output, report_id, preparsed, report_buf, report_len);
|
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_InitializeReportForID returned %#lx\n", status);
|
|
|
|
status = HidP_SetUsageValue(HidP_Output, HID_USAGE_PAGE_HAPTICS, 0, HID_USAGE_HAPTICS_MANUAL_TRIGGER,
|
|
|
|
controller->hid.haptics_stop_ordinal, preparsed, report_buf, report_len);
|
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_SetUsageValue MANUAL_TRIGGER returned %#lx\n", status);
|
|
|
|
if (!HidD_SetOutputReport(controller->device, report_buf, report_len))
|
|
|
|
{
|
|
|
|
WARN("HidD_SetOutputReport failed with error %lu\n", GetLastError());
|
|
|
|
return GetLastError();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-16 09:26:55 +01:00
|
|
|
if (update_rumble)
|
2021-09-24 11:51:55 +02:00
|
|
|
{
|
2022-02-16 09:26:55 +01:00
|
|
|
/* send haptics rumble report (left motor) */
|
|
|
|
status = HidP_InitializeReportForID(HidP_Output, report_id, preparsed, report_buf, report_len);
|
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_InitializeReportForID returned %#lx\n", status);
|
|
|
|
status = HidP_SetUsageValue(HidP_Output, HID_USAGE_PAGE_HAPTICS, 0, HID_USAGE_HAPTICS_INTENSITY,
|
|
|
|
state->wLeftMotorSpeed, preparsed, report_buf, report_len);
|
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_SetUsageValue INTENSITY returned %#lx\n", status);
|
|
|
|
status = HidP_SetUsageValue(HidP_Output, HID_USAGE_PAGE_HAPTICS, 0, HID_USAGE_HAPTICS_MANUAL_TRIGGER,
|
|
|
|
controller->hid.haptics_rumble_ordinal, preparsed, report_buf, report_len);
|
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_SetUsageValue MANUAL_TRIGGER returned %#lx\n", status);
|
|
|
|
status = HidP_SetUsageValue(HidP_Output, HID_USAGE_PAGE_HAPTICS, 0, HID_USAGE_HAPTICS_REPEAT_COUNT,
|
|
|
|
0, preparsed, report_buf, report_len);
|
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_SetUsageValue REPEAT_COUNT returned %#lx\n", status);
|
|
|
|
if (!HidD_SetOutputReport(controller->device, report_buf, report_len))
|
|
|
|
{
|
|
|
|
WARN("HidD_SetOutputReport failed with error %lu\n", GetLastError());
|
|
|
|
return GetLastError();
|
|
|
|
}
|
2021-09-24 11:51:55 +02:00
|
|
|
}
|
2021-08-06 17:08:58 +02:00
|
|
|
|
2022-02-16 09:26:55 +01:00
|
|
|
if (update_buzz)
|
2022-02-15 09:03:41 +01:00
|
|
|
{
|
2022-02-16 09:26:55 +01:00
|
|
|
/* send haptics buzz report (right motor) */
|
|
|
|
status = HidP_InitializeReportForID(HidP_Output, report_id, preparsed, report_buf, report_len);
|
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_InitializeReportForID returned %#lx\n", status);
|
|
|
|
status = HidP_SetUsageValue(HidP_Output, HID_USAGE_PAGE_HAPTICS, 0, HID_USAGE_HAPTICS_INTENSITY,
|
|
|
|
state->wRightMotorSpeed, preparsed, report_buf, report_len);
|
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_SetUsageValue INTENSITY returned %#lx\n", status);
|
|
|
|
status = HidP_SetUsageValue(HidP_Output, HID_USAGE_PAGE_HAPTICS, 0, HID_USAGE_HAPTICS_MANUAL_TRIGGER,
|
|
|
|
controller->hid.haptics_buzz_ordinal, preparsed, report_buf, report_len);
|
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_SetUsageValue MANUAL_TRIGGER returned %#lx\n", status);
|
|
|
|
status = HidP_SetUsageValue(HidP_Output, HID_USAGE_PAGE_HAPTICS, 0, HID_USAGE_HAPTICS_REPEAT_COUNT,
|
|
|
|
0, preparsed, report_buf, report_len);
|
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_SetUsageValue REPEAT_COUNT returned %#lx\n", status);
|
|
|
|
if (!HidD_SetOutputReport(controller->device, report_buf, report_len))
|
|
|
|
{
|
|
|
|
WARN("HidD_SetOutputReport failed with error %lu\n", GetLastError());
|
|
|
|
return GetLastError();
|
|
|
|
}
|
2022-02-15 09:03:41 +01:00
|
|
|
}
|
|
|
|
|
2022-02-16 09:26:55 +01:00
|
|
|
if (update_rumble || update_buzz)
|
2021-08-06 17:08:58 +02:00
|
|
|
{
|
2022-02-16 09:26:55 +01:00
|
|
|
/* trigger haptics waveforms */
|
|
|
|
status = HidP_InitializeReportForID(HidP_Output, report_id, preparsed, report_buf, report_len);
|
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_InitializeReportForID returned %#lx\n", status);
|
|
|
|
status = HidP_SetUsageValue(HidP_Output, HID_USAGE_PAGE_HAPTICS, 0, HID_USAGE_HAPTICS_MANUAL_TRIGGER,
|
|
|
|
controller->hid.haptics_none_ordinal, preparsed, report_buf, report_len);
|
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_SetUsageValue MANUAL_TRIGGER returned %#lx\n", status);
|
|
|
|
status = HidP_SetUsageValue(HidP_Output, HID_USAGE_PAGE_HAPTICS, 0, HID_USAGE_HAPTICS_REPEAT_COUNT,
|
|
|
|
1, preparsed, report_buf, report_len);
|
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_SetUsageValue REPEAT_COUNT returned %#lx\n", status);
|
|
|
|
if (!HidD_SetOutputReport(controller->device, report_buf, report_len))
|
|
|
|
{
|
|
|
|
WARN("HidD_SetOutputReport failed with error %lu\n", GetLastError());
|
|
|
|
return GetLastError();
|
|
|
|
}
|
2021-08-06 17:08:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-10-08 13:02:23 +02:00
|
|
|
static void controller_destroy(struct xinput_controller *controller, BOOL already_removed);
|
|
|
|
|
2021-08-06 17:08:58 +02:00
|
|
|
static void controller_enable(struct xinput_controller *controller)
|
|
|
|
{
|
2021-10-08 13:02:23 +02:00
|
|
|
ULONG report_len = controller->hid.caps.InputReportByteLength;
|
|
|
|
char *report_buf = controller->hid.input_report_buf;
|
2021-08-06 17:08:58 +02:00
|
|
|
XINPUT_VIBRATION state = controller->vibration;
|
2021-10-08 13:02:23 +02:00
|
|
|
BOOL ret;
|
2021-08-06 17:08:58 +02:00
|
|
|
|
2021-08-06 17:08:59 +02:00
|
|
|
if (controller->enabled) return;
|
2021-08-06 17:08:58 +02:00
|
|
|
if (controller->caps.Flags & XINPUT_CAPS_FFB_SUPPORTED) HID_set_state(controller, &state);
|
2021-08-06 17:08:59 +02:00
|
|
|
controller->enabled = TRUE;
|
2021-08-10 13:27:24 +02:00
|
|
|
|
|
|
|
memset(&controller->hid.read_ovl, 0, sizeof(controller->hid.read_ovl));
|
|
|
|
controller->hid.read_ovl.hEvent = controller->hid.read_event;
|
2021-10-08 13:02:23 +02:00
|
|
|
ret = ReadFile(controller->device, report_buf, report_len, NULL, &controller->hid.read_ovl);
|
|
|
|
if (!ret && GetLastError() != ERROR_IO_PENDING) controller_destroy(controller, TRUE);
|
|
|
|
else SetEvent(update_event);
|
2021-08-06 17:08:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void controller_disable(struct xinput_controller *controller)
|
|
|
|
{
|
|
|
|
XINPUT_VIBRATION state = {0};
|
|
|
|
|
2021-08-06 17:08:59 +02:00
|
|
|
if (!controller->enabled) return;
|
2021-08-06 17:08:58 +02:00
|
|
|
if (controller->caps.Flags & XINPUT_CAPS_FFB_SUPPORTED) HID_set_state(controller, &state);
|
2021-08-06 17:08:59 +02:00
|
|
|
controller->enabled = FALSE;
|
2021-08-10 13:27:24 +02:00
|
|
|
|
|
|
|
CancelIoEx(controller->device, &controller->hid.read_ovl);
|
2021-10-08 13:02:22 +02:00
|
|
|
WaitForSingleObject(controller->hid.read_ovl.hEvent, INFINITE);
|
2021-08-10 13:27:24 +02:00
|
|
|
SetEvent(update_event);
|
2021-08-06 17:08:58 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 11:25:36 +02:00
|
|
|
static BOOL controller_init(struct xinput_controller *controller, PHIDP_PREPARSED_DATA preparsed,
|
2022-02-15 09:07:25 +01:00
|
|
|
HIDP_CAPS *caps, HANDLE device, const WCHAR *device_path)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
2021-08-10 13:27:24 +02:00
|
|
|
HANDLE event = NULL;
|
|
|
|
|
2021-08-09 11:25:35 +02:00
|
|
|
controller->hid.caps = *caps;
|
2021-09-24 11:51:55 +02:00
|
|
|
if (!(controller->hid.feature_report_buf = calloc(1, controller->hid.caps.FeatureReportByteLength))) goto failed;
|
|
|
|
if (!controller_check_caps(controller, device, preparsed)) goto failed;
|
2021-10-08 13:02:22 +02:00
|
|
|
if (!(event = CreateEventW(NULL, TRUE, FALSE, NULL))) goto failed;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
TRACE("Found gamepad %s\n", debugstr_w(device_path));
|
|
|
|
|
2021-08-09 11:25:35 +02:00
|
|
|
controller->hid.preparsed = preparsed;
|
2021-08-10 13:27:24 +02:00
|
|
|
controller->hid.read_event = event;
|
|
|
|
if (!(controller->hid.input_report_buf = calloc(1, controller->hid.caps.InputReportByteLength))) goto failed;
|
2021-08-09 11:25:35 +02:00
|
|
|
if (!(controller->hid.output_report_buf = calloc(1, controller->hid.caps.OutputReportByteLength))) goto failed;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
memset(&controller->state, 0, sizeof(controller->state));
|
|
|
|
memset(&controller->vibration, 0, sizeof(controller->vibration));
|
2021-08-09 11:25:33 +02:00
|
|
|
lstrcpynW(controller->device_path, device_path, MAX_PATH);
|
2021-08-06 17:08:59 +02:00
|
|
|
controller->enabled = FALSE;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-06 17:08:58 +02:00
|
|
|
EnterCriticalSection(&controller->crit);
|
2021-08-09 11:25:33 +02:00
|
|
|
controller->device = device;
|
2021-08-06 17:08:58 +02:00
|
|
|
controller_enable(controller);
|
|
|
|
LeaveCriticalSection(&controller->crit);
|
2021-08-06 17:08:55 +02:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
failed:
|
2021-08-10 13:27:24 +02:00
|
|
|
free(controller->hid.input_report_buf);
|
2021-08-09 11:25:35 +02:00
|
|
|
free(controller->hid.output_report_buf);
|
2021-09-24 11:51:55 +02:00
|
|
|
free(controller->hid.feature_report_buf);
|
2021-08-09 11:25:35 +02:00
|
|
|
memset(&controller->hid, 0, sizeof(controller->hid));
|
2021-08-10 13:27:24 +02:00
|
|
|
CloseHandle(event);
|
2021-08-06 17:08:55 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-09-24 10:10:34 +02:00
|
|
|
static void get_registry_keys(HKEY *defkey, HKEY *appkey)
|
|
|
|
{
|
|
|
|
WCHAR buffer[MAX_PATH + 26], *name = buffer, *tmp;
|
|
|
|
DWORD len;
|
|
|
|
HKEY hkey;
|
|
|
|
|
|
|
|
*appkey = 0;
|
|
|
|
if (RegOpenKeyW(HKEY_CURRENT_USER, L"Software\\Wine\\DirectInput\\Joysticks", defkey))
|
|
|
|
*defkey = 0;
|
|
|
|
|
|
|
|
if (!(len = GetModuleFileNameW(0, buffer, MAX_PATH)) || len >= MAX_PATH)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!RegOpenKeyW(HKEY_CURRENT_USER, L"Software\\Wine\\AppDefaults", &hkey))
|
|
|
|
{
|
|
|
|
if ((tmp = wcsrchr(name, '/'))) name = tmp + 1;
|
|
|
|
if ((tmp = wcsrchr(name, '\\'))) name = tmp + 1;
|
|
|
|
wcscat(name, L"\\DirectInput\\Joysticks");
|
|
|
|
if (RegOpenKeyW(hkey, name, appkey)) *appkey = 0;
|
|
|
|
RegCloseKey(hkey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 20:53:59 +01:00
|
|
|
static BOOL device_is_overridden(HANDLE device)
|
2021-09-24 10:10:34 +02:00
|
|
|
{
|
|
|
|
WCHAR name[MAX_PATH], buffer[MAX_PATH];
|
|
|
|
DWORD size = sizeof(buffer);
|
|
|
|
BOOL disable = FALSE;
|
|
|
|
HKEY defkey, appkey;
|
|
|
|
|
|
|
|
if (!HidD_GetProductString(device, name, MAX_PATH)) return FALSE;
|
|
|
|
|
|
|
|
get_registry_keys(&defkey, &appkey);
|
|
|
|
if (!defkey && !appkey) return FALSE;
|
|
|
|
if ((appkey && !RegQueryValueExW(appkey, name, 0, NULL, (LPBYTE)buffer, &size)) ||
|
|
|
|
(defkey && !RegQueryValueExW(defkey, name, 0, NULL, (LPBYTE)buffer, &size)))
|
|
|
|
{
|
|
|
|
if ((disable = !wcscmp(buffer, L"override")))
|
|
|
|
TRACE("Disabling gamepad '%s' based on registry key.\n", debugstr_w(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (appkey) RegCloseKey(appkey);
|
|
|
|
if (defkey) RegCloseKey(defkey);
|
|
|
|
return disable;
|
|
|
|
}
|
|
|
|
|
2022-02-15 09:07:25 +01:00
|
|
|
static BOOL try_add_device(const WCHAR *device_path)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
|
|
|
SP_DEVICE_INTERFACE_DATA iface = {sizeof(iface)};
|
|
|
|
PHIDP_PREPARSED_DATA preparsed;
|
|
|
|
HIDP_CAPS caps;
|
|
|
|
NTSTATUS status;
|
|
|
|
HANDLE device;
|
2022-02-15 09:07:25 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (find_opened_device(device_path, &i)) return TRUE; /* already opened */
|
|
|
|
if (i == XUSER_MAX_COUNT) return FALSE; /* no more slots */
|
|
|
|
|
|
|
|
device = CreateFileW(device_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING, NULL);
|
|
|
|
if (device == INVALID_HANDLE_VALUE) return TRUE;
|
|
|
|
|
|
|
|
preparsed = NULL;
|
|
|
|
if (!HidD_GetPreparsedData(device, &preparsed))
|
|
|
|
WARN("ignoring HID device, HidD_GetPreparsedData failed with error %lu\n", GetLastError());
|
|
|
|
else if ((status = HidP_GetCaps(preparsed, &caps)) != HIDP_STATUS_SUCCESS)
|
|
|
|
WARN("ignoring HID device, HidP_GetCaps returned %#lx\n", status);
|
|
|
|
else if (caps.UsagePage != HID_USAGE_PAGE_GENERIC)
|
|
|
|
WARN("ignoring HID device, unsupported usage page %04x\n", caps.UsagePage);
|
|
|
|
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);
|
2022-02-17 20:53:59 +01:00
|
|
|
else if (device_is_overridden(device))
|
|
|
|
WARN("ignoring HID device, overridden for dinput\n");
|
2022-02-15 09:07:25 +01:00
|
|
|
else if (!controller_init(&controllers[i], preparsed, &caps, device, device_path))
|
|
|
|
WARN("ignoring HID device, failed to initialize\n");
|
|
|
|
else
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
CloseHandle(device);
|
|
|
|
HidD_FreePreparsedData(preparsed);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void try_remove_device(const WCHAR *device_path)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (find_opened_device(device_path, &i))
|
|
|
|
controller_destroy(&controllers[i], TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_controller_list(void)
|
|
|
|
{
|
|
|
|
char buffer[sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W) + MAX_PATH * sizeof(WCHAR)];
|
|
|
|
SP_DEVICE_INTERFACE_DETAIL_DATA_W *detail = (SP_DEVICE_INTERFACE_DETAIL_DATA_W *)buffer;
|
|
|
|
SP_DEVICE_INTERFACE_DATA iface = {sizeof(iface)};
|
|
|
|
HDEVINFO set;
|
2021-08-06 17:08:55 +02:00
|
|
|
DWORD idx;
|
|
|
|
GUID guid;
|
|
|
|
|
2021-09-03 07:57:39 +02:00
|
|
|
guid = GUID_DEVINTERFACE_WINEXINPUT;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
set = SetupDiGetClassDevsW(&guid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
|
|
|
|
detail->cbSize = sizeof(*detail);
|
|
|
|
|
|
|
|
idx = 0;
|
|
|
|
while (SetupDiEnumDeviceInterfaces(set, NULL, &guid, idx++, &iface))
|
|
|
|
{
|
|
|
|
if (!SetupDiGetDeviceInterfaceDetailW(set, &iface, detail, sizeof(buffer), NULL, NULL))
|
|
|
|
continue;
|
2022-02-15 09:07:25 +01:00
|
|
|
if (!try_add_device(detail->DevicePath))
|
|
|
|
break;
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SetupDiDestroyDeviceInfoList(set);
|
|
|
|
}
|
|
|
|
|
2021-10-08 13:02:23 +02:00
|
|
|
static void controller_destroy(struct xinput_controller *controller, BOOL already_removed)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
2021-08-09 11:25:32 +02:00
|
|
|
EnterCriticalSection(&controller->crit);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-09 11:25:33 +02:00
|
|
|
if (controller->device)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
2021-10-08 13:02:23 +02:00
|
|
|
if (!already_removed) controller_disable(controller);
|
2021-08-09 11:25:33 +02:00
|
|
|
CloseHandle(controller->device);
|
|
|
|
controller->device = NULL;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
free(controller->hid.input_report_buf);
|
2021-08-09 11:25:35 +02:00
|
|
|
free(controller->hid.output_report_buf);
|
2021-09-24 11:51:55 +02:00
|
|
|
free(controller->hid.feature_report_buf);
|
2021-08-09 11:25:35 +02:00
|
|
|
HidD_FreePreparsedData(controller->hid.preparsed);
|
|
|
|
memset(&controller->hid, 0, sizeof(controller->hid));
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 11:25:32 +02:00
|
|
|
LeaveCriticalSection(&controller->crit);
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 13:27:22 +02:00
|
|
|
static void stop_update_thread(void)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
|
|
|
int i;
|
2021-08-10 13:27:22 +02:00
|
|
|
|
|
|
|
SetEvent(stop_event);
|
|
|
|
WaitForSingleObject(done_event, INFINITE);
|
|
|
|
|
2021-10-08 13:02:25 +02:00
|
|
|
CloseHandle(start_event);
|
2021-08-10 13:27:22 +02:00
|
|
|
CloseHandle(stop_event);
|
|
|
|
CloseHandle(done_event);
|
2021-08-10 13:27:24 +02:00
|
|
|
CloseHandle(update_event);
|
2021-08-10 13:27:22 +02:00
|
|
|
|
2021-10-08 13:02:23 +02:00
|
|
|
for (i = 0; i < XUSER_MAX_COUNT; i++) controller_destroy(&controllers[i], FALSE);
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 11:25:34 +02:00
|
|
|
static LONG sign_extend(ULONG value, const HIDP_VALUE_CAPS *caps)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
2021-08-09 11:25:34 +02:00
|
|
|
UINT sign = 1 << (caps->BitSize - 1);
|
|
|
|
if (sign <= 1 || caps->LogicalMin >= 0) return value;
|
|
|
|
return value - ((value & sign) << 1);
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 11:25:34 +02:00
|
|
|
static LONG scale_value(ULONG value, const HIDP_VALUE_CAPS *caps, LONG min, LONG max)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
2021-08-09 11:25:34 +02:00
|
|
|
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);
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
static void read_controller_state(struct xinput_controller *controller)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
2021-08-10 13:27:24 +02:00
|
|
|
ULONG read_len, report_len = controller->hid.caps.InputReportByteLength;
|
|
|
|
char *report_buf = controller->hid.input_report_buf;
|
|
|
|
XINPUT_STATE state;
|
2021-08-06 17:08:55 +02:00
|
|
|
NTSTATUS status;
|
|
|
|
USAGE buttons[11];
|
2021-08-10 13:27:24 +02:00
|
|
|
ULONG i, button_length, value;
|
2021-10-08 13:02:23 +02:00
|
|
|
BOOL ret;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
if (!GetOverlappedResult(controller->device, &controller->hid.read_ovl, &read_len, TRUE))
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
2021-08-10 13:27:24 +02:00
|
|
|
if (GetLastError() == ERROR_OPERATION_ABORTED) return;
|
2021-10-08 13:02:23 +02:00
|
|
|
if (GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == ERROR_INVALID_HANDLE) controller_destroy(controller, TRUE);
|
2022-01-28 10:46:45 +01:00
|
|
|
else ERR("Failed to read input report, GetOverlappedResult failed with error %lu\n", GetLastError());
|
2021-08-06 17:08:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
button_length = ARRAY_SIZE(buttons);
|
|
|
|
status = HidP_GetUsages(HidP_Input, HID_USAGE_PAGE_BUTTON, 0, buttons, &button_length, controller->hid.preparsed, report_buf, report_len);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsages HID_USAGE_PAGE_BUTTON returned %#lx\n", status);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
state.Gamepad.wButtons = 0;
|
|
|
|
for (i = 0; i < button_length; i++)
|
|
|
|
{
|
|
|
|
switch (buttons[i])
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
2021-08-10 13:27:24 +02:00
|
|
|
case 1: state.Gamepad.wButtons |= XINPUT_GAMEPAD_A; break;
|
|
|
|
case 2: state.Gamepad.wButtons |= XINPUT_GAMEPAD_B; break;
|
|
|
|
case 3: state.Gamepad.wButtons |= XINPUT_GAMEPAD_X; break;
|
|
|
|
case 4: state.Gamepad.wButtons |= XINPUT_GAMEPAD_Y; break;
|
|
|
|
case 5: state.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_SHOULDER; break;
|
|
|
|
case 6: state.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_SHOULDER; break;
|
|
|
|
case 7: state.Gamepad.wButtons |= XINPUT_GAMEPAD_BACK; break;
|
|
|
|
case 8: state.Gamepad.wButtons |= XINPUT_GAMEPAD_START; break;
|
|
|
|
case 9: state.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_THUMB; break;
|
|
|
|
case 10: state.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_THUMB; break;
|
|
|
|
case 11: state.Gamepad.wButtons |= XINPUT_GAMEPAD_GUIDE; break;
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
2021-08-10 13:27:24 +02:00
|
|
|
}
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_HATSWITCH, &value, controller->hid.preparsed, report_buf, report_len);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_HATSWITCH returned %#lx\n", status);
|
2021-08-10 13:27:24 +02:00
|
|
|
else switch (value)
|
|
|
|
{
|
|
|
|
/* 8 1 2
|
|
|
|
* 7 0 3
|
|
|
|
* 6 5 4 */
|
|
|
|
case 0: break;
|
|
|
|
case 1: state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP; break;
|
|
|
|
case 2: state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP | XINPUT_GAMEPAD_DPAD_RIGHT; break;
|
|
|
|
case 3: state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT; break;
|
|
|
|
case 4: state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT | XINPUT_GAMEPAD_DPAD_DOWN; break;
|
|
|
|
case 5: state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN; break;
|
|
|
|
case 6: state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN | XINPUT_GAMEPAD_DPAD_LEFT; break;
|
|
|
|
case 7: state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT; break;
|
|
|
|
case 8: state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT | XINPUT_GAMEPAD_DPAD_UP; break;
|
|
|
|
}
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_X, &value, controller->hid.preparsed, report_buf, report_len);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_X returned %#lx\n", status);
|
2021-08-10 13:27:24 +02:00
|
|
|
else state.Gamepad.sThumbLX = scale_value(value, &controller->hid.lx_caps, -32768, 32767);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_Y, &value, controller->hid.preparsed, report_buf, report_len);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_Y returned %#lx\n", status);
|
2021-08-10 13:27:24 +02:00
|
|
|
else state.Gamepad.sThumbLY = -scale_value(value, &controller->hid.ly_caps, -32768, 32767) - 1;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RX, &value, controller->hid.preparsed, report_buf, report_len);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_RX returned %#lx\n", status);
|
2021-08-10 13:27:24 +02:00
|
|
|
else state.Gamepad.sThumbRX = scale_value(value, &controller->hid.rx_caps, -32768, 32767);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RY, &value, controller->hid.preparsed, report_buf, report_len);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_RY returned %#lx\n", status);
|
2021-08-10 13:27:24 +02:00
|
|
|
else state.Gamepad.sThumbRY = -scale_value(value, &controller->hid.ry_caps, -32768, 32767) - 1;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RZ, &value, controller->hid.preparsed, report_buf, report_len);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_RZ returned %#lx\n", status);
|
2021-08-10 13:27:24 +02:00
|
|
|
else state.Gamepad.bRightTrigger = scale_value(value, &controller->hid.rt_caps, 0, 255);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
status = HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_Z, &value, controller->hid.preparsed, report_buf, report_len);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (status != HIDP_STATUS_SUCCESS) WARN("HidP_GetUsageValue HID_USAGE_PAGE_GENERIC / HID_USAGE_GENERIC_Z returned %#lx\n", status);
|
2021-08-10 13:27:24 +02:00
|
|
|
else state.Gamepad.bLeftTrigger = scale_value(value, &controller->hid.lt_caps, 0, 255);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
EnterCriticalSection(&controller->crit);
|
|
|
|
if (controller->enabled)
|
|
|
|
{
|
|
|
|
state.dwPacketNumber = controller->state.dwPacketNumber + 1;
|
|
|
|
controller->state = state;
|
|
|
|
memset(&controller->hid.read_ovl, 0, sizeof(controller->hid.read_ovl));
|
|
|
|
controller->hid.read_ovl.hEvent = controller->hid.read_event;
|
2021-10-08 13:02:23 +02:00
|
|
|
ret = ReadFile(controller->device, report_buf, report_len, NULL, &controller->hid.read_ovl);
|
|
|
|
if (!ret && GetLastError() != ERROR_IO_PENDING) controller_destroy(controller, TRUE);
|
2021-08-10 13:27:24 +02:00
|
|
|
}
|
|
|
|
LeaveCriticalSection(&controller->crit);
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
|
|
|
|
2021-10-08 13:02:26 +02:00
|
|
|
static LRESULT CALLBACK xinput_devnotify_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
|
|
|
|
{
|
2022-02-15 09:07:25 +01:00
|
|
|
if (msg == WM_DEVICECHANGE)
|
|
|
|
{
|
|
|
|
DEV_BROADCAST_DEVICEINTERFACE_W *iface = (DEV_BROADCAST_DEVICEINTERFACE_W *)lparam;
|
|
|
|
if (wparam == DBT_DEVICEARRIVAL) try_add_device(iface->dbcc_name);
|
|
|
|
if (wparam == DBT_DEVICEREMOVECOMPLETE) try_remove_device(iface->dbcc_name);
|
|
|
|
}
|
|
|
|
|
2021-10-08 13:02:26 +02:00
|
|
|
return DefWindowProcW(hwnd, msg, wparam, lparam);
|
|
|
|
}
|
|
|
|
|
2021-08-10 13:27:22 +02:00
|
|
|
static DWORD WINAPI hid_update_thread_proc(void *param)
|
|
|
|
{
|
2021-08-10 13:27:24 +02:00
|
|
|
struct xinput_controller *devices[XUSER_MAX_COUNT + 2];
|
|
|
|
HANDLE events[XUSER_MAX_COUNT + 2];
|
|
|
|
DWORD i, count = 2, ret = WAIT_TIMEOUT;
|
2021-10-08 13:02:26 +02:00
|
|
|
DEV_BROADCAST_DEVICEINTERFACE_W filter =
|
|
|
|
{
|
|
|
|
.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE_W),
|
|
|
|
.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE,
|
|
|
|
.dbcc_classguid = GUID_DEVINTERFACE_WINEXINPUT,
|
|
|
|
};
|
|
|
|
WNDCLASSEXW cls =
|
|
|
|
{
|
|
|
|
.cbSize = sizeof(WNDCLASSEXW),
|
|
|
|
.hInstance = xinput_instance,
|
|
|
|
.lpszClassName = L"__wine_xinput_devnotify",
|
|
|
|
.lpfnWndProc = xinput_devnotify_wndproc,
|
|
|
|
};
|
|
|
|
HDEVNOTIFY notif;
|
|
|
|
HWND hwnd;
|
|
|
|
MSG msg;
|
|
|
|
|
|
|
|
RegisterClassExW(&cls);
|
|
|
|
hwnd = CreateWindowExW(0, cls.lpszClassName, NULL, 0, 0, 0, 0, 0,
|
|
|
|
HWND_MESSAGE, NULL, NULL, NULL);
|
|
|
|
notif = RegisterDeviceNotificationW(hwnd, &filter, DEVICE_NOTIFY_WINDOW_HANDLE);
|
2021-08-10 13:27:22 +02:00
|
|
|
|
2021-10-08 13:02:25 +02:00
|
|
|
update_controller_list();
|
|
|
|
SetEvent(start_event);
|
|
|
|
|
2021-08-10 13:27:22 +02:00
|
|
|
do
|
|
|
|
{
|
2021-10-08 13:02:26 +02:00
|
|
|
if (ret == count) while (PeekMessageW(&msg, hwnd, 0, 0, PM_REMOVE)) DispatchMessageW(&msg);
|
2021-08-10 13:27:23 +02:00
|
|
|
if (ret == WAIT_TIMEOUT) update_controller_list();
|
2021-08-10 13:27:24 +02:00
|
|
|
if (ret < count - 2) read_controller_state(devices[ret]);
|
2021-08-10 13:27:23 +02:00
|
|
|
|
2021-08-10 13:27:22 +02:00
|
|
|
count = 0;
|
2021-08-10 13:27:24 +02:00
|
|
|
for (i = 0; i < XUSER_MAX_COUNT; ++i)
|
|
|
|
{
|
|
|
|
if (!controllers[i].device) continue;
|
|
|
|
EnterCriticalSection(&controllers[i].crit);
|
|
|
|
if (controllers[i].enabled)
|
|
|
|
{
|
|
|
|
devices[count] = controllers + i;
|
|
|
|
events[count] = controllers[i].hid.read_event;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&controllers[i].crit);
|
|
|
|
}
|
|
|
|
events[count++] = update_event;
|
2021-08-10 13:27:22 +02:00
|
|
|
events[count++] = stop_event;
|
|
|
|
}
|
2021-10-08 13:02:26 +02:00
|
|
|
while ((ret = MsgWaitForMultipleObjectsEx(count, events, 2000, QS_ALLINPUT, MWMO_ALERTABLE)) < count - 1 ||
|
|
|
|
ret == count || ret == WAIT_TIMEOUT);
|
|
|
|
|
|
|
|
UnregisterDeviceNotification(notif);
|
|
|
|
DestroyWindow(hwnd);
|
|
|
|
UnregisterClassW(cls.lpszClassName, xinput_instance);
|
2021-08-10 13:27:22 +02:00
|
|
|
|
2022-01-28 10:46:45 +01:00
|
|
|
if (ret != count - 1) ERR("update thread exited unexpectedly, ret %lu\n", ret);
|
2021-08-10 13:27:22 +02:00
|
|
|
SetEvent(done_event);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI start_update_thread_once( INIT_ONCE *once, void *param, void **context )
|
|
|
|
{
|
|
|
|
HANDLE thread;
|
|
|
|
|
2021-10-08 13:02:25 +02:00
|
|
|
start_event = CreateEventA(NULL, FALSE, FALSE, NULL);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (!start_event) ERR("failed to create start event, error %lu\n", GetLastError());
|
2021-10-08 13:02:25 +02:00
|
|
|
|
2021-08-10 13:27:22 +02:00
|
|
|
stop_event = CreateEventA(NULL, FALSE, FALSE, NULL);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (!stop_event) ERR("failed to create stop event, error %lu\n", GetLastError());
|
2021-08-10 13:27:22 +02:00
|
|
|
|
|
|
|
done_event = CreateEventA(NULL, FALSE, FALSE, NULL);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (!done_event) ERR("failed to create done event, error %lu\n", GetLastError());
|
2021-08-10 13:27:22 +02:00
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
update_event = CreateEventA(NULL, FALSE, FALSE, NULL);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (!update_event) ERR("failed to create update event, error %lu\n", GetLastError());
|
2021-08-10 13:27:24 +02:00
|
|
|
|
2021-08-10 13:27:22 +02:00
|
|
|
thread = CreateThread(NULL, 0, hid_update_thread_proc, NULL, 0, NULL);
|
2022-01-28 10:46:45 +01:00
|
|
|
if (!thread) ERR("failed to create update thread, error %lu\n", GetLastError());
|
2021-08-10 13:27:22 +02:00
|
|
|
CloseHandle(thread);
|
|
|
|
|
2021-10-08 13:02:25 +02:00
|
|
|
WaitForSingleObject(start_event, INFINITE);
|
2021-08-10 13:27:22 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-08-10 13:27:23 +02:00
|
|
|
static void start_update_thread(void)
|
|
|
|
{
|
|
|
|
static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
|
|
|
|
InitOnceExecuteOnce(&init_once, start_update_thread_once, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2021-08-09 11:25:36 +02:00
|
|
|
static BOOL controller_lock(struct xinput_controller *controller)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
2021-08-09 11:25:33 +02:00
|
|
|
if (!controller->device) return FALSE;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-09 11:25:32 +02:00
|
|
|
EnterCriticalSection(&controller->crit);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-09 11:25:33 +02:00
|
|
|
if (!controller->device)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
2021-08-09 11:25:32 +02:00
|
|
|
LeaveCriticalSection(&controller->crit);
|
2021-08-06 17:08:55 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-08-09 11:25:36 +02:00
|
|
|
static void controller_unlock(struct xinput_controller *controller)
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
2021-08-09 11:25:32 +02:00
|
|
|
LeaveCriticalSection(&controller->crit);
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved)
|
|
|
|
{
|
2022-01-28 10:46:45 +01:00
|
|
|
TRACE("inst %p, reason %lu, reserved %p.\n", inst, reason, reserved);
|
|
|
|
|
2021-08-06 17:08:55 +02:00
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
2021-10-08 13:02:26 +02:00
|
|
|
xinput_instance = inst;
|
2021-08-06 17:08:55 +02:00
|
|
|
DisableThreadLibraryCalls(inst);
|
|
|
|
break;
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
if (reserved) break;
|
2021-08-10 13:27:22 +02:00
|
|
|
stop_update_thread();
|
2021-08-06 17:08:55 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WINAPI DECLSPEC_HOTPATCH XInputEnable(BOOL enable)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
2022-01-28 10:46:45 +01:00
|
|
|
TRACE("enable %d.\n", enable);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
/* Setting to false will stop messages from XInputSetState being sent
|
|
|
|
to the controllers. Setting to true will send the last vibration
|
|
|
|
value (sent to XInputSetState) to the controller and allow messages to
|
|
|
|
be sent */
|
2021-08-10 13:27:23 +02:00
|
|
|
start_update_thread();
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
for (index = 0; index < XUSER_MAX_COUNT; index++)
|
|
|
|
{
|
2021-08-09 11:25:36 +02:00
|
|
|
if (!controller_lock(&controllers[index])) continue;
|
2021-08-06 17:08:58 +02:00
|
|
|
if (enable) controller_enable(&controllers[index]);
|
|
|
|
else controller_disable(&controllers[index]);
|
2021-08-09 11:25:36 +02:00
|
|
|
controller_unlock(&controllers[index]);
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI DECLSPEC_HOTPATCH XInputSetState(DWORD index, XINPUT_VIBRATION *vibration)
|
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
|
2022-01-28 10:46:45 +01:00
|
|
|
TRACE("index %lu, vibration %p.\n", index, vibration);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-10 13:27:23 +02:00
|
|
|
start_update_thread();
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
if (index >= XUSER_MAX_COUNT) return ERROR_BAD_ARGUMENTS;
|
2021-08-09 11:25:36 +02:00
|
|
|
if (!controller_lock(&controllers[index])) return ERROR_DEVICE_NOT_CONNECTED;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
ret = HID_set_state(&controllers[index], vibration);
|
|
|
|
|
2021-08-09 11:25:36 +02:00
|
|
|
controller_unlock(&controllers[index]);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Some versions of SteamOverlayRenderer hot-patch XInputGetStateEx() and call
|
|
|
|
* XInputGetState() in the hook, so we need a wrapper. */
|
|
|
|
static DWORD xinput_get_state(DWORD index, XINPUT_STATE *state)
|
|
|
|
{
|
|
|
|
if (!state) return ERROR_BAD_ARGUMENTS;
|
|
|
|
|
2021-08-10 13:27:23 +02:00
|
|
|
start_update_thread();
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
if (index >= XUSER_MAX_COUNT) return ERROR_BAD_ARGUMENTS;
|
2021-08-09 11:25:36 +02:00
|
|
|
if (!controller_lock(&controllers[index])) return ERROR_DEVICE_NOT_CONNECTED;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-10 13:27:24 +02:00
|
|
|
*state = controllers[index].state;
|
2021-08-09 11:25:36 +02:00
|
|
|
controller_unlock(&controllers[index]);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI DECLSPEC_HOTPATCH XInputGetState(DWORD index, XINPUT_STATE *state)
|
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
|
2022-01-28 10:46:45 +01:00
|
|
|
TRACE("index %lu, state %p.\n", index, state);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
ret = xinput_get_state(index, state);
|
|
|
|
if (ret != ERROR_SUCCESS) return ret;
|
|
|
|
|
|
|
|
/* The main difference between this and the Ex version is the media guide button */
|
|
|
|
state->Gamepad.wButtons &= ~XINPUT_GAMEPAD_GUIDE;
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI DECLSPEC_HOTPATCH XInputGetStateEx(DWORD index, XINPUT_STATE *state)
|
|
|
|
{
|
2022-01-28 10:46:45 +01:00
|
|
|
TRACE("index %lu, state %p.\n", index, state);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
return xinput_get_state(index, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int JS_STATE_OFF = 0;
|
|
|
|
static const int JS_STATE_LOW = 1;
|
|
|
|
static const int JS_STATE_HIGH = 2;
|
|
|
|
|
|
|
|
static int joystick_state(const SHORT value)
|
|
|
|
{
|
|
|
|
if (value > 20000) return JS_STATE_HIGH;
|
|
|
|
if (value < -20000) return JS_STATE_LOW;
|
|
|
|
return JS_STATE_OFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WORD js_vk_offs(const int x, const int y)
|
|
|
|
{
|
|
|
|
if (y == JS_STATE_OFF)
|
|
|
|
{
|
|
|
|
/*if (x == JS_STATE_OFF) shouldn't get here */
|
|
|
|
if (x == JS_STATE_LOW) return 3; /* LEFT */
|
|
|
|
/*if (x == JS_STATE_HIGH)*/ return 2; /* RIGHT */
|
|
|
|
}
|
|
|
|
if (y == JS_STATE_HIGH)
|
|
|
|
{
|
|
|
|
if (x == JS_STATE_OFF) return 0; /* UP */
|
|
|
|
if (x == JS_STATE_LOW) return 4; /* UPLEFT */
|
|
|
|
/*if (x == JS_STATE_HIGH)*/ return 5; /* UPRIGHT */
|
|
|
|
}
|
|
|
|
/*if (y == JS_STATE_LOW)*/
|
|
|
|
{
|
|
|
|
if (x == JS_STATE_OFF) return 1; /* DOWN */
|
|
|
|
if (x == JS_STATE_LOW) return 7; /* DOWNLEFT */
|
|
|
|
/*if (x == JS_STATE_HIGH)*/ return 6; /* DOWNRIGHT */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD check_joystick_keystroke(const DWORD index, XINPUT_KEYSTROKE *keystroke, const SHORT *cur_x,
|
|
|
|
const SHORT *cur_y, SHORT *last_x, SHORT *last_y, const WORD base_vk)
|
|
|
|
{
|
|
|
|
int cur_vk = 0, cur_x_st, cur_y_st;
|
|
|
|
int last_vk = 0, last_x_st, last_y_st;
|
|
|
|
|
|
|
|
cur_x_st = joystick_state(*cur_x);
|
|
|
|
cur_y_st = joystick_state(*cur_y);
|
|
|
|
if (cur_x_st || cur_y_st)
|
|
|
|
cur_vk = base_vk + js_vk_offs(cur_x_st, cur_y_st);
|
|
|
|
|
|
|
|
last_x_st = joystick_state(*last_x);
|
|
|
|
last_y_st = joystick_state(*last_y);
|
|
|
|
if (last_x_st || last_y_st)
|
|
|
|
last_vk = base_vk + js_vk_offs(last_x_st, last_y_st);
|
|
|
|
|
|
|
|
if (cur_vk != last_vk)
|
|
|
|
{
|
|
|
|
if (last_vk)
|
|
|
|
{
|
|
|
|
/* joystick was set, and now different. send a KEYUP event, and set
|
|
|
|
* last pos to centered, so the appropriate KEYDOWN event will be
|
|
|
|
* sent on the next call. */
|
|
|
|
keystroke->VirtualKey = last_vk;
|
|
|
|
keystroke->Unicode = 0; /* unused */
|
|
|
|
keystroke->Flags = XINPUT_KEYSTROKE_KEYUP;
|
|
|
|
keystroke->UserIndex = index;
|
|
|
|
keystroke->HidCode = 0;
|
|
|
|
|
|
|
|
*last_x = 0;
|
|
|
|
*last_y = 0;
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* joystick was unset, send KEYDOWN. */
|
|
|
|
keystroke->VirtualKey = cur_vk;
|
|
|
|
keystroke->Unicode = 0; /* unused */
|
|
|
|
keystroke->Flags = XINPUT_KEYSTROKE_KEYDOWN;
|
|
|
|
keystroke->UserIndex = index;
|
|
|
|
keystroke->HidCode = 0;
|
|
|
|
|
|
|
|
*last_x = *cur_x;
|
|
|
|
*last_y = *cur_y;
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
*last_x = *cur_x;
|
|
|
|
*last_y = *cur_y;
|
|
|
|
|
|
|
|
return ERROR_EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL trigger_is_on(const BYTE value)
|
|
|
|
{
|
|
|
|
return value > 30;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD check_for_keystroke(const DWORD index, XINPUT_KEYSTROKE *keystroke)
|
|
|
|
{
|
2021-08-09 11:25:32 +02:00
|
|
|
struct xinput_controller *controller = &controllers[index];
|
2021-08-06 17:08:55 +02:00
|
|
|
const XINPUT_GAMEPAD *cur;
|
|
|
|
DWORD ret = ERROR_EMPTY;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
int mask;
|
|
|
|
WORD vk;
|
|
|
|
} buttons[] = {
|
|
|
|
{ XINPUT_GAMEPAD_DPAD_UP, VK_PAD_DPAD_UP },
|
|
|
|
{ XINPUT_GAMEPAD_DPAD_DOWN, VK_PAD_DPAD_DOWN },
|
|
|
|
{ XINPUT_GAMEPAD_DPAD_LEFT, VK_PAD_DPAD_LEFT },
|
|
|
|
{ XINPUT_GAMEPAD_DPAD_RIGHT, VK_PAD_DPAD_RIGHT },
|
|
|
|
{ XINPUT_GAMEPAD_START, VK_PAD_START },
|
|
|
|
{ XINPUT_GAMEPAD_BACK, VK_PAD_BACK },
|
|
|
|
{ XINPUT_GAMEPAD_LEFT_THUMB, VK_PAD_LTHUMB_PRESS },
|
|
|
|
{ XINPUT_GAMEPAD_RIGHT_THUMB, VK_PAD_RTHUMB_PRESS },
|
|
|
|
{ XINPUT_GAMEPAD_LEFT_SHOULDER, VK_PAD_LSHOULDER },
|
|
|
|
{ XINPUT_GAMEPAD_RIGHT_SHOULDER, VK_PAD_RSHOULDER },
|
|
|
|
{ XINPUT_GAMEPAD_A, VK_PAD_A },
|
|
|
|
{ XINPUT_GAMEPAD_B, VK_PAD_B },
|
|
|
|
{ XINPUT_GAMEPAD_X, VK_PAD_X },
|
|
|
|
{ XINPUT_GAMEPAD_Y, VK_PAD_Y },
|
|
|
|
/* note: guide button does not send an event */
|
|
|
|
};
|
|
|
|
|
2021-08-09 11:25:36 +02:00
|
|
|
if (!controller_lock(controller)) return ERROR_DEVICE_NOT_CONNECTED;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-09 11:25:32 +02:00
|
|
|
cur = &controller->state.Gamepad;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
/*** buttons ***/
|
|
|
|
for (i = 0; i < ARRAY_SIZE(buttons); ++i)
|
|
|
|
{
|
2021-08-09 11:25:32 +02:00
|
|
|
if ((cur->wButtons & buttons[i].mask) ^ (controller->last_keystroke.wButtons & buttons[i].mask))
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
|
|
|
keystroke->VirtualKey = buttons[i].vk;
|
|
|
|
keystroke->Unicode = 0; /* unused */
|
|
|
|
if (cur->wButtons & buttons[i].mask)
|
|
|
|
{
|
|
|
|
keystroke->Flags = XINPUT_KEYSTROKE_KEYDOWN;
|
2021-08-09 11:25:32 +02:00
|
|
|
controller->last_keystroke.wButtons |= buttons[i].mask;
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
keystroke->Flags = XINPUT_KEYSTROKE_KEYUP;
|
2021-08-09 11:25:32 +02:00
|
|
|
controller->last_keystroke.wButtons &= ~buttons[i].mask;
|
2021-08-06 17:08:55 +02:00
|
|
|
}
|
|
|
|
keystroke->UserIndex = index;
|
|
|
|
keystroke->HidCode = 0;
|
|
|
|
ret = ERROR_SUCCESS;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** triggers ***/
|
2021-08-09 11:25:32 +02:00
|
|
|
if (trigger_is_on(cur->bLeftTrigger) ^ trigger_is_on(controller->last_keystroke.bLeftTrigger))
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
|
|
|
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;
|
2021-08-09 11:25:32 +02:00
|
|
|
controller->last_keystroke.bLeftTrigger = cur->bLeftTrigger;
|
2021-08-06 17:08:55 +02:00
|
|
|
ret = ERROR_SUCCESS;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2021-08-09 11:25:32 +02:00
|
|
|
if (trigger_is_on(cur->bRightTrigger) ^ trigger_is_on(controller->last_keystroke.bRightTrigger))
|
2021-08-06 17:08:55 +02:00
|
|
|
{
|
|
|
|
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;
|
2021-08-09 11:25:32 +02:00
|
|
|
controller->last_keystroke.bRightTrigger = cur->bRightTrigger;
|
2021-08-06 17:08:55 +02:00
|
|
|
ret = ERROR_SUCCESS;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** joysticks ***/
|
|
|
|
ret = check_joystick_keystroke(index, keystroke, &cur->sThumbLX, &cur->sThumbLY,
|
2021-08-09 11:25:32 +02:00
|
|
|
&controller->last_keystroke.sThumbLX,
|
|
|
|
&controller->last_keystroke.sThumbLY, VK_PAD_LTHUMB_UP);
|
2021-08-06 17:08:55 +02:00
|
|
|
if (ret == ERROR_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
ret = check_joystick_keystroke(index, keystroke, &cur->sThumbRX, &cur->sThumbRY,
|
2021-08-09 11:25:32 +02:00
|
|
|
&controller->last_keystroke.sThumbRX,
|
|
|
|
&controller->last_keystroke.sThumbRY, VK_PAD_RTHUMB_UP);
|
2021-08-06 17:08:55 +02:00
|
|
|
if (ret == ERROR_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
done:
|
2021-08-09 11:25:36 +02:00
|
|
|
controller_unlock(controller);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI DECLSPEC_HOTPATCH XInputGetKeystroke(DWORD index, DWORD reserved, PXINPUT_KEYSTROKE keystroke)
|
|
|
|
{
|
2022-01-28 10:46:45 +01:00
|
|
|
TRACE("index %lu, reserved %lu, keystroke %p.\n", index, reserved, keystroke);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
if (index >= XUSER_MAX_COUNT && index != XUSER_INDEX_ANY) return ERROR_BAD_ARGUMENTS;
|
|
|
|
|
|
|
|
if (index == XUSER_INDEX_ANY)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < XUSER_MAX_COUNT; ++i)
|
|
|
|
if (check_for_keystroke(i, keystroke) == ERROR_SUCCESS)
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
return ERROR_EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return check_for_keystroke(index, keystroke);
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI DECLSPEC_HOTPATCH XInputGetCapabilities(DWORD index, DWORD flags, XINPUT_CAPABILITIES *capabilities)
|
|
|
|
{
|
2022-01-28 10:46:45 +01:00
|
|
|
TRACE("index %lu, flags %#lx, capabilities %p.\n", index, flags, capabilities);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
2021-08-10 13:27:23 +02:00
|
|
|
start_update_thread();
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
if (index >= XUSER_MAX_COUNT) return ERROR_BAD_ARGUMENTS;
|
|
|
|
|
2021-08-09 11:25:36 +02:00
|
|
|
if (!controller_lock(&controllers[index])) return ERROR_DEVICE_NOT_CONNECTED;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
if (flags & XINPUT_FLAG_GAMEPAD && controllers[index].caps.SubType != XINPUT_DEVSUBTYPE_GAMEPAD)
|
|
|
|
{
|
2021-08-09 11:25:36 +02:00
|
|
|
controller_unlock(&controllers[index]);
|
2021-08-06 17:08:55 +02:00
|
|
|
return ERROR_DEVICE_NOT_CONNECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(capabilities, &controllers[index].caps, sizeof(*capabilities));
|
|
|
|
|
2021-08-09 11:25:36 +02:00
|
|
|
controller_unlock(&controllers[index]);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI DECLSPEC_HOTPATCH XInputGetDSoundAudioDeviceGuids(DWORD index, GUID *render_guid, GUID *capture_guid)
|
|
|
|
{
|
2022-01-28 10:46:45 +01:00
|
|
|
FIXME("index %lu, render_guid %s, capture_guid %s stub!\n", index, debugstr_guid(render_guid),
|
|
|
|
debugstr_guid(capture_guid));
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
if (index >= XUSER_MAX_COUNT) return ERROR_BAD_ARGUMENTS;
|
2021-08-09 11:25:33 +02:00
|
|
|
if (!controllers[index].device) return ERROR_DEVICE_NOT_CONNECTED;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
return ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI DECLSPEC_HOTPATCH XInputGetBatteryInformation(DWORD index, BYTE type, XINPUT_BATTERY_INFORMATION* battery)
|
|
|
|
{
|
|
|
|
static int once;
|
|
|
|
|
2022-01-28 10:46:45 +01:00
|
|
|
if (!once++) FIXME("index %lu, type %u, battery %p.\n", index, type, battery);
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
if (index >= XUSER_MAX_COUNT) return ERROR_BAD_ARGUMENTS;
|
2021-08-09 11:25:33 +02:00
|
|
|
if (!controllers[index].device) return ERROR_DEVICE_NOT_CONNECTED;
|
2021-08-06 17:08:55 +02:00
|
|
|
|
|
|
|
return ERROR_NOT_SUPPORTED;
|
|
|
|
}
|