diff --git a/src/logid/DeviceMonitor.cpp b/src/logid/DeviceMonitor.cpp index e69bda4..3027e6d 100644 --- a/src/logid/DeviceMonitor.cpp +++ b/src/logid/DeviceMonitor.cpp @@ -102,29 +102,28 @@ void DeviceMonitor::stopAndDeleteDevice (const std::string &path, HIDPP::DeviceI void DeviceMonitor::addDevice(std::string path) { try { - backend::hidpp::Device device(path, hidpp::DeviceIndex::WirelessDevice1); + auto device = std::make_shared(path, hidpp::DeviceIndex::WirelessDevice1); log_printf(DEBUG, "Detected HID++ device at %s", path.c_str()); - backend::hidpp::EventHandler eventHandler; - eventHandler.condition = [device](backend::hidpp::Report& report)->bool + auto eventHandler = std::make_shared(); + eventHandler->condition = [device](backend::hidpp::Report& report)->bool { return true; }; - eventHandler.callback = [device](backend::hidpp::Report& report)->void + eventHandler->callback = [device](backend::hidpp::Report& report)->void { - log_printf(DEBUG, "Event on %s:%d", device.devicePath().c_str(), - device.deviceIndex()); + log_printf(DEBUG, "Event on %s:%d", device->devicePath().c_str(), + device->deviceIndex()); for(auto& i : report.rawReport()) printf("%02x ", i); printf("\n"); }; - device.addEventHandler("MONITOR_ALL", eventHandler); + device->addEventHandler("MONITOR_ALL", eventHandler); - std::thread([](backend::hidpp::Device device) { device.listen(); }, device).detach(); + devices.push_back(device); - /* This is a temporary solution to avoid std::bad_function_call */ - while(true) {} + std::thread([device]() { device->listen(); }).detach(); } catch(backend::hidpp::Device::InvalidDevice &e) { diff --git a/src/logid/DeviceMonitor.h b/src/logid/DeviceMonitor.h index 159970e..f95f642 100644 --- a/src/logid/DeviceMonitor.h +++ b/src/logid/DeviceMonitor.h @@ -37,8 +37,8 @@ namespace logid void removeDevice(std::string path) override; private: std::mutex devices_mutex; - std::map> devices; - backend::hidpp::EventHandler eventHandler; + std::vector> devices; //tmp + //std::map> devices; }; extern DeviceMonitor* finder; diff --git a/src/logid/backend/hidpp/Device.cpp b/src/logid/backend/hidpp/Device.cpp index 38a2764..680ff91 100644 --- a/src/logid/backend/hidpp/Device.cpp +++ b/src/logid/backend/hidpp/Device.cpp @@ -45,7 +45,7 @@ Device::Device(const std::string& path, DeviceIndex index): raw_device->addEventHandler("DEV_" + std::to_string(index), rawEventHandler); } -void Device::addEventHandler(const std::string& nickname, EventHandler& handler) +void Device::addEventHandler(const std::string& nickname, const std::shared_ptr& handler) { auto it = event_handlers.find(nickname); assert(it == event_handlers.end()); @@ -61,8 +61,8 @@ void Device::removeEventHandler(const std::string& nickname) void Device::handleEvent(Report& report) { for(auto& handler : event_handlers) - if(handler.second.condition(report)) - handler.second.callback(report); + if(handler.second->condition(report)) + handler.second->callback(report); } Report Device::sendReport(Report& report) diff --git a/src/logid/backend/hidpp/Device.h b/src/logid/backend/hidpp/Device.h index fcaf732..8d5aa95 100644 --- a/src/logid/backend/hidpp/Device.h +++ b/src/logid/backend/hidpp/Device.h @@ -44,7 +44,7 @@ namespace hidpp void listen(); // Runs asynchronously void stopListening(); - void addEventHandler(const std::string& nickname, EventHandler& handler); + void addEventHandler(const std::string& nickname, const std::shared_ptr& handler); void removeEventHandler(const std::string& nickname); Report sendReport(Report& report); @@ -56,7 +56,7 @@ namespace hidpp DeviceIndex index; uint8_t supported_reports; - std::map event_handlers; + std::map> event_handlers; }; } } } diff --git a/src/logid/backend/raw/RawDevice.cpp b/src/logid/backend/raw/RawDevice.cpp index e334d5b..473ad25 100644 --- a/src/logid/backend/raw/RawDevice.cpp +++ b/src/logid/backend/raw/RawDevice.cpp @@ -194,7 +194,7 @@ void RawDevice::stopListener() interruptRead(); } -void RawDevice::addEventHandler(const std::string &nickname, RawEventHandler &handler) +void RawDevice::addEventHandler(const std::string& nickname, RawEventHandler& handler) { auto it = event_handlers.find(nickname); assert(it == event_handlers.end());