Separate appcore-ui package from appcore-efl package
[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 EXPORT_API int appcore_multiwindow_base_init(appcore_multiwindow_base_ops ops, int argc, char **argv, void *data)
75 {
76         _appcore_mw_context.ops = ops;
77         _appcore_mw_context.data = data;
78         _appcore_mw_context.argc = argc;
79         _appcore_mw_context.argv = argv;
80
81         _appcore_mw_context.hshow = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __stub_show_cb, NULL);
82         _appcore_mw_context.hhide = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __stub_hide_cb, NULL);
83         _appcore_mw_context.hvchange = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE,
84                                 __stub_visibility_cb, NULL);
85         _appcore_mw_context.hlower = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __stub_lower_cb, NULL);
86
87         return appcore_base_init(ops.base, argc, argv, data);
88 }
89
90 static void __destroy_iter(gpointer data, gpointer user_data)
91 {
92         appcore_multiwindow_base_instance *inst = data;
93
94         if (!inst)
95                 return;
96
97         appcore_multiwindow_base_instance_exit(inst);
98 }
99
100 static void __destroy_all(void)
101 {
102         g_list_foreach(_appcore_mw_context.instances, __destroy_iter, NULL);
103 }
104
105 static void __free_class(gpointer data)
106 {
107         appcore_multiwindow_base_class *cls = data;
108
109         free(cls->id);
110         free(cls);
111 }
112
113 EXPORT_API void appcore_multiwindow_base_fini(void)
114 {
115         __destroy_all();
116         g_list_free_full(_appcore_mw_context.classes, __free_class);
117         _appcore_mw_context.classes = NULL;
118
119         if (_appcore_mw_context.hshow) {
120                 ecore_event_handler_del(_appcore_mw_context.hshow);
121                 _appcore_mw_context.hshow = NULL;
122         }
123
124         if (_appcore_mw_context.hhide) {
125                 ecore_event_handler_del(_appcore_mw_context.hhide);
126                 _appcore_mw_context.hhide = NULL;
127         }
128
129         if (_appcore_mw_context.hvchange) {
130                 ecore_event_handler_del(_appcore_mw_context.hvchange);
131                 _appcore_mw_context.hvchange = NULL;
132         }
133
134         if (_appcore_mw_context.hlower) {
135                 ecore_event_handler_del(_appcore_mw_context.hlower);
136                 _appcore_mw_context.hlower = NULL;
137         }
138
139         appcore_base_fini();
140 }
141
142 EXPORT_API void appcore_multiwindow_base_exit(void)
143 {
144         if (_appcore_mw_context.ops.base.exit)
145                 _appcore_mw_context.ops.base.exit(_appcore_mw_context.data);
146 }
147
148 static int __on_receive(aul_type type, bundle *b, void *data)
149 {
150         return appcore_multiwindow_base_on_receive(type, b);
151 }
152
153 static int __on_create(void *data)
154 {
155         return appcore_multiwindow_base_on_create();
156 }
157
158 static int __on_terminate(void *data)
159 {
160         return appcore_multiwindow_base_on_terminate();
161 }
162
163 static void __window_on_show(int type, void *event, void *data)
164 {
165         appcore_multiwindow_base_window_on_show(type, event);
166 }
167
168 static void __window_on_hide(int type, void *event, void *data)
169 {
170         appcore_multiwindow_base_window_on_hide(type, event);
171 }
172
173 static void __window_on_lower(int type, void *event, void *data)
174 {
175         appcore_multiwindow_base_window_on_lower(type, event);
176 }
177
178 static void __window_on_visibility(int type, void *event, void *data)
179 {
180         appcore_multiwindow_base_window_on_visibility(type, event);
181 }
182
183 static void __run(void *data)
184 {
185         elm_run();
186 }
187
188 static void __exit(void *data)
189 {
190         elm_exit();
191 }
192
193 static int __init(int argc, char **argv, void *data)
194 {
195         elm_init(argc, argv);
196
197         return 0;
198 }
199
200 static void __finish(void)
201 {
202         elm_shutdown();
203
204         /* Check loader case */
205         if (getenv("AUL_LOADER_INIT")) {
206                 unsetenv("AUL_LOADER_INIT");
207                 elm_shutdown();
208         }
209 }
210
211 EXPORT_API appcore_multiwindow_base_ops appcore_multiwindow_base_get_default_ops(void)
212 {
213         appcore_multiwindow_base_ops ops;
214
215         ops.base = appcore_base_get_default_ops();
216
217         /* override methods */
218         ops.base.create = __on_create;
219         ops.base.terminate = __on_terminate;
220         ops.base.receive = __on_receive;
221         ops.base.init = __init;
222         ops.base.finish = __finish;
223         ops.base.run = __run;
224         ops.base.exit = __exit;
225
226         ops.window.show = __window_on_show;
227         ops.window.hide = __window_on_hide;
228         ops.window.lower = __window_on_lower;
229         ops.window.visibility = __window_on_visibility;
230
231         return ops;
232 }
233
234 EXPORT_API int appcore_multiwindow_base_on_receive(aul_type type, bundle *b)
235 {
236         appcore_base_on_receive(type, b);
237
238         return 0;
239 }
240
241 EXPORT_API int appcore_multiwindow_base_on_create(void)
242 {
243         appcore_base_on_create();
244
245         return 0;
246 }
247
248 EXPORT_API int appcore_multiwindow_base_on_terminate(void)
249 {
250         appcore_base_on_terminate();
251
252         return 0;
253 }
254
255 EXPORT_API int appcore_multiwindow_base_on_control(bundle *b)
256 {
257         appcore_base_on_control(b);
258
259         return 0;
260 }
261