diff --git a/examples/cogl-crate.c b/examples/cogl-crate.c index fa9d86f2d..393e670a6 100644 --- a/examples/cogl-crate.c +++ b/examples/cogl-crate.c @@ -272,8 +272,16 @@ main (int argc, char **argv) while (1) { + CoglPollFD *poll_fds; + int n_poll_fds; + gint64 timeout; + paint (&data); cogl_framebuffer_swap_buffers (fb); + + cogl_poll_get_info (ctx, &poll_fds, &n_poll_fds, &timeout); + g_poll ((GPollFD *) poll_fds, n_poll_fds, 0); + cogl_poll_dispatch (ctx, poll_fds, n_poll_fds); } return 0; diff --git a/examples/cogl-hello.c b/examples/cogl-hello.c index 775cc1aad..e8e4f5b4f 100644 --- a/examples/cogl-hello.c +++ b/examples/cogl-hello.c @@ -40,9 +40,17 @@ main (int argc, char **argv) triangle = cogl_primitive_new_p2c4 (COGL_VERTICES_MODE_TRIANGLES, 3, triangle_vertices); for (;;) { + CoglPollFD *poll_fds; + int n_poll_fds; + gint64 timeout; + cogl_clear (&black, COGL_BUFFER_BIT_COLOR); cogl_primitive_draw (triangle); cogl_framebuffer_swap_buffers (fb); + + cogl_poll_get_info (ctx, &poll_fds, &n_poll_fds, &timeout); + g_poll ((GPollFD *) poll_fds, n_poll_fds, 0); + cogl_poll_dispatch (ctx, poll_fds, n_poll_fds); } return 0; diff --git a/examples/cogl-msaa.c b/examples/cogl-msaa.c index 8d7d358b0..aecc90363 100644 --- a/examples/cogl-msaa.c +++ b/examples/cogl-msaa.c @@ -88,6 +88,10 @@ main (int argc, char **argv) pipeline = cogl_pipeline_new (); for (;;) { + CoglPollFD *poll_fds; + int n_poll_fds; + gint64 timeout; + cogl_clear (&black, COGL_BUFFER_BIT_COLOR); cogl_push_matrix (); @@ -106,6 +110,10 @@ main (int argc, char **argv) cogl_rectangle (0, 1, 1, -1); cogl_framebuffer_swap_buffers (fb); + + cogl_poll_get_info (ctx, &poll_fds, &n_poll_fds, &timeout); + g_poll ((GPollFD *) poll_fds, n_poll_fds, 0); + cogl_poll_dispatch (ctx, poll_fds, n_poll_fds); } return 0; diff --git a/examples/cogl-sdl-hello.c b/examples/cogl-sdl-hello.c index 315adf2e9..b74e8a2a5 100644 --- a/examples/cogl-sdl-hello.c +++ b/examples/cogl-sdl-hello.c @@ -12,6 +12,7 @@ typedef struct Data CoglPrimitive *triangle; float center_x, center_y; CoglFramebuffer *fb; + gboolean quit; } Data; static void @@ -28,6 +29,85 @@ redraw (Data *data) cogl_framebuffer_swap_buffers (data->fb); } +static void +handle_event (Data *data, SDL_Event *event) +{ + switch (event->type) + { + case SDL_VIDEOEXPOSE: + redraw (data); + break; + + case SDL_MOUSEMOTION: + { + int width = + cogl_framebuffer_get_width (COGL_FRAMEBUFFER (data->fb)); + int height = + cogl_framebuffer_get_height (COGL_FRAMEBUFFER (data->fb)); + + data->center_x = event->motion.x * 2.0f / width - 1.0f; + data->center_y = event->motion.y * 2.0f / height - 1.0f; + + redraw (data); + } + break; + + case SDL_QUIT: + data->quit = TRUE; + break; + } +} + +static Uint32 +timer_handler (Uint32 interval, void *user_data) +{ + static const SDL_UserEvent dummy_event = + { + SDL_USEREVENT + }; + + /* Post an event to wake up from SDL_WaitEvent */ + SDL_PushEvent ((SDL_Event *) &dummy_event); + + return 0; +} + +static gboolean +wait_event_with_timeout (Data *data, SDL_Event *event, gint64 timeout) +{ + if (timeout == -1) + { + if (SDL_WaitEvent (event)) + return TRUE; + else + { + data->quit = TRUE; + return FALSE; + } + } + else if (timeout == 0) + return SDL_PollEvent (event); + else + { + gboolean ret; + /* Add a timer so that we can wake up the event loop */ + SDL_TimerID timer_id = + SDL_AddTimer (timeout / 1000, timer_handler, data); + + if (SDL_WaitEvent (event)) + ret = TRUE; + else + { + data->quit = TRUE; + ret = FALSE; + } + + SDL_RemoveTimer (timer_id); + + return ret; + } +} + int main (int argc, char **argv) { @@ -55,6 +135,8 @@ main (int argc, char **argv) return 1; } + SDL_InitSubSystem (SDL_INIT_TIMER); + onscreen = cogl_onscreen_new (ctx, 800, 600); /* Eventually there will be an implicit allocate on first use so this * will become optional... */ @@ -69,6 +151,7 @@ main (int argc, char **argv) cogl_color_set_from_4ub (&data.black, 0, 0, 0, 255); data.center_x = 0.0f; data.center_y = 0.0f; + data.quit = FALSE; cogl_onscreen_show (onscreen); @@ -76,38 +159,28 @@ main (int argc, char **argv) data.triangle = cogl_primitive_new_p2c4 (COGL_VERTICES_MODE_TRIANGLES, 3, triangle_vertices); - while (SDL_WaitEvent (&event)) + while (!data.quit) { - switch (event.type) - { - case SDL_VIDEOEXPOSE: - redraw (&data); - break; + CoglPollFD *poll_fds; + int n_poll_fds; + gint64 timeout; - case SDL_MOUSEMOTION: - { - int width = - cogl_framebuffer_get_width (COGL_FRAMEBUFFER (data.fb)); - int height = - cogl_framebuffer_get_height (COGL_FRAMEBUFFER (data.fb)); + cogl_poll_get_info (ctx, &poll_fds, &n_poll_fds, &timeout); - data.center_x = event.motion.x * 2.0f / width - 1.0f; - data.center_y = event.motion.y * 2.0f / height - 1.0f; + /* It's difficult to wait for file descriptors using the SDL + event mechanism, but it the SDL winsys is documented that it + will never require this so we can assert that there are no + fds */ + g_assert (n_poll_fds == 0); - redraw (&data); - } - break; + if (wait_event_with_timeout (&data, &event, timeout)) + do + handle_event (&data, &event); + while (SDL_PollEvent (&event)); - case SDL_QUIT: - goto done; - } + cogl_poll_dispatch (ctx, poll_fds, n_poll_fds); } - fprintf (stderr, "Error waiting for an event: %s\n", - SDL_GetError ()); - - done: - cogl_pop_framebuffer (); cogl_object_unref (ctx); diff --git a/examples/cogl-x11-foreign.c b/examples/cogl-x11-foreign.c index 6c9ac40b2..b1d6acf3a 100644 --- a/examples/cogl-x11-foreign.c +++ b/examples/cogl-x11-foreign.c @@ -163,6 +163,10 @@ main (int argc, char **argv) 3, triangle_vertices); for (;;) { + CoglPollFD *poll_fds; + int n_poll_fds; + gint64 timeout; + while (XPending (xdpy)) { XEvent event; @@ -178,6 +182,10 @@ main (int argc, char **argv) cogl_clear (&black, COGL_BUFFER_BIT_COLOR); cogl_primitive_draw (triangle); cogl_framebuffer_swap_buffers (fb); + + cogl_poll_get_info (ctx, &poll_fds, &n_poll_fds, &timeout); + g_poll ((GPollFD *) poll_fds, n_poll_fds, 0); + cogl_poll_dispatch (ctx, poll_fds, n_poll_fds); } return 0; diff --git a/examples/cogland.c b/examples/cogland.c index e7f0a8339..03acf398b 100644 --- a/examples/cogland.c +++ b/examples/cogland.c @@ -705,6 +705,7 @@ main (int argc, char **argv) {-0.7, -0.7, 0x00, 0xff, 0x00, 0xff}, {0.7, -0.7, 0x00, 0x00, 0xff, 0xff} }; + GSource *cogl_source; memset (&compositor, 0, sizeof (compositor)); @@ -761,6 +762,11 @@ main (int argc, char **argv) g_timeout_add (16, paint_cb, &compositor); + cogl_source = cogl_glib_source_new (compositor.cogl_context, + G_PRIORITY_DEFAULT); + + g_source_attach (cogl_source, NULL); + g_main_loop_run (loop); return 0;