MissionControl/btdrv-mitm/source/controllers/xboxone.cpp

87 lines
3.9 KiB
C++

#include <cstring>
#include <stratosphere.hpp>
#include "xboxone.hpp"
namespace controller {
namespace {
const constexpr float scale_factor = float(UINT12_MAX) / UINT16_MAX;
}
XboxOneController::XboxOneController(const BluetoothAddress *address)
: BluetoothController(ControllerType_XboxOne, address) {
}
void XboxOneController::convertReportFormat(const HidReport *inReport, HidReport *outReport) {
auto xboxData = reinterpret_cast<const XboxOneReportData *>(&inReport->data);
auto switchData = reinterpret_cast<SwitchReportData *>(&outReport->data);
outReport->type = 0x31;
outReport->id = 0x30;
switchData->report0x30.conn_info = 0x0;
switchData->report0x30.battery = 0x8;
switch(inReport->id) {
case 0x01:
handleInputReport0x01(xboxData, switchData);
break;
case 0x02:
handleInputReport0x02(xboxData, switchData);
break;
default:
break;
}
}
void XboxOneController::handleInputReport0x01(const XboxOneReportData *src, SwitchReportData *dst) {
dst->report0x30.left_stick.x = static_cast<uint16_t>(src->report0x01.left_stick.x * scale_factor) & 0xfff;
dst->report0x30.left_stick.y = static_cast<uint16_t>((UINT16_MAX - src->report0x01.left_stick.y) * scale_factor) & 0xfff;
dst->report0x30.right_stick.x = static_cast<uint16_t>(src->report0x01.right_stick.x * scale_factor) & 0xfff;
dst->report0x30.right_stick.y = static_cast<uint16_t>((UINT16_MAX - src->report0x01.right_stick.y) * scale_factor) & 0xfff;
dst->report0x30.buttons.dpad_down = (src->report0x01.buttons.dpad == XboxOneDPad_S) ||
(src->report0x01.buttons.dpad == XboxOneDPad_SE) ||
(src->report0x01.buttons.dpad == XboxOneDPad_SW);
dst->report0x30.buttons.dpad_up = (src->report0x01.buttons.dpad == XboxOneDPad_N) ||
(src->report0x01.buttons.dpad == XboxOneDPad_NE) ||
(src->report0x01.buttons.dpad == XboxOneDPad_NW);
dst->report0x30.buttons.dpad_right = (src->report0x01.buttons.dpad == XboxOneDPad_E) ||
(src->report0x01.buttons.dpad == XboxOneDPad_NE) ||
(src->report0x01.buttons.dpad == XboxOneDPad_SE);
dst->report0x30.buttons.dpad_left = (src->report0x01.buttons.dpad == XboxOneDPad_W) ||
(src->report0x01.buttons.dpad == XboxOneDPad_NW) ||
(src->report0x01.buttons.dpad == XboxOneDPad_SW);
dst->report0x30.buttons.A = src->report0x01.buttons.B;
dst->report0x30.buttons.B = src->report0x01.buttons.A;
dst->report0x30.buttons.X = src->report0x01.buttons.Y;
dst->report0x30.buttons.Y = src->report0x01.buttons.X;
dst->report0x30.buttons.R = src->report0x01.buttons.RB;
dst->report0x30.buttons.ZR = src->report0x01.right_trigger > 0;
dst->report0x30.buttons.L = src->report0x01.buttons.LB;
dst->report0x30.buttons.ZL = src->report0x01.left_trigger > 0;
dst->report0x30.buttons.minus = src->report0x01.buttons.view;
dst->report0x30.buttons.plus = src->report0x01.buttons.menu;
dst->report0x30.buttons.lstick_press = src->report0x01.buttons.lstick_press;
dst->report0x30.buttons.rstick_press = src->report0x01.buttons.rstick_press;
dst->report0x30.buttons.capture = 0;
}
void XboxOneController::handleInputReport0x02(const XboxOneReportData *src, SwitchReportData *dst) {
std::memset(&dst->report0x30.buttons, 0, sizeof(SwitchButtonData));
dst->report0x30.buttons.home = src->report0x02.guide;
}
}