xinput: Detect and setup HID gamepads.

Signed-off-by: Aric Stewart <aric@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Aric Stewart 2018-02-14 12:40:59 -06:00 committed by Alexandre Julliard
parent e48296c2f0
commit 755422a2be
8 changed files with 387 additions and 11 deletions

View File

@ -1,7 +1,9 @@
MODULE = xinput1_1.dll
PARENTSRC = ../xinput1_3
DELAYIMPORTS = hid setupapi
C_SRCS = \
hid.c \
xinput_main.c
RC_SRCS = version.rc

View File

@ -1,7 +1,9 @@
MODULE = xinput1_2.dll
PARENTSRC = ../xinput1_3
DELAYIMPORTS = hid setupapi
C_SRCS = \
hid.c \
xinput_main.c
RC_SRCS = version.rc

View File

@ -1,7 +1,9 @@
MODULE = xinput1_3.dll
IMPORTLIB = xinput
DELAYIMPORTS = hid setupapi
C_SRCS = \
hid.c \
xinput_main.c
RC_SRCS = version.rc

331
dlls/xinput1_3/hid.c Normal file
View File

@ -0,0 +1,331 @@
/*
* The Wine project - Xinput Joystick HID interface
* Copyright 2018 Aric Stewart
*
* 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 "config.h"
#include "wine/port.h"
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include "wine/debug.h"
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "winerror.h"
#include "winreg.h"
#include "wingdi.h"
#include "winnls.h"
#include "winternl.h"
#include "setupapi.h"
#include "devpkey.h"
#include "hidusage.h"
#include "ddk/hidsdi.h"
#include "initguid.h"
#include "devguid.h"
#include "xinput.h"
#include "xinput_private.h"
#include "wine/unicode.h"
WINE_DEFAULT_DEBUG_CHANNEL(xinput);
#define XINPUT_GAMEPAD_GUIDE 0x0400
static CRITICAL_SECTION hid_xinput_crit;
static CRITICAL_SECTION_DEBUG hid_critsect_debug =
{
0, 0, &hid_xinput_crit,
{ &hid_critsect_debug.ProcessLocksList, &hid_critsect_debug.ProcessLocksList },
0, 0, { (DWORD_PTR)(__FILE__ ": hid_xinput_crit") }
};
static CRITICAL_SECTION hid_xinput_crit = { &hid_critsect_debug, -1, 0, 0, 0, 0 };
struct hid_platform_private {
PHIDP_PREPARSED_DATA ppd;
HANDLE device;
WCHAR *device_path;
CRITICAL_SECTION crit;
CHAR *last_report;
CHAR *current_report;
DWORD report_length;
LONG ThumbLXRange[3];
LONG ThumbLYRange[3];
LONG LeftTriggerRange[3];
LONG ThumbRXRange[3];
LONG ThumbRYRange[3];
LONG RightTriggerRange[3];
};
static DWORD last_check = 0;
static void MarkUsage(struct hid_platform_private *private, WORD usage, LONG min, LONG max, USHORT bits)
{
switch (usage)
{
case HID_USAGE_GENERIC_X:
private->ThumbLXRange[0] = min;
private->ThumbLXRange[1] = bits;
private->ThumbLXRange[2] = max - min;
break;
case HID_USAGE_GENERIC_Y:
private->ThumbLYRange[0] = min;
private->ThumbLYRange[1] = bits;
private->ThumbLYRange[2] = max - min;
break;
case HID_USAGE_GENERIC_Z:
private->LeftTriggerRange[0] = min;
private->LeftTriggerRange[1] = bits;
private->LeftTriggerRange[2] = max - min;
break;
case HID_USAGE_GENERIC_RX:
private->ThumbRXRange[0] = min;
private->ThumbRXRange[1] = bits;
private->ThumbRXRange[2] = max - min;
break;
case HID_USAGE_GENERIC_RY:
private->ThumbRYRange[0] = min;
private->ThumbRYRange[1] = bits;
private->ThumbRYRange[2] = max - min;
break;
case HID_USAGE_GENERIC_RZ:
private->RightTriggerRange[0] = min;
private->RightTriggerRange[1] = bits;
private->RightTriggerRange[2] = max - min;
break;
}
}
static BOOL VerifyGamepad(PHIDP_PREPARSED_DATA ppd, XINPUT_CAPABILITIES *xinput_caps, struct hid_platform_private *private, HIDP_CAPS *caps)
{
HIDP_BUTTON_CAPS *button_caps;
HIDP_VALUE_CAPS *value_caps;
int i;
int button_count = 0;
USHORT button_caps_count = 0;
USHORT value_caps_count = 0;
/* Count buttons */
memset(xinput_caps, 0, sizeof(XINPUT_CAPABILITIES));
button_caps_count = caps->NumberInputButtonCaps;
button_caps = HeapAlloc(GetProcessHeap(), 0, sizeof(*button_caps) * button_caps_count);
HidP_GetButtonCaps(HidP_Input, button_caps, &button_caps_count, ppd);
for (i = 0; i < button_caps_count; i++)
{
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);
}
HeapFree(GetProcessHeap(), 0, button_caps);
if (button_count < 14)
WARN("Too few buttons, Continue\n");
xinput_caps->Gamepad.wButtons = 0xffff;
value_caps_count = caps->NumberInputValueCaps;
value_caps = HeapAlloc(GetProcessHeap(), 0, sizeof(*value_caps) * value_caps_count);
HidP_GetValueCaps(HidP_Input, value_caps, &value_caps_count, ppd);
for (i = 0; i < value_caps_count; 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);
}
HeapFree(GetProcessHeap(), 0, value_caps);
if (private->LeftTriggerRange[1])
xinput_caps->Gamepad.bLeftTrigger = (1u << (sizeof(xinput_caps->Gamepad.bLeftTrigger) + 1)) - 1;
else
WARN("Missing axis LeftTrigger\n");
if (private->RightTriggerRange[1])
xinput_caps->Gamepad.bRightTrigger = (1u << (sizeof(xinput_caps->Gamepad.bRightTrigger) + 1)) - 1;
else
WARN("Missing axis RightTrigger\n");
if (private->ThumbLXRange[1])
xinput_caps->Gamepad.sThumbLX = (1u << (sizeof(xinput_caps->Gamepad.sThumbLX) + 1)) - 1;
else
WARN("Missing axis ThumbLX\n");
if (private->ThumbLYRange[1])
xinput_caps->Gamepad.sThumbLY = (1u << (sizeof(xinput_caps->Gamepad.sThumbLY) + 1)) - 1;
else
WARN("Missing axis ThumbLY\n");
if (private->ThumbRXRange[1])
xinput_caps->Gamepad.sThumbRX = (1u << (sizeof(xinput_caps->Gamepad.sThumbRX) + 1)) - 1;
else
WARN("Missing axis ThumbRX\n");
if (private->ThumbRYRange[1])
xinput_caps->Gamepad.sThumbRY = (1u << (sizeof(xinput_caps->Gamepad.sThumbRY) + 1)) - 1;
else
WARN("Missing axis ThumbRY\n");
xinput_caps->Type = XINPUT_DEVTYPE_GAMEPAD;
xinput_caps->SubType = XINPUT_DEVSUBTYPE_GAMEPAD;
return TRUE;
}
static void build_private(struct hid_platform_private *private, PHIDP_PREPARSED_DATA ppd, HIDP_CAPS *caps, HANDLE device, WCHAR *path)
{
size_t size;
private->ppd = ppd;
private->device = device;
private->report_length = caps->InputReportByteLength + 1;
private->current_report = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, private->report_length);
private->last_report = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, private->report_length);
size = (strlenW(path) + 1) * sizeof(WCHAR);
private->device_path = HeapAlloc(GetProcessHeap(), 0, size);
memcpy(private->device_path, path, size);
InitializeCriticalSection(&private->crit);
private->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
}
void HID_find_gamepads(xinput_controller *devices)
{
HDEVINFO device_info_set;
GUID hid_guid;
SP_DEVICE_INTERFACE_DATA interface_data;
SP_DEVICE_INTERFACE_DETAIL_DATA_W *data;
PHIDP_PREPARSED_DATA ppd;
DWORD detail_size = MAX_PATH * sizeof(WCHAR);
HANDLE device = INVALID_HANDLE_VALUE;
HIDP_CAPS Caps;
DWORD idx,didx;
int i;
idx = GetTickCount();
if ((idx - last_check) < 2000)
return;
last_check = idx;
HidD_GetHidGuid(&hid_guid);
EnterCriticalSection(&hid_xinput_crit);
device_info_set = SetupDiGetClassDevsW(&hid_guid, NULL, NULL, DIGCF_DEVICEINTERFACE);
data = HeapAlloc(GetProcessHeap(), 0 , sizeof(*data) + detail_size);
data->cbSize = sizeof(*data);
ZeroMemory(&interface_data, sizeof(interface_data));
interface_data.cbSize = sizeof(interface_data);
idx = didx = 0;
while (SetupDiEnumDeviceInterfaces(device_info_set, NULL, &hid_guid, idx++,
&interface_data) && didx < XUSER_MAX_COUNT)
{
static const WCHAR ig[] = {'I','G','_',0};
if (!SetupDiGetDeviceInterfaceDetailW(device_info_set,
&interface_data, data, sizeof(*data) + detail_size, NULL, NULL))
continue;
if (!strstrW(data->DevicePath, ig))
continue;
for (i = 0; i < XUSER_MAX_COUNT; i++)
{
struct hid_platform_private *private = devices[i].platform_private;
if (devices[i].connected && !strcmpW(data->DevicePath, private->device_path))
break;
}
if (i != XUSER_MAX_COUNT)
continue;
device = CreateFileW(data->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
if (device == INVALID_HANDLE_VALUE)
continue;
HidD_GetPreparsedData(device, &ppd);
HidP_GetCaps(ppd, &Caps);
if (Caps.UsagePage == HID_USAGE_PAGE_GENERIC &&
(Caps.Usage == HID_USAGE_GENERIC_GAMEPAD ||
Caps.Usage == HID_USAGE_GENERIC_JOYSTICK ||
Caps.Usage == 0x8 /* Multi-axis Controller */))
{
struct hid_platform_private *private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct hid_platform_private));
if (VerifyGamepad(ppd, &(devices[didx].caps), private, &Caps))
{
TRACE("Found gamepad %i %s\n",didx, debugstr_w(data->DevicePath));
devices[didx].connected = TRUE;
build_private(private, ppd, &Caps, device, data->DevicePath);
devices[didx].platform_private = private;
didx++;
}
else
{
CloseHandle(device);
HidD_FreePreparsedData(ppd);
HeapFree(GetProcessHeap(), 0, private);
}
}
else
{
CloseHandle(device);
HidD_FreePreparsedData(ppd);
}
device = INVALID_HANDLE_VALUE;
}
HeapFree(GetProcessHeap(), 0, data);
SetupDiDestroyDeviceInfoList(device_info_set);
LeaveCriticalSection(&hid_xinput_crit);
return;
}
static void remove_gamepad(xinput_controller *device)
{
if (device->connected)
{
struct hid_platform_private *private = device->platform_private;
EnterCriticalSection(&private->crit);
CloseHandle(private->device);
HeapFree(GetProcessHeap(), 0, private->current_report);
HeapFree(GetProcessHeap(), 0, private->last_report);
HeapFree(GetProcessHeap(), 0, private->device_path);
HidD_FreePreparsedData(private->ppd);
device->platform_private = NULL;
device->connected = FALSE;
LeaveCriticalSection(&private->crit);
DeleteCriticalSection(&private->crit);
HeapFree(GetProcessHeap(), 0, private);
}
}
void HID_destroy_gamepads(xinput_controller *devices)
{
int i;
EnterCriticalSection(&hid_xinput_crit);
for (i = 0; i < XUSER_MAX_COUNT; i++)
remove_gamepad(&devices[i]);
LeaveCriticalSection(&hid_xinput_crit);
}

