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. 4 5 #ifndef ASH_DISPLAY_DISPLAY_CONTROLLER_H_ 6 #define ASH_DISPLAY_DISPLAY_CONTROLLER_H_ 7 8 #include <map> 9 #include <vector> 10 11 #include "ash/ash_export.h" 12 #include "ash/display/display_layout.h" 13 #include "ash/display/display_manager.h" 14 #include "base/basictypes.h" 15 #include "base/compiler_specific.h" 16 #include "base/gtest_prod_util.h" 17 #include "base/memory/scoped_ptr.h" 18 #include "base/observer_list.h" 19 #include "base/time/time.h" 20 #include "ui/aura/root_window_observer.h" 21 #include "ui/gfx/display_observer.h" 22 #include "ui/gfx/point.h" 23 24 namespace aura { 25 class Display; 26 class RootWindow; 27 } 28 29 namespace base { 30 class Value; 31 template <typename T> class JSONValueConverter; 32 } 33 34 namespace gfx { 35 class Display; 36 class Insets; 37 } 38 39 namespace ash { 40 namespace internal { 41 class DisplayInfo; 42 class DisplayManager; 43 class FocusActivationStore; 44 class MirrorWindowController; 45 class RootWindowController; 46 } 47 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 { 53 public: 54 class ASH_EXPORT Observer { 55 public: 56 // Invoked when the display configuration change is requested, 57 // but before the change is applied to aura/ash. 58 virtual void OnDisplayConfigurationChanging() {} 59 60 // Invoked when the all display configuration changes 61 // have been applied. 62 virtual void OnDisplayConfigurationChanged() {}; 63 64 protected: 65 virtual ~Observer() {} 66 }; 67 68 DisplayController(); 69 virtual ~DisplayController(); 70 71 void Start(); 72 void Shutdown(); 73 74 // Returns primary display. This is safe to use after ash::Shell is 75 // deleted. 76 static const gfx::Display& GetPrimaryDisplay(); 77 78 // Returns the number of display. This is safe to use after 79 // ash::Shell is deleted. 80 static int GetNumDisplays(); 81 82 internal::MirrorWindowController* mirror_window_controller() { 83 return mirror_window_controller_.get(); 84 } 85 86 // Initializes primary display. 87 void InitPrimaryDisplay(); 88 89 // Initialize secondary displays. 90 void InitSecondaryDisplays(); 91 92 // Add/Remove observers. 93 void AddObserver(Observer* observer); 94 void RemoveObserver(Observer* observer); 95 96 // Returns the root window for primary display. 97 aura::RootWindow* GetPrimaryRootWindow(); 98 99 // Returns the root window for |display_id|. 100 aura::RootWindow* GetRootWindowForDisplayId(int64 id); 101 102 // Toggle mirror mode. 103 void ToggleMirrorMode(); 104 105 // Swap primary and secondary display. 106 void SwapPrimaryDisplay(); 107 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); 111 112 // Sets primary display. This re-assigns the current root 113 // window to given |display|. 114 void SetPrimaryDisplay(const gfx::Display& display); 115 116 // Closes all child windows in the all root windows. 117 void CloseChildWindows(); 118 119 // Returns all root windows. In non extended desktop mode, this 120 // returns the primary root window only. 121 std::vector<aura::RootWindow*> GetAllRootWindows(); 122 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(); 126 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); 131 132 // Sets the layout for the current display pair. The |layout| specifies 133 // the locaion of the secondary display relative to the primary. 134 void SetLayoutForCurrentDisplays(const DisplayLayout& layout); 135 136 // Returns the display layout used for current displays. 137 DisplayLayout GetCurrentDisplayLayout(); 138 139 // Returns the current display pair. 140 DisplayIdPair GetCurrentDisplayIdPair() const; 141 142 // Checks if the mouse pointer is on one of displays, and moves to 143 // the center of the nearest display if it's outside of all displays. 144 void EnsurePointerInDisplays(); 145 146 // Sets the work area's |insets| to the display assigned to |window|. 147 bool UpdateWorkAreaOfDisplayNearestWindow(const aura::Window* window, 148 const gfx::Insets& insets); 149 150 // Returns the display object nearest given |point|. 151 const gfx::Display& GetDisplayNearestPoint( 152 const gfx::Point& point) const; 153 154 // Returns the display object nearest given |window|. 155 const gfx::Display& GetDisplayNearestWindow( 156 const aura::Window* window) const; 157 158 // Returns the display that most closely intersects |match_rect|. 159 const gfx::Display& GetDisplayMatching( 160 const gfx::Rect& match_rect)const; 161 162 // aura::DisplayObserver overrides: 163 virtual void OnDisplayBoundsChanged( 164 const gfx::Display& display) OVERRIDE; 165 virtual void OnDisplayAdded(const gfx::Display& display) OVERRIDE; 166 virtual void OnDisplayRemoved(const gfx::Display& display) OVERRIDE; 167 168 // RootWindowObserver overrides: 169 virtual void OnRootWindowHostResized(const aura::RootWindow* root) OVERRIDE; 170 171 // aura::DisplayManager::Delegate overrides: 172 virtual void CreateOrUpdateMirrorWindow( 173 const internal::DisplayInfo& info) OVERRIDE; 174 virtual void CloseMirrorWindow() OVERRIDE; 175 virtual void PreDisplayConfigurationChange() OVERRIDE; 176 virtual void PostDisplayConfigurationChange() OVERRIDE; 177 178 private: 179 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, BoundsUpdated); 180 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, SecondaryDisplayLayout); 181 friend class internal::DisplayManager; 182 friend class internal::MirrorWindowController; 183 184 // Creates a root window for |display| and stores it in the |root_windows_| 185 // map. 186 aura::RootWindow* AddRootWindowForDisplay(const gfx::Display& display); 187 188 void UpdateDisplayBoundsForLayout(); 189 190 void SetLayoutForDisplayIdPair(const DisplayIdPair& display_pair, 191 const DisplayLayout& layout); 192 193 void OnFadeOutForSwapDisplayFinished(); 194 195 void UpdateHostWindowNames(); 196 197 class DisplayChangeLimiter { 198 public: 199 DisplayChangeLimiter(); 200 201 // Sets how long the throttling should last. 202 void SetThrottleTimeout(int64 throttle_ms); 203 204 bool IsThrottled() const; 205 206 private: 207 // The time when the throttling ends. 208 base::Time throttle_timeout_; 209 210 DISALLOW_COPY_AND_ASSIGN(DisplayChangeLimiter); 211 }; 212 213 // The limiter to throttle how fast a user can 214 // change the display configuration. 215 scoped_ptr<DisplayChangeLimiter> limiter_; 216 217 // The mapping from display ID to its root window. 218 std::map<int64, aura::RootWindow*> root_windows_; 219 220 ObserverList<Observer> observers_; 221 222 // Store the primary root window temporarily while replacing 223 // display. 224 aura::RootWindow* primary_root_window_for_replace_; 225 226 scoped_ptr<internal::FocusActivationStore> focus_activation_store_; 227 228 229 scoped_ptr<internal::MirrorWindowController> mirror_window_controller_; 230 231 // Stores the curent cursor location (in native coordinates) used to 232 // restore the cursor location when display configuration 233 // changed. 234 gfx::Point cursor_location_in_native_coords_for_restore_; 235 236 DISALLOW_COPY_AND_ASSIGN(DisplayController); 237 }; 238 239 } // namespace ash 240 241 #endif // ASH_DISPLAY_DISPLAY_CONTROLLER_H_ 242