Rearrange the scope of each temporary variables
[platform/core/uifw/voice-control.git] / server / vcd_client_data.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_manager.h>
18 #include <aul.h>
19 #include <aul_window.h>
20
21 #include "vcd_client_data.h"
22 #include "vcd_config.h"
23 #include "vcd_main.h"
24
25
26 /* Client list */
27 static GSList* g_client_list = NULL;
28
29 static GSList* g_widget_list = NULL;
30
31 static manager_info_s g_manager;
32
33 /* Client IPC info list */
34 static GSList* g_client_tidl_info_list = NULL;
35
36 /* Manager IPC info */
37 static manager_tidl_info_s* g_mgr_tidl_info = NULL;
38
39 /* Widget IPC info */
40 static GSList* g_widget_tidl_info_list = NULL;
41
42 /* Setting IPC info list */
43 static GSList* g_setting_tidl_info_list = NULL;
44
45 /* Command list */
46 static current_commands_list_s g_cur_cmd_list;
47
48 /* Demandable client list */
49 static GSList* g_demandable_client = NULL;
50
51 /* Runtime info */
52 static bool g_silence_detection;
53 static vcd_recognition_mode_e g_recognition_mode;
54 static char* g_result_text = NULL;
55 static bool g_is_waiting_recording = false;
56 static int g_waiting_recording_pid = -1;
57
58 /* Function definitions */
59 widget_info_s* __widget_get_element(int pid);
60
61 vc_client_info_s* __client_get_element(int pid);
62
63
64 int vcd_client_manager_set(int pid)
65 {
66         // Get appid by pid using app control
67         char* appid = NULL;
68         int ret = app_manager_get_app_id(pid, &appid);
69         if (0 != ret || NULL == appid) {
70                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
71                 return VCD_ERROR_OPERATION_FAILED;
72         }
73
74         if (-1 != g_manager.pid && NULL != g_manager.appid && 0 == strncmp(g_manager.appid, appid, strlen(g_manager.appid))) {
75                 SLOG(LOG_WARN, TAG_VCD, "Same manager has already registered. It doesn't need to set manager again.");
76                 if (appid)
77                         free(appid);
78                 return VCD_ERROR_NONE;
79         }
80
81         g_manager.pid = pid;
82         g_manager.manager_cmd = false;
83         g_manager.exclusive_cmd_option = false;
84         g_manager.appid = NULL;
85
86         g_manager.appid = strdup(appid);
87
88         free(appid);
89         appid = NULL;
90         return VCD_ERROR_NONE;
91 }
92
93 int vcd_client_manager_unset()
94 {
95         g_manager.pid = -1;
96         g_manager.manager_cmd = false;
97         g_manager.exclusive_cmd_option = false;
98
99         return VCD_ERROR_NONE;
100 }
101
102 int vcd_client_manager_unset_appid()
103 {
104         if (NULL != g_manager.appid) {
105                 free(g_manager.appid);
106                 g_manager.appid = NULL;
107         }
108         return VCD_ERROR_NONE;
109 }
110
111 bool vcd_client_manager_is_valid(int pid)
112 {
113         if (-1 == g_manager.pid || pid == g_manager.pid) {
114                 return true;
115         }
116         return false;
117 }
118
119 int vcd_client_manager_set_command(int pid)
120 {
121         if (pid != g_manager.pid) {
122                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
123                 return VCD_ERROR_INVALID_PARAMETER;
124         }
125         g_manager.manager_cmd = true;
126         return VCD_ERROR_NONE;
127 }
128
129 int vcd_client_manager_unset_command(int pid)
130 {
131         if (pid != g_manager.pid) {
132                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
133                 return VCD_ERROR_INVALID_PARAMETER;
134         }
135         g_manager.manager_cmd = false;
136         return VCD_ERROR_NONE;
137 }
138
139 bool vcd_client_manager_is_system_command_valid(int pid)
140 {
141         if (pid != g_manager.pid) {
142                 SLOG(LOG_WARN, TAG_VCD, "[Client Data WARNING] pid(%d) is NOT valid", pid);
143                 return false;
144         }
145
146         if (true == g_manager.manager_cmd)
147                 return true;
148
149         return false;
150 }
151
152 int vcd_client_manager_set_demandable_client(int pid, GSList* client_list)
153 {
154         if (0 != g_slist_length(g_demandable_client)) {
155                 /* release data */
156                 GSList *iter = NULL;
157                 vc_demandable_client_s* temp_client;
158                 iter = g_slist_nth(g_demandable_client, 0);
159
160                 while (NULL != iter) {
161                         temp_client = iter->data;
162
163                         if (NULL != temp_client) {
164                                 if (NULL != temp_client->appid)         free(temp_client->appid);
165                                 free(temp_client);
166                         }
167
168                         iter = g_slist_next(iter);
169                 }
170                 g_demandable_client = NULL;
171         }
172
173         g_demandable_client = client_list;
174
175         return VCD_ERROR_NONE;
176 }
177
178 bool vcd_client_manager_check_demandable_client(int pid)
179 {
180         if (0 == g_slist_length(g_demandable_client)) {
181                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] All client is available to request start");
182                 return true;
183         }
184
185         /* Check demandable appid */
186         char appid[1024] = {0, };
187         if (0 == aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
188                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] %s(%d) requests start", appid, pid);
189         } else {
190                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] daemon(%d) requests start", pid);
191         }
192
193         /* Compare appid */
194         GSList *iter = NULL;
195         vc_demandable_client_s* temp_client;
196         iter = g_slist_nth(g_demandable_client, 0);
197
198         while (NULL != iter) {
199                 temp_client = iter->data;
200
201                 if (NULL != temp_client) {
202
203                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] demandable appid(%s)", temp_client->appid);
204
205                         if (NULL != temp_client->appid) {
206                                 if (0 == strcmp(temp_client->appid, appid)) {
207                                         SLOG(LOG_INFO, TAG_VCD, "[Client Data] pid(%d) is available", pid);
208                                         return true;
209                                 }
210                         } else {
211                                 if (0 == strlen(appid)) {
212                                         SLOG(LOG_INFO, TAG_VCD, "[Client Data] pid(%d) is available", pid);
213                                         return true;
214                                 }
215                         }
216                 }
217
218                 iter = g_slist_next(iter);
219         }
220
221         return false;
222 }
223
224 bool vcd_client_manager_get_exclusive()
225 {
226         return g_manager.exclusive_cmd_option;
227 }
228
229 int vcd_client_manager_set_exclusive(bool value)
230 {
231         g_manager.exclusive_cmd_option = value;
232         return VCD_ERROR_NONE;
233 }
234
235 int vcd_client_manager_get_pid()
236 {
237         return g_manager.pid;
238 }
239
240 int vcd_client_manager_get_appid(char** appid)
241 {
242         if (NULL != g_manager.appid)
243                 *appid = strdup(g_manager.appid);
244
245         return VCD_ERROR_NONE;
246 }
247
248 int vcd_client_manager_set_result_text(const char* result)
249 {
250         if (NULL != g_result_text) {
251                 free(g_result_text);
252                 g_result_text = NULL;
253         }
254
255         if (NULL != result) {
256                 g_result_text = strdup(result);
257         }
258
259         return VCD_ERROR_NONE;
260 }
261
262 char* vcd_client_manager_get_result_text()
263 {
264         return g_result_text;
265 }
266
267 int vcd_client_manager_create_tidl_info()
268 {
269         /*Check already created*/
270         if (NULL != g_mgr_tidl_info) {
271                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Manager tidl info pid is already registered");
272                 return VCD_ERROR_NONE;
273         }
274
275         SLOG(LOG_INFO, TAG_VCD, "[Client Data] There is no manager tidl info. Create new one.");
276         g_mgr_tidl_info = (manager_tidl_info_s*)calloc(1, sizeof(manager_tidl_info_s));
277         if (NULL == g_mgr_tidl_info) {
278                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
279                 return VCD_ERROR_OUT_OF_MEMORY;
280         }
281
282         g_mgr_tidl_info->notify_cb = NULL;
283         g_mgr_tidl_info->notify_cb_user_data = NULL;
284         g_mgr_tidl_info->send_buffer_cb = NULL;
285         g_mgr_tidl_info->send_buffer_cb_user_data = NULL;
286
287         g_mgr_tidl_info->connected = false;
288         g_mgr_tidl_info->connection_requesting = false;
289         g_mgr_tidl_info->rpc_h = NULL;
290
291         SLOG(LOG_INFO, TAG_VCD, "[Client Data SUCCESS] Add new manager tidl info.");
292
293         return VCD_ERROR_NONE;
294 }
295
296 int vcd_client_manager_set_tidl_notify_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_notify_cb_h callback, void* user_data)
297 {
298         if (NULL == g_mgr_tidl_info) {
299                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no manager tidl info");
300                 return VCD_ERROR_INVALID_PARAMETER;
301         }
302
303         int ret = -1;
304         ret = rpc_port_stub_vcd_mgr_stub_vc_mgr_notify_cb_clone(callback, &(g_mgr_tidl_info->notify_cb));
305         if (0 != ret) {
306                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to clone notify callback. ret(%d)", ret);
307         } else {
308                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to clone notify callback. ret(%d)", ret);
309         }
310         g_mgr_tidl_info->notify_cb_user_data = user_data;
311
312         return VCD_ERROR_NONE;
313 }
314
315 int vcd_client_manager_unset_tidl_notify_cb()
316 {
317         if (NULL == g_mgr_tidl_info) {
318                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no manager tidl info");
319                 return VCD_ERROR_INVALID_PARAMETER;
320         }
321
322         int ret = -1;
323         ret = rpc_port_stub_vcd_mgr_stub_vc_mgr_notify_cb_destroy(g_mgr_tidl_info->notify_cb);
324         if (0 != ret) {
325                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to destroy notify callback. ret(%d)", ret);
326         } else {
327                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to destroy notify callback. ret(%d)", ret);
328         }
329         g_mgr_tidl_info->notify_cb = NULL;
330         g_mgr_tidl_info->notify_cb_user_data = NULL;
331
332         return VCD_ERROR_NONE;
333 }
334
335 int vcd_client_manager_set_tidl_send_buffer_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_send_buffer_cb_h callback, void* user_data)
336 {
337         if (NULL == g_mgr_tidl_info) {
338                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no manager tidl info");
339                 return VCD_ERROR_INVALID_PARAMETER;
340         }
341
342         int ret = -1;
343         ret = rpc_port_stub_vcd_mgr_stub_vc_mgr_send_buffer_cb_clone(callback, &(g_mgr_tidl_info->send_buffer_cb));
344         if (0 != ret) {
345                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to clone feedback callback. ret(%d)", ret);
346         } else {
347                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to clone feedback callback. ret(%d)", ret);
348         }
349         g_mgr_tidl_info->send_buffer_cb_user_data = user_data;
350
351         return VCD_ERROR_NONE;
352 }
353
354 int vcd_client_manager_unset_tidl_send_buffer_cb()
355 {
356         if (NULL == g_mgr_tidl_info) {
357                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no manager tidl info");
358                 return VCD_ERROR_INVALID_PARAMETER;
359         }
360
361         int ret = -1;
362         ret = rpc_port_stub_vcd_mgr_stub_vc_mgr_send_buffer_cb_destroy(g_mgr_tidl_info->send_buffer_cb);
363         if (0 != ret) {
364                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to destroy feedback callback. ret(%d)", ret);
365         } else {
366                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to destroy feedback callback. ret(%d)", ret);
367         }
368         g_mgr_tidl_info->send_buffer_cb = NULL;
369         g_mgr_tidl_info->send_buffer_cb_user_data = NULL;
370
371         return VCD_ERROR_NONE;
372 }
373
374 int vcd_client_manager_delete_tidl_info()
375 {
376         if (NULL == g_mgr_tidl_info) {
377                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no manager tidl info");
378                 return VCD_ERROR_INVALID_PARAMETER;
379         }
380
381         if (0 != rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_destroy(g_mgr_tidl_info->rpc_h)) {
382                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to destroy tidl handle");
383         }
384
385         g_mgr_tidl_info->rpc_h = NULL;
386         free(g_mgr_tidl_info);
387         g_mgr_tidl_info = NULL;
388
389         return VCD_ERROR_NONE;
390 }
391
392 manager_tidl_info_s* vcd_client_manager_get_tidl_info()
393 {
394         return g_mgr_tidl_info;
395 }
396
397 static void __vcd_client_release_each_commands(GSList** cmds)
398 {
399         if (0 < g_slist_length(*cmds)) {
400                 GSList *iter = g_slist_nth(*cmds, 0);
401                 while (NULL != iter) {
402                         vc_cmd_s *temp_cmd = iter->data;
403
404                         if (NULL != temp_cmd) {
405                                 if (NULL != temp_cmd->command) {
406                                         free(temp_cmd->command);
407                                         temp_cmd->command = NULL;
408                                 }
409                                 if (NULL != temp_cmd->appid) {
410                                         free(temp_cmd->appid);
411                                         temp_cmd->appid = NULL;
412                                 }
413                                 if (NULL != temp_cmd->parameter) {
414                                         free(temp_cmd->parameter);
415                                         temp_cmd->parameter = NULL;
416                                 }
417                                 if (NULL != temp_cmd->invocation_name) {
418                                         free(temp_cmd->invocation_name);
419                                         temp_cmd->invocation_name = NULL;
420                                 }
421                                 if (NULL != temp_cmd->fixed) {
422                                         free(temp_cmd->fixed);
423                                         temp_cmd->fixed = NULL;
424                                 }
425                                 free(temp_cmd);
426                                 temp_cmd = NULL;
427                         }
428                         *cmds = g_slist_remove_link(*cmds, iter);
429
430                         iter = g_slist_nth(*cmds, 0);
431                 }
432                 *cmds = NULL;
433         }
434 }
435
436 int __vcd_client_release_commands()
437 {
438         g_cur_cmd_list.total_cmd_count = 0;
439         g_cur_cmd_list.foreground = VC_NO_FOREGROUND_PID;
440
441         __vcd_client_release_each_commands(&(g_cur_cmd_list.widget_cmds));
442         __vcd_client_release_each_commands(&(g_cur_cmd_list.system_cmds));
443         __vcd_client_release_each_commands(&(g_cur_cmd_list.system_background_cmds));
444         __vcd_client_release_each_commands(&(g_cur_cmd_list.exclusive_system_cmds));
445         __vcd_client_release_each_commands(&(g_cur_cmd_list.foreground_cmds));
446         __vcd_client_release_each_commands(&(g_cur_cmd_list.background_cmds));
447
448         return VCD_ERROR_NONE;
449 }
450
451 int vcd_client_command_collect_command()
452 {
453         /* 1. Get foreground pid */
454         int fg_pid = 0;
455         int ret = -1;
456         if (0 != vcd_config_get_foreground(&fg_pid)) {
457                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get foreground pid");
458                 /* There is no foreground app for voice control */
459         }
460
461         SLOG(LOG_INFO, TAG_VCD, "[Client Data] Foreground pid(%d)", fg_pid);
462
463         /* 2. Clean up command list */
464         __vcd_client_release_commands();
465
466         /* Check exclusive system command */
467         if (true == g_manager.exclusive_cmd_option && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_EXCLUSIVE)) {
468                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Exclusive option of manager is ON");
469
470                 GSList* ex_sys_cmd_list = NULL;
471                 if (true == g_manager.manager_cmd) {
472                         ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_EXCLUSIVE, &ex_sys_cmd_list);
473                         if (0 != ret) {
474                                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
475                         } else {
476                                 g_cur_cmd_list.exclusive_system_cmds = ex_sys_cmd_list;
477                         }
478                 } else {
479                         SLOG(LOG_INFO, TAG_VCD, "[Client Data] No exclusive system commands");
480                 }
481
482                 return VCD_ERROR_NONE;
483         }
484
485         /* 3. Set system command */
486         GSList* sys_cmd_list = NULL;
487         if (true == g_manager.manager_cmd && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_SYSTEM)) {
488                 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_SYSTEM, &sys_cmd_list);
489                 if (0 != ret) {
490                         SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
491                 } else {
492                         g_cur_cmd_list.system_cmds = sys_cmd_list;
493                 }
494         } else {
495                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No system commands");
496         }
497
498         GSList* sys_back_cmd_list = NULL;
499         if (true == g_manager.manager_cmd && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_SYSTEM_BACKGROUND)) {
500                 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_SYSTEM_BACKGROUND, &sys_back_cmd_list);
501                 if (0 != ret) {
502                         SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
503                 } else {
504                         g_cur_cmd_list.system_background_cmds = sys_back_cmd_list;
505                 }
506         } else {
507                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No system background commands");
508         }
509
510         /* 4. Set foreground commands and widget */
511         GSList* fg_cmd_list = NULL;
512         if (VC_NO_FOREGROUND_PID != fg_pid) {
513                 GSList* widget_cmd_list = NULL;
514
515                 g_cur_cmd_list.foreground = fg_pid;
516
517                 /* 4-1. Set widget command */
518                 widget_info_s* widget_info = NULL;
519                 widget_info = __widget_get_element(fg_pid);
520                 if (NULL != widget_info && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
521                         if (true == widget_info->widget_cmd) {
522                                 ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_WIDGET, &widget_cmd_list);
523                                 if (0 != ret) {
524                                         SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the WIDGET command list");
525                                 } else {
526                                         g_cur_cmd_list.widget_cmds = widget_cmd_list;
527                                 }
528                         }
529                 } else {
530                         SLOG(LOG_INFO, TAG_VCD, "[Client Data] No widget commands");
531                 }
532
533                 /* 4-2. Set foreground command of foreground app */
534                 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_FOREGROUND)) {
535                         ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_FOREGROUND, &fg_cmd_list);
536                         if (0 != ret) {
537                                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the fg command list");
538                         } else {
539                                 g_cur_cmd_list.foreground_cmds = fg_cmd_list;
540                         }
541                 }
542         } else {
543                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No foreground app");
544         }
545         if (true == g_manager.manager_cmd && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_FOREGROUND)) {
546                 /* 4-3. Set foreground command by manager */
547                 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_FOREGROUND, &fg_cmd_list);
548                 if (0 != ret) {
549                         SLOG(LOG_INFO, TAG_VCD, "[Client Data] No foreground commands of manager app");
550                 } else {
551                         g_cur_cmd_list.foreground_cmds = fg_cmd_list;
552                 }
553         }
554
555         /* 5. Set background commands */
556         GSList* bg_cmd_list = NULL;
557         if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_BACKGROUND)) {
558                 ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_BACKGROUND, &bg_cmd_list);
559                 if (0 != ret) {
560                         SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the bg command list");
561                 } else {
562                         /* Add item to global command list */
563                         g_cur_cmd_list.background_cmds = bg_cmd_list;
564                 }
565         }
566         return VCD_ERROR_NONE;
567 }
568
569 int vcd_client_get_length()
570 {
571         int command_count = 0;
572         command_count += g_slist_length(g_cur_cmd_list.widget_cmds);
573         command_count += g_slist_length(g_cur_cmd_list.system_cmds);
574         command_count += g_slist_length(g_cur_cmd_list.exclusive_system_cmds);
575         command_count += g_slist_length(g_cur_cmd_list.system_background_cmds);
576         command_count += g_slist_length(g_cur_cmd_list.foreground_cmds);
577         command_count += g_slist_length(g_cur_cmd_list.background_cmds);
578
579         g_cur_cmd_list.total_cmd_count = command_count;
580
581         SLOG(LOG_INFO, TAG_VCD, "[Client Data] Command count : %d ", g_cur_cmd_list.total_cmd_count);
582         return command_count;
583 }
584
585 int vcd_client_foreach_command(client_foreach_command_cb callback, void* user_data)
586 {
587         if (NULL == callback) {
588                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] input parameter is NULL");
589                 return VCD_ERROR_INVALID_PARAMETER;
590         }
591
592         if (0 != vcd_client_command_collect_command()) {
593                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
594                 return VCD_ERROR_OPERATION_FAILED;
595         }
596
597         if (0 >= vcd_client_get_length()) {
598                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] No current command");
599                 return VCD_ERROR_OPERATION_FAILED;
600         }
601
602 //      int id_count = 1;
603         GSList *iter = NULL;
604         vc_cmd_s* temp_cmd;
605
606         if (0 < g_slist_length(g_cur_cmd_list.widget_cmds)) {
607                 iter = g_slist_nth(g_cur_cmd_list.widget_cmds, 0);
608                 while (NULL != iter) {
609                         temp_cmd = iter->data;
610
611                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Widget : id(%d) type(%d) format(%d) command(%s) domain(%d)"
612                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->domain);
613
614                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
615
616                         iter = g_slist_next(iter);
617                 }
618         } else {
619                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No widget commands");
620         }
621
622         if (0 < g_slist_length(g_cur_cmd_list.foreground_cmds)) {
623                 iter = g_slist_nth(g_cur_cmd_list.foreground_cmds, 0);
624                 while (NULL != iter) {
625                         temp_cmd = iter->data;
626
627                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Fore : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d) fixed(%s)"
628                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, temp_cmd->fixed);
629
630                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
631
632                         iter = g_slist_next(iter);
633                 }
634         } else {
635                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No foreground commands");
636         }
637
638         if (0 < g_slist_length(g_cur_cmd_list.system_cmds)) {
639                 iter = g_slist_nth(g_cur_cmd_list.system_cmds, 0);
640                 while (NULL != iter) {
641                         temp_cmd = iter->data;
642
643                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] System : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d)"
644                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
645
646                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
647
648                         iter = g_slist_next(iter);
649                 }
650         } else {
651                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No system commands");
652         }
653
654         if (0 < g_slist_length(g_cur_cmd_list.system_background_cmds)) {
655                 iter = g_slist_nth(g_cur_cmd_list.system_background_cmds, 0);
656                 while (NULL != iter) {
657                         temp_cmd = iter->data;
658
659                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] System background : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d)"
660                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
661
662                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
663
664                         iter = g_slist_next(iter);
665                 }
666         } else {
667                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No system background commands");
668         }
669
670         if (0 < g_slist_length(g_cur_cmd_list.exclusive_system_cmds)) {
671                 iter = g_slist_nth(g_cur_cmd_list.exclusive_system_cmds, 0);
672                 while (NULL != iter) {
673                         temp_cmd = iter->data;
674
675                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Exclusive system : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d)"
676                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
677
678                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
679
680                         iter = g_slist_next(iter);
681                 }
682         } else {
683                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No exclusive system commands");
684         }
685
686         if (0 < g_slist_length(g_cur_cmd_list.background_cmds)) {
687                 iter = g_slist_nth(g_cur_cmd_list.background_cmds, 0);
688                 while (NULL != iter) {
689                         temp_cmd = iter->data;
690
691                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Back : id(%d) format(%d) type(%d) command(%s) param(%s) domain(%d) appid(%s) invocation(%s) fixed(%s)"
692                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, temp_cmd->appid, temp_cmd->invocation_name, temp_cmd->fixed);
693
694                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
695
696                         iter = g_slist_next(iter);
697                 }
698         } else {
699                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No background commands");
700         }
701         return VCD_ERROR_NONE;
702 }
703
704 static vc_cmd_s* __command_copy(vc_cmd_s* src_cmd)
705 {
706         if (NULL == src_cmd) {
707                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Input command is NULL");
708                 return NULL;
709         }
710
711         vc_cmd_s* temp_cmd = NULL;
712         temp_cmd = (vc_cmd_s*)calloc(sizeof(vc_cmd_s), 1);
713         if (NULL == temp_cmd) {
714                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
715                 return NULL;
716         }
717
718         temp_cmd->id = src_cmd->id;
719         temp_cmd->pid = src_cmd->pid;
720         temp_cmd->index = src_cmd->index;
721         temp_cmd->type = src_cmd->type;
722         temp_cmd->format = src_cmd->format;
723         temp_cmd->domain = src_cmd->domain;
724
725         if (VC_COMMAND_TYPE_SYSTEM == temp_cmd->type)           temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM;
726         else if (VC_COMMAND_TYPE_EXCLUSIVE == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_EXCLUSIVE;
727         else if (VC_COMMAND_TYPE_WIDGET == temp_cmd->type)      temp_cmd->priority = VC_COMMAND_PRIORITY_WIDGET;
728         else if (VC_COMMAND_TYPE_FOREGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
729         else if (VC_COMMAND_TYPE_SYSTEM_BACKGROUND == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM_BACKGROUND;
730         else if (VC_COMMAND_TYPE_BACKGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_BACKGROUND;
731
732         if (NULL != src_cmd->command) {
733                 temp_cmd->command = strdup(src_cmd->command);
734         }
735
736         if (NULL != src_cmd->parameter) {
737                 temp_cmd->parameter = strdup(src_cmd->parameter);
738         }
739
740         if (NULL != src_cmd->appid) {
741                 temp_cmd->appid = strdup(src_cmd->appid);
742         }
743
744         if (NULL != src_cmd->invocation_name) {
745                 temp_cmd->invocation_name = strdup(src_cmd->invocation_name);
746         }
747
748         if (NULL != src_cmd->fixed) {
749                 temp_cmd->fixed = strdup(src_cmd->fixed);
750         }
751
752         temp_cmd->key = src_cmd->key;
753         temp_cmd->modifier = src_cmd->modifier;
754
755         return temp_cmd;
756 }
757
758 int vcd_client_get_cmd_from_result_id(int result_id, vc_cmd_s** result)
759 {
760         GSList *iter = NULL;
761         vc_cmd_s* temp_cmd;
762
763         SLOG(LOG_INFO, TAG_VCD, "[Client Data] Result id(%d)", result_id);
764
765         if (0 < g_slist_length(g_cur_cmd_list.widget_cmds)) {
766                 iter = g_slist_nth(g_cur_cmd_list.widget_cmds, 0);
767                 while (NULL != iter) {
768                         temp_cmd = iter->data;
769
770                         if (result_id == temp_cmd->id) {
771                                 /**pid = g_cur_cmd_list.foreground; */
772                                 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_UI_CONTROL; */
773                                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
774
775                                 *result = __command_copy(temp_cmd);
776
777                                 return VCD_ERROR_NONE;
778                         }
779
780                         iter = g_slist_next(iter);
781                 }
782         } else {
783                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No widget commands");
784         }
785
786         if (0 < g_slist_length(g_cur_cmd_list.foreground_cmds)) {
787                 iter = g_slist_nth(g_cur_cmd_list.foreground_cmds, 0);
788
789                 while (NULL != iter) {
790                         temp_cmd = iter->data;
791
792                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] command id (%d)", temp_cmd->id);
793
794                         if (result_id == temp_cmd->id) {
795                                 /**pid = g_cur_cmd_list.foreground; */
796                                 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_FOREGROUND; */
797                                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
798
799                                 *result = __command_copy(temp_cmd);
800                                 return VCD_ERROR_NONE;
801                         }
802
803                         iter = g_slist_next(iter);
804                 }
805         } else {
806                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No foreground commands");
807         }
808
809         if (0 < g_slist_length(g_cur_cmd_list.system_cmds)) {
810                 iter = g_slist_nth(g_cur_cmd_list.system_cmds, 0);
811                 while (NULL != iter) {
812                         temp_cmd = iter->data;
813
814                         if (result_id == temp_cmd->id) {
815                                 /**pid = g_manager.pid; */
816                                 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM; */
817                                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
818
819                                 *result = __command_copy(temp_cmd);
820                                 return VCD_ERROR_NONE;
821                         }
822
823                         iter = g_slist_next(iter);
824                 }
825         } else {
826                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No system commands");
827         }
828
829         if (0 < g_slist_length(g_cur_cmd_list.system_background_cmds)) {
830                 iter = g_slist_nth(g_cur_cmd_list.system_background_cmds, 0);
831                 while (NULL != iter) {
832                         temp_cmd = iter->data;
833
834                         if (result_id == temp_cmd->id) {
835                                 /**pid = g_manager.pid; */
836                                 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM_BACKGROUND; */
837                                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
838
839                                 *result = __command_copy(temp_cmd);
840                                 return VCD_ERROR_NONE;
841                         }
842
843                         iter = g_slist_next(iter);
844                 }
845         } else {
846                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No system background commands");
847         }
848
849         if (0 < g_slist_length(g_cur_cmd_list.exclusive_system_cmds)) {
850                 iter = g_slist_nth(g_cur_cmd_list.exclusive_system_cmds, 0);
851                 while (NULL != iter) {
852                         temp_cmd = iter->data;
853
854                         if (result_id == temp_cmd->id) {
855                                 /**pid = g_manager.pid; */
856                                 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM_EXCLUSIVE; */
857                                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
858
859                                 *result = __command_copy(temp_cmd);
860                                 return VCD_ERROR_NONE;
861                         }
862
863                         iter = g_slist_next(iter);
864                 }
865         } else {
866                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No exclusive system commands");
867         }
868
869         if (0 < g_slist_length(g_cur_cmd_list.background_cmds)) {
870                 iter = g_slist_nth(g_cur_cmd_list.background_cmds, 0);
871
872                 while (NULL != iter) {
873                         temp_cmd = iter->data;
874
875                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] command id (%d)", temp_cmd->id);
876
877                         if (result_id == temp_cmd->id) {
878                                 *result = __command_copy(temp_cmd);
879                                 return VCD_ERROR_NONE;
880                         }
881
882                         iter = g_slist_next(iter);
883                 }
884         } else {
885                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No background commands");
886         }
887
888         SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Not find matched result");
889         return VCD_ERROR_INVALID_PARAMETER;
890 }
891
892 int vcd_client_set_slience_detection(bool value)
893 {
894         g_silence_detection = value;
895         return VCD_ERROR_NONE;
896 }
897
898 bool vcd_client_get_slience_detection()
899 {
900         return g_silence_detection;
901 }
902
903 int vcd_client_set_recognition_mode(vcd_recognition_mode_e mode)
904 {
905         g_recognition_mode = mode;
906         return VCD_ERROR_NONE;
907 }
908
909 vcd_recognition_mode_e vcd_client_get_recognition_mode()
910 {
911         return g_recognition_mode;
912 }
913
914 int vcd_client_set_server_dialog(int pid, bool is_server_dialog)
915 {
916         vc_client_info_s* client_info = NULL;
917
918         client_info = __client_get_element(pid);
919         if (NULL == client_info) {
920                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
921                 return VCD_ERROR_INVALID_PARAMETER;
922         }
923
924         client_info->server_dialog = is_server_dialog;
925
926         SLOG(LOG_INFO, TAG_VCD, "[Client Data] Set server dialog, client pid(%d), server_dialog(%d)", pid, client_info->server_dialog);
927         return VCD_ERROR_NONE;
928 }
929
930 int vcd_client_get_server_dialog(int pid, bool* is_server_dialog)
931 {
932         vc_client_info_s* client_info = NULL;
933
934         client_info = __client_get_element(pid);
935         if (NULL == client_info) {
936                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
937                 return VCD_ERROR_INVALID_PARAMETER;
938         }
939
940         *is_server_dialog = client_info->server_dialog;
941
942         SLOG(LOG_INFO, TAG_VCD, "[Client Data] Get server dialog, client pid(%d), server_dialog(%d)", pid, *is_server_dialog);
943         return VCD_ERROR_NONE;
944 }
945
946 int vcd_client_append_cmd_from_type(int type, vc_cmd_list_h list)
947 {
948         GSList *item = NULL;
949
950         if (NULL == list) {
951                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid list parameter");
952                 return VCD_ERROR_INVALID_PARAMETER;
953         }
954
955         switch (type) {
956         case VC_COMMAND_TYPE_SYSTEM:
957                 item = g_cur_cmd_list.system_cmds;
958                 break;
959         case VC_COMMAND_TYPE_FOREGROUND:
960                 item = g_cur_cmd_list.foreground_cmds;
961                 break;
962         case VC_COMMAND_TYPE_WIDGET:
963                 item = g_cur_cmd_list.widget_cmds;
964                 break;
965         case VC_COMMAND_TYPE_SYSTEM_BACKGROUND:
966                 item = g_cur_cmd_list.system_background_cmds;
967                 break;
968         case VC_COMMAND_TYPE_BACKGROUND:
969                 item = g_cur_cmd_list.background_cmds;
970                 break;
971         case VC_COMMAND_TYPE_EXCLUSIVE:
972                 item = g_cur_cmd_list.exclusive_system_cmds;
973                 break;
974         default:
975                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid type parameter");
976                 return VCD_ERROR_INVALID_PARAMETER;
977         }
978
979         while (NULL != item) {
980                 vc_cmd_h temp_cmd = NULL;
981                 vc_cmd_h target_cmd = (vc_cmd_h)item->data;
982
983                 temp_cmd = (vc_cmd_h)__command_copy((vc_cmd_s*)target_cmd);
984
985                 if (NULL == temp_cmd) {
986                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to copy the command");
987                         return VCD_ERROR_OUT_OF_MEMORY;
988                 }
989
990                 if (0 != vc_cmd_list_add(list, temp_cmd)) {
991                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add the command in the list");
992                         if (NULL != temp_cmd) {
993                                 vc_cmd_destroy(temp_cmd);
994                                 temp_cmd = NULL;
995                         }
996
997                         return VCD_ERROR_OPERATION_FAILED;
998                 }
999
1000                 item = item->next;
1001         }
1002
1003         return VCD_ERROR_NONE;
1004 }
1005
1006 int __show_client_list()
1007 {
1008         GSList *iter = NULL;
1009         vc_client_info_s *data = NULL;
1010
1011         SLOG(LOG_DEBUG, TAG_VCD, "@@@ client list");
1012
1013         int count = g_slist_length(g_client_list);
1014         int i;
1015
1016         if (0 == count) {
1017                 SLOG(LOG_DEBUG, TAG_VCD, "No Client");
1018         } else {
1019                 iter = g_slist_nth(g_client_list, 0);
1020                 for (i = 0; i < count; i++) {
1021                         if (NULL == iter)
1022                                 break;
1023
1024                         data = iter->data;
1025
1026                         if (NULL != data) {
1027                                 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
1028                         }
1029                         iter = g_slist_next(iter);
1030                 }
1031         }
1032
1033         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1034
1035         SLOG(LOG_DEBUG, TAG_VCD, "@@@ widget list");
1036
1037         widget_info_s *widget_data = NULL;
1038
1039         count = g_slist_length(g_widget_list);
1040
1041         if (0 == count) {
1042                 SLOG(LOG_DEBUG, TAG_VCD, "No widget");
1043         } else {
1044                 iter = g_slist_nth(g_widget_list, 0);
1045                 for (i = 0; i < count; i++) {
1046                         if (NULL == iter)
1047                                 break;
1048
1049                         widget_data = iter->data;
1050
1051                         if (NULL != widget_data) {
1052                                 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
1053                         }
1054                         iter = g_slist_next(iter);
1055                 }
1056         }
1057
1058         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1059
1060         return VCD_ERROR_NONE;
1061 }
1062
1063 int __show_command_list(GSList* cmd_group)
1064 {
1065         GSList *iter = NULL;
1066         vc_cmd_s *data = NULL;
1067
1068         SLOG(LOG_DEBUG, TAG_VCD, "@@@ command group");
1069
1070         int count = g_slist_length(cmd_group);
1071         int i;
1072
1073         if (0 == count) {
1074                 SLOG(LOG_DEBUG, TAG_VCD, "No command");
1075         } else {
1076                 iter = g_slist_nth(cmd_group, 0);
1077                 for (i = 0; i < count; i++) {
1078                         if (NULL == iter)
1079                                 break;
1080
1081                         data = iter->data;
1082                         if (NULL != data) {
1083                                 if (NULL != data->parameter) {
1084                                         SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) parameter(%s) key(%d)", i, data->command, data->parameter, data->key);
1085                                 } else {
1086                                         SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) key(%d)", i, data->command, data->key);
1087                                 }
1088                         }
1089                         iter = g_slist_next(iter);
1090                 }
1091         }
1092
1093         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1094
1095         return VCD_ERROR_NONE;
1096 }
1097
1098 GSList* __client_get_item(const int pid)
1099 {
1100         GSList *iter = NULL;
1101         vc_client_info_s *data = NULL;
1102
1103         int count = g_slist_length(g_client_list);
1104         int i;
1105
1106         if (0 < count) {
1107                 iter = g_slist_nth(g_client_list, 0);
1108                 for (i = 0; i < count; i++) {
1109                         if (NULL == iter)
1110                                 break;
1111
1112                         data = iter->data;
1113                         if (NULL != data) {
1114                                 if (pid == data->pid)
1115                                         return iter;
1116                         }
1117
1118                         iter = g_slist_next(iter);
1119                 }
1120         }
1121
1122         return NULL;
1123 }
1124
1125 vc_client_info_s* __client_get_element(int pid)
1126 {
1127         GSList *iter = NULL;
1128         vc_client_info_s *data = NULL;
1129
1130         int count = g_slist_length(g_client_list);
1131         int i;
1132
1133         if (0 < count) {
1134                 iter = g_slist_nth(g_client_list, 0);
1135                 for (i = 0; i < count; i++) {
1136                         if (NULL == iter)
1137                                 break;
1138
1139                         data = iter->data;
1140
1141                         if (NULL != data) {
1142                                 if (pid == data->pid)
1143                                         return data;
1144                         }
1145
1146                         iter = g_slist_next(iter);
1147                 }
1148         }
1149
1150         return NULL;
1151 }
1152
1153 int vcd_client_add(int pid)
1154 {
1155         /*Check pid is duplicated*/
1156         GSList *tmp = NULL;
1157         tmp = __client_get_item(pid);
1158
1159         if (NULL != tmp) {
1160                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Client pid is already registered");
1161                 return VCD_ERROR_INVALID_PARAMETER;
1162         }
1163
1164         vc_client_info_s *info = (vc_client_info_s*)calloc(1, sizeof(vc_client_info_s));
1165         if (NULL == info) {
1166                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1167                 return VCD_ERROR_OUT_OF_MEMORY;
1168         }
1169
1170         info->pid = pid;
1171
1172         info->fg_cmd = false;
1173         info->bg_cmd = false;
1174         info->exclusive_cmd = false;
1175         info->server_dialog = false;
1176
1177         /* Add item to global list */
1178         g_client_list = g_slist_append(g_client_list, info);
1179
1180         if (NULL == g_client_list) {
1181                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new client");
1182
1183                 free(info);
1184                 info = NULL;
1185
1186                 return VCD_ERROR_OUT_OF_MEMORY;
1187         } else {
1188                 SLOG(LOG_INFO, TAG_VCD, "[Client Data SUCCESS] Add new client");
1189         }
1190
1191 #ifdef CLIENT_DATA_DEBUG
1192         __show_client_list();
1193 #endif
1194         return VCD_ERROR_NONE;
1195 }
1196
1197 int vcd_client_delete(int pid)
1198 {
1199         GSList *tmp = NULL;
1200         vc_client_info_s* client_info = NULL;
1201
1202         /*Get handle*/
1203         tmp = __client_get_item(pid);
1204         if (NULL == tmp) {
1205                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1206                 return VCD_ERROR_INVALID_PARAMETER;
1207         }
1208
1209         /*Free client structure*/
1210         client_info = tmp->data;
1211         if (NULL != client_info) {
1212                 free(client_info);
1213         }
1214
1215         /*Remove handle from list*/
1216         g_client_list = g_slist_remove_link(g_client_list, tmp);
1217
1218
1219 #ifdef CLIENT_DATA_DEBUG
1220         __show_client_list();
1221 #endif
1222
1223         return VCD_ERROR_NONE;
1224 }
1225
1226 GSList* __get_client_tidl_info_item(const int pid)
1227 {
1228         GSList *iter = NULL;
1229         client_tidl_info_s *data = NULL;
1230
1231         int count = g_slist_length(g_client_tidl_info_list);
1232         int i;
1233
1234         if (0 < count) {
1235                 iter = g_slist_nth(g_client_tidl_info_list, 0);
1236                 for (i = 0; i < count; i++) {
1237                         if (NULL == iter)
1238                                 break;
1239
1240                         data = iter->data;
1241                         if (NULL != data) {
1242                                 if (pid == data->pid)
1243                                         return iter;
1244                         }
1245
1246                         iter = g_slist_next(iter);
1247                 }
1248         }
1249
1250         return NULL;
1251 }
1252
1253 client_tidl_info_s* __get_client_tidl_info_element(int pid)
1254 {
1255         GSList *iter = NULL;
1256         client_tidl_info_s *data = NULL;
1257
1258         int count = g_slist_length(g_client_tidl_info_list);
1259         int i;
1260
1261         if (0 < count) {
1262                 iter = g_slist_nth(g_client_tidl_info_list, 0);
1263                 for (i = 0; i < count; i++) {
1264                         if (NULL == iter)
1265                                 break;
1266
1267                         data = iter->data;
1268
1269                         if (NULL != data) {
1270                                 if (pid == data->pid)
1271                                         return data;
1272                         }
1273
1274                         iter = g_slist_next(iter);
1275                 }
1276         }
1277
1278         return NULL;
1279 }
1280
1281 int vcd_client_add_tidl_info(int pid)
1282 {
1283         /*Check pid is duplicated*/
1284         client_tidl_info_s* info = NULL;
1285         info = __get_client_tidl_info_element(pid);
1286
1287         if (NULL != info) {
1288                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Client tidl info pid is already registered");
1289                 return VCD_ERROR_NONE;
1290         }
1291
1292         SLOG(LOG_INFO, TAG_VCD, "[Client Data] There is no tidl info of pid(%d). Create new one.", pid);
1293         info = (client_tidl_info_s*)calloc(1, sizeof(client_tidl_info_s));
1294         if (NULL == info) {
1295                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1296                 return VCD_ERROR_OUT_OF_MEMORY;
1297         }
1298
1299         info->pid = pid;
1300         info->notify_cb = NULL;
1301         info->notify_cb_user_data = NULL;
1302         info->feedback_cb = NULL;
1303         info->feedback_cb_user_data = NULL;
1304
1305         g_client_tidl_info_list = g_slist_append(g_client_tidl_info_list, info);
1306         if (NULL == g_client_tidl_info_list) {
1307                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new client tidl info");
1308
1309                 free(info);
1310                 info = NULL;
1311
1312                 return VCD_ERROR_OUT_OF_MEMORY;
1313         } else {
1314                 SLOG(LOG_INFO, TAG_VCD, "[Client Data SUCCESS] Add new client tidl info. pid(%d)", pid);
1315         }
1316
1317         return VCD_ERROR_NONE;
1318 }
1319
1320 int vcd_client_set_tidl_notify_cb(int pid, rpc_port_stub_vcd_stub_vc_notify_cb_h callback, void* user_data)
1321 {
1322         /*Check pid*/
1323         client_tidl_info_s* info = NULL;
1324         info = __get_client_tidl_info_element(pid);
1325
1326         if (NULL == info) {
1327                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no tidl info in the list. pid(%d)", pid);
1328                 return VCD_ERROR_INVALID_PARAMETER;
1329         }
1330
1331         int ret = -1;
1332         ret = rpc_port_stub_vcd_stub_vc_notify_cb_clone(callback, &(info->notify_cb));
1333         if (0 != ret) {
1334                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to clone notify callback. ret(%d)", ret);
1335         } else {
1336                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to clone notify callback. ret(%d)", ret);
1337         }
1338         info->notify_cb_user_data = user_data;
1339
1340         return VCD_ERROR_NONE;
1341 }
1342
1343 int vcd_client_unset_tidl_notify_cb(int pid)
1344 {
1345         /*Check pid*/
1346         client_tidl_info_s* info = NULL;
1347         info = __get_client_tidl_info_element(pid);
1348
1349         if (NULL == info) {
1350                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no tidl info in the list. pid(%d)", pid);
1351                 return VCD_ERROR_INVALID_PARAMETER;
1352         }
1353
1354         int ret = -1;
1355         ret = rpc_port_stub_vcd_stub_vc_notify_cb_destroy(info->notify_cb);
1356         if (0 != ret) {
1357                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to destroy notify callback. ret(%d)", ret);
1358         } else {
1359                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to destroy notify callback. ret(%d)", ret);
1360         }
1361         info->notify_cb = NULL;
1362         info->notify_cb_user_data = NULL;
1363
1364         return VCD_ERROR_NONE;
1365 }
1366
1367 int vcd_client_set_tidl_feedback_cb(int pid, rpc_port_stub_vcd_stub_vc_feedback_cb_h callback, void* user_data)
1368 {
1369         /*Check pid*/
1370         client_tidl_info_s* info = NULL;
1371         info = __get_client_tidl_info_element(pid);
1372
1373         if (NULL == info) {
1374                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no tidl info in the list. pid(%d)", pid);
1375                 return VCD_ERROR_INVALID_PARAMETER;
1376         }
1377
1378         int ret = -1;
1379         ret = rpc_port_stub_vcd_stub_vc_feedback_cb_clone(callback, &(info->feedback_cb));
1380         if (0 != ret) {
1381                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to clone feedback callback. ret(%d)", ret);
1382         } else {
1383                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to clone feedback callback. ret(%d)", ret);
1384         }
1385         info->feedback_cb_user_data = user_data;
1386
1387         return VCD_ERROR_NONE;
1388 }
1389
1390 int vcd_client_unset_tidl_feedback_cb(int pid)
1391 {
1392         /*Check pid*/
1393         client_tidl_info_s* info = NULL;
1394         info = __get_client_tidl_info_element(pid);
1395
1396         if (NULL == info) {
1397                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no tidl info in the list. pid(%d)", pid);
1398                 return VCD_ERROR_INVALID_PARAMETER;
1399         }
1400
1401         int ret = -1;
1402         ret = rpc_port_stub_vcd_stub_vc_feedback_cb_destroy(info->feedback_cb);
1403         if (0 != ret) {
1404                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to destroy feedback callback. ret(%d)", ret);
1405         } else {
1406                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to destroy feedback callback. ret(%d)", ret);
1407         }
1408         info->feedback_cb = NULL;
1409         info->feedback_cb_user_data = NULL;
1410
1411         return VCD_ERROR_NONE;
1412 }
1413
1414 int vcd_client_delete_tidl_info(int pid)
1415 {
1416         GSList *tmp = NULL;
1417         client_tidl_info_s* client_tidl_info = NULL;
1418
1419         /*Get handle*/
1420         tmp = __get_client_tidl_info_item(pid);
1421         if (NULL == tmp) {
1422                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1423                 return VCD_ERROR_INVALID_PARAMETER;
1424         }
1425
1426         /*Free client structure*/
1427         client_tidl_info = tmp->data;
1428         if (NULL != client_tidl_info) {
1429                 free(client_tidl_info);
1430         }
1431
1432         /*Delete handle from list*/
1433         g_client_tidl_info_list = g_slist_delete_link(g_client_tidl_info_list, tmp);
1434
1435         return VCD_ERROR_NONE;
1436 }
1437
1438 client_tidl_info_s* vcd_client_get_tidl_info(int pid)
1439 {
1440         return __get_client_tidl_info_element(pid);
1441 }
1442
1443 int vcd_client_get_tidl_list(int** pids, int* pid_count)
1444 {
1445         if (NULL == pids || NULL == pid_count)
1446                 return VCD_ERROR_INVALID_PARAMETER;
1447
1448         int count = g_slist_length(g_client_tidl_info_list);
1449
1450         if (0 == count)
1451                 return VCD_ERROR_OUT_OF_MEMORY;
1452
1453         int *tmp;
1454         tmp = (int*)calloc(count, sizeof(int));
1455         if (NULL == tmp) {
1456                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1457                 return VCD_ERROR_OUT_OF_MEMORY;
1458         }
1459
1460         GSList *iter = NULL;
1461         client_tidl_info_s *data = NULL;
1462         int i = 0;
1463
1464         iter = g_slist_nth(g_client_tidl_info_list, 0);
1465
1466         while (NULL != iter) {
1467                 data = iter->data;
1468
1469                 if (NULL != data) {
1470                         tmp[i] = data->pid;
1471                 }
1472
1473                 iter = g_slist_next(iter);
1474                 i++;
1475         }
1476
1477         *pids = tmp;
1478         *pid_count = count;
1479
1480         return VCD_ERROR_NONE;
1481 }
1482
1483 bool vcd_client_is_available(int pid)
1484 {
1485         vc_client_info_s* client_info = NULL;
1486
1487         client_info = __client_get_element(pid);
1488         if (NULL == client_info) {
1489                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1490                 return false;
1491         }
1492
1493         return true;
1494 }
1495
1496 int vcd_client_get_ref_count()
1497 {
1498         int count = 0;
1499
1500         count = g_slist_length(g_client_list) + g_slist_length(g_widget_list);
1501         if (0 < g_manager.pid) {
1502                 count++;
1503         }
1504
1505         SLOG(LOG_INFO, TAG_VCD, "[Client Data] client count : %d", count);
1506
1507         return count;
1508 }
1509
1510 int vcd_client_get_list(int** pids, int* pid_count)
1511 {
1512         if (NULL == pids || NULL == pid_count)
1513                 return VCD_ERROR_INVALID_PARAMETER;
1514
1515         int count = g_slist_length(g_client_list);
1516
1517         if (0 == count)
1518                 return VCD_ERROR_OUT_OF_MEMORY;
1519
1520         int *tmp;
1521         tmp = (int*)calloc(count, sizeof(int));
1522         if (NULL == tmp) {
1523                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1524                 return VCD_ERROR_OUT_OF_MEMORY;
1525         }
1526
1527         GSList *iter = NULL;
1528         vc_client_info_s *data = NULL;
1529         int i = 0;
1530
1531         iter = g_slist_nth(g_client_list, 0);
1532
1533         while (NULL != iter) {
1534                 data = iter->data;
1535
1536                 if (NULL != data) {
1537                         tmp[i] = data->pid;
1538                 }
1539
1540                 iter = g_slist_next(iter);
1541                 i++;
1542         }
1543
1544         *pids = tmp;
1545         *pid_count = count;
1546
1547         return VCD_ERROR_NONE;
1548 }
1549
1550 int vcd_client_set_command_type(int pid, int type)
1551 {
1552         vc_client_info_s* client_info = NULL;
1553
1554         client_info = __client_get_element(pid);
1555         if (NULL == client_info) {
1556                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1557                 return VCD_ERROR_INVALID_PARAMETER;
1558         }
1559
1560         switch (type) {
1561         case VC_COMMAND_TYPE_FOREGROUND:
1562                 client_info->fg_cmd = true;
1563                 break;
1564         case VC_COMMAND_TYPE_BACKGROUND:
1565                 client_info->bg_cmd = true;
1566                 break;
1567         default:
1568                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1569                 return VCD_ERROR_INVALID_PARAMETER;
1570         }
1571
1572         return VCD_ERROR_NONE;
1573 }
1574
1575 int vcd_client_unset_command_type(int pid, int type)
1576 {
1577         vc_client_info_s* client_info = NULL;
1578
1579         client_info = __client_get_element(pid);
1580         if (NULL == client_info) {
1581                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1582                 return VCD_ERROR_INVALID_PARAMETER;
1583         }
1584
1585         switch (type) {
1586         case VC_COMMAND_TYPE_FOREGROUND:
1587                 client_info->fg_cmd = false;
1588                 break;
1589         case VC_COMMAND_TYPE_BACKGROUND:
1590                 client_info->bg_cmd = false;
1591                 break;
1592         default:
1593                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1594                 return VCD_ERROR_INVALID_PARAMETER;
1595         }
1596
1597         return VCD_ERROR_NONE;
1598 }
1599
1600 int vcd_client_set_exclusive_command(int pid)
1601 {
1602         vc_client_info_s* client_info = NULL;
1603
1604         client_info = __client_get_element(pid);
1605         if (NULL == client_info) {
1606                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1607                 return VCD_ERROR_INVALID_PARAMETER;
1608         }
1609
1610         client_info->exclusive_cmd = true;
1611
1612         return VCD_ERROR_NONE;
1613 }
1614
1615 int vcd_client_unset_exclusive_command(int pid)
1616 {
1617         vc_client_info_s* client_info = NULL;
1618
1619         client_info = __client_get_element(pid);
1620         if (NULL == client_info) {
1621                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1622                 return VCD_ERROR_INVALID_PARAMETER;
1623         }
1624
1625         client_info->exclusive_cmd = false;
1626
1627         return VCD_ERROR_NONE;
1628 }
1629
1630 int vcd_client_save_client_info()
1631 {
1632         if (0 != vc_info_parser_set_client_info(g_client_list)) {
1633                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to save client info");
1634                 return VCD_ERROR_OPERATION_FAILED;
1635         }
1636
1637         return VCD_ERROR_NONE;
1638 }
1639
1640 /*
1641 * Functions for widget
1642 */
1643 GSList* __widget_get_item(int pid)
1644 {
1645         GSList *iter = NULL;
1646         widget_info_s *data = NULL;
1647
1648         int count = g_slist_length(g_widget_list);
1649         int i;
1650
1651         if (0 < count) {
1652                 iter = g_slist_nth(g_widget_list, 0);
1653                 for (i = 0; i < count; i++) {
1654                         if (NULL == iter)
1655                                 break;
1656
1657                         data = iter->data;
1658
1659                         if (NULL != data) {
1660                                 if (pid == data->pid)
1661                                         return iter;
1662                         }
1663
1664                         iter = g_slist_next(iter);
1665                 }
1666         }
1667
1668         return NULL;
1669 }
1670
1671 widget_info_s* __widget_get_element(int pid)
1672 {
1673         GSList *iter = NULL;
1674         widget_info_s *data = NULL;
1675
1676         int count = g_slist_length(g_widget_list);
1677         int i;
1678
1679         if (0 < count) {
1680                 iter = g_slist_nth(g_widget_list, 0);
1681                 i = 0;
1682
1683                 while (iter && i < count) {
1684                         data = iter->data;
1685
1686                         if (NULL != data) {
1687                                 if (pid == data->pid)
1688                                         return data;
1689                         }
1690
1691                         iter = g_slist_next(iter);
1692
1693                         i++;
1694                 }
1695         }
1696
1697         return NULL;
1698 }
1699
1700 int vcd_client_widget_get_list(int** pids, int* pid_count)
1701 {
1702         if (NULL == pids || NULL == pid_count)
1703                 return VCD_ERROR_INVALID_PARAMETER;
1704
1705         int count = g_slist_length(g_widget_list);
1706
1707         if (0 == count)
1708                 return VCD_ERROR_OUT_OF_MEMORY;
1709
1710         int *tmp;
1711         tmp = (int*)calloc(count, sizeof(int));
1712         if (NULL == tmp) {
1713                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1714                 return VCD_ERROR_OUT_OF_MEMORY;
1715         }
1716
1717         GSList *iter = NULL;
1718         widget_info_s *data = NULL;
1719         int i = 0;
1720
1721         iter = g_slist_nth(g_widget_list, 0);
1722         while (NULL != iter) {
1723                 data = iter->data;
1724
1725                 if (NULL != data) {
1726                         tmp[i] = data->pid;
1727                 }
1728
1729                 iter = g_slist_next(iter);
1730                 i++;
1731         }
1732
1733         *pids = tmp;
1734         *pid_count = count;
1735
1736         return VCD_ERROR_NONE;
1737 }
1738
1739 int vcd_client_widget_add(int pid)
1740 {
1741         /*Check pid is duplicated*/
1742         GSList *tmp = NULL;
1743         tmp = __widget_get_item(pid);
1744
1745         if (NULL != tmp) {
1746                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] widget pid is already registered");
1747                 return VCD_ERROR_INVALID_PARAMETER;
1748         }
1749
1750         widget_info_s *info = (widget_info_s*)calloc(1, sizeof(widget_info_s));
1751         if (NULL == info) {
1752                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1753                 return VCD_ERROR_OUT_OF_MEMORY;
1754         }
1755
1756         info->pid = pid;
1757         info->widget_cmd = false;
1758
1759         /* Add item to global list */
1760         g_widget_list = g_slist_append(g_widget_list, info);
1761
1762         if (NULL == g_widget_list) {
1763                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new widget");
1764
1765                 free(info);
1766                 info = NULL;
1767
1768                 return VCD_ERROR_OUT_OF_MEMORY;
1769         } else {
1770                 SLOG(LOG_INFO, TAG_VCD, "[Client Data SUCCESS] Add new widget");
1771         }
1772
1773 #ifdef CLIENT_DATA_DEBUG
1774         __show_client_list();
1775 #endif
1776         return VCD_ERROR_NONE;
1777 }
1778
1779 int vcd_client_widget_delete(int pid)
1780 {
1781         GSList *tmp = NULL;
1782         widget_info_s* widget_info = NULL;
1783
1784         /*Get handle*/
1785         tmp = __widget_get_item(pid);
1786         if (NULL == tmp) {
1787                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1788                 return VCD_ERROR_INVALID_PARAMETER;
1789         }
1790
1791         /*Free client structure*/
1792         widget_info = tmp->data;
1793         if (NULL != widget_info) {
1794                 free(widget_info);
1795         }
1796
1797         /*Remove handle from list*/
1798         g_widget_list = g_slist_remove_link(g_widget_list, tmp);
1799
1800
1801 #ifdef CLIENT_DATA_DEBUG
1802         __show_client_list();
1803 #endif
1804
1805         return VCD_ERROR_NONE;
1806 }
1807
1808 int vcd_client_widget_get_foreground_pid()
1809 {
1810         /* 1. Get foreground pid */
1811         int fg_pid = 0;
1812         if (0 != vcd_config_get_foreground(&fg_pid)) {
1813                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get foreground pid");
1814                 /* There is no foreground app for voice control */
1815         }
1816
1817         widget_info_s* widget = NULL;
1818
1819         widget = __widget_get_element(fg_pid);
1820         if (NULL == widget) {
1821                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Not found foreground pid of widget");
1822                 return -1;
1823         }
1824
1825         return widget->pid;
1826 }
1827
1828
1829 bool vcd_client_widget_is_available(int pid)
1830 {
1831         widget_info_s* widget_info = NULL;
1832
1833         widget_info = __widget_get_element(pid);
1834         if (NULL == widget_info) {
1835                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1836                 return false;
1837         }
1838
1839         return true;
1840 }
1841
1842 int vcd_client_widget_set_command(int pid)
1843 {
1844         widget_info_s *info = NULL;
1845         info = __widget_get_element(pid);
1846         if (NULL == info) {
1847                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1848                 return VCD_ERROR_INVALID_PARAMETER;
1849         }
1850
1851         info->widget_cmd = true;
1852
1853         return VCD_ERROR_NONE;
1854 }
1855
1856 int vcd_client_widget_unset_command(int pid)
1857 {
1858         widget_info_s *info = NULL;
1859         info = __widget_get_element(pid);
1860         if (NULL == info) {
1861                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1862                 return VCD_ERROR_INVALID_PARAMETER;
1863         }
1864
1865         info->widget_cmd = false;
1866
1867         return VCD_ERROR_NONE;
1868 }
1869
1870 int vcd_client_widget_set_asr_result_enabled(int pid, bool enable)
1871 {
1872         widget_info_s *info = NULL;
1873         info = __widget_get_element(pid);
1874         if (NULL == info) {
1875                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1876                 return VCD_ERROR_INVALID_PARAMETER;
1877         }
1878
1879         info->asr_result_enabled = enable;
1880
1881         return VCD_ERROR_NONE;
1882 }
1883
1884 int vcd_client_widget_get_asr_result_enabled(int pid, bool* enable)
1885 {
1886         widget_info_s *info = NULL;
1887         info = __widget_get_element(pid);
1888         if (NULL == info) {
1889                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1890                 return VCD_ERROR_INVALID_PARAMETER;
1891         }
1892
1893         *enable = info->asr_result_enabled;
1894
1895         return VCD_ERROR_NONE;
1896 }
1897
1898 int vcd_client_widget_set_waiting_for_recording(int pid, bool waiting)
1899 {
1900
1901         if (TRUE == waiting && pid != vcd_client_widget_get_foreground_pid()) {
1902                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT foreground pid", pid);
1903                 return VCD_ERROR_INVALID_PARAMETER;
1904         }
1905
1906         g_is_waiting_recording = waiting;
1907         g_waiting_recording_pid = pid;
1908         SLOG(LOG_ERROR, TAG_VCD, "[INFO] Success to set waiting for recording, pid(%d), waiting(%d)", pid, waiting);
1909         return VCD_ERROR_NONE;
1910 }
1911
1912 int vcd_client_widget_get_waiting_for_recording(int pid, bool* waiting)
1913 {
1914         if (pid != g_waiting_recording_pid) {
1915                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT waiting pid", pid);
1916                 return VCD_ERROR_INVALID_PARAMETER;
1917         }
1918
1919         *waiting = g_is_waiting_recording;
1920         SLOG(LOG_ERROR, TAG_VCD, "[INFO] Success to get waiting for recording, waiting(%d)", *waiting);
1921         return VCD_ERROR_NONE;
1922 }
1923
1924 GSList* __get_widget_tidl_info_item(const int pid)
1925 {
1926         GSList *iter = NULL;
1927         widget_tidl_info_s *data = NULL;
1928
1929         int count = g_slist_length(g_widget_tidl_info_list);
1930         int i;
1931
1932         if (0 < count) {
1933                 iter = g_slist_nth(g_widget_tidl_info_list, 0);
1934                 for (i = 0; i < count; i++) {
1935                         if (NULL == iter)
1936                                 break;
1937
1938                         data = iter->data;
1939                         if (NULL != data) {
1940                                 if (pid == data->pid)
1941                                         return iter;
1942                         }
1943
1944                         iter = g_slist_next(iter);
1945                 }
1946         }
1947
1948         return NULL;
1949 }
1950
1951 widget_tidl_info_s* __get_widget_tidl_info_element(int pid)
1952 {
1953         GSList *iter = NULL;
1954         widget_tidl_info_s *data = NULL;
1955
1956         int count = g_slist_length(g_widget_tidl_info_list);
1957         int i;
1958
1959         if (0 < count) {
1960                 iter = g_slist_nth(g_widget_tidl_info_list, 0);
1961                 for (i = 0; i < count; i++) {
1962                         if (NULL == iter)
1963                                 break;
1964
1965                         data = iter->data;
1966
1967                         if (NULL != data) {
1968                                 if (pid == data->pid)
1969                                         return data;
1970                         }
1971
1972                         iter = g_slist_next(iter);
1973                 }
1974         }
1975
1976         return NULL;
1977 }
1978
1979 int vcd_client_widget_add_tidl_info(int pid)
1980 {
1981         /*Check pid is duplicated*/
1982         widget_tidl_info_s* info = NULL;
1983         info = __get_widget_tidl_info_element(pid);
1984
1985         if (NULL != info) {
1986                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Widget tidl info pid is already registered");
1987                 return VCD_ERROR_NONE;
1988         }
1989
1990         SLOG(LOG_INFO, TAG_VCD, "[Client Data] There is no tidl info of pid(%d). Create new one.", pid);
1991         info = (widget_tidl_info_s*)calloc(1, sizeof(widget_tidl_info_s));
1992         if (NULL == info) {
1993                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1994                 return VCD_ERROR_OUT_OF_MEMORY;
1995         }
1996
1997         info->pid = pid;
1998         info->notify_cb = NULL;
1999         info->notify_cb_user_data = NULL;
2000
2001         info->connected = false;
2002         info->connection_requesting = false;
2003         info->rpc_h = NULL;
2004
2005         g_widget_tidl_info_list = g_slist_append(g_widget_tidl_info_list, info);
2006         if (NULL == g_widget_tidl_info_list) {
2007                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new widget tidl info");
2008
2009                 free(info);
2010                 info = NULL;
2011
2012                 return VCD_ERROR_OUT_OF_MEMORY;
2013         } else {
2014                 SLOG(LOG_INFO, TAG_VCD, "[Client Data SUCCESS] Add new widget tidl info. pid(%d)", pid);
2015         }
2016
2017         return VCD_ERROR_NONE;
2018 }
2019
2020 int vcd_client_widget_set_tidl_notify_cb(int pid, rpc_port_stub_vcd_widget_stub_vc_widget_notify_cb_h callback, void* user_data)
2021 {
2022         /*Check pid*/
2023         widget_tidl_info_s* info = NULL;
2024         info = __get_widget_tidl_info_element(pid);
2025
2026         if (NULL == info) {
2027                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no tidl info in the list. pid(%d)", pid);
2028                 return VCD_ERROR_INVALID_PARAMETER;
2029         }
2030
2031         int ret = -1;
2032         ret = rpc_port_stub_vcd_widget_stub_vc_widget_notify_cb_clone(callback, &(info->notify_cb));
2033         if (0 != ret) {
2034                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to clone notify callback. ret(%d)", ret);
2035         } else {
2036                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to clone notify callback. ret(%d)", ret);
2037         }
2038         info->notify_cb_user_data = user_data;
2039
2040         return VCD_ERROR_NONE;
2041 }
2042
2043 int vcd_client_widget_unset_tidl_notify_cb(int pid)
2044 {
2045         /*Check pid*/
2046         widget_tidl_info_s* info = NULL;
2047         info = __get_widget_tidl_info_element(pid);
2048
2049         if (NULL == info) {
2050                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no tidl info in the list. pid(%d)", pid);
2051                 return VCD_ERROR_INVALID_PARAMETER;
2052         }
2053
2054         int ret = -1;
2055         ret = rpc_port_stub_vcd_widget_stub_vc_widget_notify_cb_destroy(info->notify_cb);
2056         if (0 != ret) {
2057                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to destroy notify callback. ret(%d)", ret);
2058         } else {
2059                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to destroy notify callback. ret(%d)", ret);
2060         }
2061         info->notify_cb = NULL;
2062         info->notify_cb_user_data = NULL;
2063
2064         return VCD_ERROR_NONE;
2065 }
2066
2067 int vcd_client_widget_delete_tidl_info(int pid)
2068 {
2069         GSList *tmp = NULL;
2070         widget_tidl_info_s* widget_tidl_info = NULL;
2071
2072         /*Get handle*/
2073         tmp = __get_widget_tidl_info_item(pid);
2074         if (NULL == tmp) {
2075                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
2076                 return VCD_ERROR_INVALID_PARAMETER;
2077         }
2078
2079         /*Free widget structure*/
2080         widget_tidl_info = tmp->data;
2081         if (NULL != widget_tidl_info) {
2082                 free(widget_tidl_info);
2083         }
2084
2085         /*Delete handle from list*/
2086         g_widget_tidl_info_list = g_slist_delete_link(g_widget_tidl_info_list, tmp);
2087
2088         return VCD_ERROR_NONE;
2089 }
2090
2091 widget_tidl_info_s* vcd_client_widget_get_tidl_info(int pid)
2092 {
2093         return __get_widget_tidl_info_element(pid);
2094 }
2095
2096 GSList* vcd_client_widget_get_tidl_info_list()
2097 {
2098         return g_widget_tidl_info_list;
2099 }
2100
2101
2102 void vcd_client_update_foreground_pid()
2103 {
2104         int tmp_pid = VC_RUNTIME_INFO_NO_FOREGROUND;
2105         vcd_config_get_foreground(&tmp_pid);
2106
2107         int pid = VC_RUNTIME_INFO_NO_FOREGROUND;
2108         int ret = aul_window_get_focused_pid(&pid);
2109         if (0 != ret) {
2110                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get focused pid, ret(%d)", ret);
2111         }
2112         SLOG(LOG_INFO, TAG_VCD, "[INFO] Foreground pid (%d), Focused pid (%d)", tmp_pid, pid);
2113
2114         GSList *iter = NULL;
2115         vc_client_info_s *data = NULL;
2116
2117         int count = g_slist_length(g_client_list);
2118         int i;
2119
2120         if (0 == count) {
2121                 SLOG(LOG_INFO, TAG_VCD, "No Client");
2122         } else {
2123                 iter = g_slist_nth(g_client_list, 0);
2124                 for (i = 0; i < count; i++) {
2125                         if (NULL == iter)
2126                                 break;
2127
2128                         data = iter->data;
2129
2130                         if (NULL != data) {
2131                                 SLOG(LOG_INFO, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
2132                                 if (pid == data->pid) {
2133                                         SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid (%d)", data->pid);
2134                                         vcd_config_set_foreground(data->pid, true);
2135                                         if (tmp_pid != data->pid) {
2136                                                 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid is different");
2137                                         }
2138                                         return;
2139                                 }
2140                         }
2141                         iter = g_slist_next(iter);
2142                 }
2143         }
2144
2145         widget_info_s *widget_data = NULL;
2146         count = g_slist_length(g_widget_list);
2147
2148         if (0 == count) {
2149                 SLOG(LOG_INFO, TAG_VCD, "No widget");
2150         } else {
2151                 iter = g_slist_nth(g_widget_list, 0);
2152                 for (i = 0; i < count; i++) {
2153                         if (NULL == iter)
2154                                 break;
2155
2156                         widget_data = iter->data;
2157
2158                         if (NULL != widget_data) {
2159                                 SLOG(LOG_INFO, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
2160                                 if (pid == widget_data->pid) {
2161                                         SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid (%d)", widget_data->pid);
2162                                         vcd_config_set_foreground(widget_data->pid, true);
2163                                         if (tmp_pid != widget_data->pid) {
2164                                                 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid is changed");
2165                                         }
2166                                         return;
2167                                 }
2168                         }
2169                         iter = g_slist_next(iter);
2170                 }
2171         }
2172
2173         SLOG(LOG_INFO, TAG_VCD, "[INFO] No foreground");
2174         vcd_config_set_foreground(VC_RUNTIME_INFO_NO_FOREGROUND, true);
2175         return;
2176 }
2177
2178 /*
2179 * setting API
2180 */
2181 GSList* __get_setting_tidl_info_item(const int pid)
2182 {
2183         GSList *iter = NULL;
2184         setting_tidl_info_s *data = NULL;
2185
2186         int count = g_slist_length(g_setting_tidl_info_list);
2187         int i;
2188
2189         if (0 < count) {
2190                 iter = g_slist_nth(g_setting_tidl_info_list, 0);
2191                 for (i = 0; i < count; i++) {
2192                         if (NULL == iter)
2193                                 break;
2194
2195                         data = iter->data;
2196                         if (NULL != data) {
2197                                 if (pid == data->pid)
2198                                         return iter;
2199                         }
2200
2201                         iter = g_slist_next(iter);
2202                 }
2203         }
2204
2205         return NULL;
2206 }
2207
2208 setting_tidl_info_s* __get_setting_tidl_info_element(int pid)
2209 {
2210         GSList *iter = NULL;
2211         setting_tidl_info_s *data = NULL;
2212
2213         int count = g_slist_length(g_setting_tidl_info_list);
2214         int i;
2215
2216         if (0 < count) {
2217                 iter = g_slist_nth(g_setting_tidl_info_list, 0);
2218                 for (i = 0; i < count; i++) {
2219                         if (NULL == iter)
2220                                 break;
2221
2222                         data = iter->data;
2223
2224                         if (NULL != data) {
2225                                 if (pid == data->pid)
2226                                         return data;
2227                         }
2228
2229                         iter = g_slist_next(iter);
2230                 }
2231         }
2232
2233         return NULL;
2234 }
2235
2236 int vcd_client_setting_add_tidl_info(int pid)
2237 {
2238         /*Check pid is duplicated*/
2239         setting_tidl_info_s* info = NULL;
2240         info = __get_setting_tidl_info_element(pid);
2241
2242         if (NULL != info) {
2243                 SLOG(LOG_WARN, TAG_VCD, "[Setting Data] Setting tidl info pid is already registered");
2244                 return VCD_ERROR_NONE;
2245         }
2246
2247         SLOG(LOG_INFO, TAG_VCD, "[Setting Data] There is no tidl info of pid(%d). Create new one.", pid);
2248         info = (setting_tidl_info_s*)calloc(1, sizeof(setting_tidl_info_s));
2249         if (NULL == info) {
2250                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
2251                 return VCD_ERROR_OUT_OF_MEMORY;
2252         }
2253
2254         info->pid = pid;
2255         info->notify_cb = NULL;
2256         info->notify_cb_user_data = NULL;
2257
2258         g_setting_tidl_info_list = g_slist_append(g_setting_tidl_info_list, info);
2259         if (NULL == g_setting_tidl_info_list) {
2260                 SLOG(LOG_ERROR, TAG_VCD, "[Setting Data ERROR] Fail to add new setting client tidl info");
2261
2262                 free(info);
2263                 info = NULL;
2264
2265                 return VCD_ERROR_OUT_OF_MEMORY;
2266         } else {
2267                 SLOG(LOG_INFO, TAG_VCD, "[Setting Data SUCCESS] Add new setting client tidl info. pid(%d)", pid);
2268         }
2269
2270         return VCD_ERROR_NONE;
2271 }
2272
2273 int vcd_client_setting_set_tidl_notify_cb(int pid, rpc_port_stub_vcd_setting_stub_vc_setting_notify_cb_h callback, void* user_data)
2274 {
2275         /*Check pid*/
2276         setting_tidl_info_s* info = NULL;
2277         info = __get_setting_tidl_info_element(pid);
2278
2279         if (NULL == info) {
2280                 SLOG(LOG_ERROR, TAG_VCD, "[Setting Data ERROR] There is no tidl info in the list. pid(%d)", pid);
2281                 return VCD_ERROR_INVALID_PARAMETER;
2282         }
2283
2284         int ret = -1;
2285         ret = rpc_port_stub_vcd_setting_stub_vc_setting_notify_cb_clone(callback, &(info->notify_cb));
2286         if (0 != ret) {
2287                 SLOG(LOG_ERROR, TAG_VCD, "[Setting Data ERROR] Fail to clone notify callback. ret(%d)", ret);
2288         } else {
2289                 SLOG(LOG_INFO, TAG_VCD, "[Setting Data] Succeed to clone notify callback. ret(%d)", ret);
2290         }
2291         info->notify_cb_user_data = user_data;
2292
2293         return VCD_ERROR_NONE;
2294 }
2295
2296 int vcd_client_setting_unset_tidl_notify_cb(int pid)
2297 {
2298         /*Check pid*/
2299         setting_tidl_info_s* info = NULL;
2300         info = __get_setting_tidl_info_element(pid);
2301
2302         if (NULL == info) {
2303                 SLOG(LOG_ERROR, TAG_VCD, "[Setting Data ERROR] There is no tidl info in the list. pid(%d)", pid);
2304                 return VCD_ERROR_INVALID_PARAMETER;
2305         }
2306
2307         int ret = -1;
2308         ret = rpc_port_stub_vcd_setting_stub_vc_setting_notify_cb_destroy(info->notify_cb);
2309         if (0 != ret) {
2310                 SLOG(LOG_ERROR, TAG_VCD, "[Setting Data ERROR] Fail to destroy notify callback. ret(%d)", ret);
2311         } else {
2312                 SLOG(LOG_INFO, TAG_VCD, "[Setting Data] Succeed to destroy notify callback. ret(%d)", ret);
2313         }
2314         info->notify_cb = NULL;
2315         info->notify_cb_user_data = NULL;
2316
2317         return VCD_ERROR_NONE;
2318 }
2319
2320 int vcd_client_setting_delete_tidl_info(int pid)
2321 {
2322         GSList *tmp = NULL;
2323         setting_tidl_info_s* setting_tidl_info = NULL;
2324
2325         /*Get handle*/
2326         tmp = __get_setting_tidl_info_item(pid);
2327         if (NULL == tmp) {
2328                 SLOG(LOG_ERROR, TAG_VCD, "[Setting Data ERROR] pid(%d) is NOT valid", pid);
2329                 return VCD_ERROR_INVALID_PARAMETER;
2330         }
2331
2332         /*Free setting client structure*/
2333         setting_tidl_info = tmp->data;
2334         if (NULL != setting_tidl_info) {
2335                 free(setting_tidl_info);
2336         }
2337
2338         /*Delete handle from list*/
2339         g_setting_tidl_info_list = g_slist_delete_link(g_setting_tidl_info_list, tmp);
2340
2341         return VCD_ERROR_NONE;
2342 }
2343
2344 setting_tidl_info_s* vcd_client_setting_get_tidl_info(int pid)
2345 {
2346         return __get_setting_tidl_info_element(pid);
2347 }
2348
2349 int vcd_client_setting_get_tidl_list(int** pids, int* pid_count)
2350 {
2351         if (NULL == pids || NULL == pid_count)
2352                 return VCD_ERROR_INVALID_PARAMETER;
2353
2354         int count = g_slist_length(g_setting_tidl_info_list);
2355
2356         if (0 == count)
2357                 return VCD_ERROR_OUT_OF_MEMORY;
2358
2359         int *tmp;
2360         tmp = (int*)calloc(count, sizeof(int));
2361         if (NULL == tmp) {
2362                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
2363                 return VCD_ERROR_OUT_OF_MEMORY;
2364         }
2365
2366         GSList *iter = NULL;
2367         setting_tidl_info_s *data = NULL;
2368         int i = 0;
2369
2370         iter = g_slist_nth(g_setting_tidl_info_list, 0);
2371
2372         while (NULL != iter) {
2373                 data = iter->data;
2374
2375                 if (NULL != data) {
2376                         tmp[i] = data->pid;
2377                 }
2378
2379                 iter = g_slist_next(iter);
2380                 i++;
2381         }
2382
2383         *pids = tmp;
2384         *pid_count = count;
2385
2386         return VCD_ERROR_NONE;
2387 }