Merge branch 'text-direction'
[profile/ivi/clutter.git] / clutter / clutter-main.c
index a1eaceb..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"
@@ -60,19 +65,26 @@ static GMutex *clutter_threads_mutex         = NULL;
 static GCallback clutter_threads_lock        = NULL;
 static GCallback clutter_threads_unlock      = NULL;
 
+/* command line options */
 static gboolean clutter_is_initialized       = FALSE;
 static gboolean clutter_show_fps             = FALSE;
 static gboolean clutter_fatal_warnings       = FALSE;
+static gboolean clutter_disable_mipmap_text  = FALSE;
+static gboolean clutter_use_fuzzy_picking    = FALSE;
 
 static guint clutter_default_fps             = 60;
 
+static PangoDirection clutter_text_direction = CLUTTER_TEXT_DIRECTION_LTR;
+
 static guint clutter_main_loop_level         = 0;
 static GSList *main_loops                    = NULL;
 
-static PangoDirection clutter_text_direction = PANGO_DIRECTION_LTR;
-
 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 },
@@ -89,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 */
 
@@ -114,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 */
@@ -135,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);
     }
@@ -147,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;
+  ClutterMasterClock *master_clock;
+  static GTimer *timer = NULL;
+  static guint timer_n_frames = 0;
 
-  ctx  = clutter_context_get_default ();
-
-  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));
@@ -205,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);
 
@@ -222,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
  *
@@ -246,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;
 }
@@ -263,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;
 }
@@ -278,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;
 }
@@ -287,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))
@@ -316,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
@@ -325,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);
@@ -346,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,
@@ -353,25 +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);
-  cogl_paint_init (&white);
+  if (G_LIKELY (!(clutter_debug_flags & CLUTTER_DEBUG_DUMP_PICK_BUFFERS)))
+    cogl_clip_push_window_rectangle (x, y, 1, 1);
+
+  cogl_disable_fog ();
+  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)
@@ -380,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)
@@ -403,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;
@@ -413,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
     {
@@ -428,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.");
     }
@@ -443,12 +580,19 @@ update_pango_context (ClutterBackend *backend,
                       PangoContext   *context)
 {
   PangoFontDescription *font_desc;
-  cairo_font_options_t *font_options;
+  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);
@@ -497,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;
 }
@@ -915,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
@@ -944,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)
@@ -959,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
  *
@@ -977,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);
 }
@@ -1035,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
     }
@@ -1072,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);
@@ -1089,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;
 }
@@ -1138,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)
@@ -1171,6 +1326,8 @@ clutter_init_real (GError **error)
       return CLUTTER_INIT_ERROR_INTERNAL;
     }
 
+  clutter_stage_set_title (CLUTTER_STAGE (stage), g_get_prgname ());
+
   clutter_actor_realize (stage);
 
   if (!CLUTTER_ACTOR_IS_REALIZED (stage))
@@ -1188,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
@@ -1199,10 +1358,11 @@ clutter_init_real (GError **error)
 
   resolution = clutter_backend_get_resolution (ctx->backend);
   cogl_pango_font_map_set_resolution (ctx->font_map, resolution);
-  cogl_pango_font_map_set_use_mipmapping (ctx->font_map, TRUE);
 
-  clutter_text_direction = clutter_get_text_direction ();
+  if (G_LIKELY (!clutter_disable_mipmap_text))
+    cogl_pango_font_map_set_use_mipmapping (ctx->font_map, TRUE);
 
+  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);
@@ -1211,10 +1371,8 @@ clutter_init_real (GError **error)
   ctx->fb_g_mask_used = ctx->fb_g_mask;
   ctx->fb_b_mask_used = ctx->fb_b_mask;
 
