Load voice-control-elm library in the thread
[platform/core/appfw/app-core.git] / src / multiwindow_base / appcore_multiwindow_base.c
1 /*
2  * Copyright (c) 2017 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 #include <sys/types.h>
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <linux/limits.h>
24
25 #include <Ecore_Wl2.h>
26 #include <glib-object.h>
27 #include <malloc.h>
28 #include <glib.h>
29 #include <gio/gio.h>
30 #include <stdbool.h>
31 #include <aul.h>
32 #include <aul_svc.h>
33 #include <bundle_internal.h>
34
35 #include "appcore_base.h"
36 #include "appcore_multiwindow_base.h"
37 #include "appcore_multiwindow_base_private.h"
38
39 appcore_multiwindow_base_context _appcore_mw_context;
40 static guint __flush_timer = 0;
41
42 static gboolean __flush_memory(gpointer data)
43 {
44         _DBG("Flush memory");
45         if (_appcore_mw_context.ops.base.trim_memory)
46                 _appcore_mw_context.ops.base.trim_memory(_appcore_mw_context.data);
47         __flush_timer = 0;
48         return G_SOURCE_REMOVE;
49 }
50
51 static void __add_flush_timer(void)
52 {
53         if (__flush_timer)
54                 return;
55
56         __flush_timer = g_timeout_add(5000, __flush_memory, NULL);
57 }
58
59 static void __remove_flush_timer(void)
60 {
61         if (!__flush_timer)
62                 return;
63
64         g_source_remove(__flush_timer);
65         __flush_timer = 0;
66 }
67
68 static Eina_Bool __stub_show_cb(void *data, int type, void *event)
69 {
70         if (_appcore_mw_context.ops.window.show)
71                 _appcore_mw_context.ops.window.show(type, event, _appcore_mw_context.data);
72
73         return ECORE_CALLBACK_RENEW;
74 }
75
76 static Eina_Bool __stub_hide_cb(void *data, int type, void *event)
77 {
78         if (_appcore_mw_context.ops.window.hide)
79                 _appcore_mw_context.ops.window.hide(type, event, _appcore_mw_context.data);
80
81         return ECORE_CALLBACK_RENEW;
82 }
83
84 static Eina_Bool __stub_visibility_cb(void *data, int type, void *event)
85 {
86         Ecore_Wl2_Event_Window_Visibility_Change *ev = event;
87
88         if (_appcore_mw_context.ops.window.visibility)
89                 _appcore_mw_context.ops.window.visibility(type, event, _appcore_mw_context.data);
90
91         if (ev && ev->fully_obscured) {
92                 if (!appcore_multiwindow_base_window_is_resumed())
93                         __add_flush_timer();
94         } else {
95                 __remove_flush_timer();
96         }
97
98         return ECORE_CALLBACK_RENEW;
99 }
100
101 static Eina_Bool __stub_lower_cb(void *data, int type, void *event)
102 {
103         if (_appcore_mw_context.ops.window.lower)
104                 _appcore_mw_context.ops.window.lower(type, event, _appcore_mw_context.data);
105
106         return ECORE_CALLBACK_RENEW;
107 }
108
109 static Eina_Bool __stub_pre_visibility_cb(void *data, int type, void *event)
110 {
111         Ecore_Wl2_Event_Window_Pre_Visibility_Change *ev = event;
112
113         if (_appcore_mw_context.ops.window.pre_visibility)
114                 _appcore_mw_context.ops.window.pre_visibility(type, event, _appcore_mw_context.data);
115
116         if (ev->type == ECORE_WL2_WINDOW_VISIBILITY_TYPE_PRE_UNOBSCURED)
117                 __remove_flush_timer();
118
119         return ECORE_CALLBACK_RENEW;
120 }
121
122 static Eina_Bool __stub_aux_message_cb(void *data, int type, void *event)
123 {
124         if (_appcore_mw_context.ops.window.aux_message)
125                 _appcore_mw_context.ops.window.aux_message(type, event, _appcore_mw_context.data);
126
127         return ECORE_CALLBACK_RENEW;
128 }
129
130 static void __add_ecore_events(void)
131 {
132         if (!_appcore_mw_context.hshow) {
133                 _appcore_mw_context.hshow = ecore_event_handler_add(
134                                 ECORE_WL2_EVENT_WINDOW_SHOW,
135                                 __stub_show_cb, NULL);
136                 if (!_appcore_mw_context.hshow)
137                         _ERR("Failed to add window show event");
138         }
139
140         if (!_appcore_mw_context.hhide) {
141                 _appcore_mw_context.hhide = ecore_event_handler_add(
142                                 ECORE_WL2_EVENT_WINDOW_HIDE,
143                                 __stub_hide_cb, NULL);
144                 if (!_appcore_mw_context.hhide)
145                         _ERR("Failed to add window hide event");
146         }
147
148         if (!_appcore_mw_context.hvchange) {
149                 _appcore_mw_context.hvchange = ecore_event_handler_add(
150                                 ECORE_WL2_EVENT_WINDOW_VISIBILITY_CHANGE,
151                                 __stub_visibility_cb, NULL);
152                 if (!_appcore_mw_context.hvchange)
153                         _ERR("Failed to add window visibility change event");
154         }
155
156         if (!_appcore_mw_context.hlower) {
157                 _appcore_mw_context.hlower = ecore_event_handler_add(
158                                 ECORE_WL2_EVENT_WINDOW_LOWER,
159                                 __stub_lower_cb, NULL);
160                 if (!_appcore_mw_context.hlower)
161                         _ERR("Failed to add window lower event");
162         }
163
164         if (!_appcore_mw_context.hpvchange) {
165                 _appcore_mw_context.hpvchange = ecore_event_handler_add(
166                                 ECORE_WL2_EVENT_WINDOW_PRE_VISIBILITY_CHANGE,
167                                 __stub_pre_visibility_cb, NULL);
168                 if (!_appcore_mw_context.hpvchange)
169                         _ERR("Failed to add window pre-visbiility change event");
170         }
171
172         if (!_appcore_mw_context.hauxmsg) {
173                 _appcore_mw_context.hauxmsg = ecore_event_handler_add(
174                                 ECORE_WL2_EVENT_AUX_MESSAGE,
175                                 __stub_aux_message_cb, NULL);
176                 if (!_appcore_mw_context.hauxmsg)
177                         _ERR("Failed to add aux message event");
178         }
179 }
180
181 static void __remove_ecore_events(void)
182 {
183         if (_appcore_mw_context.hshow) {
184                 ecore_event_handler_del(_appcore_mw_context.hshow);
185                 _appcore_mw_context.hshow = NULL;
186         }
187
188         if (_appcore_mw_context.hhide) {
189                 ecore_event_handler_del(_appcore_mw_context.hhide);
190                 _appcore_mw_context.hhide = NULL;
191         }
192
193         if (_appcore_mw_context.hvchange) {
194                 ecore_event_handler_del(_appcore_mw_context.hvchange);
195                 _appcore_mw_context.hvchange = NULL;
196         }
197
198         if (_appcore_mw_context.hlower) {
199                 ecore_event_handler_del(_appcore_mw_context.hlower);
200                 _appcore_mw_context.hlower = NULL;
201         }
202
203         if (_appcore_mw_context.hpvchange) {
204                 ecore_event_handler_del(_appcore_mw_context.hpvchange);
205                 _appcore_mw_context.hpvchange = NULL;
206         }
207
208         if (_appcore_mw_context.hauxmsg) {
209                 ecore_event_handler_del(_appcore_mw_context.hauxmsg);
210                 _appcore_mw_context.hauxmsg = NULL;
211         }
212 }
213
214 EXPORT_API int appcore_multiwindow_base_init(appcore_multiwindow_base_ops ops, int argc, char **argv, void *data)
215 {
216         _appcore_mw_context.ops = ops;
217         _appcore_mw_context.data = data;
218         _appcore_mw_context.argc = argc;
219         _appcore_mw_context.argv = argv;
220
221         return appcore_base_init(ops.base, argc, argv, data);
222 }
223
224 static void __destroy_iter(gpointer data, gpointer user_data)
225 {
226         appcore_multiwindow_base_instance *inst = data;
227
228         if (!inst)
229                 return;
230
231         appcore_multiwindow_base_instance_exit(inst);
232 }
233
234 static void __destroy_all(void)
235 {
236         g_list_foreach(_appcore_mw_context.instances, __destroy_iter, NULL);
237 }
238
239 static void __free_class(gpointer data)
240 {
241         appcore_multiwindow_base_class *cls = data;
242
243         free(cls->id);
244         free(cls);
245 }
246
247 EXPORT_API void appcore_multiwindow_base_fini(void)
248 {
249         __destroy_all();
250         g_list_free_full(_appcore_mw_context.classes, __free_class);
251         _appcore_mw_context.classes = NULL;
252
253         __remove_ecore_events();
254         appcore_base_fini();
255 }
256
257 EXPORT_API void appcore_multiwindow_base_exit(void)
258 {
259         if (_appcore_mw_context.ops.base.exit)
260                 _appcore_mw_context.ops.base.exit(_appcore_mw_context.data);
261 }
262
263 static int __on_receive(aul_type type, bundle *b, void *data)
264 {
265         return appcore_multiwindow_base_on_receive(type, b);
266 }
267
268 static int __on_create(void *data)
269 {
270         return appcore_multiwindow_base_on_create();
271 }
272
273 static int __on_terminate(void *data)
274 {
275         return appcore_multiwindow_base_on_terminate();
276 }
277
278 static void __on_trim_memory(void *data)
279 {
280         appcore_multiwindow_base_on_trim_memory();
281 }
282
283 static void __window_on_show(int type, void *event, void *data)
284 {
285         appcore_multiwindow_base_window_on_show(type, event);
286 }
287
288 static void __window_on_hide(int type, void *event, void *data)
289 {
290         appcore_multiwindow_base_window_on_hide(type, event);
291 }
292
293 static void __window_on_lower(int type, void *event, void *data)
294 {
295         appcore_multiwindow_base_window_on_lower(type, event);
296 }
297
298 static void __window_on_visibility(int type, void *event, void *data)
299 {
300         appcore_multiwindow_base_window_on_visibility(type, event);
301 }
302
303 static void __window_on_pre_visibility(int type, void *event, void *data)
304 {
305         appcore_multiwindow_base_window_on_pre_visibility(type, event);
306 }
307
308 static void __window_on_aux_message(int type, void *event, void *data)
309 {
310         appcore_multiwindow_base_window_on_aux_message(type, event);
311 }
312
313 EXPORT_API appcore_multiwindow_base_ops appcore_multiwindow_base_get_default_ops(void)
314 {
315         appcore_multiwindow_base_ops ops;
316
317         ops.base = appcore_base_get_default_ops();
318
319         /* override methods */
320         ops.base.create = __on_create;
321         ops.base.terminate = __on_terminate;
322         ops.base.receive = __on_receive;
323         ops.base.init = NULL;
324         ops.base.finish = NULL;
325         ops.base.run = NULL;
326         ops.base.exit = NULL;
327         ops.base.trim_memory = __on_trim_memory;
328
329         ops.window.show = __window_on_show;
330         ops.window.hide = __window_on_hide;
331         ops.window.lower = __window_on_lower;
332         ops.window.visibility = __window_on_visibility;
333         ops.window.pre_visibility = __window_on_pre_visibility;
334         ops.window.aux_message = __window_on_aux_message;
335
336         return ops;
337 }
338
339 EXPORT_API int appcore_multiwindow_base_on_receive(aul_type type, bundle *b)
340 {
341         appcore_base_on_receive(type, b);
342
343         return 0;
344 }
345
346 EXPORT_API int appcore_multiwindow_base_on_create(void)
347 {
348         appcore_base_on_create();
349         __add_ecore_events();
350
351         return 0;
352 }
353
354 EXPORT_API int appcore_multiwindow_base_on_terminate(void)
355 {
356         appcore_base_on_terminate();
357
358         return 0;
359 }
360
361 EXPORT_API int appcore_multiwindow_base_on_control(bundle *b)
362 {
363         appcore_base_on_control(b);
364
365         return 0;
366 }
367
368 EXPORT_API int appcore_multiwindow_base_on_trim_memory(void)
369 {
370         return appcore_base_on_trim_memory();
371 }