Debian Codegraph @

tests/interactive/test-touch-events.c

raw
1 /*
2 * Copyright (C) 2012 Collabora Ltd.
3 *
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.
7 *
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
11 * more details.
12 *
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.
17 *
18 */
19 #include <stdlib.h>
20 #include <math.h>
21 #include <cairo.h>
22 #include <glib.h>
23 #include <clutter/clutter.h>
24
25 #define STAGE_WIDTH 800
26 #define STAGE_HEIGHT 550
27 #define NUM_COLORS 10
28 #define NUM_ACTORS 10
29
30 static GQueue events = G_QUEUE_INIT;
31 static GQueue all_events = G_QUEUE_INIT;
32 static gboolean new_surface = TRUE;
33
34 static const ClutterColor const static_colors[] = {
35 { 0xff, 0x00, 0x00, 0xff }, /* red */
36 { 0x80, 0x00, 0x00, 0xff }, /* dark red */
37 { 0x00, 0xff, 0x00, 0xff }, /* green */
38 { 0x00, 0x80, 0x00, 0xff }, /* dark green */
39 { 0x00, 0x00, 0xff, 0xff }, /* blue */
40 { 0x00, 0x00, 0x80, 0xff }, /* dark blue */
41 { 0x00, 0xff, 0xff, 0xff }, /* cyan */
42 { 0x00, 0x80, 0x80, 0xff }, /* dark cyan */
43 { 0xff, 0x00, 0xff, 0xff }, /* magenta */
44 { 0xff, 0xff, 0x00, 0xff }, /* yellow */
45 };
46 static GHashTable *sequence_to_color = NULL;
47
48 static void
49 canvas_paint (ClutterCairoTexture *canvas)
50 {
51 clutter_cairo_texture_invalidate (canvas);
52 }
53
54 static void
55 draw_touch (ClutterEvent *event,
56 cairo_t *cr)
57 {
58 ClutterEventSequence *sequence = clutter_event_get_event_sequence (event);
59 const ClutterColor *color;
60
61 color = g_hash_table_lookup (sequence_to_color, sequence);
62 if (color == NULL)
63 {
64 color = &static_colors[g_random_int_range (0, NUM_COLORS)];
65 g_hash_table_insert (sequence_to_color, (gpointer) sequence, (gpointer) color);
66 }
67
68 cairo_set_source_rgba (cr, color->red / 255,
69 color->green / 255,
70 color->blue / 255,
71 color->alpha / 255);
72 cairo_arc (cr, event->touch.x, event->touch.y, 5, 0, 2 * G_PI);
73 cairo_fill (cr);
74 }
75
76 static gboolean
77 draw_touches (ClutterCairoTexture *canvas,
78 cairo_t *cr)
79 {
80 g_queue_foreach (new_surface ? &all_events : &events, (GFunc) draw_touch, cr);
81 g_queue_clear (&events);
82
83 new_surface = FALSE;
84
85 return TRUE;
86 }
87
88 static cairo_surface_t *
89 create_surface (ClutterCairoTexture *texture,
90 guint width,
91 guint height,
92 gpointer user_data)
93 {
94 new_surface = TRUE;
95
96 return NULL;
97 }
98
99 static gboolean
100 event_cb (ClutterActor *actor, ClutterEvent *event, ClutterActor *canvas)
101 {
102 ClutterEvent *copy;
103
104 if (event->type != CLUTTER_TOUCH_UPDATE)
105 return FALSE;
106
107 copy = clutter_event_copy (event);
108 g_queue_push_tail (&events, copy);
109 g_queue_push_tail (&all_events, copy);
110 clutter_actor_queue_redraw (canvas);
111
112 return TRUE;
113 }
114
115 static gboolean
116 rect_event_cb (ClutterActor *actor, ClutterEvent *event, gpointer data)
117 {
118 ClutterColor color;
119
120 if (event->type != CLUTTER_TOUCH_BEGIN)
121 return FALSE;
122
123 color = static_colors[g_random_int_range (0, NUM_COLORS)];
124 clutter_rectangle_set_color (CLUTTER_RECTANGLE (actor), &color);
125
126 return TRUE;
127 }
128
129 G_MODULE_EXPORT int
130 test_touch_events_main (int argc, char *argv[])
131 {
132 ClutterActor *stage, *canvas;
133 int i;
134
135 /* initialize Clutter */
136 if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
137 return EXIT_FAILURE;
138
139 /* create a resizable stage */
140 stage = clutter_stage_new ();
141 g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
142 clutter_stage_set_title (CLUTTER_STAGE (stage), "Touch events");
143 clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE);
144 clutter_actor_set_size (stage, STAGE_WIDTH, STAGE_HEIGHT);
145 clutter_actor_set_reactive (stage, TRUE);
146 clutter_actor_show (stage);
147
148 /* our 2D canvas, courtesy of Cairo */
149 canvas = clutter_cairo_texture_new (1, 1);
150 g_signal_connect (canvas, "paint", G_CALLBACK (canvas_paint), NULL);
151 g_signal_connect (canvas, "draw", G_CALLBACK (draw_touches), NULL);
152 g_signal_connect (canvas, "create-surface", G_CALLBACK (create_surface), NULL);
153 clutter_cairo_texture_set_auto_resize (CLUTTER_CAIRO_TEXTURE (canvas), TRUE);
154 clutter_actor_add_constraint (canvas,
155 clutter_bind_constraint_new (stage,
156 CLUTTER_BIND_SIZE,
157 0));
158 clutter_container_add_actor (CLUTTER_CONTAINER (stage), canvas);
159
160 g_signal_connect (stage, "event", G_CALLBACK (event_cb), canvas);
161
162 for (i = 0; i < NUM_ACTORS; i++)
163 {
164 gfloat size = STAGE_HEIGHT / NUM_ACTORS;
165 ClutterColor color = static_colors[i % NUM_COLORS];
166 ClutterActor *rectangle = clutter_rectangle_new_with_color (&color);
167
168 /* Test that event delivery to actors work */
169 g_signal_connect (rectangle, "event", G_CALLBACK (rect_event_cb), NULL);
170
171 clutter_container_add_actor (CLUTTER_CONTAINER (stage), rectangle);
172 clutter_actor_set_size (rectangle, size, size);
173 clutter_actor_set_position (rectangle, 0, i * size);
174 clutter_actor_set_reactive (rectangle, TRUE);
175 }
176
177 sequence_to_color = g_hash_table_new (NULL, NULL);
178
179 clutter_main ();
180
181 g_queue_foreach (&all_events, (GFunc) clutter_event_free, NULL);
182 g_queue_clear (&events);
183 g_queue_clear (&all_events);
184 g_hash_table_destroy (sequence_to_color);
185
186 return EXIT_SUCCESS;
187 }
188
189 G_MODULE_EXPORT const char *
190 test_touch_events_describe (void)
191 {
192 return "Draw shapes based on touch events";
193 }