Changeset View
Changeset View
Standalone View
Standalone View
plugins/platforms/drm/edid.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | KWin - the KDE window manager | ||||
3 | This file is part of the KDE project. | ||||
4 | | ||||
5 | Copyright (C) 2015 Martin Flöser <mgraesslin@kde.org> | ||||
6 | Copyright (C) 2019 Vlad Zagorodniy <vladzzag@gmail.com> | ||||
7 | | ||||
8 | This program is free software; you can redistribute it and/or modify | ||||
9 | it under the terms of the GNU General Public License as published by | ||||
10 | the Free Software Foundation; either version 2 of the License, or | ||||
11 | (at your option) any later version. | ||||
12 | | ||||
13 | This program is distributed in the hope that it will be useful, | ||||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
16 | GNU General Public License for more details. | ||||
17 | | ||||
18 | You should have received a copy of the GNU General Public License | ||||
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
20 | *********************************************************************/ | ||||
21 | | ||||
22 | #include "edid.h" | ||||
23 | | ||||
24 | namespace KWin | ||||
25 | { | ||||
26 | | ||||
27 | static bool verifyHeader(const uint8_t *data) | ||||
28 | { | ||||
29 | if (data[0] != 0x0 || data[7] != 0x0) { | ||||
30 | return false; | ||||
31 | } | ||||
32 | | ||||
33 | return std::all_of(data + 1, data + 7, | ||||
34 | [](uint8_t byte) { return byte == 0xff; }); | ||||
35 | } | ||||
36 | | ||||
37 | static QSize parsePhysicalSize(const uint8_t *data) | ||||
38 | { | ||||
39 | // Convert physical size from centimeters to millimeters. | ||||
40 | return QSize(data[0x15], data[0x16]) * 10; | ||||
41 | } | ||||
42 | | ||||
43 | static QByteArray parseEisaId(const uint8_t *data) | ||||
44 | { | ||||
45 | for (int i = 72; i <= 108; i += 18) { | ||||
46 | // Skip the block if it isn't used as monitor descriptor. | ||||
47 | if (data[i]) { | ||||
48 | continue; | ||||
49 | } | ||||
50 | if (data[i + 1]) { | ||||
51 | continue; | ||||
52 | } | ||||
53 | | ||||
54 | // We have found the EISA ID, it's stored as ASCII. | ||||
55 | if (data[i + 3] == 0xfe) { | ||||
56 | return QByteArray(reinterpret_cast<const char *>(&data[i + 5]), 12).trimmed(); | ||||
57 | } | ||||
58 | } | ||||
59 | | ||||
60 | // If there isn't an ASCII EISA ID descriptor, try to decode PNP ID from | ||||
61 | // three 5 bit words packed into 2 bytes: | ||||
62 | // | ||||
63 | // | Byte | Bit | | ||||
64 | // | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | | ||||
65 | // ---------------------------------------- | ||||
66 | // | 1 | 0)| (4| 3 | 2 | 1 | 0)| (4| 3 | | ||||
67 | // | | * | Character 1 | Char 2| | ||||
68 | // ---------------------------------------- | ||||
69 | // | 2 | 2 | 1 | 0)| (4| 3 | 2 | 1 | 0)| | ||||
70 | // | | Character2| Character 3 | | ||||
71 | // ---------------------------------------- | ||||
72 | const uint offset = 0x8; | ||||
73 | | ||||
74 | char pnpId[4]; | ||||
75 | pnpId[0] = 'A' + ((data[offset + 0] >> 2) & 0x1f) - 1; | ||||
76 | pnpId[1] = 'A' + (((data[offset + 0] & 0x3) << 3) | ((data[offset + 1] >> 5) & 0x7)) - 1; | ||||
77 | pnpId[2] = 'A' + (data[offset + 1] & 0x1f) - 1; | ||||
78 | pnpId[3] = '\0'; | ||||
79 | | ||||
80 | return QByteArray(pnpId); | ||||
81 | } | ||||
82 | | ||||
83 | static QByteArray parseMonitorName(const uint8_t *data) | ||||
84 | { | ||||
85 | for (int i = 72; i <= 108; i += 18) { | ||||
86 | // Skip the block if it isn't used as monitor descriptor. | ||||
87 | if (data[i]) { | ||||
88 | continue; | ||||
89 | } | ||||
90 | if (data[i + 1]) { | ||||
91 | continue; | ||||
92 | } | ||||
93 | | ||||
94 | // We have found the monitor name, it's stored as ASCII. | ||||
95 | if (data[i + 3] == 0xfc) { | ||||
96 | return QByteArray(reinterpret_cast<const char *>(&data[i + 5]), 12).trimmed(); | ||||
97 | } | ||||
98 | } | ||||
99 | | ||||
100 | return QByteArray(); | ||||
101 | } | ||||
102 | | ||||
103 | static QByteArray parseSerialNumber(const uint8_t *data) | ||||
104 | { | ||||
105 | for (int i = 72; i <= 108; i += 18) { | ||||
106 | // Skip the block if it isn't used as monitor descriptor. | ||||
107 | if (data[i]) { | ||||
108 | continue; | ||||
109 | } | ||||
110 | if (data[i + 1]) { | ||||
111 | continue; | ||||
112 | } | ||||
113 | | ||||
114 | // We have found the serial number, it's stored as ASCII. | ||||
115 | if (data[i + 3] == 0xff) { | ||||
116 | return QByteArray(reinterpret_cast<const char *>(&data[i + 5]), 12).trimmed(); | ||||
117 | } | ||||
118 | } | ||||
119 | | ||||
120 | // Maybe there isn't an ASCII serial number descriptor, so use this instead. | ||||
121 | const uint32_t offset = 0xc; | ||||
122 | | ||||
123 | uint32_t serialNumber = data[offset + 0]; | ||||
124 | serialNumber |= uint32_t(data[offset + 1]) << 8; | ||||
125 | serialNumber |= uint32_t(data[offset + 2]) << 16; | ||||
126 | serialNumber |= uint32_t(data[offset + 3]) << 24; | ||||
127 | if (serialNumber) { | ||||
128 | return QByteArray::number(serialNumber); | ||||
129 | } | ||||
130 | | ||||
131 | return QByteArray(); | ||||
132 | } | ||||
133 | | ||||
134 | Edid::Edid() | ||||
135 | { | ||||
136 | } | ||||
137 | | ||||
138 | Edid::Edid(const void *data, uint32_t size) | ||||
139 | { | ||||
140 | const uint8_t *bytes = static_cast<const uint8_t *>(data); | ||||
141 | | ||||
142 | if (size < 128) { | ||||
143 | return; | ||||
144 | } | ||||
145 | | ||||
146 | if (!verifyHeader(bytes)) { | ||||
147 | return; | ||||
148 | } | ||||
149 | | ||||
150 | m_physicalSize = parsePhysicalSize(bytes); | ||||
151 | m_eisaId = parseEisaId(bytes); | ||||
152 | m_monitorName = parseMonitorName(bytes); | ||||
153 | m_serialNumber = parseSerialNumber(bytes); | ||||
154 | | ||||
155 | m_isValid = true; | ||||
156 | } | ||||
157 | | ||||
158 | bool Edid::isValid() const | ||||
159 | { | ||||
160 | return m_isValid; | ||||
161 | } | ||||
162 | | ||||
163 | QSize Edid::physicalSize() const | ||||
164 | { | ||||
165 | return m_physicalSize; | ||||
166 | } | ||||
167 | | ||||
168 | QByteArray Edid::eisaId() const | ||||
169 | { | ||||
170 | return m_eisaId; | ||||
171 | } | ||||
172 | | ||||
173 | QByteArray Edid::monitorName() const | ||||
174 | { | ||||
175 | return m_monitorName; | ||||
176 | } | ||||
177 | | ||||
178 | QByteArray Edid::serialNumber() const | ||||
179 | { | ||||
180 | return m_serialNumber; | ||||
181 | } | ||||
182 | | ||||
183 | } // namespace KWin |