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 | #if ENABLE(MAC_GESTURE_EVENTS) |
32 | #include "WebGestureEvent.h" |
33 | #endif |
34 | |
35 | namespace WebKit { |
36 | |
37 | class WebKit2PlatformMouseEvent : public WebCore::PlatformMouseEvent { |
38 | public: |
39 | WebKit2PlatformMouseEvent(const WebMouseEvent& webEvent) |
40 | { |
41 | // PlatformEvent |
42 | switch (webEvent.type()) { |
43 | case WebEvent::MouseDown: |
44 | m_type = WebCore::PlatformEvent::MousePressed; |
45 | m_force = WebCore::ForceAtClick; |
46 | break; |
47 | case WebEvent::MouseUp: |
48 | m_type = WebCore::PlatformEvent::MouseReleased; |
49 | m_force = WebCore::ForceAtClick; |
50 | break; |
51 | case WebEvent::MouseMove: |
52 | m_type = WebCore::PlatformEvent::MouseMoved; |
53 | m_force = webEvent.force(); |
54 | break; |
55 | case WebEvent::MouseForceChanged: |
56 | m_type = WebCore::PlatformEvent::MouseForceChanged; |
57 | m_force = webEvent.force(); |
58 | break; |
59 | case WebEvent::MouseForceDown: |
60 | m_type = WebCore::PlatformEvent::MouseForceDown; |
61 | m_force = WebCore::ForceAtForceClick; |
62 | break; |
63 | case WebEvent::MouseForceUp: |
64 | m_type = WebCore::PlatformEvent::MouseForceUp; |
65 | m_force = WebCore::ForceAtForceClick; |
66 | break; |
67 | default: |
68 | ASSERT_NOT_REACHED(); |
69 | } |
70 | |
71 | if (webEvent.shiftKey()) |
72 | m_modifiers.add(Modifier::ShiftKey); |
73 | if (webEvent.controlKey()) |
74 | m_modifiers.add(Modifier::ControlKey); |
75 | if (webEvent.altKey()) |
76 | m_modifiers.add(Modifier::AltKey); |
77 | if (webEvent.metaKey()) |
78 | m_modifiers.add(Modifier::MetaKey); |
79 | if (webEvent.capsLockKey()) |
80 | m_modifiers.add(Modifier::CapsLockKey); |
81 | |
82 | m_timestamp = webEvent.timestamp(); |
83 | |
84 | // PlatformMouseEvent |
85 | switch (webEvent.button()) { |
86 | case WebMouseEvent::NoButton: |
87 | m_button = WebCore::NoButton; |
88 | break; |
89 | case WebMouseEvent::LeftButton: |
90 | m_button = WebCore::LeftButton; |
91 | break; |
92 | case WebMouseEvent::MiddleButton: |
93 | m_button = WebCore::MiddleButton; |
94 | break; |
95 | case WebMouseEvent::RightButton: |
96 | m_button = WebCore::RightButton; |
97 | break; |
98 | default: |
99 | ASSERT_NOT_REACHED(); |
100 | } |
101 | |
102 | m_buttons = webEvent.buttons(); |
103 | |
104 | m_position = webEvent.position(); |
105 | #if ENABLE(POINTER_LOCK) |
106 | m_movementDelta = WebCore::IntPoint(webEvent.deltaX(), webEvent.deltaY()); |
107 | #endif |
108 | m_globalPosition = webEvent.globalPosition(); |
109 | m_clickCount = webEvent.clickCount(); |
110 | #if PLATFORM(MAC) |
111 | m_eventNumber = webEvent.eventNumber(); |
112 | m_menuTypeForEvent = webEvent.menuTypeForEvent(); |
113 | #endif |
114 | m_modifierFlags = 0; |
115 | if (webEvent.shiftKey()) |
116 | m_modifierFlags |= static_cast<unsigned>(WebEvent::Modifier::ShiftKey); |
117 | if (webEvent.controlKey()) |
118 | m_modifierFlags |= static_cast<unsigned>(WebEvent::Modifier::ControlKey); |
119 | if (webEvent.altKey()) |
120 | m_modifierFlags |= static_cast<unsigned>(WebEvent::Modifier::AltKey); |
121 | if (webEvent.metaKey()) |
122 | m_modifierFlags |= static_cast<unsigned>(WebEvent::Modifier::MetaKey); |
123 | } |
124 | }; |
125 | |
126 | WebCore::PlatformMouseEvent platform(const WebMouseEvent& webEvent) |
127 | { |
128 | return WebKit2PlatformMouseEvent(webEvent); |
129 | } |
130 | |
131 | class WebKit2PlatformWheelEvent : public WebCore::PlatformWheelEvent { |
132 | public: |
133 | WebKit2PlatformWheelEvent(const WebWheelEvent& webEvent) |
134 | { |
135 | // PlatformEvent |
136 | m_type = PlatformEvent::Wheel; |
137 | |
138 | if (webEvent.shiftKey()) |
139 | m_modifiers.add(Modifier::ShiftKey); |
140 | if (webEvent.controlKey()) |
141 | m_modifiers.add(Modifier::ControlKey); |
142 | if (webEvent.altKey()) |
143 | m_modifiers.add(Modifier::AltKey); |
144 | if (webEvent.metaKey()) |
145 | m_modifiers.add(Modifier::MetaKey); |
146 | if (webEvent.capsLockKey()) |
147 | m_modifiers.add(Modifier::CapsLockKey); |
148 | |
149 | m_timestamp = webEvent.timestamp(); |
150 | |
151 | // PlatformWheelEvent |
152 | m_position = webEvent.position(); |
153 | m_globalPosition = webEvent.globalPosition(); |
154 | m_deltaX = webEvent.delta().width(); |
155 | m_deltaY = webEvent.delta().height(); |
156 | m_wheelTicksX = webEvent.wheelTicks().width(); |
157 | m_wheelTicksY = webEvent.wheelTicks().height(); |
158 | m_granularity = (webEvent.granularity() == WebWheelEvent::ScrollByPageWheelEvent) ? WebCore::ScrollByPageWheelEvent : WebCore::ScrollByPixelWheelEvent; |
159 | m_directionInvertedFromDevice = webEvent.directionInvertedFromDevice(); |
160 | #if (PLATFORM(COCOA) || PLATFORM(GTK)) && ENABLE(ASYNC_SCROLLING) |
161 | m_phase = static_cast<WebCore::PlatformWheelEventPhase>(webEvent.phase()); |
162 | m_momentumPhase = static_cast<WebCore::PlatformWheelEventPhase>(webEvent.momentumPhase()); |
163 | #endif |
164 | #if PLATFORM(COCOA) |
165 | m_hasPreciseScrollingDeltas = webEvent.hasPreciseScrollingDeltas(); |
166 | m_scrollCount = webEvent.scrollCount(); |
167 | m_unacceleratedScrollingDeltaX = webEvent.unacceleratedScrollingDelta().width(); |
168 | m_unacceleratedScrollingDeltaY = webEvent.unacceleratedScrollingDelta().height(); |
169 | #endif |
170 | } |
171 | }; |
172 | |
173 | WebCore::PlatformWheelEvent platform(const WebWheelEvent& webEvent) |
174 | { |
175 | return WebKit2PlatformWheelEvent(webEvent); |
176 | } |
177 | |
178 | class WebKit2PlatformKeyboardEvent : public WebCore::PlatformKeyboardEvent { |
179 | public: |
180 | WebKit2PlatformKeyboardEvent(const WebKeyboardEvent& webEvent) |
181 | { |
182 | // PlatformEvent |
183 | switch (webEvent.type()) { |
184 | case WebEvent::KeyDown: |
185 | m_type = WebCore::PlatformEvent::KeyDown; |
186 | break; |
187 | case WebEvent::KeyUp: |
188 | m_type = WebCore::PlatformEvent::KeyUp; |
189 | break; |
190 | case WebEvent::RawKeyDown: |
191 | m_type = WebCore::PlatformEvent::RawKeyDown; |
192 | break; |
193 | case WebEvent::Char: |
194 | m_type = WebCore::PlatformEvent::Char; |
195 | break; |
196 | default: |
197 | ASSERT_NOT_REACHED(); |
198 | } |
199 | |
200 | if (webEvent.shiftKey()) |
201 | m_modifiers.add(Modifier::ShiftKey); |
202 | if (webEvent.controlKey()) |
203 | m_modifiers.add(Modifier::ControlKey); |
204 | if (webEvent.altKey()) |
205 | m_modifiers.add(Modifier::AltKey); |
206 | if (webEvent.metaKey()) |
207 | m_modifiers.add(Modifier::MetaKey); |
208 | if (webEvent.capsLockKey()) |
209 | m_modifiers.add(Modifier::CapsLockKey); |
210 | |
211 | m_timestamp = webEvent.timestamp(); |
212 | |
213 | // PlatformKeyboardEvent |
214 | m_text = webEvent.text(); |
215 | m_unmodifiedText = webEvent.unmodifiedText(); |
216 | #if ENABLE(KEYBOARD_KEY_ATTRIBUTE) |
217 | m_key = webEvent.key(); |
218 | #endif |
219 | #if ENABLE(KEYBOARD_CODE_ATTRIBUTE) |
220 | m_code = webEvent.code(); |
221 | #endif |
222 | m_keyIdentifier = webEvent.keyIdentifier(); |
223 | m_windowsVirtualKeyCode = webEvent.windowsVirtualKeyCode(); |
224 | #if USE(APPKIT) || USE(UIKIT_KEYBOARD_ADDITIONS) || PLATFORM(GTK) |
225 | m_handledByInputMethod = webEvent.handledByInputMethod(); |
226 | #endif |
227 | #if USE(APPKIT) || PLATFORM(GTK) |
228 | m_commands = webEvent.commands(); |
229 | #endif |
230 | m_autoRepeat = webEvent.isAutoRepeat(); |
231 | m_isKeypad = webEvent.isKeypad(); |
232 | m_isSystemKey = webEvent.isSystemKey(); |
233 | } |
234 | }; |
235 | |
236 | WebCore::PlatformKeyboardEvent platform(const WebKeyboardEvent& webEvent) |
237 | { |
238 | return WebKit2PlatformKeyboardEvent(webEvent); |
239 | } |
240 | |
241 | #if ENABLE(TOUCH_EVENTS) |
242 | |
243 | #if PLATFORM(IOS_FAMILY) |
244 | |
245 | static WebCore::PlatformTouchPoint::TouchPhaseType touchEventType(const WebPlatformTouchPoint& webTouchPoint) |
246 | { |
247 | switch (webTouchPoint.phase()) { |
248 | case WebPlatformTouchPoint::TouchReleased: |
249 | return WebCore::PlatformTouchPoint::TouchPhaseEnded; |
250 | case WebPlatformTouchPoint::TouchPressed: |
251 | return WebCore::PlatformTouchPoint::TouchPhaseBegan; |
252 | case WebPlatformTouchPoint::TouchMoved: |
253 | return WebCore::PlatformTouchPoint::TouchPhaseMoved; |
254 | case WebPlatformTouchPoint::TouchStationary: |
255 | return WebCore::PlatformTouchPoint::TouchPhaseStationary; |
256 | case WebPlatformTouchPoint::TouchCancelled: |
257 | return WebCore::PlatformTouchPoint::TouchPhaseCancelled; |
258 | } |
259 | } |
260 | |
261 | static WebCore::PlatformTouchPoint::TouchType webPlatformTouchTypeToPlatform(const WebPlatformTouchPoint::TouchType& webTouchType) |
262 | { |
263 | switch (webTouchType) { |
264 | case WebPlatformTouchPoint::TouchType::Direct: |
265 | return WebCore::PlatformTouchPoint::TouchType::Direct; |
266 | case WebPlatformTouchPoint::TouchType::Stylus: |
267 | return WebCore::PlatformTouchPoint::TouchType::Stylus; |
268 | } |
269 | } |
270 | |
271 | class WebKit2PlatformTouchPoint : public WebCore::PlatformTouchPoint { |
272 | public: |
273 | WebKit2PlatformTouchPoint(const WebPlatformTouchPoint& webTouchPoint) |
274 | : PlatformTouchPoint(webTouchPoint.identifier(), webTouchPoint.location(), touchEventType(webTouchPoint) |
275 | #if ENABLE(IOS_TOUCH_EVENTS) |
276 | , webTouchPoint.radiusX(), webTouchPoint.radiusY(), webTouchPoint.rotationAngle(), webTouchPoint.force(), webTouchPoint.altitudeAngle(), webTouchPoint.azimuthAngle(), webPlatformTouchTypeToPlatform(webTouchPoint.touchType()) |
277 | #endif |
278 | ) |
279 | { |
280 | } |
281 | }; |
282 | |
283 | #else |
284 | |
285 | class WebKit2PlatformTouchPoint : public WebCore::PlatformTouchPoint { |
286 | public: |
287 | WebKit2PlatformTouchPoint(const WebPlatformTouchPoint& webTouchPoint) |
288 | { |
289 | m_id = webTouchPoint.id(); |
290 | |
291 | switch (webTouchPoint.state()) { |
292 | case WebPlatformTouchPoint::TouchReleased: |
293 | m_state = PlatformTouchPoint::TouchReleased; |
294 | break; |
295 | case WebPlatformTouchPoint::TouchPressed: |
296 | m_state = PlatformTouchPoint::TouchPressed; |
297 | break; |
298 | case WebPlatformTouchPoint::TouchMoved: |
299 | m_state = PlatformTouchPoint::TouchMoved; |
300 | break; |
301 | case WebPlatformTouchPoint::TouchStationary: |
302 | m_state = PlatformTouchPoint::TouchStationary; |
303 | break; |
304 | case WebPlatformTouchPoint::TouchCancelled: |
305 | m_state = PlatformTouchPoint::TouchCancelled; |
306 | break; |
307 | default: |
308 | ASSERT_NOT_REACHED(); |
309 | } |
310 | |
311 | m_screenPos = webTouchPoint.screenPosition(); |
312 | m_pos = webTouchPoint.position(); |
313 | m_radiusX = webTouchPoint.radius().width(); |
314 | m_radiusY = webTouchPoint.radius().height(); |
315 | m_force = webTouchPoint.force(); |
316 | m_rotationAngle = webTouchPoint.rotationAngle(); |
317 | } |
318 | }; |
319 | #endif // PLATFORM(IOS_FAMILY) |
320 | |
321 | class WebKit2PlatformTouchEvent : public WebCore::PlatformTouchEvent { |
322 | public: |
323 | WebKit2PlatformTouchEvent(const WebTouchEvent& webEvent) |
324 | { |
325 | // PlatformEvent |
326 | switch (webEvent.type()) { |
327 | case WebEvent::TouchStart: |
328 | m_type = WebCore::PlatformEvent::TouchStart; |
329 | break; |
330 | case WebEvent::TouchMove: |
331 | m_type = WebCore::PlatformEvent::TouchMove; |
332 | break; |
333 | case WebEvent::TouchEnd: |
334 | m_type = WebCore::PlatformEvent::TouchEnd; |
335 | break; |
336 | case WebEvent::TouchCancel: |
337 | m_type = WebCore::PlatformEvent::TouchCancel; |
338 | break; |
339 | default: |
340 | ASSERT_NOT_REACHED(); |
341 | } |
342 | |
343 | if (webEvent.shiftKey()) |
344 | m_modifiers.add(Modifier::ShiftKey); |
345 | if (webEvent.controlKey()) |
346 | m_modifiers.add(Modifier::ControlKey); |
347 | if (webEvent.altKey()) |
348 | m_modifiers.add(Modifier::AltKey); |
349 | if (webEvent.metaKey()) |
350 | m_modifiers.add(Modifier::MetaKey); |
351 | if (webEvent.capsLockKey()) |
352 | m_modifiers.add(Modifier::CapsLockKey); |
353 | |
354 | m_timestamp = webEvent.timestamp(); |
355 | |
356 | #if PLATFORM(IOS_FAMILY) |
357 | unsigned touchCount = webEvent.touchPoints().size(); |
358 | m_touchPoints.reserveInitialCapacity(touchCount); |
359 | for (unsigned i = 0; i < touchCount; ++i) |
360 | m_touchPoints.uncheckedAppend(WebKit2PlatformTouchPoint(webEvent.touchPoints().at(i))); |
361 | |
362 | m_gestureScale = webEvent.gestureScale(); |
363 | m_gestureRotation = webEvent.gestureRotation(); |
364 | m_canPreventNativeGestures = webEvent.canPreventNativeGestures(); |
365 | m_isGesture = webEvent.isGesture(); |
366 | m_isPotentialTap = webEvent.isPotentialTap(); |
367 | m_position = webEvent.position(); |
368 | m_globalPosition = webEvent.position(); |
369 | #else |
370 | // PlatformTouchEvent |
371 | for (size_t i = 0; i < webEvent.touchPoints().size(); ++i) |
372 | m_touchPoints.append(WebKit2PlatformTouchPoint(webEvent.touchPoints().at(i))); |
373 | #endif //PLATFORM(IOS_FAMILY) |
374 | } |
375 | }; |
376 | |
377 | WebCore::PlatformTouchEvent platform(const WebTouchEvent& webEvent) |
378 | { |
379 | return WebKit2PlatformTouchEvent(webEvent); |
380 | } |
381 | #endif |
382 | |
383 | #if ENABLE(MAC_GESTURE_EVENTS) |
384 | class WebKit2PlatformGestureEvent : public WebCore::PlatformGestureEvent { |
385 | public: |
386 | WebKit2PlatformGestureEvent(const WebGestureEvent& webEvent) |
387 | { |
388 | switch (webEvent.type()) { |
389 | case WebEvent::GestureStart: |
390 | m_type = WebCore::PlatformEvent::GestureStart; |
391 | break; |
392 | case WebEvent::GestureChange: |
393 | m_type = WebCore::PlatformEvent::GestureChange; |
394 | break; |
395 | case WebEvent::GestureEnd: |
396 | m_type = WebCore::PlatformEvent::GestureEnd; |
397 | break; |
398 | default: |
399 | ASSERT_NOT_REACHED(); |
400 | } |
401 | |
402 | if (webEvent.shiftKey()) |
403 | m_modifiers.add(Modifier::ShiftKey); |
404 | if (webEvent.controlKey()) |
405 | m_modifiers.add(Modifier::ControlKey); |
406 | if (webEvent.altKey()) |
407 | m_modifiers.add(Modifier::AltKey); |
408 | if (webEvent.metaKey()) |
409 | m_modifiers.add(Modifier::MetaKey); |
410 | if (webEvent.capsLockKey()) |
411 | m_modifiers.add(Modifier::CapsLockKey); |
412 | |
413 | m_timestamp = webEvent.timestamp(); |
414 | |
415 | m_gestureScale = webEvent.gestureScale(); |
416 | m_gestureRotation = webEvent.gestureRotation(); |
417 | m_position = webEvent.position(); |
418 | m_globalPosition = webEvent.position(); |
419 | } |
420 | }; |
421 | |
422 | WebCore::PlatformGestureEvent platform(const WebGestureEvent& webEvent) |
423 | { |
424 | return WebKit2PlatformGestureEvent(webEvent); |
425 | } |
426 | #endif |
427 | |
428 | } // namespace WebKit |
429 | |