[gnome-games/wip/exalm/subprocess: 2/6] retro: Adapt to API changes in retro-gtk



commit 2a2118a0a9f807920be8986c626c692d0541c8c0
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Fri Jan 10 18:42:17 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.
    
    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, 36 insertions(+), 85 deletions(-)
---
diff --git a/plugins/nintendo-ds/src/nintendo-ds-runner.vala b/plugins/nintendo-ds/src/nintendo-ds-runner.vala
index bfebab0f..4cd1677d 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 192a85a1..36e690e7 100644
--- a/src/core/savestate.vala
+++ b/src/core/savestate.vala
@@ -80,33 +80,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 637f8438..d8117bde 100644
--- a/src/retro/retro-gamepad.vala
+++ b/src/retro/retro-gamepad.vala
@@ -21,8 +21,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:
@@ -54,17 +52,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) {
@@ -101,22 +98,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 f80a8db1..52f453d0 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.set_state (previewed_savestate.get_snapshot_path ());
 
                if (previewed_savestate.has_media_data ())
                        media_set.selected_media_number = previewed_savestate.get_media_data ();
@@ -242,7 +241,7 @@ public class Games.RetroRunner : Object, Runner {
                }
                core.reset ();
 
-               loop.start ();
+               core.start ();
 
                if (latest_savestate != null)
                        load_extra_savestate_metadata (latest_savestate, true);
@@ -251,13 +250,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.start ();
                running = true;
        }
 
@@ -271,11 +273,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.run (); // Needed to finish preparing some cores.
+               core.shutdown.connect (stop);
 
-               loop = new Retro.MainLoop (core);
                running = false;
 
                is_initialized = true;
@@ -294,7 +293,6 @@ public class Games.RetroRunner : Object, Runner {
                view = null;
 
                input_manager = null;
-               loop = null;
 
                _running = false;
                is_initialized = false;
@@ -322,14 +320,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 ();
 
@@ -361,7 +357,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
@@ -479,7 +475,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.get_state (tmp_live_savestate.get_snapshot_path ());
                save_screenshot_in_tmp ();
 
                // Populate the metadata file
@@ -544,27 +540,15 @@ 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)
-                       return;
-
-               tmp_live_savestate.set_save_ram_data (save);
+               core.get_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);
-
-               var bytes = new Bytes.take (data);
-               core.set_memory (Retro.MemoryType.SAVE_RAM, bytes);
+               core.set_memory (Retro.MemoryType.SAVE_RAM, save_ram_path);
        }
 
        private void save_screenshot_in_tmp () throws Error {
@@ -595,12 +579,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]