OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/mus/ws/event_dispatcher.h" | 5 #include "components/mus/ws/event_dispatcher.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "cc/surfaces/surface_hittest.h" | 9 #include "cc/surfaces/surface_hittest.h" |
10 #include "components/mus/surfaces/surfaces_state.h" | 10 #include "components/mus/surfaces/surfaces_state.h" |
11 #include "components/mus/ws/event_dispatcher_delegate.h" | 11 #include "components/mus/ws/event_dispatcher_delegate.h" |
12 #include "components/mus/ws/server_window.h" | 12 #include "components/mus/ws/server_window.h" |
13 #include "components/mus/ws/server_window_delegate.h" | 13 #include "components/mus/ws/server_window_delegate.h" |
14 #include "components/mus/ws/window_coordinate_conversions.h" | 14 #include "components/mus/ws/window_coordinate_conversions.h" |
15 #include "components/mus/ws/window_finder.h" | 15 #include "components/mus/ws/window_finder.h" |
16 #include "components/mus/ws/window_tree_host_impl.h" | 16 #include "components/mus/ws/window_tree_host_impl.h" |
17 #include "mojo/converters/geometry/geometry_type_converters.h" | 17 #include "mojo/converters/geometry/geometry_type_converters.h" |
18 #include "ui/gfx/geometry/point.h" | 18 #include "ui/gfx/geometry/point.h" |
19 #include "ui/gfx/geometry/point_conversions.h" | 19 #include "ui/gfx/geometry/point_conversions.h" |
20 | 20 |
21 namespace mus { | 21 namespace mus { |
22 namespace ws { | 22 namespace ws { |
23 namespace { | 23 namespace { |
24 | 24 |
25 bool IsOnlyOneMouseButtonDown(mojom::EventFlags flags) { | 25 bool IsOnlyOneMouseButtonDown(int flags) { |
26 const uint32_t mouse_only_flags = | 26 const uint32_t mouse_only_flags = |
27 flags & (mojom::EVENT_FLAGS_LEFT_MOUSE_BUTTON | | 27 flags & |
28 mojom::EVENT_FLAGS_MIDDLE_MOUSE_BUTTON | | 28 (mojom::kEventFlagLeftMouseButton | mojom::kEventFlagMiddleMouseButton | |
29 mojom::EVENT_FLAGS_RIGHT_MOUSE_BUTTON); | 29 mojom::kEventFlagRightMouseButton); |
30 return mouse_only_flags == mojom::EVENT_FLAGS_LEFT_MOUSE_BUTTON || | 30 return mouse_only_flags == mojom::kEventFlagLeftMouseButton || |
31 mouse_only_flags == mojom::EVENT_FLAGS_MIDDLE_MOUSE_BUTTON || | 31 mouse_only_flags == mojom::kEventFlagMiddleMouseButton || |
32 mouse_only_flags == mojom::EVENT_FLAGS_RIGHT_MOUSE_BUTTON; | 32 mouse_only_flags == mojom::kEventFlagRightMouseButton; |
33 } | 33 } |
34 | 34 |
35 bool IsLocationInNonclientArea(const ServerWindow* target, | 35 bool IsLocationInNonclientArea(const ServerWindow* target, |
36 const gfx::Point& location) { | 36 const gfx::Point& location) { |
37 if (!target->parent()) | 37 if (!target->parent()) |
38 return false; | 38 return false; |
39 | 39 |
40 gfx::Rect client_area(target->bounds().size()); | 40 gfx::Rect client_area(target->bounds().size()); |
41 client_area.Inset(target->client_area()); | 41 client_area.Inset(target->client_area()); |
42 if (client_area.Contains(location)) | 42 if (client_area.Contains(location)) |
(...skipping 11 matching lines...) Expand all Loading... |
54 return gfx::ToFlooredPoint(gfx::PointF(event.pointer_data->location->x, | 54 return gfx::ToFlooredPoint(gfx::PointF(event.pointer_data->location->x, |
55 event.pointer_data->location->y)); | 55 event.pointer_data->location->y)); |
56 } | 56 } |
57 | 57 |
58 } // namespace | 58 } // namespace |
59 | 59 |
60 class EventMatcher { | 60 class EventMatcher { |
61 public: | 61 public: |
62 explicit EventMatcher(const mojom::EventMatcher& matcher) | 62 explicit EventMatcher(const mojom::EventMatcher& matcher) |
63 : fields_to_match_(NONE), | 63 : fields_to_match_(NONE), |
64 event_type_(mojom::EVENT_TYPE_UNKNOWN), | 64 event_type_(mojom::EventType::UNKNOWN), |
65 event_flags_(mojom::EVENT_FLAGS_NONE), | 65 event_flags_(mojom::kEventFlagNone), |
66 ignore_event_flags_(mojom::EVENT_FLAGS_NONE), | 66 ignore_event_flags_(mojom::kEventFlagNone), |
67 keyboard_code_(mojom::KEYBOARD_CODE_UNKNOWN), | 67 keyboard_code_(mojom::KeyboardCode::UNKNOWN), |
68 pointer_kind_(mojom::POINTER_KIND_MOUSE) { | 68 pointer_kind_(mojom::PointerKind::MOUSE) { |
69 if (matcher.type_matcher) { | 69 if (matcher.type_matcher) { |
70 fields_to_match_ |= TYPE; | 70 fields_to_match_ |= TYPE; |
71 event_type_ = matcher.type_matcher->type; | 71 event_type_ = matcher.type_matcher->type; |
72 } | 72 } |
73 if (matcher.flags_matcher) { | 73 if (matcher.flags_matcher) { |
74 fields_to_match_ |= FLAGS; | 74 fields_to_match_ |= FLAGS; |
75 event_flags_ = matcher.flags_matcher->flags; | 75 event_flags_ = matcher.flags_matcher->flags; |
76 if (matcher.ignore_flags_matcher) | 76 if (matcher.ignore_flags_matcher) |
77 ignore_event_flags_ = matcher.ignore_flags_matcher->flags; | 77 ignore_event_flags_ = matcher.ignore_flags_matcher->flags; |
78 } | 78 } |
(...skipping 10 matching lines...) Expand all Loading... |
89 pointer_region_ = | 89 pointer_region_ = |
90 matcher.pointer_location_matcher->region.To<gfx::RectF>(); | 90 matcher.pointer_location_matcher->region.To<gfx::RectF>(); |
91 } | 91 } |
92 } | 92 } |
93 | 93 |
94 ~EventMatcher() {} | 94 ~EventMatcher() {} |
95 | 95 |
96 bool MatchesEvent(const mojom::Event& event) const { | 96 bool MatchesEvent(const mojom::Event& event) const { |
97 if ((fields_to_match_ & TYPE) && event.action != event_type_) | 97 if ((fields_to_match_ & TYPE) && event.action != event_type_) |
98 return false; | 98 return false; |
99 mojom::EventFlags flags = | 99 int flags = event.flags & ~ignore_event_flags_; |
100 static_cast<mojom::EventFlags>(event.flags & ~ignore_event_flags_); | |
101 if ((fields_to_match_ & FLAGS) && flags != event_flags_) | 100 if ((fields_to_match_ & FLAGS) && flags != event_flags_) |
102 return false; | 101 return false; |
103 if (fields_to_match_ & KEYBOARD_CODE) { | 102 if (fields_to_match_ & KEYBOARD_CODE) { |
104 if (!event.key_data) | 103 if (!event.key_data) |
105 return false; | 104 return false; |
106 if (keyboard_code_ != event.key_data->key_code) | 105 if (static_cast<int32_t>(keyboard_code_) != event.key_data->key_code) |
107 return false; | 106 return false; |
108 } | 107 } |
109 if (fields_to_match_ & POINTER_KIND) { | 108 if (fields_to_match_ & POINTER_KIND) { |
110 if (!event.pointer_data) | 109 if (!event.pointer_data) |
111 return false; | 110 return false; |
112 if (pointer_kind_ != event.pointer_data->kind) | 111 if (pointer_kind_ != event.pointer_data->kind) |
113 return false; | 112 return false; |
114 } | 113 } |
115 if (fields_to_match_ & POINTER_LOCATION) { | 114 if (fields_to_match_ & POINTER_LOCATION) { |
116 // TODO(sad): The tricky part here is to make sure the same coord-space is | 115 // TODO(sad): The tricky part here is to make sure the same coord-space is |
(...skipping 20 matching lines...) Expand all Loading... |
137 NONE = 0, | 136 NONE = 0, |
138 TYPE = 1 << 0, | 137 TYPE = 1 << 0, |
139 FLAGS = 1 << 1, | 138 FLAGS = 1 << 1, |
140 KEYBOARD_CODE = 1 << 2, | 139 KEYBOARD_CODE = 1 << 2, |
141 POINTER_KIND = 1 << 3, | 140 POINTER_KIND = 1 << 3, |
142 POINTER_LOCATION = 1 << 4, | 141 POINTER_LOCATION = 1 << 4, |
143 }; | 142 }; |
144 | 143 |
145 uint32_t fields_to_match_; | 144 uint32_t fields_to_match_; |
146 mojom::EventType event_type_; | 145 mojom::EventType event_type_; |
147 mojom::EventFlags event_flags_; | 146 // Bitfields of kEventFlag* and kMouseEventFlag* values in |
148 mojom::EventFlags ignore_event_flags_; | 147 // input_event_constants.mojom. |
| 148 int event_flags_; |
| 149 int ignore_event_flags_; |
149 mojom::KeyboardCode keyboard_code_; | 150 mojom::KeyboardCode keyboard_code_; |
150 mojom::PointerKind pointer_kind_; | 151 mojom::PointerKind pointer_kind_; |
151 gfx::RectF pointer_region_; | 152 gfx::RectF pointer_region_; |
152 }; | 153 }; |
153 | 154 |
154 //////////////////////////////////////////////////////////////////////////////// | 155 //////////////////////////////////////////////////////////////////////////////// |
155 | 156 |
156 EventDispatcher::EventDispatcher(EventDispatcherDelegate* delegate) | 157 EventDispatcher::EventDispatcher(EventDispatcherDelegate* delegate) |
157 : delegate_(delegate), | 158 : delegate_(delegate), |
158 root_(nullptr), | 159 root_(nullptr), |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 void EventDispatcher::RemoveAccelerator(uint32_t id) { | 193 void EventDispatcher::RemoveAccelerator(uint32_t id) { |
193 auto it = accelerators_.find(id); | 194 auto it = accelerators_.find(id); |
194 DCHECK(it != accelerators_.end()); | 195 DCHECK(it != accelerators_.end()); |
195 accelerators_.erase(it); | 196 accelerators_.erase(it); |
196 } | 197 } |
197 | 198 |
198 void EventDispatcher::ProcessEvent(mojom::EventPtr event) { | 199 void EventDispatcher::ProcessEvent(mojom::EventPtr event) { |
199 if (!root_) | 200 if (!root_) |
200 return; | 201 return; |
201 | 202 |
202 if (event->action == mojom::EVENT_TYPE_KEY_PRESSED && | 203 if (event->action == mojom::EventType::KEY_PRESSED && |
203 !event->key_data->is_char) { | 204 !event->key_data->is_char) { |
204 uint32_t accelerator = 0u; | 205 uint32_t accelerator = 0u; |
205 if (FindAccelerator(*event, &accelerator)) { | 206 if (FindAccelerator(*event, &accelerator)) { |
206 delegate_->OnAccelerator(accelerator, std::move(event)); | 207 delegate_->OnAccelerator(accelerator, std::move(event)); |
207 return; | 208 return; |
208 } | 209 } |
209 } | 210 } |
210 | 211 |
211 if (event->key_data) { | 212 if (event->key_data) { |
212 ProcessKeyEvent(std::move(event)); | 213 ProcessKeyEvent(std::move(event)); |
(...skipping 10 matching lines...) Expand all Loading... |
223 | 224 |
224 void EventDispatcher::ProcessKeyEvent(mojom::EventPtr event) { | 225 void EventDispatcher::ProcessKeyEvent(mojom::EventPtr event) { |
225 ServerWindow* focused_window = | 226 ServerWindow* focused_window = |
226 delegate_->GetFocusedWindowForEventDispatcher(); | 227 delegate_->GetFocusedWindowForEventDispatcher(); |
227 if (focused_window) | 228 if (focused_window) |
228 delegate_->DispatchInputEventToWindow(focused_window, false, | 229 delegate_->DispatchInputEventToWindow(focused_window, false, |
229 std::move(event)); | 230 std::move(event)); |
230 } | 231 } |
231 | 232 |
232 void EventDispatcher::ProcessPointerEvent(mojom::EventPtr event) { | 233 void EventDispatcher::ProcessPointerEvent(mojom::EventPtr event) { |
233 const bool is_mouse_event = | 234 const bool is_mouse_event = event->pointer_data && |
234 event->pointer_data && | 235 event->pointer_data->kind == mojom::PointerKind::MOUSE; |
235 event->pointer_data->kind == mojom::PointerKind::POINTER_KIND_MOUSE; | |
236 | 236 |
237 if (is_mouse_event) | 237 if (is_mouse_event) |
238 mouse_pointer_last_location_ = EventLocationToPoint(*event); | 238 mouse_pointer_last_location_ = EventLocationToPoint(*event); |
239 | 239 |
240 const int32_t pointer_id = event->pointer_data->pointer_id; | 240 const int32_t pointer_id = event->pointer_data->pointer_id; |
241 if (!IsTrackingPointer(pointer_id) || | 241 if (!IsTrackingPointer(pointer_id) || |
242 !pointer_targets_[pointer_id].is_pointer_down) { | 242 !pointer_targets_[pointer_id].is_pointer_down) { |
243 const bool any_pointers_down = AreAnyPointersDown(); | 243 const bool any_pointers_down = AreAnyPointersDown(); |
244 UpdateTargetForPointer(*event); | 244 UpdateTargetForPointer(*event); |
245 if (is_mouse_event) | 245 if (is_mouse_event) |
246 mouse_cursor_source_window_ = pointer_targets_[pointer_id].window; | 246 mouse_cursor_source_window_ = pointer_targets_[pointer_id].window; |
247 | 247 |
248 PointerTarget& pointer_target = pointer_targets_[pointer_id]; | 248 PointerTarget& pointer_target = pointer_targets_[pointer_id]; |
249 if (pointer_target.is_pointer_down) { | 249 if (pointer_target.is_pointer_down) { |
250 if (is_mouse_event) { | 250 if (is_mouse_event) { |
251 mouse_button_down_ = true; | 251 mouse_button_down_ = true; |
252 mouse_cursor_source_window_ = pointer_target.window; | 252 mouse_cursor_source_window_ = pointer_target.window; |
253 } | 253 } |
254 if (!any_pointers_down) | 254 if (!any_pointers_down) |
255 delegate_->SetFocusedWindowFromEventDispatcher(pointer_target.window); | 255 delegate_->SetFocusedWindowFromEventDispatcher(pointer_target.window); |
256 } | 256 } |
257 } | 257 } |
258 | 258 |
259 // Release capture on pointer up. For mouse we only release if there are | 259 // Release capture on pointer up. For mouse we only release if there are |
260 // no buttons down. | 260 // no buttons down. |
261 const bool is_pointer_going_up = | 261 const bool is_pointer_going_up = |
262 (event->action == mojom::EVENT_TYPE_POINTER_UP || | 262 (event->action == mojom::EventType::POINTER_UP || |
263 event->action == mojom::EVENT_TYPE_POINTER_CANCEL) && | 263 event->action == mojom::EventType::POINTER_CANCEL) && |
264 (event->pointer_data->kind != mojom::POINTER_KIND_MOUSE || | 264 (event->pointer_data->kind != mojom::PointerKind::MOUSE || |
265 IsOnlyOneMouseButtonDown(event->flags)); | 265 IsOnlyOneMouseButtonDown(event->flags)); |
266 | 266 |
267 if (is_pointer_going_up && is_mouse_event) { | 267 if (is_pointer_going_up && is_mouse_event) { |
268 // When we release the mouse button, we want the cursor to be sourced from | 268 // When we release the mouse button, we want the cursor to be sourced from |
269 // the window under the mouse pointer, even though we're sending the button | 269 // the window under the mouse pointer, even though we're sending the button |
270 // up event to the window that had implicit capture. We have to set this | 270 // up event to the window that had implicit capture. We have to set this |
271 // before we perform dispatch because the Delegate is going to read this | 271 // before we perform dispatch because the Delegate is going to read this |
272 // information from us. | 272 // information from us. |
273 mouse_button_down_ = false; | 273 mouse_button_down_ = false; |
274 gfx::Point location(EventLocationToPoint(*event)); | 274 gfx::Point location(EventLocationToPoint(*event)); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 pointer_target.in_nonclient_area == | 315 pointer_target.in_nonclient_area == |
316 pointer_targets_[pointer_id].in_nonclient_area) { | 316 pointer_targets_[pointer_id].in_nonclient_area) { |
317 // The targets are the same, only set the down state to true if necessary. | 317 // The targets are the same, only set the down state to true if necessary. |
318 // Down going to up is handled by ProcessPointerEvent(). | 318 // Down going to up is handled by ProcessPointerEvent(). |
319 if (pointer_target.is_pointer_down) | 319 if (pointer_target.is_pointer_down) |
320 pointer_targets_[pointer_id].is_pointer_down = true; | 320 pointer_targets_[pointer_id].is_pointer_down = true; |
321 return; | 321 return; |
322 } | 322 } |
323 | 323 |
324 // The targets are changing. Send an exit if appropriate. | 324 // The targets are changing. Send an exit if appropriate. |
325 if (event.pointer_data->kind == mojom::POINTER_KIND_MOUSE) { | 325 if (event.pointer_data->kind == mojom::PointerKind::MOUSE) { |
326 mojom::EventPtr exit_event = mojom::Event::New(); | 326 mojom::EventPtr exit_event = mojom::Event::New(); |
327 exit_event->action = mojom::EVENT_TYPE_MOUSE_EXIT; | 327 exit_event->action = mojom::EventType::MOUSE_EXIT; |
328 // TODO(sky): copy flags from existing event? | 328 // TODO(sky): copy flags from existing event? |
329 exit_event->flags = mojom::EVENT_FLAGS_NONE; | 329 exit_event->flags = mojom::kEventFlagNone; |
330 exit_event->time_stamp = event.time_stamp; | 330 exit_event->time_stamp = event.time_stamp; |
331 exit_event->pointer_data = mojom::PointerData::New(); | 331 exit_event->pointer_data = mojom::PointerData::New(); |
332 exit_event->pointer_data->pointer_id = event.pointer_data->pointer_id; | 332 exit_event->pointer_data->pointer_id = event.pointer_data->pointer_id; |
333 exit_event->pointer_data->kind = event.pointer_data->kind; | 333 exit_event->pointer_data->kind = event.pointer_data->kind; |
334 exit_event->pointer_data->location = event.pointer_data->location.Clone(); | 334 exit_event->pointer_data->location = event.pointer_data->location.Clone(); |
335 DispatchToPointerTarget(pointer_targets_[pointer_id], | 335 DispatchToPointerTarget(pointer_targets_[pointer_id], |
336 std::move(exit_event)); | 336 std::move(exit_event)); |
337 } | 337 } |
338 | 338 |
339 // Technically we're updating in place, but calling start then stop makes for | 339 // Technically we're updating in place, but calling start then stop makes for |
340 // simpler code. | 340 // simpler code. |
341 StopTrackingPointer(pointer_id); | 341 StopTrackingPointer(pointer_id); |
342 StartTrackingPointer(pointer_id, pointer_target); | 342 StartTrackingPointer(pointer_id, pointer_target); |
343 } | 343 } |
344 | 344 |
345 EventDispatcher::PointerTarget EventDispatcher::PointerTargetForEvent( | 345 EventDispatcher::PointerTarget EventDispatcher::PointerTargetForEvent( |
346 const mojom::Event& event) const { | 346 const mojom::Event& event) const { |
347 PointerTarget pointer_target; | 347 PointerTarget pointer_target; |
348 gfx::Point location(EventLocationToPoint(event)); | 348 gfx::Point location(EventLocationToPoint(event)); |
349 pointer_target.window = | 349 pointer_target.window = |
350 FindDeepestVisibleWindowForEvents(root_, surface_id_, &location); | 350 FindDeepestVisibleWindowForEvents(root_, surface_id_, &location); |
351 pointer_target.in_nonclient_area = | 351 pointer_target.in_nonclient_area = |
352 IsLocationInNonclientArea(pointer_target.window, location); | 352 IsLocationInNonclientArea(pointer_target.window, location); |
353 pointer_target.is_pointer_down = | 353 pointer_target.is_pointer_down = |
354 event.action == mojom::EVENT_TYPE_POINTER_DOWN; | 354 event.action == mojom::EventType::POINTER_DOWN; |
355 return pointer_target; | 355 return pointer_target; |
356 } | 356 } |
357 | 357 |
358 bool EventDispatcher::AreAnyPointersDown() const { | 358 bool EventDispatcher::AreAnyPointersDown() const { |
359 for (const auto& pair : pointer_targets_) { | 359 for (const auto& pair : pointer_targets_) { |
360 if (pair.second.is_pointer_down) | 360 if (pair.second.is_pointer_down) |
361 return true; | 361 return true; |
362 } | 362 } |
363 return false; | 363 return false; |
364 } | 364 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 | 418 |
419 void EventDispatcher::OnWindowDestroyed(ServerWindow* window) { | 419 void EventDispatcher::OnWindowDestroyed(ServerWindow* window) { |
420 CancelPointerEventsToTarget(window); | 420 CancelPointerEventsToTarget(window); |
421 | 421 |
422 if (mouse_cursor_source_window_ == window) | 422 if (mouse_cursor_source_window_ == window) |
423 mouse_cursor_source_window_ = nullptr; | 423 mouse_cursor_source_window_ = nullptr; |
424 } | 424 } |
425 | 425 |
426 } // namespace ws | 426 } // namespace ws |
427 } // namespace mus | 427 } // namespace mus |
OLD | NEW |