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.
5 #include "ozone/ui/desktop_aura/window_tree_host_delegate_wayland.h"
9 #include "ozone/ui/desktop_aura/desktop_window_tree_host_wayland.h"
10 #include "ozone/ui/events/event_factory_ozone_wayland.h"
11 #include "ui/events/event_utils.h"
12 #include "ui/events/platform/platform_event_source.h"
16 WindowTreeHostDelegateWayland::WindowTreeHostDelegateWayland()
17 : current_focus_window_(0),
19 stop_propogation_(false),
20 current_dispatcher_(NULL),
21 current_capture_(NULL),
22 current_active_window_(NULL),
25 if (ui::PlatformEventSource::GetInstance())
26 ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this);
27 ui::EventFactoryOzoneWayland::GetInstance()->SetWindowChangeObserver(this);
30 WindowTreeHostDelegateWayland::~WindowTreeHostDelegateWayland() {
33 void WindowTreeHostDelegateWayland::OnRootWindowCreated(unsigned handle) {
34 open_windows().push_back(handle);
37 aura_windows_->clear();
43 void WindowTreeHostDelegateWayland::OnRootWindowClosed(unsigned handle) {
44 open_windows().remove(handle);
45 if (open_windows().empty()) {
48 SetActiveWindow(NULL);
50 ui::PlatformEventSource* event_source =
51 ui::PlatformEventSource::GetInstance();
53 event_source->RemovePlatformEventDispatcher(this);
54 ui::EventFactoryOzoneWayland::GetInstance()->SetWindowChangeObserver(NULL);
58 aura_windows_->clear();
63 if (!current_active_window_ ||
64 GetWindowHandle(current_active_window_->window_) != handle ||
69 DCHECK(!current_active_window_->window_parent_);
70 // Set first top level window in the list of open windows as dispatcher.
71 // This is just a guess of the window which would eventually be focussed.
72 // We should set the correct root window as dispatcher in OnWindowFocused.
73 const std::list<unsigned>& windows = open_windows();
74 DesktopWindowTreeHostWayland* rootWindow =
75 DesktopWindowTreeHostWayland::GetHostForAcceleratedWidget(
77 SetActiveWindow(rootWindow);
78 rootWindow->HandleNativeWidgetActivationChanged(true);
81 void WindowTreeHostDelegateWayland::SetActiveWindow(
82 DesktopWindowTreeHostWayland* dispatcher) {
83 current_active_window_ = dispatcher;
84 current_dispatcher_ = current_active_window_;
85 if (!current_active_window_)
88 // Make sure the stacking order is correct. The activated window should be
89 // first one in list of open windows.
90 std::list<unsigned>& windows = open_windows();
91 DCHECK(windows.size());
92 unsigned window_handle = current_active_window_->window_;
93 if (windows.front() != window_handle) {
94 windows.remove(window_handle);
95 windows.insert(windows.begin(), window_handle);
98 current_active_window_->Activate();
101 DesktopWindowTreeHostWayland*
102 WindowTreeHostDelegateWayland::GetActiveWindow() const {
103 return current_active_window_;
106 void WindowTreeHostDelegateWayland::SetCapture(
107 DesktopWindowTreeHostWayland* dispatcher) {
108 if (current_capture_)
109 current_capture_->OnCaptureReleased();
111 current_capture_ = dispatcher;
112 stop_propogation_ = current_capture_ ? true : false;
113 current_dispatcher_ = current_capture_;
114 if (!current_dispatcher_)
115 current_dispatcher_ = current_active_window_;
118 DesktopWindowTreeHostWayland*
119 WindowTreeHostDelegateWayland::GetCurrentCapture() const {
120 return current_capture_;
123 const std::vector<aura::Window*>&
124 WindowTreeHostDelegateWayland::GetAllOpenWindows() {
125 if (!aura_windows_) {
126 const std::list<unsigned>& windows = open_windows();
127 DCHECK(windows.size());
128 aura_windows_ = new std::vector<aura::Window*>(windows.size());
130 windows.begin(), windows.end(), aura_windows_->begin(),
131 DesktopWindowTreeHostWayland::GetContentWindowForAcceleratedWidget);
134 return *aura_windows_;
137 ////////////////////////////////////////////////////////////////////////////////
138 // WindowTreeHostDelegateWayland, Private implementation:
139 void WindowTreeHostDelegateWayland::DispatchMouseEvent(
140 ui::MouseEvent* event) {
142 SendEventToProcessor(event);
143 else if (event->type() == ui::ET_MOUSE_PRESSED)
146 // Stop event propogation as this window is acting as event grabber. All
147 // event's we create are "cancelable". If in future we use events that are not
148 // cancelable, then a check for cancelable events needs to be added here.
149 if (stop_propogation_)
150 event->StopPropagation();
154 WindowTreeHostDelegateWayland::open_windows() {
156 open_windows_ = new std::list<unsigned>();
158 return *open_windows_;
162 WindowTreeHostDelegateWayland::GetWindowHandle(gfx::AcceleratedWidget widget) {
163 return static_cast<unsigned>(widget);
166 ui::EventProcessor* WindowTreeHostDelegateWayland::GetEventProcessor() {
167 return current_dispatcher_->dispatcher();
170 ////////////////////////////////////////////////////////////////////////////////
171 // WindowTreeHostDelegateWayland, ui::PlatformEventDispatcher implementation:
172 bool WindowTreeHostDelegateWayland::CanDispatchEvent(
173 const ui::PlatformEvent& ne) {
178 uint32_t WindowTreeHostDelegateWayland::DispatchEvent(
179 const ui::PlatformEvent& ne) {
180 ui::EventType type = ui::EventTypeFromNative(ne);
181 DCHECK(current_dispatcher_);
184 case ui::ET_TOUCH_MOVED:
185 case ui::ET_TOUCH_PRESSED:
186 case ui::ET_TOUCH_CANCELLED:
187 case ui::ET_TOUCH_RELEASED: {
188 ui::TouchEvent* touchev = static_cast<ui::TouchEvent*>(ne);
189 SendEventToProcessor(touchev);
192 case ui::ET_KEY_PRESSED: {
193 ui::KeyEvent* keydown_event = static_cast<ui::KeyEvent*>(ne);
194 SendEventToProcessor(keydown_event);
197 case ui::ET_KEY_RELEASED: {
198 ui::KeyEvent* keyup_event = static_cast<ui::KeyEvent*>(ne);
199 SendEventToProcessor(keyup_event);
202 case ui::ET_MOUSEWHEEL: {
203 ui::MouseWheelEvent* wheelev = static_cast<ui::MouseWheelEvent*>(ne);
204 DispatchMouseEvent(wheelev);
207 case ui::ET_MOUSE_MOVED:
208 case ui::ET_MOUSE_DRAGGED:
209 case ui::ET_MOUSE_PRESSED:
210 case ui::ET_MOUSE_RELEASED:
211 case ui::ET_MOUSE_ENTERED:
212 case ui::ET_MOUSE_EXITED: {
213 ui::MouseEvent* mouseev = static_cast<ui::MouseEvent*>(ne);
214 DispatchMouseEvent(mouseev);
217 case ui::ET_SCROLL_FLING_START:
218 case ui::ET_SCROLL_FLING_CANCEL:
219 case ui::ET_SCROLL: {
220 ui::ScrollEvent* scrollev = static_cast<ui::ScrollEvent*>(ne);
221 SendEventToProcessor(scrollev);
224 case ui::ET_UMA_DATA:
229 NOTIMPLEMENTED() << "WindowTreeHostDelegateWayland: unknown event type.";
231 return ui::POST_DISPATCH_STOP_PROPAGATION;
234 ////////////////////////////////////////////////////////////////////////////////
235 // DesktopWindowTreeHostWayland, WindowChangeObserver implementation:
236 void WindowTreeHostDelegateWayland::OnWindowFocused(unsigned handle) {
237 current_focus_window_ = handle;
238 // Don't dispatch events in case a window has installed itself as capture
239 // window but doesn't have the focus.
240 handle_event_ = current_capture_ ? current_focus_window_ ==
241 GetWindowHandle(current_capture_->GetAcceleratedWidget()) : true;
242 if (GetWindowHandle(current_active_window_->window_) == handle)
245 // A new window should not steal focus in case the current window has a open
247 if (current_capture_ && current_capture_ != current_active_window_)
250 DesktopWindowTreeHostWayland* window = NULL;
252 window = DesktopWindowTreeHostWayland::GetHostForAcceleratedWidget(handle);
254 if (!window || window->window_parent_)
257 current_active_window_->HandleNativeWidgetActivationChanged(false);
259 SetActiveWindow(window);
260 window->HandleNativeWidgetActivationChanged(true);
263 void WindowTreeHostDelegateWayland::OnWindowEnter(unsigned handle) {
264 OnWindowFocused(handle);
267 void WindowTreeHostDelegateWayland::OnWindowLeave(unsigned handle) {
270 void WindowTreeHostDelegateWayland::OnWindowClose(unsigned handle) {
271 // we specially treat grabbed windows in this function, thus the need for
272 // current_capture_ always be a valid pointer.
273 if (!handle || !current_capture_)
275 if (GetWindowHandle(current_capture_->window_) != handle)
277 DesktopWindowTreeHostWayland* window = NULL;
278 window = DesktopWindowTreeHostWayland::GetHostForAcceleratedWidget(handle);
279 window->OnCaptureReleased();
283 void WindowTreeHostDelegateWayland::OnWindowResized(unsigned handle,
286 DesktopWindowTreeHostWayland* window =
287 DesktopWindowTreeHostWayland::GetHostForAcceleratedWidget(handle);
289 window->HandleWindowResize(width, height);
292 void WindowTreeHostDelegateWayland::OnWindowUnminimized(unsigned handle) {
293 DesktopWindowTreeHostWayland* window =
294 DesktopWindowTreeHostWayland::GetHostForAcceleratedWidget(handle);
296 window->HandleWindowUnminimized();
299 void WindowTreeHostDelegateWayland::OnCommit(unsigned handle,
300 const std::string& text) {
301 DesktopWindowTreeHostWayland* window =
302 DesktopWindowTreeHostWayland::GetHostForAcceleratedWidget(handle);
304 window->HandleCommit(text);
307 void WindowTreeHostDelegateWayland::OnPreeditChanged(unsigned handle,
308 const std::string& text,
309 const std::string& commit) {
310 DesktopWindowTreeHostWayland* window =
311 DesktopWindowTreeHostWayland::GetHostForAcceleratedWidget(handle);
313 window->HandlePreeditChanged(text, commit);