Modified flushing memory logic
[platform/core/appfw/app-core.git] / src / efl_base / appcore_efl_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 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <stdbool.h>
22 #include <dlfcn.h>
23 #include <Elementary.h>
24 #include <vconf.h>
25
26 #include "appcore_efl_base_private.h"
27 #include "appcore_efl_base.h"
28
29 #define PATH_LIB_VC_ELM "/usr/lib/libvc-elm.so.0"
30
31 static bool __vc_elm_initialized;
32 static void *__vc_elm_handle;
33 static int (*__vc_elm_initialize)(void);
34 static int (*__vc_elm_deinitialize)(void);
35 static int (*__vc_elm_set_auto_register_mode)(int, int);
36
37 static void __unload_vc_elm(void)
38 {
39         if (!__vc_elm_handle)
40                 return;
41
42         __vc_elm_initialize = NULL;
43         __vc_elm_deinitialize = NULL;
44         __vc_elm_set_auto_register_mode = NULL;
45
46         dlclose(__vc_elm_handle);
47         __vc_elm_handle = NULL;
48 }
49
50 static int __load_vc_elm(void)
51 {
52         if (__vc_elm_handle)
53                 return 0;
54
55         if (access(PATH_LIB_VC_ELM, F_OK) != 0) {
56                 _ERR("Failed to access %s", PATH_LIB_VC_ELM);
57                 return -1;
58         }
59
60         __vc_elm_handle = dlopen(PATH_LIB_VC_ELM, RTLD_LAZY | RTLD_GLOBAL);
61         if (!__vc_elm_handle) {
62                 _ERR("Failed to open %s", PATH_LIB_VC_ELM);
63                 return -1;
64         }
65
66         __vc_elm_initialize = dlsym(__vc_elm_handle, "vc_elm_initialize");
67         if (!__vc_elm_initialize) {
68                 _ERR("Failed to load vc_elm_initialize");
69                 __unload_vc_elm();
70                 return -1;
71         }
72
73         __vc_elm_deinitialize = dlsym(__vc_elm_handle, "vc_elm_deinitialize");
74         if (!__vc_elm_deinitialize) {
75                 _ERR("Failed to load vc_elm_deinitialize");
76                 __unload_vc_elm();
77                 return -1;
78         }
79
80         __vc_elm_set_auto_register_mode = dlsym(__vc_elm_handle,
81                         "vc_elm_set_auto_register_mode");
82         if (!__vc_elm_set_auto_register_mode) {
83                 _ERR("Failed to load vc_elm_set_auto_register_mode");
84                 __unload_vc_elm();
85                 return -1;
86         }
87
88         return 0;
89 }
90
91 static void __vc_vtauto_changed_cb(keynode_t *key, void *data)
92 {
93         const char *name;
94         int vt_automode;
95
96         name = vconf_keynode_get_name(key);
97         if (!name || strcmp(name, VCONFKEY_VC_VOICE_TOUCH_AUTOMODE) != 0)
98                 return;
99
100         vt_automode = vconf_keynode_get_bool(key);
101         if (vt_automode) {
102                 if (!__vc_elm_initialized) {
103                         __vc_elm_initialize();
104                         __vc_elm_initialized = true;
105                 }
106                 __vc_elm_set_auto_register_mode(2, 0);
107         } else {
108                 __vc_elm_deinitialize();
109                 __vc_elm_initialized = false;
110         }
111 }
112
113 static void __vc_elm_init(void)
114 {
115         int vt_automode = 0;
116         int r;
117
118         r = __load_vc_elm();
119         if (r < 0)
120                 return;
121
122         vconf_notify_key_changed(VCONFKEY_VC_VOICE_TOUCH_AUTOMODE,
123                         __vc_vtauto_changed_cb, NULL);
124         vconf_get_bool(VCONFKEY_VC_VOICE_TOUCH_AUTOMODE, &vt_automode);
125         if (vt_automode) {
126                 if (!__vc_elm_initialized) {
127                         __vc_elm_initialize();
128                         __vc_elm_initialized = true;
129                 }
130                 __vc_elm_set_auto_register_mode(2, 0);
131         }
132 }
133
134 static void __vc_elm_finish(void)
135 {
136         vconf_ignore_key_changed(VCONFKEY_VC_VOICE_TOUCH_AUTOMODE,
137                         __vc_vtauto_changed_cb);
138         if (__vc_elm_initialized) {
139                 __vc_elm_deinitialize();
140                 __vc_elm_initialized = false;
141         }
142 }
143
144 static void __efl_app_init(int argc, char **argv, void *data)
145 {
146         int hint;
147         const char *hwacc;
148
149         elm_init(argc, argv);
150
151         hint = appcore_efl_base_get_hint();
152         if (hint & APPCORE_EFL_BASE_HINT_HW_ACC_CONTROL) {
153                 hwacc = getenv("HWACC");
154                 if (hwacc == NULL) {
155                         _DBG("elm_config_accel_preference_set is not called");
156                 } else if (strcmp(hwacc, "USE") == 0) {
157                         elm_config_accel_preference_set("hw");
158                         _DBG("elm_config_accel_preference_set : hw");
159                 } else if (strcmp(hwacc, "NOT_USE") == 0) {
160                         elm_config_accel_preference_set("none");
161                         _DBG("elm_config_accel_preference_set : none");
162                 } else {
163                         _DBG("elm_config_accel_preference_set is not called");
164                 }
165         }
166
167         /* VC voice touch setting */
168         if (!getenv("VC_ELM_INIT"))
169                 __vc_elm_init();
170 }
171
172 static void __efl_app_finish(void)
173 {
174         __vc_elm_finish();
175         unsetenv("VC_ELM_INIT");
176
177         elm_shutdown();
178
179         /* Check loader case */
180         if (getenv("AUL_LOADER_INIT")) {
181                 unsetenv("AUL_LOADER_INIT");
182                 elm_shutdown();
183         }
184 }
185
186 static void __efl_app_run(void *data)
187 {
188         elm_run();
189 }
190
191 static void __efl_app_exit(void *data)
192 {
193         elm_exit();
194 }
195
196 static void __efl_app_trim_memory(void *data)
197 {
198         _DBG("Trim memory");
199         elm_cache_all_flush();
200         appcore_base_on_trim_memory();
201 }
202
203 EXPORT_API int appcore_efl_base_init(appcore_efl_base_ops ops, int argc,
204                 char **argv, void *data, unsigned int hint)
205 {
206         return appcore_ui_base_init(ops.ui_base, argc, argv, data, hint);
207 }
208
209 EXPORT_API void appcore_efl_base_fini(void)
210 {
211         appcore_ui_base_fini();
212 }
213
214 EXPORT_API appcore_efl_base_ops appcore_efl_base_get_default_ops(void)
215 {
216         appcore_efl_base_ops ops;
217
218         ops.ui_base = appcore_ui_base_get_default_ops();
219
220         /* override methods */
221         ops.ui_base.base.init = __efl_app_init;
222         ops.ui_base.base.finish = __efl_app_finish;
223         ops.ui_base.base.run = __efl_app_run;
224         ops.ui_base.base.exit = __efl_app_exit;
225         ops.ui_base.base.trim_memory = __efl_app_trim_memory;
226
227         return ops;
228 }
229
230 EXPORT_API int appcore_efl_base_on_receive(aul_type type, bundle *b)
231 {
232         return appcore_ui_base_on_receive(type, b);
233 }
234
235 EXPORT_API int appcore_efl_base_on_create(void)
236 {
237         return appcore_ui_base_on_create();
238 }
239
240 EXPORT_API int appcore_efl_base_on_terminate(void)
241 {
242         return appcore_ui_base_on_terminate();
243 }
244
245 EXPORT_API int appcore_efl_base_on_pause(void)
246 {
247         return appcore_ui_base_on_pause();
248 }
249
250 EXPORT_API int appcore_efl_base_on_resume(void)
251 {
252         return appcore_ui_base_on_resume();
253 }
254
255 EXPORT_API int appcore_efl_base_on_control(bundle *b)
256 {
257         return appcore_ui_base_on_control(b);
258 }
259
260 EXPORT_API int appcore_efl_base_on_trim_memory(void)
261 {
262         return appcore_ui_base_on_trim_memory();
263 }
264
265 EXPORT_API void appcore_efl_base_window_on_show(int type, void *event)
266 {
267         appcore_ui_base_window_on_show(type, event);
268 }
269
270 EXPORT_API void appcore_efl_base_window_on_hide(int type, void *event)
271 {
272         appcore_ui_base_window_on_hide(type, event);
273 }
274
275 EXPORT_API void appcore_efl_base_window_on_lower(int type, void *event)
276 {
277         appcore_ui_base_window_on_lower(type, event);
278 }
279
280 EXPORT_API void appcore_efl_base_window_on_visibility(int type, void *event)
281 {
282         appcore_ui_base_window_on_visibility(type, event);
283 }
284
285 EXPORT_API void appcore_efl_base_pause(void)
286 {
287         appcore_ui_base_pause();
288 }
289
290 EXPORT_API void appcore_efl_base_resume(void)
291 {
292         appcore_ui_base_resume();
293 }
294
295 EXPORT_API bool appcore_efl_base_is_resumed(void)
296 {
297         return appcore_ui_base_is_resumed();
298 }
299
300 EXPORT_API void appcore_efl_base_exit(void)
301 {
302         appcore_ui_base_exit();
303 }
304
305 EXPORT_API void appcore_efl_base_group_add(void)
306 {
307         appcore_ui_base_group_add();
308 }
309
310 EXPORT_API void appcore_efl_base_group_remove(void)
311 {
312         appcore_ui_base_group_remove();
313 }
314
315 EXPORT_API unsigned int appcore_efl_base_get_main_window(void)
316 {
317         return appcore_ui_base_get_main_window();
318 }
319
320 EXPORT_API unsigned int appcore_efl_base_get_main_surface(void)
321 {
322         return appcore_ui_base_get_main_surface();
323 }
324
325 EXPORT_API int appcore_efl_base_get_hint(void)
326 {
327         return appcore_ui_base_get_hint();
328 }
329
330 EXPORT_API bool appcore_efl_base_get_bg_state(void)
331 {
332         return appcore_ui_base_get_bg_state();
333 }
334
335 EXPORT_API void appcore_efl_base_set_bg_state(bool bg_state)
336 {
337         appcore_ui_base_set_bg_state(bg_state);
338 }
339
340 EXPORT_API void appcore_efl_base_set_system_resource_reclaiming(bool enable)
341 {
342         appcore_ui_base_set_system_resource_reclaiming(enable);
343 }