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"
26 static GSList* g_client_list = NULL;
28 static GSList* g_widget_list = NULL;
30 static manager_info_s g_manager;
33 static current_commands_list_s g_cur_cmd_list;
35 /* Demandable client list */
36 static GSList* g_demandable_client = NULL;
39 static bool g_silence_detection;
40 static vcd_recognition_mode_e g_recognition_mode;
41 static char* g_result_text = NULL;
42 static bool g_is_waiting_recording = false;
43 static int g_waiting_recording_pid = -1;
45 /* Function definitions */
46 widget_info_s* __widget_get_element(int pid);
48 vc_client_info_s* __client_get_element(int pid);
51 int vcd_client_manager_set(int pid)
53 if (-1 != g_manager.pid && NULL != g_manager.appid) {
54 SLOG(LOG_DEBUG, TAG_VCD, "Manager has already registered");
58 g_manager.manager_cmd = false;
59 g_manager.exclusive_cmd_option = false;
60 g_manager.appid = NULL;
62 // Get appid by pid using app control
64 int ret = app_manager_get_app_id(pid, &appid);
65 if (0 != ret || NULL == appid) {
66 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
69 g_manager.appid = strdup(appid);
76 int vcd_client_manager_unset()
79 g_manager.manager_cmd = false;
80 g_manager.exclusive_cmd_option = false;
85 int vcd_client_manager_unset_appid()
87 if (NULL != g_manager.appid) {
88 free(g_manager.appid);
89 g_manager.appid = NULL;
94 bool vcd_client_manager_is_valid(int pid)
96 if (-1 == g_manager.pid || pid == g_manager.pid) {
102 int vcd_client_manager_set_command(int pid)
104 if (pid != g_manager.pid) {
105 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
108 g_manager.manager_cmd = true;
112 int vcd_client_manager_unset_command(int pid)
114 if (pid != g_manager.pid) {
115 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
118 g_manager.manager_cmd = false;
122 bool vcd_client_manager_is_system_command_valid(int pid)
124 if (pid != g_manager.pid) {
125 SLOG(LOG_WARN, TAG_VCD, "[Client Data WARNING] pid(%d) is NOT valid", pid);
129 if (true == g_manager.manager_cmd)
135 int vcd_client_manager_set_demandable_client(int pid, GSList* client_list)
137 if (0 != g_slist_length(g_demandable_client)) {
140 vc_demandable_client_s* temp_client;
141 iter = g_slist_nth(g_demandable_client, 0);
143 while (NULL != iter) {
144 temp_client = iter->data;
146 if (NULL != temp_client) {
147 if (NULL != temp_client->appid) free(temp_client->appid);
151 iter = g_slist_next(iter);
153 g_demandable_client = NULL;
156 g_demandable_client = client_list;
161 bool vcd_client_manager_check_demandable_client(int pid)
163 if (0 == g_slist_length(g_demandable_client)) {
164 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] All client is available to request start");
168 /* Check demandable appid */
169 char appid[1024] = {0, };
170 aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1);
172 if (0 < strlen(appid)) {
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 __show_client_list()
775 vc_client_info_s *data = NULL;
777 SLOG(LOG_DEBUG, TAG_VCD, "@@@ client list");
779 int count = g_slist_length(g_client_list);
783 SLOG(LOG_DEBUG, TAG_VCD, "No Client");
785 iter = g_slist_nth(g_client_list, 0);
786 for (i = 0; i < count; i++) {
793 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
795 iter = g_slist_next(iter);
799 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
801 SLOG(LOG_DEBUG, TAG_VCD, "@@@ widget list");
803 widget_info_s *widget_data = NULL;
805 count = g_slist_length(g_widget_list);
808 SLOG(LOG_DEBUG, TAG_VCD, "No widget");
810 iter = g_slist_nth(g_widget_list, 0);
811 for (i = 0; i < count; i++) {
815 widget_data = iter->data;
817 if (NULL != widget_data) {
818 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
820 iter = g_slist_next(iter);
824 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
829 int __show_command_list(GSList* cmd_group)
832 vc_cmd_s *data = NULL;
834 SLOG(LOG_DEBUG, TAG_VCD, "@@@ command group");
836 int count = g_slist_length(cmd_group);
840 SLOG(LOG_DEBUG, TAG_VCD, "No command");
842 iter = g_slist_nth(cmd_group, 0);
843 for (i = 0; i < count; i++) {
849 if (NULL != data->parameter) {
850 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) parameter(%s) key(%d)", i, data->command, data->parameter, data->key);
852 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) key(%d)", i, data->command, data->key);
855 iter = g_slist_next(iter);
859 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
864 GSList* __client_get_item(const int pid)
867 vc_client_info_s *data = NULL;
869 int count = g_slist_length(g_client_list);
873 iter = g_slist_nth(g_client_list, 0);
874 for (i = 0; i < count; i++) {
880 if (pid == data->pid)
884 iter = g_slist_next(iter);
891 vc_client_info_s* __client_get_element(int pid)
894 vc_client_info_s *data = NULL;
896 int count = g_slist_length(g_client_list);
900 iter = g_slist_nth(g_client_list, 0);
901 for (i = 0; i < count; i++) {
908 if (pid == data->pid)
912 iter = g_slist_next(iter);
919 int vcd_client_add(int pid)
921 /*Check pid is duplicated*/
923 tmp = __client_get_item(pid);
926 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Client pid is already registered");
927 return VCD_ERROR_INVALID_PARAMETER;
930 vc_client_info_s *info = (vc_client_info_s*)calloc(1, sizeof(vc_client_info_s));
932 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
933 return VCD_ERROR_OUT_OF_MEMORY;
938 info->fg_cmd = false;
939 info->bg_cmd = false;
940 info->exclusive_cmd = false;
942 /* Add item to global list */
943 g_client_list = g_slist_append(g_client_list, info);
945 if (NULL == g_client_list) {
946 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new client");
953 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data SUCCESS] Add new client");
956 #ifdef CLIENT_DATA_DEBUG
957 __show_client_list();
962 int vcd_client_delete(int pid)
965 vc_client_info_s* client_info = NULL;
968 tmp = __client_get_item(pid);
970 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
971 return VCD_ERROR_INVALID_PARAMETER;
974 /*Free client structure*/
975 client_info = tmp->data;
976 if (NULL != client_info) {
980 /*Remove handle from list*/
981 g_client_list = g_slist_remove_link(g_client_list, tmp);
984 #ifdef CLIENT_DATA_DEBUG
985 __show_client_list();
991 bool vcd_client_is_available(int pid)
993 vc_client_info_s* client_info = NULL;
995 client_info = __client_get_element(pid);
996 if (NULL == client_info) {
997 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1004 int vcd_client_get_ref_count()
1008 count = g_slist_length(g_client_list) + g_slist_length(g_widget_list);
1009 if (0 < g_manager.pid) {
1013 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] client count : %d", count);
1018 int vcd_client_get_list(int** pids, int* pid_count)
1020 if (NULL == pids || NULL == pid_count)
1023 int count = g_slist_length(g_client_list);
1029 tmp = (int*)calloc(count, sizeof(int));
1031 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1032 return VCD_ERROR_OUT_OF_MEMORY;
1035 GSList *iter = NULL;
1036 vc_client_info_s *data = NULL;
1039 iter = g_slist_nth(g_client_list, 0);
1041 while (NULL != iter) {
1048 iter = g_slist_next(iter);
1058 int vcd_client_set_command_type(int pid, int type)
1060 vc_client_info_s* client_info = NULL;
1062 client_info = __client_get_element(pid);
1063 if (NULL == client_info) {
1064 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1065 return VCD_ERROR_INVALID_PARAMETER;
1069 case VC_COMMAND_TYPE_FOREGROUND:
1070 client_info->fg_cmd = true;
1072 case VC_COMMAND_TYPE_BACKGROUND:
1073 client_info->bg_cmd = true;
1076 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1083 int vcd_client_unset_command_type(int pid, int type)
1085 vc_client_info_s* client_info = NULL;
1087 client_info = __client_get_element(pid);
1088 if (NULL == client_info) {
1089 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1090 return VCD_ERROR_INVALID_PARAMETER;
1094 case VC_COMMAND_TYPE_FOREGROUND:
1095 client_info->fg_cmd = false;
1097 case VC_COMMAND_TYPE_BACKGROUND:
1098 client_info->bg_cmd = false;
1101 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1108 int vcd_client_set_exclusive_command(int pid)
1110 vc_client_info_s* client_info = NULL;
1112 client_info = __client_get_element(pid);
1113 if (NULL == client_info) {
1114 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1115 return VCD_ERROR_INVALID_PARAMETER;
1118 client_info->exclusive_cmd = true;
1123 int vcd_client_unset_exclusive_command(int pid)
1125 vc_client_info_s* client_info = NULL;
1127 client_info = __client_get_element(pid);
1128 if (NULL == client_info) {
1129 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1130 return VCD_ERROR_INVALID_PARAMETER;
1133 client_info->exclusive_cmd = false;
1138 int vcd_client_save_client_info()
1140 if (0 != vc_info_parser_set_client_info(g_client_list)) {
1141 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to save client info");
1149 * Functions for widget
1151 GSList* __widget_get_item(int pid)
1153 GSList *iter = NULL;
1154 widget_info_s *data = NULL;
1156 int count = g_slist_length(g_widget_list);
1160 iter = g_slist_nth(g_widget_list, 0);
1161 for (i = 0; i < count; i++) {
1168 if (pid == data->pid)
1172 iter = g_slist_next(iter);
1179 widget_info_s* __widget_get_element(int pid)
1181 GSList *iter = NULL;
1182 widget_info_s *data = NULL;
1184 int count = g_slist_length(g_widget_list);
1188 iter = g_slist_nth(g_widget_list, 0);
1191 while (iter && i < count) {
1195 if (pid == data->pid)
1199 iter = g_slist_next(iter);
1208 int vcd_client_widget_get_list(int** pids, int* pid_count)
1210 if (NULL == pids || NULL == pid_count)
1213 int count = g_slist_length(g_widget_list);
1219 tmp = (int*)calloc(count, sizeof(int));
1221 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1222 return VCD_ERROR_OUT_OF_MEMORY;
1225 GSList *iter = NULL;
1226 widget_info_s *data = NULL;
1229 iter = g_slist_nth(g_widget_list, 0);
1230 while (NULL != iter) {
1237 iter = g_slist_next(iter);
1247 int vcd_client_widget_add(int pid)
1249 /*Check pid is duplicated*/
1251 tmp = __widget_get_item(pid);
1254 SLOG(LOG_WARN, TAG_VCD, "[Client Data] widget pid is already registered");
1255 return VCD_ERROR_INVALID_PARAMETER;
1258 widget_info_s *info = (widget_info_s*)calloc(1, sizeof(widget_info_s));
1260 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1261 return VCD_ERROR_OUT_OF_MEMORY;
1265 info->widget_cmd = false;
1267 /* Add item to global list */
1268 g_widget_list = g_slist_append(g_widget_list, info);
1270 if (NULL == g_widget_list) {
1271 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new widget");
1278 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data SUCCESS] Add new widget");
1281 #ifdef CLIENT_DATA_DEBUG
1282 __show_client_list();
1287 int vcd_client_widget_delete(int pid)
1290 widget_info_s* widget_info = NULL;
1293 tmp = __widget_get_item(pid);
1295 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1296 return VCD_ERROR_INVALID_PARAMETER;
1299 /*Free client structure*/
1300 widget_info = tmp->data;
1301 if (NULL != widget_info) {
1305 /*Remove handle from list*/
1306 g_widget_list = g_slist_remove_link(g_widget_list, tmp);
1309 #ifdef CLIENT_DATA_DEBUG
1310 __show_client_list();
1316 int vcd_client_widget_get_foreground_pid()
1318 /* 1. Get foreground pid */
1320 if (0 != vcd_config_get_foreground(&fg_pid)) {
1321 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get foreground pid");
1322 /* There is no foreground app for voice control */
1325 widget_info_s* widget = NULL;
1327 widget = __widget_get_element(fg_pid);
1328 if (NULL == widget) {
1329 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Not found foreground pid of widget");
1337 bool vcd_client_widget_is_available(int pid)
1339 widget_info_s* widget_info = NULL;
1341 widget_info = __widget_get_element(pid);
1342 if (NULL == widget_info) {
1343 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1350 int vcd_client_widget_set_command(int pid)
1352 widget_info_s *info = NULL;
1353 info = __widget_get_element(pid);
1355 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1356 return VCD_ERROR_INVALID_PARAMETER;
1359 info->widget_cmd = true;
1364 int vcd_client_widget_unset_command(int pid)
1366 widget_info_s *info = NULL;
1367 info = __widget_get_element(pid);
1369 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1370 return VCD_ERROR_INVALID_PARAMETER;
1373 info->widget_cmd = false;
1378 int vcd_client_widget_set_asr_result_enabled(int pid, bool enable)
1380 widget_info_s *info = NULL;
1381 info = __widget_get_element(pid);
1383 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1384 return VCD_ERROR_INVALID_PARAMETER;
1387 info->asr_result_enabled = enable;
1392 int vcd_client_widget_get_asr_result_enabled(int pid, bool* enable)
1394 widget_info_s *info = NULL;
1395 info = __widget_get_element(pid);
1397 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1398 return VCD_ERROR_INVALID_PARAMETER;
1401 *enable = info->asr_result_enabled;
1406 int vcd_client_widget_set_waiting_for_recording(int pid, bool waiting)
1409 if (TRUE == waiting && pid != vcd_client_widget_get_foreground_pid()) {
1410 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT forground pid", pid);
1414 g_is_waiting_recording = waiting;
1415 g_waiting_recording_pid = pid;
1416 SLOG(LOG_ERROR, TAG_VCD, "[INFO] Success to set waiting for recording, pid(%d), waiting(%d)", pid, waiting);
1420 int vcd_client_widget_get_waiting_for_recording(int pid, bool* waiting)
1422 if (pid != g_waiting_recording_pid) {
1423 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT waiting pid", pid);
1427 *waiting = g_is_waiting_recording;
1428 SLOG(LOG_ERROR, TAG_VCD, "[INFO] Success to get waiting for recording, waiting(%d)", *waiting);
1432 void vcd_client_update_foreground_pid()
1434 int tmp_pid = VC_RUNTIME_INFO_NO_FOREGROUND;
1435 vcd_config_get_foreground(&tmp_pid);
1437 int pid = VC_RUNTIME_INFO_NO_FOREGROUND;
1438 int ret = aul_window_get_focused_pid(&pid);
1440 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get focused pid, ret(%d)", ret);
1442 SLOG(LOG_INFO, TAG_VCD, "[INFO] Foreground pid (%d), Focused pid (%d)", tmp_pid, pid);
1444 GSList *iter = NULL;
1445 vc_client_info_s *data = NULL;
1447 int count = g_slist_length(g_client_list);
1451 SLOG(LOG_DEBUG, TAG_VCD, "No Client");
1453 iter = g_slist_nth(g_client_list, 0);
1454 for (i = 0; i < count; i++) {
1461 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
1462 if (pid == data->pid) {
1463 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid (%d)", data->pid);
1464 vcd_config_set_foreground(data->pid, true);
1465 if (tmp_pid != data->pid) {
1466 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid is different");
1471 iter = g_slist_next(iter);
1475 widget_info_s *widget_data = NULL;
1476 count = g_slist_length(g_widget_list);
1479 SLOG(LOG_DEBUG, TAG_VCD, "No widget");
1481 iter = g_slist_nth(g_widget_list, 0);
1482 for (i = 0; i < count; i++) {
1486 widget_data = iter->data;
1488 if (NULL != widget_data) {
1489 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
1490 if (pid == widget_data->pid) {
1491 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid (%d)", widget_data->pid);
1492 vcd_config_set_foreground(widget_data->pid, true);
1493 if (tmp_pid != widget_data->pid) {
1494 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid is changed");
1499 iter = g_slist_next(iter);
1503 SLOG(LOG_INFO, TAG_VCD, "[INFO] No foreground");
1504 vcd_config_set_foreground(VC_RUNTIME_INFO_NO_FOREGROUND, true);