display_controller.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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 "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/gfx/display_observer.h" 20 21namespace aura { 22class Display; 23class RootWindow; 24} 25 26namespace base { 27class Value; 28template <typename T> class JSONValueConverter; 29} 30 31namespace gfx { 32class Display; 33class Insets; 34class Point; 35} 36 37namespace ash { 38namespace internal { 39class DisplayManager; 40class FocusActivationStore; 41class MirrorWindowController; 42class RootWindowController; 43} 44 45// DisplayController owns and maintains RootWindows for each attached 46// display, keeping them in sync with display configuration changes. 47// TODO(oshima): Factor out the layout registration class. 48class ASH_EXPORT DisplayController : public gfx::DisplayObserver { 49 public: 50 class ASH_EXPORT Observer { 51 public: 52 // Invoked when the display configuration change is requested, 53 // but before the change is applied to aura/ash. 54 virtual void OnDisplayConfigurationChanging() {} 55 56 // Invoked when the all display configuration changes 57 // have been applied. 58 virtual void OnDisplayConfigurationChanged() {}; 59 60 protected: 61 virtual ~Observer() {} 62 }; 63 64 DisplayController(); 65 virtual ~DisplayController(); 66 67 void Start(); 68 void Shutdown(); 69 70 // Returns primary display. This is safe to use after ash::Shell is 71 // deleted. 72 static const gfx::Display& GetPrimaryDisplay(); 73 74 // Returns the number of display. This is safe to use after 75 // ash::Shell is deleted. 76 static int GetNumDisplays(); 77 78 // True if the primary display has been initialized. 79 static bool HasPrimaryDisplay(); 80 81 // Initializes primary display. 82 void InitPrimaryDisplay(); 83 84 // Initialize secondary displays. 85 void InitSecondaryDisplays(); 86 87 // Add/Remove observers. 88 void AddObserver(Observer* observer); 89 void RemoveObserver(Observer* observer); 90 91 // Returns the root window for primary display. 92 aura::RootWindow* GetPrimaryRootWindow(); 93 94 // Returns the root window for |display_id|. 95 aura::RootWindow* GetRootWindowForDisplayId(int64 id); 96 97 // Toggle mirror mode. 98 void ToggleMirrorMode(); 99 100 // Swap primary and secondary display. 101 void SwapPrimaryDisplay(); 102 103 // Sets the ID of the primary display. If the display is not connected, it 104 // will switch the primary display when connected. 105 void SetPrimaryDisplayId(int64 id); 106 107 // Sets primary display. This re-assigns the current root 108 // window to given |display|. 109 void SetPrimaryDisplay(const gfx::Display& display); 110 111 // Returns the secondary display. 112 gfx::Display* GetSecondaryDisplay(); 113 114 // Closes all child windows in the all root windows. 115 void CloseChildWindows(); 116 117 // Returns all root windows. In non extended desktop mode, this 118 // returns the primary root window only. 119 std::vector<aura::RootWindow*> GetAllRootWindows(); 120 121 // Returns all oot window controllers. In non extended desktop 122 // mode, this return a RootWindowController for the primary root window only. 123 std::vector<internal::RootWindowController*> GetAllRootWindowControllers(); 124 125 // Gets/Sets/Clears the overscan insets for the specified |display_id|. See 126 // display_manager.h for the details. 127 gfx::Insets GetOverscanInsets(int64 display_id) const; 128 void SetOverscanInsets(int64 display_id, const gfx::Insets& insets_in_dip); 129 130 const DisplayLayout& default_display_layout() const { 131 return default_display_layout_; 132 } 133 void SetDefaultDisplayLayout(const DisplayLayout& layout); 134 135 // Registeres the display layout info for the specified display(s). 136 void RegisterLayoutForDisplayIdPair(int64 id1, 137 int64 id2, 138 const DisplayLayout& layout); 139 // Sets the layout for the current display pair. The |layout| specifies 140 // the locaion of the secondary display relative to the primary. 141 void SetLayoutForCurrentDisplays(const DisplayLayout& layout); 142 143 // Returns the display layout used for current displays. 144 DisplayLayout GetCurrentDisplayLayout(); 145 146 // Returns the current display pair. 147 DisplayIdPair GetCurrentDisplayIdPair() const; 148 149 // Returns the display layout registered for the given display id |pair|. 150 // If no layout is registered, it creatas new layout using 151 // |default_display_layout_|. 152 DisplayLayout GetRegisteredDisplayLayout(const DisplayIdPair& pair); 153 154 // Checks if the mouse pointer is on one of displays, and moves to 155 // the center of the nearest display if it's outside of all displays. 156 void EnsurePointerInDisplays(); 157 158 gfx::Point GetNativeMouseCursorLocation() const; 159 160 // Update the current cursor image that is sutable for the given 161 // |point_in_native|. 162 void UpdateMouseCursor(const gfx::Point& point_in_native); 163 164 // aura::DisplayObserver overrides: 165 virtual void OnDisplayBoundsChanged( 166 const gfx::Display& display) OVERRIDE; 167 virtual void OnDisplayAdded(const gfx::Display& display) OVERRIDE; 168 virtual void OnDisplayRemoved(const gfx::Display& display) OVERRIDE; 169 170 private: 171 friend class internal::DisplayManager; 172 friend class internal::MirrorWindowController; 173 174 // Creates a root window for |display| and stores it in the |root_windows_| 175 // map. 176 aura::RootWindow* AddRootWindowForDisplay(const gfx::Display& display); 177 178 void UpdateDisplayBoundsForLayout(); 179 180 void NotifyDisplayConfigurationChanging(); 181 void NotifyDisplayConfigurationChanged(); 182 183 void SetLayoutForDisplayIdPair(const DisplayIdPair& display_pair, 184 const DisplayLayout& layout); 185 186 void RegisterLayoutForDisplayIdPairInternal( 187 int64 id1, 188 int64 id2, 189 const DisplayLayout& layout, 190 bool override); 191 192 void OnFadeOutForSwapDisplayFinished(); 193 194 // Returns the display layout for the display id pair 195 // with display swapping applied. That is, this returns 196 // flipped layout if the displays are swapped. 197 DisplayLayout ComputeDisplayLayoutForDisplayIdPair( 198 const DisplayIdPair& display_pair); 199 200 void UpdateHostWindowNames(); 201 202 // Creates new layout for display pair from |default_display_layout_|. 203 DisplayLayout CreateDisplayLayout(const DisplayIdPair& display_pair); 204 205 bool in_bootstrap() const { return in_bootstrap_; } 206 207 class DisplayChangeLimiter { 208 public: 209 DisplayChangeLimiter(); 210 211 // Sets how long the throttling should last. 212 void SetThrottleTimeout(int64 throttle_ms); 213 214 bool IsThrottled() const; 215 216 private: 217 // The time when the throttling ends. 218 base::Time throttle_timeout_; 219 220 DISALLOW_COPY_AND_ASSIGN(DisplayChangeLimiter); 221 }; 222 223 // The limiter to throttle how fast a user can 224 // change the display configuration. 225 scoped_ptr<DisplayChangeLimiter> limiter_; 226 227 // The mapping from display ID to its root window. 228 std::map<int64, aura::RootWindow*> root_windows_; 229 230 // The default display layout. 231 DisplayLayout default_display_layout_; 232 233 // Display layout per pair of devices. 234 std::map<DisplayIdPair, DisplayLayout> paired_layouts_; 235 236 ObserverList<Observer> observers_; 237 238 // Store the primary root window temporarily while replacing 239 // display. 240 aura::RootWindow* primary_root_window_for_replace_; 241 242 bool in_bootstrap_; 243 244 scoped_ptr<internal::FocusActivationStore> focus_activation_store_; 245 246 DISALLOW_COPY_AND_ASSIGN(DisplayController); 247}; 248 249} // namespace ash 250 251#endif // ASH_DISPLAY_DISPLAY_CONTROLLER_H_ 252