OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromeos/display/output_util.h" |
| 6 |
| 7 #include <X11/Xlib.h> |
| 8 #include <X11/extensions/Xrandr.h> |
| 9 #include <X11/Xatom.h> |
| 10 |
| 11 #include "base/message_loop.h" |
| 12 #include "base/string_util.h" |
| 13 #include "base/sys_byteorder.h" |
| 14 |
| 15 namespace chromeos { |
| 16 namespace { |
| 17 |
| 18 // Prefixes for the built-in displays. |
| 19 const char kInternal_LVDS[] = "LVDS"; |
| 20 const char kInternal_eDP[] = "eDP"; |
| 21 |
| 22 // Returns 64-bit persistent ID for the specified manufacturer's ID and |
| 23 // product_code, and the index of the output it is connected to. |
| 24 // |output_index| is used to distinguish the displays of the same type. For |
| 25 // example, swapping two identical display between two outputs will not be |
| 26 // treated as swap. The 'serial number' field in EDID isn't used here because |
| 27 // it is not guaranteed to have unique number and it may have the same fixed |
| 28 // value (like 0). |
| 29 int64 GetID(uint16 manufacturer_id, |
| 30 uint16 product_code, |
| 31 uint8 output_index) { |
| 32 return ((static_cast<int64>(manufacturer_id) << 24) | |
| 33 (static_cast<int64>(product_code) << 8) | output_index); |
| 34 } |
| 35 |
| 36 bool IsRandRAvailable() { |
| 37 int randr_version_major = 0; |
| 38 int randr_version_minor = 0; |
| 39 static bool is_randr_available = XRRQueryVersion( |
| 40 base::MessagePumpAuraX11::GetDefaultXDisplay(), |
| 41 &randr_version_major, &randr_version_minor); |
| 42 return is_randr_available; |
| 43 } |
| 44 |
| 45 // Get the EDID data from the |output| and stores to |prop|. |nitem| will store |
| 46 // the number of characters |prop| will have. It doesn't take the ownership of |
| 47 // |prop|, so caller must release it by XFree(). |
| 48 // Returns true if EDID property is successfully obtained. Otherwise returns |
| 49 // false and does not touch |prop| and |nitems|. |
| 50 bool GetEDIDProperty(XID output, unsigned long* nitems, unsigned char** prop) { |
| 51 if (!IsRandRAvailable()) |
| 52 return false; |
| 53 |
| 54 Display* display = base::MessagePumpAuraX11::GetDefaultXDisplay(); |
| 55 |
| 56 static Atom edid_property = XInternAtom( |
| 57 base::MessagePumpAuraX11::GetDefaultXDisplay(), |
| 58 RR_PROPERTY_RANDR_EDID, false); |
| 59 |
| 60 bool has_edid_property = false; |
| 61 int num_properties = 0; |
| 62 Atom* properties = XRRListOutputProperties(display, output, &num_properties); |
| 63 for (int i = 0; i < num_properties; ++i) { |
| 64 if (properties[i] == edid_property) { |
| 65 has_edid_property = true; |
| 66 break; |
| 67 } |
| 68 } |
| 69 XFree(properties); |
| 70 if (!has_edid_property) |
| 71 return false; |
| 72 |
| 73 Atom actual_type; |
| 74 int actual_format; |
| 75 unsigned long bytes_after; |
| 76 XRRGetOutputProperty(display, |
| 77 output, |
| 78 edid_property, |
| 79 0, // offset |
| 80 128, // length |
| 81 false, // _delete |
| 82 false, // pending |
| 83 AnyPropertyType, // req_type |
| 84 &actual_type, |
| 85 &actual_format, |
| 86 nitems, |
| 87 &bytes_after, |
| 88 prop); |
| 89 DCHECK_EQ(XA_INTEGER, actual_type); |
| 90 DCHECK_EQ(8, actual_format); |
| 91 return true; |
| 92 } |
| 93 |
| 94 // Gets some useful data from the specified output device, such like |
| 95 // manufacturer's ID, product code, and human readable name. Returns false if it |
| 96 // fails to get those data and doesn't touch manufacturer ID/product code/name. |
| 97 // NULL can be passed for unwanted output parameters. |
| 98 bool GetOutputDeviceData(XID output, |
| 99 uint16* manufacturer_id, |
| 100 uint16* product_code, |
| 101 std::string* human_readable_name) { |
| 102 unsigned long nitems = 0; |
| 103 unsigned char *prop = NULL; |
| 104 if (!GetEDIDProperty(output, &nitems, &prop)) |
| 105 return false; |
| 106 |
| 107 bool result = ParseOutputDeviceData( |
| 108 prop, nitems, manufacturer_id, product_code, human_readable_name); |
| 109 XFree(prop); |
| 110 return result; |
| 111 } |
| 112 |
| 113 } // namespace |
| 114 |
| 115 std::string GetDisplayName(XID output_id) { |
| 116 std::string display_name; |
| 117 GetOutputDeviceData(output_id, NULL, NULL, &display_name); |
| 118 return display_name; |
| 119 } |
| 120 |
| 121 bool GetDisplayId(XID output_id, size_t output_index, int64* display_id_out) { |
| 122 uint16 manufacturer_id = 0; |
| 123 uint16 product_code = 0; |
| 124 if (GetOutputDeviceData( |
| 125 output_id, &manufacturer_id, &product_code, NULL) && |
| 126 manufacturer_id != 0) { |
| 127 // An ID based on display's index will be assigned later if this call |
| 128 // fails. |
| 129 *display_id_out = GetID(manufacturer_id, product_code, output_index); |
| 130 return true; |
| 131 } |
| 132 return false; |
| 133 } |
| 134 |
| 135 bool ParseOutputDeviceData(const unsigned char* prop, |
| 136 unsigned long nitems, |
| 137 uint16* manufacturer_id, |
| 138 uint16* product_code, |
| 139 std::string* human_readable_name) { |
| 140 // See http://en.wikipedia.org/wiki/Extended_display_identification_data |
| 141 // for the details of EDID data format. We use the following data: |
| 142 // bytes 8-9: manufacturer EISA ID, in big-endian |
| 143 // bytes 10-11: represents product code, in little-endian |
| 144 // bytes 54-125: four descriptors (18-bytes each) which may contain |
| 145 // the display name. |
| 146 const unsigned int kManufacturerOffset = 8; |
| 147 const unsigned int kManufacturerLength = 2; |
| 148 const unsigned int kProductCodeOffset = 10; |
| 149 const unsigned int kProductCodeLength = 2; |
| 150 const unsigned int kDescriptorOffset = 54; |
| 151 const unsigned int kNumDescriptors = 4; |
| 152 const unsigned int kDescriptorLength = 18; |
| 153 // The specifier types. |
| 154 const unsigned char kMonitorNameDescriptor = 0xfc; |
| 155 |
| 156 if (manufacturer_id) { |
| 157 if (nitems < kManufacturerOffset + kManufacturerLength) { |
| 158 LOG(ERROR) << "too short EDID data: manifacturer id"; |
| 159 return false; |
| 160 } |
| 161 |
| 162 *manufacturer_id = |
| 163 *reinterpret_cast<const uint16*>(prop + kManufacturerOffset); |
| 164 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 165 *manufacturer_id = base::ByteSwap(*manufacturer_id); |
| 166 #endif |
| 167 } |
| 168 |
| 169 if (product_code) { |
| 170 if (nitems < kProductCodeOffset + kProductCodeLength) { |
| 171 LOG(ERROR) << "too short EDID data: product code"; |
| 172 return false; |
| 173 } |
| 174 |
| 175 *product_code = base::ByteSwapToLE16( |
| 176 *reinterpret_cast<const uint16*>(prop + kProductCodeOffset)); |
| 177 } |
| 178 |
| 179 if (!human_readable_name) |
| 180 return true; |
| 181 |
| 182 human_readable_name->clear(); |
| 183 for (unsigned int i = 0; i < kNumDescriptors; ++i) { |
| 184 if (nitems < kDescriptorOffset + (i + 1) * kDescriptorLength) |
| 185 break; |
| 186 |
| 187 const unsigned char* desc_buf = |
| 188 prop + kDescriptorOffset + i * kDescriptorLength; |
| 189 // If the descriptor contains the display name, it has the following |
| 190 // structure: |
| 191 // bytes 0-2, 4: \0 |
| 192 // byte 3: descriptor type, defined above. |
| 193 // bytes 5-17: text data, ending with \r, padding with spaces |
| 194 // we should check bytes 0-2 and 4, since it may have other values in |
| 195 // case that the descriptor contains other type of data. |
| 196 if (desc_buf[0] == 0 && desc_buf[1] == 0 && desc_buf[2] == 0 && |
| 197 desc_buf[4] == 0) { |
| 198 if (desc_buf[3] == kMonitorNameDescriptor) { |
| 199 std::string found_name( |
| 200 reinterpret_cast<const char*>(desc_buf + 5), kDescriptorLength - 5); |
| 201 TrimWhitespaceASCII(found_name, TRIM_TRAILING, human_readable_name); |
| 202 break; |
| 203 } |
| 204 } |
| 205 } |
| 206 |
| 207 if (human_readable_name->empty()) { |
| 208 LOG(ERROR) << "invalid EDID: empty readable name"; |
| 209 return false; |
| 210 } |
| 211 |
| 212 // Verify if the |human_readable_name| consists of printable characters only. |
| 213 for (size_t i = 0; i < human_readable_name->size(); ++i) { |
| 214 char c = (*human_readable_name)[i]; |
| 215 if (!isascii(c) || !isprint(c)) { |
| 216 human_readable_name->clear(); |
| 217 LOG(ERROR) << "invalid EDID: human unreadable char in name"; |
| 218 return false; |
| 219 } |
| 220 } |
| 221 |
| 222 return true; |
| 223 } |
| 224 |
| 225 bool GetOutputOverscanFlag(XID output, bool* flag) { |
| 226 unsigned long nitems = 0; |
| 227 unsigned char *prop = NULL; |
| 228 if (!GetEDIDProperty(output, &nitems, &prop)) |
| 229 return false; |
| 230 |
| 231 bool found = ParseOutputOverscanFlag(prop, nitems, flag); |
| 232 XFree(prop); |
| 233 return found; |
| 234 } |
| 235 |
| 236 bool ParseOutputOverscanFlag(const unsigned char* prop, |
| 237 unsigned long nitems, |
| 238 bool *flag) { |
| 239 // See http://en.wikipedia.org/wiki/Extended_display_identification_data |
| 240 // for the extension format of EDID. Also see EIA/CEA-861 spec for |
| 241 // the format of the extensions and how video capability is encoded. |
| 242 // - byte 0: tag. should be 02h. |
| 243 // - byte 1: revision. only cares revision 3 (03h). |
| 244 // - byte 4-: data block. |
| 245 const unsigned int kExtensionBase = 128; |
| 246 const unsigned int kExtensionSize = 128; |
| 247 const unsigned int kNumExtensionsOffset = 126; |
| 248 const unsigned int kDataBlockOffset = 4; |
| 249 const unsigned char kCEAExtensionTag = '\x02'; |
| 250 const unsigned char kExpectedExtensionRevision = '\x03'; |
| 251 const unsigned char kExtendedTag = 7; |
| 252 const unsigned char kExtendedVideoCapabilityTag = 0; |
| 253 const unsigned int kPTOverscan = 4; |
| 254 const unsigned int kITOverscan = 2; |
| 255 const unsigned int kCEOverscan = 0; |
| 256 |
| 257 if (nitems <= kNumExtensionsOffset) |
| 258 return false; |
| 259 |
| 260 unsigned char num_extensions = prop[kNumExtensionsOffset]; |
| 261 |
| 262 for (size_t i = 0; i < num_extensions; ++i) { |
| 263 // Skip parsing the whole extension if size is not enough. |
| 264 if (nitems < kExtensionBase + (i + 1) * kExtensionSize) |
| 265 break; |
| 266 |
| 267 const unsigned char* extension = prop + kExtensionBase + i * kExtensionSize; |
| 268 unsigned char tag = extension[0]; |
| 269 unsigned char revision = extension[1]; |
| 270 if (tag != kCEAExtensionTag || revision != kExpectedExtensionRevision) |
| 271 continue; |
| 272 |
| 273 unsigned char timing_descriptors_start = |
| 274 std::min(extension[2], static_cast<unsigned char>(kExtensionSize)); |
| 275 const unsigned char* data_block = extension + kDataBlockOffset; |
| 276 while (data_block < extension + timing_descriptors_start) { |
| 277 // A data block is encoded as: |
| 278 // - byte 1 high 3 bits: tag. '07' for extended tags. |
| 279 // - byte 1 remaining bits: the length of data block. |
| 280 // - byte 2: the extended tag. '0' for video capability. |
| 281 // - byte 3: the capability. |
| 282 unsigned char tag = data_block[0] >> 5; |
| 283 unsigned char payload_length = data_block[0] & 0x1f; |
| 284 if (static_cast<unsigned long>(data_block + payload_length - prop) > |
| 285 nitems) |
| 286 break; |
| 287 |
| 288 if (tag != kExtendedTag || payload_length < 2) { |
| 289 data_block += payload_length + 1; |
| 290 continue; |
| 291 } |
| 292 |
| 293 unsigned char extended_tag_code = data_block[1]; |
| 294 if (extended_tag_code != kExtendedVideoCapabilityTag) { |
| 295 data_block += payload_length + 1; |
| 296 continue; |
| 297 } |
| 298 |
| 299 // The difference between preferred, IT, and CE video formats |
| 300 // doesn't matter. Sets |flag| to true if any of these flags are true. |
| 301 if ((data_block[2] & (1 << kPTOverscan)) || |
| 302 (data_block[2] & (1 << kITOverscan)) || |
| 303 (data_block[2] & (1 << kCEOverscan))) { |
| 304 *flag = true; |
| 305 } else { |
| 306 *flag = false; |
| 307 } |
| 308 return true; |
| 309 } |
| 310 } |
| 311 |
| 312 return false; |
| 313 } |
| 314 |
| 315 bool IsInternalOutputName(const std::string& name) { |
| 316 return name.find(kInternal_LVDS) == 0 || name.find(kInternal_eDP) == 0; |
| 317 } |
| 318 |
| 319 } // namespace chromeos |
OLD | NEW |