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