]> Pileus Git - ~andy/gtk/blobdiff - gdk/gdk.c
Use G_DEFINE_TYPE.
[~andy/gtk] / gdk / gdk.c
index f1712e876aed9210ab9f17cb4762e45294ced1b1..a6aec950ae6955cbc5d205310ce039134d60c6b9 100644 (file)
--- a/gdk/gdk.c
+++ b/gdk/gdk.c
  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
  */
 
-#include "config.h"
+#include <config.h>
 
 #include <string.h>
 #include <stdlib.h>
 
 #include "gdk.h"
 #include "gdkinternals.h"
+#include "gdkintl.h"
 
 #ifndef HAVE_XCONVERTCASE
 #include "gdkkeysyms.h"
 #endif
+#include "gdkalias.h"
 
 typedef struct _GdkPredicate  GdkPredicate;
 
@@ -51,8 +53,6 @@ static int gdk_initialized = 0;                           /* 1 if the library is initialized,
                                                     */
 
 static gchar  *gdk_progclass = NULL;
-static gint    gdk_argc = 0;
-static gchar **gdk_argv = NULL;
 
 #ifdef G_ENABLE_DEBUG
 static const GDebugKey gdk_debug_keys[] = {
@@ -69,142 +69,14 @@ static const GDebugKey gdk_debug_keys[] = {
   {"input",        GDK_DEBUG_INPUT},
   {"cursor",       GDK_DEBUG_CURSOR},
   {"multihead",            GDK_DEBUG_MULTIHEAD},
+  {"xinerama",     GDK_DEBUG_XINERAMA},
+  {"draw",         GDK_DEBUG_DRAW}
 };
 
 static const int gdk_ndebug_keys = G_N_ELEMENTS (gdk_debug_keys);
 
 #endif /* G_ENABLE_DEBUG */
 
-static GdkArgContext *
-gdk_arg_context_new (gpointer cb_data)
-{
-  GdkArgContext *result = g_new (GdkArgContext, 1);
-  result->tables = g_ptr_array_new ();
-  result->cb_data = cb_data;
-
-  return result;
-}
-
-static void
-gdk_arg_context_destroy (GdkArgContext *context)
-{
-  g_ptr_array_free (context->tables, TRUE);
-  g_free (context);
-}
-
-static void
-gdk_arg_context_add_table (GdkArgContext *context, GdkArgDesc *table)
-{
-  g_ptr_array_add (context->tables, table);
-}
-
-static void
-gdk_arg_context_parse (GdkArgContext *context, gint *argc, gchar ***argv)
-{
-  int i, j, k;
-
-  /* Save a copy of the original argc and argv */
-  if (argc && argv)
-    {
-      for (i = 1; i < *argc; i++)
-       {
-         char *arg;
-         
-         if (!(*argv)[i][0] == '-' && (*argv)[i][1] == '-')
-           continue;
-         
-         arg = (*argv)[i] + 2;
-
-         /* '--' terminates list of arguments */
-         if (arg == 0)
-           {
-             (*argv)[i] = NULL;
-             break;
-           }
-             
-         for (j = 0; j < context->tables->len; j++)
-           {
-             GdkArgDesc *table = context->tables->pdata[j];
-             for (k = 0; table[k].name; k++)
-               {
-                 switch (table[k].type)
-                   {
-                   case GDK_ARG_STRING:
-                   case GDK_ARG_CALLBACK:
-                   case GDK_ARG_INT:
-                     {
-                       int len = strlen (table[k].name);
-                       
-                       if (strncmp (arg, table[k].name, len) == 0 &&
-                           (arg[len] == '=' || arg[len] == 0))
-                         {
-                           char *value = NULL;
-
-                           (*argv)[i] = NULL;
-
-                           if (arg[len] == '=')
-                             value = arg + len + 1;
-                           else if (i < *argc - 1)
-                             {
-                               value = (*argv)[i + 1];
-                               (*argv)[i+1] = NULL;
-                               i++;
-                             }
-                           else
-                             value = "";
-
-                           switch (table[k].type)
-                             {
-                             case GDK_ARG_STRING:
-                               *(gchar **)table[k].location = g_strdup (value);
-                               break;
-                             case GDK_ARG_INT:
-                               *(gint *)table[k].location = atoi (value);
-                               break;
-                             case GDK_ARG_CALLBACK:
-                               (*table[k].callback)(table[k].name, value, context->cb_data);
-                               break;
-                             default:
-                               ;
-                             }
-
-                           goto next_arg;
-                         }
-                       break;
-                     }
-                   case GDK_ARG_BOOL:
-                   case GDK_ARG_NOBOOL:
-                     if (strcmp (arg, table[k].name) == 0)
-                       {
-                         (*argv)[i] = NULL;
-                         
-                         *(gboolean *)table[k].location = (table[k].type == GDK_ARG_BOOL) ? TRUE : FALSE;
-                         goto next_arg;
-                       }
-                   }
-               }
-           }
-       next_arg:
-         ;
-       }
-         
-      for (i = 1; i < *argc; i++)
-       {
-         for (k = i; k < *argc; k++)
-           if ((*argv)[k] != NULL)
-             break;
-         
-         if (k > i)
-           {
-             k -= i;
-             for (j = i + k; j < *argc; j++)
-               (*argv)[j-k] = (*argv)[j];
-             *argc -= k;
-           }
-       }
-    }
-}
-
 #ifdef G_ENABLE_DEBUG
 static void
 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
@@ -223,49 +95,90 @@ gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
 }
 #endif /* G_ENABLE_DEBUG */
 
