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