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