File indexing completed on 2024-11-10 04:56:29

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2016 Roman Gilg <subdiff@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #include "drm_object.h"
0010 
0011 #include <errno.h>
0012 
0013 #include "drm_commit.h"
0014 #include "drm_gpu.h"
0015 #include "drm_logging.h"
0016 #include "drm_pointer.h"
0017 
0018 namespace KWin
0019 {
0020 
0021 DrmObject::DrmObject(DrmGpu *gpu, uint32_t objectId, uint32_t objectType)
0022     : m_gpu(gpu)
0023     , m_id(objectId)
0024     , m_objectType(objectType)
0025 {
0026 }
0027 
0028 bool DrmObject::init()
0029 {
0030     return updateProperties();
0031 }
0032 
0033 DrmPropertyList DrmObject::queryProperties() const
0034 {
0035     DrmUniquePtr<drmModeObjectProperties> properties(drmModeObjectGetProperties(m_gpu->fd(), m_id, m_objectType));
0036     if (!properties) {
0037         qCWarning(KWIN_DRM) << "Failed to get properties for object" << m_id;
0038         return {};
0039     }
0040     DrmPropertyList ret;
0041     for (uint32_t i = 0; i < properties->count_props; i++) {
0042         DrmUniquePtr<drmModePropertyRes> prop(drmModeGetProperty(m_gpu->fd(), properties->props[i]));
0043         if (!prop) {
0044             qCWarning(KWIN_DRM, "Getting property %d of object %d failed!", properties->props[i], m_id);
0045             continue;
0046         }
0047         ret.addProperty(std::move(prop), properties->prop_values[i]);
0048     }
0049     return ret;
0050 }
0051 
0052 uint32_t DrmObject::id() const
0053 {
0054     return m_id;
0055 }
0056 
0057 DrmGpu *DrmObject::gpu() const
0058 {
0059     return m_gpu;
0060 }
0061 
0062 uint32_t DrmObject::type() const
0063 {
0064     return m_objectType;
0065 }
0066 
0067 QString DrmObject::typeName() const
0068 {
0069     switch (m_objectType) {
0070     case DRM_MODE_OBJECT_CONNECTOR:
0071         return QStringLiteral("connector");
0072     case DRM_MODE_OBJECT_CRTC:
0073         return QStringLiteral("crtc");
0074     case DRM_MODE_OBJECT_PLANE:
0075         return QStringLiteral("plane");
0076     default:
0077         return QStringLiteral("unknown?");
0078     }
0079 }
0080 
0081 void DrmPropertyList::addProperty(DrmUniquePtr<drmModePropertyRes> &&prop, uint64_t value)
0082 {
0083     m_properties.push_back(std::make_pair(std::move(prop), value));
0084 }
0085 
0086 std::optional<std::pair<DrmUniquePtr<drmModePropertyRes>, uint64_t>> DrmPropertyList::takeProperty(const QByteArray &name)
0087 {
0088     const auto it = std::find_if(m_properties.begin(), m_properties.end(), [&name](const auto &pair) {
0089         return pair.first->name == name;
0090     });
0091     if (it != m_properties.end()) {
0092         auto ret = std::move(*it);
0093         m_properties.erase(it);
0094         return ret;
0095     } else {
0096         return std::nullopt;
0097     }
0098 }
0099 }
0100 
0101 QDebug operator<<(QDebug s, const KWin::DrmObject *obj)
0102 {
0103     QDebugStateSaver saver(s);
0104     if (obj) {
0105         s.nospace() << "DrmObject(id=" << obj->id() << ", gpu=" << obj->gpu() << ')';
0106     } else {
0107         s << "DrmObject(0x0)";
0108     }
0109     return s;
0110 }