diff options
Diffstat (limited to '')
| -rw-r--r-- | src/input_common/helpers/joycon_driver.cpp | 245 |
1 files changed, 180 insertions, 65 deletions
diff --git a/src/input_common/helpers/joycon_driver.cpp b/src/input_common/helpers/joycon_driver.cpp index 95106f16d..cf51f3481 100644 --- a/src/input_common/helpers/joycon_driver.cpp +++ b/src/input_common/helpers/joycon_driver.cpp | |||
| @@ -1,7 +1,9 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project | 1 | // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | 3 | ||
| 4 | #include "common/input.h" | ||
| 4 | #include "common/logging/log.h" | 5 | #include "common/logging/log.h" |
| 6 | #include "common/scope_exit.h" | ||
| 5 | #include "common/swap.h" | 7 | #include "common/swap.h" |
| 6 | #include "common/thread.h" | 8 | #include "common/thread.h" |
| 7 | #include "input_common/helpers/joycon_driver.h" | 9 | #include "input_common/helpers/joycon_driver.h" |
| @@ -27,13 +29,13 @@ void JoyconDriver::Stop() { | |||
| 27 | input_thread = {}; | 29 | input_thread = {}; |
| 28 | } | 30 | } |
| 29 | 31 | ||
| 30 | DriverResult JoyconDriver::RequestDeviceAccess(SDL_hid_device_info* device_info) { | 32 | Common::Input::DriverResult JoyconDriver::RequestDeviceAccess(SDL_hid_device_info* device_info) { |
| 31 | std::scoped_lock lock{mutex}; | 33 | std::scoped_lock lock{mutex}; |
| 32 | 34 | ||
| 33 | handle_device_type = ControllerType::None; | 35 | handle_device_type = ControllerType::None; |
| 34 | GetDeviceType(device_info, handle_device_type); | 36 | GetDeviceType(device_info, handle_device_type); |
| 35 | if (handle_device_type == ControllerType::None) { | 37 | if (handle_device_type == ControllerType::None) { |
| 36 | return DriverResult::UnsupportedControllerType; | 38 | return Common::Input::DriverResult::UnsupportedControllerType; |
| 37 | } | 39 | } |
| 38 | 40 | ||
| 39 | hidapi_handle->handle = | 41 | hidapi_handle->handle = |
| @@ -42,15 +44,15 @@ DriverResult JoyconDriver::RequestDeviceAccess(SDL_hid_device_info* device_info) | |||
| 42 | if (!hidapi_handle->handle) { | 44 | if (!hidapi_handle->handle) { |
| 43 | LOG_ERROR(Input, "Yuzu can't gain access to this device: ID {:04X}:{:04X}.", | 45 | LOG_ERROR(Input, "Yuzu can't gain access to this device: ID {:04X}:{:04X}.", |
| 44 | device_info->vendor_id, device_info->product_id); | 46 | device_info->vendor_id, device_info->product_id); |
| 45 | return DriverResult::HandleInUse; | 47 | return Common::Input::DriverResult::HandleInUse; |
| 46 | } | 48 | } |
| 47 | SDL_hid_set_nonblocking(hidapi_handle->handle, 1); | 49 | SDL_hid_set_nonblocking(hidapi_handle->handle, 1); |
| 48 | return DriverResult::Success; | 50 | return Common::Input::DriverResult::Success; |
| 49 | } | 51 | } |
| 50 | 52 | ||
| 51 | DriverResult JoyconDriver::InitializeDevice() { | 53 | Common::Input::DriverResult JoyconDriver::InitializeDevice() { |
| 52 | if (!hidapi_handle->handle) { | 54 | if (!hidapi_handle->handle) { |
| 53 | return DriverResult::InvalidHandle; | 55 | return Common::Input::DriverResult::InvalidHandle; |
| 54 | } | 56 | } |
| 55 | std::scoped_lock lock{mutex}; | 57 | std::scoped_lock lock{mutex}; |
| 56 | disable_input_thread = true; | 58 | disable_input_thread = true; |
| @@ -71,6 +73,7 @@ DriverResult JoyconDriver::InitializeDevice() { | |||
| 71 | nfc_enabled = false; | 73 | nfc_enabled = false; |
| 72 | passive_enabled = false; | 74 | passive_enabled = false; |
| 73 | irs_enabled = false; | 75 | irs_enabled = false; |
| 76 | input_only_device = false; | ||
| 74 | gyro_sensitivity = Joycon::GyroSensitivity::DPS2000; | 77 | gyro_sensitivity = Joycon::GyroSensitivity::DPS2000; |
| 75 | gyro_performance = Joycon::GyroPerformance::HZ833; | 78 | gyro_performance = Joycon::GyroPerformance::HZ833; |
| 76 | accelerometer_sensitivity = Joycon::AccelerometerSensitivity::G8; | 79 | accelerometer_sensitivity = Joycon::AccelerometerSensitivity::G8; |
| @@ -85,16 +88,23 @@ DriverResult JoyconDriver::InitializeDevice() { | |||
| 85 | rumble_protocol = std::make_unique<RumbleProtocol>(hidapi_handle); | 88 | rumble_protocol = std::make_unique<RumbleProtocol>(hidapi_handle); |
| 86 | 89 | ||
| 87 | // Get fixed joycon info | 90 | // Get fixed joycon info |
| 88 | generic_protocol->GetVersionNumber(version); | 91 | if (generic_protocol->GetVersionNumber(version) != Common::Input::DriverResult::Success) { |
| 89 | generic_protocol->SetLowPowerMode(false); | 92 | // If this command fails the device doesn't accept configuration commands |
| 90 | generic_protocol->GetColor(color); | 93 | input_only_device = true; |
| 91 | if (handle_device_type == ControllerType::Pro) { | 94 | } |
| 92 | // Some 3rd party controllers aren't pro controllers | 95 | |
| 93 | generic_protocol->GetControllerType(device_type); | 96 | if (!input_only_device) { |
| 94 | } else { | 97 | generic_protocol->SetLowPowerMode(false); |
| 95 | device_type = handle_device_type; | 98 | generic_protocol->GetColor(color); |
| 99 | if (handle_device_type == ControllerType::Pro) { | ||
| 100 | // Some 3rd party controllers aren't pro controllers | ||
| 101 | generic_protocol->GetControllerType(device_type); | ||
| 102 | } else { | ||
| 103 | device_type = handle_device_type; | ||
| 104 | } | ||
| 105 | generic_protocol->GetSerialNumber(serial_number); | ||
| 96 | } | 106 | } |
| 97 | generic_protocol->GetSerialNumber(serial_number); | 107 | |
| 98 | supported_features = GetSupportedFeatures(); | 108 | supported_features = GetSupportedFeatures(); |
| 99 | 109 | ||
| 100 | // Get Calibration data | 110 | // Get Calibration data |
| @@ -112,7 +122,7 @@ DriverResult JoyconDriver::InitializeDevice() { | |||
| 112 | joycon_poller = std::make_unique<JoyconPoller>(device_type, left_stick_calibration, | 122 | joycon_poller = std::make_unique<JoyconPoller>(device_type, left_stick_calibration, |
| 113 | right_stick_calibration, motion_calibration); | 123 | right_stick_calibration, motion_calibration); |
| 114 | 124 | ||
| 115 | // Start pooling for data | 125 | // Start polling for data |
| 116 | is_connected = true; | 126 | is_connected = true; |
| 117 | if (!input_thread_running) { | 127 | if (!input_thread_running) { |
| 118 | input_thread = | 128 | input_thread = |
| @@ -120,7 +130,7 @@ DriverResult JoyconDriver::InitializeDevice() { | |||
| 120 | } | 130 | } |
| 121 | 131 | ||
| 122 | disable_input_thread = false; | 132 | disable_input_thread = false; |
| 123 | return DriverResult::Success; | 133 | return Common::Input::DriverResult::Success; |
| 124 | } | 134 | } |
| 125 | 135 | ||
| 126 | void JoyconDriver::InputThread(std::stop_token stop_token) { | 136 | void JoyconDriver::InputThread(std::stop_token stop_token) { |
| @@ -208,7 +218,7 @@ void JoyconDriver::OnNewData(std::span<u8> buffer) { | |||
| 208 | joycon_poller->UpdateCamera(irs_protocol->GetImage(), irs_protocol->GetIrsFormat()); | 218 | joycon_poller->UpdateCamera(irs_protocol->GetImage(), irs_protocol->GetIrsFormat()); |
| 209 | } | 219 | } |
| 210 | 220 | ||
| 211 | if (nfc_protocol->IsEnabled()) { | 221 | if (nfc_protocol->IsPolling()) { |
| 212 | if (amiibo_detected) { | 222 | if (amiibo_detected) { |
| 213 | if (!nfc_protocol->HasAmiibo()) { | 223 | if (!nfc_protocol->HasAmiibo()) { |
| 214 | joycon_poller->UpdateAmiibo({}); | 224 | joycon_poller->UpdateAmiibo({}); |
| @@ -218,10 +228,10 @@ void JoyconDriver::OnNewData(std::span<u8> buffer) { | |||
| 218 | } | 228 | } |
| 219 | 229 | ||
| 220 | if (!amiibo_detected) { | 230 | if (!amiibo_detected) { |
| 221 | std::vector<u8> data(0x21C); | 231 | Joycon::TagInfo tag_info; |
| 222 | const auto result = nfc_protocol->ScanAmiibo(data); | 232 | const auto result = nfc_protocol->GetTagInfo(tag_info); |
| 223 | if (result == DriverResult::Success) { | 233 | if (result == Common::Input::DriverResult::Success) { |
| 224 | joycon_poller->UpdateAmiibo(data); | 234 | joycon_poller->UpdateAmiibo(tag_info); |
| 225 | amiibo_detected = true; | 235 | amiibo_detected = true; |
| 226 | } | 236 | } |
| 227 | } | 237 | } |
| @@ -246,7 +256,8 @@ void JoyconDriver::OnNewData(std::span<u8> buffer) { | |||
| 246 | } | 256 | } |
| 247 | } | 257 | } |
| 248 | 258 | ||
| 249 | DriverResult JoyconDriver::SetPollingMode() { | 259 | Common::Input::DriverResult JoyconDriver::SetPollingMode() { |
| 260 | SCOPE_EXIT({ disable_input_thread = false; }); | ||
| 250 | disable_input_thread = true; | 261 | disable_input_thread = true; |
| 251 | 262 | ||
| 252 | rumble_protocol->EnableRumble(vibration_enabled && supported_features.vibration); | 263 | rumble_protocol->EnableRumble(vibration_enabled && supported_features.vibration); |
| @@ -259,6 +270,10 @@ DriverResult JoyconDriver::SetPollingMode() { | |||
| 259 | generic_protocol->EnableImu(false); | 270 | generic_protocol->EnableImu(false); |
| 260 | } | 271 | } |
| 261 | 272 | ||
| 273 | if (input_only_device) { | ||
| 274 | return Common::Input::DriverResult::NotSupported; | ||
| 275 | } | ||
| 276 | |||
| 262 | if (irs_protocol->IsEnabled()) { | 277 | if (irs_protocol->IsEnabled()) { |
| 263 | irs_protocol->DisableIrs(); | 278 | irs_protocol->DisableIrs(); |
| 264 | } | 279 | } |
| @@ -275,46 +290,42 @@ DriverResult JoyconDriver::SetPollingMode() { | |||
| 275 | 290 | ||
| 276 | if (irs_enabled && supported_features.irs) { | 291 | if (irs_enabled && supported_features.irs) { |
| 277 | auto result = irs_protocol->EnableIrs(); | 292 | auto result = irs_protocol->EnableIrs(); |
| 278 | if (result == DriverResult::Success) { | 293 | if (result == Common::Input::DriverResult::Success) { |
| 279 | disable_input_thread = false; | ||
| 280 | return result; | 294 | return result; |
| 281 | } | 295 | } |
| 282 | irs_protocol->DisableIrs(); | 296 | irs_protocol->DisableIrs(); |
| 283 | LOG_ERROR(Input, "Error enabling IRS"); | 297 | LOG_ERROR(Input, "Error enabling IRS"); |
| 298 | return result; | ||
| 284 | } | 299 | } |
| 285 | 300 | ||
| 286 | if (nfc_enabled && supported_features.nfc) { | 301 | if (nfc_enabled && supported_features.nfc) { |
| 287 | auto result = nfc_protocol->EnableNfc(); | 302 | auto result = nfc_protocol->EnableNfc(); |
| 288 | if (result == DriverResult::Success) { | 303 | if (result == Common::Input::DriverResult::Success) { |
| 289 | result = nfc_protocol->StartNFCPollingMode(); | ||
| 290 | } | ||
| 291 | if (result == DriverResult::Success) { | ||
| 292 | disable_input_thread = false; | ||
| 293 | return result; | 304 | return result; |
| 294 | } | 305 | } |
| 295 | nfc_protocol->DisableNfc(); | 306 | nfc_protocol->DisableNfc(); |
| 296 | LOG_ERROR(Input, "Error enabling NFC"); | 307 | LOG_ERROR(Input, "Error enabling NFC"); |
| 308 | return result; | ||
| 297 | } | 309 | } |
| 298 | 310 | ||
| 299 | if (hidbus_enabled && supported_features.hidbus) { | 311 | if (hidbus_enabled && supported_features.hidbus) { |
| 300 | auto result = ring_protocol->EnableRingCon(); | 312 | auto result = ring_protocol->EnableRingCon(); |
| 301 | if (result == DriverResult::Success) { | 313 | if (result == Common::Input::DriverResult::Success) { |
| 302 | result = ring_protocol->StartRingconPolling(); | 314 | result = ring_protocol->StartRingconPolling(); |
| 303 | } | 315 | } |
| 304 | if (result == DriverResult::Success) { | 316 | if (result == Common::Input::DriverResult::Success) { |
| 305 | ring_connected = true; | 317 | ring_connected = true; |
| 306 | disable_input_thread = false; | ||
| 307 | return result; | 318 | return result; |
| 308 | } | 319 | } |
| 309 | ring_connected = false; | 320 | ring_connected = false; |
| 310 | ring_protocol->DisableRingCon(); | 321 | ring_protocol->DisableRingCon(); |
| 311 | LOG_ERROR(Input, "Error enabling Ringcon"); | 322 | LOG_ERROR(Input, "Error enabling Ringcon"); |
| 323 | return result; | ||
| 312 | } | 324 | } |
| 313 | 325 | ||
| 314 | if (passive_enabled && supported_features.passive) { | 326 | if (passive_enabled && supported_features.passive) { |
| 315 | const auto result = generic_protocol->EnablePassiveMode(); | 327 | const auto result = generic_protocol->EnablePassiveMode(); |
| 316 | if (result == DriverResult::Success) { | 328 | if (result == Common::Input::DriverResult::Success) { |
| 317 | disable_input_thread = false; | ||
| 318 | return result; | 329 | return result; |
| 319 | } | 330 | } |
| 320 | LOG_ERROR(Input, "Error enabling passive mode"); | 331 | LOG_ERROR(Input, "Error enabling passive mode"); |
| @@ -322,13 +333,12 @@ DriverResult JoyconDriver::SetPollingMode() { | |||
| 322 | 333 | ||
| 323 | // Default Mode | 334 | // Default Mode |
| 324 | const auto result = generic_protocol->EnableActiveMode(); | 335 | const auto result = generic_protocol->EnableActiveMode(); |
| 325 | if (result != DriverResult::Success) { | 336 | if (result != Common::Input::DriverResult::Success) { |
| 326 | LOG_ERROR(Input, "Error enabling active mode"); | 337 | LOG_ERROR(Input, "Error enabling active mode"); |
| 327 | } | 338 | } |
| 328 | // Switch calls this function after enabling active mode | 339 | // Switch calls this function after enabling active mode |
| 329 | generic_protocol->TriggersElapsed(); | 340 | generic_protocol->TriggersElapsed(); |
| 330 | 341 | ||
| 331 | disable_input_thread = false; | ||
| 332 | return result; | 342 | return result; |
| 333 | } | 343 | } |
| 334 | 344 | ||
| @@ -339,6 +349,10 @@ JoyconDriver::SupportedFeatures JoyconDriver::GetSupportedFeatures() { | |||
| 339 | .vibration = true, | 349 | .vibration = true, |
| 340 | }; | 350 | }; |
| 341 | 351 | ||
| 352 | if (input_only_device) { | ||
| 353 | return features; | ||
| 354 | } | ||
| 355 | |||
| 342 | if (device_type == ControllerType::Right) { | 356 | if (device_type == ControllerType::Right) { |
| 343 | features.nfc = true; | 357 | features.nfc = true; |
| 344 | features.irs = true; | 358 | features.irs = true; |
| @@ -383,26 +397,26 @@ bool JoyconDriver::IsPayloadCorrect(int status, std::span<const u8> buffer) { | |||
| 383 | return true; | 397 | return true; |
| 384 | } | 398 | } |
| 385 | 399 | ||
| 386 | DriverResult JoyconDriver::SetVibration(const VibrationValue& vibration) { | 400 | Common::Input::DriverResult JoyconDriver::SetVibration(const VibrationValue& vibration) { |
| 387 | std::scoped_lock lock{mutex}; | 401 | std::scoped_lock lock{mutex}; |
| 388 | if (disable_input_thread) { | 402 | if (disable_input_thread) { |
| 389 | return DriverResult::HandleInUse; | 403 | return Common::Input::DriverResult::HandleInUse; |
| 390 | } | 404 | } |
| 391 | return rumble_protocol->SendVibration(vibration); | 405 | return rumble_protocol->SendVibration(vibration); |
| 392 | } | 406 | } |
| 393 | 407 | ||
| 394 | DriverResult JoyconDriver::SetLedConfig(u8 led_pattern) { | 408 | Common::Input::DriverResult JoyconDriver::SetLedConfig(u8 led_pattern) { |
| 395 | std::scoped_lock lock{mutex}; | 409 | std::scoped_lock lock{mutex}; |
| 396 | if (disable_input_thread) { | 410 | if (disable_input_thread) { |
| 397 | return DriverResult::HandleInUse; | 411 | return Common::Input::DriverResult::HandleInUse; |
| 398 | } | 412 | } |
| 399 | return generic_protocol->SetLedPattern(led_pattern); | 413 | return generic_protocol->SetLedPattern(led_pattern); |
| 400 | } | 414 | } |
| 401 | 415 | ||
| 402 | DriverResult JoyconDriver::SetIrsConfig(IrsMode mode_, IrsResolution format_) { | 416 | Common::Input::DriverResult JoyconDriver::SetIrsConfig(IrsMode mode_, IrsResolution format_) { |
| 403 | std::scoped_lock lock{mutex}; | 417 | std::scoped_lock lock{mutex}; |
| 404 | if (disable_input_thread) { | 418 | if (disable_input_thread) { |
| 405 | return DriverResult::HandleInUse; | 419 | return Common::Input::DriverResult::HandleInUse; |
| 406 | } | 420 | } |
| 407 | disable_input_thread = true; | 421 | disable_input_thread = true; |
| 408 | const auto result = irs_protocol->SetIrsConfig(mode_, format_); | 422 | const auto result = irs_protocol->SetIrsConfig(mode_, format_); |
| @@ -410,7 +424,7 @@ DriverResult JoyconDriver::SetIrsConfig(IrsMode mode_, IrsResolution format_) { | |||
| 410 | return result; | 424 | return result; |
| 411 | } | 425 | } |
| 412 | 426 | ||
| 413 | DriverResult JoyconDriver::SetPassiveMode() { | 427 | Common::Input::DriverResult JoyconDriver::SetPassiveMode() { |
| 414 | std::scoped_lock lock{mutex}; | 428 | std::scoped_lock lock{mutex}; |
| 415 | motion_enabled = false; | 429 | motion_enabled = false; |
| 416 | hidbus_enabled = false; | 430 | hidbus_enabled = false; |
| @@ -420,7 +434,7 @@ DriverResult JoyconDriver::SetPassiveMode() { | |||
| 420 | return SetPollingMode(); | 434 | return SetPollingMode(); |
| 421 | } | 435 | } |
| 422 | 436 | ||
| 423 | DriverResult JoyconDriver::SetActiveMode() { | 437 | Common::Input::DriverResult JoyconDriver::SetActiveMode() { |
| 424 | if (is_ring_disabled_by_irs) { | 438 | if (is_ring_disabled_by_irs) { |
| 425 | is_ring_disabled_by_irs = false; | 439 | is_ring_disabled_by_irs = false; |
| 426 | SetActiveMode(); | 440 | SetActiveMode(); |
| @@ -436,11 +450,11 @@ DriverResult JoyconDriver::SetActiveMode() { | |||
| 436 | return SetPollingMode(); | 450 | return SetPollingMode(); |
| 437 | } | 451 | } |
| 438 | 452 | ||
| 439 | DriverResult JoyconDriver::SetIrMode() { | 453 | Common::Input::DriverResult JoyconDriver::SetIrMode() { |
| 440 | std::scoped_lock lock{mutex}; | 454 | std::scoped_lock lock{mutex}; |
| 441 | 455 | ||
| 442 | if (!supported_features.irs) { | 456 | if (!supported_features.irs) { |
| 443 | return DriverResult::NotSupported; | 457 | return Common::Input::DriverResult::NotSupported; |
| 444 | } | 458 | } |
| 445 | 459 | ||
| 446 | if (ring_connected) { | 460 | if (ring_connected) { |
| @@ -455,11 +469,11 @@ DriverResult JoyconDriver::SetIrMode() { | |||
| 455 | return SetPollingMode(); | 469 | return SetPollingMode(); |
| 456 | } | 470 | } |
| 457 | 471 | ||
| 458 | DriverResult JoyconDriver::SetNfcMode() { | 472 | Common::Input::DriverResult JoyconDriver::SetNfcMode() { |
| 459 | std::scoped_lock lock{mutex}; | 473 | std::scoped_lock lock{mutex}; |
| 460 | 474 | ||
| 461 | if (!supported_features.nfc) { | 475 | if (!supported_features.nfc) { |
| 462 | return DriverResult::NotSupported; | 476 | return Common::Input::DriverResult::NotSupported; |
| 463 | } | 477 | } |
| 464 | 478 | ||
| 465 | motion_enabled = true; | 479 | motion_enabled = true; |
| @@ -470,11 +484,11 @@ DriverResult JoyconDriver::SetNfcMode() { | |||
| 470 | return SetPollingMode(); | 484 | return SetPollingMode(); |
| 471 | } | 485 | } |
| 472 | 486 | ||
| 473 | DriverResult JoyconDriver::SetRingConMode() { | 487 | Common::Input::DriverResult JoyconDriver::SetRingConMode() { |
| 474 | std::scoped_lock lock{mutex}; | 488 | std::scoped_lock lock{mutex}; |
| 475 | 489 | ||
| 476 | if (!supported_features.hidbus) { | 490 | if (!supported_features.hidbus) { |
| 477 | return DriverResult::NotSupported; | 491 | return Common::Input::DriverResult::NotSupported; |
| 478 | } | 492 | } |
| 479 | 493 | ||
| 480 | motion_enabled = true; | 494 | motion_enabled = true; |
| @@ -486,29 +500,130 @@ DriverResult JoyconDriver::SetRingConMode() { | |||
| 486 | const auto result = SetPollingMode(); | 500 | const auto result = SetPollingMode(); |
| 487 | 501 | ||
| 488 | if (!ring_connected) { | 502 | if (!ring_connected) { |
| 489 | return DriverResult::NoDeviceDetected; | 503 | return Common::Input::DriverResult::NoDeviceDetected; |
| 504 | } | ||
| 505 | |||
| 506 | return result; | ||
| 507 | } | ||
| 508 | |||
| 509 | Common::Input::DriverResult JoyconDriver::StartNfcPolling() { | ||
| 510 | std::scoped_lock lock{mutex}; | ||
| 511 | |||
| 512 | if (!supported_features.nfc) { | ||
| 513 | return Common::Input::DriverResult::NotSupported; | ||
| 514 | } | ||
| 515 | if (!nfc_protocol->IsEnabled()) { | ||
| 516 | return Common::Input::DriverResult::Disabled; | ||
| 490 | } | 517 | } |
| 491 | 518 | ||
| 519 | disable_input_thread = true; | ||
| 520 | const auto result = nfc_protocol->StartNFCPollingMode(); | ||
| 521 | disable_input_thread = false; | ||
| 522 | |||
| 492 | return result; | 523 | return result; |
| 493 | } | 524 | } |
| 494 | 525 | ||
| 495 | DriverResult JoyconDriver::WriteNfcData(std::span<const u8> data) { | 526 | Common::Input::DriverResult JoyconDriver::StopNfcPolling() { |
| 496 | std::scoped_lock lock{mutex}; | 527 | std::scoped_lock lock{mutex}; |
| 528 | |||
| 529 | if (!supported_features.nfc) { | ||
| 530 | return Common::Input::DriverResult::NotSupported; | ||
| 531 | } | ||
| 532 | if (!nfc_protocol->IsEnabled()) { | ||
| 533 | return Common::Input::DriverResult::Disabled; | ||
| 534 | } | ||
| 535 | |||
| 497 | disable_input_thread = true; | 536 | disable_input_thread = true; |
| 537 | const auto result = nfc_protocol->StopNFCPollingMode(); | ||
| 538 | disable_input_thread = false; | ||
| 539 | |||
| 540 | if (amiibo_detected) { | ||
| 541 | amiibo_detected = false; | ||
| 542 | joycon_poller->UpdateAmiibo({}); | ||
| 543 | } | ||
| 544 | |||
| 545 | return result; | ||
| 546 | } | ||
| 547 | |||
| 548 | Common::Input::DriverResult JoyconDriver::ReadAmiiboData(std::vector<u8>& out_data) { | ||
| 549 | std::scoped_lock lock{mutex}; | ||
| 498 | 550 | ||
| 499 | if (!supported_features.nfc) { | 551 | if (!supported_features.nfc) { |
| 500 | return DriverResult::NotSupported; | 552 | return Common::Input::DriverResult::NotSupported; |
| 501 | } | 553 | } |
| 502 | if (!nfc_protocol->IsEnabled()) { | 554 | if (!nfc_protocol->IsEnabled()) { |
| 503 | return DriverResult::Disabled; | 555 | return Common::Input::DriverResult::Disabled; |
| 504 | } | 556 | } |
| 505 | if (!amiibo_detected) { | 557 | if (!amiibo_detected) { |
| 506 | return DriverResult::ErrorWritingData; | 558 | return Common::Input::DriverResult::ErrorWritingData; |
| 507 | } | 559 | } |
| 508 | 560 | ||
| 561 | out_data.resize(0x21C); | ||
| 562 | disable_input_thread = true; | ||
| 563 | const auto result = nfc_protocol->ReadAmiibo(out_data); | ||
| 564 | disable_input_thread = false; | ||
| 565 | |||
| 566 | return result; | ||
| 567 | } | ||
| 568 | |||
| 569 | Common::Input::DriverResult JoyconDriver::WriteNfcData(std::span<const u8> data) { | ||
| 570 | std::scoped_lock lock{mutex}; | ||
| 571 | |||
| 572 | if (!supported_features.nfc) { | ||
| 573 | return Common::Input::DriverResult::NotSupported; | ||
| 574 | } | ||
| 575 | if (!nfc_protocol->IsEnabled()) { | ||
| 576 | return Common::Input::DriverResult::Disabled; | ||
| 577 | } | ||
| 578 | if (!amiibo_detected) { | ||
| 579 | return Common::Input::DriverResult::ErrorWritingData; | ||
| 580 | } | ||
| 581 | |||
| 582 | disable_input_thread = true; | ||
| 509 | const auto result = nfc_protocol->WriteAmiibo(data); | 583 | const auto result = nfc_protocol->WriteAmiibo(data); |
| 584 | disable_input_thread = false; | ||
| 585 | |||
| 586 | return result; | ||
| 587 | } | ||
| 510 | 588 | ||
| 589 | Common::Input::DriverResult JoyconDriver::ReadMifareData(std::span<const MifareReadChunk> data, | ||
| 590 | std::span<MifareReadData> out_data) { | ||
| 591 | std::scoped_lock lock{mutex}; | ||
| 592 | |||
| 593 | if (!supported_features.nfc) { | ||
| 594 | return Common::Input::DriverResult::NotSupported; | ||
| 595 | } | ||
| 596 | if (!nfc_protocol->IsEnabled()) { | ||
| 597 | return Common::Input::DriverResult::Disabled; | ||
| 598 | } | ||
| 599 | if (!amiibo_detected) { | ||
| 600 | return Common::Input::DriverResult::ErrorWritingData; | ||
| 601 | } | ||
| 602 | |||
| 603 | disable_input_thread = true; | ||
| 604 | const auto result = nfc_protocol->ReadMifare(data, out_data); | ||
| 511 | disable_input_thread = false; | 605 | disable_input_thread = false; |
| 606 | |||
| 607 | return result; | ||
| 608 | } | ||
| 609 | |||
| 610 | Common::Input::DriverResult JoyconDriver::WriteMifareData(std::span<const MifareWriteChunk> data) { | ||
| 611 | std::scoped_lock lock{mutex}; | ||
| 612 | |||
| 613 | if (!supported_features.nfc) { | ||
| 614 | return Common::Input::DriverResult::NotSupported; | ||
| 615 | } | ||
| 616 | if (!nfc_protocol->IsEnabled()) { | ||
| 617 | return Common::Input::DriverResult::Disabled; | ||
| 618 | } | ||
| 619 | if (!amiibo_detected) { | ||
| 620 | return Common::Input::DriverResult::ErrorWritingData; | ||
| 621 | } | ||
| 622 | |||
| 623 | disable_input_thread = true; | ||
| 624 | const auto result = nfc_protocol->WriteMifare(data); | ||
| 625 | disable_input_thread = false; | ||
| 626 | |||
| 512 | return result; | 627 | return result; |
| 513 | } | 628 | } |
| 514 | 629 | ||
| @@ -561,8 +676,8 @@ void JoyconDriver::SetCallbacks(const JoyconCallbacks& callbacks) { | |||
| 561 | joycon_poller->SetCallbacks(callbacks); | 676 | joycon_poller->SetCallbacks(callbacks); |
| 562 | } | 677 | } |
| 563 | 678 | ||
| 564 | DriverResult JoyconDriver::GetDeviceType(SDL_hid_device_info* device_info, | 679 | Common::Input::DriverResult JoyconDriver::GetDeviceType(SDL_hid_device_info* device_info, |
| 565 | ControllerType& controller_type) { | 680 | ControllerType& controller_type) { |
| 566 | static constexpr std::array<std::pair<u32, ControllerType>, 6> supported_devices{ | 681 | static constexpr std::array<std::pair<u32, ControllerType>, 6> supported_devices{ |
| 567 | std::pair<u32, ControllerType>{0x2006, ControllerType::Left}, | 682 | std::pair<u32, ControllerType>{0x2006, ControllerType::Left}, |
| 568 | {0x2007, ControllerType::Right}, | 683 | {0x2007, ControllerType::Right}, |
| @@ -572,25 +687,25 @@ DriverResult JoyconDriver::GetDeviceType(SDL_hid_device_info* device_info, | |||
| 572 | 687 | ||
| 573 | controller_type = ControllerType::None; | 688 | controller_type = ControllerType::None; |
| 574 | if (device_info->vendor_id != nintendo_vendor_id) { | 689 | if (device_info->vendor_id != nintendo_vendor_id) { |
| 575 | return DriverResult::UnsupportedControllerType; | 690 | return Common::Input::DriverResult::UnsupportedControllerType; |
| 576 | } | 691 | } |
| 577 | 692 | ||
| 578 | for (const auto& [product_id, type] : supported_devices) { | 693 | for (const auto& [product_id, type] : supported_devices) { |
| 579 | if (device_info->product_id == static_cast<u16>(product_id)) { | 694 | if (device_info->product_id == static_cast<u16>(product_id)) { |
| 580 | controller_type = type; | 695 | controller_type = type; |
| 581 | return Joycon::DriverResult::Success; | 696 | return Common::Input::DriverResult::Success; |
| 582 | } | 697 | } |
| 583 | } | 698 | } |
| 584 | return Joycon::DriverResult::UnsupportedControllerType; | 699 | return Common::Input::DriverResult::UnsupportedControllerType; |
| 585 | } | 700 | } |
| 586 | 701 | ||
| 587 | DriverResult JoyconDriver::GetSerialNumber(SDL_hid_device_info* device_info, | 702 | Common::Input::DriverResult JoyconDriver::GetSerialNumber(SDL_hid_device_info* device_info, |
| 588 | SerialNumber& serial_number) { | 703 | SerialNumber& serial_number) { |
| 589 | if (device_info->serial_number == nullptr) { | 704 | if (device_info->serial_number == nullptr) { |
| 590 | return DriverResult::Unknown; | 705 | return Common::Input::DriverResult::Unknown; |
| 591 | } | 706 | } |
| 592 | std::memcpy(&serial_number, device_info->serial_number, 15); | 707 | std::memcpy(&serial_number, device_info->serial_number, 15); |
| 593 | return Joycon::DriverResult::Success; | 708 | return Common::Input::DriverResult::Success; |
| 594 | } | 709 | } |
| 595 | 710 | ||
| 596 | } // namespace InputCommon::Joycon | 711 | } // namespace InputCommon::Joycon |