mirror of
https://bitbucket.org/smil3y/kdelibs.git
synced 2025-02-23 18:32:49 +00:00
solid: merge private UDev wrapper classes into their parent class
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
4a5ca906b1
commit
ef87483d3b
4 changed files with 165 additions and 258 deletions
|
@ -26,68 +26,83 @@
|
|||
#include <QtCore/QString>
|
||||
#include <QtCore/QStringList>
|
||||
#include <QtCore/QVariant>
|
||||
#include <QtCore/QSocketNotifier>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
|
||||
#include <libudev.h>
|
||||
}
|
||||
|
||||
namespace UdevQt
|
||||
{
|
||||
|
||||
class DevicePrivate;
|
||||
class Device
|
||||
{
|
||||
public:
|
||||
Device();
|
||||
Device(const Device &other);
|
||||
~Device();
|
||||
Device &operator= (const Device &other);
|
||||
public:
|
||||
Device();
|
||||
Device(struct udev_device *udev_, bool ref = true);
|
||||
Device(const Device &other);
|
||||
~Device();
|
||||
Device &operator= (const Device &other);
|
||||
|
||||
bool isValid() const;
|
||||
QString subsystem() const;
|
||||
QString devType() const;
|
||||
QString name() const;
|
||||
QString sysfsPath() const;
|
||||
int sysfsNumber() const;
|
||||
QString driver() const;
|
||||
QStringList alternateDeviceSymlinks() const;
|
||||
QStringList deviceProperties() const;
|
||||
Device parent() const;
|
||||
bool isValid() const;
|
||||
QString subsystem() const;
|
||||
QString devType() const;
|
||||
QString name() const;
|
||||
QString sysfsPath() const;
|
||||
int sysfsNumber() const;
|
||||
QString driver() const;
|
||||
QStringList alternateDeviceSymlinks() const;
|
||||
QStringList deviceProperties() const;
|
||||
Device parent() const;
|
||||
|
||||
QString deviceProperty(const QString &name) const;
|
||||
QString sysfsProperty(const QString &name) const;
|
||||
QString deviceProperty(const QString &name) const;
|
||||
QString sysfsProperty(const QString &name) const;
|
||||
|
||||
private:
|
||||
Device(DevicePrivate *devPrivate);
|
||||
friend class Client;
|
||||
friend class ClientPrivate;
|
||||
|
||||
DevicePrivate *d;
|
||||
private:
|
||||
struct udev_device *m_udev;
|
||||
};
|
||||
|
||||
typedef QList<Device> DeviceList;
|
||||
|
||||
class ClientPrivate;
|
||||
class Client : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Client(const QStringList &subsystemList, QObject *parent = 0);
|
||||
~Client();
|
||||
|
||||
public:
|
||||
Client(const QStringList &subsystemList, QObject *parent = 0);
|
||||
~Client();
|
||||
DeviceList allDevices();
|
||||
Device deviceBySysfsPath(const QString &sysfsPath);
|
||||
|
||||
DeviceList allDevices();
|
||||
Device deviceBySysfsPath(const QString &sysfsPath);
|
||||
signals:
|
||||
void deviceAdded(const UdevQt::Device &dev);
|
||||
void deviceRemoved(const UdevQt::Device &dev);
|
||||
void deviceChanged(const UdevQt::Device &dev);
|
||||
void deviceOnlined(const UdevQt::Device &dev);
|
||||
void deviceOfflined(const UdevQt::Device &dev);
|
||||
|
||||
signals:
|
||||
void deviceAdded(const UdevQt::Device &dev);
|
||||
void deviceRemoved(const UdevQt::Device &dev);
|
||||
void deviceChanged(const UdevQt::Device &dev);
|
||||
void deviceOnlined(const UdevQt::Device &dev);
|
||||
void deviceOfflined(const UdevQt::Device &dev);
|
||||
private slots:
|
||||
void monitorReadyRead(int fd);
|
||||
|
||||
private:
|
||||
friend class ClientPrivate;
|
||||
Q_PRIVATE_SLOT(d, void _uq_monitorReadyRead(int fd))
|
||||
ClientPrivate *d;
|
||||
private:
|
||||
struct udev *m_udev;
|
||||
struct udev_monitor *m_monitor;
|
||||
QSocketNotifier *m_monitorNotifier;
|
||||
};
|
||||
|
||||
|
||||
inline QStringList listFromListEntry(struct udev_list_entry *list)
|
||||
{
|
||||
QStringList ret;
|
||||
struct udev_list_entry *entry;
|
||||
udev_list_entry_foreach(entry, list) {
|
||||
ret << QString::fromLatin1(udev_list_entry_get_name(entry));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,73 +0,0 @@
|
|||
/*
|
||||
Copyright 2009 Benjamin K. Stuhl <bks24@cornell.edu>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) version 3, or any
|
||||
later version accepted by the membership of KDE e.V. (or its
|
||||
successor approved by the membership of KDE e.V.), which shall
|
||||
act as a proxy defined in Section 6 of version 3 of the license.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef UDEVQT_P_H
|
||||
#define UDEVQT_P_H
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
|
||||
#include <libudev.h>
|
||||
}
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QSocketNotifier>
|
||||
|
||||
namespace UdevQt
|
||||
{
|
||||
|
||||
class DevicePrivate
|
||||
{
|
||||
public:
|
||||
DevicePrivate(struct udev_device *udev_, bool ref = true);
|
||||
~DevicePrivate();
|
||||
DevicePrivate &operator=(const DevicePrivate& other);
|
||||
|
||||
struct udev_device *udev;
|
||||
};
|
||||
|
||||
|
||||
class ClientPrivate
|
||||
{
|
||||
public:
|
||||
ClientPrivate(Client *q_, const QStringList &subsystemList);
|
||||
~ClientPrivate();
|
||||
|
||||
void _uq_monitorReadyRead(int fd);
|
||||
|
||||
struct udev *udev;
|
||||
struct udev_monitor *monitor;
|
||||
Client *q;
|
||||
QSocketNotifier *monitorNotifier;
|
||||
};
|
||||
|
||||
inline QStringList listFromListEntry(struct udev_list_entry *list)
|
||||
{
|
||||
QStringList ret;
|
||||
struct udev_list_entry *entry;
|
||||
udev_list_entry_foreach(entry, list) {
|
||||
ret << QString::fromLatin1(udev_list_entry_get_name(entry));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
|
@ -19,92 +19,45 @@
|
|||
*/
|
||||
|
||||
#include "udevqt.h"
|
||||
#include "udevqt_p.h"
|
||||
|
||||
#include <QtCore/QSocketNotifier>
|
||||
#include <qplatformdefs.h>
|
||||
|
||||
namespace UdevQt {
|
||||
|
||||
ClientPrivate::ClientPrivate(Client *q_, const QStringList &subsystemList)
|
||||
: udev(udev_new()), monitor(0), q(q_), monitorNotifier(0)
|
||||
Client::Client(const QStringList& subsystemList, QObject *parent)
|
||||
: QObject(parent), m_udev(udev_new()), m_monitor(0), m_monitorNotifier(0)
|
||||
{
|
||||
// create a listener
|
||||
monitor = udev_monitor_new_from_netlink(udev, "udev");
|
||||
m_monitor = udev_monitor_new_from_netlink(m_udev, "udev");
|
||||
|
||||
if (!monitor) {
|
||||
if (!m_monitor) {
|
||||
qWarning("UdevQt: unable to create udev monitor connection");
|
||||
return;
|
||||
}
|
||||
|
||||
// apply subsystem filters
|
||||
foreach (const QString &subsysDevtype, subsystemList) {
|
||||
udev_monitor_filter_add_match_subsystem_devtype(monitor, subsysDevtype.toLatin1().constData(), NULL);
|
||||
udev_monitor_filter_add_match_subsystem_devtype(m_monitor, subsysDevtype.toLatin1().constData(), NULL);
|
||||
}
|
||||
|
||||
// start the monitor receiving
|
||||
udev_monitor_enable_receiving(monitor);
|
||||
monitorNotifier = new QSocketNotifier(udev_monitor_get_fd(monitor), QSocketNotifier::Read);
|
||||
QObject::connect(monitorNotifier, SIGNAL(activated(int)), q, SLOT(_uq_monitorReadyRead(int)));
|
||||
}
|
||||
|
||||
ClientPrivate::~ClientPrivate()
|
||||
{
|
||||
udev_unref(udev);
|
||||
delete monitorNotifier;
|
||||
|
||||
if (monitor)
|
||||
udev_monitor_unref(monitor);
|
||||
}
|
||||
|
||||
void ClientPrivate::_uq_monitorReadyRead(int fd)
|
||||
{
|
||||
Q_UNUSED(fd);
|
||||
monitorNotifier->setEnabled(false);
|
||||
struct udev_device *dev = udev_monitor_receive_device(monitor);
|
||||
monitorNotifier->setEnabled(true);
|
||||
|
||||
if (!dev)
|
||||
return;
|
||||
|
||||
Device device(new DevicePrivate(dev, false));
|
||||
|
||||
QByteArray action(udev_device_get_action(dev));
|
||||
if (action == "add") {
|
||||
emit q->deviceAdded(device);
|
||||
} else if (action == "remove") {
|
||||
emit q->deviceRemoved(device);
|
||||
} else if (action == "change") {
|
||||
emit q->deviceChanged(device);
|
||||
} else if (action == "online") {
|
||||
emit q->deviceOnlined(device);
|
||||
} else if (action == "offline") {
|
||||
emit q->deviceOfflined(device);
|
||||
/*
|
||||
bind/unbind are driver changing for device type of event, on some systems it appears to be
|
||||
broken and doing it all the time thus ignore the actions
|
||||
*/
|
||||
} else if (action != "bind" && action != "unbind") {
|
||||
qWarning("UdevQt: unhandled device action \"%s\"", action.constData());
|
||||
}
|
||||
}
|
||||
|
||||
Client::Client(const QStringList& subsystemList, QObject *parent)
|
||||
: QObject(parent)
|
||||
, d(new ClientPrivate(this, subsystemList))
|
||||
{
|
||||
udev_monitor_enable_receiving(m_monitor);
|
||||
m_monitorNotifier = new QSocketNotifier(udev_monitor_get_fd(m_monitor), QSocketNotifier::Read);
|
||||
QObject::connect(m_monitorNotifier, SIGNAL(activated(int)), this, SLOT(monitorReadyRead(int)));
|
||||
}
|
||||
|
||||
Client::~Client()
|
||||
{
|
||||
delete d;
|
||||
udev_unref(m_udev);
|
||||
delete m_monitorNotifier;
|
||||
|
||||
if (m_monitor)
|
||||
udev_monitor_unref(m_monitor);
|
||||
}
|
||||
|
||||
DeviceList Client::allDevices()
|
||||
{
|
||||
DeviceList ret;
|
||||
|
||||
struct udev_enumerate *en = udev_enumerate_new(d->udev);
|
||||
struct udev_enumerate *en = udev_enumerate_new(m_udev);
|
||||
udev_enumerate_scan_devices(en);
|
||||
|
||||
struct udev_list_entry *entry;
|
||||
|
@ -116,7 +69,7 @@ DeviceList Client::allDevices()
|
|||
if (!ud)
|
||||
continue;
|
||||
|
||||
ret << Device(new DevicePrivate(ud, false));
|
||||
ret << Device(ud, false);
|
||||
}
|
||||
|
||||
udev_enumerate_unref(en);
|
||||
|
@ -126,12 +79,45 @@ DeviceList Client::allDevices()
|
|||
|
||||
Device Client::deviceBySysfsPath(const QString &sysfsPath)
|
||||
{
|
||||
struct udev_device *ud = udev_device_new_from_syspath(d->udev, sysfsPath.toLatin1().constData());
|
||||
struct udev_device *ud = udev_device_new_from_syspath(m_udev, sysfsPath.toLatin1().constData());
|
||||
|
||||
if (!ud)
|
||||
return Device();
|
||||
|
||||
return Device(new DevicePrivate(ud, false));
|
||||
return Device(ud, false);
|
||||
}
|
||||
|
||||
|
||||
void Client::monitorReadyRead(int fd)
|
||||
{
|
||||
Q_UNUSED(fd);
|
||||
m_monitorNotifier->setEnabled(false);
|
||||
struct udev_device *dev = udev_monitor_receive_device(m_monitor);
|
||||
m_monitorNotifier->setEnabled(true);
|
||||
|
||||
if (!dev)
|
||||
return;
|
||||
|
||||
Device device(dev, false);
|
||||
|
||||
QByteArray action(udev_device_get_action(dev));
|
||||
if (action == "add") {
|
||||
emit deviceAdded(device);
|
||||
} else if (action == "remove") {
|
||||
emit deviceRemoved(device);
|
||||
} else if (action == "change") {
|
||||
emit deviceChanged(device);
|
||||
} else if (action == "online") {
|
||||
emit deviceOnlined(device);
|
||||
} else if (action == "offline") {
|
||||
emit deviceOfflined(device);
|
||||
/*
|
||||
bind/unbind are driver changing for device type of event, on some systems it appears to be
|
||||
broken and doing it all the time thus ignore the actions
|
||||
*/
|
||||
} else if (action != "bind" && action != "unbind") {
|
||||
qWarning("UdevQt: unhandled device action \"%s\"", action.constData());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,175 +19,154 @@
|
|||
*/
|
||||
|
||||
#include "udevqt.h"
|
||||
#include "udevqt_p.h"
|
||||
|
||||
#include <QtCore/QByteArray>
|
||||
|
||||
namespace UdevQt {
|
||||
|
||||
DevicePrivate::DevicePrivate(struct udev_device *udev_, bool ref)
|
||||
: udev(udev_)
|
||||
{
|
||||
if (ref)
|
||||
udev_device_ref(udev);
|
||||
}
|
||||
|
||||
DevicePrivate::~DevicePrivate()
|
||||
{
|
||||
udev_device_unref(udev);
|
||||
}
|
||||
|
||||
DevicePrivate &DevicePrivate::operator=(const DevicePrivate &other)
|
||||
{
|
||||
udev_device_unref(udev);
|
||||
udev = udev_device_ref(other.udev);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Device::Device()
|
||||
: d(0)
|
||||
: m_udev(Q_NULLPTR)
|
||||
{
|
||||
}
|
||||
|
||||
Device::Device(struct udev_device *udev_, bool ref)
|
||||
: m_udev(udev_)
|
||||
{
|
||||
if (m_udev && ref)
|
||||
udev_device_ref(m_udev);
|
||||
}
|
||||
|
||||
Device::Device(const Device &other)
|
||||
: d(0)
|
||||
: m_udev(other.m_udev)
|
||||
{
|
||||
if (other.d) {
|
||||
d = new DevicePrivate(other.d->udev);
|
||||
if (other.m_udev) {
|
||||
udev_device_ref(other.m_udev);
|
||||
}
|
||||
}
|
||||
|
||||
Device::Device(DevicePrivate *devPrivate)
|
||||
: d(devPrivate)
|
||||
{
|
||||
}
|
||||
|
||||
Device::~Device()
|
||||
{
|
||||
delete d;
|
||||
if (m_udev) {
|
||||
udev_device_unref(m_udev);
|
||||
}
|
||||
}
|
||||
|
||||
Device &Device::operator=(const Device &other)
|
||||
{
|
||||
if (this == &other)
|
||||
return *this;
|
||||
if (!other.d) {
|
||||
delete d;
|
||||
d = 0;
|
||||
return *this;
|
||||
if (m_udev) {
|
||||
udev_device_unref(m_udev);
|
||||
}
|
||||
if (!d) {
|
||||
d = new DevicePrivate(other.d->udev);
|
||||
if (other.m_udev) {
|
||||
m_udev = udev_device_ref(other.m_udev);
|
||||
} else {
|
||||
*d = *other.d;
|
||||
m_udev = Q_NULLPTR;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool Device::isValid() const
|
||||
{
|
||||
return (d != 0);
|
||||
return (m_udev != Q_NULLPTR);
|
||||
}
|
||||
|
||||
QString Device::subsystem() const
|
||||
{
|
||||
if (!d)
|
||||
if (!m_udev) {
|
||||
return QString();
|
||||
|
||||
return QString::fromLatin1(udev_device_get_subsystem(d->udev));
|
||||
}
|
||||
return QString::fromLatin1(udev_device_get_subsystem(m_udev));
|
||||
}
|
||||
|
||||
QString Device::devType() const
|
||||
{
|
||||
if (!d)
|
||||
if (!m_udev) {
|
||||
return QString();
|
||||
|
||||
return QString::fromLatin1(udev_device_get_devtype(d->udev));
|
||||
}
|
||||
return QString::fromLatin1(udev_device_get_devtype(m_udev));
|
||||
}
|
||||
|
||||
QString Device::name() const
|
||||
{
|
||||
if (!d)
|
||||
if (!m_udev) {
|
||||
return QString();
|
||||
|
||||
return QString::fromLatin1(udev_device_get_sysname(d->udev));
|
||||
}
|
||||
return QString::fromLatin1(udev_device_get_sysname(m_udev));
|
||||
}
|
||||
|
||||
QString Device::sysfsPath() const
|
||||
{
|
||||
if (!d)
|
||||
if (!m_udev) {
|
||||
return QString();
|
||||
|
||||
return QString::fromLatin1(udev_device_get_syspath(d->udev));
|
||||
}
|
||||
return QString::fromLatin1(udev_device_get_syspath(m_udev));
|
||||
}
|
||||
|
||||
int Device::sysfsNumber() const
|
||||
{
|
||||
if (!d)
|
||||
if (!m_udev) {
|
||||
return -1;
|
||||
|
||||
QString value = QString::fromLatin1(udev_device_get_sysnum(d->udev));
|
||||
}
|
||||
QString value = QString::fromLatin1(udev_device_get_sysnum(m_udev));
|
||||
bool success = false;
|
||||
int number = value.toInt(&success);
|
||||
if (success)
|
||||
if (success) {
|
||||
return number;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
QString Device::driver() const
|
||||
{
|
||||
if (!d)
|
||||
if (!m_udev) {
|
||||
return QString();
|
||||
|
||||
return QString::fromLatin1(udev_device_get_driver(d->udev));
|
||||
}
|
||||
return QString::fromLatin1(udev_device_get_driver(m_udev));
|
||||
}
|
||||
|
||||
QStringList Device::alternateDeviceSymlinks() const
|
||||
{
|
||||
if (!d)
|
||||
if (!m_udev) {
|
||||
return QStringList();
|
||||
|
||||
return listFromListEntry(udev_device_get_devlinks_list_entry(d->udev));
|
||||
}
|
||||
return listFromListEntry(udev_device_get_devlinks_list_entry(m_udev));
|
||||
}
|
||||
|
||||
QStringList Device::deviceProperties() const
|
||||
{
|
||||
if (!d)
|
||||
if (!m_udev) {
|
||||
return QStringList();
|
||||
|
||||
return listFromListEntry(udev_device_get_properties_list_entry(d->udev));
|
||||
}
|
||||
return listFromListEntry(udev_device_get_properties_list_entry(m_udev));
|
||||
}
|
||||
|
||||
Device Device::parent() const
|
||||
{
|
||||
if (!d)
|
||||
if (!m_udev) {
|
||||
return Device();
|
||||
|
||||
struct udev_device *p = udev_device_get_parent(d->udev);
|
||||
|
||||
if (!p)
|
||||
}
|
||||
struct udev_device *p = udev_device_get_parent(m_udev);
|
||||
if (!p) {
|
||||
return Device();
|
||||
|
||||
return Device(new DevicePrivate(p));
|
||||
}
|
||||
return Device(p);
|
||||
}
|
||||
|
||||
QString Device::deviceProperty(const QString &name) const
|
||||
{
|
||||
if (!d)
|
||||
if (!m_udev) {
|
||||
return QString();
|
||||
|
||||
QByteArray propName = name.toLatin1();
|
||||
return QString::fromLatin1(udev_device_get_property_value(d->udev, propName.constData()));
|
||||
}
|
||||
const QByteArray propName(name.toLatin1());
|
||||
return QString::fromLatin1(udev_device_get_property_value(m_udev, propName.constData()));
|
||||
}
|
||||
|
||||
QString Device::sysfsProperty(const QString &name) const
|
||||
{
|
||||
if (!d)
|
||||
if (!m_udev) {
|
||||
return QString();
|
||||
|
||||
QByteArray propName = name.toLatin1();
|
||||
return QString::fromLatin1(udev_device_get_sysattr_value(d->udev, propName.constData()));
|
||||
}
|
||||
const QByteArray propName(name.toLatin1());
|
||||
return QString::fromLatin1(udev_device_get_sysattr_value(m_udev, propName.constData()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue