Merge branch 'text-direction'
[profile/ivi/clutter.git] / clutter / clutter-main.c
index 2acee48..e8bfd2e 100644 (file)
 #include <glib/gi18n-lib.h>
 #include <locale.h>
 
+#ifdef USE_GDKPIXBUF
+#include <gdk-pixbuf/gdk-pixbuf.h>
+#endif
+
 #include "clutter-event.h"
 #include "clutter-backend.h"
 #include "clutter-main.h"
+#include "clutter-master-clock.h"
 #include "clutter-feature.h"
 #include "clutter-actor.h"
 #include "clutter-stage.h"
@@ -69,13 +74,17 @@ static gboolean clutter_use_fuzzy_picking    = FALSE;
 
 static guint clutter_default_fps             = 60;
 
-static PangoDirection clutter_text_direction = PANGO_DIRECTION_LTR;
+static PangoDirection clutter_text_direction = CLUTTER_TEXT_DIRECTION_LTR;
 
 static guint clutter_main_loop_level         = 0;
 static GSList *main_loops                    = NULL;
 
 guint clutter_debug_flags = 0;  /* global clutter debug flag */
 
+const guint clutter_major_version = CLUTTER_MAJOR_VERSION;
+const guint clutter_minor_version = CLUTTER_MINOR_VERSION;
+const guint clutter_micro_version = CLUTTER_MICRO_VERSION;
+
 #ifdef CLUTTER_ENABLE_DEBUG
 static const GDebugKey clutter_debug_keys[] = {
   { "misc", CLUTTER_DEBUG_MISC },
@@ -92,7 +101,10 @@ static const GDebugKey clutter_debug_keys[] = {
   { "script", CLUTTER_DEBUG_SCRIPT },
   { "shader", CLUTTER_DEBUG_SHADER },
   { "multistage", CLUTTER_DEBUG_MULTISTAGE },
-  { "animation", CLUTTER_DEBUG_ANIMATION }
+  { "animation", CLUTTER_DEBUG_ANIMATION },
+  { "layout", CLUTTER_DEBUG_LAYOUT },
+  { "nop-picking", CLUTTER_DEBUG_NOP_PICKING },
+  { "dump-pick-buffers", CLUTTER_DEBUG_DUMP_PICK_BUFFERS }
 };
 #endif /* CLUTTER_ENABLE_DEBUG */
 
@@ -117,7 +129,7 @@ clutter_get_show_fps (void)
 void
 _clutter_stage_maybe_relayout (ClutterActor *stage)
 {
-  ClutterUnit natural_width, natural_height;
+  gfloat natural_width, natural_height;
   ClutterActorBox box = { 0, };
 
   /* avoid reentrancy */
@@ -138,10 +150,10 @@ _clutter_stage_maybe_relayout (ClutterActor *stage)
       box.y2 = natural_height;
 
       CLUTTER_NOTE (ACTOR, "Allocating (0, 0 - %d, %d) for the stage",
-                    CLUTTER_UNITS_TO_DEVICE (natural_width),
-                    CLUTTER_UNITS_TO_DEVICE (natural_height));
+                    (int) natural_width,
+                    (int) natural_height);
 
-      clutter_actor_allocate (stage, &box, FALSE);
+      clutter_actor_allocate (stage, &box, CLUTTER_ALLOCATION_NONE);
 
       CLUTTER_UNSET_PRIVATE_FLAGS (stage, CLUTTER_ACTOR_IN_RELAYOUT);
     }
@@ -150,44 +162,41 @@ _clutter_stage_maybe_relayout (ClutterActor *stage)
 void
 _clutter_stage_maybe_setup_viewport (ClutterStage *stage)
 {
-  if (CLUTTER_PRIVATE_FLAGS (stage) & CLUTTER_ACTOR_SYNC_MATRICES)
+  if ((CLUTTER_PRIVATE_FLAGS (stage) & CLUTTER_ACTOR_SYNC_MATRICES) &&
+      !(CLUTTER_PRIVATE_FLAGS (stage) & CLUTTER_STAGE_IN_RESIZE))
     {
       ClutterPerspective perspective;
-      guint width, height;
+      gfloat width, height;
 
-      clutter_actor_get_size (CLUTTER_ACTOR (stage), &width, &height);
-      clutter_stage_get_perspectivex (stage, &perspective);
+      clutter_actor_get_preferred_size (CLUTTER_ACTOR (stage),
+                                        NULL, NULL,
+                                        &width, &height);
+      clutter_stage_get_perspective (stage, &perspective);
 
-      CLUTTER_NOTE (PAINT, "Setting up the viewport");
+      CLUTTER_NOTE (PAINT,
+                    "Setting up the viewport { w:%.2f, h:%.2f }",
+                    width, height);
 
-      cogl_setup_viewport (width, height,
-                          perspective.fovy,
-                          perspective.aspect,
-                          perspective.z_near,
-                          perspective.z_far);
+      _cogl_setup_viewport (width, height,
+                            perspective.fovy,
+                            perspective.aspect,
+                            perspective.z_near,
+                            perspective.z_far);
 
       CLUTTER_UNSET_PRIVATE_FLAGS (stage, CLUTTER_ACTOR_SYNC_MATRICES);
     }
 }
 
-/**
- * clutter_redraw:
- *
- * Forces a redraw of the entire stage. Applications should never use this
- * function, but queue a redraw using clutter_actor_queue_redraw().
- */
 void
-clutter_redraw (ClutterStage *stage)
+_clutter_do_redraw (ClutterStage *stage)
 {
   ClutterMainContext *ctx;
-  static GTimer      *timer = NULL;
-  static guint        timer_n_frames = 0;
-
-  ctx  = clutter_context_get_default ();
+  ClutterMasterClock *master_clock;
+  static GTimer *timer = NULL;
+  static guint timer_n_frames = 0;
 
-  CLUTTER_TIMESTAMP (SCHEDULER, "Redraw start for stage:%p", stage);
-  CLUTTER_NOTE (PAINT, " Redraw enter for stage:%p", stage);
-  CLUTTER_NOTE (MULTISTAGE, "Redraw called for stage:%p", stage);
+  ctx  = _clutter_context_get_default ();
+  master_clock = _clutter_master_clock_get_default ();
 
   /* Before we can paint, we have to be sure we have the latest layout */
   _clutter_stage_maybe_relayout (CLUTTER_ACTOR (stage));
@@ -208,7 +217,7 @@ clutter_redraw (ClutterStage *stage)
 
   /* Call through to the actual backend to do the painting down from
    * the stage. It will likely need to swap buffers, vblank sync etc
-   * which will be windowing system dependant.
+   * which will be windowing system dependent
   */
   _clutter_backend_redraw (ctx->backend, stage);
 
@@ -225,11 +234,27 @@ clutter_redraw (ClutterStage *stage)
        }
     }
 
