[gnome-games] gamepad: Add mappings from user's config directory



commit 8fcb91fbe14575c2fdf30d5f0598e22264a1a850
Author: Megh Parikh <meghprkh gmail com>
Date:   Fri Jul 22 00:30:55 2016 +0530

    gamepad: Add mappings from user's config directory
    
    Extend the known mappings with the ones in the "gamepadcontrollerdb.txt"
    file in the user's config directory.
    
    This allows users to extend or replace the existing gamepad mappings.
    
    Fixes #309

 src/gamepad/gamepad-mappings-manager.vala |   17 +++++++++++++++++
 src/ui/application.vala                   |    6 ++++++
 2 files changed, 23 insertions(+), 0 deletions(-)
---
diff --git a/src/gamepad/gamepad-mappings-manager.vala b/src/gamepad/gamepad-mappings-manager.vala
index 3c408fd..a774410 100644
--- a/src/gamepad/gamepad-mappings-manager.vala
+++ b/src/gamepad/gamepad-mappings-manager.vala
@@ -6,6 +6,8 @@
  * The client interfaces with this class primarily
  */
 private class Games.GamepadMappingsManager : Object {
+       private const string MAPPINGS_FILE_NAME = "gamecontrollerdb.txt";
+
        private HashTable<string, string> names;
        private HashTable<string, string> mappings;
 
@@ -22,6 +24,16 @@ private class Games.GamepadMappingsManager : Object {
                catch (Error e) {
                        warning ("GamepadMappingsManager: Can't find gamecontrollerdb.txt: %s", e.message);
                }
+               try {
+                       var dir = Application.get_config_dir ();
+                       var path = @"$dir/$MAPPINGS_FILE_NAME";
+                       var file = File.new_for_path (path);
+                       if (file.query_exists ())
+                               add_from_file (path);
+               }
+               catch (Error e) {
+                       warning ("GamepadMappingsManager: Can't add from user's config dir's %s: %s", 
MAPPINGS_FILE_NAME, e.message);
+               }
        }
 
        public static GamepadMappingsManager get_instance () {
@@ -34,6 +46,11 @@ private class Games.GamepadMappingsManager : Object {
                add_from_input_stream (resources_open_stream (path, ResourceLookupFlags.NONE));
        }
 
+       public void add_from_file (string file_name) throws Error {
+               var file = File.new_for_path (file_name);
+               add_from_input_stream (file.read ());
+       }
+
        public void add_from_input_stream (InputStream input_stream) throws IOError {
                var data_stream = new DataInputStream (input_stream);
                var mapping_string = data_stream.read_line ();
diff --git a/src/ui/application.vala b/src/ui/application.vala
index b99591a..576bfc4 100644
--- a/src/ui/application.vala
+++ b/src/ui/application.vala
@@ -82,6 +82,12 @@ public class Games.Application : Gtk.Application {
                return @"$cache_dir/gnome-games";
        }
 
+       public static string get_config_dir () {
+               var config_dir = Environment.get_user_config_dir ();
+
+               return @"$config_dir/gnome-games";
+       }
+
        public static string get_covers_dir () {
                var cache_dir = get_cache_dir ();
 


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