diff options
Diffstat (limited to 'src/input_common/drivers/joycon.cpp')
| -rw-r--r-- | src/input_common/drivers/joycon.cpp | 724 |
1 files changed, 724 insertions, 0 deletions
diff --git a/src/input_common/drivers/joycon.cpp b/src/input_common/drivers/joycon.cpp new file mode 100644 index 000000000..b4cd39a20 --- /dev/null +++ b/src/input_common/drivers/joycon.cpp | |||
| @@ -0,0 +1,724 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #include <fmt/format.h> | ||
| 5 | |||
| 6 | #include "common/param_package.h" | ||
| 7 | #include "common/polyfill_ranges.h" | ||
| 8 | #include "common/polyfill_thread.h" | ||
| 9 | #include "common/settings.h" | ||
| 10 | #include "common/thread.h" | ||
| 11 | #include "input_common/drivers/joycon.h" | ||
| 12 | #include "input_common/helpers/joycon_driver.h" | ||
| 13 | #include "input_common/helpers/joycon_protocol/joycon_types.h" | ||
| 14 | |||
| 15 | namespace InputCommon { | ||
| 16 | |||
| 17 | Joycons::Joycons(const std::string& input_engine_) : InputEngine(input_engine_) { | ||
| 18 | // Avoid conflicting with SDL driver | ||
| 19 | if (!Settings::values.enable_joycon_driver && !Settings::values.enable_procon_driver) { | ||
| 20 | return; | ||
| 21 | } | ||
| 22 | LOG_INFO(Input, "Joycon driver Initialization started"); | ||
| 23 | const int init_res = SDL_hid_init(); | ||
| 24 | if (init_res == 0) { | ||
| 25 | Setup(); | ||
| 26 | } else { | ||
| 27 | LOG_ERROR(Input, "Hidapi could not be initialized. failed with error = {}", init_res); | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | Joycons::~Joycons() { | ||
| 32 | Reset(); | ||
| 33 | } | ||
| 34 | |||
| 35 | void Joycons::Reset() { | ||
| 36 | scan_thread = {}; | ||
| 37 | for (const auto& device : left_joycons) { | ||
| 38 | if (!device) { | ||
| 39 | continue; | ||
| 40 | } | ||
| 41 | device->Stop(); | ||
| 42 | } | ||
| 43 | for (const auto& device : right_joycons) { | ||
| 44 | if (!device) { | ||
| 45 | continue; | ||
| 46 | } | ||
| 47 | device->Stop(); | ||
| 48 | } | ||
| 49 | for (const auto& device : pro_controller) { | ||
| 50 | if (!device) { | ||
| 51 | continue; | ||
| 52 | } | ||
| 53 | device->Stop(); | ||
| 54 | } | ||
| 55 | SDL_hid_exit(); | ||
| 56 | } | ||
| 57 | |||
| 58 | void Joycons::Setup() { | ||
| 59 | u32 port = 0; | ||
| 60 | PreSetController(GetIdentifier(0, Joycon::ControllerType::None)); | ||
| 61 | for (auto& device : left_joycons) { | ||
| 62 | PreSetController(GetIdentifier(port, Joycon::ControllerType::Left)); | ||
| 63 | device = std::make_shared<Joycon::JoyconDriver>(port++); | ||
| 64 | } | ||
| 65 | port = 0; | ||
| 66 | for (auto& device : right_joycons) { | ||
| 67 | PreSetController(GetIdentifier(port, Joycon::ControllerType::Right)); | ||
| 68 | device = std::make_shared<Joycon::JoyconDriver>(port++); | ||
| 69 | } | ||
| 70 | port = 0; | ||
| 71 | for (auto& device : pro_controller) { | ||
| 72 | PreSetController(GetIdentifier(port, Joycon::ControllerType::Pro)); | ||
| 73 | device = std::make_shared<Joycon::JoyconDriver>(port++); | ||
| 74 | } | ||
| 75 | |||
| 76 | scan_thread = std::jthread([this](std::stop_token stop_token) { ScanThread(stop_token); }); | ||
| 77 | } | ||
| 78 | |||
| 79 | void Joycons::ScanThread(std::stop_token stop_token) { | ||
| 80 | constexpr u16 nintendo_vendor_id = 0x057e; | ||
| 81 | Common::SetCurrentThreadName("JoyconScanThread"); | ||
| 82 | |||
| 83 | do { | ||
| 84 | SDL_hid_device_info* devs = SDL_hid_enumerate(nintendo_vendor_id, 0x0); | ||
| 85 | SDL_hid_device_info* cur_dev = devs; | ||
| 86 | |||
| 87 | while (cur_dev) { | ||
| 88 | if (IsDeviceNew(cur_dev)) { | ||
| 89 | LOG_DEBUG(Input, "Device Found,type : {:04X} {:04X}", cur_dev->vendor_id, | ||
| 90 | cur_dev->product_id); | ||
| 91 | RegisterNewDevice(cur_dev); | ||
| 92 | } | ||
| 93 | cur_dev = cur_dev->next; | ||
| 94 | } | ||
| 95 | |||
| 96 | SDL_hid_free_enumeration(devs); | ||
| 97 | } while (Common::StoppableTimedWait(stop_token, std::chrono::seconds{5})); | ||
| 98 | } | ||
| 99 | |||
| 100 | bool Joycons::IsDeviceNew(SDL_hid_device_info* device_info) const { | ||
| 101 | Joycon::ControllerType type{}; | ||
| 102 | Joycon::SerialNumber serial_number{}; | ||
| 103 | |||
| 104 | const auto result = Joycon::JoyconDriver::GetDeviceType(device_info, type); | ||
| 105 | if (result != Joycon::DriverResult::Success) { | ||
| 106 | return false; | ||
| 107 | } | ||
| 108 | |||
| 109 | const auto result2 = Joycon::JoyconDriver::GetSerialNumber(device_info, serial_number); | ||
| 110 | if (result2 != Joycon::DriverResult::Success) { | ||
| 111 | return false; | ||
| 112 | } | ||
| 113 | |||
| 114 | auto is_handle_identical = [serial_number](std::shared_ptr<Joycon::JoyconDriver> device) { | ||
| 115 | if (!device) { | ||
| 116 | return false; | ||
| 117 | } | ||
| 118 | if (!device->IsConnected()) { | ||
| 119 | return false; | ||
| 120 | } | ||
| 121 | if (device->GetHandleSerialNumber() != serial_number) { | ||
| 122 | return false; | ||
| 123 | } | ||
| 124 | return true; | ||
| 125 | }; | ||
| 126 | |||
| 127 | // Check if device already exist | ||
| 128 | switch (type) { | ||
| 129 | case Joycon::ControllerType::Left: | ||
| 130 | if (!Settings::values.enable_joycon_driver) { | ||
| 131 | return false; | ||
| 132 | } | ||
| 133 | for (const auto& device : left_joycons) { | ||
| 134 | if (is_handle_identical(device)) { | ||
| 135 | return false; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | break; | ||
| 139 | case Joycon::ControllerType::Right: | ||
| 140 | if (!Settings::values.enable_joycon_driver) { | ||
| 141 | return false; | ||
| 142 | } | ||
| 143 | for (const auto& device : right_joycons) { | ||
| 144 | if (is_handle_identical(device)) { | ||
| 145 | return false; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | break; | ||
| 149 | case Joycon::ControllerType::Pro: | ||
| 150 | if (!Settings::values.enable_procon_driver) { | ||
| 151 | return false; | ||
| 152 | } | ||
| 153 | for (const auto& device : pro_controller) { | ||
| 154 | if (is_handle_identical(device)) { | ||
| 155 | return false; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | break; | ||
| 159 | default: | ||
| 160 | return false; | ||
| 161 | } | ||
| 162 | |||
| 163 | return true; | ||
| 164 | } | ||
| 165 | |||
| 166 | void Joycons::RegisterNewDevice(SDL_hid_device_info* device_info) { | ||
| 167 | Joycon::ControllerType type{}; | ||
| 168 | auto result = Joycon::JoyconDriver::GetDeviceType(device_info, type); | ||
| 169 | auto handle = GetNextFreeHandle(type); | ||
| 170 | if (handle == nullptr) { | ||
| 171 | LOG_WARNING(Input, "No free handles available"); | ||
| 172 | return; | ||
| 173 | } | ||
| 174 | if (result == Joycon::DriverResult::Success) { | ||
| 175 | result = handle->RequestDeviceAccess(device_info); | ||
| 176 | } | ||
| 177 | if (result == Joycon::DriverResult::Success) { | ||
| 178 | LOG_WARNING(Input, "Initialize device"); | ||
| 179 | |||
| 180 | const std::size_t port = handle->GetDevicePort(); | ||
| 181 | const Joycon::JoyconCallbacks callbacks{ | ||
| 182 | .on_battery_data = {[this, port, type](Joycon::Battery value) { | ||
| 183 | OnBatteryUpdate(port, type, value); | ||
| 184 | }}, | ||
| 185 | .on_color_data = {[this, port, type](Joycon::Color value) { | ||
| 186 | OnColorUpdate(port, type, value); | ||
| 187 | }}, | ||
| 188 | .on_button_data = {[this, port, type](int id, bool value) { | ||
| 189 | OnButtonUpdate(port, type, id, value); | ||
| 190 | }}, | ||
| 191 | .on_stick_data = {[this, port, type](int id, f32 value) { | ||
| 192 | OnStickUpdate(port, type, id, value); | ||
| 193 | }}, | ||
| 194 | .on_motion_data = {[this, port, type](int id, const Joycon::MotionData& value) { | ||
| 195 | OnMotionUpdate(port, type, id, value); | ||
| 196 | }}, | ||
| 197 | .on_ring_data = {[this](f32 ring_data) { OnRingConUpdate(ring_data); }}, | ||
| 198 | .on_amiibo_data = {[this, port](const std::vector<u8>& amiibo_data) { | ||
| 199 | OnAmiiboUpdate(port, amiibo_data); | ||
| 200 | }}, | ||
| 201 | .on_camera_data = {[this, port](const std::vector<u8>& camera_data, | ||
| 202 | Joycon::IrsResolution format) { | ||
| 203 | OnCameraUpdate(port, camera_data, format); | ||
| 204 | }}, | ||
| 205 | }; | ||
| 206 | |||
| 207 | handle->InitializeDevice(); | ||
| 208 | handle->SetCallbacks(callbacks); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | std::shared_ptr<Joycon::JoyconDriver> Joycons::GetNextFreeHandle( | ||
| 213 | Joycon::ControllerType type) const { | ||
| 214 | if (type == Joycon::ControllerType::Left) { | ||
| 215 | const auto unconnected_device = | ||
| 216 | std::ranges::find_if(left_joycons, [](auto& device) { return !device->IsConnected(); }); | ||
| 217 | if (unconnected_device != left_joycons.end()) { | ||
| 218 | return *unconnected_device; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | if (type == Joycon::ControllerType::Right) { | ||
| 222 | const auto unconnected_device = std::ranges::find_if( | ||
| 223 | right_joycons, [](auto& device) { return !device->IsConnected(); }); | ||
| 224 | |||
| 225 | if (unconnected_device != right_joycons.end()) { | ||
| 226 | return *unconnected_device; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | if (type == Joycon::ControllerType::Pro) { | ||
| 230 | const auto unconnected_device = std::ranges::find_if( | ||
| 231 | pro_controller, [](auto& device) { return !device->IsConnected(); }); | ||
| 232 | |||
| 233 | if (unconnected_device != pro_controller.end()) { | ||
| 234 | return *unconnected_device; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | return nullptr; | ||
| 238 | } | ||
| 239 | |||
| 240 | bool Joycons::IsVibrationEnabled(const PadIdentifier& identifier) { | ||
| 241 | const auto handle = GetHandle(identifier); | ||
| 242 | if (handle == nullptr) { | ||
| 243 | return false; | ||
| 244 | } | ||
| 245 | return handle->IsVibrationEnabled(); | ||
| 246 | } | ||
| 247 | |||
| 248 | Common::Input::DriverResult Joycons::SetVibration(const PadIdentifier& identifier, | ||
| 249 | const Common::Input::VibrationStatus& vibration) { | ||
| 250 | const Joycon::VibrationValue native_vibration{ | ||
| 251 | .low_amplitude = vibration.low_amplitude, | ||
| 252 | .low_frequency = vibration.low_frequency, | ||
| 253 | .high_amplitude = vibration.high_amplitude, | ||
| 254 | .high_frequency = vibration.high_frequency, | ||
| 255 | }; | ||
| 256 | auto handle = GetHandle(identifier); | ||
| 257 | if (handle == nullptr) { | ||
| 258 | return Common::Input::DriverResult::InvalidHandle; | ||
| 259 | } | ||
| 260 | |||
| 261 | handle->SetVibration(native_vibration); | ||
| 262 | return Common::Input::DriverResult::Success; | ||
| 263 | } | ||
| 264 | |||
| 265 | Common::Input::DriverResult Joycons::SetLeds(const PadIdentifier& identifier, | ||
| 266 | const Common::Input::LedStatus& led_status) { | ||
| 267 | auto handle = GetHandle(identifier); | ||
| 268 | if (handle == nullptr) { | ||
| 269 | return Common::Input::DriverResult::InvalidHandle; | ||
| 270 | } | ||
| 271 | int led_config = led_status.led_1 ? 1 : 0; | ||
| 272 | led_config += led_status.led_2 ? 2 : 0; | ||
| 273 | led_config += led_status.led_3 ? 4 : 0; | ||
| 274 | led_config += led_status.led_4 ? 8 : 0; | ||
| 275 | |||
| 276 | return static_cast<Common::Input::DriverResult>( | ||
| 277 | handle->SetLedConfig(static_cast<u8>(led_config))); | ||
| 278 | } | ||
| 279 | |||
| 280 | Common::Input::DriverResult Joycons::SetCameraFormat(const PadIdentifier& identifier, | ||
| 281 | Common::Input::CameraFormat camera_format) { | ||
| 282 | auto handle = GetHandle(identifier); | ||
| 283 | if (handle == nullptr) { | ||
| 284 | return Common::Input::DriverResult::InvalidHandle; | ||
| 285 | } | ||
| 286 | return static_cast<Common::Input::DriverResult>(handle->SetIrsConfig( | ||
| 287 | Joycon::IrsMode::ImageTransfer, static_cast<Joycon::IrsResolution>(camera_format))); | ||
| 288 | }; | ||
| 289 | |||
| 290 | Common::Input::NfcState Joycons::SupportsNfc(const PadIdentifier& identifier_) const { | ||
| 291 | return Common::Input::NfcState::Success; | ||
| 292 | }; | ||
| 293 | |||
| 294 | Common::Input::NfcState Joycons::WriteNfcData(const PadIdentifier& identifier_, | ||
| 295 | const std::vector<u8>& data) { | ||
| 296 | return Common::Input::NfcState::NotSupported; | ||
| 297 | }; | ||
| 298 | |||
| 299 | Common::Input::DriverResult Joycons::SetPollingMode(const PadIdentifier& identifier, | ||
| 300 | const Common::Input::PollingMode polling_mode) { | ||
| 301 | auto handle = GetHandle(identifier); | ||
| 302 | if (handle == nullptr) { | ||
| 303 | LOG_ERROR(Input, "Invalid handle {}", identifier.port); | ||
| 304 | return Common::Input::DriverResult::InvalidHandle; | ||
| 305 | } | ||
| 306 | |||
| 307 | switch (polling_mode) { | ||
| 308 | case Common::Input::PollingMode::Active: | ||
| 309 | return static_cast<Common::Input::DriverResult>(handle->SetActiveMode()); | ||
| 310 | case Common::Input::PollingMode::Pasive: | ||
| 311 | return static_cast<Common::Input::DriverResult>(handle->SetPasiveMode()); | ||
| 312 | case Common::Input::PollingMode::IR: | ||
| 313 | return static_cast<Common::Input::DriverResult>(handle->SetIrMode()); | ||
| 314 | case Common::Input::PollingMode::NFC: | ||
| 315 | return static_cast<Common::Input::DriverResult>(handle->SetNfcMode()); | ||
| 316 | case Common::Input::PollingMode::Ring: | ||
| 317 | return static_cast<Common::Input::DriverResult>(handle->SetRingConMode()); | ||
| 318 | default: | ||
| 319 | return Common::Input::DriverResult::NotSupported; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | void Joycons::OnBatteryUpdate(std::size_t port, Joycon::ControllerType type, | ||
| 324 | Joycon::Battery value) { | ||
| 325 | const auto identifier = GetIdentifier(port, type); | ||
| 326 | if (value.charging != 0) { | ||
| 327 | SetBattery(identifier, Common::Input::BatteryLevel::Charging); | ||
| 328 | return; | ||
| 329 | } | ||
| 330 | |||
| 331 | Common::Input::BatteryLevel battery{}; | ||
| 332 | switch (value.status) { | ||
| 333 | case 0: | ||
| 334 | battery = Common::Input::BatteryLevel::Empty; | ||
| 335 | break; | ||
| 336 | case 1: | ||
| 337 | battery = Common::Input::BatteryLevel::Critical; | ||
| 338 | break; | ||
| 339 | case 2: | ||
| 340 | battery = Common::Input::BatteryLevel::Low; | ||
| 341 | break; | ||
| 342 | case 3: | ||
| 343 | battery = Common::Input::BatteryLevel::Medium; | ||
| 344 | break; | ||
| 345 | case 4: | ||
| 346 | default: | ||
| 347 | battery = Common::Input::BatteryLevel::Full; | ||
| 348 | break; | ||
| 349 | } | ||
| 350 | SetBattery(identifier, battery); | ||
| 351 | } | ||
| 352 | |||
| 353 | void Joycons::OnColorUpdate(std::size_t port, Joycon::ControllerType type, | ||
| 354 | const Joycon::Color& value) { | ||
| 355 | const auto identifier = GetIdentifier(port, type); | ||
| 356 | Common::Input::BodyColorStatus color{ | ||
| 357 | .body = value.body, | ||
| 358 | .buttons = value.buttons, | ||
| 359 | .left_grip = value.left_grip, | ||
| 360 | .right_grip = value.right_grip, | ||
| 361 | }; | ||
| 362 | SetColor(identifier, color); | ||
| 363 | } | ||
| 364 | |||
| 365 | void Joycons::OnButtonUpdate(std::size_t port, Joycon::ControllerType type, int id, bool value) { | ||
| 366 | const auto identifier = GetIdentifier(port, type); | ||
| 367 | SetButton(identifier, id, value); | ||
| 368 | } | ||
| 369 | |||
| 370 | void Joycons::OnStickUpdate(std::size_t port, Joycon::ControllerType type, int id, f32 value) { | ||
| 371 | const auto identifier = GetIdentifier(port, type); | ||
| 372 | SetAxis(identifier, id, value); | ||
| 373 | } | ||
| 374 | |||
| 375 | void Joycons::OnMotionUpdate(std::size_t port, Joycon::ControllerType type, int id, | ||
| 376 | const Joycon::MotionData& value) { | ||
| 377 | const auto identifier = GetIdentifier(port, type); | ||
| 378 | BasicMotion motion_data{ | ||
| 379 | .gyro_x = value.gyro_x, | ||
| 380 | .gyro_y = value.gyro_y, | ||
| 381 | .gyro_z = value.gyro_z, | ||
| 382 | .accel_x = value.accel_x, | ||
| 383 | .accel_y = value.accel_y, | ||
| 384 | .accel_z = value.accel_z, | ||
| 385 | .delta_timestamp = 15000, | ||
| 386 | }; | ||
| 387 | SetMotion(identifier, id, motion_data); | ||
| 388 | } | ||
| 389 | |||
| 390 | void Joycons::OnRingConUpdate(f32 ring_data) { | ||
| 391 | // To simplify ring detection it will always be mapped to an empty identifier for all | ||
| 392 | // controllers | ||
| 393 | static constexpr PadIdentifier identifier = { | ||
| 394 | .guid = Common::UUID{}, | ||
| 395 | .port = 0, | ||
| 396 | .pad = 0, | ||
| 397 | }; | ||
| 398 | SetAxis(identifier, 100, ring_data); | ||
| 399 | } | ||
| 400 | |||
| 401 | void Joycons::OnAmiiboUpdate(std::size_t port, const std::vector<u8>& amiibo_data) { | ||
| 402 | const auto identifier = GetIdentifier(port, Joycon::ControllerType::Right); | ||
| 403 | const auto nfc_state = amiibo_data.empty() ? Common::Input::NfcState::AmiiboRemoved | ||
| 404 | : Common::Input::NfcState::NewAmiibo; | ||
| 405 | SetNfc(identifier, {nfc_state, amiibo_data}); | ||
| 406 | } | ||
| 407 | |||
| 408 | void Joycons::OnCameraUpdate(std::size_t port, const std::vector<u8>& camera_data, | ||
| 409 | Joycon::IrsResolution format) { | ||
| 410 | const auto identifier = GetIdentifier(port, Joycon::ControllerType::Right); | ||
| 411 | SetCamera(identifier, {static_cast<Common::Input::CameraFormat>(format), camera_data}); | ||
| 412 | } | ||
| 413 | |||
| 414 | std::shared_ptr<Joycon::JoyconDriver> Joycons::GetHandle(PadIdentifier identifier) const { | ||
| 415 | auto is_handle_active = [&](std::shared_ptr<Joycon::JoyconDriver> device) { | ||
| 416 | if (!device) { | ||
| 417 | return false; | ||
| 418 | } | ||
| 419 | if (!device->IsConnected()) { | ||
| 420 | return false; | ||
| 421 | } | ||
| 422 | if (device->GetDevicePort() == identifier.port) { | ||
| 423 | return true; | ||
| 424 | } | ||
| 425 | return false; | ||
| 426 | }; | ||
| 427 | const auto type = static_cast<Joycon::ControllerType>(identifier.pad); | ||
| 428 | |||
| 429 | if (type == Joycon::ControllerType::Left) { | ||
| 430 | const auto matching_device = std::ranges::find_if( | ||
| 431 | left_joycons, [is_handle_active](auto& device) { return is_handle_active(device); }); | ||
| 432 | |||
| 433 | if (matching_device != left_joycons.end()) { | ||
| 434 | return *matching_device; | ||
| 435 | } | ||
| 436 | } | ||
| 437 | |||
| 438 | if (type == Joycon::ControllerType::Right) { | ||
| 439 | const auto matching_device = std::ranges::find_if( | ||
| 440 | right_joycons, [is_handle_active](auto& device) { return is_handle_active(device); }); | ||
| 441 | |||
| 442 | if (matching_device != right_joycons.end()) { | ||
| 443 | return *matching_device; | ||
| 444 | } | ||
| 445 | } | ||
| 446 | |||
| 447 | if (type == Joycon::ControllerType::Pro) { | ||
| 448 | const auto matching_device = std::ranges::find_if( | ||
| 449 | pro_controller, [is_handle_active](auto& device) { return is_handle_active(device); }); | ||
| 450 | |||
| 451 | if (matching_device != pro_controller.end()) { | ||
| 452 | return *matching_device; | ||
| 453 | } | ||
| 454 | } | ||
| 455 | |||
| 456 | return nullptr; | ||
| 457 | } | ||
| 458 | |||
| 459 | PadIdentifier Joycons::GetIdentifier(std::size_t port, Joycon::ControllerType type) const { | ||
| 460 | const std::array<u8, 16> guid{0, 0, 0, 0, 0, 0, 0, 0, | ||
| 461 | 0, 0, 0, 0, 0, 0, 0, static_cast<u8>(type)}; | ||
| 462 | return { | ||
| 463 | .guid = Common::UUID{guid}, | ||
| 464 | .port = port, | ||
| 465 | .pad = static_cast<std::size_t>(type), | ||
| 466 | }; | ||
| 467 | } | ||
| 468 | |||
| 469 | Common::ParamPackage Joycons::GetParamPackage(std::size_t port, Joycon::ControllerType type) const { | ||
| 470 | const auto identifier = GetIdentifier(port, type); | ||
| 471 | return { | ||
| 472 | {"engine", GetEngineName()}, | ||
| 473 | {"guid", identifier.guid.RawString()}, | ||
| 474 | {"port", std::to_string(identifier.port)}, | ||
| 475 | {"pad", std::to_string(identifier.pad)}, | ||
| 476 | }; | ||
| 477 | } | ||
| 478 | |||
| 479 | std::vector<Common::ParamPackage> Joycons::GetInputDevices() const { | ||
| 480 | std::vector<Common::ParamPackage> devices{}; | ||
| 481 | |||
| 482 | auto add_entry = [&](std::shared_ptr<Joycon::JoyconDriver> device) { | ||
| 483 | if (!device) { | ||
| 484 | return; | ||
| 485 | } | ||
| 486 | if (!device->IsConnected()) { | ||
| 487 | return; | ||
| 488 | } | ||
| 489 | auto param = GetParamPackage(device->GetDevicePort(), device->GetHandleDeviceType()); | ||
| 490 | std::string name = fmt::format("{} {}", JoyconName(device->GetHandleDeviceType()), | ||
| 491 | device->GetDevicePort() + 1); | ||
| 492 | param.Set("display", std::move(name)); | ||
| 493 | devices.emplace_back(param); | ||
| 494 | }; | ||
| 495 | |||
| 496 | for (const auto& controller : left_joycons) { | ||
| 497 | add_entry(controller); | ||
| 498 | } | ||
| 499 | for (const auto& controller : right_joycons) { | ||
| 500 | add_entry(controller); | ||
| 501 | } | ||
| 502 | for (const auto& controller : pro_controller) { | ||
| 503 | add_entry(controller); | ||
| 504 | } | ||
| 505 | |||
| 506 | // List dual joycon pairs | ||
| 507 | for (std::size_t i = 0; i < MaxSupportedControllers; i++) { | ||
| 508 | if (!left_joycons[i] || !right_joycons[i]) { | ||
| 509 | continue; | ||
| 510 | } | ||
| 511 | if (!left_joycons[i]->IsConnected() || !right_joycons[i]->IsConnected()) { | ||
| 512 | continue; | ||
| 513 | } | ||
| 514 | auto main_param = GetParamPackage(i, left_joycons[i]->GetHandleDeviceType()); | ||
| 515 | const auto second_param = GetParamPackage(i, right_joycons[i]->GetHandleDeviceType()); | ||
| 516 | const auto type = Joycon::ControllerType::Dual; | ||
| 517 | std::string name = fmt::format("{} {}", JoyconName(type), i + 1); | ||
| 518 | |||
| 519 | main_param.Set("display", std::move(name)); | ||
| 520 | main_param.Set("guid2", second_param.Get("guid", "")); | ||
| 521 | main_param.Set("pad", std::to_string(static_cast<size_t>(type))); | ||
| 522 | devices.emplace_back(main_param); | ||
| 523 | } | ||
| 524 | |||
| 525 | return devices; | ||
| 526 | } | ||
| 527 | |||
| 528 | ButtonMapping Joycons::GetButtonMappingForDevice(const Common::ParamPackage& params) { | ||
| 529 | static constexpr std::array<std::tuple<Settings::NativeButton::Values, Joycon::PadButton, bool>, | ||
| 530 | 18> | ||
| 531 | switch_to_joycon_button = { | ||
| 532 | std::tuple{Settings::NativeButton::A, Joycon::PadButton::A, true}, | ||
| 533 | {Settings::NativeButton::B, Joycon::PadButton::B, true}, | ||
| 534 | {Settings::NativeButton::X, Joycon::PadButton::X, true}, | ||
| 535 | {Settings::NativeButton::Y, Joycon::PadButton::Y, true}, | ||
| 536 | {Settings::NativeButton::DLeft, Joycon::PadButton::Left, false}, | ||
| 537 | {Settings::NativeButton::DUp, Joycon::PadButton::Up, false}, | ||
| 538 | {Settings::NativeButton::DRight, Joycon::PadButton::Right, false}, | ||
| 539 | {Settings::NativeButton::DDown, Joycon::PadButton::Down, false}, | ||
| 540 | {Settings::NativeButton::L, Joycon::PadButton::L, false}, | ||
| 541 | {Settings::NativeButton::R, Joycon::PadButton::R, true}, | ||
| 542 | {Settings::NativeButton::ZL, Joycon::PadButton::ZL, false}, | ||
| 543 | {Settings::NativeButton::ZR, Joycon::PadButton::ZR, true}, | ||
| 544 | {Settings::NativeButton::Plus, Joycon::PadButton::Plus, true}, | ||
| 545 | {Settings::NativeButton::Minus, Joycon::PadButton::Minus, false}, | ||
| 546 | {Settings::NativeButton::Home, Joycon::PadButton::Home, true}, | ||
| 547 | {Settings::NativeButton::Screenshot, Joycon::PadButton::Capture, false}, | ||
| 548 | {Settings::NativeButton::LStick, Joycon::PadButton::StickL, false}, | ||
| 549 | {Settings::NativeButton::RStick, Joycon::PadButton::StickR, true}, | ||
| 550 | }; | ||
| 551 | |||
| 552 | if (!params.Has("port")) { | ||
| 553 | return {}; | ||
| 554 | } | ||
| 555 | |||
| 556 | ButtonMapping mapping{}; | ||
| 557 | for (const auto& [switch_button, joycon_button, side] : switch_to_joycon_button) { | ||
| 558 | const std::size_t port = static_cast<std::size_t>(params.Get("port", 0)); | ||
| 559 | auto pad = static_cast<Joycon::ControllerType>(params.Get("pad", 0)); | ||
| 560 | if (pad == Joycon::ControllerType::Dual) { | ||
| 561 | pad = side ? Joycon::ControllerType::Right : Joycon::ControllerType::Left; | ||
| 562 | } | ||
| 563 | |||
| 564 | Common::ParamPackage button_params = GetParamPackage(port, pad); | ||
| 565 | button_params.Set("button", static_cast<int>(joycon_button)); | ||
| 566 | mapping.insert_or_assign(switch_button, std::move(button_params)); | ||
| 567 | } | ||
| 568 | |||
| 569 | // Map SL and SR buttons for left joycons | ||
| 570 | if (params.Get("pad", 0) == static_cast<int>(Joycon::ControllerType::Left)) { | ||
| 571 | const std::size_t port = static_cast<std::size_t>(params.Get("port", 0)); | ||
| 572 | Common::ParamPackage button_params = GetParamPackage(port, Joycon::ControllerType::Left); | ||
| 573 | |||
| 574 | Common::ParamPackage sl_button_params = button_params; | ||
| 575 | Common::ParamPackage sr_button_params = button_params; | ||
| 576 | sl_button_params.Set("button", static_cast<int>(Joycon::PadButton::LeftSL)); | ||
| 577 | sr_button_params.Set("button", static_cast<int>(Joycon::PadButton::LeftSR)); | ||
| 578 | mapping.insert_or_assign(Settings::NativeButton::SL, std::move(sl_button_params)); | ||
| 579 | mapping.insert_or_assign(Settings::NativeButton::SR, std::move(sr_button_params)); | ||
| 580 | } | ||
| 581 | |||
| 582 | // Map SL and SR buttons for right joycons | ||
| 583 | if (params.Get("pad", 0) == static_cast<int>(Joycon::ControllerType::Right)) { | ||
| 584 | const std::size_t port = static_cast<std::size_t>(params.Get("port", 0)); | ||
| 585 | Common::ParamPackage button_params = GetParamPackage(port, Joycon::ControllerType::Right); | ||
| 586 | |||
| 587 | Common::ParamPackage sl_button_params = button_params; | ||
| 588 | Common::ParamPackage sr_button_params = button_params; | ||
| 589 | sl_button_params.Set("button", static_cast<int>(Joycon::PadButton::RightSL)); | ||
| 590 | sr_button_params.Set("button", static_cast<int>(Joycon::PadButton::RightSR)); | ||
| 591 | mapping.insert_or_assign(Settings::NativeButton::SL, std::move(sl_button_params)); | ||
| 592 | mapping.insert_or_assign(Settings::NativeButton::SR, std::move(sr_button_params)); | ||
| 593 | } | ||
| 594 | |||
| 595 | return mapping; | ||
| 596 | } | ||
| 597 | |||
| 598 | AnalogMapping Joycons::GetAnalogMappingForDevice(const Common::ParamPackage& params) { | ||
| 599 | if (!params.Has("port")) { | ||
| 600 | return {}; | ||
| 601 | } | ||
| 602 | |||
| 603 | const std::size_t port = static_cast<std::size_t>(params.Get("port", 0)); | ||
| 604 | auto pad_left = static_cast<Joycon::ControllerType>(params.Get("pad", 0)); | ||
| 605 | auto pad_right = pad_left; | ||
| 606 | if (pad_left == Joycon::ControllerType::Dual) { | ||
| 607 | pad_left = Joycon::ControllerType::Left; | ||
| 608 | pad_right = Joycon::ControllerType::Right; | ||
| 609 | } | ||
| 610 | |||
| 611 | AnalogMapping mapping = {}; | ||
| 612 | Common::ParamPackage left_analog_params = GetParamPackage(port, pad_left); | ||
| 613 | left_analog_params.Set("axis_x", static_cast<int>(Joycon::PadAxes::LeftStickX)); | ||
| 614 | left_analog_params.Set("axis_y", static_cast<int>(Joycon::PadAxes::LeftStickY)); | ||
| 615 | mapping.insert_or_assign(Settings::NativeAnalog::LStick, std::move(left_analog_params)); | ||
| 616 | Common::ParamPackage right_analog_params = GetParamPackage(port, pad_right); | ||
| 617 | right_analog_params.Set("axis_x", static_cast<int>(Joycon::PadAxes::RightStickX)); | ||
| 618 | right_analog_params.Set("axis_y", static_cast<int>(Joycon::PadAxes::RightStickY)); | ||
| 619 | mapping.insert_or_assign(Settings::NativeAnalog::RStick, std::move(right_analog_params)); | ||
| 620 | return mapping; | ||
| 621 | } | ||
| 622 | |||
| 623 | MotionMapping Joycons::GetMotionMappingForDevice(const Common::ParamPackage& params) { | ||
| 624 | if (!params.Has("port")) { | ||
| 625 | return {}; | ||
| 626 | } | ||
| 627 | |||
| 628 | const std::size_t port = static_cast<std::size_t>(params.Get("port", 0)); | ||
| 629 | auto pad_left = static_cast<Joycon::ControllerType>(params.Get("pad", 0)); | ||
| 630 | auto pad_right = pad_left; | ||
| 631 | if (pad_left == Joycon::ControllerType::Dual) { | ||
| 632 | pad_left = Joycon::ControllerType::Left; | ||
| 633 | pad_right = Joycon::ControllerType::Right; | ||
| 634 | } | ||
| 635 | |||
| 636 | MotionMapping mapping = {}; | ||
| 637 | Common::ParamPackage left_motion_params = GetParamPackage(port, pad_left); | ||
| 638 | left_motion_params.Set("motion", 0); | ||
| 639 | mapping.insert_or_assign(Settings::NativeMotion::MotionLeft, std::move(left_motion_params)); | ||
| 640 | Common::ParamPackage right_Motion_params = GetParamPackage(port, pad_right); | ||
| 641 | right_Motion_params.Set("motion", 1); | ||
| 642 | mapping.insert_or_assign(Settings::NativeMotion::MotionRight, std::move(right_Motion_params)); | ||
| 643 | return mapping; | ||
| 644 | } | ||
| 645 | |||
| 646 | Common::Input::ButtonNames Joycons::GetUIButtonName(const Common::ParamPackage& params) const { | ||
| 647 | const auto button = static_cast<Joycon::PadButton>(params.Get("button", 0)); | ||
| 648 | switch (button) { | ||
| 649 | case Joycon::PadButton::Left: | ||
| 650 | return Common::Input::ButtonNames::ButtonLeft; | ||
| 651 | case Joycon::PadButton::Right: | ||
| 652 | return Common::Input::ButtonNames::ButtonRight; | ||
| 653 | case Joycon::PadButton::Down: | ||
| 654 | return Common::Input::ButtonNames::ButtonDown; | ||
| 655 | case Joycon::PadButton::Up: | ||
| 656 | return Common::Input::ButtonNames::ButtonUp; | ||
| 657 | case Joycon::PadButton::LeftSL: | ||
| 658 | case Joycon::PadButton::RightSL: | ||
| 659 | return Common::Input::ButtonNames::TriggerSL; | ||
| 660 | case Joycon::PadButton::LeftSR: | ||
| 661 | case Joycon::PadButton::RightSR: | ||
| 662 | return Common::Input::ButtonNames::TriggerSR; | ||
| 663 | case Joycon::PadButton::L: | ||
| 664 | return Common::Input::ButtonNames::TriggerL; | ||
| 665 | case Joycon::PadButton::R: | ||
| 666 | return Common::Input::ButtonNames::TriggerR; | ||
| 667 | case Joycon::PadButton::ZL: | ||
| 668 | return Common::Input::ButtonNames::TriggerZL; | ||
| 669 | case Joycon::PadButton::ZR: | ||
| 670 | return Common::Input::ButtonNames::TriggerZR; | ||
| 671 | case Joycon::PadButton::A: | ||
| 672 | return Common::Input::ButtonNames::ButtonA; | ||
| 673 | case Joycon::PadButton::B: | ||
| 674 | return Common::Input::ButtonNames::ButtonB; | ||
| 675 | case Joycon::PadButton::X: | ||
| 676 | return Common::Input::ButtonNames::ButtonX; | ||
| 677 | case Joycon::PadButton::Y: | ||
| 678 | return Common::Input::ButtonNames::ButtonY; | ||
| 679 | case Joycon::PadButton::Plus: | ||
| 680 | return Common::Input::ButtonNames::ButtonPlus; | ||
| 681 | case Joycon::PadButton::Minus: | ||
| 682 | return Common::Input::ButtonNames::ButtonMinus; | ||
| 683 | case Joycon::PadButton::Home: | ||
| 684 | return Common::Input::ButtonNames::ButtonHome; | ||
| 685 | case Joycon::PadButton::Capture: | ||
| 686 | return Common::Input::ButtonNames::ButtonCapture; | ||
| 687 | case Joycon::PadButton::StickL: | ||
| 688 | return Common::Input::ButtonNames::ButtonStickL; | ||
| 689 | case Joycon::PadButton::StickR: | ||
| 690 | return Common::Input::ButtonNames::ButtonStickR; | ||
| 691 | default: | ||
| 692 | return Common::Input::ButtonNames::Undefined; | ||
| 693 | } | ||
| 694 | } | ||
| 695 | |||
| 696 | Common::Input::ButtonNames Joycons::GetUIName(const Common::ParamPackage& params) const { | ||
| 697 | if (params.Has("button")) { | ||
| 698 | return GetUIButtonName(params); | ||
| 699 | } | ||
| 700 | if (params.Has("axis")) { | ||
| 701 | return Common::Input::ButtonNames::Value; | ||
| 702 | } | ||
| 703 | if (params.Has("motion")) { | ||
| 704 | return Common::Input::ButtonNames::Engine; | ||
| 705 | } | ||
| 706 | |||
| 707 | return Common::Input::ButtonNames::Invalid; | ||
| 708 | } | ||
| 709 | |||
| 710 | std::string Joycons::JoyconName(Joycon::ControllerType type) const { | ||
| 711 | switch (type) { | ||
| 712 | case Joycon::ControllerType::Left: | ||
| 713 | return "Left Joycon"; | ||
| 714 | case Joycon::ControllerType::Right: | ||
| 715 | return "Right Joycon"; | ||
| 716 | case Joycon::ControllerType::Pro: | ||
| 717 | return "Pro Controller"; | ||
| 718 | case Joycon::ControllerType::Dual: | ||
| 719 | return "Dual Joycon"; | ||
| 720 | default: | ||
| 721 | return "Unknown Switch Controller"; | ||
| 722 | } | ||
| 723 | } | ||
| 724 | } // namespace InputCommon | ||