[gnome-games/wip/abhinavsingh/gamepad-config: 16/26] gamepad: Separate mappings in GamepadMappingsManager



commit 4db725ae28cccd641bc8dafc981857182034a542
Author: theawless <theawless gmail com>
Date:   Wed Jun 21 02:43:18 2017 +0530

    gamepad: Separate mappings in GamepadMappingsManager
    
    This will allow querying the default mappings or the user mappings
    for a gamepad separately.

 src/gamepad/gamepad-mappings-manager.c |   61 +++++++++++++++++++++++++------
 src/gamepad/gamepad-mappings-manager.h |    4 ++
 2 files changed, 53 insertions(+), 12 deletions(-)
---
diff --git a/src/gamepad/gamepad-mappings-manager.c b/src/gamepad/gamepad-mappings-manager.c
index 680ff3f..d8a3e57 100644
--- a/src/gamepad/gamepad-mappings-manager.c
+++ b/src/gamepad/gamepad-mappings-manager.c
@@ -12,7 +12,8 @@ struct _GamesGamepadMappingsManager {
   GObject parent_instance;
 
   GHashTable *names;
-  GHashTable *mappings;
+  GHashTable *default_mappings;
+  GHashTable *user_mappings;
 };
 
 G_DEFINE_TYPE (GamesGamepadMappingsManager, games_gamepad_mappings_manager, G_TYPE_OBJECT);
