7#include "udevqtclient.h"
10#include "devices_debug.h"
12#include <QSocketNotifier>
13#include <qplatformdefs.h>
17ClientPrivate::ClientPrivate(Client *q_)
21 , monitorNotifier(nullptr)
25ClientPrivate::~ClientPrivate()
28 delete monitorNotifier;
31 udev_monitor_unref(monitor);
35void ClientPrivate::init(
const QStringList &subsystemList, ListenToWhat what)
39 if (what != ListenToNone) {
40 setWatchedSubsystems(subsystemList);
44void ClientPrivate::setWatchedSubsystems(
const QStringList &subsystemList)
47 struct udev_monitor *newM = udev_monitor_new_from_netlink(udev,
"udev");
50 qWarning(
"UdevQt: unable to create udev monitor connection");
55 for (
const QString &subsysDevtype : subsystemList) {
61 udev_monitor_filter_add_match_subsystem_devtype(newM, subsystem.
constData(), devType.
constData());
63 udev_monitor_filter_add_match_subsystem_devtype(newM, subsysDevtype.toLatin1().constData(),
nullptr);
68 udev_monitor_enable_receiving(newM);
75 delete monitorNotifier;
77 udev_monitor_unref(monitor);
83 watchedSubsystems = subsystemList;
86void ClientPrivate::dispatchEvent()
89 struct udev_device *dev = udev_monitor_receive_device(monitor);
90 monitorNotifier->setEnabled(
true);
96 Device device(
new DevicePrivate(dev,
false));
98 QByteArray action(udev_device_get_action(dev));
99 if (action ==
"add") {
100 Q_EMIT q->deviceAdded(device);
101 }
else if (action ==
"remove") {
102 Q_EMIT q->deviceRemoved(device);
103 }
else if (action ==
"change") {
104 Q_EMIT q->deviceChanged(device);
105 }
else if (action ==
"online") {
106 Q_EMIT q->deviceOnlined(device);
107 }
else if (action ==
"offline") {
108 Q_EMIT q->deviceOfflined(device);
109 }
else if (action ==
"bind") {
110 Q_EMIT q->deviceBound(device);
111 }
else if (action ==
"unbind") {
112 Q_EMIT q->deviceUnbound(device);
114 qCDebug(Solid::Frontend::DeviceManager::DEVICEMANAGER) <<
"UdevQt: unhandled action:" << action.constData() <<
"for device:" << device.sysfsPath();
118DeviceList ClientPrivate::deviceListFromEnumerate(
struct udev_enumerate *en)
121 struct udev_list_entry *
list;
122 struct udev_list_entry *entry;
124 udev_enumerate_scan_devices(en);
125 list = udev_enumerate_get_list_entry(en);
126 udev_list_entry_foreach(entry, list)
128 struct udev_device *ud = udev_device_new_from_syspath(udev_enumerate_get_udev(en), udev_list_entry_get_name(entry));
134 ret << Device(
new DevicePrivate(ud,
false));
137 udev_enumerate_unref(en);
144 , d(new ClientPrivate(this))
146 d->init(
QStringList(), ClientPrivate::ListenToNone);
151 , d(new ClientPrivate(this))
153 d->init(subsystemList, ClientPrivate::ListenToList);
164 if (!d->watchedSubsystems.isEmpty()) {
165 return d->watchedSubsystems;
175 struct udev_enumerate *en = udev_enumerate_new(d->udev);
176 udev_enumerate_scan_subsystems(en);
177 QStringList s = listFromListEntry(udev_enumerate_get_list_entry(en));
178 udev_enumerate_unref(en);
182void Client::setWatchedSubsystems(
const QStringList &subsystemList)
184 d->setWatchedSubsystems(subsystemList);
187DeviceList Client::devicesByProperty(
const QString &property,
const QVariant &value)
189 struct udev_enumerate *en = udev_enumerate_new(d->udev);
197 return d->deviceListFromEnumerate(en);
200DeviceList Client::allDevices()
202 struct udev_enumerate *en = udev_enumerate_new(d->udev);
203 return d->deviceListFromEnumerate(en);
206DeviceList Client::devicesBySubsystem(
const QString &subsystem)
208 struct udev_enumerate *en = udev_enumerate_new(d->udev);
211 return d->deviceListFromEnumerate(en);
214DeviceList Client::devicesBySubsystemsAndProperties(
const QStringList &subsystems,
const QVariantMap &properties)
216 struct udev_enumerate *en = udev_enumerate_new(d->udev);
218 for (
const QString &subsystem : subsystems) {
223 if (it.value().isValid()) {
224 udev_enumerate_add_match_property(en, it.key().toLatin1().constData(), it.value().toString().toLatin1().constData());
226 udev_enumerate_add_match_property(en, it.key().toLatin1().constData(),
nullptr);
230 return d->deviceListFromEnumerate(en);
233Device Client::deviceByDeviceFile(
const QString &deviceFile)
241 struct udev_device *ud =
nullptr;
243 if (S_ISBLK(sb.st_mode)) {
244 ud = udev_device_new_from_devnum(d->udev,
'b', sb.st_rdev);
245 }
else if (S_ISCHR(sb.st_mode)) {
246 ud = udev_device_new_from_devnum(d->udev,
'c', sb.st_rdev);
253 return Device(
new DevicePrivate(ud,
false));
256Device Client::deviceBySysfsPath(
const QString &sysfsPath)
258 struct udev_device *ud = udev_device_new_from_syspath(d->udev, sysfsPath.
toLatin1().
constData());
264 return Device(
new DevicePrivate(ud,
false));
267Device Client::deviceBySubsystemAndName(
const QString &subsystem,
const QString &name)
269 struct udev_device *ud = udev_device_new_from_subsystem_sysname(d->udev,
277 return Device(
new DevicePrivate(ud,
false));
282#include "moc_udevqtclient.cpp"
QString name(GameStandardAction id)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
const char * constData() const const
QByteArray left(qsizetype len) const const
QByteArray mid(qsizetype pos, qsizetype len) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void activated(QSocketDescriptor socket, QSocketNotifier::Type type)
void setEnabled(bool enable)
QByteArray toLatin1() const const
bool isValid() const const
QString toString() const const