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
33namespace WebCore {
34
35const String& PointerEvent::mousePointerType()
36{
37 static NeverDestroyed<const String> mouseType(MAKE_STATIC_STRING_IMPL("mouse"));
38 return mouseType;
39}
40
41const String& PointerEvent::penPointerType()
42{
43 static NeverDestroyed<const String> penType(MAKE_STATIC_STRING_IMPL("pen"));
44 return penType;
45}
46
47const String& PointerEvent::touchPointerType()
48{
49 static NeverDestroyed<const String> touchType(MAKE_STATIC_STRING_IMPL("touch"));
50 return touchType;
51}
52
53static AtomString pointerEventType(const AtomString& 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
74RefPtr<PointerEvent> PointerEvent::create(short button, const MouseEvent& mouseEvent)
75{
76 auto type = pointerEventType(mouseEvent.type());
77 if (type.isEmpty())
78 return nullptr;
79
80 return create(type, button, mouseEvent);
81}
82
83Ref<PointerEvent> PointerEvent::create(const String& type, short button, const MouseEvent& mouseEvent)
84{
85 return adoptRef(*new PointerEvent(type, button, mouseEvent));
86}
87
88Ref<PointerEvent> PointerEvent::create(const String& type, PointerID pointerId, const String& pointerType, IsPrimary isPrimary)
89{
90 return adoptRef(*new PointerEvent(type, pointerId, pointerType, isPrimary));
91}
92
93PointerEvent::PointerEvent() = default;
94
95PointerEvent::PointerEvent(const AtomString& type, Init&& initializer)
96 : MouseEvent(type, initializer)
97 , m_pointerId(initializer.pointerId)
98 , m_width(initializer.width)
99 , m_height(initializer.height)
100 , m_pressure(initializer.pressure)
101 , m_tangentialPressure(initializer.tangentialPressure)
102 , m_tiltX(initializer.tiltX)
103 , m_tiltY(initializer.tiltY)
104 , m_twist(initializer.twist)
105 , m_pointerType(initializer.pointerType)
106 , m_isPrimary(initializer.isPrimary)
107{
108}
109
110PointerEvent::PointerEvent(const AtomString& type, short button, const MouseEvent& mouseEvent)
111 : MouseEvent(type, typeCanBubble(type), typeIsCancelable(type), typeIsComposed(type), mouseEvent.view(), mouseEvent.detail(), mouseEvent.screenLocation(), { mouseEvent.clientX(), mouseEvent.clientY() }, mouseEvent.modifierKeys(), button, mouseEvent.buttons(), mouseEvent.syntheticClickType(), mouseEvent.relatedTarget())
112 , m_isPrimary(true)
113{
114}
115
116PointerEvent::PointerEvent(const AtomString& type, PointerID pointerId, const String& pointerType, IsPrimary isPrimary)
117 : MouseEvent(type, typeCanBubble(type), typeIsCancelable(type), typeIsComposed(type), nullptr, 0, { }, { }, { }, 0, 0, 0, nullptr)
118 , m_pointerId(pointerId)
119 , m_pointerType(pointerType)
120 , m_isPrimary(isPrimary == IsPrimary::Yes)
121{
122}
123
124PointerEvent::~PointerEvent() = default;
125
126EventInterface PointerEvent::eventInterface() const
127{
128 return PointerEventInterfaceType;
129}
130
131} // namespace WebCore
132
133#endif // ENABLE(POINTER_EVENTS)
134