diff --git a/examples/cogland.c b/examples/cogland.c index 50811adb7..4e95bc1a4 100644 --- a/examples/cogland.c +++ b/examples/cogland.c @@ -8,6 +8,10 @@ #include +#ifdef COGL_HAS_XLIB_SUPPORT +#include +#endif + typedef struct _CoglandCompositor CoglandCompositor; typedef struct @@ -1046,6 +1050,78 @@ create_cogl_context (CoglandCompositor *compositor, return context; } +#ifdef COGL_HAS_XLIB_SUPPORT + +static CoglFilterReturn +x_event_cb (XEvent *event, + void *data) +{ + CoglandCompositor *compositor = data; + + if (event->type == Expose) + cogland_queue_redraw (compositor); + + return COGL_FILTER_CONTINUE; +} + +#endif /* COGL_HAS_XLIB_SUPPORT */ + +static gboolean +timeout_cb (void *data) +{ + cogland_queue_redraw (data); + + return TRUE; +} + +static void +init_redraws (CoglandCompositor *compositor) +{ +#ifdef COGL_HAS_XLIB_SUPPORT + CoglRenderer *renderer = cogl_context_get_renderer (compositor->cogl_context); + CoglWinsysID winsys = cogl_renderer_get_winsys_id (renderer); + + /* If Cogl is using X then we can listen for Expose events to know + * when to repaint the window. Otherwise we don't have any code to + * know when the contents of the window is dirty so we'll just + * redraw constantly */ + switch (winsys) + { + case COGL_WINSYS_ID_GLX: + case COGL_WINSYS_ID_EGL_XLIB: + { + Display *display = cogl_xlib_renderer_get_display (renderer); + GList *l; + + for (l = compositor->outputs; l; l = l->next) + { + CoglandOutput *output = l->data; + XWindowAttributes win_attribs; + Window win; + + win = cogl_x11_onscreen_get_window_xid (output->onscreen); + if (XGetWindowAttributes (display, win, &win_attribs)) + { + XSelectInput (display, + win, + win_attribs.your_event_mask | ExposureMask); + cogl_xlib_renderer_add_filter (renderer, + x_event_cb, + compositor); + + } + } + } + return; + + default: + break; + } +#endif /* COGL_HAS_XLIB_SUPPORT */ + + g_timeout_add (16, timeout_cb, compositor); +} + int main (int argc, char **argv) { @@ -1152,7 +1228,7 @@ main (int argc, char **argv) g_source_attach (cogl_source, NULL); - cogland_queue_redraw (&compositor); + init_redraws (&compositor); g_main_loop_run (loop);