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/window.h"
20 #include "ui/aura/window_tree_host_observer.h"
21 #include "ui/gfx/display_observer.h"
22 #include "ui/gfx/point.h"
31 template <typename T> class JSONValueConverter;
40 class AshWindowTreeHost;
41 class CursorWindowController;
44 class FocusActivationStore;
45 class MirrorWindowController;
46 class RootWindowController;
47 class VirtualKeyboardWindowController;
49 // DisplayController owns and maintains RootWindows for each attached
50 // display, keeping them in sync with display configuration changes.
51 class ASH_EXPORT DisplayController : public gfx::DisplayObserver,
52 public aura::WindowTreeHostObserver,
53 public DisplayManager::Delegate {
55 class ASH_EXPORT Observer {
57 // Invoked only once after all displays are initialized
59 virtual void OnDisplaysInitialized() {}
61 // Invoked when the display configuration change is requested,
62 // but before the change is applied to aura/ash.
63 virtual void OnDisplayConfigurationChanging() {}
65 // Invoked when the all display configuration changes
67 virtual void OnDisplayConfigurationChanged() {};
70 virtual ~Observer() {}
74 virtual ~DisplayController();
79 // Returns primary display's ID.
80 // TODO(oshima): Move this out from DisplayController;
81 static int64 GetPrimaryDisplayId();
83 CursorWindowController* cursor_window_controller() {
84 return cursor_window_controller_.get();
87 MirrorWindowController* mirror_window_controller() {
88 return mirror_window_controller_.get();
91 VirtualKeyboardWindowController* virtual_keyboard_window_controller() {
92 return virtual_keyboard_window_controller_.get();
95 // Create a WindowTreeHost for the primary display.
96 void CreatePrimaryHost();
98 // Initializes all displays.
101 // Add/Remove observers.
102 void AddObserver(Observer* observer);
103 void RemoveObserver(Observer* observer);
105 // Returns the root window for primary display.
106 aura::Window* GetPrimaryRootWindow();
108 // Returns the root window for |display_id|.
109 aura::Window* GetRootWindowForDisplayId(int64 id);
111 // Toggle mirror mode.
112 void ToggleMirrorMode();
114 // Swap primary and secondary display.
115 void SwapPrimaryDisplay();
117 // Sets the ID of the primary display. If the display is not connected, it
118 // will switch the primary display when connected.
119 void SetPrimaryDisplayId(int64 id);
121 // Sets primary display. This re-assigns the current root
122 // window to given |display|.
123 void SetPrimaryDisplay(const gfx::Display& display);
125 // Closes all child windows in the all root windows.
126 void CloseChildWindows();
128 // Returns all root windows. In non extended desktop mode, this
129 // returns the primary root window only.
130 aura::Window::Windows GetAllRootWindows();
132 // Returns all oot window controllers. In non extended desktop
133 // mode, this return a RootWindowController for the primary root window only.
134 std::vector<RootWindowController*> GetAllRootWindowControllers();
136 // Gets/Sets/Clears the overscan insets for the specified |display_id|. See
137 // display_manager.h for the details.
138 gfx::Insets GetOverscanInsets(int64 display_id) const;
139 void SetOverscanInsets(int64 display_id, const gfx::Insets& insets_in_dip);
141 // Checks if the mouse pointer is on one of displays, and moves to
142 // the center of the nearest display if it's outside of all displays.
143 void EnsurePointerInDisplays();
145 // Sets the work area's |insets| to the display assigned to |window|.
146 bool UpdateWorkAreaOfDisplayNearestWindow(const aura::Window* window,
147 const gfx::Insets& insets);
148 // aura::DisplayObserver overrides:
149 virtual void OnDisplayBoundsChanged(
150 const gfx::Display& display) OVERRIDE;
151 virtual void OnDisplayAdded(const gfx::Display& display) OVERRIDE;
152 virtual void OnDisplayRemoved(const gfx::Display& display) OVERRIDE;
154 // aura::WindowTreeHostObserver overrides:
155 virtual void OnHostResized(const aura::WindowTreeHost* host) OVERRIDE;
157 // aura::DisplayManager::Delegate overrides:
158 virtual void CreateOrUpdateNonDesktopDisplay(const DisplayInfo& info)
160 virtual void CloseNonDesktopDisplay() OVERRIDE;
161 virtual void PreDisplayConfigurationChange(bool clear_focus) OVERRIDE;
162 virtual void PostDisplayConfigurationChange() OVERRIDE;
165 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, BoundsUpdated);
166 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, SecondaryDisplayLayout);
167 friend class DisplayManager;
168 friend class MirrorWindowController;
170 // Creates a WindowTreeHost for |display| and stores it in the
171 // |window_tree_hosts_| map.
172 AshWindowTreeHost* AddWindowTreeHostForDisplay(const gfx::Display& display);
174 void OnFadeOutForSwapDisplayFinished();
176 void UpdateHostWindowNames();
178 class DisplayChangeLimiter {
180 DisplayChangeLimiter();
182 // Sets how long the throttling should last.
183 void SetThrottleTimeout(int64 throttle_ms);
185 bool IsThrottled() const;
188 // The time when the throttling ends.
189 base::Time throttle_timeout_;
191 DISALLOW_COPY_AND_ASSIGN(DisplayChangeLimiter);
194 // The limiter to throttle how fast a user can
195 // change the display configuration.
196 scoped_ptr<DisplayChangeLimiter> limiter_;
198 typedef std::map<int64, AshWindowTreeHost*> WindowTreeHostMap;
199 // The mapping from display ID to its window tree host.
200 WindowTreeHostMap window_tree_hosts_;
202 ObserverList<Observer> observers_;
204 // Store the primary window tree host temporarily while replacing
206 AshWindowTreeHost* primary_tree_host_for_replace_;
208 scoped_ptr<FocusActivationStore> focus_activation_store_;
210 scoped_ptr<CursorWindowController> cursor_window_controller_;
211 scoped_ptr<MirrorWindowController> mirror_window_controller_;
212 scoped_ptr<VirtualKeyboardWindowController>
213 virtual_keyboard_window_controller_;
215 // Stores the curent cursor location (in native coordinates) used to
216 // restore the cursor location when display configuration
218 gfx::Point cursor_location_in_native_coords_for_restore_;
220 DISALLOW_COPY_AND_ASSIGN(DisplayController);
225 #endif // ASH_DISPLAY_DISPLAY_CONTROLLER_H_