display_controller.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 AshWindowTreeHost;
41struct AshWindowTreeHostInitParams;
42class CursorWindowController;
43class DisplayInfo;
44class DisplayManager;
45class FocusActivationStore;
46class MirrorWindowController;
47class RootWindowController;
48class VirtualKeyboardWindowController;
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 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  CursorWindowController* cursor_window_controller() {
85    return cursor_window_controller_.get();
86  }
87
88  MirrorWindowController* mirror_window_controller() {
89    return mirror_window_controller_.get();
90  }
91
92  VirtualKeyboardWindowController* virtual_keyboard_window_controller() {
93    return virtual_keyboard_window_controller_.get();
94  }
95
96  // Create a WindowTreeHost for the primary display. This replaces
97  // |initial_bounds| in |init_params|.
98  void CreatePrimaryHost(const AshWindowTreeHostInitParams& init_params);
99
100  // Initializes all displays.
101  void InitDisplays();
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<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  // gfx::DisplayObserver overrides:
151  virtual void OnDisplayAdded(const gfx::Display& display) OVERRIDE;
152  virtual void OnDisplayRemoved(const gfx::Display& display) OVERRIDE;
153  virtual void OnDisplayMetricsChanged(const gfx::Display& display,
154                                       uint32_t metrics) 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(const DisplayInfo& info)
161      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 DisplayManager;
170  friend class MirrorWindowController;
171
172  // Creates a WindowTreeHost for |display| and stores it in the
173  // |window_tree_hosts_| map.
174  AshWindowTreeHost* AddWindowTreeHostForDisplay(
175      const gfx::Display& display,
176      const AshWindowTreeHostInitParams& params);
177
178  void OnFadeOutForSwapDisplayFinished();
179
180  void UpdateHostWindowNames();
181
182  class DisplayChangeLimiter {
183   public:
184    DisplayChangeLimiter();
185
186    // Sets how long the throttling should last.
187    void SetThrottleTimeout(int64 throttle_ms);
188
189    bool IsThrottled() const;
190
191   private:
192    // The time when the throttling ends.
193    base::Time throttle_timeout_;
194
195    DISALLOW_COPY_AND_ASSIGN(DisplayChangeLimiter);
196  };
197
198  // The limiter to throttle how fast a user can
199  // change the display configuration.
200  scoped_ptr<DisplayChangeLimiter> limiter_;
201
202  typedef std::map<int64, AshWindowTreeHost*> WindowTreeHostMap;
203  // The mapping from display ID to its window tree host.
204  WindowTreeHostMap window_tree_hosts_;
205
206  ObserverList<Observer> observers_;
207
208  // Store the primary window tree host temporarily while replacing
209  // display.
210  AshWindowTreeHost* primary_tree_host_for_replace_;
211
212  scoped_ptr<FocusActivationStore> focus_activation_store_;
213
214  scoped_ptr<CursorWindowController> cursor_window_controller_;
215  scoped_ptr<MirrorWindowController> mirror_window_controller_;
216  scoped_ptr<VirtualKeyboardWindowController>
217      virtual_keyboard_window_controller_;
218
219  // Stores the curent cursor location (in native coordinates) used to
220  // restore the cursor location when display configuration
221  // changed.
222  gfx::Point cursor_location_in_native_coords_for_restore_;
223
224  DISALLOW_COPY_AND_ASSIGN(DisplayController);
225};
226
227}  // namespace ash
228
229#endif  // ASH_DISPLAY_DISPLAY_CONTROLLER_H_
230