Fix memory leak
[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 #include "voice_control_command_expand.h"
26
27 /* Client list */
28 static GSList* g_client_list = NULL;
29
30 static GSList* g_widget_list = NULL;
31
32 static manager_info_s g_manager;
33
34 /* Command list */
35 static current_commands_list_s g_cur_cmd_list;
36
37 /* Demandable client list */
38 static GSList* g_demandable_client = NULL;
39
40 /* Runtime info */
41 static bool g_silence_detection;
42 static vcd_recognition_mode_e g_recognition_mode;
43 static char* g_result_text = NULL;
44 static bool g_is_waiting_recording = false;
45 static int g_waiting_recording_pid = -1;
46
47 /* Function definitions */
48 widget_info_s* __widget_get_element(int pid);
49
50 vc_client_info_s* __client_get_element(int pid);
51
52
53 int vcd_client_manager_set(int pid)
54 {
55         // Get appid by pid using app control
56         char* appid = NULL;
57         int ret = app_manager_get_app_id(pid, &appid);
58         if (0 != ret || NULL == appid) {
59                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
60                 return -1;
61         }
62
63         if (-1 != g_manager.pid && NULL != g_manager.appid && 0 == strncmp(g_manager.appid, appid, strlen(g_manager.appid))) {
64                 SLOG(LOG_WARN, TAG_VCD, "Same manager has already registered. It doesn't need to set manager again.");
65                 if (appid)
66                         free(appid);
67                 return 0;
68         }
69
70         g_manager.pid = pid;
71         g_manager.manager_cmd = false;
72         g_manager.exclusive_cmd_option = false;
73         g_manager.appid = NULL;
74
75         g_manager.appid = strdup(appid);
76
77         free(appid);
78         appid = NULL;
79         return 0;
80 }
81
82 int vcd_client_manager_unset()
83 {
84         g_manager.pid = -1;
85         g_manager.manager_cmd = false;
86         g_manager.exclusive_cmd_option = false;
87
88         return 0;
89 }
90
91 int vcd_client_manager_unset_appid()
92 {
93         if (NULL != g_manager.appid) {
94                 free(g_manager.appid);
95                 g_manager.appid = NULL;
96         }
97         return 0;
98 }
99
100 bool vcd_client_manager_is_valid(int pid)
101 {
102         if (-1 == g_manager.pid || pid == g_manager.pid) {
103                 return true;
104         }
105         return false;
106 }
107
108 int vcd_client_manager_set_command(int pid)
109 {
110         if (pid != g_manager.pid) {
111                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
112                 return -1;
113         }
114         g_manager.manager_cmd = true;
115         return 0;
116 }
117
118 int vcd_client_manager_unset_command(int pid)
119 {
120         if (pid != g_manager.pid) {
121                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
122                 return -1;
123         }
124         g_manager.manager_cmd = false;
125         return 0;
126 }
127
128 bool vcd_client_manager_is_system_command_valid(int pid)
129 {
130         if (pid != g_manager.pid) {
131                 SLOG(LOG_WARN, TAG_VCD, "[Client Data WARNING] pid(%d) is NOT valid", pid);
132                 return false;
133         }
134
135         if (true == g_manager.manager_cmd)
136                 return true;
137
138         return false;
139 }
140
141 int vcd_client_manager_set_demandable_client(int pid, GSList* client_list)
142 {
143         if (0 != g_slist_length(g_demandable_client)) {
144                 /* release data */
145                 GSList *iter = NULL;
146                 vc_demandable_client_s* temp_client;
147                 iter = g_slist_nth(g_demandable_client, 0);
148
149                 while (NULL != iter) {
150                         temp_client = iter->data;
151
152                         if (NULL != temp_client) {
153                                 if (NULL != temp_client->appid)         free(temp_client->appid);
154                                 free(temp_client);
155                         }
156
157                         iter = g_slist_next(iter);
158                 }
159                 g_demandable_client = NULL;
160         }
161
162         g_demandable_client = client_list;
163
164         return 0;
165 }
166
167 bool vcd_client_manager_check_demandable_client(int pid)
168 {
169         if (0 == g_slist_length(g_demandable_client)) {
170                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] All client is available to request start");
171                 return true;
172         }
173
174         /* Check demandable appid */
175         char appid[1024] = {0, };
176         if (0 == aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
177                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] %s(%d) requests start", appid, pid);
178         } else {
179                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] daemon(%d) requests start", pid);
180         }
181
182         /* Compare appid */
183         GSList *iter = NULL;
184         vc_demandable_client_s* temp_client;
185         iter = g_slist_nth(g_demandable_client, 0);
186
187         while (NULL != iter) {
188                 temp_client = iter->data;
189
190                 if (NULL != temp_client) {
191
192                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] demandable appid(%s)", temp_client->appid);
193
194                         if (NULL != temp_client->appid) {
195                                 if (0 == strcmp(temp_client->appid, appid)) {
196                                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] pid(%d) is available", pid);
197                                         return true;
198                                 }
199                         } else {
200                                 if (0 == strlen(appid)) {
201                                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] pid(%d) is available", pid);
202                                         return true;
203                                 }
204                         }
205                 }
206
207                 iter = g_slist_next(iter);
208         }
209
210         return false;
211 }
212
213 bool vcd_client_manager_get_exclusive()
214 {
215         return g_manager.exclusive_cmd_option;
216 }
217
218 int vcd_client_manager_set_exclusive(bool value)
219 {
220         g_manager.exclusive_cmd_option = value;
221         return 0;
222 }
223
224 int vcd_client_manager_get_pid()
225 {
226         return g_manager.pid;
227 }
228
229 int vcd_client_manager_get_appid(char** appid)
230 {
231         if (NULL != g_manager.appid)
232                 *appid = strdup(g_manager.appid);
233
234         return 0;
235 }
236
237 int vcd_client_manager_set_result_text(const char* result)
238 {
239         if (NULL != g_result_text) {
240                 free(g_result_text);
241                 g_result_text = NULL;
242         }
243
244         if (NULL != result) {
245                 g_result_text = strdup(result);
246         }
247
248         return 0;
249 }
250
251 char* vcd_client_manager_get_result_text()
252 {
253         return g_result_text;
254 }
255
256 static void __vcd_client_release_each_commands(GSList** cmds)
257 {
258         GSList *iter = NULL;
259         vc_cmd_s* temp_cmd;
260
261         if (0 < g_slist_length(*cmds)) {
262                 iter = g_slist_nth(*cmds, 0);
263                 while (NULL != iter) {
264                         temp_cmd = iter->data;
265
266                         if (NULL != temp_cmd) {
267                                 if (NULL != temp_cmd->command) {
268                                         free(temp_cmd->command);
269                                         temp_cmd->command = NULL;
270                                 }
271                                 if (NULL != temp_cmd->appid) {
272                                         free(temp_cmd->appid);
273                                         temp_cmd->appid = NULL;
274                                 }
275                                 if (NULL != temp_cmd->parameter) {
276                                         free(temp_cmd->parameter);
277                                         temp_cmd->parameter = NULL;
278                                 }
279                                 if (NULL != temp_cmd->invocation_name) {
280                                         free(temp_cmd->invocation_name);
281                                         temp_cmd->invocation_name = NULL;
282                                 }
283                                 if (NULL != temp_cmd->fixed) {
284                                         free(temp_cmd->fixed);
285                                         temp_cmd->fixed = NULL;
286                                 }
287                                 free(temp_cmd);
288                                 temp_cmd = NULL;
289                         }
290                         *cmds = g_slist_remove_link(*cmds, iter);
291
292                         iter = g_slist_nth(*cmds, 0);
293                 }
294                 *cmds = NULL;
295         }
296 }
297
298 int __vcd_client_release_commands()
299 {
300         g_cur_cmd_list.total_cmd_count = 0;
301         g_cur_cmd_list.foreground = VC_NO_FOREGROUND_PID;
302
303         __vcd_client_release_each_commands(&(g_cur_cmd_list.widget_cmds));
304         __vcd_client_release_each_commands(&(g_cur_cmd_list.system_cmds));
305         __vcd_client_release_each_commands(&(g_cur_cmd_list.system_background_cmds));
306         __vcd_client_release_each_commands(&(g_cur_cmd_list.exclusive_system_cmds));
307         __vcd_client_release_each_commands(&(g_cur_cmd_list.foreground_cmds));
308         __vcd_client_release_each_commands(&(g_cur_cmd_list.background_cmds));
309
310         return 0;
311 }
312
313 int vcd_client_command_collect_command()
314 {
315         /* 1. Get foreground pid */
316         int fg_pid = 0;
317         int ret = -1;
318         if (0 != vcd_config_get_foreground(&fg_pid)) {
319                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get foreground pid");
320                 /* There is no foreground app for voice control */
321         }
322
323         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Foreground pid(%d)", fg_pid);
324
325         /* 2. Clean up command list */
326         __vcd_client_release_commands();
327
328         /* Check exclusive system command */
329         if (true == g_manager.exclusive_cmd_option && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_EXCLUSIVE)) {
330                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Exclusive option of manager is ON");
331
332                 GSList* ex_sys_cmd_list = NULL;
333                 if (true == g_manager.manager_cmd) {
334                         ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_EXCLUSIVE, &ex_sys_cmd_list);
335                         if (0 != ret) {
336                                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
337                         } else {
338                                 g_cur_cmd_list.exclusive_system_cmds = ex_sys_cmd_list;
339                         }
340                 } else {
341                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No exclusive system commands");
342                 }
343
344                 return 0;
345         }
346
347         /* 3. Set system command */
348         GSList* sys_cmd_list = NULL;
349         if (true == g_manager.manager_cmd && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_SYSTEM)) {
350                 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_SYSTEM, &sys_cmd_list);
351                 if (0 != ret) {
352                         SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
353                 } else {
354                         g_cur_cmd_list.system_cmds = sys_cmd_list;
355                 }
356         } else {
357                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system commands");
358         }
359
360         GSList* sys_back_cmd_list = NULL;
361         if (true == g_manager.manager_cmd && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_SYSTEM_BACKGROUND)) {
362                 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_SYSTEM_BACKGROUND, &sys_back_cmd_list);
363                 if (0 != ret) {
364                         SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
365                 } else {
366                         g_cur_cmd_list.system_background_cmds = sys_back_cmd_list;
367                 }
368         } else {
369                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system background commands");
370         }
371
372         /* 4. Set foreground commands and widget */
373         GSList* fg_cmd_list = NULL;
374         if (VC_NO_FOREGROUND_PID != fg_pid) {
375                 GSList* widget_cmd_list = NULL;
376
377                 g_cur_cmd_list.foreground = fg_pid;
378
379                 /* 4-1. Set widget command */
380                 widget_info_s* widget_info = NULL;
381                 widget_info = __widget_get_element(fg_pid);
382                 if (NULL != widget_info && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
383                         if (true == widget_info->widget_cmd) {
384                                 ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_WIDGET, &widget_cmd_list);
385                                 if (0 != ret) {
386                                         SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the WIDGET command list");
387                                 } else {
388                                         g_cur_cmd_list.widget_cmds = widget_cmd_list;
389                                 }
390                         }
391                 } else {
392                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No widget commands");
393                 }
394
395                 /* 4-2. Set foreground command of foreground app */
396                 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_FOREGROUND)) {
397                         ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_FOREGROUND, &fg_cmd_list);
398                         if (0 != ret) {
399                                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the fg command list");
400                         } else {
401                                 g_cur_cmd_list.foreground_cmds = fg_cmd_list;
402                         }
403                 }
404         } else {
405                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground app");
406         }
407         if (true == g_manager.manager_cmd && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_FOREGROUND)) {
408                 /* 4-3. Set foreground command by manager */
409                 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_FOREGROUND, &fg_cmd_list);
410                 if (0 != ret) {
411                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground commands of manager app");
412                 } else {
413                         g_cur_cmd_list.foreground_cmds = fg_cmd_list;
414                 }
415         }
416
417         /* 5. Set background commands */
418         GSList* bg_cmd_list = NULL;
419         if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_BACKGROUND)) {
420                 ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_BACKGROUND, &bg_cmd_list);
421                 if (0 != ret) {
422                         SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the bg command list");
423                 } else {
424                         /* Add item to global command list */
425                         g_cur_cmd_list.background_cmds = bg_cmd_list;
426                 }
427         }
428         return 0;
429 }
430
431 int vcd_client_get_length()
432 {
433         int command_count = 0;
434         command_count += g_slist_length(g_cur_cmd_list.widget_cmds);
435         command_count += g_slist_length(g_cur_cmd_list.system_cmds);
436         command_count += g_slist_length(g_cur_cmd_list.exclusive_system_cmds);
437         command_count += g_slist_length(g_cur_cmd_list.system_background_cmds);
438         command_count += g_slist_length(g_cur_cmd_list.foreground_cmds);
439         command_count += g_slist_length(g_cur_cmd_list.background_cmds);
440
441         g_cur_cmd_list.total_cmd_count = command_count;
442
443         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Command count : %d ", g_cur_cmd_list.total_cmd_count);
444         return command_count;
445 }
446
447 int vcd_client_foreach_command(client_foreach_command_cb callback, void* user_data)
448 {
449         if (NULL == callback) {
450                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] input parameter is NULL");
451                 return VCD_ERROR_INVALID_PARAMETER;
452         }
453
454         if (0 != vcd_client_command_collect_command()) {
455                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
456                 return VCD_ERROR_OPERATION_FAILED;
457         }
458
459         if (0 >= vcd_client_get_length()) {
460                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] No current command");
461                 return VCD_ERROR_OPERATION_FAILED;
462         }
463
464 //      int id_count = 1;
465         GSList *iter = NULL;
466         vc_cmd_s* temp_cmd;
467
468         if (0 < g_slist_length(g_cur_cmd_list.widget_cmds)) {
469                 iter = g_slist_nth(g_cur_cmd_list.widget_cmds, 0);
470                 while (NULL != iter) {
471                         temp_cmd = iter->data;
472
473                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Widget : id(%d) type(%d) format(%d) command(%s) domain(%d)"
474                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->domain);
475
476                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
477
478                         iter = g_slist_next(iter);
479                 }
480         } else {
481                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No widget commands");
482         }
483
484         if (0 < g_slist_length(g_cur_cmd_list.foreground_cmds)) {
485                 iter = g_slist_nth(g_cur_cmd_list.foreground_cmds, 0);
486                 while (NULL != iter) {
487                         temp_cmd = iter->data;
488
489                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Fore : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d) fixed(%s)"
490                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, temp_cmd->fixed);
491
492                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
493
494                         iter = g_slist_next(iter);
495                 }
496         } else {
497                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground commands");
498         }
499
500         if (0 < g_slist_length(g_cur_cmd_list.system_cmds)) {
501                 iter = g_slist_nth(g_cur_cmd_list.system_cmds, 0);
502                 while (NULL != iter) {
503                         temp_cmd = iter->data;
504
505                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] System : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d)"
506                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
507
508                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
509
510                         iter = g_slist_next(iter);
511                 }
512         } else {
513                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system commands");
514         }
515
516         if (0 < g_slist_length(g_cur_cmd_list.system_background_cmds)) {
517                 iter = g_slist_nth(g_cur_cmd_list.system_background_cmds, 0);
518                 while (NULL != iter) {
519                         temp_cmd = iter->data;
520
521                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] System background : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d)"
522                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
523
524                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
525
526                         iter = g_slist_next(iter);
527                 }
528         } else {
529                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system background commands");
530         }
531
532         if (0 < g_slist_length(g_cur_cmd_list.exclusive_system_cmds)) {
533                 iter = g_slist_nth(g_cur_cmd_list.exclusive_system_cmds, 0);
534                 while (NULL != iter) {
535                         temp_cmd = iter->data;
536
537                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Exclusive system : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d)"
538                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
539
540                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
541
542                         iter = g_slist_next(iter);
543                 }
544         } else {
545                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No exclusive system commands");
546         }
547
548         if (0 < g_slist_length(g_cur_cmd_list.background_cmds)) {
549                 iter = g_slist_nth(g_cur_cmd_list.background_cmds, 0);
550                 while (NULL != iter) {
551                         temp_cmd = iter->data;
552
553                         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)"
554                                  , 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);
555
556                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
557
558                         iter = g_slist_next(iter);
559                 }
560         } else {
561                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No background commands");
562         }
563         return 0;
564 }
565
566 static vc_cmd_s* __command_copy(vc_cmd_s* src_cmd)
567 {
568         if (NULL == src_cmd) {
569                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Input command is NULL");
570                 return NULL;
571         }
572
573         vc_cmd_s* temp_cmd = NULL;
574         temp_cmd = (vc_cmd_s*)calloc(sizeof(vc_cmd_s), 1);
575         if (NULL == temp_cmd) {
576                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
577                 return NULL;
578         }
579
580         temp_cmd->id = src_cmd->id;
581         temp_cmd->pid = src_cmd->pid;
582         temp_cmd->index = src_cmd->index;
583         temp_cmd->type = src_cmd->type;
584         temp_cmd->format = src_cmd->format;
585         temp_cmd->domain = src_cmd->domain;
586
587         if (VC_COMMAND_TYPE_SYSTEM == temp_cmd->type)           temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM;
588         else if (VC_COMMAND_TYPE_EXCLUSIVE == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_EXCLUSIVE;
589         else if (VC_COMMAND_TYPE_WIDGET == temp_cmd->type)      temp_cmd->priority = VC_COMMAND_PRIORITY_WIDGET;
590         else if (VC_COMMAND_TYPE_FOREGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
591         else if (VC_COMMAND_TYPE_SYSTEM_BACKGROUND == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM_BACKGROUND;
592         else if (VC_COMMAND_TYPE_BACKGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_BACKGROUND;
593
594         if (NULL != src_cmd->command) {
595                 temp_cmd->command = strdup(src_cmd->command);
596         }
597
598         if (NULL != src_cmd->parameter) {
599                 temp_cmd->parameter = strdup(src_cmd->parameter);
600         }
601
602         if (NULL != src_cmd->appid) {
603                 temp_cmd->appid = strdup(src_cmd->appid);
604         }
605
606         if (NULL != src_cmd->invocation_name) {
607                 temp_cmd->invocation_name = strdup(src_cmd->invocation_name);
608         }
609
610         if (NULL != src_cmd->fixed) {
611                 temp_cmd->fixed = strdup(src_cmd->fixed);
612         }
613
614         temp_cmd->key = src_cmd->key;
615         temp_cmd->modifier = src_cmd->modifier;
616
617         return temp_cmd;
618 }
619
620 int vcd_client_get_cmd_from_result_id(int result_id, vc_cmd_s** result)
621 {
622         GSList *iter = NULL;
623         vc_cmd_s* temp_cmd;
624
625         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Result id(%d)", result_id);
626
627         if (0 < g_slist_length(g_cur_cmd_list.widget_cmds)) {
628                 iter = g_slist_nth(g_cur_cmd_list.widget_cmds, 0);
629                 while (NULL != iter) {
630                         temp_cmd = iter->data;
631
632                         if (result_id == temp_cmd->id) {
633                                 /**pid = g_cur_cmd_list.foreground; */
634                                 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_UI_CONTROL; */
635                                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
636
637                                 *result = __command_copy(temp_cmd);
638
639                                 return 0;
640                         }
641
642                         iter = g_slist_next(iter);
643                 }
644         } else {
645                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No widget commands");
646         }
647
648         if (0 < g_slist_length(g_cur_cmd_list.foreground_cmds)) {
649                 iter = g_slist_nth(g_cur_cmd_list.foreground_cmds, 0);
650
651                 while (NULL != iter) {
652                         temp_cmd = iter->data;
653
654                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] command id (%d)", temp_cmd->id);
655
656                         if (result_id == temp_cmd->id) {
657                                 /**pid = g_cur_cmd_list.foreground; */
658                                 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_FOREGROUND; */
659                                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
660
661                                 *result = __command_copy(temp_cmd);
662                                 return 0;
663                         }
664
665                         iter = g_slist_next(iter);
666                 }
667         } else {
668                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground commands");
669         }
670
671         if (0 < g_slist_length(g_cur_cmd_list.system_cmds)) {
672                 iter = g_slist_nth(g_cur_cmd_list.system_cmds, 0);
673                 while (NULL != iter) {
674                         temp_cmd = iter->data;
675
676                         if (result_id == temp_cmd->id) {
677                                 /**pid = g_manager.pid; */
678                                 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM; */
679                                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
680
681                                 *result = __command_copy(temp_cmd);
682                                 return 0;
683                         }
684
685                         iter = g_slist_next(iter);
686                 }
687         } else {
688                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system commands");
689         }
690
691         if (0 < g_slist_length(g_cur_cmd_list.system_background_cmds)) {
692                 iter = g_slist_nth(g_cur_cmd_list.system_background_cmds, 0);
693                 while (NULL != iter) {
694                         temp_cmd = iter->data;
695
696                         if (result_id == temp_cmd->id) {
697                                 /**pid = g_manager.pid; */
698                                 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM_BACKGROUND; */
699                                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
700
701                                 *result = __command_copy(temp_cmd);
702                                 return 0;
703                         }
704
705                         iter = g_slist_next(iter);
706                 }
707         } else {
708                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system background commands");
709         }
710
711         if (0 < g_slist_length(g_cur_cmd_list.exclusive_system_cmds)) {
712                 iter = g_slist_nth(g_cur_cmd_list.exclusive_system_cmds, 0);
713                 while (NULL != iter) {
714                         temp_cmd = iter->data;
715
716                         if (result_id == temp_cmd->id) {
717                                 /**pid = g_manager.pid; */
718                                 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM_EXCLUSIVE; */
719                                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
720
721                                 *result = __command_copy(temp_cmd);
722                                 return 0;
723                         }
724
725                         iter = g_slist_next(iter);
726                 }
727         } else {
728                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No exclusive system commands");
729         }
730
731         if (0 < g_slist_length(g_cur_cmd_list.background_cmds)) {
732                 iter = g_slist_nth(g_cur_cmd_list.background_cmds, 0);
733
734                 while (NULL != iter) {
735                         temp_cmd = iter->data;
736
737                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] command id (%d)", temp_cmd->id);
738
739                         if (result_id == temp_cmd->id) {
740                                 *result = __command_copy(temp_cmd);
741                                 return 0;
742                         }
743
744                         iter = g_slist_next(iter);
745                 }
746         } else {
747                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No background commands");
748         }
749
750         SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Not find matched result");
751         return -1;
752 }
753
754 int vcd_client_set_slience_detection(bool value)
755 {
756         g_silence_detection = value;
757         return 0;
758 }
759
760 bool vcd_client_get_slience_detection()
761 {
762         return g_silence_detection;
763 }
764
765 int vcd_client_set_recognition_mode(vcd_recognition_mode_e mode)
766 {
767         g_recognition_mode = mode;
768         return 0;
769 }
770
771 vcd_recognition_mode_e vcd_client_get_recognition_mode()
772 {
773         return g_recognition_mode;
774 }
775
776 int vcd_client_set_server_dialog(int pid, bool is_server_dialog)
777 {
778         vc_client_info_s* client_info = NULL;
779
780         client_info = __client_get_element(pid);
781         if (NULL == client_info) {
782                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
783                 return VCD_ERROR_INVALID_PARAMETER;
784         }
785
786         client_info->server_dialog = is_server_dialog;
787
788         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Set server dialog, client pid(%d), server_dialog(%d)", pid, client_info->server_dialog);
789         return 0;
790 }
791
792 int vcd_client_get_server_dialog(int pid, bool* is_server_dialog)
793 {
794         vc_client_info_s* client_info = NULL;
795
796         client_info = __client_get_element(pid);
797         if (NULL == client_info) {
798                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
799                 return VCD_ERROR_INVALID_PARAMETER;
800         }
801
802         *is_server_dialog = client_info->server_dialog;
803
804         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Get server dialog, client pid(%d), server_dialog(%d)", pid, *is_server_dialog);
805         return 0;
806 }
807
808 int vcd_client_append_cmd_from_type(int type, vc_cmd_list_h list)
809 {
810         GSList *item = NULL;
811
812         if (NULL == list) {
813                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid list parameter");
814                 return VCD_ERROR_INVALID_PARAMETER;
815         }
816
817         switch (type) {
818         case VC_COMMAND_TYPE_SYSTEM:
819                 item = g_cur_cmd_list.system_cmds;
820                 break;
821         case VC_COMMAND_TYPE_FOREGROUND:
822                 item = g_cur_cmd_list.foreground_cmds;
823                 break;
824         case VC_COMMAND_TYPE_WIDGET:
825                 item = g_cur_cmd_list.widget_cmds;
826                 break;
827         case VC_COMMAND_TYPE_SYSTEM_BACKGROUND:
828                 item = g_cur_cmd_list.system_background_cmds;
829                 break;
830         case VC_COMMAND_TYPE_BACKGROUND:
831                 item = g_cur_cmd_list.background_cmds;
832                 break;
833         case VC_COMMAND_TYPE_EXCLUSIVE:
834                 item = g_cur_cmd_list.exclusive_system_cmds;
835                 break;
836         default:
837                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid type parameter");
838                 return VCD_ERROR_INVALID_PARAMETER;
839         }
840
841         while (NULL != item) {
842                 vc_cmd_h temp_cmd = NULL;
843                 vc_cmd_h target_cmd = (vc_cmd_h)item->data;
844
845                 temp_cmd = (vc_cmd_h)__command_copy((vc_cmd_s*)target_cmd);
846
847                 if (NULL == temp_cmd) {
848                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to copy the command");
849                         return VCD_ERROR_OUT_OF_MEMORY;
850                 }
851
852                 if (0 != vc_cmd_list_add(list, temp_cmd)) {
853                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add the command in the list");
854                         if (NULL != temp_cmd) {
855                                 vc_cmd_destroy(temp_cmd);
856                                 temp_cmd = NULL;
857                         }
858
859                         return VCD_ERROR_OPERATION_FAILED;
860                 }
861
862                 item = item->next;
863         }
864
865         return 0;
866 }
867
868 int __show_client_list()
869 {
870         GSList *iter = NULL;
871         vc_client_info_s *data = NULL;
872
873         SLOG(LOG_DEBUG, TAG_VCD, "@@@ client list");
874
875         int count = g_slist_length(g_client_list);
876         int i;
877
878         if (0 == count) {
879                 SLOG(LOG_DEBUG, TAG_VCD, "No Client");
880         } else {
881                 iter = g_slist_nth(g_client_list, 0);
882                 for (i = 0; i < count; i++) {
883                         if (NULL == iter)
884                                 break;
885
886                         data = iter->data;
887
888                         if (NULL != data) {
889                                 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
890                         }
891                         iter = g_slist_next(iter);
892                 }
893         }
894
895         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
896
897         SLOG(LOG_DEBUG, TAG_VCD, "@@@ widget list");
898
899         widget_info_s *widget_data = NULL;
900
901         count = g_slist_length(g_widget_list);
902
903         if (0 == count) {
904                 SLOG(LOG_DEBUG, TAG_VCD, "No widget");
905         } else {
906                 iter = g_slist_nth(g_widget_list, 0);
907                 for (i = 0; i < count; i++) {
908                         if (NULL == iter)
909                                 break;
910
911                         widget_data = iter->data;
912
913                         if (NULL != widget_data) {
914                                 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
915                         }
916                         iter = g_slist_next(iter);
917                 }
918         }
919
920         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
921
922         return 0;
923 }
924
925 int __show_command_list(GSList* cmd_group)
926 {
927         GSList *iter = NULL;
928         vc_cmd_s *data = NULL;
929
930         SLOG(LOG_DEBUG, TAG_VCD, "@@@ command group");
931
932         int count = g_slist_length(cmd_group);
933         int i;
934
935         if (0 == count) {
936                 SLOG(LOG_DEBUG, TAG_VCD, "No command");
937         } else {
938                 iter = g_slist_nth(cmd_group, 0);
939                 for (i = 0; i < count; i++) {
940                         if (NULL == iter)
941                                 break;
942
943                         data = iter->data;
944                         if (NULL != data) {
945                                 if (NULL != data->parameter) {
946                                         SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) parameter(%s) key(%d)", i, data->command, data->parameter, data->key);
947                                 } else {
948                                         SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) key(%d)", i, data->command, data->key);
949                                 }
950                         }
951                         iter = g_slist_next(iter);
952                 }
953         }
954
955         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
956
957         return 0;
958 }
959
960 GSList* __client_get_item(const int pid)
961 {
962         GSList *iter = NULL;
963         vc_client_info_s *data = NULL;
964
965         int count = g_slist_length(g_client_list);
966         int i;
967
968         if (0 < count) {
969                 iter = g_slist_nth(g_client_list, 0);
970                 for (i = 0; i < count; i++) {
971                         if (NULL == iter)
972                                 break;
973
974                         data = iter->data;
975                         if (NULL != data) {
976                                 if (pid == data->pid)
977                                         return iter;
978                         }
979
980                         iter = g_slist_next(iter);
981                 }
982         }
983
984         return NULL;
985 }
986
987 vc_client_info_s* __client_get_element(int pid)
988 {
989         GSList *iter = NULL;
990         vc_client_info_s *data = NULL;
991
992         int count = g_slist_length(g_client_list);
993         int i;
994
995         if (0 < count) {
996                 iter = g_slist_nth(g_client_list, 0);
997                 for (i = 0; i < count; i++) {
998                         if (NULL == iter)
999                                 break;
1000
1001                         data = iter->data;
1002
1003                         if (NULL != data) {
1004                                 if (pid == data->pid)
1005                                         return data;
1006                         }
1007
1008                         iter = g_slist_next(iter);
1009                 }
1010         }
1011
1012         return NULL;
1013 }
1014
1015 int vcd_client_add(int pid)
1016 {
1017         /*Check pid is duplicated*/
1018         GSList *tmp = NULL;
1019         tmp = __client_get_item(pid);
1020
1021         if (NULL != tmp) {
1022                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Client pid is already registered");
1023                 return VCD_ERROR_INVALID_PARAMETER;
1024         }
1025
1026         vc_client_info_s *info = (vc_client_info_s*)calloc(1, sizeof(vc_client_info_s));
1027         if (NULL == info) {
1028                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1029                 return VCD_ERROR_OUT_OF_MEMORY;
1030         }
1031
1032         info->pid = pid;
1033
1034         info->fg_cmd = false;
1035         info->bg_cmd = false;
1036         info->exclusive_cmd = false;
1037         info->server_dialog = false;
1038
1039         /* Add item to global list */
1040         g_client_list = g_slist_append(g_client_list, info);
1041
1042         if (NULL == g_client_list) {
1043                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new client");
1044
1045                 free(info);
1046                 info = NULL;
1047
1048                 return -1;
1049         } else {
1050                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data SUCCESS] Add new client");
1051         }
1052
1053 #ifdef CLIENT_DATA_DEBUG
1054         __show_client_list();
1055 #endif
1056         return 0;
1057 }
1058
1059 int vcd_client_delete(int pid)
1060 {
1061         GSList *tmp = NULL;
1062         vc_client_info_s* client_info = NULL;
1063
1064         /*Get handle*/
1065         tmp = __client_get_item(pid);
1066         if (NULL == tmp) {
1067                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1068                 return VCD_ERROR_INVALID_PARAMETER;
1069         }
1070
1071         /*Free client structure*/
1072         client_info = tmp->data;
1073         if (NULL != client_info) {
1074                 free(client_info);
1075         }
1076
1077         /*Remove handle from list*/
1078         g_client_list = g_slist_remove_link(g_client_list, tmp);
1079
1080
1081 #ifdef CLIENT_DATA_DEBUG
1082         __show_client_list();
1083 #endif
1084
1085         return 0;
1086 }
1087
1088 bool vcd_client_is_available(int pid)
1089 {
1090         vc_client_info_s* client_info = NULL;
1091
1092         client_info = __client_get_element(pid);
1093         if (NULL == client_info) {
1094                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1095                 return false;
1096         }
1097
1098         return true;
1099 }
1100
1101 int vcd_client_get_ref_count()
1102 {
1103         int count = 0;
1104
1105         count = g_slist_length(g_client_list) + g_slist_length(g_widget_list);
1106         if (0 < g_manager.pid) {
1107                 count++;
1108         }
1109
1110         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] client count : %d", count);
1111
1112         return count;
1113 }
1114
1115 int vcd_client_get_list(int** pids, int* pid_count)
1116 {
1117         if (NULL == pids || NULL == pid_count)
1118                 return -1;
1119
1120         int count = g_slist_length(g_client_list);
1121
1122         if (0 == count)
1123                 return -1;
1124
1125         int *tmp;
1126         tmp = (int*)calloc(count, sizeof(int));
1127         if (NULL == tmp) {
1128                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1129                 return VCD_ERROR_OUT_OF_MEMORY;
1130         }
1131
1132         GSList *iter = NULL;
1133         vc_client_info_s *data = NULL;
1134         int i = 0;
1135
1136         iter = g_slist_nth(g_client_list, 0);
1137
1138         while (NULL != iter) {
1139                 data = iter->data;
1140
1141                 if (NULL != data) {
1142                         tmp[i] = data->pid;
1143                 }
1144
1145                 iter = g_slist_next(iter);
1146                 i++;
1147         }
1148
1149         *pids = tmp;
1150         *pid_count = count;
1151
1152         return 0;
1153 }
1154
1155 int vcd_client_set_command_type(int pid, int type)
1156 {
1157         vc_client_info_s* client_info = NULL;
1158
1159         client_info = __client_get_element(pid);
1160         if (NULL == client_info) {
1161                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1162                 return VCD_ERROR_INVALID_PARAMETER;
1163         }
1164
1165         switch (type) {
1166         case VC_COMMAND_TYPE_FOREGROUND:
1167                 client_info->fg_cmd = true;
1168                 break;
1169         case VC_COMMAND_TYPE_BACKGROUND:
1170                 client_info->bg_cmd = true;
1171                 break;
1172         default:
1173                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1174                 return -1;
1175         }
1176
1177         return 0;
1178 }
1179
1180 int vcd_client_unset_command_type(int pid, int type)
1181 {
1182         vc_client_info_s* client_info = NULL;
1183
1184         client_info = __client_get_element(pid);
1185         if (NULL == client_info) {
1186                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1187                 return VCD_ERROR_INVALID_PARAMETER;
1188         }
1189
1190         switch (type) {
1191         case VC_COMMAND_TYPE_FOREGROUND:
1192                 client_info->fg_cmd = false;
1193                 break;
1194         case VC_COMMAND_TYPE_BACKGROUND:
1195                 client_info->bg_cmd = false;
1196                 break;
1197         default:
1198                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1199                 return -1;
1200         }
1201
1202         return 0;
1203 }
1204
1205 int vcd_client_set_exclusive_command(int pid)
1206 {
1207         vc_client_info_s* client_info = NULL;
1208
1209         client_info = __client_get_element(pid);
1210         if (NULL == client_info) {
1211                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1212                 return VCD_ERROR_INVALID_PARAMETER;
1213         }
1214
1215         client_info->exclusive_cmd = true;
1216
1217         return 0;
1218 }
1219
1220 int vcd_client_unset_exclusive_command(int pid)
1221 {
1222         vc_client_info_s* client_info = NULL;
1223
1224         client_info = __client_get_element(pid);
1225         if (NULL == client_info) {
1226                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1227                 return VCD_ERROR_INVALID_PARAMETER;
1228         }
1229
1230         client_info->exclusive_cmd = false;
1231
1232         return 0;
1233 }
1234
1235 int vcd_client_save_client_info()
1236 {
1237         if (0 != vc_info_parser_set_client_info(g_client_list)) {
1238                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to save client info");
1239                 return -1;
1240         }
1241
1242         return 0;
1243 }
1244
1245 /*
1246 * Functions for widget
1247 */
1248 GSList* __widget_get_item(int pid)
1249 {
1250         GSList *iter = NULL;
1251         widget_info_s *data = NULL;
1252
1253         int count = g_slist_length(g_widget_list);
1254         int i;
1255
1256         if (0 < count) {
1257                 iter = g_slist_nth(g_widget_list, 0);
1258                 for (i = 0; i < count; i++) {
1259                         if (NULL == iter)
1260                                 break;
1261
1262                         data = iter->data;
1263
1264                         if (NULL != data) {
1265                                 if (pid == data->pid)
1266                                         return iter;
1267                         }
1268
1269                         iter = g_slist_next(iter);
1270                 }
1271         }
1272
1273         return NULL;
1274 }
1275
1276 widget_info_s* __widget_get_element(int pid)
1277 {
1278         GSList *iter = NULL;
1279         widget_info_s *data = NULL;
1280
1281         int count = g_slist_length(g_widget_list);
1282         int i;
1283
1284         if (0 < count) {
1285                 iter = g_slist_nth(g_widget_list, 0);
1286                 i = 0;
1287
1288                 while (iter && i < count) {
1289                         data = iter->data;
1290
1291                         if (NULL != data) {
1292                                 if (pid == data->pid)
1293                                         return data;
1294                         }
1295
1296                         iter = g_slist_next(iter);
1297
1298                         i++;
1299                 }
1300         }
1301
1302         return NULL;
1303 }
1304
1305 int vcd_client_widget_get_list(int** pids, int* pid_count)
1306 {
1307         if (NULL == pids || NULL == pid_count)
1308                 return -1;
1309
1310         int count = g_slist_length(g_widget_list);
1311
1312         if (0 == count)
1313                 return -1;
1314
1315         int *tmp;
1316         tmp = (int*)calloc(count, sizeof(int));
1317         if (NULL == tmp) {
1318                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1319                 return VCD_ERROR_OUT_OF_MEMORY;
1320         }
1321
1322         GSList *iter = NULL;
1323         widget_info_s *data = NULL;
1324         int i = 0;
1325
1326         iter = g_slist_nth(g_widget_list, 0);
1327         while (NULL != iter) {
1328                 data = iter->data;
1329
1330                 if (NULL != data) {
1331                         tmp[i] = data->pid;
1332                 }
1333
1334                 iter = g_slist_next(iter);
1335                 i++;
1336         }
1337
1338         *pids = tmp;
1339         *pid_count = count;
1340
1341         return 0;
1342 }
1343
1344 int vcd_client_widget_add(int pid)
1345 {
1346         /*Check pid is duplicated*/
1347         GSList *tmp = NULL;
1348         tmp = __widget_get_item(pid);
1349
1350         if (NULL != tmp) {
1351                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] widget pid is already registered");
1352                 return VCD_ERROR_INVALID_PARAMETER;
1353         }
1354
1355         widget_info_s *info = (widget_info_s*)calloc(1, sizeof(widget_info_s));
1356         if (NULL == info) {
1357                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1358                 return VCD_ERROR_OUT_OF_MEMORY;
1359         }
1360
1361         info->pid = pid;
1362         info->widget_cmd = false;
1363
1364         /* Add item to global list */
1365         g_widget_list = g_slist_append(g_widget_list, info);
1366
1367         if (NULL == g_widget_list) {
1368                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new widget");
1369
1370                 free(info);
1371                 info = NULL;
1372
1373                 return -1;
1374         } else {
1375                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data SUCCESS] Add new widget");
1376         }
1377
1378 #ifdef CLIENT_DATA_DEBUG
1379         __show_client_list();
1380 #endif
1381         return 0;
1382 }
1383
1384 int vcd_client_widget_delete(int pid)
1385 {
1386         GSList *tmp = NULL;
1387         widget_info_s* widget_info = NULL;
1388
1389         /*Get handle*/
1390         tmp = __widget_get_item(pid);
1391         if (NULL == tmp) {
1392                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1393                 return VCD_ERROR_INVALID_PARAMETER;
1394         }
1395
1396         /*Free client structure*/
1397         widget_info = tmp->data;
1398         if (NULL != widget_info) {
1399                 free(widget_info);
1400         }
1401
1402         /*Remove handle from list*/
1403         g_widget_list = g_slist_remove_link(g_widget_list, tmp);
1404
1405
1406 #ifdef CLIENT_DATA_DEBUG
1407         __show_client_list();
1408 #endif
1409
1410         return 0;
1411 }
1412
1413 int vcd_client_widget_get_foreground_pid()
1414 {
1415         /* 1. Get foreground pid */
1416         int fg_pid = 0;
1417         if (0 != vcd_config_get_foreground(&fg_pid)) {
1418                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get foreground pid");
1419                 /* There is no foreground app for voice control */
1420         }
1421
1422         widget_info_s* widget = NULL;
1423
1424         widget = __widget_get_element(fg_pid);
1425         if (NULL == widget) {
1426                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Not found foreground pid of widget");
1427                 return -1;
1428         }
1429
1430         return widget->pid;
1431 }
1432
1433
1434 bool vcd_client_widget_is_available(int pid)
1435 {
1436         widget_info_s* widget_info = NULL;
1437
1438         widget_info = __widget_get_element(pid);
1439         if (NULL == widget_info) {
1440                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1441                 return false;
1442         }
1443
1444         return true;
1445 }
1446
1447 int vcd_client_widget_set_command(int pid)
1448 {
1449         widget_info_s *info = NULL;
1450         info = __widget_get_element(pid);
1451         if (NULL == info) {
1452                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1453                 return VCD_ERROR_INVALID_PARAMETER;
1454         }
1455
1456         info->widget_cmd = true;
1457
1458         return 0;
1459 }
1460
1461 int vcd_client_widget_unset_command(int pid)
1462 {
1463         widget_info_s *info = NULL;
1464         info = __widget_get_element(pid);
1465         if (NULL == info) {
1466                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1467                 return VCD_ERROR_INVALID_PARAMETER;
1468         }
1469
1470         info->widget_cmd = false;
1471
1472         return 0;
1473 }
1474
1475 int vcd_client_widget_set_asr_result_enabled(int pid, bool enable)
1476 {
1477         widget_info_s *info = NULL;
1478         info = __widget_get_element(pid);
1479         if (NULL == info) {
1480                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1481                 return VCD_ERROR_INVALID_PARAMETER;
1482         }
1483
1484         info->asr_result_enabled = enable;
1485
1486         return 0;
1487 }
1488
1489 int vcd_client_widget_get_asr_result_enabled(int pid, bool* enable)
1490 {
1491         widget_info_s *info = NULL;
1492         info = __widget_get_element(pid);
1493         if (NULL == info) {
1494                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1495                 return VCD_ERROR_INVALID_PARAMETER;
1496         }
1497
1498         *enable = info->asr_result_enabled;
1499
1500         return 0;
1501 }
1502
1503 int vcd_client_widget_set_waiting_for_recording(int pid, bool waiting)
1504 {
1505
1506         if (TRUE == waiting && pid != vcd_client_widget_get_foreground_pid()) {
1507                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT foreground pid", pid);
1508                 return -1;
1509         }
1510
1511         g_is_waiting_recording = waiting;
1512         g_waiting_recording_pid = pid;
1513         SLOG(LOG_ERROR, TAG_VCD, "[INFO] Success to set waiting for recording, pid(%d), waiting(%d)", pid, waiting);
1514         return 0;
1515 }
1516
1517 int vcd_client_widget_get_waiting_for_recording(int pid, bool* waiting)
1518 {
1519         if (pid != g_waiting_recording_pid) {
1520                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT waiting pid", pid);
1521                 return -1;
1522         }
1523
1524         *waiting = g_is_waiting_recording;
1525         SLOG(LOG_ERROR, TAG_VCD, "[INFO] Success to get waiting for recording, waiting(%d)", *waiting);
1526         return 0;
1527 }
1528
1529 void vcd_client_update_foreground_pid()
1530 {
1531         int tmp_pid = VC_RUNTIME_INFO_NO_FOREGROUND;
1532         vcd_config_get_foreground(&tmp_pid);
1533
1534         int pid = VC_RUNTIME_INFO_NO_FOREGROUND;
1535         int ret = aul_window_get_focused_pid(&pid);
1536         if (0 != ret) {
1537                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get focused pid, ret(%d)", ret);
1538         }
1539         SLOG(LOG_INFO, TAG_VCD, "[INFO] Foreground pid (%d), Focused pid (%d)", tmp_pid, pid);
1540
1541         GSList *iter = NULL;
1542         vc_client_info_s *data = NULL;
1543
1544         int count = g_slist_length(g_client_list);
1545         int i;
1546
1547         if (0 == count) {
1548                 SLOG(LOG_DEBUG, TAG_VCD, "No Client");
1549         } else {
1550                 iter = g_slist_nth(g_client_list, 0);
1551                 for (i = 0; i < count; i++) {
1552                         if (NULL == iter)
1553                                 break;
1554
1555                         data = iter->data;
1556
1557                         if (NULL != data) {
1558                                 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
1559                                 if (pid == data->pid) {
1560                                         SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid (%d)", data->pid);
1561                                         vcd_config_set_foreground(data->pid, true);
1562                                         if (tmp_pid != data->pid) {
1563                                                 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid is different");
1564                                         }
1565                                         return;
1566                                 }
1567                         }
1568                         iter = g_slist_next(iter);
1569                 }
1570         }
1571
1572         widget_info_s *widget_data = NULL;
1573         count = g_slist_length(g_widget_list);
1574
1575         if (0 == count) {
1576                 SLOG(LOG_DEBUG, TAG_VCD, "No widget");
1577         } else {
1578                 iter = g_slist_nth(g_widget_list, 0);
1579                 for (i = 0; i < count; i++) {
1580                         if (NULL == iter)
1581                                 break;
1582
1583                         widget_data = iter->data;
1584
1585                         if (NULL != widget_data) {
1586                                 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
1587                                 if (pid == widget_data->pid) {
1588                                         SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid (%d)", widget_data->pid);
1589                                         vcd_config_set_foreground(widget_data->pid, true);
1590                                         if (tmp_pid != widget_data->pid) {
1591                                                 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid is changed");
1592                                         }
1593                                         return;
1594                                 }
1595                         }
1596                         iter = g_slist_next(iter);
1597                 }
1598         }
1599
1600         SLOG(LOG_INFO, TAG_VCD, "[INFO] No foreground");
1601         vcd_config_set_foreground(VC_RUNTIME_INFO_NO_FOREGROUND, true);
1602         return;
1603 }