WebEventFactory.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (C) 2011 Samsung Electronics
  3. * Copyright (C) 2012 Intel Corporation
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  18. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  24. * THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "config.h"
  27. #include "WebEventFactory.h"
  28. #include "EflKeyboardUtilities.h"
  29. #include <WebCore/AffineTransform.h>
  30. #include <WebCore/Scrollbar.h>
  31. using namespace WebCore;
  32. namespace WebKit {
  33. enum {
  34. VerticalScrollDirection = 0,
  35. HorizontalScrollDirection = 1
  36. };
  37. enum {
  38. LeftButton = 1,
  39. MiddleButton = 2,
  40. RightButton = 3
  41. };
  42. static const char keyPadPrefix[] = "KP_";
  43. static inline WebEvent::Modifiers modifiersForEvent(const Evas_Modifier* modifiers)
  44. {
  45. unsigned result = 0;
  46. if (evas_key_modifier_is_set(modifiers, "Shift"))
  47. result |= WebEvent::ShiftKey;
  48. if (evas_key_modifier_is_set(modifiers, "Control"))
  49. result |= WebEvent::ControlKey;
  50. if (evas_key_modifier_is_set(modifiers, "Alt"))
  51. result |= WebEvent::AltKey;
  52. if (evas_key_modifier_is_set(modifiers, "Meta"))
  53. result |= WebEvent::MetaKey;
  54. return static_cast<WebEvent::Modifiers>(result);
  55. }
  56. static inline WebMouseEvent::Button buttonForEvent(int button)
  57. {
  58. if (button == LeftButton)
  59. return WebMouseEvent::LeftButton;
  60. if (button == MiddleButton)
  61. return WebMouseEvent::MiddleButton;
  62. if (button == RightButton)
  63. return WebMouseEvent::RightButton;
  64. return WebMouseEvent::NoButton;
  65. }
  66. static inline int clickCountForEvent(const Evas_Button_Flags flags)
  67. {
  68. if (flags & EVAS_BUTTON_TRIPLE_CLICK)
  69. return 3;
  70. if (flags & EVAS_BUTTON_DOUBLE_CLICK)
  71. return 2;
  72. return 1;
  73. }
  74. static inline double convertMillisecondToSecond(unsigned timestamp)
  75. {
  76. return static_cast<double>(timestamp) / 1000;
  77. }
  78. WebMouseEvent WebEventFactory::createWebMouseEvent(const Evas_Event_Mouse_Down* event, const AffineTransform& toWebContent, const AffineTransform& toDeviceScreen)
  79. {
  80. IntPoint pos(event->canvas.x, event->canvas.y);
  81. return WebMouseEvent(WebEvent::MouseDown,
  82. buttonForEvent(event->button),
  83. toWebContent.mapPoint(pos),
  84. toDeviceScreen.mapPoint(pos),
  85. 0 /* deltaX */,
  86. 0 /* deltaY */,
  87. 0 /* deltaZ */,
  88. clickCountForEvent(event->flags),
  89. modifiersForEvent(event->modifiers),
  90. convertMillisecondToSecond(event->timestamp));
  91. }
  92. WebMouseEvent WebEventFactory::createWebMouseEvent(const Evas_Event_Mouse_Up* event, const AffineTransform& toWebContent, const AffineTransform& toDeviceScreen)
  93. {
  94. IntPoint pos(event->canvas.x, event->canvas.y);
  95. return WebMouseEvent(WebEvent::MouseUp,
  96. buttonForEvent(event->button),
  97. toWebContent.mapPoint(pos),
  98. toDeviceScreen.mapPoint(pos),
  99. 0 /* deltaX */,
  100. 0 /* deltaY */,
  101. 0 /* deltaZ */,
  102. clickCountForEvent(event->flags),
  103. modifiersForEvent(event->modifiers),
  104. convertMillisecondToSecond(event->timestamp));
  105. }
  106. WebMouseEvent WebEventFactory::createWebMouseEvent(const Evas_Event_Mouse_Move* event, const AffineTransform& toWebContent, const AffineTransform& toDeviceScreen)
  107. {
  108. IntPoint pos(event->cur.canvas.x, event->cur.canvas.y);
  109. return WebMouseEvent(WebEvent::MouseMove,
  110. buttonForEvent(event->buttons),
  111. toWebContent.mapPoint(pos),
  112. toDeviceScreen.mapPoint(pos),
  113. 0 /* deltaX */,
  114. 0 /* deltaY */,
  115. 0 /* deltaZ */,
  116. 0 /* clickCount */,
  117. modifiersForEvent(event->modifiers),
  118. convertMillisecondToSecond(event->timestamp));
  119. }
  120. WebWheelEvent WebEventFactory::createWebWheelEvent(const Evas_Event_Mouse_Wheel* event, const AffineTransform& toWebContent, const AffineTransform& toDeviceScreen)
  121. {
  122. float deltaX = 0;
  123. float deltaY = 0;
  124. float wheelTicksX = 0;
  125. float wheelTicksY = 0;
  126. // A negative z value means (in EFL) that we are scrolling down, so we need
  127. // to invert the value.
  128. if (event->direction == VerticalScrollDirection) {
  129. deltaX = 0;
  130. deltaY = - event->z;
  131. } else if (event->direction == HorizontalScrollDirection) {
  132. deltaX = - event->z;
  133. deltaY = 0;
  134. }
  135. wheelTicksX = deltaX;
  136. wheelTicksY = deltaY;
  137. deltaX *= static_cast<float>(Scrollbar::pixelsPerLineStep());
  138. deltaY *= static_cast<float>(Scrollbar::pixelsPerLineStep());
  139. IntPoint pos(event->canvas.x, event->canvas.y);
  140. return WebWheelEvent(WebEvent::Wheel,
  141. toWebContent.mapPoint(pos),
  142. toDeviceScreen.mapPoint(pos),
  143. FloatSize(deltaX, deltaY),
  144. FloatSize(wheelTicksX, wheelTicksY),
  145. WebWheelEvent::ScrollByPixelWheelEvent,
  146. modifiersForEvent(event->modifiers),
  147. convertMillisecondToSecond(event->timestamp));
  148. }
  149. WebKeyboardEvent WebEventFactory::createWebKeyboardEvent(const Evas_Event_Key_Down* event)
  150. {
  151. const String keyName(event->key);
  152. return WebKeyboardEvent(WebEvent::KeyDown,
  153. String::fromUTF8(event->string),
  154. String::fromUTF8(event->string),
  155. keyIdentifierForEvasKeyName(keyName),
  156. windowsKeyCodeForEvasKeyName(keyName),
  157. 0 /* FIXME: nativeVirtualKeyCode */,
  158. 0 /* macCharCode */,
  159. false /* FIXME: isAutoRepeat */,
  160. keyName.startsWith(keyPadPrefix),
  161. false /* isSystemKey */,
  162. modifiersForEvent(event->modifiers),
  163. convertMillisecondToSecond(event->timestamp));
  164. }
  165. WebKeyboardEvent WebEventFactory::createWebKeyboardEvent(const Evas_Event_Key_Up* event)
  166. {
  167. const String keyName(event->key);
  168. return WebKeyboardEvent(WebEvent::KeyUp,
  169. String::fromUTF8(event->string),
  170. String::fromUTF8(event->string),
  171. keyIdentifierForEvasKeyName(keyName),
  172. windowsKeyCodeForEvasKeyName(keyName),
  173. 0 /* FIXME: nativeVirtualKeyCode */,
  174. 0 /* macCharCode */,
  175. false /* FIXME: isAutoRepeat */,
  176. keyName.startsWith(keyPadPrefix),
  177. false /* isSystemKey */,
  178. modifiersForEvent(event->modifiers),
  179. convertMillisecondToSecond(event->timestamp));
  180. }
  181. #if ENABLE(TOUCH_EVENTS)
  182. static inline WebEvent::Type typeForTouchEvent(Ewk_Touch_Event_Type type)
  183. {
  184. if (type == EWK_TOUCH_START)
  185. return WebEvent::TouchStart;
  186. if (type == EWK_TOUCH_MOVE)
  187. return WebEvent::TouchMove;
  188. if (type == EWK_TOUCH_END)
  189. return WebEvent::TouchEnd;
  190. if (type == EWK_TOUCH_CANCEL)
  191. return WebEvent::TouchCancel;
  192. return WebEvent::NoType;
  193. }
  194. WebTouchEvent WebEventFactory::createWebTouchEvent(Ewk_Touch_Event_Type type, const Eina_List* points, const Evas_Modifier* modifiers, const AffineTransform& toWebContent, const AffineTransform& toDeviceScreen, double timestamp)
  195. {
  196. Vector<WebPlatformTouchPoint> touchPoints;
  197. touchPoints.reserveInitialCapacity(eina_list_count(points));
  198. const Eina_List* list;
  199. void* item;
  200. EINA_LIST_FOREACH(points, list, item) {
  201. Ewk_Touch_Point* point = static_cast<Ewk_Touch_Point*>(item);
  202. WebPlatformTouchPoint::TouchPointState state;
  203. switch (point->state) {
  204. case EVAS_TOUCH_POINT_UP:
  205. state = WebPlatformTouchPoint::TouchReleased;
  206. break;
  207. case EVAS_TOUCH_POINT_MOVE:
  208. state = WebPlatformTouchPoint::TouchMoved;
  209. break;
  210. case EVAS_TOUCH_POINT_DOWN:
  211. state = WebPlatformTouchPoint::TouchPressed;
  212. break;
  213. case EVAS_TOUCH_POINT_STILL:
  214. state = WebPlatformTouchPoint::TouchStationary;
  215. break;
  216. case EVAS_TOUCH_POINT_CANCEL:
  217. state = WebPlatformTouchPoint::TouchCancelled;
  218. break;
  219. default:
  220. ASSERT_NOT_REACHED();
  221. continue;
  222. }
  223. IntPoint pos(point->x, point->y);
  224. touchPoints.uncheckedAppend(WebPlatformTouchPoint(point->id, state, toDeviceScreen.mapPoint(pos), toWebContent.mapPoint(pos)));
  225. }
  226. return WebTouchEvent(typeForTouchEvent(type), touchPoints, modifiersForEvent(modifiers), timestamp);
  227. }
  228. #endif
  229. } // namespace WebKit