-#ifndef HAVE_CLUTTER_FRUITY
-  /* We always do fuzzy picking for the fruity backend */
-  if (g_getenv ("CLUTTER_FUZZY_PICK") != NULL)
-#endif
+  /* XXX - describe what "fuzzy picking" is */
+  if (clutter_use_fuzzy_picking)
     {
       ctx->fb_r_mask_used--;
       ctx->fb_g_mask_used--;
@@ -1224,11 +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_stage_set_title (CLUTTER_STAGE (stage), g_get_prgname ());
-
   clutter_is_initialized = TRUE;
   ctx->is_initialized = TRUE;
 
@@ -1245,6 +1398,12 @@ static GOptionEntry clutter_args[] = {
   { "clutter-text-direction", 0, 0, G_OPTION_ARG_CALLBACK,
     clutter_arg_direction_cb,
     N_("Direction for the text"), "DIRECTION" },
+  { "clutter-disable-mipmapped-text", 0, 0, G_OPTION_ARG_NONE,
+    &clutter_disable_mipmap_text,
+    N_("Disable mipmapping on text"), NULL },
+  { "clutter-use-fuzzy-picking", 0, 0, G_OPTION_ARG_NONE,
+    &clutter_use_fuzzy_picking,
+    N_("Use 'fuzzy' picking"), NULL },
 #ifdef CLUTTER_ENABLE_DEBUG
   { "clutter-debug", 0, 0, G_OPTION_ARG_CALLBACK, clutter_arg_debug_cb,
     N_("Clutter debugging flags to set"), "FLAGS" },
@@ -1275,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);
 
@@ -1306,6 +1465,19 @@ pre_parse_hook (GOptionContext  *context,
       clutter_default_fps = CLAMP (default_fps, 1, 1000);
     }
 
+  env_string = g_getenv ("CLUTTER_DISABLE_MIPMAPPED_TEXT");
+  if (env_string)
+    clutter_disable_mipmap_text = TRUE;
+
+#ifdef HAVE_CLUTTER_FRUITY
+  /* we always enable fuzzy picking in the "fruity" backend */
+  clutter_use_fuzzy_picking = TRUE;
+#else
+  env_string = g_getenv ("CLUTTER_FUZZY_PICK");
+  if (env_string)
+    clutter_use_fuzzy_picking = TRUE;
+#endif /* HAVE_CLUTTER_FRUITY */
+
   return _clutter_backend_pre_parse (backend, error);
 }
 
@@ -1324,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));
 
@@ -1376,7 +1548,7 @@ post_parse_hook (GOptionContext  *context,
  * Clutter #GOptionGroup has returned %TRUE, Clutter is guaranteed to be
  * initialized.
  *
- * Return value: a #GOptionGroup for the commandline arguments
+ * Return value: (transfer full): a #GOptionGroup for the commandline arguments
  *   recognized by Clutter
  *
  * Since: 0.2
@@ -1389,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"),
@@ -1419,7 +1591,7 @@ clutter_get_option_group (void)
  * function when needing to set foreign display connection with
  * clutter_x11_set_display(), or with gtk_clutter_init().
  *
- * Return value: a #GOptionGroup for the commandline arguments
+ * Return value: (transfer full): a #GOptionGroup for the commandline arguments
  *   recognized by Clutter
  *
  * Since: 0.8.2
@@ -1432,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 ();
@@ -1440,19 +1612,25 @@ clutter_get_option_group_without_init (void)
   return group;
 }
 
