[retro-gtk/wip/aplazas/test: 1/4] core: Add disk control callback support



commit 381dfc55fbb38b4e7f6e0d12bf052c1605737d2a
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Thu Aug 31 08:46:38 2017 +0200

    core: Add disk control callback support
    
    Replace disk handling with the DiskControl Vala class by directly
    handling it in Core in C.
    
    This makes the code simpler and helps porting the library to C.

 retro-gtk/Makefile.am                   |    1 -
 retro-gtk/core.vala                     |    7 -
 retro-gtk/disk-controller.vala          |  256 -------------------------------
 retro-gtk/retro-core.c                  |  181 ++++++++++++++++++++--
 retro-gtk/retro-core.h                  |    2 +
 retro-gtk/retro-disk-control-callback.h |   33 ++++
 retro-gtk/retro-environment.c           |    3 +-
 7 files changed, 201 insertions(+), 282 deletions(-)
---
diff --git a/retro-gtk/Makefile.am b/retro-gtk/Makefile.am
index 49098e2..1265bae 100644
--- a/retro-gtk/Makefile.am
+++ b/retro-gtk/Makefile.am
@@ -44,7 +44,6 @@ libretro_gtk_la_SOURCES = \
        av-info.vala \
        core.vala \
        core-error.vala \
-       disk-controller.vala \
        game-info.vala \
        memory.vala \
        retro.vala \
diff --git a/retro-gtk/core.vala b/retro-gtk/core.vala
index e969d5d..ab07277 100644
--- a/retro-gtk/core.vala
+++ b/retro-gtk/core.vala
@@ -85,13 +85,6 @@ public class Core : Object {
                get { return _frames_per_second; }
        }
 
-       /**
-        * The disk controlling interface.
-        *
-        * The Core can set it to let the frontend insert and eject disks images.
-        */
-       internal DiskControl disk_control_interface { set; get; }
-
        private weak Input _input_interface;
        private ulong input_controller_connected_id;
        private ulong input_controller_disconnected_id;
diff --git a/retro-gtk/retro-core.c b/retro-gtk/retro-core.c
index 46ca8d2..243b575 100644
--- a/retro-gtk/retro-core.c
+++ b/retro-gtk/retro-core.c
@@ -244,11 +244,164 @@ retro_core_on_key_event (RetroCore   *self,
   return FALSE;
 }
 
