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.
19 #include "vc_widget_client.h"
20 #include "voice_control_command.h"
21 #include "voice_control_common.h"
23 /* Max number of handle */
24 static const unsigned int MAX_NUMBER_OF_HANDLE = 999;
25 /* allocated handle */
26 static unsigned int g_allocated_handle = 0;
28 static GSList *g_widget_list = NULL;
30 static pthread_mutex_t g_widget_list_mutex = PTHREAD_MUTEX_INITIALIZER;
32 vc_widget_s* widget_get(vc_h vc)
34 pthread_mutex_lock(&g_widget_list_mutex);
37 SLOG(LOG_WARN, TAG_VCW, "[WARNING] Input parameter is NULL");
38 pthread_mutex_unlock(&g_widget_list_mutex);
42 vc_widget_s *data = NULL;
44 int count = g_slist_length(g_widget_list);
47 for (i = 0; i < count; i++) {
48 data = g_slist_nth_data(g_widget_list, i);
50 if (NULL != data && NULL != data->vc) {
51 if (vc->handle == data->vc->handle) {
52 pthread_mutex_unlock(&g_widget_list_mutex);
58 SLOG(LOG_DEBUG, TAG_VCW, "[DEBUG] Fail to get widget by vc");
60 pthread_mutex_unlock(&g_widget_list_mutex);
64 static unsigned int __client_generate_uid(unsigned int pid)
68 if (g_allocated_handle > MAX_NUMBER_OF_HANDLE) {
69 g_allocated_handle = 1;
72 /* generate uid, handle number should be smaller than 1000 */
73 return pid * 1000u + g_allocated_handle;
76 int vc_widget_client_create(vc_h* vc)
78 vc_widget_s *widget = NULL;
80 widget = (vc_widget_s*)calloc(1, sizeof(vc_widget_s));
82 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Fail to allocate memory");
83 return VC_ERROR_OUT_OF_MEMORY;
86 vc_h temp = (vc_h)calloc(1, sizeof(struct vc_s));
88 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Fail to allocate memory");
90 return VC_ERROR_OUT_OF_MEMORY;
93 temp->handle = __client_generate_uid((unsigned int)getpid());
95 /* initialize widget data */
97 widget->pid = getpid();
98 widget->uid = temp->handle;
101 widget->result_cb = NULL;
102 widget->result_user_data = NULL;
103 widget->asr_result_cb = NULL;
104 widget->asr_result_user_data = NULL;
105 widget->asr_result_enabled = false;
106 widget->service_state_changed_cb = NULL;
107 widget->service_state_changed_user_data = NULL;
108 widget->state_changed_cb = NULL;
109 widget->state_changed_user_data = NULL;
110 widget->show_tooltip_cb = NULL;
111 widget->show_tooltip_user_data = NULL;
112 widget->error_cb = NULL;
113 widget->error_user_data = NULL;
115 widget->previous_state = VC_STATE_INITIALIZED;
116 widget->current_state = VC_STATE_INITIALIZED;
118 widget->conn_timer = NULL;
120 widget->cb_ref_count = 0;
122 pthread_mutex_lock(&g_widget_list_mutex);
123 g_widget_list = g_slist_append(g_widget_list, widget);
124 pthread_mutex_unlock(&g_widget_list_mutex);
128 return VC_ERROR_NONE;
131 int vc_widget_client_destroy(vc_h vc)
134 SLOG(LOG_ERROR, TAG_VCW, "Input parameter is NULL");
135 return VC_ERROR_NONE;
138 vc_widget_s *data = NULL;
140 pthread_mutex_lock(&g_widget_list_mutex);
141 int count = g_slist_length(g_widget_list);
144 for (i = 0; i < count; i++) {
145 data = g_slist_nth_data(g_widget_list, i);
148 if (vc->handle == data->vc->handle) {
149 g_widget_list = g_slist_remove(g_widget_list, data);
151 while (0 != data->cb_ref_count) {
152 /* wait for release callback function */
160 pthread_mutex_unlock(&g_widget_list_mutex);
161 return VC_ERROR_NONE;
166 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] widget Not found");
168 pthread_mutex_unlock(&g_widget_list_mutex);
169 return VC_ERROR_INVALID_PARAMETER;
172 GSList* vc_widget_client_get_client_list()
174 pthread_mutex_lock(&g_widget_list_mutex);
175 GSList *ret = g_widget_list;
176 pthread_mutex_unlock(&g_widget_list_mutex);
180 bool vc_widget_client_is_valid(vc_h vc)
182 vc_widget_s* widget = widget_get(vc);
185 if (NULL == widget) {
186 SLOG(LOG_DEBUG, TAG_VCW, "[DEBUG] vc is not valid");
193 bool vc_widget_client_is_valid_by_uid(unsigned int uid)
195 vc_widget_s *data = NULL;
197 pthread_mutex_lock(&g_widget_list_mutex);
198 int count = g_slist_length(g_widget_list);
201 for (i = 0; i < count; i++) {
202 data = g_slist_nth_data(g_widget_list, i);
205 if (uid == data->vc->handle) {
206 pthread_mutex_unlock(&g_widget_list_mutex);
212 SLOG(LOG_DEBUG, TAG_VCW, "[DEBUG] Fail to get widget by vc");
214 pthread_mutex_unlock(&g_widget_list_mutex);
218 int vc_widget_client_get_handle(unsigned int uid, vc_h* vc)
220 vc_widget_s *data = NULL;
222 pthread_mutex_lock(&g_widget_list_mutex);
223 int count = g_slist_length(g_widget_list);
226 for (i = 0; i < count; i++) {
227 data = g_slist_nth_data(g_widget_list, i);
230 if (uid == data->vc->handle) {
232 pthread_mutex_unlock(&g_widget_list_mutex);
233 return VC_ERROR_NONE;
238 pthread_mutex_unlock(&g_widget_list_mutex);
239 return VC_ERROR_INVALID_PARAMETER;
242 /* set/get callback function */
243 int vc_widget_client_set_result_cb(vc_h vc, vc_result_cb callback, void* user_data)
245 vc_widget_s* widget = widget_get(vc);
249 return VC_ERROR_INVALID_PARAMETER;
251 widget->result_cb = callback;
252 widget->result_user_data = user_data;
254 return VC_ERROR_NONE;
257 int vc_widget_client_get_result_cb(vc_h vc, vc_result_cb* callback, void** user_data)
259 vc_widget_s* widget = widget_get(vc);
263 return VC_ERROR_INVALID_PARAMETER;
265 *callback = widget->result_cb;
266 *user_data = widget->result_user_data;
268 return VC_ERROR_NONE;
271 int vc_widget_client_set_asr_result_enabled(vc_h vc, bool enabled)
273 vc_widget_s* widget = widget_get(vc);
277 return VC_ERROR_INVALID_PARAMETER;
279 widget->asr_result_enabled = enabled;
281 return VC_ERROR_NONE;
284 int vc_widget_client_get_asr_result_enabled(vc_h vc, bool* enabled)
286 vc_widget_s* widget = widget_get(vc);
290 return VC_ERROR_INVALID_PARAMETER;
292 *enabled = widget->asr_result_enabled;
294 return VC_ERROR_NONE;
297 int vc_widget_client_set_asr_result_cb(vc_h vc, vc_asr_result_cb callback, void* user_data)
299 vc_widget_s* widget = widget_get(vc);
303 return VC_ERROR_INVALID_PARAMETER;
305 widget->asr_result_cb = callback;
306 widget->asr_result_user_data = user_data;
308 return VC_ERROR_NONE;
311 int vc_widget_client_get_asr_result_cb(vc_h vc, vc_asr_result_cb* callback, void** user_data)
313 vc_widget_s* widget = widget_get(vc);
317 return VC_ERROR_INVALID_PARAMETER;
319 *callback = widget->asr_result_cb;
320 *user_data = widget->asr_result_user_data;
322 return VC_ERROR_NONE;
325 int vc_widget_client_set_service_state_changed_cb(vc_h vc, vc_service_state_changed_cb callback, void* user_data)
327 vc_widget_s* widget = widget_get(vc);
331 return VC_ERROR_INVALID_PARAMETER;
333 widget->service_state_changed_cb = callback;
334 widget->service_state_changed_user_data = user_data;
336 return VC_ERROR_NONE;
339 int vc_widget_client_get_service_state_changed_cb(vc_h vc, vc_service_state_changed_cb* callback, void** user_data)
341 vc_widget_s* widget = widget_get(vc);
345 return VC_ERROR_INVALID_PARAMETER;
347 *callback = widget->service_state_changed_cb;
348 *user_data = widget->service_state_changed_user_data;
350 return VC_ERROR_NONE;
353 int vc_widget_client_set_state_changed_cb(vc_h vc, vc_state_changed_cb callback, void* user_data)
355 vc_widget_s* widget = widget_get(vc);
359 return VC_ERROR_INVALID_PARAMETER;
361 widget->state_changed_cb = callback;
362 widget->state_changed_user_data = user_data;
364 return VC_ERROR_NONE;
367 int vc_widget_client_get_state_changed_cb(vc_h vc, vc_state_changed_cb* callback, void** user_data)
369 vc_widget_s* widget = widget_get(vc);
373 return VC_ERROR_INVALID_PARAMETER;
375 *callback = widget->state_changed_cb;
376 *user_data = widget->state_changed_user_data;
378 return VC_ERROR_NONE;
381 int vc_widget_client_set_show_tooltip_cb(vc_h vc, vc_widget_show_tooltip_cb callback, void* user_data)
383 vc_widget_s* widget = widget_get(vc);
387 return VC_ERROR_INVALID_PARAMETER;
389 widget->show_tooltip_cb = callback;
390 widget->show_tooltip_user_data = user_data;
392 return VC_ERROR_NONE;
395 int vc_widget_client_get_show_tooltip_cb(vc_h vc, vc_widget_show_tooltip_cb* callback, void** user_data)
397 vc_widget_s* widget = widget_get(vc);
401 return VC_ERROR_INVALID_PARAMETER;
403 *callback = widget->show_tooltip_cb;
404 *user_data = widget->show_tooltip_user_data;
406 return VC_ERROR_NONE;
409 int vc_widget_client_set_current_lang_changed_cb(vc_h vc, vc_current_language_changed_cb callback, void* user_data)
411 vc_widget_s* widget = widget_get(vc);
415 return VC_ERROR_INVALID_PARAMETER;
417 widget->current_lang_changed_cb = callback;
418 widget->current_lang_changed_user_data = user_data;
420 return VC_ERROR_NONE;
423 int vc_widget_client_get_current_lang_changed_cb(vc_h vc, vc_current_language_changed_cb* callback, void** user_data)
425 vc_widget_s* widget = widget_get(vc);
429 return VC_ERROR_INVALID_PARAMETER;
431 *callback = widget->current_lang_changed_cb;
432 *user_data = widget->current_lang_changed_user_data;
434 return VC_ERROR_NONE;
437 int vc_widget_client_set_error_cb(vc_h vc, vc_error_cb callback, void* user_data)
439 vc_widget_s* widget = widget_get(vc);
443 return VC_ERROR_INVALID_PARAMETER;
445 widget->error_cb = callback;
446 widget->error_user_data = user_data;
448 return VC_ERROR_NONE;
451 int vc_widget_client_get_error_cb(vc_h vc, vc_error_cb* callback, void** user_data)
453 vc_widget_s* widget = widget_get(vc);
457 return VC_ERROR_INVALID_PARAMETER;
459 *callback = widget->error_cb;
460 *user_data = widget->error_user_data;
462 return VC_ERROR_NONE;
465 int vc_widget_client_set_send_command_list_cb(vc_h vc, vc_widget_send_current_command_list_cb callback, void* user_data)
467 vc_widget_s* widget = widget_get(vc);
471 return VC_ERROR_INVALID_PARAMETER;
473 widget->send_command_list_cb = callback;
474 widget->send_command_list_user_data = user_data;
476 return VC_ERROR_NONE;
479 int vc_widget_client_get_send_command_list_cb(vc_h vc, vc_widget_send_current_command_list_cb* callback, void** user_data)
481 vc_widget_s* widget = widget_get(vc);
485 return VC_ERROR_INVALID_PARAMETER;
487 *callback = widget->send_command_list_cb;
488 *user_data = widget->send_command_list_user_data;
490 return VC_ERROR_NONE;
495 int vc_widget_client_set_service_state(vc_h vc, vc_service_state_e state)
497 vc_widget_s* widget = widget_get(vc);
501 return VC_ERROR_INVALID_PARAMETER;
503 widget->service_state = state;
505 return VC_ERROR_NONE;
508 int vc_widget_client_get_service_state(vc_h vc, vc_service_state_e* state)
510 vc_widget_s* widget = widget_get(vc);
514 return VC_ERROR_INVALID_PARAMETER;
516 *state = widget->service_state;
518 return VC_ERROR_NONE;
522 int vc_widget_client_set_state(vc_h vc, vc_state_e state)
524 vc_widget_s* widget = widget_get(vc);
528 return VC_ERROR_INVALID_PARAMETER;
530 widget->previous_state = widget->current_state;
531 widget->current_state = state;
533 return VC_ERROR_NONE;
536 int vc_widget_client_get_state(vc_h vc, vc_state_e* state)
538 vc_widget_s* widget = widget_get(vc);
542 return VC_ERROR_INVALID_PARAMETER;
544 *state = widget->current_state;
546 return VC_ERROR_NONE;
549 int vc_widget_client_get_state_by_uid(unsigned int uid, vc_state_e* state)
551 vc_widget_s *data = NULL;
553 pthread_mutex_lock(&g_widget_list_mutex);
554 int count = g_slist_length(g_widget_list);
557 for (i = 0; i < count; i++) {
558 data = g_slist_nth_data(g_widget_list, i);
561 if (uid == data->vc->handle) {
562 *state = data->current_state;
563 pthread_mutex_unlock(&g_widget_list_mutex);
564 return VC_ERROR_NONE;
569 pthread_mutex_unlock(&g_widget_list_mutex);
570 return VC_ERROR_INVALID_PARAMETER;
573 int vc_widget_client_get_previous_state(vc_h vc, vc_state_e* state, vc_state_e* previous_state)
575 vc_widget_s* widget = widget_get(vc);
579 return VC_ERROR_INVALID_PARAMETER;
581 *previous_state = widget->previous_state;
582 *state = widget->current_state;
584 return VC_ERROR_NONE;
587 int vc_widget_client_set_xid(vc_h vc, int xid)
589 vc_widget_s* widget = widget_get(vc);
593 return VC_ERROR_INVALID_PARAMETER;
597 return VC_ERROR_NONE;
600 int vc_widget_cilent_get_xid(vc_h vc, int* xid)
602 vc_widget_s* widget = widget_get(vc);
606 return VC_ERROR_INVALID_PARAMETER;
610 return VC_ERROR_NONE;
613 int vc_widget_client_set_error(vc_h vc, int reason)
615 vc_widget_s* widget = widget_get(vc);
619 return VC_ERROR_INVALID_PARAMETER;
621 widget->reason = reason;
623 return VC_ERROR_NONE;
626 int vc_widget_client_get_error(vc_h vc, int* reason)
628 vc_widget_s* widget = widget_get(vc);
632 return VC_ERROR_INVALID_PARAMETER;
634 *reason = widget->reason;
636 return VC_ERROR_NONE;
639 int vc_widget_client_set_show_tooltip(vc_h vc, bool show)
641 vc_widget_s* widget = widget_get(vc);
645 return VC_ERROR_INVALID_PARAMETER;
647 widget->show_tooltip = show;
649 return VC_ERROR_NONE;
652 int vc_widget_client_get_show_tooltip(vc_h vc, bool* show)
654 vc_widget_s* widget = widget_get(vc);
658 return VC_ERROR_INVALID_PARAMETER;
660 *show = widget->show_tooltip;
662 return VC_ERROR_NONE;
665 int vc_widget_client_get_count()
667 pthread_mutex_lock(&g_widget_list_mutex);
668 int ret = g_slist_length(g_widget_list);
669 pthread_mutex_unlock(&g_widget_list_mutex);
673 int vc_widget_client_use_callback(vc_h vc)
675 vc_widget_s* widget = widget_get(vc);
679 return VC_ERROR_INVALID_PARAMETER;
681 widget->cb_ref_count++;
682 return VC_ERROR_NONE;
685 int vc_widget_client_not_use_callback(vc_h vc)
687 vc_widget_s* widget = widget_get(vc);
691 return VC_ERROR_INVALID_PARAMETER;
693 widget->cb_ref_count--;
694 return VC_ERROR_NONE;