Fix plugin parameter
[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, int argc, char **argv,
36                 unsigned int *hint);
37 static int (*__plugin_fini)(void);
38 static void *__handle;
39
40 static void __unload_appcore_ui_plugin(void)
41 {
42         if (__handle) {
43                 dlclose(__handle);
44                 __handle = NULL;
45         }
46
47         __plugin_init = NULL;
48         __plugin_fini = NULL;
49 }
50
51 static void __load_appcore_ui_plugin(void)
52 {
53         if (access(PATH_LIBAPPCORE_UI_PLUGIN, F_OK) != 0)
54                 return;
55
56         if (!__handle) {
57                 __handle = dlopen(PATH_LIBAPPCORE_UI_PLUGIN, RTLD_LAZY);
58                 if (!__handle) {
59                         LOGE("Failed to open %s", PATH_LIBAPPCORE_UI_PLUGIN);
60                         return;
61                 }
62         }
63
64         __plugin_init = dlsym(__handle, APPCORE_UI_PLUGIN_INIT);
65         if (!__plugin_init)
66                 LOGW("Failed to load %s symbol", APPCORE_UI_PLUGIN_INIT);
67
68         __plugin_fini = dlsym(__handle, APPCORE_UI_PLUGIN_FINI);
69         if (!__plugin_fini)
70                 LOGW("Failed to load %s symbol", APPCORE_UI_PLUGIN_FINI);
71 }
72
73 void appcore_ui_plugin_init(appcore_ui_base_ops *ops, int argc, char **argv,
74                 unsigned int *hint)
75 {
76         LOGI("[PLUGIN] init");
77
78         if (!__plugin_init && !__plugin_fini)
79                 __load_appcore_ui_plugin();
80
81         if (__plugin_init)
82                 __plugin_init(ops, argc, argv, hint);
83 }
84
85 void appcore_ui_plugin_fini(void)
86 {
87         LOGI("[PLUGIN] fini");
88
89         if (__plugin_fini)
90                 __plugin_fini();
91
92         __unload_appcore_ui_plugin();
93 }