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/gfx/display_observer.h"
21 #include "ui/gfx/point.h"
30 template <typename T> class JSONValueConverter;
42 class FocusActivationStore;
43 class MirrorWindowController;
44 class RootWindowController;
45 class VirtualKeyboardWindowController;
48 // DisplayController owns and maintains RootWindows for each attached
49 // display, keeping them in sync with display configuration changes.
50 class ASH_EXPORT DisplayController : public gfx::DisplayObserver,
51 public aura::RootWindowObserver,
52 public internal::DisplayManager::Delegate {
54 class ASH_EXPORT Observer {
56 // Invoked when the display configuration change is requested,
57 // but before the change is applied to aura/ash.
58 virtual void OnDisplayConfigurationChanging() {}
60 // Invoked when the all display configuration changes
62 virtual void OnDisplayConfigurationChanged() {};
65 virtual ~Observer() {}
69 virtual ~DisplayController();
74 // Returns primary display. This is safe to use after ash::Shell is
76 static const gfx::Display& GetPrimaryDisplay();
78 // Returns the number of display. This is safe to use after
79 // ash::Shell is deleted.
80 static int GetNumDisplays();
82 internal::MirrorWindowController* mirror_window_controller() {
83 return mirror_window_controller_.get();
86 // Initializes primary display.
87 void InitPrimaryDisplay();
89 // Initialize secondary displays.
90 void InitSecondaryDisplays();
92 // Add/Remove observers.
93 void AddObserver(Observer* observer);
94 void RemoveObserver(Observer* observer);
96 // Returns the root window for primary display.
97 aura::Window* GetPrimaryRootWindow();
99 // Returns the root window for |display_id|.
100 aura::Window* GetRootWindowForDisplayId(int64 id);
102 // Toggle mirror mode.
103 void ToggleMirrorMode();
105 // Swap primary and secondary display.
106 void SwapPrimaryDisplay();
108 // Sets the ID of the primary display. If the display is not connected, it
109 // will switch the primary display when connected.
110 void SetPrimaryDisplayId(int64 id);
112 // Sets primary display. This re-assigns the current root
113 // window to given |display|.
114 void SetPrimaryDisplay(const gfx::Display& display);
116 // Closes all child windows in the all root windows.
117 void CloseChildWindows();
119 // Returns all root windows. In non extended desktop mode, this
120 // returns the primary root window only.
121 std::vector<aura::RootWindow*> GetAllRootWindows();
123 // Returns all oot window controllers. In non extended desktop
124 // mode, this return a RootWindowController for the primary root window only.
125 std::vector<internal::RootWindowController*> GetAllRootWindowControllers();
127 // Gets/Sets/Clears the overscan insets for the specified |display_id|. See
128 // display_manager.h for the details.
129 gfx::Insets GetOverscanInsets(int64 display_id) const;
130 void SetOverscanInsets(int64 display_id, const gfx::Insets& insets_in_dip);
132 // Checks if the mouse pointer is on one of displays, and moves to
133 // the center of the nearest display if it's outside of all displays.
134 void EnsurePointerInDisplays();
136 // Sets the work area's |insets| to the display assigned to |window|.
137 bool UpdateWorkAreaOfDisplayNearestWindow(const aura::Window* window,
138 const gfx::Insets& insets);
140 // Returns the display object nearest given |point|.
141 const gfx::Display& GetDisplayNearestPoint(
142 const gfx::Point& point) const;
144 // Returns the display object nearest given |window|.
145 const gfx::Display& GetDisplayNearestWindow(
146 const aura::Window* window) const;
148 // Returns the display that most closely intersects |match_rect|.
149 const gfx::Display& GetDisplayMatching(
150 const gfx::Rect& match_rect)const;
152 // aura::DisplayObserver overrides:
153 virtual void OnDisplayBoundsChanged(
154 const gfx::Display& display) OVERRIDE;
155 virtual void OnDisplayAdded(const gfx::Display& display) OVERRIDE;
156 virtual void OnDisplayRemoved(const gfx::Display& display) OVERRIDE;
158 // RootWindowObserver overrides:
159 virtual void OnRootWindowHostResized(const aura::RootWindow* root) OVERRIDE;
161 // aura::DisplayManager::Delegate overrides:
162 virtual void CreateOrUpdateNonDesktopDisplay(
163 const internal::DisplayInfo& info) OVERRIDE;
164 virtual void CloseNonDesktopDisplay() OVERRIDE;
165 virtual void PreDisplayConfigurationChange(bool dispay_removed) OVERRIDE;
166 virtual void PostDisplayConfigurationChange() OVERRIDE;
169 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, BoundsUpdated);
170 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, SecondaryDisplayLayout);
171 friend class internal::DisplayManager;
172 friend class internal::MirrorWindowController;
174 // Creates a root window for |display| and stores it in the |root_windows_|
176 aura::RootWindow* AddRootWindowForDisplay(const gfx::Display& display);
178 void OnFadeOutForSwapDisplayFinished();
180 void UpdateHostWindowNames();
182 class DisplayChangeLimiter {
184 DisplayChangeLimiter();
186 // Sets how long the throttling should last.
187 void SetThrottleTimeout(int64 throttle_ms);
189 bool IsThrottled() const;
192 // The time when the throttling ends.
193 base::Time throttle_timeout_;
195 DISALLOW_COPY_AND_ASSIGN(DisplayChangeLimiter);
198 // The limiter to throttle how fast a user can
199 // change the display configuration.
200 scoped_ptr<DisplayChangeLimiter> limiter_;
202 // The mapping from display ID to its root window.
203 std::map<int64, aura::RootWindow*> root_windows_;
205 ObserverList<Observer> observers_;
207 // Store the primary root window temporarily while replacing
209 aura::RootWindow* primary_root_window_for_replace_;
211 scoped_ptr<internal::FocusActivationStore> focus_activation_store_;
213 scoped_ptr<internal::MirrorWindowController> mirror_window_controller_;
214 scoped_ptr<internal::VirtualKeyboardWindowController>
215 virtual_keyboard_window_controller_;
217 // Stores the curent cursor location (in native coordinates) used to
218 // restore the cursor location when display configuration
220 gfx::Point cursor_location_in_native_coords_for_restore_;
222 DISALLOW_COPY_AND_ASSIGN(DisplayController);
227 #endif // ASH_DISPLAY_DISPLAY_CONTROLLER_H_