-static void
-gdk_arg_class_cb (const char *key, const char *value, gpointer user_data)
+static gboolean
+gdk_arg_class_cb (const char *key, const char *value, gpointer user_data, GError **error)
 {
   gdk_set_program_class (value);
+
+  return TRUE;
 }
 
-static void
-gdk_arg_name_cb (const char *key, const char *value, gpointer user_data)
+static gboolean
+gdk_arg_name_cb (const char *key, const char *value, gpointer user_data, GError **error)
 {
   g_set_prgname (value);
-}
 
-static GdkArgDesc gdk_args[] = {
-  { "class" ,       GDK_ARG_CALLBACK, NULL, gdk_arg_class_cb                   },
-  { "name",         GDK_ARG_CALLBACK, NULL, gdk_arg_name_cb                    },
-  { "display",      GDK_ARG_STRING,   &_gdk_display_name,     (GdkArgFunc)NULL },
-  { "screen",       GDK_ARG_INT,      &_gdk_screen_number,    (GdkArgFunc)NULL },
+  return TRUE;
+}
 
+static const GOptionEntry gdk_args[] = {
+  { "class",        0, 0,                     G_OPTION_ARG_CALLBACK, gdk_arg_class_cb,
+    /* Description of --class=CLASS in --help output */        N_("Program class as used by the window manager"),
+    /* Placeholder in --class=CLASS in --help output */        N_("CLASS") },
+  { "name",         0, 0,                     G_OPTION_ARG_CALLBACK, gdk_arg_name_cb,
+    /* Description of --name=NAME in --help output */          N_("Program name as used by the window manager"),
+    /* Placeholder in --name=NAME in --help output */          N_("NAME") },
+  { "display",      0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,   &_gdk_display_name,
+    /* Description of --display=DISPLAY in --help output */    N_("X display to use"),
+    /* Placeholder in --display=DISPLAY in --help output */    N_("DISPLAY") },
+  { "screen",       0, 0, G_OPTION_ARG_INT,      &_gdk_screen_number,
+    /* Description of --screen=SCREEN in --help output */      N_("X screen to use"),
+    /* Placeholder in --screen=SCREEN in --help output */      N_("SCREEN") },
 #ifdef G_ENABLE_DEBUG
-  { "gdk-debug",    GDK_ARG_CALLBACK, NULL, gdk_arg_debug_cb    },
-  { "gdk-no-debug", GDK_ARG_CALLBACK, NULL, gdk_arg_no_debug_cb },
-#endif /* G_ENABLE_DEBUG */
+  { "gdk-debug",    0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_debug_cb,  
+    /* Description of --gdk-debug=FLAGS in --help output */    N_("Gdk debugging flags to set"),
+    /* Placeholder in --gdk-debug=FLAGS in --help output */    N_("FLAGS") },
+  { "gdk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_no_debug_cb, 
+    /* Description of --gdk-no-debug=FLAGS in --help output */ N_("Gdk debugging flags to unset"), 
+    /* Placeholder in --gdk-no-debug=FLAGS in --help output */ N_("FLAGS") },
+#endif 
   { NULL }
 };
 
-
 /**
- * _gdk_get_command_line_args:
- * @argv: location to store argv pointer
- * @argc: location 
+ * gdk_add_option_entries_libgtk_only:
+ * @group: An option group.
  * 
- * Retrieve the command line arguments passed to gdk_init().
- * The returned argv pointer points to static storage ; no
- * copy is made.
+ * Appends gdk option entries to the passed in option group. This is
+ * not public API and must not be used by applications.
  **/
 void
