diff --git a/src/logid/backend/dj/Receiver.cpp b/src/logid/backend/dj/Receiver.cpp index b3d183a..e32a1dd 100644 --- a/src/logid/backend/dj/Receiver.cpp +++ b/src/logid/backend/dj/Receiver.cpp @@ -20,6 +20,7 @@ #include "Report.h" #include "Receiver.h" #include "Error.h" +#include "../../util/thread.h" using namespace logid::backend::dj; using namespace logid::backend; @@ -214,13 +215,14 @@ std::string Receiver::getDeviceName(hidpp::DeviceIndex index) return name; } -hidpp::DeviceIndex Receiver::deviceDisconnectionEvent(hidpp::Report& report) +hidpp::DeviceIndex Receiver::deviceDisconnectionEvent(const hidpp::Report& +report) { assert(report.subId() == DeviceDisconnection); return report.deviceIndex(); } -hidpp::DeviceConnectionEvent Receiver::deviceConnectionEvent( +hidpp::DeviceConnectionEvent Receiver::deviceConnectionEvent(const hidpp::Report &report) { assert(report.subId() == DeviceConnection); @@ -322,7 +324,10 @@ Receiver::ConnectionStatusEvent Receiver::connectionStatusEvent(Report& report) void Receiver::listen() { if(!_raw_device->isListening()) - std::thread{[this]() { this->_raw_device->listen(); }}.detach(); + ///TODO: Kill RawDevice? + thread::spawn({[raw=this->_raw_device]() { + raw->listen(); + }}); if(_raw_device->eventHandlers().find("RECV_HIDPP") == _raw_device->eventHandlers().end()) { diff --git a/src/logid/backend/dj/Receiver.h b/src/logid/backend/dj/Receiver.h index 97aca4a..86430b2 100644 --- a/src/logid/backend/dj/Receiver.h +++ b/src/logid/backend/dj/Receiver.h @@ -140,9 +140,9 @@ namespace dj std::string getDeviceName(hidpp::DeviceIndex index); static hidpp::DeviceIndex deviceDisconnectionEvent( - hidpp::Report& report); + const hidpp::Report& report); static hidpp::DeviceConnectionEvent deviceConnectionEvent( - hidpp::Report& report); + const hidpp::Report& report); void listen(); void stopListening(); diff --git a/src/logid/backend/dj/ReceiverMonitor.cpp b/src/logid/backend/dj/ReceiverMonitor.cpp index 6447f75..4199178 100644 --- a/src/logid/backend/dj/ReceiverMonitor.cpp +++ b/src/logid/backend/dj/ReceiverMonitor.cpp @@ -17,6 +17,8 @@ */ #include "ReceiverMonitor.h" +#include "../../util/thread.h" +#include "../../util.h" #include #include @@ -55,14 +57,24 @@ void ReceiverMonitor::run() /* Running in a new thread prevents deadlocks since the * receiver may be enumerating. */ - std::thread{[this](hidpp::Report report) { + thread::spawn({[this, report]() { if (report.subId() == Receiver::DeviceConnection) - this->addDevice(this->_receiver->deviceConnectionEvent( - report)); + this->addDevice(this->_receiver->deviceConnectionEvent + (report)); else if (report.subId() == Receiver::DeviceDisconnection) this->removeDevice(this->_receiver-> deviceDisconnectionEvent(report)); - }, report}.detach(); + }}, {[report, path=this->_receiver->rawDevice()->hidrawPath()] + (std::exception& e) { + if(report.subId() == Receiver::DeviceConnection) + log_printf(ERROR, "Failed to add device %d to receiver " + "on %s: %s", report.deviceIndex(), + path.c_str(), e.what()); + else if(report.subId() == Receiver::DeviceDisconnection) + log_printf(ERROR, "Failed to remove device %d from " + "receiver on %s: %s", report.deviceIndex() + ,path.c_str(), e.what()); + }}); }; _receiver->addHidppEventHandler("RECVMON", event_handler); diff --git a/src/logid/backend/hidpp/Device.cpp b/src/logid/backend/hidpp/Device.cpp index 029dda4..110f134 100644 --- a/src/logid/backend/hidpp/Device.cpp +++ b/src/logid/backend/hidpp/Device.cpp @@ -18,6 +18,7 @@ #include #include +#include "../../util/thread.h" #include "Device.h" #include "Report.h" #include "../hidpp20/features/Root.h" @@ -182,7 +183,10 @@ Report Device::sendReport(Report& report) void Device::listen() { if(!_raw_device->isListening()) - std::thread{[=]() { _raw_device->listen(); }}.detach(); + ///TODO: Kill RawDevice? + thread::spawn({[raw=this->_raw_device]() { + raw->listen(); + }}); // Pass all HID++ events with device index to this device. std::shared_ptr handler; diff --git a/src/logid/backend/hidpp/Report.cpp b/src/logid/backend/hidpp/Report.cpp index c1e20df..8cc5c9b 100644 --- a/src/logid/backend/hidpp/Report.cpp +++ b/src/logid/backend/hidpp/Report.cpp @@ -199,7 +199,7 @@ void Report::setType(Report::Type type) _data[Offset::Type] = type; } -hidpp::DeviceIndex Report::deviceIndex() +hidpp::DeviceIndex Report::deviceIndex() const { return static_cast(_data[Offset::DeviceIndex]); } @@ -261,6 +261,26 @@ void Report::setAddress(uint8_t address) _data[Offset::Address] = address; } +std::vector::iterator Report::paramBegin() +{ + return _data.begin() + Offset::Parameters; +} + +std::vector::iterator Report::paramEnd() +{ + return _data.end(); +} + +std::vector::const_iterator Report::paramBegin() const +{ + return _data.begin() + Offset::Parameters; +} + +std::vector::const_iterator Report::paramEnd() const +{ + return _data.end(); +} + void Report::setParams(const std::vector& _params) { assert(_params.size() <= _data.size()-HeaderLength); diff --git a/src/logid/backend/hidpp/Report.h b/src/logid/backend/hidpp/Report.h index 7e2c814..e406a6d 100644 --- a/src/logid/backend/hidpp/Report.h +++ b/src/logid/backend/hidpp/Report.h @@ -78,7 +78,7 @@ namespace hidpp Report::Type type() const; void setType(Report::Type type); - logid::backend::hidpp::DeviceIndex deviceIndex(); + logid::backend::hidpp::DeviceIndex deviceIndex() const; void setDeviceIndex(hidpp::DeviceIndex index); uint8_t feature() const; @@ -96,11 +96,10 @@ namespace hidpp uint8_t address() const; void setAddress(uint8_t address); - std::vector::iterator paramBegin() - { - return _data.begin() + Offset::Parameters; - } - std::vector::iterator paramEnd() { return _data.end(); } + std::vector::iterator paramBegin(); + std::vector::iterator paramEnd(); + std::vector::const_iterator paramBegin() const; + std::vector::const_iterator paramEnd() const; void setParams(const std::vector& _params); struct Hidpp10Error diff --git a/src/logid/backend/raw/DeviceMonitor.cpp b/src/logid/backend/raw/DeviceMonitor.cpp index 8d4beda..133b471 100644 --- a/src/logid/backend/raw/DeviceMonitor.cpp +++ b/src/logid/backend/raw/DeviceMonitor.cpp @@ -17,6 +17,8 @@ */ #include "DeviceMonitor.h" +#include "../../util/thread.h" +#include "../../util.h" #include #include @@ -96,13 +98,19 @@ void DeviceMonitor::run() std::string devnode = udev_device_get_devnode(device); if (action == "add") - std::thread([this](const std::string name) { + thread::spawn([this, name=devnode]() { this->addDevice(name); - }, devnode).detach(); + }, [name=devnode](std::exception& e){ + log_printf(WARN, "Error adding device %s: %s", + name.c_str(), e.what()); + }); else if (action == "remove") - std::thread([this](const std::string name) { + thread::spawn([this, name=devnode]() { this->removeDevice(name); - }, devnode).detach(); + }, [name=devnode](std::exception& e){ + log_printf(WARN, "Error removing device %s: %s", + name.c_str(), e.what()); + }); udev_device_unref (device); } @@ -149,9 +157,12 @@ void DeviceMonitor::enumerate() std::string devnode = udev_device_get_devnode(device); udev_device_unref(device); - std::thread([this](const std::string& name) { + thread::spawn([this, name=devnode]() { this->addDevice(name); - }, devnode).detach(); + }, [name=devnode](std::exception& e){ + log_printf(ERROR, "Error adding device %s: %s", + name.c_str(), e.what()); + }); } udev_enumerate_unref(udev_enum);