From 014234fe0d919b1c703b747bdc116440d243f5d4 Mon Sep 17 00:00:00 2001 From: Souryo Date: Tue, 20 Dec 2016 17:05:02 -0500 Subject: [PATCH] Linux: Add a bit of logging for gamepads + fixed file descriptor leak --- Linux/LinuxGameController.cpp | 49 +++++++++++++++++++++-------------- Linux/LinuxGameController.h | 2 +- Linux/LinuxKeyManager.cpp | 4 +-- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/Linux/LinuxGameController.cpp b/Linux/LinuxGameController.cpp index e51d69a0..e47175c9 100644 --- a/Linux/LinuxGameController.cpp +++ b/Linux/LinuxGameController.cpp @@ -10,27 +10,38 @@ #include #include -std::shared_ptr LinuxGameController::GetController(int deviceID) +std::shared_ptr LinuxGameController::GetController(int deviceID, bool logInformation) { - int fd = open(("/dev/input/event" + std::to_string(deviceID)).c_str(), O_RDONLY | O_NONBLOCK); - if(fd < 0) { - //fprintf(stderr, "error: %d %s\n", errno, strerror(errno)); - return nullptr; - } - - libevdev* device = nullptr; - int rc = libevdev_new_from_fd(fd, &device); - if(rc < 0) { - //fprintf(stderr, "error: %d %s\n", -rc, strerror(-rc)); - return nullptr; - } - - if(libevdev_has_event_type(device, EV_KEY) && libevdev_has_event_code(device, EV_KEY, BTN_GAMEPAD) || - libevdev_has_event_type(device, EV_ABS) && libevdev_has_event_code(device, EV_ABS, ABS_X)) { - MessageManager::Log(std::string("[Input Connected] Name: ") + libevdev_get_name(device) + " Vendor: " + std::to_string(libevdev_get_id_vendor(device)) + " Product: " + std::to_string(libevdev_get_id_product(device))); - return std::shared_ptr(new LinuxGameController(deviceID, fd, device)); - } + std::string deviceName = "/dev/input/event" + std::to_string(deviceID); + struct stat buffer; + if(stat(deviceName.c_str(), &buffer) == 0) { + int fd = open(deviceName.c_str(), O_RDONLY | O_NONBLOCK); + if(fd < 0) { + if(logInformation) { + MessageManager::Log("[Input] " + deviceName + " error: " + std::to_string(errno) + " " + strerror(errno)); + } + return nullptr; + } + + libevdev* device = nullptr; + int rc = libevdev_new_from_fd(fd, &device); + if(rc < 0) { + if(logInformation) { + MessageManager::Log("[Input] " + deviceName + " error: " + std::to_string(errno) + " " + strerror(errno)); + } + close(fd); + return nullptr; + } + if(libevdev_has_event_type(device, EV_KEY) && libevdev_has_event_code(device, EV_KEY, BTN_GAMEPAD) || + libevdev_has_event_type(device, EV_ABS) && libevdev_has_event_code(device, EV_ABS, ABS_X)) { + MessageManager::Log(std::string("[Input Connected] Name: ") + libevdev_get_name(device) + " Vendor: " + std::to_string(libevdev_get_id_vendor(device)) + " Product: " + std::to_string(libevdev_get_id_product(device))); + return std::shared_ptr(new LinuxGameController(deviceID, fd, device)); + } else { + MessageManager::Log(std::string("[Input] Device ignored (Not a gamepad) - Name: ") + libevdev_get_name(device) + " Vendor: " + std::to_string(libevdev_get_id_vendor(device)) + " Product: " + std::to_string(libevdev_get_id_product(device))); + close(fd); + } + } return nullptr; } diff --git a/Linux/LinuxGameController.h b/Linux/LinuxGameController.h index 330b3c49..7dce6ed7 100644 --- a/Linux/LinuxGameController.h +++ b/Linux/LinuxGameController.h @@ -22,7 +22,7 @@ private: public: ~LinuxGameController(); - static std::shared_ptr GetController(int deviceID); + static std::shared_ptr GetController(int deviceID, bool logInformation); bool IsDisconnected(); int GetDeviceID(); diff --git a/Linux/LinuxKeyManager.cpp b/Linux/LinuxKeyManager.cpp index ea4027c3..2a7c8c19 100755 --- a/Linux/LinuxKeyManager.cpp +++ b/Linux/LinuxKeyManager.cpp @@ -256,7 +256,7 @@ LinuxKeyManager::LinuxKeyManager() } for(int i = 0; i < 30; i++) { - std::shared_ptr controller = LinuxGameController::GetController(i); + std::shared_ptr controller = LinuxGameController::GetController(i, true); if(controller) { _controllers.push_back(controller); } @@ -363,7 +363,7 @@ void LinuxKeyManager::StartUpdateDeviceThread() for(int i = 0; i < 30; i++) { if(std::find(connectedIDs.begin(), connectedIDs.end(), i) == connectedIDs.end()) { - std::shared_ptr controller = LinuxGameController::GetController(i); + std::shared_ptr controller = LinuxGameController::GetController(i, false); if(controller) { controllersToAdd.push_back(controller); }