feedback: Change feedback pattern data from list to GHashTable 86/302786/3
authorYunhee Seo <yuni.seo@samsung.com>
Wed, 13 Dec 2023 07:43:25 +0000 (16:43 +0900)
committerYunhee Seo <yuni.seo@samsung.com>
Mon, 18 Dec 2023 08:17:58 +0000 (17:17 +0900)
Before applying this patch, feedback pattern data was handled as a list,
also the sound path of the feedback pattern was get through a linear search.
To handle the pattern data more efficiently, it has been changed to a GHashTable
with a feedback pattern enum key value and a char* sound path value.

Change-Id: Ibb80f05af247f19df9721ec78ee247807300c77f
Signed-off-by: Yunhee Seo <yuni.seo@samsung.com>
src/sound-parser.c
src/sound-parser.h
src/sound-theme-manager.c
src/sound-theme-manager.h
src/sound.c

index d1c89a1..2050968 100644 (file)
@@ -25,6 +25,7 @@
 #include <libsyscommon/list.h>
 
 #include "log.h"
+#include "profiles.h"
 #include "sound-theme-manager.h"
 
 #define INITIAL_DEFAULT_SOUND_THEME_ID                         -1
@@ -198,4 +199,50 @@ destroy_hash_table:
 void sound_parser_exit(void)
 {
        default_sound_theme_id = INITIAL_DEFAULT_SOUND_THEME_ID;
+}
+
+static int parse_sound_property(const struct parse_result *result, void *data)
+{
+       struct section_property *extracted_section_prop = NULL;
+       GList *temp_glist = NULL;
+       int pattern = 0;
+       GHashTable *sound_config_info;
+
+       if (!result || !result->props)
+               return 0;
+
+       if (!data)
+               return -EINVAL;
+
+       if (!MATCH(result->section, "Sound"))
+               return 0;
+
+       sound_config_info = (GHashTable*)data;
+
+       SYS_G_LIST_FOREACH(result->props, temp_glist, extracted_section_prop) {
+               pattern = profile->get_pattern_enum(extracted_section_prop->key);
+               if (pattern < 0 || pattern >= profile->max_pattern)
+                       return -EINVAL;
+               g_hash_table_insert(sound_config_info, GINT_TO_POINTER(pattern),
+                                       g_strdup(extracted_section_prop->value));
+       }
+       return 0;
+}
+
+int sound_parser_get_config_info(const char *file_path, void *config_info)
+{
+       int ret = 0;
+       GHashTable *sound_info;
+
+       if (!file_path || !config_info)
+               return -EINVAL;
+
+       sound_info = (GHashTable*)config_info;
+       ret = libsys_config_parse_by_section(file_path, parse_sound_property, sound_info);
+       if (ret < 0) {
+               _E("Failed to parse sound conf feedback pattern.");
+               return -EPERM;
+       }
+
+       return 0;
 }
\ No newline at end of file
index b54fcf7..ed759c7 100644 (file)
@@ -20,5 +20,6 @@
 
 int sound_parser_init(void);
 void sound_parser_exit(void);
+int sound_parser_get_config_info(const char *file_path, void *config_info);
 
 #endif
\ No newline at end of file
index 0147ca1..7cea4b4 100644 (file)
 #include "feedback-config.h"
 #include "profiles.h"
 #include "log.h"
+#include "sound-parser.h"
+#include "sound-theme-manager.h"
 
 #define SOUND_NAME                             "Sound"
 
 struct sound_theme_element {
        unsigned int id;
-       struct feedback_config_info *sound_info;
+       GHashTable *sound_info;
 };
 
 static unsigned int default_sound_theme_id;
@@ -68,63 +70,57 @@ int sound_thememan_get_number_of_theme(unsigned int *number_of_theme)
        return 0;
 }
 
