ca72c671ffa5772c2eb392c9687b451b11b22545
[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_Wayland.h>
26 #include <Elementary.h>
27 #include <glib-object.h>
28 #include <malloc.h>
29 #include <glib.h>
30 #include <gio/gio.h>
31 #include <stdbool.h>
32 #include <aul.h>
33 #include <aul_svc.h>
34 #include <bundle_internal.h>
35
36 #include "appcore_base.h"
37 #include "appcore_multiwindow_base.h"
38 #include "appcore_multiwindow_base_private.h"
39
40 appcore_multiwindow_base_context _appcore_mw_context;
41
42 static Eina_Bool __stub_show_cb(void *data, int type, void *event)
43 {
44         if (_appcore_mw_context.ops.window.show)
45                 _appcore_mw_context.ops.window.show(type, event, _appcore_mw_context.data);
46
47         return ECORE_CALLBACK_RENEW;
48 }
49
50 static Eina_Bool __stub_hide_cb(void *data, int type, void *event)
51 {
52         if (_appcore_mw_context.ops.window.hide)
53                 _appcore_mw_context.ops.window.hide(type, event, _appcore_mw_context.data);
54
55         return ECORE_CALLBACK_RENEW;
56 }
57
58 static Eina_Bool __stub_visibility_cb(void *data, int type, void *event)
59 {
60         if (_appcore_mw_context.ops.window.visibility)
61                 _appcore_mw_context.ops.window.visibility(type, event, _appcore_mw_context.data);
62
63         return ECORE_CALLBACK_RENEW;
64 }
65
66 static Eina_Bool __stub_lower_cb(void *data, int type, void *event)
67 {
68         if (_appcore_mw_context.ops.window.lower)
69                 _appcore_mw_context.ops.window.lower(type, event, _appcore_mw_context.data);
70
71         return ECORE_CALLBACK_RENEW;
72 }
73
74 static Eina_Bool __stub_pre_visibility_cb(void *data, int type, void *event)
75 {
76         if (_appcore_mw_context.ops.window.pre_visibility)
77                 _appcore_mw_context.ops.window.pre_visibility(type, event, _appcore_mw_context.data);
78
79         return ECORE_CALLBACK_RENEW;
80 }
81
82 EXPORT_API int appcore_multiwindow_base_init(appcore_multiwindow_base_ops ops, int argc, char **argv, void *data)
83 {
84         _appcore_mw_context.ops = ops;
85         _appcore_mw_context.data = data;
86         _appcore_mw_context.argc = argc;
87         _appcore_mw_context.argv = argv;
88
89         return appcore_base_init(ops.base, argc, argv, data);
90 }
91
92 static void __destroy_iter(gpointer data, gpointer user_data)
93 {
94         appcore_multiwindow_base_instance *inst = data;
95
96         if (!inst)
97                 return;
98
99         appcore_multiwindow_base_instance_exit(inst);
100 }
101
102 static void __destroy_all(void)
103 {
104         g_list_foreach(_appcore_mw_context.instances, __destroy_iter, NULL);
105 }
106
107 static void __free_class(gpointer data)
108 {
109         appcore_multiwindow_base_class *cls = data;
110
111         free(cls->id);
112         free(cls);
113 }
114
115 EXPORT_API void appcore_multiwindow_base_fini(void)
116 {
117         __destroy_all();
118         g_list_free_full(_appcore_mw_context.classes, __free_class);
119         _appcore_mw_context.classes = NULL;
120
121         if (_appcore_mw_context.hshow) {
122                 ecore_event_handler_del(_appcore_mw_context.hshow);
123                 _appcore_mw_context.hshow = NULL;
124         }
125
126         if (_appcore_mw_context.hhide) {
127                 ecore_event_handler_del(_appcore_mw_context.hhide);
128                 _appcore_mw_context.hhide = NULL;
129         }
130
131         if (_appcore_mw_context.hvchange) {
132                 ecore_event_handler_del(_appcore_mw_context.hvchange);
133                 _appcore_mw_context.hvchange = NULL;
134         }
135
136         if (_appcore_mw_context.hlower) {
137                 ecore_event_handler_del(_appcore_mw_context.hlower);
138                 _appcore_mw_context.hlower = NULL;
139         }
140
141         if (_appcore_mw_context.hpvchange) {
142                 ecore_event_handler_del(_appcore_mw_context.hpvchange);
143                 _appcore_mw_context.hpvchange = NULL;
144         }
145
146         appcore_base_fini();
147 }
148
149 EXPORT_API void appcore_multiwindow_base_exit(void)
150 {
151         if (_appcore_mw_context.ops.base.exit)
152                 _appcore_mw_context.ops.base.exit(_appcore_mw_context.data);
153 }
154
155 static int __on_receive(aul_type type, bundle *b, void *data)
156 {
157         return appcore_multiwindow_base_on_receive(type, b);
158 }
159
160 static int __on_create(void *data)
161 {
162         return appcore_multiwindow_base_on_create();
163 }
164
165 static int __on_terminate(void *data)
166 {
167         return appcore_multiwindow_base_on_terminate();
168 }
169
170 static void __window_on_show(int type, void *event, void *data)
171 {
172         appcore_multiwindow_base_window_on_show(type, event);
173 }
174
175 static void __window_on_hide(int type, void *event, void *data)
176 {
177         appcore_multiwindow_base_window_on_hide(type, event);
178 }
179
180 static void __window_on_lower(int type, void *event, void *data)
181 {
182         appcore_multiwindow_base_window_on_lower(type, event);
183 }
184
185 static void __window_on_visibility(int type, void *event, void *data)
186 {
187         appcore_multiwindow_base_window_on_visibility(type, event);
188 }
189
190 static void __window_on_pre_visibility(int type, void *event, void *data)
191 {
192         appcore_multiwindow_base_window_on_pre_visibility(type, event);
193 }
194
195 static void __run(void *data)
196 {
197         elm_run();
198 }
199
200 static void __exit(void *data)
201 {
202         elm_exit();
203 }
204
205 static int __init(int argc, char **argv, void *data)
206 {
207         elm_init(argc, argv);
208
209         return 0;
210 }
211
212 static void __finish(void)
213 {
214         elm_shutdown();
215
216         /* Check loader case */
217         if (getenv("AUL_LOADER_INIT")) {
218                 unsetenv("AUL_LOADER_INIT");
219                 elm_shutdown();
220         }
221 }
222
223 EXPORT_API appcore_multiwindow_base_ops appcore_multiwindow_base_get_default_ops(void)
224 {
225         appcore_multiwindow_base_ops ops;
226
227         ops.base = appcore_base_get_default_ops();
228
229         /* override methods */
230         ops.base.create = __on_create;
231         ops.base.terminate = __on_terminate;
232         ops.base.receive = __on_receive;
233         ops.base.init = __init;
234         ops.base.finish = __finish;
235         ops.base.run = __run;
236         ops.base.exit = __exit;
237
238         ops.window.show = __window_on_show;
239         ops.window.hide = __window_on_hide;
240         ops.window.lower = __window_on_lower;
241         ops.window.visibility = __window_on_visibility;
242         ops.window.pre_visibility = __window_on_pre_visibility;
243
244         return ops;
245 }
246
247 EXPORT_API int appcore_multiwindow_base_on_receive(aul_type type, bundle *b)
248 {
249         appcore_base_on_receive(type, b);
250
251         return 0;
252 }
253
254 EXPORT_API int appcore_multiwindow_base_on_create(void)
255 {
256         appcore_base_on_create();
257
258         _appcore_mw_context.hshow = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __stub_show_cb, NULL);
259         _appcore_mw_context.hhide = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __stub_hide_cb, NULL);
260         _appcore_mw_context.hvchange = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE,
261                         __stub_visibility_cb, NULL);
262         _appcore_mw_context.hlower = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __stub_lower_cb, NULL);
263         _appcore_mw_context.hpvchange = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_PRE_VISIBILITY_CHANGE,
264                         __stub_pre_visibility_cb, NULL);
265
266         return 0;
267 }
268
269 EXPORT_API int appcore_multiwindow_base_on_terminate(void)
270 {
271         appcore_base_on_terminate();
272
273         return 0;
274 }
275
276 EXPORT_API int appcore_multiwindow_base_on_control(bundle *b)
277 {
278         appcore_base_on_control(b);
279
280         return 0;
281 }
282