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/observer_list.h"
18 #include "base/time/time.h"
19 #include "ui/aura/root_window_observer.h"
20 #include "ui/aura/window.h"
21 #include "ui/gfx/display_observer.h"
22 #include "ui/gfx/point.h"
31 template <typename T> class JSONValueConverter;
41 class CursorWindowController;
44 class FocusActivationStore;
45 class MirrorWindowController;
46 class RootWindowController;
47 class VirtualKeyboardWindowController;
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::RootWindowObserver,
54 public internal::DisplayManager::Delegate {
56 class ASH_EXPORT Observer {
58 // Invoked when the display configuration change is requested,
59 // but before the change is applied to aura/ash.
60 virtual void OnDisplayConfigurationChanging() {}
62 // Invoked when the all display configuration changes
64 virtual void OnDisplayConfigurationChanged() {};
67 virtual ~Observer() {}
71 virtual ~DisplayController();
76 // Returns primary display's ID.
77 // TODO(oshima): Move this out from DisplayController;
78 static int64 GetPrimaryDisplayId();
80 internal::CursorWindowController* cursor_window_controller() {
81 return cursor_window_controller_.get();
84 internal::MirrorWindowController* mirror_window_controller() {
85 return mirror_window_controller_.get();
88 internal::VirtualKeyboardWindowController*
89 virtual_keyboard_window_controller() {
90 return virtual_keyboard_window_controller_.get();
93 // Initializes primary display.
94 void InitPrimaryDisplay();
96 // Initialize secondary displays.
97 void InitSecondaryDisplays();
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<internal::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 // aura::DisplayObserver overrides:
147 virtual void OnDisplayBoundsChanged(
148 const gfx::Display& display) OVERRIDE;
149 virtual void OnDisplayAdded(const gfx::Display& display) OVERRIDE;
150 virtual void OnDisplayRemoved(const gfx::Display& display) OVERRIDE;
152 // RootWindowObserver overrides:
153 virtual void OnWindowTreeHostResized(const aura::RootWindow* root) OVERRIDE;
155 // aura::DisplayManager::Delegate overrides:
156 virtual void CreateOrUpdateNonDesktopDisplay(
157 const internal::DisplayInfo& info) OVERRIDE;
158 virtual void CloseNonDesktopDisplay() OVERRIDE;
159 virtual void PreDisplayConfigurationChange(bool clear_focus) OVERRIDE;
160 virtual void PostDisplayConfigurationChange() OVERRIDE;
163 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, BoundsUpdated);
164 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, SecondaryDisplayLayout);
165 friend class internal::DisplayManager;
166 friend class internal::MirrorWindowController;
168 // Creates a root window for |display| and stores it in the |root_windows_|
170 aura::RootWindow* AddRootWindowForDisplay(const gfx::Display& display);
172 void OnFadeOutForSwapDisplayFinished();
174 void UpdateHostWindowNames();
176 class DisplayChangeLimiter {
178 DisplayChangeLimiter();
180 // Sets how long the throttling should last.
181 void SetThrottleTimeout(int64 throttle_ms);
183 bool IsThrottled() const;
186 // The time when the throttling ends.
187 base::Time throttle_timeout_;
189 DISALLOW_COPY_AND_ASSIGN(DisplayChangeLimiter);
192 // The limiter to throttle how fast a user can
193 // change the display configuration.
194 scoped_ptr<DisplayChangeLimiter> limiter_;
196 // The mapping from display ID to its root window.
197 std::map<int64, aura::Window*> root_windows_;
199 ObserverList<Observer> observers_;
201 // Store the primary root window temporarily while replacing
203 aura::Window* primary_root_window_for_replace_;
205 scoped_ptr<internal::FocusActivationStore> focus_activation_store_;
207 scoped_ptr<internal::CursorWindowController> cursor_window_controller_;
208 scoped_ptr<internal::MirrorWindowController> mirror_window_controller_;
209 scoped_ptr<internal::VirtualKeyboardWindowController>
210 virtual_keyboard_window_controller_;
212 // Stores the curent cursor location (in native coordinates) used to
213 // restore the cursor location when display configuration
215 gfx::Point cursor_location_in_native_coords_for_restore_;
217 DISALLOW_COPY_AND_ASSIGN(DisplayController);
222 #endif // ASH_DISPLAY_DISPLAY_CONTROLLER_H_