[retro-gtk/wip/aplazas/options: 3/3] Merge options into core



commit c010c6d69aaa527c22316ede467ab27474fbf69e
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Fri Nov 24 11:30:39 2017 +0100

    Merge options into core

 retro-gtk/meson.build            |    2 +-
 retro-gtk/retro-core-private.h   |    8 +-
 retro-gtk/retro-core.c           |   80 ++++++++++++++++-
 retro-gtk/retro-core.h           |    6 +
 retro-gtk/retro-environment.c    |   12 ++-
 retro-gtk/retro-gtk.h            |    1 +
 retro-gtk/retro-option-private.h |   20 ++++
 retro-gtk/retro-option.c         |    2 +-
 retro-gtk/retro-option.h         |    3 -
 retro-gtk/retro-options.c        |  187 --------------------------------------
 retro-gtk/retro-options.h        |   39 --------
 11 files changed, 120 insertions(+), 240 deletions(-)
---
diff --git a/retro-gtk/meson.build b/retro-gtk/meson.build
index 00e9486..ef131a7 100644
--- a/retro-gtk/meson.build
+++ b/retro-gtk/meson.build
@@ -34,7 +34,6 @@ retro_gtk_sources = [
   'retro-module-iterator.c',
   'retro-module-query.c',
   'retro-option.c',
-  'retro-options.c',
   'retro-pa-player.c',
   'retro-pixdata.c',
   'retro-pixel-format.c',
@@ -59,6 +58,7 @@ retro_gtk_headers = [
   'retro-memory-type.h',
   'retro-module-iterator.h',
   'retro-module-query.h',
+  'retro-option.h',
   'retro-pixdata.h',
   'retro-rumble-effect.h',
   'retro-video-filter.h',
diff --git a/retro-gtk/retro-core-private.h b/retro-gtk/retro-core-private.h
index 5a1a73b..c878125 100644
--- a/retro-gtk/retro-core-private.h
+++ b/retro-gtk/retro-core-private.h
@@ -12,7 +12,7 @@
 #include "retro-input.h"
 #include "retro-input-descriptor.h"
 #include "retro-module.h"
-#include "retro-options.h"
+#include "retro-option-private.h"
 #include "retro-pixel-format.h"
 #include "retro-rotation.h"
 
@@ -51,7 +51,8 @@ struct _RetroCore
   GtkWidget *keyboard_widget;
   gulong key_press_event_id;
   gulong key_release_event_id;
-  RetroOptions *options;
+  GHashTable *options;
+  gboolean variable_updated;
 };
 
 void retro_core_push_cb_data (RetroCore *self);
@@ -74,6 +75,9 @@ void retro_core_set_controller_port_device (RetroCore           *self,
 void retro_core_set_controller_descriptors (RetroCore            *self,
                                             RetroInputDescriptor *input_descriptors,
                                             gsize                 length);
+void retro_core_insert_variable (RetroCore           *self,
+                                 const RetroVariable *variable);
+gboolean retro_core_get_variable_update (RetroCore *self);
 
 G_END_DECLS
 
diff --git a/retro-gtk/retro-core.c b/retro-gtk/retro-core.c
index 84bcaad..a03aff4 100644
--- a/retro-gtk/retro-core.c
+++ b/retro-gtk/retro-core.c
@@ -151,7 +151,7 @@ retro_core_finalize (GObject *object)
     if (self->default_controllers[i] != NULL)
       g_object_unref (self->default_controllers[i]);
   g_hash_table_unref (self->controllers);
-  g_object_unref (self->options);
+  g_hash_table_unref (self->options);
 
   g_free (self->filename);
   g_free (self->system_directory);
@@ -495,6 +495,8 @@ retro_core_class_init (RetroCoreClass *klass)
 static void
 retro_core_init (RetroCore *self)
 {
+  self->options = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                         g_free, g_object_unref);
 }
 
 static void
@@ -990,6 +992,60 @@ on_key_event (GtkWidget   *widget,
   return retro_core_key_event (RETRO_CORE (self), event);
 }
 
+static void
+on_option_value_changed (RetroOption *option,
+                         RetroCore   *self)
+{
+  g_return_if_fail (RETRO_IS_CORE (self));
+
+  self->variable_updated = TRUE;
+}
+
+void
+retro_core_insert_variable (RetroCore           *self,
+                            const RetroVariable *variable)
+{
+  RetroOption *option;
+  const gchar *key;
+  GError *tmp_error = NULL;
+
+  g_return_if_fail (RETRO_IS_CORE (self));
+  g_return_if_fail (variable != NULL);
+
+  option = retro_option_new (variable, &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_debug ("%s", tmp_error->message);
+    g_clear_error (&tmp_error);
+
+    return;
+  }
+
+  key = retro_option_get_key (option);
+
+  g_hash_table_insert (self->options, g_strdup (key), option);
+
+  g_signal_connect_object (option,
+                           "value-changed",
+                           G_CALLBACK (on_option_value_changed),
+                           self,
+                           0);
+
+  self->variable_updated = TRUE;
+}
+
+gboolean
+retro_core_get_variable_update (RetroCore *self)
+{
+  g_return_val_if_fail (RETRO_IS_CORE (self), FALSE);
+
+  if (!self->variable_updated)
+    return FALSE;
+
+  self->variable_updated = FALSE;
+
+  return TRUE;
+}
+
 /* Public */
 
 /**
@@ -1876,6 +1932,27 @@ retro_core_iterate_controllers (RetroCore *self)
   return retro_controller_iterator_new (self->controllers);
 }
 
+
+gboolean
+retro_core_has_option (RetroCore   *self,
+                       const gchar *key)
+{
+  g_return_val_if_fail (RETRO_IS_CORE (self), FALSE);
+  g_return_val_if_fail (key != NULL, FALSE);
+
+  return g_hash_table_contains (self->options, key);
+}
+
+RetroOption *
+retro_core_get_option (RetroCore    *self,
+                       const gchar  *key)
+{
+  g_return_val_if_fail (RETRO_IS_CORE (self), NULL);
+  g_return_val_if_fail (key != NULL, NULL);
+
+  return RETRO_OPTION (g_hash_table_lookup (self->options, key));
+}
+
 /**
  * retro_core_new:
  * @filename: the filename of a Libretro core
@@ -1911,7 +1988,6 @@ retro_core_new (const gchar *filename)
   retro_core_set_callbacks (self);
   self->controllers = g_hash_table_new_full (g_int_hash, g_int_equal,
                                              g_free, g_object_unref);
-  self->options = retro_options_new ();
 
   return self;
 }
diff --git a/retro-gtk/retro-core.h b/retro-gtk/retro-core.h
index e08bdb1..636dbdd 100644
--- a/retro-gtk/retro-core.h
+++ b/retro-gtk/retro-core.h
@@ -10,6 +10,7 @@
 #include <gtk/gtk.h>
 #include "retro-controller-iterator.h"
 #include "retro-memory-type.h"
+#include "retro-option.h"
 
 G_BEGIN_DECLS
 
@@ -64,6 +65,11 @@ void retro_core_set_controller (RetroCore       *self,
 void retro_core_set_keyboard (RetroCore *self,
                               GtkWidget *widget);
 RetroControllerIterator *retro_core_iterate_controllers (RetroCore *self);
+gboolean retro_core_has_option (RetroCore   *self,
+                                const gchar *key);
+RetroOption *retro_core_get_option (RetroCore   *self,
+                                    const gchar *key);
+// RetroOptionIterator *retro_core_iterate_options (RetroCore *self);
 
 G_END_DECLS
 
diff --git a/retro-gtk/retro-environment.c b/retro-gtk/retro-environment.c
index e65720d..6a054fa 100644
--- a/retro-gtk/retro-environment.c
+++ b/retro-gtk/retro-environment.c
@@ -187,13 +187,15 @@ static gboolean
 get_variable (RetroCore     *self,
               RetroVariable *variable)
 {
+  RetroOption *option;
   const gchar *value;
 
-  if (!retro_options_contains (self->options, variable->key))
+  if (!retro_core_has_option (self, variable->key))
     return FALSE;
 
-  value = retro_options_get_option_value (self->options, variable->key);
-  variable->value = g_strdup (value); // FIXME Is that a memory leak?
+  option = retro_core_get_option (self, variable->key);
+  value = retro_option_get_value (option);
+  variable->value = value;
 
   return TRUE;
 }
@@ -203,7 +205,7 @@ static gboolean
 get_variable_update (RetroCore *self,
                      bool      *update)
 {
-  *update = retro_options_get_variable_update (self->options);
+  *update = retro_core_get_variable_update (self);
 
   return TRUE;
 }
@@ -291,7 +293,7 @@ set_variables (RetroCore     *self,
   int i;
 
   for (i = 0 ; variable_array[i].key && variable_array[i].value ; i++)
-    retro_options_insert_variable (self->options, &variable_array[i]);
+    retro_core_insert_variable (self, &variable_array[i]);
 
   return TRUE;
 }
diff --git a/retro-gtk/retro-gtk.h b/retro-gtk/retro-gtk.h
index 78ee742..7304459 100644
--- a/retro-gtk/retro-gtk.h
+++ b/retro-gtk/retro-gtk.h
@@ -22,6 +22,7 @@
 #include "retro-memory-type.h"
 #include "retro-module-iterator.h"
 #include "retro-module-query.h"
+#include "retro-option.h"
 #include "retro-pixdata.h"
 #include "retro-video-filter.h"
 
diff --git a/retro-gtk/retro-option-private.h b/retro-gtk/retro-option-private.h
new file mode 100644
index 0000000..33f631e
--- /dev/null
+++ b/retro-gtk/retro-option-private.h
@@ -0,0 +1,20 @@
+// This file is part of retro-gtk. License: GPL-3.0+.
+
+#ifndef RETRO_OPTION_PRIVATE_H
+#define RETRO_OPTION_PRIVATE_H
+
+#if !defined(__RETRO_GTK_INSIDE__) && !defined(RETRO_GTK_COMPILATION)
+# error "Only <retro-gtk.h> can be included directly."
+#endif
+
+#include "retro-option.h"
+#include "retro-variable.h"
+
+G_BEGIN_DECLS
+
+RetroOption *retro_option_new (const RetroVariable  *variable,
+                               GError              **error);
+
+G_END_DECLS
+
+#endif /* RETRO_OPTION_PRIVATE_H */
diff --git a/retro-gtk/retro-option.c b/retro-gtk/retro-option.c
index 8c8a7ce..f40c12b 100644
--- a/retro-gtk/retro-option.c
+++ b/retro-gtk/retro-option.c
@@ -1,6 +1,6 @@
 // This file is part of retro-gtk. License: GPL-3.0+.
 
-#include "retro-option.h"
+#include "retro-option-private.h"
 
 struct _RetroOption
 {
diff --git a/retro-gtk/retro-option.h b/retro-gtk/retro-option.h
index e1f1224..54500b5 100644
--- a/retro-gtk/retro-option.h
+++ b/retro-gtk/retro-option.h
@@ -8,7 +8,6 @@
 #endif
 
 #include <glib-object.h>
-#include "retro-variable.h"
 
 G_BEGIN_DECLS
 
@@ -16,8 +15,6 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (RetroOption, retro_option, RETRO, OPTION, GObject)
 
-RetroOption *retro_option_new (const RetroVariable  *variable,
-                               GError              **error);
 const gchar *retro_option_get_key (RetroOption *self);
 const gchar *retro_option_get_description (RetroOption *self);
 const gchar **retro_option_get_values (RetroOption *self);


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