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>
This commit is contained in:
Jonas Ådahl 2021-03-02 16:46:05 +01:00
parent 3d2160b473
commit e09de6787f
3 changed files with 53 additions and 0 deletions

View File

@ -51,12 +51,26 @@ typedef struct _MetaContextPrivate
char *name; char *name;
char *plugin_name; char *plugin_name;
GOptionContext *option_context;
GMainLoop *main_loop; GMainLoop *main_loop;
GError *termination_error; GError *termination_error;
} MetaContextPrivate; } MetaContextPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (MetaContext, meta_context, G_TYPE_OBJECT) 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 void
meta_context_set_plugin_name (MetaContext *context, meta_context_set_plugin_name (MetaContext *context,
const char *plugin_name) 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); 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 gboolean
meta_context_configure (MetaContext *context, meta_context_configure (MetaContext *context,
int *argc, int *argc,
@ -347,6 +381,7 @@ meta_context_finalize (GObject *object)
meta_release_backend (); 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->main_loop, g_main_loop_unref);
g_clear_pointer (&priv->plugin_name, g_free); g_clear_pointer (&priv->plugin_name, g_free);
g_clear_pointer (&priv->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->set_property = meta_context_set_property;
object_class->finalize = meta_context_finalize; object_class->finalize = meta_context_finalize;
klass->configure = meta_context_real_configure;
klass->setup = meta_context_real_setup; klass->setup = meta_context_real_setup;
obj_props[PROP_NAME] = obj_props[PROP_NAME] =
@ -397,6 +433,8 @@ meta_context_class_init (MetaContextClass *klass)
static void static void
meta_context_init (MetaContext *context) meta_context_init (MetaContext *context)
{ {
MetaContextPrivate *priv = meta_context_get_instance_private (context);
g_assert (!_context_temporary); g_assert (!_context_temporary);
_context_temporary = context; _context_temporary = context;
@ -404,4 +442,9 @@ meta_context_init (MetaContext *context)
g_warning ("Locale not understood by C library"); g_warning ("Locale not understood by C library");
bindtextdomain (GETTEXT_PACKAGE, MUTTER_LOCALEDIR); bindtextdomain (GETTEXT_PACKAGE, MUTTER_LOCALEDIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); 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));
} }

View File

@ -29,6 +29,11 @@
META_EXPORT META_EXPORT
G_DECLARE_DERIVABLE_TYPE (MetaContext, meta_context, META, CONTEXT, GObject) 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 META_EXPORT
void meta_context_set_plugin_name (MetaContext *context, void meta_context_set_plugin_name (MetaContext *context,
const char *plugin_name); const char *plugin_name);

View File

@ -58,8 +58,13 @@ meta_context_test_configure (MetaContext *context,
char ***argv, char ***argv,
GError **error) GError **error)
{ {
MetaContextClass *context_class =
META_CONTEXT_CLASS (meta_context_test_parent_class);
const char *plugin_name; const char *plugin_name;
if (!context_class->configure (context, argc, argv, error))
return FALSE;
g_test_init (argc, argv, NULL); g_test_init (argc, argv, NULL);
g_test_bug_base ("https://gitlab.gnome.org/GNOME/mutter/issues/"); g_test_bug_base ("https://gitlab.gnome.org/GNOME/mutter/issues/");