[gnome-games/wip/exalm/retro-gtk-1.0: 3/4] retro: Adapt to API changes in retro-gtk



commit 4f2720d2b46805aa077719d3eae781c2067fd271
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Thu Jan 23 02:01:55 2020 +0500

    retro: Adapt to API changes in retro-gtk
    
    Stop using options-set signal, use retro_core_override_option_default()
    for applying options instead.
    
    Stop using RetroMainLoop, use new RetroCore methods instead. Use
    core.iterate() instead of core.run().
    
    Stop returning value in RetroGamepad.set_rumble_state() and add
    get_supports_rumble() instead. Remove poll() and emit state-changed
    signal when required.
    
    Use file paths for state and memory instead of doing IO manually.
    
    Stop returning value in RetroCore.shutdown signal.

 plugins/nintendo-ds/src/nintendo-ds-runner.vala | 10 -----
 src/core/savestate.vala                         | 23 +---------
 src/retro/retro-gamepad.vala                    | 25 ++++++-----
 src/retro/retro-options.vala                    |  3 +-
 src/retro/retro-runner.vala                     | 60 ++++++++++---------------
 5 files changed, 40 insertions(+), 81 deletions(-)
---
diff --git a/plugins/nintendo-ds/src/nintendo-ds-runner.vala b/plugins/nintendo-ds/src/nintendo-ds-runner.vala
index 037424a9..84ee4eed 100644
--- a/plugins/nintendo-ds/src/nintendo-ds-runner.vala
+++ b/plugins/nintendo-ds/src/nintendo-ds-runner.vala
@@ -33,22 +33,12 @@ private class Games.NintendoDsRunner : RetroRunner {
                layouts[Gdk.Key.@4] = NintendoDsLayout.QUICK_SWITCH;
        }
 
-       construct {
-               game_init.connect (on_init);
-       }
-
        private bool core_supports_layouts () {
                var core = get_core ();
 
                return core != null && core.has_option (SCREENS_LAYOUT_OPTION);
        }
 
-       private void on_init () {
-               var core = get_core ();
-
-               core.options_set.connect (update_screen_layout);
-       }
-
        private void update_screen_layout () {
                if (!core_supports_layouts ())
                        return;
diff --git a/src/core/savestate.vala b/src/core/savestate.vala
index e7ef4824..0148977d 100644
--- a/src/core/savestate.vala
+++ b/src/core/savestate.vala
@@ -43,33 +43,14 @@ public class Games.Savestate : Object {
                }
        }
 