@@ -30,7 +31,8 @@ gchar *games_application_get_config_dir (void);
 
 static void
 add_mapping (GamesGamepadMappingsManager *self,
-             const gchar                 *mapping_string)
+             const gchar                 *mapping_string,
+             GHashTable                  *mappings)
 {
   const gchar *platform;
   gchar **split;
@@ -49,7 +51,7 @@ add_mapping (GamesGamepadMappingsManager *self,
   g_hash_table_insert (self->names,
                        g_strdup (split[0]),
                        g_strdup (split[1]));
-  g_hash_table_insert (self->mappings,
+  g_hash_table_insert (mappings,
                        g_strdup (split[0]),
                        g_strdup (split[2]));
   g_strfreev (split);
@@ -58,6 +60,7 @@ add_mapping (GamesGamepadMappingsManager *self,
 static void
 add_from_input_stream (GamesGamepadMappingsManager  *self,
                        GInputStream                 *input_stream,
+                       GHashTable                   *mappings,
                        GError                      **error)
 {
   GDataInputStream *data_stream;
@@ -83,7 +86,7 @@ add_from_input_stream (GamesGamepadMappingsManager  *self,
     if (mapping_string == NULL)
       break;
 
-    add_mapping (self, mapping_string);
+    add_mapping (self, mapping_string, mappings);
   }
   g_object_unref (data_stream);
 }
@@ -91,6 +94,7 @@ add_from_input_stream (GamesGamepadMappingsManager  *self,
 static void
 add_from_file_uri (GamesGamepadMappingsManager  *self,
                    const gchar                  *file_uri,
+                   GHashTable                   *mappings,
                    GError                      **error)
 {
   GFile *file;
@@ -109,7 +113,7 @@ add_from_file_uri (GamesGamepadMappingsManager  *self,
     return;
   }
 
-  add_from_input_stream (self, G_INPUT_STREAM (stream), &inner_error);
+  add_from_input_stream (self, G_INPUT_STREAM (stream), mappings, &inner_error);
   if (G_UNLIKELY (inner_error != NULL)) {
     g_propagate_error (error, inner_error);
     g_object_unref (stream);
@@ -136,10 +140,12 @@ games_gamepad_mappings_manager_new (void)
   if (self->names == NULL)
     self->names = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 
-  if (self->mappings == NULL)
-    self->mappings = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+  if (self->default_mappings == NULL)
+    self->default_mappings = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+  if (self->user_mappings == NULL)
+    self->user_mappings = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 
-  add_from_file_uri (self, DEFAULT_MAPPINGS_URI, &inner_error);
+  add_from_file_uri (self, DEFAULT_MAPPINGS_URI, self->default_mappings, &inner_error);
   if (G_UNLIKELY (inner_error != NULL)) {
     g_critical ("GamepadMappingsManager: Can’t add mappings from %s: %s"
                DEFAULT_MAPPINGS_URI,
@@ -162,7 +168,7 @@ games_gamepad_mappings_manager_new (void)
     return self;
   }
 
-  add_from_file_uri (self, user_mappings_uri, &inner_error);
+  add_from_file_uri (self, user_mappings_uri, self->user_mappings, &inner_error);
   if (G_UNLIKELY (inner_error != NULL)) {
     g_debug ("GamepadMappingsManager: Can’t add mappings from %s: %s",
                user_mappings_uri,
@@ -189,15 +195,45 @@ games_gamepad_mappings_manager_get_instance (void)
 }
 
 gchar *
+games_gamepad_mappings_manager_get_default_mapping (GamesGamepadMappingsManager *self,
+                                                    const gchar                 *guid)
+{
+  const gchar *mapping;
+
+  g_return_val_if_fail (self != NULL, NULL);
+  g_return_val_if_fail (guid != NULL, NULL);
+
+  mapping = g_hash_table_lookup (self->default_mappings, guid);
+
+  return g_strdup (mapping);
+}
+
+gchar *
+games_gamepad_mappings_manager_get_user_mapping (GamesGamepadMappingsManager *self,
+                                                 const gchar                 *guid)
+{
+  const gchar *mapping;
+
+  g_return_val_if_fail (self != NULL, NULL);
+  g_return_val_if_fail (guid != NULL, NULL);
+
+  mapping = g_hash_table_lookup (self->user_mappings, guid);
+
+  return g_strdup (mapping);
+}
+
+gchar *
 games_gamepad_mappings_manager_get_mapping (GamesGamepadMappingsManager *self,
                                             const gchar                 *guid)
 {
-  const gchar *mapping;
+  gchar *mapping;
 
   g_return_val_if_fail (self != NULL, NULL);
   g_return_val_if_fail (guid != NULL, NULL);
 
-  mapping = g_hash_table_lookup (self->mappings, guid);
+  mapping = games_gamepad_mappings_manager_get_user_mapping (self, guid);
+  if (mapping == NULL)
+    mapping = games_gamepad_mappings_manager_get_default_mapping (self, guid);
 
   return g_strdup (mapping);
 }
@@ -210,7 +246,8 @@ finalize (GObject *object)
   GamesGamepadMappingsManager *self = GAMES_GAMEPAD_MAPPINGS_MANAGER (object);
 
   g_hash_table_unref (self->names);
-  g_hash_table_unref (self->mappings);
+  g_hash_table_unref (self->default_mappings);
+  g_hash_table_unref (self->user_mappings);
 
   G_OBJECT_CLASS (games_gamepad_mappings_manager_parent_class)->finalize (object);
 }
diff --git a/src/gamepad/gamepad-mappings-manager.h b/src/gamepad/gamepad-mappings-manager.h
index c15be3b..82d344e 100644
--- a/src/gamepad/gamepad-mappings-manager.h
+++ b/src/gamepad/gamepad-mappings-manager.h
@@ -10,6 +10,10 @@ G_BEGIN_DECLS
 G_DECLARE_FINAL_TYPE (GamesGamepadMappingsManager, games_gamepad_mappings_manager, GAMES, 
GAMEPAD_MAPPINGS_MANAGER, GObject)
 
 GamesGamepadMappingsManager *games_gamepad_mappings_manager_get_instance (void);
+gchar *games_gamepad_mappings_manager_get_default_mapping (GamesGamepadMappingsManager *self,
+                                                           const gchar                 *guid);
+gchar *games_gamepad_mappings_manager_get_user_mapping (GamesGamepadMappingsManager *self,
+                                                        const gchar                 *guid);
 gchar *games_gamepad_mappings_manager_get_mapping (GamesGamepadMappingsManager *self,
                                                    const gchar                 *guid);
 


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