f25b60e5890d95d214be3cc524c73425d48fee00
[platform/core/appfw/app-core.git] / src / ui_base / appcore_ui_plugin.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 <unistd.h>
19 #include <dlfcn.h>
20 #include <dlog.h>
21
22 #include "appcore_ui_plugin.h"
23
24 #ifdef LOG_TAG
25 #undef LOG_TAG
26 #endif
27
28 #define LOG_TAG "APP_CORE_UI_PLUGIN"
29
30 #define PATH_LIBAPPCORE_UI_PLUGIN \
31         "/usr/share/appcore/plugins/libappcore-ui-plugin.so"
32 #define APPCORE_UI_PLUGIN_INIT "APPCORE_UI_PLUGIN_INIT"
33 #define APPCORE_UI_PLUGIN_FINI "APPCORE_UI_PLUGIN_FINI"
34
35 static int (*__plugin_init)(appcore_ui_base_ops *ops, unsigned int *hint);
36 static int (*__plugin_fini)(void);
37 static void *__handle;
38
39 static void __unload_appcore_ui_plugin(void)
40 {
41         if (__handle) {
42                 dlclose(__handle);
43                 __handle = NULL;
44         }
45
46         __plugin_init = NULL;
47         __plugin_fini = NULL;
48 }
49
50 static void __load_appcore_ui_plugin(void)
51 {
52         if (access(PATH_LIBAPPCORE_UI_PLUGIN, F_OK) != 0)
53                 return;
54
55         if (!__handle) {
56                 __handle = dlopen(PATH_LIBAPPCORE_UI_PLUGIN, RTLD_LAZY);
57                 if (!__handle) {
58                         LOGE("Failed to open %s", PATH_LIBAPPCORE_UI_PLUGIN);
59                         return;
60                 }
61         }
62
63         __plugin_init = dlsym(__handle, APPCORE_UI_PLUGIN_INIT);
64         if (!__plugin_init)
65                 LOGW("Failed to load %s symbol", APPCORE_UI_PLUGIN_INIT);
66
67         __plugin_fini = dlsym(__handle, APPCORE_UI_PLUGIN_FINI);
68         if (!__plugin_fini)
69                 LOGW("Failed to load %s symbol", APPCORE_UI_PLUGIN_FINI);
70 }
71
72 void appcore_ui_plugin_init(appcore_ui_base_ops *ops, unsigned int *hint)
73 {
74         LOGI("[PLUGIN] init");
75
76         if (!__plugin_init && !__plugin_fini)
77                 __load_appcore_ui_plugin();
78
79         if (__plugin_init)
80                 __plugin_init(ops, hint);
81 }
82
83 void appcore_ui_plugin_fini(void)
84 {
85         LOGI("[PLUGIN] fini");
86
87         if (__plugin_fini)
88                 __plugin_fini();
89
90         __unload_appcore_ui_plugin();
91 }