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#pragma once
27
28#if ENABLE(POINTER_EVENTS)
29
30#include "MouseEvent.h"
31#include "PointerID.h"
32#include <wtf/text/WTFString.h>
33
34#if ENABLE(TOUCH_EVENTS) && PLATFORM(IOS_FAMILY)
35#include "PlatformTouchEventIOS.h"
36#endif
37
38namespace WebCore {
39
40class PointerEvent final : public MouseEvent {
41public:
42 struct Init : MouseEventInit {
43 PointerID pointerId { PointerEvent::defaultMousePointerIdentifier() };
44 double width { 1 };
45 double height { 1 };
46 float pressure { 0 };
47 float tangentialPressure { 0 };
48 long tiltX { 0 };
49 long tiltY { 0 };
50 long twist { 0 };
51 String pointerType { PointerEvent::mousePointerType() };
52 bool isPrimary { false };
53 };
54
55 enum class IsPrimary : uint8_t { No, Yes };
56
57 static Ref<PointerEvent> create(const AtomicString& type, Init&& initializer)
58 {
59 return adoptRef(*new PointerEvent(type, WTFMove(initializer)));
60 }
61
62 static Ref<PointerEvent> createForPointerCapture(const AtomicString& type, const PointerEvent& pointerEvent)
63 {
64 Init initializer;
65 initializer.bubbles = true;
66 initializer.pointerId = pointerEvent.pointerId();
67 initializer.isPrimary = pointerEvent.isPrimary();
68 initializer.pointerType = pointerEvent.pointerType();
69 return adoptRef(*new PointerEvent(type, WTFMove(initializer)));
70 }
71
72 static Ref<PointerEvent> createForBindings()
73 {
74 return adoptRef(*new PointerEvent);
75 }
76
77 static RefPtr<PointerEvent> create(const MouseEvent&);
78 static Ref<PointerEvent> create(const String& type, PointerID, const String& pointerType, IsPrimary = IsPrimary::No);
79
80#if ENABLE(TOUCH_EVENTS) && PLATFORM(IOS_FAMILY)
81 static Ref<PointerEvent> create(const PlatformTouchEvent&, unsigned touchIndex, bool isPrimary, Ref<WindowProxy>&&);
82 static Ref<PointerEvent> create(const String& type, const PlatformTouchEvent&, unsigned touchIndex, bool isPrimary, Ref<WindowProxy>&&);
83#endif
84
85 static const String& mousePointerType();
86 static const String& penPointerType();
87 static const String& touchPointerType();
88 static PointerID defaultMousePointerIdentifier() { return 1; }
89
90 virtual ~PointerEvent();
91
92 PointerID pointerId() const { return m_pointerId; }
93 double width() const { return m_width; }
94 double height() const { return m_height; }
95 float pressure() const { return m_pressure; }
96 float tangentialPressure() const { return m_tangentialPressure; }
97 long tiltX() const { return m_tiltX; }
98 long tiltY() const { return m_tiltY; }
99 long twist() const { return m_twist; }
100 String pointerType() const { return m_pointerType; }
101 bool isPrimary() const { return m_isPrimary; }
102
103 bool isPointerEvent() const final { return true; }
104
105 EventInterface eventInterface() const override;
106
107private:
108 PointerEvent();
109 PointerEvent(const AtomicString&, Init&&);
110 PointerEvent(const AtomicString& type, CanBubble, IsCancelable, IsComposed, const MouseEvent&);
111 PointerEvent(const AtomicString& type, CanBubble, IsCancelable, IsComposed, PointerID, const String& pointerType, IsPrimary);
112#if ENABLE(TOUCH_EVENTS) && PLATFORM(IOS_FAMILY)
113 PointerEvent(const AtomicString& type, const PlatformTouchEvent&, IsCancelable isCancelable, unsigned touchIndex, bool isPrimary, Ref<WindowProxy>&&);
114#endif
115
116 PointerID m_pointerId { PointerEvent::defaultMousePointerIdentifier() };
117 double m_width { 1 };
118 double m_height { 1 };
119 float m_pressure { 0 };
120 float m_tangentialPressure { 0 };
121 long m_tiltX { 0 };
122 long m_tiltY { 0 };
123 long m_twist { 0 };
124 String m_pointerType { PointerEvent::mousePointerType() };
125 bool m_isPrimary { false };
126};
127
128} // namespace WebCore
129
130SPECIALIZE_TYPE_TRAITS_EVENT(PointerEvent)
131
132#endif // ENABLE(POINTER_EVENTS)
133