OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <map> |
| 6 |
5 #include "webkit/plugins/ppapi/event_conversion.h" | 7 #include "webkit/plugins/ppapi/event_conversion.h" |
6 | 8 |
7 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
8 #include "base/i18n/char_iterator.h" | 10 #include "base/i18n/char_iterator.h" |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
11 #include "base/string_util.h" | 13 #include "base/string_util.h" |
12 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 15 #include "base/utf_string_conversion_utils.h" |
13 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
14 #include "base/utf_string_conversion_utils.h" | |
15 #include "ppapi/c/pp_input_event.h" | 17 #include "ppapi/c/pp_input_event.h" |
16 #include "ppapi/shared_impl/ppb_input_event_shared.h" | 18 #include "ppapi/shared_impl/ppb_input_event_shared.h" |
17 #include "ppapi/shared_impl/time_conversion.h" | 19 #include "ppapi/shared_impl/time_conversion.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGamepads.
h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGamepads.
h" |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" |
21 #include "webkit/plugins/ppapi/common.h" | 23 #include "webkit/plugins/ppapi/common.h" |
22 #include "webkit/plugins/ppapi/usb_key_code_conversion.h" | 24 #include "webkit/plugins/ppapi/usb_key_code_conversion.h" |
23 | 25 |
24 using ppapi::EventTimeToPPTimeTicks; | 26 using ppapi::EventTimeToPPTimeTicks; |
25 using ppapi::InputEventData; | 27 using ppapi::InputEventData; |
26 using ppapi::PPTimeTicksToEventTime; | 28 using ppapi::PPTimeTicksToEventTime; |
27 using WebKit::WebInputEvent; | 29 using WebKit::WebInputEvent; |
28 using WebKit::WebKeyboardEvent; | 30 using WebKit::WebKeyboardEvent; |
29 using WebKit::WebMouseEvent; | 31 using WebKit::WebMouseEvent; |
30 using WebKit::WebMouseWheelEvent; | 32 using WebKit::WebMouseWheelEvent; |
31 using WebKit::WebString; | 33 using WebKit::WebString; |
| 34 using WebKit::WebTouchEvent; |
| 35 using WebKit::WebTouchPoint; |
32 using WebKit::WebUChar; | 36 using WebKit::WebUChar; |
33 | 37 |
34 namespace webkit { | 38 namespace webkit { |
35 namespace ppapi { | 39 namespace ppapi { |
36 | 40 |
37 namespace { | 41 namespace { |
38 | 42 |
39 // Verify the modifier flags WebKit uses match the Pepper ones. If these start | 43 // Verify the modifier flags WebKit uses match the Pepper ones. If these start |
40 // not matching, we'll need to write conversion code to preserve the Pepper | 44 // not matching, we'll need to write conversion code to preserve the Pepper |
41 // values (since plugins will be depending on them). | 45 // values (since plugins will be depending on them). |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 case WebInputEvent::MouseWheel: | 94 case WebInputEvent::MouseWheel: |
91 return PP_INPUTEVENT_TYPE_WHEEL; | 95 return PP_INPUTEVENT_TYPE_WHEEL; |
92 case WebInputEvent::RawKeyDown: | 96 case WebInputEvent::RawKeyDown: |
93 return PP_INPUTEVENT_TYPE_RAWKEYDOWN; | 97 return PP_INPUTEVENT_TYPE_RAWKEYDOWN; |
94 case WebInputEvent::KeyDown: | 98 case WebInputEvent::KeyDown: |
95 return PP_INPUTEVENT_TYPE_KEYDOWN; | 99 return PP_INPUTEVENT_TYPE_KEYDOWN; |
96 case WebInputEvent::KeyUp: | 100 case WebInputEvent::KeyUp: |
97 return PP_INPUTEVENT_TYPE_KEYUP; | 101 return PP_INPUTEVENT_TYPE_KEYUP; |
98 case WebInputEvent::Char: | 102 case WebInputEvent::Char: |
99 return PP_INPUTEVENT_TYPE_CHAR; | 103 return PP_INPUTEVENT_TYPE_CHAR; |
| 104 case WebInputEvent::TouchStart: |
| 105 return PP_INPUTEVENT_TYPE_TOUCHSTART; |
| 106 case WebInputEvent::TouchMove: |
| 107 return PP_INPUTEVENT_TYPE_TOUCHMOVE; |
| 108 case WebInputEvent::TouchEnd: |
| 109 return PP_INPUTEVENT_TYPE_TOUCHEND; |
| 110 case WebInputEvent::TouchCancel: |
| 111 return PP_INPUTEVENT_TYPE_TOUCHCANCEL; |
100 case WebInputEvent::Undefined: | 112 case WebInputEvent::Undefined: |
101 default: | 113 default: |
102 return PP_INPUTEVENT_TYPE_UNDEFINED; | 114 return PP_INPUTEVENT_TYPE_UNDEFINED; |
103 } | 115 } |
104 } | 116 } |
105 | 117 |
106 // Generates a PP_InputEvent with the fields common to all events, as well as | 118 // Generates a PP_InputEvent with the fields common to all events, as well as |
107 // the event type from the given web event. Event-specific fields will be zero | 119 // the event type from the given web event. Event-specific fields will be zero |
108 // initialized. | 120 // initialized. |
109 InputEventData GetEventWithCommonFieldsAndType(const WebInputEvent& web_event) { | 121 InputEventData GetEventWithCommonFieldsAndType(const WebInputEvent& web_event) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 InputEventData result = GetEventWithCommonFieldsAndType(event); | 204 InputEventData result = GetEventWithCommonFieldsAndType(event); |
193 result.event_modifiers = mouse_wheel_event.modifiers; | 205 result.event_modifiers = mouse_wheel_event.modifiers; |
194 result.wheel_delta.x = mouse_wheel_event.deltaX; | 206 result.wheel_delta.x = mouse_wheel_event.deltaX; |
195 result.wheel_delta.y = mouse_wheel_event.deltaY; | 207 result.wheel_delta.y = mouse_wheel_event.deltaY; |
196 result.wheel_ticks.x = mouse_wheel_event.wheelTicksX; | 208 result.wheel_ticks.x = mouse_wheel_event.wheelTicksX; |
197 result.wheel_ticks.y = mouse_wheel_event.wheelTicksY; | 209 result.wheel_ticks.y = mouse_wheel_event.wheelTicksY; |
198 result.wheel_scroll_by_page = !!mouse_wheel_event.scrollByPage; | 210 result.wheel_scroll_by_page = !!mouse_wheel_event.scrollByPage; |
199 result_events->push_back(result); | 211 result_events->push_back(result); |
200 } | 212 } |
201 | 213 |
| 214 void SetPPTouchPoints(const WebTouchPoint* touches, uint32_t touches_length, |
| 215 std::vector<PP_TouchPoint>* result) { |
| 216 for (uint32_t i = 0; i < touches_length; i++) { |
| 217 const WebTouchPoint& touch_point = touches[i]; |
| 218 PP_TouchPoint pp_pt; |
| 219 pp_pt.id = touch_point.id; |
| 220 pp_pt.position.x = touch_point.position.x; |
| 221 pp_pt.position.y = touch_point.position.y; |
| 222 pp_pt.radius.x = touch_point.radiusX; |
| 223 pp_pt.radius.y = touch_point.radiusY; |
| 224 pp_pt.rotation_angle = touch_point.rotationAngle; |
| 225 pp_pt.pressure = touch_point.force; |
| 226 result->push_back(pp_pt); |
| 227 } |
| 228 } |
| 229 |
| 230 void AppendTouchEvent(const WebInputEvent& event, |
| 231 std::vector<InputEventData>* result_events) { |
| 232 const WebTouchEvent& touch_event = |
| 233 reinterpret_cast<const WebTouchEvent&>(event); |
| 234 |
| 235 InputEventData result = GetEventWithCommonFieldsAndType(event); |
| 236 SetPPTouchPoints(touch_event.touches, touch_event.touchesLength, |
| 237 &result.touches); |
| 238 SetPPTouchPoints(touch_event.changedTouches, touch_event.changedTouchesLength, |
| 239 &result.changed_touches); |
| 240 SetPPTouchPoints(touch_event.targetTouches, touch_event.targetTouchesLength, |
| 241 &result.target_touches); |
| 242 |
| 243 result_events->push_back(result); |
| 244 } |
| 245 |
| 246 // Structure used to map touch point id's to touch states. Since the pepper |
| 247 // touch event structure does not have states for individual touch points and |
| 248 // instead relies on the event type in combination with the set of touch lists, |
| 249 // we have to set the state for the changed touches to be the same as the event |
| 250 // type and all others to be 'stationary.' |
| 251 typedef std::map<uint32_t, WebTouchPoint::State> TouchStateMap; |
| 252 |
| 253 void SetWebTouchPoints(const std::vector<PP_TouchPoint>& pp_touches, |
| 254 const TouchStateMap& states_map, |
| 255 WebTouchPoint* web_touches, |
| 256 uint32_t* web_touches_length) { |
| 257 |
| 258 for (uint32_t i = 0; i < pp_touches.size() && |
| 259 i < WebTouchEvent::touchesLengthCap; i++) { |
| 260 WebTouchPoint pt; |
| 261 const PP_TouchPoint& pp_pt = pp_touches[i]; |
| 262 pt.id = pp_pt.id; |
| 263 |
| 264 if (states_map.find(pt.id) == states_map.end()) |
| 265 pt.state = WebTouchPoint::StateStationary; |
| 266 else |
| 267 pt.state = states_map.find(pt.id)->second; |
| 268 |
| 269 pt.position.x = pp_pt.position.x; |
| 270 pt.position.y = pp_pt.position.y; |
| 271 // TODO bug:http://code.google.com/p/chromium/issues/detail?id=93902 |
| 272 pt.screenPosition.x = 0; |
| 273 pt.screenPosition.y = 0; |
| 274 pt.force = pp_pt.pressure; |
| 275 pt.radiusX = pp_pt.radius.x; |
| 276 pt.radiusY = pp_pt.radius.y; |
| 277 pt.rotationAngle = pp_pt.rotation_angle; |
| 278 web_touches[i] = pt; |
| 279 (*web_touches_length)++; |
| 280 } |
| 281 } |
| 282 |
| 283 WebTouchEvent* BuildTouchEvent(const InputEventData& event) { |
| 284 WebTouchEvent* web_event = new WebTouchEvent(); |
| 285 WebTouchPoint::State state = WebTouchPoint::StateUndefined; |
| 286 switch (event.event_type) { |
| 287 case PP_INPUTEVENT_TYPE_TOUCHSTART: |
| 288 web_event->type = WebInputEvent::TouchStart; |
| 289 state = WebTouchPoint::StatePressed; |
| 290 break; |
| 291 case PP_INPUTEVENT_TYPE_TOUCHMOVE: |
| 292 web_event->type = WebInputEvent::TouchMove; |
| 293 state = WebTouchPoint::StateMoved; |
| 294 break; |
| 295 case PP_INPUTEVENT_TYPE_TOUCHEND: |
| 296 web_event->type = WebInputEvent::TouchEnd; |
| 297 state = WebTouchPoint::StateReleased; |
| 298 break; |
| 299 case PP_INPUTEVENT_TYPE_TOUCHCANCEL: |
| 300 web_event->type = WebInputEvent::TouchCancel; |
| 301 state = WebTouchPoint::StateCancelled; |
| 302 break; |
| 303 default: |
| 304 NOTREACHED(); |
| 305 } |
| 306 |
| 307 TouchStateMap states_map; |
| 308 for (uint32_t i = 0; i < event.changed_touches.size(); i++) |
| 309 states_map[event.changed_touches[i].id] = state; |
| 310 |
| 311 web_event->timeStampSeconds = PPTimeTicksToEventTime(event.event_time_stamp); |
| 312 |
| 313 SetWebTouchPoints(event.changed_touches, states_map, |
| 314 web_event->changedTouches, |
| 315 &web_event->changedTouchesLength); |
| 316 |
| 317 SetWebTouchPoints(event.touches, states_map, web_event->touches, |
| 318 &web_event->touchesLength); |
| 319 |
| 320 SetWebTouchPoints(event.target_touches, states_map, web_event->targetTouches, |
| 321 &web_event->targetTouchesLength); |
| 322 |
| 323 if (web_event->type == WebInputEvent::TouchEnd || |
| 324 web_event->type == WebInputEvent::TouchCancel) { |
| 325 SetWebTouchPoints(event.changed_touches, states_map, |
| 326 web_event->touches, &web_event->touchesLength); |
| 327 SetWebTouchPoints(event.changed_touches, states_map, |
| 328 web_event->targetTouches, |
| 329 &web_event->targetTouchesLength); |
| 330 } |
| 331 |
| 332 return web_event; |
| 333 } |
| 334 |
202 WebKeyboardEvent* BuildKeyEvent(const InputEventData& event) { | 335 WebKeyboardEvent* BuildKeyEvent(const InputEventData& event) { |
203 WebKeyboardEvent* key_event = new WebKeyboardEvent(); | 336 WebKeyboardEvent* key_event = new WebKeyboardEvent(); |
204 switch (event.event_type) { | 337 switch (event.event_type) { |
205 case PP_INPUTEVENT_TYPE_RAWKEYDOWN: | 338 case PP_INPUTEVENT_TYPE_RAWKEYDOWN: |
206 key_event->type = WebInputEvent::RawKeyDown; | 339 key_event->type = WebInputEvent::RawKeyDown; |
207 break; | 340 break; |
208 case PP_INPUTEVENT_TYPE_KEYDOWN: | 341 case PP_INPUTEVENT_TYPE_KEYDOWN: |
209 key_event->type = WebInputEvent::KeyDown; | 342 key_event->type = WebInputEvent::KeyDown; |
210 break; | 343 break; |
211 case PP_INPUTEVENT_TYPE_KEYUP: | 344 case PP_INPUTEVENT_TYPE_KEYUP: |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 AppendMouseWheelEvent(event, result); | 538 AppendMouseWheelEvent(event, result); |
406 break; | 539 break; |
407 case WebInputEvent::RawKeyDown: | 540 case WebInputEvent::RawKeyDown: |
408 case WebInputEvent::KeyDown: | 541 case WebInputEvent::KeyDown: |
409 case WebInputEvent::KeyUp: | 542 case WebInputEvent::KeyUp: |
410 AppendKeyEvent(event, result); | 543 AppendKeyEvent(event, result); |
411 break; | 544 break; |
412 case WebInputEvent::Char: | 545 case WebInputEvent::Char: |
413 AppendCharEvent(event, result); | 546 AppendCharEvent(event, result); |
414 break; | 547 break; |
| 548 case WebInputEvent::TouchStart: |
| 549 case WebInputEvent::TouchMove: |
| 550 case WebInputEvent::TouchEnd: |
| 551 case WebInputEvent::TouchCancel: |
| 552 AppendTouchEvent(event, result); |
| 553 break; |
415 case WebInputEvent::Undefined: | 554 case WebInputEvent::Undefined: |
416 default: | 555 default: |
417 break; | 556 break; |
418 } | 557 } |
419 } | 558 } |
420 | 559 |
421 WebInputEvent* CreateWebInputEvent(const InputEventData& event) { | 560 WebInputEvent* CreateWebInputEvent(const InputEventData& event) { |
422 scoped_ptr<WebInputEvent> web_input_event; | 561 scoped_ptr<WebInputEvent> web_input_event; |
423 switch (event.event_type) { | 562 switch (event.event_type) { |
424 case PP_INPUTEVENT_TYPE_UNDEFINED: | 563 case PP_INPUTEVENT_TYPE_UNDEFINED: |
(...skipping 18 matching lines...) Expand all Loading... |
443 web_input_event.reset(BuildCharEvent(event)); | 582 web_input_event.reset(BuildCharEvent(event)); |
444 break; | 583 break; |
445 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_START: | 584 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_START: |
446 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE: | 585 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE: |
447 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_END: | 586 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_END: |
448 case PP_INPUTEVENT_TYPE_IME_TEXT: | 587 case PP_INPUTEVENT_TYPE_IME_TEXT: |
449 // TODO(kinaba) implement in WebKit an event structure to handle | 588 // TODO(kinaba) implement in WebKit an event structure to handle |
450 // composition events. | 589 // composition events. |
451 NOTREACHED(); | 590 NOTREACHED(); |
452 break; | 591 break; |
| 592 case PP_INPUTEVENT_TYPE_TOUCHSTART: |
| 593 case PP_INPUTEVENT_TYPE_TOUCHMOVE: |
| 594 case PP_INPUTEVENT_TYPE_TOUCHEND: |
| 595 case PP_INPUTEVENT_TYPE_TOUCHCANCEL: |
| 596 web_input_event.reset(BuildTouchEvent(event)); |
| 597 break; |
453 } | 598 } |
454 | 599 |
455 return web_input_event.release(); | 600 return web_input_event.release(); |
456 } | 601 } |
457 | 602 |
458 // Generate a coherent sequence of input events to simulate a user event. | 603 // Generate a coherent sequence of input events to simulate a user event. |
459 // From src/third_party/WebKit/Tools/DumpRenderTree/chromium/EventSender.cpp. | 604 // From src/third_party/WebKit/Tools/DumpRenderTree/chromium/EventSender.cpp. |
460 std::vector<linked_ptr<WebInputEvent> > CreateSimulatedWebInputEvents( | 605 std::vector<linked_ptr<WebInputEvent> > CreateSimulatedWebInputEvents( |
461 const ::ppapi::InputEventData& event, | 606 const ::ppapi::InputEventData& event, |
462 int plugin_x, | 607 int plugin_x, |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 case WebInputEvent::MouseLeave: | 701 case WebInputEvent::MouseLeave: |
557 case WebInputEvent::ContextMenu: | 702 case WebInputEvent::ContextMenu: |
558 return PP_INPUTEVENT_CLASS_MOUSE; | 703 return PP_INPUTEVENT_CLASS_MOUSE; |
559 case WebInputEvent::MouseWheel: | 704 case WebInputEvent::MouseWheel: |
560 return PP_INPUTEVENT_CLASS_WHEEL; | 705 return PP_INPUTEVENT_CLASS_WHEEL; |
561 case WebInputEvent::RawKeyDown: | 706 case WebInputEvent::RawKeyDown: |
562 case WebInputEvent::KeyDown: | 707 case WebInputEvent::KeyDown: |
563 case WebInputEvent::KeyUp: | 708 case WebInputEvent::KeyUp: |
564 case WebInputEvent::Char: | 709 case WebInputEvent::Char: |
565 return PP_INPUTEVENT_CLASS_KEYBOARD; | 710 return PP_INPUTEVENT_CLASS_KEYBOARD; |
| 711 case WebInputEvent::TouchCancel: |
| 712 case WebInputEvent::TouchEnd: |
| 713 case WebInputEvent::TouchMove: |
| 714 case WebInputEvent::TouchStart: |
| 715 return PP_INPUTEVENT_CLASS_TOUCH; |
566 case WebInputEvent::Undefined: | 716 case WebInputEvent::Undefined: |
567 default: | 717 default: |
568 NOTREACHED(); | 718 NOTREACHED(); |
569 return PP_InputEvent_Class(0); | 719 return PP_InputEvent_Class(0); |
570 } | 720 } |
571 } | 721 } |
572 | 722 |
573 void ConvertWebKitGamepadData(WebKit::WebGamepads& webkit_data, | 723 void ConvertWebKitGamepadData(WebKit::WebGamepads& webkit_data, |
574 PP_GamepadsSampleData* output_data) { | 724 PP_GamepadsSampleData* output_data) { |
575 output_data->length = webkit_data.length; | 725 output_data->length = webkit_data.length; |
(...skipping 15 matching lines...) Expand all Loading... |
591 output_pad.buttons_length = webkit_pad.buttonsLength; | 741 output_pad.buttons_length = webkit_pad.buttonsLength; |
592 memcpy(output_pad.buttons, | 742 memcpy(output_pad.buttons, |
593 webkit_pad.buttons, | 743 webkit_pad.buttons, |
594 sizeof(output_pad.buttons)); | 744 sizeof(output_pad.buttons)); |
595 } | 745 } |
596 } | 746 } |
597 } | 747 } |
598 | 748 |
599 } // namespace ppapi | 749 } // namespace ppapi |
600 } // namespace webkit | 750 } // namespace webkit |
OLD | NEW |