Fix spelling errata
[platform/core/uifw/voice-control.git] / server / vcd_client_data.c
1 /*
2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <app_manager.h>
18 #include <aul.h>
19 #include <aul_window.h>
20
21 #include "vcd_client_data.h"
22 #include "vcd_config.h"
23 #include "vcd_main.h"
24
25 #include "voice_control_command_expand.h"
26
27 /* Client list */
28 static GSList* g_client_list = NULL;
29
30 static GSList* g_widget_list = NULL;
31
32 static manager_info_s g_manager;
33
34 /* Command list */
35 static current_commands_list_s g_cur_cmd_list;
36
37 /* Demandable client list */
38 static GSList* g_demandable_client = NULL;
39
40 /* Runtime info */
41 static bool g_silence_detection;
42 static vcd_recognition_mode_e g_recognition_mode;
43 static char* g_result_text = NULL;
44 static bool g_is_waiting_recording = false;
45 static int g_waiting_recording_pid = -1;
46
47 /* Function definitions */
48 widget_info_s* __widget_get_element(int pid);
49
50 vc_client_info_s* __client_get_element(int pid);
51
52
53 int vcd_client_manager_set(int pid)
54 {
55         if (-1 != g_manager.pid && NULL != g_manager.appid) {
56                 SLOG(LOG_DEBUG, TAG_VCD, "Manager has already registered");
57                 return -1;
58         }
59         g_manager.pid = pid;
60         g_manager.manager_cmd = false;
61         g_manager.exclusive_cmd_option = false;
62         g_manager.appid = NULL;
63
64         // Get appid by pid using app control
65         char* appid = NULL;
66         int ret = app_manager_get_app_id(pid, &appid);
67         if (0 != ret || NULL == appid) {
68                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
69                 return -1;
70         }
71         g_manager.appid = strdup(appid);
72
73         free(appid);
74         appid = NULL;
75         return 0;
76 }
77
78 int vcd_client_manager_unset()
79 {
80         g_manager.pid = -1;
81         g_manager.manager_cmd = false;
82         g_manager.exclusive_cmd_option = false;
83
84         return 0;
85 }
86
87 int vcd_client_manager_unset_appid()
88 {
89         if (NULL != g_manager.appid) {
90                 free(g_manager.appid);
91                 g_manager.appid = NULL;
92         }
93         return 0;
94 }
95
96 bool vcd_client_manager_is_valid(int pid)
97 {
98         if (-1 == g_manager.pid || pid == g_manager.pid) {
99                 return true;
100         }
101         return false;
102 }
103
104 int vcd_client_manager_set_command(int pid)
105 {
106         if (pid != g_manager.pid) {
107                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
108                 return -1;
109         }
110         g_manager.manager_cmd = true;
111         return 0;
112 }
113
114 int vcd_client_manager_unset_command(int pid)
115 {
116         if (pid != g_manager.pid) {
117                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
118                 return -1;
119         }
120         g_manager.manager_cmd = false;
121         return 0;
122 }
123
124 bool vcd_client_manager_is_system_command_valid(int pid)
125 {
126         if (pid != g_manager.pid) {
127                 SLOG(LOG_WARN, TAG_VCD, "[Client Data WARNING] pid(%d) is NOT valid", pid);
128                 return false;
129         }
130
131         if (true == g_manager.manager_cmd)
132                 return true;
133
134         return false;
135 }
136
137 int vcd_client_manager_set_demandable_client(int pid, GSList* client_list)
138 {
139         if (0 != g_slist_length(g_demandable_client)) {
140                 /* release data */
141                 GSList *iter = NULL;
142                 vc_demandable_client_s* temp_client;
143                 iter = g_slist_nth(g_demandable_client, 0);
144
145                 while (NULL != iter) {
146                         temp_client = iter->data;
147
148                         if (NULL != temp_client) {
149                                 if (NULL != temp_client->appid)         free(temp_client->appid);
150                                 free(temp_client);
151                         }
152
153                         iter = g_slist_next(iter);
154                 }
155                 g_demandable_client = NULL;
156         }
157
158         g_demandable_client = client_list;
159
160         return 0;
161 }
162
163 bool vcd_client_manager_check_demandable_client(int pid)
164 {
165         if (0 == g_slist_length(g_demandable_client)) {
166                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] All client is available to request start");
167                 return true;
168         }
169
170         /* Check demandable appid */
171         char appid[1024] = {0, };
172         if (0 == aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
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 vcd_client_set_server_dialog(int pid, bool is_server_dialog)
773 {
774         vc_client_info_s* client_info = NULL;
775
776         client_info = __client_get_element(pid);
777         if (NULL == client_info) {
778                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
779                 return VCD_ERROR_INVALID_PARAMETER;
780         }
781
782         client_info->server_dialog = is_server_dialog;
783
784         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Set server dialog, client pid(%d), server_dialog(%d)", pid, client_info->server_dialog);
785         return 0;
786 }
787
788 int vcd_client_get_server_dialog(int pid, bool* is_server_dialog)
789 {
790         vc_client_info_s* client_info = NULL;
791
792         client_info = __client_get_element(pid);
793         if (NULL == client_info) {
794                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
795                 return VCD_ERROR_INVALID_PARAMETER;
796         }
797
798         *is_server_dialog = client_info->server_dialog;
799
800         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Get server dialog, client pid(%d), server_dialog(%d)", pid, *is_server_dialog);
801         return 0;
802 }
803
804 int vcd_client_append_cmd_from_type(int type, vc_cmd_list_h list)
805 {
806         GSList *item = NULL;
807
808         if (NULL == list) {
809                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid list parameter");
810                 return VCD_ERROR_INVALID_PARAMETER;
811         }
812
813         switch (type) {
814         case VC_COMMAND_TYPE_SYSTEM:
815                 item = g_cur_cmd_list.system_cmds;
816                 break;
817         case VC_COMMAND_TYPE_FOREGROUND:
818                 item = g_cur_cmd_list.foreground_cmds;
819                 break;
820         case VC_COMMAND_TYPE_WIDGET:
821                 item = g_cur_cmd_list.widget_cmds;
822                 break;
823         case VC_COMMAND_TYPE_SYSTEM_BACKGROUND:
824                 item = g_cur_cmd_list.system_background_cmds;
825                 break;
826         case VC_COMMAND_TYPE_BACKGROUND:
827                 item = g_cur_cmd_list.background_cmds;
828                 break;
829         case VC_COMMAND_TYPE_EXCLUSIVE:
830                 item = g_cur_cmd_list.exclusive_system_cmds;
831                 break;
832         default:
833                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid type parameter");
834                 return VCD_ERROR_INVALID_PARAMETER;
835         }
836
837         while (NULL != item) {
838                 vc_cmd_h temp_cmd = NULL;
839                 vc_cmd_h target_cmd = (vc_cmd_h)item->data;
840
841                 temp_cmd = (vc_cmd_h)__command_copy((vc_cmd_s*)target_cmd);
842
843                 if (NULL == temp_cmd) {
844                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to copy the command");
845                         return VCD_ERROR_OUT_OF_MEMORY;
846                 }
847
848                 if (0 != vc_cmd_list_add(list, temp_cmd)) {
849                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add the command in the list");
850                         if (NULL != temp_cmd) {
851                                 vc_cmd_destroy(temp_cmd);
852                                 temp_cmd = NULL;
853                         }
854
855                         return VCD_ERROR_OPERATION_FAILED;
856                 }
857
858                 item = item->next;
859         }
860
861         return 0;
862 }
863
864 int __show_client_list()
865 {
866         GSList *iter = NULL;
867         vc_client_info_s *data = NULL;
868
869         SLOG(LOG_DEBUG, TAG_VCD, "@@@ client list");
870
871         int count = g_slist_length(g_client_list);
872         int i;
873
874         if (0 == count) {
875                 SLOG(LOG_DEBUG, TAG_VCD, "No Client");
876         } else {
877                 iter = g_slist_nth(g_client_list, 0);
878                 for (i = 0; i < count; i++) {
879                         if (NULL == iter)
880                                 break;
881
882                         data = iter->data;
883
884                         if (NULL != data) {
885                                 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
886                         }
887                         iter = g_slist_next(iter);
888                 }
889         }
890
891         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
892
893         SLOG(LOG_DEBUG, TAG_VCD, "@@@ widget list");
894
895         widget_info_s *widget_data = NULL;
896
897         count = g_slist_length(g_widget_list);
898
899         if (0 == count) {
900                 SLOG(LOG_DEBUG, TAG_VCD, "No widget");
901         } else {
902                 iter = g_slist_nth(g_widget_list, 0);
903                 for (i = 0; i < count; i++) {
904                         if (NULL == iter)
905                                 break;
906
907                         widget_data = iter->data;
908
909                         if (NULL != widget_data) {
910                                 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
911                         }
912                         iter = g_slist_next(iter);
913                 }
914         }
915
916         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
917
918         return 0;
919 }
920
921 int __show_command_list(GSList* cmd_group)
922 {
923         GSList *iter = NULL;
924         vc_cmd_s *data = NULL;
925
926         SLOG(LOG_DEBUG, TAG_VCD, "@@@ command group");
927
928         int count = g_slist_length(cmd_group);
929         int i;
930
931         if (0 == count) {
932                 SLOG(LOG_DEBUG, TAG_VCD, "No command");
933         } else {
934                 iter = g_slist_nth(cmd_group, 0);
935                 for (i = 0; i < count; i++) {
936                         if (NULL == iter)
937                                 break;
938
939                         data = iter->data;
940                         if (NULL != data) {
941                                 if (NULL != data->parameter) {
942                                         SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) parameter(%s) key(%d)", i, data->command, data->parameter, data->key);
943                                 } else {
944                                         SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) key(%d)", i, data->command, data->key);
945                                 }
946                         }
947                         iter = g_slist_next(iter);
948                 }
949         }
950
951         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
952
953         return 0;
954 }
955
956 GSList* __client_get_item(const int pid)
957 {
958         GSList *iter = NULL;
959         vc_client_info_s *data = NULL;
960
961         int count = g_slist_length(g_client_list);
962         int i;
963
964         if (0 < count) {
965                 iter = g_slist_nth(g_client_list, 0);
966                 for (i = 0; i < count; i++) {
967                         if (NULL == iter)
968                                 break;
969
970                         data = iter->data;
971                         if (NULL != data) {
972                                 if (pid == data->pid)
973                                         return iter;
974                         }
975
976                         iter = g_slist_next(iter);
977                 }
978         }
979
980         return NULL;
981 }
982
983 vc_client_info_s* __client_get_element(int pid)
984 {
985         GSList *iter = NULL;
986         vc_client_info_s *data = NULL;
987
988         int count = g_slist_length(g_client_list);
989         int i;
990
991         if (0 < count) {
992                 iter = g_slist_nth(g_client_list, 0);
993                 for (i = 0; i < count; i++) {
994                         if (NULL == iter)
995                                 break;
996
997                         data = iter->data;
998
999                         if (NULL != data) {
1000                                 if (pid == data->pid)
1001                                         return data;
1002                         }
1003
1004                         iter = g_slist_next(iter);
1005                 }
1006         }
1007
1008         return NULL;
1009 }
1010
1011 int vcd_client_add(int pid)
1012 {
1013         /*Check pid is duplicated*/
1014         GSList *tmp = NULL;
1015         tmp = __client_get_item(pid);
1016
1017         if (NULL != tmp) {
1018                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Client pid is already registered");
1019                 return VCD_ERROR_INVALID_PARAMETER;
1020         }
1021
1022         vc_client_info_s *info = (vc_client_info_s*)calloc(1, sizeof(vc_client_info_s));
1023         if (NULL == info) {
1024                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1025                 return VCD_ERROR_OUT_OF_MEMORY;
1026         }
1027
1028         info->pid = pid;
1029
1030         info->fg_cmd = false;
1031         info->bg_cmd = false;
1032         info->exclusive_cmd = false;
1033         info->server_dialog = false;
1034
1035         /* Add item to global list */
1036         g_client_list = g_slist_append(g_client_list, info);
1037
1038         if (NULL == g_client_list) {
1039                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new client");
1040
1041                 free(info);
1042                 info = NULL;
1043
1044                 return -1;
1045         } else {
1046                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data SUCCESS] Add new client");
1047         }
1048
1049 #ifdef CLIENT_DATA_DEBUG
1050         __show_client_list();
1051 #endif
1052         return 0;
1053 }
1054
1055 int vcd_client_delete(int pid)
1056 {
1057         GSList *tmp = NULL;
1058         vc_client_info_s* client_info = NULL;
1059
1060         /*Get handle*/
1061         tmp = __client_get_item(pid);
1062         if (NULL == tmp) {
1063                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1064                 return VCD_ERROR_INVALID_PARAMETER;
1065         }
1066
1067         /*Free client structure*/
1068         client_info = tmp->data;
1069         if (NULL != client_info) {
1070                 free(client_info);
1071         }
1072
1073         /*Remove handle from list*/
1074         g_client_list = g_slist_remove_link(g_client_list, tmp);
1075
1076
1077 #ifdef CLIENT_DATA_DEBUG
1078         __show_client_list();
1079 #endif
1080
1081         return 0;
1082 }
1083
1084 bool vcd_client_is_available(int pid)
1085 {
1086         vc_client_info_s* client_info = NULL;
1087
1088         client_info = __client_get_element(pid);
1089         if (NULL == client_info) {
1090                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1091                 return false;
1092         }
1093
1094         return true;
1095 }
1096
1097 int vcd_client_get_ref_count()
1098 {
1099         int count = 0;
1100
1101         count = g_slist_length(g_client_list) + g_slist_length(g_widget_list);
1102         if (0 < g_manager.pid) {
1103                 count++;
1104         }
1105
1106         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] client count : %d", count);
1107
1108         return count;
1109 }
1110
1111 int vcd_client_get_list(int** pids, int* pid_count)
1112 {
1113         if (NULL == pids || NULL == pid_count)
1114                 return -1;
1115
1116         int count = g_slist_length(g_client_list);
1117
1118         if (0 == count)
1119                 return -1;
1120
1121         int *tmp;
1122         tmp = (int*)calloc(count, sizeof(int));
1123         if (NULL == tmp) {
1124                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1125                 return VCD_ERROR_OUT_OF_MEMORY;
1126         }
1127
1128         GSList *iter = NULL;
1129         vc_client_info_s *data = NULL;
1130         int i = 0;
1131
1132         iter = g_slist_nth(g_client_list, 0);
1133
1134         while (NULL != iter) {
1135                 data = iter->data;
1136
1137                 if (NULL != data) {
1138                         tmp[i] = data->pid;
1139                 }
1140
1141                 iter = g_slist_next(iter);
1142                 i++;
1143         }
1144
1145         *pids = tmp;
1146         *pid_count = count;
1147
1148         return 0;
1149 }
1150
1151 int vcd_client_set_command_type(int pid, int type)
1152 {
1153         vc_client_info_s* client_info = NULL;
1154
1155         client_info = __client_get_element(pid);
1156         if (NULL == client_info) {
1157                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1158                 return VCD_ERROR_INVALID_PARAMETER;
1159         }
1160
1161         switch (type) {
1162         case VC_COMMAND_TYPE_FOREGROUND:
1163                 client_info->fg_cmd = true;
1164                 break;
1165         case VC_COMMAND_TYPE_BACKGROUND:
1166                 client_info->bg_cmd = true;
1167                 break;
1168         default:
1169                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1170                 return -1;
1171         }
1172
1173         return 0;
1174 }
1175
1176 int vcd_client_unset_command_type(int pid, int type)
1177 {
1178         vc_client_info_s* client_info = NULL;
1179
1180         client_info = __client_get_element(pid);
1181         if (NULL == client_info) {
1182                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1183                 return VCD_ERROR_INVALID_PARAMETER;
1184         }
1185
1186         switch (type) {
1187         case VC_COMMAND_TYPE_FOREGROUND:
1188                 client_info->fg_cmd = false;
1189                 break;
1190         case VC_COMMAND_TYPE_BACKGROUND:
1191                 client_info->bg_cmd = false;
1192                 break;
1193         default:
1194                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1195                 return -1;
1196         }
1197
1198         return 0;
1199 }
1200
1201 int vcd_client_set_exclusive_command(int pid)
1202 {
1203         vc_client_info_s* client_info = NULL;
1204
1205         client_info = __client_get_element(pid);
1206         if (NULL == client_info) {
1207                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1208                 return VCD_ERROR_INVALID_PARAMETER;
1209         }
1210
1211         client_info->exclusive_cmd = true;
1212
1213         return 0;
1214 }
1215
1216 int vcd_client_unset_exclusive_command(int pid)
1217 {
1218         vc_client_info_s* client_info = NULL;
1219
1220         client_info = __client_get_element(pid);
1221         if (NULL == client_info) {
1222                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1223                 return VCD_ERROR_INVALID_PARAMETER;
1224         }
1225
1226         client_info->exclusive_cmd = false;
1227
1228         return 0;
1229 }
1230
1231 int vcd_client_save_client_info()
1232 {
1233         if (0 != vc_info_parser_set_client_info(g_client_list)) {
1234                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to save client info");
1235                 return -1;
1236         }
1237
1238         return 0;
1239 }
1240
1241 /*
1242 * Functions for widget
1243 */
1244 GSList* __widget_get_item(int pid)
1245 {
1246         GSList *iter = NULL;
1247         widget_info_s *data = NULL;
1248
1249         int count = g_slist_length(g_widget_list);
1250         int i;
1251
1252         if (0 < count) {
1253                 iter = g_slist_nth(g_widget_list, 0);
1254                 for (i = 0; i < count; i++) {
1255                         if (NULL == iter)
1256                                 break;
1257
1258                         data = iter->data;
1259
1260                         if (NULL != data) {
1261                                 if (pid == data->pid)
1262                                         return iter;
1263                         }
1264
1265                         iter = g_slist_next(iter);
1266                 }
1267         }
1268
1269         return NULL;
1270 }
1271
1272 widget_info_s* __widget_get_element(int pid)
1273 {
1274         GSList *iter = NULL;
1275         widget_info_s *data = NULL;
1276
1277         int count = g_slist_length(g_widget_list);
1278         int i;
1279
1280         if (0 < count) {
1281                 iter = g_slist_nth(g_widget_list, 0);
1282                 i = 0;
1283
1284                 while (iter && i < count) {
1285                         data = iter->data;
1286
1287                         if (NULL != data) {
1288                                 if (pid == data->pid)
1289                                         return data;
1290                         }
1291
1292                         iter = g_slist_next(iter);
1293
1294                         i++;
1295                 }
1296         }
1297
1298         return NULL;
1299 }
1300
1301 int vcd_client_widget_get_list(int** pids, int* pid_count)
1302 {
1303         if (NULL == pids || NULL == pid_count)
1304                 return -1;
1305
1306         int count = g_slist_length(g_widget_list);
1307
1308         if (0 == count)
1309                 return -1;
1310
1311         int *tmp;
1312         tmp = (int*)calloc(count, sizeof(int));
1313         if (NULL == tmp) {
1314                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1315                 return VCD_ERROR_OUT_OF_MEMORY;
1316         }
1317
1318         GSList *iter = NULL;
1319         widget_info_s *data = NULL;
1320         int i = 0;
1321
1322         iter = g_slist_nth(g_widget_list, 0);
1323         while (NULL != iter) {
1324                 data = iter->data;
1325
1326                 if (NULL != data) {
1327                         tmp[i] = data->pid;
1328                 }
1329
1330                 iter = g_slist_next(iter);
1331                 i++;
1332         }
1333
1334         *pids = tmp;
1335         *pid_count = count;
1336
1337         return 0;
1338 }
1339
1340 int vcd_client_widget_add(int pid)
1341 {
1342         /*Check pid is duplicated*/
1343         GSList *tmp = NULL;
1344         tmp = __widget_get_item(pid);
1345
1346         if (NULL != tmp) {
1347                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] widget pid is already registered");
1348                 return VCD_ERROR_INVALID_PARAMETER;
1349         }
1350
1351         widget_info_s *info = (widget_info_s*)calloc(1, sizeof(widget_info_s));
1352         if (NULL == info) {
1353                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1354                 return VCD_ERROR_OUT_OF_MEMORY;
1355         }
1356
1357         info->pid = pid;
1358         info->widget_cmd = false;
1359
1360         /* Add item to global list */
1361         g_widget_list = g_slist_append(g_widget_list, info);
1362
1363         if (NULL == g_widget_list) {
1364                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new widget");
1365
1366                 free(info);
1367                 info = NULL;
1368
1369                 return -1;
1370         } else {
1371                 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data SUCCESS] Add new widget");
1372         }
1373
1374 #ifdef CLIENT_DATA_DEBUG
1375         __show_client_list();
1376 #endif
1377         return 0;
1378 }
1379
1380 int vcd_client_widget_delete(int pid)
1381 {
1382         GSList *tmp = NULL;
1383         widget_info_s* widget_info = NULL;
1384
1385         /*Get handle*/
1386         tmp = __widget_get_item(pid);
1387         if (NULL == tmp) {
1388                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1389                 return VCD_ERROR_INVALID_PARAMETER;
1390         }
1391
1392         /*Free client structure*/
1393         widget_info = tmp->data;
1394         if (NULL != widget_info) {
1395                 free(widget_info);
1396         }
1397
1398         /*Remove handle from list*/
1399         g_widget_list = g_slist_remove_link(g_widget_list, tmp);
1400
1401
1402 #ifdef CLIENT_DATA_DEBUG
1403         __show_client_list();
1404 #endif
1405
1406         return 0;
1407 }
1408
1409 int vcd_client_widget_get_foreground_pid()
1410 {
1411         /* 1. Get foreground pid */
1412         int fg_pid = 0;
1413         if (0 != vcd_config_get_foreground(&fg_pid)) {
1414                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get foreground pid");
1415                 /* There is no foreground app for voice control */
1416         }
1417
1418         widget_info_s* widget = NULL;
1419
1420         widget = __widget_get_element(fg_pid);
1421         if (NULL == widget) {
1422                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Not found foreground pid of widget");
1423                 return -1;
1424         }
1425
1426         return widget->pid;
1427 }
1428
1429
1430 bool vcd_client_widget_is_available(int pid)
1431 {
1432         widget_info_s* widget_info = NULL;
1433
1434         widget_info = __widget_get_element(pid);
1435         if (NULL == widget_info) {
1436                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1437                 return false;
1438         }
1439
1440         return true;
1441 }
1442
1443 int vcd_client_widget_set_command(int pid)
1444 {
1445         widget_info_s *info = NULL;
1446         info = __widget_get_element(pid);
1447         if (NULL == info) {
1448                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1449                 return VCD_ERROR_INVALID_PARAMETER;
1450         }
1451
1452         info->widget_cmd = true;
1453
1454         return 0;
1455 }
1456
1457 int vcd_client_widget_unset_command(int pid)
1458 {
1459         widget_info_s *info = NULL;
1460         info = __widget_get_element(pid);
1461         if (NULL == info) {
1462                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1463                 return VCD_ERROR_INVALID_PARAMETER;
1464         }
1465
1466         info->widget_cmd = false;
1467
1468         return 0;
1469 }
1470
1471 int vcd_client_widget_set_asr_result_enabled(int pid, bool enable)
1472 {
1473         widget_info_s *info = NULL;
1474         info = __widget_get_element(pid);
1475         if (NULL == info) {
1476                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1477                 return VCD_ERROR_INVALID_PARAMETER;
1478         }
1479
1480         info->asr_result_enabled = enable;
1481
1482         return 0;
1483 }
1484
1485 int vcd_client_widget_get_asr_result_enabled(int pid, bool* enable)
1486 {
1487         widget_info_s *info = NULL;
1488         info = __widget_get_element(pid);
1489         if (NULL == info) {
1490                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1491                 return VCD_ERROR_INVALID_PARAMETER;
1492         }
1493
1494         *enable = info->asr_result_enabled;
1495
1496         return 0;
1497 }
1498
1499 int vcd_client_widget_set_waiting_for_recording(int pid, bool waiting)
1500 {
1501
1502         if (TRUE == waiting && pid != vcd_client_widget_get_foreground_pid()) {
1503                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT foreground pid", pid);
1504                 return -1;
1505         }
1506
1507         g_is_waiting_recording = waiting;
1508         g_waiting_recording_pid = pid;
1509         SLOG(LOG_ERROR, TAG_VCD, "[INFO] Success to set waiting for recording, pid(%d), waiting(%d)", pid, waiting);
1510         return 0;
1511 }
1512
1513 int vcd_client_widget_get_waiting_for_recording(int pid, bool* waiting)
1514 {
1515         if (pid != g_waiting_recording_pid) {
1516                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT waiting pid", pid);
1517                 return -1;
1518         }
1519
1520         *waiting = g_is_waiting_recording;
1521         SLOG(LOG_ERROR, TAG_VCD, "[INFO] Success to get waiting for recording, waiting(%d)", *waiting);
1522         return 0;
1523 }
1524
1525 void vcd_client_update_foreground_pid()
1526 {
1527         int tmp_pid = VC_RUNTIME_INFO_NO_FOREGROUND;
1528         vcd_config_get_foreground(&tmp_pid);
1529
1530         int pid = VC_RUNTIME_INFO_NO_FOREGROUND;
1531         int ret = aul_window_get_focused_pid(&pid);
1532         if (0 != ret) {
1533                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get focused pid, ret(%d)", ret);
1534         }
1535         SLOG(LOG_INFO, TAG_VCD, "[INFO] Foreground pid (%d), Focused pid (%d)", tmp_pid, pid);
1536
1537         GSList *iter = NULL;
1538         vc_client_info_s *data = NULL;
1539
1540         int count = g_slist_length(g_client_list);
1541         int i;
1542
1543         if (0 == count) {
1544                 SLOG(LOG_DEBUG, TAG_VCD, "No Client");
1545         } else {
1546                 iter = g_slist_nth(g_client_list, 0);
1547                 for (i = 0; i < count; i++) {
1548                         if (NULL == iter)
1549                                 break;
1550
1551                         data = iter->data;
1552
1553                         if (NULL != data) {
1554                                 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
1555                                 if (pid == data->pid) {
1556                                         SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid (%d)", data->pid);
1557                                         vcd_config_set_foreground(data->pid, true);
1558                                         if (tmp_pid != data->pid) {
1559                                                 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid is different");
1560                                         }
1561                                         return;
1562                                 }
1563                         }
1564                         iter = g_slist_next(iter);
1565                 }
1566         }
1567
1568         widget_info_s *widget_data = NULL;
1569         count = g_slist_length(g_widget_list);
1570
1571         if (0 == count) {
1572                 SLOG(LOG_DEBUG, TAG_VCD, "No widget");
1573         } else {
1574                 iter = g_slist_nth(g_widget_list, 0);
1575                 for (i = 0; i < count; i++) {
1576                         if (NULL == iter)
1577                                 break;
1578
1579                         widget_data = iter->data;
1580
1581                         if (NULL != widget_data) {
1582                                 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
1583                                 if (pid == widget_data->pid) {
1584                                         SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid (%d)", widget_data->pid);
1585                                         vcd_config_set_foreground(widget_data->pid, true);
1586                                         if (tmp_pid != widget_data->pid) {
1587                                                 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid is changed");
1588                                         }
1589                                         return;
1590                                 }
1591                         }
1592                         iter = g_slist_next(iter);
1593                 }
1594         }
1595
1596         SLOG(LOG_INFO, TAG_VCD, "[INFO] No foreground");
1597         vcd_config_set_foreground(VC_RUNTIME_INFO_NO_FOREGROUND, true);
1598         return;
1599 }