Release version 1.18.7
[platform/core/appfw/app-core.git] / legacy / 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 <glib.h>
24 #include <Elementary.h>
25 #include <vconf.h>
26 #include <ttrace.h>
27
28 #include "appcore_efl_base_private.h"
29 #include "appcore_efl_base.h"
30
31 #define PATH_LIB_VC_ELM "/usr/lib/libvc-elm.so.0"
32
33 static bool __vc_elm_initialized;
34 static void *__vc_elm_handle;
35 static int (*__vc_elm_initialize)(void);
36 static int (*__vc_elm_deinitialize)(void);
37 static int (*__vc_elm_set_auto_register_mode)(int, int);
38 static GThread *__vc_elm_thread;
39
40 static void __unload_vc_elm(void)
41 {
42         if (!__vc_elm_handle)
43                 return;
44
45         __vc_elm_initialize = NULL;
46         __vc_elm_deinitialize = NULL;
47         __vc_elm_set_auto_register_mode = NULL;
48
49         dlclose(__vc_elm_handle);
50         __vc_elm_handle = NULL;
51 }
52
53 static int __load_vc_elm(void)
54 {
55         _DBG("Load voice-control-elm");
56
57         if (__vc_elm_handle) {
58                 _DBG("Already exists");
59                 return 0;
60         }
61
62         if (access(PATH_LIB_VC_ELM, F_OK) != 0) {
63                 _ERR("Failed to access %s", PATH_LIB_VC_ELM);
64                 return -1;
65         }
66
67         __vc_elm_handle = dlopen(PATH_LIB_VC_ELM, RTLD_LAZY | RTLD_LOCAL);
68         if (!__vc_elm_handle) {
69                 _ERR("Failed to open %s", PATH_LIB_VC_ELM);
70                 return -1;
71         }
72
73         __vc_elm_initialize = dlsym(__vc_elm_handle, "vc_elm_initialize");
74         if (!__vc_elm_initialize) {
75                 _ERR("Failed to load vc_elm_initialize");
76                 __unload_vc_elm();
77                 return -1;
78         }
79
80         __vc_elm_deinitialize = dlsym(__vc_elm_handle, "vc_elm_deinitialize");
81         if (!__vc_elm_deinitialize) {
82                 _ERR("Failed to load vc_elm_deinitialize");
83                 __unload_vc_elm();
84                 return -1;
85         }
86
87         __vc_elm_set_auto_register_mode = dlsym(__vc_elm_handle,
88                         "vc_elm_set_auto_register_mode");
89         if (!__vc_elm_set_auto_register_mode) {
90                 _ERR("Failed to load vc_elm_set_auto_register_mode");
91                 __unload_vc_elm();
92                 return -1;
93         }
94
95         return 0;
96 }
97
98 static void __vc_vtauto_changed_cb(keynode_t *key, void *data)
99 {
100         const char *name;
101         int vt_automode;
102
103         name = vconf_keynode_get_name(key);
104         if (!name || strcmp(name, VCONFKEY_VC_VOICE_TOUCH_AUTOMODE) != 0)
105                 return;
106
107         vt_automode = vconf_keynode_get_bool(key);
108         if (vt_automode) {
109                 if (!__vc_elm_initialized) {
110                         __vc_elm_initialize();
111                         __vc_elm_initialized = true;
112                 }
113                 __vc_elm_set_auto_register_mode(2, 0);
114         } else {
115                 __vc_elm_deinitialize();
116                 __vc_elm_initialized = false;
117         }
118 }
119
120 static void __vc_elm_init(void)
121 {
122         int vt_automode = 0;
123
124         if (!__vc_elm_handle)
125                 return;
126
127         vconf_notify_key_changed(VCONFKEY_VC_VOICE_TOUCH_AUTOMODE,
128                         __vc_vtauto_changed_cb, NULL);
129         vconf_get_bool(VCONFKEY_VC_VOICE_TOUCH_AUTOMODE, &vt_automode);
130         if (vt_automode) {
131                 if (!__vc_elm_initialized) {
132                         __vc_elm_initialize();
133                         __vc_elm_initialized = true;
134                 }
135                 __vc_elm_set_auto_register_mode(2, 0);
136         }
137 }
138
139 static void __vc_elm_finish(void)
140 {
141         if (!__vc_elm_handle)
142                 return;
143
144         vconf_ignore_key_changed(VCONFKEY_VC_VOICE_TOUCH_AUTOMODE,
145                         __vc_vtauto_changed_cb);
146         if (__vc_elm_initialized) {
147                 __vc_elm_deinitialize();
148                 __vc_elm_initialized = false;
149         }
150 }
151
152 static gboolean __init_vc_elm(gpointer data)
153 {
154         _DBG("Initialize vc-elm");
155         /* Postpone initialization to improve app launching performance */
156         /* VC voice touch setting */
157         __vc_elm_init();
158
159         return G_SOURCE_REMOVE;
160 }
161
162 static gpointer __vc_elm_loader(gpointer data)
163 {
164         int r = 0;
165         int retry_count = 3;
166
167         do {
168                 r = __load_vc_elm();
169                 if (r == 0) {
170                         g_idle_add(__init_vc_elm, NULL);
171                         break;
172                 }
173         } while (retry_count--);
174         LOGW("[vc-elm-loader] Result: %d", r);
175
176         return GINT_TO_POINTER(r);
177 }
178
179 static void __efl_app_init(int argc, char **argv, void *data)
180 {
181         int hint;
182         const char *hwacc;
183         int vt_automode = 0;
184
185         traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:ELM_INIT");
186         elm_init(argc, argv);
187         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
188
189         traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:ELM_CONFIG_SET");
190         hint = appcore_efl_base_get_hint();
191         if ((hint & APPCORE_EFL_BASE_HINT_HW_ACC_CONTROL) &&
192                         !getenv("AUL_HWACC")) {
193                 hwacc = getenv("HWACC");
194                 if (hwacc == NULL) {
195                         _DBG("elm_config_accel_preference_set is not called");
196                 } else if (strcmp(hwacc, "USE") == 0) {
197                         elm_config_accel_preference_set("hw");
198                         _DBG("elm_config_accel_preference_set : hw");
199                 } else if (strcmp(hwacc, "NOT_USE") == 0) {
200                         elm_config_accel_preference_set("none");
201                         _DBG("elm_config_accel_preference_set : none");
202                 } else {
203                         _DBG("elm_config_accel_preference_set is not called");
204                 }
205         }
206         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
207
208         traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:VC_ELM_CHECK");
209         vconf_get_bool(VCONFKEY_VC_VOICE_TOUCH_AUTOMODE, &vt_automode);
210         if (vt_automode) {
211                 __vc_elm_thread = g_thread_new("vc-elm-loader",
212                                 __vc_elm_loader, NULL);
213         }
214         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
215 }
216
217 static void __efl_app_finish(void)
218 {
219         const char *env;
220         gpointer r;
221
222         __vc_elm_finish();
223         if (__vc_elm_thread) {
224                 r = g_thread_join(__vc_elm_thread);
225                 __vc_elm_thread = NULL;
226                 _DBG("vc-elm-loader. result(%d)", GPOINTER_TO_INT(r));
227         }
228
229         elm_shutdown();
230
231         /* Check loader case */
232         env = getenv("AUL_LOADER_INIT");
233         if (env && env[0] == '1') {
234                 setenv("AUL_LOADER_INIT", "0", 1);
235                 elm_shutdown();
236         }
237 }
238
239 static void __efl_app_run(void *data)
240 {
241         elm_run();
242 }
243
244 static void __efl_app_exit(void *data)
245 {
246         elm_exit();
247 }
248
249 static void __efl_app_trim_memory(void *data)
250 {
251         _DBG("Trim memory");
252         elm_cache_all_flush();
253         appcore_base_on_trim_memory();
254 }
255
256 EXPORT_API int appcore_efl_base_init(appcore_efl_base_ops ops, int argc,
257                 char **argv, void *data, unsigned int hint)
258 {
259         return appcore_ui_base_init(ops.ui_base, argc, argv, data, hint);
260 }
261
262 EXPORT_API void appcore_efl_base_fini(void)
263 {
264         appcore_ui_base_fini();
265 }
266
267 EXPORT_API appcore_efl_base_ops appcore_efl_base_get_default_ops(void)
268 {
269         appcore_efl_base_ops ops;
270
271         ops.ui_base = appcore_ui_base_get_default_ops();
272
273         /* override methods */
274         ops.ui_base.base.init = __efl_app_init;
275         ops.ui_base.base.finish = __efl_app_finish;
276         ops.ui_base.base.run = __efl_app_run;
277         ops.ui_base.base.exit = __efl_app_exit;
278         ops.ui_base.base.trim_memory = __efl_app_trim_memory;
279
280         return ops;
281 }
282
283 EXPORT_API int appcore_efl_base_on_receive(aul_type type, bundle *b)
284 {
285         return appcore_ui_base_on_receive(type, b);
286 }
287
288 EXPORT_API int appcore_efl_base_on_create(void)
289 {
290         return appcore_ui_base_on_create();
291 }
292
293 EXPORT_API int appcore_efl_base_on_terminate(void)
294 {
295         return appcore_ui_base_on_terminate();
296 }
297
298 EXPORT_API int appcore_efl_base_on_pause(void)
299 {
300         return appcore_ui_base_on_pause();
301 }
302
303 EXPORT_API int appcore_efl_base_on_resume(void)
304 {
305         return appcore_ui_base_on_resume();
306 }
307
308 EXPORT_API int appcore_efl_base_on_control(bundle *b)
309 {
310         return appcore_ui_base_on_control(b);
311 }
312
313 EXPORT_API int appcore_efl_base_on_trim_memory(void)
314 {
315         return appcore_ui_base_on_trim_memory();
316 }
317
318 EXPORT_API void appcore_efl_base_window_on_show(int type, void *event)
319 {
320         appcore_ui_base_window_on_show(type, event);
321 }
322
323 EXPORT_API void appcore_efl_base_window_on_hide(int type, void *event)
324 {
325         appcore_ui_base_window_on_hide(type, event);
326 }
327
328 EXPORT_API void appcore_efl_base_window_on_lower(int type, void *event)
329 {
330         appcore_ui_base_window_on_lower(type, event);
331 }
332
333 EXPORT_API void appcore_efl_base_window_on_visibility(int type, void *event)
334 {
335         appcore_ui_base_window_on_visibility(type, event);
336 }
337
338 EXPORT_API void appcore_efl_base_window_on_pre_visibility(int type, void *event)
339 {
340         appcore_ui_base_window_on_pre_visibility(type, event);
341 }
342
343 EXPORT_API void appcore_efl_base_window_on_aux_message(int type, void *event)
344 {
345         appcore_ui_base_window_on_aux_message(type, event);
346 }
347
348 EXPORT_API void appcore_efl_base_pause(void)
349 {
350         appcore_ui_base_pause();
351 }
352
353 EXPORT_API void appcore_efl_base_resume(void)
354 {
355         appcore_ui_base_resume();
356 }
357
358 EXPORT_API bool appcore_efl_base_is_resumed(void)
359 {
360         return appcore_ui_base_is_resumed();
361 }
362
363 EXPORT_API void appcore_efl_base_exit(void)
364 {
365         appcore_ui_base_exit();
366 }
367
368 EXPORT_API void appcore_efl_base_group_add(void)
369 {
370         appcore_ui_base_group_add();
371 }
372
373 EXPORT_API void appcore_efl_base_group_remove(void)
374 {
375         appcore_ui_base_group_remove();
376 }
377
378 EXPORT_API unsigned int appcore_efl_base_get_main_window(void)
379 {
380         return appcore_ui_base_get_main_window();
381 }
382
383 EXPORT_API unsigned int appcore_efl_base_get_main_surface(void)
384 {
385         return appcore_ui_base_get_main_surface();
386 }
387
388 EXPORT_API int appcore_efl_base_get_hint(void)
389 {
390         return appcore_ui_base_get_hint();
391 }
392
393 EXPORT_API bool appcore_efl_base_get_bg_state(void)
394 {
395         return appcore_ui_base_get_bg_state();
396 }
397
398 EXPORT_API void appcore_efl_base_set_bg_state(bool bg_state)
399 {
400         appcore_ui_base_set_bg_state(bg_state);
401 }
402
403 EXPORT_API void appcore_efl_base_set_system_resource_reclaiming(bool enable)
404 {
405         appcore_ui_base_set_system_resource_reclaiming(enable);
406 }