[PATCH libinput v2 3/4] touchpad: Extend the touchpad gesture API with pinch gestures

Hans de Goede hdegoede at redhat.com
Thu Mar 26 02:04:39 PDT 2015


Extend the touchpad gesture API with pinch gestures. Note that this
new API offers a single event stream for both pinch and rotate data, this
is deliberate as some applications may be interested in getting both at
the same time. Applications which are only interested in one or the other
can simply ignore the other.

Signed-off-by: Hans de Goede <hdegoede at redhat.com>
Reviewed-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 src/libinput-private.h |  9 ++++++
 src/libinput.c         | 61 +++++++++++++++++++++++++++++++++------
 src/libinput.h         | 78 ++++++++++++++++++++++++++++++++++++++++++--------
 src/libinput.sym       |  2 ++
 test/litest.c          |  9 ++++++
 tools/event-debug.c    | 27 ++++++++++++++++-
 6 files changed, 165 insertions(+), 21 deletions(-)

diff --git a/src/libinput-private.h b/src/libinput-private.h
index a70f5b5..2c9f42b 100644
--- a/src/libinput-private.h
+++ b/src/libinput-private.h
@@ -355,6 +355,15 @@ gesture_notify_swipe(struct libinput_device *device,
 		     const struct normalized_coords *unaccel);
 
 void
+gesture_notify_pinch(struct libinput_device *device,
+		     uint64_t time,
+		     enum libinput_event_type type,
+		     const struct normalized_coords *delta,
+		     const struct normalized_coords *unaccel,
+		     double distance,
+		     double angle);
+
+void
 touch_notify_frame(struct libinput_device *device,
 		   uint64_t time);
 
diff --git a/src/libinput.c b/src/libinput.c
index 5cc09d3..869e96a 100644
--- a/src/libinput.c
+++ b/src/libinput.c
@@ -118,6 +118,8 @@ struct libinput_event_gesture {
 	int finger_count;
 	struct normalized_coords delta;
 	struct normalized_coords delta_unaccel;
+	double distance;
+	double angle;
 };
 
 static void
@@ -251,7 +253,10 @@ libinput_event_get_gesture_event(struct libinput_event *event)
 			   NULL,
 			   LIBINPUT_EVENT_GESTURE_SWIPE_START,
 			   LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
-			   LIBINPUT_EVENT_GESTURE_SWIPE_END);
+			   LIBINPUT_EVENT_GESTURE_SWIPE_END,
+			   LIBINPUT_EVENT_GESTURE_PINCH_START,
+			   LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
+			   LIBINPUT_EVENT_GESTURE_PINCH_END);
 
 	return (struct libinput_event_gesture *) event;
 }
@@ -694,6 +699,18 @@ libinput_event_gesture_get_dy_unaccelerated(
 	return event->delta_unaccel.y;
 }
 
+LIBINPUT_EXPORT double
+libinput_event_gesture_get_distance(struct libinput_event_gesture *event)
+{
+	return event->distance;
+}
+
+LIBINPUT_EXPORT double
+libinput_event_gesture_get_angle(struct libinput_event_gesture *event)
+{
+	return event->angle;
+}
+
 struct libinput_source *
 libinput_add_fd(struct libinput *libinput,
 		int fd,
@@ -1343,13 +1360,15 @@ touch_notify_frame(struct libinput_device *device,
 			  &touch_event->base);
 }
 
-void
-gesture_notify_swipe(struct libinput_device *device,
-		     uint64_t time,
-		     enum libinput_event_type type,
-		     int finger_count,
-		     const struct normalized_coords *delta,
-		     const struct normalized_coords *unaccel)
+static void
+gesture_notify(struct libinput_device *device,
+	       uint64_t time,
+	       enum libinput_event_type type,
+	       int finger_count,
+	       const struct normalized_coords *delta,
+	       const struct normalized_coords *unaccel,
+	       double distance,
+	       double angle)
 {
 	struct libinput_event_gesture *gesture_event;
 
@@ -1362,12 +1381,38 @@ gesture_notify_swipe(struct libinput_device *device,
 		.finger_count = finger_count,
 		.delta = *delta,
 		.delta_unaccel = *unaccel,
+		.distance = distance,
+		.angle = angle,
 	};
 
 	post_device_event(device, time, type,
 			  &gesture_event->base);
 }
 
