3 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include <libsyscommon/list.h>
22 #include "feedback-config.h"
25 #include "sound-parser.h"
26 #include "sound-theme-manager.h"
28 #define SOUND_NAME "Sound"
30 struct sound_theme_element {
32 GHashTable *sound_info;
35 static unsigned int default_sound_theme_id;
36 static GList *g_sound_theme_list;
38 bool sound_thememan_is_sound_theme_id_exist(unsigned int sound_theme_id)
40 struct sound_theme_element *sound_theme_elem = NULL;
41 GList *temp_glist = NULL;
43 SYS_G_LIST_FOREACH(g_sound_theme_list, temp_glist, sound_theme_elem) {
44 if (sound_theme_elem->id == sound_theme_id) {
52 void sound_thememan_set_default_sound_theme_id(unsigned int sound_theme_id)
54 default_sound_theme_id = sound_theme_id;
57 void sound_thememan_get_default_sound_theme_id(unsigned int *sound_theme_id)
59 *sound_theme_id = default_sound_theme_id;
62 int sound_thememan_get_number_of_theme(unsigned int *number_of_theme)
64 if (!number_of_theme) {
65 _E("Invalid parameter: number_of_theme(NULL)");
69 *number_of_theme = (unsigned int)g_list_length(g_sound_theme_list);
73 const char* sound_thememan_get_pattern_sound_path(unsigned int sound_theme_id, feedback_pattern_e pattern)
75 struct sound_theme_element *sound_theme_elem = NULL;
76 GList *temp_glist = NULL;
78 SYS_G_LIST_FOREACH(g_sound_theme_list, temp_glist, sound_theme_elem) {
79 if (sound_theme_elem->id == sound_theme_id)
80 return g_hash_table_lookup(sound_theme_elem->sound_info, GINT_TO_POINTER(pattern));
86 int sound_thememan_add_sound_theme(unsigned int sound_theme_id, const char* conf_file_path)
88 struct sound_theme_element *sound_theme_elem = NULL;
90 GHashTable *sound_info = g_hash_table_new_full(g_direct_hash, g_direct_equal,
94 _E("Failed to allocate memory for sound_info.");
98 sound_theme_elem = (struct sound_theme_element*)calloc(1, sizeof(struct sound_theme_element));
99 if (!sound_theme_elem) {
100 _E("Failed to allocate memory for sound_theme_elem.");
102 goto out_add_sound_theme_fail;
105 ret = sound_parser_get_config_info(conf_file_path, sound_info);
107 _E("Failed to load config file %s", conf_file_path);
108 goto out_add_sound_theme_fail;
111 sound_theme_elem->id = sound_theme_id;
112 sound_theme_elem->sound_info = sound_info;
113 g_sound_theme_list = g_list_append(g_sound_theme_list, sound_theme_elem);
116 out_add_sound_theme_fail:
118 g_hash_table_destroy(g_steal_pointer(&sound_info));
120 if (sound_theme_elem)
121 free(sound_theme_elem);
126 int sound_thememan_get_sound_theme_ids(unsigned int *count_of_theme, unsigned int **sound_theme_ids)
129 unsigned int *temp_sound_theme_ids = NULL;
130 struct sound_theme_element* sound_theme_elem = NULL;
131 GList *temp_glist = NULL;
133 sound_thememan_get_number_of_theme(count_of_theme);
135 temp_sound_theme_ids = calloc(*count_of_theme, sizeof(unsigned int));
136 if (!temp_sound_theme_ids)
139 SYS_G_LIST_FOREACH(g_sound_theme_list, temp_glist, sound_theme_elem) {
140 temp_sound_theme_ids[index++] = sound_theme_elem->id;
143 *sound_theme_ids = g_steal_pointer(&temp_sound_theme_ids);
147 static void cleanup_sound_theme_element(gpointer data)
149 struct sound_theme_element *sound_theme_elem = NULL;
154 sound_theme_elem = data;
155 if (sound_theme_elem->sound_info)
156 g_hash_table_destroy(g_steal_pointer(&sound_theme_elem->sound_info));
158 free(sound_theme_elem);
161 int sound_thememan_init(void)
163 g_sound_theme_list = NULL;
167 void sound_thememan_exit(void)
169 if (g_sound_theme_list)
170 g_list_free_full(g_steal_pointer(&g_sound_theme_list), cleanup_sound_theme_element);