mmifw : solves ctrl+c abort issue - replace ecore_shutdown to ecore_main_loop_quit
[platform/core/uifw/mmi-framework.git] / src / mmifw.c
1 /*
2 * Copyright © 2021 Samsung Electronics co., Ltd. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "mmifw.h"
25 #include "mmifw-ipc.h"
26 #include "mmifw-dbg.h"
27
28 #define ERR(x)
29
30 int MMI_EVENT_CONNECTION;
31 int MMI_EVENT_FOCUS;
32 int MMI_EVENT_STATE_CHANGE;
33 int MMI_EVENT_WAKE_UP;
34 int MMI_EVENT_KEY;
35 int MMI_EVENT_GESTURE;
36 int MMI_EVENT_VOICE;
37 int MMI_EVENT_ACTION;
38 int MMI_EVENT_FEEDBACK;
39
40 static int _mmi_init_count = 0;
41 Eina_List *listener_list = NULL;
42
43 MMIFW_API int
44 mmi_init(void)
45 {
46         if (++_mmi_init_count != 1)
47                 return _mmi_init_count;
48
49         ecore_init();
50
51         Eina_List *l = NULL;
52         mmi_event_listener *listener = NULL;
53
54         if (listener_list)
55         {
56                 EINA_LIST_FOREACH(listener_list, l, listener)
57                         ecore_event_handler_del(listener);
58
59                 listener_list = eina_list_free(listener_list);
60                 listener_list = NULL;
61         }
62
63         MMI_EVENT_CONNECTION = ecore_event_type_new();
64         MMI_EVENT_FOCUS = ecore_event_type_new();
65         MMI_EVENT_STATE_CHANGE = ecore_event_type_new();
66         MMI_EVENT_WAKE_UP = ecore_event_type_new();
67         MMI_EVENT_KEY = ecore_event_type_new();
68         MMI_EVENT_GESTURE = ecore_event_type_new();
69         MMI_EVENT_VOICE = ecore_event_type_new();
70         MMI_EVENT_ACTION = ecore_event_type_new();
71         MMI_EVENT_FEEDBACK = ecore_event_type_new();
72
73         return _mmi_init_count;
74 err:
75         ecore_shutdown();
76         return --_mmi_init_count;
77 }
78
79 MMIFW_API int
80 mmi_shutdown(void)
81 {
82         if (_mmi_init_count <= 0)
83         {
84                 ERR("Init count must be greater than 0.");
85                 return 0;
86         }
87
88         _mmi_init_count--;
89
90         MMI_EVENT_CONNECTION = -1;
91         MMI_EVENT_FOCUS = -1;
92         MMI_EVENT_STATE_CHANGE = -1;
93         MMI_EVENT_WAKE_UP = -1;
94         MMI_EVENT_KEY = -1;
95         MMI_EVENT_GESTURE = -1;
96         MMI_EVENT_VOICE = -1;
97         MMI_EVENT_ACTION = -1;
98         MMI_EVENT_FEEDBACK = -1;
99
100         Eina_List *l = NULL;
101         mmi_event_listener *listener = NULL;
102
103         if (listener_list)
104         {
105                 EINA_LIST_FOREACH(listener_list, l, listener)
106                         ecore_event_handler_del(listener);
107
108                 listener_list = eina_list_free(listener_list);
109                 listener_list = NULL;
110         }
111
112         ecore_main_loop_quit();
113         return _mmi_init_count;
114 }
115
116 MMIFW_API mmi_handle
117 mmi_instance_create(const char *app_id)
118 {
119         mmi_handle h = NULL;
120
121         h = (mmi_handle)calloc(1, sizeof(mmi_struct));
122
123         if (!h)
124         {
125                 ERR("Failed to allocate memory for mmi_handle !\n");
126                 return NULL;
127         }
128
129         if (mmi_ipc_init(app_id))
130         {
131                 ERR("Failed to init mmifw ipc !\n");
132                 goto err;
133         }
134
135         h->rpc_h = mmi_ipc_get_rpc_h();
136         h->app_id = mmi_ipc_get_appid();
137         h->stub_appid = mmi_ipc_get_stub_appid();
138         h->uid = mmi_ipc_get_uid();
139         h->state = mmi_ipc_get_state();
140
141         return h;
142 err:
143         if (h)
144                 free(h);
145         return NULL;
146 }
147
148 MMIFW_API mmi_event_listener *
149 mmi_event_add_listener(mmi_handle h, int ev_type, mmi_event_handler_cb func, const void *data)
150 {
151         mmi_event_listener *listener;
152
153         (void) h;
154
155         listener = ecore_event_handler_add(ev_type, func, data);
156         listener_list = eina_list_append(listener_list, listener);
157         return listener;
158 }
159
160 MMIFW_API mmi_result
161 mmi_request_send_get_focus(mmi_handle h)
162 {
163         mmi_result res = MMI_RESULT_SUCCESS;
164
165         if (!h)
166         {
167                 ERR("Given mmi_handle is invalid !\n");
168                 return MMI_RESULT_FAIL;
169         }
170
171         if (mmi_ipc_is_connected())
172                 rpc_port_proxy_mmifw_invoke_get_focus(h->rpc_h);
173         else
174         {
175                 ERR("Not connected yet !\n");
176                 res = MMI_RESULT_FAIL;
177         }
178
179         LOGI("Get_focus request has been sent !\n");
180
181         return res;
182 }
183
184 MMIFW_API mmi_state
185 mmi_state_get_current_state(mmi_handle h)
186 {
187         mmi_state state = MMI_STATE_NONE;
188
189         if (!h)
190         {
191                 ERR("Given mmi_handle is invalid !\n");
192                 return state;
193         }
194
195         state = h->state;
196         return state;
197 }
198
199 MMIFW_API mmi_result
200 mmi_request_send_set_state(mmi_handle h, mmi_state state)
201 {
202         mmi_result res = MMI_RESULT_SUCCESS;
203
204         if (!h)
205         {
206                 ERR("Given mmi_handle is invalid !\n");
207                 return MMI_RESULT_FAIL;
208         }
209
210         if (state == h->state)
211         {
212                 ERR("Given state equals to the current state. Ignored !\n");
213                 return res;
214         }
215
216         rpc_port_proxy_mmifw_invoke_set_state(h->rpc_h, state);
217         return res;
218 }
219
220 MMIFW_API void
221 mmi_event_remove_listener(mmi_handle h, mmi_event_listener *listener)
222 {
223         (void) h;
224
225         if (listener_list)
226                 listener_list = eina_list_remove(listener_list, listener);
227         if (listener)
228                 ecore_event_handler_del(listener);
229 }
230
231 MMIFW_API void
232 mmi_event_remove_all_listeners(mmi_handle h)
233 {
234         Eina_List *l = NULL;
235         mmi_event_listener *listener = NULL;
236
237         (void) h;
238
239         EINA_LIST_FOREACH(listener_list, l, listener)
240                 ecore_event_handler_del(listener);
241
242         listener_list = eina_list_free(listener_list);
243         listener_list = NULL;
244 }
245
246 MMIFW_API void
247 mmi_instance_destroy(mmi_handle h)
248 {
249         if (!h)
250                 return;
251
252         mmi_ipc_shutdown();
253         free(h);
254 }
255