packaging: Update dependencies and bump version to 0.9.23
[profile/ivi/ico-uxf-weston-plugin.git] / src / ico_plugin_loader.c
1 /*
2  * Copyright © 2010-2011 Intel Corporation
3  * Copyright © 2008-2011 Kristian Høgsberg
4  * Copyright © 2013-2014 TOYOTA MOTOR CORPORATION.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and
7  * its documentation for any purpose is hereby granted without fee, provided
8  * that the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of the copyright holders not be used in
11  * advertising or publicity pertaining to distribution of the software
12  * without specific, written prior permission.  The copyright holders make
13  * no representations about the suitability of this software for any
14  * purpose.  It is provided "as is" without express or implied warranty.
15  *
16  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  */
24 /**
25  * @brief   Weston(Wayland) Plugin Loader
26  * @brief   Load the Weston plugins, because plugin loader of main body of Weston
27  * @brief   cannot use other plugin functions by a other plugin.
28  *
29  * @date    Feb-21-2014
30  */
31
32 #define _GNU_SOURCE
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <stdbool.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <dlfcn.h>
40 #include <sys/time.h>
41 #include <time.h>
42
43 #include <weston/compositor.h>
44 #include <weston/config-parser.h>
45 #include "ico_ivi_common_private.h"
46 #include "ico_plugin_version.h"
47
48 /* Internal function to load one plugin.    */
49 static void load_module(struct weston_compositor *ec, const char *path, const char *entry,
50                         int *argc, char *argv[]);
51
52 /* Static valiables                         */
53 static int  debug_level = 3;                /* Debug Level                          */
54
55
56 /*--------------------------------------------------------------------------*/
57 /**
58  * @brief   ico_ivi_debuglevel: answer debug output level.
59  *
60  * @param       none
61  * @return      debug output level
62  * @retval      0       No debug output
63  * @retval      1       Only error output
64  * @retval      2       Error and Warning output
65  * @retval      3       Error, Warning and information output
66  * @retval      4       All output with debug write
67  */
68 /*--------------------------------------------------------------------------*/
69 int
70 ico_ivi_debuglevel(void)
71 {
72     return debug_level;
73 }
74
75 /*--------------------------------------------------------------------------*/
76 /**
77  * @brief   load_module: load one plugin module.
78  *
79  * @param[in]   ec          weston compositor. (from weston)
80  * @param[in]   path        file path of locading plugin module.
81  * @param[in]   entry       entry function name of locading plugin module.
82  * @param[in]   argc        number of arguments.
83  * @param[in]   argv        arguments list.
84  * @return      none
85  */
86 /*--------------------------------------------------------------------------*/
87 static void
88 load_module(struct weston_compositor *ec, const char *path, const char *entry,
89             int *argc, char *argv[])
90 {
91     void    *module;                    /* module informations (dlopen)             */
92     int (*init)(struct weston_compositor *ec, int *argc, char *argv[]);
93                                         /* enter function of loaded plugin          */
94
95     uifw_info("ico_plugin_loader: Load(path=%s entry=%s)", path, entry);
96
97     /* get module informations                      */
98     module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
99
100     if (module) {
101         /* plugin module already loaded             */
102         dlclose(module);
103         uifw_error("ico_plugin_loader: Load Error(%s already loaded)", path);
104         return;
105     }
106
107     /* load plugin module                           */
108     uifw_trace("ico_plugin_loader: %s loading", path);
109
110     module = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
111
112     if (! module)   {
113         /* plugin module dose not exist             */
114         uifw_error("ico_plugin_loader: Load Error(%s error<%s>)", path, dlerror());
115         return;
116     }
117
118     /* find initialize function                     */
119     if (entry)  {
120         init = dlsym(module, entry);
121         if (! init) {
122             uifw_error("ico_plugin_loader: Load Error(%s, function %s dose not exist(%s))",
123                        path, entry, dlerror());
124         }
125         else    {
126             /* call initialize function             */
127             uifw_trace("ico_plugin_loader: Call %s:%s(%08x)", path, entry, (int)init);
128             init(ec, argc, argv);
129             uifw_info("ico_plugin_loader: %s Loaded", path);
130         }
131     }
132 }
133
134 /*--------------------------------------------------------------------------*/
135 /**
136  * @brief   module_init: initialize function of ico_plugin_loader
137  *                       called from weston compositor.
138  *
139  * @param[in]   ec          weston compositor(from weston)
140  * @param[in]   argc        number of arguments(unused)
141  * @param[in]   argv        argument list(unused)
142  * @return      result
143  * @retval      0           sccess
144  * @retval      -1          error
145  */
146 /*--------------------------------------------------------------------------*/
147 WL_EXPORT int
148 module_init(struct weston_compositor *ec, int *argc, char *argv[])
149 {
150     struct weston_config_section *section;
151     char    *moddir = NULL;                 /* Answer back from configuration       */
152     char    *modules = NULL;                /* Answer back from configuration       */
153     char    *p;
154     char    *end;
155     char    buffer[256];
156
157     weston_log("INF>ico-uxf-weston-plugin " ICO_PLUIGN_VERSION "\n");
158
159     uifw_info("ico_plugin_loader: Enter(module_init)");
160
161     /* get ivi debug level          */
162     section = weston_config_get_section(ec->config, "ivi-option", NULL, NULL);
163     if (section)    {
164         weston_config_section_get_int(section, "log", &debug_level, 3);
165     }
166
167     /* get plugin module name from config file(weston_ivi_plugin.ini)   */
168     section = weston_config_get_section(ec->config, "ivi-plugin", NULL, NULL);
169     if (section)    {
170         weston_config_section_get_string(section, "moddir", &moddir, NULL);
171         weston_config_section_get_string(section, "modules", &modules, NULL);
172     }
173
174     if (modules == NULL)    {
175         uifw_error("ico_plugin_loader: Leave(No Plugin in config)");
176         free(moddir);
177         return -1;
178     }
179     p = getenv("WESTON_IVI_PLUGIN_DIR");
180     if (p)  {
181         free(moddir);
182         moddir = strdup(p);
183     }
184
185     p = modules;
186     while (*p) {
187         end = strchrnul(p, ',');
188         if (*p == '/')  {
189             snprintf(buffer, sizeof(buffer), "%.*s", (int) (end - p), p);
190         }
191         else if (moddir)    {
192             snprintf(buffer, sizeof(buffer), "%s/%.*s", moddir, (int) (end - p), p);
193         }
194         else    {
195             snprintf(buffer, sizeof(buffer), "%s/%.*s", MODULEDIR, (int) (end - p), p);
196         }
197         load_module(ec, buffer, "module_init", argc, argv);
198         p = end;
199         while (*p == ',')   {
200             p++;
201         }
202     }
203     free(moddir);
204     free(modules);
205     uifw_info("ico_plugin_loader: Leave(module_init)");
206
207     return 0;
208 }