Linux: Add a bit of logging for gamepads + fixed file descriptor leak

This commit is contained in:
Souryo 2016-12-20 17:05:02 -05:00
parent 2eaa349c12
commit 014234fe0d
3 changed files with 33 additions and 22 deletions

View file

@ -10,18 +10,26 @@
#include <fcntl.h>
#include <iostream>
std::shared_ptr<LinuxGameController> LinuxGameController::GetController(int deviceID)
std::shared_ptr<LinuxGameController> LinuxGameController::GetController(int deviceID, bool logInformation)
{
int fd = open(("/dev/input/event" + std::to_string(deviceID)).c_str(), O_RDONLY | O_NONBLOCK);
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) {
//fprintf(stderr, "error: %d %s\n", errno, strerror(errno));
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) {
//fprintf(stderr, "error: %d %s\n", -rc, strerror(-rc));
if(logInformation) {
MessageManager::Log("[Input] " + deviceName + " error: " + std::to_string(errno) + " " + strerror(errno));
}
close(fd);
return nullptr;
}
@ -29,8 +37,11 @@ std::shared_ptr<LinuxGameController> LinuxGameController::GetController(int devi
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<LinuxGameController>(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;
}

View file

@ -22,7 +22,7 @@ private:
public:
~LinuxGameController();
static std::shared_ptr<LinuxGameController> GetController(int deviceID);
static std::shared_ptr<LinuxGameController> GetController(int deviceID, bool logInformation);
bool IsDisconnected();
int GetDeviceID();

View file

@ -256,7 +256,7 @@ LinuxKeyManager::LinuxKeyManager()
}
for(int i = 0; i < 30; i++) {
std::shared_ptr<LinuxGameController> controller = LinuxGameController::GetController(i);
std::shared_ptr<LinuxGameController> 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<LinuxGameController> controller = LinuxGameController::GetController(i);
std::shared_ptr<LinuxGameController> controller = LinuxGameController::GetController(i, false);
if(controller) {
controllersToAdd.push_back(controller);
}