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