2013-01-17 20:02:28 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
* Copyright (C) 2012, One Laptop Per Child.
|
|
|
|
* Copyright (C) 2014, Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author(s): Carlos Garnacho <carlosg@gnome.org>
|
|
|
|
*/
|
2014-04-09 16:28:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gtkgestureswipe
|
|
|
|
* @Short_description: Swipe gesture
|
|
|
|
* @Title: GtkGestureSwipe
|
|
|
|
*
|
|
|
|
* #GtkGestureSwipe is a #GtkGesture implementation able to recognize
|
|
|
|
* swipes, after a press/move/.../move/release sequence happens, the
|
2014-05-20 02:16:03 +00:00
|
|
|
* #GtkGestureSwipe::swipe signal will be emitted, providing the velocity
|
2014-04-09 16:28:27 +00:00
|
|
|
* and directionality of the sequence at the time it was lifted.
|
|
|
|
*
|
|
|
|
* If the velocity is desired in intermediate points,
|
|
|
|
* gtk_gesture_swipe_get_velocity() can be called on eg. a
|
2014-04-10 11:45:00 +00:00
|
|
|
* #GtkGesture::update handler.
|
2014-04-09 16:28:27 +00:00
|
|
|
*
|
2014-05-20 02:16:03 +00:00
|
|
|
* All velocities are reported in pixels/sec units.
|
2014-04-09 16:28:27 +00:00
|
|
|
*/
|
|
|
|
|
2013-01-17 20:02:28 +00:00
|
|
|
#include "config.h"
|
2014-05-06 02:40:18 +00:00
|
|
|
#include "gtkgestureswipe.h"
|
|
|
|
#include "gtkgestureswipeprivate.h"
|
2014-05-06 16:22:57 +00:00
|
|
|
#include "gtkgestureprivate.h"
|
2013-01-17 20:02:28 +00:00
|
|
|
#include "gtkmarshalers.h"
|
2015-09-12 13:13:00 +00:00
|
|
|
#include "gtkintl.h"
|
2019-06-02 21:07:27 +00:00
|
|
|
#include "gtkmarshalers.h"
|
2013-01-17 20:02:28 +00:00
|
|
|
|
|
|
|
#define CAPTURE_THRESHOLD_MS 150
|
|
|
|
|
|
|
|
typedef struct _GtkGestureSwipePrivate GtkGestureSwipePrivate;
|
|
|
|
typedef struct _EventData EventData;
|
|
|
|
|
|
|
|
struct _EventData
|
|
|
|
{
|
|
|
|
guint32 evtime;
|
2017-12-26 16:30:37 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
2013-01-17 20:02:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkGestureSwipePrivate
|
|
|
|
{
|
|
|
|
GArray *events;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
SWIPE,
|
|
|
|
N_SIGNALS
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint signals[N_SIGNALS] = { 0 };
|
|
|
|
|
2014-03-21 18:45:27 +00:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GtkGestureSwipe, gtk_gesture_swipe, GTK_TYPE_GESTURE_SINGLE)
|
2013-01-17 20:02:28 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_swipe_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GtkGestureSwipePrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_gesture_swipe_get_instance_private (GTK_GESTURE_SWIPE (object));
|
|
|
|
g_array_free (priv->events, TRUE);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_gesture_swipe_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2015-07-24 11:03:09 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_gesture_swipe_filter_event (GtkEventController *controller,
|
2020-02-16 16:09:02 +00:00
|
|
|
GdkEvent *event)
|
2015-07-24 11:03:09 +00:00
|
|
|
{
|
|
|
|
/* Let touchpad swipe events go through, only if they match n-points */
|
2017-08-25 14:47:11 +00:00
|
|
|
if (gdk_event_get_event_type (event) == GDK_TOUCHPAD_SWIPE)
|
2015-07-24 11:03:09 +00:00
|
|
|
{
|
|
|
|
guint n_points;
|
2017-08-26 14:53:01 +00:00
|
|
|
guint n_fingers;
|
2015-07-24 11:03:09 +00:00
|
|
|
|
|
|
|
g_object_get (G_OBJECT (controller), "n-points", &n_points, NULL);
|
|
|
|
|
2020-02-18 03:11:56 +00:00
|
|
|
n_fingers = gdk_touchpad_event_get_n_fingers (event);
|
2017-08-26 14:53:01 +00:00
|
|
|
|
|
|
|
if (n_fingers == n_points)
|
2015-07-24 11:03:09 +00:00
|
|
|
return FALSE;
|
|
|
|
else
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GTK_EVENT_CONTROLLER_CLASS (gtk_gesture_swipe_parent_class)->filter_event (controller, event);
|
|
|
|
}
|
|
|
|
|
2013-01-17 20:02:28 +00:00
|
|
|
static void
|
|
|
|
_gtk_gesture_swipe_clear_backlog (GtkGestureSwipe *gesture,
|
|
|
|
guint32 evtime)
|
|
|
|
{
|
|
|
|
GtkGestureSwipePrivate *priv;
|
2020-07-24 13:54:49 +00:00
|
|
|
int i, length = 0;
|
2013-01-17 20:02:28 +00:00
|
|
|
|
|
|
|
priv = gtk_gesture_swipe_get_instance_private (gesture);
|
|
|
|
|
2020-07-24 13:54:49 +00:00
|
|
|
for (i = 0; i < (int) priv->events->len; i++)
|
2013-01-17 20:02:28 +00:00
|
|
|
{
|
|
|
|
EventData *data;
|
|
|
|
|
|
|
|
data = &g_array_index (priv->events, EventData, i);
|
|
|
|
|
|
|
|
if (data->evtime >= evtime - CAPTURE_THRESHOLD_MS)
|
|
|
|
{
|
|
|
|
length = i - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
g_array_remove_range (priv->events, 0, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_swipe_update (GtkGesture *gesture,
|
|
|
|
GdkEventSequence *sequence)
|
|
|
|
{
|
|
|
|
GtkGestureSwipe *swipe = GTK_GESTURE_SWIPE (gesture);
|
|
|
|
GtkGestureSwipePrivate *priv;
|
|
|
|
EventData new;
|
2020-07-24 20:32:16 +00:00
|
|
|
double x, y;
|
2013-01-17 20:02:28 +00:00
|
|
|
|
|
|
|
priv = gtk_gesture_swipe_get_instance_private (swipe);
|
2014-05-06 16:22:57 +00:00
|
|
|
_gtk_gesture_get_last_update_time (gesture, sequence, &new.evtime);
|
2013-01-17 20:02:28 +00:00
|
|
|
gtk_gesture_get_point (gesture, sequence, &x, &y);
|
|
|
|
|
2017-12-26 16:30:37 +00:00
|
|
|
new.x = x;
|
|
|
|
new.y = y;
|
2013-01-17 20:02:28 +00:00
|
|
|
|
|
|
|
_gtk_gesture_swipe_clear_backlog (swipe, new.evtime);
|
|
|
|
g_array_append_val (priv->events, new);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_gtk_gesture_swipe_calculate_velocity (GtkGestureSwipe *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double *velocity_x,
|
|
|
|
double *velocity_y)
|
2013-01-17 20:02:28 +00:00
|
|
|
{
|
|
|
|
GtkGestureSwipePrivate *priv;
|
2014-04-08 19:19:46 +00:00
|
|
|
GdkEventSequence *sequence;
|
|
|
|
guint32 evtime, diff_time;
|
2013-01-17 20:02:28 +00:00
|
|
|
EventData *start, *end;
|
2020-07-24 20:32:16 +00:00
|
|
|
double diff_x, diff_y;
|
2013-01-17 20:02:28 +00:00
|
|
|
|
|
|
|
priv = gtk_gesture_swipe_get_instance_private (gesture);
|
|
|
|
*velocity_x = *velocity_y = 0;
|
|
|
|
|
2014-04-08 19:19:46 +00:00
|
|
|
sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
|
2014-05-06 16:22:57 +00:00
|
|
|
_gtk_gesture_get_last_update_time (GTK_GESTURE (gesture), sequence, &evtime);
|
2014-04-08 19:19:46 +00:00
|
|
|
_gtk_gesture_swipe_clear_backlog (gesture, evtime);
|
|
|
|
|
2013-01-17 20:02:28 +00:00
|
|
|
if (priv->events->len == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
start = &g_array_index (priv->events, EventData, 0);
|
|
|
|
end = &g_array_index (priv->events, EventData, priv->events->len - 1);
|
|
|
|
|
|
|
|
diff_time = end->evtime - start->evtime;
|
2017-12-26 16:30:37 +00:00
|
|
|
diff_x = end->x - start->x;
|
|
|
|
diff_y = end->y - start->y;
|
2013-01-17 20:02:28 +00:00
|
|
|
|
|
|
|
if (diff_time == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Velocity in pixels/sec */
|
|
|
|
*velocity_x = diff_x * 1000 / diff_time;
|
|
|
|
*velocity_y = diff_y * 1000 / diff_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_swipe_end (GtkGesture *gesture,
|
|
|
|
GdkEventSequence *sequence)
|
|
|
|
{
|
|
|
|
GtkGestureSwipe *swipe = GTK_GESTURE_SWIPE (gesture);
|
|
|
|
GtkGestureSwipePrivate *priv;
|
2020-07-24 20:32:16 +00:00
|
|
|
double velocity_x, velocity_y;
|
2014-04-08 19:19:46 +00:00
|
|
|
GdkEventSequence *seq;
|
|
|
|
|
|
|
|
seq = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
|
2013-01-17 20:02:28 +00:00
|
|
|
|
2014-04-08 19:19:46 +00:00
|
|
|
if (gtk_gesture_get_sequence_state (gesture, seq) == GTK_EVENT_SEQUENCE_DENIED)
|
2014-03-25 12:45:17 +00:00
|
|
|
return;
|
|
|
|
|
2014-04-17 11:05:11 +00:00
|
|
|
if (gtk_gesture_is_active (gesture))
|
|
|
|
return;
|
|
|
|
|
2013-01-17 20:02:28 +00:00
|
|
|
priv = gtk_gesture_swipe_get_instance_private (swipe);
|
|
|
|
_gtk_gesture_swipe_calculate_velocity (swipe, &velocity_x, &velocity_y);
|
|
|
|
g_signal_emit (gesture, signals[SWIPE], 0, velocity_x, velocity_y);
|
|
|
|
|
|
|
|
if (priv->events->len > 0)
|
|
|
|
g_array_remove_range (priv->events, 0, priv->events->len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_swipe_class_init (GtkGestureSwipeClass *klass)
|
|
|
|
{
|
|
|
|
GtkGestureClass *gesture_class = GTK_GESTURE_CLASS (klass);
|
2015-07-24 11:03:09 +00:00
|
|
|
GtkEventControllerClass *event_controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);
|
2013-01-17 20:02:28 +00:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->finalize = gtk_gesture_swipe_finalize;
|
|
|
|
|
2015-07-24 11:03:09 +00:00
|
|
|
event_controller_class->filter_event = gtk_gesture_swipe_filter_event;
|
|
|
|
|
2013-01-17 20:02:28 +00:00
|
|
|
gesture_class->update = gtk_gesture_swipe_update;
|
|
|
|
gesture_class->end = gtk_gesture_swipe_end;
|
|
|
|
|
2014-04-09 16:28:27 +00:00
|
|
|
/**
|
2014-04-10 11:45:00 +00:00
|
|
|
* GtkGestureSwipe::swipe:
|
2014-04-09 16:28:27 +00:00
|
|
|
* @gesture: object which received the signal
|
|
|
|
* @velocity_x: velocity in the X axis, in pixels/sec
|
|
|
|
* @velocity_y: velocity in the Y axis, in pixels/sec
|
|
|
|
*
|
|
|
|
* This signal is emitted when the recognized gesture is finished, velocity
|
|
|
|
* and direction are a product of previously recorded events.
|
|
|
|
*/
|
2013-01-17 20:02:28 +00:00
|
|
|
signals[SWIPE] =
|
2015-09-12 13:13:00 +00:00
|
|
|
g_signal_new (I_("swipe"),
|
2013-01-17 20:02:28 +00:00
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GtkGestureSwipeClass, swipe),
|
2019-06-02 21:07:27 +00:00
|
|
|
NULL, NULL,
|
|
|
|
_gtk_marshal_VOID__DOUBLE_DOUBLE,
|
2013-01-17 20:02:28 +00:00
|
|
|
G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
|
2019-06-02 21:07:27 +00:00
|
|
|
g_signal_set_va_marshaller (signals[SWIPE],
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
_gtk_marshal_VOID__DOUBLE_DOUBLEv);
|
2013-01-17 20:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_gesture_swipe_init (GtkGestureSwipe *gesture)
|
|
|
|
{
|
|
|
|
GtkGestureSwipePrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_gesture_swipe_get_instance_private (gesture);
|
|
|
|
priv->events = g_array_new (FALSE, FALSE, sizeof (EventData));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_gesture_swipe_new:
|
|
|
|
*
|
2014-05-20 02:16:03 +00:00
|
|
|
* Returns a newly created #GtkGesture that recognizes swipes.
|
2013-01-17 20:02:28 +00:00
|
|
|
*
|
|
|
|
* Returns: a newly created #GtkGestureSwipe
|
|
|
|
**/
|
|
|
|
GtkGesture *
|
2018-03-08 22:49:41 +00:00
|
|
|
gtk_gesture_swipe_new (void)
|
2013-01-17 20:02:28 +00:00
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_GESTURE_SWIPE,
|
|
|
|
NULL);
|
|
|
|
}
|
2014-04-08 19:19:46 +00:00
|
|
|
|
2014-04-09 16:28:27 +00:00
|
|
|
/**
|
|
|
|
* gtk_gesture_swipe_get_velocity:
|
|
|
|
* @gesture: a #GtkGestureSwipe
|
|
|
|
* @velocity_x: (out): return value for the velocity in the X axis, in pixels/sec
|
|
|
|
* @velocity_y: (out): return value for the velocity in the Y axis, in pixels/sec
|
|
|
|
*
|
|
|
|
* If the gesture is recognized, this function returns %TRUE and fill in
|
|
|
|
* @velocity_x and @velocity_y with the recorded velocity, as per the
|
|
|
|
* last event(s) processed.
|
|
|
|
*
|
|
|
|
* Returns: whether velocity could be calculated
|
|
|
|
**/
|
2014-04-08 19:19:46 +00:00
|
|
|
gboolean
|
|
|
|
gtk_gesture_swipe_get_velocity (GtkGestureSwipe *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double *velocity_x,
|
|
|
|
double *velocity_y)
|
2014-04-08 19:19:46 +00:00
|
|
|
{
|
2020-07-24 20:32:16 +00:00
|
|
|
double vel_x, vel_y;
|
2014-04-08 19:19:46 +00:00
|
|
|
|
2014-05-24 14:16:41 +00:00
|
|
|
g_return_val_if_fail (GTK_IS_GESTURE (gesture), FALSE);
|
2014-04-08 19:19:46 +00:00
|
|
|
|
|
|
|
if (!gtk_gesture_is_recognized (GTK_GESTURE (gesture)))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
_gtk_gesture_swipe_calculate_velocity (gesture, &vel_x, &vel_y);
|
|
|
|
|
|
|
|
if (velocity_x)
|
|
|
|
*velocity_x = vel_x;
|
|
|
|
if (velocity_y)
|
|
|
|
*velocity_y = vel_y;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|