btdrv-mitm: add button mappings for ipega controller

This commit is contained in:
ndeadly 2020-09-04 17:03:42 +02:00
parent cc4395e3a8
commit d9d4e825b2
2 changed files with 121 additions and 1 deletions

View file

@ -19,11 +19,23 @@
namespace ams::controller {
namespace {
const constexpr float stick_scale_factor = float(UINT12_MAX) / UINT8_MAX;
}
void IpegaController::ConvertReportFormat(const bluetooth::HidReport *in_report, bluetooth::HidReport *out_report) {
auto ipega_report = reinterpret_cast<const IpegaReportData *>(&in_report->data);
auto switch_report = reinterpret_cast<SwitchReportData *>(&out_report->data);
switch(ipega_report->id) {
case 0x02:
this->HandleInputReport0x02(ipega_report, switch_report);
break;
case 0x07:
this->HandleInputReport0x07(ipega_report, switch_report);
break;
default:
break;
}
@ -35,4 +47,54 @@ namespace ams::controller {
switch_report->input0x30.timer = os::ConvertToTimeSpan(os::GetSystemTick()).GetMilliSeconds() & 0xff;
}
void IpegaController::HandleInputReport0x02(const IpegaReportData *src, SwitchReportData *dst) {
dst->input0x30.buttons.home = src->input0x02.home;
this->PackStickData(&dst->input0x30.left_stick, STICK_ZERO, STICK_ZERO);
this->PackStickData(&dst->input0x30.right_stick, STICK_ZERO, STICK_ZERO);
}
void IpegaController::HandleInputReport0x07(const IpegaReportData *src, SwitchReportData *dst) {
this->PackStickData(&dst->input0x30.left_stick,
static_cast<uint16_t>(stick_scale_factor * src->input0x07.left_stick.x) & 0xfff,
static_cast<uint16_t>(stick_scale_factor * (UINT8_MAX - src->input0x07.left_stick.y)) & 0xfff
);
this->PackStickData(&dst->input0x30.right_stick,
static_cast<uint16_t>(stick_scale_factor * src->input0x07.right_stick.x) & 0xfff,
static_cast<uint16_t>(stick_scale_factor * (UINT8_MAX - src->input0x07.right_stick.y)) & 0xfff
);
dst->input0x30.buttons.dpad_down = (src->input0x07.buttons.dpad == IpegaDPad_S) ||
(src->input0x07.buttons.dpad == IpegaDPad_SE) ||
(src->input0x07.buttons.dpad == IpegaDPad_SW);
dst->input0x30.buttons.dpad_up = (src->input0x07.buttons.dpad == IpegaDPad_N) ||
(src->input0x07.buttons.dpad == IpegaDPad_NE) ||
(src->input0x07.buttons.dpad == IpegaDPad_NW);
dst->input0x30.buttons.dpad_right = (src->input0x07.buttons.dpad == IpegaDPad_E) ||
(src->input0x07.buttons.dpad == IpegaDPad_NE) ||
(src->input0x07.buttons.dpad == IpegaDPad_SE);
dst->input0x30.buttons.dpad_left = (src->input0x07.buttons.dpad == IpegaDPad_W) ||
(src->input0x07.buttons.dpad == IpegaDPad_NW) ||
(src->input0x07.buttons.dpad == IpegaDPad_SW);
dst->input0x30.buttons.A = src->input0x07.buttons.B;
dst->input0x30.buttons.B = src->input0x07.buttons.A;
dst->input0x30.buttons.X = src->input0x07.buttons.Y;
dst->input0x30.buttons.Y = src->input0x07.buttons.X;
dst->input0x30.buttons.R = src->input0x07.buttons.RB;
dst->input0x30.buttons.ZR = src->input0x07.buttons.RT;
dst->input0x30.buttons.L = src->input0x07.buttons.LB;
dst->input0x30.buttons.ZL = src->input0x07.buttons.LT;
dst->input0x30.buttons.minus = src->input0x07.buttons.view;
dst->input0x30.buttons.plus = src->input0x07.buttons.menu;
dst->input0x30.buttons.lstick_press = src->input0x07.buttons.lstick_press;
dst->input0x30.buttons.rstick_press = src->input0x07.buttons.rstick_press;
dst->input0x30.buttons.capture = 0;
dst->input0x30.buttons.home = 0;
}
}

View file

@ -19,8 +19,65 @@
namespace ams::controller {
enum IpegaDPadDirection {
IpegaDPad_N,
IpegaDPad_NE,
IpegaDPad_E,
IpegaDPad_SE,
IpegaDPad_S,
IpegaDPad_SW,
IpegaDPad_W,
IpegaDPad_NW,
IpegaDPad_Released = 0x88
};
struct IpegaStickData {
uint8_t x;
uint8_t y;
} __attribute__((packed));
struct IpegaButtonData {
uint8_t dpad;
uint8_t A : 1;
uint8_t B : 1;
uint8_t : 1;
uint8_t X : 1;
uint8_t Y : 1;
uint8_t : 1;
uint8_t LB : 1;
uint8_t RB : 1;
uint8_t LT : 1;
uint8_t RT : 1;
uint8_t view : 1;
uint8_t menu : 1;
uint8_t : 1;
uint8_t lstick_press : 1;
uint8_t rstick_press : 1;
uint8_t : 0;
} __attribute__((packed));
struct IpegaInputReport0x02 {
uint8_t : 6;
uint8_t home : 1;
uint8_t : 0;
} __attribute__((packed));
struct IpegaInputReport0x07 {
IpegaStickData left_stick;
IpegaStickData right_stick;
IpegaButtonData buttons;
uint8_t right_trigger;
uint8_t left_trigger;
} __attribute__((packed));
struct IpegaReportData {
uint8_t id;
union {
IpegaInputReport0x02 input0x02;
IpegaInputReport0x07 input0x07;
};
} __attribute__ ((__packed__));
class IpegaController : public EmulatedSwitchController {
@ -36,7 +93,8 @@ namespace ams::controller {
void ConvertReportFormat(const bluetooth::HidReport *in_report, bluetooth::HidReport *out_report);
private:
void HandleInputReport0x02(const IpegaReportData *src, SwitchReportData *dst);
void HandleInputReport0x07(const IpegaReportData *src, SwitchReportData *dst);
};