[udev] Add wrapper for udev_monitor

This commit is contained in:
Martin Gräßlin 2015-04-23 15:08:24 +02:00
parent cf27a056b8
commit d9a29f3a49
2 changed files with 77 additions and 1 deletions

View file

@ -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))));
}
}

23
udev.h
View file

@ -23,10 +23,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
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;
}