+void
+gesture_notify_swipe(struct libinput_device *device,
+		     uint64_t time,
+		     enum libinput_event_type type,
+		     int finger_count,
+		     const struct normalized_coords *delta,
+		     const struct normalized_coords *unaccel)
+{
+	gesture_notify(device, time, type, finger_count, delta, unaccel,
+		       0.0, 0.0);
+}
+
+void
+gesture_notify_pinch(struct libinput_device *device,
+		     uint64_t time,
+		     enum libinput_event_type type,
+		     const struct normalized_coords *delta,
+		     const struct normalized_coords *unaccel,
+		     double distance,
+		     double angle)
+{
+	gesture_notify(device, time, type, 2, delta, unaccel, distance, angle);
+}
+
 static void
 libinput_post_event(struct libinput *libinput,
 		    struct libinput_event *event)
diff --git a/src/libinput.h b/src/libinput.h
index e24ad4a..0a9dbff 100644
--- a/src/libinput.h
+++ b/src/libinput.h
@@ -178,6 +178,9 @@ enum libinput_event_type {
 	LIBINPUT_EVENT_GESTURE_SWIPE_START = 800,
 	LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
 	LIBINPUT_EVENT_GESTURE_SWIPE_END,
+	LIBINPUT_EVENT_GESTURE_PINCH_START,
+	LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
+	LIBINPUT_EVENT_GESTURE_PINCH_END,
 };
 
 /**
@@ -983,8 +986,8 @@ libinput_event_gesture_get_finger_count(struct libinput_event_gesture *event);
  * @ingroup event_gesture
  *
  * Return the delta between the last event and the current event. For gesture
- * events that are not of type @ref LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE, this
- * function returns 0.
+ * events that are not of type @ref LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE or
+ * @ref LIBINPUT_EVENT_GESTURE_PINCH_UPDATE, this function returns 0.
  *
  * If a device employs pointer acceleration, the delta returned by this
  * function is the accelerated delta.
@@ -993,7 +996,8 @@ libinput_event_gesture_get_finger_count(struct libinput_event_gesture *event);
  * 1000dpi resolution. See @ref motion_normalization for more details.
  *
  * @note It is an application bug to call this function for events other than
- * @ref LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE.
+ * @ref LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE or
+ * @ref LIBINPUT_EVENT_GESTURE_PINCH_UPDATE.
  *
  * @return the relative x movement since the last event
  */
@@ -1004,8 +1008,8 @@ libinput_event_gesture_get_dx(struct libinput_event_gesture *event);
  * @ingroup event_gesture
  *
  * Return the delta between the last event and the current event. For gesture
- * events that are not of type @ref LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE, this
- * function returns 0.
+ * events that are not of type @ref LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE or
+ * @ref LIBINPUT_EVENT_GESTURE_PINCH_UPDATE, this function returns 0.
  *
  * If a device employs pointer acceleration, the delta returned by this
  * function is the accelerated delta.
@@ -1014,7 +1018,8 @@ libinput_event_gesture_get_dx(struct libinput_event_gesture *event);
  * 1000dpi resolution. See @ref motion_normalization for more details.
  *
  * @note It is an application bug to call this function for events other than
- * @ref LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE.
+ * @ref LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE or
+ * @ref LIBINPUT_EVENT_GESTURE_PINCH_UPDATE.
  *
  * @return the relative y movement since the last event
  */
@@ -1025,8 +1030,9 @@ libinput_event_gesture_get_dy(struct libinput_event_gesture *event);
  * @ingroup event_gesture
  *
  * Return the relative delta of the unaccelerated motion vector of the
