Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ash / display / screen_ash.cc
1 // Copyright 2014 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 #include "ash/display/screen_ash.h"
6
7 #include "ash/display/display_controller.h"
8 #include "ash/display/display_manager.h"
9 #include "ash/root_window_controller.h"
10 #include "ash/root_window_settings.h"
11 #include "ash/shelf/shelf_layout_manager.h"
12 #include "ash/shelf/shelf_widget.h"
13 #include "ash/shell.h"
14 #include "ash/wm/coordinate_conversion.h"
15 #include "base/logging.h"
16 #include "ui/aura/client/screen_position_client.h"
17 #include "ui/aura/env.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/gfx/display.h"
20 #include "ui/gfx/screen.h"
21
22 namespace ash {
23
24 namespace {
25
26 DisplayManager* GetDisplayManager() {
27   return Shell::GetInstance()->display_manager();
28 }
29
30 gfx::Display FindDisplayNearestPoint(const std::vector<gfx::Display>& displays,
31                                      const gfx::Point& point) {
32   int min_distance = INT_MAX;
33   const gfx::Display* nearest_display = NULL;
34   for (std::vector<gfx::Display>::const_iterator iter = displays.begin();
35        iter != displays.end(); ++iter) {
36     const gfx::Display& display = *iter;
37     int distance = display.bounds().ManhattanDistanceToPoint(point);
38     if (distance < min_distance) {
39       min_distance = distance;
40       nearest_display = &display;
41     }
42   }
43   // There should always be at least one display that is less than INT_MAX away.
44   DCHECK(nearest_display);
45   return *nearest_display;
46 }
47
48 const gfx::Display* FindDisplayMatching(
49     const std::vector<gfx::Display>& displays,
50     const gfx::Rect& match_rect) {
51   int max_area = 0;
52   const gfx::Display* matching = NULL;
53   for (std::vector<gfx::Display>::const_iterator iter = displays.begin();
54        iter != displays.end(); ++iter) {
55     const gfx::Display& display = *iter;
56     gfx::Rect intersect = gfx::IntersectRects(display.bounds(), match_rect);
57     int area = intersect.width() * intersect.height();
58     if (area > max_area) {
59       max_area = area;
60       matching = &display;
61     }
62   }
63   return matching;
64 }
65
66 class ScreenForShutdown : public gfx::Screen {
67  public:
68   explicit ScreenForShutdown(ScreenAsh* screen_ash)
69       : display_list_(screen_ash->GetAllDisplays()),
70         primary_display_(screen_ash->GetPrimaryDisplay()) {
71   }
72
73   // gfx::Screen overrides:
74   gfx::Point GetCursorScreenPoint() override { return gfx::Point(); }
75   gfx::NativeWindow GetWindowUnderCursor() override { return NULL; }
76   gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override {
77     return NULL;
78   }
79   int GetNumDisplays() const override { return display_list_.size(); }
80   std::vector<gfx::Display> GetAllDisplays() const override {
81     return display_list_;
82   }
83   gfx::Display GetDisplayNearestWindow(gfx::NativeView view) const override {
84     return primary_display_;
85   }
86   gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override {
87     return FindDisplayNearestPoint(display_list_, point);
88   }
89   gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override {
90     const gfx::Display* matching =
91         FindDisplayMatching(display_list_, match_rect);
92     // Fallback to the primary display if there is no matching display.
93     return matching ? *matching : GetPrimaryDisplay();
94   }
95   gfx::Display GetPrimaryDisplay() const override { return primary_display_; }
96   void AddObserver(gfx::DisplayObserver* observer) override {
97     NOTREACHED() << "Observer should not be added during shutdown";
98   }
99   void RemoveObserver(gfx::DisplayObserver* observer) override {}
100
101  private:
102   const std::vector<gfx::Display> display_list_;
103   const gfx::Display primary_display_;
104
105   DISALLOW_COPY_AND_ASSIGN(ScreenForShutdown);
106 };
107
108 }  // namespace
109
110 ScreenAsh::ScreenAsh() {
111 }
112
113 ScreenAsh::~ScreenAsh() {
114 }
115
116 // static
117 gfx::Display ScreenAsh::FindDisplayContainingPoint(const gfx::Point& point) {
118   return GetDisplayManager()->FindDisplayContainingPoint(point);
119 }
120
121 // static
122 gfx::Rect ScreenAsh::GetMaximizedWindowBoundsInParent(aura::Window* window) {
123   if (GetRootWindowController(window->GetRootWindow())->shelf())
124     return GetDisplayWorkAreaBoundsInParent(window);
125   else
126     return GetDisplayBoundsInParent(window);
127 }
128
129 // static
130 gfx::Rect ScreenAsh::GetDisplayBoundsInParent(aura::Window* window) {
131   return ConvertRectFromScreen(
132       window->parent(),
133       Shell::GetScreen()->GetDisplayNearestWindow(window).bounds());
134 }
135
136 // static
137 gfx::Rect ScreenAsh::GetDisplayWorkAreaBoundsInParent(aura::Window* window) {
138   return ConvertRectFromScreen(
139       window->parent(),
140       Shell::GetScreen()->GetDisplayNearestWindow(window).work_area());
141 }
142
143 // static
144 gfx::Rect ScreenAsh::ConvertRectToScreen(aura::Window* window,
145                                          const gfx::Rect& rect) {
146   gfx::Point point = rect.origin();
147   aura::client::GetScreenPositionClient(window->GetRootWindow())->
148       ConvertPointToScreen(window, &point);
149   return gfx::Rect(point, rect.size());
150 }
151
152 // static
153 gfx::Rect ScreenAsh::ConvertRectFromScreen(aura::Window* window,
154                                            const gfx::Rect& rect) {
155   gfx::Point point = rect.origin();
156   aura::client::GetScreenPositionClient(window->GetRootWindow())->
157       ConvertPointFromScreen(window, &point);
158   return gfx::Rect(point, rect.size());
159 }
160
161 // static
162 const gfx::Display& ScreenAsh::GetSecondaryDisplay() {
163   DisplayManager* display_manager = GetDisplayManager();
164   CHECK_EQ(2U, display_manager->GetNumDisplays());
165   return display_manager->GetDisplayAt(0).id() ==
166       Shell::GetScreen()->GetPrimaryDisplay().id() ?
167       display_manager->GetDisplayAt(1) : display_manager->GetDisplayAt(0);
168 }
169
170 // static
171 const gfx::Display& ScreenAsh::GetDisplayForId(int64 display_id) {
172   return GetDisplayManager()->GetDisplayForId(display_id);
173 }
174
175 void ScreenAsh::NotifyMetricsChanged(const gfx::Display& display,
176                                      uint32_t metrics) {
177   FOR_EACH_OBSERVER(gfx::DisplayObserver,
178                     observers_,
179                     OnDisplayMetricsChanged(display, metrics));
180 }
181
182 void ScreenAsh::NotifyDisplayAdded(const gfx::Display& display) {
183   FOR_EACH_OBSERVER(gfx::DisplayObserver, observers_, OnDisplayAdded(display));
184 }
185
186 void ScreenAsh::NotifyDisplayRemoved(const gfx::Display& display) {
187   FOR_EACH_OBSERVER(
188       gfx::DisplayObserver, observers_, OnDisplayRemoved(display));
189 }
190
191 gfx::Point ScreenAsh::GetCursorScreenPoint() {
192   return aura::Env::GetInstance()->last_mouse_location();
193 }
194
195 gfx::NativeWindow ScreenAsh::GetWindowUnderCursor() {
196   return GetWindowAtScreenPoint(Shell::GetScreen()->GetCursorScreenPoint());
197 }
198
199 gfx::NativeWindow ScreenAsh::GetWindowAtScreenPoint(const gfx::Point& point) {
200   return wm::GetRootWindowAt(point)->GetTopWindowContainingPoint(point);
201 }
202
203 int ScreenAsh::GetNumDisplays() const {
204   return GetDisplayManager()->GetNumDisplays();
205 }
206
207 std::vector<gfx::Display> ScreenAsh::GetAllDisplays() const {
208   return GetDisplayManager()->displays();
209 }
210
211 gfx::Display ScreenAsh::GetDisplayNearestWindow(gfx::NativeView window) const {
212   if (!window)
213     return GetPrimaryDisplay();
214   const aura::Window* root_window = window->GetRootWindow();
215   if (!root_window)
216     return GetPrimaryDisplay();
217   const RootWindowSettings* rws = GetRootWindowSettings(root_window);
218   int64 id = rws->display_id;
219   // if id is |kInvaildDisplayID|, it's being deleted.
220   DCHECK(id != gfx::Display::kInvalidDisplayID);
221   if (id == gfx::Display::kInvalidDisplayID)
222     return GetPrimaryDisplay();
223
224   DisplayManager* display_manager = GetDisplayManager();
225   // RootWindow needs Display to determine its device scale factor
226   // for non desktop display.
227   if (display_manager->non_desktop_display().id() == id)
228     return display_manager->non_desktop_display();
229   return display_manager->GetDisplayForId(id);
230 }
231
232 gfx::Display ScreenAsh::GetDisplayNearestPoint(const gfx::Point& point) const {
233   const gfx::Display& display =
234       GetDisplayManager()->FindDisplayContainingPoint(point);
235   if (display.is_valid())
236     return display;
237   // Fallback to the display that has the shortest Manhattan distance from
238   // the |point|. This is correct in the only areas that matter, namely in the
239   // corners between the physical screens.
240   return FindDisplayNearestPoint(GetDisplayManager()->displays(), point);
241 }
242
243 gfx::Display ScreenAsh::GetDisplayMatching(const gfx::Rect& match_rect) const {
244   if (match_rect.IsEmpty())
245     return GetDisplayNearestPoint(match_rect.origin());
246   const gfx::Display* matching =
247       FindDisplayMatching(GetDisplayManager()->displays(), match_rect);
248   // Fallback to the primary display if there is no matching display.
249   return matching ? *matching : GetPrimaryDisplay();
250 }
251
252 gfx::Display ScreenAsh::GetPrimaryDisplay() const {
253   return GetDisplayManager()->GetDisplayForId(
254       DisplayController::GetPrimaryDisplayId());
255 }
256
257 void ScreenAsh::AddObserver(gfx::DisplayObserver* observer) {
258   observers_.AddObserver(observer);
259 }
260
261 void ScreenAsh::RemoveObserver(gfx::DisplayObserver* observer) {
262   observers_.RemoveObserver(observer);
263 }
264
265 gfx::Screen* ScreenAsh::CloneForShutdown() {
266   return new ScreenForShutdown(this);
267 }
268
269 }  // namespace ash