[gnome-games] gamepad: Add GamepadMappingsManager



commit 5ebba4c84e0f37165b1b6252794865238a688311
Author: Megh Parikh <meghprkh gmail com>
Date:   Mon Jul 18 21:59:03 2016 +0530

    gamepad: Add GamepadMappingsManager
    
    This class is responsible for managing mappings of gamepads and load them
    from files or resources.
    
    This is part of a series of commits to add gamepad support.

 src/Makefile.am                           |    1 +
 src/gamepad/gamepad-mappings-manager.vala |   68 +++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 2a8b529..042a1d1 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -54,6 +54,7 @@ gnome_games_SOURCES = \
        dummy/dummy-runner.vala \
        \
        gamepad/gamepad-callbacks.vala \
+       gamepad/gamepad-mappings-manager.vala \
        gamepad/linux/linux-raw-gamepad.vala \
        gamepad/linux/linux-raw-gamepad-monitor.vala \
        gamepad/raw-gamepad.vala \
diff --git a/src/gamepad/gamepad-mappings-manager.vala b/src/gamepad/gamepad-mappings-manager.vala
new file mode 100644
index 0000000..3c408fd
--- /dev/null
+++ b/src/gamepad/gamepad-mappings-manager.vala
@@ -0,0 +1,68 @@
+// This file is part of GNOME Games. License: GPLv3
+
+/**
+ * This class gives methods to set/update the mappings
+ *
+ * The client interfaces with this class primarily
+ */
+private class Games.GamepadMappingsManager : Object {
+       private HashTable<string, string> names;
+       private HashTable<string, string> mappings;
+
+       private static GamepadMappingsManager? instance;
+
+       private GamepadMappingsManager () {
+               if (names == null)
+                       names = new HashTable<string, string> (str_hash, str_equal);
+               if (mappings == null)
+                       mappings = new HashTable<string, string> (str_hash, str_equal);
+               try {
+                       add_from_resource ("/org/gnome/Games/gamepads/gamecontrollerdb.txt");
+               }
+               catch (Error e) {
+                       warning ("GamepadMappingsManager: Can't find gamecontrollerdb.txt: %s", e.message);
+               }
+       }
+
+       public static GamepadMappingsManager get_instance () {
+               if (instance == null)
+                       instance = new GamepadMappingsManager ();
+               return instance;
+       }
+
+       public void add_from_resource (string path) throws Error {
+               add_from_input_stream (resources_open_stream (path, ResourceLookupFlags.NONE));
+       }
+
+       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 ();
+               while (mapping_string != null) {
+                       add_mapping (mapping_string);
+                       mapping_string = data_stream.read_line ();
+               }
+       }
+
+       /**
+        * Adds a mapping from a SDL2 mapping string (only one gamepad)
+        */
+       public void add_mapping (string mapping_string) {
+               if (mapping_string == "" || mapping_string[0] == '#')
+                       return;
+
+               if (mapping_string.index_of ("platform") == -1 || mapping_string.index_of ("platform:Linux") 
!= -1) {
+                       var split = mapping_string.split (",", 3);
+                       names[split[0]] = split[1];
+                       mappings[split[0]] = split[2];
+               }
+       }
+
+       /**
+        * Gets the current mapping from the databse
+        * @param  guid          The guid of the wanted gamepad
+        * @return The mapping if present in the database
+        */
+       public string? get_mapping (string guid) {
+               return mappings[guid];
+       }
+}


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