-_gdk_get_command_line_args (int    *argc,
-                           char ***argv)
+gdk_add_option_entries_libgtk_only (GOptionGroup *group)
+{
+  g_option_group_add_entries (group, gdk_args);
+  g_option_group_add_entries (group, _gdk_windowing_args);
+}
+
+void
+gdk_pre_parse_libgtk_only (void)
 {
-  *argc = gdk_argc;
-  *argv = gdk_argv;
+  gdk_initialized = TRUE;
+
+  /* We set the fallback program class here, rather than lazily in
+   * gdk_get_program_class, since we don't want -name to override it.
+   */
+  gdk_progclass = g_strdup (g_get_prgname ());
+  if (gdk_progclass && gdk_progclass[0])
+    gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
+  
+#ifdef G_ENABLE_DEBUG
+  {
+    gchar *debug_string = getenv("GDK_DEBUG");
+    if (debug_string != NULL)
+      _gdk_debug_flags = g_parse_debug_string (debug_string,
+                                             (GDebugKey *) gdk_debug_keys,
+                                             gdk_ndebug_keys);
+  }
+#endif /* G_ENABLE_DEBUG */
+
+  g_type_init ();
+
+  /* Do any setup particular to the windowing system
+   */
+  _gdk_windowing_init ();  
 }
 
+  
 /**
  * gdk_parse_args:
  * @argc: the number of command line arguments.
@@ -279,74 +192,39 @@ _gdk_get_command_line_args (int    *argc,
  *
  * You shouldn't call this function explicitely if you are using
  * gtk_init(), gtk_init_check(), gdk_init(), or gdk_init_check().
+ *
+ * Since: 2.2
  **/
 void
 gdk_parse_args (int    *argc,
                char ***argv)
 {
-  GdkArgContext *arg_context;
-  gint i;
+  GOptionContext *option_context;
+  GOptionGroup *option_group;
+  GError *error = NULL;
 
   if (gdk_initialized)
     return;
 
-  gdk_initialized = TRUE;
+  gdk_pre_parse_libgtk_only ();
+  
+  option_context = g_option_context_new (NULL);
+  g_option_context_set_ignore_unknown_options (option_context, TRUE);
+  g_option_context_set_help_enabled (option_context, FALSE);
+  option_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
+  g_option_context_set_main_group (option_context, option_group);
+  
+  g_option_group_add_entries (option_group, gdk_args);
+  g_option_group_add_entries (option_group, _gdk_windowing_args);
 
-  if (argc && argv)
+  if (!g_option_context_parse (option_context, argc, argv, &error))
     {
-      gdk_argc = *argc;
-      
-      gdk_argv = g_malloc ((gdk_argc + 1) * sizeof (char*));
-      for (i = 0; i < gdk_argc; i++)
-       gdk_argv[i] = g_strdup ((*argv)[i]);
-      gdk_argv[gdk_argc] = NULL;
-
-      if (*argc > 0)
-       {
-         gchar *d;
-         
-         d = strrchr((*argv)[0], G_DIR_SEPARATOR);
-         if (d != NULL)
-           g_set_prgname (d + 1);
-         else
-           g_set_prgname ((*argv)[0]);
-       }
+      g_warning ("%s", error->message);
+      g_error_free (error);
     }
-  else
-    {
-      g_set_prgname ("<unknown>");
-    }
-
-  /* We set the fallback program class here, rather than lazily in
-   * gdk_get_program_class, since we don't want -name to override it.
-   */
-  gdk_progclass = g_strdup (g_get_prgname ());
-  if (gdk_progclass[0])
-    gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
-  
-#ifdef G_ENABLE_DEBUG
-  {
-    gchar *debug_string = getenv("GDK_DEBUG");
-    if (debug_string != NULL)
-      _gdk_debug_flags = g_parse_debug_string (debug_string,
-                                             (GDebugKey *) gdk_debug_keys,
-                                             gdk_ndebug_keys);
-  }
-#endif /* G_ENABLE_DEBUG */
-  
-  arg_context = gdk_arg_context_new (NULL);
-  gdk_arg_context_add_table (arg_context, gdk_args);
-  gdk_arg_context_add_table (arg_context, _gdk_windowing_args);
-  gdk_arg_context_parse (arg_context, argc, argv);
-  gdk_arg_context_destroy (arg_context);
+  g_option_context_free (option_context);
   
   GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
-
-  g_type_init ();
-
-  /* Do any setup particular to the windowing system
-   */
-  _gdk_windowing_init (argc, argv);
 }
 
 /** 
@@ -357,6 +235,8 @@ gdk_parse_args (int    *argc,
  *
  * Returns: the display name, if specified explicitely, otherwise %NULL
  *   this string is owned by GTK+ and must not be modified or freed.
+ *
+ * Since: 2.2
  */
 G_CONST_RETURN gchar *
 gdk_get_display_arg_name (void)
