2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <app_manager.h>
19 #include <aul_window.h>
21 #include "vcd_client_data.h"
22 #include "vcd_config.h"
25 #include "voice_control_command_expand.h"
28 static GSList* g_client_list = NULL;
30 static GSList* g_widget_list = NULL;
32 static manager_info_s g_manager;
35 static current_commands_list_s g_cur_cmd_list;
37 /* Demandable client list */
38 static GSList* g_demandable_client = NULL;
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;
47 /* Function definitions */
48 widget_info_s* __widget_get_element(int pid);
50 vc_client_info_s* __client_get_element(int pid);
53 int vcd_client_manager_set(int pid)
55 if (-1 != g_manager.pid && NULL != g_manager.appid) {
56 SLOG(LOG_DEBUG, TAG_VCD, "Manager has already registered");
60 g_manager.manager_cmd = false;
61 g_manager.exclusive_cmd_option = false;
62 g_manager.appid = NULL;
64 // Get appid by pid using app control
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);
71 g_manager.appid = strdup(appid);
78 int vcd_client_manager_unset()
81 g_manager.manager_cmd = false;
82 g_manager.exclusive_cmd_option = false;
87 int vcd_client_manager_unset_appid()
89 if (NULL != g_manager.appid) {
90 free(g_manager.appid);
91 g_manager.appid = NULL;
96 bool vcd_client_manager_is_valid(int pid)
98 if (-1 == g_manager.pid || pid == g_manager.pid) {
104 int vcd_client_manager_set_command(int pid)
106 if (pid != g_manager.pid) {
107 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
110 g_manager.manager_cmd = true;
114 int vcd_client_manager_unset_command(int pid)
116 if (pid != g_manager.pid) {
117 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
120 g_manager.manager_cmd = false;
124 bool vcd_client_manager_is_system_command_valid(int pid)
126 if (pid != g_manager.pid) {
127 SLOG(LOG_WARN, TAG_VCD, "[Client Data WARNING] pid(%d) is NOT valid", pid);
131 if (true == g_manager.manager_cmd)
137 int vcd_client_manager_set_demandable_client(int pid, GSList* client_list)
139 if (0 != g_slist_length(g_demandable_client)) {
142 vc_demandable_client_s* temp_client;
143 iter = g_slist_nth(g_demandable_client, 0);
145 while (NULL != iter) {
146 temp_client = iter->data;
148 if (NULL != temp_client) {
149 if (NULL != temp_client->appid) free(temp_client->appid);
153 iter = g_slist_next(iter);
155 g_demandable_client = NULL;
158 g_demandable_client = client_list;
163 bool vcd_client_manager_check_demandable_client(int pid)
165 if (0 == g_slist_length(g_demandable_client)) {
166 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] All client is available to request start");
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);
175 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] daemon(%d) requests start", pid);
180 vc_demandable_client_s* temp_client;
181 iter = g_slist_nth(g_demandable_client, 0);
183 while (NULL != iter) {
184 temp_client = iter->data;
186 if (NULL != temp_client) {
188 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] demandable appid(%s)", temp_client->appid);
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);
196 if (0 == strlen(appid)) {
197 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] pid(%d) is available", pid);
203 iter = g_slist_next(iter);
209 bool vcd_client_manager_get_exclusive()
211 return g_manager.exclusive_cmd_option;
214 int vcd_client_manager_set_exclusive(bool value)
216 g_manager.exclusive_cmd_option = value;
220 int vcd_client_manager_get_pid()
222 return g_manager.pid;
225 int vcd_client_manager_get_appid(char** appid)
227 if (NULL != g_manager.appid)
228 *appid = strdup(g_manager.appid);
233 int vcd_client_manager_set_result_text(const char* result)
235 if (NULL != g_result_text) {
237 g_result_text = NULL;
240 if (NULL != result) {
241 g_result_text = strdup(result);
247 char* vcd_client_manager_get_result_text()
249 return g_result_text;
252 static void __vcd_client_release_each_commands(GSList** cmds)
257 if (0 < g_slist_length(*cmds)) {
258 iter = g_slist_nth(*cmds, 0);
259 while (NULL != iter) {
260 temp_cmd = iter->data;
262 if (NULL != temp_cmd) {
263 if (NULL != temp_cmd->command) {
264 free(temp_cmd->command);
265 temp_cmd->command = NULL;
267 if (NULL != temp_cmd->appid) {
268 free(temp_cmd->appid);
269 temp_cmd->appid = NULL;
271 if (NULL != temp_cmd->parameter) {
272 free(temp_cmd->parameter);
273 temp_cmd->parameter = NULL;
275 if (NULL != temp_cmd->invocation_name) {
276 free(temp_cmd->invocation_name);
277 temp_cmd->invocation_name = NULL;
279 if (NULL != temp_cmd->fixed) {
280 free(temp_cmd->fixed);
281 temp_cmd->fixed = NULL;
286 *cmds = g_slist_remove_link(*cmds, iter);
288 iter = g_slist_nth(*cmds, 0);
294 int __vcd_client_release_commands()
296 g_cur_cmd_list.total_cmd_count = 0;
297 g_cur_cmd_list.foreground = VC_NO_FOREGROUND_PID;
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));
309 int vcd_client_command_collect_command()
311 /* 1. Get foreground pid */
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 */
319 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Foreground pid(%d)", fg_pid);
321 /* 2. Clean up command list */
322 __vcd_client_release_commands();
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");
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);
332 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
334 g_cur_cmd_list.exclusive_system_cmds = ex_sys_cmd_list;
337 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No exclusive system commands");
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);
348 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
350 g_cur_cmd_list.system_cmds = sys_cmd_list;
353 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system commands");
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);
360 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
362 g_cur_cmd_list.system_background_cmds = sys_back_cmd_list;
365 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system background commands");
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;
373 g_cur_cmd_list.foreground = fg_pid;
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);
382 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the WIDGET command list");
384 g_cur_cmd_list.widget_cmds = widget_cmd_list;
388 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No widget commands");
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);
395 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the fg command list");
397 g_cur_cmd_list.foreground_cmds = fg_cmd_list;
401 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground app");
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);
407 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground commands of manager app");
409 g_cur_cmd_list.foreground_cmds = fg_cmd_list;
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);
418 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the bg command list");
420 /* Add item to global command list */
421 g_cur_cmd_list.background_cmds = bg_cmd_list;
427 int vcd_client_get_length()
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);
437 g_cur_cmd_list.total_cmd_count = command_count;
439 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Command count : %d ", g_cur_cmd_list.total_cmd_count);
440 return command_count;
443 int vcd_client_foreach_command(client_foreach_command_cb callback, void* user_data)
445 if (NULL == callback) {
446 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] input parameter is NULL");
447 return VCD_ERROR_INVALID_PARAMETER;
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;
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;
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;
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);
472 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
474 iter = g_slist_next(iter);
477 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No widget commands");
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;
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);
488 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
490 iter = g_slist_next(iter);
493 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground commands");
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;
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);
504 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
506 iter = g_slist_next(iter);
509 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system commands");
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;
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);
520 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
522 iter = g_slist_next(iter);
525 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system background commands");
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;
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);
536 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
538 iter = g_slist_next(iter);
541 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No exclusive system commands");
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;
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);
552 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
554 iter = g_slist_next(iter);
557 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No background commands");
562 static vc_cmd_s* __command_copy(vc_cmd_s* src_cmd)
564 if (NULL == src_cmd) {
565 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Input command is NULL");
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");
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;
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;
590 if (NULL != src_cmd->command) {
591 temp_cmd->command = strdup(src_cmd->command);
594 if (NULL != src_cmd->parameter) {
595 temp_cmd->parameter = strdup(src_cmd->parameter);
598 if (NULL != src_cmd->appid) {
599 temp_cmd->appid = strdup(src_cmd->appid);
602 if (NULL != src_cmd->invocation_name) {
603 temp_cmd->invocation_name = strdup(src_cmd->invocation_name);
606 if (NULL != src_cmd->fixed) {
607 temp_cmd->fixed = strdup(src_cmd->fixed);
610 temp_cmd->key = src_cmd->key;
611 temp_cmd->modifier = src_cmd->modifier;
616 int vcd_client_get_cmd_from_result_id(int result_id, vc_cmd_s** result)
621 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Result id(%d)", result_id);
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;
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); */
633 *result = __command_copy(temp_cmd);
638 iter = g_slist_next(iter);
641 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No widget commands");
644 if (0 < g_slist_length(g_cur_cmd_list.foreground_cmds)) {
645 iter = g_slist_nth(g_cur_cmd_list.foreground_cmds, 0);
647 while (NULL != iter) {
648 temp_cmd = iter->data;
650 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] command id (%d)", temp_cmd->id);
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); */
657 *result = __command_copy(temp_cmd);
661 iter = g_slist_next(iter);
664 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground commands");
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;
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); */
677 *result = __command_copy(temp_cmd);
681 iter = g_slist_next(iter);
684 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system commands");
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;
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); */
697 *result = __command_copy(temp_cmd);
701 iter = g_slist_next(iter);
704 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system background commands");
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;
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); */
717 *result = __command_copy(temp_cmd);
721 iter = g_slist_next(iter);
724 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No exclusive system commands");
727 if (0 < g_slist_length(g_cur_cmd_list.background_cmds)) {
728 iter = g_slist_nth(g_cur_cmd_list.background_cmds, 0);
730 while (NULL != iter) {
731 temp_cmd = iter->data;
733 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] command id (%d)", temp_cmd->id);
735 if (result_id == temp_cmd->id) {
736 *result = __command_copy(temp_cmd);
740 iter = g_slist_next(iter);
743 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No background commands");
746 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Not find matched result");
750 int vcd_client_set_slience_detection(bool value)
752 g_silence_detection = value;
756 bool vcd_client_get_slience_detection()
758 return g_silence_detection;
761 int vcd_client_set_recognition_mode(vcd_recognition_mode_e mode)
763 g_recognition_mode = mode;
767 vcd_recognition_mode_e vcd_client_get_recognition_mode()
769 return g_recognition_mode;
772 int vcd_client_set_server_dialog(int pid, bool is_server_dialog)
774 vc_client_info_s* client_info = NULL;
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;
782 client_info->server_dialog = is_server_dialog;
784 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Set server dialog, client pid(%d), server_dialog(%d)", pid, client_info->server_dialog);
788 int vcd_client_get_server_dialog(int pid, bool* is_server_dialog)
790 vc_client_info_s* client_info = NULL;
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;
798 *is_server_dialog = client_info->server_dialog;
800 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Get server dialog, client pid(%d), server_dialog(%d)", pid, *is_server_dialog);
804 int vcd_client_append_cmd_from_type(int type, vc_cmd_list_h list)
809 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid list parameter");
810 return VCD_ERROR_INVALID_PARAMETER;
814 case VC_COMMAND_TYPE_SYSTEM:
815 item = g_cur_cmd_list.system_cmds;
817 case VC_COMMAND_TYPE_FOREGROUND:
818 item = g_cur_cmd_list.foreground_cmds;
820 case VC_COMMAND_TYPE_WIDGET:
821 item = g_cur_cmd_list.widget_cmds;
823 case VC_COMMAND_TYPE_SYSTEM_BACKGROUND:
824 item = g_cur_cmd_list.system_background_cmds;
826 case VC_COMMAND_TYPE_BACKGROUND:
827 item = g_cur_cmd_list.background_cmds;
829 case VC_COMMAND_TYPE_EXCLUSIVE:
830 item = g_cur_cmd_list.exclusive_system_cmds;
833 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid type parameter");
834 return VCD_ERROR_INVALID_PARAMETER;
837 while (NULL != item) {
838 vc_cmd_h temp_cmd = NULL;
839 vc_cmd_h target_cmd = (vc_cmd_h)item->data;
841 temp_cmd = (vc_cmd_h)__command_copy((vc_cmd_s*)target_cmd);
843 if (NULL == temp_cmd) {
844 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to copy the command");
845 return VCD_ERROR_OUT_OF_MEMORY;
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);
855 return VCD_ERROR_OPERATION_FAILED;
864 int __show_client_list()
867 vc_client_info_s *data = NULL;
869 SLOG(LOG_DEBUG, TAG_VCD, "@@@ client list");
871 int count = g_slist_length(g_client_list);
875 SLOG(LOG_DEBUG, TAG_VCD, "No Client");
877 iter = g_slist_nth(g_client_list, 0);
878 for (i = 0; i < count; i++) {
885 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
887 iter = g_slist_next(iter);
891 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
893 SLOG(LOG_DEBUG, TAG_VCD, "@@@ widget list");
895 widget_info_s *widget_data = NULL;
897 count = g_slist_length(g_widget_list);
900 SLOG(LOG_DEBUG, TAG_VCD, "No widget");
902 iter = g_slist_nth(g_widget_list, 0);
903 for (i = 0; i < count; i++) {
907 widget_data = iter->data;
909 if (NULL != widget_data) {
910 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
912 iter = g_slist_next(iter);
916 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
921 int __show_command_list(GSList* cmd_group)
924 vc_cmd_s *data = NULL;
926 SLOG(LOG_DEBUG, TAG_VCD, "@@@ command group");
928 int count = g_slist_length(cmd_group);
932 SLOG(LOG_DEBUG, TAG_VCD, "No command");
934 iter = g_slist_nth(cmd_group, 0);
935 for (i = 0; i < count; i++) {
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);
944 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) key(%d)", i, data->command, data->key);
947 iter = g_slist_next(iter);
951 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
956 GSList* __client_get_item(const int pid)
959 vc_client_info_s *data = NULL;
961 int count = g_slist_length(g_client_list);
965 iter = g_slist_nth(g_client_list, 0);
966 for (i = 0; i < count; i++) {
972 if (pid == data->pid)
976 iter = g_slist_next(iter);
983 vc_client_info_s* __client_get_element(int pid)
986 vc_client_info_s *data = NULL;
988 int count = g_slist_length(g_client_list);
992 iter = g_slist_nth(g_client_list, 0);
993 for (i = 0; i < count; i++) {
1000 if (pid == data->pid)
1004 iter = g_slist_next(iter);
1011 int vcd_client_add(int pid)
1013 /*Check pid is duplicated*/
1015 tmp = __client_get_item(pid);
1018 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Client pid is already registered");
1019 return VCD_ERROR_INVALID_PARAMETER;
1022 vc_client_info_s *info = (vc_client_info_s*)calloc(1, sizeof(vc_client_info_s));
1024 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1025 return VCD_ERROR_OUT_OF_MEMORY;
1030 info->fg_cmd = false;
1031 info->bg_cmd = false;
1032 info->exclusive_cmd = false;
1033 info->server_dialog = false;
1035 /* Add item to global list */
1036 g_client_list = g_slist_append(g_client_list, info);
1038 if (NULL == g_client_list) {
1039 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new client");
1046 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data SUCCESS] Add new client");
1049 #ifdef CLIENT_DATA_DEBUG
1050 __show_client_list();
1055 int vcd_client_delete(int pid)
1058 vc_client_info_s* client_info = NULL;
1061 tmp = __client_get_item(pid);
1063 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1064 return VCD_ERROR_INVALID_PARAMETER;
1067 /*Free client structure*/
1068 client_info = tmp->data;
1069 if (NULL != client_info) {
1073 /*Remove handle from list*/
1074 g_client_list = g_slist_remove_link(g_client_list, tmp);
1077 #ifdef CLIENT_DATA_DEBUG
1078 __show_client_list();
1084 bool vcd_client_is_available(int pid)
1086 vc_client_info_s* client_info = NULL;
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);
1097 int vcd_client_get_ref_count()
1101 count = g_slist_length(g_client_list) + g_slist_length(g_widget_list);
1102 if (0 < g_manager.pid) {
1106 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] client count : %d", count);
1111 int vcd_client_get_list(int** pids, int* pid_count)
1113 if (NULL == pids || NULL == pid_count)
1116 int count = g_slist_length(g_client_list);
1122 tmp = (int*)calloc(count, sizeof(int));
1124 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1125 return VCD_ERROR_OUT_OF_MEMORY;
1128 GSList *iter = NULL;
1129 vc_client_info_s *data = NULL;
1132 iter = g_slist_nth(g_client_list, 0);
1134 while (NULL != iter) {
1141 iter = g_slist_next(iter);
1151 int vcd_client_set_command_type(int pid, int type)
1153 vc_client_info_s* client_info = NULL;
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;
1162 case VC_COMMAND_TYPE_FOREGROUND:
1163 client_info->fg_cmd = true;
1165 case VC_COMMAND_TYPE_BACKGROUND:
1166 client_info->bg_cmd = true;
1169 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1176 int vcd_client_unset_command_type(int pid, int type)
1178 vc_client_info_s* client_info = NULL;
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;
1187 case VC_COMMAND_TYPE_FOREGROUND:
1188 client_info->fg_cmd = false;
1190 case VC_COMMAND_TYPE_BACKGROUND:
1191 client_info->bg_cmd = false;
1194 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1201 int vcd_client_set_exclusive_command(int pid)
1203 vc_client_info_s* client_info = NULL;
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;
1211 client_info->exclusive_cmd = true;
1216 int vcd_client_unset_exclusive_command(int pid)
1218 vc_client_info_s* client_info = NULL;
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;
1226 client_info->exclusive_cmd = false;
1231 int vcd_client_save_client_info()
1233 if (0 != vc_info_parser_set_client_info(g_client_list)) {
1234 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to save client info");
1242 * Functions for widget
1244 GSList* __widget_get_item(int pid)
1246 GSList *iter = NULL;
1247 widget_info_s *data = NULL;
1249 int count = g_slist_length(g_widget_list);
1253 iter = g_slist_nth(g_widget_list, 0);
1254 for (i = 0; i < count; i++) {
1261 if (pid == data->pid)
1265 iter = g_slist_next(iter);
1272 widget_info_s* __widget_get_element(int pid)
1274 GSList *iter = NULL;
1275 widget_info_s *data = NULL;
1277 int count = g_slist_length(g_widget_list);
1281 iter = g_slist_nth(g_widget_list, 0);
1284 while (iter && i < count) {
1288 if (pid == data->pid)
1292 iter = g_slist_next(iter);
1301 int vcd_client_widget_get_list(int** pids, int* pid_count)
1303 if (NULL == pids || NULL == pid_count)
1306 int count = g_slist_length(g_widget_list);
1312 tmp = (int*)calloc(count, sizeof(int));
1314 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1315 return VCD_ERROR_OUT_OF_MEMORY;
1318 GSList *iter = NULL;
1319 widget_info_s *data = NULL;
1322 iter = g_slist_nth(g_widget_list, 0);
1323 while (NULL != iter) {
1330 iter = g_slist_next(iter);
1340 int vcd_client_widget_add(int pid)
1342 /*Check pid is duplicated*/
1344 tmp = __widget_get_item(pid);
1347 SLOG(LOG_WARN, TAG_VCD, "[Client Data] widget pid is already registered");
1348 return VCD_ERROR_INVALID_PARAMETER;
1351 widget_info_s *info = (widget_info_s*)calloc(1, sizeof(widget_info_s));
1353 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1354 return VCD_ERROR_OUT_OF_MEMORY;
1358 info->widget_cmd = false;
1360 /* Add item to global list */
1361 g_widget_list = g_slist_append(g_widget_list, info);
1363 if (NULL == g_widget_list) {
1364 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new widget");
1371 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data SUCCESS] Add new widget");
1374 #ifdef CLIENT_DATA_DEBUG
1375 __show_client_list();
1380 int vcd_client_widget_delete(int pid)
1383 widget_info_s* widget_info = NULL;
1386 tmp = __widget_get_item(pid);
1388 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1389 return VCD_ERROR_INVALID_PARAMETER;
1392 /*Free client structure*/
1393 widget_info = tmp->data;
1394 if (NULL != widget_info) {
1398 /*Remove handle from list*/
1399 g_widget_list = g_slist_remove_link(g_widget_list, tmp);
1402 #ifdef CLIENT_DATA_DEBUG
1403 __show_client_list();
1409 int vcd_client_widget_get_foreground_pid()
1411 /* 1. Get foreground pid */
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 */
1418 widget_info_s* widget = NULL;
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");
1430 bool vcd_client_widget_is_available(int pid)
1432 widget_info_s* widget_info = NULL;
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);
1443 int vcd_client_widget_set_command(int pid)
1445 widget_info_s *info = NULL;
1446 info = __widget_get_element(pid);
1448 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1449 return VCD_ERROR_INVALID_PARAMETER;
1452 info->widget_cmd = true;
1457 int vcd_client_widget_unset_command(int pid)
1459 widget_info_s *info = NULL;
1460 info = __widget_get_element(pid);
1462 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1463 return VCD_ERROR_INVALID_PARAMETER;
1466 info->widget_cmd = false;
1471 int vcd_client_widget_set_asr_result_enabled(int pid, bool enable)
1473 widget_info_s *info = NULL;
1474 info = __widget_get_element(pid);
1476 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1477 return VCD_ERROR_INVALID_PARAMETER;
1480 info->asr_result_enabled = enable;
1485 int vcd_client_widget_get_asr_result_enabled(int pid, bool* enable)
1487 widget_info_s *info = NULL;
1488 info = __widget_get_element(pid);
1490 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1491 return VCD_ERROR_INVALID_PARAMETER;
1494 *enable = info->asr_result_enabled;
1499 int vcd_client_widget_set_waiting_for_recording(int pid, bool waiting)
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);
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);
1513 int vcd_client_widget_get_waiting_for_recording(int pid, bool* waiting)
1515 if (pid != g_waiting_recording_pid) {
1516 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT waiting pid", pid);
1520 *waiting = g_is_waiting_recording;
1521 SLOG(LOG_ERROR, TAG_VCD, "[INFO] Success to get waiting for recording, waiting(%d)", *waiting);
1525 void vcd_client_update_foreground_pid()
1527 int tmp_pid = VC_RUNTIME_INFO_NO_FOREGROUND;
1528 vcd_config_get_foreground(&tmp_pid);
1530 int pid = VC_RUNTIME_INFO_NO_FOREGROUND;
1531 int ret = aul_window_get_focused_pid(&pid);
1533 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get focused pid, ret(%d)", ret);
1535 SLOG(LOG_INFO, TAG_VCD, "[INFO] Foreground pid (%d), Focused pid (%d)", tmp_pid, pid);
1537 GSList *iter = NULL;
1538 vc_client_info_s *data = NULL;
1540 int count = g_slist_length(g_client_list);
1544 SLOG(LOG_DEBUG, TAG_VCD, "No Client");
1546 iter = g_slist_nth(g_client_list, 0);
1547 for (i = 0; i < count; i++) {
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");
1564 iter = g_slist_next(iter);
1568 widget_info_s *widget_data = NULL;
1569 count = g_slist_length(g_widget_list);
1572 SLOG(LOG_DEBUG, TAG_VCD, "No widget");
1574 iter = g_slist_nth(g_widget_list, 0);
1575 for (i = 0; i < count; i++) {
1579 widget_data = iter->data;
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");
1592 iter = g_slist_next(iter);
1596 SLOG(LOG_INFO, TAG_VCD, "[INFO] No foreground");
1597 vcd_config_set_foreground(VC_RUNTIME_INFO_NO_FOREGROUND, true);