[clutter/wip/touch-events: 2/2] tests: Add interactive test for touch events



commit 241581dfa1dabc79722b8335eb81e65dacab7c36
Author: Tomeu Vizoso <tomeu vizoso collabora com>
Date:   Mon Jun 4 10:28:56 2012 +0200

    tests: Add interactive test for touch events

 tests/interactive/Makefile.am         |    3 +-
 tests/interactive/test-touch-events.c |  124 +++++++++++++++++++++++++++++++++
 2 files changed, 126 insertions(+), 1 deletions(-)
---
diff --git a/tests/interactive/Makefile.am b/tests/interactive/Makefile.am
index 5773708..4fa6bf6 100644
--- a/tests/interactive/Makefile.am
+++ b/tests/interactive/Makefile.am
@@ -49,7 +49,8 @@ UNIT_TESTS = \
 	test-devices.c \
 	test-content.c \
 	test-keyframe-transition.c \
-	test-bind-constraint.c
+	test-bind-constraint.c \
+	test-touch-events.c
 
 if X11_TESTS
 UNIT_TESTS += test-pixmap.c
diff --git a/tests/interactive/test-touch-events.c b/tests/interactive/test-touch-events.c
new file mode 100644
index 0000000..57a3e74
--- /dev/null
+++ b/tests/interactive/test-touch-events.c
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2012 Collabora Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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 program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ * Boston, MA 02111-1307, USA.
+ *
+ */
+#include <stdlib.h>
+#include <math.h>
+#include <cairo.h>
+#include <glib.h>
+#include <clutter/clutter.h>
+
+#define NUM_COLORS 10
+
+static GSList *events = NULL;
+static const ClutterColor const static_colors[] = {
+  { 0xff, 0x00, 0x00, 0xff },   /* red */
+  { 0x80, 0x00, 0x00, 0xff },   /* dark red */
+  { 0x00, 0xff, 0x00, 0xff },   /* green */
+  { 0x00, 0x80, 0x00, 0xff },   /* dark green */
+  { 0x00, 0x00, 0xff, 0xff },   /* blue */
+  { 0x00, 0x00, 0x80, 0xff },   /* dark blue */
+  { 0x00, 0xff, 0xff, 0xff },   /* cyan */
+  { 0x00, 0x80, 0x80, 0xff },   /* dark cyan */
+  { 0xff, 0x00, 0xff, 0xff },   /* magenta */
+  { 0xff, 0xff, 0x00, 0xff },   /* yellow */
+};
+
+static void
+canvas_paint (ClutterActor *canvas)
+{
+  clutter_cairo_texture_invalidate (canvas);
+}
+
+static void
+draw_touch (ClutterEvent *event,
+            cairo_t      *cr)
+{
+  unsigned int sequence = clutter_event_get_event_sequence (event);
+  ClutterColor color = static_colors[sequence % NUM_COLORS];
+
+  cairo_set_source_rgba (cr, color.red / 255,
+                             color.green / 255,
+                             color.blue / 255,
+                             color.alpha / 255);
+  cairo_arc (cr, event->motion.x, event->motion.y, 5, 0, 2 * G_PI);
+  cairo_fill (cr);
+}
+
+static gboolean
+draw_touches (ClutterCairoTexture *canvas,
+              cairo_t             *cr)
+{
+  g_slist_foreach (events, (GFunc) draw_touch, cr);
+}
+
+static gboolean
+event_cb (ClutterActor *actor, ClutterEvent *event, ClutterActor *canvas)
+{
+  if (event->type != CLUTTER_TOUCH_UPDATE)
+    return FALSE;
+
+  events = g_slist_append (events, clutter_event_copy (event));
+
+  clutter_actor_queue_redraw (canvas);
+
+  return TRUE;
+}
+
+G_MODULE_EXPORT int
+test_touch_events_main (int argc, char *argv[])
+{
+  ClutterActor *stage, *canvas;
+
+  clutter_x11_enable_xinput ();
+
+  /* initialize Clutter */
+  if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
+    return EXIT_FAILURE;
+
+  /* create a resizable stage */
+  stage = clutter_stage_new ();
+  g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
+  clutter_stage_set_title (CLUTTER_STAGE (stage), "Touch events");
+  clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE);
+  clutter_actor_set_size (stage, 1600, 800);
+  clutter_actor_set_reactive (stage, TRUE);
+  clutter_actor_show (stage);
+
+  /* our 2D canvas, courtesy of Cairo */
+  canvas = clutter_cairo_texture_new (1, 1);
+  g_signal_connect (canvas, "paint", G_CALLBACK (canvas_paint), NULL);
+  g_signal_connect (canvas, "draw", G_CALLBACK (draw_touches), NULL);
+  clutter_cairo_texture_set_auto_resize (CLUTTER_CAIRO_TEXTURE (canvas), TRUE);
+  clutter_actor_add_constraint (canvas,
+                                clutter_bind_constraint_new (stage,
+                                                             CLUTTER_BIND_SIZE,
+                                                             0));
+  clutter_container_add_actor (CLUTTER_CONTAINER (stage), canvas);
+
+  g_signal_connect (stage, "event", G_CALLBACK (event_cb), canvas);
+
+  clutter_main ();
+
+  return EXIT_SUCCESS;
+}
+
+G_MODULE_EXPORT const char *
+test_touch_events_describe (void)
+{
+  return "Draw shapes based on touch events";
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]