diff --git a/SteamTarget/VirtualControllerThread.cpp b/SteamTarget/VirtualControllerThread.cpp index 5d5e7bd..2e68278 100644 --- a/SteamTarget/VirtualControllerThread.cpp +++ b/SteamTarget/VirtualControllerThread.cpp @@ -14,13 +14,10 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "VirtualControllerThread.h" - -ULONG VirtualControllerThread::ulTargetSerials[XUSER_MAX_COUNT]; +// VirtualControllerThread::VirtualControllerThread() { - resetControllers(); - if (!VIGEM_SUCCESS(vigem_init())) { std::cout << "Error initializing ViGem!" << std::endl; @@ -28,14 +25,11 @@ VirtualControllerThread::VirtualControllerThread() bShouldRun = false; } - VIGEM_TARGET vtX360[XUSER_MAX_COUNT]; for (int i = 0; i < XUSER_MAX_COUNT; i++) { VIGEM_TARGET_INIT(&vtX360[i]); - VirtualControllerThread::ulTargetSerials[i] = NULL; } - } @@ -60,10 +54,6 @@ void VirtualControllerThread::stop() } } -void VirtualControllerThread::resetControllers() -{ - iRealControllers = getRealControllers(); -} bool VirtualControllerThread::isRunning() { @@ -73,82 +63,97 @@ bool VirtualControllerThread::isRunning() void VirtualControllerThread::controllerLoop() { DWORD result; - sf::Clock reCheckControllerTimer; - int i, j; + DWORD result2; + sf::Clock waitForHookTimer; while (bShouldRun) { sfClock.restart(); - if (reCheckControllerTimer.getElapsedTime().asSeconds() >= 1.f) + // We have to retrieve the XInputGetState function by loading it via GetProcAdress + // otherwise the M$ compiler calls to a jumptable, jumping to the real function + // We can't have this if we wan't to dynamically unpatch and repatch Valve's XInput hook + // Also wait a second, jut to be sure Steam has done it's hooking thing... + if (XGetState == nullptr && waitForHookTimer.getElapsedTime().asSeconds() > 1) { - iTotalControllers = 0; - for (i = 0; i < XUSER_MAX_COUNT; i++) - { - ZeroMemory(&xsState[i], sizeof(XINPUT_STATE)); + HMODULE xinputmod = nullptr; - result = XInputGetState(i, &xsState[i]); + HANDLE hProcess = GetCurrentProcess(); + HMODULE hMods[1024]; + DWORD cbNeeded; + EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded); + for (int i = 0; i < (cbNeeded / sizeof(HMODULE)); i++) + { + TCHAR szModName[MAX_PATH]; - if (result == ERROR_SUCCESS) + if (GetModuleBaseName(hProcess, hMods[i], szModName, + sizeof(szModName) / sizeof(TCHAR))) { - iTotalControllers++; - } - else { - break; + std::wstring name(&szModName[0]); + auto& f = std::use_facet>(std::locale()); + f.tolower(&name[0], &name[0] + name.size()); + if (name.find(std::wstring(L"xinput")) != std::wstring::npos) + { + xinputmod = hMods[i]; + break; + } } } - iTotalControllers -= iVirtualControllers; - reCheckControllerTimer.restart(); - } - - for (i = iRealControllers; i < iTotalControllers && i < XUSER_MAX_COUNT; i++) - { - //////// - ZeroMemory(&xsState[i], sizeof(XINPUT_STATE)); - - result = XInputGetState(i, &xsState[i]); + XInputGetState_t realXgstate = reinterpret_cast(GetProcAddress(xinputmod, "XInputGetState")); - if (result == ERROR_SUCCESS) + std::cout << "realXgstate: " << std::hex << realXgstate << "\n"; + for (int i = 0; i < 5; i++) { + valveHookBytes[i] = *reinterpret_cast(reinterpret_cast(*realXgstate) + i); + } - if (VIGEM_SUCCESS(vigem_target_plugin(Xbox360Wired, &vtX360[i]))) - { - iVirtualControllers++; - - std::cout << "Plugged in controller " << vtX360[i].SerialNo << std::endl; - - VirtualControllerThread::ulTargetSerials[i] = vtX360[i].SerialNo; - - vigem_register_xusb_notification(reinterpret_cast(&VirtualControllerThread::controllerCallback), vtX360[i]); - } - - RtlCopyMemory(&xrReport[i], &xsState[i].Gamepad, sizeof(XUSB_REPORT)); + XGetState = realXgstate; + controllerCount = 1; + } - vigem_xusb_submit_report(vtX360[i], xrReport[i]); - } - else + if (XGetState != nullptr) + { + for (int i = 0; i < XUSER_MAX_COUNT; i++) { - if (VIGEM_SUCCESS(vigem_target_unplug(&vtX360[i]))) + //////// + XINPUT_STATE state = { 0 }; + result = XInputGetStateWrapper(i, &state); + XINPUT_STATE state2 = { 0 }; + result2 = callRealXinputGetState(i, &state2); + + if (result == ERROR_SUCCESS) { - iVirtualControllers--; - iTotalControllers = 0; - for (j = 0; j < XUSER_MAX_COUNT; j++) + if (result2 != ERROR_SUCCESS) { - ZeroMemory(&xsState[j], sizeof(XINPUT_STATE)); - - result = XInputGetState(j, &xsState[j]); - - if (result == ERROR_SUCCESS) + // By using VID and PID of Valve's SteamController, Steam doesn't give us ANOTHER "fake" XInput device + // Leading to endless pain and suffering. + // Or really, leading to pluggin in one virtual controller after another and mirroring inputs + // Also annoying the shit out of the user when they open the overlay as steam prompts to setup new XInput devices + // Also avoiding any fake inputs from Valve's default controllerprofile + // -> Leading to endless pain and suffering + vigem_target_set_vid(&vtX360[i], 0x28de); //Valve SteamController VID + vigem_target_set_pid(&vtX360[i], 0x1102); //Valve SteamController PID + + int vigem_res = vigem_target_plugin(Xbox360Wired, &vtX360[i]); + if (vigem_res == VIGEM_ERROR_TARGET_UNINITIALIZED) { - iTotalControllers++; + VIGEM_TARGET_INIT(&vtX360[i]); } - else { - break; + if (vigem_res == VIGEM_ERROR_NONE) + { + std::cout << "Plugged in controller " << vtX360[i].SerialNo << std::endl; + vigem_register_xusb_notification(reinterpret_cast(&VirtualControllerThread::controllerCallback), vtX360[i]); } } - iTotalControllers -= iVirtualControllers; - std::cout << "Unplugged controller " << vtX360[i].SerialNo << std::endl; - VirtualControllerThread::ulTargetSerials[i] = NULL; + + vigem_xusb_submit_report(vtX360[i], *reinterpret_cast(&state.Gamepad)); + } + else + { + if (VIGEM_SUCCESS(vigem_target_unplug(&vtX360[i]))) + { + std::cout << "Unplugged controller " << vtX360[i].SerialNo << std::endl; + } } } } @@ -158,35 +163,8 @@ void VirtualControllerThread::controllerLoop() { std::this_thread::sleep_for(std::chrono::microseconds(delay - tickTime)); } - } -} - -int VirtualControllerThread::getRealControllers() -{ - int realControllers = 0; - UINT numDevices = NULL; - - GetRawInputDeviceList(NULL, &numDevices, sizeof(RAWINPUTDEVICELIST)); - - PRAWINPUTDEVICELIST rawInputDeviceList; - rawInputDeviceList = (PRAWINPUTDEVICELIST)malloc(sizeof(RAWINPUTDEVICELIST) * numDevices); - GetRawInputDeviceList(rawInputDeviceList, &numDevices, sizeof(RAWINPUTDEVICELIST)); - - for (unsigned int i = 0; i < numDevices; i++) - { - RID_DEVICE_INFO devInfo; - devInfo.cbSize = sizeof(RID_DEVICE_INFO); - GetRawInputDeviceInfo(rawInputDeviceList[i].hDevice, RIDI_DEVICEINFO, &devInfo, (PUINT)&devInfo.cbSize); - if (devInfo.hid.dwVendorId == 0x45e && devInfo.hid.dwProductId == 0x28e) - { - realControllers++; - } } - - free(rawInputDeviceList); - std::cout << "Detected " << realControllers << " real connected X360 Controllers" << std::endl; - return realControllers; } void VirtualControllerThread::controllerCallback(VIGEM_TARGET Target, UCHAR LargeMotor, UCHAR SmallMotor, UCHAR LedNumber) @@ -194,14 +172,37 @@ void VirtualControllerThread::controllerCallback(VIGEM_TARGET Target, UCHAR Larg XINPUT_VIBRATION vibration; ZeroMemory(&vibration, sizeof(XINPUT_VIBRATION)); vibration.wLeftMotorSpeed = LargeMotor * 0xff; //Controllers only use 1 byte, XInput-API uses two, ViGEm also only uses one, like the hardware does, so we have to multiply - vibration.wRightMotorSpeed = SmallMotor * 0xff; + vibration.wRightMotorSpeed = SmallMotor * 0xff; //Yeah yeah I do know about bitshifting and the multiplication not being 100% correct... + XInputSetState(Target.SerialNo-1, &vibration); +} - for (int i = 0; i < XUSER_MAX_COUNT; i++) +DWORD VirtualControllerThread::XInputGetStateWrapper(DWORD dwUserIndex, XINPUT_STATE* pState) +{ + return XInputGetState(dwUserIndex, pState); +} + +DWORD VirtualControllerThread::callRealXinputGetState(DWORD dwUserIndex, XINPUT_STATE* pState) +{ + DWORD ret; + DWORD dwOldProtect, dwBkup; + + BYTE* Address = reinterpret_cast(XGetState); + VirtualProtect(Address, opPatchLenght, PAGE_EXECUTE_READWRITE, &dwOldProtect); //Change permissions of memory.. + for (DWORD i = 0; i < opPatchLenght; i++) //unpatch Valve's hook { - if (VirtualControllerThread::ulTargetSerials[i] == Target.SerialNo) - { - XInputSetState(i, &vibration); - } + *(Address + i) = realBytes[i]; } + + ret = XGetState(dwUserIndex, pState); //Cal REAL XInputGetState... + + for (int i = 0; i < opPatchLenght; i++) //repatch Valve's hook + { + *(Address + i) = valveHookBytes[i]; + } + VirtualProtect(Address, opPatchLenght, dwOldProtect, &dwBkup); //Revert permission change... + + return ret; } + + diff --git a/SteamTarget/VirtualControllerThread.h b/SteamTarget/VirtualControllerThread.h index 5a07a50..3565d02 100644 --- a/SteamTarget/VirtualControllerThread.h +++ b/SteamTarget/VirtualControllerThread.h @@ -17,14 +17,17 @@ limitations under the License. #include #include -#include -#include +#include +#include +#include #include +#include -#include +#include -#include +#include +#include class VirtualControllerThread { @@ -35,22 +38,31 @@ public: void run(); void stop(); - void resetControllers(); - bool isRunning(); private: - bool bShouldRun = false; + std::atomic bShouldRun = false; - int iRealControllers = 0; - int iTotalControllers = 0; - int iVirtualControllers = 0; - static ULONG ulTargetSerials[XUSER_MAX_COUNT]; + typedef DWORD(WINAPI* XInputGetState_t)(DWORD dwUserIndex, XINPUT_STATE* pState); + + static const uint8_t opPatchLenght = 5; + uint8_t valveHookBytes[5]; + + // First 5 bytes are the same for XInput1_4.dll and XInput9_1_0.dll (on AMD64 at least, didn't check yet for x86, there is no ViGEm build for Win7 anway...) + // So no change has to be made for Win7 Targets +#ifdef _AMD64_ + const uint8_t realBytes[5] = {0x48, 0x89, 0x5C, 0x24, 0x08}; +#else + const uint8_t realBytes[5] = { 0x8B, 0xFF, 0x55, 0x8B, 0xEC }; +#endif + //uint8_t realBytes[5] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x90 }; + + int controllerCount = 0; + XInputGetState_t XGetState = nullptr; + VIGEM_TARGET vtX360[XUSER_MAX_COUNT]; - XINPUT_STATE xsState[XUSER_MAX_COUNT]; - XUSB_REPORT xrReport[XUSER_MAX_COUNT]; std::thread controllerThread; @@ -60,9 +72,11 @@ private: void controllerLoop(); - int getRealControllers(); - static void controllerCallback(VIGEM_TARGET Target, UCHAR LargeMotor, UCHAR SmallMotor, UCHAR LedNumber); + static DWORD XInputGetStateWrapper(DWORD dwUserIndex, XINPUT_STATE* pState); //Easier to find in x64dbg... + + DWORD callRealXinputGetState(DWORD dwUserIndex, XINPUT_STATE* pState); + };