Aric Stewart : xinput: Update state of HID device.

Alexandre Julliard julliard at winehq.org
Thu Feb 15 14:42:28 CST 2018


Module: wine
Branch: master
Commit: 9e321a6f96714111c51466303caed89c9fabacf0
URL:    https://source.winehq.org/git/wine.git/?a=commit;h=9e321a6f96714111c51466303caed89c9fabacf0

Author: Aric Stewart <aric at codeweavers.com>
Date:   Wed Feb 14 12:41:33 2018 -0600

xinput: Update state of HID device.

Signed-off-by: Aric Stewart <aric at codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 dlls/xinput1_3/hid.c            | 94 ++++++++++++++++++++++++++++++++++++++---
 dlls/xinput1_3/xinput_main.c    | 14 +++---
 dlls/xinput1_3/xinput_private.h |  1 +
 3 files changed, 96 insertions(+), 13 deletions(-)

diff --git a/dlls/xinput1_3/hid.c b/dlls/xinput1_3/hid.c
index 32a7ef5..5d82f05 100644
--- a/dlls/xinput1_3/hid.c
+++ b/dlls/xinput1_3/hid.c
@@ -66,9 +66,9 @@ struct hid_platform_private {
 
     CRITICAL_SECTION crit;
 
-    CHAR *last_report;
-    CHAR *current_report;
     DWORD report_length;
+    BYTE current_report;
+    CHAR *reports[2];
 
     LONG ThumbLXRange[3];
     LONG ThumbLYRange[3];
@@ -201,8 +201,9 @@ static void build_private(struct hid_platform_private *private, PHIDP_PREPARSED_
     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);
+    private->current_report = 0;
+    private->reports[0] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, private->report_length);
+    private->reports[1] = 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);
@@ -309,8 +310,8 @@ static void remove_gamepad(xinput_controller *device)
 
         EnterCriticalSection(&private->crit);
         CloseHandle(private->device);
-        HeapFree(GetProcessHeap(), 0, private->current_report);
-        HeapFree(GetProcessHeap(), 0, private->last_report);
+        HeapFree(GetProcessHeap(), 0, private->reports[0]);
+        HeapFree(GetProcessHeap(), 0, private->reports[1]);
         HeapFree(GetProcessHeap(), 0, private->device_path);
         HidD_FreePreparsedData(private->ppd);
         device->platform_private = NULL;
@@ -329,3 +330,84 @@ void HID_destroy_gamepads(xinput_controller *devices)
         remove_gamepad(&devices[i]);
     LeaveCriticalSection(&hid_xinput_crit);
 }
