Use shared_ptr for devices in DeviceMonitor
This commit is contained in:
		@@ -102,29 +102,28 @@ void DeviceMonitor::stopAndDeleteDevice (const std::string &path, HIDPP::DeviceI
 | 
				
			|||||||
void DeviceMonitor::addDevice(std::string path)
 | 
					void DeviceMonitor::addDevice(std::string path)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
        backend::hidpp::Device device(path, hidpp::DeviceIndex::WirelessDevice1);
 | 
					        auto device = std::make_shared<backend::hidpp::Device>(path, hidpp::DeviceIndex::WirelessDevice1);
 | 
				
			||||||
        log_printf(DEBUG, "Detected HID++ device at %s", path.c_str());
 | 
					        log_printf(DEBUG, "Detected HID++ device at %s", path.c_str());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        backend::hidpp::EventHandler eventHandler;
 | 
					        auto eventHandler = std::make_shared<backend::hidpp::EventHandler>();
 | 
				
			||||||
        eventHandler.condition = [device](backend::hidpp::Report& report)->bool
 | 
					        eventHandler->condition = [device](backend::hidpp::Report& report)->bool
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            return true;
 | 
					            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(),
 | 
					            log_printf(DEBUG, "Event on %s:%d", device->devicePath().c_str(),
 | 
				
			||||||
                    device.deviceIndex());
 | 
					                    device->deviceIndex());
 | 
				
			||||||
            for(auto& i : report.rawReport())
 | 
					            for(auto& i : report.rawReport())
 | 
				
			||||||
                    printf("%02x ", i);
 | 
					                    printf("%02x ", i);
 | 
				
			||||||
            printf("\n");
 | 
					            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 */
 | 
					        std::thread([device]() { device->listen(); }).detach();
 | 
				
			||||||
        while(true) {}
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    catch(backend::hidpp::Device::InvalidDevice &e)
 | 
					    catch(backend::hidpp::Device::InvalidDevice &e)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -37,8 +37,8 @@ namespace logid
 | 
				
			|||||||
        void removeDevice(std::string path) override;
 | 
					        void removeDevice(std::string path) override;
 | 
				
			||||||
    private:
 | 
					    private:
 | 
				
			||||||
    	std::mutex devices_mutex;
 | 
					    	std::mutex devices_mutex;
 | 
				
			||||||
        std::map<std::string, std::map<backend::hidpp::DeviceIndex, ConnectedDevice>> devices;
 | 
					    	std::vector<std::shared_ptr<backend::hidpp::Device>> devices; //tmp
 | 
				
			||||||
        backend::hidpp::EventHandler eventHandler;
 | 
					        //std::map<std::string, std::map<backend::hidpp::DeviceIndex, ConnectedDevice>> devices;
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    extern DeviceMonitor* finder;
 | 
					    extern DeviceMonitor* finder;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -45,7 +45,7 @@ Device::Device(const std::string& path, DeviceIndex index):
 | 
				
			|||||||
    raw_device->addEventHandler("DEV_" + std::to_string(index), rawEventHandler);
 | 
					    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<EventHandler>& handler)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    auto it = event_handlers.find(nickname);
 | 
					    auto it = event_handlers.find(nickname);
 | 
				
			||||||
    assert(it == event_handlers.end());
 | 
					    assert(it == event_handlers.end());
 | 
				
			||||||
@@ -61,8 +61,8 @@ void Device::removeEventHandler(const std::string& nickname)
 | 
				
			|||||||
void Device::handleEvent(Report& report)
 | 
					void Device::handleEvent(Report& report)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    for(auto& handler : event_handlers)
 | 
					    for(auto& handler : event_handlers)
 | 
				
			||||||
        if(handler.second.condition(report))
 | 
					        if(handler.second->condition(report))
 | 
				
			||||||
            handler.second.callback(report);
 | 
					            handler.second->callback(report);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Report Device::sendReport(Report& report)
 | 
					Report Device::sendReport(Report& report)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -44,7 +44,7 @@ namespace hidpp
 | 
				
			|||||||
        void listen(); // Runs asynchronously
 | 
					        void listen(); // Runs asynchronously
 | 
				
			||||||
        void stopListening();
 | 
					        void stopListening();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        void addEventHandler(const std::string& nickname, EventHandler& handler);
 | 
					        void addEventHandler(const std::string& nickname, const std::shared_ptr<EventHandler>& handler);
 | 
				
			||||||
        void removeEventHandler(const std::string& nickname);
 | 
					        void removeEventHandler(const std::string& nickname);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Report sendReport(Report& report);
 | 
					        Report sendReport(Report& report);
 | 
				
			||||||
@@ -56,7 +56,7 @@ namespace hidpp
 | 
				
			|||||||
        DeviceIndex index;
 | 
					        DeviceIndex index;
 | 
				
			||||||
        uint8_t supported_reports;
 | 
					        uint8_t supported_reports;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        std::map<std::string, EventHandler> event_handlers;
 | 
					        std::map<std::string, std::shared_ptr<EventHandler>> event_handlers;
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
} } }
 | 
					} } }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user