Remove unloading vc-elm library
[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         __vc_elm_init();
169 }
170
171 static void __efl_app_finish(void)
172 {
173         __vc_elm_finish();
174
175         elm_shutdown();
176
177         /* Check loader case */
178         if (getenv("AUL_LOADER_INIT")) {
179                 unsetenv("AUL_LOADER_INIT");
180                 elm_shutdown();
181         }
182 }
183
184 static void __efl_app_run(void *data)
185 {
186         elm_run();
187 }
188
189 static void __efl_app_exit(void *data)
190 {
191         elm_exit();
192 }
193
194 EXPORT_API int appcore_efl_base_init(appcore_efl_base_ops ops, int argc,
195                 char **argv, void *data, unsigned int hint)
196 {
197         return appcore_ui_base_init(ops.ui_base, argc, argv, data, hint);
198 }
199
200 EXPORT_API void appcore_efl_base_fini(void)
201 {
202         appcore_ui_base_fini();
203 }
204
205 EXPORT_API appcore_efl_base_ops appcore_efl_base_get_default_ops(void)
206 {
207         appcore_efl_base_ops ops;
208
209         ops.ui_base = appcore_ui_base_get_default_ops();
210
211         /* override methods */
212         ops.ui_base.base.init = __efl_app_init;
213         ops.ui_base.base.finish = __efl_app_finish;
214         ops.ui_base.base.run = __efl_app_run;
215         ops.ui_base.base.exit = __efl_app_exit;
216
217         return ops;
218 }
219
220 EXPORT_API int appcore_efl_base_on_receive(aul_type type, bundle *b)
221 {
222         return appcore_ui_base_on_receive(type, b);
223 }
224
225 EXPORT_API int appcore_efl_base_on_create(void)
226 {
227         return appcore_ui_base_on_create();
228 }
229
230 EXPORT_API int appcore_efl_base_on_terminate(void)
231 {
232         return appcore_ui_base_on_terminate();
233 }
234
235 EXPORT_API int appcore_efl_base_on_pause(void)
236 {
237         return appcore_ui_base_on_pause();
238 }
239
240 EXPORT_API int appcore_efl_base_on_resume(void)
241 {
242         return appcore_ui_base_on_resume();
243 }
244
245 EXPORT_API int appcore_efl_base_on_control(bundle *b)
246 {
247         return appcore_ui_base_on_control(b);
248 }
249
250 EXPORT_API void appcore_efl_base_window_on_show(int type, void *event)
251 {
252         appcore_ui_base_window_on_show(type, event);
253 }
254
255 EXPORT_API void appcore_efl_base_window_on_hide(int type, void *event)
256 {
257         appcore_ui_base_window_on_hide(type, event);
258 }
259
260 EXPORT_API void appcore_efl_base_window_on_lower(int type, void *event)
261 {
262         appcore_ui_base_window_on_lower(type, event);
263 }
264
265 EXPORT_API void appcore_efl_base_window_on_visibility(int type, void *event)
266 {
267         appcore_ui_base_window_on_visibility(type, event);
268 }
269
270 EXPORT_API void appcore_efl_base_pause(void)
271 {
272         appcore_ui_base_pause();
273 }
274
275 EXPORT_API void appcore_efl_base_resume(void)
276 {
277         appcore_ui_base_resume();
278 }
279
280 EXPORT_API bool appcore_efl_base_is_resumed(void)
281 {
282         return appcore_ui_base_is_resumed();
283 }
284
285 EXPORT_API void appcore_efl_base_exit(void)
286 {
287         appcore_ui_base_exit();
288 }
289
290 EXPORT_API void appcore_efl_base_group_add(void)
291 {
292         appcore_ui_base_group_add();
293 }
294
295 EXPORT_API void appcore_efl_base_group_remove(void)
296 {
297         appcore_ui_base_group_remove();
298 }
299
300 EXPORT_API unsigned int appcore_efl_base_get_main_window(void)
301 {
302         return appcore_ui_base_get_main_window();
303 }
304
305 EXPORT_API unsigned int appcore_efl_base_get_main_surface(void)
306 {
307         return appcore_ui_base_get_main_surface();
308 }
309
310 EXPORT_API int appcore_efl_base_get_hint(void)
311 {
312         return appcore_ui_base_get_hint();
313 }
314
315 EXPORT_API bool appcore_efl_base_get_bg_state(void)
316 {
317         return appcore_ui_base_get_bg_state();
318 }
319
320 EXPORT_API void appcore_efl_base_set_bg_state(bool bg_state)
321 {
322         appcore_ui_base_set_bg_state(bg_state);
323 }