From f0c8e9de4f4f4e70655cd55e89d6dcd1b45977d7 Mon Sep 17 00:00:00 2001 From: Carlos Garnacho Date: Thu, 1 Feb 2018 17:52:40 +0100 Subject: [PATCH] gtk: Add GtkGestureStylus This is a GtkGesture done to deal with stylus events from drawing tablets. Those have a special number of characteristics that extend a regular pointer, so it makes sense to wrap that. --- gtk/gtk.h | 1 + gtk/gtkgesturestylus.c | 330 ++++++++++++++++++++++++++++++++++ gtk/gtkgesturestylus.h | 63 +++++++ gtk/gtkgesturestylusprivate.h | 51 ++++++ gtk/gtkprivate.h | 7 + gtk/gtkwidget.c | 2 +- gtk/meson.build | 2 + po/POTFILES.in | 1 + 8 files changed, 456 insertions(+), 1 deletion(-) create mode 100644 gtk/gtkgesturestylus.c create mode 100644 gtk/gtkgesturestylus.h create mode 100644 gtk/gtkgesturestylusprivate.h diff --git a/gtk/gtk.h b/gtk/gtk.h index b5b2dbd10e..107abd78e4 100644 --- a/gtk/gtk.h +++ b/gtk/gtk.h @@ -117,6 +117,7 @@ #include #include #include +#include #include #include #include diff --git a/gtk/gtkgesturestylus.c b/gtk/gtkgesturestylus.c new file mode 100644 index 0000000000..6ba3bf14a3 --- /dev/null +++ b/gtk/gtkgesturestylus.c @@ -0,0 +1,330 @@ +/* GTK - The GIMP Toolkit + * Copyright (C) 2017-2018, 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 . + * + * Author(s): Carlos Garnacho + */ + +/** + * SECTION:gtkgesturestylus + * @Short_description: Gesture for stylus input + * @Title: GtkGestureStylus + * @See_also: #GtkGesture, #GtkGestureSingle + * + * #GtkGestureStylus is a #GtkGesture implementation specific to stylus + * input. The provided signals just provide the basic information + */ + +#include "config.h" +#include "gtkgesturestylus.h" +#include "gtkgesturestylusprivate.h" +#include "gtkprivate.h" +#include "gtkintl.h" +#include "gtkmain.h" + +G_DEFINE_TYPE (GtkGestureStylus, gtk_gesture_stylus, GTK_TYPE_GESTURE_SINGLE) + +enum { + PROXIMITY, + DOWN, + MOTION, + UP, + N_SIGNALS +}; + +static guint signals[N_SIGNALS] = { 0, }; + +static gboolean +gtk_gesture_stylus_handle_event (GtkEventController *controller, + const GdkEvent *event) +{ + GdkModifierType modifiers; + guint n_signal; + gdouble x, y; + + GTK_EVENT_CONTROLLER_CLASS (gtk_gesture_stylus_parent_class)->handle_event (controller, event); + + if (!gdk_event_get_device_tool (event)) + return FALSE; + if (!gdk_event_get_coords (event, &x, &y)) + return FALSE; + + switch ((guint) gdk_event_get_event_type (event)) + { + case GDK_BUTTON_PRESS: + n_signal = DOWN; + break; + case GDK_BUTTON_RELEASE: + n_signal = UP; + break; + case GDK_MOTION_NOTIFY: + gdk_event_get_state (event, &modifiers); + + if (modifiers & GDK_BUTTON1_MASK) + n_signal = MOTION; + else + n_signal = PROXIMITY; + break; + default: + return FALSE; + } + + g_signal_emit (controller, signals[n_signal], 0, x, y); + + return TRUE; +} + +static void +gtk_gesture_stylus_class_init (GtkGestureStylusClass *klass) +{ + GtkEventControllerClass *event_controller_class; + + event_controller_class = GTK_EVENT_CONTROLLER_CLASS (klass); + event_controller_class->handle_event = gtk_gesture_stylus_handle_event; + + signals[PROXIMITY] = + g_signal_new (I_("proximity"), + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (GtkGestureStylusClass, proximity), + NULL, NULL, NULL, + G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE); + signals[DOWN] = + g_signal_new (I_("down"), + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (GtkGestureStylusClass, down), + NULL, NULL, NULL, + G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE); + signals[MOTION] = + g_signal_new (I_("motion"), + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (GtkGestureStylusClass, motion), + NULL, NULL, NULL, + G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE); + signals[UP] = + g_signal_new (I_("up"), + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (GtkGestureStylusClass, up), + NULL, NULL, NULL, + G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE); +} + +static void +gtk_gesture_stylus_init (GtkGestureStylus *gesture) +{ +} + +/** + * gtk_gesture_stylus_new: + * @widget: a #GtkWidget + * + * Creates a new #GtkGestureStylus. + * + * Returns: a newly created stylus gesture + * + * Since: 3.94 + **/ +GtkGesture * +gtk_gesture_stylus_new (GtkWidget *widget) +{ + return g_object_new (GTK_TYPE_GESTURE_STYLUS, + "widget", widget, + NULL); +} + +static const GdkEvent * +gesture_get_current_event (GtkGestureStylus *gesture) +{ + GdkEventSequence *sequence; + + sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture)); + + return gtk_gesture_get_last_event (GTK_GESTURE (gesture), sequence); +} + +/** + * gtk_gesture_stylus_get_axis: + * @gesture: a #GtkGestureStylus + * @axis: requested device axis + * @value: (out): return location for the axis value + * + * Returns the current value for the requested @axis. This function + * must be called from either the #GtkGestureStylus:down, + * #GtkGestureStylus:motion, #GtkGestureStylus:up or #GtkGestureStylus:proximity + * signals. + * + * Returns: #TRUE if there is a current value for the axis + * + * Since: 3.94 + **/ +gboolean +gtk_gesture_stylus_get_axis (GtkGestureStylus *gesture, + GdkAxisUse axis, + gdouble *value) +{ + const GdkEvent *event; + + g_return_val_if_fail (GTK_IS_GESTURE_STYLUS (gesture), FALSE); + g_return_val_if_fail (axis < GDK_AXIS_LAST, FALSE); + g_return_val_if_fail (value != NULL, FALSE); + + event = gesture_get_current_event (gesture); + if (!event) + return FALSE; + + return gdk_event_get_axis (event, axis, value); +} + +/** + * gtk_gesture_stylus_get_axes: + * @gesture: a GtkGestureStylus + * @axes: array of requested axes, terminated with #GDK_AXIS_IGNORE + * @values: (out): return location for the axis values + * + * Returns the current values for the requested @axes. This function + * must be called from either the #GtkGestureStylus:down, + * #GtkGestureStylus:motion, #GtkGestureStylus:up or #GtkGestureStylus:proximity + * signals. + * + * Returns: #TRUE if there is a current value for the axes + **/ +gboolean +gtk_gesture_stylus_get_axes (GtkGestureStylus *gesture, + GdkAxisUse axes[], + gdouble **values) +{ + const GdkEvent *event; + GArray *array; + gint i = 0; + + g_return_val_if_fail (GTK_IS_GESTURE_STYLUS (gesture), FALSE); + g_return_val_if_fail (values != NULL, FALSE); + + event = gesture_get_current_event (gesture); + if (!event) + return FALSE; + + array = g_array_new (TRUE, FALSE, sizeof (gdouble)); + + while (axes[i] != GDK_AXIS_IGNORE) + { + gdouble value; + + if (axes[i] >= GDK_AXIS_LAST) + { + g_warning ("Requesting unknown axis %d, did you " + "forget to add a last GDK_AXIS_IGNORE axis?", + axes[i]); + g_array_free (array, TRUE); + return FALSE; + } + + gdk_event_get_axis (event, axes[i], &value); + g_array_append_val (array, value); + i++; + } + + *values = (gdouble *) g_array_free (array, FALSE); + return TRUE; +} + +/** + * gtk_gesture_stylus_get_backlog: + * @gesture: a #GtkGestureStylus + * @backlog: (out): coordinates and times for the backlog events + * @n_elems: (out): return location for the number of elements + * + * By default, GTK+ will limit rate of input events. On stylus input where + * accuracy of strokes is paramount, this function returns the accumulated + * coordinate/timing state before the emission of the current + * #GtkGestureStylus:motion signal. + * + * This function may only be called within a #GtkGestureStylus::motion + * signal handler, the state given in this signal and obtainable through + * gtk_gesture_stylus_get_axis() call express the latest (most up-to-date) + * state in motion history. + * + * @backlog is provided in chronological order. + * + * Returns: #TRUE if there is a backlog to unfold in the current state. + **/ +gboolean +gtk_gesture_stylus_get_backlog (GtkGestureStylus *gesture, + GdkTimeCoord **backlog, + guint *n_elems) +{ + const GdkEvent *event; + GArray *backlog_array; + GList *history = NULL, *l; + + g_return_val_if_fail (GTK_IS_GESTURE_STYLUS (gesture), FALSE); + g_return_val_if_fail (backlog != NULL && n_elems != NULL, FALSE); + + event = gesture_get_current_event (gesture); + + if (event) + history = gdk_event_get_motion_history (event); + if (!history) + return FALSE; + + backlog_array = g_array_new (FALSE, FALSE, sizeof (GdkTimeCoord)); + for (l = history; l; l = l->next) + { + GdkTimeCoord *time_coord = l->data; + + g_array_append_val (backlog_array, *time_coord); + time_coord = &g_array_index (backlog_array, GdkTimeCoord, backlog_array->len - 1); + gtk_widget_translate_coordinatesf (gtk_get_event_widget (event), + gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture)), + time_coord->axes[GDK_AXIS_X], + time_coord->axes[GDK_AXIS_Y], + &time_coord->axes[GDK_AXIS_X], + &time_coord->axes[GDK_AXIS_Y]); + } + + *n_elems = backlog_array->len; + *backlog = (GdkTimeCoord *) g_array_free (backlog_array, FALSE); + g_list_free (history); + + return TRUE; +} + +/** + * gtk_gesture_stylus_get_device_tool: + * @gesture: a #GtkGestureStylus + * + * Returns the #GdkDeviceTool currently driving input through this gesture. + * This function must be called from either the #GtkGestureStylus:down, + * #GtkGestureStylus:motion, #GtkGestureStylus:up or #GtkGestureStylus:proximity + * signals. + * + * Returns: (nullable) (transfer none): The current stylus tool + **/ +GdkDeviceTool * +gtk_gesture_stylus_get_device_tool (GtkGestureStylus *gesture) +{ + const GdkEvent *event; + + g_return_val_if_fail (GTK_IS_GESTURE_STYLUS (gesture), FALSE); + + event = gesture_get_current_event (gesture); + if (!event) + return NULL; + + return gdk_event_get_device_tool (event); +} diff --git a/gtk/gtkgesturestylus.h b/gtk/gtkgesturestylus.h new file mode 100644 index 0000000000..eebfe4e8b3 --- /dev/null +++ b/gtk/gtkgesturestylus.h @@ -0,0 +1,63 @@ +/* GTK - The GIMP Toolkit + * Copyright (C) 2017-2018, 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 . + * + * Author(s): Carlos Garnacho + */ +#ifndef __GTK_GESTURE_STYLUS_H__ +#define __GTK_GESTURE_STYLUS_H__ + +#include + +#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION) +#error "Only can be included directly." +#endif + +G_BEGIN_DECLS + +#define GTK_TYPE_GESTURE_STYLUS (gtk_gesture_stylus_get_type ()) +#define GTK_GESTURE_STYLUS(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_GESTURE_STYLUS, GtkGestureStylus)) +#define GTK_GESTURE_STYLUS_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GTK_TYPE_GESTURE_STYLUS, GtkGestureStylusClass)) +#define GTK_IS_GESTURE_STYLUS(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_GESTURE_STYLUS)) +#define GTK_IS_GESTURE_STYLUS_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GTK_TYPE_GESTURE_STYLUS)) +#define GTK_GESTURE_STYLUS_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GTK_TYPE_GESTURE_STYLUS, GtkGestureStylusClass)) + +typedef struct _GtkGestureStylus GtkGestureStylus; +typedef struct _GtkGestureStylusClass GtkGestureStylusClass; + +GDK_AVAILABLE_IN_ALL +GType gtk_gesture_stylus_get_type (void) G_GNUC_CONST; + +GDK_AVAILABLE_IN_ALL +GtkGesture * gtk_gesture_stylus_new (GtkWidget *widget); + +GDK_AVAILABLE_IN_ALL +gboolean gtk_gesture_stylus_get_axis (GtkGestureStylus *gesture, + GdkAxisUse axis, + gdouble *value); +GDK_AVAILABLE_IN_ALL +gboolean gtk_gesture_stylus_get_axes (GtkGestureStylus *gesture, + GdkAxisUse axes[], + gdouble **values); +GDK_AVAILABLE_IN_ALL +gboolean gtk_gesture_stylus_get_backlog (GtkGestureStylus *gesture, + GdkTimeCoord **backlog, + guint *n_elems); +GDK_AVAILABLE_IN_ALL +GdkDeviceTool * gtk_gesture_stylus_get_device_tool (GtkGestureStylus *gesture); + +G_END_DECLS + +#endif /* __GTK_GESTURE_STYLUS_H__ */ diff --git a/gtk/gtkgesturestylusprivate.h b/gtk/gtkgesturestylusprivate.h new file mode 100644 index 0000000000..9869b528a5 --- /dev/null +++ b/gtk/gtkgesturestylusprivate.h @@ -0,0 +1,51 @@ +/* GTK - The GIMP Toolkit + * Copyright (C) 2017-2018, 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 . + * + * Author(s): Carlos Garnacho + */ +#ifndef __GTK_GESTURE_STYLUS_PRIVATE_H__ +#define __GTK_GESTURE_STYLUS_PRIVATE_H__ + +#include "gtkgesturesingleprivate.h" +#include "gtkgesturestylus.h" + +struct _GtkGestureStylus +{ + GtkGestureSingle parent_instance; +}; + +struct _GtkGestureStylusClass +{ + GtkGestureSingleClass parent_class; + + void (*proximity) (GtkGestureStylus *gesture, + gdouble x, + gdouble y); + void (*down) (GtkGestureStylus *gesture, + gdouble x, + gdouble y); + void (*motion) (GtkGestureStylus *gesture, + gdouble x, + gdouble y); + void (*up) (GtkGestureStylus *gesture, + gdouble x, + gdouble y); + + /*< private >*/ + gpointer padding[10]; +}; + +#endif /* __GTK_GESTURE_STYLUS_PRIVATE_H__ */ diff --git a/gtk/gtkprivate.h b/gtk/gtkprivate.h index 817694f513..981e515010 100644 --- a/gtk/gtkprivate.h +++ b/gtk/gtkprivate.h @@ -93,6 +93,13 @@ void gtk_propagate_event_internal (GtkWidget *widget, GdkEvent *event, GtkWidget *topmost); +gboolean gtk_widget_translate_coordinatesf (GtkWidget *src_widget, + GtkWidget *dest_widget, + double src_x, + double src_y, + double *dest_x, + double *dest_y); + GtkWidget * _gtk_toplevel_pick (GtkWindow *toplevel, gdouble x, gdouble y, diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c index a140e12fce..8bb3b57c34 100644 --- a/gtk/gtkwidget.c +++ b/gtk/gtkwidget.c @@ -4551,7 +4551,7 @@ gtk_widget_translate_coordinates (GtkWidget *src_widget, * We use this for event coordinates. * * We should probably decide for only one of the 2 versions at some point */ -static gboolean +gboolean gtk_widget_translate_coordinatesf (GtkWidget *src_widget, GtkWidget *dest_widget, double src_x, diff --git a/gtk/meson.build b/gtk/meson.build index 6c68274a25..f1e057189d 100644 --- a/gtk/meson.build +++ b/gtk/meson.build @@ -227,6 +227,7 @@ gtk_public_sources = files([ 'gtkgesturepan.c', 'gtkgesturerotate.c', 'gtkgesturesingle.c', + 'gtkgesturestylus.c', 'gtkgestureswipe.c', 'gtkgesturezoom.c', 'gtkglarea.c', @@ -465,6 +466,7 @@ gtk_public_headers = files([ 'gtkgesturepan.h', 'gtkgesturerotate.h', 'gtkgesturesingle.h', + 'gtkgesturestylus.h', 'gtkgestureswipe.h', 'gtkgesturezoom.h', 'gtkglarea.h', diff --git a/po/POTFILES.in b/po/POTFILES.in index 03237acd36..627262bf47 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -162,6 +162,7 @@ gtk/gtkgesturemultipress.c gtk/gtkgesturepan.c gtk/gtkgesturerotate.c gtk/gtkgesturesingle.c +gtk/gtkgesturestylus.c gtk/gtkgestureswipe.c gtk/gtkgesturezoom.c gtk/gtkglarea.c