Fix unintended recording cancel by changing language
[platform/core/uifw/voice-control.git] / server / vcd_server.c
1 /*
2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <app_control.h>
18 #include <app_manager.h>
19 #include <dirent.h>
20 #include <sound_manager.h>
21
22 #include "vc_cmd_db.h"
23 #include "vc_info_parser.h"
24 #include "vcd_main.h"
25 #include "vcd_server.h"
26 #include "vcd_client_data.h"
27
28 #include "vcd_engine_agent.h"
29 #include "vcd_config.h"
30 #include "vcd_recorder.h"
31 #include "vcd_dbus.h"
32
33 #include "voice_control_command_expand.h"
34 #include "voice_control_common.h"
35
36 /*
37 * VC Server static variable
38 */
39 static bool     g_is_engine;
40
41 static GList *g_proc_list = NULL;
42
43 static Ecore_Timer *g_restart_timer = NULL;
44
45 /**
46 * @brief Enumerations of send event type.
47 */
48 typedef enum {
49         VCD_SEND_EVENT_TYPE_TEXT,               /**< send text event to vc engine*/
50         VCD_SEND_EVENT_TYPE_LIST_EVENT,         /**< send list event to vc engine */
51         VCD_SEND_EVENT_TYPE_HAPTIC_EVENT        /**< send haptic event to vc engine */
52 } vcd_send_event_type_e;
53
54 static int __vcd_server_launch_manager_app();
55
56 /*
57 * VC Server Internal Functions
58 */
59 static Eina_Bool __stop_by_silence(void *data)
60 {
61         SLOG(LOG_DEBUG, TAG_VCD, "@@@ Silence Detected ");
62
63         vcd_server_mgr_stop();
64
65         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
66         return EINA_FALSE;
67 }
68
69 static Eina_Bool __cancel_by_interrupt(void *data)
70 {
71         SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by interrupt");
72
73         vcd_server_mgr_cancel();
74
75         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
76         return EINA_FALSE;
77 }
78
79 static void __cancel_by_error(void *data)
80 {
81         SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by error");
82
83         vcd_server_mgr_cancel();
84
85         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
86         return;
87 }
88
89 static Eina_Bool __restart_engine(void *data)
90 {
91         SLOG(LOG_DEBUG, TAG_VCD, "@@@ Restart by no result");
92
93         g_restart_timer = NULL;
94
95         /* Restart recognition */
96         int ret = vcd_engine_recognize_start(true);
97         if (0 != ret) {
98                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to restart recognition : result(%d)", ret);
99                 return EINA_FALSE;
100         }
101
102         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
103
104         if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
105                 vcd_config_set_service_state(VCD_STATE_RECORDING);
106                 vcdc_send_service_state(VCD_STATE_RECORDING);
107         }
108
109         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Restart recognition");
110
111         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
112         return EINA_FALSE;
113 }
114
115 static int __server_recorder_callback(const void* data, const unsigned int length)
116 {
117         vcd_state_e state = vcd_config_get_service_state();
118         if (VCD_STATE_READY == state) {
119                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Ready state, but recording");
120         } else if (VCD_STATE_PROCESSING == state) {
121                 return 0;
122         }
123
124         vcp_speech_detect_e speech_detected = VCP_SPEECH_DETECT_NONE;
125         int ret;
126
127         ret = vcd_engine_recognize_audio(data, length, &speech_detected);
128
129         if (0 > ret) {
130                 /* Error */
131                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
132                 ecore_timer_add(0, __cancel_by_interrupt, NULL);
133                 /* Send error cb to manager */
134                 if (VCP_ERROR_OUT_OF_NETWORK == ret) {
135                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_TIMED_OUT, "Engine connection failed");
136                 } else {
137                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "Engine recognition failed");
138                 }
139                 return 0;
140         }
141
142         if (VCP_SPEECH_DETECT_BEGIN == speech_detected) {
143                 if (-1 != vcd_client_manager_get_pid()) {
144                         /* Manager client is available */
145                         if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
146                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
147                         }
148                 }
149         } else if (VCP_SPEECH_DETECT_END == speech_detected) {
150                 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
151                         /* silence detected */
152                         ecore_timer_add(0, __stop_by_silence, NULL);
153                 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
154                         /* Stop engine recognition */
155                         int ret = vcd_engine_recognize_stop();
156                         if (0 != ret) {
157                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
158                         }
159                         vcd_config_set_service_state(VCD_STATE_PROCESSING);
160                         vcdc_send_service_state(VCD_STATE_PROCESSING);
161
162                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop engine only by silence");
163                 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
164                         /* Stop engine recognition */
165                         int ret = vcd_engine_recognize_stop();
166                         if (0 != ret) {
167                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
168                         }
169                 }
170         }
171
172         return 0;
173 }
174
175 void __server_recorder_interrupt_callback()
176 {
177         SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by sound interrupt");
178
179         ecore_timer_add(0, __cancel_by_interrupt, NULL);
180
181         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
182 }
183
184 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
185 {
186         SLOG(LOG_DEBUG, TAG_VCD, "@@@ Change language ");
187
188         /* Current state is recording */
189         vcd_state_e state = vcd_config_get_service_state();
190         if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
191                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
192                 vcd_server_mgr_cancel();
193         }
194
195         int ret;
196         ret = vcd_engine_set_current_language(current_lang);
197         if (0 != ret) {
198                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
199         }
200
201         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
202
203         return;
204 }
205
206 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
207 {
208         SLOG(LOG_DEBUG, TAG_VCD, "@@@ Change foreground");
209
210         SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
211
212         if (VC_NO_FOREGROUND_PID != current) {
213                 /* Foreground app is changed */
214                 vcd_state_e state = vcd_config_get_service_state();
215                 if (VCD_STATE_RECORDING == state) {
216                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
217                         ecore_timer_add(0, __cancel_by_interrupt, NULL);
218                 }
219         }
220
221         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
222
223         return;
224 }
225
226 static int __vcd_activate_app_by_appcontrol(const char* appid)
227 {
228         if (NULL == appid) {
229                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
230                 return VCD_ERROR_INVALID_PARAMETER;
231         }
232
233         int ret = -1;
234         app_control_h app_control = NULL;
235         ret = app_control_create(&app_control);
236         if (APP_CONTROL_ERROR_NONE == ret) {
237                 // Set extra data
238                 ret = app_control_add_extra_data(app_control, "voice_launch", "get_result");
239                 if (APP_CONTROL_ERROR_NONE != ret) {
240                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add extra data, ret(%d)", ret);
241                         app_control_destroy(app_control);
242                         return VCD_ERROR_OPERATION_FAILED;
243                 }
244                 // Set an app ID.
245                 ret = app_control_set_app_id(app_control, appid);
246                 if (APP_CONTROL_ERROR_NONE != ret) {
247                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set app id, ret(%d)", ret);
248                         app_control_destroy(app_control);
249                         return VCD_ERROR_OPERATION_FAILED;
250                 }
251                 // Sent launch request
252                 ret = app_control_send_launch_request(app_control, NULL, NULL);
253                 if (APP_CONTROL_ERROR_NONE != ret) {
254                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send launch request, ret(%d)", ret);
255                         app_control_destroy(app_control);
256                         return VCD_ERROR_OPERATION_FAILED;
257                 }
258                 // Destroy app control
259                 ret = app_control_destroy(app_control);
260                 if (APP_CONTROL_ERROR_NONE != ret) {
261                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy, ret(%d)", ret);
262                         return VCD_ERROR_OPERATION_FAILED;
263                 }
264         }
265         return VCD_ERROR_NONE;
266 }
267
268 static int __vcd_resume_app(const char* appid)
269 {
270         app_context_h app_context = NULL;
271         int ret = app_manager_get_app_context(appid, &app_context);
272         if (APP_MANAGER_ERROR_NONE != ret) {
273                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
274                 return VCD_ERROR_OPERATION_FAILED;
275         }
276
277         ret = app_manager_resume_app(app_context);
278         if (APP_MANAGER_ERROR_NONE != ret) {
279                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app, ret(%d), appid(%s)", ret, appid);
280                 return VCD_ERROR_OPERATION_FAILED;
281         }
282         return VCD_ERROR_NONE;
283 }
284
285 static bool __vcd_is_package_installed(const char* appid)
286 {
287         app_info_h app_info = NULL;
288         int ret = app_manager_get_app_info(appid, &app_info);
289         if (APP_MANAGER_ERROR_NONE != ret || NULL == app_info)
290                 return false;
291         ret = app_info_destroy(app_info);
292         if (APP_MANAGER_ERROR_NONE != ret)
293                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy app_info, ret(%d)", ret);
294         return true;
295 }
296
297 static bool __vcd_launch_app(const char* result)
298 {
299         if (NULL == result) {
300                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
301                 return VCD_ERROR_INVALID_PARAMETER;
302         }
303
304         GSList* app_list = NULL;
305         if (0 != vc_db_get_appid_list(result, &app_list)) {
306                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] result text is NULL");
307                 return VCD_ERROR_INVALID_PARAMETER;
308         }
309
310         if (0 != g_slist_length(app_list)) {
311                 /* releaes data */
312                 GSList *iter = NULL;
313                 vc_deactivated_app_s* temp_app = NULL;
314                 iter = g_slist_nth(app_list, 0);
315
316                 while (NULL != iter) {
317                         temp_app = iter->data;
318
319                         if (NULL != temp_app) {
320                                 if (NULL != temp_app->appid) {
321                                         int ret = -1;
322                                         bool running = false;
323                                         ret = app_manager_is_running(temp_app->appid, &running);
324                                         if (APP_MANAGER_ERROR_NONE != ret) {
325                                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", temp_app->appid);
326                                                 free(temp_app->appid);
327                                                 temp_app->appid = NULL;
328                                                 free(temp_app);
329                                                 temp_app = NULL;
330                                                 return VCD_ERROR_OPERATION_FAILED;
331                                         }
332                                         if (false == running) {
333                                                 int tmp_ret = __vcd_is_package_installed(temp_app->appid);
334                                                 if (false == tmp_ret) {
335                                                         SLOG(LOG_WARN, TAG_VCD, "[WARNING] app is not installed, appid(%s)", temp_app->appid);
336                                                 } else {
337                                                         ret = __vcd_activate_app_by_appcontrol(temp_app->appid);
338                                                         if (0 != ret) {
339                                                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
340                                                                 free(temp_app->appid);
341                                                                 temp_app->appid = NULL;
342                                                                 free(temp_app);
343                                                                 temp_app = NULL;
344                                                                 return ret;
345                                                         }
346                                                         SLOG(LOG_ERROR, TAG_VCD, "Launch app: appid(%s) result(%s)", temp_app->appid, result);
347                                                 }
348                                         } else {
349                                                 ret = __vcd_resume_app(temp_app->appid);
350                                                 if (0 != ret) {
351                                                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
352                                                         free(temp_app->appid);
353                                                         temp_app->appid = NULL;
354                                                         free(temp_app);
355                                                         temp_app = NULL;
356                                                         return ret;
357                                                 }
358                                                 SLOG(LOG_ERROR, TAG_VCD, "Resume app: appid(%s) result(%s)", temp_app->appid, result);
359                                         }
360                                         free(temp_app->appid);
361                                         temp_app->appid = NULL;
362                                 }
363                                 free(temp_app);
364                                 temp_app = NULL;
365                         }
366                         iter = g_slist_next(iter);
367                 }
368                 app_list = NULL;
369         }
370
371         return VCD_ERROR_NONE;
372 }
373
374 static Eina_Bool __vcd_send_selected_result(void *data)
375 {
376         GSList* pid_list = NULL;
377         const char* result = vcd_client_manager_get_result_text();
378
379         if (0 != vc_info_parser_get_result_pid_list(&pid_list, result)) {
380                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
381         } else {
382                 if (0 < g_slist_length(pid_list)) {
383                         GSList* iter = NULL;
384                         vc_cmd_s* temp_cmd = NULL;
385                         int ret = 0;
386                         int pre_pid = -1;
387                         int pre_type = -1;
388
389                         iter = g_slist_nth(pid_list, 0);
390                         while (NULL != iter) {
391                                 temp_cmd = iter->data;
392
393                                 if (NULL != temp_cmd && (pre_pid != temp_cmd->pid || pre_type == VC_COMMAND_TYPE_WIDGET || temp_cmd->type == VC_COMMAND_TYPE_WIDGET)) {
394                                         /* Launch deactivated several apps that is matched with result */
395                                         ret = __vcd_launch_app(result);
396                                         if (0 != ret) {
397                                                 SLOG(LOG_ERROR, TAG_VCD, "Fail to launch or resume app, ret(%d) result(%s)", ret, result);
398                                         } else {
399                                                 /* send result noti */
400                                                 ret = vcdc_send_result(temp_cmd->pid, vcd_client_manager_get_pid(), temp_cmd->type);
401                                                 if (0 != ret) {
402                                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result, ret(%d)", ret);
403                                                         break;
404                                                 } else {
405                                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
406                                                         pre_pid = temp_cmd->pid;
407                                                         pre_type = temp_cmd->type;
408                                                 }
409                                         }
410                                         free(temp_cmd);
411                                         temp_cmd = NULL;
412                                 }
413                                 pid_list = g_slist_remove_link(pid_list, iter);
414                                 iter = g_slist_nth(pid_list, 0);
415                         }
416                 }
417         }
418
419         if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
420                 vcd_config_set_service_state(VCD_STATE_READY);
421                 vcdc_send_service_state(VCD_STATE_READY);
422         }
423
424         return EINA_FALSE;
425 }
426
427 //static void __vcd_server_pre_result_cb(vcp_pre_result_event_e event, const char* pre_result, void *user_data)
428 static void __vcd_server_asr_result_cb(vcp_asr_result_event_e event, const char* asr_result, void *user_data)
429 {
430         if (NULL != asr_result) {
431                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result - Event(%d), Text(%s)", event, asr_result);
432                 vcdc_send_pre_result_to_manager(vcd_client_manager_get_pid(), event, asr_result);
433         }
434
435         return;
436 }
437
438 static void __vcd_server_nlg_result_cb(const char* nlg_result, void *user_data)
439 {
440         int ret = __vcd_server_launch_manager_app();
441         if (0 != ret) {
442                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
443                 return;
444         }
445
446         ret = vcdc_send_dialog(vcd_client_manager_get_pid(), -1, nlg_result, NULL, 0); //0: VC_DIALOG_END
447         if (0 != ret)
448                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
449         return;
450 }
451
452 static void* __recorder_stop(void *data)
453 {
454         vcd_recorder_stop();
455         return NULL;
456 }
457
458 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
459                                                                    const char* non_fixed_result, const char* nlu_result, const char* msg, void *user_data)
460 {
461         vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
462         vcd_client_manager_set_result_text(all_result);
463
464         SLOG(LOG_INFO, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)", 
465                 event, all_result, non_fixed_result, msg, count);
466
467         if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
468                 if (VCP_RESULT_EVENT_REJECTED == event) {
469                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
470                         /* If no result and restart option is ON */
471                         /* Send reject message */
472                         bool temp = vcd_client_manager_get_exclusive();
473                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
474                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
475                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
476                         }
477
478                         g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
479                         return;
480                 }
481                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
482                 //vcd_recorder_stop();
483                 ecore_main_loop_thread_safe_call_sync(__recorder_stop, NULL);
484         } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
485                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
486                 /* Restart option is ON */
487                 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
488                 if (VCP_RESULT_EVENT_REJECTED == event) {
489                         bool temp = vcd_client_manager_get_exclusive();
490                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
491                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
492                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
493                         }
494                         return;
495                 }
496         }
497 #if 1
498
499         int pid = vcd_client_widget_get_foreground_pid();
500         if (-1 != pid) {
501                 if (NULL != all_result) {
502                         vc_info_parser_set_result(all_result, event, msg, NULL, false);
503                         bool enable = false;
504                         vcd_client_widget_get_asr_result_enabled(pid, &enable);
505                         if (true == enable) {
506                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send ASR result to Widget client");
507                                 bool is_consumed = false;
508                                 if (0 != vcdc_send_asr_result(pid, VC_COMMAND_TYPE_WIDGET, &is_consumed)) {
509                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send asr result");
510                                 } else {
511                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result is consumed(%d)", is_consumed);
512                                         if (true == is_consumed) {
513                                                 vcdc_send_show_tooltip(pid, false);
514                                                 if (-1 != vcd_client_manager_get_pid()) {
515                                                         /* Manager client is available */
516                                                         vc_info_parser_unset_result(false);
517                                                         vc_info_parser_set_result(all_result, VC_RESULT_EVENT_RESULT_SUCCESS, msg, NULL, false);
518                                                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
519                                                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
520                                                         }
521                                                 }
522
523                                                 vcd_client_manager_set_exclusive(false);
524
525                                                 vcd_config_set_service_state(VCD_STATE_READY);
526                                                 vcdc_send_service_state(VCD_STATE_READY);
527                                                 return;
528                                         }
529                                 }
530                         }
531                 }
532         }
533
534         /* if nlu_result is exist, Add command handle(is_action) into result list */
535         /* Normal result */
536         SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
537
538         int ret = -1;
539         vc_cmd_s* temp_cmd = NULL;
540         vc_cmd_list_h vc_cmd_list = NULL;
541
542         if (0 != vc_cmd_list_create(&vc_cmd_list)) {
543                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
544                 vcd_client_manager_set_exclusive(false);
545                 vcd_config_set_service_state(VCD_STATE_READY);
546                 vcdc_send_service_state(VCD_STATE_READY);
547                 return;
548         }
549
550         /* priority filter */
551         /* system > exclusive > foreground = widget > system_background > background */
552         int i = 0;
553         int* filtered_id = (int*)calloc(count, sizeof(int));
554         if (!filtered_id) {
555                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to allocate memory");
556                 return;
557         }
558         int filtered_count = 0;
559         int top_priority = VC_COMMAND_PRIORITY_BACKGROUND;
560         for (i = 0; i < count; i++) {
561                 SLOG(LOG_INFO, TAG_VCD, "[Server]   [%d] Result id(%d)", i, result_id[i]);
562
563                 if (0 > result_id[i]) {
564                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
565                         continue;
566                 }
567
568                 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
569                 if (0 == ret && NULL != temp_cmd) {
570                         if (top_priority == temp_cmd->priority) {
571                                 filtered_id[filtered_count] = result_id[i];
572                                 filtered_count++;
573                         } else if (top_priority < temp_cmd->priority) {
574                                 continue;
575                         } else {
576                                 filtered_id[0] = result_id[i];
577                                 filtered_count = 1;
578                                 top_priority = temp_cmd->priority;
579                         }
580                 }
581         }
582
583         int is_action = 0;
584         for (i = 0; i < filtered_count; i++) {
585                 SLOG(LOG_INFO, TAG_VCD, "[Server]   [%d] Filtered Result id(%d)", i, filtered_id[i]);
586
587                 if (filtered_id[i] < 0) {
588                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Filtered ID(%d) is NOT valid", filtered_id[i]);
589                         continue;
590                 }
591
592                 ret = vcd_client_get_cmd_from_result_id(filtered_id[i], &temp_cmd);
593                 if (0 == ret && NULL != temp_cmd) {
594                         switch (temp_cmd->format) {
595                         case VC_CMD_FORMAT_FIXED:
596                         case VC_CMD_FORMAT_FIXED_AND_VFIXED:
597                         case VC_CMD_FORMAT_VFIXED_AND_FIXED:
598                         case VC_CMD_FORMAT_PARTIAL:
599                         case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
600                         case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
601                                 break;
602                         case VC_CMD_FORMAT_ACTION:
603                                 is_action = 1;
604                                 break;
605                         default:
606                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
607                         }
608
609                         temp_cmd->id = i;
610                         if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
611                                 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
612                                 vc_cmd_destroy((vc_cmd_h)temp_cmd);
613                         }
614                 } else {
615                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", filtered_id[i]);
616                 }
617         }
618
619         if (NULL != filtered_id) {
620                 free(filtered_id);
621                 filtered_id = NULL;
622         }
623
624         if (NULL != nlu_result) {
625                 SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
626                 vc_info_parser_set_nlu_result(nlu_result);
627                 if (0 == is_action) {
628                         vc_cmd_h nlu_cmd;
629                         if (0 != vc_cmd_create(&nlu_cmd)) {
630                                 SLOG(LOG_ERROR, TAG_VCD, "Fail to nlu cmd create");
631                         } else {
632                                 if (0 != vc_cmd_set_type(nlu_cmd, VC_COMMAND_TYPE_SYSTEM)) {
633                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to set type");
634                                 }
635                                 if (0 != vc_cmd_set_pid(nlu_cmd, vcd_client_manager_get_pid())) {
636                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to set pid");
637                                 }
638                                 if (0 != vc_cmd_set_format(nlu_cmd, VC_CMD_FORMAT_ACTION)) {
639                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to set format");
640                                 }
641                                 if (0 != vc_cmd_list_add(vc_cmd_list, nlu_cmd)) {
642                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to add nlu cmd to list");
643                                         vc_cmd_destroy(nlu_cmd);
644                                 }
645                         }
646                 }
647         }
648
649         vc_cmd_print_list(vc_cmd_list);
650
651         SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
652
653         int result_count = 0;
654         vc_cmd_list_get_count(vc_cmd_list, &result_count);
655
656         if (0 == result_count) {
657                 /* No result */
658                 if (NULL != all_result) {
659                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
660                 } else {
661                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is NULL");
662                 }
663                 bool temp = vcd_client_manager_get_exclusive();
664                 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
665
666                 int pid = vcd_client_widget_get_foreground_pid();
667                 if (-1 != pid) {
668                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
669                         /* Send to hide tooltip */
670                         vcdc_send_show_tooltip(pid, false);
671                 }
672
673                 if (-1 != vcd_client_manager_get_pid()) {
674                         /* Manager client is available */
675                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
676                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
677                         }
678                 }
679
680                 vcd_client_manager_set_exclusive(false);
681
682                 return;
683         } else {
684                 if (false == vcd_client_manager_get_exclusive()) {
685                         int pid = vcd_client_widget_get_foreground_pid();
686                         if (-1 != pid) {
687                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
688                                 vcdc_send_show_tooltip(pid, false);
689                         }
690
691                         vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
692
693                         if (-1 != vcd_client_manager_get_pid()) {
694                                 /* Manager client is available */
695                                 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
696                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
697                                 }
698                         } else {
699                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
700                                 /* Send result to client */
701                                 ecore_timer_add(0, __vcd_send_selected_result, NULL);
702                         }
703                 } else {
704                         /* exclusive command */
705                         vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
706
707                         if (-1 != vcd_client_manager_get_pid()) {
708                                 /* Manager client is available */
709                                 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
710                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
711                                 }
712                         } else {
713                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
714                         }
715
716                         vcd_client_manager_set_exclusive(false);
717                 }
718         }
719
720         vc_cmd_list_destroy(vc_cmd_list, true);
721
722         return;
723
724 #else
725         /* No result */
726         if (NULL == result_id) {
727                 /* No result */
728                 if (NULL != all_result) {
729                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
730                         bool temp = vcd_client_manager_get_exclusive();
731                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
732                 }
733
734                 int pid = vcd_client_widget_get_foreground_pid();
735                 if (-1 != pid) {
736                         if (NULL != all_result) {
737                                 /* Send result text to widget */
738                                 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
739                         }
740
741                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
742                         /* Send to hide tooltip */
743                         vcdc_send_show_tooltip(pid, false);
744                 }
745
746                 if (-1 != vcd_client_manager_get_pid()) {
747                         /* Manager client is available */
748                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
749                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
750                         }
751                 }
752
753                 vcd_client_manager_set_exclusive(false);
754
755                 return;
756         }
757
758         /* Normal result */
759         SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
760
761         int ret = -1;
762         vc_cmd_s* temp_cmd = NULL;
763         vc_cmd_list_h vc_cmd_list = NULL;
764
765         if (0 != vc_cmd_list_create(&vc_cmd_list)) {
766                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
767                 vcd_client_manager_set_exclusive(false);
768                 vcd_config_set_service_state(VCD_STATE_READY);
769                 vcdc_send_service_state(VCD_STATE_READY);
770                 return;
771         }
772
773         int i = 0;
774         for (i = 0; i < count; i++) {
775                 SLOG(LOG_DEBUG, TAG_VCD, "[Server]   [%d] Result ID(%d)", i, result_id[i]);
776
777                 if (result_id[i] < 0) {
778                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
779                         continue;
780                 }
781
782                 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
783                 if (0 == ret && NULL != temp_cmd) {
784                         switch (temp_cmd->format) {
785                         case VC_CMD_FORMAT_FIXED:
786                         case VC_CMD_FORMAT_FIXED_AND_VFIXED:
787                         case VC_CMD_FORMAT_VFIXED_AND_FIXED:
788                         case VC_CMD_FORMAT_PARTIAL:
789                                 /* Nonfixed result is NOT valid */
790                                 break;
791                         case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
792                                 if (NULL == temp_cmd->parameter) {
793                                         if (NULL != non_fixed_result) {
794                                                 temp_cmd->parameter = strdup(non_fixed_result);
795                                         }
796                                 } else {
797                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
798                                 }
799                                 break;
800                         case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
801                                 if (NULL == temp_cmd->command) {
802                                         if (NULL != non_fixed_result) {
803                                                 temp_cmd->command = strdup(non_fixed_result);
804                                         }
805                                 } else {
806                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
807                                 }
808
809                                 break;
810                         default:
811                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
812                         }
813
814                         temp_cmd->id = i;
815                         if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
816                                 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
817                                 vc_cmd_destroy((vc_cmd_h)temp_cmd);
818                         }
819                 } else {
820                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
821                 }
822         }
823
824         vc_cmd_print_list(vc_cmd_list);
825
826         SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
827
828         int result_count = 0;
829         vc_cmd_list_get_count(vc_cmd_list, &result_count);
830
831         if (false == vcd_client_manager_get_exclusive()) {
832                 int pid = vcd_client_widget_get_foreground_pid();
833                 if (-1 != pid) {
834                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
835                         vcdc_send_show_tooltip(pid, false);
836                 }
837
838                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
839
840                 if (-1 != vcd_client_manager_get_pid()) {
841                         /* Manager client is available */
842                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
843                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
844                         }
845                 } else {
846                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
847                         /* Send result to client */
848                         ecore_timer_add(0, __vcd_send_selected_result, NULL);
849                 }
850         } else {
851                 /* exclusive command */
852                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
853
854                 if (-1 != vcd_client_manager_get_pid()) {
855                         /* Manager client is available */
856                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
857                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
858                         }
859                 } else {
860                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
861                 }
862
863                 vcd_client_manager_set_exclusive(false);
864         }
865
866         vc_cmd_list_destroy(vc_cmd_list, true);
867
868         return;
869 #endif
870 }
871
872 #if 0
873 static void __vcd_server_nlu_result_cb(vcp_result_event_e event, const char* nlu_result, void *user_data)
874 {
875         SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
876         SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
877
878         int ret = vc_info_parser_set_nlu_result(nlu_result);
879         if (0 != ret) {
880                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
881         }
882
883         return;
884 }
885 #endif
886
887 static void __vcd_server_error_cb(vcp_error_e error, const char* msg, void *user_data)
888 {
889         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
890         ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
891
892         char* error_msg = NULL;
893         if (NULL != msg) {
894                 error_msg = strdup(msg);
895         }
896
897         if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
898                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
899         }
900
901         if (NULL != error_msg) {
902                 free(error_msg);
903                 error_msg = NULL;
904         }
905
906         return;
907 }
908
909 /*
910 * vcd server Interfaces
911 */
912 static void __vcd_file_clean_up()
913 {
914         SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
915
916         DIR *dp = NULL;
917         struct dirent *dirp = NULL;
918
919         dp = opendir(VC_RUNTIME_INFO_ROOT);
920         if (dp == NULL) {
921                 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
922                 return;
923         }
924
925         char remove_path[256] = {0, };
926         do {
927                 dirp = readdir(dp);
928
929                 if (NULL != dirp) {
930                         if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
931                                 memset(remove_path, 0, 256);
932                                 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
933
934                                 /* Clean up code */
935                                 if (0 != remove(remove_path)) {
936                                         SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
937                                 } else {
938                                         SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
939                                 }
940                         }
941                 }
942         } while (NULL != dirp);
943
944         closedir(dp);
945
946         return;
947 }
948
949 static int __vcd_db_clean_up()
950 {
951         int ret = 0;
952         int cnt = VC_COMMAND_TYPE_FOREGROUND;
953         do {
954                 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
955                         ret = vc_db_delete_commands(-1, cnt, NULL);
956         } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
957
958         return ret;
959 }
960
961 int vcd_initialize()
962 {
963         int ret = 0;
964
965         /* Remove old file */
966         __vcd_file_clean_up();
967
968         /* initialize modules */
969         ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
970         if (0 != ret) {
971                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
972         }
973
974         /* Remove db data */
975         ret = __vcd_db_clean_up();
976         if (0 != ret) {
977                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
978         }
979
980         vcd_config_set_service_state(VCD_STATE_NONE);
981
982         //ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb, __vcd_server_nlu_result_cb, __vcd_server_error_cb);
983         ret = vcd_engine_agent_init(__vcd_server_asr_result_cb, __vcd_server_result_cb, __vcd_server_nlg_result_cb, __vcd_server_error_cb);
984         if (0 != ret) {
985                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
986                 return ret;
987         }
988
989         if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
990                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
991                 return VCD_ERROR_OPERATION_FAILED;
992         }
993
994         /* Find engine */
995         ret = vcd_engine_agent_initialize_current_engine();
996         if (0 != ret) {
997                 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
998                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
999                 else
1000                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
1001
1002                 g_is_engine = false;
1003         } else {
1004                 g_is_engine = true;
1005         }
1006
1007         /* Load engine */
1008         if (0 != vcd_engine_agent_load_current_engine()) {
1009                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
1010                 return VCD_ERROR_OPERATION_FAILED;
1011         }
1012
1013         /* Initialize manager info */
1014         vcd_client_manager_unset();
1015
1016         vcd_config_set_service_state(VCD_STATE_READY);
1017         vcdc_send_service_state(VCD_STATE_READY);
1018
1019         SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
1020
1021         return 0;
1022 }
1023
1024 void vcd_finalize()
1025 {
1026         GList *iter = NULL;
1027         if (0 < g_list_length(g_proc_list)) {
1028                 iter = g_list_first(g_proc_list);
1029                 while (NULL != iter) {
1030                         g_proc_list = g_list_remove_link(g_proc_list, iter);
1031                         iter = g_list_first(g_proc_list);
1032                 }
1033         }
1034
1035         if (g_restart_timer != NULL) {
1036                 ecore_timer_del(g_restart_timer);
1037                 g_restart_timer = NULL;
1038         }
1039
1040         vcd_state_e state = vcd_config_get_service_state();
1041         if (VCD_STATE_READY != state) {
1042                 if (VCD_STATE_RECORDING == state) {
1043                         vcd_recorder_stop();
1044                 }
1045                 vcd_engine_recognize_cancel();
1046         }
1047         if (0 != vcd_recorder_destroy()) {
1048                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1049         } else {
1050                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1051         }
1052
1053         if (0 != vcd_engine_agent_release()) {
1054                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1055         } else {
1056                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1057         }
1058
1059         vcd_client_manager_unset_appid();
1060
1061         vcd_config_set_service_state(VCD_STATE_NONE);
1062         vcdc_send_service_state(VCD_STATE_NONE);
1063
1064         SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
1065
1066         return;
1067 }
1068
1069 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1070 {
1071         SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
1072         ecore_main_loop_quit();
1073         return EINA_FALSE;
1074 }
1075
1076 static void __read_proc()
1077 {
1078         DIR *dp = NULL;
1079         struct dirent *dirp = NULL;
1080         int tmp;
1081
1082         GList *iter = NULL;
1083         if (0 < g_list_length(g_proc_list)) {
1084                 iter = g_list_first(g_proc_list);
1085                 while (NULL != iter) {
1086                         g_proc_list = g_list_remove_link(g_proc_list, iter);
1087                         iter = g_list_first(g_proc_list);
1088                 }
1089         }
1090
1091         dp = opendir("/proc");
1092         if (NULL == dp) {
1093                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1094         } else {
1095                 do {
1096                         dirp = readdir(dp);
1097
1098                         if (NULL != dirp) {
1099                                 tmp = atoi(dirp->d_name);
1100                                 if (0 >= tmp)   continue;
1101                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1102                         }
1103                 } while (NULL != dirp);
1104                 closedir(dp);
1105         }
1106         return;
1107 }
1108
1109 static void __vcd_cleanup_client(vcd_client_type_e type)
1110 {
1111         int* client_list = NULL;
1112         int client_count = 0;
1113         int i = 0;
1114         int j = 0;
1115         bool exist = false;
1116         int mgr_pid = -1;
1117         int ret = -1;
1118
1119         if (VCD_CLIENT_TYPE_NORMAL == type) {
1120                 ret = vcd_client_get_list(&client_list, &client_count);
1121         } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1122                 ret = vcd_client_widget_get_list(&client_list, &client_count);
1123         } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1124                 mgr_pid = vcd_client_manager_get_pid();
1125                 client_list = &mgr_pid;
1126                 client_count = 1;
1127                 if (-1 == mgr_pid) {
1128                         SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1129                         return;
1130                 }
1131         }
1132
1133         if (0 == ret || mgr_pid > 0) {
1134                 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1135                 if (NULL != client_list && client_count > 0) {
1136                         for (i = 0; i < client_count; i++) {
1137                                 exist = false;
1138                                 GList *iter = NULL;
1139                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1140                                         iter = g_list_nth(g_proc_list, j);
1141                                         if (NULL != iter) {
1142                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1143                                                         SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1144                                                         exist = true;
1145                                                         break;
1146                                                 }
1147                                         }
1148                                 }
1149
1150                                 if (false == exist) {
1151                                         SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1152                                         if (VCD_CLIENT_TYPE_NORMAL == type)
1153                                                 vcd_server_finalize(client_list[i]);
1154                                         else if (VCD_CLIENT_TYPE_WIDGET == type)
1155                                                 vcd_server_widget_finalize(client_list[i]);
1156                                         else
1157                                                 vcd_server_mgr_finalize(mgr_pid);
1158                                 }
1159                         }
1160                 }
1161                 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1162         }
1163         if (NULL != client_list && -1 == mgr_pid) {
1164                 free(client_list);
1165                 client_list = NULL;
1166         }
1167         return;
1168 }
1169
1170 Eina_Bool vcd_cleanup_client_all(void *data)
1171 {
1172         __read_proc();
1173         
1174         __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1175         __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1176         __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1177
1178 #if 0
1179         if (0 == vcd_client_get_list(&client_list, &client_count)) {
1180                 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up client ");
1181                 if (NULL != client_list && client_count > 0) {
1182                         for (i = 0; i < client_count; i++) {
1183                                 exist = false;
1184                                 GList *iter = NULL;
1185                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1186                                         iter = g_list_nth(g_proc_list, j);
1187                                         if (NULL != iter) {
1188                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1189                                                         SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1190                                                         exist = true;
1191                                                         break;
1192                                                 }
1193                                         }
1194                                 }
1195
1196                                 if (false == exist) {
1197                                         SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1198                                         vcd_server_finalize(client_list[i]);
1199                                 }
1200 #if 0
1201                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1202
1203                                 if (0 == result) {
1204                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1205                                         vcd_server_finalize(client_list[i]);
1206                                 } else if (-1 == result) {
1207                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1208                                 }
1209 #endif
1210                         }
1211                 }
1212                 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1213         }
1214         if (NULL != client_list) {
1215                 free(client_list);
1216                 client_list = NULL;
1217         }
1218
1219         /* If app is in background state, app cannot response message. */
1220         if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1221                 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up widget");
1222                 if (NULL != client_list && client_count > 0) {
1223                         for (i = 0; i < client_count; i++) {
1224                                 exist = false;
1225                                 GList *iter = NULL;
1226                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1227                                         iter = g_list_nth(g_proc_list, j);
1228                                         if (NULL != iter) {
1229                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1230                                                         SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1231                                                         exist = true;
1232                                                         break;
1233                                                 }
1234                                         }
1235                                 }
1236
1237                                 if (false == exist) {
1238                                         SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1239                                         vcd_server_widget_finalize(client_list[i]);
1240                                 }
1241 #if 0
1242                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1243
1244                                 if (0 == result) {
1245                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1246                                         vcd_server_widget_finalize(client_list[i]);
1247                                 } else if (-1 == result) {
1248                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1249                                 }
1250 #endif
1251                         }
1252                 }
1253                 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1254         }
1255
1256         if (NULL != client_list) {
1257                 free(client_list);
1258                 client_list = NULL;
1259         }
1260
1261         /* manager */
1262         exist = false;
1263         GList *iter = NULL;
1264         int mgr_pid = vcd_client_manager_get_pid();
1265         if (0 < mgr_pid) {
1266                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1267                         iter = g_list_nth(g_proc_list, j);
1268                         if (NULL != iter) {
1269                                 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1270                                         SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1271                                         exist = true;
1272                                         break;
1273                                 }
1274                         }
1275                 }
1276
1277                 if (false == exist) {
1278                         SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1279                         vcd_server_mgr_finalize(mgr_pid);
1280                 }
1281         }
1282 #endif
1283         return EINA_TRUE;
1284 }
1285
1286 int vcd_server_get_service_state()
1287 {
1288         return vcd_config_get_service_state();
1289 }
1290
1291 int vcd_server_get_foreground()
1292 {
1293         int pid;
1294         vcd_config_get_foreground(&pid);
1295         return pid;
1296 }
1297
1298
1299 /*
1300 * API for manager
1301 */
1302 int vcd_server_mgr_initialize(int pid)
1303 {
1304         if (false == g_is_engine) {
1305                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1306                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1307                         g_is_engine = false;
1308                         return VCD_ERROR_ENGINE_NOT_FOUND;
1309                 } else {
1310                         g_is_engine = true;
1311                 }
1312         }
1313
1314         /* check if pid is valid */
1315         if (false == vcd_client_manager_is_valid(pid)) {
1316                 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1317                 vcd_server_mgr_cancel();
1318                 vcd_client_manager_unset();
1319         }
1320
1321         /* Add client information to client manager */
1322         if (0 != vcd_client_manager_set(pid)) {
1323                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1324                 return VCD_ERROR_OPERATION_FAILED;
1325         }
1326
1327         if (0 != vcdc_send_manager_pid(pid))
1328                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1329
1330         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1331
1332         return VCD_ERROR_NONE;
1333 }
1334
1335 int vcd_server_mgr_finalize(int pid)
1336 {
1337         /* check if pid is valid */
1338         if (false == vcd_client_manager_is_valid(pid)) {
1339                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1340                 return VCD_ERROR_INVALID_PARAMETER;
1341         }
1342
1343         /* Cancel recognition */
1344         vcd_server_mgr_cancel();
1345
1346         if (0 != vcdc_send_manager_pid(-1))
1347                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1348
1349         /* Remove manager information */
1350         if (0 != vcd_client_manager_unset()) {
1351                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1352         }
1353
1354         if (0 == vcd_client_get_ref_count()) {
1355                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1356                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1357         }
1358
1359         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1360
1361         return VCD_ERROR_NONE;
1362 }
1363
1364 int vcd_server_mgr_set_command(int pid)
1365 {
1366         if (0 != vcd_client_manager_set_command(pid)) {
1367                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1368                 return VCD_ERROR_INVALID_PARAMETER;
1369         }
1370         return VCD_ERROR_NONE;
1371 }
1372
1373 int vcd_server_mgr_unset_command(int pid)
1374 {
1375         if (0 != vcd_client_manager_unset_command(pid)) {
1376                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1377                 return VCD_ERROR_INVALID_PARAMETER;
1378         }
1379         return VCD_ERROR_NONE;
1380 }
1381
1382 int vcd_server_mgr_set_demandable_client(int pid)
1383 {
1384         /* check if pid is valid */
1385         if (false == vcd_client_manager_is_valid(pid)) {
1386                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1387                 return VCD_ERROR_INVALID_PARAMETER;
1388         }
1389
1390         GSList* client_list = NULL;
1391         if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1392                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1393                 return VCD_ERROR_OPERATION_FAILED;
1394         }
1395
1396         /* Save client list */
1397         if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1398                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1399                 return VCD_ERROR_OPERATION_FAILED;
1400         }
1401
1402         return VCD_ERROR_NONE;
1403 }
1404
1405 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1406 {
1407         /* check if pid is valid */
1408         if (false == vcd_client_manager_is_valid(pid)) {
1409                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1410                 return VCD_ERROR_INVALID_PARAMETER;
1411         }
1412
1413         int ret = 0;
1414         vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
1415         int rate = 16000;
1416         int channel = 1;
1417
1418         ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1419         if (0 != ret) {
1420                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1421                 return VCD_ERROR_OPERATION_FAILED;
1422         }
1423
1424         ret = vcd_recorder_set(audio_type, type, rate, channel);
1425         if (0 != ret) {
1426                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1427                 return VCD_ERROR_OPERATION_FAILED;
1428         }
1429
1430         return VCD_ERROR_NONE;
1431 }
1432
1433 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1434 {
1435         /* check if pid is valid */
1436         if (false == vcd_client_manager_is_valid(pid)) {
1437                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1438                 return VCD_ERROR_INVALID_PARAMETER;
1439         }
1440
1441         int ret = vcd_recorder_get(audio_type);
1442         if (0 != ret) {
1443                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1444                 return VCD_ERROR_OPERATION_FAILED;
1445         }
1446
1447         return VCD_ERROR_NONE;
1448 }
1449
1450 int vcd_server_mgr_set_client_info(int pid)
1451 {
1452         /* check if pid is valid */
1453         if (false == vcd_client_manager_is_valid(pid)) {
1454                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1455                 return VCD_ERROR_INVALID_PARAMETER;
1456         }
1457
1458         int ret = vcd_client_save_client_info();
1459         if (0 != ret) {
1460                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1461                 return VCD_ERROR_OPERATION_FAILED;
1462         }
1463
1464         return VCD_ERROR_NONE;
1465 }
1466
1467 static int __start_internal_recognition()
1468 {
1469         int ret;
1470
1471         if (0 != vcd_client_command_collect_command()) {
1472                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
1473                 /* Send error cb to manager */
1474                 int pid = vcd_client_widget_get_foreground_pid();
1475                 if (-1 != pid)
1476                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1477                 return VCD_ERROR_OPERATION_FAILED;
1478         }
1479
1480         /* 3. Set command to engine */
1481         ret = vcd_engine_set_commands();
1482         if (0 != ret) {
1483                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set commands : %d", ret);
1484                 /* Send error cb to manager */
1485                 int pid = vcd_client_widget_get_foreground_pid();
1486                 if (-1 != pid)
1487                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1488                 return VCD_ERROR_OPERATION_FAILED;
1489         }
1490
1491         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1492
1493         bool stop_by_silence = true;
1494         if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1495                 stop_by_silence = false;
1496         }
1497
1498         vcd_client_manager_set_result_text(NULL);
1499
1500         /* 4. start recognition */
1501         ret = vcd_engine_recognize_start(stop_by_silence);
1502         if (0 != ret) {
1503                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1504                 /* Send error cb to manager */
1505                 int pid = vcd_client_widget_get_foreground_pid();
1506                 if (-1 != pid)
1507                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1508                 return VCD_ERROR_OPERATION_FAILED;
1509         }
1510
1511         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1512
1513 #if 1
1514         /* 5. recorder start */
1515         ret = vcd_recorder_start();
1516         if (0 != ret) {
1517                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1518                 vcd_engine_recognize_cancel();
1519                 /* Send error cb to manager */
1520                 int pid = vcd_client_widget_get_foreground_pid();
1521                 if (-1 != pid)
1522                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1523                 return ret;
1524         }
1525 #endif
1526
1527         vcd_config_set_service_state(VCD_STATE_RECORDING);
1528         vcdc_send_service_state(VCD_STATE_RECORDING);
1529
1530         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1531
1532         return 0;
1533 }
1534
1535 static Eina_Bool __vcd_request_show_tooltip(void *data)
1536 {
1537         int pid = vcd_client_widget_get_foreground_pid();
1538         if (-1 != pid) {
1539                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1540                 vcdc_send_show_tooltip(pid, (bool)data);
1541         }
1542
1543         return EINA_FALSE;
1544 }
1545
1546 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1547 {
1548         /* 1. check current state */
1549         vcd_state_e state = vcd_config_get_service_state();
1550
1551         if (VCD_STATE_READY != state) {
1552                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1553                 return VCD_ERROR_INVALID_STATE;
1554         }
1555         if (-1 == vcd_client_manager_get_pid()) {
1556                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1557                 return VCD_ERROR_OPERATION_FAILED;
1558         }
1559
1560         SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1561         vcd_client_set_recognition_mode(recognition_mode);
1562
1563         if (false == exclusive_cmd) {
1564                 vcd_client_update_foreground_pid();
1565                 /* Notify show tooltip */
1566                 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1567                         int pid = vcd_client_widget_get_foreground_pid();
1568                         if (-1 != pid) {
1569                                 SLOG(LOG_INFO, TAG_VCD, "[Server] Request tooltip show and widget command");
1570                                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1571                                 return 0;
1572                         }
1573                 }
1574         } else {
1575                 vcd_client_manager_set_exclusive(exclusive_cmd);
1576         }
1577
1578         int fg_pid = -1;
1579         if (true == start_by_client) {
1580                 /* Get foreground pid */
1581                 if (0 != vcd_config_get_foreground(&fg_pid)) {
1582                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1583                 }
1584
1585                 /* Set client exclusive option */
1586                 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1587                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1588                 }
1589         }
1590
1591         int ret = __start_internal_recognition();
1592         if (0 != ret) {
1593                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1594                 return ret;
1595         }
1596
1597         if (true == start_by_client) {
1598                 vcd_client_unset_exclusive_command(fg_pid);
1599         }
1600
1601         return VCD_ERROR_NONE;
1602 }
1603
1604 int vcd_server_mgr_stop()
1605 {
1606         /* 1. Check current state is recording */
1607         if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1608                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1609                 return VCD_ERROR_INVALID_STATE;
1610         }
1611         if (-1 == vcd_client_manager_get_pid()) {
1612                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1613                 return VCD_ERROR_OPERATION_FAILED;
1614         }
1615
1616 #if 1
1617         /* 2. Stop recorder */
1618         vcd_recorder_stop();
1619 #endif
1620
1621         /* 3. Stop engine recognition */
1622         int ret = vcd_engine_recognize_stop();
1623         if (0 != ret) {
1624                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1625         }
1626
1627         /* 4. Set original mode */
1628         vcd_config_set_service_state(VCD_STATE_PROCESSING);
1629         vcdc_send_service_state(VCD_STATE_PROCESSING);
1630
1631         return VCD_ERROR_NONE;
1632 }
1633
1634 int vcd_server_mgr_cancel()
1635 {
1636         if (-1 == vcd_client_manager_get_pid()) {
1637                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1638                 return VCD_ERROR_OPERATION_FAILED;
1639         }
1640
1641         if (g_restart_timer != NULL) {
1642                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1643                 ecore_timer_del(g_restart_timer);
1644                 g_restart_timer = NULL;
1645         }
1646
1647         /* 1. Check current state */
1648         vcd_state_e state = vcd_config_get_service_state();
1649         if (VCD_STATE_READY == state) {
1650                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is READY");
1651                 vcd_recorder_stop();
1652                 vcdc_send_service_state(VCD_STATE_READY);
1653                 return VCD_ERROR_NONE;
1654         }
1655
1656 #if 1
1657         /* 2. Stop recorder */
1658         vcd_recorder_stop();
1659 #endif
1660
1661         /* 3. Cancel engine */
1662         int ret = vcd_engine_recognize_cancel();
1663         if (0 != ret) {
1664                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1665         }
1666
1667         if (false == vcd_client_manager_get_exclusive()) {
1668                 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1669                         int pid = vcd_client_widget_get_foreground_pid();
1670                         if (-1 != pid) {
1671                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1672                                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1673                         }
1674                 }
1675         } else {
1676                 vcd_client_manager_set_exclusive(false);
1677         }
1678
1679         /* 4. Set state */
1680         vcd_config_set_service_state(VCD_STATE_READY);
1681         vcdc_send_service_state(VCD_STATE_READY);
1682
1683         return VCD_ERROR_NONE;
1684 }
1685
1686
1687 int vcd_server_mgr_result_select()
1688 {
1689         __vcd_send_selected_result(NULL);
1690
1691         return VCD_ERROR_NONE;
1692 }
1693
1694 int vcd_server_mgr_set_domain(int pid, const char* domain)
1695 {
1696         /* check if pid is valid */
1697         if (false == vcd_client_manager_is_valid(pid)) {
1698                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1699                 return VCD_ERROR_INVALID_PARAMETER;
1700         }
1701
1702         vcd_state_e state = vcd_config_get_service_state();
1703         if (VCD_STATE_READY != state) {
1704                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1705                 return VCD_ERROR_INVALID_STATE;
1706         }
1707
1708         /* Set domain to engine */
1709         int ret = vcd_engine_set_domain(pid, domain);
1710         if (0 != ret) {
1711                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1712         } else {
1713                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1714         }
1715
1716         return ret;
1717 }
1718
1719 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1720 {
1721         /* check if pid is valid */
1722         if (false == vcd_client_manager_is_valid(pid)) {
1723                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1724                 return VCD_ERROR_INVALID_PARAMETER;
1725         }
1726
1727         vcd_state_e state = vcd_config_get_service_state();
1728         if (VCD_STATE_READY != state) {
1729                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1730                 return VCD_ERROR_INVALID_STATE;
1731         }
1732
1733         /* Set private data to engine */
1734         int ret = vcd_engine_set_private_data(pid, key, data);
1735         if (0 != ret) {
1736                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1737         } else {
1738                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1739         }
1740
1741         return ret;
1742 }
1743
1744 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1745 {
1746         /* check if pid is valid */
1747         if (false == vcd_client_manager_is_valid(pid)) {
1748                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1749                 return VCD_ERROR_INVALID_PARAMETER;
1750         }
1751         vcd_state_e state = vcd_config_get_service_state();
1752         if (VCD_STATE_READY != state) {
1753                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1754                 return VCD_ERROR_INVALID_STATE;
1755         }
1756
1757         /* Get private data to engine */
1758         int ret = vcd_engine_get_private_data(pid, key, data);
1759         if (0 != ret) {
1760                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1761         } else {
1762                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1763         }
1764
1765         return ret;
1766 }
1767
1768 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1769 {
1770         int ret = -1;
1771
1772         /* check if pid is valid */
1773         if (false == vcd_client_manager_is_valid(pid)) {
1774                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1775                 return VCD_ERROR_INVALID_PARAMETER;
1776         }
1777
1778         vcd_state_e state = vcd_config_get_service_state();
1779         if (VCD_STATE_READY != state) {
1780                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1781                 return VCD_ERROR_INVALID_STATE;
1782         }
1783
1784         /* Reqeust do action to engine */
1785         if (VCD_SEND_EVENT_TYPE_TEXT == type)
1786                 ret = vcd_engine_process_text(pid, action);
1787         else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1788                 ret = vcd_engine_process_list_event(pid, action);
1789         else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1790                 ret = vcd_engine_process_haptic_event(pid, action);
1791
1792         if (0 != ret) {
1793                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1794         } else {
1795                 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1796                 vcdc_send_service_state(VCD_STATE_PROCESSING);
1797                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1798         }
1799
1800         return ret;
1801 }
1802
1803 int vcd_server_mgr_enable_command_type(int pid, int cmd_type)
1804 {
1805         int ret = -1;
1806
1807         /* check if pid is valid */
1808         if (false == vcd_client_manager_is_valid(pid)) {
1809                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1810                 return VCD_ERROR_INVALID_PARAMETER;
1811         }
1812
1813         vcd_state_e state = vcd_config_get_service_state();
1814         if (VCD_STATE_READY != state) {
1815                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1816                 return VCD_ERROR_INVALID_STATE;
1817         }
1818
1819         ret = vcd_config_enable_command_type(cmd_type);
1820         if (0 != ret) {
1821                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to enable command type");
1822         } else {
1823                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Enable command type(%d)", cmd_type);
1824         }
1825
1826         return ret;
1827 }
1828
1829 int vcd_server_mgr_disable_command_type(int pid, int cmd_type)
1830 {
1831         int ret = -1;
1832
1833         /* check if pid is valid */
1834         if (false == vcd_client_manager_is_valid(pid)) {
1835                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1836                 return VCD_ERROR_INVALID_PARAMETER;
1837         }
1838
1839         vcd_state_e state = vcd_config_get_service_state();
1840         if (VCD_STATE_READY != state) {
1841                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1842                 return VCD_ERROR_INVALID_STATE;
1843         }
1844
1845         ret = vcd_config_disable_command_type(cmd_type);
1846         if (0 != ret) {
1847                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to disable command type");
1848         } else {
1849                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Disable command type(%d)", cmd_type);
1850         }
1851
1852         return ret;
1853 }
1854
1855 /*
1856 * VC Server Functions for Client
1857 */
1858 int vcd_server_initialize(int pid)
1859 {
1860         if (false == g_is_engine) {
1861                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1862                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1863                         g_is_engine = false;
1864                         return VCD_ERROR_ENGINE_NOT_FOUND;
1865                 } else {
1866                         g_is_engine = true;
1867                 }
1868         }
1869
1870         if (false == vcd_engine_is_available_engine()) {
1871                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1872                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1873                         return VCD_ERROR_ENGINE_NOT_FOUND;
1874                 }
1875         }
1876
1877         /* check if pid is valid */
1878         if (true == vcd_client_is_available(pid)) {
1879                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1880                 return VCD_ERROR_INVALID_PARAMETER;
1881         }
1882
1883         /* Add client information to client manager */
1884         if (0 != vcd_client_add(pid)) {
1885                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1886                 return VCD_ERROR_OPERATION_FAILED;
1887         }
1888
1889         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1890
1891         return VCD_ERROR_NONE;
1892 }
1893
1894 int vcd_server_finalize(int pid)
1895 {
1896         /* check if pid is valid */
1897         if (false == vcd_client_is_available(pid)) {
1898                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1899                 return VCD_ERROR_INVALID_PARAMETER;
1900         }
1901
1902         /* Remove client information */
1903         if (0 != vcd_client_delete(pid)) {
1904                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1905         }
1906
1907         if (0 == vcd_client_get_ref_count()) {
1908                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1909                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1910         }
1911
1912         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1913
1914         return VCD_ERROR_NONE;
1915 }
1916
1917 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1918 {
1919         /* check if pid is valid */
1920         if (false == vcd_client_is_available(pid)) {
1921                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1922                 return VCD_ERROR_INVALID_PARAMETER;
1923         }
1924
1925         if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1926                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1927                 return VCD_ERROR_OPERATION_FAILED;
1928         }
1929
1930         return 0;
1931 }
1932
1933 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1934 {
1935         /* check if pid is valid */
1936         if (false == vcd_client_is_available(pid)) {
1937                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1938                 return VCD_ERROR_INVALID_PARAMETER;
1939         }
1940
1941         if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1942                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1943                 return VCD_ERROR_OPERATION_FAILED;
1944         }
1945
1946         return 0;
1947 }
1948
1949 int vcd_server_set_foreground(int pid, bool value)
1950 {
1951         /* check if pid is valid */
1952         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1953                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1954                 return VCD_ERROR_INVALID_PARAMETER;
1955         }
1956
1957         if (0 != vcd_config_set_foreground(pid, value)) {
1958                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1959                 return VCD_ERROR_OPERATION_FAILED;
1960         }
1961
1962         return 0;
1963 }
1964
1965 static int __vcd_server_launch_manager_app()
1966 {
1967         int ret = -1;
1968         bool running = false;
1969         char* appid = NULL;
1970
1971         ret = vcd_client_manager_get_appid(&appid);
1972         if (0 != ret || NULL == appid) {
1973                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1974                 return VCD_ERROR_OPERATION_FAILED;
1975         }
1976         ret = app_manager_is_running(appid, &running);
1977         if (0 != ret) {
1978                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1979                 free(appid);
1980                 appid = NULL;
1981                 return VCD_ERROR_OPERATION_FAILED;
1982         }
1983         if (false == running) {
1984                 int tmp_ret = __vcd_is_package_installed(appid);
1985                 if (false == tmp_ret) {
1986                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1987                 } else {
1988                         ret = __vcd_activate_app_by_appcontrol(appid);
1989                         if (0 != ret) {
1990                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
1991                                 free(appid);
1992                                 appid = NULL;
1993                                 return ret;
1994                         }
1995                         SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
1996                 }
1997         } else {
1998                 ret = __vcd_resume_app(appid);
1999                 if (0 != ret) {
2000                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
2001                         free(appid);
2002                         appid = NULL;
2003                         return ret;
2004                 }
2005                 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
2006         }
2007
2008         free(appid);
2009         appid = NULL;
2010
2011         return VCD_ERROR_NONE;
2012 }
2013
2014 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
2015 {
2016         /* check if pid is valid */
2017         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2018                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2019                 return VCD_ERROR_INVALID_PARAMETER;
2020         }
2021
2022         int ret = __vcd_server_launch_manager_app();
2023         if (0 != ret) {
2024                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), pid(%d), disp_text(%s), utt_text(%s), continue(%d)", vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2025                 return ret;
2026         }
2027
2028         ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2029         if (0 != ret) {
2030                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), pid(%d), disp_text(%s), utt_text(%s), continue(%d)", vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2031                 return ret;
2032         }
2033
2034         return 0;
2035 }
2036
2037 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
2038 {
2039         /* check if pid is valid */
2040         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2041                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2042                 return VCD_ERROR_INVALID_PARAMETER;
2043         }
2044
2045         /* check if system command is valid */
2046         if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
2047                 *is_sys_cmd_valid = true;
2048         else
2049                 *is_sys_cmd_valid = false;
2050
2051         return 0;
2052 }
2053
2054 #if 0
2055 int vcd_server_set_exclusive_command(int pid, bool value)
2056 {
2057         /* check if pid is valid */
2058         if (false == vcd_client_is_available(pid)) {
2059                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2060                 return VCD_ERROR_INVALID_PARAMETER;
2061         }
2062
2063         if (true == value) {
2064                 if (0 != vcd_client_set_exclusive_command(pid)) {
2065                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
2066                         return VCD_ERROR_OPERATION_FAILED;
2067                 }
2068         } else {
2069                 if (0 != vcd_client_unset_exclusive_command(pid)) {
2070                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
2071                         return VCD_ERROR_OPERATION_FAILED;
2072                 }
2073         }
2074
2075         return 0;
2076 }
2077
2078 int vcd_server_request_start(int pid, bool stop_by_silence)
2079 {
2080         /* check if pid is valid */
2081         if (false == vcd_client_is_available(pid)) {
2082                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
2083                 return VCD_ERROR_INVALID_PARAMETER;
2084         }
2085
2086         int ret;
2087         /* Check current state */
2088         vcd_state_e state = vcd_config_get_service_state();
2089
2090         /* Service state should be ready */
2091         if (VCD_STATE_READY != state) {
2092                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2093                 return VCD_ERROR_INVALID_STATE;
2094         }
2095
2096         if (-1 != vcd_client_manager_get_pid()) {
2097                 /* Check current pid is valid */
2098                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2099                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2100                         return VCD_ERROR_INVALID_PARAMETER;
2101                 }
2102         }
2103
2104         ret = vcd_server_mgr_start(stop_by_silence, false);
2105         if (0 != ret) {
2106                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2107                 return VCD_ERROR_INVALID_PARAMETER;
2108         }
2109
2110         return 0;
2111 }
2112
2113 int vcd_server_request_stop(int pid)
2114 {
2115         int ret;
2116         /* Check current state */
2117         vcd_state_e state = vcd_config_get_service_state();
2118
2119         /* Service state should be ready */
2120         if (VCD_STATE_RECORDING != state) {
2121                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2122                 return VCD_ERROR_INVALID_STATE;
2123         }
2124
2125         if (-1 != vcd_client_manager_get_pid()) {
2126                 /* Check current pid is valid */
2127                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2128                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2129                         return VCD_ERROR_INVALID_PARAMETER;
2130                 }
2131         }
2132
2133         ret = vcd_server_mgr_stop();
2134         if (0 != ret) {
2135                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2136                 return VCD_ERROR_OPERATION_FAILED;
2137         }
2138
2139         return VCD_ERROR_NONE;
2140 }
2141
2142 int vcd_server_request_cancel(int pid)
2143 {
2144         int ret;
2145         /* Check current state */
2146         vcd_state_e state = vcd_config_get_service_state();
2147
2148         /* Service state should be recording or processing */
2149         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2150                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2151                 return VCD_ERROR_INVALID_STATE;
2152         }
2153
2154         if (-1 != vcd_client_manager_get_pid()) {
2155                 /* Check current pid is valid */
2156                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2157                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2158                         return VCD_ERROR_INVALID_PARAMETER;
2159                 }
2160         }
2161
2162         ret = vcd_server_mgr_cancel();
2163         if (0 != ret) {
2164                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2165                 return VCD_ERROR_OPERATION_FAILED;
2166         }
2167
2168         return VCD_ERROR_NONE;
2169 }
2170 #endif
2171
2172 /*
2173 * VC Server Functions for Widget lib
2174 */
2175 int vcd_server_widget_initialize(int pid)
2176 {
2177         if (false == g_is_engine) {
2178                 if (0 != vcd_engine_agent_initialize_current_engine()) {
2179                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2180                         g_is_engine = false;
2181                         return VCD_ERROR_ENGINE_NOT_FOUND;
2182                 } else {
2183                         g_is_engine = true;
2184                 }
2185         }
2186
2187         if (false == vcd_engine_is_available_engine()) {
2188                 if (0 != vcd_engine_agent_initialize_current_engine()) {
2189                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2190                         return VCD_ERROR_ENGINE_NOT_FOUND;
2191                 }
2192         }
2193
2194         /* check if pid is valid */
2195         if (true == vcd_client_widget_is_available(pid)) {
2196                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2197                 return VCD_ERROR_NONE;
2198         }
2199
2200         /* Add client information to client manager */
2201         if (0 != vcd_client_widget_add(pid)) {
2202                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2203                 return VCD_ERROR_OPERATION_FAILED;
2204         }
2205
2206         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2207
2208         return VCD_ERROR_NONE;
2209 }
2210
2211 int vcd_server_widget_finalize(int pid)
2212 {
2213         /* check if pid is valid */
2214         if (false == vcd_client_widget_is_available(pid)) {
2215                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2216                 return VCD_ERROR_INVALID_PARAMETER;
2217         }
2218
2219         /* Remove client information */
2220         if (0 != vcd_client_widget_delete(pid)) {
2221                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2222         }
2223
2224         if (0 == vcd_client_get_ref_count()) {
2225                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
2226                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2227         }
2228
2229         return VCD_ERROR_NONE;
2230 }
2231
2232 int vcd_server_widget_start_recording(int pid, bool widget_command)
2233 {
2234         /* check if pid is valid */
2235         if (false == vcd_client_widget_is_available(pid)) {
2236                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2237                 return VCD_ERROR_INVALID_PARAMETER;
2238         }
2239
2240         if (true == widget_command) {
2241                 vcd_client_widget_set_command(pid);
2242                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2243         } else {
2244                 vcd_client_widget_unset_command(pid);
2245                 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2246         }
2247
2248         int ret = __start_internal_recognition();
2249         if (0 != ret) {
2250                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2251                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2252         }
2253
2254         return 0;
2255 }
2256
2257 int vcd_server_widget_start(int pid, bool stop_by_silence)
2258 {
2259         /* check if pid is valid */
2260         int fore_pid = vcd_client_widget_get_foreground_pid();
2261         if (pid != fore_pid) {
2262                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2263                 return VCD_ERROR_INVALID_PARAMETER;
2264         }
2265
2266         /* Check current state */
2267         vcd_state_e state = vcd_config_get_service_state();
2268
2269         /* Service state should be ready */
2270         if (VCD_STATE_READY != state) {
2271                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2272                 return VCD_ERROR_INVALID_STATE;
2273         }
2274
2275         vcd_client_set_slience_detection(stop_by_silence);
2276
2277         /* Notify show tooltip */
2278         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2279
2280         return 0;
2281 }
2282
2283 int vcd_server_widget_stop(int pid)
2284 {
2285         /* check if pid is valid */
2286         int fore_pid = vcd_client_widget_get_foreground_pid();
2287         if (pid != fore_pid) {
2288                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2289                 return VCD_ERROR_INVALID_PARAMETER;
2290         }
2291
2292         int ret;
2293         /* Check current state */
2294         vcd_state_e state = vcd_config_get_service_state();
2295
2296         /* Service state should be recording */
2297         if (VCD_STATE_RECORDING != state) {
2298                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2299                 return VCD_ERROR_INVALID_STATE;
2300         }
2301
2302         ret = vcd_server_mgr_stop();
2303         if (0 != ret) {
2304                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2305                 return VCD_ERROR_OPERATION_FAILED;
2306         }
2307
2308         return VCD_ERROR_NONE;
2309 }
2310
2311 int vcd_server_widget_cancel(int pid)
2312 {
2313         /* check if pid is valid */
2314         int fore_pid = vcd_client_widget_get_foreground_pid();
2315         if (pid != fore_pid) {
2316                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2317                 return VCD_ERROR_INVALID_PARAMETER;
2318         }
2319
2320         int ret;
2321         /* Check current state */
2322         vcd_state_e state = vcd_config_get_service_state();
2323
2324         /* Service state should be recording or processing */
2325         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2326                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2327                 return VCD_ERROR_INVALID_STATE;
2328         }
2329
2330         ret = vcd_server_mgr_cancel();
2331         if (0 != ret) {
2332                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2333                 return ret;
2334         }
2335
2336         return VCD_ERROR_NONE;
2337 }
2338
2339 int vcd_server_widget_enable_asr_result(int pid, bool enable)
2340 {
2341         int ret;
2342         ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
2343         if (0 != ret) {
2344                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);
2345         }
2346
2347         return ret;
2348 }
2349
2350 int vcd_server_set_language(const char* language)
2351 {
2352         int ret = VCD_ERROR_NONE;
2353
2354         ret = vcd_config_set_default_language(language);
2355         if (0 != ret) {
2356                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to set language : %d", ret);
2357                 return ret;
2358         }
2359
2360         ret = vcd_engine_set_current_language(language);
2361         if (0 != ret) {
2362                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to set language : %d", ret);
2363                 return ret;
2364         }
2365
2366         return ret;
2367 }