libgui/include/gui/WPadController.h

218 lines
6.3 KiB
C
Raw Permalink Normal View History

2017-10-29 10:28:14 +01:00
/****************************************************************************
* Copyright (C) 2015 Dimok
*
* 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/>.
****************************************************************************/
#ifndef WPAD_CONTROLLER_H_
#define WPAD_CONTROLLER_H_
2018-06-21 20:44:58 +02:00
#include <gui/GuiController.h>
#include <padscore/kpad.h>
#include <padscore/wpad.h>
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
class WPadController : public GuiController {
2017-10-29 10:28:14 +01:00
public:
//!Constructor
2018-06-21 20:44:58 +02:00
WPadController(int32_t channel)
2020-08-13 12:38:07 +02:00
: GuiController(channel) {
2017-10-29 10:28:14 +01:00
memset(&kpadData, 0, sizeof(kpadData));
}
//!Destructor
2020-08-13 12:38:07 +02:00
virtual ~WPadController() {}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
uint32_t remapWiiMoteButtons(uint32_t buttons) {
uint32_t conv_buttons = 0;
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_BUTTON_LEFT) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_LEFT;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_BUTTON_RIGHT) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_RIGHT;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_BUTTON_DOWN) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_DOWN;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_BUTTON_UP) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_UP;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_BUTTON_PLUS) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_PLUS;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_BUTTON_2) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_2;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_BUTTON_1) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_1;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_BUTTON_B) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_B;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_BUTTON_A) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_A;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_BUTTON_MINUS) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_MINUS;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_BUTTON_Z) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_Z;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_BUTTON_C) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_C;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_BUTTON_HOME) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_HOME;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
return conv_buttons;
}
2020-08-13 12:38:07 +02:00
2018-06-21 20:44:58 +02:00
uint32_t remapClassicButtons(uint32_t buttons) {
uint32_t conv_buttons = 0;
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_CLASSIC_BUTTON_LEFT) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_LEFT;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_CLASSIC_BUTTON_RIGHT) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_RIGHT;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_CLASSIC_BUTTON_DOWN) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_DOWN;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_CLASSIC_BUTTON_UP) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_UP;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_CLASSIC_BUTTON_PLUS) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_PLUS;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_CLASSIC_BUTTON_X) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_X;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_CLASSIC_BUTTON_Y) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_Y;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_CLASSIC_BUTTON_B) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_B;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_CLASSIC_BUTTON_A) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_A;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_CLASSIC_BUTTON_MINUS) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_MINUS;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_CLASSIC_BUTTON_HOME) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_HOME;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_CLASSIC_BUTTON_ZR) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_ZR;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_CLASSIC_BUTTON_ZL) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_ZL;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_CLASSIC_BUTTON_R) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_R;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buttons & WPAD_CLASSIC_BUTTON_L) {
2017-10-29 10:28:14 +01:00
conv_buttons |= GuiTrigger::BUTTON_L;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
return conv_buttons;
}
2018-06-21 20:44:58 +02:00
WPADChan getChanByInt(int32_t chan) {
2020-08-13 12:38:07 +02:00
if (chan == 0) {
2018-06-21 20:44:58 +02:00
return WPAD_CHAN_0;
}
2020-08-13 12:38:07 +02:00
if (chan == 1) {
2018-06-21 20:44:58 +02:00
return WPAD_CHAN_1;
}
2020-08-13 12:38:07 +02:00
if (chan == 2) {
2018-06-21 20:44:58 +02:00
return WPAD_CHAN_3;
}
2020-08-13 12:38:07 +02:00
if (chan == 3) {
2018-06-21 20:44:58 +02:00
return WPAD_CHAN_3;
}
return WPAD_CHAN_0;
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
bool update(int32_t width, int32_t height) {
lastData = data;
WPADExtensionType controller_type;
2017-10-29 10:28:14 +01:00
//! check if the controller is connected
2020-08-13 12:58:19 +02:00
if (WPADProbe(getChanByInt(chanIdx - 1), &controller_type) != 0) {
2017-10-29 10:28:14 +01:00
return false;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
KPADRead(getChanByInt(chanIdx - 1), &kpadData, 1);
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (kpadData.extensionType <= 1) {
2018-06-21 20:44:58 +02:00
data.buttons_r = remapWiiMoteButtons(kpadData.release);
data.buttons_h = remapWiiMoteButtons(kpadData.hold);
data.buttons_d = remapWiiMoteButtons(kpadData.trigger);
} else {
data.buttons_r = remapClassicButtons(kpadData.classic.release);
data.buttons_h = remapClassicButtons(kpadData.classic.hold);
data.buttons_d = remapClassicButtons(kpadData.classic.trigger);
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
data.validPointer = (kpadData.posValid == 1 || kpadData.posValid == 2) && (kpadData.pos.x >= -1.0f && kpadData.pos.x <= 1.0f) && (kpadData.pos.y >= -1.0f && kpadData.pos.y <= 1.0f);
2017-10-29 10:28:14 +01:00
//! calculate the screen offsets if pointer is valid else leave old value
2020-08-13 12:38:07 +02:00
if (data.validPointer) {
2018-06-21 20:44:58 +02:00
data.x = (width >> 1) * kpadData.pos.x;
data.y = (height >> 1) * (-kpadData.pos.y);
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (kpadData.angle.y > 0.0f) {
2018-06-21 20:44:58 +02:00
data.pointerAngle = (-kpadData.angle.x + 1.0f) * 0.5f * 180.0f;
2020-08-13 12:58:19 +02:00
} else {
2018-06-21 20:44:58 +02:00
data.pointerAngle = (kpadData.angle.x + 1.0f) * 0.5f * 180.0f - 180.0f;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
}
return true;
}
private:
2018-06-21 20:44:58 +02:00
KPADStatus kpadData;
uint32_t lastButtons;
2017-10-29 10:28:14 +01:00
};
#endif