From d0fbad17d3a6f11a3aa18d7f425cd5a4395b725d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=B6glinger-Stelzer?= Date: Mon, 14 Sep 2020 13:59:24 +0200 Subject: [PATCH] Reworked native API for extended DS4 report --- sdk/include/ViGEm/Client.h | 10 ++--- sdk/include/ViGEm/Common.h | 54 ++++++++++++++++++++++++ sdk/include/ViGEm/km/BusShared.h | 2 +- sdk/src/ViGEmClient.cpp | 70 ++++++++++++++++---------------- 4 files changed, 94 insertions(+), 42 deletions(-) diff --git a/sdk/include/ViGEm/Client.h b/sdk/include/ViGEm/Client.h index 591d742..95d3ac8 100644 --- a/sdk/include/ViGEm/Client.h +++ b/sdk/include/ViGEm/Client.h @@ -380,15 +380,13 @@ extern "C" { * @author Benjamin "Nefarius" Höglinger-Stelzer * @date 07.09.2020 * - * @param vigem The driver connection object. - * @param target The target device object. - * @param report The report buffer. - * @param reportLength Length of the report buffer. + * @param vigem The driver connection object. + * @param target The target device object. + * @param report The report buffer. * * @returns A VIGEM_ERROR. */ - VIGEM_API VIGEM_ERROR vigem_target_ds4_update_ex(PVIGEM_CLIENT vigem, PVIGEM_TARGET target, PUCHAR report, - ULONG reportLength); + VIGEM_API VIGEM_ERROR vigem_target_ds4_update_ex(PVIGEM_CLIENT vigem, PVIGEM_TARGET target, DS4_REPORT_EX report); /** * Returns the internal index (serial number) the bus driver assigned to the provided diff --git a/sdk/include/ViGEm/Common.h b/sdk/include/ViGEm/Common.h index 3943293..f90b697 100644 --- a/sdk/include/ViGEm/Common.h +++ b/sdk/include/ViGEm/Common.h @@ -200,3 +200,57 @@ VOID FORCEINLINE DS4_REPORT_INIT( DS4_SET_DPAD(Report, DS4_BUTTON_DPAD_NONE); } +#include // pack structs tightly +// +// DualShock 4 HID Touchpad structure +// +typedef struct _DS4_TOUCH +{ + BYTE bPacketCounter; // timestamp / packet counter associated with touch event + BYTE bIsUpTrackingNum1; // 0 means down; active low + // unique to each finger down, so for a lift and repress the value is incremented + BYTE bTouchData1[3]; // Two 12 bits values (for X and Y) + // middle byte holds last 4 bits of X and the starting 4 bits of Y + BYTE bIsUpTrackingNum2; // second touch data immediately follows data of first touch + BYTE bTouchData2[3]; // resolution is 1920x943 +} DS4_TOUCH, * PDS4_TOUCH; + +// +// DualShock 4 v1 complete HID Input report +// +typedef struct _DS4_REPORT_EX +{ + union + { + struct + { + BYTE bThumbLX; + BYTE bThumbLY; + BYTE bThumbRX; + BYTE bThumbRY; + USHORT wButtons; + BYTE bSpecial; + BYTE bTriggerL; + BYTE bTriggerR; + USHORT wTimestamp; + BYTE bBatteryLvl; + SHORT wGyroX; + SHORT wGyroY; + SHORT wGyroZ; + SHORT wAccelX; + SHORT wAccelY; + SHORT wAccelZ; + BYTE _bUnknown1[5]; + BYTE bBatteryLvlSpecial; + // really should have a enum to show everything that this can represent (USB charging, battery level; EXT, headset, microphone connected) + BYTE _bUnknown2[2]; + BYTE bTouchPacketsN; // 0x00 to 0x03 (USB max) + DS4_TOUCH sCurrentTouch; + DS4_TOUCH sPreviousTouch[2]; + } Report; + + UCHAR ReportBuffer[63]; + }; +} DS4_REPORT_EX, *PDS4_REPORT_EX; + +#include diff --git a/sdk/include/ViGEm/km/BusShared.h b/sdk/include/ViGEm/km/BusShared.h index 2e83137..92ee477 100644 --- a/sdk/include/ViGEm/km/BusShared.h +++ b/sdk/include/ViGEm/km/BusShared.h @@ -444,7 +444,7 @@ typedef struct _DS4_SUBMIT_REPORT_EX // // Full size HID report excluding fixed Report ID. // - _In_ UCHAR Report[63]; + _In_ DS4_REPORT_EX Report; } DS4_SUBMIT_REPORT_EX, * PDS4_SUBMIT_REPORT_EX; diff --git a/sdk/src/ViGEmClient.cpp b/sdk/src/ViGEmClient.cpp index 5377adf..1d6f357 100644 --- a/sdk/src/ViGEmClient.cpp +++ b/sdk/src/ViGEmClient.cpp @@ -975,52 +975,52 @@ VIGEM_ERROR vigem_target_ds4_update( return VIGEM_ERROR_NONE; } -VIGEM_ERROR vigem_target_ds4_update_ex(PVIGEM_CLIENT vigem, PVIGEM_TARGET target, PUCHAR report, ULONG reportLength) +VIGEM_ERROR vigem_target_ds4_update_ex(PVIGEM_CLIENT vigem, PVIGEM_TARGET target, DS4_REPORT_EX report) { - if (!vigem) - return VIGEM_ERROR_BUS_INVALID_HANDLE; + if (!vigem) + return VIGEM_ERROR_BUS_INVALID_HANDLE; - if (!target) - return VIGEM_ERROR_INVALID_TARGET; + if (!target) + return VIGEM_ERROR_INVALID_TARGET; - if (vigem->hBusDevice == INVALID_HANDLE_VALUE) - return VIGEM_ERROR_BUS_NOT_FOUND; + if (vigem->hBusDevice == INVALID_HANDLE_VALUE) + return VIGEM_ERROR_BUS_NOT_FOUND; - if (target->SerialNo == 0) - return VIGEM_ERROR_INVALID_TARGET; + if (target->SerialNo == 0) + return VIGEM_ERROR_INVALID_TARGET; - DWORD transferred = 0; - OVERLAPPED lOverlapped = { 0 }; - lOverlapped.hEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr); + DWORD transferred = 0; + OVERLAPPED lOverlapped = {0}; + lOverlapped.hEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr); - DS4_SUBMIT_REPORT_EX dsr; - DS4_SUBMIT_REPORT_EX_INIT(&dsr, target->SerialNo); + DS4_SUBMIT_REPORT_EX dsr; + DS4_SUBMIT_REPORT_EX_INIT(&dsr, target->SerialNo); - memcpy_s(dsr.Report, sizeof(dsr.Report), report, reportLength); + dsr.Report = report; - DeviceIoControl( - vigem->hBusDevice, - IOCTL_DS4_SUBMIT_REPORT, // Same IOCTL, just different size - &dsr, - dsr.Size, - nullptr, - 0, - &transferred, - &lOverlapped - ); + DeviceIoControl( + vigem->hBusDevice, + IOCTL_DS4_SUBMIT_REPORT, // Same IOCTL, just different size + &dsr, + dsr.Size, + nullptr, + 0, + &transferred, + &lOverlapped + ); - if (GetOverlappedResult(vigem->hBusDevice, &lOverlapped, &transferred, TRUE) == 0) - { - if (GetLastError() == ERROR_ACCESS_DENIED) - { - CloseHandle(lOverlapped.hEvent); - return VIGEM_ERROR_INVALID_TARGET; - } - } + if (GetOverlappedResult(vigem->hBusDevice, &lOverlapped, &transferred, TRUE) == 0) + { + if (GetLastError() == ERROR_ACCESS_DENIED) + { + CloseHandle(lOverlapped.hEvent); + return VIGEM_ERROR_INVALID_TARGET; + } + } - CloseHandle(lOverlapped.hEvent); + CloseHandle(lOverlapped.hEvent); - return VIGEM_ERROR_NONE; + return VIGEM_ERROR_NONE; } ULONG vigem_target_get_index(PVIGEM_TARGET target)