From 0675fbdfe3d145dcaa469fb76914278f1da4a273 Mon Sep 17 00:00:00 2001 From: Sour Date: Fri, 8 Feb 2019 20:34:10 -0500 Subject: [PATCH] Linux: Read all /dev/input/event entries, not just the first 30 --- Linux/LinuxKeyManager.cpp | 56 ++++++++++++++++++++++------------- Linux/LinuxKeyManager.h | 1 + Utilities/FolderUtilities.cpp | 4 +-- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/Linux/LinuxKeyManager.cpp b/Linux/LinuxKeyManager.cpp index 224f8623..29321431 100755 --- a/Linux/LinuxKeyManager.cpp +++ b/Linux/LinuxKeyManager.cpp @@ -1,6 +1,7 @@ #include #include "LinuxKeyManager.h" #include "LinuxGameController.h" +#include "../Utilities/FolderUtilities.h" #include "../Core/ControlManager.h" #include "../Core/Console.h" @@ -257,12 +258,7 @@ LinuxKeyManager::LinuxKeyManager(shared_ptr console) _keyCodes[keyDef.description] = keyDef.keyCode; } - for(int i = 0; i < 30; i++) { - std::shared_ptr controller = LinuxGameController::GetController(_console, i, true); - if(controller) { - _controllers.push_back(controller); - } - } + CheckForGamepads(true); _disableAllKeys = false; _stopUpdateDeviceThread = false; @@ -354,30 +350,50 @@ void LinuxKeyManager::UpdateDevices() //Only needed to detect newly plugged in devices } +void LinuxKeyManager::CheckForGamepads(bool logInformation) +{ + vector connectedIDs; + for(int i = _controllers.size() - 1; i >= 0; i--) { + if(!_controllers[i]->IsDisconnected()) { + connectedIDs.push_back(_controllers[i]->GetDeviceID()); + } + } + + vector files = FolderUtilities::GetFilesInFolder("/dev/input/", {}, false); + for(size_t i = 0; i < files.size(); i++) { + string filename = FolderUtilities::GetFilename(files[i], false); + if(filename.find("event", 0) == 0) { + int deviceId = 0; + try { + deviceId = std::stoi(filename.substr(5)); + } catch(std::exception e) { + continue; + } + + if(std::find(connectedIDs.begin(), connectedIDs.end(), deviceId) == connectedIDs.end()) { + std::shared_ptr controller = LinuxGameController::GetController(_console, deviceId, logInformation); + if(controller) { + _controllers.push_back(controller); + } + } + } + } +} + void LinuxKeyManager::StartUpdateDeviceThread() { _updateDeviceThread = std::thread([=]() { while(!_stopUpdateDeviceThread) { - //Check for newly plugged in controllers every 2 secs + //Check for newly plugged in controllers every 5 secs + vector> controllersToAdd; vector indexesToRemove; - vector connectedIDs; - std::vector> controllersToAdd; for(int i = _controllers.size() - 1; i >= 0; i--) { if(_controllers[i]->IsDisconnected()) { indexesToRemove.push_back(i); - } else { - connectedIDs.push_back(_controllers[i]->GetDeviceID()); } } - for(int i = 0; i < 30; i++) { - if(std::find(connectedIDs.begin(), connectedIDs.end(), i) == connectedIDs.end()) { - std::shared_ptr controller = LinuxGameController::GetController(_console, i, false); - if(controller) { - controllersToAdd.push_back(controller); - } - } - } + CheckForGamepads(false); if(!indexesToRemove.empty() || !controllersToAdd.empty()) { _console->Pause(); @@ -390,7 +406,7 @@ void LinuxKeyManager::StartUpdateDeviceThread() _console->Resume(); } - _stopSignal.Wait(2000); + _stopSignal.Wait(5000); } }); } diff --git a/Linux/LinuxKeyManager.h b/Linux/LinuxKeyManager.h index 449a4ac9..975540a9 100755 --- a/Linux/LinuxKeyManager.h +++ b/Linux/LinuxKeyManager.h @@ -31,6 +31,7 @@ private: bool _disableAllKeys; void StartUpdateDeviceThread(); + void CheckForGamepads(bool logInformation); public: LinuxKeyManager(shared_ptr console); diff --git a/Utilities/FolderUtilities.cpp b/Utilities/FolderUtilities.cpp index 45bc8d2d..bb4d4f34 100644 --- a/Utilities/FolderUtilities.cpp +++ b/Utilities/FolderUtilities.cpp @@ -167,7 +167,7 @@ vector FolderUtilities::GetFilesInFolder(string rootFolder, std::unorder } else { string extension = i->path().extension().u8string(); std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower); - if(extensions.find(extension) != extensions.end()) { + if(extensions.empty() || extensions.find(extension) != extensions.end()) { files.push_back(i->path().u8string()); } } @@ -176,7 +176,7 @@ vector FolderUtilities::GetFilesInFolder(string rootFolder, std::unorder for(fs::directory_iterator i(fs::u8path(rootFolder)), end; i != end; i++) { string extension = i->path().extension().u8string(); std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower); - if(extensions.find(extension) != extensions.end()) { + if(extensions.empty() || extensions.find(extension) != extensions.end()) { files.push_back(i->path().u8string()); } }