ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ash / display / display_controller.cc
blob5d5144b7064bff010abc48a7b9518876381e0d49
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.
5 #include "ash/display/display_controller.h"
7 #include <algorithm>
8 #include <cmath>
9 #include <map>
11 #include "ash/ash_switches.h"
12 #include "ash/display/cursor_window_controller.h"
13 #include "ash/display/display_layout_store.h"
14 #include "ash/display/display_manager.h"
15 #include "ash/display/mirror_window_controller.h"
16 #include "ash/display/root_window_transformers.h"
17 #include "ash/host/ash_window_tree_host.h"
18 #include "ash/host/ash_window_tree_host_init_params.h"
19 #include "ash/host/root_window_transformer.h"
20 #include "ash/root_window_controller.h"
21 #include "ash/root_window_settings.h"
22 #include "ash/screen_util.h"
23 #include "ash/shell.h"
24 #include "ash/shell_delegate.h"
25 #include "ash/wm/coordinate_conversion.h"
26 #include "base/command_line.h"
27 #include "base/stl_util.h"
28 #include "base/strings/stringprintf.h"
29 #include "base/strings/utf_string_conversions.h"
30 #include "ui/aura/client/capture_client.h"
31 #include "ui/aura/client/focus_client.h"
32 #include "ui/aura/client/screen_position_client.h"
33 #include "ui/aura/window.h"
34 #include "ui/aura/window_event_dispatcher.h"
35 #include "ui/aura/window_property.h"
36 #include "ui/aura/window_tracker.h"
37 #include "ui/aura/window_tree_host.h"
38 #include "ui/compositor/compositor.h"
39 #include "ui/compositor/compositor_vsync_manager.h"
40 #include "ui/gfx/display.h"
41 #include "ui/gfx/screen.h"
42 #include "ui/wm/core/coordinate_conversion.h"
43 #include "ui/wm/public/activation_client.h"
45 #if defined(OS_CHROMEOS)
46 #include "ash/display/display_configurator_animation.h"
47 #include "base/sys_info.h"
48 #include "base/time/time.h"
49 #endif // defined(OS_CHROMEOS)
51 #if defined(USE_X11)
52 #include "ui/base/x/x11_util.h"
53 #include "ui/gfx/x/x11_types.h"
55 // Including this at the bottom to avoid other
56 // potential conflict with chrome headers.
57 #include <X11/extensions/Xrandr.h>
58 #undef RootWindow
59 #endif // defined(USE_X11)
61 #if defined(OS_CHROMEOS) && defined(USE_OZONE)
62 #include "ui/events/ozone/chromeos/cursor_controller.h"
63 #endif
65 namespace ash {
66 namespace {
68 // Primary display stored in global object as it can be
69 // accessed after Shell is deleted. A separate display instance is created
70 // during the shutdown instead of always keeping two display instances
71 // (one here and another one in display_manager) in sync, which is error prone.
72 // This is initialized in the constructor, and then in CreatePrimaryHost().
73 int64 primary_display_id = -1;
75 // Specifies how long the display change should have been disabled
76 // after each display change operations.
77 // |kCycleDisplayThrottleTimeoutMs| is set to be longer to avoid
78 // changing the settings while the system is still configurating
79 // displays. It will be overriden by |kAfterDisplayChangeThrottleTimeoutMs|
80 // when the display change happens, so the actual timeout is much shorter.
81 const int64 kAfterDisplayChangeThrottleTimeoutMs = 500;
82 const int64 kCycleDisplayThrottleTimeoutMs = 4000;
83 const int64 kSwapDisplayThrottleTimeoutMs = 500;
85 #if defined(USE_OZONE) && defined(OS_CHROMEOS)
86 // Add 20% more cursor motion on non-integrated displays.
87 const float kCursorMultiplierForExternalDisplays = 1.2f;
88 #endif
90 DisplayManager* GetDisplayManager() {
91 return Shell::GetInstance()->display_manager();
94 void SetDisplayPropertiesOnHost(AshWindowTreeHost* ash_host,
95 const gfx::Display& display) {
96 DisplayInfo info = GetDisplayManager()->GetDisplayInfo(display.id());
97 aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
98 #if defined(OS_CHROMEOS)
99 #if defined(USE_X11)
100 // Native window property (Atom in X11) that specifies the display's
101 // rotation, scale factor and if it's internal display. They are
102 // read and used by touchpad/mouse driver directly on X (contact
103 // adlr@ for more details on touchpad/mouse driver side). The value
104 // of the rotation is one of 0 (normal), 1 (90 degrees clockwise), 2
105 // (180 degree) or 3 (270 degrees clockwise). The value of the
106 // scale factor is in percent (100, 140, 200 etc).
107 const char kRotationProp[] = "_CHROME_DISPLAY_ROTATION";
108 const char kScaleFactorProp[] = "_CHROME_DISPLAY_SCALE_FACTOR";
109 const char kInternalProp[] = "_CHROME_DISPLAY_INTERNAL";
110 const char kCARDINAL[] = "CARDINAL";
111 int xrandr_rotation = RR_Rotate_0;
112 switch (info.rotation()) {
113 case gfx::Display::ROTATE_0:
114 xrandr_rotation = RR_Rotate_0;
115 break;
116 case gfx::Display::ROTATE_90:
117 xrandr_rotation = RR_Rotate_90;
118 break;
119 case gfx::Display::ROTATE_180:
120 xrandr_rotation = RR_Rotate_180;
121 break;
122 case gfx::Display::ROTATE_270:
123 xrandr_rotation = RR_Rotate_270;
124 break;
127 int internal = display.IsInternal() ? 1 : 0;
128 gfx::AcceleratedWidget xwindow = host->GetAcceleratedWidget();
129 ui::SetIntProperty(xwindow, kInternalProp, kCARDINAL, internal);
130 ui::SetIntProperty(xwindow, kRotationProp, kCARDINAL, xrandr_rotation);
131 ui::SetIntProperty(xwindow,
132 kScaleFactorProp,
133 kCARDINAL,
134 100 * display.device_scale_factor());
135 #elif defined(USE_OZONE)
136 // Scale all motion on High-DPI displays.
137 float scale = display.device_scale_factor();
139 if (!display.IsInternal())
140 scale *= kCursorMultiplierForExternalDisplays;
142 ui::CursorController::GetInstance()->SetCursorConfigForWindow(
143 host->GetAcceleratedWidget(), info.rotation(), scale);
144 #endif
145 #endif
146 scoped_ptr<RootWindowTransformer> transformer(
147 CreateRootWindowTransformerForDisplay(host->window(), display));
148 ash_host->SetRootWindowTransformer(transformer.Pass());
150 DisplayMode mode =
151 GetDisplayManager()->GetActiveModeForDisplayId(display.id());
152 if (mode.refresh_rate > 0.0f) {
153 host->compositor()->vsync_manager()->SetAuthoritativeVSyncInterval(
154 base::TimeDelta::FromMicroseconds(
155 base::Time::kMicrosecondsPerSecond / mode.refresh_rate));
158 // Just movnig the display requires the full redraw.
159 // chrome-os-partner:33558.
160 host->compositor()->ScheduleFullRedraw();
163 void ClearDisplayPropertiesOnHost(AshWindowTreeHost* ash_host,
164 const gfx::Display& display) {
165 #if defined(OS_CHROMEOS) && defined(USE_OZONE)
166 aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
167 ui::CursorController::GetInstance()->ClearCursorConfigForWindow(
168 host->GetAcceleratedWidget());
169 #endif
172 aura::Window* GetWindow(AshWindowTreeHost* ash_host) {
173 CHECK(ash_host->AsWindowTreeHost());
174 return ash_host->AsWindowTreeHost()->window();
177 } // namespace
179 // A utility class to store/restore focused/active window
180 // when the display configuration has changed.
181 class FocusActivationStore {
182 public:
183 FocusActivationStore()
184 : activation_client_(NULL),
185 capture_client_(NULL),
186 focus_client_(NULL),
187 focused_(NULL),
188 active_(NULL) {
191 void Store(bool clear_focus) {
192 if (!activation_client_) {
193 aura::Window* root = Shell::GetPrimaryRootWindow();
194 activation_client_ = aura::client::GetActivationClient(root);
195 capture_client_ = aura::client::GetCaptureClient(root);
196 focus_client_ = aura::client::GetFocusClient(root);
198 focused_ = focus_client_->GetFocusedWindow();
199 if (focused_)
200 tracker_.Add(focused_);
201 active_ = activation_client_->GetActiveWindow();
202 if (active_ && focused_ != active_)
203 tracker_.Add(active_);
205 // Deactivate the window to close menu / bubble windows.
206 if (clear_focus)
207 activation_client_->DeactivateWindow(active_);
209 // Release capture if any.
210 capture_client_->SetCapture(NULL);
211 // Clear the focused window if any. This is necessary because a
212 // window may be deleted when losing focus (fullscreen flash for
213 // example). If the focused window is still alive after move, it'll
214 // be re-focused below.
215 if (clear_focus)
216 focus_client_->FocusWindow(NULL);
219 void Restore() {
220 // Restore focused or active window if it's still alive.
221 if (focused_ && tracker_.Contains(focused_)) {
222 focus_client_->FocusWindow(focused_);
223 } else if (active_ && tracker_.Contains(active_)) {
224 activation_client_->ActivateWindow(active_);
226 if (focused_)
227 tracker_.Remove(focused_);
228 if (active_)
229 tracker_.Remove(active_);
230 focused_ = NULL;
231 active_ = NULL;
234 private:
235 aura::client::ActivationClient* activation_client_;
236 aura::client::CaptureClient* capture_client_;
237 aura::client::FocusClient* focus_client_;
238 aura::WindowTracker tracker_;
239 aura::Window* focused_;
240 aura::Window* active_;
242 DISALLOW_COPY_AND_ASSIGN(FocusActivationStore);
245 ////////////////////////////////////////////////////////////////////////////////
246 // DisplayChangeLimiter
248 DisplayController::DisplayChangeLimiter::DisplayChangeLimiter()
249 : throttle_timeout_(base::Time::Now()) {
252 void DisplayController::DisplayChangeLimiter::SetThrottleTimeout(
253 int64 throttle_ms) {
254 throttle_timeout_ =
255 base::Time::Now() + base::TimeDelta::FromMilliseconds(throttle_ms);
258 bool DisplayController::DisplayChangeLimiter::IsThrottled() const {
259 return base::Time::Now() < throttle_timeout_;
262 ////////////////////////////////////////////////////////////////////////////////
263 // DisplayController
265 DisplayController::DisplayController()
266 : primary_tree_host_for_replace_(NULL),
267 focus_activation_store_(new FocusActivationStore()),
268 cursor_window_controller_(new CursorWindowController()),
269 mirror_window_controller_(new MirrorWindowController()),
270 cursor_display_id_for_restore_(gfx::Display::kInvalidDisplayID),
271 weak_ptr_factory_(this) {
272 #if defined(OS_CHROMEOS)
273 if (base::SysInfo::IsRunningOnChromeOS())
274 limiter_.reset(new DisplayChangeLimiter);
275 #endif
276 // Reset primary display to make sure that tests don't use
277 // stale display info from previous tests.
278 primary_display_id = gfx::Display::kInvalidDisplayID;
281 DisplayController::~DisplayController() {
284 void DisplayController::Start() {
285 Shell::GetScreen()->AddObserver(this);
286 Shell::GetInstance()->display_manager()->set_delegate(this);
289 void DisplayController::Shutdown() {
290 // Unset the display manager's delegate here because
291 // DisplayManager outlives DisplayController.
292 Shell::GetInstance()->display_manager()->set_delegate(NULL);
294 cursor_window_controller_.reset();
295 mirror_window_controller_.reset();
297 Shell::GetScreen()->RemoveObserver(this);
299 int64 primary_id = Shell::GetScreen()->GetPrimaryDisplay().id();
301 // Delete non primary root window controllers first, then
302 // delete the primary root window controller.
303 aura::Window::Windows root_windows = DisplayController::GetAllRootWindows();
304 std::vector<RootWindowController*> to_delete;
305 RootWindowController* primary_rwc = NULL;
306 for (aura::Window::Windows::iterator iter = root_windows.begin();
307 iter != root_windows.end();
308 ++iter) {
309 RootWindowController* rwc = GetRootWindowController(*iter);
310 if (GetRootWindowSettings(*iter)->display_id == primary_id)
311 primary_rwc = rwc;
312 else
313 to_delete.push_back(rwc);
315 CHECK(primary_rwc);
317 STLDeleteElements(&to_delete);
318 delete primary_rwc;
321 void DisplayController::CreatePrimaryHost(
322 const AshWindowTreeHostInitParams& init_params) {
323 const gfx::Display& primary_candidate =
324 GetDisplayManager()->GetPrimaryDisplayCandidate();
325 primary_display_id = primary_candidate.id();
326 CHECK_NE(gfx::Display::kInvalidDisplayID, primary_display_id);
327 AddWindowTreeHostForDisplay(primary_candidate, init_params);
330 void DisplayController::InitDisplays() {
331 RootWindowController::CreateForPrimaryDisplay(
332 window_tree_hosts_[primary_display_id]);
334 DisplayManager* display_manager = GetDisplayManager();
335 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) {
336 const gfx::Display& display = display_manager->GetDisplayAt(i);
337 if (primary_display_id != display.id()) {
338 AshWindowTreeHost* ash_host = AddWindowTreeHostForDisplay(
339 display, AshWindowTreeHostInitParams());
340 RootWindowController::CreateForSecondaryDisplay(ash_host);
343 UpdateHostWindowNames();
345 FOR_EACH_OBSERVER(Observer, observers_, OnDisplaysInitialized());
348 void DisplayController::AddObserver(Observer* observer) {
349 observers_.AddObserver(observer);
352 void DisplayController::RemoveObserver(Observer* observer) {
353 observers_.RemoveObserver(observer);
356 // static
357 int64 DisplayController::GetPrimaryDisplayId() {
358 CHECK_NE(gfx::Display::kInvalidDisplayID, primary_display_id);
359 return primary_display_id;
362 aura::Window* DisplayController::GetPrimaryRootWindow() {
363 return GetRootWindowForDisplayId(primary_display_id);
366 aura::Window* DisplayController::GetRootWindowForDisplayId(int64 id) {
367 CHECK_EQ(1u, window_tree_hosts_.count(id));
368 AshWindowTreeHost* host = window_tree_hosts_[id];
369 CHECK(host);
370 return GetWindow(host);
373 void DisplayController::CloseChildWindows() {
374 for (WindowTreeHostMap::const_iterator it = window_tree_hosts_.begin();
375 it != window_tree_hosts_.end();
376 ++it) {
377 aura::Window* root_window = GetWindow(it->second);
378 RootWindowController* controller = GetRootWindowController(root_window);
379 if (controller) {
380 controller->CloseChildWindows();
381 } else {
382 while (!root_window->children().empty()) {
383 aura::Window* child = root_window->children()[0];
384 delete child;
390 aura::Window::Windows DisplayController::GetAllRootWindows() {
391 aura::Window::Windows windows;
392 for (WindowTreeHostMap::const_iterator it = window_tree_hosts_.begin();
393 it != window_tree_hosts_.end();
394 ++it) {
395 DCHECK(it->second);
396 if (GetRootWindowController(GetWindow(it->second)))
397 windows.push_back(GetWindow(it->second));
399 return windows;
402 gfx::Insets DisplayController::GetOverscanInsets(int64 display_id) const {
403 return GetDisplayManager()->GetOverscanInsets(display_id);
406 void DisplayController::SetOverscanInsets(int64 display_id,
407 const gfx::Insets& insets_in_dip) {
408 GetDisplayManager()->SetOverscanInsets(display_id, insets_in_dip);
411 std::vector<RootWindowController*>
412 DisplayController::GetAllRootWindowControllers() {
413 std::vector<RootWindowController*> controllers;
414 for (WindowTreeHostMap::const_iterator it = window_tree_hosts_.begin();
415 it != window_tree_hosts_.end();
416 ++it) {
417 RootWindowController* controller =
418 GetRootWindowController(GetWindow(it->second));
419 if (controller)
420 controllers.push_back(controller);
422 return controllers;
425 void DisplayController::ToggleMirrorMode() {
426 DisplayManager* display_manager = GetDisplayManager();
427 if (display_manager->num_connected_displays() <= 1)
428 return;
430 if (limiter_) {
431 if (limiter_->IsThrottled())
432 return;
433 limiter_->SetThrottleTimeout(kCycleDisplayThrottleTimeoutMs);
435 #if defined(OS_CHROMEOS)
436 Shell* shell = Shell::GetInstance();
437 DisplayConfiguratorAnimation* animation =
438 shell->display_configurator_animation();
439 animation->StartFadeOutAnimation(
440 base::Bind(&DisplayController::SetMirrorModeAfterAnimation,
441 weak_ptr_factory_.GetWeakPtr(),
442 !display_manager->IsMirrored()));
443 #endif
446 void DisplayController::SwapPrimaryDisplay() {
447 if (limiter_) {
448 if (limiter_->IsThrottled())
449 return;
450 limiter_->SetThrottleTimeout(kSwapDisplayThrottleTimeoutMs);
453 if (Shell::GetScreen()->GetNumDisplays() > 1) {
454 #if defined(OS_CHROMEOS)
455 DisplayConfiguratorAnimation* animation =
456 Shell::GetInstance()->display_configurator_animation();
457 if (animation) {
458 animation->StartFadeOutAnimation(base::Bind(
459 &DisplayController::OnFadeOutForSwapDisplayFinished,
460 weak_ptr_factory_.GetWeakPtr()));
461 } else {
462 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
464 #else
465 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
466 #endif
470 void DisplayController::SetPrimaryDisplayId(int64 id) {
471 DCHECK_NE(gfx::Display::kInvalidDisplayID, id);
472 if (id == gfx::Display::kInvalidDisplayID || primary_display_id == id)
473 return;
475 const gfx::Display& display = GetDisplayManager()->GetDisplayForId(id);
476 if (display.is_valid())
477 SetPrimaryDisplay(display);
480 void DisplayController::SetPrimaryDisplay(
481 const gfx::Display& new_primary_display) {
482 DisplayManager* display_manager = GetDisplayManager();
483 DCHECK(new_primary_display.is_valid());
484 DCHECK(display_manager->IsActiveDisplay(new_primary_display));
486 if (!new_primary_display.is_valid() ||
487 !display_manager->IsActiveDisplay(new_primary_display)) {
488 LOG(ERROR) << "Invalid or non-existent display is requested:"
489 << new_primary_display.ToString();
490 return;
493 if (primary_display_id == new_primary_display.id() ||
494 window_tree_hosts_.size() < 2) {
495 return;
498 AshWindowTreeHost* non_primary_host =
499 window_tree_hosts_[new_primary_display.id()];
500 LOG_IF(ERROR, !non_primary_host)
501 << "Unknown display is requested in SetPrimaryDisplay: id="
502 << new_primary_display.id();
503 if (!non_primary_host)
504 return;
506 gfx::Display old_primary_display = Shell::GetScreen()->GetPrimaryDisplay();
508 // Swap root windows between current and new primary display.
509 AshWindowTreeHost* primary_host = window_tree_hosts_[primary_display_id];
510 CHECK(primary_host);
511 CHECK_NE(primary_host, non_primary_host);
513 window_tree_hosts_[new_primary_display.id()] = primary_host;
514 GetRootWindowSettings(GetWindow(primary_host))->display_id =
515 new_primary_display.id();
517 window_tree_hosts_[old_primary_display.id()] = non_primary_host;
518 GetRootWindowSettings(GetWindow(non_primary_host))->display_id =
519 old_primary_display.id();
521 primary_display_id = new_primary_display.id();
522 GetDisplayManager()->layout_store()->UpdatePrimaryDisplayId(
523 display_manager->GetCurrentDisplayIdPair(), primary_display_id);
525 UpdateWorkAreaOfDisplayNearestWindow(GetWindow(primary_host),
526 old_primary_display.GetWorkAreaInsets());
527 UpdateWorkAreaOfDisplayNearestWindow(GetWindow(non_primary_host),
528 new_primary_display.GetWorkAreaInsets());
530 // Update the dispay manager with new display info.
531 std::vector<DisplayInfo> display_info_list;
532 display_info_list.push_back(display_manager->GetDisplayInfo(
533 primary_display_id));
534 display_info_list.push_back(display_manager->GetDisplayInfo(
535 ScreenUtil::GetSecondaryDisplay().id()));
536 GetDisplayManager()->set_force_bounds_changed(true);
537 GetDisplayManager()->UpdateDisplays(display_info_list);
538 GetDisplayManager()->set_force_bounds_changed(false);
541 void DisplayController::EnsurePointerInDisplays() {
542 // If the mouse is currently on a display in native location,
543 // use the same native location. Otherwise find the display closest
544 // to the current cursor location in screen coordinates.
546 gfx::Point point_in_screen = Shell::GetScreen()->GetCursorScreenPoint();
547 gfx::Point target_location_in_native;
548 int64 closest_distance_squared = -1;
549 DisplayManager* display_manager = GetDisplayManager();
551 aura::Window* dst_root_window = NULL;
552 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) {
553 const gfx::Display& display = display_manager->GetDisplayAt(i);
554 const DisplayInfo display_info =
555 display_manager->GetDisplayInfo(display.id());
556 aura::Window* root_window = GetRootWindowForDisplayId(display.id());
557 if (display_info.bounds_in_native().Contains(
558 cursor_location_in_native_coords_for_restore_)) {
559 dst_root_window = root_window;
560 target_location_in_native = cursor_location_in_native_coords_for_restore_;
561 break;
563 gfx::Point center = display.bounds().CenterPoint();
564 // Use the distance squared from the center of the dislay. This is not
565 // exactly "closest" display, but good enough to pick one
566 // appropriate (and there are at most two displays).
567 // We don't care about actual distance, only relative to other displays, so
568 // using the LengthSquared() is cheaper than Length().
570 int64 distance_squared = (center - point_in_screen).LengthSquared();
571 if (closest_distance_squared < 0 ||
572 closest_distance_squared > distance_squared) {
573 aura::Window* root_window = GetRootWindowForDisplayId(display.id());
574 ::wm::ConvertPointFromScreen(root_window, &center);
575 root_window->GetHost()->ConvertPointToNativeScreen(&center);
576 dst_root_window = root_window;
577 target_location_in_native = center;
578 closest_distance_squared = distance_squared;
582 gfx::Point target_location_in_root = target_location_in_native;
583 dst_root_window->GetHost()->ConvertPointFromNativeScreen(
584 &target_location_in_root);
586 #if defined(USE_OZONE)
587 gfx::Point target_location_in_screen = target_location_in_root;
588 ::wm::ConvertPointToScreen(dst_root_window, &target_location_in_screen);
589 int64 target_display_id =
590 display_manager->FindDisplayContainingPoint(target_location_in_screen)
591 .id();
593 // Do not move the cursor if the cursor's location did not change. This avoids
594 // moving (and showing) the cursor on startup.
595 // - |cursor_location_in_screen_coords_for_restore_| is checked to ensure that
596 // the cursor is moved when the cursor's native position does not change but
597 // the scale factor or rotation of the display it is on have changed.
598 // - |cursor_display_id_for_restore_| is checked to ensure that the cursor is
599 // moved when the cursor's native position and screen position do not change
600 // but the display that it is on has changed. This occurs when swapping the
601 // primary display.
602 if (target_location_in_native !=
603 cursor_location_in_native_coords_for_restore_ ||
604 target_location_in_screen !=
605 cursor_location_in_screen_coords_for_restore_ ||
606 target_display_id != cursor_display_id_for_restore_) {
607 dst_root_window->MoveCursorTo(target_location_in_root);
609 #else
610 dst_root_window->MoveCursorTo(target_location_in_root);
611 #endif
614 bool DisplayController::UpdateWorkAreaOfDisplayNearestWindow(
615 const aura::Window* window,
616 const gfx::Insets& insets) {
617 const aura::Window* root_window = window->GetRootWindow();
618 int64 id = GetRootWindowSettings(root_window)->display_id;
619 // if id is |kInvaildDisplayID|, it's being deleted.
620 DCHECK(id != gfx::Display::kInvalidDisplayID);
621 return GetDisplayManager()->UpdateWorkAreaOfDisplay(id, insets);
624 void DisplayController::OnDisplayAdded(const gfx::Display& display) {
625 if (primary_tree_host_for_replace_) {
626 DCHECK(window_tree_hosts_.empty());
627 primary_display_id = display.id();
628 window_tree_hosts_[display.id()] = primary_tree_host_for_replace_;
629 GetRootWindowSettings(GetWindow(primary_tree_host_for_replace_))
630 ->display_id = display.id();
631 primary_tree_host_for_replace_ = NULL;
632 const DisplayInfo& display_info =
633 GetDisplayManager()->GetDisplayInfo(display.id());
634 AshWindowTreeHost* ash_host = window_tree_hosts_[display.id()];
635 ash_host->AsWindowTreeHost()->SetBounds(display_info.bounds_in_native());
636 SetDisplayPropertiesOnHost(ash_host, display);
637 } else {
638 if (primary_display_id == gfx::Display::kInvalidDisplayID)
639 primary_display_id = display.id();
640 DCHECK(!window_tree_hosts_.empty());
641 AshWindowTreeHost* ash_host = AddWindowTreeHostForDisplay(
642 display, AshWindowTreeHostInitParams());
643 RootWindowController::CreateForSecondaryDisplay(ash_host);
647 void DisplayController::OnDisplayRemoved(const gfx::Display& display) {
648 AshWindowTreeHost* host_to_delete = window_tree_hosts_[display.id()];
649 CHECK(host_to_delete) << display.ToString();
651 // Display for root window will be deleted when the Primary RootWindow
652 // is deleted by the Shell.
653 window_tree_hosts_.erase(display.id());
655 // When the primary root window's display is removed, move the primary
656 // root to the other display.
657 if (primary_display_id == display.id()) {
658 // Temporarily store the primary root window in
659 // |primary_root_window_for_replace_| when replacing the display.
660 if (window_tree_hosts_.size() == 0) {
661 primary_display_id = gfx::Display::kInvalidDisplayID;
662 primary_tree_host_for_replace_ = host_to_delete;
663 return;
665 primary_display_id = window_tree_hosts_.begin()->first;
666 CHECK_NE(gfx::Display::kInvalidDisplayID, primary_display_id);
668 AshWindowTreeHost* primary_host = host_to_delete;
669 // Delete the other host instead.
670 host_to_delete = window_tree_hosts_[primary_display_id];
671 GetRootWindowSettings(GetWindow(host_to_delete))->display_id = display.id();
673 // Setup primary root.
674 window_tree_hosts_[primary_display_id] = primary_host;
675 GetRootWindowSettings(GetWindow(primary_host))->display_id =
676 primary_display_id;
678 OnDisplayMetricsChanged(
679 GetDisplayManager()->GetDisplayForId(primary_display_id),
680 DISPLAY_METRIC_BOUNDS);
682 ClearDisplayPropertiesOnHost(host_to_delete, display);
683 RootWindowController* controller =
684 GetRootWindowController(GetWindow(host_to_delete));
685 DCHECK(controller);
686 controller->MoveWindowsTo(GetPrimaryRootWindow());
687 // Delete most of root window related objects, but don't delete
688 // root window itself yet because the stack may be using it.
689 controller->Shutdown();
690 base::MessageLoop::current()->DeleteSoon(FROM_HERE, controller);
693 void DisplayController::OnDisplayMetricsChanged(const gfx::Display& display,
694 uint32_t metrics) {
695 if (!(metrics & (DISPLAY_METRIC_BOUNDS | DISPLAY_METRIC_ROTATION |
696 DISPLAY_METRIC_DEVICE_SCALE_FACTOR)))
697 return;
698 const DisplayInfo& display_info =
699 GetDisplayManager()->GetDisplayInfo(display.id());
700 DCHECK(!display_info.bounds_in_native().IsEmpty());
701 AshWindowTreeHost* ash_host = window_tree_hosts_[display.id()];
702 ash_host->AsWindowTreeHost()->SetBounds(display_info.bounds_in_native());
703 SetDisplayPropertiesOnHost(ash_host, display);
706 void DisplayController::OnHostResized(const aura::WindowTreeHost* host) {
707 gfx::Display display = Shell::GetScreen()->GetDisplayNearestWindow(
708 const_cast<aura::Window*>(host->window()));
710 DisplayManager* display_manager = GetDisplayManager();
711 if (display_manager->UpdateDisplayBounds(display.id(), host->GetBounds())) {
712 mirror_window_controller_->UpdateWindow();
713 cursor_window_controller_->UpdateContainer();
717 void DisplayController::CreateOrUpdateMirroringDisplay(
718 const DisplayInfo& info) {
719 switch (GetDisplayManager()->second_display_mode()) {
720 case DisplayManager::MIRRORING:
721 mirror_window_controller_->UpdateWindow(info);
722 cursor_window_controller_->UpdateContainer();
723 break;
724 case DisplayManager::EXTENDED:
725 NOTREACHED();
729 void DisplayController::CloseMirroringDisplay() {
730 mirror_window_controller_->Close();
731 // If cursor_compositing is enabled for large cursor, the cursor window is
732 // always on the desktop display (the visible cursor on the non-desktop
733 // display is drawn through compositor mirroring). Therefore, it's unnecessary
734 // to handle the cursor_window at all. See: http://crbug.com/412910
735 if (!cursor_window_controller_->is_cursor_compositing_enabled())
736 cursor_window_controller_->UpdateContainer();
739 void DisplayController::PreDisplayConfigurationChange(bool clear_focus) {
740 FOR_EACH_OBSERVER(Observer, observers_, OnDisplayConfigurationChanging());
741 focus_activation_store_->Store(clear_focus);
742 gfx::Screen* screen = Shell::GetScreen();
743 gfx::Point point_in_screen = screen->GetCursorScreenPoint();
744 cursor_location_in_screen_coords_for_restore_ = point_in_screen;
746 gfx::Display display = screen->GetDisplayNearestPoint(point_in_screen);
747 cursor_display_id_for_restore_ = display.id();
749 gfx::Point point_in_native = point_in_screen;
750 aura::Window* root_window = GetRootWindowForDisplayId(display.id());
751 ::wm::ConvertPointFromScreen(root_window, &point_in_native);
752 root_window->GetHost()->ConvertPointToNativeScreen(&point_in_native);
753 cursor_location_in_native_coords_for_restore_ = point_in_native;
756 void DisplayController::PostDisplayConfigurationChange() {
757 if (limiter_)
758 limiter_->SetThrottleTimeout(kAfterDisplayChangeThrottleTimeoutMs);
760 focus_activation_store_->Restore();
762 DisplayManager* display_manager = GetDisplayManager();
763 DisplayLayoutStore* layout_store = display_manager->layout_store();
764 if (display_manager->num_connected_displays() > 1) {
765 DisplayIdPair pair = display_manager->GetCurrentDisplayIdPair();
766 layout_store->UpdateMirrorStatus(pair, display_manager->IsMirrored());
767 DisplayLayout layout = layout_store->GetRegisteredDisplayLayout(pair);
769 if (Shell::GetScreen()->GetNumDisplays() > 1 ) {
770 int64 primary_id = layout.primary_id;
771 SetPrimaryDisplayId(
772 primary_id == gfx::Display::kInvalidDisplayID ?
773 pair.first : primary_id);
774 // Update the primary_id in case the above call is
775 // ignored. Happens when a) default layout's primary id
776 // doesn't exist, or b) the primary_id has already been
777 // set to the same and didn't update it.
778 layout_store->UpdatePrimaryDisplayId(
779 pair, Shell::GetScreen()->GetPrimaryDisplay().id());
782 FOR_EACH_OBSERVER(Observer, observers_, OnDisplayConfigurationChanged());
783 UpdateHostWindowNames();
784 EnsurePointerInDisplays();
787 AshWindowTreeHost* DisplayController::AddWindowTreeHostForDisplay(
788 const gfx::Display& display,
789 const AshWindowTreeHostInitParams& init_params) {
790 static int host_count = 0;
791 const DisplayInfo& display_info =
792 GetDisplayManager()->GetDisplayInfo(display.id());
793 AshWindowTreeHostInitParams params_with_bounds(init_params);
794 params_with_bounds.initial_bounds = display_info.bounds_in_native();
795 AshWindowTreeHost* ash_host = AshWindowTreeHost::Create(params_with_bounds);
796 aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
798 host->window()->SetName(base::StringPrintf("RootWindow-%d", host_count++));
799 host->window()->SetTitle(base::UTF8ToUTF16(display_info.name()));
800 host->compositor()->SetBackgroundColor(SK_ColorBLACK);
801 // No need to remove our observer observer because the DisplayController
802 // outlives the host.
803 host->AddObserver(this);
804 InitRootWindowSettings(host->window())->display_id = display.id();
805 host->InitHost();
807 window_tree_hosts_[display.id()] = ash_host;
808 SetDisplayPropertiesOnHost(ash_host, display);
810 #if defined(OS_CHROMEOS)
811 static bool force_constrain_pointer_to_root =
812 base::CommandLine::ForCurrentProcess()->HasSwitch(
813 switches::kAshConstrainPointerToRoot);
814 if (base::SysInfo::IsRunningOnChromeOS() || force_constrain_pointer_to_root)
815 ash_host->ConfineCursorToRootWindow();
816 #endif
817 return ash_host;
820 void DisplayController::OnFadeOutForSwapDisplayFinished() {
821 #if defined(OS_CHROMEOS)
822 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
823 Shell::GetInstance()->display_configurator_animation()
824 ->StartFadeInAnimation();
825 #endif
828 void DisplayController::SetMirrorModeAfterAnimation(bool mirror) {
829 GetDisplayManager()->SetMirrorMode(mirror);
832 void DisplayController::UpdateHostWindowNames() {
833 #if defined(USE_X11)
834 // crbug.com/120229 - set the window title for the primary dislpay
835 // to "aura_root_0" so gtalk can find the primary root window to broadcast.
836 // TODO(jhorwich) Remove this once Chrome supports window-based broadcasting.
837 aura::Window* primary = Shell::GetPrimaryRootWindow();
838 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
839 for (size_t i = 0; i < root_windows.size(); ++i) {
840 std::string name =
841 root_windows[i] == primary ? "aura_root_0" : "aura_root_x";
842 gfx::AcceleratedWidget xwindow =
843 root_windows[i]->GetHost()->GetAcceleratedWidget();
844 XStoreName(gfx::GetXDisplay(), xwindow, name.c_str());
846 #endif
849 } // namespace ash