Fix memory leaks
[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                                 filtered_id[0] = result_id[i];
575                                 filtered_count = 1;
576                                 top_priority = temp_cmd->priority;
577                         }
578
579                         vc_cmd_destroy((vc_cmd_h)temp_cmd);
580                         temp_cmd = NULL;
581                 }
582         }
583
584         int is_action = 0;
585         for (i = 0; i < filtered_count; i++) {
586                 SLOG(LOG_INFO, TAG_VCD, "[Server]   [%d] Filtered Result id(%d)", i, filtered_id[i]);
587
588                 if (filtered_id[i] < 0) {
589                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Filtered ID(%d) is NOT valid", filtered_id[i]);
590                         continue;
591                 }
592
593                 ret = vcd_client_get_cmd_from_result_id(filtered_id[i], &temp_cmd);
594                 if (0 == ret && NULL != temp_cmd) {
595                         switch (temp_cmd->format) {
596                         case VC_CMD_FORMAT_FIXED:
597                         case VC_CMD_FORMAT_FIXED_AND_VFIXED:
598                         case VC_CMD_FORMAT_VFIXED_AND_FIXED:
599                         case VC_CMD_FORMAT_PARTIAL:
600                         case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
601                         case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
602                                 break;
603                         case VC_CMD_FORMAT_ACTION:
604                                 is_action = 1;
605                                 break;
606                         default:
607                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
608                         }
609
610                         temp_cmd->id = i;
611                         if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
612                                 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
613                                 vc_cmd_destroy((vc_cmd_h)temp_cmd);
614                                 temp_cmd = NULL;
615                         }
616                 } else {
617                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", filtered_id[i]);
618                 }
619         }
620
621         if (NULL != filtered_id) {
622                 free(filtered_id);
623                 filtered_id = NULL;
624         }
625
626         if (NULL != nlu_result) {
627                 SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
628                 vc_info_parser_set_nlu_result(nlu_result);
629                 if (0 == is_action) {
630                         vc_cmd_h nlu_cmd;
631                         if (0 != vc_cmd_create(&nlu_cmd)) {
632                                 SLOG(LOG_ERROR, TAG_VCD, "Fail to nlu cmd create");
633                         } else {
634                                 if (0 != vc_cmd_set_type(nlu_cmd, VC_COMMAND_TYPE_SYSTEM)) {
635                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to set type");
636                                 }
637                                 if (0 != vc_cmd_set_pid(nlu_cmd, vcd_client_manager_get_pid())) {
638                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to set pid");
639                                 }
640                                 if (0 != vc_cmd_set_format(nlu_cmd, VC_CMD_FORMAT_ACTION)) {
641                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to set format");
642                                 }
643                                 if (0 != vc_cmd_list_add(vc_cmd_list, nlu_cmd)) {
644                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to add nlu cmd to list");
645                                         vc_cmd_destroy(nlu_cmd);
646                                 }
647                         }
648                 }
649         }
650
651         vc_cmd_print_list(vc_cmd_list);
652
653         SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
654
655         int result_count = 0;
656         vc_cmd_list_get_count(vc_cmd_list, &result_count);
657
658         if (0 == result_count) {
659                 /* No result */
660                 if (NULL != all_result) {
661                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
662                 } else {
663                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is NULL");
664                 }
665                 bool temp = vcd_client_manager_get_exclusive();
666                 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
667
668                 int pid = vcd_client_widget_get_foreground_pid();
669                 if (-1 != pid) {
670                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
671                         /* Send to hide tooltip */
672                         vcdc_send_show_tooltip(pid, false);
673                 }
674
675                 if (-1 != vcd_client_manager_get_pid()) {
676                         /* Manager client is available */
677                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
678                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
679                         }
680                 }
681
682                 vcd_client_manager_set_exclusive(false);
683
684                 return;
685         } else {
686                 if (false == vcd_client_manager_get_exclusive()) {
687                         int pid = vcd_client_widget_get_foreground_pid();
688                         if (-1 != pid) {
689                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
690                                 vcdc_send_show_tooltip(pid, false);
691                         }
692
693                         vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
694
695                         if (-1 != vcd_client_manager_get_pid()) {
696                                 /* Manager client is available */
697                                 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
698                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
699                                 }
700                         } else {
701                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
702                                 /* Send result to client */
703                                 ecore_timer_add(0, __vcd_send_selected_result, NULL);
704                         }
705                 } else {
706                         /* exclusive command */
707                         vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
708
709                         if (-1 != vcd_client_manager_get_pid()) {
710                                 /* Manager client is available */
711                                 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
712                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
713                                 }
714                         } else {
715                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
716                         }
717
718                         vcd_client_manager_set_exclusive(false);
719                 }
720         }
721
722         vc_cmd_list_destroy(vc_cmd_list, true);
723
724         return;
725
726 #else
727         /* No result */
728         if (NULL == result_id) {
729                 /* No result */
730                 if (NULL != all_result) {
731                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
732                         bool temp = vcd_client_manager_get_exclusive();
733                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
734                 }
735
736                 int pid = vcd_client_widget_get_foreground_pid();
737                 if (-1 != pid) {
738                         if (NULL != all_result) {
739                                 /* Send result text to widget */
740                                 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
741                         }
742
743                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
744                         /* Send to hide tooltip */
745                         vcdc_send_show_tooltip(pid, false);
746                 }
747
748                 if (-1 != vcd_client_manager_get_pid()) {
749                         /* Manager client is available */
750                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
751                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
752                         }
753                 }
754
755                 vcd_client_manager_set_exclusive(false);
756
757                 return;
758         }
759
760         /* Normal result */
761         SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
762
763         int ret = -1;
764         vc_cmd_s* temp_cmd = NULL;
765         vc_cmd_list_h vc_cmd_list = NULL;
766
767         if (0 != vc_cmd_list_create(&vc_cmd_list)) {
768                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
769                 vcd_client_manager_set_exclusive(false);
770                 vcd_config_set_service_state(VCD_STATE_READY);
771                 vcdc_send_service_state(VCD_STATE_READY);
772                 return;
773         }
774
775         int i = 0;
776         for (i = 0; i < count; i++) {
777                 SLOG(LOG_DEBUG, TAG_VCD, "[Server]   [%d] Result ID(%d)", i, result_id[i]);
778
779                 if (result_id[i] < 0) {
780                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
781                         continue;
782                 }
783
784                 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
785                 if (0 == ret && NULL != temp_cmd) {
786                         switch (temp_cmd->format) {
787                         case VC_CMD_FORMAT_FIXED:
788                         case VC_CMD_FORMAT_FIXED_AND_VFIXED:
789                         case VC_CMD_FORMAT_VFIXED_AND_FIXED:
790                         case VC_CMD_FORMAT_PARTIAL:
791                                 /* Nonfixed result is NOT valid */
792                                 break;
793                         case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
794                                 if (NULL == temp_cmd->parameter) {
795                                         if (NULL != non_fixed_result) {
796                                                 temp_cmd->parameter = strdup(non_fixed_result);
797                                         }
798                                 } else {
799                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
800                                 }
801                                 break;
802                         case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
803                                 if (NULL == temp_cmd->command) {
804                                         if (NULL != non_fixed_result) {
805                                                 temp_cmd->command = strdup(non_fixed_result);
806                                         }
807                                 } else {
808                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
809                                 }
810
811                                 break;
812                         default:
813                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
814                         }
815
816                         temp_cmd->id = i;
817                         if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
818                                 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
819                                 vc_cmd_destroy((vc_cmd_h)temp_cmd);
820                         }
821                 } else {
822                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
823                 }
824         }
825
826         vc_cmd_print_list(vc_cmd_list);
827
828         SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
829
830         int result_count = 0;
831         vc_cmd_list_get_count(vc_cmd_list, &result_count);
832
833         if (false == vcd_client_manager_get_exclusive()) {
834                 int pid = vcd_client_widget_get_foreground_pid();
835                 if (-1 != pid) {
836                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
837                         vcdc_send_show_tooltip(pid, false);
838                 }
839
840                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
841
842                 if (-1 != vcd_client_manager_get_pid()) {
843                         /* Manager client is available */
844                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
845                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
846                         }
847                 } else {
848                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
849                         /* Send result to client */
850                         ecore_timer_add(0, __vcd_send_selected_result, NULL);
851                 }
852         } else {
853                 /* exclusive command */
854                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
855
856                 if (-1 != vcd_client_manager_get_pid()) {
857                         /* Manager client is available */
858                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
859                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
860                         }
861                 } else {
862                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
863                 }
864
865                 vcd_client_manager_set_exclusive(false);
866         }
867
868         vc_cmd_list_destroy(vc_cmd_list, true);
869
870         return;
871 #endif
872 }
873
874 #if 0
875 static void __vcd_server_nlu_result_cb(vcp_result_event_e event, const char* nlu_result, void *user_data)
876 {
877         SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
878         SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
879
880         int ret = vc_info_parser_set_nlu_result(nlu_result);
881         if (0 != ret) {
882                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
883         }
884
885         return;
886 }
887 #endif
888
889 static void __vcd_server_error_cb(vcp_error_e error, const char* msg, void *user_data)
890 {
891         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
892         ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
893
894         char* error_msg = NULL;
895         if (NULL != msg) {
896                 error_msg = strdup(msg);
897         }
898
899         if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
900                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
901         }
902
903         if (NULL != error_msg) {
904                 free(error_msg);
905                 error_msg = NULL;
906         }
907
908         return;
909 }
910
911 /*
912 * vcd server Interfaces
913 */
914 static void __vcd_file_clean_up()
915 {
916         SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
917
918         DIR *dp = NULL;
919         struct dirent *dirp = NULL;
920
921         dp = opendir(VC_RUNTIME_INFO_ROOT);
922         if (dp == NULL) {
923                 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
924                 return;
925         }
926
927         char remove_path[256] = {0, };
928         do {
929                 dirp = readdir(dp);
930
931                 if (NULL != dirp) {
932                         if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
933                                 memset(remove_path, 0, 256);
934                                 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
935
936                                 /* Clean up code */
937                                 if (0 != remove(remove_path)) {
938                                         SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
939                                 } else {
940                                         SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
941                                 }
942                         }
943                 }
944         } while (NULL != dirp);
945
946         closedir(dp);
947
948         return;
949 }
950
951 static int __vcd_db_clean_up()
952 {
953         int ret = 0;
954         int cnt = VC_COMMAND_TYPE_FOREGROUND;
955         do {
956                 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
957                         ret = vc_db_delete_commands(-1, cnt, NULL);
958         } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
959
960         return ret;
961 }
962
963 int vcd_initialize()
964 {
965         int ret = 0;
966
967         /* Remove old file */
968         __vcd_file_clean_up();
969
970         /* initialize modules */
971         ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
972         if (0 != ret) {
973                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
974         }
975
976         /* Remove db data */
977         ret = __vcd_db_clean_up();
978         if (0 != ret) {
979                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
980         }
981
982         vcd_config_set_service_state(VCD_STATE_NONE);
983
984         //ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb, __vcd_server_nlu_result_cb, __vcd_server_error_cb);
985         ret = vcd_engine_agent_init(__vcd_server_asr_result_cb, __vcd_server_result_cb, __vcd_server_nlg_result_cb, __vcd_server_error_cb);
986         if (0 != ret) {
987                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
988                 return ret;
989         }
990
991         if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
992                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
993                 return VCD_ERROR_OPERATION_FAILED;
994         }
995
996         /* Find engine */
997         ret = vcd_engine_agent_initialize_current_engine();
998         if (0 != ret) {
999                 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
1000                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
1001                 else
1002                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
1003
1004                 g_is_engine = false;
1005         } else {
1006                 g_is_engine = true;
1007         }
1008
1009         /* Load engine */
1010         if (0 != vcd_engine_agent_load_current_engine()) {
1011                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
1012                 return VCD_ERROR_OPERATION_FAILED;
1013         }
1014
1015         /* Initialize manager info */
1016         vcd_client_manager_unset();
1017
1018         vcd_config_set_service_state(VCD_STATE_READY);
1019         vcdc_send_service_state(VCD_STATE_READY);
1020
1021         SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
1022
1023         return 0;
1024 }
1025
1026 void vcd_finalize()
1027 {
1028         GList *iter = NULL;
1029         if (0 < g_list_length(g_proc_list)) {
1030                 iter = g_list_first(g_proc_list);
1031                 while (NULL != iter) {
1032                         g_proc_list = g_list_remove_link(g_proc_list, iter);
1033                         iter = g_list_first(g_proc_list);
1034                 }
1035         }
1036
1037         if (g_restart_timer != NULL) {
1038                 ecore_timer_del(g_restart_timer);
1039                 g_restart_timer = NULL;
1040         }
1041
1042         vcd_state_e state = vcd_config_get_service_state();
1043         if (VCD_STATE_READY != state) {
1044                 if (VCD_STATE_RECORDING == state) {
1045                         vcd_recorder_stop();
1046                 }
1047                 vcd_engine_recognize_cancel();
1048         }
1049         if (0 != vcd_recorder_destroy()) {
1050                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1051         } else {
1052                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1053         }
1054
1055         if (0 != vcd_engine_agent_release()) {
1056                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1057         } else {
1058                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1059         }
1060
1061         vcd_client_manager_unset_appid();
1062
1063         vcd_config_set_service_state(VCD_STATE_NONE);
1064         vcdc_send_service_state(VCD_STATE_NONE);
1065
1066         SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
1067
1068         return;
1069 }
1070
1071 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1072 {
1073         SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
1074         ecore_main_loop_quit();
1075         return EINA_FALSE;
1076 }
1077
1078 static void __read_proc()
1079 {
1080         DIR *dp = NULL;
1081         struct dirent *dirp = NULL;
1082         int tmp;
1083
1084         GList *iter = NULL;
1085         if (0 < g_list_length(g_proc_list)) {
1086                 iter = g_list_first(g_proc_list);
1087                 while (NULL != iter) {
1088                         g_proc_list = g_list_remove_link(g_proc_list, iter);
1089                         iter = g_list_first(g_proc_list);
1090                 }
1091         }
1092
1093         dp = opendir("/proc");
1094         if (NULL == dp) {
1095                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1096         } else {
1097                 do {
1098                         dirp = readdir(dp);
1099
1100                         if (NULL != dirp) {
1101                                 tmp = atoi(dirp->d_name);
1102                                 if (0 >= tmp)   continue;
1103                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1104                         }
1105                 } while (NULL != dirp);
1106                 closedir(dp);
1107         }
1108         return;
1109 }
1110
1111 static void __vcd_cleanup_client(vcd_client_type_e type)
1112 {
1113         int* client_list = NULL;
1114         int client_count = 0;
1115         int i = 0;
1116         int j = 0;
1117         bool exist = false;
1118         int mgr_pid = -1;
1119         int ret = -1;
1120
1121         if (VCD_CLIENT_TYPE_NORMAL == type) {
1122                 ret = vcd_client_get_list(&client_list, &client_count);
1123         } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1124                 ret = vcd_client_widget_get_list(&client_list, &client_count);
1125         } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1126                 mgr_pid = vcd_client_manager_get_pid();
1127                 client_list = &mgr_pid;
1128                 client_count = 1;
1129                 if (-1 == mgr_pid) {
1130                         SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1131                         return;
1132                 }
1133         }
1134
1135         if (0 == ret || mgr_pid > 0) {
1136                 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1137                 if (NULL != client_list && client_count > 0) {
1138                         for (i = 0; i < client_count; i++) {
1139                                 exist = false;
1140                                 GList *iter = NULL;
1141                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1142                                         iter = g_list_nth(g_proc_list, j);
1143                                         if (NULL != iter) {
1144                                                 if (*(client_list + i) == GPOINTER_TO_INT(iter->data)) {
1145                                                         SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1146                                                         exist = true;
1147                                                         break;
1148                                                 }
1149                                         }
1150                                 }
1151
1152                                 if (false == exist) {
1153                                         SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1154                                         if (VCD_CLIENT_TYPE_NORMAL == type)
1155                                                 vcd_server_finalize(*(client_list + i));
1156                                         else if (VCD_CLIENT_TYPE_WIDGET == type)
1157                                                 vcd_server_widget_finalize(*(client_list + i));
1158                                         else
1159                                                 vcd_server_mgr_finalize(mgr_pid);
1160                                 }
1161                         }
1162                 }
1163                 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1164         }
1165         if (NULL != client_list && -1 == mgr_pid) {
1166                 free(client_list);
1167                 client_list = NULL;
1168         }
1169         return;
1170 }
1171
1172 Eina_Bool vcd_cleanup_client_all(void *data)
1173 {
1174         __read_proc();
1175         
1176         __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1177         __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1178         __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1179
1180 #if 0
1181         if (0 == vcd_client_get_list(&client_list, &client_count)) {
1182                 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up client ");
1183                 if (NULL != client_list && client_count > 0) {
1184                         for (i = 0; i < client_count; i++) {
1185                                 exist = false;
1186                                 GList *iter = NULL;
1187                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1188                                         iter = g_list_nth(g_proc_list, j);
1189                                         if (NULL != iter) {
1190                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1191                                                         SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1192                                                         exist = true;
1193                                                         break;
1194                                                 }
1195                                         }
1196                                 }
1197
1198                                 if (false == exist) {
1199                                         SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1200                                         vcd_server_finalize(client_list[i]);
1201                                 }
1202 #if 0
1203                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1204
1205                                 if (0 == result) {
1206                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1207                                         vcd_server_finalize(client_list[i]);
1208                                 } else if (-1 == result) {
1209                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1210                                 }
1211 #endif
1212                         }
1213                 }
1214                 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1215         }
1216         if (NULL != client_list) {
1217                 free(client_list);
1218                 client_list = NULL;
1219         }
1220
1221         /* If app is in background state, app cannot response message. */
1222         if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1223                 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up widget");
1224                 if (NULL != client_list && client_count > 0) {
1225                         for (i = 0; i < client_count; i++) {
1226                                 exist = false;
1227                                 GList *iter = NULL;
1228                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1229                                         iter = g_list_nth(g_proc_list, j);
1230                                         if (NULL != iter) {
1231                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1232                                                         SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1233                                                         exist = true;
1234                                                         break;
1235                                                 }
1236                                         }
1237                                 }
1238
1239                                 if (false == exist) {
1240                                         SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1241                                         vcd_server_widget_finalize(client_list[i]);
1242                                 }
1243 #if 0
1244                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1245
1246                                 if (0 == result) {
1247                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1248                                         vcd_server_widget_finalize(client_list[i]);
1249                                 } else if (-1 == result) {
1250                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1251                                 }
1252 #endif
1253                         }
1254                 }
1255                 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1256         }
1257
1258         if (NULL != client_list) {
1259                 free(client_list);
1260                 client_list = NULL;
1261         }
1262
1263         /* manager */
1264         exist = false;
1265         GList *iter = NULL;
1266         int mgr_pid = vcd_client_manager_get_pid();
1267         if (0 < mgr_pid) {
1268                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1269                         iter = g_list_nth(g_proc_list, j);
1270                         if (NULL != iter) {
1271                                 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1272                                         SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1273                                         exist = true;
1274                                         break;
1275                                 }
1276                         }
1277                 }
1278
1279                 if (false == exist) {
1280                         SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1281                         vcd_server_mgr_finalize(mgr_pid);
1282                 }
1283         }
1284 #endif
1285         return EINA_TRUE;
1286 }
1287
1288 int vcd_server_get_service_state()
1289 {
1290         return vcd_config_get_service_state();
1291 }
1292
1293 int vcd_server_get_foreground()
1294 {
1295         int pid;
1296         vcd_config_get_foreground(&pid);
1297         return pid;
1298 }
1299
1300
1301 /*
1302 * API for manager
1303 */
1304 int vcd_server_mgr_initialize(int pid)
1305 {
1306         if (false == g_is_engine) {
1307                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1308                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1309                         g_is_engine = false;
1310                         return VCD_ERROR_ENGINE_NOT_FOUND;
1311                 } else {
1312                         g_is_engine = true;
1313                 }
1314         }
1315
1316         /* check if pid is valid */
1317         if (false == vcd_client_manager_is_valid(pid)) {
1318                 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1319                 vcd_server_mgr_cancel();
1320                 vcd_client_manager_unset();
1321         }
1322
1323         /* Add client information to client manager */
1324         if (0 != vcd_client_manager_set(pid)) {
1325                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1326                 return VCD_ERROR_OPERATION_FAILED;
1327         }
1328
1329         if (0 != vcdc_send_manager_pid(pid))
1330                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1331
1332         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1333
1334         return VCD_ERROR_NONE;
1335 }
1336
1337 int vcd_server_mgr_finalize(int pid)
1338 {
1339         /* check if pid is valid */
1340         if (false == vcd_client_manager_is_valid(pid)) {
1341                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1342                 return VCD_ERROR_INVALID_PARAMETER;
1343         }
1344
1345         /* Cancel recognition */
1346         vcd_server_mgr_cancel();
1347
1348         if (0 != vcdc_send_manager_pid(-1))
1349                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1350
1351         /* Remove manager information */
1352         if (0 != vcd_client_manager_unset()) {
1353                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1354         }
1355
1356         if (0 == vcd_client_get_ref_count()) {
1357                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1358                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1359         }
1360
1361         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1362
1363         return VCD_ERROR_NONE;
1364 }
1365
1366 int vcd_server_mgr_set_command(int pid)
1367 {
1368         if (0 != vcd_client_manager_set_command(pid)) {
1369                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1370                 return VCD_ERROR_INVALID_PARAMETER;
1371         }
1372         return VCD_ERROR_NONE;
1373 }
1374
1375 int vcd_server_mgr_unset_command(int pid)
1376 {
1377         if (0 != vcd_client_manager_unset_command(pid)) {
1378                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1379                 return VCD_ERROR_INVALID_PARAMETER;
1380         }
1381         return VCD_ERROR_NONE;
1382 }
1383
1384 int vcd_server_mgr_set_demandable_client(int pid)
1385 {
1386         /* check if pid is valid */
1387         if (false == vcd_client_manager_is_valid(pid)) {
1388                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1389                 return VCD_ERROR_INVALID_PARAMETER;
1390         }
1391
1392         GSList* client_list = NULL;
1393         if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1394                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1395                 return VCD_ERROR_OPERATION_FAILED;
1396         }
1397
1398         /* Save client list */
1399         if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1400                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1401                 return VCD_ERROR_OPERATION_FAILED;
1402         }
1403
1404         return VCD_ERROR_NONE;
1405 }
1406
1407 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1408 {
1409         /* check if pid is valid */
1410         if (false == vcd_client_manager_is_valid(pid)) {
1411                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1412                 return VCD_ERROR_INVALID_PARAMETER;
1413         }
1414
1415         int ret = 0;
1416         vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
1417         int rate = 16000;
1418         int channel = 1;
1419
1420         ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1421         if (0 != ret) {
1422                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1423                 return VCD_ERROR_OPERATION_FAILED;
1424         }
1425
1426         ret = vcd_recorder_set(audio_type, type, rate, channel);
1427         if (0 != ret) {
1428                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1429                 return VCD_ERROR_OPERATION_FAILED;
1430         }
1431
1432         return VCD_ERROR_NONE;
1433 }
1434
1435 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1436 {
1437         /* check if pid is valid */
1438         if (false == vcd_client_manager_is_valid(pid)) {
1439                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1440                 return VCD_ERROR_INVALID_PARAMETER;
1441         }
1442
1443         int ret = vcd_recorder_get(audio_type);
1444         if (0 != ret) {
1445                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1446                 return VCD_ERROR_OPERATION_FAILED;
1447         }
1448
1449         return VCD_ERROR_NONE;
1450 }
1451
1452 int vcd_server_mgr_set_client_info(int pid)
1453 {
1454         /* check if pid is valid */
1455         if (false == vcd_client_manager_is_valid(pid)) {
1456                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1457                 return VCD_ERROR_INVALID_PARAMETER;
1458         }
1459
1460         int ret = vcd_client_save_client_info();
1461         if (0 != ret) {
1462                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1463                 return VCD_ERROR_OPERATION_FAILED;
1464         }
1465
1466         return VCD_ERROR_NONE;
1467 }
1468
1469 static int __start_internal_recognition()
1470 {
1471         int ret;
1472
1473         if (0 != vcd_client_command_collect_command()) {
1474                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
1475                 /* Send error cb to manager */
1476                 int pid = vcd_client_widget_get_foreground_pid();
1477                 if (-1 != pid)
1478                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1479                 return VCD_ERROR_OPERATION_FAILED;
1480         }
1481
1482         /* 3. Set command to engine */
1483         ret = vcd_engine_set_commands();
1484         if (0 != ret) {
1485                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set commands : %d", ret);
1486                 /* Send error cb to manager */
1487                 int pid = vcd_client_widget_get_foreground_pid();
1488                 if (-1 != pid)
1489                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1490                 return VCD_ERROR_OPERATION_FAILED;
1491         }
1492
1493         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1494
1495         bool stop_by_silence = true;
1496         if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1497                 stop_by_silence = false;
1498         }
1499
1500         vcd_client_manager_set_result_text(NULL);
1501
1502         /* 4. start recognition */
1503         ret = vcd_engine_recognize_start(stop_by_silence);
1504         if (0 != ret) {
1505                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1506                 /* Send error cb to manager */
1507                 int pid = vcd_client_widget_get_foreground_pid();
1508                 if (-1 != pid)
1509                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1510                 return VCD_ERROR_OPERATION_FAILED;
1511         }
1512
1513         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1514
1515 #if 1
1516         /* 5. recorder start */
1517         ret = vcd_recorder_start();
1518         if (0 != ret) {
1519                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1520                 vcd_engine_recognize_cancel();
1521                 /* Send error cb to manager */
1522                 int pid = vcd_client_widget_get_foreground_pid();
1523                 if (-1 != pid)
1524                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1525                 return ret;
1526         }
1527 #endif
1528
1529         vcd_config_set_service_state(VCD_STATE_RECORDING);
1530         vcdc_send_service_state(VCD_STATE_RECORDING);
1531
1532         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1533
1534         return 0;
1535 }
1536
1537 static Eina_Bool __vcd_request_show_tooltip(void *data)
1538 {
1539         int pid = vcd_client_widget_get_foreground_pid();
1540         if (-1 != pid) {
1541                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1542                 vcdc_send_show_tooltip(pid, (bool)data);
1543         }
1544
1545         return EINA_FALSE;
1546 }
1547
1548 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1549 {
1550         /* 1. check current state */
1551         vcd_state_e state = vcd_config_get_service_state();
1552
1553         if (VCD_STATE_READY != state) {
1554                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1555                 return VCD_ERROR_INVALID_STATE;
1556         }
1557         if (-1 == vcd_client_manager_get_pid()) {
1558                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1559                 return VCD_ERROR_OPERATION_FAILED;
1560         }
1561
1562         SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1563         vcd_client_set_recognition_mode(recognition_mode);
1564
1565         if (false == exclusive_cmd) {
1566                 vcd_client_update_foreground_pid();
1567                 /* Notify show tooltip */
1568                 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1569                         int pid = vcd_client_widget_get_foreground_pid();
1570                         if (-1 != pid) {
1571                                 SLOG(LOG_INFO, TAG_VCD, "[Server] Request tooltip show and widget command");
1572                                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1573                                 return 0;
1574                         }
1575                 }
1576         } else {
1577                 vcd_client_manager_set_exclusive(exclusive_cmd);
1578         }
1579
1580         int fg_pid = -1;
1581         if (true == start_by_client) {
1582                 /* Get foreground pid */
1583                 if (0 != vcd_config_get_foreground(&fg_pid)) {
1584                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1585                 }
1586
1587                 /* Set client exclusive option */
1588                 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1589                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1590                 }
1591         }
1592
1593         int ret = __start_internal_recognition();
1594         if (0 != ret) {
1595                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1596                 return ret;
1597         }
1598
1599         if (true == start_by_client) {
1600                 vcd_client_unset_exclusive_command(fg_pid);
1601         }
1602
1603         return VCD_ERROR_NONE;
1604 }
1605
1606 int vcd_server_mgr_stop()
1607 {
1608         /* 1. Check current state is recording */
1609         if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1610                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1611                 return VCD_ERROR_INVALID_STATE;
1612         }
1613         if (-1 == vcd_client_manager_get_pid()) {
1614                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1615                 return VCD_ERROR_OPERATION_FAILED;
1616         }
1617
1618 #if 1
1619         /* 2. Stop recorder */
1620         vcd_recorder_stop();
1621 #endif
1622
1623         /* 3. Stop engine recognition */
1624         int ret = vcd_engine_recognize_stop();
1625         if (0 != ret) {
1626                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1627         }
1628
1629         /* 4. Set original mode */
1630         vcd_config_set_service_state(VCD_STATE_PROCESSING);
1631         vcdc_send_service_state(VCD_STATE_PROCESSING);
1632
1633         return VCD_ERROR_NONE;
1634 }
1635
1636 int vcd_server_mgr_cancel()
1637 {
1638         if (-1 == vcd_client_manager_get_pid()) {
1639                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1640                 return VCD_ERROR_OPERATION_FAILED;
1641         }
1642
1643         if (g_restart_timer != NULL) {
1644                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1645                 ecore_timer_del(g_restart_timer);
1646                 g_restart_timer = NULL;
1647         }
1648
1649         /* 1. Check current state */
1650         vcd_state_e state = vcd_config_get_service_state();
1651         if (VCD_STATE_READY == state) {
1652                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is READY");
1653                 vcd_recorder_stop();
1654                 vcdc_send_service_state(VCD_STATE_READY);
1655                 return VCD_ERROR_NONE;
1656         }
1657
1658 #if 1
1659         /* 2. Stop recorder */
1660         vcd_recorder_stop();
1661 #endif
1662
1663         /* 3. Cancel engine */
1664         int ret = vcd_engine_recognize_cancel();
1665         if (0 != ret) {
1666                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1667         }
1668
1669         if (false == vcd_client_manager_get_exclusive()) {
1670                 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1671                         int pid = vcd_client_widget_get_foreground_pid();
1672                         if (-1 != pid) {
1673                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1674                                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1675                         }
1676                 }
1677         } else {
1678                 vcd_client_manager_set_exclusive(false);
1679         }
1680
1681         /* 4. Set state */
1682         vcd_config_set_service_state(VCD_STATE_READY);
1683         vcdc_send_service_state(VCD_STATE_READY);
1684
1685         return VCD_ERROR_NONE;
1686 }
1687
1688
1689 int vcd_server_mgr_result_select()
1690 {
1691         __vcd_send_selected_result(NULL);
1692
1693         return VCD_ERROR_NONE;
1694 }
1695
1696 int vcd_server_mgr_set_domain(int pid, const char* domain)
1697 {
1698         /* check if pid is valid */
1699         if (false == vcd_client_manager_is_valid(pid)) {
1700                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1701                 return VCD_ERROR_INVALID_PARAMETER;
1702         }
1703
1704         vcd_state_e state = vcd_config_get_service_state();
1705         if (VCD_STATE_READY != state) {
1706                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1707                 return VCD_ERROR_INVALID_STATE;
1708         }
1709
1710         /* Set domain to engine */
1711         int ret = vcd_engine_set_domain(pid, domain);
1712         if (0 != ret) {
1713                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1714         } else {
1715                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1716         }
1717
1718         return ret;
1719 }
1720
1721 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1722 {
1723         /* check if pid is valid */
1724         if (false == vcd_client_manager_is_valid(pid)) {
1725                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1726                 return VCD_ERROR_INVALID_PARAMETER;
1727         }
1728
1729         vcd_state_e state = vcd_config_get_service_state();
1730         if (VCD_STATE_READY != state) {
1731                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1732                 return VCD_ERROR_INVALID_STATE;
1733         }
1734
1735         /* Set private data to engine */
1736         int ret = vcd_engine_set_private_data(pid, key, data);
1737         if (0 != ret) {
1738                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1739         } else {
1740                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1741         }
1742
1743         return ret;
1744 }
1745
1746 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1747 {
1748         /* check if pid is valid */
1749         if (false == vcd_client_manager_is_valid(pid)) {
1750                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1751                 return VCD_ERROR_INVALID_PARAMETER;
1752         }
1753         vcd_state_e state = vcd_config_get_service_state();
1754         if (VCD_STATE_READY != state) {
1755                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1756                 return VCD_ERROR_INVALID_STATE;
1757         }
1758
1759         /* Get private data to engine */
1760         int ret = vcd_engine_get_private_data(pid, key, data);
1761         if (0 != ret) {
1762                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1763         } else {
1764                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1765         }
1766
1767         return ret;
1768 }
1769
1770 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1771 {
1772         int ret = -1;
1773
1774         /* check if pid is valid */
1775         if (false == vcd_client_manager_is_valid(pid)) {
1776                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1777                 return VCD_ERROR_INVALID_PARAMETER;
1778         }
1779
1780         vcd_state_e state = vcd_config_get_service_state();
1781         if (VCD_STATE_READY != state) {
1782                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1783                 return VCD_ERROR_INVALID_STATE;
1784         }
1785
1786         /* Reqeust do action to engine */
1787         if (VCD_SEND_EVENT_TYPE_TEXT == type)
1788                 ret = vcd_engine_process_text(pid, action);
1789         else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1790                 ret = vcd_engine_process_list_event(pid, action);
1791         else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1792                 ret = vcd_engine_process_haptic_event(pid, action);
1793
1794         if (0 != ret) {
1795                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1796         } else {
1797                 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1798                 vcdc_send_service_state(VCD_STATE_PROCESSING);
1799                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1800         }
1801
1802         return ret;
1803 }
1804
1805 int vcd_server_mgr_enable_command_type(int pid, int cmd_type)
1806 {
1807         int ret = -1;
1808
1809         /* check if pid is valid */
1810         if (false == vcd_client_manager_is_valid(pid)) {
1811                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1812                 return VCD_ERROR_INVALID_PARAMETER;
1813         }
1814
1815         vcd_state_e state = vcd_config_get_service_state();
1816         if (VCD_STATE_READY != state) {
1817                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1818                 return VCD_ERROR_INVALID_STATE;
1819         }
1820
1821         ret = vcd_config_enable_command_type(cmd_type);
1822         if (0 != ret) {
1823                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to enable command type");
1824         } else {
1825                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Enable command type(%d)", cmd_type);
1826         }
1827
1828         return ret;
1829 }
1830
1831 int vcd_server_mgr_disable_command_type(int pid, int cmd_type)
1832 {
1833         int ret = -1;
1834
1835         /* check if pid is valid */
1836         if (false == vcd_client_manager_is_valid(pid)) {
1837                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1838                 return VCD_ERROR_INVALID_PARAMETER;
1839         }
1840
1841         vcd_state_e state = vcd_config_get_service_state();
1842         if (VCD_STATE_READY != state) {
1843                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1844                 return VCD_ERROR_INVALID_STATE;
1845         }
1846
1847         ret = vcd_config_disable_command_type(cmd_type);
1848         if (0 != ret) {
1849                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to disable command type");
1850         } else {
1851                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Disable command type(%d)", cmd_type);
1852         }
1853
1854         return ret;
1855 }
1856
1857 /*
1858 * VC Server Functions for Client
1859 */
1860 int vcd_server_initialize(int pid)
1861 {
1862         if (false == g_is_engine) {
1863                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1864                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1865                         g_is_engine = false;
1866                         return VCD_ERROR_ENGINE_NOT_FOUND;
1867                 } else {
1868                         g_is_engine = true;
1869                 }
1870         }
1871
1872         if (false == vcd_engine_is_available_engine()) {
1873                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1874                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1875                         return VCD_ERROR_ENGINE_NOT_FOUND;
1876                 }
1877         }
1878
1879         /* check if pid is valid */
1880         if (true == vcd_client_is_available(pid)) {
1881                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1882                 return VCD_ERROR_INVALID_PARAMETER;
1883         }
1884
1885         /* Add client information to client manager */
1886         if (0 != vcd_client_add(pid)) {
1887                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1888                 return VCD_ERROR_OPERATION_FAILED;
1889         }
1890
1891         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1892
1893         return VCD_ERROR_NONE;
1894 }
1895
1896 int vcd_server_finalize(int pid)
1897 {
1898         /* check if pid is valid */
1899         if (false == vcd_client_is_available(pid)) {
1900                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1901                 return VCD_ERROR_INVALID_PARAMETER;
1902         }
1903
1904         /* Remove client information */
1905         if (0 != vcd_client_delete(pid)) {
1906                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1907         }
1908
1909         if (0 == vcd_client_get_ref_count()) {
1910                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1911                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1912         }
1913
1914         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1915
1916         return VCD_ERROR_NONE;
1917 }
1918
1919 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1920 {
1921         /* check if pid is valid */
1922         if (false == vcd_client_is_available(pid)) {
1923                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1924                 return VCD_ERROR_INVALID_PARAMETER;
1925         }
1926
1927         if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1928                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1929                 return VCD_ERROR_OPERATION_FAILED;
1930         }
1931
1932         return 0;
1933 }
1934
1935 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1936 {
1937         /* check if pid is valid */
1938         if (false == vcd_client_is_available(pid)) {
1939                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1940                 return VCD_ERROR_INVALID_PARAMETER;
1941         }
1942
1943         if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1944                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1945                 return VCD_ERROR_OPERATION_FAILED;
1946         }
1947
1948         return 0;
1949 }
1950
1951 int vcd_server_set_foreground(int pid, bool value)
1952 {
1953         /* check if pid is valid */
1954         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1955                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1956                 return VCD_ERROR_INVALID_PARAMETER;
1957         }
1958
1959         if (0 != vcd_config_set_foreground(pid, value)) {
1960                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1961                 return VCD_ERROR_OPERATION_FAILED;
1962         }
1963
1964         return 0;
1965 }
1966
1967 static int __vcd_server_launch_manager_app()
1968 {
1969         int ret = -1;
1970         bool running = false;
1971         char* appid = NULL;
1972
1973         ret = vcd_client_manager_get_appid(&appid);
1974         if (0 != ret || NULL == appid) {
1975                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1976                 return VCD_ERROR_OPERATION_FAILED;
1977         }
1978         ret = app_manager_is_running(appid, &running);
1979         if (0 != ret) {
1980                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1981                 free(appid);
1982                 appid = NULL;
1983                 return VCD_ERROR_OPERATION_FAILED;
1984         }
1985         if (false == running) {
1986                 int tmp_ret = __vcd_is_package_installed(appid);
1987                 if (false == tmp_ret) {
1988                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1989                 } else {
1990                         ret = __vcd_activate_app_by_appcontrol(appid);
1991                         if (0 != ret) {
1992                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
1993                                 free(appid);
1994                                 appid = NULL;
1995                                 return ret;
1996                         }
1997                         SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
1998                 }
1999         } else {
2000                 ret = __vcd_resume_app(appid);
2001                 if (0 != ret) {
2002                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
2003                         free(appid);
2004                         appid = NULL;
2005                         return ret;
2006                 }
2007                 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
2008         }
2009
2010         free(appid);
2011         appid = NULL;
2012
2013         return VCD_ERROR_NONE;
2014 }
2015
2016 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
2017 {
2018         /* check if pid is valid */
2019         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2020                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2021                 return VCD_ERROR_INVALID_PARAMETER;
2022         }
2023
2024         int ret = __vcd_server_launch_manager_app();
2025         if (0 != ret) {
2026                 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);
2027                 return ret;
2028         }
2029
2030         ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2031         if (0 != ret) {
2032                 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);
2033                 return ret;
2034         }
2035
2036         return 0;
2037 }
2038
2039 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
2040 {
2041         /* check if pid is valid */
2042         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2043                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2044                 return VCD_ERROR_INVALID_PARAMETER;
2045         }
2046
2047         /* check if system command is valid */
2048         if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
2049                 *is_sys_cmd_valid = true;
2050         else
2051                 *is_sys_cmd_valid = false;
2052
2053         return 0;
2054 }
2055
2056 #if 0
2057 int vcd_server_set_exclusive_command(int pid, bool value)
2058 {
2059         /* check if pid is valid */
2060         if (false == vcd_client_is_available(pid)) {
2061                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2062                 return VCD_ERROR_INVALID_PARAMETER;
2063         }
2064
2065         if (true == value) {
2066                 if (0 != vcd_client_set_exclusive_command(pid)) {
2067                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
2068                         return VCD_ERROR_OPERATION_FAILED;
2069                 }
2070         } else {
2071                 if (0 != vcd_client_unset_exclusive_command(pid)) {
2072                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
2073                         return VCD_ERROR_OPERATION_FAILED;
2074                 }
2075         }
2076
2077         return 0;
2078 }
2079
2080 int vcd_server_request_start(int pid, bool stop_by_silence)
2081 {
2082         /* check if pid is valid */
2083         if (false == vcd_client_is_available(pid)) {
2084                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
2085                 return VCD_ERROR_INVALID_PARAMETER;
2086         }
2087
2088         int ret;
2089         /* Check current state */
2090         vcd_state_e state = vcd_config_get_service_state();
2091
2092         /* Service state should be ready */
2093         if (VCD_STATE_READY != state) {
2094                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2095                 return VCD_ERROR_INVALID_STATE;
2096         }
2097
2098         if (-1 != vcd_client_manager_get_pid()) {
2099                 /* Check current pid is valid */
2100                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2101                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2102                         return VCD_ERROR_INVALID_PARAMETER;
2103                 }
2104         }
2105
2106         ret = vcd_server_mgr_start(stop_by_silence, false);
2107         if (0 != ret) {
2108                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2109                 return VCD_ERROR_INVALID_PARAMETER;
2110         }
2111
2112         return 0;
2113 }
2114
2115 int vcd_server_request_stop(int pid)
2116 {
2117         int ret;
2118         /* Check current state */
2119         vcd_state_e state = vcd_config_get_service_state();
2120
2121         /* Service state should be ready */
2122         if (VCD_STATE_RECORDING != state) {
2123                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2124                 return VCD_ERROR_INVALID_STATE;
2125         }
2126
2127         if (-1 != vcd_client_manager_get_pid()) {
2128                 /* Check current pid is valid */
2129                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2130                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2131                         return VCD_ERROR_INVALID_PARAMETER;
2132                 }
2133         }
2134
2135         ret = vcd_server_mgr_stop();
2136         if (0 != ret) {
2137                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2138                 return VCD_ERROR_OPERATION_FAILED;
2139         }
2140
2141         return VCD_ERROR_NONE;
2142 }
2143
2144 int vcd_server_request_cancel(int pid)
2145 {
2146         int ret;
2147         /* Check current state */
2148         vcd_state_e state = vcd_config_get_service_state();
2149
2150         /* Service state should be recording or processing */
2151         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2152                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2153                 return VCD_ERROR_INVALID_STATE;
2154         }
2155
2156         if (-1 != vcd_client_manager_get_pid()) {
2157                 /* Check current pid is valid */
2158                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2159                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2160                         return VCD_ERROR_INVALID_PARAMETER;
2161                 }
2162         }
2163
2164         ret = vcd_server_mgr_cancel();
2165         if (0 != ret) {
2166                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2167                 return VCD_ERROR_OPERATION_FAILED;
2168         }
2169
2170         return VCD_ERROR_NONE;
2171 }
2172 #endif
2173
2174 /*
2175 * VC Server Functions for Widget lib
2176 */
2177 int vcd_server_widget_initialize(int pid)
2178 {
2179         if (false == g_is_engine) {
2180                 if (0 != vcd_engine_agent_initialize_current_engine()) {
2181                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2182                         g_is_engine = false;
2183                         return VCD_ERROR_ENGINE_NOT_FOUND;
2184                 } else {
2185                         g_is_engine = true;
2186                 }
2187         }
2188
2189         if (false == vcd_engine_is_available_engine()) {
2190                 if (0 != vcd_engine_agent_initialize_current_engine()) {
2191                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2192                         return VCD_ERROR_ENGINE_NOT_FOUND;
2193                 }
2194         }
2195
2196         /* check if pid is valid */
2197         if (true == vcd_client_widget_is_available(pid)) {
2198                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2199                 return VCD_ERROR_NONE;
2200         }
2201
2202         /* Add client information to client manager */
2203         if (0 != vcd_client_widget_add(pid)) {
2204                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2205                 return VCD_ERROR_OPERATION_FAILED;
2206         }
2207
2208         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2209
2210         return VCD_ERROR_NONE;
2211 }
2212
2213 int vcd_server_widget_finalize(int pid)
2214 {
2215         /* check if pid is valid */
2216         if (false == vcd_client_widget_is_available(pid)) {
2217                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2218                 return VCD_ERROR_INVALID_PARAMETER;
2219         }
2220
2221         /* Remove client information */
2222         if (0 != vcd_client_widget_delete(pid)) {
2223                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2224         }
2225
2226         if (0 == vcd_client_get_ref_count()) {
2227                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
2228                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2229         }
2230
2231         return VCD_ERROR_NONE;
2232 }
2233
2234 int vcd_server_widget_start_recording(int pid, bool widget_command)
2235 {
2236         /* check if pid is valid */
2237         if (false == vcd_client_widget_is_available(pid)) {
2238                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2239                 return VCD_ERROR_INVALID_PARAMETER;
2240         }
2241
2242         if (true == widget_command) {
2243                 vcd_client_widget_set_command(pid);
2244                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2245         } else {
2246                 vcd_client_widget_unset_command(pid);
2247                 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2248         }
2249
2250         int ret = __start_internal_recognition();
2251         if (0 != ret) {
2252                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2253                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2254         }
2255
2256         return 0;
2257 }
2258
2259 int vcd_server_widget_start(int pid, bool stop_by_silence)
2260 {
2261         /* check if pid is valid */
2262         int fore_pid = vcd_client_widget_get_foreground_pid();
2263         if (pid != fore_pid) {
2264                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2265                 return VCD_ERROR_INVALID_PARAMETER;
2266         }
2267
2268         /* Check current state */
2269         vcd_state_e state = vcd_config_get_service_state();
2270
2271         /* Service state should be ready */
2272         if (VCD_STATE_READY != state) {
2273                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2274                 return VCD_ERROR_INVALID_STATE;
2275         }
2276
2277         vcd_client_set_slience_detection(stop_by_silence);
2278
2279         /* Notify show tooltip */
2280         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2281
2282         return 0;
2283 }
2284
2285 int vcd_server_widget_stop(int pid)
2286 {
2287         /* check if pid is valid */
2288         int fore_pid = vcd_client_widget_get_foreground_pid();
2289         if (pid != fore_pid) {
2290                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2291                 return VCD_ERROR_INVALID_PARAMETER;
2292         }
2293
2294         int ret;
2295         /* Check current state */
2296         vcd_state_e state = vcd_config_get_service_state();
2297
2298         /* Service state should be recording */
2299         if (VCD_STATE_RECORDING != state) {
2300                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2301                 return VCD_ERROR_INVALID_STATE;
2302         }
2303
2304         ret = vcd_server_mgr_stop();
2305         if (0 != ret) {
2306                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2307                 return VCD_ERROR_OPERATION_FAILED;
2308         }
2309
2310         return VCD_ERROR_NONE;
2311 }
2312
2313 int vcd_server_widget_cancel(int pid)
2314 {
2315         /* check if pid is valid */
2316         int fore_pid = vcd_client_widget_get_foreground_pid();
2317         if (pid != fore_pid) {
2318                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2319                 return VCD_ERROR_INVALID_PARAMETER;
2320         }
2321
2322         int ret;
2323         /* Check current state */
2324         vcd_state_e state = vcd_config_get_service_state();
2325
2326         /* Service state should be recording or processing */
2327         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2328                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2329                 return VCD_ERROR_INVALID_STATE;
2330         }
2331
2332         ret = vcd_server_mgr_cancel();
2333         if (0 != ret) {
2334                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2335                 return ret;
2336         }
2337
2338         return VCD_ERROR_NONE;
2339 }
2340
2341 int vcd_server_widget_enable_asr_result(int pid, bool enable)
2342 {
2343         int ret;
2344         ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
2345         if (0 != ret) {
2346                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);
2347         }
2348
2349         return ret;
2350 }
2351
2352 int vcd_server_set_language(const char* language)
2353 {
2354         int ret = VCD_ERROR_NONE;
2355
2356         ret = vcd_config_set_default_language(language);
2357         if (0 != ret) {
2358                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to set language : %d", ret);
2359                 return ret;
2360         }
2361
2362         ret = vcd_engine_set_current_language(language);
2363         if (0 != ret) {
2364                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to set language : %d", ret);
2365                 return ret;
2366         }
2367
2368         return ret;
2369 }