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