Debian Codegraph @

tests/interactive/test-pixmap.c

raw
1 #include <stdlib.h>
2 #include <string.h>
3 #include <gmodule.h>
4
5 #include <cairo.h>
6
7 #ifdef CAIRO_HAS_XLIB_SURFACE
8 #include <cairo-xlib.h>
9 #endif
10
11 #include <clutter/clutter.h>
12
13 #ifdef CLUTTER_WINDOWING_X11
14 #include <X11/Xlib.h>
15 #include <X11/extensions/Xcomposite.h>
16
17 #include <clutter/x11/clutter-x11.h>
18 #include <clutter/x11/clutter-x11-texture-pixmap.h>
19 #endif
20
21 #define IMAGE TESTS_DATADIR G_DIR_SEPARATOR_S "redhand.png"
22
23 static gboolean disable_x11 = FALSE;
24 static gboolean disable_animation = FALSE;
25
26 static GOptionEntry g_options[] =
27 {
28 { "disable-x11",
29 0, 0,
30 G_OPTION_ARG_NONE,
31 &disable_x11,
32 "Disable redirection through X11 pixmap",
33 NULL },
34 { "disable-animation",
35 0, 0,
36 G_OPTION_ARG_NONE,
37 &disable_animation,
38 "Disable the animations",
39 NULL },
40
41 { NULL }
42 };
43
44 static void
45 toggle_texture_quality (ClutterActor *actor)
46 {
47 if (CLUTTER_IS_CONTAINER (actor))
48 clutter_container_foreach (CLUTTER_CONTAINER (actor),
49 (ClutterCallback) toggle_texture_quality,
50 NULL);
51
52 if (CLUTTER_IS_TEXTURE (actor))
53 {
54 ClutterTextureQuality quality;
55
56 quality = clutter_texture_get_filter_quality (CLUTTER_TEXTURE (actor));
57
58 if (quality == CLUTTER_TEXTURE_QUALITY_HIGH)
59 quality = CLUTTER_TEXTURE_QUALITY_MEDIUM;
60 else
61 quality = CLUTTER_TEXTURE_QUALITY_HIGH;
62
63 g_print ("switching to quality %s for %p\n",
64 quality == CLUTTER_TEXTURE_QUALITY_HIGH
65 ? "high" : "medium",
66 actor);
67
68 clutter_texture_set_filter_quality (CLUTTER_TEXTURE (actor), quality);
69 }
70 }
71
72 static gboolean
73 stage_key_release_cb (ClutterActor *actor,
74 ClutterEvent *event,
75 gpointer data)
76 {
77 switch (clutter_event_get_key_symbol (event))
78 {
79 case CLUTTER_KEY_q:
80 case CLUTTER_KEY_Q:
81 clutter_main_quit ();
82 break;
83
84 case CLUTTER_KEY_m:
85 toggle_texture_quality (actor);
86 break;
87 }
88 return FALSE;
89 }
90
91 static gboolean
92 draw_arc (gpointer data)
93 {
94 Pixmap pixmap = GPOINTER_TO_UINT (data);
95 Display *dpy = clutter_x11_get_default_display ();
96
97 static GC gc = None;
98 static int x = 100, y = 100;
99
100 if (gc == None)
101 {
102 XGCValues gc_values = { 0 };
103
104 gc_values.line_width = 12;
105 /* This is an attempt to get a black pixel will full
106 opacity. Seemingly the BlackPixel macro and the default GC
107 value are a fully transparent color */
108 gc_values.foreground = 0xff000000;
109
110 gc = XCreateGC (dpy,
111 pixmap,
112 GCLineWidth | GCForeground,
113 &gc_values);
114 }
115
116 XDrawArc (dpy, pixmap, gc, x, y, 100, 100, 0, 360 * 64);
117
118 x -= 5;
119 y -= 5;
120
121 return G_SOURCE_CONTINUE;
122 }
123
124 static gboolean
125 stage_button_press_cb (ClutterActor *actor,
126 ClutterEvent *event,
127 gpointer data)
128 {
129 draw_arc (data);
130
131 return CLUTTER_EVENT_STOP;
132 }
133
134 Pixmap
135 create_pixmap (guint *width, guint *height, guint *depth)
136 {
137 Display *dpy = clutter_x11_get_default_display ();
138 cairo_surface_t *image;
139 Pixmap pixmap;
140 XVisualInfo xvisinfo;
141 XVisualInfo *xvisinfos;
142 int n;
143 cairo_surface_t *xlib_surface;
144 cairo_t *cr;
145 guint w, h;
146
147 image = cairo_image_surface_create_from_png (IMAGE);
148 if (cairo_surface_status (image) != CAIRO_STATUS_SUCCESS)
149 g_error ("Failed to load %s", IMAGE);
150
151 w = cairo_image_surface_get_width (image);
152 h = cairo_image_surface_get_height (image);
153
154 pixmap = XCreatePixmap (dpy,
155 DefaultRootWindow (dpy),
156 w, h,
157 32);
158
159 xvisinfo.depth = 32;
160 xvisinfos = XGetVisualInfo (dpy, VisualDepthMask, &xvisinfo, &n);
161 if (!xvisinfos)
162 g_error ("Failed to find a 32bit X Visual");
163
164 xlib_surface =
165 cairo_xlib_surface_create (dpy,
166 pixmap,
167 xvisinfos->visual,
168 w, h);
169 XFree (xvisinfos);
170
171 cr = cairo_create (xlib_surface);
172 cairo_set_source_surface (cr, image, 0, 0);
173 cairo_paint (cr);
174 cairo_surface_destroy (image);
175
176 if (width)
177 *width = w;
178 if (height)
179 *height = h;
180 if (depth)
181 *depth = 32;
182
183 return pixmap;
184 }
185
186 /* each time the timeline animating the label completes, swap the direction */
187 static void
188 timeline_completed (ClutterTimeline *timeline,
189 gpointer user_data)
190 {
191 clutter_timeline_set_direction (timeline,
192 !clutter_timeline_get_direction (timeline));
193 clutter_timeline_start (timeline);
194 }
195
196 G_MODULE_EXPORT int
197 test_pixmap_main (int argc, char **argv)
198 {
199 GOptionContext *context;
200 Display *xdpy;
201 int screen;
202 ClutterActor *group = NULL, *label, *stage, *tex;
203 Pixmap pixmap;
204 const ClutterColor gry = { 0x99, 0x99, 0x99, 0xFF };
205 Window win_remote;
206 guint w, h, d;
207 GC gc;
208 ClutterTimeline *timeline;
209 ClutterAlpha *alpha;
210 ClutterBehaviour *depth_behavior;
211 int i;
212 int row_height;
213
214 #ifdef CLUTTER_WINDOWING_X11
215 clutter_set_windowing_backend (CLUTTER_WINDOWING_X11);
216 #endif
217
218 if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
219 return 1;
220
221 #ifdef CLUTTER_WINDOWING_X11
222 if (!clutter_check_windowing_backend (CLUTTER_WINDOWING_X11))
223 g_error ("test-pixmap requires the X11 Clutter backend.");
224 #endif
225
226 xdpy = clutter_x11_get_default_display ();
227 XSynchronize (xdpy, True);
228
229 context = g_option_context_new (" - test-pixmap options");
230 g_option_context_add_main_entries (context, g_options, NULL);
231 g_option_context_parse (context, &argc, &argv, NULL);
232
233 pixmap = create_pixmap (&w, &h, &d);
234
235 screen = DefaultScreen(xdpy);
236 win_remote = XCreateSimpleWindow (xdpy, DefaultRootWindow(xdpy),
237 0, 0, 200, 200,
238 0,
239 WhitePixel(xdpy, screen),
240 WhitePixel(xdpy, screen));
241
242 XMapWindow (xdpy, win_remote);
243
244 stage = clutter_stage_new ();
245 clutter_actor_set_position (stage, 0, 150);
246 clutter_actor_set_background_color (stage, &gry);
247 clutter_stage_set_title (CLUTTER_STAGE (stage), "X11 Texture from Pixmap");
248 g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
249
250 timeline = clutter_timeline_new (5000);
251 g_signal_connect (timeline,
252 "completed", G_CALLBACK (timeline_completed),
253 NULL);
254
255 alpha = clutter_alpha_new_full (timeline, CLUTTER_LINEAR);
256 depth_behavior = clutter_behaviour_depth_new (alpha, -2500, 400);
257
258 if (!disable_x11)
259 {
260 group = clutter_group_new ();
261 clutter_container_add_actor (CLUTTER_CONTAINER (stage), group);
262 label = clutter_text_new_with_text ("fixed",
263 "ClutterX11Texture (Window)");
264 clutter_container_add_actor (CLUTTER_CONTAINER (group), label);
265 tex = clutter_x11_texture_pixmap_new_with_window (win_remote);
266 clutter_container_add_actor (CLUTTER_CONTAINER (group), tex);
267 clutter_actor_set_position (tex, 0, 20);
268 clutter_x11_texture_pixmap_set_automatic (CLUTTER_X11_TEXTURE_PIXMAP (tex),
269 TRUE);
270 clutter_texture_set_filter_quality (CLUTTER_TEXTURE (tex),
271 CLUTTER_TEXTURE_QUALITY_HIGH);
272 clutter_actor_set_position (group, 0, 0);
273 if (!disable_animation)
274 clutter_behaviour_apply (depth_behavior, group);
275 }
276
277 if (group)
278 row_height = clutter_actor_get_height (group);
279 else
280 row_height = 0;
281
282 /* NB: We only draw on the window after being redirected, so we dont
283 * have to worry about handling expose events... */
284 gc = XCreateGC (xdpy, win_remote, 0, NULL);
285 XSetForeground (xdpy, gc, BlackPixel (xdpy, screen));
286 XSetLineAttributes(xdpy, gc, 5, LineSolid, CapButt, JoinMiter);
287
288 for (i = 0; i < 10; i++)
289 XDrawLine (xdpy, win_remote, gc, 0+i*20, 0, 10+i*20+i, 200);
290
291
292 group = clutter_group_new ();
293 clutter_container_add_actor (CLUTTER_CONTAINER (stage), group);
294 label = clutter_text_new_with_text ("fixed", "ClutterX11Texture (Pixmap)");
295 clutter_container_add_actor (CLUTTER_CONTAINER (group), label);
296 tex = clutter_x11_texture_pixmap_new_with_pixmap (pixmap);
297 clutter_x11_texture_pixmap_set_automatic (CLUTTER_X11_TEXTURE_PIXMAP (tex),
298 TRUE);
299 clutter_container_add_actor (CLUTTER_CONTAINER (group), tex);
300 clutter_actor_set_position (tex, 0, 20);
301 clutter_texture_set_filter_quality (CLUTTER_TEXTURE (tex),
302 CLUTTER_TEXTURE_QUALITY_HIGH);
303 /* oddly, the actor's size is 0 until it is realized, even though
304 pixmap-height is set */
305 clutter_actor_set_position (group, 0, row_height);
306 if (!disable_animation)
307 clutter_behaviour_apply (depth_behavior, group);
308
309
310 g_signal_connect (stage, "key-release-event",
311 G_CALLBACK (stage_key_release_cb), (gpointer)pixmap);
312 g_signal_connect (stage, "button-press-event",
313 G_CALLBACK (stage_button_press_cb), (gpointer)pixmap);
314
315 clutter_actor_show (stage);
316
317 if (!disable_animation)
318 clutter_timeline_start (timeline);
319
320 clutter_threads_add_timeout (1000, draw_arc, GUINT_TO_POINTER (pixmap));
321
322 clutter_main ();
323
324 return EXIT_SUCCESS;
325 }
326
327 G_MODULE_EXPORT const char *
328 test_pixmap_describe (void)
329 {
330 return "GLX Texture from pixmap extension support.";
331 }