diff options
| author | 2023-07-01 15:01:11 -0700 | |
|---|---|---|
| committer | 2023-07-01 15:01:11 -0700 | |
| commit | 98685d48e3cb9f25f6919f004ec62cadf33afad2 (patch) | |
| tree | 9df2ce7f57370641589bfae7196c77b090bcbe0f /src/input_common/drivers/joycon.cpp | |
| parent | PR feedback + constification (diff) | |
| parent | Update translations (2023-07-01) (#10972) (diff) | |
| download | yuzu-98685d48e3cb9f25f6919f004ec62cadf33afad2.tar.gz yuzu-98685d48e3cb9f25f6919f004ec62cadf33afad2.tar.xz yuzu-98685d48e3cb9f25f6919f004ec62cadf33afad2.zip | |
Merge remote-tracking branch 'origin/master' into ssl
Diffstat (limited to 'src/input_common/drivers/joycon.cpp')
| -rw-r--r-- | src/input_common/drivers/joycon.cpp | 158 |
1 files changed, 136 insertions, 22 deletions
diff --git a/src/input_common/drivers/joycon.cpp b/src/input_common/drivers/joycon.cpp index b2b5677c8..0aca5a3a3 100644 --- a/src/input_common/drivers/joycon.cpp +++ b/src/input_common/drivers/joycon.cpp | |||
| @@ -102,12 +102,12 @@ bool Joycons::IsDeviceNew(SDL_hid_device_info* device_info) const { | |||
| 102 | Joycon::SerialNumber serial_number{}; | 102 | Joycon::SerialNumber serial_number{}; |
| 103 | 103 | ||
| 104 | const auto result = Joycon::JoyconDriver::GetDeviceType(device_info, type); | 104 | const auto result = Joycon::JoyconDriver::GetDeviceType(device_info, type); |
| 105 | if (result != Joycon::DriverResult::Success) { | 105 | if (result != Common::Input::DriverResult::Success) { |
| 106 | return false; | 106 | return false; |
| 107 | } | 107 | } |
| 108 | 108 | ||
| 109 | const auto result2 = Joycon::JoyconDriver::GetSerialNumber(device_info, serial_number); | 109 | const auto result2 = Joycon::JoyconDriver::GetSerialNumber(device_info, serial_number); |
| 110 | if (result2 != Joycon::DriverResult::Success) { | 110 | if (result2 != Common::Input::DriverResult::Success) { |
| 111 | return false; | 111 | return false; |
| 112 | } | 112 | } |
| 113 | 113 | ||
| @@ -171,10 +171,10 @@ void Joycons::RegisterNewDevice(SDL_hid_device_info* device_info) { | |||
| 171 | LOG_WARNING(Input, "No free handles available"); | 171 | LOG_WARNING(Input, "No free handles available"); |
| 172 | return; | 172 | return; |
| 173 | } | 173 | } |
| 174 | if (result == Joycon::DriverResult::Success) { | 174 | if (result == Common::Input::DriverResult::Success) { |
| 175 | result = handle->RequestDeviceAccess(device_info); | 175 | result = handle->RequestDeviceAccess(device_info); |
| 176 | } | 176 | } |
| 177 | if (result == Joycon::DriverResult::Success) { | 177 | if (result == Common::Input::DriverResult::Success) { |
| 178 | LOG_WARNING(Input, "Initialize device"); | 178 | LOG_WARNING(Input, "Initialize device"); |
| 179 | 179 | ||
| 180 | const std::size_t port = handle->GetDevicePort(); | 180 | const std::size_t port = handle->GetDevicePort(); |
| @@ -195,8 +195,8 @@ void Joycons::RegisterNewDevice(SDL_hid_device_info* device_info) { | |||
| 195 | OnMotionUpdate(port, type, id, value); | 195 | OnMotionUpdate(port, type, id, value); |
| 196 | }}, | 196 | }}, |
| 197 | .on_ring_data = {[this](f32 ring_data) { OnRingConUpdate(ring_data); }}, | 197 | .on_ring_data = {[this](f32 ring_data) { OnRingConUpdate(ring_data); }}, |
| 198 | .on_amiibo_data = {[this, port, type](const std::vector<u8>& amiibo_data) { | 198 | .on_amiibo_data = {[this, port, type](const Joycon::TagInfo& tag_info) { |
| 199 | OnAmiiboUpdate(port, type, amiibo_data); | 199 | OnAmiiboUpdate(port, type, tag_info); |
| 200 | }}, | 200 | }}, |
| 201 | .on_camera_data = {[this, port](const std::vector<u8>& camera_data, | 201 | .on_camera_data = {[this, port](const std::vector<u8>& camera_data, |
| 202 | Joycon::IrsResolution format) { | 202 | Joycon::IrsResolution format) { |
| @@ -273,8 +273,7 @@ Common::Input::DriverResult Joycons::SetLeds(const PadIdentifier& identifier, | |||
| 273 | led_config += led_status.led_3 ? 4 : 0; | 273 | led_config += led_status.led_3 ? 4 : 0; |
| 274 | led_config += led_status.led_4 ? 8 : 0; | 274 | led_config += led_status.led_4 ? 8 : 0; |
| 275 | 275 | ||
| 276 | return static_cast<Common::Input::DriverResult>( | 276 | return handle->SetLedConfig(static_cast<u8>(led_config)); |
| 277 | handle->SetLedConfig(static_cast<u8>(led_config))); | ||
| 278 | } | 277 | } |
| 279 | 278 | ||
| 280 | Common::Input::DriverResult Joycons::SetCameraFormat(const PadIdentifier& identifier, | 279 | Common::Input::DriverResult Joycons::SetCameraFormat(const PadIdentifier& identifier, |
| @@ -283,21 +282,113 @@ Common::Input::DriverResult Joycons::SetCameraFormat(const PadIdentifier& identi | |||
| 283 | if (handle == nullptr) { | 282 | if (handle == nullptr) { |
| 284 | return Common::Input::DriverResult::InvalidHandle; | 283 | return Common::Input::DriverResult::InvalidHandle; |
| 285 | } | 284 | } |
| 286 | return static_cast<Common::Input::DriverResult>(handle->SetIrsConfig( | 285 | return handle->SetIrsConfig(Joycon::IrsMode::ImageTransfer, |
| 287 | Joycon::IrsMode::ImageTransfer, static_cast<Joycon::IrsResolution>(camera_format))); | 286 | static_cast<Joycon::IrsResolution>(camera_format)); |
| 288 | }; | 287 | }; |
| 289 | 288 | ||
| 290 | Common::Input::NfcState Joycons::SupportsNfc(const PadIdentifier& identifier_) const { | 289 | Common::Input::NfcState Joycons::SupportsNfc(const PadIdentifier& identifier_) const { |
| 291 | return Common::Input::NfcState::Success; | 290 | return Common::Input::NfcState::Success; |
| 292 | }; | 291 | }; |
| 293 | 292 | ||
| 293 | Common::Input::NfcState Joycons::StartNfcPolling(const PadIdentifier& identifier) { | ||
| 294 | auto handle = GetHandle(identifier); | ||
| 295 | if (handle == nullptr) { | ||
| 296 | return Common::Input::NfcState::Unknown; | ||
| 297 | } | ||
| 298 | return TranslateDriverResult(handle->StartNfcPolling()); | ||
| 299 | }; | ||
| 300 | |||
| 301 | Common::Input::NfcState Joycons::StopNfcPolling(const PadIdentifier& identifier) { | ||
| 302 | auto handle = GetHandle(identifier); | ||
| 303 | if (handle == nullptr) { | ||
| 304 | return Common::Input::NfcState::Unknown; | ||
| 305 | } | ||
| 306 | return TranslateDriverResult(handle->StopNfcPolling()); | ||
| 307 | }; | ||
| 308 | |||
| 309 | Common::Input::NfcState Joycons::ReadAmiiboData(const PadIdentifier& identifier, | ||
| 310 | std::vector<u8>& out_data) { | ||
| 311 | auto handle = GetHandle(identifier); | ||
| 312 | if (handle == nullptr) { | ||
| 313 | return Common::Input::NfcState::Unknown; | ||
| 314 | } | ||
| 315 | return TranslateDriverResult(handle->ReadAmiiboData(out_data)); | ||
| 316 | } | ||
| 317 | |||
| 294 | Common::Input::NfcState Joycons::WriteNfcData(const PadIdentifier& identifier, | 318 | Common::Input::NfcState Joycons::WriteNfcData(const PadIdentifier& identifier, |
| 295 | const std::vector<u8>& data) { | 319 | const std::vector<u8>& data) { |
| 296 | auto handle = GetHandle(identifier); | 320 | auto handle = GetHandle(identifier); |
| 297 | if (handle->WriteNfcData(data) != Joycon::DriverResult::Success) { | 321 | if (handle == nullptr) { |
| 298 | return Common::Input::NfcState::WriteFailed; | 322 | return Common::Input::NfcState::Unknown; |
| 299 | } | 323 | } |
| 300 | return Common::Input::NfcState::Success; | 324 | return TranslateDriverResult(handle->WriteNfcData(data)); |
| 325 | }; | ||
| 326 | |||
| 327 | Common::Input::NfcState Joycons::ReadMifareData(const PadIdentifier& identifier, | ||
| 328 | const Common::Input::MifareRequest& request, | ||
| 329 | Common::Input::MifareRequest& data) { | ||
| 330 | auto handle = GetHandle(identifier); | ||
| 331 | if (handle == nullptr) { | ||
| 332 | return Common::Input::NfcState::Unknown; | ||
| 333 | } | ||
| 334 | |||
| 335 | const auto command = static_cast<Joycon::MifareCmd>(request.data[0].command); | ||
| 336 | std::vector<Joycon::MifareReadChunk> read_request{}; | ||
| 337 | for (const auto& request_data : request.data) { | ||
| 338 | if (request_data.command == 0) { | ||
| 339 | continue; | ||
| 340 | } | ||
| 341 | Joycon::MifareReadChunk chunk = { | ||
| 342 | .command = command, | ||
| 343 | .sector_key = {}, | ||
| 344 | .sector = request_data.sector, | ||
| 345 | }; | ||
| 346 | memcpy(chunk.sector_key.data(), request_data.key.data(), | ||
| 347 | sizeof(Joycon::MifareReadChunk::sector_key)); | ||
| 348 | read_request.emplace_back(chunk); | ||
| 349 | } | ||
| 350 | |||
| 351 | std::vector<Joycon::MifareReadData> read_data(read_request.size()); | ||
| 352 | const auto result = handle->ReadMifareData(read_request, read_data); | ||
| 353 | if (result == Common::Input::DriverResult::Success) { | ||
| 354 | for (std::size_t i = 0; i < read_request.size(); i++) { | ||
| 355 | data.data[i] = { | ||
| 356 | .command = static_cast<u8>(command), | ||
| 357 | .sector = read_data[i].sector, | ||
| 358 | .key = {}, | ||
| 359 | .data = read_data[i].data, | ||
| 360 | }; | ||
| 361 | } | ||
| 362 | } | ||
| 363 | return TranslateDriverResult(result); | ||
| 364 | }; | ||
| 365 | |||
| 366 | Common::Input::NfcState Joycons::WriteMifareData(const PadIdentifier& identifier, | ||
| 367 | const Common::Input::MifareRequest& request) { | ||
| 368 | auto handle = GetHandle(identifier); | ||
| 369 | if (handle == nullptr) { | ||
| 370 | return Common::Input::NfcState::Unknown; | ||
| 371 | } | ||
| 372 | |||
| 373 | const auto command = static_cast<Joycon::MifareCmd>(request.data[0].command); | ||
| 374 | std::vector<Joycon::MifareWriteChunk> write_request{}; | ||
| 375 | for (const auto& request_data : request.data) { | ||
| 376 | if (request_data.command == 0) { | ||
| 377 | continue; | ||
| 378 | } | ||
| 379 | Joycon::MifareWriteChunk chunk = { | ||
| 380 | .command = command, | ||
| 381 | .sector_key = {}, | ||
| 382 | .sector = request_data.sector, | ||
| 383 | .data = {}, | ||
| 384 | }; | ||
| 385 | memcpy(chunk.sector_key.data(), request_data.key.data(), | ||
| 386 | sizeof(Joycon::MifareReadChunk::sector_key)); | ||
| 387 | memcpy(chunk.data.data(), request_data.data.data(), sizeof(Joycon::MifareWriteChunk::data)); | ||
| 388 | write_request.emplace_back(chunk); | ||
| 389 | } | ||
| 390 | |||
| 391 | return TranslateDriverResult(handle->WriteMifareData(write_request)); | ||
| 301 | }; | 392 | }; |
| 302 | 393 | ||
| 303 | Common::Input::DriverResult Joycons::SetPollingMode(const PadIdentifier& identifier, | 394 | Common::Input::DriverResult Joycons::SetPollingMode(const PadIdentifier& identifier, |
| @@ -310,15 +401,15 @@ Common::Input::DriverResult Joycons::SetPollingMode(const PadIdentifier& identif | |||
| 310 | 401 | ||
| 311 | switch (polling_mode) { | 402 | switch (polling_mode) { |
| 312 | case Common::Input::PollingMode::Active: | 403 | case Common::Input::PollingMode::Active: |
| 313 | return static_cast<Common::Input::DriverResult>(handle->SetActiveMode()); | 404 | return handle->SetActiveMode(); |
| 314 | case Common::Input::PollingMode::Passive: | 405 | case Common::Input::PollingMode::Passive: |
| 315 | return static_cast<Common::Input::DriverResult>(handle->SetPassiveMode()); | 406 | return handle->SetPassiveMode(); |
| 316 | case Common::Input::PollingMode::IR: | 407 | case Common::Input::PollingMode::IR: |
| 317 | return static_cast<Common::Input::DriverResult>(handle->SetIrMode()); | 408 | return handle->SetIrMode(); |
| 318 | case Common::Input::PollingMode::NFC: | 409 | case Common::Input::PollingMode::NFC: |
| 319 | return static_cast<Common::Input::DriverResult>(handle->SetNfcMode()); | 410 | return handle->SetNfcMode(); |
| 320 | case Common::Input::PollingMode::Ring: | 411 | case Common::Input::PollingMode::Ring: |
| 321 | return static_cast<Common::Input::DriverResult>(handle->SetRingConMode()); | 412 | return handle->SetRingConMode(); |
| 322 | default: | 413 | default: |
| 323 | return Common::Input::DriverResult::NotSupported; | 414 | return Common::Input::DriverResult::NotSupported; |
| 324 | } | 415 | } |
| @@ -403,11 +494,20 @@ void Joycons::OnRingConUpdate(f32 ring_data) { | |||
| 403 | } | 494 | } |
| 404 | 495 | ||
| 405 | void Joycons::OnAmiiboUpdate(std::size_t port, Joycon::ControllerType type, | 496 | void Joycons::OnAmiiboUpdate(std::size_t port, Joycon::ControllerType type, |
| 406 | const std::vector<u8>& amiibo_data) { | 497 | const Joycon::TagInfo& tag_info) { |
| 407 | const auto identifier = GetIdentifier(port, type); | 498 | const auto identifier = GetIdentifier(port, type); |
| 408 | const auto nfc_state = amiibo_data.empty() ? Common::Input::NfcState::AmiiboRemoved | 499 | const auto nfc_state = tag_info.uuid_length == 0 ? Common::Input::NfcState::AmiiboRemoved |
| 409 | : Common::Input::NfcState::NewAmiibo; | 500 | : Common::Input::NfcState::NewAmiibo; |
| 410 | SetNfc(identifier, {nfc_state, amiibo_data}); | 501 | |
| 502 | const Common::Input::NfcStatus nfc_status{ | ||
| 503 | .state = nfc_state, | ||
| 504 | .uuid_length = tag_info.uuid_length, | ||
| 505 | .protocol = tag_info.protocol, | ||
| 506 | .tag_type = tag_info.tag_type, | ||
| 507 | .uuid = tag_info.uuid, | ||
| 508 | }; | ||
| 509 | |||
| 510 | SetNfc(identifier, nfc_status); | ||
| 411 | } | 511 | } |
| 412 | 512 | ||
| 413 | void Joycons::OnCameraUpdate(std::size_t port, const std::vector<u8>& camera_data, | 513 | void Joycons::OnCameraUpdate(std::size_t port, const std::vector<u8>& camera_data, |
| @@ -726,4 +826,18 @@ std::string Joycons::JoyconName(Joycon::ControllerType type) const { | |||
| 726 | return "Unknown Switch Controller"; | 826 | return "Unknown Switch Controller"; |
| 727 | } | 827 | } |
| 728 | } | 828 | } |
| 829 | |||
| 830 | Common::Input::NfcState Joycons::TranslateDriverResult(Common::Input::DriverResult result) const { | ||
| 831 | switch (result) { | ||
| 832 | case Common::Input::DriverResult::Success: | ||
| 833 | return Common::Input::NfcState::Success; | ||
| 834 | case Common::Input::DriverResult::Disabled: | ||
| 835 | return Common::Input::NfcState::WrongDeviceState; | ||
| 836 | case Common::Input::DriverResult::NotSupported: | ||
| 837 | return Common::Input::NfcState::NotSupported; | ||
| 838 | default: | ||
| 839 | return Common::Input::NfcState::Unknown; | ||
| 840 | } | ||
| 841 | } | ||
| 842 | |||
| 729 | } // namespace InputCommon | 843 | } // namespace InputCommon |