[retro-gtk] retro-core: Add retro_core_override_option_default()



commit e7349b2c750fbd331b9931ccde9c57ef8ddcc520
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Mon Jan 6 19:30:31 2020 +0500

    retro-core: Add retro_core_override_option_default()
    
    This will allow to avoid problems with multiprocess model, where
    options-set signal won't work because setting options must be sync.

 retro-gtk/retro-core-private.h |  1 +
 retro-gtk/retro-core.c         | 37 +++++++++++++++++++++++++++++++++++++
 retro-gtk/retro-core.h         |  3 +++
 3 files changed, 41 insertions(+)
---
diff --git a/retro-gtk/retro-core-private.h b/retro-gtk/retro-core-private.h
index 4c82b06..8e92eed 100644
--- a/retro-gtk/retro-core-private.h
+++ b/retro-gtk/retro-core-private.h
@@ -51,6 +51,7 @@ struct _RetroCore
   gulong key_press_event_id;
   gulong key_release_event_id;
   GHashTable *options;
+  GHashTable *option_overrides;
   gboolean variable_updated;
   guint runahead;
   gssize run_remaining;
diff --git a/retro-gtk/retro-core.c b/retro-gtk/retro-core.c
index d2ce107..d925dac 100644
--- a/retro-gtk/retro-core.c
+++ b/retro-gtk/retro-core.c
@@ -174,6 +174,7 @@ retro_core_finalize (GObject *object)
       g_object_unref (self->default_controllers[i]);
   g_hash_table_unref (self->controllers);
   g_hash_table_unref (self->options);
+  g_hash_table_unref (self->option_overrides);
 
   g_free (self->filename);
   g_free (self->system_directory);
@@ -549,6 +550,8 @@ retro_core_init (RetroCore *self)
 {
   self->options = g_hash_table_new_full (g_str_hash, g_str_equal,
                                          g_free, g_object_unref);
+  self->option_overrides = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                                  g_free, g_free);
 
   self->controllers = g_hash_table_new_full (g_int_hash, g_int_equal,
                                              g_free, g_object_unref);
@@ -1099,6 +1102,16 @@ retro_core_insert_variable (RetroCore           *self,
 
   key = retro_option_get_key (option);
 
+  if (g_hash_table_contains (self->option_overrides, key)) {
+    const gchar *value = g_hash_table_lookup (self->option_overrides, key);
+
+    retro_option_set_value (option, value, &tmp_error);
+    if (G_UNLIKELY (tmp_error != NULL)) {
+      g_warning ("%s", tmp_error->message);
+      g_clear_error (&tmp_error);
+    }
+  }
+
   g_hash_table_insert (self->options, g_strdup (key), option);
 
   g_signal_connect_object (option,
@@ -2170,6 +2183,30 @@ retro_core_iterate_options (RetroCore *self)
   return retro_option_iterator_new (self->options);
 }
 
+/**
+ * retro_core_override_option_default:
+ * @self: a #RetroCore
+ * @key: the key of the option
+ * @value: the default value
+ *
+ * Overrides default value for the option @key. This can be used to set value
+ * for a startup-only option.
+ *
+ * You can use this before booting the core.
+ */
+void
+retro_core_override_option_default (RetroCore   *self,
+                                    const gchar *key,
+                                    const gchar *value)
+{
+  g_return_if_fail (RETRO_IS_CORE (self));
+  g_return_if_fail (key != NULL);
+  g_return_if_fail (key != NULL);
+  g_return_if_fail (!retro_core_get_is_initiated (self));
+
+  g_hash_table_replace (self->option_overrides, g_strdup (key), g_strdup (value));
+}
+
 /**
  * retro_core_new:
  * @filename: the filename of a Libretro core
diff --git a/retro-gtk/retro-core.h b/retro-gtk/retro-core.h
index 146627a..13ca1ab 100644
--- a/retro-gtk/retro-core.h
+++ b/retro-gtk/retro-core.h
@@ -72,5 +72,8 @@ gboolean retro_core_has_option (RetroCore   *self,
 RetroOption *retro_core_get_option (RetroCore   *self,
                                     const gchar *key);
 RetroOptionIterator *retro_core_iterate_options (RetroCore *self);
+void retro_core_override_option_default (RetroCore   *self,
+                                         const gchar *key,
+                                         const gchar *value);
 
 G_END_DECLS


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