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.
5 #include "chromeos/display/output_util.h"
7 #include <X11/extensions/Xrandr.h>
11 #include "base/strings/string_util.h"
12 #include "base/x11/edid_parser_x11.h"
17 // Prefixes for the built-in displays.
18 const char kInternal_LVDS[] = "LVDS";
19 const char kInternal_eDP[] = "eDP";
20 const char kInternal_DSI[] = "DSI";
22 // Gets some useful data from the specified output device, such like
23 // manufacturer's ID, product code, and human readable name. Returns false if it
24 // fails to get those data and doesn't touch manufacturer ID/product code/name.
25 // NULL can be passed for unwanted output parameters.
26 bool GetOutputDeviceData(XID output,
27 uint16* manufacturer_id,
28 std::string* human_readable_name) {
29 unsigned long nitems = 0;
30 unsigned char *prop = NULL;
31 if (!base::GetEDIDProperty(output, &nitems, &prop))
34 bool result = base::ParseOutputDeviceData(
35 prop, nitems, manufacturer_id, human_readable_name);
42 std::string GetDisplayName(XID output_id) {
43 std::string display_name;
44 GetOutputDeviceData(output_id, NULL, &display_name);
48 bool GetOutputOverscanFlag(XID output, bool* flag) {
49 unsigned long nitems = 0;
50 unsigned char *prop = NULL;
51 if (!base::GetEDIDProperty(output, &nitems, &prop))
54 bool found = ParseOutputOverscanFlag(prop, nitems, flag);
59 bool ParseOutputOverscanFlag(const unsigned char* prop,
62 // See http://en.wikipedia.org/wiki/Extended_display_identification_data
63 // for the extension format of EDID. Also see EIA/CEA-861 spec for
64 // the format of the extensions and how video capability is encoded.
65 // - byte 0: tag. should be 02h.
66 // - byte 1: revision. only cares revision 3 (03h).
67 // - byte 4-: data block.
68 const unsigned int kExtensionBase = 128;
69 const unsigned int kExtensionSize = 128;
70 const unsigned int kNumExtensionsOffset = 126;
71 const unsigned int kDataBlockOffset = 4;
72 const unsigned char kCEAExtensionTag = '\x02';
73 const unsigned char kExpectedExtensionRevision = '\x03';
74 const unsigned char kExtendedTag = 7;
75 const unsigned char kExtendedVideoCapabilityTag = 0;
76 const unsigned int kPTOverscan = 4;
77 const unsigned int kITOverscan = 2;
78 const unsigned int kCEOverscan = 0;
80 if (nitems <= kNumExtensionsOffset)
83 unsigned char num_extensions = prop[kNumExtensionsOffset];
85 for (size_t i = 0; i < num_extensions; ++i) {
86 // Skip parsing the whole extension if size is not enough.
87 if (nitems < kExtensionBase + (i + 1) * kExtensionSize)
90 const unsigned char* extension = prop + kExtensionBase + i * kExtensionSize;
91 unsigned char tag = extension[0];
92 unsigned char revision = extension[1];
93 if (tag != kCEAExtensionTag || revision != kExpectedExtensionRevision)
96 unsigned char timing_descriptors_start =
97 std::min(extension[2], static_cast<unsigned char>(kExtensionSize));
98 const unsigned char* data_block = extension + kDataBlockOffset;
99 while (data_block < extension + timing_descriptors_start) {
100 // A data block is encoded as:
101 // - byte 1 high 3 bits: tag. '07' for extended tags.
102 // - byte 1 remaining bits: the length of data block.
103 // - byte 2: the extended tag. '0' for video capability.
104 // - byte 3: the capability.
105 unsigned char tag = data_block[0] >> 5;
106 unsigned char payload_length = data_block[0] & 0x1f;
107 if (static_cast<unsigned long>(data_block + payload_length - prop) >
111 if (tag != kExtendedTag || payload_length < 2) {
112 data_block += payload_length + 1;
116 unsigned char extended_tag_code = data_block[1];
117 if (extended_tag_code != kExtendedVideoCapabilityTag) {
118 data_block += payload_length + 1;
122 // The difference between preferred, IT, and CE video formats
123 // doesn't matter. Sets |flag| to true if any of these flags are true.
124 if ((data_block[2] & (1 << kPTOverscan)) ||
125 (data_block[2] & (1 << kITOverscan)) ||
126 (data_block[2] & (1 << kCEOverscan))) {
138 bool IsInternalOutputName(const std::string& name) {
139 return name.find(kInternal_LVDS) == 0 || name.find(kInternal_eDP) == 0 ||
140 name.find(kInternal_DSI) == 0;
143 const XRRModeInfo* FindXRRModeInfo(const XRRScreenResources* screen_resources,
145 for (int m = 0; m < screen_resources->nmode; m++) {
146 XRRModeInfo *mode = &screen_resources->modes[m];
147 if (mode->id == current_mode)
155 XRRModeInfo CreateModeInfo(int id,
159 float refresh_rate) {
160 XRRModeInfo mode_info = {0};
162 mode_info.width = width;
163 mode_info.height = height;
165 mode_info.modeFlags = RR_Interlace;
166 if (refresh_rate != 0.0f) {
167 mode_info.hTotal = 1;
168 mode_info.vTotal = 1;
169 mode_info.dotClock = refresh_rate;
176 } // namespace chromeos