diff --git a/SteamTarget/VirtualControllerThread.cpp b/SteamTarget/VirtualControllerThread.cpp index 5d5e7bd..85f08f8 100644 --- a/SteamTarget/VirtualControllerThread.cpp +++ b/SteamTarget/VirtualControllerThread.cpp @@ -14,8 +14,7 @@ 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() { @@ -33,7 +32,6 @@ VirtualControllerThread::VirtualControllerThread() for (int i = 0; i < XUSER_MAX_COUNT; i++) { VIGEM_TARGET_INIT(&vtX360[i]); - VirtualControllerThread::ulTargetSerials[i] = NULL; } } @@ -62,7 +60,7 @@ void VirtualControllerThread::stop() void VirtualControllerThread::resetControllers() { - iRealControllers = getRealControllers(); + //iRealControllers = getRealControllers(); } bool VirtualControllerThread::isRunning() @@ -73,91 +71,108 @@ bool VirtualControllerThread::isRunning() void VirtualControllerThread::controllerLoop() { DWORD result; - sf::Clock reCheckControllerTimer; - int i, j; + DWORD result2; + sf::Clock testTimer; + //int i, j; while (bShouldRun) { sfClock.restart(); - if (reCheckControllerTimer.getElapsedTime().asSeconds() >= 1.f) + if (realXGetState == nullptr && testTimer.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)); + uint64_t testaddr = reinterpret_cast(GetProcAddress(xinputmod, "XInputGetState")); + std::cout << "testaddr: " << std::hex << testaddr << "\n"; - result = XInputGetState(i, &xsState[i]); + XInputGetState_t realXgstate = reinterpret_cast(testaddr); - 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; + realXGetState = realXgstate; + } - vigem_register_xusb_notification(reinterpret_cast(&VirtualControllerThread::controllerCallback), vtX360[i]); + if (realXGetState != nullptr) + { + if (!checkedControllers) + { + for (int i = 0; i < XUSER_MAX_COUNT; i++) + { + XINPUT_STATE state = { 0 }; + result = XInputGetStateWrapper(i, &state); + result2 = callRealXinputGetState(i, &state); + if (result != result2) + controllerCount++; } - - RtlCopyMemory(&xrReport[i], &xsState[i].Gamepad, sizeof(XUSB_REPORT)); - - vigem_xusb_submit_report(vtX360[i], xrReport[i]); + std::cout << "ControllerCount: " << std::to_string(controllerCount) << "\n"; + checkedControllers = true; } - else + + for (int i = 0; i < controllerCount; i++) { - if (VIGEM_SUCCESS(vigem_target_unplug(&vtX360[i]))) + //////// + ZeroMemory(&xsState[i], sizeof(XINPUT_STATE)); + result = XInputGetStateWrapper(i, &xsState[i]); + + if (result == ERROR_SUCCESS) { - iVirtualControllers--; - iTotalControllers = 0; - for (j = 0; j < XUSER_MAX_COUNT; j++) - { - ZeroMemory(&xsState[j], sizeof(XINPUT_STATE)); + vigem_target_set_vid(&vtX360[i], 0x1234); + vigem_target_set_pid(&vtX360[i], 0x0001); - result = XInputGetState(j, &xsState[j]); + if (VIGEM_SUCCESS(vigem_target_plugin(Xbox360Wired, &vtX360[i]))) + { + std::cout << "Plugged in controller " << vtX360[i].SerialNo << std::endl; + vigem_register_xusb_notification(reinterpret_cast(&VirtualControllerThread::controllerCallback), vtX360[i]); + } - if (result == ERROR_SUCCESS) - { - iTotalControllers++; - } - else { - break; - } + vigem_xusb_submit_report(vtX360[i], *reinterpret_cast(&xsState[i].Gamepad)); + } + else + { + if (VIGEM_SUCCESS(vigem_target_unplug(&vtX360[i]))) + { + std::cout << "Unplugged controller " << vtX360[i].SerialNo << std::endl; } - iTotalControllers -= iVirtualControllers; - std::cout << "Unplugged controller " << vtX360[i].SerialNo << std::endl; - VirtualControllerThread::ulTargetSerials[i] = NULL; } } } + + tickTime = sfClock.getElapsedTime().asMicroseconds(); if (tickTime < delay) { std::this_thread::sleep_for(std::chrono::microseconds(delay - tickTime)); } + + //std::this_thread::sleep_for(std::chrono::seconds(1)); } } @@ -194,14 +209,46 @@ 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... - for (int i = 0; i < XUSER_MAX_COUNT; i++) + //for (int i = 0; i < XUSER_MAX_COUNT; i++) + //{ + // if (VirtualControllerThread::ulTargetSerials[i] == Target.SerialNo) + // { + XInputSetState(Target.SerialNo-1, &vibration); + // } + //} +} + +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(realXGetState); + VirtualProtect(Address, 5, PAGE_EXECUTE_READWRITE, &dwOldProtect); + for (DWORD i = 0; i < 5; i++) { - if (VirtualControllerThread::ulTargetSerials[i] == Target.SerialNo) - { - XInputSetState(i, &vibration); - } + *(Address + i) = realBytes[i]; + } + VirtualProtect(Address, 4, dwOldProtect, &dwBkup); + + ret = realXGetState(dwUserIndex, pState); + + VirtualProtect(Address, 5, PAGE_EXECUTE_READWRITE, &dwOldProtect); + for (int i = 0; i < 5; i++) + { + *(Address + i) = valveHookBytes[i]; } + VirtualProtect(Address, 5, dwOldProtect, &dwBkup); + + return ret; } + + diff --git a/SteamTarget/VirtualControllerThread.h b/SteamTarget/VirtualControllerThread.h index 5a07a50..3c011f6 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 { @@ -41,16 +44,30 @@ public: private: - bool bShouldRun = false; + std::atomic bShouldRun = false; + + + typedef DWORD(WINAPI* XInputGetState_t)(DWORD dwUserIndex, XINPUT_STATE* pState); - int iRealControllers = 0; - int iTotalControllers = 0; + uint8_t valveHookBytes[5]; + uint8_t realBytes[5] = {0x48, 0x89, 0x5C, 0x24, 0x08}; + //uint8_t realBytes[5] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x90 }; + + XInputGetState_t x_get_state = &XInputGetState; + + XInputGetState_t realXGetState = nullptr; + + //int iRealControllers = 0; + //int iTotalControllers = 0; int iVirtualControllers = 0; + int controllerCount = 0; + + bool checkedControllers = false; + + //static std::vector ulTargetSerials; - static ULONG ulTargetSerials[XUSER_MAX_COUNT]; VIGEM_TARGET vtX360[XUSER_MAX_COUNT]; XINPUT_STATE xsState[XUSER_MAX_COUNT]; - XUSB_REPORT xrReport[XUSER_MAX_COUNT]; std::thread controllerThread; @@ -64,5 +81,9 @@ private: static void controllerCallback(VIGEM_TARGET Target, UCHAR LargeMotor, UCHAR SmallMotor, UCHAR LedNumber); + static DWORD XInputGetStateWrapper(DWORD dwUserIndex, XINPUT_STATE* pState); + + DWORD callRealXinputGetState(DWORD dwUserIndex, XINPUT_STATE* pState); + };