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        switch (webEvent.type()) {
38        case WebEvent::MouseDown:
39            m_eventType = WebCore::MouseEventPressed;
40            break;
41        case WebEvent::MouseUp:
42            m_eventType = WebCore::MouseEventReleased;
43            break;
44        case WebEvent::MouseMove:
45            m_eventType = WebCore::MouseEventMoved;
46            break;
47        default:
48            ASSERT_NOT_REACHED();
49        }
50
51        switch (webEvent.button()) {
52        case WebMouseEvent::NoButton:
53            m_button = WebCore::NoButton;
54            break;
55        case WebMouseEvent::LeftButton:
56            m_button = WebCore::LeftButton;
57            break;
58        case WebMouseEvent::MiddleButton:
59            m_button = WebCore::MiddleButton;
60            break;
61        case WebMouseEvent::RightButton:
62            m_button = WebCore::RightButton;
63            break;
64        default:
65            ASSERT_NOT_REACHED();
66        }
67
68        m_position = webEvent.position();
69        m_globalPosition = webEvent.globalPosition();
70        m_clickCount = webEvent.clickCount();
71        m_timestamp = webEvent.timestamp();
72        m_shiftKey = webEvent.shiftKey();
73        m_ctrlKey = webEvent.controlKey();
74        m_altKey = webEvent.altKey();
75        m_metaKey = webEvent.metaKey();
76
77        m_modifierFlags = 0;
78        if (m_shiftKey)
79            m_modifierFlags |= WebEvent::ShiftKey;
80        if (m_ctrlKey)
81            m_modifierFlags |= WebEvent::ControlKey;
82        if (m_altKey)
83            m_modifierFlags |= WebEvent::AltKey;
84        if (m_metaKey)
85            m_modifierFlags |= WebEvent::MetaKey;
86
87#if PLATFORM(WIN)
88        m_didActivateWebView = webEvent.didActivateWebView();
89#endif
90    }
91};
92
93WebCore::PlatformMouseEvent platform(const WebMouseEvent& webEvent)
94{
95    return WebKit2PlatformMouseEvent(webEvent);
96}
97
98class WebKit2PlatformWheelEvent : public WebCore::PlatformWheelEvent {
99public:
100    WebKit2PlatformWheelEvent(const WebWheelEvent& webEvent)
101    {
102        m_position = webEvent.position();
103        m_globalPosition = webEvent.globalPosition();
104        m_deltaX = webEvent.delta().width();
105        m_deltaY = webEvent.delta().height();
106        m_wheelTicksX = webEvent.wheelTicks().width();
107        m_wheelTicksY = webEvent.wheelTicks().height();
108        m_granularity = (webEvent.granularity() == WebWheelEvent::ScrollByPageWheelEvent) ? WebCore::ScrollByPageWheelEvent : WebCore::ScrollByPixelWheelEvent;
109        m_shiftKey = webEvent.shiftKey();
110        m_ctrlKey = webEvent.controlKey();
111        m_altKey = webEvent.altKey();
112        m_metaKey = webEvent.metaKey();
113#if PLATFORM(MAC)
114        m_phase = static_cast<WebCore::PlatformWheelEventPhase>(webEvent.phase());
115        m_momentumPhase = static_cast<WebCore::PlatformWheelEventPhase>(webEvent.momentumPhase());
116        m_hasPreciseScrollingDeltas = webEvent.hasPreciseScrollingDeltas();
117        m_timestamp = webEvent.timestamp();
118#endif
119    }
120};
121
122WebCore::PlatformWheelEvent platform(const WebWheelEvent& webEvent)
123{
124    return WebKit2PlatformWheelEvent(webEvent);
125}
126
127class WebKit2PlatformKeyboardEvent : public WebCore::PlatformKeyboardEvent {
128public:
129    WebKit2PlatformKeyboardEvent(const WebKeyboardEvent& webEvent)
130    {
131        switch (webEvent.type()) {
132        case WebEvent::KeyDown:
133            m_type = PlatformKeyboardEvent::KeyDown;
134            break;
135        case WebEvent::KeyUp:
136            m_type = PlatformKeyboardEvent::KeyUp;
137            break;
138        case WebEvent::RawKeyDown:
139            m_type = PlatformKeyboardEvent::RawKeyDown;
140            break;
141        case WebEvent::Char:
142            m_type = PlatformKeyboardEvent::Char;
143            break;
144        default:
145            ASSERT_NOT_REACHED();
146        }
147        m_text = webEvent.text();
148        m_unmodifiedText = webEvent.unmodifiedText();
149        m_keyIdentifier = webEvent.keyIdentifier();
150        m_windowsVirtualKeyCode = webEvent.windowsVirtualKeyCode();
151        m_nativeVirtualKeyCode = webEvent.nativeVirtualKeyCode();
152        m_autoRepeat = webEvent.isAutoRepeat();
153        m_isKeypad = webEvent.isKeypad();
154        m_shiftKey = webEvent.shiftKey();
155        m_ctrlKey = webEvent.controlKey();
156        m_altKey = webEvent.altKey();
157        m_metaKey = webEvent.metaKey();
158
159#if PLATFORM(WIN)
160        // FIXME: We should make m_isSystemKey available (and false) on all platforms
161        // to avoid this #define.
162        m_isSystemKey = webEvent.isSystemKey();
163#endif
164    }
165};
166
167WebCore::PlatformKeyboardEvent platform(const WebKeyboardEvent& webEvent)
168{
169    return WebKit2PlatformKeyboardEvent(webEvent);
170}
171
172#if ENABLE(GESTURE_EVENTS)
173class WebKit2PlatformGestureEvent : public WebCore::PlatformGestureEvent {
174public:
175    WebKit2PlatformGestureEvent(const WebGestureEvent& webEvent)
176    {
177        switch (webEvent.type()) {
178        case WebEvent::GestureScrollBegin:
179            m_type = PlatformGestureEvent::ScrollBeginType;
180            break;
181        case WebEvent::GestureScrollEnd:
182            m_type = PlatformGestureEvent::ScrollEndType;
183            break;
184        default:
185            ASSERT_NOT_REACHED();
186        }
187
188        m_position = webEvent.position();
189        m_globalPosition = webEvent.globalPosition();
190        m_timestamp = webEvent.timestamp();
191    }
192};
193
194WebCore::PlatformGestureEvent platform(const WebGestureEvent& webEvent)
195{
196    return WebKit2PlatformGestureEvent(webEvent);
197}
198#endif
199
200#if ENABLE(TOUCH_EVENTS)
201class WebKit2PlatformTouchPoint : public WebCore::PlatformTouchPoint {
202public:
203    WebKit2PlatformTouchPoint(const WebPlatformTouchPoint& webTouchPoint)
204    {
205        m_id = webTouchPoint.id();
206
207        switch (webTouchPoint.state()) {
208        case WebPlatformTouchPoint::TouchReleased:
209            m_state = PlatformTouchPoint::TouchReleased;
210            break;
211        case WebPlatformTouchPoint::TouchPressed:
212            m_state = PlatformTouchPoint::TouchPressed;
213            break;
214        case WebPlatformTouchPoint::TouchMoved:
215            m_state = PlatformTouchPoint::TouchMoved;
216            break;
217        case WebPlatformTouchPoint::TouchStationary:
218            m_state = PlatformTouchPoint::TouchStationary;
219            break;
220        case WebPlatformTouchPoint::TouchCancelled:
221            m_state = PlatformTouchPoint::TouchCancelled;
222            break;
223        default:
224            ASSERT_NOT_REACHED();
225        }
226
227        m_screenPos = webTouchPoint.screenPosition();
228        m_pos = webTouchPoint.position();
229    }
230};
231
232class WebKit2PlatformTouchEvent : public WebCore::PlatformTouchEvent {
233public:
234    WebKit2PlatformTouchEvent(const WebTouchEvent& webEvent)
235    {
236        switch (webEvent.type()) {
237        case WebEvent::TouchStart:
238            m_type = WebCore::TouchStart;
239            break;
240        case WebEvent::TouchMove:
241            m_type = WebCore::TouchMove;
242            break;
243        case WebEvent::TouchEnd:
244            m_type = WebCore::TouchEnd;
245            break;
246        case WebEvent::TouchCancel:
247            m_type = WebCore::TouchCancel;
248            break;
249        default:
250            ASSERT_NOT_REACHED();
251        }
252
253        for (int i = 0; i < webEvent.touchPoints().size(); ++i)
254            m_touchPoints.append(WebKit2PlatformTouchPoint(webEvent.touchPoints().at(i)));
255
256        m_ctrlKey = webEvent.controlKey();
257        m_altKey = webEvent.altKey();
258        m_shiftKey = webEvent.shiftKey();
259        m_metaKey = webEvent.metaKey();
260    }
261};
262
263WebCore::PlatformTouchEvent platform(const WebTouchEvent& webEvent)
264{
265    return WebKit2PlatformTouchEvent(webEvent);
266}
267#endif
268
269} // namespace WebKit
270