+/* Note that the gobject-introspection annotations for the argc/argv
+ * parameters do not produce the right result; however, they do
+ * allow the common case of argc=NULL, argv=NULL to work.
+ */
+
 /**
  * clutter_init_with_args:
- * @argc: a pointer to the number of command line arguments
- * @argv: a pointer to the array of command line arguments
- * @parameter_string: a string which is displayed in the
+ * @argc: (inout): a pointer to the number of command line arguments
+ * @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
@@ -1486,16 +1664,19 @@ clutter_init_with_args (int            *argc,
 
   clutter_base_init ();
 
-  ctx = clutter_context_get_default ();
+  ctx = _clutter_context_get_default ();
 
   if (!ctx->defer_display_setup)
     {
       if (argc && *argc > 0 && *argv)
        g_set_prgname ((*argv)[0]);
 
-      group   = clutter_get_option_group ();
       context = g_option_context_new (parameter_string);
 
+      group = clutter_get_option_group ();
+      g_option_context_add_group (context, group);
+
+      group = cogl_get_option_group ();
       g_option_context_add_group (context, group);
 
       if (entries)
@@ -1529,7 +1710,7 @@ clutter_parse_args (int    *argc,
                     char ***argv)
 {
   GOptionContext *option_context;
-  GOptionGroup   *clutter_group;
+  GOptionGroup   *clutter_group, *cogl_group;
   GError         *error = NULL;
   gboolean        ret = TRUE;
 
@@ -1545,6 +1726,9 @@ clutter_parse_args (int    *argc,
   clutter_group = clutter_get_option_group ();
   g_option_context_set_main_group (option_context, clutter_group);
 
+  cogl_group = cogl_get_option_group ();
+  g_option_context_add_group (option_context, cogl_group);
+
   if (!g_option_context_parse (option_context, argc, argv, &error))
     {
       if (error)
@@ -1563,8 +1747,9 @@ clutter_parse_args (int    *argc,
 
 /**
  * clutter_init:
- * @argc: The number of arguments in @argv
- * @argv: A pointer to an array of arguments.
+ * @argc: (inout): The number of arguments in @argv
+ * @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
@@ -1585,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)
     {
@@ -1637,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);
 
@@ -1700,34 +1885,33 @@ static inline void
 emit_event (ClutterEvent *event,
             gboolean      is_key_event)
 {
-#define MAX_EVENT_DEPTH 512
+  static gboolean      lock = FALSE;
 
-  static ClutterActor **event_tree = NULL;
-  static gboolean       lock = FALSE;
-
-  ClutterActor         *actor;
-  gint                  i = 0, n_tree_events = 0;
+  GPtrArray *event_tree = NULL;
+  ClutterActor *actor;
+  gint i = 0;
 
   if (!event->any.source)
     {
-      g_warning ("No event source set, discarding event");
+      CLUTTER_NOTE (EVENT, "No source set, discarding event");
       return;
     }
 
   /* reentrancy check */
   if (lock != FALSE)
-    return;
+    {
+      g_warning ("Tried emitting event during event delivery, bailing out.n");
+      return;
+    }
 
   lock = TRUE;
 
-  /* Sorry Mr Bassi. */
-  if (G_UNLIKELY (event_tree == NULL))
-    event_tree = g_new0 (ClutterActor *, MAX_EVENT_DEPTH);
+  event_tree = g_ptr_array_sized_new (64);
 
   actor = event->any.source;
 
   /* Build 'tree' of emitters for the event */
-  while (actor && n_tree_events < MAX_EVENT_DEPTH)
+  while (actor)
     {
       ClutterActor *parent;
 
@@ -1737,30 +1921,29 @@ emit_event (ClutterEvent *event,
           parent == NULL ||         /* stage gets all events */
           is_key_event)             /* keyboard events are always emitted */
         {
-          event_tree[n_tree_events++] = g_object_ref (actor);
+          g_ptr_array_add (event_tree, g_object_ref (actor));
         }
 
       actor = parent;
     }
 
   /* Capture */
-  for (i = n_tree_events-1; i >= 0; i--)
-    if (clutter_actor_event (event_tree[i], event, TRUE))
+  for (i = event_tree->len - 1; i >= 0; i--)
+    if (clutter_actor_event (g_ptr_array_index (event_tree, i), event, TRUE))
       goto done;
 
   /* Bubble */
-  for (i = 0; i < n_tree_events; i++)
-    if (clutter_actor_event (event_tree[i], event, FALSE))
+  for (i = 0; i < event_tree->len; i++)
+    if (clutter_actor_event (g_ptr_array_index (event_tree, i), event, FALSE))
       goto done;
 
 done:
+  for (i = 0; i < event_tree->len; i++)
+    g_object_unref (g_ptr_array_index (event_tree, i));
 
-  for (i = 0; i < n_tree_events; i++)
-    g_object_unref (event_tree[i]);
+  g_ptr_array_free (event_tree, TRUE);
 
   lock = FALSE;
