diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,8 +48,6 @@ "Support turning off signal-transmitting devices to save energy" ) -find_package(UDev REQUIRED) - find_package(XCB REQUIRED COMPONENTS XCB RANDR DPMS) find_package(DDCUtil) diff --git a/cmake/FindUDev.cmake b/cmake/FindUDev.cmake deleted file mode 100644 --- a/cmake/FindUDev.cmake +++ /dev/null @@ -1,40 +0,0 @@ -# - Try to find UDev -# Once done this will define -# -# UDEV_FOUND - system has UDev -# UDEV_INCLUDE_DIR - the libudev include directory -# UDEV_LIBS - The libudev libraries - -# Copyright (c) 2010, Rafael Fernández López, -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# 1. Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# 2. Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# 3. Neither the name of the University nor the names of its contributors -# may be used to endorse or promote products derived from this software -# without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -# SUCH DAMAGE. - -find_path(UDEV_INCLUDE_DIR libudev.h) -find_library(UDEV_LIBS udev) - -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(UDev DEFAULT_MSG UDEV_INCLUDE_DIR UDEV_LIBS) - -mark_as_advanced(UDEV_INCLUDE_DIR UDEV_LIBS) diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt --- a/daemon/CMakeLists.txt +++ b/daemon/CMakeLists.txt @@ -130,8 +130,6 @@ target_link_libraries(powerdevil Qt5::X11Extras) endif () -target_link_libraries(powerdevil ${UDEV_LIBS}) - install(TARGETS powerdevil DESTINATION ${CMAKE_INSTALL_FULL_LIBEXECDIR}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/powerdevil.desktop DESTINATION ${KDE_INSTALL_AUTOSTARTDIR}) diff --git a/daemon/backends/CMakeLists.txt b/daemon/backends/CMakeLists.txt --- a/daemon/backends/CMakeLists.txt +++ b/daemon/backends/CMakeLists.txt @@ -10,8 +10,6 @@ upower/powerdevilupowerbackend.cpp upower/xrandrbrightness.cpp upower/xrandrxcbhelper.cpp - upower/udevqtclient.cpp - upower/udevqtdevice.cpp upower/ddcutilbrightness.cpp ) @@ -55,7 +53,6 @@ KF5::CoreAddons KF5::DBusAddons KF5::I18n - ${UDEV_LIBS} ${X11_LIBRARIES} ${X11_Xrandr_LIB} ${XCB_XCB_LIBRARY} diff --git a/daemon/backends/upower/powerdevilupowerbackend.h b/daemon/backends/upower/powerdevilupowerbackend.h --- a/daemon/backends/upower/powerdevilupowerbackend.h +++ b/daemon/backends/upower/powerdevilupowerbackend.h @@ -31,7 +31,6 @@ #include "upower_device_interface.h" #include "upower_interface.h" #include "upower_kbdbacklight_interface.h" -#include "udevqt.h" #define UPOWER_SERVICE "org.freedesktop.UPower" #define UPOWER_PATH "/org/freedesktop/UPower" @@ -41,7 +40,6 @@ #define LOGIN1_SERVICE "org.freedesktop.login1" #define CONSOLEKIT2_SERVICE "org.freedesktop.ConsoleKit" -class UdevHelper; class XRandRXCBHelper; class XRandrBrightness; class QPropertyAnimation; @@ -83,7 +81,6 @@ void slotPropertyChanged(); void slotLogin1PrepareForSleep(bool active); void slotScreenBrightnessChanged(); - void onDeviceChanged(const UdevQt::Device &device); void onKeyboardBrightnessChanged(int); void onPropertiesChanged(const QString &ifaceName, const QVariantMap &changedProps, const QStringList &invalidatedProps); @@ -119,9 +116,6 @@ bool m_lidIsClosed; bool m_onBattery; - // property if brightness control is leds subsystem - bool m_isLedBrightnessControl; - //helper path QString m_syspath; }; diff --git a/daemon/backends/upower/powerdevilupowerbackend.cpp b/daemon/backends/upower/powerdevilupowerbackend.cpp --- a/daemon/backends/upower/powerdevilupowerbackend.cpp +++ b/daemon/backends/upower/powerdevilupowerbackend.cpp @@ -41,7 +41,6 @@ #include "ddcutilbrightness.h" #include "upowersuspendjob.h" #include "login1suspendjob.h" -#include "udevqt.h" #define HELPER_ID "org.kde.powerdevil.backlighthelper" @@ -56,7 +55,6 @@ , m_lidIsPresent(false) , m_lidIsClosed(false) , m_onBattery(false) - , m_isLedBrightnessControl(false) { } @@ -198,12 +196,6 @@ m_syspath = syspathJob->data()["syspath"].toString(); m_syspath = QFileInfo(m_syspath).readLink(); - m_isLedBrightnessControl = m_syspath.contains(QLatin1String("/leds/")); - if (!m_isLedBrightnessControl) { - UdevQt::Client *client = new UdevQt::Client(QStringList("backlight"), this); - connect(client, SIGNAL(deviceChanged(UdevQt::Device)), SLOT(onDeviceChanged(UdevQt::Device))); - } - Q_EMIT brightnessSupportQueried(m_brightnessMax > 0); } ); @@ -356,25 +348,6 @@ setBackendIsReady(controls, supported); } -void PowerDevilUPowerBackend::onDeviceChanged(const UdevQt::Device &device) -{ - qCDebug(POWERDEVIL) << "Udev device changed" << m_syspath << device.sysfsPath(); - if (device.sysfsPath() != m_syspath) { - return; - } - - int maxBrightness = device.sysfsProperty("max_brightness").toInt(); - if (maxBrightness <= 0) { - return; - } - int newBrightness = device.sysfsProperty("brightness").toInt(); - - if (newBrightness != m_cachedBrightnessMap[Screen]) { - m_cachedBrightnessMap[Screen] = newBrightness; - onBrightnessChanged(Screen, newBrightness, maxBrightness); - } -} - int PowerDevilUPowerBackend::brightnessKeyPressed(PowerDevil::BrightnessLogic::BrightnessKeyType type, BrightnessControlType controlType) { BrightnessControlsList allControls = brightnessControlsAvailable(); @@ -493,10 +466,8 @@ KAuth::ExecuteJob *job = action.execute(); // we don't care about the result since executing the job sync is bad job->start(); - if (m_isLedBrightnessControl) { - m_cachedBrightnessMap[Screen] = value; - slotScreenBrightnessChanged(); - } + m_cachedBrightnessMap[Screen] = value; + slotScreenBrightnessChanged(); } } else if (type == Keyboard) { qCDebug(POWERDEVIL) << "set kbd backlight value: " << value; @@ -511,7 +482,7 @@ } int value = brightness(Screen); - if (value != m_cachedBrightnessMap[Screen] || m_isLedBrightnessControl) { + if (value != m_cachedBrightnessMap[Screen]) { m_cachedBrightnessMap[Screen] = value; onBrightnessChanged(Screen, value, brightnessMax(Screen)); } diff --git a/daemon/backends/upower/udevqt.h b/daemon/backends/upower/udevqt.h deleted file mode 100644 --- a/daemon/backends/upower/udevqt.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef UDEVQT_H -#define UDEVQT_H - -#include "udevqtclient.h" -#include "udevqtdevice.h" - -#endif diff --git a/daemon/backends/upower/udevqt_p.h b/daemon/backends/upower/udevqt_p.h deleted file mode 100644 --- a/daemon/backends/upower/udevqt_p.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - Copyright 2009 Benjamin K. Stuhl - - 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 . -*/ - -#ifndef UDEVQT_P_H -#define UDEVQT_P_H - -extern "C" -{ -#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE -#include -} - -class QByteArray; -class QSocketNotifier; - -namespace UdevQt -{ - -class DevicePrivate -{ - public: - DevicePrivate(struct udev_device *udev_, bool ref = true); - ~DevicePrivate(); - DevicePrivate &operator=(const DevicePrivate& other); - - QString decodePropertyValue(const QByteArray &encoded) const; - - struct udev_device *udev; -}; - -class Client; -class ClientPrivate -{ - public: - enum ListenToWhat { ListenToList, ListenToNone }; - - ClientPrivate(Client *q_); - ~ClientPrivate(); - - void init(const QStringList &subsystemList, ListenToWhat what); - void setWatchedSubsystems(const QStringList &subsystemList); - void _uq_monitorReadyRead(int fd); - DeviceList deviceListFromEnumerate(struct udev_enumerate *en); - - struct udev *udev; - struct udev_monitor *monitor; - Client *q; - QSocketNotifier *monitorNotifier; - QStringList watchedSubsystems; -}; - -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 diff --git a/daemon/backends/upower/udevqtclient.h b/daemon/backends/upower/udevqtclient.h deleted file mode 100644 --- a/daemon/backends/upower/udevqtclient.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - Copyright 2009 Benjamin K. Stuhl - - 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 . -*/ - -#ifndef UDEVQTCLIENT_H -#define UDEVQTCLIENT_H - -#include -#include -#include -#include -#include - -#include "udevqtdevice.h" - -namespace UdevQt -{ - -class ClientPrivate; -class Client : public QObject -{ - Q_OBJECT - - Q_PROPERTY(QStringList watchedSubsystems READ watchedSubsystems WRITE setWatchedSubsystems) - - public: - Client(QObject *parent = 0); - Client(const QStringList &subsystemList, QObject *parent = 0); - ~Client(); - - QStringList watchedSubsystems() const; - void setWatchedSubsystems(const QStringList &subsystemList); - - DeviceList allDevices(); - DeviceList devicesByProperty(const QString &property, const QVariant &value); - DeviceList devicesBySubsystem(const QString &subsystem); - Device deviceByDeviceFile(const QString &deviceFile); - Device deviceBySysfsPath(const QString &sysfsPath); - Device deviceBySubsystemAndName(const QString &subsystem, const QString &name); - - Q_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: - friend class ClientPrivate; - Q_PRIVATE_SLOT(d, void _uq_monitorReadyRead(int fd)) - ClientPrivate *d; -}; - -} - -#endif diff --git a/daemon/backends/upower/udevqtclient.cpp b/daemon/backends/upower/udevqtclient.cpp deleted file mode 100644 --- a/daemon/backends/upower/udevqtclient.cpp +++ /dev/null @@ -1,258 +0,0 @@ -/* - Copyright 2009 Benjamin K. Stuhl - - 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 . -*/ - -#include "udevqtclient.h" -#include "udevqt_p.h" - -#include - -#include -#include - -namespace UdevQt { - -ClientPrivate::ClientPrivate(Client *q_) - : udev(0), monitor(0), q(q_), monitorNotifier(0) -{ -} - -ClientPrivate::~ClientPrivate() -{ - udev_unref(udev); - delete monitorNotifier; - - if (monitor) - udev_monitor_unref(monitor); -} - -void ClientPrivate::init(const QStringList &subsystemList, ListenToWhat what) -{ - udev = udev_new(); - - if (what != ListenToNone) { - setWatchedSubsystems(subsystemList); - } -} - -void ClientPrivate::setWatchedSubsystems(const QStringList &subsystemList) -{ - // create a listener - struct udev_monitor *newM = udev_monitor_new_from_netlink(udev, "udev"); - - if (!newM) { - qCWarning(POWERDEVIL, "UdevQt: unable to create udev monitor connection"); - return; - } - - // apply our filters; an empty list means listen to everything - Q_FOREACH (const QString& subsysDevtype, subsystemList) { - int ix = subsysDevtype.indexOf(u'/'); - - if (ix > 0) { - QByteArray subsystem = subsysDevtype.leftRef(ix).toLatin1(); - QByteArray devType = subsysDevtype.midRef(ix + 1).toLatin1(); - udev_monitor_filter_add_match_subsystem_devtype(newM, subsystem.constData(), devType.constData()); - } else { - udev_monitor_filter_add_match_subsystem_devtype(newM, subsysDevtype.toLatin1().constData(), NULL); - } - } - - // start the new monitor receiving - udev_monitor_enable_receiving(newM); - QSocketNotifier *sn = new QSocketNotifier(udev_monitor_get_fd(newM), QSocketNotifier::Read); - QObject::connect(sn, SIGNAL(activated(int)), q, SLOT(_uq_monitorReadyRead(int))); - - // kill any previous monitor - delete monitorNotifier; - if (monitor) - udev_monitor_unref(monitor); - - // and save our new one - monitor = newM; - monitorNotifier = sn; - watchedSubsystems = subsystemList; -} - -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") { - Q_EMIT q->deviceAdded(device); - } else if (action == "remove") { - Q_EMIT q->deviceRemoved(device); - } else if (action == "change") { - Q_EMIT q->deviceChanged(device); - } else if (action == "online") { - Q_EMIT q->deviceOnlined(device); - } else if (action == "offline") { - Q_EMIT q->deviceOfflined(device); - } else { - qCWarning(POWERDEVIL, "UdevQt: unhandled device action \"%s\"", action.constData()); - } -} - -DeviceList ClientPrivate::deviceListFromEnumerate(struct udev_enumerate *en) -{ - DeviceList ret; - struct udev_list_entry *list, *entry; - - udev_enumerate_scan_devices(en); - list = udev_enumerate_get_list_entry(en); - udev_list_entry_foreach(entry, list) { - struct udev_device *ud = udev_device_new_from_syspath(udev_enumerate_get_udev(en), - udev_list_entry_get_name(entry)); - - if (!ud) - continue; - - ret << Device(new DevicePrivate(ud, false)); - } - - udev_enumerate_unref(en); - - return ret; -} - - -Client::Client(QObject *parent) - : QObject(parent) - , d(new ClientPrivate(this)) -{ - d->init(QStringList(), ClientPrivate::ListenToNone); -} - -Client::Client(const QStringList& subsystemList, QObject *parent) - : QObject(parent) - , d(new ClientPrivate(this)) -{ - d->init(subsystemList, ClientPrivate::ListenToList); -} - -Client::~Client() -{ - delete d; -} - -QStringList Client::watchedSubsystems() const -{ - // we're watching a specific list - if (!d->watchedSubsystems.isEmpty()) - return d->watchedSubsystems; - - // we're not watching anything - if (!d->monitor) - return QStringList(); - - // we're watching everything: figure out what "everything" currently is - // we don't cache it, since it may be subject to change, depending on hotplug - struct udev_enumerate *en = udev_enumerate_new(d->udev); - udev_enumerate_scan_subsystems(en); - QStringList s = listFromListEntry(udev_enumerate_get_list_entry(en)); - udev_enumerate_unref(en); - return s; -} - -void Client::setWatchedSubsystems(const QStringList &subsystemList) -{ - d->setWatchedSubsystems(subsystemList); -} - -DeviceList Client::devicesByProperty(const QString &property, const QVariant &value) -{ - struct udev_enumerate *en = udev_enumerate_new(d->udev); - - if (value.isValid()) { - udev_enumerate_add_match_property(en, property.toLatin1().constData(), value.toString().toLatin1().constData()); - } else { - udev_enumerate_add_match_property(en, property.toLatin1().constData(), NULL); - } - - return d->deviceListFromEnumerate(en); -} - -DeviceList Client::allDevices() -{ - struct udev_enumerate *en = udev_enumerate_new(d->udev); - return d->deviceListFromEnumerate(en); -} - -DeviceList Client::devicesBySubsystem(const QString &subsystem) -{ - struct udev_enumerate *en = udev_enumerate_new(d->udev); - - udev_enumerate_add_match_subsystem(en, subsystem.toLatin1().constData()); - return d->deviceListFromEnumerate(en); -} - -Device Client::deviceByDeviceFile(const QString &deviceFile) -{ - QT_STATBUF sb; - - if (QT_STAT(deviceFile.toLatin1().constData(), &sb) != 0) - return Device(); - - struct udev_device *ud = 0; - - if (S_ISBLK(sb.st_mode)) - ud = udev_device_new_from_devnum(d->udev, 'b', sb.st_rdev); - else if (S_ISCHR(sb.st_mode)) - ud = udev_device_new_from_devnum(d->udev, 'c', sb.st_rdev); - - if (!ud) - return Device(); - - return Device(new DevicePrivate(ud, false)); -} - -Device Client::deviceBySysfsPath(const QString &sysfsPath) -{ - struct udev_device *ud = udev_device_new_from_syspath(d->udev, sysfsPath.toLatin1().constData()); - - if (!ud) - return Device(); - - return Device(new DevicePrivate(ud, false)); -} - -Device Client::deviceBySubsystemAndName(const QString &subsystem, const QString &name) -{ - struct udev_device *ud = udev_device_new_from_subsystem_sysname(d->udev, - subsystem.toLatin1().constData(), - name.toLatin1().constData()); - - if (!ud) - return Device(); - - return Device(new DevicePrivate(ud, false)); -} - -} - -#include "moc_udevqtclient.cpp" diff --git a/daemon/backends/upower/udevqtdevice.h b/daemon/backends/upower/udevqtdevice.h deleted file mode 100644 --- a/daemon/backends/upower/udevqtdevice.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - Copyright 2009 Benjamin K. Stuhl - - 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 . -*/ - -#ifndef UDEVQTDEVICE_H -#define UDEVQTDEVICE_H - -#include -#include -#include -#include -#include - -namespace UdevQt -{ - -class DevicePrivate; -class Device -{ - public: - Device(); - 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; - QString primaryDeviceFile() const; - QStringList alternateDeviceSymlinks() const; - QStringList deviceProperties() const; - QStringList sysfsProperties() const; - Device parent() const; - - // ### should this really be a QVariant? as far as udev knows, everything is a string... - // see also Client::devicesByProperty - QVariant deviceProperty(const QString &name) const; - QString decodedDeviceProperty(const QString &name) const; - QVariant sysfsProperty(const QString &name) const; - Device ancestorOfType(const QString &subsys, const QString &devtype) const; - - private: - Device(DevicePrivate *devPrivate); - friend class Client; - friend class ClientPrivate; - - DevicePrivate *d; -}; - -typedef QList DeviceList; - -} - -#endif diff --git a/daemon/backends/upower/udevqtdevice.cpp b/daemon/backends/upower/udevqtdevice.cpp deleted file mode 100644 --- a/daemon/backends/upower/udevqtdevice.cpp +++ /dev/null @@ -1,270 +0,0 @@ -/* - Copyright 2009 Benjamin K. Stuhl - - 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 . -*/ - -#include "udevqtdevice.h" -#include "udevqt_p.h" - -#include - -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; -} - -QString DevicePrivate::decodePropertyValue(const QByteArray &encoded) const -{ - QByteArray decoded; - const int len = encoded.length(); - - for (int i = 0; i < len; i++) { - quint8 ch = encoded.at(i); - - if (ch == '\\') { - if (i + 1 < len && encoded.at(i + 1) == '\\') { - decoded.append('\\'); - i++; - continue; - } else if (i + 3 < len && encoded.at(i + 1) == 'x') { - QByteArray hex = encoded.mid(i + 2, 2); - bool ok; - int code = hex.toInt(&ok, 16); - if (ok) - decoded.append(char(code)); - i += 3; - continue; - } - } else { - decoded.append(ch); - } - } - return QString::fromUtf8(decoded); -} - -Device::Device() - : d(0) -{ -} - -Device::Device(const Device &other) -{ - if (other.d) { - d = new DevicePrivate(other.d->udev); - } else { - d = 0; - } -} - -Device::Device(DevicePrivate *devPrivate) - : d(devPrivate) -{ -} - -Device::~Device() -{ - delete d; -} - -Device &Device::operator=(const Device &other) -{ - if (this == &other) - return *this; - if (!other.d) { - delete d; - d = 0; - return *this; - } - if (!d) { - d = new DevicePrivate(other.d->udev); - } else { - *d = *other.d; - } - - return *this; -} - -bool Device::isValid() const -{ - return (d != 0); -} - -QString Device::subsystem() const -{ - if (!d) - return QString(); - - return QString::fromLatin1(udev_device_get_subsystem(d->udev)); -} - -QString Device::devType() const -{ - if (!d) - return QString(); - - return QString::fromLatin1(udev_device_get_devtype(d->udev)); -} - -QString Device::name() const -{ - if (!d) - return QString(); - - return QString::fromLatin1(udev_device_get_sysname(d->udev)); -} - -QString Device::sysfsPath() const -{ - if (!d) - return QString(); - - return QString::fromLatin1(udev_device_get_syspath(d->udev)); -} - -int Device::sysfsNumber() const -{ - if (!d) - return -1; - - QString value = QString::fromLatin1(udev_device_get_sysnum(d->udev)); - bool success = false; - int number = value.toInt(&success); - if (success) - return number; - return -1; -} - -QString Device::driver() const -{ - if (!d) - return QString(); - - return QString::fromLatin1(udev_device_get_driver(d->udev)); -} - -QString Device::primaryDeviceFile() const -{ - if (!d) - return QString(); - - return QString::fromLatin1(udev_device_get_devnode(d->udev)); -} - -QStringList Device::alternateDeviceSymlinks() const -{ - if (!d) - return QStringList(); - - return listFromListEntry(udev_device_get_devlinks_list_entry(d->udev)); -} - -QStringList Device::deviceProperties() const -{ - if (!d) - return QStringList(); - - return listFromListEntry(udev_device_get_properties_list_entry(d->udev)); -} - -QStringList Device::sysfsProperties() const -{ - if (!d) - return QStringList(); - - return listFromListEntry(udev_device_get_sysattr_list_entry(d->udev)); -} - -Device Device::parent() const -{ - if (!d) - return Device(); - - struct udev_device *p = udev_device_get_parent(d->udev); - - if (!p) - return Device(); - - return Device(new DevicePrivate(p)); -} - -QVariant Device::deviceProperty(const QString &name) const -{ - if (!d) - return QVariant(); - - QByteArray propName = name.toLatin1(); - QString propValue = QString::fromLatin1(udev_device_get_property_value(d->udev, propName.constData())); - if (!propValue.isEmpty()) { - return QVariant::fromValue(propValue); - } - return QVariant(); -} - -QString Device::decodedDeviceProperty(const QString &name) const -{ - if (!d) - return QString(); - - QByteArray propName = name.toLatin1(); - return d->decodePropertyValue(udev_device_get_property_value(d->udev, propName.constData())); -} - -QVariant Device::sysfsProperty(const QString &name) const -{ - if (!d) - return QVariant(); - - QByteArray propName = name.toLatin1(); - QString propValue = QString::fromLatin1(udev_device_get_sysattr_value(d->udev, propName.constData())); - if (!propValue.isEmpty()) { - return QVariant::fromValue(propValue); - } - return QVariant(); -} - -Device Device::ancestorOfType(const QString &subsys, const QString &devtype) const -{ - if (!d) - return Device(); - - struct udev_device *p = udev_device_get_parent_with_subsystem_devtype(d->udev, - subsys.toLatin1().constData(), devtype.toLatin1().constData()); - - if (!p) - return Device(); - - return Device(new DevicePrivate(p)); -} - -}