diff --git a/udev.cpp b/udev.cpp index 2832d69a14..f7257cad40 100644 --- a/udev.cpp +++ b/udev.cpp @@ -153,6 +153,16 @@ UdevDevice::Ptr Udev::deviceFromSyspath(const char *syspath) return std::move(UdevDevice::Ptr(new UdevDevice(udev_device_new_from_syspath(m_udev, syspath)))); } +UdevMonitor *Udev::monitor() +{ + UdevMonitor *m = new UdevMonitor(this); + if (!m->isValid()) { + delete m; + m = nullptr; + } + return m; +} + UdevDevice::UdevDevice(udev_device *device) : m_device(device) { @@ -206,4 +216,49 @@ bool UdevDevice::hasProperty(const char *key, const char *value) return qstrcmp(p, value) == 0; } +UdevMonitor::UdevMonitor(Udev *udev) + : m_udev(udev) + , m_monitor(udev_monitor_new_from_netlink(*udev, "udev")) +{ +} + +UdevMonitor::~UdevMonitor() +{ + if (m_monitor) { + udev_monitor_unref(m_monitor); + } +} + +int UdevMonitor::fd() const +{ + if (m_monitor) { + return udev_monitor_get_fd(m_monitor); + } + return -1; +} + +void UdevMonitor::filterSubsystemDevType(const char *subSystem, const char *devType) +{ + if (!m_monitor) { + return; + } + udev_monitor_filter_add_match_subsystem_devtype(m_monitor, subSystem, devType); +} + +void UdevMonitor::enable() +{ + if (!m_monitor) { + return; + } + udev_monitor_enable_receiving(m_monitor); +} + +UdevDevice::Ptr UdevMonitor::getDevice() +{ + if (!m_monitor) { + return std::move(UdevDevice::Ptr()); + } + return std::move(UdevDevice::Ptr(new UdevDevice(udev_monitor_receive_device(m_monitor)))); +} + } diff --git a/udev.h b/udev.h index bf165c7de1..e6bc80307c 100644 --- a/udev.h +++ b/udev.h @@ -23,10 +23,11 @@ along with this program. If not, see . struct udev; struct udev_device; +struct udev_monitor; namespace KWin { - +class Udev; class UdevDevice { @@ -52,6 +53,25 @@ private: udev_device *m_device; }; +class UdevMonitor +{ +public: + explicit UdevMonitor(Udev *udev); + ~UdevMonitor(); + + int fd() const; + bool isValid() const { + return m_monitor != nullptr; + } + void filterSubsystemDevType(const char *subSystem, const char *devType = nullptr); + void enable(); + UdevDevice::Ptr getDevice(); + +private: + Udev *m_udev; + udev_monitor *m_monitor; +}; + class Udev { public: @@ -63,6 +83,7 @@ public: } UdevDevice::Ptr primaryGpu(); UdevDevice::Ptr deviceFromSyspath(const char *syspath); + UdevMonitor *monitor(); operator udev*() const { return m_udev; }