1/****************************************************************************
2**
3** Copyright (C) 2017 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com>
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the plugins of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <QtCore/QFile>
41
42#include "qedidparser_p.h"
43#include "qedidvendortable_p.h"
44
45#define EDID_DESCRIPTOR_ALPHANUMERIC_STRING 0xfe
46#define EDID_DESCRIPTOR_PRODUCT_NAME 0xfc
47#define EDID_DESCRIPTOR_SERIAL_NUMBER 0xff
48
49#define EDID_OFFSET_DATA_BLOCKS 0x36
50#define EDID_OFFSET_LAST_BLOCK 0x6c
51#define EDID_OFFSET_PNP_ID 0x08
52#define EDID_OFFSET_SERIAL 0x0c
53#define EDID_PHYSICAL_WIDTH 0x15
54#define EDID_OFFSET_PHYSICAL_HEIGHT 0x16
55
56QT_BEGIN_NAMESPACE
57
58QEdidParser::QEdidParser()
59{
60 // Cache vendors list from pnp.ids
61 const QString fileName = QLatin1String("/usr/share/hwdata/pnp.ids");
62 if (QFile::exists(fileName)) {
63 QFile file(fileName);
64
65 if (file.open(flags: QFile::ReadOnly)) {
66 while (!file.atEnd()) {
67 QString line = QString::fromUtf8(str: file.readLine()).trimmed();
68
69 if (line.startsWith(c: QLatin1Char('#')))
70 continue;
71
72 QStringList parts = line.split(sep: QLatin1Char('\t'));
73 if (parts.count() > 1) {
74 QString pnpId = parts.at(i: 0);
75 parts.removeFirst();
76 m_vendorCache[pnpId] = parts.join(sep: QLatin1Char(' '));
77 }
78 }
79
80 file.close();
81 }
82 }
83}
84
85bool QEdidParser::parse(const QByteArray &blob)
86{
87 const quint8 *data = reinterpret_cast<const quint8 *>(blob.constData());
88 const size_t length = blob.length();
89
90 // Verify header
91 if (length < 128)
92 return false;
93 if (data[0] != 0x00 || data[1] != 0xff)
94 return false;
95
96 /* Decode the PNP ID from three 5 bit words packed into 2 bytes
97 * /--08--\/--09--\
98 * 7654321076543210
99 * |\---/\---/\---/
100 * R C1 C2 C3 */
101 char pnpId[3];
102 pnpId[0] = 'A' + ((data[EDID_OFFSET_PNP_ID] & 0x7c) / 4) - 1;
103 pnpId[1] = 'A' + ((data[EDID_OFFSET_PNP_ID] & 0x3) * 8) + ((data[EDID_OFFSET_PNP_ID + 1] & 0xe0) / 32) - 1;
104 pnpId[2] = 'A' + (data[EDID_OFFSET_PNP_ID + 1] & 0x1f) - 1;
105 QString pnpIdString = QString::fromLatin1(str: pnpId, size: 3);
106
107 // Clear manufacturer
108 manufacturer = QString();
109
110 // Serial number, will be overwritten by an ASCII descriptor
111 // when and if it will be found
112 quint32 serial = data[EDID_OFFSET_SERIAL]
113 + (data[EDID_OFFSET_SERIAL + 1] << 8)
114 + (data[EDID_OFFSET_SERIAL + 2] << 16)
115 + (data[EDID_OFFSET_SERIAL + 3] << 24);
116 if (serial > 0)
117 serialNumber = QString::number(serial);
118 else
119 serialNumber = QString();
120
121 // Parse EDID data
122 for (int i = 0; i < 5; ++i) {
123 const uint offset = EDID_OFFSET_DATA_BLOCKS + i * 18;
124
125 if (data[offset] != 0 || data[offset + 1] != 0 || data[offset + 2] != 0)
126 continue;
127
128 if (data[offset + 3] == EDID_DESCRIPTOR_PRODUCT_NAME)
129 model = parseEdidString(data: &data[offset + 5]);
130 else if (data[offset + 3] == EDID_DESCRIPTOR_ALPHANUMERIC_STRING)
131 identifier = parseEdidString(data: &data[offset + 5]);
132 else if (data[offset + 3] == EDID_DESCRIPTOR_SERIAL_NUMBER)
133 serialNumber = parseEdidString(data: &data[offset + 5]);
134 }
135
136 // Try to use cache first because it is potentially more updated
137 manufacturer = m_vendorCache.value(akey: pnpIdString);
138 if (manufacturer.isEmpty()) {
139 // Find the manufacturer from the vendor lookup table
140 for (const auto &vendor : q_edidVendorTable) {
141 if (strncmp(s1: vendor.id, s2: pnpId, n: 3) == 0) {
142 manufacturer = QString::fromUtf8(str: vendor.name);
143 break;
144 }
145 }
146 }
147
148 // If we don't know the manufacturer, fallback to PNP ID
149 if (manufacturer.isEmpty())
150 manufacturer = pnpIdString;
151
152 // Physical size
153 physicalSize = QSizeF(data[EDID_PHYSICAL_WIDTH], data[EDID_OFFSET_PHYSICAL_HEIGHT]) * 10;
154
155 return true;
156}
157
158QString QEdidParser::parseEdidString(const quint8 *data)
159{
160 QByteArray buffer(reinterpret_cast<const char *>(data), 13);
161
162 // Erase carriage return and line feed
163 buffer = buffer.replace(before: '\r', after: '\0').replace(before: '\n', after: '\0');
164
165 // Replace non-printable characters with dash
166 for (int i = 0; i < buffer.count(); ++i) {
167 if (buffer[i] < '\040' || buffer[i] > '\176')
168 buffer[i] = '-';
169 }
170
171 return QString::fromLatin1(str: buffer.trimmed());
172}
173
174QT_END_NAMESPACE
175

source code of qtbase/src/platformsupport/edid/qedidparser.cpp