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/impl/window_tree_host_delegate_wayland.h"
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"
12 namespace ozonewayland {
14 WindowTreeHostDelegateWayland::WindowTreeHostDelegateWayland()
15 : current_focus_window_(0),
17 stop_propogation_(false),
18 current_dispatcher_(NULL),
19 current_capture_(NULL),
20 current_active_window_(NULL),
23 DCHECK(base::MessagePumpOzone::Current());
24 base::MessagePumpOzone::Current()->AddDispatcherForRootWindow(this);
25 EventConverterOzoneWayland::GetInstance()->SetWindowChangeObserver(this);
28 WindowTreeHostDelegateWayland::~WindowTreeHostDelegateWayland() {
31 void WindowTreeHostDelegateWayland::OnRootWindowCreated(unsigned handle) {
32 open_windows().push_back(handle);
35 aura_windows_->clear();
41 void WindowTreeHostDelegateWayland::OnRootWindowClosed(unsigned handle) {
42 open_windows().remove(handle);
43 if (open_windows().empty()) {
46 SetActiveWindow(NULL);
48 DCHECK(base::MessagePumpOzone::Current());
49 base::MessagePumpOzone::Current()->RemoveDispatcherForRootWindow(this);
50 EventConverterOzoneWayland::GetInstance()->SetWindowChangeObserver(NULL);
54 aura_windows_->clear();
59 if (!current_active_window_ || current_active_window_->window_ != handle ||
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(
72 SetActiveWindow(rootWindow);
73 rootWindow->HandleNativeWidgetActivationChanged(true);
76 void WindowTreeHostDelegateWayland::SetActiveWindow(
77 DesktopWindowTreeHostWayland* dispatcher) {
78 current_active_window_ = dispatcher;
79 current_dispatcher_ = current_active_window_;
80 if (!current_active_window_)
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(windows.begin(), window_handle);
93 current_active_window_->Activate();
96 DesktopWindowTreeHostWayland*
97 WindowTreeHostDelegateWayland::GetActiveWindow() const {
98 return current_active_window_;
101 void WindowTreeHostDelegateWayland::SetCapture(
102 DesktopWindowTreeHostWayland* dispatcher) {
103 if (current_capture_)
104 current_capture_->OnCaptureReleased();
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_;
113 DesktopWindowTreeHostWayland*
114 WindowTreeHostDelegateWayland::GetCurrentCapture() const {
115 return current_capture_;
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());
125 windows.begin(), windows.end(), aura_windows_->begin(),
126 DesktopWindowTreeHostWayland::GetContentWindowForAcceleratedWidget);
129 return *aura_windows_;
132 ////////////////////////////////////////////////////////////////////////////////
133 // WindowTreeHostDelegateWayland, Private implementation:
134 void WindowTreeHostDelegateWayland::DispatchMouseEvent(
135 ui::MouseEvent* event) {
137 SendEventToProcessor(event);
138 else if (event->type() == ui::ET_MOUSE_PRESSED)
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();
148 std::list<gfx::AcceleratedWidget>&
149 WindowTreeHostDelegateWayland::open_windows() {
151 open_windows_ = new std::list<gfx::AcceleratedWidget>();
153 return *open_windows_;
156 ui::EventProcessor* WindowTreeHostDelegateWayland::GetEventProcessor() {
157 return current_dispatcher_->delegate_->GetEventProcessor();
160 ////////////////////////////////////////////////////////////////////////////////
161 // WindowTreeHostDelegateWayland, MessagePumpDispatcher implementation:
162 bool WindowTreeHostDelegateWayland::Dispatch(const base::NativeEvent& ne) {
163 ui::EventType type = ui::EventTypeFromNative(ne);
164 DCHECK(current_dispatcher_);
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);
175 case ui::ET_KEY_PRESSED: {
176 ui::KeyEvent* keydown_event = static_cast<ui::KeyEvent*>(ne);
177 SendEventToProcessor(keydown_event);
180 case ui::ET_KEY_RELEASED: {
181 ui::KeyEvent* keyup_event = static_cast<ui::KeyEvent*>(ne);
182 SendEventToProcessor(keyup_event);
185 case ui::ET_MOUSEWHEEL: {
186 ui::MouseWheelEvent* wheelev = static_cast<ui::MouseWheelEvent*>(ne);
187 DispatchMouseEvent(wheelev);
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);
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);
207 case ui::ET_UMA_DATA:
212 NOTIMPLEMENTED() << "WindowTreeHostDelegateWayland: unknown event type.";
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)
228 // A new window should not steal focus in case the current window has a open
230 if (current_capture_ && current_capture_ != current_active_window_)
233 DesktopWindowTreeHostWayland* window = NULL;
235 window = DesktopWindowTreeHostWayland::GetHostForAcceleratedWidget(handle);
237 if (!window || window->window_parent_)
240 current_active_window_->HandleNativeWidgetActivationChanged(false);
242 SetActiveWindow(window);
243 window->HandleNativeWidgetActivationChanged(true);
246 void WindowTreeHostDelegateWayland::OnWindowEnter(unsigned handle) {
247 OnWindowFocused(handle);
250 void WindowTreeHostDelegateWayland::OnWindowLeave(unsigned handle) {
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_)
258 if (current_capture_->window_ != handle)
260 DesktopWindowTreeHostWayland* window = NULL;
261 window = DesktopWindowTreeHostWayland::GetHostForAcceleratedWidget(handle);
262 window->OnCaptureReleased();
266 void WindowTreeHostDelegateWayland::OnWindowResized(unsigned handle,
269 DesktopWindowTreeHostWayland* window =
270 DesktopWindowTreeHostWayland::GetHostForAcceleratedWidget(handle);
272 window->HandleWindowResize(width, height);
275 } // namespace ozonewayland