|
| 1 | +#define WIN32_LEAN_AND_MEAN |
| 2 | +#include <Windows.h> |
| 3 | +#include <shellapi.h> |
| 4 | +#include <winioctl.h> |
| 5 | +#include <hidsdi.h> |
| 6 | + |
| 7 | +#include "hook_mgr.hpp" |
| 8 | +#include "plugin.hpp" |
| 9 | +#include "game_addrs.hpp" |
| 10 | + |
| 11 | +// Hooks into XINPUT1_4's DeviceIoControl via undocumented DriverHook(0xBAAD0001) call |
| 12 | +// Allows us to detect SET_GAMEPAD_STATE ioctl and send the GIP HID command for trigger impulses |
| 13 | +// Hopefully will work for most series controller connection types... |
| 14 | + |
| 15 | +// Defs from OpenXInput, GIP command code from X1nput |
| 16 | +constexpr DWORD IOCTL_XINPUT_BASE = 0x8000; |
| 17 | +static DWORD IOCTL_XINPUT_SET_GAMEPAD_STATE = CTL_CODE(IOCTL_XINPUT_BASE, 0x804, METHOD_BUFFERED, FILE_WRITE_ACCESS); // 0x8000A010 |
| 18 | +struct InSetState_t |
| 19 | +{ |
| 20 | + BYTE deviceIndex; |
| 21 | + BYTE ledState; |
| 22 | + BYTE leftMotorSpeed; |
| 23 | + BYTE rightMotorSpeed; |
| 24 | + BYTE flags; |
| 25 | +}; |
| 26 | + |
| 27 | +#define XUSB_SET_STATE_FLAG_VIBRATION ((BYTE)0x02) |
| 28 | + |
| 29 | +class ImpulseVibration : public Hook |
| 30 | +{ |
| 31 | +#define MAX_STR 255 |
| 32 | + inline static wchar_t wstr[MAX_STR]; |
| 33 | + |
| 34 | + static BOOL WINAPI DetourDeviceIoControl( |
| 35 | + HANDLE hDevice, |
| 36 | + DWORD dwIoControlCode, |
| 37 | + LPVOID lpInBuffer, |
| 38 | + DWORD nInBufferSize, |
| 39 | + LPVOID lpOutBuffer, |
| 40 | + DWORD nOutBufferSize, |
| 41 | + LPDWORD lpBytesReturned, |
| 42 | + LPOVERLAPPED lpOverlapped |
| 43 | + ) |
| 44 | + { |
| 45 | + auto ret = DeviceIoControl(hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, lpBytesReturned, lpOverlapped); |
| 46 | + if (dwIoControlCode != IOCTL_XINPUT_SET_GAMEPAD_STATE) |
| 47 | + return ret; |
| 48 | + |
| 49 | + // Don't send GIP command to x360, may cause issues with some bad third-party ones? |
| 50 | + if (HidD_GetProductString(hDevice, wstr, MAX_STR) && wcsstr(wstr, L"360")) |
| 51 | + return ret; |
| 52 | + |
| 53 | + if (!Settings::ImpulseVibrationMode) |
| 54 | + return ret; // how did we get here? |
| 55 | + |
| 56 | + InSetState_t* inData = (InSetState_t*)lpInBuffer; |
| 57 | + if ((inData->flags & XUSB_SET_STATE_FLAG_VIBRATION) != 0) |
| 58 | + { |
| 59 | + float leftTriggerInput = float(inData->leftMotorSpeed); |
| 60 | + float rightTriggerInput = float(inData->rightMotorSpeed); |
| 61 | + |
| 62 | + if (Settings::ImpulseVibrationMode == 2) // Swap L/R |
| 63 | + { |
| 64 | + leftTriggerInput = float(inData->rightMotorSpeed); |
| 65 | + rightTriggerInput = float(inData->leftMotorSpeed); |
| 66 | + } |
| 67 | + else if (Settings::ImpulseVibrationMode == 3) // Merge L/R by using whichever is highest |
| 68 | + { |
| 69 | + leftTriggerInput = rightTriggerInput = max(leftTriggerInput, rightTriggerInput); |
| 70 | + } |
| 71 | + |
| 72 | + uint8_t buf[9] = { 0 }; |
| 73 | + buf[0] = 0x03; // HID report ID (3 for bluetooth, any for USB) |
| 74 | + buf[1] = 0x0F; // Motor flag mask(?) |
| 75 | + buf[2] = uint8_t(leftTriggerInput * Settings::ImpulseVibrationLeftMultiplier); // Left trigger impulse |
| 76 | + buf[3] = uint8_t(rightTriggerInput * Settings::ImpulseVibrationRightMultiplier); // Right trigger impulse |
| 77 | + buf[4] = inData->leftMotorSpeed; // Left rumble |
| 78 | + buf[5] = inData->rightMotorSpeed; // Right rumble |
| 79 | + // "Pulse" |
| 80 | + buf[6] = 0xFF; // On time |
| 81 | + buf[7] = 0x00; // Off time |
| 82 | + buf[8] = 0xFF; // Number of repeats |
| 83 | + WriteFile(hDevice, buf, 9, lpBytesReturned, lpOverlapped); |
| 84 | + } |
| 85 | + |
| 86 | + return ret; |
| 87 | + } |
| 88 | + |
| 89 | +public: |
| 90 | + std::string_view description() override |
| 91 | + { |
| 92 | + return "ImpulseVibration"; |
| 93 | + } |
| 94 | + |
| 95 | + bool validate() override |
| 96 | + { |
| 97 | + return Settings::ImpulseVibrationMode != 0 && Settings::VibrationMode != 0; |
| 98 | + } |
| 99 | + |
| 100 | + bool apply() override |
| 101 | + { |
| 102 | + auto xinput = LoadLibraryA("xinput1_4.dll"); |
| 103 | + if (!xinput) |
| 104 | + return false; |
| 105 | + |
| 106 | + typedef BOOL(__stdcall* DllMain_fn)(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved); |
| 107 | + auto dllmain = (DllMain_fn)GetProcAddress(xinput, "DllMain"); |
| 108 | + if (!dllmain) |
| 109 | + return false; |
| 110 | + |
| 111 | + dllmain(nullptr, 0xBAAD0001, DetourDeviceIoControl); |
| 112 | + |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + static ImpulseVibration instance; |
| 117 | +}; |
| 118 | +ImpulseVibration ImpulseVibration::instance; |
0 commit comments