[mutter] context: Handle dealing with option entries



commit e09de6787f01c7160be8a8bdd83cfaf0a7e35a3f
Author: Jonas Ã…dahl <jadahl gmail com>
Date:   Tue Mar 2 16:46:05 2021 +0100

    context: Handle dealing with option entries
    
    Users can add option entries, and it'll be part of the configuration
    phase.
    
    Create the main group manually to be able to set a user_data pointer;
    this will be required to not have to rely on globals when parsing
    options using a callback.
    
    Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1861>

 src/core/meta-context.c       | 43 +++++++++++++++++++++++++++++++++++++++++++
 src/meta/meta-context.h       |  5 +++++
 src/tests/meta-context-test.c |  5 +++++
 3 files changed, 53 insertions(+)
---
diff --git a/src/core/meta-context.c b/src/core/meta-context.c
index 491cfe9387..a7a29d241e 100644
--- a/src/core/meta-context.c
+++ b/src/core/meta-context.c
@@ -51,12 +51,26 @@ typedef struct _MetaContextPrivate
   char *name;
   char *plugin_name;
 
+  GOptionContext *option_context;
+
   GMainLoop *main_loop;
   GError *termination_error;
 } MetaContextPrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (MetaContext, meta_context, G_TYPE_OBJECT)
 
+void
+meta_context_add_option_entries (MetaContext        *context,
+                                 const GOptionEntry *entries,
+                                 const char         *translation_domain)
+{
+  MetaContextPrivate *priv = meta_context_get_instance_private (context);
+
+  g_option_context_add_main_entries (priv->option_context,
+                                     entries,
+                                     translation_domain);
+}
+
 void
 meta_context_set_plugin_name (MetaContext *context,
                               const char  *plugin_name)
@@ -78,6 +92,26 @@ meta_context_get_compositor_type (MetaContext *context)
   return META_CONTEXT_GET_CLASS (context)->get_compositor_type (context);
 }
 
+static gboolean
+meta_context_real_configure (MetaContext   *context,
+                             int           *argc,
+                             char        ***argv,
+                             GError       **error)
+{
+  MetaContextPrivate *priv = meta_context_get_instance_private (context);
+  g_autoptr (GOptionContext) option_context = NULL;
+
+  if (!priv->option_context)
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                   "Tried to configure multiple times");
+      return FALSE;
+    }
+
+  option_context = g_steal_pointer (&priv->option_context);
+  return g_option_context_parse (option_context, argc, argv, error);
+}
+
 gboolean
 meta_context_configure (MetaContext   *context,
                         int           *argc,
@@ -347,6 +381,7 @@ meta_context_finalize (GObject *object)
 
   meta_release_backend ();
 
+  g_clear_pointer (&priv->option_context, g_option_context_free);
   g_clear_pointer (&priv->main_loop, g_main_loop_unref);
   g_clear_pointer (&priv->plugin_name, g_free);
   g_clear_pointer (&priv->name, g_free);
@@ -381,6 +416,7 @@ meta_context_class_init (MetaContextClass *klass)
   object_class->set_property = meta_context_set_property;
   object_class->finalize = meta_context_finalize;
 
+  klass->configure = meta_context_real_configure;
   klass->setup = meta_context_real_setup;
 
   obj_props[PROP_NAME] =
@@ -397,6 +433,8 @@ meta_context_class_init (MetaContextClass *klass)
 static void
 meta_context_init (MetaContext *context)
 {
+  MetaContextPrivate *priv = meta_context_get_instance_private (context);
+
   g_assert (!_context_temporary);
   _context_temporary = context;
 
@@ -404,4 +442,9 @@ meta_context_init (MetaContext *context)
     g_warning ("Locale not understood by C library");
   bindtextdomain (GETTEXT_PACKAGE, MUTTER_LOCALEDIR);
   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+
+  priv->option_context = g_option_context_new (NULL);
+  g_option_context_set_main_group (priv->option_context,
+                                   g_option_group_new (NULL, NULL, NULL,
+                                                       context, NULL));
 }
diff --git a/src/meta/meta-context.h b/src/meta/meta-context.h
index 0d91c2cc67..003e863e92 100644
--- a/src/meta/meta-context.h
+++ b/src/meta/meta-context.h
@@ -29,6 +29,11 @@
 META_EXPORT
 G_DECLARE_DERIVABLE_TYPE (MetaContext, meta_context, META, CONTEXT, GObject)
 
+META_EXPORT
+void meta_context_add_option_entries (MetaContext        *context,
+                                      const GOptionEntry *entries,
+                                      const char         *translation_domain);
+
 META_EXPORT
 void meta_context_set_plugin_name (MetaContext *context,
                                    const char  *plugin_name);
diff --git a/src/tests/meta-context-test.c b/src/tests/meta-context-test.c
index b9dcf327c7..dce9aa523d 100644
--- a/src/tests/meta-context-test.c
+++ b/src/tests/meta-context-test.c
@@ -58,8 +58,13 @@ meta_context_test_configure (MetaContext   *context,
                              char        ***argv,
                              GError       **error)
 {
+  MetaContextClass *context_class =
+    META_CONTEXT_CLASS (meta_context_test_parent_class);
   const char *plugin_name;
 
+  if (!context_class->configure (context, argc, argv, error))
+    return FALSE;
+
   g_test_init (argc, argv, NULL);
   g_test_bug_base ("https://gitlab.gnome.org/GNOME/mutter/issues/";);
 


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