[clutter-gtk] Make calling gtk_clutter_init* multiple times safe



commit fc96f3db0b88454f2f2a52c0c7aa54a018c3ce28
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Fri Mar 18 14:57:20 2011 +0000

    Make calling gtk_clutter_init* multiple times safe
    
    Clutter-GTK should be resilient against multiple initializations, just
    like Clutter and GTK+ are.

 clutter-gtk/gtk-clutter-util.c |   14 +++++++++++++-
 examples/gtk-clutter-test.c    |   31 ++++++++++++++++++++-----------
 2 files changed, 33 insertions(+), 12 deletions(-)
---
diff --git a/clutter-gtk/gtk-clutter-util.c b/clutter-gtk/gtk-clutter-util.c
index 3c3df7e..b4d7bbd 100644
--- a/clutter-gtk/gtk-clutter-util.c
+++ b/clutter-gtk/gtk-clutter-util.c
@@ -40,12 +40,16 @@ static const guint clutter_gtk_major_version = CLUTTER_GTK_MAJOR_VERSION;
 static const guint clutter_gtk_minor_version = CLUTTER_GTK_MINOR_VERSION;
 static const guint clutter_gtk_micro_version = CLUTTER_GTK_MICRO_VERSION;
 
+static gboolean gtk_clutter_is_initialized = FALSE;
+
 static gboolean
 post_parse_hook (GOptionContext  *context,
                  GOptionGroup    *group,
                  gpointer         data,
                  GError         **error)
 {
+  gtk_clutter_is_initialized = TRUE;
+
 #if defined(GDK_WINDOWING_X11)
   /* share the X11 Display with GTK+ */
   clutter_x11_set_display (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()));
@@ -60,7 +64,7 @@ post_parse_hook (GOptionContext  *context,
   /* this is required since parsing clutter's option group did not
    * complete the initialization process
    */
-  return clutter_init_with_args (NULL, NULL, NULL, NULL, NULL, error);
+  return clutter_init_with_args (NULL, NULL, NULL, NULL, NULL, error) == CLUTTER_INIT_SUCCESS;
 }
 
 /**
@@ -132,6 +136,11 @@ ClutterInitError
 gtk_clutter_init (int    *argc,
                   char ***argv)
 {
+  if (gtk_clutter_is_initialized)
+    return CLUTTER_INIT_SUCCESS;
+
+  gtk_clutter_is_initialized = TRUE;
+
   gdk_disable_multidevice ();
 
   if (!gtk_init_check (argc, argv))
@@ -190,6 +199,9 @@ gtk_clutter_init_with_args (int            *argc,
   GOptionContext *context;
   gboolean res;
 
+  if (gtk_clutter_is_initialized)
+    return CLUTTER_INIT_SUCCESS;
+
   gdk_disable_multidevice ();
 
 #if defined(GDK_WINDOWING_X11) && CLUTTER_CHECK_VERSION (1, 1, 5)
diff --git a/examples/gtk-clutter-test.c b/examples/gtk-clutter-test.c
index e1aefbb..2ec4c26 100644
--- a/examples/gtk-clutter-test.c
+++ b/examples/gtk-clutter-test.c
@@ -1,10 +1,8 @@
-#include "config.h"
+#include <stdlib.h>
+#include <math.h>
 
 #include <gtk/gtk.h>
 #include <clutter/clutter.h>
-#include <math.h>
-
-#include <glib/gi18n.h>
 
 #include <clutter-gtk/clutter-gtk.h>
 
@@ -121,13 +119,24 @@ main (int argc, char *argv[])
   GError          *error;
 
   error = NULL;
-  gtk_clutter_init_with_args (&argc, &argv,
-                              NULL,
-                              NULL,
-                              NULL,
-                              &error);
-  if (error)
-    g_error ("Unable to initialize: %s", error->message);
+  if (gtk_clutter_init_with_args (&argc, &argv,
+                                  NULL,
+                                  NULL,
+                                  NULL,
+                                  &error) != CLUTTER_INIT_SUCCESS)
+    {
+      if (error)
+        {
+          g_critical ("Unable to initialize Clutter-GTK: %s", error->message);
+          g_error_free (error);
+          return EXIT_FAILURE;
+        }
+      else
+        g_error ("Unable to initialize Clutter-GTK");
+    }
+
+  /* calling gtk_clutter_init* multiple times should be safe */
+  g_assert (gtk_clutter_init (NULL, NULL) == CLUTTER_INIT_SUCCESS);
 
   pixbuf = gdk_pixbuf_new_from_file ("redhand.png", NULL);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]