| 1 | /* |
| 2 | * Copyright (C) 2018 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. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "PointerEvent.h" |
| 28 | |
| 29 | #if ENABLE(POINTER_EVENTS) |
| 30 | |
| 31 | #import "EventNames.h" |
| 32 | |
| 33 | namespace WebCore { |
| 34 | |
| 35 | const String& PointerEvent::mousePointerType() |
| 36 | { |
| 37 | static NeverDestroyed<const String> mouseType(MAKE_STATIC_STRING_IMPL("mouse" )); |
| 38 | return mouseType; |
| 39 | } |
| 40 | |
| 41 | const String& PointerEvent::penPointerType() |
| 42 | { |
| 43 | static NeverDestroyed<const String> penType(MAKE_STATIC_STRING_IMPL("pen" )); |
| 44 | return penType; |
| 45 | } |
| 46 | |
| 47 | const String& PointerEvent::touchPointerType() |
| 48 | { |
| 49 | static NeverDestroyed<const String> touchType(MAKE_STATIC_STRING_IMPL("touch" )); |
| 50 | return touchType; |
| 51 | } |
| 52 | |
| 53 | static AtomicString pointerEventType(const AtomicString& mouseEventType) |
| 54 | { |
| 55 | auto& names = eventNames(); |
| 56 | if (mouseEventType == names.mousedownEvent) |
| 57 | return names.pointerdownEvent; |
| 58 | if (mouseEventType == names.mouseoverEvent) |
| 59 | return names.pointeroverEvent; |
| 60 | if (mouseEventType == names.mouseenterEvent) |
| 61 | return names.pointerenterEvent; |
| 62 | if (mouseEventType == names.mousemoveEvent) |
| 63 | return names.pointermoveEvent; |
| 64 | if (mouseEventType == names.mouseleaveEvent) |
| 65 | return names.pointerleaveEvent; |
| 66 | if (mouseEventType == names.mouseoutEvent) |
| 67 | return names.pointeroutEvent; |
| 68 | if (mouseEventType == names.mouseupEvent) |
| 69 | return names.pointerupEvent; |
| 70 | |
| 71 | return nullAtom(); |
| 72 | } |
| 73 | |
| 74 | RefPtr<PointerEvent> PointerEvent::create(const MouseEvent& mouseEvent) |
| 75 | { |
| 76 | auto type = pointerEventType(mouseEvent.type()); |
| 77 | if (type.isEmpty()) |
| 78 | return nullptr; |
| 79 | |
| 80 | auto isEnterOrLeave = type == eventNames().pointerenterEvent || type == eventNames().pointerleaveEvent; |
| 81 | auto canBubble = isEnterOrLeave ? CanBubble::No : CanBubble::Yes; |
| 82 | auto isCancelable = isEnterOrLeave ? IsCancelable::No : IsCancelable::Yes; |
| 83 | auto isComposed = isEnterOrLeave ? IsComposed::No : IsComposed::Yes; |
| 84 | return adoptRef(*new PointerEvent(type, canBubble, isCancelable, isComposed, mouseEvent)); |
| 85 | } |
| 86 | |
| 87 | Ref<PointerEvent> PointerEvent::create(const String& type, PointerID pointerId, const String& pointerType, IsPrimary isPrimary) |
| 88 | { |
| 89 | return adoptRef(*new PointerEvent(type, CanBubble::Yes, IsCancelable::No, IsComposed::Yes, pointerId, pointerType, isPrimary)); |
| 90 | } |
| 91 | |
| 92 | PointerEvent::PointerEvent() = default; |
| 93 | |
| 94 | PointerEvent::PointerEvent(const AtomicString& type, Init&& initializer) |
| 95 | : MouseEvent(type, initializer) |
| 96 | , m_pointerId(initializer.pointerId) |
| 97 | , m_width(initializer.width) |
| 98 | , m_height(initializer.height) |
| 99 | , m_pressure(initializer.pressure) |
| 100 | , m_tangentialPressure(initializer.tangentialPressure) |
| 101 | , m_tiltX(initializer.tiltX) |
| 102 | , m_tiltY(initializer.tiltY) |
| 103 | , m_twist(initializer.twist) |
| 104 | , m_pointerType(initializer.pointerType) |
| 105 | , m_isPrimary(initializer.isPrimary) |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | PointerEvent::PointerEvent(const AtomicString& type, CanBubble canBubble, IsCancelable isCancelable, IsComposed isComposed, const MouseEvent& mouseEvent) |
| 110 | : MouseEvent(type, canBubble, isCancelable, isComposed, mouseEvent.view(), mouseEvent.detail(), mouseEvent.screenLocation(), { mouseEvent.clientX(), mouseEvent.clientY() }, mouseEvent.modifierKeys(), mouseEvent.button(), mouseEvent.buttons(), mouseEvent.syntheticClickType(), mouseEvent.relatedTarget()) |
| 111 | , m_isPrimary(true) |
| 112 | { |
| 113 | } |
| 114 | |
| 115 | PointerEvent::PointerEvent(const AtomicString& type, CanBubble canBubble, IsCancelable isCancelable, IsComposed isComposed, PointerID pointerId, const String& pointerType, IsPrimary isPrimary) |
| 116 | : MouseEvent(type, canBubble, isCancelable, isComposed, nullptr, 0, { }, { }, { }, 0, 0, 0, nullptr) |
| 117 | , m_pointerId(pointerId) |
| 118 | , m_pointerType(pointerType) |
| 119 | , m_isPrimary(isPrimary == IsPrimary::Yes) |
| 120 | { |
| 121 | } |
| 122 | |
| 123 | PointerEvent::~PointerEvent() = default; |
| 124 | |
| 125 | EventInterface PointerEvent::eventInterface() const |
| 126 | { |
| 127 | return PointerEventInterfaceType; |
| 128 | } |
| 129 | |
| 130 | } // namespace WebCore |
| 131 | |
| 132 | #endif // ENABLE(POINTER_EVENTS) |
| 133 | |