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