2021-10-20 19:47:24 +00:00
|
|
|
/*
|
2022-01-06 23:38:21 +00:00
|
|
|
Copyright 2021-2022 Peter Repukat - FlatspotSoftware
|
2021-10-20 19:47:24 +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.
|
|
|
|
*/
|
2021-10-17 21:41:15 +00:00
|
|
|
#include "InputRedirector.h"
|
|
|
|
|
|
|
|
#include <SFML/System/Clock.hpp>
|
|
|
|
#include <SFML/System/Sleep.hpp>
|
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
|
2021-10-30 01:07:51 +00:00
|
|
|
#include "Overlay.h"
|
2022-03-05 15:48:09 +00:00
|
|
|
#include "Settings.h"
|
2021-10-30 01:07:51 +00:00
|
|
|
|
2021-10-17 21:41:15 +00:00
|
|
|
InputRedirector::InputRedirector()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
driver_ = vigem_alloc();
|
|
|
|
vigem_connected_ = VIGEM_SUCCESS(vigem_connect(driver_));
|
|
|
|
if (vigem_connected_) {
|
|
|
|
spdlog::debug("Connected to ViGEm");
|
2021-10-20 19:47:24 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-10-17 21:41:15 +00:00
|
|
|
spdlog::error("Error initializing ViGEm");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
InputRedirector::~InputRedirector()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (controller_thread_.joinable())
|
|
|
|
controller_thread_.join();
|
|
|
|
vigem_disconnect(driver_);
|
|
|
|
vigem_free(driver_);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputRedirector::run()
|
|
|
|
{
|
|
|
|
run_ = vigem_connected_;
|
|
|
|
controller_thread_ = std::thread(&InputRedirector::runLoop, this);
|
2022-03-05 15:48:09 +00:00
|
|
|
max_controller_count_ = Settings::controller.maxControllers;
|
2022-03-07 14:26:16 +00:00
|
|
|
use_real_vid_pid_ = Settings::devices.realDeviceIds;
|
2021-10-30 01:07:51 +00:00
|
|
|
#ifdef _WIN32
|
2022-04-08 11:11:20 +00:00
|
|
|
Overlay::AddOverlayElem([this](bool window_has_focus) {
|
2021-10-30 01:07:51 +00:00
|
|
|
ImGui::SetNextWindowPos({650, 450}, ImGuiCond_FirstUseEver);
|
|
|
|
ImGui::SetNextWindowSizeConstraints({400, 270}, {1000, 1000});
|
|
|
|
ImGui::Begin("Controller Emulation");
|
|
|
|
int countcopy = max_controller_count_;
|
2021-10-30 13:03:12 +00:00
|
|
|
ImGui::Text("Max. controller count");
|
2021-10-30 13:56:01 +00:00
|
|
|
ImGui::InputInt("##Max. controller count", &countcopy, 1, 1);
|
|
|
|
if (countcopy > XUSER_MAX_COUNT) {
|
|
|
|
countcopy = XUSER_MAX_COUNT;
|
|
|
|
}
|
|
|
|
if (countcopy < 0) {
|
|
|
|
countcopy = 0;
|
|
|
|
}
|
2021-10-30 01:07:51 +00:00
|
|
|
max_controller_count_ = countcopy;
|
|
|
|
bool enable_rumbe_copy = enable_rumble_;
|
|
|
|
ImGui::Checkbox("Enable Rumble", &enable_rumbe_copy);
|
|
|
|
enable_rumble_ = enable_rumbe_copy;
|
2022-03-05 14:44:08 +00:00
|
|
|
if (ImGui::CollapsingHeader("Advanced", ImGuiTreeNodeFlags_DefaultOpen)) {
|
2021-10-30 01:07:51 +00:00
|
|
|
ImGui::Text("GlosSI uses different USB-IDs for it's emulated controllers");
|
|
|
|
ImGui::Text("This can cause issues with some games");
|
|
|
|
ImGui::Text("When changing this, it's advised to also set Max. controller count");
|
|
|
|
ImGui::Text("to prevent having multiple mirrored controllers plugged in");
|
|
|
|
ImGui::Spacing();
|
2022-03-07 14:26:16 +00:00
|
|
|
ImGui::Text("If enabled, Device hiding won't work! Use \"Max. Controller count\"-setting.");
|
2021-10-30 01:07:51 +00:00
|
|
|
bool use_real_copy = use_real_vid_pid_;
|
|
|
|
ImGui::Checkbox("Use real USB-IDs", &use_real_copy);
|
|
|
|
if (use_real_vid_pid_ != use_real_copy) {
|
|
|
|
use_real_vid_pid_changed_ = true;
|
|
|
|
}
|
|
|
|
use_real_vid_pid_ = use_real_copy;
|
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
});
|
|
|
|
#endif
|
2021-10-17 21:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputRedirector::stop()
|
|
|
|
{
|
|
|
|
run_ = false;
|
2021-10-22 13:40:37 +00:00
|
|
|
controller_thread_.join();
|
2021-10-17 21:41:15 +00:00
|
|
|
if (vigem_connected_) {
|
2022-06-06 18:23:05 +00:00
|
|
|
for (const auto& target : vt_pad_) {
|
2021-10-17 21:41:15 +00:00
|
|
|
vigem_target_remove(driver_, target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputRedirector::runLoop()
|
|
|
|
{
|
|
|
|
// wait for steam to do all of it's hooking
|
|
|
|
const sf::Clock clock;
|
|
|
|
while (clock.getElapsedTime().asMilliseconds() < start_delay_ms_) {
|
|
|
|
sf::sleep(sf::milliseconds(20));
|
|
|
|
}
|
|
|
|
while (run_) {
|
|
|
|
#ifdef _WIN32
|
2021-10-30 01:07:51 +00:00
|
|
|
if (use_real_vid_pid_changed_) {
|
|
|
|
// unplug all.
|
|
|
|
use_real_vid_pid_changed_ = false;
|
|
|
|
for (int i = 0; i < XUSER_MAX_COUNT; i++) {
|
2022-06-06 18:23:05 +00:00
|
|
|
unplugVigemPad(i);
|
2021-10-30 01:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (max_controller_count_ < XUSER_MAX_COUNT) {
|
|
|
|
for (int i = max_controller_count_; i < XUSER_MAX_COUNT; i++) {
|
2022-06-06 18:23:05 +00:00
|
|
|
unplugVigemPad(i);
|
2021-10-30 01:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < XUSER_MAX_COUNT && i < max_controller_count_; i++) {
|
2021-10-17 21:41:15 +00:00
|
|
|
XINPUT_STATE state{};
|
|
|
|
if (XInputGetState(i, &state) == ERROR_SUCCESS) {
|
2022-06-06 18:23:05 +00:00
|
|
|
if (vt_pad_[i] != nullptr) {
|
|
|
|
if (Settings::controller.emulateDS4) {
|
|
|
|
DS4_REPORT rep;
|
|
|
|
DS4_REPORT_INIT(&rep);
|
|
|
|
|
|
|
|
// The DualShock 4 expects a different report format, so we call a helper
|
|
|
|
// function which will translate buttons and axes 1:1 from XUSB to DS4
|
|
|
|
// format and submit it to the update function afterwards.
|
|
|
|
XUSB_TO_DS4_REPORT(reinterpret_cast<PXUSB_REPORT>(&state.Gamepad), &rep);
|
|
|
|
vigem_target_ds4_update(driver_, vt_pad_[i], rep);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vigem_target_x360_update(driver_, vt_pad_[i], *reinterpret_cast<XUSB_REPORT*>(&state.Gamepad));
|
|
|
|
}
|
2021-10-20 19:47:24 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-06-06 18:23:05 +00:00
|
|
|
if (Settings::controller.emulateDS4) {
|
|
|
|
vt_pad_[i] = vigem_target_ds4_alloc();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vt_pad_[i] = vigem_target_x360_alloc();
|
|
|
|
}
|
|
|
|
|
2021-10-19 17:19:56 +00:00
|
|
|
// By using VID and PID of Valve's Emulated Controller
|
|
|
|
// ( https://partner.steamgames.com/doc/features/steam_controller/steam_input_gamepad_emulation_bestpractices )
|
|
|
|
// Steam doesn't give us ANOTHER "fake" XInput device
|
2021-10-17 21:41:15 +00:00
|
|
|
// -> Leading to endless pain and suffering.
|
|
|
|
// Or really, leading to plugging 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 controller profile
|
|
|
|
// -> Leading to endless pain and suffering
|
|
|
|
//
|
2021-10-19 17:19:56 +00:00
|
|
|
// Additonaly, Steam does pick up the emulated controller this way, and Hides it from our application
|
|
|
|
// but Steam ONLY does this if it is configured to support X360 controller rebinding!!!
|
|
|
|
// Otherwise, this application (GloSC/GlosSI) will pickup the emulated controller as well!
|
2021-10-30 01:07:51 +00:00
|
|
|
// This however is configurable withon GlosSI overlay;
|
|
|
|
// Multiple controllers can be worked around with by setting max count.
|
|
|
|
if (!use_real_vid_pid_) {
|
2022-06-06 18:23:05 +00:00
|
|
|
vigem_target_set_vid(vt_pad_[i], 0x28de); //VALVE_DIRECTINPUT_GAMEPAD_VID
|
2022-09-20 18:25:12 +00:00
|
|
|
vigem_target_set_pid(vt_pad_[i], 0x11FF); //VALVE_DIRECTINPUT_GAMEPAD_PID
|
|
|
|
} else {
|
|
|
|
vigem_target_set_vid(vt_pad_[i], 0x045E); // MICROSOFT
|
|
|
|
vigem_target_set_pid(vt_pad_[i], 0x028E); // XBOX 360 Controller
|
2021-10-30 01:07:51 +00:00
|
|
|
}
|
2021-10-19 17:19:56 +00:00
|
|
|
// TODO: MAYBE!: In a future version, use something like OpenXInput
|
|
|
|
//and filter out emulated controllers to support a greater amount of controllers simultaneously
|
2021-10-17 21:41:15 +00:00
|
|
|
|
2022-06-06 18:23:05 +00:00
|
|
|
const int target_add_res = vigem_target_add(driver_, vt_pad_[i]);
|
2021-10-17 21:41:15 +00:00
|
|
|
if (target_add_res == VIGEM_ERROR_TARGET_UNINITIALIZED) {
|
2022-06-06 18:23:05 +00:00
|
|
|
if (Settings::controller.emulateDS4) {
|
|
|
|
vt_pad_[i] = vigem_target_ds4_alloc();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vt_pad_[i] = vigem_target_x360_alloc();
|
|
|
|
}
|
2021-10-17 21:41:15 +00:00
|
|
|
}
|
|
|
|
if (target_add_res == VIGEM_ERROR_NONE) {
|
2022-09-20 18:08:22 +00:00
|
|
|
spdlog::info("Plugged in controller {}, {}; VID: {:x}; PID: {:x}",
|
|
|
|
i,
|
|
|
|
vigem_target_get_index(vt_pad_[i]),
|
|
|
|
vigem_target_get_vid(vt_pad_[i]),
|
|
|
|
vigem_target_get_pid(vt_pad_[i]));
|
2022-06-06 18:23:05 +00:00
|
|
|
|
|
|
|
if (Settings::controller.emulateDS4) {
|
|
|
|
const auto callback_register_res = vigem_target_ds4_register_notification(
|
|
|
|
driver_,
|
|
|
|
vt_pad_[i],
|
|
|
|
&InputRedirector::ds4ControllerCallback,
|
|
|
|
reinterpret_cast<LPVOID>(i));
|
|
|
|
if (!VIGEM_SUCCESS(callback_register_res)) {
|
|
|
|
spdlog::error("Registering controller {}, {} for notification failed with error code: {:#x}", i, vigem_target_get_index(vt_pad_[i]), callback_register_res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const auto callback_register_res = vigem_target_x360_register_notification(
|
|
|
|
driver_,
|
|
|
|
vt_pad_[i],
|
|
|
|
&InputRedirector::x360ControllerCallback,
|
|
|
|
reinterpret_cast<LPVOID>(i));
|
|
|
|
if (!VIGEM_SUCCESS(callback_register_res)) {
|
|
|
|
spdlog::error("Registering controller {}, {} for notification failed with error code: {:#x}", i, vigem_target_get_index(vt_pad_[i]), callback_register_res);
|
|
|
|
}
|
2021-10-17 21:41:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-20 19:47:24 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-06-06 18:23:05 +00:00
|
|
|
unplugVigemPad(i);
|
2021-10-17 21:41:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sf::sleep(sf::milliseconds(1));
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2022-06-06 18:23:05 +00:00
|
|
|
void InputRedirector::x360ControllerCallback(PVIGEM_CLIENT client, PVIGEM_TARGET Target, UCHAR LargeMotor, UCHAR SmallMotor, UCHAR LedNumber, LPVOID UserData)
|
2021-10-17 21:41:15 +00:00
|
|
|
{
|
2021-10-30 01:07:51 +00:00
|
|
|
if (!enable_rumble_) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-17 21:41:15 +00:00
|
|
|
XINPUT_VIBRATION vibration;
|
|
|
|
ZeroMemory(&vibration, sizeof(XINPUT_VIBRATION));
|
2022-06-06 18:23:05 +00:00
|
|
|
vibration.wLeftMotorSpeed = LargeMotor * 257;
|
|
|
|
vibration.wRightMotorSpeed = SmallMotor * 257;
|
2021-10-17 21:41:15 +00:00
|
|
|
|
2021-10-29 23:41:44 +00:00
|
|
|
XInputSetState(reinterpret_cast<int>(UserData), &vibration);
|
2021-10-17 21:41:15 +00:00
|
|
|
}
|
2021-10-30 01:07:51 +00:00
|
|
|
|
2022-06-06 18:23:05 +00:00
|
|
|
void InputRedirector::unplugVigemPad(int idx)
|
2021-10-30 01:07:51 +00:00
|
|
|
{
|
2022-06-06 18:23:05 +00:00
|
|
|
if (vt_pad_[idx] != nullptr) {
|
|
|
|
if (VIGEM_SUCCESS(vigem_target_remove(driver_, vt_pad_[idx]))) {
|
|
|
|
spdlog::info("Unplugged controller {}, {}", idx, vigem_target_get_index(vt_pad_[idx]));
|
|
|
|
vt_pad_[idx] = nullptr;
|
2021-10-30 01:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-06 18:23:05 +00:00
|
|
|
|
|
|
|
void InputRedirector::ds4ControllerCallback(PVIGEM_CLIENT client, PVIGEM_TARGET Target, UCHAR LargeMotor, UCHAR SmallMotor, DS4_LIGHTBAR_COLOR LightbarColor, LPVOID UserData)
|
|
|
|
{
|
|
|
|
if (!enable_rumble_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
XINPUT_VIBRATION vibration;
|
|
|
|
ZeroMemory(&vibration, sizeof(XINPUT_VIBRATION));
|
|
|
|
vibration.wLeftMotorSpeed = LargeMotor * 257;
|
|
|
|
vibration.wRightMotorSpeed = SmallMotor * 257;
|
|
|
|
|
|
|
|
XInputSetState(reinterpret_cast<int>(UserData), &vibration);
|
|
|
|
}
|
2021-10-17 21:41:15 +00:00
|
|
|
#endif
|