2 * Copyright (C) 2012 Collabora Ltd.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU Lesser General Public License,
6 * version 2.1, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT ANY
9 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16 * Boston, MA 02111-1307, USA.
23 #include <clutter/clutter.h>
25 #define STAGE_WIDTH 800
26 #define STAGE_HEIGHT 550
30 static GSList *events = NULL;
31 static const ClutterColor const static_colors[] = {
32 { 0xff, 0x00, 0x00, 0xff }, /* red */
33 { 0x80, 0x00, 0x00, 0xff }, /* dark red */
34 { 0x00, 0xff, 0x00, 0xff }, /* green */
35 { 0x00, 0x80, 0x00, 0xff }, /* dark green */
36 { 0x00, 0x00, 0xff, 0xff }, /* blue */
37 { 0x00, 0x00, 0x80, 0xff }, /* dark blue */
38 { 0x00, 0xff, 0xff, 0xff }, /* cyan */
39 { 0x00, 0x80, 0x80, 0xff }, /* dark cyan */
40 { 0xff, 0x00, 0xff, 0xff }, /* magenta */
41 { 0xff, 0xff, 0x00, 0xff }, /* yellow */
43 static GHashTable *sequence_to_color = NULL;
46 canvas_paint (ClutterCairoTexture *canvas)
48 clutter_cairo_texture_invalidate (canvas);
52 draw_touch (ClutterEvent *event,
55 ClutterEventSequence *sequence = clutter_event_get_event_sequence (event);
56 const ClutterColor *color;
58 color = g_hash_table_lookup (sequence_to_color, sequence);
61 color = &static_colors[g_random_int_range (0, NUM_COLORS)];
62 g_hash_table_insert (sequence_to_color, (gpointer) sequence, (gpointer) color);
65 cairo_set_source_rgba (cr, color->red / 255,
69 cairo_arc (cr, event->touch.x, event->touch.y, 5, 0, 2 * G_PI);
74 draw_touches (ClutterCairoTexture *canvas,
77 g_slist_foreach (events, (GFunc) draw_touch, cr);
83 event_cb (ClutterActor *actor, ClutterEvent *event, ClutterActor *canvas)
85 if (event->type != CLUTTER_TOUCH_UPDATE)
88 events = g_slist_append (events, clutter_event_copy (event));
90 clutter_actor_queue_redraw (canvas);
96 rect_event_cb (ClutterActor *actor, ClutterEvent *event, gpointer data)
100 if (event->type != CLUTTER_TOUCH_BEGIN)
103 color = static_colors[g_random_int_range (0, NUM_COLORS)];
104 clutter_rectangle_set_color (CLUTTER_RECTANGLE (actor), &color);
110 test_touch_events_main (int argc, char *argv[])
112 ClutterActor *stage, *canvas;
115 clutter_x11_enable_xinput ();
117 /* initialize Clutter */
118 if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
121 /* create a resizable stage */
122 stage = clutter_stage_new ();
123 g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
124 clutter_stage_set_title (CLUTTER_STAGE (stage), "Touch events");
125 clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE);
126 clutter_actor_set_size (stage, STAGE_WIDTH, STAGE_HEIGHT);
127 clutter_actor_set_reactive (stage, TRUE);
128 clutter_actor_show (stage);
130 /* our 2D canvas, courtesy of Cairo */
131 canvas = clutter_cairo_texture_new (1, 1);
132 g_signal_connect (canvas, "paint", G_CALLBACK (canvas_paint), NULL);
133 g_signal_connect (canvas, "draw", G_CALLBACK (draw_touches), NULL);
134 clutter_cairo_texture_set_auto_resize (CLUTTER_CAIRO_TEXTURE (canvas), TRUE);
135 clutter_actor_add_constraint (canvas,
136 clutter_bind_constraint_new (stage,
139 clutter_container_add_actor (CLUTTER_CONTAINER (stage), canvas);
141 g_signal_connect (stage, "event", G_CALLBACK (event_cb), canvas);
143 for (i = 0; i < NUM_ACTORS; i++)
145 gfloat size = STAGE_HEIGHT / NUM_ACTORS;
146 ClutterColor color = static_colors[i % NUM_COLORS];
147 ClutterActor *rectangle = clutter_rectangle_new_with_color (&color);
149 /* Test that event delivery to actors work */
150 g_signal_connect (rectangle, "event", G_CALLBACK (rect_event_cb), NULL);
152 clutter_container_add_actor (CLUTTER_CONTAINER (stage), rectangle);
153 clutter_actor_set_size (rectangle, size, size);
154 clutter_actor_set_position (rectangle, 0, i * size);
155 clutter_actor_set_reactive (rectangle, TRUE);
158 sequence_to_color = g_hash_table_new (NULL, NULL);
162 g_slist_free_full (events, (GDestroyNotify) clutter_event_free);
163 g_hash_table_destroy (sequence_to_color);
168 G_MODULE_EXPORT const char *
169 test_touch_events_describe (void)
171 return "Draw shapes based on touch events";