- * current event. For gesture events that are not of type @ref
- * LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE, this function returns 0.
+ * current event. For gesture events that are not of type
+ * @ref LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE or
+ * @ref LIBINPUT_EVENT_GESTURE_PINCH_UPDATE, this function returns 0.
  *
  * Relative unaccelerated motion deltas are normalized to represent those of a
  * device with 1000dpi resolution. See @ref motion_normalization for more
@@ -1034,7 +1040,8 @@ libinput_event_gesture_get_dy(struct libinput_event_gesture *event);
  * as read from the device.
  *
  * @note It is an application bug to call this function for events other than
- * @ref LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE.
+ * @ref LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE or
+ * @ref LIBINPUT_EVENT_GESTURE_PINCH_UPDATE.
  *
  * @return the unaccelerated relative x movement since the last event
  */
@@ -1046,8 +1053,9 @@ libinput_event_gesture_get_dx_unaccelerated(
  * @ingroup event_gesture
  *
  * Return the relative delta of the unaccelerated motion vector of the
- * current event. For gesture events that are not of type @ref
- * LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE, this function returns 0.
+ * current event. For gesture events that are not of type
+ * @ref LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE or
+ * @ref LIBINPUT_EVENT_GESTURE_PINCH_UPDATE, this function returns 0.
  *
  * Relative unaccelerated motion deltas are normalized to represent those of a
  * device with 1000dpi resolution. See @ref motion_normalization for more
@@ -1055,7 +1063,8 @@ libinput_event_gesture_get_dx_unaccelerated(
  * as read from the device.
  *
  * @note It is an application bug to call this function for events other than
- * @ref LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE.
+ * @ref LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE or
+ * @ref LIBINPUT_EVENT_GESTURE_PINCH_UPDATE.
  *
  * @return the unaccelerated relative y movement since the last event
  */
@@ -1064,6 +1073,51 @@ libinput_event_gesture_get_dy_unaccelerated(
 	struct libinput_event_gesture *event);
 
 /**
+ * @ingroup event_gesture
+ *
+ * Return the change in distance between the fingers of a pinch gesture.
+ * For gesture events that are not of type @ref
+ * LIBINPUT_EVENT_GESTURE_PINCH_UPDATE, this function returns 0.
+ *
+ * The distance deltas are normalized to represent those of a device with
+ * 1000dpi resolution. See @ref motion_normalization for more details.
+ *
+ * @note It is an application bug to call this function for events other than
+ * @ref LIBINPUT_EVENT_GESTURE_PINCH_UPDATE.
+ *
+ * @return the change in finger distance
+ */
+double
+libinput_event_gesture_get_distance(struct libinput_event_gesture *event);
+
+/**
+ * @ingroup event_gesture
+ *
+ * Return the angle delta in degrees between the last and the current @ref
+ * LIBINPUT_EVENT_GESTURE_PINCH_UPDATE event. For gesture events that
+ * are not of type @ref LIBINPUT_EVENT_GESTURE_PINCH_UPDATE, this
+ * function returns 0.
+ *
+ * The angle delta is defined as the change in angle of the line formed by
+ * the 2 fingers of a pinch gesture. Clockwise rotation is represented
+ * by a postive delta, counter-clockwise by a negative delta. If e.g. the
+ * fingers are on the 12 and 6 location of a clock face plate and they move
+ * to the 1 resp. 7 location in a single event then the angle delta is
+ * 30 degrees.
+ *
+ * If more than two fingers are present, the angle represents the rotation
+ * around the center of gravity. The calculation of the center of gravity is
+ * implementation-dependent.
+ *
+ * @note It is an application bug to call this function for events other than
+ * @ref LIBINPUT_EVENT_GESTURE_PINCH_UPDATE.
+ *
+ * @return the angle delta since the last event
+ */
+double
+libinput_event_gesture_get_angle(struct libinput_event_gesture *event);
+
+/**
  * @defgroup base Initialization and manipulation of libinput contexts
  */
 
diff --git a/src/libinput.sym b/src/libinput.sym
index 050c696..650069f 100644
--- a/src/libinput.sym
+++ b/src/libinput.sym
@@ -129,7 +129,9 @@ local:
 };
 
 LIBINPUT_0.13.0 {
+	libinput_event_gesture_get_angle;
 	libinput_event_gesture_get_base_event;
+	libinput_event_gesture_get_distance;
 	libinput_event_gesture_get_dx;
 	libinput_event_gesture_get_dx_unaccelerated;
 	libinput_event_gesture_get_dy;
diff --git a/test/litest.c b/test/litest.c
index 310e354..455f9c6 100644
--- a/test/litest.c
+++ b/test/litest.c
@@ -1106,6 +1106,15 @@ litest_event_type_str(struct libinput_event *event)
 	case LIBINPUT_EVENT_GESTURE_SWIPE_END:
 		str = "GESTURE SWIPE END";
 		break;
+	case LIBINPUT_EVENT_GESTURE_PINCH_START:
+		str = "GESTURE PINCH START";
+		break;
+	case LIBINPUT_EVENT_GESTURE_PINCH_UPDATE:
+		str = "GESTURE PINCH UPDATE";
+		break;
+	case LIBINPUT_EVENT_GESTURE_PINCH_END:
+		str = "GESTURE PINCH END";
+		break;
 	}
 	return str;
 }
