display_controller.h revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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_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" 23 24namespace aura { 25class Display; 26class WindowTreeHost; 27} 28 29namespace base { 30class Value; 31template <typename T> class JSONValueConverter; 32} 33 34namespace gfx { 35class Display; 36class Insets; 37} 38 39namespace ash { 40class CursorWindowController; 41class DisplayInfo; 42class DisplayManager; 43class FocusActivationStore; 44class MirrorWindowController; 45class RootWindowController; 46class VirtualKeyboardWindowController; 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::WindowTreeHostObserver, 52 public DisplayManager::Delegate { 53 public: 54 class ASH_EXPORT Observer { 55 public: 56 // Invoked only once after all displays are initialized 57 // after startup. 58 virtual void OnDisplaysInitialized() {} 59 60 // Invoked when the display configuration change is requested, 61 // but before the change is applied to aura/ash. 62 virtual void OnDisplayConfigurationChanging() {} 63 64 // Invoked when the all display configuration changes 65 // have been applied. 66 virtual void OnDisplayConfigurationChanged() {}; 67 68 protected: 69 virtual ~Observer() {} 70 }; 71 72 DisplayController(); 73 virtual ~DisplayController(); 74 75 void Start(); 76 void Shutdown(); 77 78 // Returns primary display's ID. 79 // TODO(oshima): Move this out from DisplayController; 80 static int64 GetPrimaryDisplayId(); 81 82 CursorWindowController* cursor_window_controller() { 83 return cursor_window_controller_.get(); 84 } 85 86 MirrorWindowController* mirror_window_controller() { 87 return mirror_window_controller_.get(); 88 } 89 90 VirtualKeyboardWindowController* virtual_keyboard_window_controller() { 91 return virtual_keyboard_window_controller_.get(); 92 } 93 94 // Initializes primary display. 95 void InitPrimaryDisplay(); 96 97 // Initialize secondary displays. 98 void InitSecondaryDisplays(); 99 100 // Add/Remove observers. 101 void AddObserver(Observer* observer); 102 void RemoveObserver(Observer* observer); 103 104 // Returns the root window for primary display. 105 aura::Window* GetPrimaryRootWindow(); 106 107 // Returns the root window for |display_id|. 108 aura::Window* GetRootWindowForDisplayId(int64 id); 109 110 // Toggle mirror mode. 111 void ToggleMirrorMode(); 112 113 // Swap primary and secondary display. 114 void SwapPrimaryDisplay(); 115 116 // Sets the ID of the primary display. If the display is not connected, it 117 // will switch the primary display when connected. 118 void SetPrimaryDisplayId(int64 id); 119 120 // Sets primary display. This re-assigns the current root 121 // window to given |display|. 122 void SetPrimaryDisplay(const gfx::Display& display); 123 124 // Closes all child windows in the all root windows. 125 void CloseChildWindows(); 126 127 // Returns all root windows. In non extended desktop mode, this 128 // returns the primary root window only. 129 aura::Window::Windows GetAllRootWindows(); 130 131 // Returns all oot window controllers. In non extended desktop 132 // mode, this return a RootWindowController for the primary root window only. 133 std::vector<RootWindowController*> GetAllRootWindowControllers(); 134 135 // Gets/Sets/Clears the overscan insets for the specified |display_id|. See 136 // display_manager.h for the details. 137 gfx::Insets GetOverscanInsets(int64 display_id) const; 138 void SetOverscanInsets(int64 display_id, const gfx::Insets& insets_in_dip); 139 140 // Checks if the mouse pointer is on one of displays, and moves to 141 // the center of the nearest display if it's outside of all displays. 142 void EnsurePointerInDisplays(); 143 144 // Sets the work area's |insets| to the display assigned to |window|. 145 bool UpdateWorkAreaOfDisplayNearestWindow(const aura::Window* window, 146 const gfx::Insets& insets); 147 // aura::DisplayObserver overrides: 148 virtual void OnDisplayBoundsChanged( 149 const gfx::Display& display) OVERRIDE; 150 virtual void OnDisplayAdded(const gfx::Display& display) OVERRIDE; 151 virtual void OnDisplayRemoved(const gfx::Display& display) OVERRIDE; 152 153 // aura::WindowTreeHostObserver overrides: 154 virtual void OnHostResized(const aura::WindowTreeHost* host) OVERRIDE; 155 156 // aura::DisplayManager::Delegate overrides: 157 virtual void CreateOrUpdateNonDesktopDisplay(const DisplayInfo& info) 158 OVERRIDE; 159 virtual void CloseNonDesktopDisplay() OVERRIDE; 160 virtual void PreDisplayConfigurationChange(bool clear_focus) OVERRIDE; 161 virtual void PostDisplayConfigurationChange() OVERRIDE; 162 163 private: 164 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, BoundsUpdated); 165 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, SecondaryDisplayLayout); 166 friend class DisplayManager; 167 friend class MirrorWindowController; 168 169 // Creates a WindowTreeHost for |display| and stores it in the |root_windows_| 170 // map. 171 aura::WindowTreeHost* AddWindowTreeHostForDisplay( 172 const gfx::Display& display); 173 174 void OnFadeOutForSwapDisplayFinished(); 175 176 void UpdateHostWindowNames(); 177 178 class DisplayChangeLimiter { 179 public: 180 DisplayChangeLimiter(); 181 182 // Sets how long the throttling should last. 183 void SetThrottleTimeout(int64 throttle_ms); 184 185 bool IsThrottled() const; 186 187 private: 188 // The time when the throttling ends. 189 base::Time throttle_timeout_; 190 191 DISALLOW_COPY_AND_ASSIGN(DisplayChangeLimiter); 192 }; 193 194 // The limiter to throttle how fast a user can 195 // change the display configuration. 196 scoped_ptr<DisplayChangeLimiter> limiter_; 197 198 // The mapping from display ID to its root window. 199 std::map<int64, aura::Window*> root_windows_; 200 201 ObserverList<Observer> observers_; 202 203 // Store the primary root window temporarily while replacing 204 // display. 205 aura::Window* primary_root_window_for_replace_; 206 207 scoped_ptr<FocusActivationStore> focus_activation_store_; 208 209 scoped_ptr<CursorWindowController> cursor_window_controller_; 210 scoped_ptr<MirrorWindowController> mirror_window_controller_; 211 scoped_ptr<VirtualKeyboardWindowController> 212 virtual_keyboard_window_controller_; 213 214 // Stores the curent cursor location (in native coordinates) used to 215 // restore the cursor location when display configuration 216 // changed. 217 gfx::Point cursor_location_in_native_coords_for_restore_; 218 219 DISALLOW_COPY_AND_ASSIGN(DisplayController); 220}; 221 222} // namespace ash 223 224#endif // ASH_DISPLAY_DISPLAY_CONTROLLER_H_ 225