2 * Copyright (c) 2012, 2013 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.
15 #include <Ecore_File.h>
16 #include <runtime_info.h>
17 #include <sys/inotify.h>
20 /* For multi-user support */
21 #include <tzplatform_config.h>
23 #include "ttsd_main.h"
24 #include "ttsd_config.h"
25 #include "ttsd_engine_agent.h"
26 #include "ttsd_data.h"
28 #define NOTI_ERROR_FILE_NAME tzplatform_mkpath(TZ_USER_HOME, ".voice/ttsd_noti.err")
30 #define ENGINE_ID "ENGINE_ID"
35 static char* g_engine_id;
36 static char* g_language;
40 static Ecore_Fd_Handler* g_fd_handler_noti = NULL;
44 static ttsd_config_changed_cb g_callback;
47 int __ttsd_config_compare()
50 char buf_id[256] = {0};
51 char buf_param[256] = {0};
54 config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "r");
56 if (NULL == config_fp) {
57 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Not open file(%s)", DEFAULT_CONFIG_FILE_NAME);
63 if (EOF == fscanf(config_fp, "%s %s", buf_id, buf_param)) {
65 SLOG(LOG_ERROR, get_tag(), "[Config WARNING] Fail to read config (engine id)");
68 if (0 == strncmp(ENGINE_ID, buf_id, strlen(ENGINE_ID))) {
69 if (NULL != g_engine_id) {
70 if (0 != strcmp(g_engine_id, buf_param)) {
72 g_engine_id = strdup(buf_param);
74 SLOG(LOG_DEBUG, get_tag(), "[Config] engine id is changed : %s", g_engine_id);
76 if (NULL != g_callback)
77 g_callback(TTS_CONFIG_TYPE_ENGINE, g_engine_id, 0);
80 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current engine id is not available");
81 g_engine_id = strdup(buf_param);
82 if (NULL != g_callback)
83 g_callback(TTS_CONFIG_TYPE_ENGINE, g_engine_id, 0);
87 SLOG(LOG_ERROR, get_tag(), "[Config] Fail to load config (engine id)");
93 if (EOF == fscanf(config_fp, "%s %s %d", buf_id, buf_param, &int_param)) {
95 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (voice)");
98 if (0 == strncmp(VOICE, buf_id, strlen(VOICE))) {
99 if (NULL != g_language) {
100 if ((0 != strcmp(g_language, buf_param)) || (g_vc_type != int_param)) {
102 g_language = strdup(buf_param);
103 g_vc_type = int_param;
105 SLOG(LOG_DEBUG, get_tag(), "[Config] voice is changed : lang(%s) type(%d)", g_language, g_vc_type);
107 if (NULL != g_callback)
108 g_callback(TTS_CONFIG_TYPE_VOICE, g_language, g_vc_type);
111 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current voice is not available");
112 g_language = strdup(buf_param);
113 g_vc_type = int_param;
115 if (NULL != g_callback)
116 g_callback(TTS_CONFIG_TYPE_VOICE, g_language, g_vc_type);
120 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (voice)");
126 if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
128 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (speed)");
131 if (0 == strncmp(SPEED, buf_id, strlen(SPEED))) {
132 if (g_speed != int_param) {
135 SLOG(LOG_DEBUG, get_tag(), "[Config] speech rate is changed : %d", g_speed);
137 if (NULL != g_callback)
138 g_callback(TTS_CONFIG_TYPE_SPEED, NULL, g_speed);
142 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (speed)");
152 int __ttsd_config_save()
154 if (0 != access(DEFAULT_CONFIG_FILE_NAME, R_OK|W_OK)) {
155 if (0 == ecore_file_mkpath(CONFIG_DIRECTORY)) {
156 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to create directory (%s)", CONFIG_DIRECTORY);
158 SLOG(LOG_DEBUG, get_tag(), "[Config] Create directory (%s)", CONFIG_DIRECTORY);
163 config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "w+");
165 if (NULL == config_fp) {
166 /* make file and file default */
167 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to open config (%s)", DEFAULT_CONFIG_FILE_NAME);
171 SLOG(LOG_DEBUG, get_tag(), "[Config] Rewrite config file");
173 /* Write engine id */
174 fprintf(config_fp, "%s %s\n", ENGINE_ID, g_engine_id);
177 fprintf(config_fp, "%s %s %d\n", VOICE, g_language, g_vc_type);
180 fprintf(config_fp, "%s %d\n", SPEED, g_speed);
187 int __ttsd_config_load()
190 char buf_id[256] = {0};
191 char buf_param[256] = {0};
193 bool is_default_open = false;
195 config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "r");
197 if (NULL == config_fp) {
198 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Not open file(%s)", DEFAULT_CONFIG_FILE_NAME);
200 config_fp = fopen(CONFIG_DEFAULT, "r");
201 if (NULL == config_fp) {
202 SLOG(LOG_ERROR, get_tag(), "[Config WARNING] Not open original config file(%s)", CONFIG_DEFAULT);
206 is_default_open = true;
210 if (EOF == fscanf(config_fp, "%s %s", buf_id, buf_param)) {
212 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (engine id)");
215 if (0 == strncmp(ENGINE_ID, buf_id, strlen(ENGINE_ID))) {
216 g_engine_id = strdup(buf_param);
219 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (engine id)");
225 if (EOF == fscanf(config_fp, "%s %s %d", buf_id, buf_param, &int_param)) {
227 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (voice)");
228 __ttsd_config_save();
231 if (0 == strncmp(VOICE, buf_id, strlen(VOICE))) {
232 g_language = strdup(buf_param);
233 g_vc_type = int_param;
236 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (voice)");
241 if (true == is_default_open) {
242 /* Change default language to display language */
244 value = vconf_get_str(VCONFKEY_LANGSET);
247 SLOG(LOG_DEBUG, get_tag(), "[Config] System language : %s", value);
248 strncpy(g_language, value, strlen(g_language));
249 SLOG(LOG_DEBUG, get_tag(), "[Config] Default language : %s", g_language);
253 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail to get system language");
258 if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
260 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (speed)");
263 if (0 == strncmp(SPEED, buf_id, strlen(SPEED))) {
267 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (speed)");
274 SLOG(LOG_DEBUG, get_tag(), "[Config] Load config : engine(%s), voice(%s,%d), speed(%d)",
275 g_engine_id, g_language, g_vc_type, g_speed);
277 if (true == is_default_open) {
278 if(0 == __ttsd_config_save()) {
279 SLOG(LOG_DEBUG, get_tag(), "[Config] Create config(%s)", DEFAULT_CONFIG_FILE_NAME);
286 static Eina_Bool inotify_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
288 SLOG(LOG_DEBUG, get_tag(), "===== [Config] Inotify event call");
291 char buffer[sizeof(struct inotify_event)];
292 memset(buffer, 0, (sizeof(struct inotify_event)));
294 length = read(g_fd_noti, buffer, (sizeof(struct inotify_event)));
296 SLOG(LOG_ERROR, get_tag(), "[Config] Empty Inotify event");
297 SLOG(LOG_DEBUG, get_tag(), "=====");
298 SLOG(LOG_DEBUG, get_tag(), " ");
299 return ECORE_CALLBACK_RENEW;
302 struct inotify_event *event;
303 event = (struct inotify_event *)&buffer;
305 if (IN_CLOSE_WRITE == event->mask) {
306 __ttsd_config_compare();
308 SLOG(LOG_ERROR, get_tag(), "[Config] Undefined event");
311 SLOG(LOG_DEBUG, get_tag(), "=====");
312 SLOG(LOG_DEBUG, get_tag(), " ");
314 return ECORE_CALLBACK_PASS_ON;
317 int __config_file_open_connection()
319 /* get file notification handler */
325 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail get inotify_fd");
330 wd = inotify_add_watch(fd, DEFAULT_CONFIG_FILE_NAME, IN_CLOSE_WRITE);
332 g_fd_handler_noti = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)inotify_event_callback, NULL, NULL, NULL);
333 if (NULL == g_fd_handler_noti) {
334 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail to get handler_noti");
341 int ttsd_config_initialize(ttsd_config_changed_cb callback)
348 g_callback = callback;
350 ecore_file_mkpath(CONFIG_DIRECTORY);
352 __ttsd_config_load();
354 if (0 != __config_file_open_connection()) {
361 int ttsd_config_finalize()
363 if (NULL != g_language) {
367 if (NULL != g_engine_id) {
371 /* del inotify variable */
372 ecore_main_fd_handler_del(g_fd_handler_noti);
373 inotify_rm_watch(g_fd_noti, g_wd_noti);
379 int ttsd_config_update_language()
381 /* no work in notification mode */
385 int ttsd_config_get_default_engine(char** engine_id)
387 if (NULL == engine_id)
390 if (NULL != g_engine_id) {
391 *engine_id = strdup(g_engine_id);
393 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current engine id is NULL");
400 int ttsd_config_set_default_engine(const char* engine_id)
402 /* Not available in notification mode */
406 int ttsd_config_get_default_voice(char** language, int* type)
408 if (NULL == language || NULL == type)
411 if (NULL != g_language) {
412 *language = strdup(g_language);
415 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current language is NULL");
422 int ttsd_config_set_default_voice(const char* language, int type)
424 /* Not available in notification mode */
428 int ttsd_config_get_default_speed(int* speed)
438 int ttsd_config_set_default_speed(int speed)
440 /* Not available in notification mode */
444 int ttsd_config_save_error(int uid, int uttid, const char* lang, int vctype, const char* text,
445 const char* func, int line, const char* message)
447 SLOG(LOG_ERROR, get_tag(), "=============== TTS ERROR LOG ====================");
449 SLOG(LOG_ERROR, get_tag(), "uid(%d) uttid(%d)", uid, uttid);
451 if (NULL != func) SLOG(LOG_ERROR, get_tag(), "Function(%s) Line(%d)", func, line);
452 if (NULL != message) SLOG(LOG_ERROR, get_tag(), "Message(%s)", message);
453 if (NULL != lang) SLOG(LOG_ERROR, get_tag(), "Lang(%s), type(%d)", lang, vctype);
454 if (NULL != text) SLOG(LOG_ERROR, get_tag(), "Text(%s)", text);
456 SLOG(LOG_ERROR, get_tag(), "==================================================");