OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "ui/events/mojo/event_struct_traits.h" | |
6 | |
7 #include "components/mus/public/interfaces/input_event_constants.mojom.h" | |
8 #include "ui/events/event.h" | |
9 #include "ui/events/event_utils.h" | |
10 #include "ui/events/keycodes/dom/keycode_converter.h" | |
11 | |
12 namespace mojo { | |
13 namespace { | |
14 | |
15 mus::mojom::EventType UIEventTypeToMojo(ui::EventType type) { | |
16 switch (type) { | |
17 case ui::ET_MOUSE_PRESSED: | |
18 case ui::ET_TOUCH_PRESSED: | |
19 case ui::ET_POINTER_DOWN: | |
20 return mus::mojom::EventType::POINTER_DOWN; | |
21 | |
22 case ui::ET_MOUSE_DRAGGED: | |
23 case ui::ET_MOUSE_MOVED: | |
24 case ui::ET_MOUSE_ENTERED: | |
25 case ui::ET_TOUCH_MOVED: | |
26 case ui::ET_POINTER_MOVED: | |
27 return mus::mojom::EventType::POINTER_MOVE; | |
28 | |
29 case ui::ET_MOUSE_EXITED: | |
30 case ui::ET_POINTER_EXITED: | |
31 return mus::mojom::EventType::MOUSE_EXIT; | |
32 | |
33 case ui::ET_MOUSEWHEEL: | |
34 return mus::mojom::EventType::WHEEL; | |
35 | |
36 case ui::ET_MOUSE_RELEASED: | |
37 case ui::ET_TOUCH_RELEASED: | |
38 case ui::ET_POINTER_UP: | |
39 return mus::mojom::EventType::POINTER_UP; | |
40 | |
41 case ui::ET_TOUCH_CANCELLED: | |
42 case ui::ET_POINTER_CANCELLED: | |
43 return mus::mojom::EventType::POINTER_CANCEL; | |
44 | |
45 case ui::ET_KEY_PRESSED: | |
46 return mus::mojom::EventType::KEY_PRESSED; | |
47 | |
48 case ui::ET_KEY_RELEASED: | |
49 return mus::mojom::EventType::KEY_RELEASED; | |
50 | |
51 default: | |
52 break; | |
53 } | |
54 return mus::mojom::EventType::UNKNOWN; | |
55 } | |
56 | |
57 ui::EventType MojoMouseEventTypeToUIEvent(mus::mojom::EventType action, | |
58 int32_t flags) { | |
59 switch (action) { | |
60 case mus::mojom::EventType::POINTER_DOWN: | |
61 return ui::ET_MOUSE_PRESSED; | |
62 | |
63 case mus::mojom::EventType::POINTER_UP: | |
64 return ui::ET_MOUSE_RELEASED; | |
65 | |
66 case mus::mojom::EventType::POINTER_MOVE: | |
67 if (flags & (mus::mojom::kEventFlagLeftMouseButton | | |
68 mus::mojom::kEventFlagMiddleMouseButton | | |
69 mus::mojom::kEventFlagRightMouseButton)) { | |
70 return ui::ET_MOUSE_DRAGGED; | |
71 } | |
72 return ui::ET_MOUSE_MOVED; | |
73 | |
74 case mus::mojom::EventType::MOUSE_EXIT: | |
75 return ui::ET_MOUSE_EXITED; | |
76 | |
77 default: | |
78 NOTREACHED(); | |
79 } | |
80 | |
81 return ui::ET_MOUSE_RELEASED; | |
82 } | |
83 | |
84 ui::EventType MojoTouchEventTypeToUIEvent(mus::mojom::EventType action) { | |
85 switch (action) { | |
86 case mus::mojom::EventType::POINTER_DOWN: | |
87 return ui::ET_TOUCH_PRESSED; | |
88 | |
89 case mus::mojom::EventType::POINTER_UP: | |
90 return ui::ET_TOUCH_RELEASED; | |
91 | |
92 case mus::mojom::EventType::POINTER_MOVE: | |
93 return ui::ET_TOUCH_MOVED; | |
94 | |
95 case mus::mojom::EventType::POINTER_CANCEL: | |
96 return ui::ET_TOUCH_CANCELLED; | |
97 | |
98 default: | |
99 NOTREACHED(); | |
100 } | |
101 | |
102 return ui::ET_TOUCH_CANCELLED; | |
103 } | |
104 | |
105 } // namespace | |
106 | |
107 static_assert(mus::mojom::kEventFlagNone == static_cast<int32_t>(ui::EF_NONE), | |
108 "EVENT_FLAGS must match"); | |
109 static_assert(mus::mojom::kEventFlagIsSynthesized == | |
110 static_cast<int32_t>(ui::EF_IS_SYNTHESIZED), | |
111 "EVENT_FLAGS must match"); | |
112 static_assert(mus::mojom::kEventFlagShiftDown == | |
113 static_cast<int32_t>(ui::EF_SHIFT_DOWN), | |
114 "EVENT_FLAGS must match"); | |
115 static_assert(mus::mojom::kEventFlagControlDown == | |
116 static_cast<int32_t>(ui::EF_CONTROL_DOWN), | |
117 "EVENT_FLAGS must match"); | |
118 static_assert(mus::mojom::kEventFlagAltDown == | |
119 static_cast<int32_t>(ui::EF_ALT_DOWN), | |
120 "EVENT_FLAGS must match"); | |
121 static_assert(mus::mojom::kEventFlagCommandDown == | |
122 static_cast<int32_t>(ui::EF_COMMAND_DOWN), | |
123 "EVENT_FLAGS must match"); | |
124 static_assert(mus::mojom::kEventFlagAltgrDown == | |
125 static_cast<int32_t>(ui::EF_ALTGR_DOWN), | |
126 "EVENT_FLAGS must match"); | |
127 static_assert(mus::mojom::kEventFlagMod3Down == | |
128 static_cast<int32_t>(ui::EF_MOD3_DOWN), | |
129 "EVENT_FLAGS must match"); | |
130 static_assert(mus::mojom::kEventFlagNumLockOn == | |
131 static_cast<int32_t>(ui::EF_NUM_LOCK_ON), | |
132 "EVENT_FLAGS must match"); | |
133 static_assert(mus::mojom::kEventFlagCapsLockOn == | |
134 static_cast<int32_t>(ui::EF_CAPS_LOCK_ON), | |
135 "EVENT_FLAGS must match"); | |
136 static_assert(mus::mojom::kEventFlagScrollLockOn == | |
137 static_cast<int32_t>(ui::EF_SCROLL_LOCK_ON), | |
138 "EVENT_FLAGS must match"); | |
139 static_assert(mus::mojom::kEventFlagLeftMouseButton == | |
140 static_cast<int32_t>(ui::EF_LEFT_MOUSE_BUTTON), | |
141 "EVENT_FLAGS must match"); | |
142 static_assert(mus::mojom::kEventFlagMiddleMouseButton == | |
143 static_cast<int32_t>(ui::EF_MIDDLE_MOUSE_BUTTON), | |
144 "EVENT_FLAGS must match"); | |
145 static_assert(mus::mojom::kEventFlagRightMouseButton == | |
146 static_cast<int32_t>(ui::EF_RIGHT_MOUSE_BUTTON), | |
147 "EVENT_FLAGS must match"); | |
148 static_assert(mus::mojom::kEventFlagBackMouseButton == | |
149 static_cast<int32_t>(ui::EF_BACK_MOUSE_BUTTON), | |
150 "EVENT_FLAGS must match"); | |
151 static_assert(mus::mojom::kEventFlagForwardMouseButton == | |
152 static_cast<int32_t>(ui::EF_FORWARD_MOUSE_BUTTON), | |
153 "EVENT_FLAGS must match"); | |
154 | |
155 int32_t StructTraits<mus::mojom::Event, EventUniquePtr>::action( | |
156 const EventUniquePtr& event) { | |
157 return static_cast<int32_t>(UIEventTypeToMojo(event->type())); | |
158 } | |
159 | |
160 int32_t StructTraits<mus::mojom::Event, EventUniquePtr>::flags( | |
161 const EventUniquePtr& event) { | |
162 return event->flags(); | |
163 } | |
164 | |
165 int64_t StructTraits<mus::mojom::Event, EventUniquePtr>::time_stamp( | |
166 const EventUniquePtr& event) { | |
167 return event->time_stamp().ToInternalValue(); | |
168 } | |
169 | |
170 bool StructTraits<mus::mojom::Event, EventUniquePtr>::has_key_data( | |
171 const EventUniquePtr& event) { | |
172 return event->IsKeyEvent(); | |
173 } | |
174 | |
175 mus::mojom::KeyDataPtr | |
176 StructTraits<mus::mojom::Event, EventUniquePtr>::key_data( | |
177 const EventUniquePtr& event) { | |
178 if (!event->IsKeyEvent()) | |
179 return nullptr; | |
180 | |
181 const ui::KeyEvent* key_event = event->AsKeyEvent(); | |
182 mus::mojom::KeyDataPtr key_data(mus::mojom::KeyData::New()); | |
183 key_data->key_code = key_event->GetConflatedWindowsKeyCode(); | |
184 key_data->native_key_code = | |
185 ui::KeycodeConverter::DomCodeToNativeKeycode(key_event->code()); | |
186 key_data->is_char = key_event->is_char(); | |
187 key_data->character = key_event->GetCharacter(); | |
188 key_data->windows_key_code = static_cast<mus::mojom::KeyboardCode>( | |
189 key_event->GetLocatedWindowsKeyboardCode()); | |
190 key_data->text = key_event->GetText(); | |
191 key_data->unmodified_text = key_event->GetUnmodifiedText(); | |
192 | |
193 return key_data; | |
194 } | |
195 | |
196 bool StructTraits<mus::mojom::Event, EventUniquePtr>::has_pointer_data( | |
197 const EventUniquePtr& event) { | |
198 return event->IsPointerEvent() || event->IsMouseWheelEvent(); | |
199 } | |
200 | |
201 mus::mojom::PointerDataPtr | |
202 StructTraits<mus::mojom::Event, EventUniquePtr>::pointer_data( | |
203 const EventUniquePtr& event) { | |
204 if (!event->IsPointerEvent() && !event->IsMouseWheelEvent()) | |
sadrul
2016/06/06 17:10:59
Use has_pointer_data()?
Hadi
2016/06/06 17:45:48
Done.
| |
205 return nullptr; | |
206 | |
207 mus::mojom::PointerDataPtr pointer_data(mus::mojom::PointerData::New()); | |
208 | |
209 const ui::PointerDetails* pointer_details = nullptr; | |
210 if (event->IsPointerEvent()) { | |
211 const ui::PointerEvent* pointer_event = event->AsPointerEvent(); | |
212 pointer_data->pointer_id = pointer_event->pointer_id(); | |
213 pointer_details = &pointer_event->pointer_details(); | |
214 } else { | |
215 const ui::MouseWheelEvent* wheel_event = event->AsMouseWheelEvent(); | |
216 pointer_data->pointer_id = ui::PointerEvent::kMousePointerId; | |
217 pointer_details = &wheel_event->pointer_details(); | |
218 } | |
219 | |
220 switch (pointer_details->pointer_type) { | |
221 case ui::EventPointerType::POINTER_TYPE_MOUSE: | |
222 pointer_data->kind = mus::mojom::PointerKind::MOUSE; | |
223 break; | |
224 case ui::EventPointerType::POINTER_TYPE_TOUCH: | |
225 pointer_data->kind = mus::mojom::PointerKind::TOUCH; | |
226 break; | |
227 default: | |
228 NOTREACHED(); | |
229 } | |
230 | |
231 mus::mojom::BrushDataPtr brush_data(mus::mojom::BrushData::New()); | |
232 // TODO(rjk): this is in the wrong coordinate system | |
233 brush_data->width = pointer_details->radius_x; | |
234 brush_data->height = pointer_details->radius_y; | |
235 // TODO(rjk): update for touch_event->rotation_angle(); | |
236 brush_data->pressure = pointer_details->force; | |
237 brush_data->tilt_x = pointer_details->tilt_x; | |
238 brush_data->tilt_y = pointer_details->tilt_y; | |
239 pointer_data->brush_data = std::move(brush_data); | |
240 | |
241 // TODO(rjkroege): Plumb raw pointer events on windows. | |
242 // TODO(rjkroege): Handle force-touch on MacOS | |
243 // TODO(rjkroege): Adjust brush data appropriately for Android. | |
244 | |
245 mus::mojom::LocationDataPtr location_data(mus::mojom::LocationData::New()); | |
246 const ui::LocatedEvent* located_event = event->AsLocatedEvent(); | |
247 location_data->x = located_event->location_f().x(); | |
248 location_data->y = located_event->location_f().y(); | |
249 location_data->screen_x = located_event->root_location_f().x(); | |
250 location_data->screen_y = located_event->root_location_f().y(); | |
251 pointer_data->location = std::move(location_data); | |
252 | |
253 if (event->IsMouseWheelEvent()) { | |
254 const ui::MouseWheelEvent* wheel_event = event->AsMouseWheelEvent(); | |
255 | |
256 mus::mojom::WheelDataPtr wheel_data(mus::mojom::WheelData::New()); | |
257 | |
258 // TODO(rjkroege): Support page scrolling on windows by directly | |
259 // cracking into a mojo event when the native event is available. | |
260 wheel_data->mode = mus::mojom::WheelMode::LINE; | |
261 // TODO(rjkroege): Support precise scrolling deltas. | |
262 | |
263 if ((event->flags() & ui::EF_SHIFT_DOWN) != 0 && | |
264 wheel_event->x_offset() == 0) { | |
265 wheel_data->delta_x = wheel_event->y_offset(); | |
266 wheel_data->delta_y = 0; | |
267 wheel_data->delta_z = 0; | |
268 } else { | |
269 // TODO(rjkroege): support z in ui::Events. | |
270 wheel_data->delta_x = wheel_event->x_offset(); | |
271 wheel_data->delta_y = wheel_event->y_offset(); | |
272 wheel_data->delta_z = 0; | |
273 } | |
274 pointer_data->wheel_data = std::move(wheel_data); | |
275 } | |
276 | |
277 return pointer_data; | |
278 } | |
279 | |
280 bool StructTraits<mus::mojom::Event, EventUniquePtr>::Read( | |
281 mus::mojom::EventDataView event, | |
282 EventUniquePtr* out) { | |
283 switch (event.action()) { | |
284 case mus::mojom::EventType::KEY_PRESSED: | |
285 case mus::mojom::EventType::KEY_RELEASED: { | |
286 mus::mojom::KeyDataPtr key_data; | |
287 if (!event.ReadKeyData<mus::mojom::KeyDataPtr>(&key_data)) | |
288 return false; | |
289 | |
290 if (key_data->is_char) { | |
291 out->reset(new ui::KeyEvent( | |
292 static_cast<base::char16>(key_data->character), | |
293 static_cast<ui::KeyboardCode>(key_data->key_code), event.flags())); | |
294 return true; | |
295 } else { | |
sadrul
2016/06/06 17:10:59
Don't need the else.
Hadi
2016/06/06 17:45:48
Done.
| |
296 out->reset(new ui::KeyEvent( | |
297 event.action() == mus::mojom::EventType::KEY_PRESSED | |
298 ? ui::ET_KEY_PRESSED | |
299 : ui::ET_KEY_RELEASED, | |
300 | |
301 static_cast<ui::KeyboardCode>(key_data->key_code), event.flags())); | |
302 return true; | |
303 } | |
304 } | |
305 case mus::mojom::EventType::POINTER_DOWN: | |
306 case mus::mojom::EventType::POINTER_UP: | |
307 case mus::mojom::EventType::POINTER_MOVE: | |
308 case mus::mojom::EventType::POINTER_CANCEL: | |
309 case mus::mojom::EventType::MOUSE_EXIT: | |
310 case mus::mojom::EventType::WHEEL: { | |
311 mus::mojom::PointerDataPtr pointer_data; | |
312 if (!event.ReadPointerData<mus::mojom::PointerDataPtr>(&pointer_data)) | |
313 return false; | |
314 | |
315 const gfx::PointF location(pointer_data->location->x, | |
316 pointer_data->location->y); | |
317 const gfx::PointF screen_location(pointer_data->location->screen_x, | |
318 pointer_data->location->screen_y); | |
319 | |
320 switch (pointer_data->kind) { | |
321 case mus::mojom::PointerKind::MOUSE: { | |
322 if (event.action() == mus::mojom::EventType::WHEEL) { | |
323 std::unique_ptr<ui::MouseEvent> pre_wheel_event(new ui::MouseEvent( | |
sadrul
2016/06/06 17:10:59
Why do you need to create two events?
Hadi
2016/06/06 17:45:48
Done.
| |
324 ui::ET_MOUSEWHEEL, gfx::Point(), gfx::Point(), | |
325 ui::EventTimeForNow(), ui::EventFlags(event.flags()), | |
326 ui::EventFlags(event.flags()))); | |
327 pre_wheel_event->set_location_f(location); | |
328 pre_wheel_event->set_root_location_f(screen_location); | |
329 out->reset(new ui::MouseWheelEvent( | |
330 *pre_wheel_event, | |
331 static_cast<int>(pointer_data->wheel_data->delta_x), | |
332 static_cast<int>(pointer_data->wheel_data->delta_y))); | |
333 return true; | |
334 } else { | |
sadrul
2016/06/06 17:10:59
don't need the else.
Hadi
2016/06/06 17:45:48
Done.
| |
335 // TODO(moshayedi): Construct pointer event directly. | |
336 ui::PointerEvent* mouse_event = new ui::PointerEvent(ui::MouseEvent( | |
337 MojoMouseEventTypeToUIEvent(event.action(), event.flags()), | |
338 gfx::Point(), gfx::Point(), ui::EventTimeForNow(), | |
sadrul
2016/06/06 17:10:59
send in |location| and |screen_location| here, ins
Hadi
2016/06/06 17:45:48
Done.
| |
339 ui::EventFlags(event.flags()), ui::EventFlags(event.flags()))); | |
340 mouse_event->set_location_f(location); | |
341 mouse_event->set_root_location_f(screen_location); | |
342 out->reset(mouse_event); | |
343 return true; | |
344 } | |
345 } | |
346 case mus::mojom::PointerKind::TOUCH: { | |
347 // TODO(moshayedi): Construct pointer event directly. | |
348 ui::PointerEvent* touch_event = new ui::PointerEvent(ui::TouchEvent( | |
sadrul
2016/06/06 17:10:59
s/touch_event/pointer_event/g
Hadi
2016/06/06 17:45:48
Done.
| |
349 MojoTouchEventTypeToUIEvent(event.action()), gfx::Point(), | |
350 ui::EventFlags(event.flags()), pointer_data->pointer_id, | |
351 base::TimeDelta::FromInternalValue(event.time_stamp()), | |
352 pointer_data->brush_data->width, pointer_data->brush_data->height, | |
353 0, pointer_data->brush_data->pressure)); | |
354 touch_event->set_location_f(location); | |
355 touch_event->set_root_location_f(screen_location); | |
356 out->reset(touch_event); | |
357 return true; | |
358 } | |
359 case mus::mojom::PointerKind::PEN: | |
360 NOTIMPLEMENTED(); | |
361 return false; | |
362 } | |
363 } | |
364 default: | |
sadrul
2016/06/06 17:10:59
Do not have the default
Hadi
2016/06/06 17:45:48
Done. Replaced it with "case UNKNOWN:" (the only r
| |
365 NOTIMPLEMENTED(); | |
366 } | |
367 | |
368 return false; | |
369 } | |
370 | |
371 } // namespace mojo | |
OLD | NEW |