[gnome-games/wip/exalm/subprocess] Adapt for subprocess retro-gtk



commit 3cbab9a3e363d7e89a19aa6eb2cc8130a45410d9
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Sun Jan 5 20:54:01 2020 +0500

    Adapt for subprocess retro-gtk

 data/ui/display-box.ui                          |  1 +
 data/ui/error-display.ui                        | 10 ++++
 flatpak/org.gnome.Games.json                    |  3 +-
 plugins/nintendo-ds/src/nintendo-ds-runner.vala | 10 ----
 src/core/runner.vala                            |  1 +
 src/core/savestate.vala                         | 23 +-------
 src/retro/retro-gamepad.vala                    | 11 ++--
 src/retro/retro-options.vala                    |  3 +-
 src/retro/retro-runner.vala                     | 71 ++++++++-----------------
 src/ui/display-box.vala                         | 12 +++++
 src/ui/display-view.vala                        | 18 +++++++
 src/ui/error-display.vala                       | 17 ++++++
 12 files changed, 92 insertions(+), 88 deletions(-)
---
diff --git a/data/ui/display-box.ui b/data/ui/display-box.ui
index 0e2044ff..452a2849 100644
--- a/data/ui/display-box.ui
+++ b/data/ui/display-box.ui
@@ -21,6 +21,7 @@
             <child>
               <object class="GamesErrorDisplay" id="error_display">
                 <property name="visible">True</property>
+                <signal name="restart" handler="restart_cb"/>
               </object>
               <packing>
                 <property name="name">error</property>
diff --git a/data/ui/error-display.ui b/data/ui/error-display.ui
index 37c45a4e..40c44d6f 100644
--- a/data/ui/error-display.ui
+++ b/data/ui/error-display.ui
@@ -43,6 +43,16 @@
             <property name="wrap">True</property>
           </object>
         </child>
+        <child>
+          <object class="GtkButton" id="restart_btn">
+            <property name="visible">True</property>
+            <property name="label">_Restart</property>
+            <property name="use-underline">True</property>
+            <property name="margin-top">12</property>
+            <property name="halign">center</property>
+            <signal name="clicked" handler="restart_cb"/>
+          </object>
+        </child>
       </object>
     </child>
   </template>
diff --git a/flatpak/org.gnome.Games.json b/flatpak/org.gnome.Games.json
index 83ebd0cf..0be9bff3 100644
--- a/flatpak/org.gnome.Games.json
+++ b/flatpak/org.gnome.Games.json
@@ -218,7 +218,8 @@
             "sources" : [
                 {
                     "type" : "git",
-                    "url" : "https://gitlab.gnome.org/GNOME/retro-gtk.git";
+                    "url" : "https://gitlab.gnome.org/exalm/retro-gtk.git";,
+                    "branch" : "wip/exalm/subprocess3"
                 }
             ]
         },
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/runner.vala b/src/core/runner.vala
index 7a10160b..98d23542 100644
--- a/src/core/runner.vala
+++ b/src/core/runner.vala
@@ -3,6 +3,7 @@
 public interface Games.Runner : Object {
        public signal void new_savestate_created ();
        public signal void stopped ();
+       public signal void crash (string message);
 
        public abstract bool can_fullscreen { get; }
        public abstract bool can_resume { get; }
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..127b6897 100644
--- a/src/retro/retro-gamepad.vala
+++ b/src/retro/retro-gamepad.vala
@@ -54,17 +54,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) {
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..03328e46 100644
--- a/src/retro/retro-runner.vala
+++ b/src/retro/retro-runner.vala
@@ -3,9 +3,6 @@
 public class Games.RetroRunner : Object, Runner {
        private const int MAX_AUTOSAVES = 5;
 
-       public signal void game_init ();
-       public signal void game_deinit ();
-
        public bool can_fullscreen {
                get { return true; }
        }
@@ -30,7 +27,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 +194,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 +238,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 +247,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 +270,12 @@ 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);
+               core.crashed.connect ((core, error) => {
+                       running = false;
+                       crash (error);
+               });
 
-               loop = new Retro.MainLoop (core);
                running = false;
 
                is_initialized = true;
@@ -287,14 +287,11 @@ public class Games.RetroRunner : Object, Runner {
 
                settings.changed["video-filter"].disconnect (on_video_filter_changed);
 
-               game_deinit ();
-
                core = null;
                view.set_core (null);
                view = null;
 
                input_manager = null;
-               loop = null;
 
                _running = false;
                is_initialized = false;
@@ -322,16 +319,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);
-                               }
-                       });
-
-               game_init ();
+                       try {
+                               var options = new RetroOptions (options_path);
+                               options.apply (core);
+                       } catch (Error e) {
+                               critical (e.message);
+                       }
 
                var platforms_dir = Application.get_platforms_dir ();
                var platform_id = platform.get_id ();
