[PATCH libinput gestures 4/4] tools: handle pinch event in event-gui

Peter Hutterer peter.hutterer at who-t.net
Thu May 21 19:03:57 PDT 2015


Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 tools/event-gui.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 68 insertions(+), 1 deletion(-)

diff --git a/tools/event-gui.c b/tools/event-gui.c
index c28aaf2..adfb9f4 100644
--- a/tools/event-gui.c
+++ b/tools/event-gui.c
@@ -77,6 +77,13 @@ struct window {
 		double x, y;
 	} swipe;
 
+	struct {
+		int nfingers;
+		double scale;
+		double angle;
+		double x, y;
+	} pinch;
+
 	struct libinput_device *devices[50];
 };
 
@@ -109,7 +116,7 @@ draw(GtkWidget *widget, cairo_t *cr, gpointer data)
 {
 	struct window *w = data;
 	struct touch *t;
-	int i;
+	int i, offset;
 
 	cairo_set_source_rgb(cr, 1, 1, 1);
 	cairo_rectangle(cr, 0, 0, w->width, w->height);
@@ -131,6 +138,26 @@ draw(GtkWidget *widget, cairo_t *cr, gpointer data)
 	}
 	cairo_restore(cr);
 
+	/* pinch */
+	cairo_save(cr);
+	offset = w->pinch.scale * 100;
+	cairo_translate(cr, w->pinch.x, w->pinch.y);
+	cairo_rotate(cr, w->pinch.angle * M_PI/180.0);
+	if (w->pinch.nfingers > 0) {
+		cairo_set_source_rgb(cr, .4, .4, .8);
+		cairo_arc(cr, offset, -offset, 20, 0, 2 * M_PI);
+		cairo_arc(cr, -offset, offset, 20, 0, 2 * M_PI);
+		cairo_fill(cr);
+	}
+
+	cairo_set_source_rgb(cr, 0, 0, 0);
+	cairo_arc(cr, offset, -offset, 20, 0, 2 * M_PI);
+	cairo_stroke(cr);
+	cairo_arc(cr, -offset, offset, 20, 0, 2 * M_PI);
+	cairo_stroke(cr);
+
+	cairo_restore(cr);
+
 	/* draw pointer sprite */
 	cairo_set_source_rgb(cr, 0, 0, 0);
 	cairo_save(cr);
@@ -211,6 +238,10 @@ map_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
 	w->swipe.x = w->width/2;
 	w->swipe.y = w->height/2;
 
+	w->pinch.scale = 1.0;
+	w->pinch.x = w->width/2;
+	w->pinch.y = w->height/2;
+
 	g_signal_connect(G_OBJECT(w->area), "draw", G_CALLBACK(draw), w);
 
 	window = gdk_event_get_window(event);
@@ -482,6 +513,41 @@ handle_event_swipe(struct libinput_event *ev, struct window *w)
 	}
 }
 
+static void
+handle_event_pinch(struct libinput_event *ev, struct window *w)
+{
+	struct libinput_event_gesture *g = libinput_event_get_gesture_event(ev);
+	int nfingers;
+	double dx, dy;
+
+	nfingers = libinput_event_gesture_get_finger_count(g);
+
+	switch (libinput_event_get_type(ev)) {
+	case LIBINPUT_EVENT_GESTURE_PINCH_START:
+		w->pinch.nfingers = nfingers;
+		w->pinch.x = w->width/2;
+		w->pinch.y = w->height/2;
+		break;
+	case LIBINPUT_EVENT_GESTURE_PINCH_UPDATE:
+		dx = libinput_event_gesture_get_dx(g);
+		dy = libinput_event_gesture_get_dy(g);
+		w->pinch.x += dx;
+		w->pinch.y += dy;
+		w->pinch.scale = libinput_event_gesture_get_scale(g);
+		w->pinch.angle += libinput_event_gesture_get_angle(g);
+		break;
+	case LIBINPUT_EVENT_GESTURE_PINCH_END:
+		w->pinch.nfingers = 0;
+		w->pinch.x = w->width/2;
+		w->pinch.y = w->height/2;
+		w->pinch.angle = 0.0;
+		w->pinch.scale = 1.0;
+		break;
+	default:
+		abort();
+	}
+}
+
 static gboolean
 handle_event_libinput(GIOChannel *source, GIOCondition condition, gpointer data)
 {
@@ -534,6 +600,7 @@ handle_event_libinput(GIOChannel *source, GIOCondition condition, gpointer data)
 		case LIBINPUT_EVENT_GESTURE_PINCH_START:
 		case LIBINPUT_EVENT_GESTURE_PINCH_UPDATE:
 		case LIBINPUT_EVENT_GESTURE_PINCH_END:
+			handle_event_pinch(ev, w);
 			break;
 		}
 
-- 
2.3.5



More information about the wayland-devel mailing list