Add g_steal_pointer to avoid double-free
[platform/core/uifw/tts.git] / server / ttsd_config.c
1 /*
2 *  Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *  Licensed under the Apache License, Version 2.0 (the "License");
4 *  you may not use this file except in compliance with the License.
5 *  You may obtain a copy of the License at
6 *  http://www.apache.org/licenses/LICENSE-2.0
7 *  Unless required by applicable law or agreed to in writing, software
8 *  distributed under the License is distributed on an "AS IS" BASIS,
9 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 *  See the License for the specific language governing permissions and
11 *  limitations under the License.
12 */
13
14
15 /* For multi-user support */
16 #include <tzplatform_config.h>
17
18 #include "tts_config_mgr.h"
19 #include "ttsd_config.h"
20 #include "ttsd_main.h"
21
22
23 static ttsd_config_changed_cb g_callback;
24
25 static ttsd_config_screen_reader_changed_cb g_sr_callback;
26
27 void __ttsd_config_engine_changed_cb(const char* engine_id, const char* setting, const char* language, int voice_type, bool auto_voice, bool need_credential, void* user_data)
28 {
29         /* Need to check engine is valid */
30         if (false == tts_config_check_default_engine_is_valid(engine_id)) {
31                 SLOG(LOG_ERROR, tts_tag(), "Engine id is NOT valid : %s", engine_id);
32                 return;
33         }
34
35         if (NULL != g_callback) {
36                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "Call the engine reload callback : engine id(%s)", engine_id);
37                 g_callback(TTS_CONFIG_TYPE_ENGINE, engine_id, 0, 0);
38                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "Call the voice changed callback : lang(%s), type(%d)", language, voice_type);
39                 g_callback(TTS_CONFIG_TYPE_VOICE, language, voice_type, 0);
40         } else {
41                 SLOG(LOG_ERROR, tts_tag(), "Config changed callback is NULL");
42         }
43 }
44
45 void __ttsd_config_voice_changed_cb(const char* before_language, int before_type, const char* language, int type, bool auto_voice, void* user_data)
46 {
47         /* Need to check voice is valid */
48         if (false == tts_config_check_default_voice_is_valid(language, type)) {
49                 SLOG(LOG_ERROR, tts_tag(), "Lang(%s) type(%d) is NOT valid", language, type);
50                 return;
51         }
52
53         if (NULL != g_callback) {
54                 g_callback(TTS_CONFIG_TYPE_VOICE, language, type, 0);
55         } else {
56                 SLOG(LOG_ERROR, tts_tag(), "Config changed callback is NULL");
57         }
58 }
59
60 void __ttsd_config_speech_rate_changed_cb(int value, void* user_data)
61 {
62         if (NULL != g_callback) {
63                 g_callback(TTS_CONFIG_TYPE_SPEED, NULL, value, 0);
64         } else {
65                 SLOG(LOG_ERROR, tts_tag(), "Config changed callback is NULL");
66         }
67 }
68
69 void __ttsd_config_pitch_changed_cb(int value, void* user_data)
70 {
71         if (NULL != g_callback) {
72                 g_callback(TTS_CONFIG_TYPE_PITCH, NULL, value, 0);
73         } else {
74                 SLOG(LOG_ERROR, tts_tag(), "Config changed callback is NULL");
75         }
76 }
77
78 void __ttsd_config_screen_reader_changed_cb(bool value, void* user_data)
79 {
80         if (NULL != g_sr_callback) {
81                 g_sr_callback(value);
82         }
83 }
84
85 void __ttsd_config_bg_volume_ratio_changed_cb(double value, void* user_data)
86 {
87         if (NULL != g_callback)
88                 g_callback(TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO, NULL, 0, value);
89         else
90                 SLOG(LOG_ERROR, tts_tag(), "Config changed callback is NULL");
91 }
92
93 int ttsd_config_initialize(ttsd_config_changed_cb config_cb)
94 {
95         if (NULL == config_cb) {
96                 SLOG(LOG_ERROR, tts_tag(), "[Config] Invalid parameter");
97                 return -1;
98         }
99
100         g_callback = config_cb;
101         g_sr_callback = NULL;
102
103         int ret = tts_config_mgr_initialize(getpid(), TTS_CONFIG_CLIENT_TYPE_SERVER);
104         if (0 != ret) {
105                 SLOG(LOG_ERROR, tts_tag(), "[Config] Fail to initialize config manager");
106                 return -1;
107         }
108
109         ret = tts_config_mgr_set_callback(getpid(), __ttsd_config_engine_changed_cb, __ttsd_config_voice_changed_cb,
110                 __ttsd_config_speech_rate_changed_cb, __ttsd_config_pitch_changed_cb, __ttsd_config_bg_volume_ratio_changed_cb, NULL);
111         if (0 != ret) {
112                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set config changed : %d", ret);
113                 return -1;
114         }
115
116         return 0;
117 }
118
119 int ttsd_config_finalize()
120 {
121         tts_config_mgr_unset_screen_reader_callback(getpid());
122         tts_config_mgr_finalize(getpid(), TTS_CONFIG_CLIENT_TYPE_SERVER);
123
124         return 0;
125 }
126
127 int ttsd_config_set_screen_reader_callback(ttsd_config_screen_reader_changed_cb sr_cb)
128 {
129         if (NULL == sr_cb) {
130                 SLOG(LOG_ERROR, tts_tag(), "[Config] Invalid parameter");
131                 return -1;
132         }
133
134         g_sr_callback = sr_cb;
135
136         int ret = tts_config_mgr_set_screen_reader_callback(getpid(), __ttsd_config_screen_reader_changed_cb, NULL) ;
137         if (0 != ret) {
138                 SLOG(LOG_ERROR, tts_tag(), "[Config] Fail to set screen reader callback");
139                 return -1;
140         }
141         return 0;
142 }
143
144 int ttsd_config_get_default_engine(char** engine_id)
145 {
146         if (NULL == engine_id)
147                 return -1;
148
149         if (0 != tts_config_mgr_get_engine(engine_id)) {
150                 SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to get engine id");
151                 return TTSD_ERROR_OPERATION_FAILED;
152         }
153
154         return 0;
155 }
156
157 int ttsd_config_set_default_engine(const char* engine_id)
158 {
159         if (NULL == engine_id)
160                 return -1;
161
162         if (true == tts_config_check_default_engine_is_valid(engine_id)) {
163                 if (0 != tts_config_mgr_set_engine(engine_id)) {
164                         SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to set engine id");
165                 }
166         }
167
168         return 0;
169 }
170
171 int ttsd_config_get_default_voice(char** language, int* type)
172 {
173         if (NULL == language || NULL == type)
174                 return -1;
175
176         if (0 != tts_config_mgr_get_voice(language, type)) {
177                 SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to get default voice");
178                 return TTSD_ERROR_OPERATION_FAILED;
179         }
180
181         return 0;
182 }
183
184 int ttsd_config_get_default_speed(int* speed)
185 {
186         if (NULL == speed)
187                 return -1;
188
189         if (0 != tts_config_mgr_get_speech_rate(speed)) {
190                 SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to get default speech rate");
191                 return TTSD_ERROR_OPERATION_FAILED;
192         }
193
194         return 0;
195 }
196
197 int ttsd_config_get_default_pitch(int* pitch)
198 {
199         if (NULL == pitch)
200                 return -1;
201
202         if (0 != tts_config_mgr_get_pitch(pitch)) {
203                 SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to get default pitch");
204                 return TTSD_ERROR_OPERATION_FAILED;
205         }
206
207         return 0;
208 }
209
210 int ttsd_config_get_bg_volume_ratio(double* ratio)
211 {
212         if (NULL == ratio)
213                 return -1;
214
215         if (0 != tts_config_mgr_get_bg_volume_ratio(ratio)) {
216                 SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to get default bg volume ratio");
217                 return TTSD_ERROR_OPERATION_FAILED;
218         }
219
220         return 0;
221 }
222
223 int ttsd_config_save_error(unsigned int uid, int uttid, const char* lang, int vctype, const char* text,
224                            const char* func, int line, const char* message)
225 {
226         SLOG(LOG_DEBUG, tts_tag(), "@@@@@ TTS ERROR LOG @@@@@");
227
228         SLOG(LOG_DEBUG, tts_tag(), "uid(%u) uttid(%d)", uid, uttid);
229         SLOG(LOG_DEBUG, tts_tag(), "Function(%s) Line(%d)", (NULL == func) ? "NULL" : func, line);
230         SLOG(LOG_DEBUG, tts_tag(), "Message(%s)", (NULL == message) ? "NULL" : message);
231         SLOG(LOG_DEBUG, tts_tag(), "Lang(%s), type(%d)", (NULL == lang) ? "NULL" : lang, vctype);
232         SLOG(LOG_DEBUG, tts_tag(), "Text(%s)", (NULL == text) ? "NULL" : text);
233
234         SLOG(LOG_DEBUG, tts_tag(), "@@@@@");
235
236         return 0;
237 }
238
239 int ttsd_config_get_instant_reprepare_client(unsigned int *uid)
240 {
241         if (NULL == uid)
242                 return TTSD_ERROR_INVALID_PARAMETER;
243
244         int ret = tts_config_mgr_get_instant_reprepare_client(uid);
245         if (TTS_CONFIG_ERROR_NONE != ret) {
246                 SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to get uid of instatly reprepared client. ret(%d)", ret);
247                 return TTSD_ERROR_OPERATION_FAILED;
248         }
249
250         return TTSD_ERROR_NONE;
251 }
252
253 int ttsd_config_set_instant_reprepare_client(const unsigned int uid)
254 {
255         int ret = tts_config_mgr_set_instant_reprepare_client(uid);
256         if (TTS_CONFIG_ERROR_NONE != ret) {
257                 SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to set uid of instatly reprepared client. ret(%d)", ret);
258                 return TTSD_ERROR_OPERATION_FAILED;
259         }
260
261         return TTSD_ERROR_NONE;
262 }