WebEventFactoryManx.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (C) 2010 Apple Inc. All rights reserved.
  3. * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
  4. * Copyright (C) 2012 Sony Computer Entertainment Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  19. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  25. * THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "config.h"
  28. #include "WebEventFactoryManx.h"
  29. #include <WebCore/FloatPoint.h>
  30. #include <WebCore/IntPoint.h>
  31. #include <WebCore/PlatformKeyboardEvent.h>
  32. #include <wtf/ASCIICType.h>
  33. #include <wtf/CurrentTime.h>
  34. using namespace WebCore;
  35. namespace WebKit {
  36. WebEvent::Type webEventTypeForEvent(const Manx::KeyboardEvent& event)
  37. {
  38. switch (event.m_type) {
  39. case Manx::KeyboardEvent::KeyDown: return WebEvent::KeyDown;
  40. case Manx::KeyboardEvent::KeyUp: return WebEvent::KeyUp;
  41. case Manx::KeyboardEvent::RawKeyDown: return WebEvent::RawKeyDown;
  42. case Manx::KeyboardEvent::Char: return WebEvent::Char;
  43. }
  44. ASSERT_NOT_REACHED();
  45. return WebEvent::KeyDown;
  46. }
  47. WebEvent::Type webEventTypeForEvent(const Manx::MouseEvent& mouseEvent)
  48. {
  49. switch (mouseEvent.m_type) {
  50. case Manx::MouseEvent::MouseDown: return WebEvent::MouseDown;
  51. case Manx::MouseEvent::MouseUp: return WebEvent::MouseUp;
  52. case Manx::MouseEvent::MouseMove: return WebEvent::MouseMove;
  53. }
  54. ASSERT_NOT_REACHED();
  55. return WebEvent::MouseDown;
  56. }
  57. WebMouseEvent::Button buttonForEvent(const Manx::MouseEvent& mouseEvent)
  58. {
  59. switch (mouseEvent.m_button) {
  60. case Manx::MouseEvent::NoButton: return WebMouseEvent::NoButton;
  61. case Manx::MouseEvent::LeftButton: return WebMouseEvent::LeftButton;
  62. case Manx::MouseEvent::MiddleButton: return WebMouseEvent::MiddleButton;
  63. case Manx::MouseEvent::RightButton: return WebMouseEvent::RightButton;
  64. }
  65. ASSERT_NOT_REACHED();
  66. return WebMouseEvent::LeftButton;
  67. }
  68. WebEvent::Modifiers modifiersForEvent(const Manx::KeyboardEvent& event)
  69. {
  70. int modifiers = 0;
  71. if (event.m_shiftKey)
  72. modifiers |= WebEvent::ShiftKey;
  73. if (event.m_controlKey)
  74. modifiers |= WebEvent::ControlKey;
  75. if (event.m_altKey)
  76. modifiers |= WebEvent::AltKey;
  77. if (event.m_metaKey)
  78. modifiers |= WebEvent::MetaKey;
  79. if (event.m_capsLockKey)
  80. modifiers |= WebEvent::CapsLockKey;
  81. return static_cast<WebEvent::Modifiers>(modifiers);
  82. }
  83. WebEvent::Modifiers modifiersForEvent(const Manx::MouseEvent& mouseEvent)
  84. {
  85. int modifiers = 0;
  86. if (mouseEvent.m_shiftKey)
  87. modifiers |= WebEvent::ShiftKey;
  88. if (mouseEvent.m_controlKey)
  89. modifiers |= WebEvent::ControlKey;
  90. if (mouseEvent.m_altKey)
  91. modifiers |= WebEvent::AltKey;
  92. if (mouseEvent.m_metaKey)
  93. modifiers |= WebEvent::MetaKey;
  94. return static_cast<WebEvent::Modifiers>(modifiers);
  95. }
  96. WebEvent::Modifiers modifiersForEvent(const Manx::WheelEvent& wheelEvent)
  97. {
  98. int modifiers = 0;
  99. if (wheelEvent.m_shiftKey)
  100. modifiers |= WebEvent::ShiftKey;
  101. if (wheelEvent.m_controlKey)
  102. modifiers |= WebEvent::ControlKey;
  103. if (wheelEvent.m_altKey)
  104. modifiers |= WebEvent::AltKey;
  105. if (wheelEvent.m_metaKey)
  106. modifiers |= WebEvent::MetaKey;
  107. return static_cast<WebEvent::Modifiers>(modifiers);
  108. }
  109. WebKeyboardEvent WebEventFactory::createWebKeyboardEvent(const Manx::KeyboardEvent& event)
  110. {
  111. WebEvent::Type type = webEventTypeForEvent(event);
  112. const String text = WTF::String::fromUTF8(event.m_text);
  113. const String unmodifiedText = WTF::String::fromUTF8(event.m_unmodifiedText);
  114. bool isAutoRepeat = event.m_isAutoRepeat;
  115. bool isSystemKey = event.m_isSystemKey;
  116. bool isKeypad = event.m_isKeypad;
  117. const String keyIdentifier(event.m_keyIdentifier);
  118. int windowsVirtualKeyCode = event.m_windowsVirtualKeyCode;
  119. int nativeVirtualKeyCode = event.m_nativeVirtualKeyCode;
  120. int macCharCode = event.m_macCharCode;
  121. WebEvent::Modifiers modifiers = modifiersForEvent(event);
  122. double timestamp = currentTime();
  123. return WebKeyboardEvent(type, text, unmodifiedText, keyIdentifier, windowsVirtualKeyCode, nativeVirtualKeyCode, macCharCode, isAutoRepeat, isKeypad, isSystemKey, modifiers, timestamp);
  124. }
  125. WebMouseEvent WebEventFactory::createWebMouseEvent(const Manx::MouseEvent& mouseEvent)
  126. {
  127. static WebCore::IntPoint lastPosition = WebCore::IntPoint();
  128. WebEvent::Type type = webEventTypeForEvent(mouseEvent);
  129. WebMouseEvent::Button button = buttonForEvent(mouseEvent);
  130. const IntPoint position(mouseEvent.m_x, mouseEvent.m_y);
  131. const IntPoint globalPosition = position;
  132. int clickCount = mouseEvent.m_clickCount;
  133. WebEvent::Modifiers modifiers = modifiersForEvent(mouseEvent);
  134. double timestamp = currentTime();
  135. float deltaX = position.x() - lastPosition.x();
  136. float deltaY = position.y() - lastPosition.y();
  137. lastPosition = position;
  138. return WebMouseEvent(type, button, position, globalPosition, deltaX, deltaY, 0, clickCount, modifiers, timestamp);
  139. }
  140. WebWheelEvent WebEventFactory::createWebWheelEvent(const Manx::WheelEvent& wheelEvent)
  141. {
  142. const IntPoint position(wheelEvent.m_x, wheelEvent.m_y);
  143. const IntPoint globalPosition = position;
  144. FloatSize delta(wheelEvent.m_deltaX, wheelEvent.m_deltaY);
  145. FloatSize wheelTicks(wheelEvent.m_wheelTicksX, wheelEvent.m_wheelTicksY);
  146. WebEvent::Modifiers modifiers = modifiersForEvent(wheelEvent);
  147. double timestamp = currentTime();
  148. return WebWheelEvent(WebEvent::Wheel, position, globalPosition, delta, wheelTicks, WebWheelEvent::ScrollByPixelWheelEvent, modifiers, timestamp);
  149. }
  150. } // namespace WebKit