Skip to content

Instantly share code, notes, and snippets.

@EigenFace
Last active December 12, 2020 21:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save EigenFace/74b56efe33eb6585e1b6384d09c4034b to your computer and use it in GitHub Desktop.
Save EigenFace/74b56efe33eb6585e1b6384d09c4034b to your computer and use it in GitHub Desktop.
Modified x360ce2Vigem
/*
MIT License
Copyright (c) 2016 Benjamin "Nefarius" Höglinger
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// x360ce2ViGEm.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Xinput.h>
#include <ViGEmUM.h>
#include <chrono>
#include <thread>
typedef void (WINAPI* HidGuardianOpen_t)();
typedef void (WINAPI* HidGuardianClose_t)();
static HidGuardianOpen_t fpOpen;
static HidGuardianOpen_t fpClose;
BOOL WINAPI HandlerRoutine(
_In_ DWORD dwCtrlType
);
int main()
{
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // nanoseconds, system_clock, seconds
SetConsoleTitle(L"x360ce to ViGEm demo application");
SetConsoleCtrlHandler(HandlerRoutine, TRUE);
HMODULE cerberus = LoadLibrary(L"HidCerberus.Lib.dll");
if (cerberus == INVALID_HANDLE_VALUE)
{
printf("Couldn't find HidCerberus.Lib.dll\n");
return 1;
}
fpOpen = reinterpret_cast<HidGuardianOpen_t>(GetProcAddress(cerberus, "HidGuardianOpen"));
fpClose = reinterpret_cast<HidGuardianOpen_t>(GetProcAddress(cerberus, "HidGuardianClose"));
printf("Bypassing HidGuardian\n");
if (fpOpen) fpOpen();
printf("Initializing emulation driver\n");
if (!VIGEM_SUCCESS(vigem_init())) {
printf("Couldn't initialize emulation driver\n");
return 1;
}
printf("Enabling XInput\n");
XInputEnable(TRUE);
VIGEM_TARGET targets[XUSER_MAX_COUNT];
for (int i = 0; i < XUSER_MAX_COUNT; i++)
{
VIGEM_TARGET_INIT(&targets[i]);
}
DWORD result;
XINPUT_STATE state;
printf("Starting translation, close window to exit...\n");
while (true)
{
auto begin = high_resolution_clock::now();
for (int i = 0; i < XUSER_MAX_COUNT; i++)
{
ZeroMemory(&state, sizeof(XINPUT_STATE));
result = XInputGetState(i, &state);
DS4_REPORT report;
BYTE bThumbLX = ((state.Gamepad.sThumbLX & 0xFF00) >> 8);
report.bThumbLX = bThumbLX + 128;
BYTE bThumbLY = ((state.Gamepad.sThumbLY & 0xFF00) >> 8);
report.bThumbLY = ~(bThumbLY + 128);
BYTE bThumbRX = ((state.Gamepad.sThumbRX & 0xFF00) >> 8);
report.bThumbRX = bThumbRX + 128;
BYTE bThumbRY = ((state.Gamepad.sThumbRY & 0xFF00) >> 8);
report.bThumbRY = ~(bThumbRY + 128);
USHORT wButtons = state.Gamepad.wButtons;
USHORT wNewButtons = 0;
BYTE directions = (wButtons & 0xF);
if (directions == 0) wNewButtons |= Ds4DpadNone;
else if (directions & XUSB_GAMEPAD_DPAD_UP) wNewButtons |= Ds4DpadN;
else if (directions == 2) wNewButtons |= Ds4DpadS;
else if (directions == 4) wNewButtons |= Ds4DpadW;
else if ((directions & XUSB_GAMEPAD_DPAD_UP) && (directions & XUSB_GAMEPAD_DPAD_LEFT)) wNewButtons |= Ds4DpadNW;
else if (directions == 6) wNewButtons |= Ds4DpadSW;
else if (directions == 8) wNewButtons |= Ds4DpadE;
else if (directions == 9) wNewButtons |= Ds4DpadNE;
else if (directions == 10) wNewButtons |= Ds4DpadSE;
if (wButtons & XUSB_GAMEPAD_X) wNewButtons |= Ds4Square;
if (wButtons & XUSB_GAMEPAD_A) wNewButtons |= Ds4Cross;
if (wButtons & XUSB_GAMEPAD_B) wNewButtons |= Ds4Circle;
if (wButtons & XUSB_GAMEPAD_Y) wNewButtons |= Ds4Triangle;
if (wButtons & XUSB_GAMEPAD_LEFT_SHOULDER) wNewButtons |= Ds4ShoulderL;
if (wButtons & XUSB_GAMEPAD_RIGHT_SHOULDER) wNewButtons |= Ds4ShoulderR;
report.bTriggerL = state.Gamepad.bLeftTrigger;
report.bTriggerR = state.Gamepad.bRightTrigger;
if (report.bTriggerL > 32) wNewButtons |= Ds4TriggerL;
if (report.bTriggerR > 32) wNewButtons |= Ds4TriggerR;
if (wButtons & XUSB_GAMEPAD_START) wNewButtons |= Ds4Options;
if (wButtons & XUSB_GAMEPAD_LEFT_THUMB) wNewButtons |= Ds4ThumbL;
if (wButtons & XUSB_GAMEPAD_RIGHT_THUMB) wNewButtons |= Ds4ThumbR;
report.wButtons = wNewButtons;
BYTE bSpecial=0;
if (wButtons & XUSB_GAMEPAD_BACK) bSpecial |= Ds4Ps;
if (wButtons & XUSB_GAMEPAD_GUIDE) bSpecial |= Ds4Touchpad;
report.bSpecial = bSpecial;
if (result == ERROR_SUCCESS)
{
if (VIGEM_SUCCESS(vigem_target_plugin(DualShock4Wired, &targets[i])))
{
printf("Plugged in controller %d\t\t\t\t\n", targets[i].SerialNo);
}
vigem_ds4_submit_report(targets[i], *reinterpret_cast<DS4_REPORT*>(&report));
}
else
{
if (VIGEM_SUCCESS(vigem_target_unplug(&targets[i])))
{
printf("Unplugged controller %d\t\t\t\t\n", targets[i].SerialNo);
}
}
}
auto end = high_resolution_clock::now();
auto dur = end - begin;
auto ns = duration_cast<nanoseconds>(dur);
auto delay = milliseconds(5) - ns;
sleep_for(delay);
auto finished = high_resolution_clock::now();
printf("Polling delay: %1lld ms (Frequency: %3.2f Hz)\t\t\r",
duration_cast<milliseconds>(delay).count(),
(1.0 / duration_cast<milliseconds>(finished - begin).count()) * 1000);
}
return 0;
}
BOOL WINAPI HandlerRoutine(
_In_ DWORD dwCtrlType
)
{
if (dwCtrlType == CTRL_CLOSE_EVENT)
{
fpClose();
return TRUE;
}
return FALSE;
}
@blacklanternhal
Copy link

Yeah, I've got no idea what I'm doing on the compiling side of things. Are you willing to share your working build? I don't plan on using this for anything other than PS4 Remote Play. If not I'm sure I could eventually figure it out.

@Questinghero
Copy link

would you mind posting a compiled version of this so I can use my GPD Win with PS4 remote play? Thanks

@drc85
Copy link

drc85 commented Jan 30, 2018

is there a tutorial to get this work or a finished version? thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment