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 #ifndef ASH_DISPLAY_DISPLAY_CONTROLLER_H_
6 #define ASH_DISPLAY_DISPLAY_CONTROLLER_H_
11 #include "ash/ash_export.h"
12 #include "ash/display/display_manager.h"
13 #include "base/basictypes.h"
14 #include "base/compiler_specific.h"
15 #include "base/gtest_prod_util.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/observer_list.h"
19 #include "base/time/time.h"
20 #include "ui/aura/window.h"
21 #include "ui/aura/window_tree_host_observer.h"
22 #include "ui/gfx/display_observer.h"
23 #include "ui/gfx/geometry/point.h"
32 template <typename T
> class JSONValueConverter
;
41 class AshWindowTreeHost
;
42 struct AshWindowTreeHostInitParams
;
43 class CursorWindowController
;
46 class FocusActivationStore
;
47 class MirrorWindowController
;
48 class RootWindowController
;
50 // DisplayController owns and maintains RootWindows for each attached
51 // display, keeping them in sync with display configuration changes.
52 class ASH_EXPORT DisplayController
: public gfx::DisplayObserver
,
53 public aura::WindowTreeHostObserver
,
54 public DisplayManager::Delegate
{
56 class ASH_EXPORT Observer
{
58 // Invoked only once after all displays are initialized
60 virtual void OnDisplaysInitialized() {}
62 // Invoked when the display configuration change is requested,
63 // but before the change is applied to aura/ash.
64 virtual void OnDisplayConfigurationChanging() {}
66 // Invoked when the all display configuration changes
68 virtual void OnDisplayConfigurationChanged() {};
71 virtual ~Observer() {}
75 ~DisplayController() override
;
80 // Returns primary display's ID.
81 // TODO(oshima): Move this out from DisplayController;
82 static int64
GetPrimaryDisplayId();
84 CursorWindowController
* cursor_window_controller() {
85 return cursor_window_controller_
.get();
88 MirrorWindowController
* mirror_window_controller() {
89 return mirror_window_controller_
.get();
92 // Create a WindowTreeHost for the primary display. This replaces
93 // |initial_bounds| in |init_params|.
94 void CreatePrimaryHost(const AshWindowTreeHostInitParams
& init_params
);
96 // Initializes all displays.
99 // Add/Remove observers.
100 void AddObserver(Observer
* observer
);
101 void RemoveObserver(Observer
* observer
);
103 // Returns the root window for primary display.
104 aura::Window
* GetPrimaryRootWindow();
106 // Returns the root window for |display_id|.
107 aura::Window
* GetRootWindowForDisplayId(int64 id
);
109 // Toggle mirror mode.
110 void ToggleMirrorMode();
112 // Swap primary and secondary display.
113 void SwapPrimaryDisplay();
115 // Sets the ID of the primary display. If the display is not connected, it
116 // will switch the primary display when connected.
117 void SetPrimaryDisplayId(int64 id
);
119 // Sets primary display. This re-assigns the current root
120 // window to given |display|.
121 void SetPrimaryDisplay(const gfx::Display
& display
);
123 // Closes all child windows in the all root windows.
124 void CloseChildWindows();
126 // Returns all root windows. In non extended desktop mode, this
127 // returns the primary root window only.
128 aura::Window::Windows
GetAllRootWindows();
130 // Returns all oot window controllers. In non extended desktop
131 // mode, this return a RootWindowController for the primary root window only.
132 std::vector
<RootWindowController
*> GetAllRootWindowControllers();
134 // Gets/Sets/Clears the overscan insets for the specified |display_id|. See
135 // display_manager.h for the details.
136 gfx::Insets
GetOverscanInsets(int64 display_id
) const;
137 void SetOverscanInsets(int64 display_id
, const gfx::Insets
& insets_in_dip
);
139 // Checks if the mouse pointer is on one of displays, and moves to
140 // the center of the nearest display if it's outside of all displays.
141 void EnsurePointerInDisplays();
143 // Sets the work area's |insets| to the display assigned to |window|.
144 bool UpdateWorkAreaOfDisplayNearestWindow(const aura::Window
* window
,
145 const gfx::Insets
& insets
);
146 // gfx::DisplayObserver overrides:
147 void OnDisplayAdded(const gfx::Display
& display
) override
;
148 void OnDisplayRemoved(const gfx::Display
& display
) override
;
149 void OnDisplayMetricsChanged(const gfx::Display
& display
,
150 uint32_t metrics
) override
;
152 // aura::WindowTreeHostObserver overrides:
153 void OnHostResized(const aura::WindowTreeHost
* host
) override
;
155 // aura::DisplayManager::Delegate overrides:
156 void CreateOrUpdateMirroringDisplay(const DisplayInfo
& info
) override
;
157 void CloseMirroringDisplay() override
;
158 void PreDisplayConfigurationChange(bool clear_focus
) override
;
159 void PostDisplayConfigurationChange() override
;
162 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest
, BoundsUpdated
);
163 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest
, SecondaryDisplayLayout
);
164 friend class DisplayManager
;
165 friend class MirrorWindowController
;
167 // Creates a WindowTreeHost for |display| and stores it in the
168 // |window_tree_hosts_| map.
169 AshWindowTreeHost
* AddWindowTreeHostForDisplay(
170 const gfx::Display
& display
,
171 const AshWindowTreeHostInitParams
& params
);
173 void OnFadeOutForSwapDisplayFinished();
175 void SetMirrorModeAfterAnimation(bool mirror
);
177 void UpdateHostWindowNames();
179 class DisplayChangeLimiter
{
181 DisplayChangeLimiter();
183 // Sets how long the throttling should last.
184 void SetThrottleTimeout(int64 throttle_ms
);
186 bool IsThrottled() const;
189 // The time when the throttling ends.
190 base::Time throttle_timeout_
;
192 DISALLOW_COPY_AND_ASSIGN(DisplayChangeLimiter
);
195 // The limiter to throttle how fast a user can
196 // change the display configuration.
197 scoped_ptr
<DisplayChangeLimiter
> limiter_
;
199 typedef std::map
<int64
, AshWindowTreeHost
*> WindowTreeHostMap
;
200 // The mapping from display ID to its window tree host.
201 WindowTreeHostMap window_tree_hosts_
;
203 ObserverList
<Observer
> observers_
;
205 // Store the primary window tree host temporarily while replacing
207 AshWindowTreeHost
* primary_tree_host_for_replace_
;
209 scoped_ptr
<FocusActivationStore
> focus_activation_store_
;
211 scoped_ptr
<CursorWindowController
> cursor_window_controller_
;
212 scoped_ptr
<MirrorWindowController
> mirror_window_controller_
;
214 // Stores the curent cursor location (in native coordinates) used to
215 // restore the cursor location when display configuration
217 gfx::Point cursor_location_in_native_coords_for_restore_
;
219 base::WeakPtrFactory
<DisplayController
> weak_ptr_factory_
;
221 DISALLOW_COPY_AND_ASSIGN(DisplayController
);
226 #endif // ASH_DISPLAY_DISPLAY_CONTROLLER_H_