1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/widget/desktop_aura/desktop_screen_x11.h"
#include <X11/extensions/Xrandr.h>
#include <X11/Xlib.h>
// It clashes with out RootWindow.
#undef RootWindow
#include "base/debug/trace_event.h"
#include "base/logging.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/layout.h"
#include "ui/display/util/display_util.h"
#include "ui/display/util/x11/edid_parser_x11.h"
#include "ui/events/platform/platform_event_source.h"
#include "ui/gfx/display.h"
#include "ui/gfx/display_observer.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/screen.h"
#include "ui/gfx/x/x11_types.h"
#include "ui/views/widget/desktop_aura/desktop_screen.h"
#include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h"
#include "ui/views/widget/desktop_aura/x11_topmost_window_finder.h"
namespace {
// The delay to perform configuration after RRNotify. See the comment
// in |Dispatch()|.
const int64 kConfigureDelayMs = 500;
float GetDeviceScaleFactor(int screen_pixels, int screen_mm) {
const int kCSSDefaultDPI = 96;
const float kInchInMm = 25.4f;
float screen_inches = screen_mm / kInchInMm;
float screen_dpi = screen_pixels / screen_inches;
float scale = screen_dpi / kCSSDefaultDPI;
return ui::GetImageScale(ui::GetSupportedScaleFactor(scale));
}
std::vector<gfx::Display> GetFallbackDisplayList() {
::XDisplay* display = gfx::GetXDisplay();
::Screen* screen = DefaultScreenOfDisplay(display);
int width = WidthOfScreen(screen);
int height = HeightOfScreen(screen);
gfx::Size physical_size(WidthMMOfScreen(screen), HeightMMOfScreen(screen));
gfx::Rect bounds_in_pixels(0, 0, width, height);
gfx::Display gfx_display(0, bounds_in_pixels);
if (!gfx::Display::HasForceDeviceScaleFactor() &&
!ui::IsDisplaySizeBlackListed(physical_size)) {
float device_scale_factor = GetDeviceScaleFactor(
width, physical_size.width());
DCHECK_LE(1.0f, device_scale_factor);
gfx_display.SetScaleAndBounds(device_scale_factor, bounds_in_pixels);
}
return std::vector<gfx::Display>(1, gfx_display);
}
} // namespace
namespace views {
////////////////////////////////////////////////////////////////////////////////
// DesktopScreenX11, public:
DesktopScreenX11::DesktopScreenX11()
: xdisplay_(gfx::GetXDisplay()),
x_root_window_(DefaultRootWindow(xdisplay_)),
has_xrandr_(false),
xrandr_event_base_(0) {
// We only support 1.3+. There were library changes before this and we should
// use the new interface instead of the 1.2 one.
int randr_version_major = 0;
int randr_version_minor = 0;
has_xrandr_ = XRRQueryVersion(
xdisplay_, &randr_version_major, &randr_version_minor) &&
randr_version_major == 1 &&
randr_version_minor >= 3;
if (has_xrandr_) {
int error_base_ignored = 0;
XRRQueryExtension(xdisplay_, &xrandr_event_base_, &error_base_ignored);
if (ui::PlatformEventSource::GetInstance())
ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this);
XRRSelectInput(xdisplay_,
x_root_window_,
RRScreenChangeNotifyMask |
RROutputChangeNotifyMask |
RRCrtcChangeNotifyMask);
displays_ = BuildDisplaysFromXRandRInfo();
} else {
displays_ = GetFallbackDisplayList();
}
}
DesktopScreenX11::~DesktopScreenX11() {
if (has_xrandr_ && ui::PlatformEventSource::GetInstance())
ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
}
void DesktopScreenX11::ProcessDisplayChange(
const std::vector<gfx::Display>& incoming) {
std::vector<gfx::Display> old_displays = displays_;
displays_ = incoming;
typedef std::vector<gfx::Display>::const_iterator DisplayIt;
std::vector<gfx::Display>::const_iterator old_it = old_displays.begin();
for (; old_it != old_displays.end(); ++old_it) {
bool found = false;
for (std::vector<gfx::Display>::const_iterator new_it =
displays_.begin(); new_it != displays_.end(); ++new_it) {
if (old_it->id() == new_it->id()) {
found = true;
break;
}
}
if (!found) {
FOR_EACH_OBSERVER(gfx::DisplayObserver, observer_list_,
OnDisplayRemoved(*old_it));
}
}
std::vector<gfx::Display>::const_iterator new_it = displays_.begin();
for (; new_it != displays_.end(); ++new_it) {
bool found = false;
for (std::vector<gfx::Display>::const_iterator old_it =
old_displays.begin(); old_it != old_displays.end(); ++old_it) {
if (new_it->id() != old_it->id())
continue;
uint32_t metrics = gfx::DisplayObserver::DISPLAY_METRIC_NONE;
if (new_it->bounds() != old_it->bounds())
metrics |= gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS;
if (new_it->rotation() != old_it->rotation())
metrics |= gfx::DisplayObserver::DISPLAY_METRIC_ROTATION;
if (new_it->work_area() != old_it->work_area())
metrics |= gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA;
if (new_it->device_scale_factor() != old_it->device_scale_factor())
metrics |= gfx::DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR;
if (metrics != gfx::DisplayObserver::DISPLAY_METRIC_NONE) {
FOR_EACH_OBSERVER(gfx::DisplayObserver,
observer_list_,
OnDisplayMetricsChanged(*new_it, metrics));
}
found = true;
break;
}
if (!found) {
FOR_EACH_OBSERVER(gfx::DisplayObserver, observer_list_,
OnDisplayAdded(*new_it));
}
}
}
////////////////////////////////////////////////////////////////////////////////
// DesktopScreenX11, gfx::Screen implementation:
bool DesktopScreenX11::IsDIPEnabled() {
return true;
}
gfx::Point DesktopScreenX11::GetCursorScreenPoint() {
TRACE_EVENT0("views", "DesktopScreenX11::GetCursorScreenPoint()");
XDisplay* display = gfx::GetXDisplay();
::Window root, child;
int root_x, root_y, win_x, win_y;
unsigned int mask;
XQueryPointer(display,
DefaultRootWindow(display),
&root,
&child,
&root_x,
&root_y,
&win_x,
&win_y,
&mask);
return gfx::Point(root_x, root_y);
}
gfx::NativeWindow DesktopScreenX11::GetWindowUnderCursor() {
return GetWindowAtScreenPoint(GetCursorScreenPoint());
}
gfx::NativeWindow DesktopScreenX11::GetWindowAtScreenPoint(
const gfx::Point& point) {
X11TopmostWindowFinder finder;
return finder.FindLocalProcessWindowAt(point, std::set<aura::Window*>());
}
int DesktopScreenX11::GetNumDisplays() const {
return displays_.size();
}
std::vector<gfx::Display> DesktopScreenX11::GetAllDisplays() const {
return displays_;
}
gfx::Display DesktopScreenX11::GetDisplayNearestWindow(
gfx::NativeView window) const {
// Getting screen bounds here safely is hard.
//
// You'd think we'd be able to just call window->GetBoundsInScreen(), but we
// can't because |window| (and the associated WindowEventDispatcher*) can be
// partially initialized at this point; WindowEventDispatcher initializations
// call through into GetDisplayNearestWindow(). But the X11 resources are
// created before we create the aura::WindowEventDispatcher. So we ask what
// the DRWHX11 believes the window bounds are instead of going through the
// aura::Window's screen bounds.
aura::WindowTreeHost* host = window->GetHost();
if (host) {
DesktopWindowTreeHostX11* rwh = DesktopWindowTreeHostX11::GetHostForXID(
host->GetAcceleratedWidget());
if (rwh)
return GetDisplayMatching(rwh->GetX11RootWindowBounds());
}
return GetPrimaryDisplay();
}
gfx::Display DesktopScreenX11::GetDisplayNearestPoint(
const gfx::Point& point) const {
for (std::vector<gfx::Display>::const_iterator it = displays_.begin();
it != displays_.end(); ++it) {
if (it->bounds().Contains(point))
return *it;
}
return GetPrimaryDisplay();
}
gfx::Display DesktopScreenX11::GetDisplayMatching(
const gfx::Rect& match_rect) const {
int max_area = 0;
const gfx::Display* matching = NULL;
for (std::vector<gfx::Display>::const_iterator it = displays_.begin();
it != displays_.end(); ++it) {
gfx::Rect intersect = gfx::IntersectRects(it->bounds(), match_rect);
int area = intersect.width() * intersect.height();
if (area > max_area) {
max_area = area;
matching = &*it;
}
}
// Fallback to the primary display if there is no matching display.
return matching ? *matching : GetPrimaryDisplay();
}
gfx::Display DesktopScreenX11::GetPrimaryDisplay() const {
return displays_.front();
}
void DesktopScreenX11::AddObserver(gfx::DisplayObserver* observer) {
observer_list_.AddObserver(observer);
}
void DesktopScreenX11::RemoveObserver(gfx::DisplayObserver* observer) {
observer_list_.RemoveObserver(observer);
}
bool DesktopScreenX11::CanDispatchEvent(const ui::PlatformEvent& event) {
return event->type - xrandr_event_base_ == RRScreenChangeNotify ||
event->type - xrandr_event_base_ == RRNotify;
}
uint32_t DesktopScreenX11::DispatchEvent(const ui::PlatformEvent& event) {
if (event->type - xrandr_event_base_ == RRScreenChangeNotify) {
// Pass the event through to xlib.
XRRUpdateConfiguration(event);
} else if (event->type - xrandr_event_base_ == RRNotify) {
// There's some sort of observer dispatch going on here, but I don't think
// it's the screen's?
if (configure_timer_.get() && configure_timer_->IsRunning()) {
configure_timer_->Reset();
} else {
configure_timer_.reset(new base::OneShotTimer<DesktopScreenX11>());
configure_timer_->Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kConfigureDelayMs),
this,
&DesktopScreenX11::ConfigureTimerFired);
}
} else {
NOTREACHED();
}
return ui::POST_DISPATCH_NONE;
}
////////////////////////////////////////////////////////////////////////////////
// DesktopScreenX11, private:
DesktopScreenX11::DesktopScreenX11(
const std::vector<gfx::Display>& test_displays)
: xdisplay_(gfx::GetXDisplay()),
x_root_window_(DefaultRootWindow(xdisplay_)),
has_xrandr_(false),
xrandr_event_base_(0),
displays_(test_displays) {
}
std::vector<gfx::Display> DesktopScreenX11::BuildDisplaysFromXRandRInfo() {
std::vector<gfx::Display> displays;
XRRScreenResources* resources =
XRRGetScreenResourcesCurrent(xdisplay_, x_root_window_);
if (!resources) {
LOG(ERROR) << "XRandR returned no displays. Falling back to Root Window.";
return GetFallbackDisplayList();
}
bool has_work_area = false;
gfx::Rect work_area;
std::vector<int> value;
if (ui::GetIntArrayProperty(x_root_window_, "_NET_WORKAREA", &value) &&
value.size() >= 4) {
work_area = gfx::Rect(value[0], value[1], value[2], value[3]);
has_work_area = true;
}
float device_scale_factor = 1.0f;
for (int i = 0; i < resources->noutput; ++i) {
RROutput output_id = resources->outputs[i];
XRROutputInfo* output_info =
XRRGetOutputInfo(xdisplay_, resources, output_id);
bool is_connected = (output_info->connection == RR_Connected);
if (!is_connected) {
XRRFreeOutputInfo(output_info);
continue;
}
if (output_info->crtc) {
XRRCrtcInfo *crtc = XRRGetCrtcInfo(xdisplay_,
resources,
output_info->crtc);
int64 display_id = -1;
if (!ui::GetDisplayId(output_id, static_cast<uint8>(i), &display_id)) {
// It isn't ideal, but if we can't parse the EDID data, fallback on the
// display number.
display_id = i;
}
gfx::Rect crtc_bounds(crtc->x, crtc->y, crtc->width, crtc->height);
gfx::Display display(display_id, crtc_bounds);
if (!gfx::Display::HasForceDeviceScaleFactor()) {
if (i == 0 && !ui::IsDisplaySizeBlackListed(
gfx::Size(output_info->mm_width, output_info->mm_height))) {
// As per display scale factor is not supported right now,
// the primary display's scale factor is always used.
device_scale_factor = GetDeviceScaleFactor(crtc->width,
output_info->mm_width);
DCHECK_LE(1.0f, device_scale_factor);
}
display.SetScaleAndBounds(device_scale_factor, crtc_bounds);
}
if (has_work_area) {
gfx::Rect intersection = crtc_bounds;
intersection.Intersect(work_area);
display.set_work_area(intersection);
}
displays.push_back(display);
XRRFreeCrtcInfo(crtc);
}
XRRFreeOutputInfo(output_info);
}
XRRFreeScreenResources(resources);
if (displays.empty())
return GetFallbackDisplayList();
return displays;
}
void DesktopScreenX11::ConfigureTimerFired() {
std::vector<gfx::Display> new_displays = BuildDisplaysFromXRandRInfo();
ProcessDisplayChange(new_displays);
}
////////////////////////////////////////////////////////////////////////////////
gfx::Screen* CreateDesktopScreen() {
return new DesktopScreenX11;
}
} // namespace views
|