-
-#undef MAX_EVENT_DEPTH
 }
 
 /*
@@ -1818,39 +2001,35 @@ 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)
+static void
+set_motion_last_actor (ClutterActor       *motion_current_actor,
+                       ClutterInputDevice *device)
 {
-  g_return_val_if_fail (event != NULL, NULL);
+  ClutterMainContext *context              = ClutterCntx;
+  ClutterActor       *last_actor           = context->motion_last_actor;
 
-  switch (event->type)
+  if (device != NULL)
+    last_actor = device->motion_last_actor;
+
+  if (last_actor && last_actor != motion_current_actor)
     {
-    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;
+      g_signal_handlers_disconnect_by_func
+                       (last_actor,
+                        G_CALLBACK (unset_motion_last_actor),
+                        device);
     }
-  return NULL;
+
+  if (motion_current_actor && last_actor != motion_current_actor)
+    {
+      g_signal_connect (motion_current_actor, "destroy",
+                        G_CALLBACK (unset_motion_last_actor),
+                        device);
+    }
+
+  if (device != NULL)
+    device->motion_last_actor = motion_current_actor;
+  else
+    context->motion_last_actor = motion_current_actor;
 }
 
 static inline void
@@ -1868,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);
@@ -1905,25 +2084,7 @@ generate_enter_leave_events (ClutterEvent *event)
         }
     }
 
-  if (last_actor && last_actor != motion_current_actor)
-    {
-      g_signal_handlers_disconnect_by_func
-                       (last_actor,
-                        G_CALLBACK (unset_motion_last_actor),
-                        device);
-    }
-
-  if (motion_current_actor && last_actor != motion_current_actor)
-    {
-      g_signal_connect (motion_current_actor, "destroy",
-                        G_CALLBACK (unset_motion_last_actor),
-                        device);
-    }
-
-  if (device != NULL)
-    device->motion_last_actor = motion_current_actor;
-  else
-    context->motion_last_actor = motion_current_actor;
+  set_motion_last_actor (motion_current_actor, device);
 }
 
 /**
@@ -1937,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.
    *
@@ -1945,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);
 
@@ -1957,14 +2139,31 @@ clutter_do_event (ClutterEvent *event)
 
   CLUTTER_TIMESTAMP (EVENT, "Event received");
 
+  context->last_event_time = clutter_event_get_time (event);
+
   switch (event->type)
     {
       case CLUTTER_NOTHING:
         event->any.source = stage;
         break;
 
-      case CLUTTER_ENTER:
       case CLUTTER_LEAVE:
+        /* The source is set for generated events, not for events
+         * resulting from the cursor leaving the stage
+         */
+        if (event->any.source == NULL)
+          {
+            ClutterActor *last_actor = context->motion_last_actor;
+
+            if (event->crossing.device != NULL)
+              last_actor = event->crossing.device->motion_last_actor;
+
+            event->any.source = last_actor;
+
+            set_motion_last_actor (NULL, event->crossing.device);
+          }
+        /* flow through */
+      case CLUTTER_ENTER:
         emit_pointer_event (event, event->crossing.device);
         break;
 
@@ -2006,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.
          */
@@ -2091,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);
 
@@ -2108,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;
@@ -2134,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);
@@ -2189,8 +2344,8 @@ clutter_do_event (ClutterEvent *event)
  *
  * Retrieves the #ClutterActor with @id.
  *
- * Return value: the actor with the passed id or %NULL. The returned
- *   actor does not have its reference count increased.
+ * Return value: (transfer none): the actor with the passed id or %NULL.
+ *   The returned actor does not have its reference count increased.
  *
  * Since: 0.6
  */
@@ -2199,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);
 
@@ -2231,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
  *
@@ -2246,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;
 }
@@ -2255,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
  */
@@ -2265,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;
@@ -2279,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)
     {
@@ -2299,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().
@@ -2314,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;
@@ -2423,7 +2582,7 @@ clutter_ungrab_pointer_for_device (gint id)
  *
  * Queries the current pointer grab of clutter.
  *
- * Return value: the actor currently holding the pointer grab, or NULL if there is no grab.
+ * Return value: (transfer none): the actor currently holding the pointer grab, or NULL if there is no grab.
  *
  * Since: 0.6
  */
@@ -2431,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;
 }
@@ -2443,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 ();
@@ -2453,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
  */
