Skip to content

Commit

Permalink
Fix up issues with event_device leaking file descriptors and keyboard…
Browse files Browse the repository at this point in the history
… detection
  • Loading branch information
Eeems committed Feb 2, 2024
1 parent 78e8f9a commit d0b07bf
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 24 deletions.
7 changes: 4 additions & 3 deletions applications/display-server/evdevdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
#include <QFileInfo>
#include <QThread>

EvDevDevice::EvDevDevice(QThread* handler, event_device device)
EvDevDevice::EvDevDevice(QThread* handler, const event_device& device)
: QObject(handler),
device(device),
sys("/sys/class/input/" + devName() + "/device/")
{
O_DEBUG(device.device.c_str() << this->device.fd);
_name = sys.strProperty("name").c_str();
notifier = new QSocketNotifier(device.fd, QSocketNotifier::Read, this);
notifier = new QSocketNotifier(this->device.fd, QSocketNotifier::Read, this);
connect(notifier, &QSocketNotifier::activated, this, &EvDevDevice::readEvents);
notifier->setEnabled(true);
libevdev_new_from_fd(device.fd, &dev);
libevdev_new_from_fd(this->device.fd, &dev);
}

EvDevDevice::~EvDevDevice(){
Expand Down
2 changes: 1 addition & 1 deletion applications/display-server/evdevdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class EvDevDevice : public QObject{
Q_OBJECT

public:
EvDevDevice(QThread* handler, event_device device);
EvDevDevice(QThread* handler, const event_device& device);
~EvDevDevice();
QString devName();
QString name();
Expand Down
2 changes: 1 addition & 1 deletion applications/display-server/evdevhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void EvDevHandler::reloadDevices(){
if(device.device == deviceSettings.getButtonsDevicePath()){
continue;
}
if(!hasDevice(device) && device.fd != -1){
if(!hasDevice(device) && device.fd > 0){
auto input = new EvDevDevice(this, device);
connect(input, &EvDevDevice::inputEvents, this, [this, input](auto events){
dbusInterface->inputEvents(input->number(), events);
Expand Down
22 changes: 11 additions & 11 deletions shared/liboxide/devicesettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include "liboxide.h"
#include "signalhandler.h"

#include <private/qdevicediscovery_p.h>
#include <private/qguiapplication_p.h>
#include <private/qinputdevicemanager_p.h>

#define BITS_PER_LONG (sizeof(long) * 8)
#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
Expand Down Expand Up @@ -80,6 +81,11 @@ namespace Oxide {
}else{
O_DEBUG(("Buttons input device: " + buttonsPath).c_str());
}
QObject::connect(signalHandler, &SignalHandler::sigCont, qApp, [this]{
for(auto& callback : callbacks){
callback();
}
});
}
DeviceSettings::~DeviceSettings(){}
bool DeviceSettings::checkBitSet(int fd, int type, int i) {
Expand Down Expand Up @@ -253,16 +259,10 @@ namespace Oxide {
}

void DeviceSettings::onInputDevicesChanged(std::function<void()> callback){
static auto deviceDiscovery = QDeviceDiscovery::create(QDeviceDiscovery::Device_InputMask, qApp);
QObject::connect(deviceDiscovery, &QDeviceDiscovery::deviceDetected, qApp, [callback](const QString& device){
Q_UNUSED(device);
callback();
});
QObject::connect(deviceDiscovery, &QDeviceDiscovery::deviceRemoved, qApp, [callback](const QString& device){
Q_UNUSED(device);
callback();
});
QObject::connect(signalHandler, &SignalHandler::sigCont, qApp, [callback]{
callbacks.push_back(callback);
auto manager = QGuiApplicationPrivate::inputDeviceManager();
QObject::connect(manager, &QInputDeviceManager::deviceListChanged, qApp, [callback](QInputDeviceManager::DeviceType type){
Q_UNUSED(type);
callback();
});
}
Expand Down
3 changes: 1 addition & 2 deletions shared/liboxide/devicesettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#pragma once
#include "liboxide_global.h"
#include "event_device.h"
#include <QFileSystemWatcher>

/*!
* \def deviceSettings()
Expand Down Expand Up @@ -156,7 +155,7 @@ namespace Oxide{
std::string buttonsPath = "";
std::string wacomPath = "";
std::string touchPath = "";
QFileSystemWatcher* watcher = nullptr;
std::vector<std::function<void()>> callbacks;
};
}
/*! @} */
12 changes: 8 additions & 4 deletions shared/liboxide/event_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ namespace Oxide {
event_device::event_device(const string& path, int flags) : device(path), flags(flags){
this->open();
}

event_device::event_device(const event_device& other)
: device(other.device),
flags(other.flags),
fd(::dup(other.fd))
{}
event_device::~event_device(){
this->close();
}

void event_device::open(){
if(fd > 0){
this->close();
}
this->close();
fd = ::open(device.c_str(), flags);
error = fd < 0 ? errno : 0;
}

void event_device::close(){
if(fd < 0){
if(fd > 0){
::close(fd);
}
fd = 0;
Expand Down
1 change: 1 addition & 0 deletions shared/liboxide/event_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace Oxide {
* \param flags
*/
event_device(const string& path, int flags);
event_device(const event_device& other);
~event_device();
/*!
* \brief close
Expand Down
1 change: 0 additions & 1 deletion shared/liboxide/liboxide.pro
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ QT += quick
QT += dbus
QT += core_private
QT += gui-private
QT += input_support-private

TEMPLATE = lib
DEFINES += LIBOXIDE_LIBRARY
Expand Down
1 change: 1 addition & 0 deletions shared/liboxide/signalhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace Oxide {
static SignalHandler* instance;
if(self != nullptr){
instance = self;
setup_unix_signal_handlers();
}
return instance;
}
Expand Down
2 changes: 1 addition & 1 deletion shared/liboxide/signalhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace Oxide {
*
* Manually constructing this class will attempt to register it as the singleton instance.
*/
SignalHandler(QObject *parent = 0);
SignalHandler(QObject* parent = 0);
~SignalHandler();
static void usr1SignalHandler(int unused);
static void usr2SignalHandler(int unused);
Expand Down

0 comments on commit d0b07bf

Please sign in to comment.