2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "web/WebInputEventConversion.h"
34 #include "core/dom/Touch.h"
35 #include "core/dom/TouchList.h"
36 #include "core/events/GestureEvent.h"
37 #include "core/events/KeyboardEvent.h"
38 #include "core/events/MouseEvent.h"
39 #include "core/events/TouchEvent.h"
40 #include "core/events/WheelEvent.h"
41 #include "core/frame/FrameHost.h"
42 #include "core/frame/FrameView.h"
43 #include "core/frame/PinchViewport.h"
44 #include "core/page/Page.h"
45 #include "core/rendering/RenderObject.h"
46 #include "platform/KeyboardCodes.h"
47 #include "platform/Widget.h"
48 #include "platform/scroll/ScrollView.h"
52 static const double millisPerSecond = 1000.0;
54 static float widgetInputEventsScaleFactor(const Widget* widget)
59 ScrollView* rootView = toScrollView(widget->root());
63 return rootView->inputEventsScaleFactor();
66 static IntSize widgetInputEventsOffset(const Widget* widget)
70 ScrollView* rootView = toScrollView(widget->root());
74 return rootView->inputEventsOffsetForEmulation();
77 static IntPoint pinchViewportOffset(const Widget* widget)
79 // Event position needs to be adjusted by the pinch viewport's offset within the
80 // main frame before being passed into the widget's convertFromContainingWindow.
81 FrameView* rootView = toFrameView(widget->root());
85 return flooredIntPoint(rootView->page()->frameHost().pinchViewport().visibleRect().location());
88 // MakePlatformMouseEvent -----------------------------------------------------
90 PlatformMouseEventBuilder::PlatformMouseEventBuilder(Widget* widget, const WebMouseEvent& e)
92 float scale = widgetInputEventsScaleFactor(widget);
93 IntSize offset = widgetInputEventsOffset(widget);
94 IntPoint pinchViewport = pinchViewportOffset(widget);
96 // FIXME: Widget is always toplevel, unless it's a popup. We may be able
97 // to get rid of this once we abstract popups into a WebKit API.
98 m_position = widget->convertFromContainingWindow(
99 IntPoint((e.x - offset.width()) / scale + pinchViewport.x(), (e.y - offset.height()) / scale + pinchViewport.y()));
100 m_globalPosition = IntPoint(e.globalX, e.globalY);
101 m_movementDelta = IntPoint(e.movementX / scale, e.movementY / scale);
102 m_button = static_cast<MouseButton>(e.button);
105 if (e.modifiers & WebInputEvent::ShiftKey)
106 m_modifiers |= PlatformEvent::ShiftKey;
107 if (e.modifiers & WebInputEvent::ControlKey)
108 m_modifiers |= PlatformEvent::CtrlKey;
109 if (e.modifiers & WebInputEvent::AltKey)
110 m_modifiers |= PlatformEvent::AltKey;
111 if (e.modifiers & WebInputEvent::MetaKey)
112 m_modifiers |= PlatformEvent::MetaKey;
114 m_modifierFlags = e.modifiers;
115 m_timestamp = e.timeStampSeconds;
116 m_clickCount = e.clickCount;
119 case WebInputEvent::MouseMove:
120 case WebInputEvent::MouseLeave: // synthesize a move event
121 m_type = PlatformEvent::MouseMoved;
124 case WebInputEvent::MouseDown:
125 m_type = PlatformEvent::MousePressed;
128 case WebInputEvent::MouseUp:
129 m_type = PlatformEvent::MouseReleased;
133 ASSERT_NOT_REACHED();
137 // PlatformWheelEventBuilder --------------------------------------------------
139 PlatformWheelEventBuilder::PlatformWheelEventBuilder(Widget* widget, const WebMouseWheelEvent& e)
141 float scale = widgetInputEventsScaleFactor(widget);
142 IntSize offset = widgetInputEventsOffset(widget);
143 IntPoint pinchViewport = pinchViewportOffset(widget);
145 m_position = widget->convertFromContainingWindow(
146 IntPoint((e.x - offset.width()) / scale + pinchViewport.x(), (e.y - offset.height()) / scale + pinchViewport.y()));
147 m_globalPosition = IntPoint(e.globalX, e.globalY);
150 m_wheelTicksX = e.wheelTicksX;
151 m_wheelTicksY = e.wheelTicksY;
152 m_granularity = e.scrollByPage ?
153 ScrollByPageWheelEvent : ScrollByPixelWheelEvent;
155 m_type = PlatformEvent::Wheel;
158 if (e.modifiers & WebInputEvent::ShiftKey)
159 m_modifiers |= PlatformEvent::ShiftKey;
160 if (e.modifiers & WebInputEvent::ControlKey)
161 m_modifiers |= PlatformEvent::CtrlKey;
162 if (e.modifiers & WebInputEvent::AltKey)
163 m_modifiers |= PlatformEvent::AltKey;
164 if (e.modifiers & WebInputEvent::MetaKey)
165 m_modifiers |= PlatformEvent::MetaKey;
167 m_hasPreciseScrollingDeltas = e.hasPreciseScrollingDeltas;
169 m_phase = static_cast<PlatformWheelEventPhase>(e.phase);
170 m_momentumPhase = static_cast<PlatformWheelEventPhase>(e.momentumPhase);
171 m_timestamp = e.timeStampSeconds;
173 m_unacceleratedScrollingDeltaX = e.deltaX;
174 m_unacceleratedScrollingDeltaY = e.deltaY;
175 m_canRubberbandLeft = e.canRubberbandLeft;
176 m_canRubberbandRight = e.canRubberbandRight;
180 // PlatformGestureEventBuilder --------------------------------------------------
182 PlatformGestureEventBuilder::PlatformGestureEventBuilder(Widget* widget, const WebGestureEvent& e)
184 float scale = widgetInputEventsScaleFactor(widget);
185 IntSize offset = widgetInputEventsOffset(widget);
186 IntPoint pinchViewport = pinchViewportOffset(widget);
189 case WebInputEvent::GestureScrollBegin:
190 m_type = PlatformEvent::GestureScrollBegin;
192 case WebInputEvent::GestureScrollEnd:
193 m_type = PlatformEvent::GestureScrollEnd;
195 case WebInputEvent::GestureFlingStart:
196 m_type = PlatformEvent::GestureFlingStart;
198 case WebInputEvent::GestureScrollUpdate:
199 m_type = PlatformEvent::GestureScrollUpdate;
200 m_data.m_scrollUpdate.m_deltaX = e.data.scrollUpdate.deltaX / scale;
201 m_data.m_scrollUpdate.m_deltaY = e.data.scrollUpdate.deltaY / scale;
202 m_data.m_scrollUpdate.m_velocityX = e.data.scrollUpdate.velocityX;
203 m_data.m_scrollUpdate.m_velocityY = e.data.scrollUpdate.velocityY;
205 case WebInputEvent::GestureScrollUpdateWithoutPropagation:
206 m_type = PlatformEvent::GestureScrollUpdateWithoutPropagation;
207 m_data.m_scrollUpdate.m_deltaX = e.data.scrollUpdate.deltaX / scale;
208 m_data.m_scrollUpdate.m_deltaY = e.data.scrollUpdate.deltaY / scale;
209 m_data.m_scrollUpdate.m_velocityX = e.data.scrollUpdate.velocityX;
210 m_data.m_scrollUpdate.m_velocityY = e.data.scrollUpdate.velocityY;
212 case WebInputEvent::GestureTap:
213 m_type = PlatformEvent::GestureTap;
214 m_area = expandedIntSize(FloatSize(e.data.tap.width / scale, e.data.tap.height / scale));
215 m_data.m_tap.m_tapCount = e.data.tap.tapCount;
217 case WebInputEvent::GestureTapUnconfirmed:
218 m_type = PlatformEvent::GestureTapUnconfirmed;
219 m_area = expandedIntSize(FloatSize(e.data.tap.width / scale, e.data.tap.height / scale));
221 case WebInputEvent::GestureTapDown:
222 m_type = PlatformEvent::GestureTapDown;
223 m_area = expandedIntSize(FloatSize(e.data.tapDown.width / scale, e.data.tapDown.height / scale));
225 case WebInputEvent::GestureShowPress:
226 m_type = PlatformEvent::GestureShowPress;
227 m_area = expandedIntSize(FloatSize(e.data.showPress.width / scale, e.data.showPress.height / scale));
229 case WebInputEvent::GestureTapCancel:
230 m_type = PlatformEvent::GestureTapDownCancel;
232 case WebInputEvent::GestureDoubleTap:
233 // DoubleTap gesture is now handled as PlatformEvent::GestureTap with tap_count = 2. So no
234 // need to convert to a Platfrom DoubleTap gesture. But in WebViewImpl::handleGestureEvent
235 // all WebGestureEvent are converted to PlatformGestureEvent, for completeness and not reach
236 // the ASSERT_NOT_REACHED() at the end, convert the DoubleTap to a NoType.
237 m_type = PlatformEvent::NoType;
239 case WebInputEvent::GestureTwoFingerTap:
240 m_type = PlatformEvent::GestureTwoFingerTap;
241 m_area = expandedIntSize(FloatSize(e.data.twoFingerTap.firstFingerWidth / scale, e.data.twoFingerTap.firstFingerHeight / scale));
243 case WebInputEvent::GestureLongPress:
244 m_type = PlatformEvent::GestureLongPress;
245 m_area = expandedIntSize(FloatSize(e.data.longPress.width / scale, e.data.longPress.height / scale));
247 case WebInputEvent::GestureLongTap:
248 m_type = PlatformEvent::GestureLongTap;
249 m_area = expandedIntSize(FloatSize(e.data.longPress.width / scale, e.data.longPress.height / scale));
251 case WebInputEvent::GesturePinchBegin:
252 m_type = PlatformEvent::GesturePinchBegin;
254 case WebInputEvent::GesturePinchEnd:
255 m_type = PlatformEvent::GesturePinchEnd;
257 case WebInputEvent::GesturePinchUpdate:
258 m_type = PlatformEvent::GesturePinchUpdate;
259 m_data.m_pinchUpdate.m_scale = e.data.pinchUpdate.scale;
262 ASSERT_NOT_REACHED();
264 m_position = widget->convertFromContainingWindow(
265 IntPoint((e.x - offset.width()) / scale + pinchViewport.x(), (e.y - offset.height()) / scale + pinchViewport.y()));
266 m_globalPosition = IntPoint(e.globalX, e.globalY);
267 m_timestamp = e.timeStampSeconds;
270 if (e.modifiers & WebInputEvent::ShiftKey)
271 m_modifiers |= PlatformEvent::ShiftKey;
272 if (e.modifiers & WebInputEvent::ControlKey)
273 m_modifiers |= PlatformEvent::CtrlKey;
274 if (e.modifiers & WebInputEvent::AltKey)
275 m_modifiers |= PlatformEvent::AltKey;
276 if (e.modifiers & WebInputEvent::MetaKey)
277 m_modifiers |= PlatformEvent::MetaKey;
280 // MakePlatformKeyboardEvent --------------------------------------------------
282 inline PlatformEvent::Type toPlatformKeyboardEventType(WebInputEvent::Type type)
285 case WebInputEvent::KeyUp:
286 return PlatformEvent::KeyUp;
287 case WebInputEvent::KeyDown:
288 return PlatformEvent::KeyDown;
289 case WebInputEvent::RawKeyDown:
290 return PlatformEvent::RawKeyDown;
291 case WebInputEvent::Char:
292 return PlatformEvent::Char;
294 ASSERT_NOT_REACHED();
296 return PlatformEvent::KeyDown;
299 PlatformKeyboardEventBuilder::PlatformKeyboardEventBuilder(const WebKeyboardEvent& e)
301 m_type = toPlatformKeyboardEventType(e.type);
302 m_text = String(e.text);
303 m_unmodifiedText = String(e.unmodifiedText);
304 m_keyIdentifier = String(e.keyIdentifier);
305 m_autoRepeat = (e.modifiers & WebInputEvent::IsAutoRepeat);
306 m_nativeVirtualKeyCode = e.nativeKeyCode;
307 m_isKeypad = (e.modifiers & WebInputEvent::IsKeyPad);
308 m_isSystemKey = e.isSystemKey;
311 if (e.modifiers & WebInputEvent::ShiftKey)
312 m_modifiers |= PlatformEvent::ShiftKey;
313 if (e.modifiers & WebInputEvent::ControlKey)
314 m_modifiers |= PlatformEvent::CtrlKey;
315 if (e.modifiers & WebInputEvent::AltKey)
316 m_modifiers |= PlatformEvent::AltKey;
317 if (e.modifiers & WebInputEvent::MetaKey)
318 m_modifiers |= PlatformEvent::MetaKey;
320 // FIXME: PlatformKeyboardEvents expect a locational version of the keycode (e.g. VK_LSHIFT
321 // instead of VK_SHIFT). This should be changed so the location/keycode are stored separately,
322 // as in other places in the code.
323 m_windowsVirtualKeyCode = e.windowsKeyCode;
324 if (e.windowsKeyCode == VK_SHIFT) {
325 if (e.modifiers & WebInputEvent::IsLeft)
326 m_windowsVirtualKeyCode = VK_LSHIFT;
327 else if (e.modifiers & WebInputEvent::IsRight)
328 m_windowsVirtualKeyCode = VK_RSHIFT;
329 } else if (e.windowsKeyCode == VK_CONTROL) {
330 if (e.modifiers & WebInputEvent::IsLeft)
331 m_windowsVirtualKeyCode = VK_LCONTROL;
332 else if (e.modifiers & WebInputEvent::IsRight)
333 m_windowsVirtualKeyCode = VK_RCONTROL;
334 } else if (e.windowsKeyCode == VK_MENU) {
335 if (e.modifiers & WebInputEvent::IsLeft)
336 m_windowsVirtualKeyCode = VK_LMENU;
337 else if (e.modifiers & WebInputEvent::IsRight)
338 m_windowsVirtualKeyCode = VK_RMENU;
343 void PlatformKeyboardEventBuilder::setKeyType(Type type)
345 // According to the behavior of Webkit in Windows platform,
346 // we need to convert KeyDown to RawKeydown and Char events
347 // See WebKit/WebKit/Win/WebView.cpp
348 ASSERT(m_type == KeyDown);
349 ASSERT(type == RawKeyDown || type == Char);
352 if (type == RawKeyDown) {
354 m_unmodifiedText = String();
356 m_keyIdentifier = String();
357 m_windowsVirtualKeyCode = 0;
361 // Please refer to bug http://b/issue?id=961192, which talks about Webkit
362 // keyboard event handling changes. It also mentions the list of keys
363 // which don't have associated character events.
364 bool PlatformKeyboardEventBuilder::isCharacterKey() const
366 switch (windowsVirtualKeyCode()) {
374 inline PlatformEvent::Type toPlatformTouchEventType(const WebInputEvent::Type type)
377 case WebInputEvent::TouchStart:
378 return PlatformEvent::TouchStart;
379 case WebInputEvent::TouchMove:
380 return PlatformEvent::TouchMove;
381 case WebInputEvent::TouchEnd:
382 return PlatformEvent::TouchEnd;
383 case WebInputEvent::TouchCancel:
384 return PlatformEvent::TouchCancel;
386 ASSERT_NOT_REACHED();
388 return PlatformEvent::TouchStart;
391 inline PlatformTouchPoint::State toPlatformTouchPointState(const WebTouchPoint::State state)
394 case WebTouchPoint::StateReleased:
395 return PlatformTouchPoint::TouchReleased;
396 case WebTouchPoint::StatePressed:
397 return PlatformTouchPoint::TouchPressed;
398 case WebTouchPoint::StateMoved:
399 return PlatformTouchPoint::TouchMoved;
400 case WebTouchPoint::StateStationary:
401 return PlatformTouchPoint::TouchStationary;
402 case WebTouchPoint::StateCancelled:
403 return PlatformTouchPoint::TouchCancelled;
404 case WebTouchPoint::StateUndefined:
405 ASSERT_NOT_REACHED();
407 return PlatformTouchPoint::TouchReleased;
410 inline WebTouchPoint::State toWebTouchPointState(const AtomicString& type)
412 if (type == EventTypeNames::touchend)
413 return WebTouchPoint::StateReleased;
414 if (type == EventTypeNames::touchcancel)
415 return WebTouchPoint::StateCancelled;
416 if (type == EventTypeNames::touchstart)
417 return WebTouchPoint::StatePressed;
418 if (type == EventTypeNames::touchmove)
419 return WebTouchPoint::StateMoved;
420 return WebTouchPoint::StateUndefined;
423 PlatformTouchPointBuilder::PlatformTouchPointBuilder(Widget* widget, const WebTouchPoint& point)
425 float scale = 1.0f / widgetInputEventsScaleFactor(widget);
426 IntSize offset = widgetInputEventsOffset(widget);
427 IntPoint pinchViewport = pinchViewportOffset(widget);
429 m_state = toPlatformTouchPointState(point.state);
430 FloatPoint pos = (point.position - offset).scaledBy(scale);
431 pos.moveBy(pinchViewport);
432 IntPoint flooredPoint = flooredIntPoint(pos);
433 // This assumes convertFromContainingWindow does only translations, not scales.
434 m_pos = widget->convertFromContainingWindow(flooredPoint) + (pos - flooredPoint);
435 m_screenPos = FloatPoint(point.screenPosition.x, point.screenPosition.y);
436 m_radius = FloatSize(point.radiusX, point.radiusY).scaledBy(scale);
437 m_rotationAngle = point.rotationAngle;
438 m_force = point.force;
441 PlatformTouchEventBuilder::PlatformTouchEventBuilder(Widget* widget, const WebTouchEvent& event)
443 m_type = toPlatformTouchEventType(event.type);
446 if (event.modifiers & WebInputEvent::ShiftKey)
447 m_modifiers |= PlatformEvent::ShiftKey;
448 if (event.modifiers & WebInputEvent::ControlKey)
449 m_modifiers |= PlatformEvent::CtrlKey;
450 if (event.modifiers & WebInputEvent::AltKey)
451 m_modifiers |= PlatformEvent::AltKey;
452 if (event.modifiers & WebInputEvent::MetaKey)
453 m_modifiers |= PlatformEvent::MetaKey;
455 m_timestamp = event.timeStampSeconds;
457 for (unsigned i = 0; i < event.touchesLength; ++i)
458 m_touchPoints.append(PlatformTouchPointBuilder(widget, event.touches[i]));
460 m_cancelable = event.cancelable;
463 static int getWebInputModifiers(const UIEventWithKeyState& event)
467 modifiers |= WebInputEvent::ControlKey;
468 if (event.shiftKey())
469 modifiers |= WebInputEvent::ShiftKey;
471 modifiers |= WebInputEvent::AltKey;
473 modifiers |= WebInputEvent::MetaKey;
477 static FloatPoint convertAbsoluteLocationForRenderObjectFloat(const LayoutPoint& location, const RenderObject& renderObject)
479 return renderObject.absoluteToLocal(location, UseTransforms);
482 static IntPoint convertAbsoluteLocationForRenderObject(const LayoutPoint& location, const RenderObject& renderObject)
484 return roundedIntPoint(convertAbsoluteLocationForRenderObjectFloat(location, renderObject));
487 static void updateWebMouseEventFromCoreMouseEvent(const MouseRelatedEvent& event, const Widget& widget, const RenderObject& renderObject, WebMouseEvent& webEvent)
489 webEvent.timeStampSeconds = event.timeStamp() / millisPerSecond;
490 webEvent.modifiers = getWebInputModifiers(event);
492 ScrollView* view = toScrollView(widget.parent());
493 IntPoint windowPoint = IntPoint(event.absoluteLocation().x(), event.absoluteLocation().y());
495 windowPoint = view->contentsToWindow(windowPoint);
496 webEvent.globalX = event.screenX();
497 webEvent.globalY = event.screenY();
498 webEvent.windowX = windowPoint.x();
499 webEvent.windowY = windowPoint.y();
500 IntPoint localPoint = convertAbsoluteLocationForRenderObject(event.absoluteLocation(), renderObject);
501 webEvent.x = localPoint.x();
502 webEvent.y = localPoint.y();
505 WebMouseEventBuilder::WebMouseEventBuilder(const Widget* widget, const RenderObject* renderObject, const MouseEvent& event)
507 if (event.type() == EventTypeNames::mousemove)
508 type = WebInputEvent::MouseMove;
509 else if (event.type() == EventTypeNames::mouseout)
510 type = WebInputEvent::MouseLeave;
511 else if (event.type() == EventTypeNames::mouseover)
512 type = WebInputEvent::MouseEnter;
513 else if (event.type() == EventTypeNames::mousedown)
514 type = WebInputEvent::MouseDown;
515 else if (event.type() == EventTypeNames::mouseup)
516 type = WebInputEvent::MouseUp;
517 else if (event.type() == EventTypeNames::contextmenu)
518 type = WebInputEvent::ContextMenu;
520 return; // Skip all other mouse events.
522 updateWebMouseEventFromCoreMouseEvent(event, *widget, *renderObject, *this);
524 switch (event.button()) {
526 button = WebMouseEvent::ButtonLeft;
529 button = WebMouseEvent::ButtonMiddle;
532 button = WebMouseEvent::ButtonRight;
535 if (event.buttonDown()) {
536 switch (event.button()) {
538 modifiers |= WebInputEvent::LeftButtonDown;
541 modifiers |= WebInputEvent::MiddleButtonDown;
544 modifiers |= WebInputEvent::RightButtonDown;
548 button = WebMouseEvent::ButtonNone;
549 movementX = event.movementX();
550 movementY = event.movementY();
551 clickCount = event.detail();
554 // Generate a synthetic WebMouseEvent given a TouchEvent (eg. for emulating a mouse
555 // with touch input for plugins that don't support touch input).
556 WebMouseEventBuilder::WebMouseEventBuilder(const Widget* widget, const RenderObject* renderObject, const TouchEvent& event)
558 if (!event.touches())
560 if (event.touches()->length() != 1) {
561 if (event.touches()->length() || event.type() != EventTypeNames::touchend || !event.changedTouches() || event.changedTouches()->length() != 1)
565 const Touch* touch = event.touches()->length() == 1 ? event.touches()->item(0) : event.changedTouches()->item(0);
566 if (touch->identifier())
569 if (event.type() == EventTypeNames::touchstart)
571 else if (event.type() == EventTypeNames::touchmove)
573 else if (event.type() == EventTypeNames::touchend)
578 timeStampSeconds = event.timeStamp() / millisPerSecond;
579 modifiers = getWebInputModifiers(event);
581 // The mouse event co-ordinates should be generated from the co-ordinates of the touch point.
582 ScrollView* view = toScrollView(widget->parent());
583 IntPoint windowPoint = roundedIntPoint(touch->absoluteLocation());
585 windowPoint = view->contentsToWindow(windowPoint);
586 IntPoint screenPoint = roundedIntPoint(touch->screenLocation());
587 globalX = screenPoint.x();
588 globalY = screenPoint.y();
589 windowX = windowPoint.x();
590 windowY = windowPoint.y();
592 button = WebMouseEvent::ButtonLeft;
593 modifiers |= WebInputEvent::LeftButtonDown;
594 clickCount = (type == MouseDown || type == MouseUp);
596 IntPoint localPoint = convertAbsoluteLocationForRenderObject(touch->absoluteLocation(), *renderObject);
601 WebMouseEventBuilder::WebMouseEventBuilder(const Widget* widget, const PlatformMouseEvent& event)
603 switch (event.type()) {
604 case PlatformEvent::MouseMoved:
607 case PlatformEvent::MousePressed:
610 case PlatformEvent::MouseReleased:
614 ASSERT_NOT_REACHED();
620 if (event.modifiers() & PlatformEvent::ShiftKey)
621 modifiers |= ShiftKey;
622 if (event.modifiers() & PlatformEvent::CtrlKey)
623 modifiers |= ControlKey;
624 if (event.modifiers() & PlatformEvent::AltKey)
626 if (event.modifiers() & PlatformEvent::MetaKey)
627 modifiers |= MetaKey;
629 timeStampSeconds = event.timestamp();
631 // FIXME: Widget is always toplevel, unless it's a popup. We may be able
632 // to get rid of this once we abstract popups into a WebKit API.
633 IntPoint position = widget->convertToContainingWindow(event.position());
634 float scale = widgetInputEventsScaleFactor(widget);
635 position.scale(scale, scale);
638 globalX = event.globalPosition().x();
639 globalY = event.globalPosition().y();
640 movementX = event.movementDelta().x() * scale;
641 movementY = event.movementDelta().y() * scale;
643 button = static_cast<Button>(event.button());
644 clickCount = event.clickCount();
647 WebMouseWheelEventBuilder::WebMouseWheelEventBuilder(const Widget* widget, const RenderObject* renderObject, const WheelEvent& event)
649 if (event.type() != EventTypeNames::wheel && event.type() != EventTypeNames::mousewheel)
651 type = WebInputEvent::MouseWheel;
652 updateWebMouseEventFromCoreMouseEvent(event, *widget, *renderObject, *this);
653 deltaX = -event.deltaX();
654 deltaY = -event.deltaY();
655 wheelTicksX = event.ticksX();
656 wheelTicksY = event.ticksY();
657 scrollByPage = event.deltaMode() == WheelEvent::DOM_DELTA_PAGE;
660 WebKeyboardEventBuilder::WebKeyboardEventBuilder(const KeyboardEvent& event)
662 if (event.type() == EventTypeNames::keydown)
664 else if (event.type() == EventTypeNames::keyup)
665 type = WebInputEvent::KeyUp;
666 else if (event.type() == EventTypeNames::keypress)
667 type = WebInputEvent::Char;
669 return; // Skip all other keyboard events.
671 modifiers = getWebInputModifiers(event);
672 if (event.location() == KeyboardEvent::DOM_KEY_LOCATION_NUMPAD)
673 modifiers |= WebInputEvent::IsKeyPad;
674 else if (event.location() == KeyboardEvent::DOM_KEY_LOCATION_LEFT)
675 modifiers |= WebInputEvent::IsLeft;
676 else if (event.location() == KeyboardEvent::DOM_KEY_LOCATION_RIGHT)
677 modifiers |= WebInputEvent::IsRight;
679 timeStampSeconds = event.timeStamp() / millisPerSecond;
680 windowsKeyCode = event.keyCode();
682 // The platform keyevent does not exist if the event was created using
683 // initKeyboardEvent.
684 if (!event.keyEvent())
686 nativeKeyCode = event.keyEvent()->nativeVirtualKeyCode();
687 unsigned numberOfCharacters = std::min(event.keyEvent()->text().length(), static_cast<unsigned>(textLengthCap));
688 for (unsigned i = 0; i < numberOfCharacters; ++i) {
689 text[i] = event.keyEvent()->text()[i];
690 unmodifiedText[i] = event.keyEvent()->unmodifiedText()[i];
692 memcpy(keyIdentifier, event.keyIdentifier().ascii().data(), event.keyIdentifier().length());
695 WebInputEvent::Type toWebKeyboardEventType(PlatformEvent::Type type)
698 case PlatformEvent::KeyUp:
699 return WebInputEvent::KeyUp;
700 case PlatformEvent::KeyDown:
701 return WebInputEvent::KeyDown;
702 case PlatformEvent::RawKeyDown:
703 return WebInputEvent::RawKeyDown;
704 case PlatformEvent::Char:
705 return WebInputEvent::Char;
707 return WebInputEvent::Undefined;
711 int toWebKeyboardEventModifiers(int modifiers)
713 int newModifiers = 0;
714 if (modifiers & PlatformEvent::ShiftKey)
715 newModifiers |= WebInputEvent::ShiftKey;
716 if (modifiers & PlatformEvent::CtrlKey)
717 newModifiers |= WebInputEvent::ControlKey;
718 if (modifiers & PlatformEvent::AltKey)
719 newModifiers |= WebInputEvent::AltKey;
720 if (modifiers & PlatformEvent::MetaKey)
721 newModifiers |= WebInputEvent::MetaKey;
725 WebKeyboardEventBuilder::WebKeyboardEventBuilder(const PlatformKeyboardEvent& event)
727 type = toWebKeyboardEventType(event.type());
728 modifiers = toWebKeyboardEventModifiers(event.modifiers());
729 if (event.isAutoRepeat())
730 modifiers |= WebInputEvent::IsAutoRepeat;
731 if (event.isKeypad())
732 modifiers |= WebInputEvent::IsKeyPad;
733 isSystemKey = event.isSystemKey();
734 nativeKeyCode = event.nativeVirtualKeyCode();
736 windowsKeyCode = windowsKeyCodeWithoutLocation(event.windowsVirtualKeyCode());
737 modifiers |= locationModifiersFromWindowsKeyCode(event.windowsVirtualKeyCode());
739 event.text().copyTo(text, 0, textLengthCap);
740 event.unmodifiedText().copyTo(unmodifiedText, 0, textLengthCap);
741 memcpy(keyIdentifier, event.keyIdentifier().ascii().data(), std::min(static_cast<unsigned>(keyIdentifierLengthCap), event.keyIdentifier().length()));
744 static WebTouchPoint toWebTouchPoint(const Touch* touch, const RenderObject* renderObject, WebTouchPoint::State state)
747 point.id = touch->identifier();
748 point.screenPosition = touch->screenLocation();
749 point.position = convertAbsoluteLocationForRenderObjectFloat(touch->absoluteLocation(), *renderObject);
750 point.radiusX = touch->radiusX();
751 point.radiusY = touch->radiusY();
752 point.rotationAngle = touch->webkitRotationAngle();
753 point.force = touch->force();
758 static bool hasTouchPointWithId(const WebTouchPoint* touchPoints, unsigned touchPointsLength, unsigned id)
760 for (unsigned i = 0; i < touchPointsLength; ++i) {
761 if (touchPoints[i].id == static_cast<int>(id))
767 static void addTouchPointsIfNotYetAdded(const Widget* widget, WebTouchPoint::State state, TouchList* touches, WebTouchPoint* touchPoints, unsigned* touchPointsLength, const RenderObject* renderObject)
769 unsigned initialTouchPointsLength = *touchPointsLength;
770 for (unsigned i = 0; i < touches->length(); ++i) {
771 const unsigned pointIndex = *touchPointsLength;
772 if (pointIndex >= static_cast<unsigned>(WebTouchEvent::touchesLengthCap))
775 const Touch* touch = touches->item(i);
776 if (hasTouchPointWithId(touchPoints, initialTouchPointsLength, touch->identifier()))
779 touchPoints[pointIndex] = toWebTouchPoint(touch, renderObject, state);
780 ++(*touchPointsLength);
784 WebTouchEventBuilder::WebTouchEventBuilder(const Widget* widget, const RenderObject* renderObject, const TouchEvent& event)
786 if (event.type() == EventTypeNames::touchstart)
788 else if (event.type() == EventTypeNames::touchmove)
790 else if (event.type() == EventTypeNames::touchend)
792 else if (event.type() == EventTypeNames::touchcancel)
795 ASSERT_NOT_REACHED();
800 modifiers = getWebInputModifiers(event);
801 timeStampSeconds = event.timeStamp() / millisPerSecond;
802 cancelable = event.cancelable();
804 addTouchPointsIfNotYetAdded(widget, toWebTouchPointState(event.type()), event.changedTouches(), touches, &touchesLength, renderObject);
805 addTouchPointsIfNotYetAdded(widget, WebTouchPoint::StateStationary, event.touches(), touches, &touchesLength, renderObject);
808 WebGestureEventBuilder::WebGestureEventBuilder(const Widget* widget, const RenderObject* renderObject, const GestureEvent& event)
810 if (event.type() == EventTypeNames::gestureshowpress)
811 type = GestureShowPress;
812 else if (event.type() == EventTypeNames::gesturetapdown)
813 type = GestureTapDown;
814 else if (event.type() == EventTypeNames::gesturescrollstart)
815 type = GestureScrollBegin;
816 else if (event.type() == EventTypeNames::gesturescrollend)
817 type = GestureScrollEnd;
818 else if (event.type() == EventTypeNames::gesturescrollupdate) {
819 type = GestureScrollUpdate;
820 data.scrollUpdate.deltaX = event.deltaX();
821 data.scrollUpdate.deltaY = event.deltaY();
822 } else if (event.type() == EventTypeNames::gesturetap) {
824 data.tap.tapCount = 1;
827 timeStampSeconds = event.timeStamp() / millisPerSecond;
828 modifiers = getWebInputModifiers(event);
830 globalX = event.screenX();
831 globalY = event.screenY();
832 IntPoint localPoint = convertAbsoluteLocationForRenderObject(event.absoluteLocation(), *renderObject);