interactive/touch-events: Protect x11-specific calls
[profile/ivi/clutter.git] / tests / interactive / test-touch-events.c
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 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 */
42 };
43 static GHashTable *sequence_to_color = NULL;
44
45 static void
46 canvas_paint (ClutterCairoTexture *canvas)
47 {
48   clutter_cairo_texture_invalidate (canvas);
49 }
50
51 static void
52 draw_touch (ClutterEvent *event,
53             cairo_t      *cr)
54 {
55   ClutterEventSequence *sequence = clutter_event_get_event_sequence (event);
56   const ClutterColor *color;
57
58   color = g_hash_table_lookup (sequence_to_color, sequence);
59   if (color == NULL)
60     {
61       color = &static_colors[g_random_int_range (0, NUM_COLORS)];
62       g_hash_table_insert (sequence_to_color, (gpointer) sequence, (gpointer) color);
63     }
64
65   cairo_set_source_rgba (cr, color->red / 255,
66                              color->green / 255,
67                              color->blue / 255,
68                              color->alpha / 255);
69   cairo_arc (cr, event->touch.x, event->touch.y, 5, 0, 2 * G_PI);
70   cairo_fill (cr);
71 }
72
73 static gboolean
74 draw_touches (ClutterCairoTexture *canvas,
75               cairo_t             *cr)
76 {
77   g_slist_foreach (events, (GFunc) draw_touch, cr);
78
79   return TRUE;
80 }
81
82 static gboolean
83 event_cb (ClutterActor *actor, ClutterEvent *event, ClutterActor *canvas)
84 {
85   if (event->type != CLUTTER_TOUCH_UPDATE)
86     return FALSE;
87
88   events = g_slist_append (events, clutter_event_copy (event));
89
90   clutter_actor_queue_redraw (canvas);
91
92   return TRUE;
93 }
94
95 static gboolean
96 rect_event_cb (ClutterActor *actor, ClutterEvent *event, gpointer data)
97 {
98   ClutterColor color;
99
100   if (event->type != CLUTTER_TOUCH_BEGIN)
101     return FALSE;
102
103   color = static_colors[g_random_int_range (0, NUM_COLORS)];
104   clutter_rectangle_set_color (CLUTTER_RECTANGLE (actor), &color);
105
106   return TRUE;
107 }
108
109 G_MODULE_EXPORT int
110 test_touch_events_main (int argc, char *argv[])
111 {
112   ClutterActor *stage, *canvas;
113   int i;
114
115 #ifdef CLUTTER_WINDOWING_X11
116   clutter_x11_enable_xinput ();
117 #endif
118
119   /* initialize Clutter */
120   if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
121     return EXIT_FAILURE;
122
123   /* create a resizable stage */
124   stage = clutter_stage_new ();
125   g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
126   clutter_stage_set_title (CLUTTER_STAGE (stage), "Touch events");
127   clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE);
128   clutter_actor_set_size (stage, STAGE_WIDTH, STAGE_HEIGHT);
129   clutter_actor_set_reactive (stage, TRUE);
130   clutter_actor_show (stage);
131
132   /* our 2D canvas, courtesy of Cairo */
133   canvas = clutter_cairo_texture_new (1, 1);
134   g_signal_connect (canvas, "paint", G_CALLBACK (canvas_paint), NULL);
135   g_signal_connect (canvas, "draw", G_CALLBACK (draw_touches), NULL);
136   clutter_cairo_texture_set_auto_resize (CLUTTER_CAIRO_TEXTURE (canvas), TRUE);
137   clutter_actor_add_constraint (canvas,
138                                 clutter_bind_constraint_new (stage,
139                                                              CLUTTER_BIND_SIZE,
140                                                              0));
141   clutter_container_add_actor (CLUTTER_CONTAINER (stage), canvas);
142
143   g_signal_connect (stage, "event", G_CALLBACK (event_cb), canvas);
144
145   for (i = 0; i < NUM_ACTORS; i++)
146     {
147       gfloat size = STAGE_HEIGHT / NUM_ACTORS;
148       ClutterColor color = static_colors[i % NUM_COLORS];
149       ClutterActor *rectangle = clutter_rectangle_new_with_color (&color);
150
151       /* Test that event delivery to actors work */
152       g_signal_connect (rectangle, "event", G_CALLBACK (rect_event_cb), NULL);
153       
154       clutter_container_add_actor (CLUTTER_CONTAINER (stage), rectangle);
155       clutter_actor_set_size (rectangle, size, size);
156       clutter_actor_set_position (rectangle, 0, i * size);
157       clutter_actor_set_reactive (rectangle, TRUE);
158     }
159
160   sequence_to_color = g_hash_table_new (NULL, NULL);
161
162   clutter_main ();
163
164   g_slist_free_full (events, (GDestroyNotify) clutter_event_free);
165   g_hash_table_destroy (sequence_to_color);
166
167   return EXIT_SUCCESS;
168 }
169
170 G_MODULE_EXPORT const char *
171 test_touch_events_describe (void)
172 {
173   return "Draw shapes based on touch events";
174 }