View File

@ -28,24 +28,26 @@
#include "winerror.h"
#include "xinput.h"
#include "xinput_private.h"
/* Not defined in the headers, used only by XInputGetStateEx */
#define XINPUT_GAMEPAD_GUIDE 0x0400
WINE_DEFAULT_DEBUG_CHANNEL(xinput);
struct
{
BOOL connected;
} controllers[XUSER_MAX_COUNT];
xinput_controller controllers[XUSER_MAX_COUNT];
BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved)
{
switch(reason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(inst);
break;
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(inst);
break;
case DLL_PROCESS_DETACH:
if (reserved) break;
HID_destroy_gamepads(controllers);
break;
}
return TRUE;
}
@ -121,17 +123,20 @@ DWORD WINAPI XInputGetKeystroke(DWORD index, DWORD reserved, PXINPUT_KEYSTROKE k
DWORD WINAPI XInputGetCapabilities(DWORD index, DWORD flags, XINPUT_CAPABILITIES* capabilities)
{
static int warn_once;
TRACE("(index %u, flags 0x%x, capabilities %p)\n", index, flags, capabilities);
if (!warn_once++)
FIXME("(index %u, flags 0x%x, capabilities %p) Stub!\n", index, flags, capabilities);
HID_find_gamepads(controllers);
if (index >= XUSER_MAX_COUNT)
return ERROR_BAD_ARGUMENTS;
if (!controllers[index].connected)
return ERROR_DEVICE_NOT_CONNECTED;
if (flags & XINPUT_FLAG_GAMEPAD && controllers[index].caps.SubType != XINPUT_DEVSUBTYPE_GAMEPAD)
return ERROR_DEVICE_NOT_CONNECTED;
return ERROR_NOT_SUPPORTED;
memcpy(capabilities, &controllers[index].caps, sizeof(*capabilities));
return ERROR_SUCCESS;
}
DWORD WINAPI XInputGetDSoundAudioDeviceGuids(DWORD index, GUID* render_guid, GUID* capture_guid)

View File

@ -0,0 +1,30 @@
/*
* The Wine project - Xinput Joystick Library
* Copyright 2018 Aric Stewart
*
* 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
*/
typedef struct _xinput_controller
{
BOOL connected;
XINPUT_CAPABILITIES caps;
void *platform_private;
XINPUT_STATE state;
} xinput_controller;
void HID_find_gamepads(xinput_controller *devices) DECLSPEC_HIDDEN;
void HID_destroy_gamepads(xinput_controller *devices) DECLSPEC_HIDDEN;

View File

@ -1,7 +1,9 @@
MODULE = xinput1_4.dll
PARENTSRC = ../xinput1_3
DELAYIMPORTS = hid setupapi
C_SRCS = \
hid.c \
xinput_main.c
RC_SRCS = version.rc

View File

@ -1,7 +1,9 @@
MODULE = xinput9_1_0.dll
PARENTSRC = ../xinput1_3
DELAYIMPORTS = hid setupapi
C_SRCS = \
hid.c \
xinput_main.c
RC_SRCS = version.rc