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 // Get appid by pid using app control
57 int ret = app_manager_get_app_id(pid, &appid);
58 if (0 != ret || NULL == appid) {
59 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
63 if (-1 != g_manager.pid && NULL != g_manager.appid && 0 == strncmp(g_manager.appid, appid, strlen(g_manager.appid))) {
64 SLOG(LOG_WARN, TAG_VCD, "Same manager has already registered. It doesn't need to set manager again.");
71 g_manager.manager_cmd = false;
72 g_manager.exclusive_cmd_option = false;
73 g_manager.appid = NULL;
75 g_manager.appid = strdup(appid);
82 int vcd_client_manager_unset()
85 g_manager.manager_cmd = false;
86 g_manager.exclusive_cmd_option = false;
91 int vcd_client_manager_unset_appid()
93 if (NULL != g_manager.appid) {
94 free(g_manager.appid);
95 g_manager.appid = NULL;
100 bool vcd_client_manager_is_valid(int pid)
102 if (-1 == g_manager.pid || pid == g_manager.pid) {
108 int vcd_client_manager_set_command(int pid)
110 if (pid != g_manager.pid) {
111 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
114 g_manager.manager_cmd = true;
118 int vcd_client_manager_unset_command(int pid)
120 if (pid != g_manager.pid) {
121 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
124 g_manager.manager_cmd = false;
128 bool vcd_client_manager_is_system_command_valid(int pid)
130 if (pid != g_manager.pid) {
131 SLOG(LOG_WARN, TAG_VCD, "[Client Data WARNING] pid(%d) is NOT valid", pid);
135 if (true == g_manager.manager_cmd)
141 int vcd_client_manager_set_demandable_client(int pid, GSList* client_list)
143 if (0 != g_slist_length(g_demandable_client)) {
146 vc_demandable_client_s* temp_client;
147 iter = g_slist_nth(g_demandable_client, 0);
149 while (NULL != iter) {
150 temp_client = iter->data;
152 if (NULL != temp_client) {
153 if (NULL != temp_client->appid) free(temp_client->appid);
157 iter = g_slist_next(iter);
159 g_demandable_client = NULL;
162 g_demandable_client = client_list;
167 bool vcd_client_manager_check_demandable_client(int pid)
169 if (0 == g_slist_length(g_demandable_client)) {
170 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] All client is available to request start");
174 /* Check demandable appid */
175 char appid[1024] = {0, };
176 if (0 == aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
177 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] %s(%d) requests start", appid, pid);
179 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] daemon(%d) requests start", pid);
184 vc_demandable_client_s* temp_client;
185 iter = g_slist_nth(g_demandable_client, 0);
187 while (NULL != iter) {
188 temp_client = iter->data;
190 if (NULL != temp_client) {
192 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] demandable appid(%s)", temp_client->appid);
194 if (NULL != temp_client->appid) {
195 if (0 == strcmp(temp_client->appid, appid)) {
196 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] pid(%d) is available", pid);
200 if (0 == strlen(appid)) {
201 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] pid(%d) is available", pid);
207 iter = g_slist_next(iter);
213 bool vcd_client_manager_get_exclusive()
215 return g_manager.exclusive_cmd_option;
218 int vcd_client_manager_set_exclusive(bool value)
220 g_manager.exclusive_cmd_option = value;
224 int vcd_client_manager_get_pid()
226 return g_manager.pid;
229 int vcd_client_manager_get_appid(char** appid)
231 if (NULL != g_manager.appid)
232 *appid = strdup(g_manager.appid);
237 int vcd_client_manager_set_result_text(const char* result)
239 if (NULL != g_result_text) {
241 g_result_text = NULL;
244 if (NULL != result) {
245 g_result_text = strdup(result);
251 char* vcd_client_manager_get_result_text()
253 return g_result_text;
256 static void __vcd_client_release_each_commands(GSList** cmds)
261 if (0 < g_slist_length(*cmds)) {
262 iter = g_slist_nth(*cmds, 0);
263 while (NULL != iter) {
264 temp_cmd = iter->data;
266 if (NULL != temp_cmd) {
267 if (NULL != temp_cmd->command) {
268 free(temp_cmd->command);
269 temp_cmd->command = NULL;
271 if (NULL != temp_cmd->appid) {
272 free(temp_cmd->appid);
273 temp_cmd->appid = NULL;
275 if (NULL != temp_cmd->parameter) {
276 free(temp_cmd->parameter);
277 temp_cmd->parameter = NULL;
279 if (NULL != temp_cmd->invocation_name) {
280 free(temp_cmd->invocation_name);
281 temp_cmd->invocation_name = NULL;
283 if (NULL != temp_cmd->fixed) {
284 free(temp_cmd->fixed);
285 temp_cmd->fixed = NULL;
290 *cmds = g_slist_remove_link(*cmds, iter);
292 iter = g_slist_nth(*cmds, 0);
298 int __vcd_client_release_commands()
300 g_cur_cmd_list.total_cmd_count = 0;
301 g_cur_cmd_list.foreground = VC_NO_FOREGROUND_PID;
303 __vcd_client_release_each_commands(&(g_cur_cmd_list.widget_cmds));
304 __vcd_client_release_each_commands(&(g_cur_cmd_list.system_cmds));
305 __vcd_client_release_each_commands(&(g_cur_cmd_list.system_background_cmds));
306 __vcd_client_release_each_commands(&(g_cur_cmd_list.exclusive_system_cmds));
307 __vcd_client_release_each_commands(&(g_cur_cmd_list.foreground_cmds));
308 __vcd_client_release_each_commands(&(g_cur_cmd_list.background_cmds));
313 int vcd_client_command_collect_command()
315 /* 1. Get foreground pid */
318 if (0 != vcd_config_get_foreground(&fg_pid)) {
319 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get foreground pid");
320 /* There is no foreground app for voice control */
323 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Foreground pid(%d)", fg_pid);
325 /* 2. Clean up command list */
326 __vcd_client_release_commands();
328 /* Check exclusive system command */
329 if (true == g_manager.exclusive_cmd_option && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_EXCLUSIVE)) {
330 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Exclusive option of manager is ON");
332 GSList* ex_sys_cmd_list = NULL;
333 if (true == g_manager.manager_cmd) {
334 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_EXCLUSIVE, &ex_sys_cmd_list);
336 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
338 g_cur_cmd_list.exclusive_system_cmds = ex_sys_cmd_list;
341 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No exclusive system commands");
347 /* 3. Set system command */
348 GSList* sys_cmd_list = NULL;
349 if (true == g_manager.manager_cmd && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_SYSTEM)) {
350 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_SYSTEM, &sys_cmd_list);
352 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
354 g_cur_cmd_list.system_cmds = sys_cmd_list;
357 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system commands");
360 GSList* sys_back_cmd_list = NULL;
361 if (true == g_manager.manager_cmd && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_SYSTEM_BACKGROUND)) {
362 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_SYSTEM_BACKGROUND, &sys_back_cmd_list);
364 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
366 g_cur_cmd_list.system_background_cmds = sys_back_cmd_list;
369 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system background commands");
372 /* 4. Set foreground commands and widget */
373 GSList* fg_cmd_list = NULL;
374 if (VC_NO_FOREGROUND_PID != fg_pid) {
375 GSList* widget_cmd_list = NULL;
377 g_cur_cmd_list.foreground = fg_pid;
379 /* 4-1. Set widget command */
380 widget_info_s* widget_info = NULL;
381 widget_info = __widget_get_element(fg_pid);
382 if (NULL != widget_info && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
383 if (true == widget_info->widget_cmd) {
384 ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_WIDGET, &widget_cmd_list);
386 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the WIDGET command list");
388 g_cur_cmd_list.widget_cmds = widget_cmd_list;
392 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No widget commands");
395 /* 4-2. Set foreground command of foreground app */
396 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_FOREGROUND)) {
397 ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_FOREGROUND, &fg_cmd_list);
399 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the fg command list");
401 g_cur_cmd_list.foreground_cmds = fg_cmd_list;
405 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground app");
407 if (true == g_manager.manager_cmd && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_FOREGROUND)) {
408 /* 4-3. Set foreground command by manager */
409 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_FOREGROUND, &fg_cmd_list);
411 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground commands of manager app");
413 g_cur_cmd_list.foreground_cmds = fg_cmd_list;
417 /* 5. Set background commands */
418 GSList* bg_cmd_list = NULL;
419 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_BACKGROUND)) {
420 ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_BACKGROUND, &bg_cmd_list);
422 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the bg command list");
424 /* Add item to global command list */
425 g_cur_cmd_list.background_cmds = bg_cmd_list;
431 int vcd_client_get_length()
433 int command_count = 0;
434 command_count += g_slist_length(g_cur_cmd_list.widget_cmds);
435 command_count += g_slist_length(g_cur_cmd_list.system_cmds);
436 command_count += g_slist_length(g_cur_cmd_list.exclusive_system_cmds);
437 command_count += g_slist_length(g_cur_cmd_list.system_background_cmds);
438 command_count += g_slist_length(g_cur_cmd_list.foreground_cmds);
439 command_count += g_slist_length(g_cur_cmd_list.background_cmds);
441 g_cur_cmd_list.total_cmd_count = command_count;
443 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Command count : %d ", g_cur_cmd_list.total_cmd_count);
444 return command_count;
447 int vcd_client_foreach_command(client_foreach_command_cb callback, void* user_data)
449 if (NULL == callback) {
450 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] input parameter is NULL");
451 return VCD_ERROR_INVALID_PARAMETER;
454 if (0 != vcd_client_command_collect_command()) {
455 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
456 return VCD_ERROR_OPERATION_FAILED;
459 if (0 >= vcd_client_get_length()) {
460 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] No current command");
461 return VCD_ERROR_OPERATION_FAILED;
468 if (0 < g_slist_length(g_cur_cmd_list.widget_cmds)) {
469 iter = g_slist_nth(g_cur_cmd_list.widget_cmds, 0);
470 while (NULL != iter) {
471 temp_cmd = iter->data;
473 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Widget : id(%d) type(%d) format(%d) command(%s) domain(%d)"
474 , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->domain);
476 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
478 iter = g_slist_next(iter);
481 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No widget commands");
484 if (0 < g_slist_length(g_cur_cmd_list.foreground_cmds)) {
485 iter = g_slist_nth(g_cur_cmd_list.foreground_cmds, 0);
486 while (NULL != iter) {
487 temp_cmd = iter->data;
489 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Fore : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d) fixed(%s)"
490 , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, temp_cmd->fixed);
492 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
494 iter = g_slist_next(iter);
497 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground commands");
500 if (0 < g_slist_length(g_cur_cmd_list.system_cmds)) {
501 iter = g_slist_nth(g_cur_cmd_list.system_cmds, 0);
502 while (NULL != iter) {
503 temp_cmd = iter->data;
505 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] System : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d)"
506 , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
508 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
510 iter = g_slist_next(iter);
513 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system commands");
516 if (0 < g_slist_length(g_cur_cmd_list.system_background_cmds)) {
517 iter = g_slist_nth(g_cur_cmd_list.system_background_cmds, 0);
518 while (NULL != iter) {
519 temp_cmd = iter->data;
521 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] System background : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d)"
522 , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
524 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
526 iter = g_slist_next(iter);
529 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system background commands");
532 if (0 < g_slist_length(g_cur_cmd_list.exclusive_system_cmds)) {
533 iter = g_slist_nth(g_cur_cmd_list.exclusive_system_cmds, 0);
534 while (NULL != iter) {
535 temp_cmd = iter->data;
537 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Exclusive system : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d)"
538 , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
540 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
542 iter = g_slist_next(iter);
545 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No exclusive system commands");
548 if (0 < g_slist_length(g_cur_cmd_list.background_cmds)) {
549 iter = g_slist_nth(g_cur_cmd_list.background_cmds, 0);
550 while (NULL != iter) {
551 temp_cmd = iter->data;
553 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)"
554 , 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);
556 callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
558 iter = g_slist_next(iter);
561 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No background commands");
566 static vc_cmd_s* __command_copy(vc_cmd_s* src_cmd)
568 if (NULL == src_cmd) {
569 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Input command is NULL");
573 vc_cmd_s* temp_cmd = NULL;
574 temp_cmd = (vc_cmd_s*)calloc(sizeof(vc_cmd_s), 1);
575 if (NULL == temp_cmd) {
576 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
580 temp_cmd->id = src_cmd->id;
581 temp_cmd->pid = src_cmd->pid;
582 temp_cmd->index = src_cmd->index;
583 temp_cmd->type = src_cmd->type;
584 temp_cmd->format = src_cmd->format;
585 temp_cmd->domain = src_cmd->domain;
587 if (VC_COMMAND_TYPE_SYSTEM == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM;
588 else if (VC_COMMAND_TYPE_EXCLUSIVE == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_EXCLUSIVE;
589 else if (VC_COMMAND_TYPE_WIDGET == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_WIDGET;
590 else if (VC_COMMAND_TYPE_FOREGROUND == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
591 else if (VC_COMMAND_TYPE_SYSTEM_BACKGROUND == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM_BACKGROUND;
592 else if (VC_COMMAND_TYPE_BACKGROUND == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_BACKGROUND;
594 if (NULL != src_cmd->command) {
595 temp_cmd->command = strdup(src_cmd->command);
598 if (NULL != src_cmd->parameter) {
599 temp_cmd->parameter = strdup(src_cmd->parameter);
602 if (NULL != src_cmd->appid) {
603 temp_cmd->appid = strdup(src_cmd->appid);
606 if (NULL != src_cmd->invocation_name) {
607 temp_cmd->invocation_name = strdup(src_cmd->invocation_name);
610 if (NULL != src_cmd->fixed) {
611 temp_cmd->fixed = strdup(src_cmd->fixed);
614 temp_cmd->key = src_cmd->key;
615 temp_cmd->modifier = src_cmd->modifier;
620 int vcd_client_get_cmd_from_result_id(int result_id, vc_cmd_s** result)
625 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Result id(%d)", result_id);
627 if (0 < g_slist_length(g_cur_cmd_list.widget_cmds)) {
628 iter = g_slist_nth(g_cur_cmd_list.widget_cmds, 0);
629 while (NULL != iter) {
630 temp_cmd = iter->data;
632 if (result_id == temp_cmd->id) {
633 /**pid = g_cur_cmd_list.foreground; */
634 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_UI_CONTROL; */
635 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
637 *result = __command_copy(temp_cmd);
642 iter = g_slist_next(iter);
645 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No widget commands");
648 if (0 < g_slist_length(g_cur_cmd_list.foreground_cmds)) {
649 iter = g_slist_nth(g_cur_cmd_list.foreground_cmds, 0);
651 while (NULL != iter) {
652 temp_cmd = iter->data;
654 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] command id (%d)", temp_cmd->id);
656 if (result_id == temp_cmd->id) {
657 /**pid = g_cur_cmd_list.foreground; */
658 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_FOREGROUND; */
659 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
661 *result = __command_copy(temp_cmd);
665 iter = g_slist_next(iter);
668 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground commands");
671 if (0 < g_slist_length(g_cur_cmd_list.system_cmds)) {
672 iter = g_slist_nth(g_cur_cmd_list.system_cmds, 0);
673 while (NULL != iter) {
674 temp_cmd = iter->data;
676 if (result_id == temp_cmd->id) {
677 /**pid = g_manager.pid; */
678 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM; */
679 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
681 *result = __command_copy(temp_cmd);
685 iter = g_slist_next(iter);
688 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system commands");
691 if (0 < g_slist_length(g_cur_cmd_list.system_background_cmds)) {
692 iter = g_slist_nth(g_cur_cmd_list.system_background_cmds, 0);
693 while (NULL != iter) {
694 temp_cmd = iter->data;
696 if (result_id == temp_cmd->id) {
697 /**pid = g_manager.pid; */
698 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM_BACKGROUND; */
699 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
701 *result = __command_copy(temp_cmd);
705 iter = g_slist_next(iter);
708 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system background commands");
711 if (0 < g_slist_length(g_cur_cmd_list.exclusive_system_cmds)) {
712 iter = g_slist_nth(g_cur_cmd_list.exclusive_system_cmds, 0);
713 while (NULL != iter) {
714 temp_cmd = iter->data;
716 if (result_id == temp_cmd->id) {
717 /**pid = g_manager.pid; */
718 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM_EXCLUSIVE; */
719 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
721 *result = __command_copy(temp_cmd);
725 iter = g_slist_next(iter);
728 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No exclusive system commands");
731 if (0 < g_slist_length(g_cur_cmd_list.background_cmds)) {
732 iter = g_slist_nth(g_cur_cmd_list.background_cmds, 0);
734 while (NULL != iter) {
735 temp_cmd = iter->data;
737 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] command id (%d)", temp_cmd->id);
739 if (result_id == temp_cmd->id) {
740 *result = __command_copy(temp_cmd);
744 iter = g_slist_next(iter);
747 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No background commands");
750 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Not find matched result");
754 int vcd_client_set_slience_detection(bool value)
756 g_silence_detection = value;
760 bool vcd_client_get_slience_detection()
762 return g_silence_detection;
765 int vcd_client_set_recognition_mode(vcd_recognition_mode_e mode)
767 g_recognition_mode = mode;
771 vcd_recognition_mode_e vcd_client_get_recognition_mode()
773 return g_recognition_mode;
776 int vcd_client_set_server_dialog(int pid, bool is_server_dialog)
778 vc_client_info_s* client_info = NULL;
780 client_info = __client_get_element(pid);
781 if (NULL == client_info) {
782 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
783 return VCD_ERROR_INVALID_PARAMETER;
786 client_info->server_dialog = is_server_dialog;
788 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Set server dialog, client pid(%d), server_dialog(%d)", pid, client_info->server_dialog);
792 int vcd_client_get_server_dialog(int pid, bool* is_server_dialog)
794 vc_client_info_s* client_info = NULL;
796 client_info = __client_get_element(pid);
797 if (NULL == client_info) {
798 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
799 return VCD_ERROR_INVALID_PARAMETER;
802 *is_server_dialog = client_info->server_dialog;
804 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Get server dialog, client pid(%d), server_dialog(%d)", pid, *is_server_dialog);
808 int vcd_client_append_cmd_from_type(int type, vc_cmd_list_h list)
813 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid list parameter");
814 return VCD_ERROR_INVALID_PARAMETER;
818 case VC_COMMAND_TYPE_SYSTEM:
819 item = g_cur_cmd_list.system_cmds;
821 case VC_COMMAND_TYPE_FOREGROUND:
822 item = g_cur_cmd_list.foreground_cmds;
824 case VC_COMMAND_TYPE_WIDGET:
825 item = g_cur_cmd_list.widget_cmds;
827 case VC_COMMAND_TYPE_SYSTEM_BACKGROUND:
828 item = g_cur_cmd_list.system_background_cmds;
830 case VC_COMMAND_TYPE_BACKGROUND:
831 item = g_cur_cmd_list.background_cmds;
833 case VC_COMMAND_TYPE_EXCLUSIVE:
834 item = g_cur_cmd_list.exclusive_system_cmds;
837 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid type parameter");
838 return VCD_ERROR_INVALID_PARAMETER;
841 while (NULL != item) {
842 vc_cmd_h temp_cmd = NULL;
843 vc_cmd_h target_cmd = (vc_cmd_h)item->data;
845 temp_cmd = (vc_cmd_h)__command_copy((vc_cmd_s*)target_cmd);
847 if (NULL == temp_cmd) {
848 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to copy the command");
849 return VCD_ERROR_OUT_OF_MEMORY;
852 if (0 != vc_cmd_list_add(list, temp_cmd)) {
853 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add the command in the list");
854 if (NULL != temp_cmd) {
855 vc_cmd_destroy(temp_cmd);
859 return VCD_ERROR_OPERATION_FAILED;
868 int __show_client_list()
871 vc_client_info_s *data = NULL;
873 SLOG(LOG_DEBUG, TAG_VCD, "@@@ client list");
875 int count = g_slist_length(g_client_list);
879 SLOG(LOG_DEBUG, TAG_VCD, "No Client");
881 iter = g_slist_nth(g_client_list, 0);
882 for (i = 0; i < count; i++) {
889 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
891 iter = g_slist_next(iter);
895 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
897 SLOG(LOG_DEBUG, TAG_VCD, "@@@ widget list");
899 widget_info_s *widget_data = NULL;
901 count = g_slist_length(g_widget_list);
904 SLOG(LOG_DEBUG, TAG_VCD, "No widget");
906 iter = g_slist_nth(g_widget_list, 0);
907 for (i = 0; i < count; i++) {
911 widget_data = iter->data;
913 if (NULL != widget_data) {
914 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
916 iter = g_slist_next(iter);
920 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
925 int __show_command_list(GSList* cmd_group)
928 vc_cmd_s *data = NULL;
930 SLOG(LOG_DEBUG, TAG_VCD, "@@@ command group");
932 int count = g_slist_length(cmd_group);
936 SLOG(LOG_DEBUG, TAG_VCD, "No command");
938 iter = g_slist_nth(cmd_group, 0);
939 for (i = 0; i < count; i++) {
945 if (NULL != data->parameter) {
946 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) parameter(%s) key(%d)", i, data->command, data->parameter, data->key);
948 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) key(%d)", i, data->command, data->key);
951 iter = g_slist_next(iter);
955 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
960 GSList* __client_get_item(const int pid)
963 vc_client_info_s *data = NULL;
965 int count = g_slist_length(g_client_list);
969 iter = g_slist_nth(g_client_list, 0);
970 for (i = 0; i < count; i++) {
976 if (pid == data->pid)
980 iter = g_slist_next(iter);
987 vc_client_info_s* __client_get_element(int pid)
990 vc_client_info_s *data = NULL;
992 int count = g_slist_length(g_client_list);
996 iter = g_slist_nth(g_client_list, 0);
997 for (i = 0; i < count; i++) {
1004 if (pid == data->pid)
1008 iter = g_slist_next(iter);
1015 int vcd_client_add(int pid)
1017 /*Check pid is duplicated*/
1019 tmp = __client_get_item(pid);
1022 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Client pid is already registered");
1023 return VCD_ERROR_INVALID_PARAMETER;
1026 vc_client_info_s *info = (vc_client_info_s*)calloc(1, sizeof(vc_client_info_s));
1028 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1029 return VCD_ERROR_OUT_OF_MEMORY;
1034 info->fg_cmd = false;
1035 info->bg_cmd = false;
1036 info->exclusive_cmd = false;
1037 info->server_dialog = false;
1039 /* Add item to global list */
1040 g_client_list = g_slist_append(g_client_list, info);
1042 if (NULL == g_client_list) {
1043 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new client");
1050 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data SUCCESS] Add new client");
1053 #ifdef CLIENT_DATA_DEBUG
1054 __show_client_list();
1059 int vcd_client_delete(int pid)
1062 vc_client_info_s* client_info = NULL;
1065 tmp = __client_get_item(pid);
1067 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1068 return VCD_ERROR_INVALID_PARAMETER;
1071 /*Free client structure*/
1072 client_info = tmp->data;
1073 if (NULL != client_info) {
1077 /*Remove handle from list*/
1078 g_client_list = g_slist_remove_link(g_client_list, tmp);
1081 #ifdef CLIENT_DATA_DEBUG
1082 __show_client_list();
1088 bool vcd_client_is_available(int pid)
1090 vc_client_info_s* client_info = NULL;
1092 client_info = __client_get_element(pid);
1093 if (NULL == client_info) {
1094 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1101 int vcd_client_get_ref_count()
1105 count = g_slist_length(g_client_list) + g_slist_length(g_widget_list);
1106 if (0 < g_manager.pid) {
1110 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] client count : %d", count);
1115 int vcd_client_get_list(int** pids, int* pid_count)
1117 if (NULL == pids || NULL == pid_count)
1120 int count = g_slist_length(g_client_list);
1126 tmp = (int*)calloc(count, sizeof(int));
1128 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1129 return VCD_ERROR_OUT_OF_MEMORY;
1132 GSList *iter = NULL;
1133 vc_client_info_s *data = NULL;
1136 iter = g_slist_nth(g_client_list, 0);
1138 while (NULL != iter) {
1145 iter = g_slist_next(iter);
1155 int vcd_client_set_command_type(int pid, int type)
1157 vc_client_info_s* client_info = NULL;
1159 client_info = __client_get_element(pid);
1160 if (NULL == client_info) {
1161 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1162 return VCD_ERROR_INVALID_PARAMETER;
1166 case VC_COMMAND_TYPE_FOREGROUND:
1167 client_info->fg_cmd = true;
1169 case VC_COMMAND_TYPE_BACKGROUND:
1170 client_info->bg_cmd = true;
1173 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1180 int vcd_client_unset_command_type(int pid, int type)
1182 vc_client_info_s* client_info = NULL;
1184 client_info = __client_get_element(pid);
1185 if (NULL == client_info) {
1186 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1187 return VCD_ERROR_INVALID_PARAMETER;
1191 case VC_COMMAND_TYPE_FOREGROUND:
1192 client_info->fg_cmd = false;
1194 case VC_COMMAND_TYPE_BACKGROUND:
1195 client_info->bg_cmd = false;
1198 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1205 int vcd_client_set_exclusive_command(int pid)
1207 vc_client_info_s* client_info = NULL;
1209 client_info = __client_get_element(pid);
1210 if (NULL == client_info) {
1211 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1212 return VCD_ERROR_INVALID_PARAMETER;
1215 client_info->exclusive_cmd = true;
1220 int vcd_client_unset_exclusive_command(int pid)
1222 vc_client_info_s* client_info = NULL;
1224 client_info = __client_get_element(pid);
1225 if (NULL == client_info) {
1226 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1227 return VCD_ERROR_INVALID_PARAMETER;
1230 client_info->exclusive_cmd = false;
1235 int vcd_client_save_client_info()
1237 if (0 != vc_info_parser_set_client_info(g_client_list)) {
1238 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to save client info");
1246 * Functions for widget
1248 GSList* __widget_get_item(int pid)
1250 GSList *iter = NULL;
1251 widget_info_s *data = NULL;
1253 int count = g_slist_length(g_widget_list);
1257 iter = g_slist_nth(g_widget_list, 0);
1258 for (i = 0; i < count; i++) {
1265 if (pid == data->pid)
1269 iter = g_slist_next(iter);
1276 widget_info_s* __widget_get_element(int pid)
1278 GSList *iter = NULL;
1279 widget_info_s *data = NULL;
1281 int count = g_slist_length(g_widget_list);
1285 iter = g_slist_nth(g_widget_list, 0);
1288 while (iter && i < count) {
1292 if (pid == data->pid)
1296 iter = g_slist_next(iter);
1305 int vcd_client_widget_get_list(int** pids, int* pid_count)
1307 if (NULL == pids || NULL == pid_count)
1310 int count = g_slist_length(g_widget_list);
1316 tmp = (int*)calloc(count, sizeof(int));
1318 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1319 return VCD_ERROR_OUT_OF_MEMORY;
1322 GSList *iter = NULL;
1323 widget_info_s *data = NULL;
1326 iter = g_slist_nth(g_widget_list, 0);
1327 while (NULL != iter) {
1334 iter = g_slist_next(iter);
1344 int vcd_client_widget_add(int pid)
1346 /*Check pid is duplicated*/
1348 tmp = __widget_get_item(pid);
1351 SLOG(LOG_WARN, TAG_VCD, "[Client Data] widget pid is already registered");
1352 return VCD_ERROR_INVALID_PARAMETER;
1355 widget_info_s *info = (widget_info_s*)calloc(1, sizeof(widget_info_s));
1357 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1358 return VCD_ERROR_OUT_OF_MEMORY;
1362 info->widget_cmd = false;
1364 /* Add item to global list */
1365 g_widget_list = g_slist_append(g_widget_list, info);
1367 if (NULL == g_widget_list) {
1368 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new widget");
1375 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data SUCCESS] Add new widget");
1378 #ifdef CLIENT_DATA_DEBUG
1379 __show_client_list();
1384 int vcd_client_widget_delete(int pid)
1387 widget_info_s* widget_info = NULL;
1390 tmp = __widget_get_item(pid);
1392 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1393 return VCD_ERROR_INVALID_PARAMETER;
1396 /*Free client structure*/
1397 widget_info = tmp->data;
1398 if (NULL != widget_info) {
1402 /*Remove handle from list*/
1403 g_widget_list = g_slist_remove_link(g_widget_list, tmp);
1406 #ifdef CLIENT_DATA_DEBUG
1407 __show_client_list();
1413 int vcd_client_widget_get_foreground_pid()
1415 /* 1. Get foreground pid */
1417 if (0 != vcd_config_get_foreground(&fg_pid)) {
1418 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get foreground pid");
1419 /* There is no foreground app for voice control */
1422 widget_info_s* widget = NULL;
1424 widget = __widget_get_element(fg_pid);
1425 if (NULL == widget) {
1426 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Not found foreground pid of widget");
1434 bool vcd_client_widget_is_available(int pid)
1436 widget_info_s* widget_info = NULL;
1438 widget_info = __widget_get_element(pid);
1439 if (NULL == widget_info) {
1440 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1447 int vcd_client_widget_set_command(int pid)
1449 widget_info_s *info = NULL;
1450 info = __widget_get_element(pid);
1452 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1453 return VCD_ERROR_INVALID_PARAMETER;
1456 info->widget_cmd = true;
1461 int vcd_client_widget_unset_command(int pid)
1463 widget_info_s *info = NULL;
1464 info = __widget_get_element(pid);
1466 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1467 return VCD_ERROR_INVALID_PARAMETER;
1470 info->widget_cmd = false;
1475 int vcd_client_widget_set_asr_result_enabled(int pid, bool enable)
1477 widget_info_s *info = NULL;
1478 info = __widget_get_element(pid);
1480 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1481 return VCD_ERROR_INVALID_PARAMETER;
1484 info->asr_result_enabled = enable;
1489 int vcd_client_widget_get_asr_result_enabled(int pid, bool* enable)
1491 widget_info_s *info = NULL;
1492 info = __widget_get_element(pid);
1494 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1495 return VCD_ERROR_INVALID_PARAMETER;
1498 *enable = info->asr_result_enabled;
1503 int vcd_client_widget_set_waiting_for_recording(int pid, bool waiting)
1506 if (TRUE == waiting && pid != vcd_client_widget_get_foreground_pid()) {
1507 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT foreground pid", pid);
1511 g_is_waiting_recording = waiting;
1512 g_waiting_recording_pid = pid;
1513 SLOG(LOG_ERROR, TAG_VCD, "[INFO] Success to set waiting for recording, pid(%d), waiting(%d)", pid, waiting);
1517 int vcd_client_widget_get_waiting_for_recording(int pid, bool* waiting)
1519 if (pid != g_waiting_recording_pid) {
1520 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT waiting pid", pid);
1524 *waiting = g_is_waiting_recording;
1525 SLOG(LOG_ERROR, TAG_VCD, "[INFO] Success to get waiting for recording, waiting(%d)", *waiting);
1529 void vcd_client_update_foreground_pid()
1531 int tmp_pid = VC_RUNTIME_INFO_NO_FOREGROUND;
1532 vcd_config_get_foreground(&tmp_pid);
1534 int pid = VC_RUNTIME_INFO_NO_FOREGROUND;
1535 int ret = aul_window_get_focused_pid(&pid);
1537 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get focused pid, ret(%d)", ret);
1539 SLOG(LOG_INFO, TAG_VCD, "[INFO] Foreground pid (%d), Focused pid (%d)", tmp_pid, pid);
1541 GSList *iter = NULL;
1542 vc_client_info_s *data = NULL;
1544 int count = g_slist_length(g_client_list);
1548 SLOG(LOG_DEBUG, TAG_VCD, "No Client");
1550 iter = g_slist_nth(g_client_list, 0);
1551 for (i = 0; i < count; i++) {
1558 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
1559 if (pid == data->pid) {
1560 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid (%d)", data->pid);
1561 vcd_config_set_foreground(data->pid, true);
1562 if (tmp_pid != data->pid) {
1563 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid is different");
1568 iter = g_slist_next(iter);
1572 widget_info_s *widget_data = NULL;
1573 count = g_slist_length(g_widget_list);
1576 SLOG(LOG_DEBUG, TAG_VCD, "No widget");
1578 iter = g_slist_nth(g_widget_list, 0);
1579 for (i = 0; i < count; i++) {
1583 widget_data = iter->data;
1585 if (NULL != widget_data) {
1586 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
1587 if (pid == widget_data->pid) {
1588 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid (%d)", widget_data->pid);
1589 vcd_config_set_foreground(widget_data->pid, true);
1590 if (tmp_pid != widget_data->pid) {
1591 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid is changed");
1596 iter = g_slist_next(iter);
1600 SLOG(LOG_INFO, TAG_VCD, "[INFO] No foreground");
1601 vcd_config_set_foreground(VC_RUNTIME_INFO_NO_FOREGROUND, true);