summaryrefslogtreecommitdiff
path: root/src/input_common/drivers/joycon.cpp
diff options
context:
space:
mode:
authorGravatar german772023-01-13 23:29:05 -0600
committerGravatar Narr the Reg2023-01-20 00:51:45 -0600
commit340f15d1fa79594dbe12a6e19140ba012751b533 (patch)
tree7a9ef54a17f927e4b8cf98dd32dd6d41c0d75201 /src/input_common/drivers/joycon.cpp
parentcore: hid: Only set the polling mode to the correct side (diff)
downloadyuzu-340f15d1fa79594dbe12a6e19140ba012751b533.tar.gz
yuzu-340f15d1fa79594dbe12a6e19140ba012751b533.tar.xz
yuzu-340f15d1fa79594dbe12a6e19140ba012751b533.zip
input_common: Address byte review
Diffstat (limited to 'src/input_common/drivers/joycon.cpp')
-rw-r--r--src/input_common/drivers/joycon.cpp50
1 files changed, 26 insertions, 24 deletions
diff --git a/src/input_common/drivers/joycon.cpp b/src/input_common/drivers/joycon.cpp
index fff886ca8..1582def13 100644
--- a/src/input_common/drivers/joycon.cpp
+++ b/src/input_common/drivers/joycon.cpp
@@ -60,15 +60,12 @@ void Joycons::Setup() {
60 device = std::make_shared<Joycon::JoyconDriver>(port++); 60 device = std::make_shared<Joycon::JoyconDriver>(port++);
61 } 61 }
62 62
63 if (!scan_thread_running) { 63 scan_thread = std::jthread([this](std::stop_token stop_token) { ScanThread(stop_token); });
64 scan_thread = std::jthread([this](std::stop_token stop_token) { ScanThread(stop_token); });
65 }
66} 64}
67 65
68void Joycons::ScanThread(std::stop_token stop_token) { 66void Joycons::ScanThread(std::stop_token stop_token) {
69 constexpr u16 nintendo_vendor_id = 0x057e; 67 constexpr u16 nintendo_vendor_id = 0x057e;
70 Common::SetCurrentThreadName("yuzu:input:JoyconScanThread"); 68 Common::SetCurrentThreadName("JoyconScanThread");
71 scan_thread_running = true;
72 while (!stop_token.stop_requested()) { 69 while (!stop_token.stop_requested()) {
73 SDL_hid_device_info* devs = SDL_hid_enumerate(nintendo_vendor_id, 0x0); 70 SDL_hid_device_info* devs = SDL_hid_enumerate(nintendo_vendor_id, 0x0);
74 SDL_hid_device_info* cur_dev = devs; 71 SDL_hid_device_info* cur_dev = devs;
@@ -82,9 +79,9 @@ void Joycons::ScanThread(std::stop_token stop_token) {
82 cur_dev = cur_dev->next; 79 cur_dev = cur_dev->next;
83 } 80 }
84 81
82 SDL_hid_free_enumeration(devs);
85 std::this_thread::sleep_for(std::chrono::seconds(5)); 83 std::this_thread::sleep_for(std::chrono::seconds(5));
86 } 84 }
87 scan_thread_running = false;
88} 85}
89 86
90bool Joycons::IsDeviceNew(SDL_hid_device_info* device_info) const { 87bool Joycons::IsDeviceNew(SDL_hid_device_info* device_info) const {
@@ -185,19 +182,19 @@ void Joycons::RegisterNewDevice(SDL_hid_device_info* device_info) {
185 182
186std::shared_ptr<Joycon::JoyconDriver> Joycons::GetNextFreeHandle( 183std::shared_ptr<Joycon::JoyconDriver> Joycons::GetNextFreeHandle(
187 Joycon::ControllerType type) const { 184 Joycon::ControllerType type) const {
188
189 if (type == Joycon::ControllerType::Left) { 185 if (type == Joycon::ControllerType::Left) {
190 for (const auto& device : left_joycons) { 186 const auto unconnected_device =
191 if (!device->IsConnected()) { 187 std::ranges::find_if(left_joycons, [](auto& device) { return !device->IsConnected(); });
192 return device; 188 if (unconnected_device != left_joycons.end()) {
193 } 189 return *unconnected_device;
194 } 190 }
195 } 191 }
196 if (type == Joycon::ControllerType::Right) { 192 if (type == Joycon::ControllerType::Right) {
197 for (const auto& device : right_joycons) { 193 const auto unconnected_device = std::ranges::find_if(
198 if (!device->IsConnected()) { 194 right_joycons, [](auto& device) { return !device->IsConnected(); });
199 return device; 195
200 } 196 if (unconnected_device != right_joycons.end()) {
197 return *unconnected_device;
201 } 198 }
202 } 199 }
203 return nullptr; 200 return nullptr;
@@ -391,20 +388,25 @@ std::shared_ptr<Joycon::JoyconDriver> Joycons::GetHandle(PadIdentifier identifie
391 return false; 388 return false;
392 }; 389 };
393 const auto type = static_cast<Joycon::ControllerType>(identifier.pad); 390 const auto type = static_cast<Joycon::ControllerType>(identifier.pad);
391
394 if (type == Joycon::ControllerType::Left) { 392 if (type == Joycon::ControllerType::Left) {
395 for (const auto& device : left_joycons) { 393 const auto matching_device = std::ranges::find_if(
396 if (is_handle_active(device)) { 394 left_joycons, [is_handle_active](auto& device) { return is_handle_active(device); });
397 return device; 395
398 } 396 if (matching_device != left_joycons.end()) {
397 return *matching_device;
399 } 398 }
400 } 399 }
400
401 if (type == Joycon::ControllerType::Right) { 401 if (type == Joycon::ControllerType::Right) {
402 for (const auto& device : right_joycons) { 402 const auto matching_device = std::ranges::find_if(
403 if (is_handle_active(device)) { 403 right_joycons, [is_handle_active](auto& device) { return is_handle_active(device); });
404 return device; 404
405 } 405 if (matching_device != right_joycons.end()) {
406 return *matching_device;
406 } 407 }
407 } 408 }
409
408 return nullptr; 410 return nullptr;
409} 411}
410 412
@@ -676,7 +678,7 @@ std::string Joycons::JoyconName(Joycon::ControllerType type) const {
676 case Joycon::ControllerType::Dual: 678 case Joycon::ControllerType::Dual:
677 return "Dual Joycon"; 679 return "Dual Joycon";
678 default: 680 default:
679 return "Unknow Joycon"; 681 return "Unknown Joycon";
680 } 682 }
681} 683}
682} // namespace InputCommon 684} // namespace InputCommon