@@ -389,7 +269,7 @@ gdk_display_open_default_libgtk_only (void)
 {
   GdkDisplay *display;
 
-  g_return_if_fail (gdk_initialized);
+  g_return_val_if_fail (gdk_initialized, NULL);
   
   display = gdk_display_get_default ();
   if (display)
@@ -438,8 +318,6 @@ gboolean
 gdk_init_check (int    *argc,
                char ***argv)
 {
-  GdkDisplay *display;
-  
   gdk_parse_args (argc, argv);
 
   return gdk_display_open_default_libgtk_only () != NULL;
@@ -477,23 +355,35 @@ gdk_init (int *argc, char ***argv)
 void
 gdk_exit (gint errorcode)
 {
-  /* de-initialisation is done by the gdk_exit_funct(),
-     no need to do this here (Alex J.) */
   exit (errorcode);
 }
 
 void
-gdk_threads_enter ()
+gdk_threads_enter (void)
 {
   GDK_THREADS_ENTER ();
 }
 
 void
-gdk_threads_leave ()
+gdk_threads_leave (void)
 {
   GDK_THREADS_LEAVE ();
 }
 
+static void
+gdk_threads_impl_lock (void)
+{
+  if (gdk_threads_mutex)
+    g_mutex_lock (gdk_threads_mutex);
+}
+
+static void
+gdk_threads_impl_unlock (void)
+{
+  if (gdk_threads_mutex)
+    g_mutex_unlock (gdk_threads_mutex);
+}
+
 /**
  * gdk_threads_init:
  * 
@@ -505,12 +395,56 @@ gdk_threads_leave ()
  * GTK+; to be safe, call it before gtk_init().
  **/
 void
-gdk_threads_init ()
+gdk_threads_init (void)
 {
   if (!g_thread_supported ())
     g_error ("g_thread_init() must be called before gdk_threads_init()");
 
   gdk_threads_mutex = g_mutex_new ();
+  if (!gdk_threads_lock)
+    gdk_threads_lock = gdk_threads_impl_lock;
+  if (!gdk_threads_unlock)
+    gdk_threads_unlock = gdk_threads_impl_unlock;
+}
+
+/**
+ * gdk_threads_set_lock_functions:
+ * @enter_fn:   function called to guard GDK
+ * @leave_fn: function called to release the guard
+ *
+ * Allows the application to replace the standard method that
+ * GDK uses to protect its data structures. Normally, GDK
+ * creates a single #GMutex that is locked by gdk_threads_enter(),
+ * and released by gdk_threads_leave(); using this function an
+ * application provides, instead, a function @enter_fn that is
+ * called by gdk_threads_enter() and a function @leave_fn that is
+ * called by gdk_threads_leave().
+ *
+ * The functions must provide at least same locking functionality
+ * as the default implementation, but can also do extra application
+ * specific processing.
+ *
+ * As an example, consider an application that has its own recursive
+ * lock that when held, holds the GTK+ lock as well. When GTK+ unlocks
+ * the GTK+ lock when entering a recursive main loop, the application
+ * must temporarily release its lock as well.
+ *
+ * Most threaded GTK+ apps won't need to use this method.
+ *
+ * This method must be called before gdk_threads_init(), and cannot
+ * be called multiple times.
+ *
+ * Since: 2.4
+ **/
+void
+gdk_threads_set_lock_functions (GCallback enter_fn,
+                               GCallback leave_fn)
+{
+  g_return_if_fail (gdk_threads_lock == NULL &&
+                   gdk_threads_unlock == NULL);
+
+  gdk_threads_lock = enter_fn;
+  gdk_threads_unlock = leave_fn;
 }
 
 G_CONST_RETURN char *
@@ -527,3 +461,6 @@ gdk_set_program_class (const char *program_class)
 
   gdk_progclass = g_strdup (program_class);
 }
+
+#define __GDK_C__
+#include "gdkaliasdef.c"