MissionControl/bluetooth-mitm/source/controllers/controller_management.cpp

197 lines
7.1 KiB
C++
Raw Normal View History

/*
* 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 <http://www.gnu.org/licenses/>.
*/
2020-08-26 18:52:47 +00:00
#include "controller_management.hpp"
#include <stratosphere.hpp>
#include <memory>
#include <mutex>
#include <vector>
#include <cstring>
namespace ams::controller {
namespace {
2020-08-26 18:52:47 +00:00
constexpr auto cod_major_peripheral = 0x05;
constexpr auto cod_minor_gamepad = 0x08;
constexpr auto cod_minor_joystick = 0x04;
2020-08-26 18:52:47 +00:00
os::Mutex g_controller_lock(false);
std::vector<std::unique_ptr<SwitchController>> g_controllers;
2020-08-26 18:52:47 +00:00
inline bool bdcmp(const bluetooth::Address *addr1, const bluetooth::Address *addr2) {
return std::memcmp(addr1, addr2, sizeof(bluetooth::Address)) == 0;
}
2020-08-26 18:52:47 +00:00
}
2020-08-26 18:52:47 +00:00
ControllerType Identify(const BluetoothDevicesSettings *device) {
if (IsOfficialSwitchControllerName(device->name, sizeof(device->name)))
return ControllerType_Switch;
2020-08-26 18:52:47 +00:00
for (auto hwId : WiiController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
2020-08-26 18:52:47 +00:00
return ControllerType_Wii;
}
}
2020-08-26 18:52:47 +00:00
for (auto hwId : Dualshock4Controller::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Dualshock4;
}
}
2020-08-26 18:52:47 +00:00
for (auto hwId : XboxOneController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_XboxOne;
}
}
for (auto hwId : OuyaController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Ouya;
}
}
for (auto hwId : GamestickController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Gamestick;
}
}
for (auto hwId : GemboxController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Gembox;
}
}
for (auto hwId : IpegaController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Ipega;
}
}
for (auto hwId : XiaomiController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Xiaomi;
}
}
for (auto hwId : GamesirController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Gamesir;
}
}
for (auto hwId : SteelseriesController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Steelseries;
}
}
return ControllerType_Unknown;
}
2020-08-26 18:52:47 +00:00
bool IsGamepad(const bluetooth::DeviceClass *cod) {
return ((cod->cod[1] & 0x0f) == cod_major_peripheral) &&
(((cod->cod[2] & 0x0f) == cod_minor_gamepad) || ((cod->cod[2] & 0x0f) == cod_minor_joystick));
}
2020-07-11 21:53:30 +00:00
bool IsOfficialSwitchControllerName(const char *name, size_t size) {
return std::strncmp(name, "Joy-Con (L)", size) == 0 ||
std::strncmp(name, "Joy-Con (R)", size) == 0 ||
std::strncmp(name, "Pro Controller", size) == 0 ||
std::strncmp(name, "Lic Pro Controller", size) == 0 ||
std::strncmp(name, "NES Controller", size) == 0 ||
std::strncmp(name, "HVC Controller", size) == 0 ||
std::strncmp(name, "SNES Controller", size) == 0 ||
std::strncmp(name, "NintendoGamepad", size) == 0 ;
}
2020-08-26 18:52:47 +00:00
void AttachHandler(const bluetooth::Address *address) {
std::scoped_lock lk(g_controller_lock);
BluetoothDevicesSettings device;
R_ABORT_UNLESS(btdrvGetPairedDeviceInfo(address, &device));
2020-08-26 18:52:47 +00:00
switch (Identify(&device)) {
case ControllerType_Switch:
g_controllers.push_back(std::make_unique<SwitchController>(address));
break;
2020-08-26 18:52:47 +00:00
case ControllerType_Wii:
g_controllers.push_back(std::make_unique<WiiController>(address));
break;
case ControllerType_Dualshock4:
g_controllers.push_back(std::make_unique<Dualshock4Controller>(address));
break;
case ControllerType_XboxOne:
g_controllers.push_back(std::make_unique<XboxOneController>(address));
break;
case ControllerType_Ouya:
g_controllers.push_back(std::make_unique<OuyaController>(address));
break;
case ControllerType_Gamestick:
g_controllers.push_back(std::make_unique<GamestickController>(address));
break;
case ControllerType_Gembox:
g_controllers.push_back(std::make_unique<GemboxController>(address));
break;
case ControllerType_Ipega:
g_controllers.push_back(std::make_unique<IpegaController>(address));
break;
case ControllerType_Xiaomi:
g_controllers.push_back(std::make_unique<XiaomiController>(address));
break;
case ControllerType_Gamesir:
g_controllers.push_back(std::make_unique<GamesirController>(address));
break;
case ControllerType_Steelseries:
g_controllers.push_back(std::make_unique<SteelseriesController>(address));
break;
default:
g_controllers.push_back(std::make_unique<DefaultController>(address));
break;
}
2020-08-26 18:52:47 +00:00
g_controllers.back()->Initialize();
}
2020-08-26 18:52:47 +00:00
void RemoveHandler(const bluetooth::Address *address) {
std::scoped_lock lk(g_controller_lock);
for (auto it = g_controllers.begin(); it < g_controllers.end(); ++it) {
2020-08-26 18:52:47 +00:00
if (bdcmp(&(*it)->Address(), address)) {
g_controllers.erase(it);
return;
}
}
}
2020-08-26 18:52:47 +00:00
SwitchController *LocateHandler(const bluetooth::Address *address) {
std::scoped_lock lk(g_controller_lock);
for (auto it = g_controllers.begin(); it < g_controllers.end(); ++it) {
if (bdcmp(&(*it)->Address(), address)) {
return (*it).get();
}
}
return nullptr;
}
}