Use shared_ptr for devices in DeviceMonitor
This commit is contained in:
parent
6b895b3015
commit
91954e8a73
|
@ -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<backend::hidpp::Device>(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<backend::hidpp::EventHandler>();
|
||||
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)
|
||||
{
|
||||
|
|
|
@ -37,8 +37,8 @@ namespace logid
|
|||
void removeDevice(std::string path) override;
|
||||
private:
|
||||
std::mutex devices_mutex;
|
||||
std::map<std::string, std::map<backend::hidpp::DeviceIndex, ConnectedDevice>> devices;
|
||||
backend::hidpp::EventHandler eventHandler;
|
||||
std::vector<std::shared_ptr<backend::hidpp::Device>> devices; //tmp
|
||||
//std::map<std::string, std::map<backend::hidpp::DeviceIndex, ConnectedDevice>> devices;
|
||||
};
|
||||
|
||||
extern DeviceMonitor* finder;
|
||||
|
|
|
@ -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<EventHandler>& 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)
|
||||
|
|
|
@ -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<EventHandler>& 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<std::string, EventHandler> event_handlers;
|
||||
std::map<std::string, std::shared_ptr<EventHandler>> event_handlers;
|
||||
};
|
||||
} } }
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in New Issue
Block a user