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 /* exclusive option */
44 vc_service_state_e service_state;
47 vc_state_e before_state;
48 vc_state_e current_state;
57 vc_auth_state_e auth_before_state;
58 vc_auth_state_e auth_current_state;
59 vc_auth_state_changed_cb auth_state_changed_cb;
60 void* auth_state_changed_user_data;
68 char* invocation_name;
72 static GSList *g_client_list = NULL;
75 static vc_client_s* __client_get(vc_h vc)
81 vc_client_s *data = NULL;
83 int count = g_slist_length(g_client_list);
86 for (i = 0; i < count; i++) {
87 data = g_slist_nth_data(g_client_list, i);
90 if (vc->handle == data->vc->handle) {
96 SLOG(LOG_DEBUG, TAG_VCC, "[DEBUG] Fail to get client by vc");
101 int vc_client_create(vc_h* vc)
103 vc_client_s *client = NULL;
105 client = (vc_client_s*)calloc(1, sizeof(vc_client_s));
106 if (NULL == client) {
107 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to allocate memory");
108 return VC_ERROR_OUT_OF_MEMORY;
111 vc_h temp = (vc_h)calloc(1, sizeof(struct vc_s));
113 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to allocate memory");
115 return VC_ERROR_OUT_OF_MEMORY;
118 temp->handle = getpid();
120 /* initialize client data */
122 client->pid = getpid();
123 client->uid = temp->handle;
126 client->result_cb = NULL;
127 client->result_user_data = NULL;
128 client->service_state_changed_cb = NULL;
129 client->service_state_changed_user_data = NULL;
130 client->state_changed_cb = NULL;
131 client->state_changed_user_data = NULL;
132 client->current_lang_changed_cb = NULL;
133 client->current_lang_changed_user_data = NULL;
134 client->error_cb = NULL;
135 client->error_user_data = NULL;
138 client->exclusive_cmd = false;
141 client->service_state = VC_RUNTIME_INFO_NO_FOREGROUND;
143 client->before_state = VC_STATE_INITIALIZED;
144 client->current_state = VC_STATE_INITIALIZED;
146 client->cb_ref_count = 0;
149 client->auth_before_state = VC_AUTH_STATE_NONE;
150 client->auth_current_state = VC_AUTH_STATE_NONE;
151 client->auth_state_changed_cb = NULL;
152 client->auth_state_changed_user_data = NULL;
154 client->is_foreground = false;
155 client->invocation_name = NULL;
157 g_client_list = g_slist_append(g_client_list, client);
164 int vc_client_destroy(vc_h vc)
167 SLOG(LOG_ERROR, TAG_VCC, "Input parameter is NULL");
171 vc_client_s *data = NULL;
173 int count = g_slist_length(g_client_list);
176 for (i = 0; i < count; i++) {
177 data = g_slist_nth_data(g_client_list, i);
180 if (vc->handle == data->vc->handle) {
181 g_client_list = g_slist_remove(g_client_list, data);
183 while (0 != data->cb_ref_count) {
184 /* wait for release callback function */
186 if (NULL != data->invocation_name) {
187 free(data->invocation_name);
188 data->invocation_name = NULL;
199 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] client Not found");
204 bool vc_client_is_valid(vc_h vc)
206 vc_client_s* client = __client_get(vc);
209 if (NULL == client) {
210 SLOG(LOG_DEBUG, TAG_VCC, "[DEBUG] vc is not valid");
217 bool vc_client_is_valid_by_uid(int uid)
219 vc_client_s *data = NULL;
221 int count = g_slist_length(g_client_list);
224 for (i = 0; i < count; i++) {
225 data = g_slist_nth_data(g_client_list, i);
228 if (uid == data->vc->handle)
233 SLOG(LOG_DEBUG, TAG_VCC, "[DEBUG] Fail to get client by vc");
238 int vc_client_get_handle(int uid, vc_h* vc)
240 vc_client_s *data = NULL;
242 int count = g_slist_length(g_client_list);
245 for (i = 0; i < count; i++) {
246 data = g_slist_nth_data(g_client_list, i);
249 if (uid == data->vc->handle) {
259 /* set/get callback function */
260 int vc_client_set_result_cb(vc_h vc, vc_result_cb callback, void* user_data)
262 vc_client_s* client = __client_get(vc);
266 return VC_ERROR_INVALID_PARAMETER;
268 client->result_cb = callback;
269 client->result_user_data = user_data;
274 int vc_client_get_result_cb(vc_h vc, vc_result_cb* callback, void** user_data)
276 vc_client_s* client = __client_get(vc);
280 return VC_ERROR_INVALID_PARAMETER;
282 *callback = client->result_cb;
283 *user_data = client->result_user_data;
288 int vc_client_set_service_state_changed_cb(vc_h vc, vc_service_state_changed_cb callback, void* user_data)
290 vc_client_s* client = __client_get(vc);
294 return VC_ERROR_INVALID_PARAMETER;
296 client->service_state_changed_cb = callback;
297 client->service_state_changed_user_data = user_data;
302 int vc_client_get_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 *callback = client->service_state_changed_cb;
311 *user_data = client->service_state_changed_user_data;
316 int vc_client_set_state_changed_cb(vc_h vc, vc_state_changed_cb callback, void* user_data)
318 vc_client_s* client = __client_get(vc);
322 return VC_ERROR_INVALID_PARAMETER;
324 client->state_changed_cb = callback;
325 client->state_changed_user_data = user_data;
330 int vc_client_get_state_changed_cb(vc_h vc, vc_state_changed_cb* callback, void** user_data)
332 vc_client_s* client = __client_get(vc);
336 return VC_ERROR_INVALID_PARAMETER;
338 *callback = client->state_changed_cb;
339 *user_data = client->state_changed_user_data;
344 int vc_client_set_current_lang_changed_cb(vc_h vc, vc_current_language_changed_cb callback, void* user_data)
346 vc_client_s* client = __client_get(vc);
350 return VC_ERROR_INVALID_PARAMETER;
352 client->current_lang_changed_cb = callback;
353 client->current_lang_changed_user_data = user_data;
358 int vc_client_get_current_lang_changed_cb(vc_h vc, vc_current_language_changed_cb* callback, void** user_data)
360 vc_client_s* client = __client_get(vc);
364 return VC_ERROR_INVALID_PARAMETER;
366 *callback = client->current_lang_changed_cb;
367 *user_data = client->current_lang_changed_user_data;
372 int vc_client_set_error_cb(vc_h vc, vc_error_cb callback, void* user_data)
374 vc_client_s* client = __client_get(vc);
378 return VC_ERROR_INVALID_PARAMETER;
380 client->error_cb = callback;
381 client->error_user_data = user_data;
386 int vc_client_get_error_cb(vc_h vc, vc_error_cb* callback, void** user_data)
388 vc_client_s* client = __client_get(vc);
392 return VC_ERROR_INVALID_PARAMETER;
394 *callback = client->error_cb;
395 *user_data = client->error_user_data;
401 int vc_client_set_service_state(vc_h vc, vc_service_state_e state)
403 vc_client_s* client = __client_get(vc);
407 return VC_ERROR_INVALID_PARAMETER;
409 client->service_state = state;
414 int vc_client_get_service_state(vc_h vc, vc_service_state_e* state)
416 vc_client_s* client = __client_get(vc);
420 return VC_ERROR_INVALID_PARAMETER;
422 *state = client->service_state;
427 int vc_client_set_client_state(vc_h vc, vc_state_e state)
429 vc_client_s* client = __client_get(vc);
433 return VC_ERROR_INVALID_PARAMETER;
435 client->before_state = client->current_state;
436 client->current_state = state;
441 int vc_client_get_client_state(vc_h vc, vc_state_e* state)
443 vc_client_s* client = __client_get(vc);
447 return VC_ERROR_INVALID_PARAMETER;
449 *state = client->current_state;
454 int vc_client_get_client_state_by_uid(int uid, vc_state_e* state)
456 vc_client_s *data = NULL;
458 int count = g_slist_length(g_client_list);
461 for (i = 0; i < count; i++) {
462 data = g_slist_nth_data(g_client_list, i);
465 if (uid == data->vc->handle) {
466 *state = data->current_state;
475 int vc_client_get_before_state(vc_h vc, vc_state_e* state, vc_state_e* before_state)
477 vc_client_s* client = __client_get(vc);
481 return VC_ERROR_INVALID_PARAMETER;
483 *before_state = client->before_state;
484 *state = client->current_state;
489 int vc_client_set_invocation_name(vc_h vc, const char* invocation_name)
491 vc_client_s* client = __client_get(vc);
495 return VC_ERROR_INVALID_PARAMETER;
497 if (NULL != client->invocation_name)
498 free(client->invocation_name);
500 client->invocation_name = NULL;
502 if (NULL != invocation_name) {
503 client->invocation_name = strdup(invocation_name);
508 int vc_client_get_invocation_name(vc_h vc, char** invocation_name)
510 vc_client_s* client = __client_get(vc);
514 return VC_ERROR_INVALID_PARAMETER;
516 if (NULL != client->invocation_name)
517 *invocation_name = strdup(client->invocation_name);
521 int vc_client_set_xid(vc_h vc, int xid)
523 vc_client_s* client = __client_get(vc);
527 return VC_ERROR_INVALID_PARAMETER;
534 int vc_client_get_xid(vc_h vc, int* xid)
536 vc_client_s* client = __client_get(vc);
540 return VC_ERROR_INVALID_PARAMETER;
547 int vc_client_set_is_foreground(vc_h vc, bool value)
549 vc_client_s* client = __client_get(vc);
553 return VC_ERROR_INVALID_PARAMETER;
555 client->is_foreground = value;
559 int vc_client_get_is_foreground(vc_h vc, bool* value)
561 vc_client_s* client = __client_get(vc);
565 return VC_ERROR_INVALID_PARAMETER;
567 *value = client->is_foreground;
573 int vc_client_set_exclusive_cmd(vc_h vc, bool value)
575 vc_client_s* client = __client_get(vc);
579 return VC_ERROR_INVALID_PARAMETER;
581 client->exclusive_cmd = value;
586 int vc_client_get_exclusive_cmd(vc_h vc, bool* value)
588 vc_client_s* client = __client_get(vc);
592 return VC_ERROR_INVALID_PARAMETER;
594 *value = client->exclusive_cmd;
600 int vc_client_set_error(vc_h vc, int reason)
602 vc_client_s* client = __client_get(vc);
606 return VC_ERROR_INVALID_PARAMETER;
608 client->reason = reason;
613 int vc_client_get_error(vc_h vc, int* reason)
615 vc_client_s* client = __client_get(vc);
619 return VC_ERROR_INVALID_PARAMETER;
621 *reason = client->reason;
628 int vc_client_get_count()
630 return g_slist_length(g_client_list);
633 int vc_client_use_callback(vc_h vc)
635 vc_client_s* client = __client_get(vc);
639 return VC_ERROR_INVALID_PARAMETER;
641 client->cb_ref_count++;
645 int vc_client_not_use_callback(vc_h vc)
647 vc_client_s* client = __client_get(vc);
651 return VC_ERROR_INVALID_PARAMETER;
653 client->cb_ref_count--;
658 int vc_client_set_auth_state_changed_cb(vc_h vc, vc_auth_state_changed_cb callback, void* user_data)
660 vc_client_s* client = __client_get(vc);
664 return VC_ERROR_INVALID_PARAMETER;
666 client->auth_state_changed_cb = callback;
667 client->auth_state_changed_user_data = user_data;
672 int vc_client_get_auth_state_changed_cb(vc_h vc, vc_auth_state_changed_cb* callback, void** user_data)
674 vc_client_s* client = __client_get(vc);
678 return VC_ERROR_INVALID_PARAMETER;
680 *callback = client->auth_state_changed_cb;
681 *user_data = client->auth_state_changed_user_data;
686 int vc_client_unset_auth_state_changed_cb(vc_h vc)
688 vc_client_s* client = __client_get(vc);
692 return VC_ERROR_INVALID_PARAMETER;
694 client->auth_state_changed_cb = NULL;
695 client->auth_state_changed_user_data = NULL;
700 int vc_client_set_auth_state(vc_h vc, vc_auth_state_e state)
702 vc_client_s* client = __client_get(vc);
706 return VC_ERROR_INVALID_PARAMETER;
708 client->auth_before_state = client->auth_current_state;
709 client->auth_current_state = state;
714 int vc_client_get_auth_state(vc_h vc, vc_auth_state_e* state)
716 vc_client_s* client = __client_get(vc);
720 return VC_ERROR_INVALID_PARAMETER;
722 *state = client->auth_current_state;
727 int vc_client_get_before_auth_state(vc_h vc, vc_auth_state_e* before, vc_auth_state_e* current)
729 vc_client_s* client = __client_get(vc);
733 return VC_ERROR_INVALID_PARAMETER;
735 *before = client->auth_before_state;
736 *current = client->auth_current_state;
741 int vc_client_set_mgr_pid(vc_h vc, int mgr_pid)
743 vc_client_s* client = __client_get(vc);
747 return VC_ERROR_INVALID_PARAMETER;
749 client->mgr_pid = mgr_pid;
754 int vc_client_get_mgr_pid(vc_h vc, int* mgr_pid)
756 vc_client_s* client = __client_get(vc);
760 return VC_ERROR_INVALID_PARAMETER;
762 *mgr_pid = client->mgr_pid;