[PATCH weston v5 2/4] clients: Request the wl_pointer_gesture_swipe/pinch interfaces

Carlos Garnacho carlosg at gnome.org
Fri Jan 27 15:41:44 UTC 2017


This is accompanied by separate handlers for the different stages
of swipe/pinch gestures, so those can be set in demos.

v5: Update to zwp namespace
v4: Indenting fix.
v3: added null checks around pointer gesture interface destruction,
    nullify afterwards.
v2: depend on standalone protocol xml.

Signed-off-by: Carlos Garnacho <carlosg at gnome.org>
Reviewed-by: Jonas Ã…dahl <jadahl at gmail.com>
Reviewed-by: Bryce Harrington <bryce at osg.samsung.com>
---
 Makefile.am      |   6 +-
 clients/window.c | 230 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 clients/window.h |  54 +++++++++++++
 3 files changed, 288 insertions(+), 2 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index d41acd6..6fd5552 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -652,6 +652,8 @@ libtoytoolkit_la_SOURCES =				\
 	shared/helpers.h
 
 nodist_libtoytoolkit_la_SOURCES =			\
+	protocol/pointer-gestures-protocol.c		\
+	protocol/pointer-gestures-client-protocol.h	\
 	protocol/text-cursor-position-protocol.c	\
 	protocol/text-cursor-position-client-protocol.h	\
 	protocol/viewporter-protocol.c			\
@@ -663,7 +665,9 @@ nodist_libtoytoolkit_la_SOURCES =			\
 	protocol/pointer-constraints-unstable-v1-protocol.c		\
 	protocol/pointer-constraints-unstable-v1-client-protocol.h	\
 	protocol/relative-pointer-unstable-v1-protocol.c		\
-	protocol/relative-pointer-unstable-v1-client-protocol.h
+	protocol/relative-pointer-unstable-v1-client-protocol.h		\
+	protocol/pointer-gestures-unstable-v1-protocol.c		\
+	protocol/pointer-gestures-unstable-v1-client-protocol.h
 
 BUILT_SOURCES += $(nodist_libtoytoolkit_la_SOURCES)
 
diff --git a/clients/window.c b/clients/window.c
index 59fc07e..248605f 100644
--- a/clients/window.c
+++ b/clients/window.c
@@ -78,6 +78,7 @@ typedef void *EGLContext;
 #include "text-cursor-position-client-protocol.h"
 #include "pointer-constraints-unstable-v1-client-protocol.h"
 #include "relative-pointer-unstable-v1-client-protocol.h"
+#include "pointer-gestures-unstable-v1-client-protocol.h"
 #include "shared/os-compatibility.h"
 
 #include "window.h"
