2020-08-25 19:23:38 +00:00
|
|
|
/*
|
|
|
|
* 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-07-09 19:20:08 +00:00
|
|
|
#include "dualshock4.hpp"
|
2020-06-09 20:39:30 +00:00
|
|
|
#include <cstring>
|
2020-06-10 21:16:08 +00:00
|
|
|
#include <switch.h>
|
2020-06-17 23:36:56 +00:00
|
|
|
#include <stratosphere.hpp>
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-06-09 20:39:30 +00:00
|
|
|
#include "../btdrv_mitm_logging.hpp"
|
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
namespace ams::controller {
|
2020-06-02 21:24:40 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-07-11 11:52:56 +00:00
|
|
|
const constexpr float stickScaleFactor = float(UINT12_MAX) / UINT8_MAX;
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-07-10 19:37:31 +00:00
|
|
|
const Dualshock4LedColour playerLedColours[] = {
|
|
|
|
{0x00, 0x00, 0x7f}, // blue
|
|
|
|
{0x7f, 0x00, 0x00}, // red
|
|
|
|
{0x00, 0x7f, 0x00}, // green
|
|
|
|
{0x7f, 0x00, 0x7f} // pink
|
|
|
|
};
|
|
|
|
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 21:30:27 +00:00
|
|
|
Result Dualshock4Controller::initialize(void) {
|
2020-08-17 21:17:02 +00:00
|
|
|
R_TRY(EmulatedSwitchController::initialize());
|
2020-08-07 21:30:27 +00:00
|
|
|
R_TRY(this->updateControllerState());
|
2020-08-25 00:05:19 +00:00
|
|
|
|
2020-08-07 21:30:27 +00:00
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-07-27 17:42:40 +00:00
|
|
|
Result Dualshock4Controller::setPlayerLed(u8 led_mask) {
|
|
|
|
u8 i = 0;
|
|
|
|
while (led_mask >>= 1) { ++i; }
|
|
|
|
Dualshock4LedColour colour = playerLedColours[i];
|
2020-08-25 00:05:19 +00:00
|
|
|
return this->setLightbarColour(colour);
|
2020-07-27 17:42:40 +00:00
|
|
|
}
|
2020-06-10 21:16:08 +00:00
|
|
|
|
2020-07-27 17:42:40 +00:00
|
|
|
Result Dualshock4Controller::setLightbarColour(Dualshock4LedColour colour) {
|
|
|
|
m_ledColour = colour;
|
2020-08-24 20:40:44 +00:00
|
|
|
return this->updateControllerState();
|
2020-06-09 20:39:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
void Dualshock4Controller::convertReportFormat(const bluetooth::HidReport *inReport, bluetooth::HidReport *outReport) {
|
|
|
|
auto ds4Report = reinterpret_cast<const Dualshock4ReportData *>(&inReport->data);
|
|
|
|
auto switchReport = reinterpret_cast<SwitchReportData *>(&outReport->data);
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
switch(ds4Report->id) {
|
2020-06-09 20:39:30 +00:00
|
|
|
case 0x01:
|
2020-07-11 11:43:21 +00:00
|
|
|
this->handleInputReport0x01(ds4Report, switchReport);
|
2020-06-09 20:39:30 +00:00
|
|
|
break;
|
|
|
|
case 0x11:
|
2020-07-11 11:43:21 +00:00
|
|
|
this->handleInputReport0x11(ds4Report, switchReport);
|
2020-06-09 20:39:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
2020-07-11 11:43:21 +00:00
|
|
|
BTDRV_LOG_FMT("DS4: RECEIVED REPORT [0x%02x]", ds4Report->id);
|
2020-06-09 20:39:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-07-11 21:19:56 +00:00
|
|
|
|
2020-08-24 20:40:44 +00:00
|
|
|
outReport->size = sizeof(SwitchInputReport0x30) + 1;
|
2020-07-11 21:19:56 +00:00
|
|
|
switchReport->id = 0x30;
|
|
|
|
switchReport->input0x30.conn_info = 0x0;
|
|
|
|
switchReport->input0x30.battery = m_battery | m_charging;
|
|
|
|
switchReport->input0x30.timer = os::ConvertToTimeSpan(os::GetSystemTick()).GetMilliSeconds() & 0xff;
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 21:19:56 +00:00
|
|
|
void Dualshock4Controller::handleInputReport0x01(const Dualshock4ReportData *src, SwitchReportData *dst) {
|
2020-07-11 13:24:00 +00:00
|
|
|
packStickData(&dst->input0x30.left_stick,
|
|
|
|
static_cast<uint16_t>(stickScaleFactor * src->input0x01.left_stick.x) & 0xfff,
|
|
|
|
static_cast<uint16_t>(stickScaleFactor * (UINT8_MAX - src->input0x01.left_stick.y)) & 0xfff
|
2020-07-01 23:34:13 +00:00
|
|
|
);
|
2020-07-11 13:24:00 +00:00
|
|
|
packStickData(&dst->input0x30.right_stick,
|
|
|
|
static_cast<uint16_t>(stickScaleFactor * src->input0x01.right_stick.x) & 0xfff,
|
|
|
|
static_cast<uint16_t>(stickScaleFactor * (UINT8_MAX - src->input0x01.right_stick.y)) & 0xfff
|
2020-07-01 23:34:13 +00:00
|
|
|
);
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-08-07 00:12:57 +00:00
|
|
|
this->mapButtons(&src->input0x01.buttons, dst);
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 20:39:30 +00:00
|
|
|
void Dualshock4Controller::handleInputReport0x11(const Dualshock4ReportData *src, SwitchReportData *dst) {
|
2020-08-08 00:05:41 +00:00
|
|
|
if (!src->input0x11.usb || src->input0x11.battery_level > 10)
|
|
|
|
m_charging = false;
|
|
|
|
else
|
|
|
|
m_charging = true;
|
|
|
|
|
|
|
|
m_battery = src->input0x11.battery_level & 0xe;
|
2020-07-09 19:22:14 +00:00
|
|
|
|
2020-07-11 13:24:00 +00:00
|
|
|
packStickData(&dst->input0x30.left_stick,
|
|
|
|
static_cast<uint16_t>(stickScaleFactor * src->input0x11.left_stick.x) & 0xfff,
|
|
|
|
static_cast<uint16_t>(stickScaleFactor * (UINT8_MAX - src->input0x11.left_stick.y)) & 0xfff
|
2020-07-01 23:34:13 +00:00
|
|
|
);
|
2020-07-11 13:24:00 +00:00
|
|
|
packStickData(&dst->input0x30.right_stick,
|
|
|
|
static_cast<uint16_t>(stickScaleFactor * src->input0x11.right_stick.x) & 0xfff,
|
|
|
|
static_cast<uint16_t>(stickScaleFactor * (UINT8_MAX - src->input0x11.right_stick.y)) & 0xfff
|
2020-07-01 23:34:13 +00:00
|
|
|
);
|
2020-06-09 20:39:30 +00:00
|
|
|
|
2020-08-07 00:12:57 +00:00
|
|
|
this->mapButtons(&src->input0x11.buttons, dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dualshock4Controller::mapButtons(const Dualshock4ButtonData *buttons, SwitchReportData *dst) {
|
|
|
|
dst->input0x30.buttons.dpad_down = (buttons->dpad == Dualshock4DPad_S) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_SE) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_SW);
|
|
|
|
dst->input0x30.buttons.dpad_up = (buttons->dpad == Dualshock4DPad_N) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_NE) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_NW);
|
|
|
|
dst->input0x30.buttons.dpad_right = (buttons->dpad == Dualshock4DPad_E) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_NE) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_SE);
|
|
|
|
dst->input0x30.buttons.dpad_left = (buttons->dpad == Dualshock4DPad_W) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_NW) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_SW);
|
|
|
|
|
|
|
|
dst->input0x30.buttons.A = buttons->circle;
|
|
|
|
dst->input0x30.buttons.B = buttons->cross;
|
|
|
|
dst->input0x30.buttons.X = buttons->triangle;
|
|
|
|
dst->input0x30.buttons.Y = buttons->square;
|
|
|
|
|
|
|
|
dst->input0x30.buttons.R = buttons->R1;
|
|
|
|
dst->input0x30.buttons.ZR = buttons->R2;
|
|
|
|
dst->input0x30.buttons.L = buttons->L1;
|
|
|
|
dst->input0x30.buttons.ZL = buttons->L2;
|
|
|
|
|
|
|
|
dst->input0x30.buttons.minus = buttons->share;
|
|
|
|
dst->input0x30.buttons.plus = buttons->options;
|
|
|
|
|
|
|
|
dst->input0x30.buttons.lstick_press = buttons->L3;
|
|
|
|
dst->input0x30.buttons.rstick_press = buttons->R3;
|
|
|
|
|
|
|
|
dst->input0x30.buttons.capture = buttons->tpad;
|
|
|
|
dst->input0x30.buttons.home = buttons->ps;
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 17:42:40 +00:00
|
|
|
Result Dualshock4Controller::updateControllerState(void) {
|
|
|
|
Dualshock4OutputReport0x11 report = {0xa2, 0x11, 0xc0, 0x20, 0xf3, 0x04, 0x00, 0x00, 0x00, m_ledColour.r, m_ledColour.g, m_ledColour.b};
|
|
|
|
report.crc = crc32Calculate(report.data, sizeof(report.data));
|
|
|
|
|
2020-08-25 19:16:43 +00:00
|
|
|
s_outputReport.size = sizeof(report) - 1;
|
|
|
|
std::memcpy(s_outputReport.data, &report.data[1], s_outputReport.size);
|
2020-07-27 17:42:40 +00:00
|
|
|
|
2020-08-25 19:16:43 +00:00
|
|
|
R_TRY(bluetooth::hid::report::SendHidReport(&m_address, &s_outputReport));
|
2020-07-27 17:42:40 +00:00
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|