From 4801bea92d8ed733f38578fbf8d8a3251633a069 Mon Sep 17 00:00:00 2001 From: ndeadly <24677491+ndeadly@users.noreply.github.com> Date: Wed, 7 Oct 2020 00:31:13 +0200 Subject: [PATCH 1/2] bluetooth-mitm: add support for mad catz C.T.R.L.R controller --- .../controllers/controller_management.cpp | 9 ++ .../controllers/controller_management.hpp | 2 + .../controllers/mad_catz_controller.cpp | 87 +++++++++++++++ .../controllers/mad_catz_controller.hpp | 103 ++++++++++++++++++ 4 files changed, 201 insertions(+) create mode 100644 bluetooth-mitm/source/controllers/mad_catz_controller.cpp create mode 100644 bluetooth-mitm/source/controllers/mad_catz_controller.hpp diff --git a/bluetooth-mitm/source/controllers/controller_management.cpp b/bluetooth-mitm/source/controllers/controller_management.cpp index aaa238d..3b3bf30 100644 --- a/bluetooth-mitm/source/controllers/controller_management.cpp +++ b/bluetooth-mitm/source/controllers/controller_management.cpp @@ -122,6 +122,12 @@ namespace ams::controller { } } + for (auto hwId : MadCatzController::hardware_ids) { + if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) { + return ControllerType_MadCatz; + } + } + return ControllerType_Unknown; } @@ -190,6 +196,9 @@ namespace ams::controller { case ControllerType_PowerA: g_controllers.push_back(std::make_unique(address)); break; + case ControllerType_MadCatz: + g_controllers.push_back(std::make_unique(address)); + break; default: g_controllers.push_back(std::make_unique(address)); break; diff --git a/bluetooth-mitm/source/controllers/controller_management.hpp b/bluetooth-mitm/source/controllers/controller_management.hpp index 4f22de7..6e4bc70 100644 --- a/bluetooth-mitm/source/controllers/controller_management.hpp +++ b/bluetooth-mitm/source/controllers/controller_management.hpp @@ -30,6 +30,7 @@ #include "nvidia_shield_controller.hpp" #include "8bitdo_controller.hpp" #include "powera_controller.hpp" +#include "mad_catz_controller.hpp" namespace ams::controller { @@ -51,6 +52,7 @@ namespace ams::controller { ControllerType_NvidiaShield, ControllerType_8BitDo, ControllerType_PowerA, + ControllerType_MadCatz, ControllerType_Unknown, }; diff --git a/bluetooth-mitm/source/controllers/mad_catz_controller.cpp b/bluetooth-mitm/source/controllers/mad_catz_controller.cpp new file mode 100644 index 0000000..2b482f7 --- /dev/null +++ b/bluetooth-mitm/source/controllers/mad_catz_controller.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2020 ndeadly + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "mad_catz_controller.hpp" +#include + +namespace ams::controller { + + namespace { + + const constexpr float stick_scale_factor = float(UINT12_MAX) / UINT8_MAX; + + } + + void MadCatzController::UpdateControllerState(const bluetooth::HidReport *report) { + auto madcatz_report = reinterpret_cast(&report->data); + + switch(madcatz_report->id) { + case 0x01: + this->HandleInputReport0x01(madcatz_report); + break; + case 0x02: + this->HandleInputReport0x02(madcatz_report); + break; + default: + break; + } + } + + void MadCatzController::HandleInputReport0x01(const MadCatzReportData *src) { + this->PackStickData(&m_left_stick, + static_cast(stick_scale_factor * src->input0x01.left_stick.x) & 0xfff, + static_cast(stick_scale_factor * (UINT8_MAX - src->input0x01.left_stick.y)) & 0xfff + ); + this->PackStickData(&m_right_stick, + static_cast(stick_scale_factor * src->input0x01.right_stick.x) & 0xfff, + static_cast(stick_scale_factor * (UINT8_MAX - src->input0x01.right_stick.y)) & 0xfff + ); + + m_buttons.dpad_down = (src->input0x01.buttons.dpad == MadCatzDPad_S) || + (src->input0x01.buttons.dpad == MadCatzDPad_SE) || + (src->input0x01.buttons.dpad == MadCatzDPad_SW); + m_buttons.dpad_up = (src->input0x01.buttons.dpad == MadCatzDPad_N) || + (src->input0x01.buttons.dpad == MadCatzDPad_NE) || + (src->input0x01.buttons.dpad == MadCatzDPad_NW); + m_buttons.dpad_right = (src->input0x01.buttons.dpad == MadCatzDPad_E) || + (src->input0x01.buttons.dpad == MadCatzDPad_NE) || + (src->input0x01.buttons.dpad == MadCatzDPad_SE); + m_buttons.dpad_left = (src->input0x01.buttons.dpad == MadCatzDPad_W) || + (src->input0x01.buttons.dpad == MadCatzDPad_NW) || + (src->input0x01.buttons.dpad == MadCatzDPad_SW); + + m_buttons.A = src->input0x01.buttons.B; + m_buttons.B = src->input0x01.buttons.A; + m_buttons.X = src->input0x01.buttons.Y; + m_buttons.Y = src->input0x01.buttons.X; + + m_buttons.R = src->input0x01.buttons.R1; + m_buttons.ZR = src->input0x01.buttons.R2; + m_buttons.L = src->input0x01.buttons.L1; + m_buttons.ZL = src->input0x01.buttons.L2; + + m_buttons.minus = src->input0x01.buttons.select; + m_buttons.plus = src->input0x01.buttons.start; + + m_buttons.lstick_press = src->input0x01.buttons.L3; + m_buttons.rstick_press = src->input0x01.buttons.R3; + } + + void MadCatzController::HandleInputReport0x02(const MadCatzReportData *src) { + // Media buttons + } + +} diff --git a/bluetooth-mitm/source/controllers/mad_catz_controller.hpp b/bluetooth-mitm/source/controllers/mad_catz_controller.hpp new file mode 100644 index 0000000..e951693 --- /dev/null +++ b/bluetooth-mitm/source/controllers/mad_catz_controller.hpp @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2020 ndeadly + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#include "emulated_switch_controller.hpp" + +namespace ams::controller { + + enum MadCatzDPadDirection { + MadCatzDPad_Released, + MadCatzDPad_N, + MadCatzDPad_NE, + MadCatzDPad_E, + MadCatzDPad_SE, + MadCatzDPad_S, + MadCatzDPad_SW, + MadCatzDPad_W, + MadCatzDPad_NW + }; + + struct MadCatzStickData { + uint8_t x; + uint8_t y; + } __attribute__ ((__packed__)); + + struct MadCatzButtonData { + uint8_t X : 1; + uint8_t A : 1; + uint8_t B : 1; + uint8_t Y : 1; + uint8_t L1 : 1; + uint8_t R1 : 1; + uint8_t L2 : 1; + uint8_t R2 : 1; + + uint8_t select : 1; + uint8_t start : 1; + uint8_t L3 : 1; + uint8_t R3 : 1; + uint8_t : 0; + + uint8_t dpad : 4; + uint8_t : 0; + } __attribute__ ((__packed__)); + + struct MadCatzInputReport0x01{ + MadCatzButtonData buttons; + MadCatzStickData left_stick; + MadCatzStickData right_stick; + uint8_t left_trigger; + uint8_t right_trigger; + } __attribute__ ((__packed__)); + + struct MadCatzInputReport0x02{ + uint8_t : 2; + uint8_t volume_up : 1; + uint8_t volume_down : 1; + uint8_t play : 1; + uint8_t forward : 1; + uint8_t rewind : 1; + uint8_t : 0; + } __attribute__ ((__packed__)); + + struct MadCatzReportData { + uint8_t id; + union { + MadCatzInputReport0x01 input0x01; + MadCatzInputReport0x02 input0x02; + }; + } __attribute__((packed)); + + class MadCatzController : public EmulatedSwitchController { + + public: + static constexpr const HardwareID hardware_ids[] = { + {0x0738, 0x5266} // Mad Catz C.T.R.L.R + }; + + MadCatzController(const bluetooth::Address *address) + : EmulatedSwitchController(address) { }; + + void UpdateControllerState(const bluetooth::HidReport *report); + + private: + void HandleInputReport0x01(const MadCatzReportData *src); + void HandleInputReport0x02(const MadCatzReportData *src); + + }; + +} From d5ec412d361d0d362cb6bf21c109bcd94ad9be5f Mon Sep 17 00:00:00 2001 From: ndeadly <24677491+ndeadly@users.noreply.github.com> Date: Wed, 7 Oct 2020 01:22:42 +0200 Subject: [PATCH 2/2] bluetooth-mitm: attempt to map home button for mad catz C.T.R.L.R --- bluetooth-mitm/source/controllers/mad_catz_controller.cpp | 2 ++ bluetooth-mitm/source/controllers/mad_catz_controller.hpp | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bluetooth-mitm/source/controllers/mad_catz_controller.cpp b/bluetooth-mitm/source/controllers/mad_catz_controller.cpp index 2b482f7..58252c3 100644 --- a/bluetooth-mitm/source/controllers/mad_catz_controller.cpp +++ b/bluetooth-mitm/source/controllers/mad_catz_controller.cpp @@ -78,6 +78,8 @@ namespace ams::controller { m_buttons.lstick_press = src->input0x01.buttons.L3; m_buttons.rstick_press = src->input0x01.buttons.R3; + + m_buttons.home = src->input0x01.buttons.home; } void MadCatzController::HandleInputReport0x02(const MadCatzReportData *src) { diff --git a/bluetooth-mitm/source/controllers/mad_catz_controller.hpp b/bluetooth-mitm/source/controllers/mad_catz_controller.hpp index e951693..0a5a1b0 100644 --- a/bluetooth-mitm/source/controllers/mad_catz_controller.hpp +++ b/bluetooth-mitm/source/controllers/mad_catz_controller.hpp @@ -50,10 +50,10 @@ namespace ams::controller { uint8_t start : 1; uint8_t L3 : 1; uint8_t R3 : 1; + uint8_t home : 1; uint8_t : 0; - uint8_t dpad : 4; - uint8_t : 0; + uint8_t dpad; } __attribute__ ((__packed__)); struct MadCatzInputReport0x01{