| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/display/output_util.h" | 5 #include "chromeos/display/output_util.h" |
| 6 | 6 |
| 7 #include <X11/Xlib.h> | |
| 8 #include <X11/extensions/Xrandr.h> | 7 #include <X11/extensions/Xrandr.h> |
| 9 #include <X11/Xatom.h> | 8 #include <X11/Xatom.h> |
| 9 #include <X11/Xlib.h> |
| 10 | 10 |
| 11 #include "base/hash.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 14 #include "base/sys_byteorder.h" | 12 #include "base/x11/edid_parser_x11.h" |
| 15 | 13 |
| 16 namespace chromeos { | 14 namespace chromeos { |
| 17 namespace { | 15 namespace { |
| 18 | 16 |
| 19 // Prefixes for the built-in displays. | 17 // Prefixes for the built-in displays. |
| 20 const char kInternal_LVDS[] = "LVDS"; | 18 const char kInternal_LVDS[] = "LVDS"; |
| 21 const char kInternal_eDP[] = "eDP"; | 19 const char kInternal_eDP[] = "eDP"; |
| 22 const char kInternal_DSI[] = "DSI"; | 20 const char kInternal_DSI[] = "DSI"; |
| 23 | 21 |
| 24 // Returns 64-bit persistent ID for the specified manufacturer's ID and | |
| 25 // product_code_hash, and the index of the output it is connected to. | |
| 26 // |output_index| is used to distinguish the displays of the same type. For | |
| 27 // example, swapping two identical display between two outputs will not be | |
| 28 // treated as swap. The 'serial number' field in EDID isn't used here because | |
| 29 // it is not guaranteed to have unique number and it may have the same fixed | |
| 30 // value (like 0). | |
| 31 int64 GetID(uint16 manufacturer_id, | |
| 32 uint32 product_code_hash, | |
| 33 uint8 output_index) { | |
| 34 return ((static_cast<int64>(manufacturer_id) << 40) | | |
| 35 (static_cast<int64>(product_code_hash) << 8) | output_index); | |
| 36 } | |
| 37 | |
| 38 bool IsRandRAvailable() { | |
| 39 int randr_version_major = 0; | |
| 40 int randr_version_minor = 0; | |
| 41 static bool is_randr_available = XRRQueryVersion( | |
| 42 base::MessagePumpX11::GetDefaultXDisplay(), | |
| 43 &randr_version_major, &randr_version_minor); | |
| 44 return is_randr_available; | |
| 45 } | |
| 46 | |
| 47 // Get the EDID data from the |output| and stores to |prop|. |nitem| will store | |
| 48 // the number of characters |prop| will have. It doesn't take the ownership of | |
| 49 // |prop|, so caller must release it by XFree(). | |
| 50 // Returns true if EDID property is successfully obtained. Otherwise returns | |
| 51 // false and does not touch |prop| and |nitems|. | |
| 52 bool GetEDIDProperty(XID output, unsigned long* nitems, unsigned char** prop) { | |
| 53 if (!IsRandRAvailable()) | |
| 54 return false; | |
| 55 | |
| 56 Display* display = base::MessagePumpX11::GetDefaultXDisplay(); | |
| 57 | |
| 58 static Atom edid_property = XInternAtom( | |
| 59 base::MessagePumpX11::GetDefaultXDisplay(), | |
| 60 RR_PROPERTY_RANDR_EDID, false); | |
| 61 | |
| 62 bool has_edid_property = false; | |
| 63 int num_properties = 0; | |
| 64 Atom* properties = XRRListOutputProperties(display, output, &num_properties); | |
| 65 for (int i = 0; i < num_properties; ++i) { | |
| 66 if (properties[i] == edid_property) { | |
| 67 has_edid_property = true; | |
| 68 break; | |
| 69 } | |
| 70 } | |
| 71 XFree(properties); | |
| 72 if (!has_edid_property) | |
| 73 return false; | |
| 74 | |
| 75 Atom actual_type; | |
| 76 int actual_format; | |
| 77 unsigned long bytes_after; | |
| 78 XRRGetOutputProperty(display, | |
| 79 output, | |
| 80 edid_property, | |
| 81 0, // offset | |
| 82 128, // length | |
| 83 false, // _delete | |
| 84 false, // pending | |
| 85 AnyPropertyType, // req_type | |
| 86 &actual_type, | |
| 87 &actual_format, | |
| 88 nitems, | |
| 89 &bytes_after, | |
| 90 prop); | |
| 91 DCHECK_EQ(XA_INTEGER, actual_type); | |
| 92 DCHECK_EQ(8, actual_format); | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 // Gets some useful data from the specified output device, such like | 22 // Gets some useful data from the specified output device, such like |
| 97 // manufacturer's ID, product code, and human readable name. Returns false if it | 23 // manufacturer's ID, product code, and human readable name. Returns false if it |
| 98 // fails to get those data and doesn't touch manufacturer ID/product code/name. | 24 // fails to get those data and doesn't touch manufacturer ID/product code/name. |
| 99 // NULL can be passed for unwanted output parameters. | 25 // NULL can be passed for unwanted output parameters. |
| 100 bool GetOutputDeviceData(XID output, | 26 bool GetOutputDeviceData(XID output, |
| 101 uint16* manufacturer_id, | 27 uint16* manufacturer_id, |
| 102 std::string* human_readable_name) { | 28 std::string* human_readable_name) { |
| 103 unsigned long nitems = 0; | 29 unsigned long nitems = 0; |
| 104 unsigned char *prop = NULL; | 30 unsigned char *prop = NULL; |
| 105 if (!GetEDIDProperty(output, &nitems, &prop)) | 31 if (!base::GetEDIDProperty(output, &nitems, &prop)) |
| 106 return false; | 32 return false; |
| 107 | 33 |
| 108 bool result = ParseOutputDeviceData( | 34 bool result = base::ParseOutputDeviceData( |
| 109 prop, nitems, manufacturer_id, human_readable_name); | 35 prop, nitems, manufacturer_id, human_readable_name); |
| 110 XFree(prop); | 36 XFree(prop); |
| 111 return result; | 37 return result; |
| 112 } | 38 } |
| 113 | 39 |
| 114 } // namespace | 40 } // namespace |
| 115 | 41 |
| 116 std::string GetDisplayName(XID output_id) { | 42 std::string GetDisplayName(XID output_id) { |
| 117 std::string display_name; | 43 std::string display_name; |
| 118 GetOutputDeviceData(output_id, NULL, &display_name); | 44 GetOutputDeviceData(output_id, NULL, &display_name); |
| 119 return display_name; | 45 return display_name; |
| 120 } | 46 } |
| 121 | 47 |
| 122 bool GetDisplayId(XID output_id, size_t output_index, int64* display_id_out) { | |
| 123 unsigned long nitems = 0; | |
| 124 unsigned char* prop = NULL; | |
| 125 if (!GetEDIDProperty(output_id, &nitems, &prop)) | |
| 126 return false; | |
| 127 | |
| 128 bool result = | |
| 129 GetDisplayIdFromEDID(prop, nitems, output_index, display_id_out); | |
| 130 XFree(prop); | |
| 131 return result; | |
| 132 } | |
| 133 | |
| 134 bool GetDisplayIdFromEDID(const unsigned char* prop, | |
| 135 unsigned long nitems, | |
| 136 size_t output_index, | |
| 137 int64* display_id_out) { | |
| 138 uint16 manufacturer_id = 0; | |
| 139 std::string product_name; | |
| 140 | |
| 141 // ParseOutputDeviceData fails if it doesn't have product_name. | |
| 142 ParseOutputDeviceData(prop, nitems, &manufacturer_id, &product_name); | |
| 143 | |
| 144 // Generates product specific value from product_name instead of product code. | |
| 145 // See crbug.com/240341 | |
| 146 uint32 product_code_hash = product_name.empty() ? | |
| 147 0 : base::Hash(product_name); | |
| 148 if (manufacturer_id != 0) { | |
| 149 // An ID based on display's index will be assigned later if this call | |
| 150 // fails. | |
| 151 *display_id_out = GetID( | |
| 152 manufacturer_id, product_code_hash, output_index); | |
| 153 return true; | |
| 154 } | |
| 155 return false; | |
| 156 } | |
| 157 | |
| 158 bool ParseOutputDeviceData(const unsigned char* prop, | |
| 159 unsigned long nitems, | |
| 160 uint16* manufacturer_id, | |
| 161 std::string* human_readable_name) { | |
| 162 // See http://en.wikipedia.org/wiki/Extended_display_identification_data | |
| 163 // for the details of EDID data format. We use the following data: | |
| 164 // bytes 8-9: manufacturer EISA ID, in big-endian | |
| 165 // bytes 54-125: four descriptors (18-bytes each) which may contain | |
| 166 // the display name. | |
| 167 const unsigned int kManufacturerOffset = 8; | |
| 168 const unsigned int kManufacturerLength = 2; | |
| 169 const unsigned int kDescriptorOffset = 54; | |
| 170 const unsigned int kNumDescriptors = 4; | |
| 171 const unsigned int kDescriptorLength = 18; | |
| 172 // The specifier types. | |
| 173 const unsigned char kMonitorNameDescriptor = 0xfc; | |
| 174 | |
| 175 if (manufacturer_id) { | |
| 176 if (nitems < kManufacturerOffset + kManufacturerLength) { | |
| 177 LOG(ERROR) << "too short EDID data: manifacturer id"; | |
| 178 return false; | |
| 179 } | |
| 180 | |
| 181 *manufacturer_id = | |
| 182 *reinterpret_cast<const uint16*>(prop + kManufacturerOffset); | |
| 183 #if defined(ARCH_CPU_LITTLE_ENDIAN) | |
| 184 *manufacturer_id = base::ByteSwap(*manufacturer_id); | |
| 185 #endif | |
| 186 } | |
| 187 | |
| 188 if (!human_readable_name) | |
| 189 return true; | |
| 190 | |
| 191 human_readable_name->clear(); | |
| 192 for (unsigned int i = 0; i < kNumDescriptors; ++i) { | |
| 193 if (nitems < kDescriptorOffset + (i + 1) * kDescriptorLength) | |
| 194 break; | |
| 195 | |
| 196 const unsigned char* desc_buf = | |
| 197 prop + kDescriptorOffset + i * kDescriptorLength; | |
| 198 // If the descriptor contains the display name, it has the following | |
| 199 // structure: | |
| 200 // bytes 0-2, 4: \0 | |
| 201 // byte 3: descriptor type, defined above. | |
| 202 // bytes 5-17: text data, ending with \r, padding with spaces | |
| 203 // we should check bytes 0-2 and 4, since it may have other values in | |
| 204 // case that the descriptor contains other type of data. | |
| 205 if (desc_buf[0] == 0 && desc_buf[1] == 0 && desc_buf[2] == 0 && | |
| 206 desc_buf[4] == 0) { | |
| 207 if (desc_buf[3] == kMonitorNameDescriptor) { | |
| 208 std::string found_name( | |
| 209 reinterpret_cast<const char*>(desc_buf + 5), kDescriptorLength - 5); | |
| 210 TrimWhitespaceASCII(found_name, TRIM_TRAILING, human_readable_name); | |
| 211 break; | |
| 212 } | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 // Verify if the |human_readable_name| consists of printable characters only. | |
| 217 for (size_t i = 0; i < human_readable_name->size(); ++i) { | |
| 218 char c = (*human_readable_name)[i]; | |
| 219 if (!isascii(c) || !isprint(c)) { | |
| 220 human_readable_name->clear(); | |
| 221 LOG(ERROR) << "invalid EDID: human unreadable char in name"; | |
| 222 return false; | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 return true; | |
| 227 } | |
| 228 | |
| 229 bool GetOutputOverscanFlag(XID output, bool* flag) { | 48 bool GetOutputOverscanFlag(XID output, bool* flag) { |
| 230 unsigned long nitems = 0; | 49 unsigned long nitems = 0; |
| 231 unsigned char *prop = NULL; | 50 unsigned char *prop = NULL; |
| 232 if (!GetEDIDProperty(output, &nitems, &prop)) | 51 if (!base::GetEDIDProperty(output, &nitems, &prop)) |
| 233 return false; | 52 return false; |
| 234 | 53 |
| 235 bool found = ParseOutputOverscanFlag(prop, nitems, flag); | 54 bool found = ParseOutputOverscanFlag(prop, nitems, flag); |
| 236 XFree(prop); | 55 XFree(prop); |
| 237 return found; | 56 return found; |
| 238 } | 57 } |
| 239 | 58 |
| 240 bool ParseOutputOverscanFlag(const unsigned char* prop, | 59 bool ParseOutputOverscanFlag(const unsigned char* prop, |
| 241 unsigned long nitems, | 60 unsigned long nitems, |
| 242 bool *flag) { | 61 bool *flag) { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 mode_info.hTotal = 1; | 167 mode_info.hTotal = 1; |
| 349 mode_info.vTotal = 1; | 168 mode_info.vTotal = 1; |
| 350 mode_info.dotClock = refresh_rate; | 169 mode_info.dotClock = refresh_rate; |
| 351 } | 170 } |
| 352 return mode_info; | 171 return mode_info; |
| 353 } | 172 } |
| 354 | 173 |
| 355 } // namespace test | 174 } // namespace test |
| 356 | 175 |
| 357 } // namespace chromeos | 176 } // namespace chromeos |
| OLD | NEW |