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"
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/magnifier/magnification_controller.h"
21 #include "ash/magnifier/partial_magnification_controller.h"
22 #include "ash/root_window_controller.h"
23 #include "ash/root_window_settings.h"
24 #include "ash/screen_util.h"
25 #include "ash/shell.h"
26 #include "ash/shell_delegate.h"
27 #include "ash/system/tray/system_tray.h"
28 #include "ash/wm/coordinate_conversion.h"
29 #include "ash/wm/window_util.h"
30 #include "base/command_line.h"
31 #include "base/stl_util.h"
32 #include "base/strings/stringprintf.h"
33 #include "base/strings/utf_string_conversions.h"
34 #include "ui/aura/client/capture_client.h"
35 #include "ui/aura/client/focus_client.h"
36 #include "ui/aura/client/screen_position_client.h"
37 #include "ui/aura/window.h"
38 #include "ui/aura/window_event_dispatcher.h"
39 #include "ui/aura/window_property.h"
40 #include "ui/aura/window_tracker.h"
41 #include "ui/aura/window_tree_host.h"
42 #include "ui/base/ime/input_method_factory.h"
43 #include "ui/compositor/compositor.h"
44 #include "ui/gfx/display.h"
45 #include "ui/gfx/screen.h"
46 #include "ui/wm/core/coordinate_conversion.h"
47 #include "ui/wm/public/activation_client.h"
49 #if defined(OS_CHROMEOS)
50 #include "ash/display/display_configurator_animation.h"
51 #include "base/sys_info.h"
52 #include "base/time/time.h"
53 #endif // defined(OS_CHROMEOS)
56 #include "ui/base/x/x11_util.h"
57 #include "ui/gfx/x/x11_types.h"
59 // Including this at the bottom to avoid other
60 // potential conflict with chrome headers.
61 #include <X11/extensions/Xrandr.h>
63 #endif // defined(USE_X11)
65 #if defined(OS_CHROMEOS) && defined(USE_OZONE)
66 #include "ui/events/ozone/chromeos/cursor_controller.h"
72 // Primary display stored in global object as it can be
73 // accessed after Shell is deleted. A separate display instance is created
74 // during the shutdown instead of always keeping two display instances
75 // (one here and another one in display_manager) in sync, which is error prone.
76 // This is initialized in the constructor, and then in CreatePrimaryHost().
77 int64 primary_display_id
= -1;
79 // Specifies how long the display change should have been disabled
80 // after each display change operations.
81 // |kCycleDisplayThrottleTimeoutMs| is set to be longer to avoid
82 // changing the settings while the system is still configurating
83 // displays. It will be overriden by |kAfterDisplayChangeThrottleTimeoutMs|
84 // when the display change happens, so the actual timeout is much shorter.
85 const int64 kAfterDisplayChangeThrottleTimeoutMs
= 500;
86 const int64 kCycleDisplayThrottleTimeoutMs
= 4000;
87 const int64 kSwapDisplayThrottleTimeoutMs
= 500;
89 #if defined(USE_OZONE) && defined(OS_CHROMEOS)
90 // Add 20% more cursor motion on non-integrated displays.
91 const float kCursorMultiplierForExternalDisplays
= 1.2f
;
94 DisplayManager
* GetDisplayManager() {
95 return Shell::GetInstance()->display_manager();
98 void SetDisplayPropertiesOnHost(AshWindowTreeHost
* ash_host
,
99 const gfx::Display
& display
) {
100 DisplayInfo info
= GetDisplayManager()->GetDisplayInfo(display
.id());
101 aura::WindowTreeHost
* host
= ash_host
->AsWindowTreeHost();
102 #if defined(OS_CHROMEOS)
104 // Native window property (Atom in X11) that specifies the display's
105 // rotation, scale factor and if it's internal display. They are
106 // read and used by touchpad/mouse driver directly on X (contact
107 // adlr@ for more details on touchpad/mouse driver side). The value
108 // of the rotation is one of 0 (normal), 1 (90 degrees clockwise), 2
109 // (180 degree) or 3 (270 degrees clockwise). The value of the
110 // scale factor is in percent (100, 140, 200 etc).
111 const char kRotationProp
[] = "_CHROME_DISPLAY_ROTATION";
112 const char kScaleFactorProp
[] = "_CHROME_DISPLAY_SCALE_FACTOR";
113 const char kInternalProp
[] = "_CHROME_DISPLAY_INTERNAL";
114 const char kCARDINAL
[] = "CARDINAL";
115 int xrandr_rotation
= RR_Rotate_0
;
116 switch (info
.GetActiveRotation()) {
117 case gfx::Display::ROTATE_0
:
118 xrandr_rotation
= RR_Rotate_0
;
120 case gfx::Display::ROTATE_90
:
121 xrandr_rotation
= RR_Rotate_90
;
123 case gfx::Display::ROTATE_180
:
124 xrandr_rotation
= RR_Rotate_180
;
126 case gfx::Display::ROTATE_270
:
127 xrandr_rotation
= RR_Rotate_270
;
131 int internal
= display
.IsInternal() ? 1 : 0;
132 gfx::AcceleratedWidget xwindow
= host
->GetAcceleratedWidget();
133 ui::SetIntProperty(xwindow
, kInternalProp
, kCARDINAL
, internal
);
134 ui::SetIntProperty(xwindow
, kRotationProp
, kCARDINAL
, xrandr_rotation
);
135 ui::SetIntProperty(xwindow
,
138 100 * display
.device_scale_factor());
139 #elif defined(USE_OZONE)
140 // Scale all motion on High-DPI displays.
141 float scale
= display
.device_scale_factor();
143 if (!display
.IsInternal())
144 scale
*= kCursorMultiplierForExternalDisplays
;
146 ui::CursorController::GetInstance()->SetCursorConfigForWindow(
147 host
->GetAcceleratedWidget(), info
.GetActiveRotation(), scale
);
150 scoped_ptr
<RootWindowTransformer
> transformer(
151 CreateRootWindowTransformerForDisplay(host
->window(), display
));
152 ash_host
->SetRootWindowTransformer(transformer
.Pass());
155 GetDisplayManager()->GetActiveModeForDisplayId(display
.id());
156 if (mode
.refresh_rate
> 0.0f
) {
157 host
->compositor()->SetAuthoritativeVSyncInterval(
158 base::TimeDelta::FromMicroseconds(base::Time::kMicrosecondsPerSecond
/
162 // Just movnig the display requires the full redraw.
163 // chrome-os-partner:33558.
164 host
->compositor()->ScheduleFullRedraw();
167 void ClearDisplayPropertiesOnHost(AshWindowTreeHost
* ash_host
) {
168 #if defined(OS_CHROMEOS) && defined(USE_OZONE)
169 aura::WindowTreeHost
* host
= ash_host
->AsWindowTreeHost();
170 ui::CursorController::GetInstance()->ClearCursorConfigForWindow(
171 host
->GetAcceleratedWidget());
175 aura::Window
* GetWindow(AshWindowTreeHost
* ash_host
) {
176 CHECK(ash_host
->AsWindowTreeHost());
177 return ash_host
->AsWindowTreeHost()->window();
182 // A utility class to store/restore focused/active window
183 // when the display configuration has changed.
184 class FocusActivationStore
{
186 FocusActivationStore()
187 : activation_client_(nullptr),
188 capture_client_(nullptr),
189 focus_client_(nullptr),
193 void Store(bool clear_focus
) {
194 if (!activation_client_
) {
195 aura::Window
* root
= Shell::GetPrimaryRootWindow();
196 activation_client_
= aura::client::GetActivationClient(root
);
197 capture_client_
= aura::client::GetCaptureClient(root
);
198 focus_client_
= aura::client::GetFocusClient(root
);
200 focused_
= focus_client_
->GetFocusedWindow();
202 tracker_
.Add(focused_
);
203 active_
= activation_client_
->GetActiveWindow();
204 if (active_
&& focused_
!= active_
)
205 tracker_
.Add(active_
);
207 // Deactivate the window to close menu / bubble windows.
209 activation_client_
->DeactivateWindow(active_
);
211 // Release capture if any.
212 capture_client_
->SetCapture(nullptr);
213 // Clear the focused window if any. This is necessary because a
214 // window may be deleted when losing focus (fullscreen flash for
215 // example). If the focused window is still alive after move, it'll
216 // be re-focused below.
218 focus_client_
->FocusWindow(nullptr);
222 // Restore focused or active window if it's still alive.
223 if (focused_
&& tracker_
.Contains(focused_
)) {
224 focus_client_
->FocusWindow(focused_
);
225 } else if (active_
&& tracker_
.Contains(active_
)) {
226 activation_client_
->ActivateWindow(active_
);
229 tracker_
.Remove(focused_
);
231 tracker_
.Remove(active_
);
237 aura::client::ActivationClient
* activation_client_
;
238 aura::client::CaptureClient
* capture_client_
;
239 aura::client::FocusClient
* focus_client_
;
240 aura::WindowTracker tracker_
;
241 aura::Window
* focused_
;
242 aura::Window
* active_
;
244 DISALLOW_COPY_AND_ASSIGN(FocusActivationStore
);
247 ////////////////////////////////////////////////////////////////////////////////
248 // DisplayChangeLimiter
250 DisplayController::DisplayChangeLimiter::DisplayChangeLimiter()
251 : throttle_timeout_(base::Time::Now()) {
254 void DisplayController::DisplayChangeLimiter::SetThrottleTimeout(
257 base::Time::Now() + base::TimeDelta::FromMilliseconds(throttle_ms
);
260 bool DisplayController::DisplayChangeLimiter::IsThrottled() const {
261 return base::Time::Now() < throttle_timeout_
;
264 ////////////////////////////////////////////////////////////////////////////////
267 DisplayController::DisplayController()
268 : primary_tree_host_for_replace_(nullptr),
269 focus_activation_store_(new FocusActivationStore()),
270 cursor_window_controller_(new CursorWindowController()),
271 mirror_window_controller_(new MirrorWindowController()),
272 cursor_display_id_for_restore_(gfx::Display::kInvalidDisplayID
),
273 weak_ptr_factory_(this) {
274 #if defined(OS_CHROMEOS)
275 if (base::SysInfo::IsRunningOnChromeOS())
276 limiter_
.reset(new DisplayChangeLimiter
);
278 // Reset primary display to make sure that tests don't use
279 // stale display info from previous tests.
280 primary_display_id
= gfx::Display::kInvalidDisplayID
;
283 DisplayController::~DisplayController() {
286 void DisplayController::Start() {
287 Shell::GetScreen()->AddObserver(this);
288 Shell::GetInstance()->display_manager()->set_delegate(this);
291 void DisplayController::Shutdown() {
292 // Unset the display manager's delegate here because
293 // DisplayManager outlives DisplayController.
294 Shell::GetInstance()->display_manager()->set_delegate(nullptr);
296 cursor_window_controller_
.reset();
297 mirror_window_controller_
.reset();
299 Shell::GetScreen()->RemoveObserver(this);
301 int64 primary_id
= Shell::GetScreen()->GetPrimaryDisplay().id();
303 // Delete non primary root window controllers first, then
304 // delete the primary root window controller.
305 aura::Window::Windows root_windows
= DisplayController::GetAllRootWindows();
306 std::vector
<RootWindowController
*> to_delete
;
307 RootWindowController
* primary_rwc
= nullptr;
308 for (aura::Window::Windows::iterator iter
= root_windows
.begin();
309 iter
!= root_windows
.end();
311 RootWindowController
* rwc
= GetRootWindowController(*iter
);
312 if (GetRootWindowSettings(*iter
)->display_id
== primary_id
)
315 to_delete
.push_back(rwc
);
319 STLDeleteElements(&to_delete
);
323 void DisplayController::CreatePrimaryHost(
324 const AshWindowTreeHostInitParams
& init_params
) {
325 const gfx::Display
& primary_candidate
=
326 GetDisplayManager()->GetPrimaryDisplayCandidate();
327 primary_display_id
= primary_candidate
.id();
328 CHECK_NE(gfx::Display::kInvalidDisplayID
, primary_display_id
);
329 AddWindowTreeHostForDisplay(primary_candidate
, init_params
);
332 void DisplayController::InitDisplays() {
333 RootWindowController::CreateForPrimaryDisplay(
334 window_tree_hosts_
[primary_display_id
]);
335 DisplayManager
* display_manager
= GetDisplayManager();
336 for (size_t i
= 0; i
< display_manager
->GetNumDisplays(); ++i
) {
337 const gfx::Display
& display
= display_manager
->GetDisplayAt(i
);
338 if (primary_display_id
!= display
.id()) {
339 AshWindowTreeHost
* ash_host
= AddWindowTreeHostForDisplay(
340 display
, AshWindowTreeHostInitParams());
341 RootWindowController::CreateForSecondaryDisplay(ash_host
);
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
);
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 AshWindowTreeHost
* host
= GetAshWindowTreeHostForDisplayId(id
);
369 return GetWindow(host
);
372 AshWindowTreeHost
* DisplayController::GetAshWindowTreeHostForDisplayId(
374 CHECK_EQ(1u, window_tree_hosts_
.count(display_id
))
375 << "display id = " << display_id
;
376 return window_tree_hosts_
[display_id
];
379 void DisplayController::CloseChildWindows() {
380 for (WindowTreeHostMap::const_iterator it
= window_tree_hosts_
.begin();
381 it
!= window_tree_hosts_
.end();
383 aura::Window
* root_window
= GetWindow(it
->second
);
384 RootWindowController
* controller
= GetRootWindowController(root_window
);
386 controller
->CloseChildWindows();
388 while (!root_window
->children().empty()) {
389 aura::Window
* child
= root_window
->children()[0];
396 aura::Window::Windows
DisplayController::GetAllRootWindows() {
397 aura::Window::Windows windows
;
398 for (WindowTreeHostMap::const_iterator it
= window_tree_hosts_
.begin();
399 it
!= window_tree_hosts_
.end();
402 if (GetRootWindowController(GetWindow(it
->second
)))
403 windows
.push_back(GetWindow(it
->second
));
408 gfx::Insets
DisplayController::GetOverscanInsets(int64 display_id
) const {
409 return GetDisplayManager()->GetOverscanInsets(display_id
);
412 void DisplayController::SetOverscanInsets(int64 display_id
,
413 const gfx::Insets
& insets_in_dip
) {
414 GetDisplayManager()->SetOverscanInsets(display_id
, insets_in_dip
);
417 std::vector
<RootWindowController
*>
418 DisplayController::GetAllRootWindowControllers() {
419 std::vector
<RootWindowController
*> controllers
;
420 for (WindowTreeHostMap::const_iterator it
= window_tree_hosts_
.begin();
421 it
!= window_tree_hosts_
.end();
423 RootWindowController
* controller
=
424 GetRootWindowController(GetWindow(it
->second
));
426 controllers
.push_back(controller
);
431 void DisplayController::ToggleMirrorMode() {
432 DisplayManager
* display_manager
= GetDisplayManager();
433 if (display_manager
->num_connected_displays() <= 1)
437 if (limiter_
->IsThrottled())
439 limiter_
->SetThrottleTimeout(kCycleDisplayThrottleTimeoutMs
);
441 #if defined(OS_CHROMEOS)
442 Shell
* shell
= Shell::GetInstance();
443 DisplayConfiguratorAnimation
* animation
=
444 shell
->display_configurator_animation();
445 animation
->StartFadeOutAnimation(base::Bind(
446 &DisplayController::SetMirrorModeAfterAnimation
,
447 weak_ptr_factory_
.GetWeakPtr(), !display_manager
->IsInMirrorMode()));
451 void DisplayController::SwapPrimaryDisplay() {
453 if (limiter_
->IsThrottled())
455 limiter_
->SetThrottleTimeout(kSwapDisplayThrottleTimeoutMs
);
458 if (Shell::GetScreen()->GetNumDisplays() > 1) {
459 #if defined(OS_CHROMEOS)
460 DisplayConfiguratorAnimation
* animation
=
461 Shell::GetInstance()->display_configurator_animation();
463 animation
->StartFadeOutAnimation(base::Bind(
464 &DisplayController::OnFadeOutForSwapDisplayFinished
,
465 weak_ptr_factory_
.GetWeakPtr()));
467 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
470 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
475 void DisplayController::SetPrimaryDisplayId(int64 id
) {
476 DCHECK_NE(gfx::Display::kInvalidDisplayID
, id
);
477 if (id
== gfx::Display::kInvalidDisplayID
|| primary_display_id
== id
)
480 const gfx::Display
& display
= GetDisplayManager()->GetDisplayForId(id
);
481 if (display
.is_valid())
482 SetPrimaryDisplay(display
);
485 void DisplayController::SetPrimaryDisplay(
486 const gfx::Display
& new_primary_display
) {
487 DisplayManager
* display_manager
= GetDisplayManager();
488 DCHECK(new_primary_display
.is_valid());
489 DCHECK(display_manager
->GetDisplayForId(new_primary_display
.id()).is_valid());
491 if (!new_primary_display
.is_valid() ||
492 !display_manager
->GetDisplayForId(new_primary_display
.id()).is_valid()) {
493 LOG(ERROR
) << "Invalid or non-existent display is requested:"
494 << new_primary_display
.ToString();
498 if (primary_display_id
== new_primary_display
.id() ||
499 window_tree_hosts_
.size() < 2) {
503 AshWindowTreeHost
* non_primary_host
=
504 window_tree_hosts_
[new_primary_display
.id()];
505 LOG_IF(ERROR
, !non_primary_host
)
506 << "Unknown display is requested in SetPrimaryDisplay: id="
507 << new_primary_display
.id();
508 if (!non_primary_host
)
511 gfx::Display old_primary_display
= Shell::GetScreen()->GetPrimaryDisplay();
513 // Swap root windows between current and new primary display.
514 AshWindowTreeHost
* primary_host
= window_tree_hosts_
[primary_display_id
];
516 CHECK_NE(primary_host
, non_primary_host
);
518 window_tree_hosts_
[new_primary_display
.id()] = primary_host
;
519 GetRootWindowSettings(GetWindow(primary_host
))->display_id
=
520 new_primary_display
.id();
522 window_tree_hosts_
[old_primary_display
.id()] = non_primary_host
;
523 GetRootWindowSettings(GetWindow(non_primary_host
))->display_id
=
524 old_primary_display
.id();
526 primary_display_id
= new_primary_display
.id();
527 GetDisplayManager()->layout_store()->UpdatePrimaryDisplayId(
528 display_manager
->GetCurrentDisplayIdPair(), primary_display_id
);
530 UpdateWorkAreaOfDisplayNearestWindow(GetWindow(primary_host
),
531 old_primary_display
.GetWorkAreaInsets());
532 UpdateWorkAreaOfDisplayNearestWindow(GetWindow(non_primary_host
),
533 new_primary_display
.GetWorkAreaInsets());
535 // Update the dispay manager with new display info.
536 std::vector
<DisplayInfo
> display_info_list
;
537 display_info_list
.push_back(display_manager
->GetDisplayInfo(
538 primary_display_id
));
539 display_info_list
.push_back(display_manager
->GetDisplayInfo(
540 ScreenUtil::GetSecondaryDisplay().id()));
541 GetDisplayManager()->set_force_bounds_changed(true);
542 GetDisplayManager()->UpdateDisplays(display_info_list
);
543 GetDisplayManager()->set_force_bounds_changed(false);
546 void DisplayController::UpdateMouseLocationAfterDisplayChange() {
547 // If the mouse is currently on a display in native location,
548 // use the same native location. Otherwise find the display closest
549 // to the current cursor location in screen coordinates.
551 gfx::Point point_in_screen
= Shell::GetScreen()->GetCursorScreenPoint();
552 gfx::Point target_location_in_native
;
553 int64 closest_distance_squared
= -1;
554 DisplayManager
* display_manager
= GetDisplayManager();
556 aura::Window
* dst_root_window
= nullptr;
557 for (size_t i
= 0; i
< display_manager
->GetNumDisplays(); ++i
) {
558 const gfx::Display
& display
= display_manager
->GetDisplayAt(i
);
559 const DisplayInfo display_info
=
560 display_manager
->GetDisplayInfo(display
.id());
561 aura::Window
* root_window
= GetRootWindowForDisplayId(display
.id());
562 if (display_info
.bounds_in_native().Contains(
563 cursor_location_in_native_coords_for_restore_
)) {
564 dst_root_window
= root_window
;
565 target_location_in_native
= cursor_location_in_native_coords_for_restore_
;
568 gfx::Point center
= display
.bounds().CenterPoint();
569 // Use the distance squared from the center of the dislay. This is not
570 // exactly "closest" display, but good enough to pick one
571 // appropriate (and there are at most two displays).
572 // We don't care about actual distance, only relative to other displays, so
573 // using the LengthSquared() is cheaper than Length().
575 int64 distance_squared
= (center
- point_in_screen
).LengthSquared();
576 if (closest_distance_squared
< 0 ||
577 closest_distance_squared
> distance_squared
) {
578 aura::Window
* root_window
= GetRootWindowForDisplayId(display
.id());
579 ::wm::ConvertPointFromScreen(root_window
, ¢er
);
580 root_window
->GetHost()->ConvertPointToNativeScreen(¢er
);
581 dst_root_window
= root_window
;
582 target_location_in_native
= center
;
583 closest_distance_squared
= distance_squared
;
587 gfx::Point target_location_in_root
= target_location_in_native
;
588 dst_root_window
->GetHost()->ConvertPointFromNativeScreen(
589 &target_location_in_root
);
591 #if defined(USE_OZONE)
592 gfx::Point target_location_in_screen
= target_location_in_root
;
593 ::wm::ConvertPointToScreen(dst_root_window
, &target_location_in_screen
);
594 const gfx::Display
& target_display
=
595 display_manager
->FindDisplayContainingPoint(target_location_in_screen
);
596 // If the original location isn't on any of new display, let ozone move
598 if (!target_display
.is_valid())
600 int64 target_display_id
= target_display
.id();
602 // Do not move the cursor if the cursor's location did not change. This avoids
603 // moving (and showing) the cursor:
605 // - When the device is rotated in maximized mode.
606 // |cursor_display_id_for_restore_| is checked to ensure that the cursor is
607 // moved when the cursor's native position does not change but the display
608 // that it is on has changed. This occurs when swapping the primary display.
609 if (target_location_in_native
!=
610 cursor_location_in_native_coords_for_restore_
||
611 target_display_id
!= cursor_display_id_for_restore_
) {
612 dst_root_window
->MoveCursorTo(target_location_in_root
);
613 } else if (target_location_in_screen
!=
614 cursor_location_in_screen_coords_for_restore_
) {
615 // The cursor's native position did not change but its screen position did
616 // change. This occurs when the scale factor or the rotation of the display
617 // that the cursor is on changes.
618 Shell::GetInstance()->cursor_manager()->SetDisplay(target_display
);
620 // Update the cursor's root location. This ends up dispatching a synthetic
621 // mouse move. The synthetic mouse move updates the composited cursor's
622 // location and hover effects. Synthetic mouse moves do not affect the
623 // cursor's visibility.
624 dst_root_window
->GetHost()->dispatcher()->OnCursorMovedToRootLocation(
625 target_location_in_root
);
628 dst_root_window
->MoveCursorTo(target_location_in_root
);
632 bool DisplayController::UpdateWorkAreaOfDisplayNearestWindow(
633 const aura::Window
* window
,
634 const gfx::Insets
& insets
) {
635 const aura::Window
* root_window
= window
->GetRootWindow();
636 int64 id
= GetRootWindowSettings(root_window
)->display_id
;
637 // if id is |kInvaildDisplayID|, it's being deleted.
638 DCHECK(id
!= gfx::Display::kInvalidDisplayID
);
639 return GetDisplayManager()->UpdateWorkAreaOfDisplay(id
, insets
);
642 void DisplayController::OnDisplayAdded(const gfx::Display
& display
) {
643 #if defined(OS_CHROMEOS)
644 // If we're switching from/to offscreen WTH, we need to
645 // create new WTH for primary display instead of reusing.
646 if (primary_tree_host_for_replace_
&&
647 (GetRootWindowSettings(GetWindow(primary_tree_host_for_replace_
))
648 ->display_id
== DisplayManager::kUnifiedDisplayId
||
649 display
.id() == DisplayManager::kUnifiedDisplayId
)) {
650 DCHECK_EQ(gfx::Display::kInvalidDisplayID
, primary_display_id
);
651 primary_display_id
= display
.id();
653 AshWindowTreeHost
* ash_host
=
654 AddWindowTreeHostForDisplay(display
, AshWindowTreeHostInitParams());
655 RootWindowController::CreateForSecondaryDisplay(ash_host
);
657 // Magnifier controllers keep pointers to the current root window.
658 // Update them here to avoid accessing them later.
659 Shell::GetInstance()->magnification_controller()->SwitchTargetRootWindow(
660 ash_host
->AsWindowTreeHost()->window(), false);
662 ->partial_magnification_controller()
663 ->SwitchTargetRootWindow(ash_host
->AsWindowTreeHost()->window());
665 AshWindowTreeHost
* to_delete
= primary_tree_host_for_replace_
;
666 primary_tree_host_for_replace_
= nullptr;
668 // Show the shelf if the original WTH had a visible system
669 // tray. It may or may not be visible depending on OOBE state.
670 ash::SystemTray
* old_tray
=
671 GetRootWindowController(to_delete
->AsWindowTreeHost()->window())
673 ash::SystemTray
* new_tray
=
674 ash::Shell::GetInstance()->GetPrimarySystemTray();
675 if (old_tray
->GetWidget()->IsVisible()) {
676 new_tray
->SetVisible(true);
677 new_tray
->GetWidget()->Show();
680 DeleteHost(to_delete
);
682 auto iter
= std::find_if(
683 window_tree_hosts_
.begin(), window_tree_hosts_
.end(),
684 [to_delete
](const std::pair
<int64
, AshWindowTreeHost
*>& pair
) {
685 return pair
.second
== to_delete
;
687 DCHECK(iter
== window_tree_hosts_
.end());
689 // the host has already been removed from the window_tree_host_.
692 // TODO(oshima): It should be possible to consolidate logic for
693 // unified and non unified, but I'm keeping them separated to minimize
694 // the risk in M44. I'll consolidate this in M45.
695 if (primary_tree_host_for_replace_
) {
696 DCHECK(window_tree_hosts_
.empty());
697 primary_display_id
= display
.id();
698 window_tree_hosts_
[display
.id()] = primary_tree_host_for_replace_
;
699 GetRootWindowSettings(GetWindow(primary_tree_host_for_replace_
))
700 ->display_id
= display
.id();
701 primary_tree_host_for_replace_
= nullptr;
702 const DisplayInfo
& display_info
=
703 GetDisplayManager()->GetDisplayInfo(display
.id());
704 AshWindowTreeHost
* ash_host
= window_tree_hosts_
[display
.id()];
705 ash_host
->AsWindowTreeHost()->SetBounds(display_info
.bounds_in_native());
706 SetDisplayPropertiesOnHost(ash_host
, display
);
708 if (primary_display_id
== gfx::Display::kInvalidDisplayID
)
709 primary_display_id
= display
.id();
710 DCHECK(!window_tree_hosts_
.empty());
711 AshWindowTreeHost
* ash_host
=
712 AddWindowTreeHostForDisplay(display
, AshWindowTreeHostInitParams());
713 RootWindowController::CreateForSecondaryDisplay(ash_host
);
717 void DisplayController::DeleteHost(AshWindowTreeHost
* host_to_delete
) {
718 ClearDisplayPropertiesOnHost(host_to_delete
);
719 RootWindowController
* controller
=
720 GetRootWindowController(GetWindow(host_to_delete
));
722 controller
->MoveWindowsTo(GetPrimaryRootWindow());
723 // Delete most of root window related objects, but don't delete
724 // root window itself yet because the stack may be using it.
725 controller
->Shutdown();
726 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, controller
);
729 void DisplayController::OnDisplayRemoved(const gfx::Display
& display
) {
730 AshWindowTreeHost
* host_to_delete
= window_tree_hosts_
[display
.id()];
731 CHECK(host_to_delete
) << display
.ToString();
733 // When the primary root window's display is removed, move the primary
734 // root to the other display.
735 if (primary_display_id
== display
.id()) {
736 // Temporarily store the primary root window in
737 // |primary_root_window_for_replace_| when replacing the display.
738 if (window_tree_hosts_
.size() == 1) {
739 primary_display_id
= gfx::Display::kInvalidDisplayID
;
740 primary_tree_host_for_replace_
= host_to_delete
;
741 // Display for root window will be deleted when the Primary RootWindow
742 // is deleted by the Shell.
743 window_tree_hosts_
.erase(display
.id());
746 for (const auto& pair
: window_tree_hosts_
) {
747 if (pair
.first
!= display
.id()) {
748 primary_display_id
= pair
.first
;
752 CHECK_NE(gfx::Display::kInvalidDisplayID
, primary_display_id
);
754 AshWindowTreeHost
* primary_host
= host_to_delete
;
755 // Delete the other host instead.
756 host_to_delete
= window_tree_hosts_
[primary_display_id
];
757 GetRootWindowSettings(GetWindow(host_to_delete
))->display_id
= display
.id();
759 // Setup primary root.
760 window_tree_hosts_
[primary_display_id
] = primary_host
;
761 GetRootWindowSettings(GetWindow(primary_host
))->display_id
=
764 OnDisplayMetricsChanged(
765 GetDisplayManager()->GetDisplayForId(primary_display_id
),
766 DISPLAY_METRIC_BOUNDS
);
769 DeleteHost(host_to_delete
);
771 // The window tree host should be erased at last because some handlers can
772 // access to the host through GetRootWindowForDisplayId() during
773 // MoveWindowsTo(). See http://crbug.com/415222
774 window_tree_hosts_
.erase(display
.id());
777 void DisplayController::OnDisplayMetricsChanged(const gfx::Display
& display
,
779 if (!(metrics
& (DISPLAY_METRIC_BOUNDS
| DISPLAY_METRIC_ROTATION
|
780 DISPLAY_METRIC_DEVICE_SCALE_FACTOR
)))
782 const DisplayInfo
& display_info
=
783 GetDisplayManager()->GetDisplayInfo(display
.id());
784 DCHECK(!display_info
.bounds_in_native().IsEmpty());
785 AshWindowTreeHost
* ash_host
= window_tree_hosts_
[display
.id()];
786 ash_host
->AsWindowTreeHost()->SetBounds(display_info
.bounds_in_native());
787 SetDisplayPropertiesOnHost(ash_host
, display
);
790 void DisplayController::OnHostResized(const aura::WindowTreeHost
* host
) {
791 gfx::Display display
= Shell::GetScreen()->GetDisplayNearestWindow(
792 const_cast<aura::Window
*>(host
->window()));
794 DisplayManager
* display_manager
= GetDisplayManager();
795 if (display_manager
->UpdateDisplayBounds(display
.id(), host
->GetBounds())) {
796 mirror_window_controller_
->UpdateWindow();
797 cursor_window_controller_
->UpdateContainer();
801 void DisplayController::CreateOrUpdateMirroringDisplay(
802 const DisplayInfoList
& info_list
) {
803 if (GetDisplayManager()->IsInMirrorMode() ||
804 GetDisplayManager()->IsInUnifiedMode()) {
805 mirror_window_controller_
->UpdateWindow(info_list
);
806 cursor_window_controller_
->UpdateContainer();
812 void DisplayController::CloseMirroringDisplayIfNotNecessary() {
813 mirror_window_controller_
->CloseIfNotNecessary();
814 // If cursor_compositing is enabled for large cursor, the cursor window is
815 // always on the desktop display (the visible cursor on the non-desktop
816 // display is drawn through compositor mirroring). Therefore, it's unnecessary
817 // to handle the cursor_window at all. See: http://crbug.com/412910
818 if (!cursor_window_controller_
->is_cursor_compositing_enabled())
819 cursor_window_controller_
->UpdateContainer();
822 void DisplayController::PreDisplayConfigurationChange(bool clear_focus
) {
823 FOR_EACH_OBSERVER(Observer
, observers_
, OnDisplayConfigurationChanging());
824 focus_activation_store_
->Store(clear_focus
);
825 gfx::Screen
* screen
= Shell::GetScreen();
826 gfx::Point point_in_screen
= screen
->GetCursorScreenPoint();
827 cursor_location_in_screen_coords_for_restore_
= point_in_screen
;
829 gfx::Display display
= screen
->GetDisplayNearestPoint(point_in_screen
);
830 cursor_display_id_for_restore_
= display
.id();
832 gfx::Point point_in_native
= point_in_screen
;
833 aura::Window
* root_window
= GetRootWindowForDisplayId(display
.id());
834 ::wm::ConvertPointFromScreen(root_window
, &point_in_native
);
835 root_window
->GetHost()->ConvertPointToNativeScreen(&point_in_native
);
836 cursor_location_in_native_coords_for_restore_
= point_in_native
;
839 void DisplayController::PostDisplayConfigurationChange() {
841 limiter_
->SetThrottleTimeout(kAfterDisplayChangeThrottleTimeoutMs
);
843 focus_activation_store_
->Restore();
845 DisplayManager
* display_manager
= GetDisplayManager();
846 DisplayLayoutStore
* layout_store
= display_manager
->layout_store();
847 if (display_manager
->num_connected_displays() > 1) {
848 DisplayIdPair pair
= display_manager
->GetCurrentDisplayIdPair();
849 layout_store
->UpdateMultiDisplayState(
850 pair
, display_manager
->IsInMirrorMode(),
851 display_manager
->default_multi_display_mode() ==
852 DisplayManager::UNIFIED
);
854 if (Shell::GetScreen()->GetNumDisplays() > 1 ) {
855 DisplayLayout layout
= layout_store
->GetRegisteredDisplayLayout(pair
);
856 int64 primary_id
= layout
.primary_id
;
858 primary_id
== gfx::Display::kInvalidDisplayID
?
859 pair
.first
: primary_id
);
860 // Update the primary_id in case the above call is
861 // ignored. Happens when a) default layout's primary id
862 // doesn't exist, or b) the primary_id has already been
863 // set to the same and didn't update it.
864 layout_store
->UpdatePrimaryDisplayId(
865 pair
, Shell::GetScreen()->GetPrimaryDisplay().id());
868 FOR_EACH_OBSERVER(Observer
, observers_
, OnDisplayConfigurationChanged());
869 UpdateMouseLocationAfterDisplayChange();
872 bool DisplayController::DispatchKeyEventPostIME(const ui::KeyEvent
& event
) {
873 // Getting the active root window to dispatch the event. This isn't
874 // significant as the event will be sent to the window resolved by
875 // aura::client::FocusClient which is FocusController in ash.
876 aura::Window
* active_window
= wm::GetActiveWindow();
877 aura::Window
* root_window
= active_window
? active_window
->GetRootWindow()
878 : Shell::GetPrimaryRootWindow();
879 return root_window
->GetHost()->DispatchKeyEventPostIME(event
);
882 AshWindowTreeHost
* DisplayController::AddWindowTreeHostForDisplay(
883 const gfx::Display
& display
,
884 const AshWindowTreeHostInitParams
& init_params
) {
885 static int host_count
= 0;
886 const DisplayInfo
& display_info
=
887 GetDisplayManager()->GetDisplayInfo(display
.id());
888 AshWindowTreeHostInitParams
params_with_bounds(init_params
);
889 params_with_bounds
.initial_bounds
= display_info
.bounds_in_native();
890 params_with_bounds
.offscreen
=
891 display
.id() == DisplayManager::kUnifiedDisplayId
;
892 AshWindowTreeHost
* ash_host
= AshWindowTreeHost::Create(params_with_bounds
);
893 aura::WindowTreeHost
* host
= ash_host
->AsWindowTreeHost();
894 if (!input_method_
) { // Singleton input method instance for Ash.
895 input_method_
= ui::CreateInputMethod(this, host
->GetAcceleratedWidget());
896 input_method_
->OnFocus();
898 host
->SetSharedInputMethod(input_method_
.get());
900 host
->window()->SetName(base::StringPrintf(
901 "%sRootWindow-%d", params_with_bounds
.offscreen
? "Offscreen" : "",
903 host
->window()->SetTitle(base::UTF8ToUTF16(display_info
.name()));
904 host
->compositor()->SetBackgroundColor(SK_ColorBLACK
);
905 // No need to remove our observer observer because the DisplayController
906 // outlives the host.
907 host
->AddObserver(this);
908 InitRootWindowSettings(host
->window())->display_id
= display
.id();
911 window_tree_hosts_
[display
.id()] = ash_host
;
912 SetDisplayPropertiesOnHost(ash_host
, display
);
914 #if defined(OS_CHROMEOS)
915 if (switches::ConstrainPointerToRoot())
916 ash_host
->ConfineCursorToRootWindow();
921 void DisplayController::OnFadeOutForSwapDisplayFinished() {
922 #if defined(OS_CHROMEOS)
923 SetPrimaryDisplay(ScreenUtil::GetSecondaryDisplay());
924 Shell::GetInstance()->display_configurator_animation()
925 ->StartFadeInAnimation();
929 void DisplayController::SetMirrorModeAfterAnimation(bool mirror
) {
930 GetDisplayManager()->SetMirrorMode(mirror
);