Merge branch 'text-direction'
[profile/ivi/clutter.git] / clutter / clutter-main.c
index a4a690b..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"
@@ -70,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 },
@@ -94,7 +102,9 @@ static const GDebugKey clutter_debug_keys[] = {
   { "shader", CLUTTER_DEBUG_SHADER },
   { "multistage", CLUTTER_DEBUG_MULTISTAGE },
   { "animation", CLUTTER_DEBUG_ANIMATION },
-  { "layout", CLUTTER_DEBUG_LAYOUT }
+  { "layout", CLUTTER_DEBUG_LAYOUT },
+  { "nop-picking", CLUTTER_DEBUG_NOP_PICKING },
+  { "dump-pick-buffers", CLUTTER_DEBUG_DUMP_PICK_BUFFERS }
 };
 #endif /* CLUTTER_ENABLE_DEBUG */
 
@@ -143,7 +153,7 @@ _clutter_stage_maybe_relayout (ClutterActor *stage)
                     (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);
     }
@@ -152,12 +162,15 @@ _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;
       gfloat width, height;
 
-      clutter_actor_get_size (CLUTTER_ACTOR (stage), &width, &height);
+      clutter_actor_get_preferred_size (CLUTTER_ACTOR (stage),
+                                        NULL, NULL,
+                                        &width, &height);
       clutter_stage_get_perspective (stage, &perspective);
 
       CLUTTER_NOTE (PAINT,
@@ -174,21 +187,15 @@ _clutter_stage_maybe_setup_viewport (ClutterStage *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().
- */
 void
-clutter_redraw (ClutterStage *stage)
+_clutter_do_redraw (ClutterStage *stage)
 {
   ClutterMainContext *ctx;
   ClutterMasterClock *master_clock;
   static GTimer *timer = NULL;
   static guint timer_n_frames = 0;
 
-  ctx  = clutter_context_get_default ();
+  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 */
@@ -214,11 +221,6 @@ clutter_redraw (ClutterStage *stage)
   */
   _clutter_backend_redraw (ctx->backend, stage);
 
-  /* prepare for the next frame; if anything queues a redraw as the
-   * result of a timeline, this will end up redrawing the scene
-   */
-  _clutter_master_clock_advance (master_clock);
-
   /* Complete FPS info */
   if (G_UNLIKELY (clutter_get_show_fps ()))
     {
@@ -236,6 +238,23 @@ clutter_redraw (ClutterStage *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
  *
@@ -255,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;
 }
@@ -272,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;
 }
@@ -287,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;
 }
@@ -296,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))
@@ -325,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
@@ -334,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);
@@ -355,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,
@@ -362,23 +473,27 @@ _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_clip_push_window_rect (x, y, 1, 1);
-  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);
 
@@ -393,13 +508,25 @@ _clutter_do_pick (ClutterStage   *stage,
   context->pick_mode = mode;
   clutter_actor_paint (CLUTTER_ACTOR (stage));
   context->pick_mode = CLUTTER_PICK_NONE;
-  cogl_clip_pop ();
 
-  /* 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 ();
+
+  /* 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)
@@ -413,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;
@@ -423,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
     {
@@ -438,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.");
     }
@@ -455,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);
@@ -507,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;
 }
@@ -1045,23 +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;
 
-      ctx->master_clock = _clutter_master_clock_get_default ();
-
 #ifdef CLUTTER_ENABLE_DEBUG
-      ctx->timer          =  g_timer_new ();
+      ctx->timer = g_timer_new ();
       g_timer_start (ctx->timer);
 #endif
     }
@@ -1084,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);
@@ -1101,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;
 }
@@ -1150,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)
@@ -1202,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
@@ -1219,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);
 
@@ -1238,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;
 
@@ -1293,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);
 
@@ -1355,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));
 
@@ -1420,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"),
@@ -1463,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 ();
@@ -1479,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
@@ -1522,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)
     {
@@ -1606,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
@@ -1627,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)
     {
@@ -1679,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);
 
@@ -1858,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)
@@ -1990,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.
    *
@@ -1998,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);
 
@@ -2076,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.
          */
@@ -2271,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);
 
@@ -2303,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
  *
@@ -2318,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;
 }
@@ -2327,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
  */
@@ -2337,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;
@@ -2351,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)
     {
@@ -2392,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;
@@ -2509,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;
 }
@@ -2521,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 ();
@@ -2552,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;
@@ -2601,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;
 
-      frequency = clutter_default_fps / 4;
-      frequency = CLAMP (frequency, 20, 45);
+  context = _clutter_context_get_default ();
 
-      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;
 }
 
 /**
@@ -2783,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;
@@ -2977,3 +3004,51 @@ _clutter_run_repaint_functions (void)
   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;
+}