2020-08-25 19:23:38 +00:00
|
|
|
/*
|
2022-03-28 02:04:49 +00:00
|
|
|
* Copyright (c) 2020-2022 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 "xbox_one_controller.hpp"
|
2020-06-17 23:36:56 +00:00
|
|
|
#include <stratosphere.hpp>
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
namespace ams::controller {
|
2020-06-02 21:24:40 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-03-15 23:09:21 +00:00
|
|
|
constexpr float stick_scale_factor = float(UINT12_MAX) / UINT16_MAX;
|
2020-12-20 16:27:58 +00:00
|
|
|
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 23:09:21 +00:00
|
|
|
Result XboxOneController::SetVibration(const SwitchRumbleData *rumble_data) {
|
2021-11-01 19:26:46 +00:00
|
|
|
auto report = reinterpret_cast<XboxOneReportData *>(m_output_report.data);
|
|
|
|
m_output_report.size = sizeof(XboxOneOutputReport0x03) + 1;
|
2020-10-12 21:57:27 +00:00
|
|
|
report->id = 0x03;
|
2021-11-01 19:26:46 +00:00
|
|
|
report->output0x03.enable = 0x3;
|
2021-11-16 23:06:00 +00:00
|
|
|
report->output0x03.magnitude_strong = static_cast<uint8_t>(100 * std::max(rumble_data[0].low_band_amp, rumble_data[1].low_band_amp));
|
|
|
|
report->output0x03.magnitude_weak = static_cast<uint8_t>(100 * std::max(rumble_data[0].high_band_amp, rumble_data[1].high_band_amp));
|
2021-11-01 19:26:46 +00:00
|
|
|
report->output0x03.pulse_sustain_10ms = 1;
|
|
|
|
report->output0x03.pulse_release_10ms = 0;
|
|
|
|
report->output0x03.loop_count = 0;
|
|
|
|
|
2022-03-30 08:27:24 +00:00
|
|
|
return this->WriteDataReport(&m_output_report);
|
2020-10-03 09:53:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 08:27:24 +00:00
|
|
|
void XboxOneController::ProcessInputData(const bluetooth::HidReport *report) {
|
2020-10-03 15:22:21 +00:00
|
|
|
auto xbox_report = reinterpret_cast<const XboxOneReportData *>(&report->data);
|
2020-06-09 20:39:30 +00:00
|
|
|
|
2020-08-26 18:52:47 +00:00
|
|
|
switch(xbox_report->id) {
|
2020-06-02 21:24:40 +00:00
|
|
|
case 0x01:
|
2022-03-30 08:27:24 +00:00
|
|
|
this->MapInputReport0x01(xbox_report, report->size >= sizeof(XboxOneInputReport0x01) + 1); break;
|
2021-06-08 18:55:36 +00:00
|
|
|
case 0x02:
|
2022-03-30 08:27:24 +00:00
|
|
|
this->MapInputReport0x02(xbox_report); break;
|
2020-07-10 20:02:24 +00:00
|
|
|
case 0x04:
|
2022-03-30 08:27:24 +00:00
|
|
|
this->MapInputReport0x04(xbox_report); break;
|
2020-06-02 21:24:40 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:27:24 +00:00
|
|
|
void XboxOneController::MapInputReport0x01(const XboxOneReportData *src, bool new_format) {
|
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 * (UINT16_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 * (UINT16_MAX - src->input0x01.right_stick.y)) & 0xfff
|
2020-07-01 23:34:13 +00:00
|
|
|
);
|
2020-07-01 13:21:05 +00:00
|
|
|
|
2020-10-03 15:22:21 +00:00
|
|
|
m_buttons.ZR = src->input0x01.right_trigger > 0;
|
|
|
|
m_buttons.ZL = src->input0x01.left_trigger > 0;
|
|
|
|
|
2021-06-08 18:55:36 +00:00
|
|
|
if (new_format) {
|
2021-11-01 19:26:46 +00:00
|
|
|
m_buttons.dpad_down = (src->input0x01.buttons.dpad == XboxOneDPad_S) ||
|
|
|
|
(src->input0x01.buttons.dpad == XboxOneDPad_SE) ||
|
|
|
|
(src->input0x01.buttons.dpad == XboxOneDPad_SW);
|
|
|
|
m_buttons.dpad_up = (src->input0x01.buttons.dpad == XboxOneDPad_N) ||
|
|
|
|
(src->input0x01.buttons.dpad == XboxOneDPad_NE) ||
|
|
|
|
(src->input0x01.buttons.dpad == XboxOneDPad_NW);
|
|
|
|
m_buttons.dpad_right = (src->input0x01.buttons.dpad == XboxOneDPad_E) ||
|
|
|
|
(src->input0x01.buttons.dpad == XboxOneDPad_NE) ||
|
|
|
|
(src->input0x01.buttons.dpad == XboxOneDPad_SE);
|
|
|
|
m_buttons.dpad_left = (src->input0x01.buttons.dpad == XboxOneDPad_W) ||
|
|
|
|
(src->input0x01.buttons.dpad == XboxOneDPad_NW) ||
|
|
|
|
(src->input0x01.buttons.dpad == XboxOneDPad_SW);
|
2021-06-08 18:55:36 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-11-01 19:26:46 +00:00
|
|
|
m_buttons.R = src->input0x01.buttons.RB;
|
|
|
|
m_buttons.L = src->input0x01.buttons.LB;
|
2021-06-08 18:55:36 +00:00
|
|
|
|
|
|
|
m_buttons.minus = src->input0x01.buttons.view;
|
|
|
|
m_buttons.plus = src->input0x01.buttons.menu;
|
|
|
|
|
|
|
|
m_buttons.lstick_press = src->input0x01.buttons.lstick_press;
|
|
|
|
m_buttons.rstick_press = src->input0x01.buttons.rstick_press;
|
|
|
|
|
|
|
|
m_buttons.home = src->input0x01.buttons.guide;
|
|
|
|
}
|
|
|
|
else {
|
2021-11-01 19:26:46 +00:00
|
|
|
m_buttons.dpad_down = (src->input0x01.old.buttons.dpad == XboxOneDPad_S) ||
|
|
|
|
(src->input0x01.old.buttons.dpad == XboxOneDPad_SE) ||
|
|
|
|
(src->input0x01.old.buttons.dpad == XboxOneDPad_SW);
|
|
|
|
m_buttons.dpad_up = (src->input0x01.old.buttons.dpad == XboxOneDPad_N) ||
|
|
|
|
(src->input0x01.old.buttons.dpad == XboxOneDPad_NE) ||
|
|
|
|
(src->input0x01.old.buttons.dpad == XboxOneDPad_NW);
|
|
|
|
m_buttons.dpad_right = (src->input0x01.old.buttons.dpad == XboxOneDPad_E) ||
|
|
|
|
(src->input0x01.old.buttons.dpad == XboxOneDPad_NE) ||
|
|
|
|
(src->input0x01.old.buttons.dpad == XboxOneDPad_SE);
|
|
|
|
m_buttons.dpad_left = (src->input0x01.old.buttons.dpad == XboxOneDPad_W) ||
|
|
|
|
(src->input0x01.old.buttons.dpad == XboxOneDPad_NW) ||
|
|
|
|
(src->input0x01.old.buttons.dpad == XboxOneDPad_SW);
|
2021-06-08 18:55:36 +00:00
|
|
|
|
|
|
|
m_buttons.A = src->input0x01.old.buttons.B;
|
|
|
|
m_buttons.B = src->input0x01.old.buttons.A;
|
|
|
|
m_buttons.X = src->input0x01.old.buttons.Y;
|
|
|
|
m_buttons.Y = src->input0x01.old.buttons.X;
|
|
|
|
|
2021-11-01 19:26:46 +00:00
|
|
|
m_buttons.R = src->input0x01.old.buttons.RB;
|
|
|
|
m_buttons.L = src->input0x01.old.buttons.LB;
|
2021-06-08 18:55:36 +00:00
|
|
|
|
|
|
|
m_buttons.minus = src->input0x01.old.buttons.view;
|
|
|
|
m_buttons.plus = src->input0x01.old.buttons.menu;
|
|
|
|
|
|
|
|
m_buttons.lstick_press = src->input0x01.old.buttons.lstick_press;
|
|
|
|
m_buttons.rstick_press = src->input0x01.old.buttons.rstick_press;
|
|
|
|
}
|
|
|
|
}
|
2020-10-03 15:22:21 +00:00
|
|
|
|
2022-03-30 08:27:24 +00:00
|
|
|
void XboxOneController::MapInputReport0x02(const XboxOneReportData *src) {
|
2021-06-08 18:55:36 +00:00
|
|
|
m_buttons.home = src->input0x02.guide;
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 08:27:24 +00:00
|
|
|
void XboxOneController::MapInputReport0x04(const XboxOneReportData *src) {
|
2021-09-15 12:00:51 +00:00
|
|
|
m_ext_power = src->input0x04.mode != XboxOnePowerMode_Battery;
|
2021-09-15 11:45:12 +00:00
|
|
|
m_battery = (src->input0x04.mode == XboxOnePowerMode_USB) ? BATTERY_MAX : src->input0x04.capacity << 1;
|
2020-08-06 19:33:41 +00:00
|
|
|
m_charging = src->input0x04.charging;
|
2020-07-10 20:02:24 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|