UnifiedDesktop: mouse warp support
[chromium-blink-merge.git] / ash / display / display_controller.h
bloba46b8d1d3f48be587a63e6b30051d92f6111ba1f
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.
5 #ifndef ASH_DISPLAY_DISPLAY_CONTROLLER_H_
6 #define ASH_DISPLAY_DISPLAY_CONTROLLER_H_
8 #include <map>
9 #include <vector>
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/geometry/point.h"
25 namespace aura {
26 class Display;
27 class WindowTreeHost;
30 namespace base {
31 class Value;
32 template <typename T> class JSONValueConverter;
35 namespace gfx {
36 class Display;
37 class Insets;
40 namespace ash {
41 class AshWindowTreeHost;
42 struct AshWindowTreeHostInitParams;
43 class CursorWindowController;
44 class DisplayInfo;
45 class DisplayManager;
46 class FocusActivationStore;
47 class MirrorWindowController;
48 class RootWindowController;
50 // DisplayController owns and maintains RootWindows for each attached
51 // display, keeping them in sync with display configuration changes.
52 class 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() {}
62 // Invoked when the display configuration change is requested,
63 // but before the change is applied to aura/ash.
64 virtual void OnDisplayConfigurationChanging() {}
66 // Invoked when the all display configuration changes
67 // have been applied.
68 virtual void OnDisplayConfigurationChanged() {};
70 protected:
71 virtual ~Observer() {}
74 DisplayController();
75 ~DisplayController() override;
77 void Start();
78 void Shutdown();
80 // Returns primary display's ID.
81 // TODO(oshima): Move this out from DisplayController;
82 static int64 GetPrimaryDisplayId();
84 CursorWindowController* cursor_window_controller() {
85 return cursor_window_controller_.get();
88 MirrorWindowController* mirror_window_controller() {
89 return mirror_window_controller_.get();
92 // Create a WindowTreeHost for the primary display. This replaces
93 // |initial_bounds| in |init_params|.
94 void CreatePrimaryHost(const AshWindowTreeHostInitParams& init_params);
96 // Initializes all displays.
97 void InitDisplays();
99 // Add/Remove observers.
100 void AddObserver(Observer* observer);
101 void RemoveObserver(Observer* observer);
103 // Returns the root window for primary display.
104 aura::Window* GetPrimaryRootWindow();
106 // Returns the root window for |display_id|.
107 aura::Window* GetRootWindowForDisplayId(int64 id);
109 // Returns AshWTH for given display |id|. Call results in CHECK failure
110 // if the WTH does not exist.
111 AshWindowTreeHost* GetAshWindowTreeHostForDisplayId(int64 id);
113 // Toggle mirror mode.
114 void ToggleMirrorMode();
116 // Swap primary and secondary display.
117 void SwapPrimaryDisplay();
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);
123 // Sets primary display. This re-assigns the current root
124 // window to given |display|.
125 void SetPrimaryDisplay(const gfx::Display& display);
127 // Closes all child windows in the all root windows.
128 void CloseChildWindows();
130 // Returns all root windows. In non extended desktop mode, this
131 // returns the primary root window only.
132 aura::Window::Windows GetAllRootWindows();
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();
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);
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 UpdateMouseLocationAfterDisplayChange();
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 void OnDisplayAdded(const gfx::Display& display) override;
152 void OnDisplayRemoved(const gfx::Display& display) override;
153 void OnDisplayMetricsChanged(const gfx::Display& display,
154 uint32_t metrics) override;
156 // aura::WindowTreeHostObserver overrides:
157 void OnHostResized(const aura::WindowTreeHost* host) override;
159 // aura::DisplayManager::Delegate overrides:
160 void CreateOrUpdateMirroringDisplay(
161 const DisplayInfoList& info_list) override;
162 void CloseMirroringDisplay() override;
163 void PreDisplayConfigurationChange(bool clear_focus) override;
164 void PostDisplayConfigurationChange() override;
166 private:
167 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, BoundsUpdated);
168 FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, SecondaryDisplayLayout);
169 friend class DisplayManager;
170 friend class MirrorWindowController;
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);
178 void OnFadeOutForSwapDisplayFinished();
180 void SetMirrorModeAfterAnimation(bool mirror);
182 // Delete the AsWindowTreeHost. This does not remove the entry from
183 // |window_tree_hosts_|. Caller has to explicitly remove it.
184 void DeleteHost(AshWindowTreeHost* host_to_delete);
186 class DisplayChangeLimiter {
187 public:
188 DisplayChangeLimiter();
190 // Sets how long the throttling should last.
191 void SetThrottleTimeout(int64 throttle_ms);
193 bool IsThrottled() const;
195 private:
196 // The time when the throttling ends.
197 base::Time throttle_timeout_;
199 DISALLOW_COPY_AND_ASSIGN(DisplayChangeLimiter);
202 // The limiter to throttle how fast a user can
203 // change the display configuration.
204 scoped_ptr<DisplayChangeLimiter> limiter_;
206 typedef std::map<int64, AshWindowTreeHost*> WindowTreeHostMap;
207 // The mapping from display ID to its window tree host.
208 WindowTreeHostMap window_tree_hosts_;
210 ObserverList<Observer> observers_;
212 // Store the primary window tree host temporarily while replacing
213 // display.
214 AshWindowTreeHost* primary_tree_host_for_replace_;
216 scoped_ptr<FocusActivationStore> focus_activation_store_;
218 scoped_ptr<CursorWindowController> cursor_window_controller_;
219 scoped_ptr<MirrorWindowController> mirror_window_controller_;
221 // Stores the current cursor location (in native coordinates and screen
222 // coordinates respectively). The locations are used to restore the cursor
223 // location when the display configuration changes and to determine whether
224 // the mouse should be moved after a display configuration change.
225 gfx::Point cursor_location_in_native_coords_for_restore_;
226 gfx::Point cursor_location_in_screen_coords_for_restore_;
228 // Stores the cursor's display. The id is used to determine whether the mouse
229 // should be moved after a display configuration change.
230 int64 cursor_display_id_for_restore_;
232 base::WeakPtrFactory<DisplayController> weak_ptr_factory_;
234 DISALLOW_COPY_AND_ASSIGN(DisplayController);
237 } // namespace ash
239 #endif // ASH_DISPLAY_DISPLAY_CONTROLLER_H_