From 9249ee1e3c605df7fd2c6f903278980006ad535b Mon Sep 17 00:00:00 2001 From: Yunhee Seo Date: Wed, 13 Dec 2023 16:43:25 +0900 Subject: [PATCH] feedback: Change feedback pattern data from list to GHashTable 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 --- src/sound-parser.c | 47 +++++++++++++++++++++++++++++++++++ src/sound-parser.h | 1 + src/sound-theme-manager.c | 63 ++++++++++++++++++++++------------------------- src/sound-theme-manager.h | 4 ++- src/sound.c | 22 ++--------------- 5 files changed, 83 insertions(+), 54 deletions(-) diff --git a/src/sound-parser.c b/src/sound-parser.c index e33a29a..9ccadd2 100644 --- a/src/sound-parser.c +++ b/src/sound-parser.c @@ -25,6 +25,7 @@ #include #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 = syscommon_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 diff --git a/src/sound-parser.h b/src/sound-parser.h index b54fcf7..ed759c7 100644 --- a/src/sound-parser.h +++ b/src/sound-parser.h @@ -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 diff --git a/src/sound-theme-manager.c b/src/sound-theme-manager.c index 0147ca1..7cea4b4 100644 --- a/src/sound-theme-manager.c +++ b/src/sound-theme-manager.c @@ -22,12 +22,14 @@ #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); } diff --git a/src/sound-theme-manager.h b/src/sound-theme-manager.h index b8891a5..9e01cfc 100644 --- a/src/sound-theme-manager.h +++ b/src/sound-theme-manager.h @@ -18,10 +18,12 @@ #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); diff --git a/src/sound.c b/src/sound.c index b746f33..9b2bb0a 100644 --- a/src/sound.c +++ b/src/sound.c @@ -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; } -- 2.7.4