- add sources.
[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/root_window_observer.h"
20 #include "ui/gfx/display_observer.h"
21 #include "ui/gfx/point.h"
22
23 namespace aura {
24 class Display;
25 class RootWindow;
26 }
27
28 namespace base {
29 class Value;
30 template <typename T> class JSONValueConverter;
31 }
32
33 namespace gfx {
34 class Display;
35 class Insets;
36 }
37
38 namespace ash {
39 namespace internal {
40 class DisplayInfo;
41 class DisplayManager;
42 class FocusActivationStore;
43 class MirrorWindowController;
44 class RootWindowController;
45 class VirtualKeyboardWindowController;
46 }
47
48 // DisplayController owns and maintains RootWindows for each attached
49 // display, keeping them in sync with display configuration changes.
50 class ASH_EXPORT DisplayController : public gfx::DisplayObserver,
51                                      public aura::RootWindowObserver,
52                                      public internal::DisplayManager::Delegate {
53  public:
54   class ASH_EXPORT Observer {
55    public:
56     // Invoked when the display configuration change is requested,
57     // but before the change is applied to aura/ash.
58     virtual void OnDisplayConfigurationChanging() {}
59
60     // Invoked when the all display configuration changes
61     // have been applied.
62     virtual void OnDisplayConfigurationChanged() {};
63
64    protected:
65     virtual ~Observer() {}
66   };
67
68   DisplayController();
69   virtual ~DisplayController();
70
71   void Start();
72   void Shutdown();
73
74   // Returns primary display. This is safe to use after ash::Shell is
75   // deleted.
76   static const gfx::Display& GetPrimaryDisplay();
77
78   // Returns the number of display. This is safe to use after
79   // ash::Shell is deleted.
80   static int GetNumDisplays();
81
82   internal::MirrorWindowController* mirror_window_controller() {
83     return mirror_window_controller_.get();
84   }
85
86   // Initializes primary display.
87   void InitPrimaryDisplay();
88
89   // Initialize secondary displays.
90   void InitSecondaryDisplays();
91
92   // Add/Remove observers.
93   void AddObserver(Observer* observer);
94   void RemoveObserver(Observer* observer);
95
96   // Returns the root window for primary display.
97   aura::Window* GetPrimaryRootWindow();
98
99   // Returns the root window for |display_id|.
100   aura::Window* GetRootWindowForDisplayId(int64 id);
101
102   // Toggle mirror mode.
103   void ToggleMirrorMode();
104
105   // Swap primary and secondary display.
106   void SwapPrimaryDisplay();
107
108   // Sets the ID of the primary display.  If the display is not connected, it
109   // will switch the primary display when connected.
110   void SetPrimaryDisplayId(int64 id);
111
112   // Sets primary display. This re-assigns the current root
113   // window to given |display|.
114   void SetPrimaryDisplay(const gfx::Display& display);
115
116   // Closes all child windows in the all root windows.
117   void CloseChildWindows();
118
119   // Returns all root windows. In non extended desktop mode, this
120   // returns the primary root window only.
121   std::vector<aura::RootWindow*> GetAllRootWindows();
122
123   // Returns all oot window controllers. In non extended desktop
124   // mode, this return a RootWindowController for the primary root window only.
125   std::vector<internal::RootWindowController*> GetAllRootWindowControllers();
126
127   // Gets/Sets/Clears the overscan insets for the specified |display_id|. See
128   // display_manager.h for the details.
129   gfx::Insets GetOverscanInsets(int64 display_id) const;
130   void SetOverscanInsets(int64 display_id, const gfx::Insets& insets_in_dip);
131
132   // Checks if the mouse pointer is on one of displays, and moves to
133   // the center of the nearest display if it's outside of all displays.
134   void EnsurePointerInDisplays();
135
136   // Sets the work area's |insets| to the display assigned to |window|.
137   bool UpdateWorkAreaOfDisplayNearestWindow(const aura::Window* window,
138                                             const gfx::Insets& insets);
139
140   // Returns the display object nearest given |point|.
141   const gfx::Display& GetDisplayNearestPoint(
142       const gfx::Point& point) const;
143
144   // Returns the display object nearest given |window|.
145   const gfx::Display& GetDisplayNearestWindow(
146       const aura::Window* window) const;
147
148   // Returns the display that most closely intersects |match_rect|.
149   const gfx::Display& GetDisplayMatching(
150       const gfx::Rect& match_rect)const;
151
152   // aura::DisplayObserver overrides:
153   virtual void OnDisplayBoundsChanged(
154       const gfx::Display& display) OVERRIDE;
155   virtual void OnDisplayAdded(const gfx::Display& display) OVERRIDE;
156   virtual void OnDisplayRemoved(const gfx::Display& display) OVERRIDE;
157
158   // RootWindowObserver overrides:
159   virtual void OnRootWindowHostResized(const aura::RootWindow* root) OVERRIDE;
160
161   // aura::DisplayManager::Delegate overrides:
162   virtual void CreateOrUpdateNonDesktopDisplay(
163       const internal::DisplayInfo& info) OVERRIDE;
164   virtual void CloseNonDesktopDisplay() OVERRIDE;
165   virtual void PreDisplayConfigurationChange(bool dispay_removed) OVERRIDE;
166   virtual void PostDisplayConfigurationChange() OVERRIDE;
167
168  private:
169   FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, BoundsUpdated);
170   FRIEND_TEST_ALL_PREFIXES(DisplayControllerTest, SecondaryDisplayLayout);
171   friend class internal::DisplayManager;
172   friend class internal::MirrorWindowController;
173
174   // Creates a root window for |display| and stores it in the |root_windows_|
175   // map.
176   aura::RootWindow* AddRootWindowForDisplay(const gfx::Display& display);
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   // The mapping from display ID to its root window.
203   std::map<int64, aura::RootWindow*> root_windows_;
204
205   ObserverList<Observer> observers_;
206
207   // Store the primary root window temporarily while replacing
208   // display.
209   aura::RootWindow* primary_root_window_for_replace_;
210
211   scoped_ptr<internal::FocusActivationStore> focus_activation_store_;
212
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_