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;
43 /* Function definitions */
44 widget_info_s* __widget_get_element(int pid);
46 vc_client_info_s* __client_get_element(int pid);
49 int vcd_client_manager_set(int pid)
51 if (-1 != g_manager.pid && NULL != g_manager.appid) {
52 SLOG(LOG_DEBUG, TAG_VCD, "Manager has already registered");
56 g_manager.manager_cmd = false;
57 g_manager.exclusive_cmd_option = false;
58 g_manager.appid = NULL;
60 // Get appid by pid using app control
62 int ret = app_manager_get_app_id(pid, &appid);
63 if (0 != ret || NULL == appid) {
64 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
67 g_manager.appid = strdup(appid);
74 int vcd_client_manager_unset()
77 g_manager.manager_cmd = false;
78 g_manager.exclusive_cmd_option = false;
83 int vcd_client_manager_unset_appid()
85 if (NULL != g_manager.appid) {
86 free(g_manager.appid);
87 g_manager.appid = NULL;
92 bool vcd_client_manager_is_valid(int pid)
94 if (-1 == g_manager.pid || pid == g_manager.pid) {
100 int vcd_client_manager_set_command(int pid)
102 if (pid != g_manager.pid) {
103 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
106 g_manager.manager_cmd = true;
110 int vcd_client_manager_unset_command(int pid)
112 if (pid != g_manager.pid) {
113 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
116 g_manager.manager_cmd = false;
120 bool vcd_client_manager_is_system_command_valid(int pid)
122 if (pid != g_manager.pid) {
123 SLOG(LOG_WARN, TAG_VCD, "[Client Data WARNING] pid(%d) is NOT valid", pid);
127 if (true == g_manager.manager_cmd)
133 int vcd_client_manager_set_demandable_client(int pid, GSList* client_list)
135 if (0 != g_slist_length(g_demandable_client)) {
138 vc_demandable_client_s* temp_client;
139 iter = g_slist_nth(g_demandable_client, 0);
141 while (NULL != iter) {
142 temp_client = iter->data;
144 if (NULL != temp_client) {
145 if (NULL != temp_client->appid) free(temp_client->appid);
149 iter = g_slist_next(iter);
151 g_demandable_client = NULL;
154 g_demandable_client = client_list;
159 bool vcd_client_manager_check_demandable_client(int pid)
161 if (0 == g_slist_length(g_demandable_client)) {
162 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] All client is available to request start");
166 /* Check demandable appid */
167 char appid[1024] = {0, };
168 aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1);
170 if (0 < strlen(appid)) {
171 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] %s(%d) requests start", appid, pid);
173 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] daemon(%d) requests start", pid);
178 vc_demandable_client_s* temp_client;
179 iter = g_slist_nth(g_demandable_client, 0);
181 while (NULL != iter) {
182 temp_client = iter->data;
184 if (NULL != temp_client) {
186 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] demandable appid(%s)", temp_client->appid);
188 if (NULL != temp_client->appid) {
189 if (0 == strcmp(temp_client->appid, appid)) {
190 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] pid(%d) is available", pid);
194 if (0 == strlen(appid)) {
195 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] pid(%d) is available", pid);
201 iter = g_slist_next(iter);
207 bool vcd_client_manager_get_exclusive()
209 return g_manager.exclusive_cmd_option;
212 int vcd_client_manager_set_exclusive(bool value)
214 g_manager.exclusive_cmd_option = value;
218 int vcd_client_manager_get_pid()
220 return g_manager.pid;
223 int vcd_client_manager_get_appid(char** appid)
225 if (NULL != g_manager.appid)
226 *appid = strdup(g_manager.appid);
231 int vcd_client_manager_set_result_text(const char* result)
233 if (NULL != g_result_text) {
235 g_result_text = NULL;
238 if (NULL != result) {
239 g_result_text = strdup(result);
245 char* vcd_client_manager_get_result_text()
247 return g_result_text;
250 static void __vcd_client_release_each_commands(GSList** cmds)
255 if (0 < g_slist_length(*cmds)) {
256 iter = g_slist_nth(*cmds, 0);
257 while (NULL != iter) {
258 temp_cmd = iter->data;
260 if (NULL != temp_cmd) {
261 if (NULL != temp_cmd->command) {
262 free(temp_cmd->command);
263 temp_cmd->command = NULL;
265 if (NULL != temp_cmd->appid) {
266 free(temp_cmd->appid);
267 temp_cmd->appid = NULL;
269 if (NULL != temp_cmd->parameter) {
270 free(temp_cmd->parameter);
271 temp_cmd->parameter = NULL;
273 if (NULL != temp_cmd->invocation_name) {
274 free(temp_cmd->invocation_name);
275 temp_cmd->invocation_name = NULL;
277 if (NULL != temp_cmd->fixed) {
278 free(temp_cmd->fixed);
279 temp_cmd->fixed = NULL;
284 *cmds = g_slist_remove_link(*cmds, iter);
286 iter = g_slist_nth(*cmds, 0);
292 int __vcd_client_release_commands()
294 g_cur_cmd_list.total_cmd_count = 0;
295 g_cur_cmd_list.foreground = VC_NO_FOREGROUND_PID;
297 __vcd_client_release_each_commands(&(g_cur_cmd_list.widget_cmds));
298 __vcd_client_release_each_commands(&(g_cur_cmd_list.system_cmds));
299 __vcd_client_release_each_commands(&(g_cur_cmd_list.system_background_cmds));
300 __vcd_client_release_each_commands(&(g_cur_cmd_list.exclusive_system_cmds));
301 __vcd_client_release_each_commands(&(g_cur_cmd_list.foreground_cmds));
302 __vcd_client_release_each_commands(&(g_cur_cmd_list.background_cmds));
307 int vcd_client_command_collect_command()
309 /* 1. Get foreground pid */
312 if (0 != vcd_config_get_foreground(&fg_pid)) {
313 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get foreground pid");
314 /* There is no foreground app for voice control */
317 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Foreground pid(%d)", fg_pid);
319 /* 2. Clean up command list */
320 __vcd_client_release_commands();
322 /* Check exclusive system command */
323 if (true == g_manager.exclusive_cmd_option && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_EXCLUSIVE)) {
324 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Exclusive option of manager is ON");
326 GSList* ex_sys_cmd_list = NULL;
327 if (true == g_manager.manager_cmd) {
328 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_EXCLUSIVE, &ex_sys_cmd_list);
330 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
332 g_cur_cmd_list.exclusive_system_cmds = ex_sys_cmd_list;
335 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No exclusive system commands");
341 /* 3. Set system command */
342 GSList* sys_cmd_list = NULL;
343 if (true == g_manager.manager_cmd && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_SYSTEM)) {
344 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_SYSTEM, &sys_cmd_list);
346 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
348 g_cur_cmd_list.system_cmds = sys_cmd_list;
351 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system commands");
354 GSList* sys_back_cmd_list = NULL;
355 if (true == g_manager.manager_cmd && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_SYSTEM_BACKGROUND)) {
356 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_SYSTEM_BACKGROUND, &sys_back_cmd_list);
358 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
360 g_cur_cmd_list.system_background_cmds = sys_back_cmd_list;
363 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system background commands");
366 /* 4. Set foreground commands and widget */
367 GSList* fg_cmd_list = NULL;
368 if (VC_NO_FOREGROUND_PID != fg_pid) {
369 GSList* widget_cmd_list = NULL;
371 g_cur_cmd_list.foreground = fg_pid;
373 /* 4-1. Set widget command */
374 widget_info_s* widget_info = NULL;
375 widget_info = __widget_get_element(fg_pid);
376 if (NULL != widget_info && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
377 if (true == widget_info->widget_cmd) {
378 ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_WIDGET, &widget_cmd_list);
380 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the WIDGET command list");
382 g_cur_cmd_list.widget_cmds = widget_cmd_list;
386 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No widget commands");
389 /* 4-2. Set foreground command of foreground app */
390 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_FOREGROUND)) {
391 ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_FOREGROUND, &fg_cmd_list);
393 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the fg command list");
395 g_cur_cmd_list.foreground_cmds = fg_cmd_list;
399 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground app");
401 if (true == g_manager.manager_cmd && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_FOREGROUND)) {
402 /* 4-3. Set foreground command by manager */
403 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_FOREGROUND, &fg_cmd_list);
405 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground commands of manager app");
407 g_cur_cmd_list.foreground_cmds = fg_cmd_list;
411 /* 5. Set background commands */
412 GSList* bg_cmd_list = NULL;
413 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_BACKGROUND)) {
414 ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_BACKGROUND, &bg_cmd_list);
416 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the bg command list");
418 /* Add item to global command list */
419 g_cur_cmd_list.background_cmds = bg_cmd_list;
425 int vcd_client_get_length()
427 int command_count = 0;
428 command_count += g_slist_length(g_cur_cmd_list.widget_cmds);
429 command_count += g_slist_length(g_cur_cmd_list.system_cmds);
430 command_count += g_slist_length(g_cur_cmd_list.exclusive_system_cmds);
431 command_count += g_slist_length(g_cur_cmd_list.system_background_cmds);
432 command_count += g_slist_length(g_cur_cmd_list.foreground_cmds);
433 command_count += g_slist_length(g_cur_cmd_list.background_cmds);
435 g_cur_cmd_list.total_cmd_count = command_count;
437 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Command count : %d ", g_cur_cmd_list.total_cmd_count);
438 return command_count;
441 int vcd_client_foreach_command(client_foreach_command_cb callback, void* user_data)
443 if (NULL == callback) {
444 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] input parameter is NULL");
445 return VCD_ERROR_INVALID_PARAMETER;
448 if (0 != vcd_client_command_collect_command()) {
449 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
450 return VCD_ERROR_OPERATION_FAILED;
453 if (0 >= vcd_client_get_length()) {
454 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] No current command");
455 return VCD_ERROR_OPERATION_FAILED;
462 if (0 < g_slist_length(g_cur_cmd_list.widget_cmds)) {
463 iter = g_slist_nth(g_cur_cmd_list.widget_cmds, 0);
464 while (NULL != iter) {
465 temp_cmd = iter->data;
467 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Widget : id(%d) type(%d) format(%d) command(%s) domain(%d)"
468 , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->domain);
470 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
472 iter = g_slist_next(iter);
475 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No widget commands");
478 if (0 < g_slist_length(g_cur_cmd_list.foreground_cmds)) {
479 iter = g_slist_nth(g_cur_cmd_list.foreground_cmds, 0);
480 while (NULL != iter) {
481 temp_cmd = iter->data;
483 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Fore : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d) fixed(%s)"
484 , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, temp_cmd->fixed);
486 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
488 iter = g_slist_next(iter);
491 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground commands");
494 if (0 < g_slist_length(g_cur_cmd_list.system_cmds)) {
495 iter = g_slist_nth(g_cur_cmd_list.system_cmds, 0);
496 while (NULL != iter) {
497 temp_cmd = iter->data;
499 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] System : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d)"
500 , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
502 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
504 iter = g_slist_next(iter);
507 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system commands");
510 if (0 < g_slist_length(g_cur_cmd_list.system_background_cmds)) {
511 iter = g_slist_nth(g_cur_cmd_list.system_background_cmds, 0);
512 while (NULL != iter) {
513 temp_cmd = iter->data;
515 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] System background : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d)"
516 , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
518 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
520 iter = g_slist_next(iter);
523 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system background commands");
526 if (0 < g_slist_length(g_cur_cmd_list.exclusive_system_cmds)) {
527 iter = g_slist_nth(g_cur_cmd_list.exclusive_system_cmds, 0);
528 while (NULL != iter) {
529 temp_cmd = iter->data;
531 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Exclusive system : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d)"
532 , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
534 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
536 iter = g_slist_next(iter);
539 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No exclusive system commands");
542 if (0 < g_slist_length(g_cur_cmd_list.background_cmds)) {
543 iter = g_slist_nth(g_cur_cmd_list.background_cmds, 0);
544 while (NULL != iter) {
545 temp_cmd = iter->data;
547 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Back : id(%d) format(%d) type(%d) command(%s) param(%s) domain(%d) appid(%s) invocation(%s) fixed(%s)"
548 , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, temp_cmd->appid, temp_cmd->invocation_name, temp_cmd->fixed);
550 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
552 iter = g_slist_next(iter);
555 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No background commands");
560 static vc_cmd_s* __command_copy(vc_cmd_s* src_cmd)
562 if (NULL == src_cmd) {
563 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Input command is NULL");
567 vc_cmd_s* temp_cmd = NULL;
568 temp_cmd = (vc_cmd_s*)calloc(sizeof(vc_cmd_s), 1);
569 if (NULL == temp_cmd) {
570 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
574 temp_cmd->id = src_cmd->id;
575 temp_cmd->pid = src_cmd->pid;
576 temp_cmd->index = src_cmd->index;
577 temp_cmd->type = src_cmd->type;
578 temp_cmd->format = src_cmd->format;
579 temp_cmd->domain = src_cmd->domain;
581 if (VC_COMMAND_TYPE_SYSTEM == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM;
582 else if (VC_COMMAND_TYPE_EXCLUSIVE == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_EXCLUSIVE;
583 else if (VC_COMMAND_TYPE_WIDGET == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_WIDGET;
584 else if (VC_COMMAND_TYPE_FOREGROUND == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
585 else if (VC_COMMAND_TYPE_SYSTEM_BACKGROUND == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM_BACKGROUND;
586 else if (VC_COMMAND_TYPE_BACKGROUND == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_BACKGROUND;
588 if (NULL != src_cmd->command) {
589 temp_cmd->command = strdup(src_cmd->command);
592 if (NULL != src_cmd->parameter) {
593 temp_cmd->parameter = strdup(src_cmd->parameter);
596 if (NULL != src_cmd->appid) {
597 temp_cmd->appid = strdup(src_cmd->appid);
600 if (NULL != src_cmd->invocation_name) {
601 temp_cmd->invocation_name = strdup(src_cmd->invocation_name);
604 if (NULL != src_cmd->fixed) {
605 temp_cmd->fixed = strdup(src_cmd->fixed);
608 temp_cmd->key = src_cmd->key;
609 temp_cmd->modifier = src_cmd->modifier;
614 int vcd_client_get_cmd_from_result_id(int result_id, vc_cmd_s** result)
619 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Result id(%d)", result_id);
621 if (0 < g_slist_length(g_cur_cmd_list.widget_cmds)) {
622 iter = g_slist_nth(g_cur_cmd_list.widget_cmds, 0);
623 while (NULL != iter) {
624 temp_cmd = iter->data;
626 if (result_id == temp_cmd->id) {
627 /**pid = g_cur_cmd_list.foreground; */
628 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_UI_CONTROL; */
629 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
631 *result = __command_copy(temp_cmd);
636 iter = g_slist_next(iter);
639 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No widget commands");
642 if (0 < g_slist_length(g_cur_cmd_list.foreground_cmds)) {
643 iter = g_slist_nth(g_cur_cmd_list.foreground_cmds, 0);
645 while (NULL != iter) {
646 temp_cmd = iter->data;
648 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] command id (%d)", temp_cmd->id);
650 if (result_id == temp_cmd->id) {
651 /**pid = g_cur_cmd_list.foreground; */
652 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_FOREGROUND; */
653 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
655 *result = __command_copy(temp_cmd);
659 iter = g_slist_next(iter);
662 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground commands");
665 if (0 < g_slist_length(g_cur_cmd_list.system_cmds)) {
666 iter = g_slist_nth(g_cur_cmd_list.system_cmds, 0);
667 while (NULL != iter) {
668 temp_cmd = iter->data;
670 if (result_id == temp_cmd->id) {
671 /**pid = g_manager.pid; */
672 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM; */
673 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
675 *result = __command_copy(temp_cmd);
679 iter = g_slist_next(iter);
682 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system commands");
685 if (0 < g_slist_length(g_cur_cmd_list.system_background_cmds)) {
686 iter = g_slist_nth(g_cur_cmd_list.system_background_cmds, 0);
687 while (NULL != iter) {
688 temp_cmd = iter->data;
690 if (result_id == temp_cmd->id) {
691 /**pid = g_manager.pid; */
692 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM_BACKGROUND; */
693 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
695 *result = __command_copy(temp_cmd);
699 iter = g_slist_next(iter);
702 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system background commands");
705 if (0 < g_slist_length(g_cur_cmd_list.exclusive_system_cmds)) {
706 iter = g_slist_nth(g_cur_cmd_list.exclusive_system_cmds, 0);
707 while (NULL != iter) {
708 temp_cmd = iter->data;
710 if (result_id == temp_cmd->id) {
711 /**pid = g_manager.pid; */
712 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM_EXCLUSIVE; */
713 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
715 *result = __command_copy(temp_cmd);
719 iter = g_slist_next(iter);
722 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No exclusive system commands");
725 if (0 < g_slist_length(g_cur_cmd_list.background_cmds)) {
726 iter = g_slist_nth(g_cur_cmd_list.background_cmds, 0);
728 while (NULL != iter) {
729 temp_cmd = iter->data;
731 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] command id (%d)", temp_cmd->id);
733 if (result_id == temp_cmd->id) {
734 *result = __command_copy(temp_cmd);
738 iter = g_slist_next(iter);
741 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No background commands");
744 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Not find matched result");
748 int vcd_client_set_slience_detection(bool value)
750 g_silence_detection = value;
754 bool vcd_client_get_slience_detection()
756 return g_silence_detection;
759 int vcd_client_set_recognition_mode(vcd_recognition_mode_e mode)
761 g_recognition_mode = mode;
765 vcd_recognition_mode_e vcd_client_get_recognition_mode()
767 return g_recognition_mode;
770 int __show_client_list()
773 vc_client_info_s *data = NULL;
775 SLOG(LOG_DEBUG, TAG_VCD, "@@@ client list");
777 int count = g_slist_length(g_client_list);
781 SLOG(LOG_DEBUG, TAG_VCD, "No Client");
783 iter = g_slist_nth(g_client_list, 0);
784 for (i = 0; i < count; i++) {
791 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
793 iter = g_slist_next(iter);
797 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
799 SLOG(LOG_DEBUG, TAG_VCD, "@@@ widget list");
801 widget_info_s *widget_data = NULL;
803 count = g_slist_length(g_widget_list);
806 SLOG(LOG_DEBUG, TAG_VCD, "No widget");
808 iter = g_slist_nth(g_widget_list, 0);
809 for (i = 0; i < count; i++) {
813 widget_data = iter->data;
815 if (NULL != widget_data) {
816 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
818 iter = g_slist_next(iter);
822 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
827 int __show_command_list(GSList* cmd_group)
830 vc_cmd_s *data = NULL;
832 SLOG(LOG_DEBUG, TAG_VCD, "@@@ command group");
834 int count = g_slist_length(cmd_group);
838 SLOG(LOG_DEBUG, TAG_VCD, "No command");
840 iter = g_slist_nth(cmd_group, 0);
841 for (i = 0; i < count; i++) {
847 if (NULL != data->parameter) {
848 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) parameter(%s) key(%d)", i, data->command, data->parameter, data->key);
850 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) key(%d)", i, data->command, data->key);
853 iter = g_slist_next(iter);
857 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
862 GSList* __client_get_item(const int pid)
865 vc_client_info_s *data = NULL;
867 int count = g_slist_length(g_client_list);
871 iter = g_slist_nth(g_client_list, 0);
872 for (i = 0; i < count; i++) {
878 if (pid == data->pid)
882 iter = g_slist_next(iter);
889 vc_client_info_s* __client_get_element(int pid)
892 vc_client_info_s *data = NULL;
894 int count = g_slist_length(g_client_list);
898 iter = g_slist_nth(g_client_list, 0);
899 for (i = 0; i < count; i++) {
906 if (pid == data->pid)
910 iter = g_slist_next(iter);
917 int vcd_client_add(int pid)
919 /*Check pid is duplicated*/
921 tmp = __client_get_item(pid);
924 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Client pid is already registered");
925 return VCD_ERROR_INVALID_PARAMETER;
928 vc_client_info_s *info = (vc_client_info_s*)calloc(1, sizeof(vc_client_info_s));
930 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
931 return VCD_ERROR_OUT_OF_MEMORY;
936 info->fg_cmd = false;
937 info->bg_cmd = false;
938 info->exclusive_cmd = false;
940 /* Add item to global list */
941 g_client_list = g_slist_append(g_client_list, info);
943 if (NULL == g_client_list) {
944 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new client");
951 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data SUCCESS] Add new client");
954 #ifdef CLIENT_DATA_DEBUG
955 __show_client_list();
960 int vcd_client_delete(int pid)
963 vc_client_info_s* client_info = NULL;
966 tmp = __client_get_item(pid);
968 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
969 return VCD_ERROR_INVALID_PARAMETER;
972 /*Free client structure*/
973 client_info = tmp->data;
974 if (NULL != client_info) {
978 /*Remove handle from list*/
979 g_client_list = g_slist_remove_link(g_client_list, tmp);
982 #ifdef CLIENT_DATA_DEBUG
983 __show_client_list();
989 bool vcd_client_is_available(int pid)
991 vc_client_info_s* client_info = NULL;
993 client_info = __client_get_element(pid);
994 if (NULL == client_info) {
995 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1002 int vcd_client_get_ref_count()
1006 count = g_slist_length(g_client_list) + g_slist_length(g_widget_list);
1007 if (0 < g_manager.pid) {
1011 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] client count : %d", count);
1016 int vcd_client_get_list(int** pids, int* pid_count)
1018 if (NULL == pids || NULL == pid_count)
1021 int count = g_slist_length(g_client_list);
1027 tmp = (int*)calloc(count, sizeof(int));
1029 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1030 return VCD_ERROR_OUT_OF_MEMORY;
1033 GSList *iter = NULL;
1034 vc_client_info_s *data = NULL;
1037 iter = g_slist_nth(g_client_list, 0);
1039 while (NULL != iter) {
1046 iter = g_slist_next(iter);
1056 int vcd_client_set_command_type(int pid, int type)
1058 vc_client_info_s* client_info = NULL;
1060 client_info = __client_get_element(pid);
1061 if (NULL == client_info) {
1062 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1063 return VCD_ERROR_INVALID_PARAMETER;
1067 case VC_COMMAND_TYPE_FOREGROUND:
1068 client_info->fg_cmd = true;
1070 case VC_COMMAND_TYPE_BACKGROUND:
1071 client_info->bg_cmd = true;
1074 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1081 int vcd_client_unset_command_type(int pid, int type)
1083 vc_client_info_s* client_info = NULL;
1085 client_info = __client_get_element(pid);
1086 if (NULL == client_info) {
1087 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1088 return VCD_ERROR_INVALID_PARAMETER;
1092 case VC_COMMAND_TYPE_FOREGROUND:
1093 client_info->fg_cmd = false;
1095 case VC_COMMAND_TYPE_BACKGROUND:
1096 client_info->bg_cmd = false;
1099 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1106 int vcd_client_set_exclusive_command(int pid)
1108 vc_client_info_s* client_info = NULL;
1110 client_info = __client_get_element(pid);
1111 if (NULL == client_info) {
1112 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1113 return VCD_ERROR_INVALID_PARAMETER;
1116 client_info->exclusive_cmd = true;
1121 int vcd_client_unset_exclusive_command(int pid)
1123 vc_client_info_s* client_info = NULL;
1125 client_info = __client_get_element(pid);
1126 if (NULL == client_info) {
1127 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1128 return VCD_ERROR_INVALID_PARAMETER;
1131 client_info->exclusive_cmd = false;
1136 int vcd_client_save_client_info()
1138 if (0 != vc_info_parser_set_client_info(g_client_list)) {
1139 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to save client info");
1147 * Functions for widget
1149 GSList* __widget_get_item(int pid)
1151 GSList *iter = NULL;
1152 widget_info_s *data = NULL;
1154 int count = g_slist_length(g_widget_list);
1158 iter = g_slist_nth(g_widget_list, 0);
1159 for (i = 0; i < count; i++) {
1166 if (pid == data->pid)
1170 iter = g_slist_next(iter);
1177 widget_info_s* __widget_get_element(int pid)
1179 GSList *iter = NULL;
1180 widget_info_s *data = NULL;
1182 int count = g_slist_length(g_widget_list);
1186 iter = g_slist_nth(g_widget_list, 0);
1189 while (iter && i < count) {
1193 if (pid == data->pid)
1197 iter = g_slist_next(iter);
1206 int vcd_client_widget_get_list(int** pids, int* pid_count)
1208 if (NULL == pids || NULL == pid_count)
1211 int count = g_slist_length(g_widget_list);
1217 tmp = (int*)calloc(count, sizeof(int));
1219 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1220 return VCD_ERROR_OUT_OF_MEMORY;
1223 GSList *iter = NULL;
1224 widget_info_s *data = NULL;
1227 iter = g_slist_nth(g_widget_list, 0);
1228 while (NULL != iter) {
1235 iter = g_slist_next(iter);
1245 int vcd_client_widget_add(int pid)
1247 /*Check pid is duplicated*/
1249 tmp = __widget_get_item(pid);
1252 SLOG(LOG_WARN, TAG_VCD, "[Client Data] widget pid is already registered");
1253 return VCD_ERROR_INVALID_PARAMETER;
1256 widget_info_s *info = (widget_info_s*)calloc(1, sizeof(widget_info_s));
1258 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1259 return VCD_ERROR_OUT_OF_MEMORY;
1263 info->widget_cmd = false;
1265 /* Add item to global list */
1266 g_widget_list = g_slist_append(g_widget_list, info);
1268 if (NULL == g_widget_list) {
1269 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new widget");
1276 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data SUCCESS] Add new widget");
1279 #ifdef CLIENT_DATA_DEBUG
1280 __show_client_list();
1285 int vcd_client_widget_delete(int pid)
1288 widget_info_s* widget_info = NULL;
1291 tmp = __widget_get_item(pid);
1293 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1294 return VCD_ERROR_INVALID_PARAMETER;
1297 /*Free client structure*/
1298 widget_info = tmp->data;
1299 if (NULL != widget_info) {
1303 /*Remove handle from list*/
1304 g_widget_list = g_slist_remove_link(g_widget_list, tmp);
1307 #ifdef CLIENT_DATA_DEBUG
1308 __show_client_list();
1314 int vcd_client_widget_get_foreground_pid()
1316 /* 1. Get foreground pid */
1318 if (0 != vcd_config_get_foreground(&fg_pid)) {
1319 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get foreground pid");
1320 /* There is no foreground app for voice control */
1323 widget_info_s* widget = NULL;
1325 widget = __widget_get_element(fg_pid);
1326 if (NULL == widget) {
1327 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Not found foreground pid of widget");
1335 bool vcd_client_widget_is_available(int pid)
1337 widget_info_s* widget_info = NULL;
1339 widget_info = __widget_get_element(pid);
1340 if (NULL == widget_info) {
1341 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1348 int vcd_client_widget_set_command(int pid)
1350 widget_info_s *info = NULL;
1351 info = __widget_get_element(pid);
1353 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1354 return VCD_ERROR_INVALID_PARAMETER;
1357 info->widget_cmd = true;
1362 int vcd_client_widget_unset_command(int pid)
1364 widget_info_s *info = NULL;
1365 info = __widget_get_element(pid);
1367 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1368 return VCD_ERROR_INVALID_PARAMETER;
1371 info->widget_cmd = false;
1376 int vcd_client_widget_set_asr_result_enabled(int pid, bool enable)
1378 widget_info_s *info = NULL;
1379 info = __widget_get_element(pid);
1381 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1382 return VCD_ERROR_INVALID_PARAMETER;
1385 info->asr_result_enabled = enable;
1390 int vcd_client_widget_get_asr_result_enabled(int pid, bool* enable)
1392 widget_info_s *info = NULL;
1393 info = __widget_get_element(pid);
1395 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1396 return VCD_ERROR_INVALID_PARAMETER;
1399 *enable = info->asr_result_enabled;
1404 void vcd_client_update_foreground_pid()
1406 int tmp_pid = VC_RUNTIME_INFO_NO_FOREGROUND;
1407 vcd_config_get_foreground(&tmp_pid);
1409 int pid = VC_RUNTIME_INFO_NO_FOREGROUND;
1410 int ret = aul_window_get_focused_pid(&pid);
1412 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get focused pid, ret(%d)", ret);
1414 SLOG(LOG_INFO, TAG_VCD, "[INFO] Foreground pid (%d), Focused pid (%d)", tmp_pid, pid);
1416 GSList *iter = NULL;
1417 vc_client_info_s *data = NULL;
1419 int count = g_slist_length(g_client_list);
1423 SLOG(LOG_DEBUG, TAG_VCD, "No Client");
1425 iter = g_slist_nth(g_client_list, 0);
1426 for (i = 0; i < count; i++) {
1433 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
1434 if (pid == data->pid) {
1435 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid (%d)", data->pid);
1436 vcd_config_set_foreground(data->pid, true);
1437 if (tmp_pid != data->pid) {
1438 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid is different");
1443 iter = g_slist_next(iter);
1447 widget_info_s *widget_data = NULL;
1448 count = g_slist_length(g_widget_list);
1451 SLOG(LOG_DEBUG, TAG_VCD, "No widget");
1453 iter = g_slist_nth(g_widget_list, 0);
1454 for (i = 0; i < count; i++) {
1458 widget_data = iter->data;
1460 if (NULL != widget_data) {
1461 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
1462 if (pid == widget_data->pid) {
1463 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid (%d)", widget_data->pid);
1464 vcd_config_set_foreground(widget_data->pid, true);
1465 if (tmp_pid != widget_data->pid) {
1466 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid is changed");
1471 iter = g_slist_next(iter);
1475 SLOG(LOG_INFO, TAG_VCD, "[INFO] No foreground");
1476 vcd_config_set_foreground(VC_RUNTIME_INFO_NO_FOREGROUND, true);