2016-11-19 01:24:49 +00:00
|
|
|
/*
|
2018-03-10 22:02:39 +00:00
|
|
|
Copyright 2018 Peter Repukat - FlatspotSoftware
|
2016-11-19 01:24:49 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
2017-02-15 03:07:27 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <atomic>
|
|
|
|
#include <vector>
|
2016-11-19 01:24:49 +00:00
|
|
|
|
|
|
|
#include <Windows.h>
|
2018-03-10 22:07:25 +00:00
|
|
|
#include <Psapi.h>
|
2016-11-19 01:24:49 +00:00
|
|
|
|
2017-02-15 03:07:27 +00:00
|
|
|
#include <SFML/System.hpp>
|
2016-11-19 01:24:49 +00:00
|
|
|
|
2017-02-15 03:07:27 +00:00
|
|
|
#include <Xinput.h>
|
2017-09-29 17:52:20 +00:00
|
|
|
#include <ViGEmClient.h>
|
2017-09-29 19:45:06 +00:00
|
|
|
#include <VersionHelpers.h>
|
2016-11-19 01:24:49 +00:00
|
|
|
|
|
|
|
class VirtualControllerThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
VirtualControllerThread();
|
2018-03-10 22:07:25 +00:00
|
|
|
VirtualControllerThread(const VirtualControllerThread& other) = delete;
|
|
|
|
VirtualControllerThread(VirtualControllerThread&& other) noexcept = delete;
|
|
|
|
VirtualControllerThread& operator=(const VirtualControllerThread& other) = delete;
|
|
|
|
VirtualControllerThread& operator=(VirtualControllerThread&& other) noexcept = delete;
|
2016-11-19 01:24:49 +00:00
|
|
|
~VirtualControllerThread();
|
|
|
|
|
|
|
|
void run();
|
|
|
|
void stop();
|
|
|
|
|
2018-03-10 22:02:39 +00:00
|
|
|
bool isRunning() const;
|
2016-11-19 01:24:49 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2018-03-10 22:07:25 +00:00
|
|
|
std::atomic<bool> b_should_run_ = false;
|
2017-02-15 03:07:27 +00:00
|
|
|
typedef DWORD(WINAPI* XInputGetState_t)(DWORD dwUserIndex, XINPUT_STATE* pState);
|
2016-11-19 01:24:49 +00:00
|
|
|
|
2018-03-10 22:02:39 +00:00
|
|
|
static const uint8_t op_patch_lenght = 5;
|
2018-03-10 22:07:25 +00:00
|
|
|
uint8_t valve_hook_bytes_[5]{};
|
2017-02-15 20:43:14 +00:00
|
|
|
|
2018-03-10 22:02:39 +00:00
|
|
|
bool seven_ = false;
|
2017-09-29 19:45:06 +00:00
|
|
|
|
2017-02-15 20:43:14 +00:00
|
|
|
#ifdef _AMD64_
|
2017-02-15 13:34:01 +00:00
|
|
|
const uint8_t realBytes[5] = {0x48, 0x89, 0x5C, 0x24, 0x08};
|
2017-02-15 20:43:14 +00:00
|
|
|
#else
|
2018-03-10 22:02:39 +00:00
|
|
|
const uint8_t real_bytes_[5] = { 0x8B, 0xFF, 0x55, 0x8B, 0xEC };
|
2017-02-15 20:43:14 +00:00
|
|
|
#endif
|
2017-02-15 03:07:27 +00:00
|
|
|
//uint8_t realBytes[5] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x90 };
|
|
|
|
|
2018-03-10 22:02:39 +00:00
|
|
|
int controller_count_ = 0;
|
|
|
|
XInputGetState_t x_get_state_ = nullptr;
|
2016-11-19 01:24:49 +00:00
|
|
|
|
2018-03-10 22:02:39 +00:00
|
|
|
PVIGEM_CLIENT driver_;
|
2018-03-10 22:07:25 +00:00
|
|
|
PVIGEM_TARGET vt_x360_[XUSER_MAX_COUNT]{};
|
2016-11-19 01:24:49 +00:00
|
|
|
|
2018-03-10 22:02:39 +00:00
|
|
|
std::thread controller_thread_;
|
2016-11-19 01:24:49 +00:00
|
|
|
|
2018-03-10 22:02:39 +00:00
|
|
|
sf::Clock sf_clock_;
|
|
|
|
int tick_time_ = 0;
|
|
|
|
int delay_ = 1000000 / 200;
|
2016-11-19 01:24:49 +00:00
|
|
|
|
|
|
|
void controllerLoop();
|
|
|
|
|
2018-03-10 22:02:39 +00:00
|
|
|
static void __RPC_CALLEE controllerCallback(PVIGEM_CLIENT client, PVIGEM_TARGET Target, UCHAR LargeMotor, UCHAR SmallMotor, UCHAR LedNumber);
|
2016-11-19 01:24:49 +00:00
|
|
|
|
2017-02-15 20:43:29 +00:00
|
|
|
static DWORD XInputGetStateWrapper(DWORD dwUserIndex, XINPUT_STATE* pState); //Easier to find in x64dbg...
|
2017-02-15 03:07:27 +00:00
|
|
|
|
|
|
|
DWORD callRealXinputGetState(DWORD dwUserIndex, XINPUT_STATE* pState);
|
2016-11-19 01:24:49 +00:00
|
|
|
};
|
|
|
|
|