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..1d17a66a18af0989958e5010314c43fa7ccabbfb 100644 |
| --- a/ui/base/x/x11_util.cc |
| +++ b/ui/base/x/x11_util.cc |
| @@ -15,6 +15,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 +27,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" |
| @@ -987,6 +991,118 @@ void FreePixmap(Display* display, XID pixmap) { |
| XFreePixmap(display, pixmap); |
| } |
| +bool GetOutputDeviceHandles(std::vector<XID>* outputs) { |
| + DCHECK(outputs); |
| + outputs->clear(); |
| + |
| + if (!XDisplayExists()) |
| + return false; |
|
oshima
2012/08/10 03:09:51
Since you get XDisplay below,
if (!display)
re
Jun Mukai
2012/08/10 09:39:52
Done.
|
| + |
| + Display* display = GetXDisplay(); |
| + Window root_window = DefaultRootWindow(display); |
| + XRRScreenResources* screen_resources = |
| + XRRGetScreenResources(display, root_window); |
|
oshima
2012/08/10 03:09:51
can this handle when the X doesn't support Xrandr?
Jun Mukai
2012/08/10 09:39:52
Done.
|
| + for (int i = 0; i < screen_resources->noutput; ++i) { |
| + outputs->push_back(screen_resources->outputs[i]); |
| + } |
|
oshima
2012/08/10 03:09:51
nuke {}
Jun Mukai
2012/08/10 09:39:52
Done.
|
| + XRRFreeScreenResources(screen_resources); |
| + return true; |
| +} |
| + |
| +bool GetOutputDeviceData(XID output, |
| + uint16* manufacturer_id, |
| + uint32* serial_number, |
| + std::string* human_readable_name) { |
| + DCHECK(manufacturer_id); |
| + DCHECK(serial_number); |
| + DCHECK(human_readable_name); |
| + |
| + if (!XDisplayExists()) |
| + return false; |
|
oshima
2012/08/10 03:09:51
ditto.
Jun Mukai
2012/08/10 09:39:52
Done.
|
| + |
| + Display* display = GetXDisplay(); |
| + Atom edid_property = GetAtom(RR_PROPERTY_RANDR_EDID); |
| + 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, 128, false, false, |
| + AnyPropertyType, &actual_type, &actual_format, &nitems, |
| + &bytes_after, &prop); |
| + DCHECK(actual_type == XA_INTEGER); |
| + DCHECK(actual_format == 8); |
| + |
| + // 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; |
| + |
| + *manufacturer_id = *reinterpret_cast<uint16*>(prop + kManufacturerOffset); |
|
oshima
2012/08/10 03:09:51
I assume the format guarantees the correct alignme
Jun Mukai
2012/08/10 09:39:52
We do not have arm devices here... :(
As we discus
|
| +#if defined(ARCH_CPU_LITTLE_ENDIAN) |
| + *manufacturer_id = base::ByteSwap(*manufacturer_id); |
| +#endif |
| + *serial_number = base::ByteSwapToLE32( |
| + *reinterpret_cast<uint32*>(prop + kSerialNumberOffset)); |
| + |
| + 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: \xfc for "Monitor name", \xfe for "Unspecified text" |
| + // 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] == 0xfc) { |
| + 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] == 0xfe && 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; |
| + |
| + XFree(prop); |
| + return true; |
| +} |
| + |
| + |
| bool GetWindowManagerName(std::string* wm_name) { |
| DCHECK(wm_name); |
| int wm_window = 0; |