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