2020-08-25 19:23:38 +00:00
|
|
|
/*
|
2021-01-26 03:03:26 +00:00
|
|
|
* Copyright (c) 2020-2021 ndeadly
|
2020-08-25 19:23:38 +00:00
|
|
|
*
|
2020-08-26 22:50:34 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
2020-08-25 19:23:38 +00:00
|
|
|
*
|
2020-08-26 22:50:34 +00:00
|
|
|
* This program is distributed in the hope 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.
|
2020-08-25 19:23:38 +00:00
|
|
|
*
|
|
|
|
* 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 "dualshock4_controller.hpp"
|
2021-05-31 21:56:03 +00:00
|
|
|
#include "../mcmitm_config.hpp"
|
2020-06-10 21:16:08 +00:00
|
|
|
#include <switch.h>
|
2020-06-17 23:36:56 +00:00
|
|
|
#include <stratosphere.hpp>
|
2020-08-26 18:52:47 +00:00
|
|
|
#include <cstring>
|
2020-06-09 20:39:30 +00:00
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
namespace ams::controller {
|
2020-06-02 21:24:40 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-08-26 18:52:47 +00:00
|
|
|
const constexpr float stick_scale_factor = float(UINT12_MAX) / UINT8_MAX;
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2021-05-31 21:56:03 +00:00
|
|
|
const constexpr RGBColour led_disable = {0x00, 0x00, 0x00};
|
|
|
|
|
2020-10-04 15:16:49 +00:00
|
|
|
const RGBColour player_led_colours[] = {
|
2020-10-14 17:57:33 +00:00
|
|
|
// Same colours used by PS4
|
|
|
|
{0x00, 0x00, 0x40}, // blue
|
|
|
|
{0x40, 0x00, 0x00}, // red
|
|
|
|
{0x00, 0x40, 0x00}, // green
|
|
|
|
{0x20, 0x00, 0x20}, // pink
|
|
|
|
// New colours for controllers 5-8
|
|
|
|
{0x00, 0x20, 0x20}, // cyan
|
|
|
|
{0x30, 0x10, 0x00}, // orange
|
|
|
|
{0x20, 0x20, 0x00}, // yellow
|
|
|
|
{0x10, 0x00, 0x30} // purple
|
2020-07-10 19:37:31 +00:00
|
|
|
};
|
|
|
|
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 18:52:47 +00:00
|
|
|
Result Dualshock4Controller::Initialize(void) {
|
|
|
|
R_TRY(EmulatedSwitchController::Initialize());
|
2020-10-06 18:40:19 +00:00
|
|
|
R_TRY(this->PushRumbleLedState());
|
2020-08-26 18:52:47 +00:00
|
|
|
|
2020-08-07 21:30:27 +00:00
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2021-03-15 23:09:21 +00:00
|
|
|
Result Dualshock4Controller::SetVibration(const SwitchRumbleData *rumble_data) {
|
|
|
|
m_rumble_state.amp_motor_left = static_cast<uint8_t>(255 * rumble_data->low_band_amp);
|
|
|
|
m_rumble_state.amp_motor_right = static_cast<uint8_t>(255 * rumble_data->high_band_amp);
|
2020-09-30 23:03:34 +00:00
|
|
|
return this->PushRumbleLedState();
|
|
|
|
}
|
|
|
|
|
2020-10-03 09:53:12 +00:00
|
|
|
Result Dualshock4Controller::CancelVibration(void) {
|
|
|
|
m_rumble_state.amp_motor_left = 0;
|
|
|
|
m_rumble_state.amp_motor_right = 0;
|
2020-10-13 19:35:31 +00:00
|
|
|
return this->PushRumbleLedState();
|
2020-10-03 09:53:12 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 18:52:47 +00:00
|
|
|
Result Dualshock4Controller::SetPlayerLed(uint8_t led_mask) {
|
2020-10-18 21:38:00 +00:00
|
|
|
uint8_t player_number;
|
|
|
|
R_TRY(LedsMaskToPlayerNumber(led_mask, &player_number));
|
|
|
|
RGBColour colour = player_led_colours[player_number];
|
2020-08-26 18:52:47 +00:00
|
|
|
return this->SetLightbarColour(colour);
|
2020-07-27 17:42:40 +00:00
|
|
|
}
|
2020-06-10 21:16:08 +00:00
|
|
|
|
2020-10-04 15:16:49 +00:00
|
|
|
Result Dualshock4Controller::SetLightbarColour(RGBColour colour) {
|
2021-05-31 21:56:03 +00:00
|
|
|
auto config = mitm::GetGlobalConfig();
|
|
|
|
m_led_colour = config->misc.disable_sony_leds ? led_disable : colour;
|
2020-10-06 18:40:19 +00:00
|
|
|
return this->PushRumbleLedState();
|
2020-06-09 20:39:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 15:22:21 +00:00
|
|
|
void Dualshock4Controller::UpdateControllerState(const bluetooth::HidReport *report) {
|
|
|
|
auto ds4_report = reinterpret_cast<const Dualshock4ReportData *>(&report->data);
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-08-26 18:52:47 +00:00
|
|
|
switch(ds4_report->id) {
|
2020-06-09 20:39:30 +00:00
|
|
|
case 0x01:
|
2020-10-03 15:22:21 +00:00
|
|
|
this->HandleInputReport0x01(ds4_report);
|
2020-06-09 20:39:30 +00:00
|
|
|
break;
|
|
|
|
case 0x11:
|
2020-10-03 15:22:21 +00:00
|
|
|
this->HandleInputReport0x11(ds4_report);
|
2020-06-09 20:39:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 15:22:21 +00:00
|
|
|
void Dualshock4Controller::HandleInputReport0x01(const Dualshock4ReportData *src) {
|
2021-06-20 20:39:28 +00:00
|
|
|
m_left_stick.SetData(
|
2020-08-26 18:52:47 +00:00
|
|
|
static_cast<uint16_t>(stick_scale_factor * src->input0x01.left_stick.x) & 0xfff,
|
|
|
|
static_cast<uint16_t>(stick_scale_factor * (UINT8_MAX - src->input0x01.left_stick.y)) & 0xfff
|
2020-07-01 23:34:13 +00:00
|
|
|
);
|
2021-06-20 20:39:28 +00:00
|
|
|
m_right_stick.SetData(
|
2020-08-26 18:52:47 +00:00
|
|
|
static_cast<uint16_t>(stick_scale_factor * src->input0x01.right_stick.x) & 0xfff,
|
|
|
|
static_cast<uint16_t>(stick_scale_factor * (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-10-03 15:22:21 +00:00
|
|
|
this->MapButtons(&src->input0x01.buttons);
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 15:22:21 +00:00
|
|
|
void Dualshock4Controller::HandleInputReport0x11(const Dualshock4ReportData *src) {
|
2021-09-15 12:00:51 +00:00
|
|
|
m_ext_power = src->input0x11.usb;
|
|
|
|
|
2020-08-08 00:05:41 +00:00
|
|
|
if (!src->input0x11.usb || src->input0x11.battery_level > 10)
|
|
|
|
m_charging = false;
|
|
|
|
else
|
|
|
|
m_charging = true;
|
|
|
|
|
2020-09-01 20:14:39 +00:00
|
|
|
uint8_t battery_level = src->input0x11.battery_level;
|
|
|
|
if (!src->input0x11.usb)
|
|
|
|
battery_level++;
|
|
|
|
if (battery_level > 10)
|
|
|
|
battery_level = 10;
|
|
|
|
|
|
|
|
m_battery = static_cast<uint8_t>(8 * (battery_level + 1) / 10) & 0x0e;
|
2020-07-09 19:22:14 +00:00
|
|
|
|
2021-06-20 20:39:28 +00:00
|
|
|
m_left_stick.SetData(
|
2020-12-05 11:55:07 +00:00
|
|
|
static_cast<uint16_t>(stick_scale_factor * src->input0x11.left_stick.x) & 0xfff,
|
|
|
|
static_cast<uint16_t>(stick_scale_factor * (UINT8_MAX - src->input0x11.left_stick.y)) & 0xfff
|
2020-07-01 23:34:13 +00:00
|
|
|
);
|
2021-06-20 20:39:28 +00:00
|
|
|
m_right_stick.SetData(
|
2020-12-05 11:55:07 +00:00
|
|
|
static_cast<uint16_t>(stick_scale_factor * src->input0x11.right_stick.x) & 0xfff,
|
|
|
|
static_cast<uint16_t>(stick_scale_factor * (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-10-03 15:22:21 +00:00
|
|
|
this->MapButtons(&src->input0x11.buttons);
|
2020-08-07 00:12:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 15:22:21 +00:00
|
|
|
void Dualshock4Controller::MapButtons(const Dualshock4ButtonData *buttons) {
|
|
|
|
m_buttons.dpad_down = (buttons->dpad == Dualshock4DPad_S) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_SE) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_SW);
|
|
|
|
m_buttons.dpad_up = (buttons->dpad == Dualshock4DPad_N) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_NE) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_NW);
|
|
|
|
m_buttons.dpad_right = (buttons->dpad == Dualshock4DPad_E) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_NE) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_SE);
|
|
|
|
m_buttons.dpad_left = (buttons->dpad == Dualshock4DPad_W) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_NW) ||
|
|
|
|
(buttons->dpad == Dualshock4DPad_SW);
|
|
|
|
|
|
|
|
m_buttons.A = buttons->circle;
|
|
|
|
m_buttons.B = buttons->cross;
|
|
|
|
m_buttons.X = buttons->triangle;
|
|
|
|
m_buttons.Y = buttons->square;
|
|
|
|
|
|
|
|
m_buttons.R = buttons->R1;
|
|
|
|
m_buttons.ZR = buttons->R2;
|
|
|
|
m_buttons.L = buttons->L1;
|
|
|
|
m_buttons.ZL = buttons->L2;
|
|
|
|
|
|
|
|
m_buttons.minus = buttons->share;
|
|
|
|
m_buttons.plus = buttons->options;
|
|
|
|
|
|
|
|
m_buttons.lstick_press = buttons->L3;
|
|
|
|
m_buttons.rstick_press = buttons->R3;
|
|
|
|
|
|
|
|
m_buttons.capture = buttons->tpad;
|
|
|
|
m_buttons.home = buttons->ps;
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 18:40:19 +00:00
|
|
|
Result Dualshock4Controller::PushRumbleLedState(void) {
|
2021-07-06 14:19:49 +00:00
|
|
|
Dualshock4OutputReport0x11 report = {0xa2, 0x11, static_cast<uint8_t>(0xc0 | (m_report_rate & 0xff)), 0x20, 0xf3, 0x04, 0x00,
|
|
|
|
m_rumble_state.amp_motor_right, m_rumble_state.amp_motor_left,
|
2020-09-30 23:03:34 +00:00
|
|
|
m_led_colour.r, m_led_colour.g, m_led_colour.b
|
|
|
|
};
|
2020-07-27 17:42:40 +00:00
|
|
|
report.crc = crc32Calculate(report.data, sizeof(report.data));
|
|
|
|
|
2020-08-26 18:52:47 +00:00
|
|
|
s_output_report.size = sizeof(report) - 1;
|
|
|
|
std::memcpy(s_output_report.data, &report.data[1], s_output_report.size);
|
2020-07-27 17:42:40 +00:00
|
|
|
|
2020-10-03 15:22:21 +00:00
|
|
|
return bluetooth::hid::report::SendHidReport(&m_address, &s_output_report);
|
2020-07-27 17:42:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|