summaryrefslogtreecommitdiffstats
path: root/ash/monitor/monitor_controller.cc
blob: 0cdb55063699879f24785d954f39e25296334cc5 (plain)
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
// 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 "ash/monitor/monitor_controller.h"

#include "ash/ash_switches.h"
#include "ash/monitor/multi_monitor_manager.h"
#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "ash/wm/window_util.h"
#include "base/command_line.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/gfx/display.h"

namespace ash {
namespace internal {
namespace {
bool extended_desktop_enabled = false;
}

MonitorController::MonitorController()
    : secondary_display_layout_(RIGHT) {
  aura::Env::GetInstance()->monitor_manager()->AddObserver(this);
}

MonitorController::~MonitorController() {
  aura::Env::GetInstance()->monitor_manager()->RemoveObserver(this);
  // Delete all root window controllers, which deletes root window
  // from the last so that the primary root window gets deleted last.
  for (std::map<int, aura::RootWindow*>::const_reverse_iterator it =
           root_windows_.rbegin(); it != root_windows_.rend(); ++it) {
    internal::RootWindowController* controller =
        wm::GetRootWindowController(it->second);
    // RootWindow may not have RootWindowController in non
    // extended desktop mode.
    if (controller)
      delete controller;
    else
      delete it->second;
  }
}

void MonitorController::InitPrimaryDisplay() {
  aura::MonitorManager* monitor_manager =
      aura::Env::GetInstance()->monitor_manager();
  const gfx::Display& display = monitor_manager->GetDisplayAt(0);
  DCHECK_EQ(0, display.id());
  aura::RootWindow* root =
      monitor_manager->CreateRootWindowForMonitor(display);
  root_windows_[display.id()] = root;
  if (aura::MonitorManager::use_fullscreen_host_window() &&
      !IsExtendedDesktopEnabled()) {
    root->ConfineCursorToWindow();
  }
  root->SetHostBounds(display.bounds_in_pixel());
}

void MonitorController::InitSecondaryDisplays() {
  aura::MonitorManager* monitor_manager =
      aura::Env::GetInstance()->monitor_manager();
  for (size_t i = 1; i < monitor_manager->GetNumDisplays(); ++i) {
    const gfx::Display& display = monitor_manager->GetDisplayAt(i);
    aura::RootWindow* root =
        monitor_manager->CreateRootWindowForMonitor(display);
    root_windows_[display.id()] = root;
    Shell::GetInstance()->InitRootWindowForSecondaryMonitor(root);
  }
}

aura::RootWindow* MonitorController::GetPrimaryRootWindow() {
  DCHECK(!root_windows_.empty());
  return root_windows_[0];
}

void MonitorController::CloseChildWindows() {
  for (std::map<int, aura::RootWindow*>::const_iterator it =
           root_windows_.begin(); it != root_windows_.end(); ++it) {
    aura::RootWindow* root_window = it->second;
    internal::RootWindowController* controller =
        wm::GetRootWindowController(root_window);
    if (controller) {
      controller->CloseChildWindows();
    } else {
      while (!root_window->children().empty()) {
        aura::Window* child = root_window->children()[0];
        delete child;
      }
    }
  }
}

std::vector<aura::RootWindow*> MonitorController::GetAllRootWindows() {
  std::vector<aura::RootWindow*> windows;
  for (std::map<int, aura::RootWindow*>::const_iterator it =
           root_windows_.begin(); it != root_windows_.end(); ++it) {
    if (wm::GetRootWindowController(it->second))
      windows.push_back(it->second);
  }
  return windows;
}

std::vector<internal::RootWindowController*>
MonitorController::GetAllRootWindowControllers() {
  std::vector<internal::RootWindowController*> controllers;
  for (std::map<int, aura::RootWindow*>::const_iterator it =
           root_windows_.begin(); it != root_windows_.end(); ++it) {
    internal::RootWindowController* controller =
        wm::GetRootWindowController(it->second);
    if (controller)
      controllers.push_back(controller);
  }
  return controllers;
}

void MonitorController::SetSecondaryDisplayLayout(
    SecondaryDisplayLayout layout) {
  secondary_display_layout_ = layout;
}

void MonitorController::OnDisplayBoundsChanged(const gfx::Display& display) {
  root_windows_[display.id()]->SetHostBounds(display.bounds_in_pixel());
}

void MonitorController::OnDisplayAdded(const gfx::Display& display) {
  if (root_windows_.empty()) {
    root_windows_[display.id()] = Shell::GetPrimaryRootWindow();
    Shell::GetPrimaryRootWindow()->SetHostBounds(display.bounds_in_pixel());
    return;
  }
  aura::RootWindow* root = aura::Env::GetInstance()->monitor_manager()->
      CreateRootWindowForMonitor(display);
  root_windows_[display.id()] = root;
  Shell::GetInstance()->InitRootWindowForSecondaryMonitor(root);
}

void MonitorController::OnDisplayRemoved(const gfx::Display& display) {
  aura::RootWindow* root = root_windows_[display.id()];
  DCHECK(root);
  // Primary monitor should never be removed by MonitorManager.
  DCHECK(root != Shell::GetPrimaryRootWindow());
  // Monitor for root window will be deleted when the Primary RootWindow
  // is deleted by the Shell.
  if (root != Shell::GetPrimaryRootWindow()) {
    root_windows_.erase(display.id());
    internal::RootWindowController* controller =
        wm::GetRootWindowController(root);
    if (controller)
      delete controller;
    else
      delete root;
  }
}

// static
bool MonitorController::IsExtendedDesktopEnabled(){
  return extended_desktop_enabled ||
      CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kAshExtendedDesktop);
}

// static
void MonitorController::SetExtendedDesktopEnabled(bool enabled) {
  extended_desktop_enabled = enabled;
}

}  // namespace internal
}  // namespace ash