File indexing completed on 2024-11-10 04:57:23

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #include "udev.h"
0010 #include "utils/common.h"
0011 // Qt
0012 #include <QByteArray>
0013 #include <QDebug>
0014 // system
0015 #include <cerrno>
0016 #include <functional>
0017 #include <libudev.h>
0018 
0019 namespace KWin
0020 {
0021 
0022 Udev::Udev()
0023     : m_udev(udev_new())
0024 {
0025 }
0026 
0027 Udev::~Udev()
0028 {
0029     if (m_udev) {
0030         udev_unref(m_udev);
0031     }
0032 }
0033 
0034 class UdevEnumerate
0035 {
0036 public:
0037     UdevEnumerate(Udev *udev);
0038     ~UdevEnumerate();
0039 
0040     enum class Match {
0041         SubSystem,
0042         SysName
0043     };
0044     void addMatch(Match match, const char *name);
0045     void scan();
0046     std::vector<std::unique_ptr<UdevDevice>> find();
0047 
0048 private:
0049     Udev *m_udev;
0050 
0051     struct EnumerateDeleter
0052     {
0053         void operator()(udev_enumerate *e)
0054         {
0055             udev_enumerate_unref(e);
0056         }
0057     };
0058     std::unique_ptr<udev_enumerate, EnumerateDeleter> m_enumerate;
0059 };
0060 
0061 UdevEnumerate::UdevEnumerate(Udev *udev)
0062     : m_udev(udev)
0063     , m_enumerate(udev_enumerate_new(*m_udev))
0064 {
0065 }
0066 
0067 UdevEnumerate::~UdevEnumerate() = default;
0068 
0069 void UdevEnumerate::addMatch(UdevEnumerate::Match match, const char *name)
0070 {
0071     if (!m_enumerate) {
0072         return;
0073     }
0074     switch (match) {
0075     case Match::SubSystem:
0076         udev_enumerate_add_match_subsystem(m_enumerate.get(), name);
0077         break;
0078     case Match::SysName:
0079         udev_enumerate_add_match_sysname(m_enumerate.get(), name);
0080         break;
0081     default:
0082         Q_UNREACHABLE();
0083         break;
0084     }
0085 }
0086 
0087 void UdevEnumerate::scan()
0088 {
0089     if (!m_enumerate) {
0090         return;
0091     }
0092     udev_enumerate_scan_devices(m_enumerate.get());
0093 }
0094 
0095 std::vector<std::unique_ptr<UdevDevice>> UdevEnumerate::find()
0096 {
0097     if (!m_enumerate) {
0098         return {};
0099     }
0100     std::vector<std::unique_ptr<UdevDevice>> vect;
0101     udev_list_entry *it = udev_enumerate_get_list_entry(m_enumerate.get());
0102     while (it) {
0103         auto current = it;
0104         it = udev_list_entry_get_next(it);
0105         auto device = m_udev->deviceFromSyspath(udev_list_entry_get_name(current));
0106         if (device) {
0107             vect.push_back(std::move(device));
0108         }
0109     }
0110     return vect;
0111 }
0112 
0113 std::vector<std::unique_ptr<UdevDevice>> Udev::listGPUs()
0114 {
0115     if (!m_udev) {
0116         return {};
0117     }
0118 #if defined(Q_OS_FREEBSD)
0119     std::vector<std::unique_ptr<UdevDevice>> r;
0120     r.push_back(deviceFromSyspath("/dev/dri/card0"));
0121     return r;
0122 #else
0123     UdevEnumerate enumerate(this);
0124     enumerate.addMatch(UdevEnumerate::Match::SubSystem, "drm");
0125     enumerate.addMatch(UdevEnumerate::Match::SysName, "card[0-9]");
0126     enumerate.scan();
0127     auto vect = enumerate.find();
0128     std::sort(vect.begin(), vect.end(), [](const auto &device1, const auto &device2) {
0129         // prevent usb devices from becoming the primaryGpu
0130         if (device1->isHotpluggable()) {
0131             return false;
0132         }
0133 
0134         if (device2->isHotpluggable()) {
0135             return true;
0136         }
0137 
0138         // if set as boot GPU, prefer 1
0139         if (device1->isBootVga()) {
0140             return true;
0141         }
0142         // if set as boot GPU, prefer 2
0143         if (device2->isBootVga()) {
0144             return false;
0145         }
0146         return true;
0147     });
0148     return vect;
0149 #endif
0150 }
0151 
0152 std::unique_ptr<UdevDevice> Udev::deviceFromSyspath(const char *syspath)
0153 {
0154     auto dev = udev_device_new_from_syspath(m_udev, syspath);
0155     if (!dev) {
0156         qCWarning(KWIN_CORE) << "failed to retrieve device for" << syspath << strerror(errno);
0157         return {};
0158     }
0159     return std::make_unique<UdevDevice>(dev);
0160 }
0161 
0162 std::unique_ptr<UdevMonitor> Udev::monitor()
0163 {
0164     auto m = std::make_unique<UdevMonitor>(this);
0165     if (m->isValid()) {
0166         return m;
0167     } else {
0168         return nullptr;
0169     }
0170 }
0171 
0172 UdevDevice::UdevDevice(udev_device *device)
0173     : m_device(device)
0174 {
0175     Q_ASSERT(device);
0176 }
0177 
0178 UdevDevice::~UdevDevice()
0179 {
0180     udev_device_unref(m_device);
0181 }
0182 
0183 QString UdevDevice::devNode() const
0184 {
0185     return QString::fromUtf8(udev_device_get_devnode(m_device));
0186 }
0187 
0188 dev_t UdevDevice::devNum() const
0189 {
0190     return udev_device_get_devnum(m_device);
0191 }
0192 
0193 const char *UdevDevice::property(const char *key)
0194 {
0195     return udev_device_get_property_value(m_device, key);
0196 }
0197 
0198 QMap<QByteArray, QByteArray> UdevDevice::properties() const
0199 {
0200     QMap<QByteArray, QByteArray> r;
0201     auto it = udev_device_get_properties_list_entry(m_device);
0202     auto current = it;
0203     udev_list_entry_foreach(current, it)
0204     {
0205         r.insert(udev_list_entry_get_name(current), udev_list_entry_get_value(current));
0206     }
0207     return r;
0208 }
0209 
0210 bool UdevDevice::hasProperty(const char *key, const char *value)
0211 {
0212     const char *p = property(key);
0213     if (!p) {
0214         return false;
0215     }
0216     return qstrcmp(p, value) == 0;
0217 }
0218 
0219 bool UdevDevice::isBootVga() const
0220 {
0221     auto pci = udev_device_get_parent_with_subsystem_devtype(m_device, "pci", nullptr);
0222     if (!pci) {
0223         return false;
0224     }
0225     const char *systAttrValue = udev_device_get_sysattr_value(pci, "boot_vga");
0226     return systAttrValue && qstrcmp(systAttrValue, "1") == 0;
0227 }
0228 
0229 QString UdevDevice::seat() const
0230 {
0231     QString deviceSeat = udev_device_get_property_value(m_device, "ID_SEAT");
0232     if (deviceSeat.isEmpty()) {
0233         deviceSeat = QStringLiteral("seat0");
0234     }
0235     return deviceSeat;
0236 }
0237 
0238 QString UdevDevice::action() const
0239 {
0240     return QString::fromLocal8Bit(udev_device_get_action(m_device));
0241 }
0242 
0243 bool UdevDevice::isHotpluggable() const
0244 {
0245     QString devPath = QString::fromUtf8(udev_device_get_devpath(m_device));
0246     return devPath.toLower().contains("usb");
0247 }
0248 
0249 UdevMonitor::UdevMonitor(Udev *udev)
0250     : m_monitor(udev_monitor_new_from_netlink(*udev, "udev"))
0251 {
0252 }
0253 
0254 UdevMonitor::~UdevMonitor()
0255 {
0256     if (m_monitor) {
0257         udev_monitor_unref(m_monitor);
0258     }
0259 }
0260 
0261 int UdevMonitor::fd() const
0262 {
0263     if (m_monitor) {
0264         return udev_monitor_get_fd(m_monitor);
0265     }
0266     return -1;
0267 }
0268 
0269 void UdevMonitor::filterSubsystemDevType(const char *subSystem, const char *devType)
0270 {
0271     if (!m_monitor) {
0272         return;
0273     }
0274     udev_monitor_filter_add_match_subsystem_devtype(m_monitor, subSystem, devType);
0275 }
0276 
0277 void UdevMonitor::enable()
0278 {
0279     if (!m_monitor) {
0280         return;
0281     }
0282     udev_monitor_enable_receiving(m_monitor);
0283 }
0284 
0285 std::unique_ptr<UdevDevice> UdevMonitor::getDevice()
0286 {
0287     if (!m_monitor) {
0288         return nullptr;
0289     }
0290     auto dev = udev_monitor_receive_device(m_monitor);
0291     return dev ? std::make_unique<UdevDevice>(dev) : nullptr;
0292 }
0293 
0294 }