[gnome-games/wip/abhinavsingh/gamepad-config: 12/22] gamepad: Separate mappings in GamepadMappingsManager



commit e454032405b58214b46c61f542e01464c183d737
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 32bb2b9..dd20bde 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);
@@ -27,7 +28,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;
@@ -46,7 +48,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);
@@ -55,6 +57,7 @@ add_mapping (GamesGamepadMappingsManager *self,
 static void
 add_from_input_stream (GamesGamepadMappingsManager  *self,
                        GInputStream                 *input_stream,
+                       GHashTable                   *mappings,
                        GError                      **error)
 {
   GDataInputStream *data_stream;
@@ -79,7 +82,7 @@ add_from_input_stream (GamesGamepadMappingsManager  *self,
     if (mapping_string == NULL)
       break;
 
-    add_mapping (self, mapping_string);
+    add_mapping (self, mapping_string, mappings);
     g_free (mapping_string);
   }
   g_object_unref (data_stream);
@@ -88,6 +91,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;
@@ -106,7 +110,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);
@@ -134,10 +138,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_warning (error_message, default_mappings_uri, inner_error->message);
     g_clear_error (&inner_error);
@@ -150,7 +156,7 @@ games_gamepad_mappings_manager_new (void)
                                    dir,
                                    "/gamecontrollerdb.txt",
                                    NULL);
-  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_warning (error_message, user_mappings_uri, inner_error->message);
     g_clear_error (&inner_error);
@@ -174,15 +180,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);
 }
@@ -195,7 +231,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]