-int sound_thememan_get_sound_theme_info(unsigned int sound_theme_id, void *sound_info)
+const char* sound_thememan_get_pattern_sound_path(unsigned int sound_theme_id, feedback_pattern_e pattern)
 {
        struct sound_theme_element *sound_theme_elem = NULL;
        GList *temp_glist = NULL;
-       int find_flag = 0;
-
-       if (!sound_info) {
-               _E("Invalid parameter: sound_info(NULL)");
-               return -EINVAL;
-       }
 
        SYS_G_LIST_FOREACH(g_sound_theme_list, temp_glist, sound_theme_elem) {
-               if (sound_theme_elem->id == sound_theme_id) {
-                       *(struct feedback_config_info**)sound_info = sound_theme_elem->sound_info;
-                       find_flag = 1;
-                       break;
-               }
+               if (sound_theme_elem->id == sound_theme_id)
+                       return g_hash_table_lookup(sound_theme_elem->sound_info, GINT_TO_POINTER(pattern));
        }
 
-       if (!find_flag)
-               return -EPERM;
-
-       return 0;
+       return NULL;
 }
 
 int sound_thememan_add_sound_theme(unsigned int sound_theme_id, const char* conf_file_path)
 {
        struct sound_theme_element *sound_theme_elem = NULL;
-       struct feedback_config_info *sound_info = NULL;
        int ret = 0;
+       GHashTable *sound_info = g_hash_table_new_full(g_direct_hash, g_direct_equal,
+                                                               NULL, g_free);
 
-       sound_theme_elem = (struct sound_theme_element*)calloc(1, sizeof(struct sound_theme_element));
-       if (!sound_theme_elem) {
-               _E("Failed to allocate memory for sound_theme_elem.");
-               return -ENOMEM;
-       }
-
-       sound_info = (struct feedback_config_info*)calloc(1, sizeof(struct feedback_config_info));
        if (!sound_info) {
                _E("Failed to allocate memory for sound_info.");
-               free(sound_theme_elem);
                return -ENOMEM;
        }
 
-       sound_info->name = SOUND_NAME;
-       ret = feedback_load_config(conf_file_path, sound_info);
+       sound_theme_elem = (struct sound_theme_element*)calloc(1, sizeof(struct sound_theme_element));
+       if (!sound_theme_elem) {
+               _E("Failed to allocate memory for sound_theme_elem.");
+               ret = -ENOMEM;
+               goto out_add_sound_theme_fail;
+       }
+
+       ret = sound_parser_get_config_info(conf_file_path, sound_info);
        if (ret < 0) {
                _E("Failed to load config file %s", conf_file_path);
-               feedback_free_config(sound_info);
-               free(sound_theme_elem);
-               return ret;
+               goto out_add_sound_theme_fail;
        }
 
        sound_theme_elem->id = sound_theme_id;
        sound_theme_elem->sound_info = sound_info;
        g_sound_theme_list = g_list_append(g_sound_theme_list, sound_theme_elem);
        return 0;
+
+out_add_sound_theme_fail:
+       if (sound_info)
+               g_hash_table_destroy(g_steal_pointer(&sound_info));
+
+       if (sound_theme_elem)
+               free(sound_theme_elem);
+
+       return ret;
 }
 
 int sound_thememan_get_sound_theme_ids(unsigned int *count_of_theme, unsigned int **sound_theme_ids)
@@ -156,8 +152,9 @@ static void cleanup_sound_theme_element(gpointer data)
                return;
 
        sound_theme_elem = data;
-       sound_theme_elem->sound_info->name = NULL;
-       feedback_free_config(sound_theme_elem->sound_info);
+       if (sound_theme_elem->sound_info)
+               g_hash_table_destroy(g_steal_pointer(&sound_theme_elem->sound_info));
+
        free(sound_theme_elem);
 }
 
index b8891a5..9e01cfc 100644 (file)
 #ifndef __SOUND_THEME_MANAGER_H__
 #define __SOUND_THEME_MANAGER_H__
 
+#include "feedback-ids.h"
+
 bool sound_thememan_is_sound_theme_id_exist(unsigned int sound_theme_id);
 void sound_thememan_set_default_sound_theme_id(unsigned int sound_theme_id);
 void sound_thememan_get_default_sound_theme_id(unsigned int *sound_theme_id);
-int sound_thememan_get_sound_theme_info(unsigned int sound_theme_id, void *sound_info);
+const char* sound_thememan_get_pattern_sound_path(unsigned int sound_theme_id, feedback_pattern_e pattern);
 int sound_thememan_get_number_of_theme(unsigned int *number_of_theme);
 int sound_thememan_add_sound_theme(unsigned int sound_theme_id, const char* conf_file_path);
 int sound_thememan_get_sound_theme_ids(unsigned int *count_of_theme, unsigned int **sound_theme_ids);
index 3c7c635..eb2c040 100644 (file)
@@ -53,10 +53,6 @@ static unsigned int current_theme_id = 0;
 static char *get_data(feedback_pattern_e pattern)
 {
        char *data;
-       int i;
-       int index = -1;
-       int ret = 0;
-       struct feedback_config_info *sound_info = NULL;
 
        if (pattern <= FEEDBACK_PATTERN_NONE ||
            pattern >= profile->max_pattern)
@@ -66,23 +62,9 @@ static char *get_data(feedback_pattern_e pattern)
        if (data)
                return data;
 
-       ret = sound_thememan_get_sound_theme_info(current_theme_id, &sound_info);
-       if (ret < 0)
-               return NULL;
-
-       for (i = 0; i < profile->get_num_of_pattern(); i++) {
-               if (pattern == sound_info->data[i].pattern) {
-                       index = i;
-                       break;
-               }
-       }
-       if (index < 0) {
+       data = sound_thememan_get_pattern_sound_path(current_theme_id, pattern);
+       if (!data)
                _E("Not supported pattern : %d", pattern);
-               return NULL;
-       }
-
-       if (sound_info->data[index].origin)
-               data = sound_info->data[index].origin;
 
        return data;
 }