summaryrefslogtreecommitdiffstats
path: root/src/input_common/drivers/joycon.cpp
diff options
context:
space:
mode:
authorgerman77 <juangerman-13@hotmail.com>2023-01-14 06:29:05 +0100
committerNarr the Reg <juangerman-13@hotmail.com>2023-01-20 07:51:45 +0100
commit340f15d1fa79594dbe12a6e19140ba012751b533 (patch)
tree7a9ef54a17f927e4b8cf98dd32dd6d41c0d75201 /src/input_common/drivers/joycon.cpp
parentcore: hid: Only set the polling mode to the correct side (diff)
downloadyuzu-340f15d1fa79594dbe12a6e19140ba012751b533.tar
yuzu-340f15d1fa79594dbe12a6e19140ba012751b533.tar.gz
yuzu-340f15d1fa79594dbe12a6e19140ba012751b533.tar.bz2
yuzu-340f15d1fa79594dbe12a6e19140ba012751b533.tar.lz
yuzu-340f15d1fa79594dbe12a6e19140ba012751b533.tar.xz
yuzu-340f15d1fa79594dbe12a6e19140ba012751b533.tar.zst
yuzu-340f15d1fa79594dbe12a6e19140ba012751b533.zip
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() {
device = std::make_shared<Joycon::JoyconDriver>(port++);
}
- if (!scan_thread_running) {
- scan_thread = std::jthread([this](std::stop_token stop_token) { ScanThread(stop_token); });
- }
+ scan_thread = std::jthread([this](std::stop_token stop_token) { ScanThread(stop_token); });
}
void Joycons::ScanThread(std::stop_token stop_token) {
constexpr u16 nintendo_vendor_id = 0x057e;
- Common::SetCurrentThreadName("yuzu:input:JoyconScanThread");
- scan_thread_running = true;
+ Common::SetCurrentThreadName("JoyconScanThread");
while (!stop_token.stop_requested()) {
SDL_hid_device_info* devs = SDL_hid_enumerate(nintendo_vendor_id, 0x0);
SDL_hid_device_info* cur_dev = devs;
@@ -82,9 +79,9 @@ void Joycons::ScanThread(std::stop_token stop_token) {
cur_dev = cur_dev->next;
}
+ SDL_hid_free_enumeration(devs);
std::this_thread::sleep_for(std::chrono::seconds(5));
}
- scan_thread_running = false;
}
bool Joycons::IsDeviceNew(SDL_hid_device_info* device_info) const {
@@ -185,19 +182,19 @@ void Joycons::RegisterNewDevice(SDL_hid_device_info* device_info) {
std::shared_ptr<Joycon::JoyconDriver> Joycons::GetNextFreeHandle(
Joycon::ControllerType type) const {
-
if (type == Joycon::ControllerType::Left) {
- for (const auto& device : left_joycons) {
- if (!device->IsConnected()) {
- return device;
- }
+ const auto unconnected_device =
+ std::ranges::find_if(left_joycons, [](auto& device) { return !device->IsConnected(); });
+ if (unconnected_device != left_joycons.end()) {
+ return *unconnected_device;
}
}
if (type == Joycon::ControllerType::Right) {
- for (const auto& device : right_joycons) {
- if (!device->IsConnected()) {
- return device;
- }
+ const auto unconnected_device = std::ranges::find_if(
+ right_joycons, [](auto& device) { return !device->IsConnected(); });
+
+ if (unconnected_device != right_joycons.end()) {
+ return *unconnected_device;
}
}
return nullptr;
@@ -391,20 +388,25 @@ std::shared_ptr<Joycon::JoyconDriver> Joycons::GetHandle(PadIdentifier identifie
return false;
};
const auto type = static_cast<Joycon::ControllerType>(identifier.pad);
+
if (type == Joycon::ControllerType::Left) {
- for (const auto& device : left_joycons) {
- if (is_handle_active(device)) {
- return device;
- }
+ const auto matching_device = std::ranges::find_if(
+ left_joycons, [is_handle_active](auto& device) { return is_handle_active(device); });
+
+ if (matching_device != left_joycons.end()) {
+ return *matching_device;
}
}
+
if (type == Joycon::ControllerType::Right) {
- for (const auto& device : right_joycons) {
- if (is_handle_active(device)) {
- return device;
- }
+ const auto matching_device = std::ranges::find_if(
+ right_joycons, [is_handle_active](auto& device) { return is_handle_active(device); });
+
+ if (matching_device != right_joycons.end()) {
+ return *matching_device;
}
}
+
return nullptr;
}
@@ -676,7 +678,7 @@ std::string Joycons::JoyconName(Joycon::ControllerType type) const {
case Joycon::ControllerType::Dual:
return "Dual Joycon";
default:
- return "Unknow Joycon";
+ return "Unknown Joycon";
}
}
} // namespace InputCommon