15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "ui/views/widget/desktop_aura/desktop_screen_x11.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include <X11/extensions/Xrandr.h>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <X11/Xlib.h>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// It clashes with out RootWindow.
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef RootWindow
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/debug/trace_event.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/aura/window.h"
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/window_event_dispatcher.h"
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/aura/window_tree_host.h"
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/base/layout.h"
190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "ui/display/util/display_util.h"
200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "ui/display/util/x11/edid_parser_x11.h"
21c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "ui/events/platform/platform_event_source.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/display.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/native_widget_types.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/screen.h"
2568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "ui/gfx/x/x11_types.h"
2668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "ui/views/widget/desktop_aura/desktop_screen.h"
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h"
28010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "ui/views/widget/desktop_aura/x11_topmost_window_finder.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)// The delay to perform configuration after RRNotify.  See the comment
3368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)// in |Dispatch()|.
3468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const int64 kConfigureDelayMs = 500;
3568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
36f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// TODO(oshima): Consider using gtk-xft-dpi instead.
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)float GetDeviceScaleFactor(int screen_pixels, int screen_mm) {
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const int kCSSDefaultDPI = 96;
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const float kInchInMm = 25.4f;
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
41f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  float screen_inches = screen_mm / kInchInMm;
42f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  float screen_dpi = screen_pixels / screen_inches;
43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  float scale = screen_dpi / kCSSDefaultDPI;
44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
45f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactor(scale));
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
4868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)std::vector<gfx::Display> GetFallbackDisplayList() {
4968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ::XDisplay* display = gfx::GetXDisplay();
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ::Screen* screen = DefaultScreenOfDisplay(display);
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int width = WidthOfScreen(screen);
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int height = HeightOfScreen(screen);
53c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  gfx::Size physical_size(WidthMMOfScreen(screen), HeightMMOfScreen(screen));
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Rect bounds_in_pixels(0, 0, width, height);
56f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Display gfx_display(0, bounds_in_pixels);
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!gfx::Display::HasForceDeviceScaleFactor() &&
58c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      !ui::IsDisplaySizeBlackListed(physical_size)) {
59c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    float device_scale_factor = GetDeviceScaleFactor(
60c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        width, physical_size.width());
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    DCHECK_LE(1.0f, device_scale_factor);
62f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    gfx_display.SetScaleAndBounds(device_scale_factor, bounds_in_pixels);
63f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
65f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return std::vector<gfx::Display>(1, gfx_display);
6668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
6768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
6868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}  // namespace
6968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
7068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)namespace views {
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DesktopScreenX11, public:
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)DesktopScreenX11::DesktopScreenX11()
76a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    : xdisplay_(gfx::GetXDisplay()),
7768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      x_root_window_(DefaultRootWindow(xdisplay_)),
7868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      has_xrandr_(false),
7968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      xrandr_event_base_(0) {
8068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // We only support 1.3+. There were library changes before this and we should
8168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // use the new interface instead of the 1.2 one.
8268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int randr_version_major = 0;
8368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int randr_version_minor = 0;
8468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  has_xrandr_ = XRRQueryVersion(
8568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        xdisplay_, &randr_version_major, &randr_version_minor) &&
8668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      randr_version_major == 1 &&
8768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      randr_version_minor >= 3;
8868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
8968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  if (has_xrandr_) {
9068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    int error_base_ignored = 0;
9168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    XRRQueryExtension(xdisplay_, &xrandr_event_base_, &error_base_ignored);
9268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
93c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    if (ui::PlatformEventSource::GetInstance())
94c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this);
9568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    XRRSelectInput(xdisplay_,
9668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                   x_root_window_,
970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                   RRScreenChangeNotifyMask |
980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                   RROutputChangeNotifyMask |
990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                   RRCrtcChangeNotifyMask);
10068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
10168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    displays_ = BuildDisplaysFromXRandRInfo();
10268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  } else {
10368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    displays_ = GetFallbackDisplayList();
10468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DesktopScreenX11::~DesktopScreenX11() {
108c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  if (has_xrandr_ && ui::PlatformEventSource::GetInstance())
109c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
11068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
11168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DesktopScreenX11, gfx::Screen implementation:
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool DesktopScreenX11::IsDIPEnabled() {
116f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return true;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)gfx::Point DesktopScreenX11::GetCursorScreenPoint() {
120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  TRACE_EVENT0("views", "DesktopScreenX11::GetCursorScreenPoint()");
121f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
12268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  XDisplay* display = gfx::GetXDisplay();
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ::Window root, child;
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int root_x, root_y, win_x, win_y;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned int mask;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  XQueryPointer(display,
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                DefaultRootWindow(display),
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                &root,
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                &child,
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                &root_x,
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                &root_y,
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                &win_x,
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                &win_y,
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                &mask);
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return gfx::Point(root_x, root_y);
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
140424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)gfx::NativeWindow DesktopScreenX11::GetWindowUnderCursor() {
14168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  return GetWindowAtScreenPoint(GetCursorScreenPoint());
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
144424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)gfx::NativeWindow DesktopScreenX11::GetWindowAtScreenPoint(
145424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    const gfx::Point& point) {
146010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  X11TopmostWindowFinder finder;
147010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return finder.FindLocalProcessWindowAt(point, std::set<aura::Window*>());
148424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
149424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
150424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)int DesktopScreenX11::GetNumDisplays() const {
15168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  return displays_.size();
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
154424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)std::vector<gfx::Display> DesktopScreenX11::GetAllDisplays() const {
15568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  return displays_;
156424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
157424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)gfx::Display DesktopScreenX11::GetDisplayNearestWindow(
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    gfx::NativeView window) const {
1604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Getting screen bounds here safely is hard.
16168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  //
1624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // You'd think we'd be able to just call window->GetBoundsInScreen(), but we
163a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // can't because |window| (and the associated WindowEventDispatcher*) can be
164a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // partially initialized at this point; WindowEventDispatcher initializations
165a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // call through into GetDisplayNearestWindow(). But the X11 resources are
166a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // created before we create the aura::WindowEventDispatcher. So we ask what
167a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // the DRWHX11 believes the window bounds are instead of going through the
168a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // aura::Window's screen bounds.
169a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  aura::WindowTreeHost* host = window->GetHost();
170a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (host) {
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DesktopWindowTreeHostX11* rwh = DesktopWindowTreeHostX11::GetHostForXID(
172a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        host->GetAcceleratedWidget());
1734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (rwh)
1744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return GetDisplayMatching(rwh->GetX11RootWindowBounds());
1754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
1764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
17768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  return GetPrimaryDisplay();
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)gfx::Display DesktopScreenX11::GetDisplayNearestPoint(
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const gfx::Point& point) const {
18268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  for (std::vector<gfx::Display>::const_iterator it = displays_.begin();
18368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)       it != displays_.end(); ++it) {
18468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    if (it->bounds().Contains(point))
18568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      return *it;
18668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
18768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
18868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  return GetPrimaryDisplay();
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)gfx::Display DesktopScreenX11::GetDisplayMatching(
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const gfx::Rect& match_rect) const {
19368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int max_area = 0;
19468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  const gfx::Display* matching = NULL;
19568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  for (std::vector<gfx::Display>::const_iterator it = displays_.begin();
19668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)       it != displays_.end(); ++it) {
19768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    gfx::Rect intersect = gfx::IntersectRects(it->bounds(), match_rect);
19868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    int area = intersect.width() * intersect.height();
19968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    if (area > max_area) {
20068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      max_area = area;
20168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      matching = &*it;
20268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    }
20368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
20468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Fallback to the primary display if there is no matching display.
20568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  return matching ? *matching : GetPrimaryDisplay();
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)gfx::Display DesktopScreenX11::GetPrimaryDisplay() const {
20968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  return displays_.front();
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DesktopScreenX11::AddObserver(gfx::DisplayObserver* observer) {
2135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  change_notifier_.AddObserver(observer);
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DesktopScreenX11::RemoveObserver(gfx::DisplayObserver* observer) {
2175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  change_notifier_.RemoveObserver(observer);
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
220c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochbool DesktopScreenX11::CanDispatchEvent(const ui::PlatformEvent& event) {
221c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  return event->type - xrandr_event_base_ == RRScreenChangeNotify ||
222c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch         event->type - xrandr_event_base_ == RRNotify;
223c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
224c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
225c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochuint32_t DesktopScreenX11::DispatchEvent(const ui::PlatformEvent& event) {
22668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  if (event->type - xrandr_event_base_ == RRScreenChangeNotify) {
22768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    // Pass the event through to xlib.
22868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    XRRUpdateConfiguration(event);
22968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  } else if (event->type - xrandr_event_base_ == RRNotify) {
23068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    // There's some sort of observer dispatch going on here, but I don't think
23168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    // it's the screen's?
232a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (configure_timer_.get() && configure_timer_->IsRunning()) {
23368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      configure_timer_->Reset();
23468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    } else {
23568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      configure_timer_.reset(new base::OneShotTimer<DesktopScreenX11>());
23668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      configure_timer_->Start(
23768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)          FROM_HERE,
23868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)          base::TimeDelta::FromMilliseconds(kConfigureDelayMs),
23968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)          this,
24068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)          &DesktopScreenX11::ConfigureTimerFired);
24168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    }
242c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  } else {
243c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    NOTREACHED();
24468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
24568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
246c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  return ui::POST_DISPATCH_NONE;
24768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
25068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)// DesktopScreenX11, private:
25168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
25268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)DesktopScreenX11::DesktopScreenX11(
25368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    const std::vector<gfx::Display>& test_displays)
254a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    : xdisplay_(gfx::GetXDisplay()),
25568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      x_root_window_(DefaultRootWindow(xdisplay_)),
25668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      has_xrandr_(false),
25768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      xrandr_event_base_(0),
25868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      displays_(test_displays) {
25968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
26168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)std::vector<gfx::Display> DesktopScreenX11::BuildDisplaysFromXRandRInfo() {
26268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  std::vector<gfx::Display> displays;
26368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  XRRScreenResources* resources =
26468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      XRRGetScreenResourcesCurrent(xdisplay_, x_root_window_);
26568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  if (!resources) {
26668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    LOG(ERROR) << "XRandR returned no displays. Falling back to Root Window.";
26768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    return GetFallbackDisplayList();
26868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
26968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
27068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  bool has_work_area = false;
27168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  gfx::Rect work_area;
27268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  std::vector<int> value;
27368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  if (ui::GetIntArrayProperty(x_root_window_, "_NET_WORKAREA", &value) &&
27468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      value.size() >= 4) {
27568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    work_area = gfx::Rect(value[0], value[1], value[2], value[3]);
27668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    has_work_area = true;
27768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
27868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
279f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  float device_scale_factor = 1.0f;
28068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  for (int i = 0; i < resources->noutput; ++i) {
28168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    RROutput output_id = resources->outputs[i];
28268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    XRROutputInfo* output_info =
28368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        XRRGetOutputInfo(xdisplay_, resources, output_id);
28468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
28568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    bool is_connected = (output_info->connection == RR_Connected);
28668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    if (!is_connected) {
28768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      XRRFreeOutputInfo(output_info);
28868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      continue;
28968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    }
29068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
29168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    if (output_info->crtc) {
29268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      XRRCrtcInfo *crtc = XRRGetCrtcInfo(xdisplay_,
29368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                                         resources,
29468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                                         output_info->crtc);
29568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
29668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      int64 display_id = -1;
297effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      if (!ui::GetDisplayId(output_id, static_cast<uint8>(i), &display_id)) {
29868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        // It isn't ideal, but if we can't parse the EDID data, fallback on the
29968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        // display number.
30068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        display_id = i;
30168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      }
30268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
30368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      gfx::Rect crtc_bounds(crtc->x, crtc->y, crtc->width, crtc->height);
30468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      gfx::Display display(display_id, crtc_bounds);
305f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
306f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      if (!gfx::Display::HasForceDeviceScaleFactor()) {
307c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        if (i == 0 && !ui::IsDisplaySizeBlackListed(
308c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch            gfx::Size(output_info->mm_width, output_info->mm_height))) {
309f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          // As per display scale factor is not supported right now,
310f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          // the primary display's scale factor is always used.
311f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          device_scale_factor = GetDeviceScaleFactor(crtc->width,
312f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                     output_info->mm_width);
313f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          DCHECK_LE(1.0f, device_scale_factor);
314f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        }
315f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        display.SetScaleAndBounds(device_scale_factor, crtc_bounds);
316f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      }
317f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
31868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      if (has_work_area) {
31968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        gfx::Rect intersection = crtc_bounds;
32068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        intersection.Intersect(work_area);
32168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        display.set_work_area(intersection);
32268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      }
32368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
324cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      switch (crtc->rotation) {
325cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        case RR_Rotate_0:
326cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          display.set_rotation(gfx::Display::ROTATE_0);
327cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          break;
328cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        case RR_Rotate_90:
329cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          display.set_rotation(gfx::Display::ROTATE_90);
330cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          break;
331cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        case RR_Rotate_180:
332cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          display.set_rotation(gfx::Display::ROTATE_180);
333cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          break;
334cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        case RR_Rotate_270:
335cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          display.set_rotation(gfx::Display::ROTATE_270);
336cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          break;
337cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      }
338cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
33968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      displays.push_back(display);
34068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
34168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      XRRFreeCrtcInfo(crtc);
34268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    }
34368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
34468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    XRRFreeOutputInfo(output_info);
34568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
34668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
34768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  XRRFreeScreenResources(resources);
34868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
34968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  if (displays.empty())
35068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    return GetFallbackDisplayList();
35168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
35268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  return displays;
35368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
35468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
35568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)void DesktopScreenX11::ConfigureTimerFired() {
3565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  std::vector<gfx::Display> old_displays = displays_;
3575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  displays_ = BuildDisplaysFromXRandRInfo();
3585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
3595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  change_notifier_.NotifyDisplaysChanged(old_displays, displays_);
36068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
36168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
36268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)gfx::Screen* CreateDesktopScreen() {
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return new DesktopScreenX11;
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace views
369