1/*
2 * Copyright (C) 2014 Igalia S.L.
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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if HAVE(GTK_GESTURES)
29
30#include <WebCore/FloatPoint.h>
31#include <wtf/Noncopyable.h>
32#include <wtf/RunLoop.h>
33#include <wtf/glib/GRefPtr.h>
34
35typedef union _GdkEvent GdkEvent;
36typedef struct _GdkEventTouch GdkEventTouch;
37typedef struct _GdkEventSequence GdkEventSequence;
38typedef struct _GtkGesture GtkGesture;
39
40namespace WebKit {
41
42class GestureControllerClient {
43public:
44 virtual ~GestureControllerClient() = default;
45
46 virtual void tap(GdkEventTouch*) = 0;
47
48 virtual void startDrag(GdkEventTouch*, const WebCore::FloatPoint&) = 0;
49 virtual void drag(GdkEventTouch*, const WebCore::FloatPoint&, const WebCore::FloatPoint&) = 0;
50
51 virtual void swipe(GdkEventTouch*, const WebCore::FloatPoint&) = 0;
52
53 virtual void startZoom(const WebCore::IntPoint& center, double& initialScale, WebCore::IntPoint& initialPoint) = 0;
54 virtual void zoom(double scale, const WebCore::IntPoint& origin) = 0;
55
56 virtual void longPress(GdkEventTouch*) = 0;
57};
58
59class GestureController {
60 WTF_MAKE_NONCOPYABLE(GestureController);
61 WTF_MAKE_FAST_ALLOCATED;
62
63public:
64 GestureController(GtkWidget*, std::unique_ptr<GestureControllerClient>&&);
65
66 bool isProcessingGestures() const;
67 bool handleEvent(GdkEvent*);
68
69 void reset()
70 {
71 m_dragGesture.reset();
72 m_swipeGesture.reset();
73 m_zoomGesture.reset();
74 m_longpressGesture.reset();
75 }
76
77private:
78 class Gesture {
79 public:
80 void reset();
81 bool isActive() const;
82 void handleEvent(GdkEvent*);
83
84 protected:
85 Gesture(GtkGesture*, GestureControllerClient&);
86
87 GRefPtr<GtkGesture> m_gesture;
88 GestureControllerClient& m_client;
89 };
90
91 class DragGesture final : public Gesture {
92 public:
93 DragGesture(GtkWidget*, GestureControllerClient&);
94
95 private:
96 // Notify that a drag started, allowing to stop kinetic deceleration.
97 void startDrag(GdkEvent*);
98 void handleDrag(GdkEvent*, double x, double y);
99 void handleTap(GdkEvent*);
100 void longPressFired();
101
102 static void begin(DragGesture*, double x, double y, GtkGesture*);
103 static void update(DragGesture*, double x, double y, GtkGesture*);
104 static void end(DragGesture*, GdkEventSequence*, GtkGesture*);
105
106 WebCore::FloatPoint m_start;
107 WebCore::FloatPoint m_offset;
108 RunLoop::Timer<DragGesture> m_longPressTimeout;
109 GRefPtr<GtkGesture> m_longPress;
110 bool m_inDrag { false };
111 };
112
113 class SwipeGesture final : public Gesture {
114 public:
115 SwipeGesture(GtkWidget*, GestureControllerClient&);
116
117 private:
118 void startMomentumScroll(GdkEvent*, double velocityX, double velocityY);
119
120 static void swipe(SwipeGesture*, double velocityX, double velocityY, GtkGesture*);
121 };
122
123 class ZoomGesture final : public Gesture {
124 public:
125 ZoomGesture(GtkWidget*, GestureControllerClient&);
126
127 private:
128 WebCore::IntPoint center() const;
129 void startZoom();
130 void handleZoom();
131
132 static void begin(ZoomGesture*, GdkEventSequence*, GtkGesture*);
133 static void scaleChanged(ZoomGesture*, double scale, GtkGesture*);
134
135 double m_initialScale { 0 };
136 double m_scale { 0 };
137 WebCore::IntPoint m_initialPoint;
138 WebCore::IntPoint m_viewPoint;
139 RunLoop::Timer<ZoomGesture> m_idle;
140 };
141
142 class LongPressGesture final : public Gesture {
143 public:
144 LongPressGesture(GtkWidget*, GestureControllerClient&);
145
146 private:
147 void longPressed(GdkEvent*);
148
149 static void pressed(LongPressGesture*, double x, double y, GtkGesture*);
150 };
151
152 std::unique_ptr<GestureControllerClient> m_client;
153 DragGesture m_dragGesture;
154 SwipeGesture m_swipeGesture;
155 ZoomGesture m_zoomGesture;
156 LongPressGesture m_longpressGesture;
157};
158
159} // namespace WebKit
160
161#endif // HAVE(GTK_GESTURES)
162