+
+#define SIGN(v,b) ((b==8)?(BYTE)v:(b==16)?(SHORT)v:(INT)v)
+#define SCALE_SHORT(v,r) (SHORT)((((0xffff)*(SIGN(v,r[1]) - r[0]))/r[2])-32767)
+#define SCALE_BYTE(v,r) (BYTE)((((0xff)*(SIGN(v,r[1]) - r[0]))/r[2]))
+
+void HID_update_state(xinput_controller* device)
+{
+    struct hid_platform_private *private = device->platform_private;
+    int i;
+    CHAR *report = private->reports[(private->current_report)%2];
+    CHAR *target_report = private->reports[(private->current_report+1)%2];
+
+    USAGE buttons[15];
+    ULONG button_length;
+    ULONG value;
+
+    EnterCriticalSection(&private->crit);
+    if (!HidD_GetInputReport(private->device, target_report, private->report_length))
+    {
+        if (GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == ERROR_INVALID_HANDLE)
+            remove_gamepad(device);
+        else
+            ERR("Failed to get Input Report (%x)\n", GetLastError());
+        LeaveCriticalSection(&private->crit);
+        return;
+    }
+    if (memcmp(report, target_report, private->report_length) == 0)
+    {
+        LeaveCriticalSection(&private->crit);
+        return;
+    }
+
+    private->current_report = (private->current_report+1)%2;
+
+    device->state.dwPacketNumber++;
+    button_length = 15;
+    HidP_GetUsages(HidP_Input, HID_USAGE_PAGE_BUTTON, 0, buttons, &button_length, private->ppd, target_report, private->report_length);
+
+    device->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_LEFT_THUMB; break;
+            case 8: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_THUMB; break;
+
+            case 9: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_START; break;
+            case 10: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_BACK; break;
+            case 11: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_GUIDE; break;
+            case 12: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP; break;
+            case 13: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN; break;
+            case 14: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT; break;
+            case 15: device->state.Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT; break;
+        }
+    }
+
+    HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_X, &value, private->ppd, target_report, private->report_length);
+    device->state.Gamepad.sThumbLX = SCALE_SHORT(value, private->ThumbLXRange);
+
+    HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_Y, &value, private->ppd, target_report, private->report_length);
+    device->state.Gamepad.sThumbLY = -SCALE_SHORT(value, private->ThumbLYRange);
+
+    HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RX, &value, private->ppd, target_report, private->report_length);
+    device->state.Gamepad.sThumbRX = SCALE_SHORT(value, private->ThumbRXRange);
+
+    HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RY, &value, private->ppd, target_report, private->report_length);
+    device->state.Gamepad.sThumbRY = -SCALE_SHORT(value, private->ThumbRYRange);
+
+    HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_RZ, &value, private->ppd, target_report, private->report_length);
+    device->state.Gamepad.bRightTrigger = SCALE_BYTE(value, private->RightTriggerRange);
+
+    HidP_GetUsageValue(HidP_Input, HID_USAGE_PAGE_GENERIC, 0, HID_USAGE_GENERIC_Z, &value, private->ppd, target_report, private->report_length);
+    device->state.Gamepad.bLeftTrigger = SCALE_BYTE(value, private->LeftTriggerRange);
+    LeaveCriticalSection(&private->crit);
+}
diff --git a/dlls/xinput1_3/xinput_main.c b/dlls/xinput1_3/xinput_main.c
index a796688..83ecb71 100644
--- a/dlls/xinput1_3/xinput_main.c
+++ b/dlls/xinput1_3/xinput_main.c
@@ -76,10 +76,8 @@ DWORD WINAPI XInputSetState(DWORD index, XINPUT_VIBRATION* vibration)
 DWORD WINAPI DECLSPEC_HOTPATCH XInputGetState(DWORD index, XINPUT_STATE* state)
 {
     DWORD ret;
-    static int warn_once;
 
-    if (!warn_once++)
-        FIXME("(index %u, state %p) Stub!\n", index, state);
+    TRACE("(index %u, state %p)!\n", index, state);
 
     ret = XInputGetStateEx(index, state);
     if (ret != ERROR_SUCCESS)
@@ -93,17 +91,19 @@ DWORD WINAPI DECLSPEC_HOTPATCH XInputGetState(DWORD index, XINPUT_STATE* state)
 
 DWORD WINAPI DECLSPEC_HOTPATCH XInputGetStateEx(DWORD index, XINPUT_STATE* state)
 {
-    static int warn_once;
+    TRACE("(index %u, state %p)!\n", index, state);
 
-    if (!warn_once++)
-        FIXME("(index %u, state %p) Stub!\n", index, state);
+    HID_find_gamepads(controllers);
 
     if (index >= XUSER_MAX_COUNT)
         return ERROR_BAD_ARGUMENTS;
     if (!controllers[index].connected)
         return ERROR_DEVICE_NOT_CONNECTED;
 
-    return ERROR_NOT_SUPPORTED;
+    HID_update_state(&controllers[index]);
+    memcpy(state, &controllers[index].state, sizeof(XINPUT_STATE));
+
+    return ERROR_SUCCESS;
 }
 
 DWORD WINAPI XInputGetKeystroke(DWORD index, DWORD reserved, PXINPUT_KEYSTROKE keystroke)
diff --git a/dlls/xinput1_3/xinput_private.h b/dlls/xinput1_3/xinput_private.h
index 35414ac..67ddbe4 100644
--- a/dlls/xinput1_3/xinput_private.h
+++ b/dlls/xinput1_3/xinput_private.h
@@ -28,3 +28,4 @@ typedef struct _xinput_controller
 
 void HID_find_gamepads(xinput_controller *devices) DECLSPEC_HIDDEN;
 void HID_destroy_gamepads(xinput_controller *devices) DECLSPEC_HIDDEN;
+void HID_update_state(xinput_controller* device) DECLSPEC_HIDDEN;




More information about the wine-cvs mailing list