Use consistent code style
Only files that are currently used in logid are changed.
This commit is contained in:
@@ -13,40 +13,39 @@ using namespace logid::backend::raw;
|
||||
|
||||
DeviceMonitor::DeviceMonitor()
|
||||
{
|
||||
if(-1 == pipe(monitor_pipe))
|
||||
throw std::system_error(errno, std::system_category(), "pipe creation failed");
|
||||
if(-1 == pipe(_pipe))
|
||||
throw std::system_error(errno, std::system_category(),
|
||||
"pipe creation failed");
|
||||
|
||||
udev_context = udev_new();
|
||||
if(!udev_context)
|
||||
_udev_context = udev_new();
|
||||
if(!_udev_context)
|
||||
throw std::runtime_error("udev_new failed");
|
||||
}
|
||||
|
||||
DeviceMonitor::~DeviceMonitor()
|
||||
{
|
||||
bool is_running = running.try_lock();
|
||||
if(is_running)
|
||||
running.unlock();
|
||||
else
|
||||
this->stop();
|
||||
this->stop();
|
||||
|
||||
udev_unref(udev_context);
|
||||
udev_unref(_udev_context);
|
||||
|
||||
for(int i : monitor_pipe)
|
||||
for(int i : _pipe)
|
||||
close(i);
|
||||
}
|
||||
|
||||
void DeviceMonitor::run()
|
||||
{
|
||||
int ret;
|
||||
const std::lock_guard<std::mutex> run_lock(running);
|
||||
std::lock_guard<std::mutex> lock(_running);
|
||||
|
||||
struct udev_monitor* monitor = udev_monitor_new_from_netlink(udev_context, "udev");
|
||||
struct udev_monitor* monitor = udev_monitor_new_from_netlink(_udev_context,
|
||||
"udev");
|
||||
if(!monitor)
|
||||
throw std::runtime_error("udev_monitor_new_from_netlink failed");
|
||||
|
||||
ret = udev_monitor_filter_add_match_subsystem_devtype(monitor, "hidraw", nullptr);
|
||||
ret = udev_monitor_filter_add_match_subsystem_devtype(monitor, "hidraw",
|
||||
nullptr);
|
||||
if (0 != ret)
|
||||
throw std::system_error (-ret, std::system_category (),
|
||||
throw std::system_error (-ret, std::system_category(),
|
||||
"udev_monitor_filter_add_match_subsystem_devtype");
|
||||
|
||||
ret = udev_monitor_enable_receiving(monitor);
|
||||
@@ -57,20 +56,27 @@ void DeviceMonitor::run()
|
||||
this->enumerate();
|
||||
|
||||
int fd = udev_monitor_get_fd(monitor);
|
||||
while (true) {
|
||||
|
||||
_run_monitor = true;
|
||||
while (_run_monitor) {
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(monitor_pipe[0], &fds);
|
||||
FD_SET(_pipe[0], &fds);
|
||||
FD_SET(fd, &fds);
|
||||
if (-1 == select (std::max (monitor_pipe[0], fd)+1, &fds, nullptr, nullptr, nullptr)) {
|
||||
|
||||
if (-1 == select (std::max (_pipe[0], fd)+1, &fds, nullptr,
|
||||
nullptr, nullptr)) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
throw std::system_error (errno, std::system_category(), "udev_monitor select");
|
||||
throw std::system_error (errno, std::system_category(),
|
||||
"udev_monitor select");
|
||||
}
|
||||
|
||||
if (FD_ISSET(fd, &fds)) {
|
||||
struct udev_device *device = udev_monitor_receive_device(monitor);
|
||||
std::string action = udev_device_get_action(device);
|
||||
std::string devnode = udev_device_get_devnode(device);
|
||||
|
||||
if (action == "add")
|
||||
std::thread([this](const std::string name) {
|
||||
this->addDevice(name);
|
||||
@@ -79,12 +85,13 @@ void DeviceMonitor::run()
|
||||
std::thread([this](const std::string name) {
|
||||
this->removeDevice(name);
|
||||
}, devnode).detach();
|
||||
|
||||
udev_device_unref (device);
|
||||
}
|
||||
if (FD_ISSET(monitor_pipe[0], &fds)) {
|
||||
if (FD_ISSET(_pipe[0], &fds)) {
|
||||
char c;
|
||||
if (-1 == read(monitor_pipe[0], &c, sizeof (char)))
|
||||
throw std::system_error (errno, std::system_category (),
|
||||
if (-1 == read(_pipe[0], &c, sizeof (char)))
|
||||
throw std::system_error (errno, std::system_category(),
|
||||
"read pipe");
|
||||
break;
|
||||
}
|
||||
@@ -93,13 +100,14 @@ void DeviceMonitor::run()
|
||||
|
||||
void DeviceMonitor::stop()
|
||||
{
|
||||
|
||||
_run_monitor = false;
|
||||
std::lock_guard<std::mutex> lock(_running);
|
||||
}
|
||||
|
||||
void DeviceMonitor::enumerate()
|
||||
{
|
||||
int ret;
|
||||
struct udev_enumerate* udev_enum = udev_enumerate_new(udev_context);
|
||||
struct udev_enumerate* udev_enum = udev_enumerate_new(_udev_context);
|
||||
ret = udev_enumerate_add_match_subsystem(udev_enum, "hidraw");
|
||||
if(0 != ret)
|
||||
throw std::system_error(-ret, std::system_category(),
|
||||
@@ -112,11 +120,10 @@ void DeviceMonitor::enumerate()
|
||||
|
||||
struct udev_list_entry* udev_enum_entry;
|
||||
udev_list_entry_foreach(udev_enum_entry,
|
||||
udev_enumerate_get_list_entry(udev_enum))
|
||||
{
|
||||
udev_enumerate_get_list_entry(udev_enum)) {
|
||||
const char* name = udev_list_entry_get_name(udev_enum_entry);
|
||||
|
||||
struct udev_device* device = udev_device_new_from_syspath(udev_context,
|
||||
struct udev_device* device = udev_device_new_from_syspath(_udev_context,
|
||||
name);
|
||||
if(!device)
|
||||
throw std::runtime_error("udev_device_new_from_syspath failed");
|
||||
|
Reference in New Issue
Block a user