diff --git a/tools/event-debug.c b/tools/event-debug.c
index f994ea1..e52dfd1 100644
--- a/tools/event-debug.c
+++ b/tools/event-debug.c
@@ -115,6 +115,15 @@ print_event_header(struct libinput_event *ev)
 	case LIBINPUT_EVENT_GESTURE_SWIPE_END:
 		type = "GESTURE_SWIPE_END";
 		break;
+	case LIBINPUT_EVENT_GESTURE_PINCH_START:
+		type = "GESTURE_PINCH_START";
+		break;
+	case LIBINPUT_EVENT_GESTURE_PINCH_UPDATE:
+		type = "GESTURE_PINCH_UPDATE";
+		break;
+	case LIBINPUT_EVENT_GESTURE_PINCH_END:
+		type = "GESTURE_PINCH_END";
+		break;
 	}
 
 	printf("%-7s	%s	", libinput_device_get_sysname(dev), type);
@@ -315,12 +324,19 @@ print_gesture_event_with_coords(struct libinput_event *ev)
 	double dy = libinput_event_gesture_get_dy(t);
 	double dx_unaccel = libinput_event_gesture_get_dx_unaccelerated(t);
 	double dy_unaccel = libinput_event_gesture_get_dy_unaccelerated(t);
+	double distance = libinput_event_gesture_get_distance(t);
+	double angle = libinput_event_gesture_get_angle(t);
 
 	print_event_time(libinput_event_gesture_get_time(t));
 
-	printf("%d %5.2f/%5.2f (%5.2f/%5.2f unaccelerated)\n",
+	printf("%d %5.2f/%5.2f (%5.2f/%5.2f unaccelerated)",
 	       libinput_event_gesture_get_finger_count(t),
 	       dx, dy, dx_unaccel, dy_unaccel);
+
+	if (libinput_event_get_type(ev) == LIBINPUT_EVENT_GESTURE_PINCH_UPDATE)
+		printf(" %5.2f @ %5.2f\n", distance, angle);
+	else
+		printf("\n");
 }
 
 static int
@@ -381,6 +397,15 @@ handle_and_print_events(struct libinput *li)
 		case LIBINPUT_EVENT_GESTURE_SWIPE_END:
 			print_gesture_event_without_coords(ev);
 			break;
+		case LIBINPUT_EVENT_GESTURE_PINCH_START:
+			print_gesture_event_without_coords(ev);
+			break;
+		case LIBINPUT_EVENT_GESTURE_PINCH_UPDATE:
+			print_gesture_event_with_coords(ev);
+			break;
+		case LIBINPUT_EVENT_GESTURE_PINCH_END:
+			print_gesture_event_without_coords(ev);
+			break;
 		}
 
 		libinput_event_destroy(ev);
-- 
2.3.3



More information about the wayland-devel mailing list