2 * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <sys/types.h>
23 #include <linux/limits.h>
25 #include <Ecore_Wl2.h>
26 #include <glib-object.h>
33 #include <bundle_internal.h>
35 #include "appcore_base.h"
36 #include "appcore_multiwindow_base.h"
37 #include "appcore_multiwindow_base_private.h"
39 appcore_multiwindow_base_context _appcore_mw_context;
40 static guint __flush_timer = 0;
42 static gboolean __flush_memory(gpointer data)
45 if (_appcore_mw_context.ops.base.trim_memory)
46 _appcore_mw_context.ops.base.trim_memory(_appcore_mw_context.data);
48 return G_SOURCE_REMOVE;
51 static void __add_flush_timer(void)
56 __flush_timer = g_timeout_add(5000, __flush_memory, NULL);
59 static void __remove_flush_timer(void)
64 g_source_remove(__flush_timer);
68 static Eina_Bool __stub_show_cb(void *data, int type, void *event)
70 if (_appcore_mw_context.ops.window.show)
71 _appcore_mw_context.ops.window.show(type, event, _appcore_mw_context.data);
73 return ECORE_CALLBACK_RENEW;
76 static Eina_Bool __stub_hide_cb(void *data, int type, void *event)
78 if (_appcore_mw_context.ops.window.hide)
79 _appcore_mw_context.ops.window.hide(type, event, _appcore_mw_context.data);
81 return ECORE_CALLBACK_RENEW;
84 static Eina_Bool __stub_visibility_cb(void *data, int type, void *event)
86 Ecore_Wl2_Event_Window_Visibility_Change *ev = event;
88 if (_appcore_mw_context.ops.window.visibility)
89 _appcore_mw_context.ops.window.visibility(type, event, _appcore_mw_context.data);
91 if (ev && ev->fully_obscured) {
92 if (!appcore_multiwindow_base_window_is_resumed())
95 __remove_flush_timer();
98 return ECORE_CALLBACK_RENEW;
101 static Eina_Bool __stub_lower_cb(void *data, int type, void *event)
103 if (_appcore_mw_context.ops.window.lower)
104 _appcore_mw_context.ops.window.lower(type, event, _appcore_mw_context.data);
106 return ECORE_CALLBACK_RENEW;
109 static Eina_Bool __stub_pre_visibility_cb(void *data, int type, void *event)
111 Ecore_Wl2_Event_Window_Pre_Visibility_Change *ev = event;
113 if (_appcore_mw_context.ops.window.pre_visibility)
114 _appcore_mw_context.ops.window.pre_visibility(type, event, _appcore_mw_context.data);
116 if (ev->type == ECORE_WL2_WINDOW_VISIBILITY_TYPE_PRE_UNOBSCURED)
117 __remove_flush_timer();
119 return ECORE_CALLBACK_RENEW;
122 static Eina_Bool __stub_aux_message_cb(void *data, int type, void *event)
124 if (_appcore_mw_context.ops.window.aux_message)
125 _appcore_mw_context.ops.window.aux_message(type, event, _appcore_mw_context.data);
127 return ECORE_CALLBACK_RENEW;
130 static void __add_ecore_events(void)
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");
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");
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");
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");
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");
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");
181 static void __remove_ecore_events(void)
183 if (_appcore_mw_context.hshow) {
184 ecore_event_handler_del(_appcore_mw_context.hshow);
185 _appcore_mw_context.hshow = NULL;
188 if (_appcore_mw_context.hhide) {
189 ecore_event_handler_del(_appcore_mw_context.hhide);
190 _appcore_mw_context.hhide = NULL;
193 if (_appcore_mw_context.hvchange) {
194 ecore_event_handler_del(_appcore_mw_context.hvchange);
195 _appcore_mw_context.hvchange = NULL;
198 if (_appcore_mw_context.hlower) {
199 ecore_event_handler_del(_appcore_mw_context.hlower);
200 _appcore_mw_context.hlower = NULL;
203 if (_appcore_mw_context.hpvchange) {
204 ecore_event_handler_del(_appcore_mw_context.hpvchange);
205 _appcore_mw_context.hpvchange = NULL;
208 if (_appcore_mw_context.hauxmsg) {
209 ecore_event_handler_del(_appcore_mw_context.hauxmsg);
210 _appcore_mw_context.hauxmsg = NULL;
214 EXPORT_API int appcore_multiwindow_base_init(appcore_multiwindow_base_ops ops, int argc, char **argv, void *data)
216 _appcore_mw_context.ops = ops;
217 _appcore_mw_context.data = data;
218 _appcore_mw_context.argc = argc;
219 _appcore_mw_context.argv = argv;
221 return appcore_base_init(ops.base, argc, argv, data);
224 static void __destroy_iter(gpointer data, gpointer user_data)
226 appcore_multiwindow_base_instance *inst = data;
231 appcore_multiwindow_base_instance_exit(inst);
234 static void __destroy_all(void)
236 g_list_foreach(_appcore_mw_context.instances, __destroy_iter, NULL);
239 static void __free_class(gpointer data)
241 appcore_multiwindow_base_class *cls = data;
247 EXPORT_API void appcore_multiwindow_base_fini(void)
250 g_list_free_full(_appcore_mw_context.classes, __free_class);
251 _appcore_mw_context.classes = NULL;
253 __remove_ecore_events();
257 EXPORT_API void appcore_multiwindow_base_exit(void)
259 if (_appcore_mw_context.ops.base.exit)
260 _appcore_mw_context.ops.base.exit(_appcore_mw_context.data);
263 static int __on_receive(aul_type type, bundle *b, void *data)
265 return appcore_multiwindow_base_on_receive(type, b);
268 static int __on_create(void *data)
270 return appcore_multiwindow_base_on_create();
273 static int __on_terminate(void *data)
275 return appcore_multiwindow_base_on_terminate();
278 static void __on_trim_memory(void *data)
280 appcore_multiwindow_base_on_trim_memory();
283 static void __window_on_show(int type, void *event, void *data)
285 appcore_multiwindow_base_window_on_show(type, event);
288 static void __window_on_hide(int type, void *event, void *data)
290 appcore_multiwindow_base_window_on_hide(type, event);
293 static void __window_on_lower(int type, void *event, void *data)
295 appcore_multiwindow_base_window_on_lower(type, event);
298 static void __window_on_visibility(int type, void *event, void *data)
300 appcore_multiwindow_base_window_on_visibility(type, event);
303 static void __window_on_pre_visibility(int type, void *event, void *data)
305 appcore_multiwindow_base_window_on_pre_visibility(type, event);
308 static void __window_on_aux_message(int type, void *event, void *data)
310 appcore_multiwindow_base_window_on_aux_message(type, event);
313 EXPORT_API appcore_multiwindow_base_ops appcore_multiwindow_base_get_default_ops(void)
315 appcore_multiwindow_base_ops ops;
317 ops.base = appcore_base_get_default_ops();
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;
326 ops.base.exit = NULL;
327 ops.base.trim_memory = __on_trim_memory;
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;
339 EXPORT_API int appcore_multiwindow_base_on_receive(aul_type type, bundle *b)
341 appcore_base_on_receive(type, b);
346 EXPORT_API int appcore_multiwindow_base_on_create(void)
348 appcore_base_on_create();
349 __add_ecore_events();
354 EXPORT_API int appcore_multiwindow_base_on_terminate(void)
356 appcore_base_on_terminate();
361 EXPORT_API int appcore_multiwindow_base_on_control(bundle *b)
363 appcore_base_on_control(b);
368 EXPORT_API int appcore_multiwindow_base_on_trim_memory(void)
370 return appcore_base_on_trim_memory();