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.
15 #include "ttsd_main.h"
16 #include "ttsd_server.h"
17 #include "ttsd_dbus.h"
19 #include "ttsd_network.h"
23 #include <bundle_internal.h>
27 #include <app_manager.h>
30 #include "ttse_internal.h"
32 static ttsd_mode_e g_tts_mode = TTSD_MODE_DEFAULT;
33 static bool g_is_terminated = false;
37 if (TTSD_MODE_NOTIFICATION == g_tts_mode) {
39 } else if (TTSD_MODE_SCREEN_READER == g_tts_mode) {
41 } else if (TTSD_MODE_INTERRUPT == g_tts_mode) {
42 return "ttsdinterrupt";
48 ttsd_mode_e ttsd_get_mode()
53 void ttsd_set_mode(ttsd_mode_e mode)
59 static bool __is_default_engine()
62 engine = vconf_get_str(VCONFKEY_TTS_ENGINE_DEFAULT);
64 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get sting for vc engine");
68 char appid[1024] = {'\0', };
69 if (0 != aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1)) {
70 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get callee appid by pid");
73 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS Default Engine(%s), appId(%s)", engine, appid);
74 if (0 == strncmp(engine, appid, strlen(engine))) {
82 static ttsd_mode_e __get_mode_from_appid()
86 int ret = app_manager_get_app_id(pid, &appid);
88 if (0 != ret || NULL == appid) {
89 return TTSD_MODE_DEFAULT;
92 SLOG(LOG_WARN, tts_tag(), "[WARNING] appid (%s)", appid);
93 char* token = strtok(appid, "-");
95 while (NULL != token) {
97 token = strtok(NULL, "-");
100 ttsd_mode_e mode = TTSD_MODE_DEFAULT;
102 if (!strncmp("noti", last, strlen(last))) {
103 mode = TTSD_MODE_NOTIFICATION;
104 } else if (!strncmp("sr", last, strlen(last))) {
105 mode = TTSD_MODE_SCREEN_READER;
106 } else if (!strncmp("interrupt", last, strlen(last))) {
107 mode = TTSD_MODE_INTERRUPT;
109 SLOG(LOG_INFO, tts_tag(), "[INFO] mode (%s)", last);
118 static void __engine_changed_cb(keynode_t* key, void* data)
120 SLOG(LOG_INFO, tts_tag(), "[INFO] TTS engine vconfkey is changed");
122 /* If a new TTS engine is different from the current engine, call ttse_terminate() */
123 if (FALSE == __is_default_engine()) {
124 SLOG(LOG_WARN, tts_tag(), "[WARNING] TTS engine is changed. Please call ttse_terminate()");
126 ecore_main_loop_quit();
128 SLOG(LOG_INFO, tts_tag(), "[INFO] A new TTS engine is same as the current engine.");
134 static bool __is_test_app()
137 int ret = app_manager_get_app_id(getpid(), &appid);
138 if (APP_MANAGER_ERROR_NONE != ret || NULL == appid) {
141 SLOG(LOG_INFO, tts_tag(), "[INFO] app id (%s)", appid);
143 bool is_test_app = false;
144 if (0 == strncmp(appid, "org.tizen.tts-native-itc", 32) || 0 == strncmp(appid, "org.tizen.tts-native-utc", 32)) {
145 SLOG(LOG_INFO, tts_tag(), "[INFO] Test mode is on");
153 int ttse_main(int argc, char** argv, ttse_request_callback_s *callback)
156 ttsd_mode_e mode = TTSD_MODE_DEFAULT;
157 int ret = TTSE_ERROR_NONE;
158 g_is_terminated = false;
160 b = bundle_import_from_argv(argc, argv);
163 if (0 == bundle_get_str(b, "mode", &val)) {
165 if (!strcmp("noti", val)) {
166 mode = TTSD_MODE_NOTIFICATION;
167 } else if (!strcmp("sr", val)) {
168 mode = TTSD_MODE_SCREEN_READER;
169 } else if (!strcmp("interrupt", val)) {
170 mode = TTSD_MODE_INTERRUPT;
172 SLOG(LOG_WARN, tts_tag(), "[WARNING] mode (%s)", val);
175 SLOG(LOG_ERROR, tts_tag(), "[ERROR] NULL data");
178 SLOG(LOG_INFO, tts_tag(), "[INFO] Get mode value from app ID");
179 mode = __get_mode_from_appid();
185 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get bundle");
190 SLOG(LOG_DEBUG, tts_tag(), "Start engine as [%d] mode", mode);
193 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize Ecore");
194 return TTSE_ERROR_OPERATION_FAILED;
197 ret = ttsd_initialize(callback);
199 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize");
204 if (TRUE == __is_default_engine()) {
205 if (0 != ttsd_ipc_open_connection()) {
206 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open ipc connection");
208 return TTSE_ERROR_OPERATION_FAILED;
212 if (0 != ttsd_network_initialize()) {
213 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to initialize network");
216 /* If a new TTS engine is different from the current engine, call ttse_terminate() */
217 if (FALSE == __is_default_engine() && !__is_test_app()) {
218 SLOG(LOG_WARN, tts_tag(), "[WARNING] TTS engine is changed. Please call ttse_terminate()");
220 return TTSE_ERROR_NONE;
223 /* Register vconfkey callback to detect engine change */
224 vconf_notify_key_changed(TTS_ENGINE_DB_DEFAULT, __engine_changed_cb, NULL);
226 SLOG(LOG_DEBUG, tts_tag(), "@@@");
228 return TTSE_ERROR_NONE;
233 if (true == g_is_terminated) {
234 SLOG(LOG_INFO, tts_tag(), "[INFO] ttse_terminate() is already invoked.");
235 return TTSE_ERROR_NONE;
238 g_is_terminated = true;
241 /* Unregister vconfkey callback */
242 vconf_ignore_key_changed(TTS_ENGINE_DB_DEFAULT, __engine_changed_cb);
244 return TTSE_ERROR_NONE;
247 int ttse_get_speed_range(int* min, int* normal, int* max)
249 if (NULL == min || NULL == normal || NULL == max) {
250 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
251 return TTSE_ERROR_INVALID_PARAMETER;
254 *min = TTS_SPEED_MIN;
255 *normal = TTS_SPEED_NORMAL;
256 *max = TTS_SPEED_MAX;
261 int ttse_get_pitch_range(int* min, int* normal, int* max)
263 if (NULL == min || NULL == normal || NULL == max) {
264 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
265 return TTSE_ERROR_INVALID_PARAMETER;
268 *min = TTS_PITCH_MIN;
269 *normal = TTS_PITCH_NORMAL;
270 *max = TTS_PITCH_MAX;
275 int ttse_send_result(ttse_result_event_e event, const void* data, unsigned int data_size, ttse_audio_type_e audio_type, int rate, void* user_data)
280 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
283 ret = ttsd_send_result(event, data, data_size, audio_type, rate, user_data);
286 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send result");
292 int ttse_send_error(ttse_error_e error, const char* msg)
296 ret = ttsd_send_error(error, msg);
299 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send error");
305 int ttse_set_private_data_set_cb(ttse_private_data_set_cb callback_func)
307 if (NULL == callback_func) {
308 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
309 return TTSE_ERROR_INVALID_PARAMETER;
312 int ret = ttsd_set_private_data_set_cb(callback_func);
315 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data set cb");
321 int ttse_set_private_data_requested_cb(ttse_private_data_requested_cb callback_func)
323 if (NULL == callback_func) {
324 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
325 return TTSE_ERROR_INVALID_PARAMETER;
328 int ret = ttsd_set_private_data_requested_cb(callback_func);
331 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data requested cb");