2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
19 #include <Elementary.h>
21 #include <voice_control_command.h>
22 #include <voice_control_common.h>
23 #include <voice_control_manager.h>
25 #include "voice_control_panel_action.h"
26 #include "voice_control_panel_command.h"
27 #include "voice_control_panel_main.h"
28 #include "voice_control_panel_vc.h"
29 #include "voice_control_panel_view.h"
31 static vc_cmd_list_h g_cmd_list_1st;
32 static vc_cmd_list_h g_cmd_list_2nd[NUM_COMMAND_1ST];
35 static Ecore_Timer *g_timeout_timer = NULL;
36 static Ecore_Timer *g_restart_timer = NULL;
38 static bool __current_command_cb(vc_cmd_h vc_command, void* user_data)
40 appdata *ad = (appdata *)user_data;
43 if (0 != vc_cmd_get_type(vc_command, &type)) {
44 LOGE("[ERROR] Fail to get type");
48 if (VC_COMMAND_TYPE_FOREGROUND == type || VC_COMMAND_TYPE_BACKGROUND == type) {
50 if (0 != vc_cmd_get_command(vc_command, &cmd)) {
51 LOGE("[ERROR] Fail to get command");
56 ad->cmd_list = g_list_append(ad->cmd_list, strdup(cmd));
57 LOGD("Add command List (%s)", cmd);
66 static int __vc_panel_vc_get_current_commands(void *data)
68 appdata *ad = (appdata *)data;
70 LOGD("==== Get current commands ====");
71 if (0 < g_list_length(ad->cmd_list)) {
73 iter = g_list_first(ad->cmd_list);
75 while (NULL != iter) {
76 char *cmd = iter->data;
82 ad->cmd_list = g_list_remove_link(ad->cmd_list, iter);
83 iter = g_list_first(ad->cmd_list);
87 vc_cmd_list_h vc_cmd_list;
88 if (0 != vc_mgr_get_current_commands(&vc_cmd_list)) {
89 LOGE("[ERROR] Fail to get current commands");
90 if (0 != vc_cmd_list_destroy(vc_cmd_list, true)) {
91 LOGE("[WARNING] Fail to cmd list destroy");
96 if (0 != vc_cmd_list_foreach_commands(vc_cmd_list, __current_command_cb, ad)) {
97 LOGE("[ERROR] Fail to get current commands");
98 if (0 != vc_cmd_list_destroy(vc_cmd_list, true)) {
99 LOGE("[WARNING] Fail to cmd list destroy");
104 if (0 != vc_cmd_list_destroy(vc_cmd_list, true)) {
105 LOGE("[WARNING] Fail to cmd list destroy");
111 static void __vc_panel_vc_destroy_command_list()
113 LOGD( "==== Destroy Command List ====");
115 if (0 != vc_cmd_list_destroy(g_cmd_list_1st, true)) {
116 LOGE("[WARNING] Fail to destroy list");
120 for (i = 0; i < NUM_COMMAND_1ST; i++) {
121 if (0 != vc_cmd_list_destroy(g_cmd_list_2nd[i], true)) {
122 LOGE("[WARNING] Fail to destroy list");
130 static int __vc_panel_vc_create_command_list()
132 LOGD( "==== Create Command List ====");
135 if (0 != vc_cmd_list_create(&g_cmd_list_1st)) {
136 LOGE( "[ERROR] Fail to cmd list create");
141 for (i = 0; i < NUM_COMMAND_1ST; i++) {
143 if (0 != vc_cmd_create(&cmd)) {
144 LOGE( "[ERROR] Fail to cmd create");
147 if (0 != vc_cmd_set_command(cmd, _(g_command_1st[i]))) {
148 LOGE( "[ERROR] Fail to set command");
152 if (0 != vc_cmd_set_type(cmd, VC_COMMAND_TYPE_SYSTEM)) {
153 LOGE( "[ERROR] Fail to set type");
157 if (0 != vc_cmd_set_format(cmd, VC_CMD_FORMAT_FIXED)) {
158 LOGE( "[ERROR] Fail to set format");
162 if (0 != vc_cmd_list_add(g_cmd_list_1st, cmd)) {
163 LOGE( "[ERROR] Fail to list add");
170 for (i = 0; i < NUM_COMMAND_1ST; i++) {
171 if (0 != vc_cmd_list_create(&g_cmd_list_2nd[i])) {
172 LOGE( "[ERROR] Fail to list create");
177 for (j = 0; j < NUM_COMMAND_2ND; j++) {
178 if (NULL != g_command_2nd[i][j]) {
180 if (0 != vc_cmd_create(&cmd)) {
181 LOGE( "[ERROR] Fail to cmd create");
184 if (0 != vc_cmd_set_command(cmd, _(g_command_2nd[i][j]))) {
185 LOGE( "[ERROR] Fail to set command");
189 if (0 != vc_cmd_set_type(cmd, VC_COMMAND_TYPE_SYSTEM)) {
190 LOGE( "[ERROR] Fail to set type");
194 if (0 != vc_cmd_set_format(cmd, VC_CMD_FORMAT_FIXED)) {
195 LOGE( "[ERROR] Fail to set format");
199 if (0 != vc_cmd_list_add(g_cmd_list_2nd[i], cmd)) {
200 LOGE( "[ERROR] Fail to list add");
211 static Eina_Bool __start_cb(void *data)
213 appdata *ad = (appdata *)data;
215 vc_service_state_e state;
216 if (0 != vc_mgr_get_service_state(&state)) {
217 LOGE("[ERROR] Fail to get service state");
221 if (VC_SERVICE_STATE_READY != state) {
222 LOGD("[WARNING] Wait for service state ready");
226 if (0 != vc_mgr_set_recognition_mode(VC_RECOGNITION_MODE_RESTART_AFTER_REJECT)) {
227 LOGE( "[ERROR] Fail to set recognition mode");
229 if (0 != vc_mgr_set_command_list(g_cmd_list_1st)) {
230 LOGE( "[ERROR] Fail to set command list");
233 if (0 != __vc_panel_vc_get_current_commands(ad)) {
234 LOGE("[ERROR] Fail to get current commands");
237 if (0 != vc_mgr_start(false)) {
238 LOGE( "[ERROR] Fail to start");
244 static void __vc_panel_vc_state_changed_cb(vc_state_e previous, vc_state_e current, void *user_data)
246 LOGD( "==== State is changed ====");
247 LOGD( "Previous(%d) -> Current(%d)", previous, current);
250 static void __vc_panel_vc_reset(void *data)
252 appdata *ad = (appdata *)data;
254 ad->current_depth = 1;
255 ad->current_path[0] = -1;
256 ad->current_path[1] = -1;
258 if (0 != vc_mgr_set_command_list(g_cmd_list_1st)) {
259 LOGE( "[ERROR] Fail to set command list");
263 static Eina_Bool __vc_panel_finalize(void *data)
265 LOGD("=== Finalize ===");
266 if (g_timeout_timer != NULL) {
267 ecore_timer_del(g_timeout_timer);
268 g_timeout_timer = NULL;
274 static Eina_Bool __vc_panel_vc_restart(void *data)
276 LOGD( "==== Restart ====");
278 appdata *ad = (appdata *)data;
280 vc_service_state_e state;
281 if (0 != vc_mgr_get_service_state(&state)) {
282 LOGE("[ERROR] Fail to get service state");
286 if (VC_SERVICE_STATE_READY != state) {
287 LOGD("[WARNING] Wait for service state ready");
291 /* set current command list */
292 if (1 == ad->current_depth) {
293 if (0 != vc_mgr_set_command_list(g_cmd_list_1st)) {
294 LOGE( "[ERROR] Fail to set command list");
296 } else if (2 == ad->current_depth) {
297 if (0 != vc_mgr_set_command_list(g_cmd_list_2nd[ad->current_path[0]])) {
298 LOGE( "[ERROR] Fail to set command list");
302 vc_panel_view_show(ad);
304 if (0 != __vc_panel_vc_get_current_commands(ad)) {
305 LOGE("[ERROR] Fail to get current commands");
308 if (0 != vc_mgr_start(false)) {
309 LOGE( "[ERROR] Fail to start");
318 static void __vc_panel_vc_service_state_changed_cb(vc_service_state_e previous, vc_service_state_e current, void *user_data)
320 LOGD( "==== Service state is changed ====");
321 LOGD( "Previous(%d) -> Current(%d)", previous, current);
323 appdata *ad = (appdata *)user_data;
325 if (VC_SERVICE_STATE_READY == previous && VC_SERVICE_STATE_RECORDING == current) {
326 LOGD( "==== Show by recording ====");
327 vc_panel_view_show(ad);
328 if (NULL != g_timeout_timer) {
329 ecore_timer_reset(g_timeout_timer);
331 } else if ((VC_SERVICE_STATE_RECORDING == previous || VC_SERVICE_STATE_PROCESSING == previous) && VC_SERVICE_STATE_READY == current) {
332 if (APP_STATE_SERVICE == ad->app_state) {
333 LOGD("==== Restart ====");
334 if (NULL != g_restart_timer) {
335 ecore_timer_del(g_restart_timer);
337 g_restart_timer = ecore_timer_add(0.5, __vc_panel_vc_restart, user_data);
339 LOGD("==== Hide ====");
340 __vc_panel_vc_reset(ad);
344 if (VC_SERVICE_STATE_RECORDING == current) {
345 if (NULL != g_timeout_timer) {
346 ecore_timer_reset(g_timeout_timer);
351 static Eina_Bool __hide_result(void *data)
354 vc_panel_view_show(data);
359 static bool __vc_panel_vc_all_result_cb(vc_result_event_e event, vc_cmd_list_h vc_cmd_list, const char* result, const char* msg, void *user_data)
361 LOGD( "==== All result cb ====");
363 if (VC_RESULT_EVENT_REJECTED == event) {
364 LOGD("Rejected event");
365 vc_panel_view_show_result(user_data, _("IDS_SORRY"));
366 ecore_timer_add(0.5, __hide_result, user_data);
368 if (NULL != result) {
369 LOGD( "Result Text - %s", result);
370 vc_panel_view_show_result(user_data, result);
372 if (strcmp(result, _("IDS_HELP"))) {
373 vc_panel_view_hide_help(user_data);
381 static void __vc_panel_vc_result_cb(vc_result_event_e event, vc_cmd_list_h vc_cmd_list, const char* result, void *user_data)
383 LOGD( "==== Result cb ====");
385 if (NULL != result) {
386 LOGD( "Result Text - %s", result);
389 ret = vc_panel_action(result, user_data);
391 ecore_idler_add(__vc_panel_finalize, user_data);
396 static void __vc_panel_vc_speech_detected_cb(void *user_data)
398 LOGD("==== Speech detected ====");
399 if (NULL != g_timeout_timer)
400 ecore_timer_reset(g_timeout_timer);
403 static void __vc_panel_vc_language_changed_cb(const char* previous, const char* current, void* user_data)
405 LOGD("Language is changed (%s) to (%s)", previous, current);
407 char loc[64] = {'\0', };
408 snprintf(loc, 64, "%s.UTF-8", current);
410 setlocale(LC_ALL, loc);
412 __vc_panel_vc_destroy_command_list();
413 if (0 != __vc_panel_vc_create_command_list()) {
414 LOGE("[ERROR] Fail to create command list");
418 int vc_panel_vc_start(void *data)
420 if (NULL != g_timeout_timer)
421 ecore_timer_reset(g_timeout_timer);
423 ecore_timer_add(0, __start_cb, data);
427 int vc_panel_vc_cancel(void *data)
429 vc_service_state_e service_state;
431 if (0 != vc_mgr_get_service_state(&service_state)) {
432 LOGE( "[ERROR] Fail to get service state");
436 if ((VC_SERVICE_STATE_RECORDING == service_state) || (VC_SERVICE_STATE_PROCESSING == service_state)) {
437 LOGD( "==== service state (%d)", service_state);
439 if (0 != vc_mgr_cancel()) {
440 LOGE( "[ERROR] Fail to cancel");
448 int vc_panel_vc_init(void *data)
450 LOGD( "==== Initialze Voice control ====");
452 if (0 != vc_mgr_initialize()) {
453 LOGE( "[ERROR] Fail to initialize");
457 if (0 != vc_mgr_set_state_changed_cb(__vc_panel_vc_state_changed_cb, data)) {
458 LOGE( "[ERROR] Fail to set state changed cb");
462 if (0 != vc_mgr_set_service_state_changed_cb(__vc_panel_vc_service_state_changed_cb, data)) {
463 LOGE( "[ERROR] Fail to set service state changed cb");
467 if (0 != vc_mgr_set_all_result_cb(__vc_panel_vc_all_result_cb, data)) {
468 LOGE( "[ERROR] Fail to set all result cb");
472 if (0 != vc_mgr_set_result_cb(__vc_panel_vc_result_cb, data)) {
473 LOGE( "[ERROR] Fail to set result cb");
477 if (0 != vc_mgr_set_speech_detected_cb(__vc_panel_vc_speech_detected_cb, data)) {
478 LOGE("[ERROR] Fail to set speech detected cb");
482 if (0 != vc_mgr_set_current_language_changed_cb(__vc_panel_vc_language_changed_cb, data)) {
483 LOGE("[ERROR] Fail to set language changed cb");
487 if (0 != vc_mgr_prepare()) {
488 LOGE( "[ERROR] Fail to prepare");
492 if (0 != __vc_panel_vc_create_command_list()) {
493 LOGE( "[ERROR] Fail to create command list");
497 if (NULL == g_timeout_timer) {
498 g_timeout_timer = ecore_timer_add(TIMEOUT, __vc_panel_finalize, NULL);
507 int vc_panel_vc_deinit(void *data)
509 LOGD( "==== De-initialize Voice control ====");
511 appdata *ad = (appdata *)data;
513 if (g_restart_timer != NULL) {
514 ecore_timer_del(g_restart_timer);
515 g_restart_timer = NULL;
518 if (g_timeout_timer != NULL) {
519 ecore_timer_del(g_timeout_timer);
520 g_timeout_timer = NULL;
523 if (0 < g_list_length(ad->cmd_list)) {
525 iter = g_list_first(ad->cmd_list);
527 while (NULL != iter) {
528 char *cmd = iter->data;
534 ad->cmd_list = g_list_remove_link(ad->cmd_list, iter);
535 iter = g_list_first(ad->cmd_list);
539 __vc_panel_vc_destroy_command_list();
541 if (0 != vc_mgr_deinitialize()) {
542 LOGE( "[ERROR] Fail to deinitialize");
553 vi:ts=4:ai:nowrap:expandtab