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/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
;
49 class VirtualKeyboardWindowController
;
51 // DisplayController owns and maintains RootWindows for each attached
52 // display, keeping them in sync with display configuration changes.
53 class ASH_EXPORT DisplayController
: public gfx::DisplayObserver
,
54 public aura::WindowTreeHostObserver
,
55 public DisplayManager::Delegate
{
57 class ASH_EXPORT Observer
{
59 // Invoked only once after all displays are initialized
61 virtual void OnDisplaysInitialized() {}
63 // Invoked when the display configuration change is requested,
64 // but before the change is applied to aura/ash.
65 virtual void OnDisplayConfigurationChanging() {}
67 // Invoked when the all display configuration changes
69 virtual void OnDisplayConfigurationChanged() {};
72 virtual ~Observer() {}
76 virtual ~DisplayController();
81 // Returns primary display's ID.
82 // TODO(oshima): Move this out from DisplayController;
83 static int64
GetPrimaryDisplayId();
85 CursorWindowController
* cursor_window_controller() {
86 return cursor_window_controller_
.get();
89 MirrorWindowController
* mirror_window_controller() {
90 return mirror_window_controller_
.get();
93 VirtualKeyboardWindowController
* virtual_keyboard_window_controller() {
94 return virtual_keyboard_window_controller_
.get();
97 // Create a WindowTreeHost for the primary display. This replaces
98 // |initial_bounds| in |init_params|.
99 void CreatePrimaryHost(const AshWindowTreeHostInitParams
& init_params
);
101 // Initializes all displays.
104 // Add/Remove observers.
105 void AddObserver(Observer
* observer
);
106 void RemoveObserver(Observer
* observer
);
108 // Returns the root window for primary display.
109 aura::Window
* GetPrimaryRootWindow();
111 // Returns the root window for |display_id|.
112 aura::Window
* GetRootWindowForDisplayId(int64 id
);
114 // Toggle mirror mode.
115 void ToggleMirrorMode();
117 // Swap primary and secondary display.
118 void SwapPrimaryDisplay();
120 // Sets the ID of the primary display. If the display is not connected, it
121 // will switch the primary display when connected.
122 void SetPrimaryDisplayId(int64 id
);
124 // Sets primary display. This re-assigns the current root
125 // window to given |display|.
126 void SetPrimaryDisplay(const gfx::Display
& display
);
128 // Closes all child windows in the all root windows.
129 void CloseChildWindows();
131 // Returns all root windows. In non extended desktop mode, this
132 // returns the primary root window only.
133 aura::Window::Windows
GetAllRootWindows();
135 // Returns all oot window controllers. In non extended desktop
136 // mode, this return a RootWindowController for the primary root window only.
137 std::vector
<RootWindowController
*> GetAllRootWindowControllers();
139 // Gets/Sets/Clears the overscan insets for the specified |display_id|. See
140 // display_manager.h for the details.
141 gfx::Insets
GetOverscanInsets(int64 display_id
) const;
142 void SetOverscanInsets(int64 display_id
, const gfx::Insets
& insets_in_dip
);
144 // Checks if the mouse pointer is on one of displays, and moves to
145 // the center of the nearest display if it's outside of all displays.
146 void EnsurePointerInDisplays();
148 // Sets the work area's |insets| to the display assigned to |window|.
149 bool UpdateWorkAreaOfDisplayNearestWindow(const aura::Window
* window
,
150 const gfx::Insets
& insets
);
151 // gfx::DisplayObserver overrides:
152 virtual void OnDisplayAdded(const gfx::Display
& display
) OVERRIDE
;
153 virtual void OnDisplayRemoved(const gfx::Display
& display
) OVERRIDE
;
154 virtual void OnDisplayMetricsChanged(const gfx::Display
& display
,
155 uint32_t metrics
) OVERRIDE
;
157 // aura::WindowTreeHostObserver overrides:
158 virtual void OnHostResized(const aura::WindowTreeHost
* host
) OVERRIDE
;
160 // aura::DisplayManager::Delegate overrides:
161 virtual void CreateOrUpdateNonDesktopDisplay(const DisplayInfo
& info
)
163 virtual void CloseNonDesktopDisplay() OVERRIDE
;
164 virtual void PreDisplayConfigurationChange(bool clear_focus
) OVERRIDE
;
165 virtual void PostDisplayConfigurationChange() OVERRIDE
;
168 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest
, BoundsUpdated
);
169 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest
, SecondaryDisplayLayout
);
170 friend class DisplayManager
;
171 friend class MirrorWindowController
;
173 // Creates a WindowTreeHost for |display| and stores it in the
174 // |window_tree_hosts_| map.
175 AshWindowTreeHost
* AddWindowTreeHostForDisplay(
176 const gfx::Display
& display
,
177 const AshWindowTreeHostInitParams
& params
);
179 void OnFadeOutForSwapDisplayFinished();
181 void SetMirrorModeAfterAnimation(bool mirror
);
183 void UpdateHostWindowNames();
185 class DisplayChangeLimiter
{
187 DisplayChangeLimiter();
189 // Sets how long the throttling should last.
190 void SetThrottleTimeout(int64 throttle_ms
);
192 bool IsThrottled() const;
195 // The time when the throttling ends.
196 base::Time throttle_timeout_
;
198 DISALLOW_COPY_AND_ASSIGN(DisplayChangeLimiter
);
201 // The limiter to throttle how fast a user can
202 // change the display configuration.
203 scoped_ptr
<DisplayChangeLimiter
> limiter_
;
205 typedef std::map
<int64
, AshWindowTreeHost
*> WindowTreeHostMap
;
206 // The mapping from display ID to its window tree host.
207 WindowTreeHostMap window_tree_hosts_
;
209 ObserverList
<Observer
> observers_
;
211 // Store the primary window tree host temporarily while replacing
213 AshWindowTreeHost
* primary_tree_host_for_replace_
;
215 scoped_ptr
<FocusActivationStore
> focus_activation_store_
;
217 scoped_ptr
<CursorWindowController
> cursor_window_controller_
;
218 scoped_ptr
<MirrorWindowController
> mirror_window_controller_
;
219 scoped_ptr
<VirtualKeyboardWindowController
>
220 virtual_keyboard_window_controller_
;
222 // Stores the curent cursor location (in native coordinates) used to
223 // restore the cursor location when display configuration
225 gfx::Point cursor_location_in_native_coords_for_restore_
;
227 base::WeakPtrFactory
<DisplayController
> weak_ptr_factory_
;
229 DISALLOW_COPY_AND_ASSIGN(DisplayController
);
234 #endif // ASH_DISPLAY_DISPLAY_CONTROLLER_H_