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/monitor_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/dispatcher_linux.h" | |
16 #include "ui/aura/env.h" | |
17 #include "ui/aura/monitor_manager.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/monitor/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 } // namespace | |
53 | |
54 MonitorChangeObserverX11::MonitorChangeObserverX11() | |
55 : xdisplay_(base::MessagePumpAuraX11::GetDefaultXDisplay()), | |
56 x_root_window_(DefaultRootWindow(xdisplay_)), | |
57 xrandr_event_base_(0) { | |
58 int error_base_ignored; | |
59 XRRQueryExtension(xdisplay_, &xrandr_event_base_, &error_base_ignored); | |
60 static_cast<DispatcherLinux*>(Env::GetInstance()->GetDispatcher())-> | |
61 AddDispatcherForRootWindow(this); | |
62 } | |
63 | |
64 MonitorChangeObserverX11::~MonitorChangeObserverX11() { | |
65 static_cast<DispatcherLinux*>(Env::GetInstance()->GetDispatcher())-> | |
66 RemoveDispatcherForRootWindow(this); | |
67 } | |
68 | |
69 bool MonitorChangeObserverX11::Dispatch(const base::NativeEvent& event) { | |
70 if (event->type - xrandr_event_base_ == RRScreenChangeNotify) { | |
71 NotifyDisplayChange(); | |
72 } | |
73 return true; | |
74 } | |
75 | |
76 void MonitorChangeObserverX11::NotifyDisplayChange() { | |
77 if (!MonitorManager::use_fullscreen_host_window()) | |
78 return; // Use the default monitor that monitor manager determined. | |
79 | |
80 XRRScreenResources* screen_resources = | |
81 XRRGetScreenResources(xdisplay_, x_root_window_); | |
82 std::map<XID, XRRCrtcInfo*> crtc_info_map; | |
83 | |
84 for (int c = 0; c < screen_resources->ncrtc; c++) { | |
85 XID crtc_id = screen_resources->crtcs[c]; | |
86 XRRCrtcInfo *crtc_info = | |
87 XRRGetCrtcInfo(xdisplay_, screen_resources, crtc_id); | |
88 crtc_info_map[crtc_id] = crtc_info; | |
89 } | |
90 | |
91 std::vector<gfx::Display> displays; | |
92 std::set<int> y_coords; | |
93 for (int o = 0; o < screen_resources->noutput; o++) { | |
94 XRROutputInfo *output_info = | |
95 XRRGetOutputInfo(xdisplay_, | |
96 screen_resources, | |
97 screen_resources->outputs[o]); | |
98 if (output_info->connection != RR_Connected) { | |
99 XRRFreeOutputInfo(output_info); | |
100 continue; | |
101 } | |
102 XRRCrtcInfo* crtc_info = crtc_info_map[output_info->crtc]; | |
103 if (!crtc_info) { | |
104 LOG(WARNING) << "Crtc not found for output: output=" << o; | |
105 continue; | |
106 } | |
107 XRRModeInfo* mode = FindMode(screen_resources, crtc_info->mode); | |
108 if (!mode) { | |
109 LOG(WARNING) << "Could not find a mode for the output: output=" << o; | |
110 continue; | |
111 } | |
112 // Mirrored monitors have the same y coordinates. | |
113 if (y_coords.find(crtc_info->y) != y_coords.end()) | |
114 continue; | |
115 // TODO(oshima): Create unique ID for the monitor. | |
116 displays.push_back(gfx::Display( | |
117 0, | |
118 gfx::Rect(crtc_info->x, crtc_info->y, mode->width, mode->height))); | |
119 | |
120 float device_scale_factor = 1.0f; | |
121 if (output_info->mm_width > 0 && | |
122 (kInchInMm * mode->width / output_info->mm_width) > | |
123 kHighDensityDIPThreshold) { | |
124 device_scale_factor = 2.0f; | |
125 } | |
126 displays.back().set_device_scale_factor(device_scale_factor); | |
127 y_coords.insert(crtc_info->y); | |
128 XRRFreeOutputInfo(output_info); | |
129 } | |
130 | |
131 // Free all allocated resources. | |
132 for (std::map<XID, XRRCrtcInfo*>::const_iterator iter = crtc_info_map.begin(); | |
133 iter != crtc_info_map.end(); ++iter) { | |
134 XRRFreeCrtcInfo(iter->second); | |
135 } | |
136 XRRFreeScreenResources(screen_resources); | |
137 | |
138 // PowerManager lays out the outputs vertically. Sort them by Y | |
139 // coordinates. | |
140 std::sort(displays.begin(), displays.end(), CompareDisplayY); | |
141 // TODO(oshima): Assisgn index as ID for now. Use unique ID. | |
142 int id = 0; | |
143 for (std::vector<gfx::Display>::iterator iter = displays.begin(); | |
144 iter != displays.end(); ++iter, ++id) | |
145 (*iter).set_id(id); | |
146 | |
147 Env::GetInstance()->monitor_manager()->OnNativeMonitorsChanged(displays); | |
148 } | |
149 | |
150 } // namespace internal | |
151 } // namespace aura | |
OLD | NEW |