rename init/shutdown in ipc
[platform/core/uifw/mmi-framework.git] / src / mmi-client.c
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *               http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include "mmi-ipc.h"
19 #include "mmi-dbg.h"
20 #include "mmi-client.h"
21
22 static mmi_handle mmi_h = NULL;
23
24 int mmi_client_create(void)
25 {
26         mmi_h = (mmi_handle)calloc(1, sizeof(mmi_struct_s));
27
28         if (mmi_h == NULL) {
29                 LOGE("Failed to allocate memory for mmi_h !\n");
30                 return MMI_ERROR_OUT_OF_MEMORY;
31         }
32
33         if (mmi_ipc_initialize()) {
34                 LOGE("Failed to init mmi ipc !\n");
35                 if(mmi_h != NULL) {
36                         free(mmi_h);
37                         mmi_h = NULL;
38                 }
39                 return MMI_ERROR_NOT_SUPPORTED;
40         }
41
42         mmi_h->rpc_h = mmi_ipc_get_rpc_h();
43         mmi_h->stub_appid = mmi_ipc_get_stub_appid();
44         mmi_h->result_cb_list = NULL;
45
46         return MMI_ERROR_NONE;
47 }
48
49 int mmi_client_destroy(void)
50 {
51         if (mmi_h == NULL) {
52                 LOGE("A mmi_h is already NULL");
53                 return MMI_ERROR_NONE;
54         }
55
56         mmi_ipc_deinitialize();
57
58         GList *iter = NULL;
59         mmi_result_cb_s *data = NULL;
60
61         if (g_list_length(mmi_h->result_cb_list) > 0) {
62                 iter = g_list_first(mmi_h->result_cb_list);
63                 while (iter != NULL) {
64                         data = iter->data;
65                         if (data != NULL) {
66                                 data->input_event_type = 0;
67                                 data->result_callback = NULL;
68                                 free(data);
69                                 data = NULL;
70
71                                 GList *temp = iter;
72                                 iter = g_list_next(iter);
73                                 mmi_h->result_cb_list = g_list_remove_link(mmi_h->result_cb_list, temp);
74                         }
75                 }
76         }
77
78         free(mmi_h);
79         mmi_h = NULL;
80
81         return MMI_ERROR_NONE;
82 }
83
84 int mmi_client_set_result_cb(int input_event_type, mmi_result_cb callback, void* user_data)
85 {
86         LOGI("Set result cb about input event type(%d) to client", input_event_type);
87
88         if (callback == NULL) {
89                 LOGE("Callback is invalid");
90                 return MMI_ERROR_INVALID_PARAMETER;
91         }
92
93         mmi_result_cb_s* input_result_callback = (mmi_result_cb_s*)calloc(1, sizeof(mmi_result_cb_s));
94         if (input_result_callback == NULL) {
95                 LOGE("[ERROR] Fail to allocate memory");
96                 return MMI_ERROR_OUT_OF_MEMORY;
97         }
98
99         input_result_callback->input_event_type = input_event_type;
100         input_result_callback->result_callback = callback;
101         mmi_h->result_cb_list  = g_list_append(mmi_h->result_cb_list, input_result_callback);
102
103         return MMI_ERROR_NONE;
104 }
105
106 mmi_handle mmi_client_get(void)
107 {
108         return mmi_h;
109 }