diff --git a/plugins/platforms/drm/CMakeLists.txt b/plugins/platforms/drm/CMakeLists.txt --- a/plugins/platforms/drm/CMakeLists.txt +++ b/plugins/platforms/drm/CMakeLists.txt @@ -7,6 +7,7 @@ drm_output.cpp drm_buffer.cpp drm_inputeventfilter.cpp + edid.cpp logging.cpp scene_qpainter_drm_backend.cpp screens_drm.cpp diff --git a/plugins/platforms/drm/drm_output.h b/plugins/platforms/drm/drm_output.h --- a/plugins/platforms/drm/drm_output.h +++ b/plugins/platforms/drm/drm_output.h @@ -24,6 +24,7 @@ #include "drm_pointer.h" #include "drm_object.h" #include "drm_object_plane.h" +#include "edid.h" #include #include @@ -45,12 +46,6 @@ { Q_OBJECT public: - struct Edid { - QByteArray eisaId; - QByteArray monitorName; - QByteArray serialNumber; - QSize physicalSize; - }; ///deletes the output, calling this whilst a page flip is pending will result in an error ~DrmOutput() override; ///queues deleting the output after a page flip has completed. diff --git a/plugins/platforms/drm/drm_output.cpp b/plugins/platforms/drm/drm_output.cpp --- a/plugins/platforms/drm/drm_output.cpp +++ b/plugins/platforms/drm/drm_output.cpp @@ -266,16 +266,16 @@ ); } - QSize physicalSize = !m_edid.physicalSize.isEmpty() ? m_edid.physicalSize : QSize(connector->mmWidth, connector->mmHeight); + QSize physicalSize = !m_edid.physicalSize().isEmpty() ? m_edid.physicalSize() : QSize(connector->mmWidth, connector->mmHeight); // the size might be completely borked. E.g. Samsung SyncMaster 2494HS reports 160x90 while in truth it's 520x292 // as this information is used to calculate DPI info, it's going to result in everything being huge const QByteArray unknown = QByteArrayLiteral("unknown"); - KConfigGroup group = kwinApp()->config()->group("EdidOverwrite").group(m_edid.eisaId.isEmpty() ? unknown : m_edid.eisaId) - .group(m_edid.monitorName.isEmpty() ? unknown : m_edid.monitorName) - .group(m_edid.serialNumber.isEmpty() ? unknown : m_edid.serialNumber); + KConfigGroup group = kwinApp()->config()->group("EdidOverwrite").group(m_edid.eisaId().isEmpty() ? unknown : m_edid.eisaId()) + .group(m_edid.monitorName().isEmpty() ? unknown : m_edid.monitorName()) + .group(m_edid.serialNumber().isEmpty() ? unknown : m_edid.serialNumber()); if (group.hasKey("PhysicalSize")) { const QSize overwriteSize = group.readEntry("PhysicalSize", physicalSize); - qCWarning(KWIN_DRM) << "Overwriting monitor physical size for" << m_edid.eisaId << "/" << m_edid.monitorName << "/" << m_edid.serialNumber << " from " << physicalSize << "to " << overwriteSize; + qCWarning(KWIN_DRM) << "Overwriting monitor physical size for" << m_edid.eisaId() << "/" << m_edid.monitorName() << "/" << m_edid.serialNumber() << " from " << physicalSize << "to " << overwriteSize; physicalSize = overwriteSize; } setRawPhysicalSize(physicalSize); @@ -290,31 +290,31 @@ { QCryptographicHash hash(QCryptographicHash::Md5); hash.addData(QByteArray::number(m_conn->id())); - hash.addData(m_edid.eisaId); - hash.addData(m_edid.monitorName); - hash.addData(m_edid.serialNumber); + hash.addData(m_edid.eisaId()); + hash.addData(m_edid.monitorName()); + hash.addData(m_edid.serialNumber()); m_uuid = hash.result().toHex().left(10); } void DrmOutput::initOutputDevice(drmModeConnector *connector) { QString manufacturer; - if (!m_edid.eisaId.isEmpty()) { - manufacturer = QString::fromLatin1(m_edid.eisaId); + if (!m_edid.eisaId().isEmpty()) { + manufacturer = QString::fromLatin1(m_edid.eisaId()); } QString connectorName = s_connectorNames.value(connector->connector_type, QByteArrayLiteral("Unknown")); QString modelName; - if (!m_edid.monitorName.isEmpty()) { - QString m = QString::fromLatin1(m_edid.monitorName); - if (!m_edid.serialNumber.isEmpty()) { + if (!m_edid.monitorName().isEmpty()) { + QString m = QString::fromLatin1(m_edid.monitorName()); + if (!m_edid.serialNumber().isEmpty()) { m.append('/'); - m.append(QString::fromLatin1(m_edid.serialNumber)); + m.append(QString::fromLatin1(m_edid.serialNumber())); } modelName = m; - } else if (!m_edid.serialNumber.isEmpty()) { - modelName = QString::fromLatin1(m_edid.serialNumber); + } else if (!m_edid.serialNumber().isEmpty()) { + modelName = QString::fromLatin1(m_edid.serialNumber()); } else { modelName = i18n("unknown"); } @@ -365,133 +365,6 @@ && qstrcmp(mode->name, m_mode.name) == 0; } -static bool verifyEdidHeader(drmModePropertyBlobPtr edid) -{ - const uint8_t *data = reinterpret_cast(edid->data); - if (data[0] != 0x00) { - return false; - } - for (int i = 1; i < 7; ++i) { - if (data[i] != 0xFF) { - return false; - } - } - if (data[7] != 0x00) { - return false; - } - return true; -} - -static QByteArray extractEisaId(drmModePropertyBlobPtr edid) -{ - /* - * From EDID standard section 3.4: - * The ID Manufacturer Name field, shown in Table 3.5, contains a 2-byte representation of the monitor's - * manufacturer. This is the same as the EISA ID. It is based on compressed ASCII, “0001=A” ... “11010=Z”. - * - * The table: - * | Byte | Bit | - * | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | - * ---------------------------------------- - * | 1 | 0)| (4| 3 | 2 | 1 | 0)| (4| 3 | - * | | * | Character 1 | Char 2| - * ---------------------------------------- - * | 2 | 2 | 1 | 0)| (4| 3 | 2 | 1 | 0)| - * | | Character2| Character 3 | - * ---------------------------------------- - **/ - const uint8_t *data = reinterpret_cast(edid->data); - static const uint offset = 0x8; - char id[4]; - if (data[offset] >> 7) { - // bit at position 7 is not a 0 - return QByteArray(); - } - // shift two bits to right, and with 7 right most bits - id[0] = 'A' + ((data[offset] >> 2) & 0x1f) -1; - // for first byte: take last two bits and shift them 3 to left (000xx000) - // for second byte: shift 5 bits to right and take 3 right most bits (00000xxx) - // or both together - id[1] = 'A' + (((data[offset] & 0x3) << 3) | ((data[offset + 1] >> 5) & 0x7)) - 1; - // take five right most bits - id[2] = 'A' + (data[offset + 1] & 0x1f) - 1; - id[3] = '\0'; - return QByteArray(id); -} - -static void extractMonitorDescriptorDescription(drmModePropertyBlobPtr blob, DrmOutput::Edid &edid) -{ - // see section 3.10.3 - const uint8_t *data = reinterpret_cast(blob->data); - static const uint offset = 0x36; - static const uint blockLength = 18; - for (int i = 0; i < 5; ++i) { - const uint co = offset + i * blockLength; - // Flag = 0000h when block used as descriptor - if (data[co] != 0) { - continue; - } - if (data[co + 1] != 0) { - continue; - } - // Reserved = 00h when block used as descriptor - if (data[co + 2] != 0) { - continue; - } - /* - * FFh: Monitor Serial Number - Stored as ASCII, code page # 437, ≤ 13 bytes. - * FEh: ASCII String - Stored as ASCII, code page # 437, ≤ 13 bytes. - * FDh: Monitor range limits, binary coded - * FCh: Monitor name, stored as ASCII, code page # 437 - * FBh: Descriptor contains additional color point data - * FAh: Descriptor contains additional Standard Timing Identifications - * F9h - 11h: Currently undefined - * 10h: Dummy descriptor, used to indicate that the descriptor space is unused - * 0Fh - 00h: Descriptor defined by manufacturer. - */ - if (data[co + 3] == 0xfc && edid.monitorName.isEmpty()) { - edid.monitorName = QByteArray((const char *)(&data[co + 5]), 12).trimmed(); - } - if (data[co + 3] == 0xfe) { - const QByteArray id = QByteArray((const char *)(&data[co + 5]), 12).trimmed(); - if (!id.isEmpty()) { - edid.eisaId = id; - } - } - if (data[co + 3] == 0xff) { - edid.serialNumber = QByteArray((const char *)(&data[co + 5]), 12).trimmed(); - } - } -} - -static QByteArray extractSerialNumber(drmModePropertyBlobPtr edid) -{ - // see section 3.4 - const uint8_t *data = reinterpret_cast(edid->data); - static const uint offset = 0x0C; - /* - * The ID serial number is a 32-bit serial number used to differentiate between individual instances of the same model - * of monitor. Its use is optional. When used, the bit order for this field follows that shown in Table 3.6. The EDID - * structure Version 1 Revision 1 and later offer a way to represent the serial number of the monitor as an ASCII string - * in a separate descriptor block. - */ - uint32_t serialNumber = 0; - serialNumber = (uint32_t) data[offset + 0]; - serialNumber |= (uint32_t) data[offset + 1] << 8; - serialNumber |= (uint32_t) data[offset + 2] << 16; - serialNumber |= (uint32_t) data[offset + 3] << 24; - if (serialNumber == 0) { - return QByteArray(); - } - return QByteArray::number(serialNumber); -} - -static QSize extractPhysicalSize(drmModePropertyBlobPtr edid) -{ - const uint8_t *data = reinterpret_cast(edid->data); - return QSize(data[0x15], data[0x16]) * 10; -} - void DrmOutput::initEdid(drmModeConnector *connector) { DrmScopedPointer edid; @@ -508,20 +381,10 @@ return; } - // for documentation see: http://read.pudn.com/downloads110/ebook/456020/E-EDID%20Standard.pdf - if (edid->length < 128) { - return; + m_edid = Edid(edid->data, edid->length); + if (!m_edid.isValid()) { + qCWarning(KWIN_DRM, "Couldn't parse EDID for connector with id %d", m_conn->id()); } - if (!verifyEdidHeader(edid.data())) { - return; - } - m_edid.eisaId = extractEisaId(edid.data()); - m_edid.serialNumber = extractSerialNumber(edid.data()); - - // parse monitor descriptor description - extractMonitorDescriptorDescription(edid.data(), m_edid); - - m_edid.physicalSize = extractPhysicalSize(edid.data()); } bool DrmOutput::initPrimaryPlane() diff --git a/plugins/platforms/drm/edid.h b/plugins/platforms/drm/edid.h new file mode 100644 --- /dev/null +++ b/plugins/platforms/drm/edid.h @@ -0,0 +1,73 @@ +/******************************************************************** + KWin - the KDE window manager + This file is part of the KDE project. + +Copyright (C) 2019 Vlad Zagorodniy + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*********************************************************************/ + +#pragma once + +#include +#include + +namespace KWin +{ + +/** + * Helper class that can be used for parsing EDID blobs. + * + * http://read.pudn.com/downloads110/ebook/456020/E-EDID%20Standard.pdf + **/ +class Edid +{ +public: + Edid(); + Edid(const void *data, uint32_t size); + + /** + * Whether this instance of EDID is valid. + **/ + bool isValid() const; + + /** + * Returns physical dimensions of the monitor, in millimeters. + **/ + QSize physicalSize() const; + + /** + * Returns EISA ID of the manufacturer of the monitor. + **/ + QByteArray eisaId() const; + + /** + * Returns the product name of the monitor. + **/ + QByteArray monitorName() const; + + /** + * Returns the serial number of the monitor. + **/ + QByteArray serialNumber() const; + +private: + QSize m_physicalSize; + QByteArray m_eisaId; + QByteArray m_monitorName; + QByteArray m_serialNumber; + bool m_isValid = false; +}; + +} // namespace KWin diff --git a/plugins/platforms/drm/edid.cpp b/plugins/platforms/drm/edid.cpp new file mode 100644 --- /dev/null +++ b/plugins/platforms/drm/edid.cpp @@ -0,0 +1,183 @@ +/******************************************************************** + KWin - the KDE window manager + This file is part of the KDE project. + +Copyright (C) 2015 Martin Flöser +Copyright (C) 2019 Vlad Zagorodniy + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*********************************************************************/ + +#include "edid.h" + +namespace KWin +{ + +static bool verifyHeader(const uint8_t *data) +{ + if (data[0] != 0x0 || data[7] != 0x0) { + return false; + } + + return std::all_of(data + 1, data + 7, + [](uint8_t byte) { return byte == 0xff; }); +} + +static QSize parsePhysicalSize(const uint8_t *data) +{ + // Convert physical size from centimeters to millimeters. + return QSize(data[0x15], data[0x16]) * 10; +} + +static QByteArray parseEisaId(const uint8_t *data) +{ + for (int i = 72; i <= 108; i += 18) { + // Skip the block if it isn't used as monitor descriptor. + if (data[i]) { + continue; + } + if (data[i + 1]) { + continue; + } + + // We have found the EISA ID, it's stored as ASCII. + if (data[i + 3] == 0xfe) { + return QByteArray(reinterpret_cast(&data[i + 5]), 12).trimmed(); + } + } + + // If there isn't an ASCII EISA ID descriptor, try to decode PNP ID from + // three 5 bit words packed into 2 bytes: + // + // | Byte | Bit | + // | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | + // ---------------------------------------- + // | 1 | 0)| (4| 3 | 2 | 1 | 0)| (4| 3 | + // | | * | Character 1 | Char 2| + // ---------------------------------------- + // | 2 | 2 | 1 | 0)| (4| 3 | 2 | 1 | 0)| + // | | Character2| Character 3 | + // ---------------------------------------- + const uint offset = 0x8; + + char pnpId[4]; + pnpId[0] = 'A' + ((data[offset + 0] >> 2) & 0x1f) - 1; + pnpId[1] = 'A' + (((data[offset + 0] & 0x3) << 3) | ((data[offset + 1] >> 5) & 0x7)) - 1; + pnpId[2] = 'A' + (data[offset + 1] & 0x1f) - 1; + pnpId[3] = '\0'; + + return QByteArray(pnpId); +} + +static QByteArray parseMonitorName(const uint8_t *data) +{ + for (int i = 72; i <= 108; i += 18) { + // Skip the block if it isn't used as monitor descriptor. + if (data[i]) { + continue; + } + if (data[i + 1]) { + continue; + } + + // We have found the monitor name, it's stored as ASCII. + if (data[i + 3] == 0xfc) { + return QByteArray(reinterpret_cast(&data[i + 5]), 12).trimmed(); + } + } + + return QByteArray(); +} + +static QByteArray parseSerialNumber(const uint8_t *data) +{ + for (int i = 72; i <= 108; i += 18) { + // Skip the block if it isn't used as monitor descriptor. + if (data[i]) { + continue; + } + if (data[i + 1]) { + continue; + } + + // We have found the serial number, it's stored as ASCII. + if (data[i + 3] == 0xff) { + return QByteArray(reinterpret_cast(&data[i + 5]), 12).trimmed(); + } + } + + // Maybe there isn't an ASCII serial number descriptor, so use this instead. + const uint32_t offset = 0xc; + + uint32_t serialNumber = data[offset + 0]; + serialNumber |= uint32_t(data[offset + 1]) << 8; + serialNumber |= uint32_t(data[offset + 2]) << 16; + serialNumber |= uint32_t(data[offset + 3]) << 24; + if (serialNumber) { + return QByteArray::number(serialNumber); + } + + return QByteArray(); +} + +Edid::Edid() +{ +} + +Edid::Edid(const void *data, uint32_t size) +{ + const uint8_t *bytes = static_cast(data); + + if (size < 128) { + return; + } + + if (!verifyHeader(bytes)) { + return; + } + + m_physicalSize = parsePhysicalSize(bytes); + m_eisaId = parseEisaId(bytes); + m_monitorName = parseMonitorName(bytes); + m_serialNumber = parseSerialNumber(bytes); + + m_isValid = true; +} + +bool Edid::isValid() const +{ + return m_isValid; +} + +QSize Edid::physicalSize() const +{ + return m_physicalSize; +} + +QByteArray Edid::eisaId() const +{ + return m_eisaId; +} + +QByteArray Edid::monitorName() const +{ + return m_monitorName; +} + +QByteArray Edid::serialNumber() const +{ + return m_serialNumber; +} + +} // namespace KWin