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.
18 #include "vc_client.h"
24 int uid; /*<< unique id = pid + handle */
25 int xid; /*<< main X window id */
27 vc_result_cb result_cb;
28 void* result_user_data;
30 void* error_user_data;
31 vc_service_state_changed_cb service_state_changed_cb;
32 void* service_state_changed_user_data;
33 vc_state_changed_cb state_changed_cb;
34 void* state_changed_user_data;
35 vc_current_language_changed_cb current_lang_changed_cb;
36 void* current_lang_changed_user_data;
39 vc_tts_streaming_cb tts_streaming_cb;
40 void* tts_streaming_user_data;
41 vc_tts_utterance_status_cb tts_utterance_status_cb;
42 void* tts_utterance_status_user_data;
45 /* exclusive option */
50 vc_service_state_e service_state;
53 vc_state_e before_state;
54 vc_state_e current_state;
63 vc_auth_state_e auth_before_state;
64 vc_auth_state_e auth_current_state;
65 vc_auth_state_changed_cb auth_state_changed_cb;
66 void* auth_state_changed_user_data;
74 char* invocation_name;
78 static GSList *g_client_list = NULL;
81 static vc_client_s* __client_get(vc_h vc)
87 vc_client_s *data = NULL;
89 int count = g_slist_length(g_client_list);
92 for (i = 0; i < count; i++) {
93 data = g_slist_nth_data(g_client_list, i);
96 if (vc->handle == data->vc->handle) {
102 SLOG(LOG_DEBUG, TAG_VCC, "[DEBUG] Fail to get client by vc"); //LCOV_EXCL_LINE
107 int vc_client_create(vc_h* vc)
109 vc_client_s *client = NULL;
111 client = (vc_client_s*)calloc(1, sizeof(vc_client_s));
112 if (NULL == client) {
113 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to allocate memory"); //LCOV_EXCL_LINE
114 return VC_ERROR_OUT_OF_MEMORY;
117 vc_h temp = (vc_h)calloc(1, sizeof(struct vc_s));
119 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to allocate memory"); //LCOV_EXCL_LINE
121 return VC_ERROR_OUT_OF_MEMORY;
124 temp->handle = getpid();
126 /* initialize client data */
128 client->pid = getpid();
129 client->uid = temp->handle;
132 client->result_cb = NULL;
133 client->result_user_data = NULL;
134 client->service_state_changed_cb = NULL;
135 client->service_state_changed_user_data = NULL;
136 client->state_changed_cb = NULL;
137 client->state_changed_user_data = NULL;
138 client->current_lang_changed_cb = NULL;
139 client->current_lang_changed_user_data = NULL;
140 client->error_cb = NULL;
141 client->error_user_data = NULL;
142 client->tts_streaming_cb = NULL;
143 client->tts_streaming_user_data = NULL;
144 client->tts_utterance_status_cb = NULL;
145 client->tts_utterance_status_user_data = NULL;
148 client->exclusive_cmd = false;
151 client->service_state = VC_RUNTIME_INFO_NO_FOREGROUND;
153 client->before_state = VC_STATE_INITIALIZED;
154 client->current_state = VC_STATE_INITIALIZED;
156 client->cb_ref_count = 0;
159 client->auth_before_state = VC_AUTH_STATE_NONE;
160 client->auth_current_state = VC_AUTH_STATE_NONE;
161 client->auth_state_changed_cb = NULL;
162 client->auth_state_changed_user_data = NULL;
164 client->is_foreground = false;
165 client->invocation_name = NULL;
167 g_client_list = g_slist_append(g_client_list, client);
174 int vc_client_destroy(vc_h vc)
177 SLOG(LOG_ERROR, TAG_VCC, "Input parameter is NULL"); //LCOV_EXCL_LINE
181 vc_client_s *data = NULL;
183 int count = g_slist_length(g_client_list);
186 for (i = 0; i < count; i++) {
187 data = g_slist_nth_data(g_client_list, i);
190 if (vc->handle == data->vc->handle) {
191 g_client_list = g_slist_remove(g_client_list, data);
193 while (0 != data->cb_ref_count) {
194 /* wait for release callback function */
196 if (NULL != data->invocation_name) {
197 free(data->invocation_name);
198 data->invocation_name = NULL;
209 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] client Not found");
214 bool vc_client_is_valid(vc_h vc)
216 vc_client_s* client = __client_get(vc);
219 if (NULL == client) {
220 SLOG(LOG_DEBUG, TAG_VCC, "[DEBUG] vc is not valid");
227 bool vc_client_is_valid_by_uid(int uid)
229 vc_client_s *data = NULL;
231 int count = g_slist_length(g_client_list);
234 for (i = 0; i < count; i++) {
235 data = g_slist_nth_data(g_client_list, i);
238 if (uid == data->vc->handle)
243 SLOG(LOG_DEBUG, TAG_VCC, "[DEBUG] Fail to get client by vc");
249 int vc_client_get_handle(int uid, vc_h* vc)
251 vc_client_s *data = NULL;
253 int count = g_slist_length(g_client_list);
256 for (i = 0; i < count; i++) {
257 data = g_slist_nth_data(g_client_list, i);
260 if (uid == data->vc->handle) {
271 /* set/get callback function */
272 int vc_client_set_result_cb(vc_h vc, vc_result_cb callback, void* user_data)
274 vc_client_s* client = __client_get(vc);
278 return VC_ERROR_INVALID_PARAMETER;
280 client->result_cb = callback;
281 client->result_user_data = user_data;
287 int vc_client_get_result_cb(vc_h vc, vc_result_cb* callback, void** user_data)
289 vc_client_s* client = __client_get(vc);
293 return VC_ERROR_INVALID_PARAMETER;
295 *callback = client->result_cb;
296 *user_data = client->result_user_data;
302 int vc_client_set_service_state_changed_cb(vc_h vc, vc_service_state_changed_cb callback, void* user_data)
304 vc_client_s* client = __client_get(vc);
308 return VC_ERROR_INVALID_PARAMETER;
310 client->service_state_changed_cb = callback;
311 client->service_state_changed_user_data = user_data;
317 int vc_client_get_service_state_changed_cb(vc_h vc, vc_service_state_changed_cb* callback, void** user_data)
319 vc_client_s* client = __client_get(vc);
323 return VC_ERROR_INVALID_PARAMETER;
325 *callback = client->service_state_changed_cb;
326 *user_data = client->service_state_changed_user_data;
332 int vc_client_set_state_changed_cb(vc_h vc, vc_state_changed_cb callback, void* user_data)
334 vc_client_s* client = __client_get(vc);
338 return VC_ERROR_INVALID_PARAMETER;
340 client->state_changed_cb = callback;
341 client->state_changed_user_data = user_data;
347 int vc_client_get_state_changed_cb(vc_h vc, vc_state_changed_cb* callback, void** user_data)
349 vc_client_s* client = __client_get(vc);
353 return VC_ERROR_INVALID_PARAMETER;
355 *callback = client->state_changed_cb;
356 *user_data = client->state_changed_user_data;
362 int vc_client_set_current_lang_changed_cb(vc_h vc, vc_current_language_changed_cb callback, void* user_data)
364 vc_client_s* client = __client_get(vc);
368 return VC_ERROR_INVALID_PARAMETER;
370 client->current_lang_changed_cb = callback;
371 client->current_lang_changed_user_data = user_data;
377 int vc_client_get_current_lang_changed_cb(vc_h vc, vc_current_language_changed_cb* callback, void** user_data)
379 vc_client_s* client = __client_get(vc);
383 return VC_ERROR_INVALID_PARAMETER;
385 *callback = client->current_lang_changed_cb;
386 *user_data = client->current_lang_changed_user_data;
392 int vc_client_set_error_cb(vc_h vc, vc_error_cb callback, void* user_data)
394 vc_client_s* client = __client_get(vc);
398 return VC_ERROR_INVALID_PARAMETER;
400 client->error_cb = callback;
401 client->error_user_data = user_data;
407 int vc_client_get_error_cb(vc_h vc, vc_error_cb* callback, void** user_data)
409 vc_client_s* client = __client_get(vc);
413 return VC_ERROR_INVALID_PARAMETER;
415 *callback = client->error_cb;
416 *user_data = client->error_user_data;
423 int vc_client_set_service_state(vc_h vc, vc_service_state_e state)
425 vc_client_s* client = __client_get(vc);
429 return VC_ERROR_INVALID_PARAMETER;
431 client->service_state = state;
436 int vc_client_get_service_state(vc_h vc, vc_service_state_e* state)
438 vc_client_s* client = __client_get(vc);
442 return VC_ERROR_INVALID_PARAMETER;
444 *state = client->service_state;
449 int vc_client_set_client_state(vc_h vc, vc_state_e state)
451 vc_client_s* client = __client_get(vc);
455 return VC_ERROR_INVALID_PARAMETER;
457 client->before_state = client->current_state;
458 client->current_state = state;
463 int vc_client_get_client_state(vc_h vc, vc_state_e* state)
465 vc_client_s* client = __client_get(vc);
469 return VC_ERROR_INVALID_PARAMETER;
471 *state = client->current_state;
476 int vc_client_get_client_state_by_uid(int uid, vc_state_e* state)
478 vc_client_s *data = NULL;
480 int count = g_slist_length(g_client_list);
483 for (i = 0; i < count; i++) {
484 data = g_slist_nth_data(g_client_list, i);
487 if (uid == data->vc->handle) {
488 *state = data->current_state;
497 int vc_client_get_before_state(vc_h vc, vc_state_e* state, vc_state_e* before_state)
499 vc_client_s* client = __client_get(vc);
503 return VC_ERROR_INVALID_PARAMETER;
505 *before_state = client->before_state;
506 *state = client->current_state;
511 int vc_client_set_invocation_name(vc_h vc, const char* invocation_name)
513 vc_client_s* client = __client_get(vc);
517 return VC_ERROR_INVALID_PARAMETER;
519 if (NULL != client->invocation_name) {
520 free(client->invocation_name);
521 client->invocation_name = NULL;
524 if (NULL != invocation_name) {
525 client->invocation_name = strdup(invocation_name);
530 int vc_client_get_invocation_name(vc_h vc, char** invocation_name)
532 vc_client_s* client = __client_get(vc);
536 return VC_ERROR_INVALID_PARAMETER;
538 if (NULL != client->invocation_name)
539 *invocation_name = strdup(client->invocation_name);
544 int vc_client_set_xid(vc_h vc, int xid)
546 vc_client_s* client = __client_get(vc);
550 return VC_ERROR_INVALID_PARAMETER;
557 int vc_client_get_xid(vc_h vc, int* xid)
559 vc_client_s* client = __client_get(vc);
563 return VC_ERROR_INVALID_PARAMETER;
571 int vc_client_set_is_foreground(vc_h vc, bool value)
573 vc_client_s* client = __client_get(vc);
577 return VC_ERROR_INVALID_PARAMETER;
579 client->is_foreground = value;
583 int vc_client_get_is_foreground(vc_h vc, bool* value)
585 vc_client_s* client = __client_get(vc);
589 return VC_ERROR_INVALID_PARAMETER;
591 *value = client->is_foreground;
598 int vc_client_set_exclusive_cmd(vc_h vc, bool value)
600 vc_client_s* client = __client_get(vc);
604 return VC_ERROR_INVALID_PARAMETER;
606 client->exclusive_cmd = value;
611 int vc_client_get_exclusive_cmd(vc_h vc, bool* value)
613 vc_client_s* client = __client_get(vc);
617 return VC_ERROR_INVALID_PARAMETER;
619 *value = client->exclusive_cmd;
626 int vc_client_set_error(vc_h vc, int reason)
628 vc_client_s* client = __client_get(vc);
632 return VC_ERROR_INVALID_PARAMETER;
634 client->reason = reason;
639 int vc_client_get_error(vc_h vc, int* reason)
641 vc_client_s* client = __client_get(vc);
645 return VC_ERROR_INVALID_PARAMETER;
647 *reason = client->reason;
654 int vc_client_get_count()
656 return g_slist_length(g_client_list);
659 int vc_client_use_callback(vc_h vc)
661 vc_client_s* client = __client_get(vc);
665 return VC_ERROR_INVALID_PARAMETER;
667 client->cb_ref_count++;
671 int vc_client_not_use_callback(vc_h vc)
673 vc_client_s* client = __client_get(vc);
677 return VC_ERROR_INVALID_PARAMETER;
679 client->cb_ref_count--;
685 int vc_client_set_auth_state_changed_cb(vc_h vc, vc_auth_state_changed_cb callback, void* user_data)
687 vc_client_s* client = __client_get(vc);
691 return VC_ERROR_INVALID_PARAMETER;
693 client->auth_state_changed_cb = callback;
694 client->auth_state_changed_user_data = user_data;
699 int vc_client_get_auth_state_changed_cb(vc_h vc, vc_auth_state_changed_cb* callback, void** user_data)
701 vc_client_s* client = __client_get(vc);
705 return VC_ERROR_INVALID_PARAMETER;
707 *callback = client->auth_state_changed_cb;
708 *user_data = client->auth_state_changed_user_data;
713 int vc_client_unset_auth_state_changed_cb(vc_h vc)
715 vc_client_s* client = __client_get(vc);
719 return VC_ERROR_INVALID_PARAMETER;
721 client->auth_state_changed_cb = NULL;
722 client->auth_state_changed_user_data = NULL;
727 int vc_client_set_auth_state(vc_h vc, vc_auth_state_e state)
729 vc_client_s* client = __client_get(vc);
733 return VC_ERROR_INVALID_PARAMETER;
735 client->auth_before_state = client->auth_current_state;
736 client->auth_current_state = state;
741 int vc_client_get_auth_state(vc_h vc, vc_auth_state_e* state)
743 vc_client_s* client = __client_get(vc);
747 return VC_ERROR_INVALID_PARAMETER;
749 *state = client->auth_current_state;
754 int vc_client_get_before_auth_state(vc_h vc, vc_auth_state_e* before, vc_auth_state_e* current)
756 vc_client_s* client = __client_get(vc);
760 return VC_ERROR_INVALID_PARAMETER;
762 *before = client->auth_before_state;
763 *current = client->auth_current_state;
769 int vc_client_set_mgr_pid(vc_h vc, int mgr_pid)
771 vc_client_s* client = __client_get(vc);
775 return VC_ERROR_INVALID_PARAMETER;
777 client->mgr_pid = mgr_pid;
782 int vc_client_get_mgr_pid(vc_h vc, int* mgr_pid)
784 vc_client_s* client = __client_get(vc);
788 return VC_ERROR_INVALID_PARAMETER;
790 *mgr_pid = client->mgr_pid;
795 int vc_client_set_tts_streaming_cb(vc_h vc, vc_tts_streaming_cb callback, void* user_data)
797 vc_client_s* client = __client_get(vc);
801 return VC_ERROR_INVALID_PARAMETER;
803 client->tts_streaming_cb = callback;
804 client->tts_streaming_user_data = user_data;
809 int vc_client_get_tts_streaming_cb(vc_h vc, vc_tts_streaming_cb* callback, void** user_data)
811 vc_client_s* client = __client_get(vc);
815 return VC_ERROR_INVALID_PARAMETER;
817 *callback = client->tts_streaming_cb;
818 *user_data = client->tts_streaming_user_data;
823 int vc_client_set_tts_utterance_status_cb(vc_h vc, vc_tts_utterance_status_cb callback, void* user_data)
825 vc_client_s* client = __client_get(vc);
829 return VC_ERROR_INVALID_PARAMETER;
831 client->tts_utterance_status_cb = callback;
832 client->tts_utterance_status_user_data = user_data;
837 int vc_client_get_tts_utterance_status_cb(vc_h vc, vc_tts_utterance_status_cb* callback, void** user_data)
839 vc_client_s* client = __client_get(vc);
843 return VC_ERROR_INVALID_PARAMETER;
845 *callback = client->tts_utterance_status_cb;
846 *user_data = client->tts_utterance_status_user_data;