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