2015-08-18 22:43:30 +00:00
|
|
|
#include "boo/inputdev/DolphinSmashAdapter.hpp"
|
2018-10-07 02:49:22 +00:00
|
|
|
#include "boo/inputdev/DeviceSignature.hpp"
|
2019-08-19 23:08:54 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
namespace boo {
|
2015-04-29 10:24:39 +00:00
|
|
|
/*
|
|
|
|
* Reference: https://github.com/ToadKing/wii-u-gc-adapter/blob/master/wii-u-gc-adapter.c
|
2015-04-24 00:24:15 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
DolphinSmashAdapter::DolphinSmashAdapter(DeviceToken* token)
|
|
|
|
: TDeviceBase<IDolphinSmashAdapterCallback>(dev_typeid(DolphinSmashAdapter), token) {}
|
2015-04-23 00:46:32 +00:00
|
|
|
|
2017-01-27 07:41:45 +00:00
|
|
|
DolphinSmashAdapter::~DolphinSmashAdapter() {}
|
2015-04-24 00:24:15 +00:00
|
|
|
|
2019-09-06 10:19:53 +00:00
|
|
|
static constexpr EDolphinControllerType parseType(unsigned char status) {
|
|
|
|
const auto type =
|
2018-12-08 05:17:51 +00:00
|
|
|
EDolphinControllerType(status) & (EDolphinControllerType::Normal | EDolphinControllerType::Wavebird);
|
2019-09-06 10:19:53 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
switch (type) {
|
|
|
|
case EDolphinControllerType::Normal:
|
|
|
|
case EDolphinControllerType::Wavebird:
|
|
|
|
return type;
|
|
|
|
default:
|
|
|
|
return EDolphinControllerType::None;
|
|
|
|
}
|
2015-04-24 00:24:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 10:19:53 +00:00
|
|
|
static EDolphinControllerType parseState(DolphinControllerState* stateOut, const uint8_t* payload, bool& rumble) {
|
|
|
|
const unsigned char status = payload[0];
|
|
|
|
const EDolphinControllerType type = parseType(status);
|
2017-01-27 07:41:45 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
rumble = ((status & 0x04) != 0) ? true : false;
|
2017-01-27 07:41:45 +00:00
|
|
|
|
2019-09-06 10:19:53 +00:00
|
|
|
stateOut->m_btns = static_cast<uint16_t>(payload[1]) << 8 | static_cast<uint16_t>(payload[2]);
|
2017-01-27 07:41:45 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
stateOut->m_leftStick[0] = payload[3];
|
|
|
|
stateOut->m_leftStick[1] = payload[4];
|
|
|
|
stateOut->m_rightStick[0] = payload[5];
|
|
|
|
stateOut->m_rightStick[1] = payload[6];
|
|
|
|
stateOut->m_analogTriggers[0] = payload[7];
|
|
|
|
stateOut->m_analogTriggers[1] = payload[8];
|
2017-01-27 07:41:45 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
return type;
|
2015-04-22 21:48:23 +00:00
|
|
|
}
|
2015-04-21 04:02:43 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
void DolphinSmashAdapter::initialCycle() {
|
2019-09-06 10:19:53 +00:00
|
|
|
constexpr std::array<uint8_t, 1> handshakePayload{0x13};
|
|
|
|
sendUSBInterruptTransfer(handshakePayload.data(), handshakePayload.size());
|
2015-05-04 04:28:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
void DolphinSmashAdapter::transferCycle() {
|
2019-09-06 10:19:53 +00:00
|
|
|
std::array<uint8_t, 37> payload;
|
|
|
|
const size_t recvSz = receiveUSBInterruptTransfer(payload.data(), payload.size());
|
|
|
|
if (recvSz != payload.size() || payload[0] != 0x21) {
|
2018-12-08 05:17:51 +00:00
|
|
|
return;
|
2019-09-06 10:19:53 +00:00
|
|
|
}
|
2018-12-08 05:17:51 +00:00
|
|
|
|
2019-08-24 20:54:11 +00:00
|
|
|
// fmt::print("RECEIVED DATA {} {:02X}\n", recvSz, payload[0]);
|
2018-12-08 05:17:51 +00:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lk(m_callbackLock);
|
2019-09-06 10:19:53 +00:00
|
|
|
if (!m_callback) {
|
2018-12-08 05:17:51 +00:00
|
|
|
return;
|
2019-09-06 10:19:53 +00:00
|
|
|
}
|
2018-12-08 05:17:51 +00:00
|
|
|
|
|
|
|
/* Parse controller states */
|
2019-09-06 10:19:53 +00:00
|
|
|
const uint8_t* controller = &payload[1];
|
2018-12-08 05:17:51 +00:00
|
|
|
uint8_t rumbleMask = 0;
|
|
|
|
for (uint32_t i = 0; i < 4; i++, controller += 9) {
|
|
|
|
DolphinControllerState state;
|
|
|
|
bool rumble = false;
|
2019-09-06 10:19:53 +00:00
|
|
|
const EDolphinControllerType type = parseState(&state, controller, rumble);
|
|
|
|
|
|
|
|
if (True(type) && (m_knownControllers & (1U << i)) == 0) {
|
|
|
|
m_leftStickCal = state.m_leftStick;
|
|
|
|
m_rightStickCal = state.m_rightStick;
|
|
|
|
m_triggersCal = state.m_analogTriggers;
|
|
|
|
m_knownControllers |= 1U << i;
|
2018-12-08 05:17:51 +00:00
|
|
|
m_callback->controllerConnected(i, type);
|
2019-09-06 10:19:53 +00:00
|
|
|
} else if (False(type) && (m_knownControllers & (1U << i)) != 0) {
|
|
|
|
m_knownControllers &= ~(1U << i);
|
2018-12-08 05:17:51 +00:00
|
|
|
m_callback->controllerDisconnected(i);
|
2015-04-30 23:17:46 +00:00
|
|
|
}
|
2019-09-06 10:19:53 +00:00
|
|
|
|
|
|
|
if ((m_knownControllers & (1U << i)) != 0) {
|
2018-12-08 05:17:51 +00:00
|
|
|
state.m_leftStick[0] = state.m_leftStick[0] - m_leftStickCal[0];
|
|
|
|
state.m_leftStick[1] = state.m_leftStick[1] - m_leftStickCal[1];
|
|
|
|
state.m_rightStick[0] = state.m_rightStick[0] - m_rightStickCal[0];
|
|
|
|
state.m_rightStick[1] = state.m_rightStick[1] - m_rightStickCal[1];
|
|
|
|
state.m_analogTriggers[0] = state.m_analogTriggers[0] - m_triggersCal[0];
|
|
|
|
state.m_analogTriggers[1] = state.m_analogTriggers[1] - m_triggersCal[1];
|
|
|
|
m_callback->controllerUpdate(i, type, state);
|
|
|
|
}
|
2019-09-06 10:19:53 +00:00
|
|
|
|
|
|
|
rumbleMask |= rumble ? 1U << i : 0;
|
2018-12-08 05:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send rumble message (if needed) */
|
2019-09-06 10:19:53 +00:00
|
|
|
const uint8_t rumbleReq = m_rumbleRequest & rumbleMask;
|
2018-12-08 05:17:51 +00:00
|
|
|
if (rumbleReq != m_rumbleState) {
|
2019-09-06 10:19:53 +00:00
|
|
|
std::array<uint8_t, 5> rumbleMessage{0x11, 0, 0, 0, 0};
|
|
|
|
for (size_t i = 0; i < 4; ++i) {
|
|
|
|
if ((rumbleReq & (1U << i)) != 0) {
|
2018-12-08 05:17:51 +00:00
|
|
|
rumbleMessage[i + 1] = 1;
|
2019-09-06 10:19:53 +00:00
|
|
|
} else if (m_hardStop[i]) {
|
2018-12-08 05:17:51 +00:00
|
|
|
rumbleMessage[i + 1] = 2;
|
2019-09-06 10:19:53 +00:00
|
|
|
} else {
|
2018-12-08 05:17:51 +00:00
|
|
|
rumbleMessage[i + 1] = 0;
|
2019-09-06 10:19:53 +00:00
|
|
|
}
|
2015-04-24 00:24:15 +00:00
|
|
|
}
|
2015-04-30 07:01:55 +00:00
|
|
|
|
2019-09-06 10:19:53 +00:00
|
|
|
sendUSBInterruptTransfer(rumbleMessage.data(), rumbleMessage.size());
|
2018-12-08 05:17:51 +00:00
|
|
|
m_rumbleState = rumbleReq;
|
|
|
|
}
|
2015-04-30 07:01:55 +00:00
|
|
|
}
|
2015-04-26 08:25:44 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
void DolphinSmashAdapter::finalCycle() {
|
2019-09-06 10:19:53 +00:00
|
|
|
constexpr std::array<uint8_t, 5> rumbleMessage{0x11, 0, 0, 0, 0};
|
|
|
|
sendUSBInterruptTransfer(rumbleMessage.data(), sizeof(rumbleMessage));
|
2018-12-08 05:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DolphinSmashAdapter::deviceDisconnected() {
|
|
|
|
for (uint32_t i = 0; i < 4; i++) {
|
2019-09-06 10:19:53 +00:00
|
|
|
if ((m_knownControllers & (1U << i)) != 0) {
|
|
|
|
m_knownControllers &= ~(1U << i);
|
2018-12-08 05:17:51 +00:00
|
|
|
std::lock_guard<std::mutex> lk(m_callbackLock);
|
2019-09-06 10:19:53 +00:00
|
|
|
if (m_callback) {
|
2018-12-08 05:17:51 +00:00
|
|
|
m_callback->controllerDisconnected(i);
|
2019-09-06 10:19:53 +00:00
|
|
|
}
|
2015-08-29 01:30:21 +00:00
|
|
|
}
|
2018-12-08 05:17:51 +00:00
|
|
|
}
|
2015-08-29 01:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The following code is derived from pad.c in libogc
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004 - 2009
|
|
|
|
* Michael Wiedenbauer (shagkur)
|
|
|
|
* Dave Murphy (WinterMute)
|
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty. In no event will the authors be held liable for any
|
|
|
|
* damages arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any
|
|
|
|
* purpose, including commercial applications, and to alter it and
|
|
|
|
* redistribute it freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you
|
|
|
|
* must not claim that you wrote the original software. If you use
|
|
|
|
* this software in a product, an acknowledgment in the product
|
|
|
|
* documentation would be appreciated but is not required.
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and
|
|
|
|
* must not be misrepresented as being the original software.
|
|
|
|
* 3. This notice may not be removed or altered from any source
|
|
|
|
* distribution.
|
|
|
|
*/
|
|
|
|
|
2019-09-06 10:19:53 +00:00
|
|
|
constexpr std::array<int16_t, 8> pad_clampregion{
|
|
|
|
30, 180, 15, 72, 40, 15, 59, 31,
|
|
|
|
};
|
2015-08-29 01:30:21 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
static void pad_clampstick(int16_t& px, int16_t& py, int16_t max, int16_t xy, int16_t min) {
|
|
|
|
int x = px;
|
|
|
|
int y = py;
|
|
|
|
|
2019-09-06 10:19:53 +00:00
|
|
|
int signX;
|
2018-12-08 05:17:51 +00:00
|
|
|
if (x > 0) {
|
|
|
|
signX = 1;
|
|
|
|
} else {
|
|
|
|
signX = -1;
|
|
|
|
x = -x;
|
|
|
|
}
|
|
|
|
|
2019-09-06 10:19:53 +00:00
|
|
|
int signY;
|
2018-12-08 05:17:51 +00:00
|
|
|
if (y > 0) {
|
|
|
|
signY = 1;
|
|
|
|
} else {
|
|
|
|
signY = -1;
|
|
|
|
y = -y;
|
|
|
|
}
|
|
|
|
|
2019-09-06 10:19:53 +00:00
|
|
|
if (x <= min) {
|
2018-12-08 05:17:51 +00:00
|
|
|
x = 0;
|
2019-09-06 10:19:53 +00:00
|
|
|
} else {
|
2018-12-08 05:17:51 +00:00
|
|
|
x -= min;
|
2019-09-06 10:19:53 +00:00
|
|
|
}
|
2018-12-08 05:17:51 +00:00
|
|
|
|
|
|
|
if (y <= min) {
|
|
|
|
y = 0;
|
|
|
|
} else {
|
|
|
|
y -= min;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x == 0 && y == 0) {
|
|
|
|
px = py = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xy * y <= xy * x) {
|
2019-09-06 10:19:53 +00:00
|
|
|
const int d = xy * x + (max - xy) * y;
|
2018-12-08 05:17:51 +00:00
|
|
|
if (xy * max < d) {
|
|
|
|
x = int16_t(xy * max * x / d);
|
|
|
|
y = int16_t(xy * max * y / d);
|
2017-01-27 07:41:45 +00:00
|
|
|
}
|
2018-12-08 05:17:51 +00:00
|
|
|
} else {
|
2019-09-06 10:19:53 +00:00
|
|
|
const int d = xy * y + (max - xy) * x;
|
2018-12-08 05:17:51 +00:00
|
|
|
if (xy * max < d) {
|
|
|
|
x = int16_t(xy * max * x / d);
|
|
|
|
y = int16_t(xy * max * y / d);
|
2015-08-29 01:30:21 +00:00
|
|
|
}
|
2018-12-08 05:17:51 +00:00
|
|
|
}
|
2017-01-27 07:41:45 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
px = int16_t(signX * x);
|
|
|
|
py = int16_t(signY * y);
|
2015-08-29 01:30:21 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
static void pad_clamptrigger(int16_t& trigger) {
|
2019-09-06 10:19:53 +00:00
|
|
|
const int16_t min = pad_clampregion[0];
|
|
|
|
const int16_t max = pad_clampregion[1];
|
2018-12-08 05:17:51 +00:00
|
|
|
|
2019-09-06 10:19:53 +00:00
|
|
|
if (min > trigger) {
|
2018-12-08 05:17:51 +00:00
|
|
|
trigger = 0;
|
2019-09-06 10:19:53 +00:00
|
|
|
} else {
|
|
|
|
if (max < trigger) {
|
2018-12-08 05:17:51 +00:00
|
|
|
trigger = max;
|
2019-09-06 10:19:53 +00:00
|
|
|
}
|
2018-12-08 05:17:51 +00:00
|
|
|
trigger -= min;
|
|
|
|
}
|
2015-08-29 01:30:21 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
void DolphinControllerState::clamp() {
|
|
|
|
pad_clampstick(m_leftStick[0], m_leftStick[1], pad_clampregion[3], pad_clampregion[4], pad_clampregion[2]);
|
|
|
|
pad_clampstick(m_rightStick[0], m_rightStick[1], pad_clampregion[6], pad_clampregion[7], pad_clampregion[5]);
|
|
|
|
pad_clamptrigger(m_analogTriggers[0]);
|
|
|
|
pad_clamptrigger(m_analogTriggers[1]);
|
2015-04-29 10:24:39 +00:00
|
|
|
}
|
2018-12-08 05:17:51 +00:00
|
|
|
} // namespace boo
|