@@ -361,7 +354,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 +472,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 +537,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 +576,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)
diff --git a/src/ui/display-box.vala b/src/ui/display-box.vala
index 412002bd..1891b0b3 100644
--- a/src/ui/display-box.vala
+++ b/src/ui/display-box.vala
@@ -3,6 +3,7 @@
 [GtkTemplate (ui = "/org/gnome/Games/ui/display-box.ui")]
 private class Games.DisplayBox : Gtk.Bin {
        public signal void back ();
+       public signal void restart ();
 
        private bool _is_fullscreen;
        public bool is_fullscreen {
@@ -83,6 +84,12 @@ private class Games.DisplayBox : Gtk.Bin {
                error_display.running_game_failed (game, error_message);
        }
 
+       public void display_game_crashed (Game game, string error_message) {
+               stack.visible_child = error_display;
+               savestates_list_state.is_revealed = false;
+               error_display.game_crashed (game, error_message);
+       }
+
        [GtkCallback]
        private void on_fullscreen_header_bar_back () {
                back ();
@@ -140,4 +147,9 @@ private class Games.DisplayBox : Gtk.Bin {
        private void on_savestates_list_size_allocate (Gtk.Allocation allocation) {
                display_bin.horizontal_offset = -allocation.width / 2;
        }
+
+       [GtkCallback]
+       private void restart_cb () {
+               restart ();
+       }
 }
diff --git a/src/ui/display-view.vala b/src/ui/display-view.vala
index b718e707..af4ed4fa 100644
--- a/src/ui/display-view.vala
+++ b/src/ui/display-view.vala
@@ -55,6 +55,7 @@ private class Games.DisplayView : Object, UiView {
        private SavestatesListState savestates_list_state;
 
        private long focus_out_timeout_id;
+       private Game game;
 
        public DisplayView (Gtk.Window window) {
                Object (window: window);
@@ -67,6 +68,7 @@ private class Games.DisplayView : Object, UiView {
 
                savestates_list_state.on_revealer_transition_end.connect (on_savestates_list_transition_end);
                box.back.connect (on_display_back);
+               box.restart.connect (restart_cb);
                header_bar.back.connect (on_display_back);
 
                settings = new Settings ("org.gnome.Games");
@@ -134,6 +136,9 @@ private class Games.DisplayView : Object, UiView {
                }
 
                // Shortcuts for the Savestates manager
+               if (box.runner == null)
+                       return false;
+
                if (!box.runner.supports_savestates)
                        return false;
 
@@ -249,11 +254,17 @@ private class Games.DisplayView : Object, UiView {
                back ();
        }
 
+       private void restart_cb () {
+               run_game (game);
+       }
+
        public void run_game (Game game) {
                // If there is a game already running we have to quit it first
                if (box.runner != null && !quit_game ())
                        return;
 
+               this.game = game;
+
                if (run_game_cancellable != null)
                        run_game_cancellable.cancel ();
 
@@ -286,6 +297,13 @@ private class Games.DisplayView : Object, UiView {
                header_bar.media_set = runner.media_set;
                box.header_bar.media_set = runner.media_set;
 
+               runner.crash.connect (message => {
+                       runner.stop ();
+                       reset_display_page ();
+
+                       box.display_game_crashed (game, message);
+               });
+
                is_fullscreen = settings.get_boolean ("fullscreen") && runner.can_fullscreen;
 
                if (!runner.can_resume) {
diff --git a/src/ui/error-display.vala b/src/ui/error-display.vala
index 97473ff8..cb00eeee 100644
--- a/src/ui/error-display.vala
+++ b/src/ui/error-display.vala
@@ -2,10 +2,14 @@
 
 [GtkTemplate (ui = "/org/gnome/Games/ui/error-display.ui")]
 private class Games.ErrorDisplay : Gtk.Box {
+       public signal void restart ();
+
        [GtkChild]
        private Gtk.Label primary_label;
        [GtkChild]
        private Gtk.Label secondary_label;
+       [GtkChild]
+       private Gtk.Button restart_btn;
 
        public void running_game_failed (Game game, string message) {
                string title;
@@ -15,10 +19,23 @@ private class Games.ErrorDisplay : Gtk.Box {
                        title = _("Oops! Unable to run the game");
 
                set_labels (title, message);
+               restart_btn.hide ();
+       }
+
+       public void game_crashed (Game game, string message) {
+               var title = _("Oops! ā€œ%sā€ stopped").printf (game.name);
+
+               set_labels (title, message);
+               restart_btn.show ();
        }
 
        private void set_labels (string primary, string secondary) {
                primary_label.label = @"<big><b>$(Markup.escape_text (primary))</b></big>";
                secondary_label.label = Markup.escape_text (secondary);
        }
+
+       [GtkCallback]
+       private void restart_cb () {
+               restart ();
+       }
 }


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