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

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(Display* display) {
311 int randr_version_major = 0;
312 int randr_version_minor = 0;
313 return XRRQueryVersion(
314 display, &randr_version_major, &randr_version_minor);
Daniel Erat 2012/08/15 14:13:04 nit: this shouldn't change while we're running. c
Jun Mukai 2012/08/16 07:52:18 Done.
315 }
316
305 } // namespace 317 } // namespace
306 318
307 bool XDisplayExists() { 319 bool XDisplayExists() {
308 return (GetXDisplay() != NULL); 320 return (GetXDisplay() != NULL);
309 } 321 }
310 322
311 Display* GetXDisplay() { 323 Display* GetXDisplay() {
312 return base::MessagePumpForUI::GetDefaultXDisplay(); 324 return base::MessagePumpForUI::GetDefaultXDisplay();
313 } 325 }
314 326
(...skipping 665 matching lines...) Expand 10 before | Expand all | Expand 10 after
980 } 992 }
981 993
982 void FreePicture(Display* display, XID picture) { 994 void FreePicture(Display* display, XID picture) {
983 XRenderFreePicture(display, picture); 995 XRenderFreePicture(display, picture);
984 } 996 }
985 997
986 void FreePixmap(Display* display, XID pixmap) { 998 void FreePixmap(Display* display, XID pixmap) {
987 XFreePixmap(display, pixmap); 999 XFreePixmap(display, pixmap);
988 } 1000 }
989 1001
1002 bool GetOutputDeviceHandles(std::vector<XID>* outputs) {
1003 DCHECK(outputs);
1004 outputs->clear();
1005
1006 Display* display = GetXDisplay();
1007 if (!display)
Daniel Erat 2012/08/15 14:13:04 is this needed? is a display handle ever not avai
Jun Mukai 2012/08/16 07:52:18 oh, I looked over the files and nobody in this fil
1008 return false;
1009
1010 if (!IsRandRAvailable(display))
1011 return false;
1012
1013 Window root_window = DefaultRootWindow(display);
1014 XRRScreenResources* screen_resources =
1015 XRRGetScreenResources(display, root_window);
1016 for (int i = 0; i < screen_resources->noutput; ++i)
1017 outputs->push_back(screen_resources->outputs[i]);
1018 XRRFreeScreenResources(screen_resources);
1019 return true;
1020 }
1021
1022 bool GetOutputDeviceData(XID output,
1023 uint16* manufacturer_id,
1024 uint32* serial_number,
1025 std::string* human_readable_name) {
1026 DCHECK(manufacturer_id);
1027 DCHECK(serial_number);
1028 DCHECK(human_readable_name);
1029
1030 Display* display = GetXDisplay();
1031 if (!display)
Daniel Erat 2012/08/15 14:13:04 ditto here
Jun Mukai 2012/08/16 07:52:18 Done.
1032 return false;
1033
1034 if (!IsRandRAvailable(display))
1035 return false;
1036
1037 Atom edid_property = GetAtom(RR_PROPERTY_RANDR_EDID);
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, output, edid_property, 0, 128, false, false,
Daniel Erat 2012/08/15 14:13:04 nit: add comments documenting what the constants a
Jun Mukai 2012/08/16 07:52:18 Done.
1057 AnyPropertyType, &actual_type, &actual_format, &nitems,
1058 &bytes_after, &prop);
1059 DCHECK(actual_type == XA_INTEGER);
Daniel Erat 2012/08/15 14:13:04 nit: DCHECK_EQ()?
Jun Mukai 2012/08/16 07:52:18 Done.
1060 DCHECK(actual_format == 8);
1061
Daniel Erat 2012/08/15 14:13:04 check the length of the returned data here and bai
Jun Mukai 2012/08/17 05:50:09 Done.
1062 // See http://en.wikipedia.org/wiki/Extended_display_identification_data
1063 // for the details of EDID data format. We use the following data:
1064 // bytes 8-9: manufacturer EISA ID, in big-endian
1065 // bytes 12-15: represents serial number, in little-endian
1066 // bytes 54-125: four descriptors (18-bytes each) which may contain
1067 // the display name.
1068 const int kManufacturerOffset = 8;
1069 const int kSerialNumberOffset = 12;
1070 const int kDescriptorOffset = 54;
1071 const int kNumDescriptors = 4;
1072 const int kDescriptorLength = 18;
1073 // The specifier types.
1074 const int kMonitorNameDescriptor = 0xfc;
1075 const int kUnspecifiedTextDescriptor = 0xfe;
1076
1077 *manufacturer_id = *reinterpret_cast<uint16*>(prop + kManufacturerOffset);
1078 #if defined(ARCH_CPU_LITTLE_ENDIAN)
1079 *manufacturer_id = base::ByteSwap(*manufacturer_id);
1080 #endif
1081 *serial_number = base::ByteSwapToLE32(
1082 *reinterpret_cast<uint32*>(prop + kSerialNumberOffset));
1083
1084 std::string name_candidate;
1085 human_readable_name->clear();
1086 for (int i = 0; i < kNumDescriptors; ++i) {
1087 unsigned char* desc_buf = prop + kDescriptorOffset + i * kDescriptorLength;
1088 // If the descriptor contains the display name, it has the following
1089 // structure:
1090 // bytes 0-2, 4: \0
1091 // byte 3: descriptor type, defined above.
1092 // bytes 5-17: text data, ending with \r, padding with spaces
1093 // we should check bytes 0-2 and 4, since it may have other values in
1094 // case that the descriptor contains other type of data.
1095 if (desc_buf[0] == 0 && desc_buf[1] == 0 && desc_buf[2] == 0 &&
1096 desc_buf[4] == 0) {
1097 if (desc_buf[3] == kMonitorNameDescriptor) {
1098 std::string found_name(
1099 reinterpret_cast<char*>(desc_buf + 5), kDescriptorLength - 5);
1100 TrimWhitespaceASCII(found_name, TRIM_TRAILING, human_readable_name);
1101 break;
1102 } else if (desc_buf[3] == kUnspecifiedTextDescriptor &&
1103 name_candidate.empty()) {
1104 // Sometimes the default display of a laptop device doesn't have "FC"
1105 // ("Monitor name") descriptor, but has some human readable text with
1106 // "FE" ("Unspecified text"). Thus here uses this value as the fallback
Daniel Erat 2012/08/15 14:13:04 nit: s/uses/use/
Jun Mukai 2012/08/17 05:50:09 Done.
1107 // of the lack of "FC". Note that multiple descriptors may have "FE",
Daniel Erat 2012/08/15 14:13:04 nit: s/of the lack of "FC"/if "FC" is missing/
Jun Mukai 2012/08/17 05:50:09 Done.
1108 // and the first one is the monitor name.
1109 std::string found_name(
1110 reinterpret_cast<char*>(desc_buf + 5), kDescriptorLength - 5);
1111 TrimWhitespaceASCII(found_name, TRIM_TRAILING, &name_candidate);
1112 }
1113 }
1114 }
1115 if (human_readable_name->empty() && !name_candidate.empty())
1116 *human_readable_name = name_candidate;
1117
1118 bool succeed = true;
Daniel Erat 2012/08/15 14:13:04 nit: s/succeed/success/ just do: bool success =
Jun Mukai 2012/08/17 05:50:09 Done.
1119 // Verify the |human_readable_name| in case.
Daniel Erat 2012/08/15 14:13:04 nit: this sentence ends a bit prematurely -- in ca
Jun Mukai 2012/08/17 05:50:09 Done.
1120 if (human_readable_name->empty())
1121 succeed = false;
1122 for (size_t i = 0; i < human_readable_name->size(); ++i) {
1123 char c = (*human_readable_name)[i];
1124 if (!isascii(c) || !isprint(c)) {
1125 human_readable_name->empty();
1126 succeed = false;
1127 break;
1128 }
1129 }
1130
1131 XFree(prop);
1132 return succeed;
1133 }
1134
1135
990 bool GetWindowManagerName(std::string* wm_name) { 1136 bool GetWindowManagerName(std::string* wm_name) {
991 DCHECK(wm_name); 1137 DCHECK(wm_name);
992 int wm_window = 0; 1138 int wm_window = 0;
993 if (!GetIntProperty(GetX11RootWindow(), 1139 if (!GetIntProperty(GetX11RootWindow(),
994 "_NET_SUPPORTING_WM_CHECK", 1140 "_NET_SUPPORTING_WM_CHECK",
995 &wm_window)) { 1141 &wm_window)) {
996 return false; 1142 return false;
997 } 1143 }
998 1144
999 // It's possible that a window manager started earlier in this X session left 1145 // 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) << ", " 1424 << "request_code " << static_cast<int>(error_event.request_code) << ", "
1279 << "minor_code " << static_cast<int>(error_event.minor_code) 1425 << "minor_code " << static_cast<int>(error_event.minor_code)
1280 << " (" << request_str << ")"; 1426 << " (" << request_str << ")";
1281 } 1427 }
1282 1428
1283 // ---------------------------------------------------------------------------- 1429 // ----------------------------------------------------------------------------
1284 // End of x11_util_internal.h 1430 // End of x11_util_internal.h
1285 1431
1286 1432
1287 } // namespace ui 1433 } // 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