Debian Codegraph @

tests/interactive/test-stage-read-pixels.c

raw
1 #include <gmodule.h>
2 #include <clutter/clutter.h>
3 #include <string.h>
4
5 #define DOT_SIZE 2
6 #define TEX_SIZE 64
7
8 typedef struct _CallbackData CallbackData;
9
10 struct _CallbackData
11 {
12 ClutterActor *stage;
13 ClutterActor *tex;
14 ClutterActor *box;
15 ClutterMotionEvent event;
16 guint idle_source;
17 };
18
19 static ClutterActor *
20 make_label (void)
21 {
22 ClutterActor *label;
23 gchar *text;
24 gchar *argv[] = { "ls", "--help", NULL };
25
26 label = clutter_text_new ();
27 clutter_text_set_font_name (CLUTTER_TEXT (label), "Sans 10");
28
29 if (g_spawn_sync (NULL, argv, NULL,
30 G_SPAWN_STDERR_TO_DEV_NULL | G_SPAWN_SEARCH_PATH,
31 NULL, NULL, &text, NULL, NULL, NULL))
32 {
33 clutter_text_set_text (CLUTTER_TEXT (label), text);
34 g_free (text);
35 }
36
37 return label;
38 }
39
40 static ClutterActor *
41 make_tex (void)
42 {
43 ClutterActor *tex = clutter_texture_new ();
44
45 clutter_actor_set_size (tex, TEX_SIZE * 2, TEX_SIZE * 2);
46
47 return tex;
48 }
49
50 static ClutterActor *
51 make_box (void)
52 {
53 ClutterActor *box;
54 static const ClutterColor blue = { 0x00, 0x00, 0xff, 0xff };
55
56 box = clutter_rectangle_new_with_color (&blue);
57 clutter_actor_set_size (box, DOT_SIZE + 2, DOT_SIZE + 2);
58 clutter_actor_hide (box);
59
60 return box;
61 }
62
63 static gboolean
64 on_motion_idle (gpointer user_data)
65 {
66 CallbackData *data = (CallbackData *) user_data;
67 guchar *pixels, *p;
68 gfloat stage_width, stage_height;
69 gint x, y;
70
71 data->idle_source = 0;
72
73 clutter_actor_get_size (data->stage, &stage_width, &stage_height);
74
75 x = CLAMP (data->event.x - TEX_SIZE / 2, 0, stage_width - TEX_SIZE);
76 y = CLAMP (data->event.y - TEX_SIZE / 2, 0, stage_height - TEX_SIZE);
77
78 clutter_actor_set_position (data->box,
79 x + TEX_SIZE / 2 - 1,
80 y + TEX_SIZE / 2 - 1);
81 clutter_actor_show (data->box);
82
83 /* Redraw so that the layouting will be done and the box will be
84 drawn in the right position */
85 clutter_stage_ensure_redraw (CLUTTER_STAGE (data->stage));
86
87 pixels = clutter_stage_read_pixels (CLUTTER_STAGE (data->stage),
88 x, y,
89 TEX_SIZE, TEX_SIZE);
90
91 /* Make a red dot in the center */
92 p = pixels + (TEX_SIZE / 2 - DOT_SIZE / 2) * TEX_SIZE * 4
93 + (TEX_SIZE / 2 - DOT_SIZE / 2) * 4;
94 for (y = 0; y < DOT_SIZE; y++)
95 {
96 for (x = 0; x < DOT_SIZE; x++)
97 {
98 *(p++) = 255;
99 memset (p, 0, 3);
100 p += 3;
101 }
102 p += TEX_SIZE * 4 - DOT_SIZE * 4;
103 }
104
105 /* Set all of the alpa values to full */
106 for (p = pixels + TEX_SIZE * TEX_SIZE * 4; p > pixels; p -= 4)
107 *(p - 1) = 255;
108
109 clutter_texture_set_from_rgb_data (CLUTTER_TEXTURE (data->tex),
110 pixels, TRUE,
111 TEX_SIZE, TEX_SIZE,
112 TEX_SIZE * 4, 4, 0, NULL);
113 g_free (pixels);
114
115 return FALSE;
116 }
117
118 static gboolean
119 on_motion (ClutterActor *stage, ClutterMotionEvent *event, CallbackData *data)
120 {
121 /* Handle the motion event in an idle handler so that multiple
122 events will be combined into one */
123 if (data->idle_source == 0)
124 data->idle_source = clutter_threads_add_idle (on_motion_idle, data);
125
126 data->event = *event;
127
128 return FALSE;
129 }
130
131 G_MODULE_EXPORT int
132 test_stage_read_pixels_main (int argc, char **argv)
133 {
134 CallbackData data;
135
136 if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
137 return 1;
138
139 data.idle_source = 0;
140 data.stage = clutter_stage_new ();
141 clutter_stage_set_title (CLUTTER_STAGE (data.stage), "Read Pixels");
142 g_signal_connect (data.stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
143
144 data.tex = make_tex ();
145 data.box = make_box ();
146 clutter_actor_set_position (data.tex,
147 clutter_actor_get_width (data.stage)
148 - clutter_actor_get_width (data.tex),
149 clutter_actor_get_height (data.stage)
150 - clutter_actor_get_height (data.tex));
151
152 clutter_container_add (CLUTTER_CONTAINER (data.stage),
153 make_label (), data.tex, data.box, NULL);
154
155 g_signal_connect (data.stage, "motion-event", G_CALLBACK (on_motion), &data);
156
157 clutter_actor_show (data.stage);
158
159 clutter_main ();
160
161 return 0;
162 }
163
164 G_MODULE_EXPORT const char *
165 test_stage_read_pixels_describe (void)
166 {
167 return "Read back pixels from a Stage.";
168 }