display_controller.h revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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 24namespace aura { 25class Display; 26class RootWindow; 27} 28 29namespace base { 30class Value; 31template <typename T> class JSONValueConverter; 32} 33 34namespace gfx { 35class Display; 36class Insets; 37} 38 39namespace ash { 40namespace internal { 41class DisplayInfo; 42class DisplayManager; 43class FocusActivationStore; 44class MirrorWindowController; 45class RootWindowController; 46} 47 48// DisplayController owns and maintains RootWindows for each attached 49// display, keeping them in sync with display configuration changes. 50class 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 // Checks if the mouse pointer is on one of displays, and moves to 137 // the center of the nearest display if it's outside of all displays. 138 void EnsurePointerInDisplays(); 139 140 // Sets the work area's |insets| to the display assigned to |window|. 141 bool UpdateWorkAreaOfDisplayNearestWindow(const aura::Window* window, 142 const gfx::Insets& insets); 143 144 // Returns the display object nearest given |point|. 145 const gfx::Display& GetDisplayNearestPoint( 146 const gfx::Point& point) const; 147 148 // Returns the display object nearest given |window|. 149 const gfx::Display& GetDisplayNearestWindow( 150 const aura::Window* window) const; 151 152 // Returns the display that most closely intersects |match_rect|. 153 const gfx::Display& GetDisplayMatching( 154 const gfx::Rect& match_rect)const; 155 156 // aura::DisplayObserver overrides: 157 virtual void OnDisplayBoundsChanged( 158 const gfx::Display& display) OVERRIDE; 159 virtual void OnDisplayAdded(const gfx::Display& display) OVERRIDE; 160 virtual void OnDisplayRemoved(const gfx::Display& display) OVERRIDE; 161 162 // RootWindowObserver overrides: 163 virtual void OnRootWindowHostResized(const aura::RootWindow* root) OVERRIDE; 164 165 // aura::DisplayManager::Delegate overrides: 166 virtual void CreateOrUpdateMirrorWindow( 167 const internal::DisplayInfo& info) OVERRIDE; 168 virtual void CloseMirrorWindow() OVERRIDE; 169 virtual void PreDisplayConfigurationChange(bool dispay_removed) OVERRIDE; 170 virtual void PostDisplayConfigurationChange() OVERRIDE; 171 172 private: 173 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, BoundsUpdated); 174 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, SecondaryDisplayLayout); 175 friend class internal::DisplayManager; 176 friend class internal::MirrorWindowController; 177 178 // Creates a root window for |display| and stores it in the |root_windows_| 179 // map. 180 aura::RootWindow* AddRootWindowForDisplay(const gfx::Display& display); 181 182 void UpdateDisplayBoundsForLayout(); 183 184 void SetLayoutForDisplayIdPair(const DisplayIdPair& display_pair, 185 const DisplayLayout& layout); 186 187 void OnFadeOutForSwapDisplayFinished(); 188 189 void UpdateHostWindowNames(); 190 191 class DisplayChangeLimiter { 192 public: 193 DisplayChangeLimiter(); 194 195 // Sets how long the throttling should last. 196 void SetThrottleTimeout(int64 throttle_ms); 197 198 bool IsThrottled() const; 199 200 private: 201 // The time when the throttling ends. 202 base::Time throttle_timeout_; 203 204 DISALLOW_COPY_AND_ASSIGN(DisplayChangeLimiter); 205 }; 206 207 // The limiter to throttle how fast a user can 208 // change the display configuration. 209 scoped_ptr<DisplayChangeLimiter> limiter_; 210 211 // The mapping from display ID to its root window. 212 std::map<int64, aura::RootWindow*> root_windows_; 213 214 ObserverList<Observer> observers_; 215 216 // Store the primary root window temporarily while replacing 217 // display. 218 aura::RootWindow* primary_root_window_for_replace_; 219 220 scoped_ptr<internal::FocusActivationStore> focus_activation_store_; 221 222 223 scoped_ptr<internal::MirrorWindowController> mirror_window_controller_; 224 225 // Stores the curent cursor location (in native coordinates) used to 226 // restore the cursor location when display configuration 227 // changed. 228 gfx::Point cursor_location_in_native_coords_for_restore_; 229 230 DISALLOW_COPY_AND_ASSIGN(DisplayController); 231}; 232 233} // namespace ash 234 235#endif // ASH_DISPLAY_DISPLAY_CONTROLLER_H_ 236