Merged branch controller_fix into master

experimental
Peter Repukat 8 years ago
commit e263746e41

@ -14,13 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
#include "VirtualControllerThread.h" #include "VirtualControllerThread.h"
//
ULONG VirtualControllerThread::ulTargetSerials[XUSER_MAX_COUNT];
VirtualControllerThread::VirtualControllerThread() VirtualControllerThread::VirtualControllerThread()
{ {
resetControllers();
if (!VIGEM_SUCCESS(vigem_init())) if (!VIGEM_SUCCESS(vigem_init()))
{ {
std::cout << "Error initializing ViGem!" << std::endl; std::cout << "Error initializing ViGem!" << std::endl;
@ -28,14 +25,11 @@ VirtualControllerThread::VirtualControllerThread()
bShouldRun = false; bShouldRun = false;
} }
VIGEM_TARGET vtX360[XUSER_MAX_COUNT]; VIGEM_TARGET vtX360[XUSER_MAX_COUNT];
for (int i = 0; i < XUSER_MAX_COUNT; i++) for (int i = 0; i < XUSER_MAX_COUNT; i++)
{ {
VIGEM_TARGET_INIT(&vtX360[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() bool VirtualControllerThread::isRunning()
{ {
@ -73,82 +63,97 @@ bool VirtualControllerThread::isRunning()
void VirtualControllerThread::controllerLoop() void VirtualControllerThread::controllerLoop()
{ {
DWORD result; DWORD result;
sf::Clock reCheckControllerTimer; DWORD result2;
int i, j; sf::Clock waitForHookTimer;
while (bShouldRun) while (bShouldRun)
{ {
sfClock.restart(); 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; HMODULE xinputmod = nullptr;
for (i = 0; i < XUSER_MAX_COUNT; i++)
{
ZeroMemory(&xsState[i], sizeof(XINPUT_STATE));
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++; std::wstring name(&szModName[0]);
} auto& f = std::use_facet<std::ctype<wchar_t>>(std::locale());
else { f.tolower(&name[0], &name[0] + name.size());
if (name.find(std::wstring(L"xinput")) != std::wstring::npos)
{
xinputmod = hMods[i];
break; break;
} }
} }
iTotalControllers -= iVirtualControllers;
reCheckControllerTimer.restart();
} }
for (i = iRealControllers; i < iTotalControllers && i < XUSER_MAX_COUNT; i++) XInputGetState_t realXgstate = reinterpret_cast<XInputGetState_t>(GetProcAddress(xinputmod, "XInputGetState"));
std::cout << "realXgstate: " << std::hex << realXgstate << "\n";
for (int i = 0; i < 5; i++)
{ {
//////// valveHookBytes[i] = *reinterpret_cast<uint8_t*>(reinterpret_cast<uint64_t>(*realXgstate) + i);
ZeroMemory(&xsState[i], sizeof(XINPUT_STATE)); }
XGetState = realXgstate;
controllerCount = 1;
}
result = XInputGetState(i, &xsState[i]); if (XGetState != nullptr)
{
for (int i = 0; i < XUSER_MAX_COUNT; i++)
{
////////
XINPUT_STATE state = { 0 };
result = XInputGetStateWrapper(i, &state);
XINPUT_STATE state2 = { 0 };
result2 = callRealXinputGetState(i, &state2);
if (result == ERROR_SUCCESS) if (result == ERROR_SUCCESS)
{ {
if (result2 != ERROR_SUCCESS)
if (VIGEM_SUCCESS(vigem_target_plugin(Xbox360Wired, &vtX360[i])))
{ {
iVirtualControllers++; // 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)
{
VIGEM_TARGET_INIT(&vtX360[i]);
}
if (vigem_res == VIGEM_ERROR_NONE)
{
std::cout << "Plugged in controller " << vtX360[i].SerialNo << std::endl; std::cout << "Plugged in controller " << vtX360[i].SerialNo << std::endl;
VirtualControllerThread::ulTargetSerials[i] = vtX360[i].SerialNo;
vigem_register_xusb_notification(reinterpret_cast<PVIGEM_XUSB_NOTIFICATION>(&VirtualControllerThread::controllerCallback), vtX360[i]); vigem_register_xusb_notification(reinterpret_cast<PVIGEM_XUSB_NOTIFICATION>(&VirtualControllerThread::controllerCallback), vtX360[i]);
} }
}
RtlCopyMemory(&xrReport[i], &xsState[i].Gamepad, sizeof(XUSB_REPORT)); vigem_xusb_submit_report(vtX360[i], *reinterpret_cast<XUSB_REPORT*>(&state.Gamepad));
vigem_xusb_submit_report(vtX360[i], xrReport[i]);
} }
else else
{ {
if (VIGEM_SUCCESS(vigem_target_unplug(&vtX360[i]))) if (VIGEM_SUCCESS(vigem_target_unplug(&vtX360[i])))
{ {
iVirtualControllers--;
iTotalControllers = 0;
for (j = 0; j < XUSER_MAX_COUNT; j++)
{
ZeroMemory(&xsState[j], sizeof(XINPUT_STATE));
result = XInputGetState(j, &xsState[j]);
if (result == ERROR_SUCCESS)
{
iTotalControllers++;
}
else {
break;
}
}
iTotalControllers -= iVirtualControllers;
std::cout << "Unplugged controller " << vtX360[i].SerialNo << std::endl; std::cout << "Unplugged controller " << vtX360[i].SerialNo << std::endl;
VirtualControllerThread::ulTargetSerials[i] = NULL; }
} }
} }
} }
@ -158,35 +163,8 @@ void VirtualControllerThread::controllerLoop()
{ {
std::this_thread::sleep_for(std::chrono::microseconds(delay - tickTime)); 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) 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; XINPUT_VIBRATION vibration;
ZeroMemory(&vibration, sizeof(XINPUT_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.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)
{ {
if (VirtualControllerThread::ulTargetSerials[i] == Target.SerialNo) return XInputGetState(dwUserIndex, pState);
}
DWORD VirtualControllerThread::callRealXinputGetState(DWORD dwUserIndex, XINPUT_STATE* pState)
{ {
XInputSetState(i, &vibration); DWORD ret;
DWORD dwOldProtect, dwBkup;
BYTE* Address = reinterpret_cast<BYTE*>(XGetState);
VirtualProtect(Address, opPatchLenght, PAGE_EXECUTE_READWRITE, &dwOldProtect); //Change permissions of memory..
for (DWORD i = 0; i < opPatchLenght; i++) //unpatch Valve's hook
{
*(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;
} }

@ -17,14 +17,17 @@ limitations under the License.
#include <thread> #include <thread>
#include <chrono> #include <chrono>
#include <Xinput.h> #include <iostream>
#include <ViGEmUM.h> #include <atomic>
#include <vector>
#include <Windows.h> #include <Windows.h>
#include <psapi.h>
#include <SFML\System.hpp> #include <SFML/System.hpp>
#include <iostream> #include <Xinput.h>
#include <ViGEmUM.h>
class VirtualControllerThread class VirtualControllerThread
{ {
@ -35,22 +38,31 @@ public:
void run(); void run();
void stop(); void stop();
void resetControllers();
bool isRunning(); bool isRunning();
private: private:
bool bShouldRun = false; std::atomic<bool> 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]; VIGEM_TARGET vtX360[XUSER_MAX_COUNT];
XINPUT_STATE xsState[XUSER_MAX_COUNT];
XUSB_REPORT xrReport[XUSER_MAX_COUNT];
std::thread controllerThread; std::thread controllerThread;
@ -60,9 +72,11 @@ private:
void controllerLoop(); void controllerLoop();
int getRealControllers();
static void controllerCallback(VIGEM_TARGET Target, UCHAR LargeMotor, UCHAR SmallMotor, UCHAR LedNumber); 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);
}; };

Loading…
Cancel
Save