Chromium Code Reviews| Index: ui/base/x/x11_util.cc |
| diff --git a/ui/base/x/x11_util.cc b/ui/base/x/x11_util.cc |
| index b058b2fe340b77e1e877279f21fc8c2077f4f360..a0ab7642dfa36d7c28fb68e67484e41e803c8254 100644 |
| --- a/ui/base/x/x11_util.cc |
| +++ b/ui/base/x/x11_util.cc |
| @@ -8,6 +8,7 @@ |
| #include "ui/base/x/x11_util.h" |
| +#include <ctype.h> |
| #include <sys/ipc.h> |
| #include <sys/shm.h> |
| @@ -15,6 +16,9 @@ |
| #include <map> |
| #include <vector> |
| +#include <X11/extensions/Xrandr.h> |
| +#include <X11/extensions/randr.h> |
| + |
| #include "base/bind.h" |
| #include "base/command_line.h" |
| #include "base/logging.h" |
| @@ -24,6 +28,7 @@ |
| #include "base/string_number_conversions.h" |
| #include "base/string_util.h" |
| #include "base/stringprintf.h" |
| +#include "base/sys_byteorder.h" |
| #include "base/threading/thread.h" |
| #include "ui/base/keycodes/keyboard_code_conversion_x.h" |
| #include "ui/base/x/x11_util_internal.h" |
| @@ -302,6 +307,20 @@ class XButtonMap { |
| DISALLOW_COPY_AND_ASSIGN(XButtonMap); |
| }; |
| +bool IsRandRAvailable() { |
| + static bool is_randr_available = false; |
| + static bool is_randr_availability_cached = false; |
| + if (is_randr_availability_cached) |
| + return is_randr_available; |
| + |
| + int randr_version_major = 0; |
| + int randr_version_minor = 0; |
| + is_randr_available = XRRQueryVersion( |
| + GetXDisplay(), &randr_version_major, &randr_version_minor); |
| + is_randr_availability_cached = true; |
| + return is_randr_available; |
| +} |
| + |
| } // namespace |
| bool XDisplayExists() { |
| @@ -987,6 +1006,151 @@ void FreePixmap(Display* display, XID pixmap) { |
| XFreePixmap(display, pixmap); |
| } |
| +bool GetOutputDeviceHandles(std::vector<XID>* outputs) { |
| + DCHECK(outputs); |
| + outputs->clear(); |
| + |
| + if (!IsRandRAvailable()) |
| + return false; |
| + |
| + Display* display = GetXDisplay(); |
| + |
| + Window root_window = DefaultRootWindow(display); |
| + XRRScreenResources* screen_resources = |
| + XRRGetScreenResources(display, root_window); |
| + for (int i = 0; i < screen_resources->noutput; ++i) |
| + outputs->push_back(screen_resources->outputs[i]); |
| + XRRFreeScreenResources(screen_resources); |
| + return true; |
| +} |
| + |
| +bool GetOutputDeviceData(XID output, |
| + uint16* manufacturer_id, |
| + uint32* serial_number, |
| + std::string* human_readable_name) { |
| + if (!IsRandRAvailable()) |
| + return false; |
| + |
| + Display* display = GetXDisplay(); |
| + |
| + Atom edid_property = GetAtom(RR_PROPERTY_RANDR_EDID); |
|
Daniel Erat
2012/08/16 15:28:49
nit: cache this too?
Jun Mukai
2012/08/17 05:50:10
Done.
|
| + bool has_edid_property = false; |
| + int num_properties = 0; |
| + Atom* properties = XRRListOutputProperties(display, output, &num_properties); |
| + for (int i = 0; i < num_properties; ++i) { |
| + if (properties[i] == edid_property) { |
| + has_edid_property = true; |
| + break; |
| + } |
| + } |
| + XFree(properties); |
| + if (!has_edid_property) |
| + return false; |
| + |
| + Atom actual_type; |
| + int actual_format; |
| + unsigned long nitems; |
| + unsigned long bytes_after; |
| + unsigned char *prop; |
| + XRRGetOutputProperty(display, |
| + output, |
| + edid_property, |
| + 0, // offset |
| + 128, // length |
| + false, // _delete |
| + false, // pending |
| + AnyPropertyType, // req_type |
| + &actual_type, |
| + &actual_format, |
| + &nitems, |
| + &bytes_after, |
| + &prop); |
| + DCHECK_EQ(XA_INTEGER, actual_type); |
| + DCHECK_EQ(8, actual_format); |
| + DCHECK_LE(128u, nitems); |
| + |
| + // See http://en.wikipedia.org/wiki/Extended_display_identification_data |
| + // for the details of EDID data format. We use the following data: |
| + // bytes 8-9: manufacturer EISA ID, in big-endian |
| + // bytes 12-15: represents serial number, in little-endian |
| + // bytes 54-125: four descriptors (18-bytes each) which may contain |
| + // the display name. |
| + const int kManufacturerOffset = 8; |
| + const int kSerialNumberOffset = 12; |
| + const int kDescriptorOffset = 54; |
| + const int kNumDescriptors = 4; |
| + const int kDescriptorLength = 18; |
| + // The specifier types. |
| + const int kMonitorNameDescriptor = 0xfc; |
| + const int kUnspecifiedTextDescriptor = 0xfe; |
| + |
| + if (manufacturer_id) { |
| + *manufacturer_id = *reinterpret_cast<uint16*>(prop + kManufacturerOffset); |
| +#if defined(ARCH_CPU_LITTLE_ENDIAN) |
| + *manufacturer_id = base::ByteSwap(*manufacturer_id); |
| +#endif |
| + } |
| + |
| + if (serial_number) { |
| + *serial_number = base::ByteSwapToLE32( |
| + *reinterpret_cast<uint32*>(prop + kSerialNumberOffset)); |
| + } |
| + |
| + if (!human_readable_name) |
| + return true; |
| + |
| + std::string name_candidate; |
| + human_readable_name->clear(); |
| + for (int i = 0; i < kNumDescriptors; ++i) { |
| + unsigned char* desc_buf = prop + kDescriptorOffset + i * kDescriptorLength; |
| + // If the descriptor contains the display name, it has the following |
| + // structure: |
| + // bytes 0-2, 4: \0 |
| + // byte 3: descriptor type, defined above. |
| + // bytes 5-17: text data, ending with \r, padding with spaces |
| + // we should check bytes 0-2 and 4, since it may have other values in |
| + // case that the descriptor contains other type of data. |
| + if (desc_buf[0] == 0 && desc_buf[1] == 0 && desc_buf[2] == 0 && |
| + desc_buf[4] == 0) { |
| + if (desc_buf[3] == kMonitorNameDescriptor) { |
| + std::string found_name( |
| + reinterpret_cast<char*>(desc_buf + 5), kDescriptorLength - 5); |
| + TrimWhitespaceASCII(found_name, TRIM_TRAILING, human_readable_name); |
| + break; |
| + } else if (desc_buf[3] == kUnspecifiedTextDescriptor && |
| + name_candidate.empty()) { |
| + // Sometimes the default display of a laptop device doesn't have "FC" |
| + // ("Monitor name") descriptor, but has some human readable text with |
| + // "FE" ("Unspecified text"). Thus here uses this value as the fallback |
| + // of the lack of "FC". Note that multiple descriptors may have "FE", |
| + // and the first one is the monitor name. |
| + std::string found_name( |
| + reinterpret_cast<char*>(desc_buf + 5), kDescriptorLength - 5); |
| + TrimWhitespaceASCII(found_name, TRIM_TRAILING, &name_candidate); |
| + } |
| + } |
| + } |
| + if (human_readable_name->empty() && !name_candidate.empty()) |
| + *human_readable_name = name_candidate; |
| + |
| + bool succeed = true; |
|
Daniel Erat
2012/08/16 15:28:49
i had some comments here in the previous round tha
Jun Mukai
2012/08/17 05:50:10
Sorry I've missed your comments. Replied.
|
| + // Verify the |human_readable_name| in case. |
| + if (human_readable_name->empty()) |
| + succeed = false; |
| + for (size_t i = 0; i < human_readable_name->size(); ++i) { |
| + char c = (*human_readable_name)[i]; |
| + if (!isascii(c) || !isprint(c)) { |
| + human_readable_name->empty(); |
| + succeed = false; |
| + break; |
| + } |
| + } |
| + |
| + XFree(prop); |
| + return succeed; |
| +} |
| + |
| + |
| bool GetWindowManagerName(std::string* wm_name) { |
| DCHECK(wm_name); |
| int wm_window = 0; |