-  CLUTTER_NOTE (PAINT, " Redraw leave for stage:%p", stage);
   CLUTTER_TIMESTAMP (SCHEDULER, "Redraw finish for stage:%p", stage);
 }
 
 /**
+ * clutter_redraw:
+ *
+ * Forces a redraw of the entire stage. Applications should never use this
+ * function, but queue a redraw using clutter_actor_queue_redraw().
+ *
+ * This function should only be used by libraries integrating Clutter from
+ * within another toolkit.
+ */
+void
+clutter_redraw (ClutterStage *stage)
+{
+  g_return_if_fail (CLUTTER_IS_STAGE (stage));
+
+  clutter_stage_ensure_redraw (stage);
+}
+
+/**
  * clutter_set_motion_events_enabled:
  * @enable: %TRUE to enable per-actor motion events
  *
@@ -249,7 +274,7 @@ clutter_redraw (ClutterStage *stage)
 void
 clutter_set_motion_events_enabled (gboolean enable)
 {
-  ClutterMainContext *context = clutter_context_get_default ();
+  ClutterMainContext *context = _clutter_context_get_default ();
 
   context->motion_events_per_actor = enable;
 }
@@ -266,7 +291,7 @@ clutter_set_motion_events_enabled (gboolean enable)
 gboolean
 clutter_get_motion_events_enabled (void)
 {
-  ClutterMainContext *context = clutter_context_get_default ();
+  ClutterMainContext *context = _clutter_context_get_default ();
 
   return context->motion_events_per_actor;
 }
@@ -281,7 +306,7 @@ static inline void init_bits (void)
   if (G_LIKELY (done))
     return;
 
-  ctx = clutter_context_get_default ();
+  ctx = _clutter_context_get_default ();
 
   done = TRUE;
 }
@@ -290,8 +315,9 @@ void
 _clutter_id_to_color (guint id, ClutterColor *col)
 {
   ClutterMainContext *ctx;
-  gint                red, green, blue;
-  ctx = clutter_context_get_default ();
+  gint red, green, blue;
+
+  ctx = _clutter_context_get_default ();
 
   /* compute the numbers we'll store in the components */
   red   = (id >> (ctx->fb_g_mask_used+ctx->fb_b_mask_used))
@@ -319,6 +345,18 @@ _clutter_id_to_color (guint id, ClutterColor *col)
   col->green = green;
   col->blue  = blue;
   col->alpha = 0xff;
+
+  /* XXX: We rotate the nibbles of the colors here so that there is a
+   * visible variation between colors of sequential actor identifiers;
+   * otherwise pick buffers dumped to an image will pretty much just look
+   * black.
+   */
+  if (G_UNLIKELY (clutter_debug_flags & CLUTTER_DEBUG_DUMP_PICK_BUFFERS))
+    {
+      col->red = (col->red << 4) | (col->red >> 4);
+      col->green = (col->green << 4) | (col->green >> 4);
+      col->blue = (col->blue << 4) | (col->blue >> 4);
+    }
 }
 
 guint
@@ -328,14 +366,33 @@ _clutter_pixel_to_id (guchar pixel[4])
   gint  red, green, blue;
   guint id;
 
-  ctx = clutter_context_get_default ();
+  ctx = _clutter_context_get_default ();
 
   /* reduce the pixel components to the number of bits actually used of the
    * 8bits.
    */
-  red   = pixel[0] >> (8 - ctx->fb_r_mask);
-  green = pixel[1] >> (8 - ctx->fb_g_mask);
-  blue  = pixel[2] >> (8 - ctx->fb_b_mask);
+  if (G_UNLIKELY (clutter_debug_flags & CLUTTER_DEBUG_DUMP_PICK_BUFFERS))
+    {
+      guchar tmp;
+
+      /* XXX: In _clutter_id_to_color we rotated the nibbles of the colors so
+       * that there is a visible variation between colors of sequential actor
+       * identifiers (otherwise pick buffers dumped to an image will pretty
+       * much just look black.) Here we reverse that rotation.
+       */
+      tmp = ((pixel[0] << 4) | (pixel[0] >> 4));
+      red = tmp >> (8 - ctx->fb_r_mask);
+      tmp = ((pixel[1] << 4) | (pixel[1] >> 4));
+      green = tmp >> (8 - ctx->fb_g_mask);
+      tmp = ((pixel[2] << 4) | (pixel[2] >> 4));
+      blue = tmp >> (8 - ctx->fb_b_mask);
+    }
+  else
+    {
+      red   = pixel[0] >> (8 - ctx->fb_r_mask);
+      green = pixel[1] >> (8 - ctx->fb_g_mask);
+      blue  = pixel[2] >> (8 - ctx->fb_b_mask);
+    }
 
   /* divide potentially by two if 'fuzzy' */
   red   = red   >> (ctx->fb_r_mask - ctx->fb_r_mask_used);
@@ -349,6 +406,66 @@ _clutter_pixel_to_id (guchar pixel[4])
   return id;
 }
 
