1a39693436293a88e98892927c3999459f474541
[platform/framework/web/crosswalk.git] / src / ozone / impl / window_tree_host_delegate_wayland.cc
1 // Copyright 2014 Intel Corporation. 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 "ozone/impl/window_tree_host_delegate_wayland.h"
6
7 #include "ozone/impl/desktop_window_tree_host_wayland.h"
8 #include "ozone/impl/ozone_display.h"
9 #include "ozone/ui/events/event_converter_ozone_wayland.h"
10 #include "ui/events/event_utils.h"
11
12 namespace ozonewayland {
13
14 WindowTreeHostDelegateWayland::WindowTreeHostDelegateWayland()
15     : current_focus_window_(0),
16       handle_event_(true),
17       stop_propogation_(false),
18       current_dispatcher_(NULL),
19       current_capture_(NULL),
20       current_active_window_(NULL),
21       open_windows_(NULL),
22       aura_windows_(NULL) {
23   DCHECK(base::MessagePumpOzone::Current());
24   base::MessagePumpOzone::Current()->AddDispatcherForRootWindow(this);
25   EventConverterOzoneWayland::GetInstance()->SetWindowChangeObserver(this);
26 }
27
28 WindowTreeHostDelegateWayland::~WindowTreeHostDelegateWayland() {
29 }
30
31 void WindowTreeHostDelegateWayland::OnRootWindowCreated(unsigned handle) {
32   open_windows().push_back(handle);
33
34   if (aura_windows_) {
35     aura_windows_->clear();
36     delete aura_windows_;
37     aura_windows_ = NULL;
38   }
39 }
40
41 void WindowTreeHostDelegateWayland::OnRootWindowClosed(unsigned handle) {
42   open_windows().remove(handle);
43   if (open_windows().empty()) {
44     delete open_windows_;
45     open_windows_ = NULL;
46     SetActiveWindow(NULL);
47
48     DCHECK(base::MessagePumpOzone::Current());
49     base::MessagePumpOzone::Current()->RemoveDispatcherForRootWindow(this);
50     EventConverterOzoneWayland::GetInstance()->SetWindowChangeObserver(NULL);
51   }
52
53   if (aura_windows_) {
54     aura_windows_->clear();
55     delete aura_windows_;
56     aura_windows_ = NULL;
57   }
58
59   if (!current_active_window_ || current_active_window_->window_ != handle ||
60         !open_windows_) {
61      return;
62   }
63
64   DCHECK(!current_active_window_->window_parent_);
65   // Set first top level window in the list of open windows as dispatcher.
66   // This is just a guess of the window which would eventually be focussed.
67   // We should set the correct root window as dispatcher in OnWindowFocused.
68   const std::list<gfx::AcceleratedWidget>& windows = open_windows();
69   DesktopWindowTreeHostWayland* rootWindow =
70       DesktopWindowTreeHostWayland::GetHostForAcceleratedWidget(
71           windows.front());
72   SetActiveWindow(rootWindow);
73   rootWindow->HandleNativeWidgetActivationChanged(true);
74 }
75
76 void WindowTreeHostDelegateWayland::SetActiveWindow(
77     DesktopWindowTreeHostWayland* dispatcher) {
78   current_active_window_ = dispatcher;
79   current_dispatcher_ = current_active_window_;
80   if (!current_active_window_)
81     return;
82
83   // Make sure the stacking order is correct. The activated window should be
84   // first one in list of open windows.
85   std::list<gfx::AcceleratedWidget>& windows = open_windows();
86   DCHECK(windows.size());
87   unsigned window_handle = current_active_window_->window_;
88   if (windows.front() != window_handle) {
89     windows.remove(window_handle);
90     windows.insert(open_windows().begin(), window_handle);
91   }
92
93   current_active_window_->Activate();
94 }
95
96 DesktopWindowTreeHostWayland*
97 WindowTreeHostDelegateWayland::GetActiveWindow() const {
98   return current_active_window_;
99 }
100
101 void WindowTreeHostDelegateWayland::SetCapture(
102     DesktopWindowTreeHostWayland* dispatcher) {
103   if (current_capture_)
104     current_capture_->OnCaptureReleased();
105
106   current_capture_ = dispatcher;
107   stop_propogation_ = current_capture_ ? true : false;
108   current_dispatcher_ = current_capture_;
109   if (!current_dispatcher_)
110     current_dispatcher_ = current_active_window_;
111 }
112
113 DesktopWindowTreeHostWayland*
114 WindowTreeHostDelegateWayland::GetCurrentCapture() const {
115   return current_capture_;
116 }
117
118 const std::vector<aura::Window*>&
119 WindowTreeHostDelegateWayland::GetAllOpenWindows() {
120   if (!aura_windows_) {
121     const std::list<gfx::AcceleratedWidget>& windows = open_windows();
122     DCHECK(windows.size());
123     aura_windows_ = new std::vector<aura::Window*>(windows.size());
124     std::transform(
125         windows.begin(), windows.end(), aura_windows_->begin(),
126             DesktopWindowTreeHostWayland::GetContentWindowForAcceleratedWidget);
127   }
128
129   return *aura_windows_;
130 }
131
132 ////////////////////////////////////////////////////////////////////////////////
133 // WindowTreeHostDelegateWayland, Private implementation:
134 void WindowTreeHostDelegateWayland::DispatchMouseEvent(
135          ui::MouseEvent* event) {
136   if (handle_event_)
137     SendEventToProcessor(event);
138   else if (event->type() == ui::ET_MOUSE_PRESSED)
139     SetCapture(NULL);
140
141   // Stop event propogation as this window is acting as event grabber. All
142   // event's we create are "cancelable". If in future we use events that are not
143   // cancelable, then a check for cancelable events needs to be added here.
144   if (stop_propogation_)
145     event->StopPropagation();
146 }
147
148 std::list<gfx::AcceleratedWidget>&
149 WindowTreeHostDelegateWayland::open_windows() {
150   if (!open_windows_)
151     open_windows_ = new std::list<gfx::AcceleratedWidget>();
152
153   return *open_windows_;
154 }
155
156 ui::EventProcessor* WindowTreeHostDelegateWayland::GetEventProcessor() {
157   return current_dispatcher_->delegate_->GetEventProcessor();
158 }
159
160 ////////////////////////////////////////////////////////////////////////////////
161 // WindowTreeHostDelegateWayland, MessagePumpDispatcher implementation:
162 bool WindowTreeHostDelegateWayland::Dispatch(const base::NativeEvent& ne) {
163   ui::EventType type = ui::EventTypeFromNative(ne);
164   DCHECK(current_dispatcher_);
165
166   switch (type) {
167     case ui::ET_TOUCH_MOVED:
168     case ui::ET_TOUCH_PRESSED:
169     case ui::ET_TOUCH_CANCELLED:
170     case ui::ET_TOUCH_RELEASED: {
171       ui::TouchEvent* touchev = static_cast<ui::TouchEvent*>(ne);
172       SendEventToProcessor(touchev);
173       break;
174     }
175     case ui::ET_KEY_PRESSED: {
176       ui::KeyEvent* keydown_event = static_cast<ui::KeyEvent*>(ne);
177       SendEventToProcessor(keydown_event);
178       break;
179     }
180     case ui::ET_KEY_RELEASED: {
181       ui::KeyEvent* keyup_event = static_cast<ui::KeyEvent*>(ne);
182       SendEventToProcessor(keyup_event);
183       break;
184     }
185     case ui::ET_MOUSEWHEEL: {
186       ui::MouseWheelEvent* wheelev = static_cast<ui::MouseWheelEvent*>(ne);
187       DispatchMouseEvent(wheelev);
188       break;
189     }
190     case ui::ET_MOUSE_MOVED:
191     case ui::ET_MOUSE_DRAGGED:
192     case ui::ET_MOUSE_PRESSED:
193     case ui::ET_MOUSE_RELEASED:
194     case ui::ET_MOUSE_ENTERED:
195     case ui::ET_MOUSE_EXITED: {
196       ui::MouseEvent* mouseev = static_cast<ui::MouseEvent*>(ne);
197       DispatchMouseEvent(mouseev);
198       break;
199     }
200     case ui::ET_SCROLL_FLING_START:
201     case ui::ET_SCROLL_FLING_CANCEL:
202     case ui::ET_SCROLL: {
203       ui::ScrollEvent* scrollev = static_cast<ui::ScrollEvent*>(ne);
204       SendEventToProcessor(scrollev);
205       break;
206     }
207     case ui::ET_UMA_DATA:
208       break;
209     case ui::ET_UNKNOWN:
210       break;
211     default:
212       NOTIMPLEMENTED() << "WindowTreeHostDelegateWayland: unknown event type.";
213   }
214   return true;
215 }
216
217 ////////////////////////////////////////////////////////////////////////////////
218 // DesktopWindowTreeHostWayland, WindowChangeObserver implementation:
219 void WindowTreeHostDelegateWayland::OnWindowFocused(unsigned handle) {
220   current_focus_window_ = handle;
221   // Don't dispatch events in case a window has installed itself as capture
222   // window but doesn't have the focus.
223   handle_event_ = current_capture_ ? current_focus_window_ ==
224           current_capture_->GetAcceleratedWidget() : true;
225   if (current_active_window_->window_ == handle)
226     return;
227
228   // A new window should not steal focus in case the current window has a open
229   // popup.
230   if (current_capture_ && current_capture_ != current_active_window_)
231     return;
232
233   DesktopWindowTreeHostWayland* window = NULL;
234   if (handle)
235     window = DesktopWindowTreeHostWayland::GetHostForAcceleratedWidget(handle);
236
237   if (!window || window->window_parent_)
238     return;
239
240   current_active_window_->HandleNativeWidgetActivationChanged(false);
241
242   SetActiveWindow(window);
243   window->HandleNativeWidgetActivationChanged(true);
244 }
245
246 void WindowTreeHostDelegateWayland::OnWindowEnter(unsigned handle) {
247   OnWindowFocused(handle);
248 }
249
250 void WindowTreeHostDelegateWayland::OnWindowLeave(unsigned handle) {
251 }
252
253 void WindowTreeHostDelegateWayland::OnWindowClose(unsigned handle) {
254   // we specially treat grabbed windows in this function, thus the need for
255   // current_capture_ always be a valid pointer.
256   if (!handle || !current_capture_)
257     return;
258   if (current_capture_->window_ != handle)
259     return;
260   DesktopWindowTreeHostWayland* window = NULL;
261   window = DesktopWindowTreeHostWayland::GetHostForAcceleratedWidget(handle);
262   window->OnCaptureReleased();
263   window->Close();
264 }
265
266 void WindowTreeHostDelegateWayland::OnWindowResized(unsigned handle,
267                                                     unsigned width,
268                                                     unsigned height) {
269   DesktopWindowTreeHostWayland* window =
270       DesktopWindowTreeHostWayland::GetHostForAcceleratedWidget(handle);
271   DCHECK(window);
272   window->HandleWindowResize(width, height);
273 }
274
275 }  // namespace ozonewayland