Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(492)

Side by Side Diff: ui/base/x/x11_util.cc

Issue 10826198: Use persistent ID/names for displays. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // This file defines utility functions for X11 (Linux only). This code has been 5 // This file defines utility functions for X11 (Linux only). This code has been
6 // ported from XCB since we can't use XCB on Ubuntu while its 32-bit support 6 // ported from XCB since we can't use XCB on Ubuntu while its 32-bit support
7 // remains woefully incomplete. 7 // remains woefully incomplete.
8 8
9 #include "ui/base/x/x11_util.h" 9 #include "ui/base/x/x11_util.h"
10 10
11 #include <ctype.h>
11 #include <sys/ipc.h> 12 #include <sys/ipc.h>
12 #include <sys/shm.h> 13 #include <sys/shm.h>
13 14
14 #include <list> 15 #include <list>
15 #include <map> 16 #include <map>
16 #include <vector> 17 #include <vector>
17 18
19 #include <X11/extensions/Xrandr.h>
20 #include <X11/extensions/randr.h>
21
18 #include "base/bind.h" 22 #include "base/bind.h"
19 #include "base/command_line.h" 23 #include "base/command_line.h"
20 #include "base/logging.h" 24 #include "base/logging.h"
21 #include "base/memory/scoped_ptr.h" 25 #include "base/memory/scoped_ptr.h"
22 #include "base/memory/singleton.h" 26 #include "base/memory/singleton.h"
23 #include "base/message_loop.h" 27 #include "base/message_loop.h"
24 #include "base/string_number_conversions.h" 28 #include "base/string_number_conversions.h"
25 #include "base/string_util.h" 29 #include "base/string_util.h"
26 #include "base/stringprintf.h" 30 #include "base/stringprintf.h"
31 #include "base/sys_byteorder.h"
27 #include "base/threading/thread.h" 32 #include "base/threading/thread.h"
28 #include "ui/base/keycodes/keyboard_code_conversion_x.h" 33 #include "ui/base/keycodes/keyboard_code_conversion_x.h"
29 #include "ui/base/x/x11_util_internal.h" 34 #include "ui/base/x/x11_util_internal.h"
30 #include "ui/gfx/rect.h" 35 #include "ui/gfx/rect.h"
31 #include "ui/gfx/size.h" 36 #include "ui/gfx/size.h"
32 37
33 #if defined(OS_FREEBSD) 38 #if defined(OS_FREEBSD)
34 #include <sys/sysctl.h> 39 #include <sys/sysctl.h>
35 #include <sys/types.h> 40 #include <sys/types.h>
36 #endif 41 #endif
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 } 300 }
296 301
297 ~XButtonMap() {} 302 ~XButtonMap() {}
298 303
299 unsigned char map_[256]; 304 unsigned char map_[256];
300 int count_; 305 int count_;
301 306
302 DISALLOW_COPY_AND_ASSIGN(XButtonMap); 307 DISALLOW_COPY_AND_ASSIGN(XButtonMap);
303 }; 308 };
304 309
310 bool IsRandRAvailable() {
311 static bool is_randr_available = false;
312 static bool is_randr_availability_cached = false;
313 if (is_randr_availability_cached)
314 return is_randr_available;
315
316 int randr_version_major = 0;
317 int randr_version_minor = 0;
318 is_randr_available = XRRQueryVersion(
319 GetXDisplay(), &randr_version_major, &randr_version_minor);
320 is_randr_availability_cached = true;
321 return is_randr_available;
322 }
323
305 } // namespace 324 } // namespace
306 325
307 bool XDisplayExists() { 326 bool XDisplayExists() {
308 return (GetXDisplay() != NULL); 327 return (GetXDisplay() != NULL);
309 } 328 }
310 329
311 Display* GetXDisplay() { 330 Display* GetXDisplay() {
312 return base::MessagePumpForUI::GetDefaultXDisplay(); 331 return base::MessagePumpForUI::GetDefaultXDisplay();
313 } 332 }
314 333
(...skipping 665 matching lines...) Expand 10 before | Expand all | Expand 10 after
980 } 999 }
981 1000
982 void FreePicture(Display* display, XID picture) { 1001 void FreePicture(Display* display, XID picture) {
983 XRenderFreePicture(display, picture); 1002 XRenderFreePicture(display, picture);
984 } 1003 }
985 1004
986 void FreePixmap(Display* display, XID pixmap) { 1005 void FreePixmap(Display* display, XID pixmap) {
987 XFreePixmap(display, pixmap); 1006 XFreePixmap(display, pixmap);
988 } 1007 }
989 1008
1009 bool GetOutputDeviceHandles(std::vector<XID>* outputs) {
1010 DCHECK(outputs);
1011 outputs->clear();
1012
1013 if (!IsRandRAvailable())
1014 return false;
1015
1016 Display* display = GetXDisplay();
1017
1018 Window root_window = DefaultRootWindow(display);
1019 XRRScreenResources* screen_resources =
1020 XRRGetScreenResources(display, root_window);
1021 for (int i = 0; i < screen_resources->noutput; ++i)
1022 outputs->push_back(screen_resources->outputs[i]);
1023 XRRFreeScreenResources(screen_resources);
1024 return true;
1025 }
1026
1027 bool GetOutputDeviceData(XID output,
1028 uint16* manufacturer_id,
1029 uint32* serial_number,
1030 std::string* human_readable_name) {
1031 if (!IsRandRAvailable())
1032 return false;
1033
1034 static Atom edid_property = GetAtom(RR_PROPERTY_RANDR_EDID);
1035
1036 Display* display = GetXDisplay();
1037
1038 bool has_edid_property = false;
1039 int num_properties = 0;
1040 Atom* properties = XRRListOutputProperties(display, output, &num_properties);
1041 for (int i = 0; i < num_properties; ++i) {
1042 if (properties[i] == edid_property) {
1043 has_edid_property = true;
1044 break;
1045 }
1046 }
1047 XFree(properties);
1048 if (!has_edid_property)
1049 return false;
1050
1051 Atom actual_type;
1052 int actual_format;
1053 unsigned long nitems;
1054 unsigned long bytes_after;
1055 unsigned char *prop;
1056 XRRGetOutputProperty(display,
1057 output,
1058 edid_property,
1059 0, // offset
1060 128, // length
1061 false, // _delete
1062 false, // pending
1063 AnyPropertyType, // req_type
1064 &actual_type,
1065 &actual_format,
1066 &nitems,
1067 &bytes_after,
1068 &prop);
1069 DCHECK_EQ(XA_INTEGER, actual_type);
1070 DCHECK_EQ(8, actual_format);
1071 DCHECK_LE(128u, nitems);
Daniel Erat 2012/08/17 14:33:46 in my earlier comment, i meant that you should che
Jun Mukai 2012/08/20 05:56:38 Done.
1072
1073 // See http://en.wikipedia.org/wiki/Extended_display_identification_data
1074 // for the details of EDID data format. We use the following data:
1075 // bytes 8-9: manufacturer EISA ID, in big-endian
1076 // bytes 12-15: represents serial number, in little-endian
1077 // bytes 54-125: four descriptors (18-bytes each) which may contain
1078 // the display name.
1079 const int kManufacturerOffset = 8;
1080 const int kSerialNumberOffset = 12;
1081 const int kDescriptorOffset = 54;
1082 const int kNumDescriptors = 4;
1083 const int kDescriptorLength = 18;
1084 // The specifier types.
1085 const int kMonitorNameDescriptor = 0xfc;
1086 const int kUnspecifiedTextDescriptor = 0xfe;
1087
1088 if (manufacturer_id) {
1089 *manufacturer_id = *reinterpret_cast<uint16*>(prop + kManufacturerOffset);
1090 #if defined(ARCH_CPU_LITTLE_ENDIAN)
1091 *manufacturer_id = base::ByteSwap(*manufacturer_id);
1092 #endif
1093 }
1094
1095 if (serial_number) {
1096 *serial_number = base::ByteSwapToLE32(
1097 *reinterpret_cast<uint32*>(prop + kSerialNumberOffset));
1098 }
1099
1100 if (!human_readable_name) {
1101 XFree(prop);
1102 return true;
1103 }
1104
1105 std::string name_candidate;
1106 human_readable_name->clear();
1107 for (int i = 0; i < kNumDescriptors; ++i) {
1108 unsigned char* desc_buf = prop + kDescriptorOffset + i * kDescriptorLength;
1109 // If the descriptor contains the display name, it has the following
1110 // structure:
1111 // bytes 0-2, 4: \0
1112 // byte 3: descriptor type, defined above.
1113 // bytes 5-17: text data, ending with \r, padding with spaces
1114 // we should check bytes 0-2 and 4, since it may have other values in
1115 // case that the descriptor contains other type of data.
1116 if (desc_buf[0] == 0 && desc_buf[1] == 0 && desc_buf[2] == 0 &&
1117 desc_buf[4] == 0) {
1118 if (desc_buf[3] == kMonitorNameDescriptor) {
1119 std::string found_name(
1120 reinterpret_cast<char*>(desc_buf + 5), kDescriptorLength - 5);
1121 TrimWhitespaceASCII(found_name, TRIM_TRAILING, human_readable_name);
1122 break;
1123 } else if (desc_buf[3] == kUnspecifiedTextDescriptor &&
1124 name_candidate.empty()) {
1125 // Sometimes the default display of a laptop device doesn't have "FC"
1126 // ("Monitor name") descriptor, but has some human readable text with
1127 // "FE" ("Unspecified text"). Thus here use this value as the fallback
1128 // if "FC" is missing. Note that multiple descriptors may have "FE",
1129 // and the first one is the monitor name.
1130 std::string found_name(
1131 reinterpret_cast<char*>(desc_buf + 5), kDescriptorLength - 5);
1132 TrimWhitespaceASCII(found_name, TRIM_TRAILING, &name_candidate);
1133 }
1134 }
1135 }
1136 if (human_readable_name->empty() && !name_candidate.empty())
1137 *human_readable_name = name_candidate;
1138
1139 XFree(prop);
1140
1141 if (human_readable_name->empty())
1142 return false;
1143
1144 // Verify if the |human_readable_name| consists of printable characters only.
1145 for (size_t i = 0; i < human_readable_name->size(); ++i) {
1146 char c = (*human_readable_name)[i];
1147 if (!isascii(c) || !isprint(c)) {
1148 human_readable_name->clear();
1149 return false;
1150 }
1151 }
1152
1153 return true;
1154 }
1155
1156
990 bool GetWindowManagerName(std::string* wm_name) { 1157 bool GetWindowManagerName(std::string* wm_name) {
991 DCHECK(wm_name); 1158 DCHECK(wm_name);
992 int wm_window = 0; 1159 int wm_window = 0;
993 if (!GetIntProperty(GetX11RootWindow(), 1160 if (!GetIntProperty(GetX11RootWindow(),
994 "_NET_SUPPORTING_WM_CHECK", 1161 "_NET_SUPPORTING_WM_CHECK",
995 &wm_window)) { 1162 &wm_window)) {
996 return false; 1163 return false;
997 } 1164 }
998 1165
999 // It's possible that a window manager started earlier in this X session left 1166 // It's possible that a window manager started earlier in this X session left
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
1278 << "request_code " << static_cast<int>(error_event.request_code) << ", " 1445 << "request_code " << static_cast<int>(error_event.request_code) << ", "
1279 << "minor_code " << static_cast<int>(error_event.minor_code) 1446 << "minor_code " << static_cast<int>(error_event.minor_code)
1280 << " (" << request_str << ")"; 1447 << " (" << request_str << ")";
1281 } 1448 }
1282 1449
1283 // ---------------------------------------------------------------------------- 1450 // ----------------------------------------------------------------------------
1284 // End of x11_util_internal.h 1451 // End of x11_util_internal.h
1285 1452
1286 1453
1287 } // namespace ui 1454 } // namespace ui
OLDNEW
« ui/base/x/x11_util.h ('K') | « ui/base/x/x11_util.h ('k') | ui/gfx/display.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698