+#ifdef USE_GDKPIXBUF
+static void
+pixbuf_free (guchar *pixels, gpointer data)
+{
+  g_free (pixels);
+}
+#endif
+
+static void
+read_pixels_to_file (char *filename_stem,
+                     int x,
+                     int y,
+                     int width,
+                     int height)
+{
+#ifdef USE_GDKPIXBUF
+  GLubyte *data;
+  GdkPixbuf *pixbuf;
+  static int read_count = 0;
+
+  data = g_malloc (4 * width * height);
+  cogl_read_pixels (x, y, width, height,
+                    COGL_READ_PIXELS_COLOR_BUFFER,
+                    COGL_PIXEL_FORMAT_RGBA_8888,
+                    data);
+  pixbuf = gdk_pixbuf_new_from_data (data,
+                                     GDK_COLORSPACE_RGB,
+                                     TRUE, /* has alpha */
+                                     8, /* bits per sample */
+                                     width, /* width */
+                                     height, /* height */
+                                     width * 4, /* rowstride */
+                                     pixbuf_free, /* callback to free data */
+                                     NULL); /* callback data */
+  if (pixbuf)
+    {
+      char *filename =
+        g_strdup_printf ("%s-%05d.png", filename_stem, read_count);
+      GError *error = NULL;
+      if (!gdk_pixbuf_save (pixbuf, filename, "png", &error, NULL))
+        {
+          g_warning ("Failed to save pick buffer to file %s: %s",
+                     filename, error->message);
+          g_error_free (error);
+        }
+      g_free (filename);
+      g_object_unref (pixbuf);
+      read_count++;
+    }
+#else
+  static gboolean seen = FALSE;
+  if (!seen)
+    {
+      g_warning ("dumping buffers to an image isn't supported on platforms "
+                 "without gdk pixbuf support\n");
+      seen = TRUE;
+    }
+#endif
+}
+
 ClutterActor *
 _clutter_do_pick (ClutterStage   *stage,
                  gint            x,
@@ -356,26 +473,34 @@ _clutter_do_pick (ClutterStage   *stage,
                  ClutterPickMode mode)
 {
   ClutterMainContext *context;
-  guchar              pixel[4];
-  GLint               viewport[4];
-  CoglColor           white;
+  guchar              pixel[4] = { 0xff, 0xff, 0xff, 0xff };
+  CoglColor           stage_pick_id;
   guint32             id;
   GLboolean           dither_was_on;
 
-  context = clutter_context_get_default ();
+  if (clutter_debug_flags & CLUTTER_DEBUG_NOP_PICKING)
+    return CLUTTER_ACTOR (stage);
+
+  context = _clutter_context_get_default ();
 
   _clutter_backend_ensure_context (context->backend, stage);
 
   /* needed for when a context switch happens */
   _clutter_stage_maybe_setup_viewport (stage);
 
-  cogl_color_set_from_4ub (&white, 255, 255, 255, 255);
+  if (G_LIKELY (!(clutter_debug_flags & CLUTTER_DEBUG_DUMP_PICK_BUFFERS)))
+    cogl_clip_push_window_rectangle (x, y, 1, 1);
+
   cogl_disable_fog ();
-  cogl_clear (&white);
+  cogl_color_set_from_4ub (&stage_pick_id, 255, 255, 255, 255);
+  cogl_clear (&stage_pick_id,
+             COGL_BUFFER_BIT_COLOR |
+             COGL_BUFFER_BIT_DEPTH);
 
   /* Disable dithering (if any) when doing the painting in pick mode */
   dither_was_on = glIsEnabled (GL_DITHER);
-  glDisable (GL_DITHER);
+  if (dither_was_on)
+    glDisable (GL_DITHER);
 
   /* Render the entire scence in pick mode - just single colored silhouette's
    * are drawn offscreen (as we never swap buffers)
@@ -384,16 +509,24 @@ _clutter_do_pick (ClutterStage   *stage,
   clutter_actor_paint (CLUTTER_ACTOR (stage));
   context->pick_mode = CLUTTER_PICK_NONE;
 
-  /* Calls should work under both GL and GLES, note GLES needs RGBA */
-  glGetIntegerv(GL_VIEWPORT, viewport);
+  if (G_LIKELY (!(clutter_debug_flags & CLUTTER_DEBUG_DUMP_PICK_BUFFERS)))
+    cogl_clip_pop ();
 
-  /* Below to be safe, particularly on GL ES. an EGL wait call or full
-   * could be nicer.
-  */
-  glFinish();
+  /* Make sure Cogl flushes any batched geometry to the GPU driver */
+  cogl_flush ();
 
   /* Read the color of the screen co-ords pixel */
-  glReadPixels (x, viewport[3] - y -1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
+  cogl_read_pixels (x, y, 1, 1,
+                    COGL_READ_PIXELS_COLOR_BUFFER,
+                    COGL_PIXEL_FORMAT_RGBA_8888,
+                    pixel);
+
+  if (G_UNLIKELY (clutter_debug_flags & CLUTTER_DEBUG_DUMP_PICK_BUFFERS))
+    {
+      read_pixels_to_file ("pick-buffer", 0, 0,
+                           clutter_actor_get_width (CLUTTER_ACTOR (stage)),
+                           clutter_actor_get_height (CLUTTER_ACTOR (stage)));
+    }
 
   /* Restore whether GL_DITHER was enabled */
   if (dither_was_on)
@@ -407,7 +540,7 @@ _clutter_do_pick (ClutterStage   *stage,
   return clutter_get_actor_by_gid (id);
 }
 
-static PangoDirection
+static ClutterTextDirection
 clutter_get_text_direction (void)
 {
   PangoDirection dir = PANGO_DIRECTION_LTR;
@@ -417,9 +550,9 @@ clutter_get_text_direction (void)
   if (direction && *direction != '\0')
     {
       if (strcmp (direction, "rtl") == 0)
-        dir = PANGO_DIRECTION_RTL;
+        dir = CLUTTER_TEXT_DIRECTION_RTL;
       else if (strcmp (direction, "ltr") == 0)
-        dir = PANGO_DIRECTION_LTR;
+        dir = CLUTTER_TEXT_DIRECTION_LTR;
     }
   else
     {
@@ -432,9 +565,9 @@ clutter_get_text_direction (void)
       char *e = _("default:LTR");
 
       if (strcmp (e, "default:RTL") == 0)
-        dir = PANGO_DIRECTION_RTL;
+        dir = CLUTTER_TEXT_DIRECTION_RTL;
       else if (strcmp (e, "default:LTR") == 0)
-        dir = PANGO_DIRECTION_LTR;
+        dir = CLUTTER_TEXT_DIRECTION_LTR;
       else
         g_warning ("Whoever translated default:LTR did so wrongly.");
     }
@@ -449,10 +582,17 @@ update_pango_context (ClutterBackend *backend,
   PangoFontDescription *font_desc;
   const cairo_font_options_t *font_options;
   const gchar *font_name;
+  PangoDirection pango_dir;
+  PangoLanguage *lang;
   gdouble resolution;
 
   /* update the text direction */
-  pango_context_set_base_dir (context, clutter_text_direction);
+  if (clutter_text_direction == CLUTTER_TEXT_DIRECTION_RTL)
+    pango_dir = PANGO_DIRECTION_RTL;
+  else
+    pango_dir = PANGO_DIRECTION_LTR;
+
+  pango_context_set_base_dir (context, pango_dir);
 
   /* get the configuration for the PangoContext from the backend */
   font_name = clutter_backend_get_font_name (backend);
@@ -501,6 +641,7 @@ _clutter_context_create_pango_context (ClutterMainContext *self)
 
   context = cogl_pango_font_map_create_context (self->font_map);
   update_pango_context (self->backend, context);
+  pango_context_set_language (context, pango_language_get_default ());
 
   return context;
 }
@@ -919,7 +1060,7 @@ clutter_threads_add_timeout (guint       interval,
  * clutter_threads_add_frame_source_full:
  * @priority: the priority of the frame source. Typically this will be in the
  *            range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
- * @interval: the time between calls to the function, in milliseconds
+ * @fps: the number of times per second to call the function
  * @func: function to call
  * @data: data to pass to the function
  * @notify: function to call when the timeout source is removed
@@ -948,7 +1089,7 @@ clutter_threads_add_timeout (guint       interval,
  */
 guint
 clutter_threads_add_frame_source_full (gint           priority,
-                                      guint          interval,
+                                      guint          fps,
                                       GSourceFunc    func,
                                       gpointer       data,
                                       GDestroyNotify notify)
@@ -963,14 +1104,14 @@ clutter_threads_add_frame_source_full (gint           priority,
   dispatch->notify = notify;
 
   return clutter_frame_source_add_full (priority,
-                                       interval,
+                                       fps,
                                        clutter_threads_dispatch, dispatch,
                                        clutter_threads_dispatch_free);
 }
 
 /**
  * clutter_threads_add_frame_source:
- * @interval: the time between calls to the function, in milliseconds
+ * @fps: the number of times per second to call the function
  * @func: function to call
  * @data: data to pass to the function
  *
@@ -981,14 +1122,14 @@ clutter_threads_add_frame_source_full (gint           priority,
  * Since: 0.8
  */
 guint
-clutter_threads_add_frame_source (guint       interval,
+clutter_threads_add_frame_source (guint       fps,
                                  GSourceFunc func,
                                  gpointer    data)
 {
   g_return_val_if_fail (func != NULL, 0);
 
   return clutter_threads_add_frame_source_full (G_PRIORITY_DEFAULT,
-                                               interval,
+                                               fps,
                                                func, data,
                                                NULL);
 }
@@ -1039,21 +1180,31 @@ clutter_get_debug_enabled (void)
 #endif
 }
 
+gboolean
+_clutter_context_is_initialized (void)
+{
+  if (ClutterCntx == NULL)
+    return FALSE;
+
+  return ClutterCntx->is_initialized;
+}
+
 ClutterMainContext *
-clutter_context_get_default (void)
+_clutter_context_get_default (void)
 {
-  if (G_UNLIKELY(!ClutterCntx))
+  if (G_UNLIKELY (ClutterCntx == NULL))
     {
       ClutterMainContext *ctx;
 
       ClutterCntx = ctx = g_new0 (ClutterMainContext, 1);
+
       ctx->backend = g_object_new (_clutter_backend_impl_get_type (), NULL);
 
       ctx->is_initialized = FALSE;
       ctx->motion_events_per_actor = TRUE;
 
 #ifdef CLUTTER_ENABLE_DEBUG
-      ctx->timer          =  g_timer_new ();
+      ctx->timer = g_timer_new ();
       g_timer_start (ctx->timer);
 #endif
     }
@@ -1076,7 +1227,7 @@ clutter_get_timestamp (void)
   ClutterMainContext *ctx;
   gdouble seconds;
 
-  ctx = clutter_context_get_default ();
+  ctx = _clutter_context_get_default ();
 
   /* FIXME: may need a custom timer for embedded setups */
   seconds = g_timer_elapsed (ctx->timer, NULL);
@@ -1093,8 +1244,8 @@ clutter_arg_direction_cb (const char *key,
                           gpointer    user_data)
 {
   clutter_text_direction =
-    (strcmp (value, "rtl") == 0) ? PANGO_DIRECTION_RTL
-                                 : PANGO_DIRECTION_LTR;
+    (strcmp (value, "rtl") == 0) ? CLUTTER_TEXT_DIRECTION_RTL
+                                 : CLUTTER_TEXT_DIRECTION_LTR;
 
   return TRUE;
 }
@@ -1142,7 +1293,7 @@ clutter_init_real (GError **error)
   /* Note, creates backend if not already existing, though parse args will
    * have likely created it
    */
-  ctx = clutter_context_get_default ();
+  ctx = _clutter_context_get_default ();
   backend = ctx->backend;
 
   if (!ctx->options_parsed)
@@ -1194,6 +1345,8 @@ clutter_init_real (GError **error)
   /* Now we can safely assume we have a valid GL context and can
    * start issueing cogl commands
   */
+  /* - will call to backend and cogl */
+  _clutter_feature_init ();
 
   /*
    * Resolution requires display to be open, so can only be queried after
@@ -1211,7 +1364,6 @@ clutter_init_real (GError **error)
 
   clutter_text_direction = clutter_get_text_direction ();
 
-
   /* Figure out framebuffer masks used for pick */
   cogl_get_bitmasks (&ctx->fb_r_mask, &ctx->fb_g_mask, &ctx->fb_b_mask, NULL);
 
@@ -1230,9 +1382,6 @@ clutter_init_real (GError **error)
   /* Initiate event collection */
   _clutter_backend_init_events (ctx->backend);
 
-  /* finally features - will call to backend and cogl */
-  _clutter_feature_init ();
-
   clutter_is_initialized = TRUE;
   ctx->is_initialized = TRUE;
 
@@ -1285,7 +1434,7 @@ pre_parse_hook (GOptionContext  *context,
     g_warning ("Locale not supported by C library.\n"
                "Using the fallback 'C' locale.");
 
-  clutter_context = clutter_context_get_default ();
+  clutter_context = _clutter_context_get_default ();
 
   clutter_context->id_pool = clutter_id_pool_new (256);
 
@@ -1347,7 +1496,7 @@ post_parse_hook (GOptionContext  *context,
   if (clutter_is_initialized)
     return TRUE;
 
-  clutter_context = clutter_context_get_default ();
+  clutter_context = _clutter_context_get_default ();
   backend = clutter_context->backend;
   g_assert (CLUTTER_IS_BACKEND (backend));
 
@@ -1412,7 +1561,7 @@ clutter_get_option_group (void)
 
   clutter_base_init ();
 
-  context = clutter_context_get_default ();
+  context = _clutter_context_get_default ();
 
   group = g_option_group_new ("clutter",
                               _("Clutter Options"),
@@ -1455,7 +1604,7 @@ clutter_get_option_group_without_init (void)
 
   clutter_base_init ();
 
-  context = clutter_context_get_default ();
+  context = _clutter_context_get_default ();
   context->defer_display_setup = TRUE;
 
   group = clutter_get_option_group ();
@@ -1471,16 +1620,17 @@ clutter_get_option_group_without_init (void)
 /**
  * clutter_init_with_args:
  * @argc: (inout): a pointer to the number of command line arguments
- * @argv: (array length=argc) (inout): a pointer to the array of command line arguments
- * @parameter_string: a string which is displayed in the
+ * @argv: (array length=argc) (inout) (allow-none): a pointer to the array
+ *   of command line arguments
+ * @parameter_string: (allow-none): a string which is displayed in the
  *   first line of <option>--help</option> output, after
  *   <literal><replaceable>programname</replaceable> [OPTION...]</literal>
- * @entries: a %NULL terminated array of #GOptionEntry<!-- -->s
+ * @entries: (allow-none): a %NULL terminated array of #GOptionEntry<!-- -->s
  *   describing the options of your program
- * @translation_domain: a translation domain to use for translating
- *   the <option>--help</option> output for the options in @entries
- *   with gettext(), or %NULL
- * @error: a return location for a #GError
+ * @translation_domain: (allow-none): a translation domain to use for
+ *   translating the <option>--help</option> output for the options in
+ *   @entries with gettext(), or %NULL
+ * @error: (allow-none): a return location for a #GError
  *
  * This function does the same work as clutter_init(). Additionally,
  * it allows you to add your own command line options, and it
@@ -1514,7 +1664,7 @@ clutter_init_with_args (int            *argc,
 
   clutter_base_init ();
 
-  ctx = clutter_context_get_default ();
+  ctx = _clutter_context_get_default ();
 
   if (!ctx->defer_display_setup)
     {
@@ -1598,7 +1748,8 @@ clutter_parse_args (int    *argc,
 /**
  * clutter_init:
  * @argc: (inout): The number of arguments in @argv
- * @argv: (array length=argc) (inout): A pointer to an array of arguments.
+ * @argv: (array length=argc) (inout) (allow-none): A pointer to an array
+ *   of arguments.
  *
  * It will initialise everything needed to operate with Clutter and
  * parses some standard command line options. @argc and @argv are
@@ -1619,7 +1770,7 @@ clutter_init (int    *argc,
 
   clutter_base_init ();
 
-  ctx = clutter_context_get_default ();
+  ctx = _clutter_context_get_default ();
 
   if (!ctx->defer_display_setup)
     {
@@ -1671,7 +1822,7 @@ event_click_count_generate (ClutterEvent *event)
   guint           double_click_time;
   guint           double_click_distance;
 
-  backend = clutter_context_get_default ()->backend;
+  backend = _clutter_context_get_default ()->backend;
   double_click_distance = clutter_backend_get_double_click_distance (backend);
   double_click_time = clutter_backend_get_double_click_time (backend);
 
@@ -1850,41 +2001,6 @@ unset_motion_last_actor (ClutterActor *actor, ClutterInputDevice *dev)
     dev->motion_last_actor = NULL;
 }
 
-static ClutterInputDevice * clutter_event_get_device (ClutterEvent *event);
-
-/* This function should perhaps be public and in clutter-event.c ?
- */
-static ClutterInputDevice *
-clutter_event_get_device (ClutterEvent *event)
-{
-  g_return_val_if_fail (event != NULL, NULL);
-
-  switch (event->type)
-    {
-    case CLUTTER_NOTHING:
-    case CLUTTER_STAGE_STATE:
-    case CLUTTER_DESTROY_NOTIFY:
-    case CLUTTER_CLIENT_MESSAGE:
-    case CLUTTER_DELETE:
-    case CLUTTER_ENTER:
-    case CLUTTER_LEAVE:
-      return NULL;
-      break;
-    case CLUTTER_BUTTON_PRESS:
-    case CLUTTER_BUTTON_RELEASE:
-      return event->button.device;
-    case CLUTTER_MOTION:
-      return event->motion.device;
-    case CLUTTER_SCROLL:
-      return event->scroll.device;
-      break;
-    case CLUTTER_KEY_PRESS:
-    case CLUTTER_KEY_RELEASE:
-      break;
-    }
-  return NULL;
-}
-
 static void
 set_motion_last_actor (ClutterActor       *motion_current_actor,
                        ClutterInputDevice *device)
@@ -1931,8 +2047,8 @@ generate_enter_leave_events (ClutterEvent *event)
     {
       if (motion_current_actor)
         {
-          gint         x, y;
           ClutterEvent cev;
+          gfloat x, y;
 
           cev.crossing.device  = device;
           clutter_event_get_coords (event, &x, &y);
@@ -1982,6 +2098,29 @@ generate_enter_leave_events (ClutterEvent *event)
 void
 clutter_do_event (ClutterEvent *event)
 {
+  if (!event->any.stage)
+    return;
+
+  /* Instead of processing events when received, we queue them up to
+   * handle per-frame before animations, layout, and drawing.
+   *
+   * This gives us the chance to reliably compress motion events
+   * because we've "looked ahead" and know all motion events that
+   * will occur before drawing the frame.
+   */
+  _clutter_stage_queue_event (event->any.stage, event);
+}
+
+/**
+ * _clutter_process_event
+ * @event: a #ClutterEvent.
+ *
+ * Does the actual work of processing an event that was queued earlier
+ * out of clutter_do_event().
+ */
+void
+_clutter_process_event (ClutterEvent *event)
+{
   /* FIXME: This should probably be clutter_cook_event() - it would
    * take a raw event from the backend and 'cook' it so its more tasty.
    *
@@ -1990,10 +2129,8 @@ clutter_do_event (ClutterEvent *event)
   ClutterBackend      *backend;
   ClutterActor        *stage;
   ClutterInputDevice  *device = NULL;
-  static gint32        motion_last_time = 0L;
-  gint32               local_motion_time;
 
-  context = clutter_context_get_default ();
+  context = _clutter_context_get_default ();
   backend = context->backend;
   stage   = CLUTTER_ACTOR(event->any.stage);
 
@@ -2068,52 +2205,6 @@ clutter_do_event (ClutterEvent *event)
       case CLUTTER_MOTION:
         device = event->motion.device;
 
-        if (device)
-          local_motion_time = device->motion_last_time;
-        else
-          local_motion_time = motion_last_time;
-
-        /* avoid rate throttling for synthetic motion events or if
-         * the per-actor events are disabled
-         */
-        if (!(event->any.flags & CLUTTER_EVENT_FLAG_SYNTHETIC) ||
-            !context->motion_events_per_actor)
-          {
-            gint32 frame_rate, delta;
-
-            /* avoid issuing too many motion events, which leads to many
-             * redraws in pick mode (performance penalty)
-             */
-            frame_rate = clutter_get_motion_events_frequency ();
-            delta = 1000 / frame_rate;
-
-            CLUTTER_NOTE (EVENT,
-                  "skip motion event: %s (last:%d, delta:%d, time:%d)",
-                  (event->any.time < (local_motion_time + delta) ? "yes" : "no"),
-                  local_motion_time,
-                  delta,
-                  event->any.time);
-
-            /* we need to guard against roll-overs and the
-             * case where the time is rolled backwards and
-             * the backend is not ensuring a monotonic clock
-             * for the events.
-             *
-             * see:
-             *   http://bugzilla.openedhand.com/show_bug.cgi?id=1130
-             */
-            if (event->any.time >= local_motion_time &&
-                event->any.time < (local_motion_time + delta))
-              break;
-            else
-              local_motion_time = event->any.time;
-          }
-
-        if (device)
-          device->motion_last_time = local_motion_time;
-        else
-          motion_last_time = local_motion_time;
-
         /* Only stage gets motion events if clutter_set_motion_events is TRUE,
          * and the event is not a synthetic event with source set.
          */
@@ -2153,7 +2244,7 @@ clutter_do_event (ClutterEvent *event)
       case CLUTTER_SCROLL:
         {
           ClutterActor *actor;
-          gint          x,y;
+          gfloat x, y;
 
           clutter_event_get_coords (event, &x, &y);
 
@@ -2170,7 +2261,7 @@ clutter_do_event (ClutterEvent *event)
                   if (event->type == CLUTTER_BUTTON_RELEASE)
                     {
                       CLUTTER_NOTE (EVENT,
-                                    "Release off stage received at %i, %i",
+                                    "Release off stage received at %.2f, %.2f",
                                     x, y);
 
                       event->button.source = stage;
@@ -2196,12 +2287,14 @@ clutter_do_event (ClutterEvent *event)
 
 
           /* FIXME: for an optimisation should check if there are
-           * actually any reactive actors and avoid the pick all togeather
+           * actually any reactive actors and avoid the pick all together
            * (signalling just the stage). Should be big help for gles.
            */
 
-          CLUTTER_NOTE (EVENT, "Reactive event received at %i, %i - actor: %p",
-                        x, y, actor);
+          CLUTTER_NOTE (EVENT,
+                        "Reactive event received at %.2f, %.2f - actor: %p",
+                        x, y,
+                        actor);
 
           /* Create, enter/leave events if needed */
           generate_enter_leave_events (event);
@@ -2261,7 +2354,7 @@ clutter_get_actor_by_gid (guint32 id)
 {
   ClutterMainContext *context;
 
-  context = clutter_context_get_default ();
+  context = _clutter_context_get_default ();
 
   g_return_val_if_fail (context != NULL, NULL);
 
@@ -2293,11 +2386,7 @@ clutter_base_init (void)
 /**
  * clutter_get_default_frame_rate:
  *
- * Retrieves the default frame rate used when creating #ClutterTimeline<!--
- * -->s.
- *
- * This value is also used to compute the default frequency of motion
- * events.
+ * Retrieves the default frame rate. See clutter_set_default_frame_rate().
  *
  * Return value: the default frame rate
  *
@@ -2308,7 +2397,7 @@ clutter_get_default_frame_rate (void)
 {
   ClutterMainContext *context;
 
-  context = clutter_context_get_default ();
+  context = _clutter_context_get_default ();
 
   return context->frame_rate;
 }
@@ -2317,8 +2406,10 @@ clutter_get_default_frame_rate (void)
  * clutter_set_default_frame_rate:
  * @frames_per_sec: the new default frame rate
  *
- * Sets the default frame rate to be used when creating #ClutterTimeline<!--
- * -->s
+ * Sets the default frame rate. This frame rate will be used to limit
+ * the number of frames drawn if Clutter is not able to synchronize
+ * with the vertical refresh rate of the display. When synchronization
+ * is possible, this value is ignored.
  *
  * Since: 0.6
  */
@@ -2327,7 +2418,7 @@ clutter_set_default_frame_rate (guint frames_per_sec)
 {
   ClutterMainContext *context;
 
-  context = clutter_context_get_default ();
+  context = _clutter_context_get_default ();
 
   if (context->frame_rate != frames_per_sec)
     context->frame_rate = frames_per_sec;
@@ -2341,7 +2432,7 @@ on_pointer_grab_weak_notify (gpointer data,
   ClutterInputDevice *dev = (ClutterInputDevice *)data;
   ClutterMainContext *context;
 
-  context = clutter_context_get_default ();
+  context = _clutter_context_get_default ();
 
   if (dev)
     {
@@ -2361,8 +2452,14 @@ on_pointer_grab_weak_notify (gpointer data,
  *
  * Grabs pointer events, after the grab is done all pointer related events
  * (press, motion, release, enter, leave and scroll) are delivered to this
- * actor directly. The source set in the event will be the actor that would
- * have received the event if the pointer grab was not in effect.
+ * actor directly without passing through both capture and bubble phases of
+ * the event delivery chain. The source set in the event will be the actor
+ * that would have received the event if the pointer grab was not in effect.
+ *
+ * <note><para>Grabs completely override the entire event delivery chain
+ * done by Clutter. Pointer grabs should only be used as a last resource;
+ * using the #ClutterActor::captured-event signal should always be the
+ * preferred way to intercept event delivery to reactive actors.</para></note>
  *
  * If you wish to grab all the pointer events for a specific input device,
  * you should use clutter_grab_pointer_for_device().
@@ -2376,7 +2473,7 @@ clutter_grab_pointer (ClutterActor *actor)
 
   g_return_if_fail (actor == NULL || CLUTTER_IS_ACTOR (actor));
 
-  context = clutter_context_get_default ();
+  context = _clutter_context_get_default ();
 
   if (context->pointer_grab_actor == actor)
     return;
@@ -2493,7 +2590,7 @@ ClutterActor *
 clutter_get_pointer_grab (void)
 {
   ClutterMainContext *context;
-  context = clutter_context_get_default ();
+  context = _clutter_context_get_default ();
 
   return context->pointer_grab_actor;
 }
@@ -2505,7 +2602,7 @@ on_keyboard_grab_weak_notify (gpointer data,
 {
   ClutterMainContext *context;
 
-  context = clutter_context_get_default ();
+  context = _clutter_context_get_default ();
   context->keyboard_grab_actor = NULL;
 
   clutter_ungrab_keyboard ();
@@ -2515,10 +2612,17 @@ on_keyboard_grab_weak_notify (gpointer data,
  * clutter_grab_keyboard:
  * @actor: a #ClutterActor
  *
- * Grabs keyboard events, after the grab is done keyboard events ("key-press-event"
- * and "key-release-event") are delivered to this actor directly. The source
- * set in the event will be the actor that would have received the event if the
- * keyboard grab was not in effect.
+ * Grabs keyboard events, after the grab is done keyboard
+ * events (#ClutterActor::key-press-event and #ClutterActor::key-release-event)
+ * are delivered to this actor directly. The source set in the event will be
+ * the actor that would have received the event if the keyboard grab was not
+ * in effect.
+ *
+ * Like pointer grabs, keyboard grabs should only be used as a last
+ * resource.
+ *
+ * See also clutter_stage_set_key_focus() and clutter_actor_grab_key_focus()
+ * to perform a "soft" key grab and assign key focus to a specific actor.
  *
  * Since: 0.6
  */
@@ -2529,7 +2633,7 @@ clutter_grab_keyboard (ClutterActor *actor)
 
   g_return_if_fail (actor == NULL || CLUTTER_IS_ACTOR (actor));
 
-  context = clutter_context_get_default ();
+  context = _clutter_context_get_default ();
 
   if (context->keyboard_grab_actor == actor)
     return;
@@ -2578,64 +2682,10 @@ ClutterActor *
 clutter_get_keyboard_grab (void)
 {
   ClutterMainContext *context;
-  context = clutter_context_get_default ();
 
-  return context->keyboard_grab_actor;
-}
+  context = _clutter_context_get_default ();
 
-/**
- * clutter_get_motion_events_frequency:
- *
- * Retrieves the number of motion events per second that are delivered
- * to the stage.
- *
- * See clutter_set_motion_events_frequency().
- *
- * Return value: the number of motion events per second
- *
- * Since: 0.6
- */
-guint
-clutter_get_motion_events_frequency (void)
-{
-  ClutterMainContext *context = clutter_context_get_default ();
-
-  if (G_LIKELY (context->motion_frequency == 0))
-    {
-      guint frequency;
-
-      frequency = clutter_default_fps / 4;
-      frequency = CLAMP (frequency, 20, 45);
-
-      return frequency;
-    }
-  else
-    return context->motion_frequency;
-}
-
-/**
- * clutter_set_motion_events_frequency:
- * @frequency: the number of motion events per second, or 0 for the
- *   default value
- *
- * Sets the motion events frequency. Setting this to a non-zero value
- * will override the default setting, so it should be rarely used.
- *
- * Motion events are delivered from the default backend to the stage
- * and are used to generate the enter/leave events pair. This might lead
- * to a performance penalty due to the way the actors are identified.
- * Using this function is possible to reduce the frequency of the motion
- * events delivery to the stage.
- *
- * Since: 0.6
- */
-void
-clutter_set_motion_events_frequency (guint frequency)
-{
-  ClutterMainContext *context = clutter_context_get_default ();
-
-  /* never allow the motion events to exceed the default frame rate */
-  context->motion_frequency = CLAMP (frequency, 1, clutter_default_fps);
+  return context->keyboard_grab_actor;
 }
 
 /**
@@ -2760,7 +2810,7 @@ clutter_get_input_device_for_id (gint id)
   ClutterInputDevice *device = NULL;
   ClutterMainContext  *context;
 
-  context = clutter_context_get_default ();
+  context = _clutter_context_get_default ();
 
   for (item = context->input_devices;
        item != NULL;
@@ -2795,3 +2845,210 @@ clutter_get_font_map (void)
 
   return NULL;
 }
+
+typedef struct _ClutterRepaintFunction
+{
+  guint id;
+  GSourceFunc func;
+  gpointer data;
+  GDestroyNotify notify;
+} ClutterRepaintFunction;
+
+/**
+ * clutter_threads_remove_repaint_func:
+ * @handle_id: an unsigned integer greater than zero
+ *
+ * Removes the repaint function with @handle_id as its id
+ *
+ * Since: 1.0
+ */
+void
+clutter_threads_remove_repaint_func (guint handle_id)
+{
+  ClutterRepaintFunction *repaint_func;
+  ClutterMainContext *context;
+  GList *l;
+
+  g_return_if_fail (handle_id > 0);
+
+  context = CLUTTER_CONTEXT ();
+  l = context->repaint_funcs;
+  while (l != NULL)
+    {
+      repaint_func = l->data;
+
+      if (repaint_func->id == handle_id)
+        {
+          context->repaint_funcs =
+            g_list_remove_link (context->repaint_funcs, l);
+
+          g_list_free (l);
+
+          if (repaint_func->notify)
+            repaint_func->notify (repaint_func->data);
+
+          g_slice_free (ClutterRepaintFunction, repaint_func);
+
+          return;
+        }
+
+      l = l->next;
+    }
+}
+
+/**
+ * clutter_threads_add_repaint_func:
+ * @func: the function to be called within the paint cycle
+ * @data: data to be passed to the function, or %NULL
+ * @notify: function to be called when removing the repaint
+ *    function, or %NULL
+ *
+ * Adds a function to be called whenever Clutter is repainting a Stage.
+ * If the function returns %FALSE it is automatically removed from the
+ * list of repaint functions and will not be called again.
+ *
+ * This function is guaranteed to be called from within the same thread
+ * that called clutter_main(), and while the Clutter lock is being held.
+ *
+ * A repaint function is useful to ensure that an update of the scenegraph
+ * is performed before the scenegraph is repainted; for instance, uploading
+ * a frame from a video into a #ClutterTexture.
+ *
+ * When the repaint function is removed (either because it returned %FALSE
+ * or because clutter_threads_remove_repaint_func() has been called) the
+ * @notify function will be called, if any is set.
+ *
+ * Return value: the ID (greater than 0) of the repaint function. You
+ *   can use the returned integer to remove the repaint function by
+ *   calling clutter_threads_remove_repaint_func().
+ *
+ * Since: 1.0
+ */
+guint
+clutter_threads_add_repaint_func (GSourceFunc    func,
+                                  gpointer       data,
+                                  GDestroyNotify notify)
+{
+  static guint repaint_id = 1;
+  ClutterMainContext *context;
+  ClutterRepaintFunction *repaint_func;
+
+  g_return_val_if_fail (func != NULL, 0);
+
+  context = CLUTTER_CONTEXT ();
+
+  /* XXX lock the context */
+
+  repaint_func = g_slice_new (ClutterRepaintFunction);
+
+  repaint_func->id = repaint_id++;
+  repaint_func->func = func;
+  repaint_func->data = data;
+  repaint_func->notify = notify;
+
+  context->repaint_funcs = g_list_prepend (context->repaint_funcs,
+                                           repaint_func);
+
+  /* XXX unlock the context */
+
+  return repaint_func->id;
+}
+
+/*
+ * _clutter_run_repaint_functions:
+ *
+ * Executes the repaint functions added using the
+ * clutter_threads_add_repaint_func() function.
+ *
+ * Must be called before calling clutter_redraw() and
+ * with the Clutter thread lock held.
+ */
+void
+_clutter_run_repaint_functions (void)
+{
+  ClutterMainContext *context = CLUTTER_CONTEXT ();
+  ClutterRepaintFunction *repaint_func;
+  GList *reinvoke_list, *l;
+
+  if (context->repaint_funcs == NULL)
+    return;
+
+  reinvoke_list = NULL;
+
+  /* consume the whole list while we execute the functions */
+  while (context->repaint_funcs)
+    {
+      gboolean res = FALSE;
+
+      repaint_func = context->repaint_funcs->data;
+
+      l = context->repaint_funcs;
+      context->repaint_funcs =
+        g_list_remove_link (context->repaint_funcs, context->repaint_funcs);
+
+      g_list_free (l);
+
+      res = repaint_func->func (repaint_func->data);
+
+      if (res)
+        reinvoke_list = g_list_prepend (reinvoke_list, repaint_func);
+      else
+        {
+          if (repaint_func->notify)
+            repaint_func->notify (repaint_func->data);
+
+          g_slice_free (ClutterRepaintFunction, repaint_func);
+        }
+    }
+
+  if (reinvoke_list)
+    context->repaint_funcs = reinvoke_list;
+}
+
+/**
+ * clutter_check_version:
+ * @major: major version, like 1 in 1.2.3
+ * @minor: minor version, like 2 in 1.2.3
+ * @micro: micro version, like 3 in 1.2.3
+ *
+ * Run-time version check, to check the version the Clutter library
+ * that an application is currently linked against
+ *
+ * This is the run-time equivalent of the compile-time %CLUTTER_CHECK_VERSION
+ * pre-processor macro
+ *
+ * Return value: %TRUE if the version of the Clutter library is
+ *   greater than (@major, @minor, @micro), and %FALSE otherwise
+ *
+ * Since: 1.2
+ */
+gboolean
+clutter_check_version (guint major,
+                       guint minor,
+                       guint micro)
+{
+  return (clutter_major_version > major ||
+          (clutter_major_version == major &&
+           clutter_minor_version > minor) ||
+          (clutter_major_version == major &&
+           clutter_minor_version == minor &&
+           clutter_micro_version >= micro));
+}
+
+void
+clutter_set_default_text_direction (ClutterTextDirection text_dir)
+{
+  if (text_dir == CLUTTER_TEXT_DIRECTION_DEFAULT)
+    text_dir = clutter_get_text_direction ();
+
+  if (text_dir != clutter_text_direction)
+    clutter_text_direction = text_dir;
+
+  /* FIXME - queue a relayout on all stages */
+}
+
+ClutterTextDirection
+clutter_get_default_text_direction (void)
+{
+  return clutter_text_direction;
+}