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