[gnome-games/wip/exalm/ds: 1/2] savestate: Store screenshot aspect ratio



commit 631677c442758c817785116cdd056c724ddcbfb5
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Sun Aug 11 16:00:40 2019 +0500

    savestate: Store screenshot aspect ratio
    
    retro-gtk provides screenshot aspect ratio in form of x-dpi and y-dpi
    options in the pixbuf. We try to save them, but fail: they aren't actually
    written in the file. Hence when saving a new screenshot, read its aspect
    ratio right away and store it in the savestate keyfile.
    
    This will be used in the next commit to use proper scpect ratio for
    thumbnails.

 src/core/savestate.vala     | 26 +++++++++++++++++++++-----
 src/retro/retro-runner.vala | 29 ++++++++++++++++++++++++++---
 2 files changed, 47 insertions(+), 8 deletions(-)
---
diff --git a/src/core/savestate.vala b/src/core/savestate.vala
index 77deb3f1..f7929cdc 100644
--- a/src/core/savestate.vala
+++ b/src/core/savestate.vala
@@ -40,6 +40,21 @@ public class Games.Savestate : Object {
                }
        }
 
+       public double get_screenshot_aspect_ratio () {
+               var metadata = new KeyFile ();
+               var metadata_file_path = Path.build_filename (path, "metadata");
+
+               try {
+                       metadata.load_from_file (metadata_file_path, KeyFileFlags.NONE);
+                       return metadata.get_double ("Screenshot", "Aspect Ratio");
+               }
+               catch (Error e) {
+                       // Migrated savestates are not going to have the correct aspect ratio.
+                       // Fail gracefully and don't print
+                       return 0;
+               }
+       }
+
        public void set_snapshot_data (Bytes snapshot_data) throws Error {
                var buffer = snapshot_data.get_data ();
                var snapshot_path = Path.build_filename (path, "snapshot");
@@ -132,17 +147,17 @@ public class Games.Savestate : Object {
        }
 
        // Set the metadata for an automatic savestate
-       public void set_metadata_automatic (DateTime creation_date, string platform, string core) throws 
Error {
-               set_metadata (true, null, creation_date, platform, core);
+       public void set_metadata_automatic (DateTime creation_date, string platform, string core, double 
aspect_ratio) throws Error {
+               set_metadata (true, null, creation_date, platform, core, aspect_ratio);
        }
 
        // Set the metadata for a manual savestate
-       public void set_metadata_manual (string name, DateTime creation_date, string platform, string core) 
throws Error {
-               set_metadata (false, name, creation_date, platform, core);
+       public void set_metadata_manual (string name, DateTime creation_date, string platform, string core, 
double aspect_ratio) throws Error {
+               set_metadata (false, name, creation_date, platform, core, aspect_ratio);
        }
 
        private void set_metadata (bool is_automatic, string? name, DateTime creation_date,
-                                  string platform, string core) throws Error {
+                                  string platform, string core, double aspect_ratio) throws Error {
                var metadata_file_path = Path.build_filename (path, "metadata");
                var metadata_file = File.new_for_path (metadata_file_path);
                var metadata = new KeyFile ();
@@ -158,6 +173,7 @@ public class Games.Savestate : Object {
                metadata.set_string ("Metadata", "Creation Date", creation_date.to_string ());
                metadata.set_string ("Metadata", "Platform", platform);
                metadata.set_string ("Metadata", "Core", core);
+               metadata.set_double ("Screenshot", "Aspect Ratio", aspect_ratio);
                metadata.save_to_file (metadata_file_path);
        }
 
diff --git a/src/retro/retro-runner.vala b/src/retro/retro-runner.vala
index 7000f52a..74fd63bd 100644
--- a/src/retro/retro-runner.vala
+++ b/src/retro/retro-runner.vala
@@ -471,12 +471,13 @@ public class Games.RetroRunner : Object, Runner {
                // Populate the metadata file
                var now_time = new DateTime.now ();
                var platform_prefix = platform.get_uid_prefix ();
+               var ratio = get_screenshot_aspect_ratio ();
                if (is_automatic)
-                       tmp_live_savestate.set_metadata_automatic (now_time, platform_prefix, get_core_id ());
+                       tmp_live_savestate.set_metadata_automatic (now_time, platform_prefix, get_core_id (), 
ratio);
                else {
                        var savestate_name = create_new_savestate_name ();
 
-                       tmp_live_savestate.set_metadata_manual (savestate_name, now_time, platform_prefix, 
get_core_id ());
+                       tmp_live_savestate.set_metadata_manual (savestate_name, now_time, platform_prefix, 
get_core_id (), ratio);
                }
 
                // Save the tmp_live_savestate into the game savestates directory
@@ -485,7 +486,7 @@ public class Games.RetroRunner : Object, Runner {
 
                // Instantiate the Savestate object
                var savestate_path = Path.build_filename (game_savestates_dir_path, now_time.to_string ());
-               Savestate savestate = new Savestate (savestate_path);
+               var savestate = new Savestate (savestate_path);
 
                // Update the game_savestates array
                // Insert the new savestate at the beginning of the array since it's the latest savestate
@@ -548,6 +549,27 @@ public class Games.RetroRunner : Object, Runner {
                core.set_memory (Retro.MemoryType.SAVE_RAM, bytes);
        }
 
+       private double get_screenshot_aspect_ratio () {
+               var pixbuf = current_state_pixbuf;
+               if (pixbuf == null)
+                       return 0;
+
+               var x_dpi = pixbuf.get_option ("x-dpi");
+               var y_dpi = pixbuf.get_option ("y-dpi");
+
+               if (x_dpi == null || y_dpi == null)
+                       return 0;
+
+               float x = 0, y = 0;
+               x_dpi.scanf ("%g", out x);
+               y_dpi.scanf ("%g", out y);
+
+               if (y == 0)
+                       return 0;
+
+               return (double) x / y;
+       }
+
        private void save_screenshot_in_tmp () throws Error {
                var pixbuf = current_state_pixbuf;
                if (pixbuf == null)
@@ -577,6 +599,7 @@ public class Games.RetroRunner : Object, Runner {
                             "tEXt::Creation Time", creation_time.to_string (),
                             "tEXt::Game Title", title,
                             "tEXt::Platform", platform_name,
+                            // FIXME: x-dpi and y-dpi are not actually being saved.
                             "x-dpi", x_dpi,
                             "y-dpi", y_dpi,
                             null);


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