@@ -110,6 +111,7 @@ struct display {
 	struct ivi_application *ivi_application; /* ivi style shell */
 	struct zwp_relative_pointer_manager_v1 *relative_pointer_manager;
 	struct zwp_pointer_constraints_v1 *pointer_constraints;
+	struct zwp_pointer_gestures_v1 *pointer_gestures;
 	EGLDisplay dpy;
 	EGLConfig argb_config;
 	EGLContext argb_ctx;
@@ -247,7 +249,6 @@ struct window {
 
 	int fullscreen;
 	int maximized;
-
 	enum preferred_format preferred_format;
 
 	window_key_handler_t key_handler;
@@ -316,6 +317,12 @@ struct widget {
 	widget_axis_source_handler_t axis_source_handler;
 	widget_axis_stop_handler_t axis_stop_handler;
 	widget_axis_discrete_handler_t axis_discrete_handler;
+	widget_pointer_gesture_swipe_begin_handler_t swipe_begin_handler;
+	widget_pointer_gesture_swipe_update_handler_t swipe_update_handler;
+	widget_pointer_gesture_swipe_end_handler_t swipe_end_handler;
+	widget_pointer_gesture_pinch_begin_handler_t pinch_begin_handler;
+	widget_pointer_gesture_pinch_update_handler_t pinch_update_handler;
+	widget_pointer_gesture_pinch_end_handler_t pinch_end_handler;
 	void *user_data;
 	int opaque;
 	int tooltip_count;
@@ -340,6 +347,8 @@ struct input {
 	struct wl_pointer *pointer;
 	struct wl_keyboard *keyboard;
 	struct wl_touch *touch;
+	struct zwp_pointer_gesture_pinch_v1 *pinch;
+	struct zwp_pointer_gesture_swipe_v1 *swipe;
 	struct wl_list touch_point_list;
 	struct window *pointer_focus;
 	struct window *keyboard_focus;
@@ -2006,6 +2015,48 @@ widget_set_axis_handlers(struct widget *widget,
 	widget->axis_discrete_handler = axis_discrete_handler;
 }
 
+void
+widget_set_pointer_gesture_swipe_begin_handler(struct widget *widget,
+					       widget_pointer_gesture_swipe_begin_handler_t handler)
+{
+	widget->swipe_begin_handler = handler;
+}
+
+void
+widget_set_pointer_gesture_swipe_update_handler(struct widget *widget,
+						widget_pointer_gesture_swipe_update_handler_t handler)
+{
+	widget->swipe_update_handler = handler;
+}
+
+void
+widget_set_pointer_gesture_swipe_end_handler(struct widget *widget,
+					     widget_pointer_gesture_swipe_end_handler_t handler)
+{
+	widget->swipe_end_handler = handler;
+}
+
+void
+widget_set_pointer_gesture_pinch_begin_handler(struct widget *widget,
+					       widget_pointer_gesture_pinch_begin_handler_t handler)
+{
+	widget->pinch_begin_handler = handler;
+}
+
+void
+widget_set_pointer_gesture_pinch_update_handler(struct widget *widget,
+						widget_pointer_gesture_pinch_update_handler_t handler)
+{
+	widget->pinch_update_handler = handler;
+}
+
+void
+widget_set_pointer_gesture_pinch_end_handler(struct widget *widget,
+					     widget_pointer_gesture_pinch_end_handler_t handler)
+{
+	widget->pinch_end_handler = handler;
+}
+
 static void
 window_schedule_redraw_task(struct window *window);
 
@@ -3434,6 +3485,177 @@ static const struct wl_touch_listener touch_listener = {
 };
 
 static void
+gesture_swipe_handle_begin(void *data,
+			   struct zwp_pointer_gesture_swipe_v1 *swipe,
+			   uint32_t serial, uint32_t time,
+			   struct wl_surface *surface, uint32_t n_fingers)
+{
+	struct input *input = data;
+	struct widget *widget;
+
+	if (input->grab)
+		widget = input->grab;
+	else
+		widget = input->focus_widget;
+
+	if (widget && widget->swipe_begin_handler) {
+		widget->swipe_begin_handler(input->focus_widget, input,
+					    time, n_fingers, widget->user_data);
+	}
+}
+
+static void
+gesture_swipe_handle_update(void *data,
+			    struct zwp_pointer_gesture_swipe_v1 *swipe,
+			    uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
+{
+	struct input *input = data;
+	struct widget *widget;
+
+	if (input->grab)
+		widget = input->grab;
+	else
+		widget = input->focus_widget;
+
+	if (widget && widget->swipe_update_handler) {
+		widget->swipe_update_handler(input->focus_widget, input, time,
+					     wl_fixed_to_double(dx),
+					     wl_fixed_to_double(dy),
+					     widget->user_data);
+	}
+}
+
+static void
+gesture_swipe_handle_end(void *data, struct zwp_pointer_gesture_swipe_v1 *swipe,
+			 uint32_t serial, uint32_t time, int32_t cancelled)
+{
+	struct input *input = data;
+	struct widget *widget;
+
+	if (input->grab)
+		widget = input->grab;
+	else
+		widget = input->focus_widget;
+
+	if (widget && widget->swipe_end_handler) {
+		widget->swipe_end_handler(input->focus_widget, input, time,
+					  cancelled, widget->user_data);
+	}
+}
+
+static const struct zwp_pointer_gesture_swipe_v1_listener pointer_swipe_listener = {
+	gesture_swipe_handle_begin,
+	gesture_swipe_handle_update,
+	gesture_swipe_handle_end
+};
+
+static void
+gesture_pinch_handle_begin(void *data,
+			   struct zwp_pointer_gesture_pinch_v1 *pinch,
+			   uint32_t serial, uint32_t time,
+			   struct wl_surface *surface, uint32_t n_fingers)
+{
+	struct input *input = data;
+	struct widget *widget;
+
+	if (input->grab)
+		widget = input->grab;
+	else
+		widget = input->focus_widget;
+
+	if (widget && widget->pinch_begin_handler) {
+		widget->pinch_begin_handler(input->focus_widget, input,
+					    time, n_fingers, widget->user_data);
+	}
+}
+
+static void
+gesture_pinch_handle_update(void *data,
+			    struct zwp_pointer_gesture_pinch_v1 *pinch,
+			    uint32_t time, wl_fixed_t dx, wl_fixed_t dy,
+			    wl_fixed_t scale, wl_fixed_t rotation_delta)
+{
+	struct input *input = data;
+	struct widget *widget;
+
+	if (input->grab)
+		widget = input->grab;
+	else
+		widget = input->focus_widget;
+
+	if (widget && widget->pinch_update_handler) {
+		widget->pinch_update_handler(input->focus_widget, input, time,
+					     wl_fixed_to_double(dx),
+					     wl_fixed_to_double(dy),
+					     wl_fixed_to_double(scale),
+					     wl_fixed_to_double(rotation_delta),
+					     widget->user_data);
+	}
+}
+
+static void
+gesture_pinch_handle_end(void *data, struct zwp_pointer_gesture_pinch_v1 *pinch,
+			 uint32_t serial, uint32_t time, int32_t cancelled)
+{
+	struct input *input = data;
+	struct widget *widget;
+
+	if (input->grab)
+		widget = input->grab;
+	else
+		widget = input->focus_widget;
+
+	if (widget && widget->pinch_end_handler) {
+		widget->pinch_end_handler(input->focus_widget, input, time,
+					  cancelled, widget->user_data);
+	}
+}
+
+static const struct zwp_pointer_gesture_pinch_v1_listener pointer_pinch_listener = {
+	gesture_pinch_handle_begin,
+	gesture_pinch_handle_update,
+	gesture_pinch_handle_end
+};
+
+static void
+init_pointer_gestures(struct input *input)
+{
+	struct display *display = input->display;
+
+	if (!display->pointer_gestures)
+		return;
+
+	input->pinch =
+		zwp_pointer_gestures_v1_get_pinch_gesture(display->pointer_gestures,
+							  input->pointer);
+	zwp_pointer_gesture_pinch_v1_add_listener(input->pinch,
+						  &pointer_pinch_listener,
+						  input);
+	zwp_pointer_gesture_pinch_v1_set_user_data(input->pinch, input);
+
+	input->swipe =
+		zwp_pointer_gestures_v1_get_swipe_gesture(display->pointer_gestures,
+							  input->pointer);
+	zwp_pointer_gesture_swipe_v1_add_listener(input->swipe,
+						  &pointer_swipe_listener,
+						  input);
+	zwp_pointer_gesture_swipe_v1_set_user_data(input->swipe, input);
+}
+
+static void
+dispose_pointer_gestures(struct input *input)
+{
+	if (input->pinch) {
+		zwp_pointer_gesture_pinch_v1_destroy(input->pinch);
+		input->pinch = NULL;
+	}
+	if (input->swipe) {
+		zwp_pointer_gesture_swipe_v1_destroy(input->swipe);
+		input->swipe = NULL;
+	}
+}
+
+static void
 seat_handle_capabilities(void *data, struct wl_seat *seat,
 			 enum wl_seat_capability caps)
 {
@@ -3444,7 +3666,9 @@ seat_handle_capabilities(void *data, struct wl_seat *seat,
 		wl_pointer_set_user_data(input->pointer, input);
 		wl_pointer_add_listener(input->pointer, &pointer_listener,
 					input);
+		init_pointer_gestures(input);
 	} else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
+		dispose_pointer_gestures(input);
 		if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
 			wl_pointer_release(input->pointer);
 		else
@@ -6012,6 +6236,10 @@ registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
 		d->subcompositor =
 			wl_registry_bind(registry, id,
 					 &wl_subcompositor_interface, 1);
+	} else if (strcmp(interface, "zwp_pointer_gestures_v1") == 0) {
+		d->pointer_gestures =
+			wl_registry_bind(registry, id,
+					 &zwp_pointer_gestures_v1_interface, 1);
 	}
 	else if (strcmp(interface, "ivi_application") == 0) {
 		d->ivi_application =
diff --git a/clients/window.h b/clients/window.h
index 1ec9eac..c11fdc3 100644
--- a/clients/window.h
+++ b/clients/window.h
@@ -307,6 +307,41 @@ typedef void (*widget_axis_discrete_handler_t)(struct widget *widget,
 					       int32_t discrete,
 					       void *data);
 
+typedef void (*widget_pointer_gesture_swipe_begin_handler_t)(struct widget *widget,
+							     struct input *input,
+							     uint32_t time,
+							     uint32_t n_fingers,
+							     void *data);
+typedef void (*widget_pointer_gesture_swipe_update_handler_t)(struct widget *widget,
+							      struct input *input,
+							      uint32_t time,
+							      float dx,
+							      float dy,
+							      void *data);
+typedef void (*widget_pointer_gesture_swipe_end_handler_t)(struct widget *widget,
+							   struct input *input,
+							   uint32_t time,
+							   int32_t cancelled,
+							   void *data);
+typedef void (*widget_pointer_gesture_pinch_begin_handler_t)(struct widget *widget,
+							     struct input *input,
+							     uint32_t time,
+							     uint32_t n_fingers,
+							     void *data);
+typedef void (*widget_pointer_gesture_pinch_update_handler_t)(struct widget *widget,
+							      struct input *input,
+							      uint32_t time,
+							      float dx,
+							      float dy,
+							      float scale,
+							      float rotation_delta,
+							      void *data);
+typedef void (*widget_pointer_gesture_pinch_end_handler_t)(struct widget *widget,
+							   struct input *input,
+							   uint32_t time,
+							   int32_t cancelled,
+							   void *data);
+
 struct window *
 window_create(struct display *display);
 struct window *
@@ -604,6 +639,25 @@ widget_set_axis_handlers(struct widget *widget,
 			widget_axis_discrete_handler_t axis_discrete_handler);
 
 void
+widget_set_pointer_gesture_swipe_begin_handler(struct widget *widget,
+					       widget_pointer_gesture_swipe_begin_handler_t handler);
+void
+widget_set_pointer_gesture_swipe_update_handler(struct widget *widget,
+						widget_pointer_gesture_swipe_update_handler_t handler);
+void
+widget_set_pointer_gesture_swipe_end_handler(struct widget *widget,
+					     widget_pointer_gesture_swipe_end_handler_t handler);
+void
+widget_set_pointer_gesture_pinch_begin_handler(struct widget *widget,
+					       widget_pointer_gesture_pinch_begin_handler_t handler);
+void
+widget_set_pointer_gesture_pinch_update_handler(struct widget *widget,
+						widget_pointer_gesture_pinch_update_handler_t handler);
+void
+widget_set_pointer_gesture_pinch_end_handler(struct widget *widget,
+					     widget_pointer_gesture_pinch_end_handler_t handler);
+
+void
 widget_schedule_redraw(struct widget *widget);
 void
 widget_set_use_cairo(struct widget *widget, int use_cairo);
-- 
2.9.3



More information about the wayland-devel mailing list