[retro-gtk/wip/aplazas/disk] core: Move usage of DiskControl to C



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

    core: Move usage of DiskControl to C
    
    This will help porting DiskControl to C.

 retro-gtk/core.vala            |    7 --
 retro-gtk/disk-controller.vala |  195 +---------------------------------------
 retro-gtk/retro-core.c         |  181 +++++++++++++++++++++++++++++++++----
 retro-gtk/retro-core.h         |    1 +
 retro-gtk/retro-environment.c  |    3 +-
 5 files changed, 168 insertions(+), 219 deletions(-)
---
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/disk-controller.vala b/retro-gtk/disk-controller.vala
index ce407fd..f659314 100644
--- a/retro-gtk/disk-controller.vala
+++ b/retro-gtk/disk-controller.vala
@@ -20,205 +20,12 @@ namespace Retro {
 private class DiskControl: Object {
        private weak Core core;
 
-       private DiskControlCallback callback_struct;
+       public DiskControlCallback callback_struct;
 
        internal DiskControl (Core core, DiskControlCallback callback_struct) {
                this.core = core;
                this.callback_struct = callback_struct;
        }
-
-       /**
-        * Sets the current eject state.
-        *
-        * When set to true, ejects the virtual disk tray.
-        * When set to false, closes the virtual disk tray.
-        *
-        * When ejected, the disk image index can be set.
-        *
-        * The default state is "closed".
-        *
-        * @param ejected the desired eject state
-        * @return //true// on successfully changed eject state, //false// otherwise
-        * @throws Error the core or its callback couldn't be found
-        */
-       public bool set_eject_state (bool ejected) throws Error {
-               if (core == null)
-                       throw new CbError.NO_CORE ("DiskControl has no core");
-
-               if (callback_struct.set_eject_state == null)
-                       throw new CbError.NO_CALLBACK ("DiskControl has no callback for this operation");
-
-               core.push_cb_data ();
-               var result = callback_struct.set_eject_state (ejected);
-               Core.pop_cb_data ();
-               return result;
-       }
-
-       /**
-        * Gets the current eject state.
-        *
-        * See {@link set_eject_state} for more informations.
-        *
-        * @return the current eject state
-        * @throws Error the core or its callback couldn't be found
-        */
-       public bool get_eject_state () throws Error {
-               if (core == null)
-                       throw new CbError.NO_CORE ("DiskControl has no core");
-
-               if (callback_struct.get_eject_state == null)
-                       throw new CbError.NO_CALLBACK ("DiskControl has no callback for this operation");
-
-               core.push_cb_data ();
-               var result = callback_struct.get_eject_state ();
-               Core.pop_cb_data ();
-               return result;
-       }
-
-       /**
-        * Sets the current disk index.
-        *
-        * Can only be set when the disk drive is ejected.
-        *
-        * If the value is >= to the total number of images,
-        * no disk is currently inserted.
-        *
-        * @param image_index the desired image index
-        * @return //true// on successfully changed image index, //false// otherwise
-        * @throws Error the core or its callback couldn't be found
-        */
-       public bool set_image_index (uint image_index) throws Error {
-               if (core == null)
-                       throw new CbError.NO_CORE ("DiskControl has no core");
-
-               if (callback_struct.set_image_index == null)
-                       throw new CbError.NO_CALLBACK ("DiskControl has no callback for this operation");
-
-               core.push_cb_data ();
-               var result = callback_struct.set_image_index (image_index);
-               Core.pop_cb_data ();
-               return result;
-       }
-
-       /**
-        * Gets the current disk index.
-        *
-        * @return the current image index
-        * @throws Error the core or its callback couldn't be found
-        */
-       public uint get_image_index () throws Error {
-               if (core == null)
-                       throw new CbError.NO_CORE ("DiskControl has no core");
-
-               if (callback_struct.get_image_index == null)
-                       throw new CbError.NO_CALLBACK ("DiskControl has no callback for this operation");
-
-               core.push_cb_data ();
-               var result = callback_struct.get_image_index ();
-               Core.pop_cb_data ();
-               return result;
-       }
-
-       /**
-        * Gets the total number of images which are available to use.
-        *
-        * @return total number of images available to use
-        * @throws Error the core or its callback couldn't be found
-        */
-       public uint get_num_images () throws Error {
-               if (core == null)
-                       throw new CbError.NO_CORE ("DiskControl has no core");
-
-               if (callback_struct.get_num_images == null)
-                       throw new CbError.NO_CALLBACK ("DiskControl has no callback for this operation");
-
-               core.push_cb_data ();
-               var result = callback_struct.get_num_images ();
-               Core.pop_cb_data ();
-               return result;
-       }
-
-       /**
-        * Replaces the disk image associated with index.
-        *
-        * Virtual disk tray must be ejected when calling this.
-        *
-        * Arguments to pass in info have same requirements as
-        * {@link Core.load_game}.
-        *
-        * @param index index of the disk image to replace
-        * @param info information on the disk image to use
-        * @return //true// on successfully replaced image, //false// otherwise
-        * @throws Error the core or its callback couldn't be found
-        */
-       public bool replace_image_index (uint index, GameInfo info) throws Error {
-               if (core == null)
-                       throw new CbError.NO_CORE ("DiskControl has no core");
-
-               if (callback_struct.replace_image_index == null)
-                       throw new CbError.NO_CALLBACK ("DiskControl has no callback for this operation");
-
-               core.push_cb_data ();
-               var result = callback_struct.replace_image_index (index, info);
-               Core.pop_cb_data ();
-               return result;
-       }
-
-       /**
-        * Removes the disk image associated with index.
-        *
-        * Virtual disk tray must be ejected when calling this.
-        *
-        * It will remove the disk image from the internal list.
-        * As a result, the current image index can change.
-        *
-        * E.g. remove_image_index (1), and previous
-        * image index was 4 before.
-        * Index 1 will be removed, and the new index is 3.
-        *
-        * @param index index of the disk image to remove
-        * @return //true// on successfully removed index, //false// otherwise
-        * @throws Error the core or its callback couldn't be found
-        */
-       public bool remove_image_index (uint index) throws Error {
-               if (core == null)
-                       throw new CbError.NO_CORE ("DiskControl has no core");
-
-               if (callback_struct.replace_image_index == null)
-                       throw new CbError.NO_CALLBACK ("DiskControl has no callback for this operation");
-
-               var i = get_image_index ();
-               core.push_cb_data ();
-               var result = callback_struct.replace_image_index (index, null);
-               Core.pop_cb_data ();
-               // Notify a change on the "image-index" property
-               if (i != get_image_index ()) notify_property ("image-index");
-               return result;
-       }
-
-       /**
-        * Adds a new valid index to the internal disk lit.
-        *
-        * This will increment subsequent return values from {@link get_num_images}
-        * by 1.
-        * This image index cannot be used until a disk image has been set with
-        * {@link replace_image_index}.
-        *
-        * @return //true// on successfully added index, //false// otherwise
-        * @throws Error the core or its callback couldn't be found
-        */
-       public bool add_image_index () throws Error {
-               if (core == null)
-                       throw new CbError.NO_CORE ("DiskControl has no core");
-
-               if (callback_struct.add_image_index == null)
-                       throw new CbError.NO_CALLBACK ("DiskControl has no callback for this operation");
-
-               core.push_cb_data ();
-               var result = callback_struct.add_image_index ();
-               Core.pop_cb_data ();
-               return result;
-       }
 }
 
 internal struct DiskControlCallback {
diff --git a/retro-gtk/retro-core.c b/retro-gtk/retro-core.c
index 46ca8d2..39837ab 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_interface->callback_struct.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_interface->callback_struct.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_interface->callback_struct.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_interface->callback_struct.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_interface->callback_struct.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_interface != 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_interface == 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..31e44ce 100644
--- a/retro-gtk/retro-core.h
+++ b/retro-gtk/retro-core.h
@@ -19,6 +19,7 @@ typedef struct _RetroCoreEnvironmentInternal RetroCoreEnvironmentInternal;
 
 struct _RetroCoreEnvironmentInternal {
   RetroModule *module;
+  RetroDiskControl *disk_control_interface;
   gchar **media_uris;
   RetroSystemInfo *system_info;
   gfloat aspect_ratio;
diff --git a/retro-gtk/retro-environment.c b/retro-gtk/retro-environment.c
index 301013b..d731838 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_interface = retro_disk_control_new (self, callback);
 
   return TRUE;
 }


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