329287cb918984ad191ba5c3857f68bd47bcbf43
[WebKit-https.git] / Source / WebKit2 / Shared / WebEventConversion.cpp
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
31 namespace WebKit {
32
33 class WebKit2PlatformMouseEvent : public WebCore::PlatformMouseEvent {
34 public:
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 #if PLATFORM(MAC)
86         m_eventNumber = webEvent.eventNumber();
87 #endif
88
89         m_modifierFlags = 0;
90         if (webEvent.shiftKey())
91             m_modifierFlags |= WebEvent::ShiftKey;
92         if (webEvent.controlKey())
93             m_modifierFlags |= WebEvent::ControlKey;
94         if (webEvent.altKey())
95             m_modifierFlags |= WebEvent::AltKey;
96         if (webEvent.metaKey())
97             m_modifierFlags |= WebEvent::MetaKey;
98     }
99 };
100
101 WebCore::PlatformMouseEvent platform(const WebMouseEvent& webEvent)
102 {
103     return WebKit2PlatformMouseEvent(webEvent);
104 }
105
106 class WebKit2PlatformWheelEvent : public WebCore::PlatformWheelEvent {
107 public:
108     WebKit2PlatformWheelEvent(const WebWheelEvent& webEvent)
109     {
110         // PlatformEvent
111         m_type = PlatformEvent::Wheel;
112
113         m_modifiers = 0;
114         if (webEvent.shiftKey())
115             m_modifiers |= ShiftKey;
116         if (webEvent.controlKey())
117             m_modifiers |= CtrlKey;
118         if (webEvent.altKey())
119             m_modifiers |= AltKey;
120         if (webEvent.metaKey())
121             m_modifiers |= MetaKey;
122
123         m_timestamp = webEvent.timestamp();
124
125         // PlatformWheelEvent
126         m_position = webEvent.position();
127         m_globalPosition = webEvent.globalPosition();
128         m_deltaX = webEvent.delta().width();
129         m_deltaY = webEvent.delta().height();
130         m_wheelTicksX = webEvent.wheelTicks().width();
131         m_wheelTicksY = webEvent.wheelTicks().height();
132         m_granularity = (webEvent.granularity() == WebWheelEvent::ScrollByPageWheelEvent) ? WebCore::ScrollByPageWheelEvent : WebCore::ScrollByPixelWheelEvent;
133         m_directionInvertedFromDevice = webEvent.directionInvertedFromDevice();
134 #if PLATFORM(COCOA)
135         m_phase = static_cast<WebCore::PlatformWheelEventPhase>(webEvent.phase());
136         m_momentumPhase = static_cast<WebCore::PlatformWheelEventPhase>(webEvent.momentumPhase());
137         m_hasPreciseScrollingDeltas = webEvent.hasPreciseScrollingDeltas();
138         m_scrollCount = webEvent.scrollCount();
139         m_unacceleratedScrollingDeltaX = webEvent.unacceleratedScrollingDelta().width();
140         m_unacceleratedScrollingDeltaY = webEvent.unacceleratedScrollingDelta().height();
141 #endif
142     }
143 };
144
145 WebCore::PlatformWheelEvent platform(const WebWheelEvent& webEvent)
146 {
147     return WebKit2PlatformWheelEvent(webEvent);
148 }
149
150 class WebKit2PlatformKeyboardEvent : public WebCore::PlatformKeyboardEvent {
151 public:
152     WebKit2PlatformKeyboardEvent(const WebKeyboardEvent& webEvent)
153     {
154         // PlatformEvent
155         switch (webEvent.type()) {
156         case WebEvent::KeyDown:
157             m_type = WebCore::PlatformEvent::KeyDown;
158             break;
159         case WebEvent::KeyUp:
160             m_type = WebCore::PlatformEvent::KeyUp;
161             break;
162         case WebEvent::RawKeyDown:
163             m_type = WebCore::PlatformEvent::RawKeyDown;
164             break;
165         case WebEvent::Char:
166             m_type = WebCore::PlatformEvent::Char;
167             break;
168         default:
169             ASSERT_NOT_REACHED();
170         }
171
172         m_modifiers = 0;
173         if (webEvent.shiftKey())
174             m_modifiers |= ShiftKey;
175         if (webEvent.controlKey())
176             m_modifiers |= CtrlKey;
177         if (webEvent.altKey())
178             m_modifiers |= AltKey;
179         if (webEvent.metaKey())
180             m_modifiers |= MetaKey;
181
182         m_timestamp = webEvent.timestamp();
183
184         // PlatformKeyboardEvent
185         m_text = webEvent.text();
186         m_unmodifiedText = webEvent.unmodifiedText();
187         m_keyIdentifier = webEvent.keyIdentifier();
188         m_windowsVirtualKeyCode = webEvent.windowsVirtualKeyCode();
189         m_nativeVirtualKeyCode = webEvent.nativeVirtualKeyCode();
190         m_macCharCode = webEvent.macCharCode();
191 #if USE(APPKIT)
192         m_handledByInputMethod = webEvent.handledByInputMethod();
193         m_commands = webEvent.commands();
194 #endif
195         m_autoRepeat = webEvent.isAutoRepeat();
196         m_isKeypad = webEvent.isKeypad();
197         m_isSystemKey = webEvent.isSystemKey();
198     }
199 };
200
201 WebCore::PlatformKeyboardEvent platform(const WebKeyboardEvent& webEvent)
202 {
203     return WebKit2PlatformKeyboardEvent(webEvent);
204 }
205
206 #if ENABLE(TOUCH_EVENTS)
207
208 #if PLATFORM(IOS)
209 static WebCore::PlatformTouchPoint::TouchPhaseType touchEventType(const WebPlatformTouchPoint& webTouchPoint)
210 {
211     switch (webTouchPoint.phase()) {
212     case WebPlatformTouchPoint::TouchReleased:
213         return WebCore::PlatformTouchPoint::TouchPhaseEnded;
214     case WebPlatformTouchPoint::TouchPressed:
215         return WebCore::PlatformTouchPoint::TouchPhaseBegan;
216     case WebPlatformTouchPoint::TouchMoved:
217         return WebCore::PlatformTouchPoint::TouchPhaseMoved;
218     case WebPlatformTouchPoint::TouchStationary:
219         return WebCore::PlatformTouchPoint::TouchPhaseStationary;
220     case WebPlatformTouchPoint::TouchCancelled:
221         return WebCore::PlatformTouchPoint::TouchPhaseCancelled;
222     }
223 }
224
225 class WebKit2PlatformTouchPoint : public WebCore::PlatformTouchPoint {
226 public:
227 WebKit2PlatformTouchPoint(const WebPlatformTouchPoint& webTouchPoint)
228     : PlatformTouchPoint(webTouchPoint.identifier(), webTouchPoint.location(), touchEventType(webTouchPoint))
229 {
230 }
231 };
232 #else
233
234 class WebKit2PlatformTouchPoint : public WebCore::PlatformTouchPoint {
235 public:
236     WebKit2PlatformTouchPoint(const WebPlatformTouchPoint& webTouchPoint)
237     {
238         m_id = webTouchPoint.id();
239
240         switch (webTouchPoint.state()) {
241         case WebPlatformTouchPoint::TouchReleased:
242             m_state = PlatformTouchPoint::TouchReleased;
243             break;
244         case WebPlatformTouchPoint::TouchPressed:
245             m_state = PlatformTouchPoint::TouchPressed;
246             break;
247         case WebPlatformTouchPoint::TouchMoved:
248             m_state = PlatformTouchPoint::TouchMoved;
249             break;
250         case WebPlatformTouchPoint::TouchStationary:
251             m_state = PlatformTouchPoint::TouchStationary;
252             break;
253         case WebPlatformTouchPoint::TouchCancelled:
254             m_state = PlatformTouchPoint::TouchCancelled;
255             break;
256         default:
257             ASSERT_NOT_REACHED();
258         }
259
260         m_screenPos = webTouchPoint.screenPosition();
261         m_pos = webTouchPoint.position();
262         m_radiusX = webTouchPoint.radius().width();
263         m_radiusY = webTouchPoint.radius().height();
264         m_force = webTouchPoint.force();
265         m_rotationAngle = webTouchPoint.rotationAngle();
266     }
267 };
268 #endif // PLATFORM(IOS)
269
270 class WebKit2PlatformTouchEvent : public WebCore::PlatformTouchEvent {
271 public:
272     WebKit2PlatformTouchEvent(const WebTouchEvent& webEvent)
273     {
274         // PlatformEvent
275         switch (webEvent.type()) {
276         case WebEvent::TouchStart: 
277             m_type = WebCore::PlatformEvent::TouchStart;
278             break;
279         case WebEvent::TouchMove: 
280             m_type = WebCore::PlatformEvent::TouchMove;
281             break;
282         case WebEvent::TouchEnd: 
283             m_type = WebCore::PlatformEvent::TouchEnd;
284             break;
285         case WebEvent::TouchCancel:
286             m_type = WebCore::PlatformEvent::TouchCancel;
287             break;
288         default:
289             ASSERT_NOT_REACHED();
290         }
291
292         m_modifiers = 0;
293         if (webEvent.shiftKey())
294             m_modifiers |= ShiftKey;
295         if (webEvent.controlKey())
296             m_modifiers |= CtrlKey;
297         if (webEvent.altKey())
298             m_modifiers |= AltKey;
299         if (webEvent.metaKey())
300             m_modifiers |= MetaKey;
301
302         m_timestamp = webEvent.timestamp();
303
304 #if PLATFORM(IOS)
305         unsigned touchCount = webEvent.touchPoints().size();
306         m_touchPoints.reserveInitialCapacity(touchCount);
307         for (unsigned i = 0; i < touchCount; ++i)
308             m_touchPoints.uncheckedAppend(WebKit2PlatformTouchPoint(webEvent.touchPoints().at(i)));
309
310         m_gestureScale = webEvent.gestureScale();
311         m_gestureRotation = webEvent.gestureRotation();
312         m_canPreventNativeGestures = webEvent.canPreventNativeGestures();
313         m_isGesture = webEvent.isGesture();
314         m_position = webEvent.position();
315         m_globalPosition = webEvent.position();
316 #else
317         // PlatformTouchEvent
318         for (size_t i = 0; i < webEvent.touchPoints().size(); ++i)
319             m_touchPoints.append(WebKit2PlatformTouchPoint(webEvent.touchPoints().at(i)));
320 #endif //PLATFORM(IOS)
321     }
322 };
323
324 WebCore::PlatformTouchEvent platform(const WebTouchEvent& webEvent)
325 {
326     return WebKit2PlatformTouchEvent(webEvent);
327 }
328 #endif
329
330 } // namespace WebKit