WebEventConversion.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #include "config.h"
  26. #include "WebEventConversion.h"
  27. #include "WebEvent.h"
  28. namespace WebKit {
  29. class WebKit2PlatformMouseEvent : public WebCore::PlatformMouseEvent {
  30. public:
  31. WebKit2PlatformMouseEvent(const WebMouseEvent& webEvent)
  32. {
  33. // PlatformEvent
  34. switch (webEvent.type()) {
  35. case WebEvent::MouseDown:
  36. m_type = WebCore::PlatformEvent::MousePressed;
  37. break;
  38. case WebEvent::MouseUp:
  39. m_type = WebCore::PlatformEvent::MouseReleased;
  40. break;
  41. case WebEvent::MouseMove:
  42. m_type = WebCore::PlatformEvent::MouseMoved;
  43. break;
  44. default:
  45. ASSERT_NOT_REACHED();
  46. }
  47. m_modifiers = 0;
  48. if (webEvent.shiftKey())
  49. m_modifiers |= ShiftKey;
  50. if (webEvent.controlKey())
  51. m_modifiers |= CtrlKey;
  52. if (webEvent.altKey())
  53. m_modifiers |= AltKey;
  54. if (webEvent.metaKey())
  55. m_modifiers |= MetaKey;
  56. m_timestamp = webEvent.timestamp();
  57. // PlatformMouseEvent
  58. switch (webEvent.button()) {
  59. case WebMouseEvent::NoButton:
  60. m_button = WebCore::NoButton;
  61. break;
  62. case WebMouseEvent::LeftButton:
  63. m_button = WebCore::LeftButton;
  64. break;
  65. case WebMouseEvent::MiddleButton:
  66. m_button = WebCore::MiddleButton;
  67. break;
  68. case WebMouseEvent::RightButton:
  69. m_button = WebCore::RightButton;
  70. break;
  71. default:
  72. ASSERT_NOT_REACHED();
  73. }
  74. m_position = webEvent.position();
  75. m_globalPosition = webEvent.globalPosition();
  76. m_clickCount = webEvent.clickCount();
  77. m_modifierFlags = 0;
  78. if (webEvent.shiftKey())
  79. m_modifierFlags |= WebEvent::ShiftKey;
  80. if (webEvent.controlKey())
  81. m_modifierFlags |= WebEvent::ControlKey;
  82. if (webEvent.altKey())
  83. m_modifierFlags |= WebEvent::AltKey;
  84. if (webEvent.metaKey())
  85. m_modifierFlags |= WebEvent::MetaKey;
  86. }
  87. };
  88. WebCore::PlatformMouseEvent platform(const WebMouseEvent& webEvent)
  89. {
  90. return WebKit2PlatformMouseEvent(webEvent);
  91. }
  92. class WebKit2PlatformWheelEvent : public WebCore::PlatformWheelEvent {
  93. public:
  94. WebKit2PlatformWheelEvent(const WebWheelEvent& webEvent)
  95. {
  96. // PlatformEvent
  97. m_type = PlatformEvent::Wheel;
  98. m_modifiers = 0;
  99. if (webEvent.shiftKey())
  100. m_modifiers |= ShiftKey;
  101. if (webEvent.controlKey())
  102. m_modifiers |= CtrlKey;
  103. if (webEvent.altKey())
  104. m_modifiers |= AltKey;
  105. if (webEvent.metaKey())
  106. m_modifiers |= MetaKey;
  107. m_timestamp = webEvent.timestamp();
  108. // PlatformWheelEvent
  109. m_position = webEvent.position();
  110. m_globalPosition = webEvent.globalPosition();
  111. m_deltaX = webEvent.delta().width();
  112. m_deltaY = webEvent.delta().height();
  113. m_wheelTicksX = webEvent.wheelTicks().width();
  114. m_wheelTicksY = webEvent.wheelTicks().height();
  115. m_granularity = (webEvent.granularity() == WebWheelEvent::ScrollByPageWheelEvent) ? WebCore::ScrollByPageWheelEvent : WebCore::ScrollByPixelWheelEvent;
  116. m_directionInvertedFromDevice = webEvent.directionInvertedFromDevice();
  117. #if PLATFORM(MAC)
  118. m_phase = static_cast<WebCore::PlatformWheelEventPhase>(webEvent.phase());
  119. m_momentumPhase = static_cast<WebCore::PlatformWheelEventPhase>(webEvent.momentumPhase());
  120. m_hasPreciseScrollingDeltas = webEvent.hasPreciseScrollingDeltas();
  121. m_scrollCount = webEvent.scrollCount();
  122. m_unacceleratedScrollingDeltaX = webEvent.unacceleratedScrollingDelta().width();
  123. m_unacceleratedScrollingDeltaY = webEvent.unacceleratedScrollingDelta().height();
  124. #endif
  125. }
  126. };
  127. WebCore::PlatformWheelEvent platform(const WebWheelEvent& webEvent)
  128. {
  129. return WebKit2PlatformWheelEvent(webEvent);
  130. }
  131. class WebKit2PlatformKeyboardEvent : public WebCore::PlatformKeyboardEvent {
  132. public:
  133. WebKit2PlatformKeyboardEvent(const WebKeyboardEvent& webEvent)
  134. {
  135. // PlatformEvent
  136. switch (webEvent.type()) {
  137. case WebEvent::KeyDown:
  138. m_type = WebCore::PlatformEvent::KeyDown;
  139. break;
  140. case WebEvent::KeyUp:
  141. m_type = WebCore::PlatformEvent::KeyUp;
  142. break;
  143. case WebEvent::RawKeyDown:
  144. m_type = WebCore::PlatformEvent::RawKeyDown;
  145. break;
  146. case WebEvent::Char:
  147. m_type = WebCore::PlatformEvent::Char;
  148. break;
  149. default:
  150. ASSERT_NOT_REACHED();
  151. }
  152. m_modifiers = 0;
  153. if (webEvent.shiftKey())
  154. m_modifiers |= ShiftKey;
  155. if (webEvent.controlKey())
  156. m_modifiers |= CtrlKey;
  157. if (webEvent.altKey())
  158. m_modifiers |= AltKey;
  159. if (webEvent.metaKey())
  160. m_modifiers |= MetaKey;
  161. m_timestamp = webEvent.timestamp();
  162. // PlatformKeyboardEvent
  163. m_text = webEvent.text();
  164. m_unmodifiedText = webEvent.unmodifiedText();
  165. m_keyIdentifier = webEvent.keyIdentifier();
  166. m_windowsVirtualKeyCode = webEvent.windowsVirtualKeyCode();
  167. m_nativeVirtualKeyCode = webEvent.nativeVirtualKeyCode();
  168. m_macCharCode = webEvent.macCharCode();
  169. m_autoRepeat = webEvent.isAutoRepeat();
  170. m_isKeypad = webEvent.isKeypad();
  171. m_isSystemKey = webEvent.isSystemKey();
  172. }
  173. };
  174. WebCore::PlatformKeyboardEvent platform(const WebKeyboardEvent& webEvent)
  175. {
  176. return WebKit2PlatformKeyboardEvent(webEvent);
  177. }
  178. #if ENABLE(GESTURE_EVENTS)
  179. class WebKit2PlatformGestureEvent : public WebCore::PlatformGestureEvent {
  180. public:
  181. WebKit2PlatformGestureEvent(const WebGestureEvent& webEvent)
  182. {
  183. // PlatformEvent
  184. switch (webEvent.type()) {
  185. case WebEvent::GestureScrollBegin:
  186. m_type = WebCore::PlatformEvent::GestureScrollBegin;
  187. break;
  188. case WebEvent::GestureScrollEnd:
  189. m_type = WebCore::PlatformEvent::GestureScrollEnd;
  190. break;
  191. case WebEvent::GestureSingleTap:
  192. m_type = WebCore::PlatformEvent::GestureTap;
  193. break;
  194. default:
  195. ASSERT_NOT_REACHED();
  196. }
  197. m_modifiers = 0;
  198. if (webEvent.shiftKey())
  199. m_modifiers |= ShiftKey;
  200. if (webEvent.controlKey())
  201. m_modifiers |= CtrlKey;
  202. if (webEvent.altKey())
  203. m_modifiers |= AltKey;
  204. if (webEvent.metaKey())
  205. m_modifiers |= MetaKey;
  206. m_timestamp = webEvent.timestamp();
  207. // PlatformGestureEvent
  208. m_position = webEvent.position();
  209. m_globalPosition = webEvent.globalPosition();
  210. m_area = webEvent.area();
  211. m_deltaX = webEvent.delta().x();
  212. m_deltaY = webEvent.delta().y();
  213. }
  214. };
  215. WebCore::PlatformGestureEvent platform(const WebGestureEvent& webEvent)
  216. {
  217. return WebKit2PlatformGestureEvent(webEvent);
  218. }
  219. #endif
  220. #if ENABLE(TOUCH_EVENTS)
  221. class WebKit2PlatformTouchPoint : public WebCore::PlatformTouchPoint {
  222. public:
  223. WebKit2PlatformTouchPoint(const WebPlatformTouchPoint& webTouchPoint)
  224. {
  225. m_id = webTouchPoint.id();
  226. switch (webTouchPoint.state()) {
  227. case WebPlatformTouchPoint::TouchReleased:
  228. m_state = PlatformTouchPoint::TouchReleased;
  229. break;
  230. case WebPlatformTouchPoint::TouchPressed:
  231. m_state = PlatformTouchPoint::TouchPressed;
  232. break;
  233. case WebPlatformTouchPoint::TouchMoved:
  234. m_state = PlatformTouchPoint::TouchMoved;
  235. break;
  236. case WebPlatformTouchPoint::TouchStationary:
  237. m_state = PlatformTouchPoint::TouchStationary;
  238. break;
  239. case WebPlatformTouchPoint::TouchCancelled:
  240. m_state = PlatformTouchPoint::TouchCancelled;
  241. break;
  242. default:
  243. ASSERT_NOT_REACHED();
  244. }
  245. m_screenPos = webTouchPoint.screenPosition();
  246. m_pos = webTouchPoint.position();
  247. m_radiusX = webTouchPoint.radius().width();
  248. m_radiusY = webTouchPoint.radius().height();
  249. m_force = webTouchPoint.force();
  250. m_rotationAngle = webTouchPoint.rotationAngle();
  251. }
  252. };
  253. class WebKit2PlatformTouchEvent : public WebCore::PlatformTouchEvent {
  254. public:
  255. WebKit2PlatformTouchEvent(const WebTouchEvent& webEvent)
  256. {
  257. // PlatformEvent
  258. switch (webEvent.type()) {
  259. case WebEvent::TouchStart:
  260. m_type = WebCore::PlatformEvent::TouchStart;
  261. break;
  262. case WebEvent::TouchMove:
  263. m_type = WebCore::PlatformEvent::TouchMove;
  264. break;
  265. case WebEvent::TouchEnd:
  266. m_type = WebCore::PlatformEvent::TouchEnd;
  267. break;
  268. case WebEvent::TouchCancel:
  269. m_type = WebCore::PlatformEvent::TouchCancel;
  270. break;
  271. default:
  272. ASSERT_NOT_REACHED();
  273. }
  274. m_modifiers = 0;
  275. if (webEvent.shiftKey())
  276. m_modifiers |= ShiftKey;
  277. if (webEvent.controlKey())
  278. m_modifiers |= CtrlKey;
  279. if (webEvent.altKey())
  280. m_modifiers |= AltKey;
  281. if (webEvent.metaKey())
  282. m_modifiers |= MetaKey;
  283. m_timestamp = webEvent.timestamp();
  284. // PlatformTouchEvent
  285. for (size_t i = 0; i < webEvent.touchPoints().size(); ++i)
  286. m_touchPoints.append(WebKit2PlatformTouchPoint(webEvent.touchPoints().at(i)));
  287. }
  288. };
  289. WebCore::PlatformTouchEvent platform(const WebTouchEvent& webEvent)
  290. {
  291. return WebKit2PlatformTouchEvent(webEvent);
  292. }
  293. #endif
  294. } // namespace WebKit