From d591d7b2b83702e901870c623e3980d623df9d59 Mon Sep 17 00:00:00 2001 From: ndeadly <24677491+ndeadly@users.noreply.github.com> Date: Wed, 10 Jun 2020 23:15:26 +0200 Subject: [PATCH] btdrv-mitm: begin adding virtual hdl controller code --- .../controllers/bluetoothcontroller.cpp | 1 + .../controllers/bluetoothcontroller.hpp | 5 ++ btdrv-mitm/source/controllers/dualshock4.cpp | 7 ++ .../controllers/hdlsvirtualcontroller.cpp | 84 +++++++++++++++++++ .../controllers/hdlsvirtualcontroller.hpp | 25 ++++++ .../source/controllers/virtualcontroller.hpp | 48 +++++++++++ btdrv-mitm/source/controllers/wiimote.cpp | 6 ++ btdrv-mitm/source/controllers/wiiupro.cpp | 6 ++ btdrv-mitm/source/controllers/xboxone.cpp | 6 ++ 9 files changed, 188 insertions(+) create mode 100644 btdrv-mitm/source/controllers/hdlsvirtualcontroller.cpp create mode 100644 btdrv-mitm/source/controllers/hdlsvirtualcontroller.hpp create mode 100644 btdrv-mitm/source/controllers/virtualcontroller.hpp diff --git a/btdrv-mitm/source/controllers/bluetoothcontroller.cpp b/btdrv-mitm/source/controllers/bluetoothcontroller.cpp index 593611c..ff0bf1b 100644 --- a/btdrv-mitm/source/controllers/bluetoothcontroller.cpp +++ b/btdrv-mitm/source/controllers/bluetoothcontroller.cpp @@ -5,6 +5,7 @@ namespace controller { BluetoothController::BluetoothController(ControllerType type, const BluetoothAddress *address) : m_type(type), m_address(*address) { m_switchController = (type == ControllerType_Joycon) || (type == ControllerType_SwitchPro); + m_virtualController = nullptr; } const BluetoothAddress& BluetoothController::address(void) const { diff --git a/btdrv-mitm/source/controllers/bluetoothcontroller.hpp b/btdrv-mitm/source/controllers/bluetoothcontroller.hpp index eb055a9..0771612 100644 --- a/btdrv-mitm/source/controllers/bluetoothcontroller.hpp +++ b/btdrv-mitm/source/controllers/bluetoothcontroller.hpp @@ -1,7 +1,10 @@ #pragma once +#include #include #include +#include "virtualcontroller.hpp" + namespace controller { struct HardwareID { @@ -34,7 +37,9 @@ namespace controller { BluetoothAddress m_address; ControllerType m_type; + bool m_switchController; + std::unique_ptr m_virtualController; }; diff --git a/btdrv-mitm/source/controllers/dualshock4.cpp b/btdrv-mitm/source/controllers/dualshock4.cpp index 1b6e355..a39a485 100644 --- a/btdrv-mitm/source/controllers/dualshock4.cpp +++ b/btdrv-mitm/source/controllers/dualshock4.cpp @@ -4,6 +4,8 @@ #include "dualshock4.hpp" #include "../btdrv_mitm_logging.hpp" +#include "hdlsvirtualcontroller.hpp" + namespace controller { @@ -13,6 +15,11 @@ namespace controller { } + Dualshock4Controller::Dualshock4Controller(const BluetoothAddress *address) + : BluetoothController(ControllerType_Dualshock4, address) { + m_virtualController = std::make_unique(); + } + Result Dualshock4Controller::initialize(void) { BTDRV_LOG_FMT("Dualshock4Controller::initialize"); return BluetoothController::initialize(); diff --git a/btdrv-mitm/source/controllers/hdlsvirtualcontroller.cpp b/btdrv-mitm/source/controllers/hdlsvirtualcontroller.cpp new file mode 100644 index 0000000..f246272 --- /dev/null +++ b/btdrv-mitm/source/controllers/hdlsvirtualcontroller.cpp @@ -0,0 +1,84 @@ +#include +#include "hdlsvirtualcontroller.hpp" + +#include "../btdrv_mitm_logging.hpp" + +namespace controller { + + HdlsVirtualController::HdlsVirtualController() { + BTDRV_LOG_FMT("HdlsVirtualController() called"); + m_handle = INVALID_HANDLE; + + std::memset(&m_device, 0, sizeof(HiddbgHdlsDeviceInfo)); + m_device.deviceType = HidDeviceType_FullKey3; + m_device.npadInterfaceType = NpadInterfaceType_Bluetooth; + // Set official pro controller colours + m_device.singleColorBody = RGBA8_MAXALPHA(45, 45, 45); + m_device.singleColorButtons = RGBA8_MAXALPHA(230, 230, 230); + m_device.colorLeftGrip = RGBA8_MAXALPHA(70, 70, 70); + m_device.colorRightGrip = RGBA8_MAXALPHA(70, 70, 70); + + std::memset(&m_state, 0, sizeof(HiddbgHdlsState)); + m_state.batteryCharge = 4; + + this->connect(); + } + + HdlsVirtualController::~HdlsVirtualController() { + BTDRV_LOG_FMT("~HdlsVirtualController() called"); + this->disconnect(); + } + + Result HdlsVirtualController::connect(void) { + BTDRV_LOG_FMT("Connecting Hdls virtual device"); + //return hiddbgAttachHdlsVirtualDevice(&m_handle, &m_device); + return 0; + } + + Result HdlsVirtualController::disconnect(void) { + BTDRV_LOG_FMT("Disconnecting Hdls virtual device"); + //return hiddbgDetachHdlsVirtualDevice(m_handle); + return 0; + } + + Result HdlsVirtualController::setState(const SwitchProGamepadState* state) { + if (m_handle != INVALID_HANDLE) { + m_state.buttons = 0; + + m_state.buttons |= state->A ? KEY_A : 0; + m_state.buttons |= state->B ? KEY_B : 0; + m_state.buttons |= state->X ? KEY_X : 0; + m_state.buttons |= state->Y ? KEY_Y : 0; + + m_state.buttons |= state->dpad_down ? KEY_DDOWN : 0; + m_state.buttons |= state->dpad_up ? KEY_DUP : 0; + m_state.buttons |= state->dpad_right ? KEY_DRIGHT : 0; + m_state.buttons |= state->dpad_left ? KEY_DLEFT : 0; + + m_state.buttons |= state->L ? KEY_L : 0; + m_state.buttons |= state->ZL ? KEY_ZL : 0; + m_state.buttons |= state->lstick_press ? KEY_LSTICK : 0; + + m_state.buttons |= state->R ? KEY_R : 0; + m_state.buttons |= state->ZR ? KEY_ZR : 0; + m_state.buttons |= state->rstick_press ? KEY_RSTICK : 0; + + m_state.buttons |= state->minus ? KEY_MINUS : 0; + m_state.buttons |= state->plus ? KEY_PLUS : 0; + m_state.buttons |= state->capture ? KEY_CAPTURE : 0; + m_state.buttons |= state->home ? KEY_HOME : 0; + + m_state.joysticks[JOYSTICK_LEFT].dx = state->left_stick.dx; + m_state.joysticks[JOYSTICK_LEFT].dy = state->left_stick.dy; + + m_state.joysticks[JOYSTICK_RIGHT].dx = state->right_stick.dx; + m_state.joysticks[JOYSTICK_RIGHT].dy = state->right_stick.dy; + + //return hiddbgSetHdlsState(m_handle, &m_state); + return 0; + } + + return -1; + } + +} \ No newline at end of file diff --git a/btdrv-mitm/source/controllers/hdlsvirtualcontroller.hpp b/btdrv-mitm/source/controllers/hdlsvirtualcontroller.hpp new file mode 100644 index 0000000..dae8018 --- /dev/null +++ b/btdrv-mitm/source/controllers/hdlsvirtualcontroller.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include +#include "virtualcontroller.hpp" + +namespace controller { + + class HdlsVirtualController : public VirtualController { + + public: + HdlsVirtualController(); + ~HdlsVirtualController(); + + Result connect(void); + Result disconnect(void); + Result setState(const SwitchProGamepadState* state); + + private: + uint64_t m_handle; + HiddbgHdlsDeviceInfo m_device; + HiddbgHdlsState m_state; + }; + + +} diff --git a/btdrv-mitm/source/controllers/virtualcontroller.hpp b/btdrv-mitm/source/controllers/virtualcontroller.hpp new file mode 100644 index 0000000..b50418d --- /dev/null +++ b/btdrv-mitm/source/controllers/virtualcontroller.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include + +namespace controller { + + struct SwitchProGamepadState { + + uint8_t Y : 1; + uint8_t X : 1; + uint8_t B : 1; + uint8_t A : 1; + uint8_t : 2; // SR, SL (Right Joy) + uint8_t R : 1; + uint8_t ZR : 1; + + uint8_t minus : 1; + uint8_t plus : 1; + uint8_t rstick_press : 1; + uint8_t lstick_press : 1; + uint8_t home : 1; + uint8_t capture : 1; + uint8_t : 0; + + uint8_t dpad_down : 1; + uint8_t dpad_up : 1; + uint8_t dpad_right : 1; + uint8_t dpad_left : 1; + uint8_t : 2; // SR, SL (Left Joy) + uint8_t L : 1; + uint8_t ZL : 1; + + JoystickPosition left_stick; + JoystickPosition right_stick; + }; + + class VirtualController { + + public: + virtual ~VirtualController() {}; + + virtual Result connect(void) = 0; + virtual Result disconnect(void) = 0; + virtual Result setState(const SwitchProGamepadState* state) = 0; + + }; + +} \ No newline at end of file diff --git a/btdrv-mitm/source/controllers/wiimote.cpp b/btdrv-mitm/source/controllers/wiimote.cpp index c854cda..0ccff20 100644 --- a/btdrv-mitm/source/controllers/wiimote.cpp +++ b/btdrv-mitm/source/controllers/wiimote.cpp @@ -1,7 +1,13 @@ #include "wiimote.hpp" +#include "hdlsvirtualcontroller.hpp" namespace controller { + WiimoteController::WiimoteController(const BluetoothAddress *address) + : WiiController(ControllerType_Wiimote, address) { + m_virtualController = std::make_unique(); + } + void WiimoteController::convertReportFormat(const HidReport *inReport, HidReport *outReport) { auto wiiData = reinterpret_cast(&inReport->data); auto switchData = reinterpret_cast(&outReport->data); diff --git a/btdrv-mitm/source/controllers/wiiupro.cpp b/btdrv-mitm/source/controllers/wiiupro.cpp index 116fb6a..f374188 100644 --- a/btdrv-mitm/source/controllers/wiiupro.cpp +++ b/btdrv-mitm/source/controllers/wiiupro.cpp @@ -1,11 +1,17 @@ #include #include "wiiupro.hpp" +#include "hdlsvirtualcontroller.hpp" #include "../btdrv_mitm_logging.hpp" namespace controller { + WiiUProController::WiiUProController(const BluetoothAddress *address) + : WiiController(ControllerType_WiiUPro, address) { + m_virtualController = std::make_unique(); + } + Result WiiUProController::initialize(void) { WiiController::initialize(); diff --git a/btdrv-mitm/source/controllers/xboxone.cpp b/btdrv-mitm/source/controllers/xboxone.cpp index 7d65e09..1afa933 100644 --- a/btdrv-mitm/source/controllers/xboxone.cpp +++ b/btdrv-mitm/source/controllers/xboxone.cpp @@ -2,6 +2,7 @@ #include #include "xboxone.hpp" +#include "hdlsvirtualcontroller.hpp" namespace controller { @@ -11,6 +12,11 @@ namespace controller { } + XboxOneController::XboxOneController(const BluetoothAddress *address) + : BluetoothController(ControllerType_XboxOne, address) { + m_virtualController = std::make_unique(); + } + void XboxOneController::convertReportFormat(const HidReport *inReport, HidReport *outReport) { auto xboxData = reinterpret_cast(&inReport->data); auto switchData = reinterpret_cast(&outReport->data);