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