7#include "udevmanager.h"
9#include "../shared/rootdevice.h"
11#include "udevdevice.h"
17using namespace Solid::Backends::UDev;
18using namespace Solid::Backends::Shared;
20class UDevManager::Private
26 bool isOfInterest(
const QString &udi,
const UdevQt::Device &device);
27 bool checkOfInterest(
const UdevQt::Device &device);
29 UdevQt::Client *m_client;
34UDevManager::Private::Private()
37 QStringLiteral(
"processor"),
38 QStringLiteral(
"cpu"),
39 QStringLiteral(
"sound"),
40 QStringLiteral(
"tty"),
41 QStringLiteral(
"dvb"),
42 QStringLiteral(
"net"),
43 QStringLiteral(
"usb"),
44 QStringLiteral(
"input"),
46 m_client =
new UdevQt::Client(subsystems);
49UDevManager::Private::~Private()
54bool UDevManager::Private::isOfInterest(
const QString &udi,
const UdevQt::Device &device)
56 if (m_devicesOfInterest.contains(udi)) {
60 bool isOfInterest = checkOfInterest(device);
62 m_devicesOfInterest.append(udi);
68bool UDevManager::Private::checkOfInterest(
const UdevQt::Device &device)
70 const QString subsystem = device.subsystem();
71#ifdef UDEV_DETAILED_OUTPUT
72 qDebug() <<
"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
73 qDebug() <<
"Path:" << device.sysfsPath();
74 qDebug() <<
"Properties:" << device.deviceProperties();
77 for (
const QString &key : properties) {
78 qDebug() <<
"\t" << key <<
":" << device.deviceProperty(key).
toString();
80 qDebug() <<
"Driver:" << device.driver();
81 qDebug() <<
"Subsystem:" << subsystem;
82 qDebug() <<
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
88 return QFile::exists(device.sysfsPath() + QStringLiteral(
"/sysdev"))
89 ||
QFile::exists(device.sysfsPath() + QStringLiteral(
"/cpufreq"))
90 ||
QFile::exists(device.sysfsPath() + QStringLiteral(
"/topology/core_id"));
92 if (subsystem ==
QLatin1String(
"sound") && device.deviceProperty(QStringLiteral(
"SOUND_FORM_FACTOR")).
toString() != QStringLiteral(
"internal")) {
109 if (device.deviceProperty(QStringLiteral(
"ID_INPUT_MOUSE")).toInt() == 1
110 || device.deviceProperty(QStringLiteral(
"ID_INPUT_TOUCHPAD")).toInt() == 1
111 || device.deviceProperty(QStringLiteral(
"ID_INPUT_TABLET")).toInt() == 1
112 || device.deviceProperty(QStringLiteral(
"ID_INPUT_JOYSTICK")).toInt() == 1
113 || device.deviceProperty(QStringLiteral(
"ID_INPUT_TOUCHSCREEN")).toInt() == 1) {
121 || (!device.deviceProperty(QStringLiteral(
"ID_MEDIA_PLAYER")).
toString().isEmpty()
122 && device.parent().deviceProperty(QStringLiteral(
"ID_MEDIA_PLAYER")).
toString().isEmpty())
123 || (device.deviceProperty(QStringLiteral(
"ID_GPHOTO2")).toInt() == 1
124 && device.parent().deviceProperty(QStringLiteral(
"ID_GPHOTO2")).toInt() != 1);
132 connect(d->m_client, SIGNAL(deviceAdded(UdevQt::Device)),
this, SLOT(slotDeviceAdded(UdevQt::Device)));
133 connect(d->m_client, SIGNAL(deviceRemoved(UdevQt::Device)),
this, SLOT(slotDeviceRemoved(UdevQt::Device)));
136 d->m_supportedInterfaces << Solid::DeviceInterface::GenericInterface
137 << Solid::DeviceInterface::Processor
138 << Solid::DeviceInterface::Camera
139 << Solid::DeviceInterface::PortableMediaPlayer
140 << Solid::DeviceInterface::Block;
144UDevManager::~UDevManager()
149QString UDevManager::udiPrefix()
const
156 return d->m_supportedInterfaces;
163 for (
const UdevQt::Device &device : deviceList) {
164 if (d->isOfInterest(udiPrefix() + device.sysfsPath(), device)) {
165 res << udiPrefix() + device.sysfsPath();
177 for (
const UdevQt::Device &dev : deviceList) {
178 UDevDevice device(dev);
179 if (device.queryDeviceInterface(type) && d->isOfInterest(udiPrefix() + dev.sysfsPath(), dev) && device.parentUdi() == parentUdi) {
180 result << udiPrefix() + dev.sysfsPath();
186 if (type == DeviceInterface::Unknown) {
193 if (type == Solid::DeviceInterface::Processor) {
194 deviceList = (d->m_client->devicesBySubsystem(QStringLiteral(
"processor"))
195 + d->m_client->devicesBySubsystem(QStringLiteral(
"cpu")));
196 }
else if (type == Solid::DeviceInterface::Camera) {
197 deviceList = d->m_client->devicesBySubsystemsAndProperties({QStringLiteral(
"usb")},
198 {{QStringLiteral(
"ID_GPHOTO2"), QStringLiteral(
"*")}});
199 }
else if (type == Solid::DeviceInterface::PortableMediaPlayer) {
200 deviceList = d->m_client->devicesBySubsystemsAndProperties({QStringLiteral(
"usb")},
201 {{QStringLiteral(
"ID_MEDIA_PLAYER"), QStringLiteral(
"*")}});
203 deviceList = d->m_client->allDevices();
206 for (
const UdevQt::Device &dev : std::as_const(deviceList)) {
207 UDevDevice device(dev);
208 if (device.queryDeviceInterface(type)
209 && d->isOfInterest(udiPrefix() + dev.sysfsPath(), dev)) {
210 result << udiPrefix() + dev.sysfsPath();
219 if (udi_ == udiPrefix()) {
220 RootDevice *
const device =
new RootDevice(QStringLiteral(UDEV_UDI_PREFIX));
221 device->setProduct(
tr(
"Devices"));
222 device->setDescription(
tr(
"Devices declared in your system"));
223 device->setIcon(QStringLiteral(
"computer"));
229 UdevQt::Device device = d->m_client->deviceBySysfsPath(udi);
232 return new UDevDevice(device);
238void UDevManager::slotDeviceAdded(
const UdevQt::Device &device)
240 if (d->isOfInterest(udiPrefix() + device.sysfsPath(), device)) {
245void UDevManager::slotDeviceRemoved(
const UdevQt::Device &device)
247 if (d->isOfInterest(udiPrefix() + device.sysfsPath(), device)) {
249 d->m_devicesOfInterest.
removeAll(udiPrefix() + device.sysfsPath());
253#include "moc_udevmanager.cpp"
Type
This enum type defines the type of device interface that a Device can have.
void deviceAdded(const QString &udi)
This signal is emitted when a new device appears in the system.
void deviceRemoved(const QString &udi)
This signal is emitted when a device disappears from the system.
DeviceManager(QObject *parent=nullptr)
Constructs a DeviceManager.
char * toString(const EngineQuery &query)
QString path(const QString &relativePath)
bool startsWith(QByteArrayView bv) const const
bool exists() const const
qsizetype removeAll(const AT &t)
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
qsizetype lastIndexOf(QChar ch, Qt::CaseSensitivity cs) const const
qsizetype length() const const
QString right(qsizetype n) const const
qsizetype size() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QByteArray toLatin1() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const