@@ -2467,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;
@@ -2508,7 +2674,7 @@ clutter_ungrab_keyboard (void)
  *
  * Queries the current keyboard grab of clutter.
  *
- * Return value: the actor currently holding the keyboard grab, or NULL if there is no grab.
+ * Return value: (transfer none): the actor currently holding the keyboard grab, or NULL if there is no grab.
  *
  * Since: 0.6
  */
@@ -2516,64 +2682,10 @@ ClutterActor *
 clutter_get_keyboard_grab (void)
 {
   ClutterMainContext *context;
-  context = clutter_context_get_default ();
 
-  return context->keyboard_grab_actor;
-}
-
-/**
- * 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;
+  context = _clutter_context_get_default ();
 
-      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;
 }
 
 /**
@@ -2612,7 +2724,11 @@ void
 clutter_set_font_flags (ClutterFontFlags flags)
 {
   ClutterFontFlags old_flags, changed_flags;
-  cairo_font_options_t *font_options;
+  const cairo_font_options_t *font_options;
+  cairo_font_options_t *new_font_options;
+  ClutterBackend *backend;
+
+  backend = clutter_get_default_backend ();
 
   if (CLUTTER_CONTEXT ()->font_map)
     cogl_pango_font_map_set_use_mipmapping (CLUTTER_CONTEXT ()->font_map,
@@ -2621,26 +2737,25 @@ clutter_set_font_flags (ClutterFontFlags flags)
 
   old_flags = clutter_get_font_flags ();
 
-  font_options = clutter_backend_get_font_options (CLUTTER_CONTEXT ()->backend);
-  font_options = cairo_font_options_copy (font_options);
+  font_options = clutter_backend_get_font_options (backend);
+  new_font_options = cairo_font_options_copy (font_options);
 
   /* Only set the font options that have actually changed so we don't
      override a detailed setting from the backend */
   changed_flags = old_flags ^ flags;
 
   if ((changed_flags & CLUTTER_FONT_HINTING))
-    cairo_font_options_set_hint_style (font_options,
+    cairo_font_options_set_hint_style (new_font_options,
                                        (flags & CLUTTER_FONT_HINTING)
                                        ? CAIRO_HINT_STYLE_FULL
                                        : CAIRO_HINT_STYLE_NONE);
 
-  clutter_backend_set_font_options (CLUTTER_CONTEXT ()->backend, font_options);
+  clutter_backend_set_font_options (backend, new_font_options);
 
-  cairo_font_options_destroy (font_options);
+  cairo_font_options_destroy (new_font_options);
 
   if (CLUTTER_CONTEXT ()->pango_context)
-    update_pango_context (CLUTTER_CONTEXT ()->backend,
-                          CLUTTER_CONTEXT ()->pango_context);
+    update_pango_context (backend, CLUTTER_CONTEXT ()->pango_context);
 }
 
 /**
@@ -2658,7 +2773,7 @@ clutter_get_font_flags (void)
 {
   ClutterMainContext *ctxt = CLUTTER_CONTEXT ();
   CoglPangoFontMap *font_map = NULL;
-  cairo_font_options_t *font_options;
+  const cairo_font_options_t *font_options;
   ClutterFontFlags flags = 0;
 
   font_map = CLUTTER_CONTEXT ()->font_map;
@@ -2684,7 +2799,7 @@ clutter_get_font_flags (void)
  *
  * Retrieves the #ClutterInputDevice from its id.
  *
- * Return value: a #ClutterInputDevice, or %NULL
+ * Return value: (transfer none): a #ClutterInputDevice, or %NULL
  *
  * Since: 0.8
  */
@@ -2695,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;
@@ -2717,9 +2832,8 @@ clutter_get_input_device_for_id (gint id)
  * You can use the global font map object with the COGL
  * Pango API.
  *
- * Return value: the #PangoFontMap instance. The returned
- *   value is owned by Clutter and it should never be
- *   unreferenced.
+ * Return value: (transfer none): the #PangoFontMap instance. The returned
+ *   value is owned by Clutter and it should never be unreferenced.
  *
  * Since: 1.0
  */
@@ -2731,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;
+}