| 1 | // Copyright 2021 Dolphin Emulator Project |
| 2 | // Licensed under GPLv2+ |
| 3 | // Refer to the license.txt file included. |
| 4 | |
| 5 | #include "InputCommon/ControllerInterface/Touch/InputOverrider.h" |
| 6 | |
| 7 | #include <array> |
| 8 | #include <map> |
| 9 | #include <optional> |
| 10 | #include <string> |
| 11 | #include <string_view> |
| 12 | #include <utility> |
| 13 | |
| 14 | #include "Common/Assert.h" |
| 15 | |
| 16 | #include "Core/HW/GCPad.h" |
| 17 | #include "Core/HW/GCPadEmu.h" |
| 18 | #include "Core/HW/Wiimote.h" |
| 19 | #include "Core/HW/WiimoteEmu/Extension/Classic.h" |
| 20 | #include "Core/HW/WiimoteEmu/Extension/Nunchuk.h" |
| 21 | #include "Core/HW/WiimoteEmu/WiimoteEmu.h" |
| 22 | |
| 23 | #include "InputCommon/ControllerEmu/ControlGroup/Attachments.h" |
| 24 | #include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h" |
| 25 | #include "InputCommon/ControllerEmu/ControllerEmu.h" |
| 26 | #include "InputCommon/ControllerEmu/StickGate.h" |
| 27 | #include "InputCommon/ControllerInterface/CoreDevice.h" |
| 28 | #include "InputCommon/InputConfig.h" |
| 29 | |
| 30 | namespace ciface::Touch |
| 31 | { |
| 32 | namespace |
| 33 | { |
| 34 | struct InputState |
| 35 | { |
| 36 | ControlState normal_state = 0; |
| 37 | ControlState override_state = 0; |
| 38 | bool overriding = false; |
| 39 | }; |
| 40 | |
| 41 | using ControlsMap = std::map<std::pair<std::string_view, std::string_view>, ControlID>; |
| 42 | using StateArray = std::array<InputState, ControlID::NUMBER_OF_CONTROLS>; |
| 43 | |
| 44 | std::array<StateArray, 4> s_state_arrays; |
| 45 | |
| 46 | const ControlsMap s_gcpad_controls_map = {{ |
| 47 | {{GCPad::BUTTONS_GROUP, GCPad::A_BUTTON}, ControlID::GCPAD_A_BUTTON}, |
| 48 | {{GCPad::BUTTONS_GROUP, GCPad::B_BUTTON}, ControlID::GCPAD_B_BUTTON}, |
| 49 | {{GCPad::BUTTONS_GROUP, GCPad::X_BUTTON}, ControlID::GCPAD_X_BUTTON}, |
| 50 | {{GCPad::BUTTONS_GROUP, GCPad::Y_BUTTON}, ControlID::GCPAD_Y_BUTTON}, |
| 51 | {{GCPad::BUTTONS_GROUP, GCPad::Z_BUTTON}, ControlID::GCPAD_Z_BUTTON}, |
| 52 | {{GCPad::BUTTONS_GROUP, GCPad::START_BUTTON}, ControlID::GCPAD_START_BUTTON}, |
| 53 | {{GCPad::DPAD_GROUP, DIRECTION_UP}, ControlID::GCPAD_DPAD_UP}, |
| 54 | {{GCPad::DPAD_GROUP, DIRECTION_DOWN}, ControlID::GCPAD_DPAD_DOWN}, |
| 55 | {{GCPad::DPAD_GROUP, DIRECTION_LEFT}, ControlID::GCPAD_DPAD_LEFT}, |
| 56 | {{GCPad::DPAD_GROUP, DIRECTION_RIGHT}, ControlID::GCPAD_DPAD_RIGHT}, |
| 57 | {{GCPad::TRIGGERS_GROUP, GCPad::L_DIGITAL}, ControlID::GCPAD_L_DIGITAL}, |
| 58 | {{GCPad::TRIGGERS_GROUP, GCPad::R_DIGITAL}, ControlID::GCPAD_R_DIGITAL}, |
| 59 | {{GCPad::TRIGGERS_GROUP, GCPad::L_ANALOG}, ControlID::GCPAD_L_ANALOG}, |
| 60 | {{GCPad::TRIGGERS_GROUP, GCPad::R_ANALOG}, ControlID::GCPAD_R_ANALOG}, |
| 61 | {{GCPad::MAIN_STICK_GROUP, ControllerEmu::ReshapableInput::X_INPUT_OVERRIDE}, |
| 62 | ControlID::GCPAD_MAIN_STICK_X}, |
| 63 | {{GCPad::MAIN_STICK_GROUP, ControllerEmu::ReshapableInput::Y_INPUT_OVERRIDE}, |
| 64 | ControlID::GCPAD_MAIN_STICK_Y}, |
| 65 | {{GCPad::C_STICK_GROUP, ControllerEmu::ReshapableInput::X_INPUT_OVERRIDE}, |
| 66 | ControlID::GCPAD_C_STICK_X}, |
| 67 | {{GCPad::C_STICK_GROUP, ControllerEmu::ReshapableInput::Y_INPUT_OVERRIDE}, |
| 68 | ControlID::GCPAD_C_STICK_Y}, |
| 69 | }}; |
| 70 | |
| 71 | const ControlsMap s_wiimote_controls_map = {{ |
| 72 | {{WiimoteEmu::Wiimote::BUTTONS_GROUP, WiimoteEmu::Wiimote::A_BUTTON}, |
| 73 | ControlID::WIIMOTE_A_BUTTON}, |
| 74 | {{WiimoteEmu::Wiimote::BUTTONS_GROUP, WiimoteEmu::Wiimote::B_BUTTON}, |
| 75 | ControlID::WIIMOTE_B_BUTTON}, |
| 76 | {{WiimoteEmu::Wiimote::BUTTONS_GROUP, WiimoteEmu::Wiimote::ONE_BUTTON}, |
| 77 | ControlID::WIIMOTE_ONE_BUTTON}, |
| 78 | {{WiimoteEmu::Wiimote::BUTTONS_GROUP, WiimoteEmu::Wiimote::TWO_BUTTON}, |
| 79 | ControlID::WIIMOTE_TWO_BUTTON}, |
| 80 | {{WiimoteEmu::Wiimote::BUTTONS_GROUP, WiimoteEmu::Wiimote::PLUS_BUTTON}, |
| 81 | ControlID::WIIMOTE_PLUS_BUTTON}, |
| 82 | {{WiimoteEmu::Wiimote::BUTTONS_GROUP, WiimoteEmu::Wiimote::MINUS_BUTTON}, |
| 83 | ControlID::WIIMOTE_MINUS_BUTTON}, |
| 84 | {{WiimoteEmu::Wiimote::BUTTONS_GROUP, WiimoteEmu::Wiimote::HOME_BUTTON}, |
| 85 | ControlID::WIIMOTE_HOME_BUTTON}, |
| 86 | {{WiimoteEmu::Wiimote::DPAD_GROUP, DIRECTION_UP}, ControlID::WIIMOTE_DPAD_UP}, |
| 87 | {{WiimoteEmu::Wiimote::DPAD_GROUP, DIRECTION_DOWN}, ControlID::WIIMOTE_DPAD_DOWN}, |
| 88 | {{WiimoteEmu::Wiimote::DPAD_GROUP, DIRECTION_LEFT}, ControlID::WIIMOTE_DPAD_LEFT}, |
| 89 | {{WiimoteEmu::Wiimote::DPAD_GROUP, DIRECTION_RIGHT}, ControlID::WIIMOTE_DPAD_RIGHT}, |
| 90 | {{WiimoteEmu::Wiimote::IR_GROUP, ControllerEmu::ReshapableInput::X_INPUT_OVERRIDE}, |
| 91 | ControlID::WIIMOTE_IR_X}, |
| 92 | {{WiimoteEmu::Wiimote::IR_GROUP, ControllerEmu::ReshapableInput::Y_INPUT_OVERRIDE}, |
| 93 | ControlID::WIIMOTE_IR_Y}, |
| 94 | }}; |
| 95 | |
| 96 | const ControlsMap s_nunchuk_controls_map = {{ |
| 97 | {{WiimoteEmu::Nunchuk::BUTTONS_GROUP, WiimoteEmu::Nunchuk::C_BUTTON}, |
| 98 | ControlID::NUNCHUK_C_BUTTON}, |
| 99 | {{WiimoteEmu::Nunchuk::BUTTONS_GROUP, WiimoteEmu::Nunchuk::Z_BUTTON}, |
| 100 | ControlID::NUNCHUK_Z_BUTTON}, |
| 101 | {{WiimoteEmu::Nunchuk::STICK_GROUP, ControllerEmu::ReshapableInput::X_INPUT_OVERRIDE}, |
| 102 | ControlID::NUNCHUK_STICK_X}, |
| 103 | {{WiimoteEmu::Nunchuk::STICK_GROUP, ControllerEmu::ReshapableInput::Y_INPUT_OVERRIDE}, |
| 104 | ControlID::NUNCHUK_STICK_Y}, |
| 105 | }}; |
| 106 | |
| 107 | const ControlsMap s_classic_controls_map = {{ |
| 108 | {{WiimoteEmu::Classic::BUTTONS_GROUP, WiimoteEmu::Classic::A_BUTTON}, |
| 109 | ControlID::CLASSIC_A_BUTTON}, |
| 110 | {{WiimoteEmu::Classic::BUTTONS_GROUP, WiimoteEmu::Classic::B_BUTTON}, |
| 111 | ControlID::CLASSIC_B_BUTTON}, |
| 112 | {{WiimoteEmu::Classic::BUTTONS_GROUP, WiimoteEmu::Classic::X_BUTTON}, |
| 113 | ControlID::CLASSIC_X_BUTTON}, |
| 114 | {{WiimoteEmu::Classic::BUTTONS_GROUP, WiimoteEmu::Classic::Y_BUTTON}, |
| 115 | ControlID::CLASSIC_Y_BUTTON}, |
| 116 | {{WiimoteEmu::Classic::BUTTONS_GROUP, WiimoteEmu::Classic::ZL_BUTTON}, |
| 117 | ControlID::CLASSIC_ZL_BUTTON}, |
| 118 | {{WiimoteEmu::Classic::BUTTONS_GROUP, WiimoteEmu::Classic::ZR_BUTTON}, |
| 119 | ControlID::CLASSIC_ZR_BUTTON}, |
| 120 | {{WiimoteEmu::Classic::BUTTONS_GROUP, WiimoteEmu::Classic::PLUS_BUTTON}, |
| 121 | ControlID::CLASSIC_PLUS_BUTTON}, |
| 122 | {{WiimoteEmu::Classic::BUTTONS_GROUP, WiimoteEmu::Classic::MINUS_BUTTON}, |
| 123 | ControlID::CLASSIC_MINUS_BUTTON}, |
| 124 | {{WiimoteEmu::Classic::BUTTONS_GROUP, WiimoteEmu::Classic::HOME_BUTTON}, |
| 125 | ControlID::CLASSIC_HOME_BUTTON}, |
| 126 | {{WiimoteEmu::Classic::DPAD_GROUP, DIRECTION_UP}, ControlID::CLASSIC_DPAD_UP}, |
| 127 | {{WiimoteEmu::Classic::DPAD_GROUP, DIRECTION_DOWN}, ControlID::CLASSIC_DPAD_DOWN}, |
| 128 | {{WiimoteEmu::Classic::DPAD_GROUP, DIRECTION_LEFT}, ControlID::CLASSIC_DPAD_LEFT}, |
| 129 | {{WiimoteEmu::Classic::DPAD_GROUP, DIRECTION_RIGHT}, ControlID::CLASSIC_DPAD_RIGHT}, |
| 130 | {{WiimoteEmu::Classic::TRIGGERS_GROUP, WiimoteEmu::Classic::L_DIGITAL}, |
| 131 | ControlID::CLASSIC_L_DIGITAL}, |
| 132 | {{WiimoteEmu::Classic::TRIGGERS_GROUP, WiimoteEmu::Classic::R_DIGITAL}, |
| 133 | ControlID::CLASSIC_R_DIGITAL}, |
| 134 | {{WiimoteEmu::Classic::TRIGGERS_GROUP, WiimoteEmu::Classic::L_ANALOG}, |
| 135 | ControlID::CLASSIC_L_ANALOG}, |
| 136 | {{WiimoteEmu::Classic::TRIGGERS_GROUP, WiimoteEmu::Classic::R_ANALOG}, |
| 137 | ControlID::CLASSIC_R_ANALOG}, |
| 138 | {{WiimoteEmu::Classic::LEFT_STICK_GROUP, ControllerEmu::ReshapableInput::X_INPUT_OVERRIDE}, |
| 139 | ControlID::CLASSIC_LEFT_STICK_X}, |
| 140 | {{WiimoteEmu::Classic::LEFT_STICK_GROUP, ControllerEmu::ReshapableInput::Y_INPUT_OVERRIDE}, |
| 141 | ControlID::CLASSIC_LEFT_STICK_Y}, |
| 142 | {{WiimoteEmu::Classic::RIGHT_STICK_GROUP, ControllerEmu::ReshapableInput::X_INPUT_OVERRIDE}, |
| 143 | ControlID::CLASSIC_RIGHT_STICK_X}, |
| 144 | {{WiimoteEmu::Classic::RIGHT_STICK_GROUP, ControllerEmu::ReshapableInput::Y_INPUT_OVERRIDE}, |
| 145 | ControlID::CLASSIC_RIGHT_STICK_Y}, |
| 146 | }}; |
| 147 | |
| 148 | ControllerEmu::InputOverrideFunction GetInputOverrideFunction(const ControlsMap& controls_map, |
| 149 | size_t i) |
| 150 | { |
| 151 | StateArray& state_array = s_state_arrays[i]; |
| 152 | |
| 153 | return [&](std::string_view group_name, std::string_view control_name, |
| 154 | ControlState controller_state) -> std::optional<ControlState> { |
| 155 | const auto it = controls_map.find(std::make_pair(group_name, control_name)); |
| 156 | if (it == controls_map.end()) |
| 157 | return std::nullopt; |
| 158 | |
| 159 | const ControlID control = it->second; |
| 160 | InputState& input_state = state_array[control]; |
| 161 | if (input_state.normal_state != controller_state) |
| 162 | { |
| 163 | input_state.normal_state = controller_state; |
| 164 | input_state.overriding = false; |
| 165 | } |
| 166 | |
| 167 | return input_state.overriding ? std::make_optional(input_state.override_state) : std::nullopt; |
| 168 | }; |
| 169 | } |
| 170 | |
| 171 | } // namespace |
| 172 | |
| 173 | void RegisterGameCubeInputOverrider(int controller_index) |
| 174 | { |
| 175 | Pad::GetConfig() |
| 176 | ->GetController(controller_index) |
| 177 | ->SetInputOverrideFunction(GetInputOverrideFunction(s_gcpad_controls_map, controller_index)); |
| 178 | } |
| 179 | |
| 180 | void RegisterWiiInputOverrider(int controller_index) |
| 181 | { |
| 182 | auto* wiimote = |
| 183 | static_cast<WiimoteEmu::Wiimote*>(Wiimote::GetConfig()->GetController(controller_index)); |
| 184 | |
| 185 | wiimote->SetInputOverrideFunction( |
| 186 | GetInputOverrideFunction(s_wiimote_controls_map, controller_index)); |
| 187 | |
| 188 | auto& attachments = static_cast<ControllerEmu::Attachments*>( |
| 189 | wiimote->GetWiimoteGroup(WiimoteEmu::WiimoteGroup::Attachments)) |
| 190 | ->GetAttachmentList(); |
| 191 | |
| 192 | attachments[WiimoteEmu::ExtensionNumber::NUNCHUK]->SetInputOverrideFunction( |
| 193 | GetInputOverrideFunction(s_nunchuk_controls_map, controller_index)); |
| 194 | attachments[WiimoteEmu::ExtensionNumber::CLASSIC]->SetInputOverrideFunction( |
| 195 | GetInputOverrideFunction(s_classic_controls_map, controller_index)); |
| 196 | } |
| 197 | |
| 198 | void UnregisterGameCubeInputOverrider(int controller_index) |
| 199 | { |
| 200 | Pad::GetConfig()->GetController(controller_index)->ClearInputOverrideFunction(); |
| 201 | |
| 202 | for (size_t i = ControlID::FIRST_GC_CONTROL; i <= ControlID::LAST_GC_CONTROL; ++i) |
| 203 | s_state_arrays[controller_index][i].overriding = false; |
| 204 | } |
| 205 | |
| 206 | void UnregisterWiiInputOverrider(int controller_index) |
| 207 | { |
| 208 | auto* wiimote = |
| 209 | static_cast<WiimoteEmu::Wiimote*>(Wiimote::GetConfig()->GetController(controller_index)); |
| 210 | |
| 211 | wiimote->ClearInputOverrideFunction(); |
| 212 | |
| 213 | auto& attachments = static_cast<ControllerEmu::Attachments*>( |
| 214 | wiimote->GetWiimoteGroup(WiimoteEmu::WiimoteGroup::Attachments)) |
| 215 | ->GetAttachmentList(); |
| 216 | |
| 217 | attachments[WiimoteEmu::ExtensionNumber::NUNCHUK]->ClearInputOverrideFunction(); |
| 218 | attachments[WiimoteEmu::ExtensionNumber::CLASSIC]->ClearInputOverrideFunction(); |
| 219 | |
| 220 | for (size_t i = ControlID::FIRST_WII_CONTROL; i <= ControlID::LAST_WII_CONTROL; ++i) |
| 221 | s_state_arrays[controller_index][i].overriding = false; |
| 222 | } |
| 223 | |
| 224 | void SetControlState(int controller_index, ControlID control, double state) |
| 225 | { |
| 226 | InputState& input_state = s_state_arrays[controller_index][control]; |
| 227 | |
| 228 | input_state.override_state = state; |
| 229 | input_state.overriding = true; |
| 230 | } |
| 231 | |
| 232 | void ClearControlState(int controller_index, ControlID control) |
| 233 | { |
| 234 | InputState& input_state = s_state_arrays[controller_index][control]; |
| 235 | |
| 236 | input_state.overriding = false; |
| 237 | } |
| 238 | |
| 239 | double GetGateRadiusAtAngle(int controller_index, ControlID stick, double angle) |
| 240 | { |
| 241 | ControllerEmu::ControlGroup* group; |
| 242 | |
| 243 | switch (stick) |
| 244 | { |
| 245 | case ControlID::GCPAD_MAIN_STICK_X: |
| 246 | case ControlID::GCPAD_MAIN_STICK_Y: |
| 247 | group = Pad::GetGroup(controller_index, PadGroup::MainStick); |
| 248 | break; |
| 249 | case ControlID::GCPAD_C_STICK_X: |
| 250 | case ControlID::GCPAD_C_STICK_Y: |
| 251 | group = Pad::GetGroup(controller_index, PadGroup::CStick); |
| 252 | break; |
| 253 | case ControlID::NUNCHUK_STICK_X: |
| 254 | case ControlID::NUNCHUK_STICK_Y: |
| 255 | group = Wiimote::GetNunchukGroup(controller_index, WiimoteEmu::NunchukGroup::Stick); |
| 256 | break; |
| 257 | case ControlID::CLASSIC_LEFT_STICK_X: |
| 258 | case ControlID::CLASSIC_LEFT_STICK_Y: |
| 259 | group = Wiimote::GetClassicGroup(controller_index, WiimoteEmu::ClassicGroup::LeftStick); |
| 260 | break; |
| 261 | case ControlID::CLASSIC_RIGHT_STICK_X: |
| 262 | case ControlID::CLASSIC_RIGHT_STICK_Y: |
| 263 | group = Wiimote::GetClassicGroup(controller_index, WiimoteEmu::ClassicGroup::RightStick); |
| 264 | break; |
| 265 | default: |
| 266 | ASSERT(false); |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | return static_cast<ControllerEmu::ReshapableInput*>(group)->GetGateRadiusAtAngle(angle); |
| 271 | } |
| 272 | } // namespace ciface::Touch |