OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/aura/display_change_observer_x11.h" | |
6 | |
7 #include <algorithm> | |
8 #include <map> | |
9 #include <set> | |
10 #include <vector> | |
11 | |
12 #include <X11/extensions/Xrandr.h> | |
13 | |
14 #include "base/message_pump_aurax11.h" | |
15 #include "ui/aura/env.h" | |
16 #include "ui/aura/display_manager.h" | |
17 #include "ui/base/x/x11_util.h" | |
18 #include "ui/compositor/dip_util.h" | |
19 #include "ui/gfx/display.h" | |
20 | |
21 namespace aura { | |
22 namespace internal { | |
23 | |
24 namespace { | |
25 | |
26 // The DPI threshold to detect high density screen. | |
27 // Higher DPI than this will use device_scale_factor=2. | |
28 // Note: This value has to be kept in sync with the mouse/touchpad driver | |
29 // which controls mouse pointer acceleration. If you need to update this value, | |
30 // please update the bug (crosbug.com/31628) first and make sure that the | |
31 // driver will use the same value. | |
32 // This value also has to be kept in sync with the value in | |
33 // chromeos/display/output_configurator.cc. See crbug.com/130188 | |
34 const unsigned int kHighDensityDIPThreshold = 160; | |
35 | |
36 // 1 inch in mm. | |
37 const float kInchInMm = 25.4f; | |
38 | |
39 XRRModeInfo* FindMode(XRRScreenResources* screen_resources, XID current_mode) { | |
40 for (int m = 0; m < screen_resources->nmode; m++) { | |
41 XRRModeInfo *mode = &screen_resources->modes[m]; | |
42 if (mode->id == current_mode) | |
43 return mode; | |
44 } | |
45 return NULL; | |
46 } | |
47 | |
48 bool CompareDisplayY(const gfx::Display& lhs, const gfx::Display& rhs) { | |
49 return lhs.bounds_in_pixel().y() < rhs.bounds_in_pixel().y(); | |
50 } | |
51 | |
52 // A list of bogus sizes in mm that X detects and should be ignored. | |
53 // See crbug.com/136533. | |
54 const unsigned long kInvalidDisplaySizeList[][2] = { | |
55 {160, 100}, | |
56 {160, 90}, | |
57 {50, 40}, | |
58 {40, 30}, | |
59 }; | |
60 | |
61 // Returns true if the size nifo in the output_info isn't valid | |
62 // and should be ignored. | |
63 bool ShouldIgnoreSize(XRROutputInfo *output_info) { | |
64 if (output_info->mm_width == 0 || output_info->mm_height == 0) { | |
65 LOG(WARNING) << "No display size available"; | |
66 return true; | |
67 } | |
68 for (unsigned long i = 0 ; i < arraysize(kInvalidDisplaySizeList); ++i) { | |
69 const unsigned long* size = kInvalidDisplaySizeList[i]; | |
70 if (output_info->mm_width == size[0] && output_info->mm_height == size[1]) { | |
71 LOG(WARNING) << "Black listed display size detected:" | |
72 << size[0] << "x" << size[1]; | |
73 return true; | |
74 } | |
75 } | |
76 return false; | |
77 } | |
78 | |
79 } // namespace | |
80 | |
81 DisplayChangeObserverX11::DisplayChangeObserverX11() | |
82 : xdisplay_(base::MessagePumpAuraX11::GetDefaultXDisplay()), | |
83 x_root_window_(DefaultRootWindow(xdisplay_)), | |
84 xrandr_event_base_(0) { | |
85 int error_base_ignored; | |
86 XRRQueryExtension(xdisplay_, &xrandr_event_base_, &error_base_ignored); | |
87 base::MessagePumpAuraX11::Current()->AddDispatcherForRootWindow(this); | |
88 } | |
89 | |
90 DisplayChangeObserverX11::~DisplayChangeObserverX11() { | |
91 base::MessagePumpAuraX11::Current()->RemoveDispatcherForRootWindow(this); | |
92 } | |
93 | |
94 bool DisplayChangeObserverX11::Dispatch(const base::NativeEvent& event) { | |
95 if (event->type - xrandr_event_base_ == RRScreenChangeNotify) { | |
96 NotifyDisplayChange(); | |
97 } | |
98 return true; | |
99 } | |
100 | |
101 void DisplayChangeObserverX11::NotifyDisplayChange() { | |
102 if (!DisplayManager::use_fullscreen_host_window()) | |
103 return; // Use the default display that display manager determined. | |
104 | |
105 XRRScreenResources* screen_resources = | |
106 XRRGetScreenResources(xdisplay_, x_root_window_); | |
107 std::map<XID, XRRCrtcInfo*> crtc_info_map; | |
108 | |
109 for (int c = 0; c < screen_resources->ncrtc; c++) { | |
110 XID crtc_id = screen_resources->crtcs[c]; | |
111 XRRCrtcInfo *crtc_info = | |
112 XRRGetCrtcInfo(xdisplay_, screen_resources, crtc_id); | |
113 crtc_info_map[crtc_id] = crtc_info; | |
114 } | |
115 | |
116 std::vector<gfx::Display> displays; | |
117 std::set<int> y_coords; | |
118 std::set<int64> ids; | |
119 for (int o = 0; o < screen_resources->noutput; o++) { | |
120 XRROutputInfo *output_info = | |
121 XRRGetOutputInfo(xdisplay_, | |
122 screen_resources, | |
123 screen_resources->outputs[o]); | |
124 if (output_info->connection != RR_Connected) { | |
125 XRRFreeOutputInfo(output_info); | |
126 continue; | |
127 } | |
128 XRRCrtcInfo* crtc_info = crtc_info_map[output_info->crtc]; | |
129 if (!crtc_info) { | |
130 LOG(WARNING) << "Crtc not found for output: output=" << o; | |
131 continue; | |
132 } | |
133 XRRModeInfo* mode = FindMode(screen_resources, crtc_info->mode); | |
134 if (!mode) { | |
135 LOG(WARNING) << "Could not find a mode for the output: output=" << o; | |
136 continue; | |
137 } | |
138 // Mirrored monitors have the same y coordinates. | |
139 if (y_coords.find(crtc_info->y) != y_coords.end()) | |
140 continue; | |
141 displays.push_back(gfx::Display()); | |
142 | |
143 float device_scale_factor = 1.0f; | |
144 if (!ShouldIgnoreSize(output_info) && | |
145 (kInchInMm * mode->width / output_info->mm_width) > | |
146 kHighDensityDIPThreshold) { | |
147 device_scale_factor = 2.0f; | |
148 } | |
149 displays.back().SetScaleAndBounds( | |
150 device_scale_factor, | |
151 gfx::Rect(crtc_info->x, crtc_info->y, mode->width, mode->height)); | |
152 | |
153 uint16 manufacturer_id = 0; | |
154 uint32 serial_number = 0; | |
155 if (ui::GetOutputDeviceData(screen_resources->outputs[o], &manufacturer_id, | |
156 &serial_number, NULL) && manufacturer_id != 0) { | |
157 // An ID based on display's index will be assigned later if this call | |
158 // fails. | |
159 int64 new_id = gfx::Display::GetID(manufacturer_id, serial_number); | |
160 if (ids.find(new_id) == ids.end()) { | |
161 displays.back().set_id(new_id); | |
162 ids.insert(new_id); | |
163 } | |
164 } | |
165 | |
166 y_coords.insert(crtc_info->y); | |
167 XRRFreeOutputInfo(output_info); | |
168 } | |
169 | |
170 // Free all allocated resources. | |
171 for (std::map<XID, XRRCrtcInfo*>::const_iterator iter = crtc_info_map.begin(); | |
172 iter != crtc_info_map.end(); ++iter) { | |
173 XRRFreeCrtcInfo(iter->second); | |
174 } | |
175 XRRFreeScreenResources(screen_resources); | |
176 | |
177 // PowerManager lays out the outputs vertically. Sort them by Y | |
178 // coordinates. | |
179 std::sort(displays.begin(), displays.end(), CompareDisplayY); | |
180 int64 id = 0; | |
181 for (std::vector<gfx::Display>::iterator iter = displays.begin(); | |
182 iter != displays.end(); ++iter) { | |
183 if (iter->id() == gfx::Display::kInvalidDisplayID) { | |
184 iter->set_id(id); | |
185 ++id; | |
186 } | |
187 } | |
188 | |
189 Env::GetInstance()->display_manager()->OnNativeDisplaysChanged(displays); | |
190 } | |
191 | |
192 } // namespace internal | |
193 } // namespace aura | |
OLD | NEW |