-       public void set_snapshot_data (Bytes snapshot_data) throws Error {
-               var buffer = snapshot_data.get_data ();
-               var snapshot_path = Path.build_filename (path, "snapshot");
-
-               FileUtils.set_data (snapshot_path, buffer);
-       }
-
-       public Bytes get_snapshot_data () throws Error {
-               var snapshot_path = Path.build_filename (path, "snapshot");
-
-               uint8[] data = null;
-               FileUtils.get_data (snapshot_path, out data);
-               var bytes = new Bytes.take (data);
-
-               return bytes;
+       public string get_snapshot_path () {
+               return Path.build_filename (path, "snapshot");
        }
 
        public string get_save_ram_path () {
                return Path.build_filename (path, "save");
        }
 
-       public void set_save_ram_data (uint8[] save_ram_data) throws Error {
-               var save_ram_path = Path.build_filename (path, "save");
-
-               FileUtils.set_data (save_ram_path, save_ram_data);
-       }
-
        public string get_screenshot_path () {
                return Path.build_filename (path, "screenshot");
        }
diff --git a/src/retro/retro-gamepad.vala b/src/retro/retro-gamepad.vala
index c4206c9e..131bf7ab 100644
--- a/src/retro/retro-gamepad.vala
+++ b/src/retro/retro-gamepad.vala
@@ -20,8 +20,6 @@ private class Games.RetroGamepad : Object, Retro.Controller {
                device.absolute_axis_event.connect (on_absolute_axis_event);
        }
 
-       public void poll () {}
-
        public int16 get_input_state (Retro.Input input) {
                switch (input.get_controller_type ()) {
                case Retro.ControllerType.JOYPAD:
@@ -50,17 +48,16 @@ private class Games.RetroGamepad : Object, Retro.Controller {
                return (1 << Retro.ControllerType.JOYPAD) | (1 << Retro.ControllerType.ANALOG);
        }
 
-       public bool set_rumble_state (Retro.RumbleEffect effect, uint16 strength) {
-               rumble_effect[effect] = strength;
+       public bool get_supports_rumble () {
+               return device.has_rumble ();
+       }
 
-               if (!device.has_rumble ())
-                       return false;
+       public void set_rumble_state (Retro.RumbleEffect effect, uint16 strength) {
+               rumble_effect[effect] = strength;
 
                device.rumble (rumble_effect[Retro.RumbleEffect.STRONG],
                               rumble_effect[Retro.RumbleEffect.WEAK],
                               uint16.MAX);
-
-               return true;
        }
 
        private bool get_button_pressed (Retro.JoypadId button) {
@@ -97,22 +94,28 @@ private class Games.RetroGamepad : Object, Retro.Controller {
        private void on_button_press_event (Manette.Event event) {
                uint16 button;
 
-               if (event.get_button (out button))
+               if (event.get_button (out button)) {
                        buttons[button] = true;
+                       emit_state_changed ();
+               }
        }
 
        private void on_button_release_event (Manette.Event event) {
                uint16 button;
 
-               if (event.get_button (out button))
+               if (event.get_button (out button)) {
                        buttons[button] = false;
+                       emit_state_changed ();
+               }
        }
 
        private void on_absolute_axis_event (Manette.Event event) {
                uint16 axis;
                double value;
 
-               if (event.get_absolute (out axis, out value))
+               if (event.get_absolute (out axis, out value)) {
                        axes[axis] = (int16) (value * int16.MAX);
+                       emit_state_changed ();
+               }
        }
 }
diff --git a/src/retro/retro-options.vala b/src/retro/retro-options.vala
index 22c4e869..ea79f822 100644
--- a/src/retro/retro-options.vala
+++ b/src/retro/retro-options.vala
@@ -15,9 +15,8 @@ private class Games.RetroOptions : Object {
 
                foreach (var key in options_keys) {
                        var val = keyfile.get_string (OPTIONS_GROUP, key);
-                       var option = core.get_option (key);
 
-                       option.set_value (val);
+                       core.override_option_default (key, val);
                }
        }
 }
diff --git a/src/retro/retro-runner.vala b/src/retro/retro-runner.vala
index 6629c532..d305b62e 100644
--- a/src/retro/retro-runner.vala
+++ b/src/retro/retro-runner.vala
@@ -30,7 +30,6 @@ public class Games.RetroRunner : Object, Runner {
        private Retro.Core core;
        private Retro.CoreView view;
        private RetroInputManager input_manager;
-       private Retro.MainLoop loop;
        private InputMode _input_mode;
        public InputMode input_mode {
                get { return _input_mode; }
@@ -198,12 +197,12 @@ public class Games.RetroRunner : Object, Runner {
        }
 
        public void load_previewed_savestate () throws Error {
-               loop.stop ();
+               core.stop ();
 
                tmp_live_savestate = previewed_savestate.clone_in_tmp ();
                core.save_directory = tmp_live_savestate.get_save_directory_path ();
                load_save_ram (previewed_savestate.get_save_ram_path ());
-               core.set_state (previewed_savestate.get_snapshot_data ());
+               core.load_state (previewed_savestate.get_snapshot_path ());
 
                if (previewed_savestate.has_media_data ())
                        media_set.selected_media_number = previewed_savestate.get_media_data ();
@@ -240,10 +239,10 @@ public class Games.RetroRunner : Object, Runner {
                        is_ready = true;
                }
 
-               core.run (); // Needed to finish preparing some cores.
+               core.iteration (); // Needed to finish preparing some cores.
                core.reset ();
 
-               loop.start ();
+               core.run ();
 
                running = true;
        }
@@ -255,13 +254,16 @@ public class Games.RetroRunner : Object, Runner {
        }
 
        public void resume () {
+               if (!is_initialized)
+                       return;
+
                if (!is_ready) {
                        critical ("RetroRunner.resume() cannot be called if the game isn't playing");
                        return;
                }
 
                // Unpause an already running game
-               loop.start ();
+               core.run ();
                running = true;
        }
 
@@ -274,9 +276,8 @@ public class Games.RetroRunner : Object, Runner {
                // Keep the internal values of input_mode in sync between RetroRunner and RetroInputManager
                _input_mode = input_manager.input_mode;
 
-               core.shutdown.connect (on_shutdown);
+               core.shutdown.connect (stop);
 
-               loop = new Retro.MainLoop (core);
                running = false;
 
                is_initialized = true;
@@ -295,7 +296,6 @@ public class Games.RetroRunner : Object, Runner {
                view = null;
 
                input_manager = null;
-               loop = null;
 
                _running = false;
                is_initialized = false;
@@ -323,14 +323,12 @@ public class Games.RetroRunner : Object, Runner {
 
                var options_path = get_options_path ();
                if (FileUtils.test (options_path, FileTest.EXISTS))
-                       core.options_set.connect (() => {
-                               try {
-                                       var options = new RetroOptions (options_path);
-                                       options.apply (core);
-                               } catch (Error e) {
-                                       critical (e.message);
-                               }
-                       });
+                       try {
+                               var options = new RetroOptions (options_path);
+                               options.apply (core);
+                       } catch (Error e) {
+                               critical (e.message);
+                       }
 
                game_init ();
 
@@ -364,7 +362,7 @@ public class Games.RetroRunner : Object, Runner {
                        return;
 
                current_state_pixbuf = view.get_pixbuf ();
-               loop.stop ();
+               core.stop ();
 
                //FIXME:
                // In the future here there will be code which updates the currently
@@ -484,7 +482,7 @@ public class Games.RetroRunner : Object, Runner {
                if (media_set.get_size () > 1)
                        tmp_live_savestate.set_media_data (media_set);
 
-               tmp_live_savestate.set_snapshot_data (core.get_state ());
+               core.save_state (tmp_live_savestate.get_snapshot_path ());
                save_screenshot_in_tmp ();
 
                // Populate the metadata file
@@ -542,27 +540,21 @@ public class Games.RetroRunner : Object, Runner {
        }
 
        private void store_save_ram_in_tmp () throws Error {
-               var bytes = core.get_memory (Retro.MemoryType.SAVE_RAM);
-               var save = bytes.get_data ();
-               if (save.length == 0)
+               if (core.get_memory_size (Retro.MemoryType.SAVE_RAM) == 0)
                        return;
 
-               tmp_live_savestate.set_save_ram_data (save);
+               core.save_memory (Retro.MemoryType.SAVE_RAM,
+                                 tmp_live_savestate.get_save_ram_path ());
        }
 
        private void load_save_ram (string save_ram_path) throws Error {
                if (!FileUtils.test (save_ram_path, FileTest.EXISTS))
                        return;
 
-               uint8[] data = null;
-               FileUtils.get_data (save_ram_path, out data);
-
-               var expected_size = core.get_memory_size (Retro.MemoryType.SAVE_RAM);
-               if (data.length != expected_size)
-                       warning ("Unexpected RAM data size: got %lu, expected %lu\n", data.length, 
expected_size);
+               if (core.get_memory_size (Retro.MemoryType.SAVE_RAM) == 0)
+                       return;
 
-               var bytes = new Bytes.take (data);
-               core.set_memory (Retro.MemoryType.SAVE_RAM, bytes);
+               core.load_memory (Retro.MemoryType.SAVE_RAM, save_ram_path);
        }
 
        private void save_screenshot_in_tmp () throws Error {
@@ -593,12 +585,6 @@ public class Games.RetroRunner : Object, Runner {
                             null);
        }
 
-       private bool on_shutdown () {
-               stop ();
-
-               return true;
-       }
-
        private string get_unsupported_system_message () {
                var platform_name = platform.get_name ();
                if (platform_name != null)


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