1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebEventConversion.h"
28
29#include "WebEvent.h"
30
31namespace WebKit {
32
33class WebKit2PlatformMouseEvent : public WebCore::PlatformMouseEvent {
34public:
35    WebKit2PlatformMouseEvent(const WebMouseEvent& webEvent)
36    {
37        // PlatformEvent
38        switch (webEvent.type()) {
39        case WebEvent::MouseDown:
40            m_type = WebCore::PlatformEvent::MousePressed;
41            break;
42        case WebEvent::MouseUp:
43            m_type = WebCore::PlatformEvent::MouseReleased;
44            break;
45        case WebEvent::MouseMove:
46            m_type = WebCore::PlatformEvent::MouseMoved;
47            break;
48        default:
49            ASSERT_NOT_REACHED();
50        }
51
52        m_modifiers = 0;
53        if (webEvent.shiftKey())
54            m_modifiers |= ShiftKey;
55        if (webEvent.controlKey())
56            m_modifiers |= CtrlKey;
57        if (webEvent.altKey())
58            m_modifiers |= AltKey;
59        if (webEvent.metaKey())
60            m_modifiers |= MetaKey;
61
62        m_timestamp = webEvent.timestamp();
63
64        // PlatformMouseEvent
65        switch (webEvent.button()) {
66        case WebMouseEvent::NoButton:
67            m_button = WebCore::NoButton;
68            break;
69        case WebMouseEvent::LeftButton:
70            m_button = WebCore::LeftButton;
71            break;
72        case WebMouseEvent::MiddleButton:
73            m_button = WebCore::MiddleButton;
74            break;
75        case WebMouseEvent::RightButton:
76            m_button = WebCore::RightButton;
77            break;
78        default:
79            ASSERT_NOT_REACHED();
80        }
81
82        m_position = webEvent.position();
83        m_globalPosition = webEvent.globalPosition();
84        m_clickCount = webEvent.clickCount();
85
86        m_modifierFlags = 0;
87        if (webEvent.shiftKey())
88            m_modifierFlags |= WebEvent::ShiftKey;
89        if (webEvent.controlKey())
90            m_modifierFlags |= WebEvent::ControlKey;
91        if (webEvent.altKey())
92            m_modifierFlags |= WebEvent::AltKey;
93        if (webEvent.metaKey())
94            m_modifierFlags |= WebEvent::MetaKey;
95    }
96};
97
98WebCore::PlatformMouseEvent platform(const WebMouseEvent& webEvent)
99{
100    return WebKit2PlatformMouseEvent(webEvent);
101}
102
103class WebKit2PlatformWheelEvent : public WebCore::PlatformWheelEvent {
104public:
105    WebKit2PlatformWheelEvent(const WebWheelEvent& webEvent)
106    {
107        // PlatformEvent
108        m_type = PlatformEvent::Wheel;
109
110        m_modifiers = 0;
111        if (webEvent.shiftKey())
112            m_modifiers |= ShiftKey;
113        if (webEvent.controlKey())
114            m_modifiers |= CtrlKey;
115        if (webEvent.altKey())
116            m_modifiers |= AltKey;
117        if (webEvent.metaKey())
118            m_modifiers |= MetaKey;
119
120        m_timestamp = webEvent.timestamp();
121
122        // PlatformWheelEvent
123        m_position = webEvent.position();
124        m_globalPosition = webEvent.globalPosition();
125        m_deltaX = webEvent.delta().width();
126        m_deltaY = webEvent.delta().height();
127        m_wheelTicksX = webEvent.wheelTicks().width();
128        m_wheelTicksY = webEvent.wheelTicks().height();
129        m_granularity = (webEvent.granularity() == WebWheelEvent::ScrollByPageWheelEvent) ? WebCore::ScrollByPageWheelEvent : WebCore::ScrollByPixelWheelEvent;
130        m_directionInvertedFromDevice = webEvent.directionInvertedFromDevice();
131#if PLATFORM(COCOA)
132        m_phase = static_cast<WebCore::PlatformWheelEventPhase>(webEvent.phase());
133        m_momentumPhase = static_cast<WebCore::PlatformWheelEventPhase>(webEvent.momentumPhase());
134        m_hasPreciseScrollingDeltas = webEvent.hasPreciseScrollingDeltas();
135        m_scrollCount = webEvent.scrollCount();
136        m_unacceleratedScrollingDeltaX = webEvent.unacceleratedScrollingDelta().width();
137        m_unacceleratedScrollingDeltaY = webEvent.unacceleratedScrollingDelta().height();
138#endif
139    }
140};
141
142WebCore::PlatformWheelEvent platform(const WebWheelEvent& webEvent)
143{
144    return WebKit2PlatformWheelEvent(webEvent);
145}
146
147class WebKit2PlatformKeyboardEvent : public WebCore::PlatformKeyboardEvent {
148public:
149    WebKit2PlatformKeyboardEvent(const WebKeyboardEvent& webEvent)
150    {
151        // PlatformEvent
152        switch (webEvent.type()) {
153        case WebEvent::KeyDown:
154            m_type = WebCore::PlatformEvent::KeyDown;
155            break;
156        case WebEvent::KeyUp:
157            m_type = WebCore::PlatformEvent::KeyUp;
158            break;
159        case WebEvent::RawKeyDown:
160            m_type = WebCore::PlatformEvent::RawKeyDown;
161            break;
162        case WebEvent::Char:
163            m_type = WebCore::PlatformEvent::Char;
164            break;
165        default:
166            ASSERT_NOT_REACHED();
167        }
168
169        m_modifiers = 0;
170        if (webEvent.shiftKey())
171            m_modifiers |= ShiftKey;
172        if (webEvent.controlKey())
173            m_modifiers |= CtrlKey;
174        if (webEvent.altKey())
175            m_modifiers |= AltKey;
176        if (webEvent.metaKey())
177            m_modifiers |= MetaKey;
178
179        m_timestamp = webEvent.timestamp();
180
181        // PlatformKeyboardEvent
182        m_text = webEvent.text();
183        m_unmodifiedText = webEvent.unmodifiedText();
184        m_keyIdentifier = webEvent.keyIdentifier();
185        m_windowsVirtualKeyCode = webEvent.windowsVirtualKeyCode();
186        m_nativeVirtualKeyCode = webEvent.nativeVirtualKeyCode();
187        m_macCharCode = webEvent.macCharCode();
188#if USE(APPKIT)
189        m_handledByInputMethod = webEvent.handledByInputMethod();
190        m_commands = webEvent.commands();
191#endif
192        m_autoRepeat = webEvent.isAutoRepeat();
193        m_isKeypad = webEvent.isKeypad();
194        m_isSystemKey = webEvent.isSystemKey();
195    }
196};
197
198WebCore::PlatformKeyboardEvent platform(const WebKeyboardEvent& webEvent)
199{
200    return WebKit2PlatformKeyboardEvent(webEvent);
201}
202
203#if ENABLE(TOUCH_EVENTS)
204
205#if PLATFORM(IOS)
206static WebCore::PlatformTouchPoint::TouchPhaseType touchEventType(const WebPlatformTouchPoint& webTouchPoint)
207{
208    switch (webTouchPoint.phase()) {
209    case WebPlatformTouchPoint::TouchReleased:
210        return WebCore::PlatformTouchPoint::TouchPhaseEnded;
211    case WebPlatformTouchPoint::TouchPressed:
212        return WebCore::PlatformTouchPoint::TouchPhaseBegan;
213    case WebPlatformTouchPoint::TouchMoved:
214        return WebCore::PlatformTouchPoint::TouchPhaseMoved;
215    case WebPlatformTouchPoint::TouchStationary:
216        return WebCore::PlatformTouchPoint::TouchPhaseStationary;
217    case WebPlatformTouchPoint::TouchCancelled:
218        return WebCore::PlatformTouchPoint::TouchPhaseCancelled;
219    }
220}
221
222class WebKit2PlatformTouchPoint : public WebCore::PlatformTouchPoint {
223public:
224WebKit2PlatformTouchPoint(const WebPlatformTouchPoint& webTouchPoint)
225    : PlatformTouchPoint(webTouchPoint.identifier(), webTouchPoint.location(), touchEventType(webTouchPoint))
226{
227}
228};
229#else
230
231class WebKit2PlatformTouchPoint : public WebCore::PlatformTouchPoint {
232public:
233    WebKit2PlatformTouchPoint(const WebPlatformTouchPoint& webTouchPoint)
234    {
235        m_id = webTouchPoint.id();
236
237        switch (webTouchPoint.state()) {
238        case WebPlatformTouchPoint::TouchReleased:
239            m_state = PlatformTouchPoint::TouchReleased;
240            break;
241        case WebPlatformTouchPoint::TouchPressed:
242            m_state = PlatformTouchPoint::TouchPressed;
243            break;
244        case WebPlatformTouchPoint::TouchMoved:
245            m_state = PlatformTouchPoint::TouchMoved;
246            break;
247        case WebPlatformTouchPoint::TouchStationary:
248            m_state = PlatformTouchPoint::TouchStationary;
249            break;
250        case WebPlatformTouchPoint::TouchCancelled:
251            m_state = PlatformTouchPoint::TouchCancelled;
252            break;
253        default:
254            ASSERT_NOT_REACHED();
255        }
256
257        m_screenPos = webTouchPoint.screenPosition();
258        m_pos = webTouchPoint.position();
259        m_radiusX = webTouchPoint.radius().width();
260        m_radiusY = webTouchPoint.radius().height();
261        m_force = webTouchPoint.force();
262        m_rotationAngle = webTouchPoint.rotationAngle();
263    }
264};
265#endif // PLATFORM(IOS)
266
267class WebKit2PlatformTouchEvent : public WebCore::PlatformTouchEvent {
268public:
269    WebKit2PlatformTouchEvent(const WebTouchEvent& webEvent)
270    {
271        // PlatformEvent
272        switch (webEvent.type()) {
273        case WebEvent::TouchStart:
274            m_type = WebCore::PlatformEvent::TouchStart;
275            break;
276        case WebEvent::TouchMove:
277            m_type = WebCore::PlatformEvent::TouchMove;
278            break;
279        case WebEvent::TouchEnd:
280            m_type = WebCore::PlatformEvent::TouchEnd;
281            break;
282        case WebEvent::TouchCancel:
283            m_type = WebCore::PlatformEvent::TouchCancel;
284            break;
285        default:
286            ASSERT_NOT_REACHED();
287        }
288
289        m_modifiers = 0;
290        if (webEvent.shiftKey())
291            m_modifiers |= ShiftKey;
292        if (webEvent.controlKey())
293            m_modifiers |= CtrlKey;
294        if (webEvent.altKey())
295            m_modifiers |= AltKey;
296        if (webEvent.metaKey())
297            m_modifiers |= MetaKey;
298
299        m_timestamp = webEvent.timestamp();
300
301#if PLATFORM(IOS)
302        unsigned touchCount = webEvent.touchPoints().size();
303        m_touchPoints.reserveInitialCapacity(touchCount);
304        for (unsigned i = 0; i < touchCount; ++i)
305            m_touchPoints.uncheckedAppend(WebKit2PlatformTouchPoint(webEvent.touchPoints().at(i)));
306
307        m_gestureScale = webEvent.gestureScale();
308        m_gestureRotation = webEvent.gestureRotation();
309        m_canPreventNativeGestures = webEvent.canPreventNativeGestures();
310        m_isGesture = webEvent.isGesture();
311        m_position = webEvent.position();
312        m_globalPosition = webEvent.position();
313#else
314        // PlatformTouchEvent
315        for (size_t i = 0; i < webEvent.touchPoints().size(); ++i)
316            m_touchPoints.append(WebKit2PlatformTouchPoint(webEvent.touchPoints().at(i)));
317#endif //PLATFORM(IOS)
318    }
319};
320
321WebCore::PlatformTouchEvent platform(const WebTouchEvent& webEvent)
322{
323    return WebKit2PlatformTouchEvent(webEvent);
324}
325#endif
326
327} // namespace WebKit
328