2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2015-04-08 08:25:51 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
|
2015-04-08 08:25:51 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2015-04-08 08:25:51 +00:00
|
|
|
#include "udev.h"
|
2018-04-05 17:47:15 +00:00
|
|
|
#include "logind.h"
|
2015-04-08 08:45:52 +00:00
|
|
|
// Qt
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QScopedPointer>
|
|
|
|
// system
|
2015-04-08 08:25:51 +00:00
|
|
|
#include <libudev.h>
|
2015-04-08 08:45:52 +00:00
|
|
|
#include <functional>
|
2015-04-08 08:25:51 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
Udev::Udev()
|
|
|
|
: m_udev(udev_new())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Udev::~Udev()
|
|
|
|
{
|
|
|
|
if (m_udev) {
|
|
|
|
udev_unref(m_udev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 08:45:52 +00:00
|
|
|
class UdevEnumerate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UdevEnumerate(Udev *udev);
|
|
|
|
~UdevEnumerate();
|
|
|
|
|
|
|
|
enum class Match {
|
|
|
|
SubSystem,
|
|
|
|
SysName
|
|
|
|
};
|
|
|
|
void addMatch(Match match, const char *name);
|
|
|
|
void scan();
|
2020-10-05 21:05:55 +00:00
|
|
|
std::vector<UdevDevice::Ptr> find();
|
2015-04-08 08:45:52 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Udev *m_udev;
|
|
|
|
|
|
|
|
struct EnumerateDeleter
|
|
|
|
{
|
|
|
|
static inline void cleanup(udev_enumerate *e)
|
|
|
|
{
|
|
|
|
udev_enumerate_unref(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
QScopedPointer<udev_enumerate, EnumerateDeleter> m_enumerate;
|
|
|
|
};
|
|
|
|
|
|
|
|
UdevEnumerate::UdevEnumerate(Udev *udev)
|
|
|
|
: m_udev(udev)
|
|
|
|
, m_enumerate(udev_enumerate_new(*m_udev))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UdevEnumerate::~UdevEnumerate() = default;
|
|
|
|
|
|
|
|
void UdevEnumerate::addMatch(UdevEnumerate::Match match, const char *name)
|
|
|
|
{
|
|
|
|
if (m_enumerate.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (match) {
|
|
|
|
case Match::SubSystem:
|
|
|
|
udev_enumerate_add_match_subsystem(m_enumerate.data(), name);
|
|
|
|
break;
|
|
|
|
case Match::SysName:
|
|
|
|
udev_enumerate_add_match_sysname(m_enumerate.data(), name);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UdevEnumerate::scan()
|
|
|
|
{
|
|
|
|
if (m_enumerate.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
udev_enumerate_scan_devices(m_enumerate.data());
|
|
|
|
}
|
|
|
|
|
2020-10-05 21:05:55 +00:00
|
|
|
std::vector<UdevDevice::Ptr> UdevEnumerate::find()
|
2015-04-08 08:45:52 +00:00
|
|
|
{
|
2020-10-05 21:05:55 +00:00
|
|
|
std::vector<UdevDevice::Ptr> vect;
|
2015-04-08 08:45:52 +00:00
|
|
|
if (m_enumerate.isNull()) {
|
2020-10-05 21:05:55 +00:00
|
|
|
vect.push_back( UdevDevice::Ptr() );
|
|
|
|
return vect;
|
2015-04-08 08:45:52 +00:00
|
|
|
}
|
2018-04-05 17:47:15 +00:00
|
|
|
QString defaultSeat = QStringLiteral("seat0");
|
2015-04-08 08:45:52 +00:00
|
|
|
udev_list_entry *it = udev_enumerate_get_list_entry(m_enumerate.data());
|
|
|
|
while (it) {
|
|
|
|
auto current = it;
|
|
|
|
it = udev_list_entry_get_next(it);
|
|
|
|
auto device = m_udev->deviceFromSyspath(udev_list_entry_get_name(current));
|
|
|
|
if (!device) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-05 17:47:15 +00:00
|
|
|
QString deviceSeat = device->property("ID_SEAT");
|
|
|
|
if (deviceSeat.isEmpty()) {
|
|
|
|
deviceSeat = defaultSeat;
|
|
|
|
}
|
|
|
|
if (deviceSeat != LogindIntegration::self()->seat()) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-12 18:04:11 +00:00
|
|
|
vect.push_back(std::move(device));
|
2015-04-08 08:45:52 +00:00
|
|
|
}
|
2020-10-05 21:05:55 +00:00
|
|
|
return vect;
|
2015-04-08 08:45:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 21:05:55 +00:00
|
|
|
std::vector<UdevDevice::Ptr> Udev::listGPUs()
|
2015-04-08 08:45:52 +00:00
|
|
|
{
|
|
|
|
if (!m_udev) {
|
2020-10-05 21:05:55 +00:00
|
|
|
std::vector<UdevDevice::Ptr> vect;
|
|
|
|
vect.push_back(UdevDevice::Ptr());
|
|
|
|
return vect;
|
2015-04-08 08:45:52 +00:00
|
|
|
}
|
2020-05-31 21:52:28 +00:00
|
|
|
#if defined(Q_OS_FREEBSD)
|
2020-10-05 21:05:55 +00:00
|
|
|
std::vector<UdevDevice::Ptr> r;
|
|
|
|
r.push_back(deviceFromSyspath("/dev/dri/card0"));
|
|
|
|
return r;
|
2020-05-31 21:52:28 +00:00
|
|
|
#else
|
2015-04-08 08:45:52 +00:00
|
|
|
UdevEnumerate enumerate(this);
|
|
|
|
enumerate.addMatch(UdevEnumerate::Match::SubSystem, "drm");
|
2020-10-05 21:05:55 +00:00
|
|
|
enumerate.addMatch(UdevEnumerate::Match::SysName, "card[0-9]");
|
2015-04-08 08:45:52 +00:00
|
|
|
enumerate.scan();
|
2020-10-05 21:05:55 +00:00
|
|
|
auto vect = enumerate.find();
|
|
|
|
std::sort(vect.begin(), vect.end(), [](const UdevDevice::Ptr &device1, const UdevDevice::Ptr &device2) {
|
|
|
|
auto pci1 = device1->getParentWithSubsystemDevType("pci");
|
|
|
|
auto pci2 = device2->getParentWithSubsystemDevType("pci");
|
2020-10-12 18:04:11 +00:00
|
|
|
const char *systAttrValue;
|
2020-10-05 21:05:55 +00:00
|
|
|
// if set as boot GPU, prefer 1
|
2020-10-12 18:04:11 +00:00
|
|
|
if (pci1) {
|
|
|
|
systAttrValue = udev_device_get_sysattr_value(pci1, "boot_vga");
|
|
|
|
if (systAttrValue && qstrcmp(systAttrValue, "1") == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-04-08 08:45:52 +00:00
|
|
|
}
|
2020-10-05 21:05:55 +00:00
|
|
|
// if set as boot GPU, prefer 2
|
2020-10-12 18:04:11 +00:00
|
|
|
if (pci2) {
|
|
|
|
systAttrValue = udev_device_get_sysattr_value(pci2, "boot_vga");
|
|
|
|
if (systAttrValue && qstrcmp(systAttrValue, "1") == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-05 21:05:55 +00:00
|
|
|
}
|
2020-10-12 18:04:11 +00:00
|
|
|
return true;
|
2015-04-08 08:45:52 +00:00
|
|
|
});
|
2020-10-05 21:05:55 +00:00
|
|
|
return vect;
|
2020-05-31 21:52:28 +00:00
|
|
|
#endif
|
2015-04-08 08:45:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 21:05:55 +00:00
|
|
|
std::vector<UdevDevice::Ptr> Udev::listFramebuffers()
|
2018-04-05 17:43:34 +00:00
|
|
|
{
|
|
|
|
if (!m_udev) {
|
2020-10-05 21:05:55 +00:00
|
|
|
std::vector<UdevDevice::Ptr> vect;
|
|
|
|
vect.push_back(UdevDevice::Ptr());
|
|
|
|
return vect;
|
2018-04-05 17:43:34 +00:00
|
|
|
}
|
|
|
|
UdevEnumerate enumerate(this);
|
|
|
|
enumerate.addMatch(UdevEnumerate::Match::SubSystem, "graphics");
|
2020-10-05 21:05:55 +00:00
|
|
|
enumerate.addMatch(UdevEnumerate::Match::SysName, "fb[0-9]");
|
2018-04-05 17:43:34 +00:00
|
|
|
enumerate.scan();
|
2020-10-05 21:05:55 +00:00
|
|
|
auto vect = enumerate.find();
|
|
|
|
std::sort(vect.begin(), vect.end(), [](const UdevDevice::Ptr &device1, const UdevDevice::Ptr &device2) {
|
|
|
|
auto pci1 = device1->getParentWithSubsystemDevType("pci");
|
|
|
|
auto pci2 = device2->getParentWithSubsystemDevType("pci");
|
2020-10-12 18:04:11 +00:00
|
|
|
const char *systAttrValue;
|
2020-10-05 21:05:55 +00:00
|
|
|
// if set as boot GPU, prefer 1
|
2020-10-12 18:04:11 +00:00
|
|
|
if (pci1) {
|
|
|
|
systAttrValue = udev_device_get_sysattr_value(pci1, "boot_vga");
|
|
|
|
if (systAttrValue && qstrcmp(systAttrValue, "1") == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-05 17:43:34 +00:00
|
|
|
}
|
2020-10-05 21:05:55 +00:00
|
|
|
// if set as boot GPU, prefer 2
|
2020-10-12 18:04:11 +00:00
|
|
|
if (pci2) {
|
|
|
|
systAttrValue = udev_device_get_sysattr_value(pci2, "boot_vga");
|
|
|
|
if (systAttrValue && qstrcmp(systAttrValue, "1") == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-05 21:05:55 +00:00
|
|
|
}
|
2020-10-12 18:04:11 +00:00
|
|
|
return true;
|
2018-04-05 17:43:34 +00:00
|
|
|
});
|
2020-10-05 21:05:55 +00:00
|
|
|
return vect;
|
2018-04-05 17:43:34 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 08:45:52 +00:00
|
|
|
UdevDevice::Ptr Udev::deviceFromSyspath(const char *syspath)
|
|
|
|
{
|
2017-07-31 16:24:05 +00:00
|
|
|
return UdevDevice::Ptr(new UdevDevice(udev_device_new_from_syspath(m_udev, syspath)));
|
2015-04-08 08:45:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 13:08:24 +00:00
|
|
|
UdevMonitor *Udev::monitor()
|
|
|
|
{
|
|
|
|
UdevMonitor *m = new UdevMonitor(this);
|
|
|
|
if (!m->isValid()) {
|
|
|
|
delete m;
|
|
|
|
m = nullptr;
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2015-04-08 08:45:52 +00:00
|
|
|
UdevDevice::UdevDevice(udev_device *device)
|
|
|
|
: m_device(device)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UdevDevice::~UdevDevice()
|
|
|
|
{
|
|
|
|
if (m_device) {
|
|
|
|
udev_device_unref(m_device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
udev_device *UdevDevice::getParentWithSubsystemDevType(const char *subsystem, const char *devtype) const
|
|
|
|
{
|
|
|
|
if (!m_device) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return udev_device_get_parent_with_subsystem_devtype(m_device, subsystem, devtype);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *UdevDevice::devNode()
|
|
|
|
{
|
|
|
|
if (!m_device) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return udev_device_get_devnode(m_device);
|
|
|
|
}
|
|
|
|
|
2015-04-09 12:49:32 +00:00
|
|
|
int UdevDevice::sysNum() const
|
|
|
|
{
|
|
|
|
if (!m_device) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return QByteArray(udev_device_get_sysnum(m_device)).toInt();
|
|
|
|
}
|
|
|
|
|
2015-04-23 13:07:30 +00:00
|
|
|
const char *UdevDevice::property(const char *key)
|
|
|
|
{
|
|
|
|
if (!m_device) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return udev_device_get_property_value(m_device, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UdevDevice::hasProperty(const char *key, const char *value)
|
|
|
|
{
|
|
|
|
const char *p = property(key);
|
|
|
|
if (!p) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return qstrcmp(p, value) == 0;
|
|
|
|
}
|
|
|
|
|
2015-04-23 13:08:24 +00:00
|
|
|
UdevMonitor::UdevMonitor(Udev *udev)
|
2017-07-31 16:22:14 +00:00
|
|
|
: m_monitor(udev_monitor_new_from_netlink(*udev, "udev"))
|
2015-04-23 13:08:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UdevMonitor::~UdevMonitor()
|
|
|
|
{
|
|
|
|
if (m_monitor) {
|
|
|
|
udev_monitor_unref(m_monitor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int UdevMonitor::fd() const
|
|
|
|
{
|
|
|
|
if (m_monitor) {
|
|
|
|
return udev_monitor_get_fd(m_monitor);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UdevMonitor::filterSubsystemDevType(const char *subSystem, const char *devType)
|
|
|
|
{
|
|
|
|
if (!m_monitor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(m_monitor, subSystem, devType);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UdevMonitor::enable()
|
|
|
|
{
|
|
|
|
if (!m_monitor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
udev_monitor_enable_receiving(m_monitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
UdevDevice::Ptr UdevMonitor::getDevice()
|
|
|
|
{
|
|
|
|
if (!m_monitor) {
|
2017-07-31 16:24:05 +00:00
|
|
|
return UdevDevice::Ptr();
|
2015-04-23 13:08:24 +00:00
|
|
|
}
|
2017-07-31 16:24:05 +00:00
|
|
|
return UdevDevice::Ptr(new UdevDevice(udev_monitor_receive_device(m_monitor)));
|
2015-04-23 13:08:24 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 08:25:51 +00:00
|
|
|
}
|