+static gboolean
+retro_core_set_disk_ejected (RetroCore  *self,
+                             gboolean    ejected,
+                             GError    **error)
+{
+  RetroCoreEnvironmentInternal *internal;
+  RetroDiskControlCallbackSetEjectState set_eject_state;
+  gboolean result;
+
+  g_return_val_if_fail (self != NULL, FALSE);
+
+  internal = RETRO_CORE_ENVIRONMENT_INTERNAL (self);
+
+  set_eject_state = internal->disk_control_callback->set_eject_state;
+
+  if (set_eject_state == NULL) {
+    g_set_error_literal (error,
+                         RETRO_CB_ERROR,
+                         RETRO_CB_ERROR_NO_CALLBACK,
+                         "DiskControl has no callback for this operation.");
+
+    return FALSE;
+  }
+
+  retro_core_push_cb_data (self);
+  result = set_eject_state (ejected);
+  retro_core_pop_cb_data ();
+
+  return result;
+}
+
+static gboolean
+retro_core_set_disk_image_index (RetroCore  *self,
+                                 guint       index,
+                                 GError    **error)
+{
+  RetroCoreEnvironmentInternal *internal;
+  RetroDiskControlCallbackSetImageIndex set_image_index;
+  gboolean result;
+
+  g_return_val_if_fail (self != NULL, FALSE);
+
+  internal = RETRO_CORE_ENVIRONMENT_INTERNAL (self);
+
+  set_image_index = internal->disk_control_callback->set_image_index;
+
+  if (set_image_index == NULL) {
+    g_set_error_literal (error,
+                         RETRO_CB_ERROR,
+                         RETRO_CB_ERROR_NO_CALLBACK,
+                         "DiskControl has no callback for this operation.");
+
+    return FALSE;
+  }
+
+  retro_core_push_cb_data (self);
+  result = set_image_index (index);
+  retro_core_pop_cb_data ();
+
+  return result;
+}
+
+static guint
+retro_core_get_disk_images_number (RetroCore  *self,
+                                   GError    **error)
+{
+  RetroCoreEnvironmentInternal *internal;
+  RetroDiskControlCallbackGetNumImages get_num_images;
+  guint result;
+
+  g_return_val_if_fail (self != NULL, FALSE);
+
+  internal = RETRO_CORE_ENVIRONMENT_INTERNAL (self);
+
+  get_num_images = internal->disk_control_callback->get_num_images;
+
+  if (get_num_images == NULL) {
+    g_set_error_literal (error,
+                         RETRO_CB_ERROR,
+                         RETRO_CB_ERROR_NO_CALLBACK,
+                         "DiskControl has no callback for this operation.");
+
+    return FALSE;
+  }
+
+  retro_core_push_cb_data (self);
+  result = get_num_images ();
+  retro_core_pop_cb_data ();
+
+  return result;
+}
+
+static gboolean
+retro_core_replace_disk_image_index (RetroCore     *self,
+                                     guint          index,
+                                     RetroGameInfo *info,
+                                     GError        **error)
+{
+  RetroCoreEnvironmentInternal *internal;
+  RetroDiskControlCallbackReplaceImageIndex replace_image_index;
+  gboolean result;
+
+  g_return_val_if_fail (self != NULL, FALSE);
+
+  internal = RETRO_CORE_ENVIRONMENT_INTERNAL (self);
+
+  replace_image_index = internal->disk_control_callback->replace_image_index;
+
+  if (replace_image_index == NULL) {
+    g_set_error_literal (error,
+                         RETRO_CB_ERROR,
+                         RETRO_CB_ERROR_NO_CALLBACK,
+                         "DiskControl has no callback for this operation.");
+
+    return FALSE;
+  }
+
+  retro_core_push_cb_data (self);
+  result = replace_image_index (index, info);
+  retro_core_pop_cb_data ();
+
+  return result;
+}
+
+static gboolean
+retro_core_add_disk_image_index (RetroCore  *self,
+                                 GError    **error)
+{
+  RetroCoreEnvironmentInternal *internal;
+  RetroDiskControlCallbackAddImageIndex add_image_index;
+  gboolean result;
+
+  g_return_val_if_fail (self != NULL, FALSE);
+
+  internal = RETRO_CORE_ENVIRONMENT_INTERNAL (self);
+
+  add_image_index = internal->disk_control_callback->add_image_index;
+
+  if (add_image_index == NULL) {
+    g_set_error_literal (error,
+                         RETRO_CB_ERROR,
+                         RETRO_CB_ERROR_NO_CALLBACK,
+                         "DiskControl has no callback for this operation.");
+
+    return FALSE;
+  }
+
+  retro_core_push_cb_data (self);
+  result = add_image_index ();
+  retro_core_pop_cb_data ();
+
+  return result;
+}
+
 static void
 retro_core_load_discs (RetroCore  *self,
                        GError    **error)
 {
-  RetroDiskControl *disk_control;
   RetroCoreEnvironmentInternal *internal;
   guint length;
   gboolean fullpath;
@@ -261,20 +414,19 @@ retro_core_load_discs (RetroCore  *self,
 
   g_return_if_fail (self != NULL);
 
-  disk_control = retro_core_get_disk_control_interface (self);
+  internal = RETRO_CORE_ENVIRONMENT_INTERNAL (self);
 
-  retro_disk_control_set_eject_state (disk_control, TRUE, &tmp_error);
+  retro_core_set_disk_ejected (self, TRUE, &tmp_error);
   if (G_UNLIKELY (tmp_error != NULL)) {
     g_propagate_error (error, tmp_error);
 
     return;
   }
 
-  internal = RETRO_CORE_ENVIRONMENT_INTERNAL (self);
   length = g_strv_length (internal->media_uris);
-  while (retro_disk_control_get_num_images (disk_control, &tmp_error) < length &&
+  while (retro_core_get_disk_images_number (self, &tmp_error) < length &&
          (tmp_error != NULL)) {
-    retro_disk_control_add_image_index (disk_control, &tmp_error);
+    retro_core_add_disk_image_index (self, &tmp_error);
     if (G_UNLIKELY (tmp_error != NULL)) {
       g_propagate_error (error, tmp_error);
 
@@ -310,7 +462,7 @@ retro_core_load_discs (RetroCore  *self,
       }
     }
 
-    retro_disk_control_replace_image_index (disk_control, index, &game_info, &tmp_error);
+    retro_core_replace_disk_image_index (self, index, &game_info, &tmp_error);
     if (G_UNLIKELY (tmp_error != NULL)) {
       g_propagate_error (error, tmp_error);
 
@@ -326,7 +478,7 @@ retro_core_load_discs (RetroCore  *self,
     g_object_unref (file);
   }
 
-  retro_disk_control_set_eject_state (disk_control, FALSE, &tmp_error);
+  retro_core_set_disk_ejected (self, FALSE, &tmp_error);
   if (G_UNLIKELY (tmp_error != NULL)) {
     g_propagate_error (error, tmp_error);
 
@@ -447,7 +599,7 @@ retro_core_load_medias (RetroCore* self,
 
     return;
   }
-  if (retro_core_get_disk_control_interface (self) != NULL) {
+  if (internal->disk_control_callback != NULL) {
     retro_core_load_discs (self, &tmp_error);
     if (G_UNLIKELY (tmp_error != NULL)) {
       g_propagate_error (error, tmp_error);
@@ -606,7 +758,6 @@ retro_core_set_current_media (RetroCore  *self,
                               GError    **error)
 {
   RetroCoreEnvironmentInternal *internal;
-  RetroDiskControl *disk_control;
   guint length;
   GError *tmp_error = NULL;
 
@@ -617,26 +768,24 @@ retro_core_set_current_media (RetroCore  *self,
 
   g_return_if_fail (media_index < length);
 
-  disk_control = retro_core_get_disk_control_interface (self);
-
-  if (disk_control == NULL)
+  if (internal->disk_control_callback == NULL)
     return;
 
-  retro_disk_control_set_eject_state (disk_control, TRUE, &tmp_error);
+  retro_core_set_disk_ejected (self, TRUE, &tmp_error);
   if (tmp_error != NULL) {
     g_propagate_error (error, tmp_error);
 
     return;
   }
 
-  retro_disk_control_set_image_index (disk_control, media_index, &tmp_error);
+  retro_core_set_disk_image_index (self, media_index, &tmp_error);
   if (tmp_error != NULL) {
     g_propagate_error (error, tmp_error);
 
     return;
   }
 
-  retro_disk_control_set_eject_state (disk_control, FALSE, &tmp_error);
+  retro_core_set_disk_ejected (self, FALSE, &tmp_error);
   if (tmp_error != NULL) {
     g_propagate_error (error, tmp_error);
 
diff --git a/retro-gtk/retro-core.h b/retro-gtk/retro-core.h
index f4bd94e..d651c05 100644
--- a/retro-gtk/retro-core.h
+++ b/retro-gtk/retro-core.h
@@ -3,6 +3,7 @@
 #ifndef RETRO_CORE_H
 #define RETRO_CORE_H
 
+#include "retro-disk-control-callback.h"
 #include "retro-gtk-internal.h"
 #include "retro-module.h"
 #include "retro-rotation.h"
@@ -19,6 +20,7 @@ typedef struct _RetroCoreEnvironmentInternal RetroCoreEnvironmentInternal;
 
 struct _RetroCoreEnvironmentInternal {
   RetroModule *module;
+  RetroDiskControlCallback *disk_control_callback;
   gchar **media_uris;
   RetroSystemInfo *system_info;
   gfloat aspect_ratio;
diff --git a/retro-gtk/retro-disk-control-callback.h b/retro-gtk/retro-disk-control-callback.h
new file mode 100644
index 0000000..fc29cc5
--- /dev/null
+++ b/retro-gtk/retro-disk-control-callback.h
@@ -0,0 +1,33 @@
+// This file is part of retro-gtk. License: GPL-3.0+.
+
+#ifndef RETRO_DISK_CONTROL_CALLBACK_H
+#define RETRO_DISK_CONTROL_CALLBACK_H
+
+#include "retro-gtk-internal.h"
+
+G_BEGIN_DECLS
+
+typedef struct _RetroDiskControlCallback RetroDiskControlCallback;
+
+typedef gboolean (*RetroDiskControlCallbackSetEjectState) (gboolean ejected);
+typedef gboolean (*RetroDiskControlCallbackGetEjectState) ();
+typedef guint (*RetroDiskControlCallbackGetImageIndex) ();
+typedef gboolean (*RetroDiskControlCallbackSetImageIndex) (guint index);
+typedef guint (*RetroDiskControlCallbackGetNumImages) ();
+typedef gboolean (*RetroDiskControlCallbackReplaceImageIndex) (guint index, RetroGameInfo *info);
+typedef gboolean (*RetroDiskControlCallbackAddImageIndex) ();
+
+struct _RetroDiskControlCallback
+{
+  RetroDiskControlCallbackSetEjectState set_eject_state;
+  RetroDiskControlCallbackGetEjectState get_eject_state;
+  RetroDiskControlCallbackGetImageIndex get_image_index;
+  RetroDiskControlCallbackSetImageIndex set_image_index;
+  RetroDiskControlCallbackGetNumImages get_num_images;
+  RetroDiskControlCallbackReplaceImageIndex replace_image_index;
+  RetroDiskControlCallbackAddImageIndex add_image_index;
+};
+
+G_END_DECLS
+
+#endif /* RETRO_DISK_CONTROL_CALLBACK_H */
diff --git a/retro-gtk/retro-environment.c b/retro-gtk/retro-environment.c
index 301013b..d09ebb9 100644
--- a/retro-gtk/retro-environment.c
+++ b/retro-gtk/retro-environment.c
@@ -218,8 +218,7 @@ static gboolean
 set_disk_control_interface (RetroCore                *self,
                             RetroDiskControlCallback *callback)
 {
-  // TODO Split this in two lines.
-  retro_core_set_disk_control_interface (self, RETRO_DISK_CONTROL (retro_disk_control_new (self, callback)));
+  RETRO_CORE_ENVIRONMENT_INTERNAL (self)->disk_control_callback = callback;
 
   return TRUE;
 }


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