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/base/ime/input_method.h"
23 #include "ui/base/ime/input_method_delegate.h"
24 #include "ui/gfx/display_observer.h"
25 #include "ui/gfx/geometry/point.h"
34 template <typename T
> class JSONValueConverter
;
43 class AshWindowTreeHost
;
44 struct AshWindowTreeHostInitParams
;
45 class CursorWindowController
;
48 class FocusActivationStore
;
49 class InputMethodEventHandler
;
50 class MirrorWindowController
;
51 class RootWindowController
;
53 // DisplayController owns and maintains RootWindows for each attached
54 // display, keeping them in sync with display configuration changes.
55 class ASH_EXPORT DisplayController
: public gfx::DisplayObserver
,
56 public aura::WindowTreeHostObserver
,
57 public DisplayManager::Delegate
,
58 public ui::internal::InputMethodDelegate
{
60 class ASH_EXPORT Observer
{
62 // Invoked only once after all displays are initialized
64 virtual void OnDisplaysInitialized() {}
66 // Invoked when the display configuration change is requested,
67 // but before the change is applied to aura/ash.
68 virtual void OnDisplayConfigurationChanging() {}
70 // Invoked when the all display configuration changes
72 virtual void OnDisplayConfigurationChanged() {};
75 virtual ~Observer() {}
79 ~DisplayController() override
;
84 // Returns primary display's ID.
85 // TODO(oshima): Move this out from DisplayController;
86 static int64
GetPrimaryDisplayId();
88 CursorWindowController
* cursor_window_controller() {
89 return cursor_window_controller_
.get();
92 MirrorWindowController
* mirror_window_controller() {
93 return mirror_window_controller_
.get();
96 // Create a WindowTreeHost for the primary display. This replaces
97 // |initial_bounds| in |init_params|.
98 void CreatePrimaryHost(const AshWindowTreeHostInitParams
& init_params
);
100 // Initializes all displays.
103 // Add/Remove observers.
104 void AddObserver(Observer
* observer
);
105 void RemoveObserver(Observer
* observer
);
107 // Returns the root window for primary display.
108 aura::Window
* GetPrimaryRootWindow();
110 // Returns the root window for |display_id|.
111 aura::Window
* GetRootWindowForDisplayId(int64 id
);
113 // Returns AshWTH for given display |id|. Call results in CHECK failure
114 // if the WTH does not exist.
115 AshWindowTreeHost
* GetAshWindowTreeHostForDisplayId(int64 id
);
117 // Toggle mirror mode.
118 void ToggleMirrorMode();
120 // Swap primary and secondary display.
121 void SwapPrimaryDisplay();
123 // Sets the ID of the primary display. If the display is not connected, it
124 // will switch the primary display when connected.
125 void SetPrimaryDisplayId(int64 id
);
127 // Sets primary display. This re-assigns the current root
128 // window to given |display|.
129 void SetPrimaryDisplay(const gfx::Display
& display
);
131 // Closes all child windows in the all root windows.
132 void CloseChildWindows();
134 // Returns all root windows. In non extended desktop mode, this
135 // returns the primary root window only.
136 aura::Window::Windows
GetAllRootWindows();
138 // Returns all oot window controllers. In non extended desktop
139 // mode, this return a RootWindowController for the primary root window only.
140 std::vector
<RootWindowController
*> GetAllRootWindowControllers();
142 // Gets/Sets/Clears the overscan insets for the specified |display_id|. See
143 // display_manager.h for the details.
144 gfx::Insets
GetOverscanInsets(int64 display_id
) const;
145 void SetOverscanInsets(int64 display_id
, const gfx::Insets
& insets_in_dip
);
147 // Checks if the mouse pointer is on one of displays, and moves to
148 // the center of the nearest display if it's outside of all displays.
149 void UpdateMouseLocationAfterDisplayChange();
151 // Sets the work area's |insets| to the display assigned to |window|.
152 bool UpdateWorkAreaOfDisplayNearestWindow(const aura::Window
* window
,
153 const gfx::Insets
& insets
);
155 ui::InputMethod
* input_method() { return input_method_
.get(); }
157 // gfx::DisplayObserver overrides:
158 void OnDisplayAdded(const gfx::Display
& display
) override
;
159 void OnDisplayRemoved(const gfx::Display
& display
) override
;
160 void OnDisplayMetricsChanged(const gfx::Display
& display
,
161 uint32_t metrics
) override
;
163 // aura::WindowTreeHostObserver overrides:
164 void OnHostResized(const aura::WindowTreeHost
* host
) override
;
166 // aura::DisplayManager::Delegate overrides:
167 void CreateOrUpdateMirroringDisplay(
168 const DisplayInfoList
& info_list
) override
;
169 void CloseMirroringDisplayIfNotNecessary() override
;
170 void PreDisplayConfigurationChange(bool clear_focus
) override
;
171 void PostDisplayConfigurationChange() override
;
173 // ui::internal::InputMethodDelegate overrides:
174 bool DispatchKeyEventPostIME(const ui::KeyEvent
& event
) override
;
176 InputMethodEventHandler
* input_method_event_handler() {
177 return input_method_event_handler_
.get();
181 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest
, BoundsUpdated
);
182 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest
, SecondaryDisplayLayout
);
183 friend class DisplayManager
;
184 friend class MirrorWindowController
;
186 // Creates a WindowTreeHost for |display| and stores it in the
187 // |window_tree_hosts_| map.
188 AshWindowTreeHost
* AddWindowTreeHostForDisplay(
189 const gfx::Display
& display
,
190 const AshWindowTreeHostInitParams
& params
);
192 void OnFadeOutForSwapDisplayFinished();
194 void SetMirrorModeAfterAnimation(bool mirror
);
196 // Delete the AsWindowTreeHost. This does not remove the entry from
197 // |window_tree_hosts_|. Caller has to explicitly remove it.
198 void DeleteHost(AshWindowTreeHost
* host_to_delete
);
200 class DisplayChangeLimiter
{
202 DisplayChangeLimiter();
204 // Sets how long the throttling should last.
205 void SetThrottleTimeout(int64 throttle_ms
);
207 bool IsThrottled() const;
210 // The time when the throttling ends.
211 base::Time throttle_timeout_
;
213 DISALLOW_COPY_AND_ASSIGN(DisplayChangeLimiter
);
216 // The limiter to throttle how fast a user can
217 // change the display configuration.
218 scoped_ptr
<DisplayChangeLimiter
> limiter_
;
220 typedef std::map
<int64
, AshWindowTreeHost
*> WindowTreeHostMap
;
221 // The mapping from display ID to its window tree host.
222 WindowTreeHostMap window_tree_hosts_
;
224 base::ObserverList
<Observer
> observers_
;
226 // Store the primary window tree host temporarily while replacing
228 AshWindowTreeHost
* primary_tree_host_for_replace_
;
230 scoped_ptr
<FocusActivationStore
> focus_activation_store_
;
232 scoped_ptr
<CursorWindowController
> cursor_window_controller_
;
233 scoped_ptr
<MirrorWindowController
> mirror_window_controller_
;
235 scoped_ptr
<ui::InputMethod
> input_method_
;
236 scoped_ptr
<InputMethodEventHandler
> input_method_event_handler_
;
238 // Stores the current cursor location (in native coordinates and screen
239 // coordinates respectively). The locations are used to restore the cursor
240 // location when the display configuration changes and to determine whether
241 // the mouse should be moved after a display configuration change.
242 gfx::Point cursor_location_in_native_coords_for_restore_
;
243 gfx::Point cursor_location_in_screen_coords_for_restore_
;
245 // Stores the cursor's display. The id is used to determine whether the mouse
246 // should be moved after a display configuration change.
247 int64 cursor_display_id_for_restore_
;
249 base::WeakPtrFactory
<DisplayController
> weak_ptr_factory_
;
251 DISALLOW_COPY_AND_ASSIGN(DisplayController
);
256 #endif // ASH_DISPLAY_DISPLAY_CONTROLLER_H_