5f3ac618e6b5c5b3ec553d7a39cef1d00fdf85f9
[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 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-08-2013
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 "ico_ivi_common.h"
45
46 /* This function is called from the main body of Weston and initializes this module.*/
47 int module_init(struct weston_compositor *ec);
48
49 /* Internal function to load one plugin.    */
50 static void load_module(struct weston_compositor *ec, const char *path, const char *entry);
51
52 /* Static valiables                         */
53 static char *moddir = NULL;                 /* Answer back from configuration       */
54 static char *modules = NULL;                /* Answer back from configuration       */
55 static int  debug_level = 3;                /* Debug Level                          */
56
57 /* Configuration key                        */
58 static const struct config_key plugin_config_keys[] = {
59         { "moddir", CONFIG_KEY_STRING, &moddir },
60         { "modules", CONFIG_KEY_STRING, &modules },
61     };
62
63 static const struct config_section conf_plugin[] = {
64         { "plugin", plugin_config_keys, ARRAY_LENGTH(plugin_config_keys) },
65     };
66
67 static const struct config_key debug_config_keys[] = {
68         { "ivi_debug", CONFIG_KEY_INTEGER, &debug_level },
69     };
70
71 static const struct config_section conf_debug[] = {
72         { "debug", debug_config_keys, ARRAY_LENGTH(debug_config_keys) },
73     };
74
75
76 /*--------------------------------------------------------------------------*/
77 /**
78  * @brief   ico_ivi_debuglevel: answer debug output level.
79  *
80  * @param       none
81  * @return      debug output level
82  * @retval      0       No debug output
83  * @retval      1       Only error output
84  * @retval      2       Error and information output
85  * @retval      3       All output with debug write
86  */
87 /*--------------------------------------------------------------------------*/
88 int
89 ico_ivi_debuglevel(void)
90 {
91     return debug_level;
92 }
93
94 /*--------------------------------------------------------------------------*/
95 /**
96  * @brief   load_module: load one plugin module.
97  *
98  * @param[in]   ec          weston compositor. (from weston)
99  * @param[in]   path        file path of plugin module.
100  * @param[in]   entry       entry function name of plugin module.
101  * @return      none
102  */
103 /*--------------------------------------------------------------------------*/
104 static void
105 load_module(struct weston_compositor *ec, const char *path, const char *entry)
106 {
107     void    *module;                    /* module informations (dlopen)             */
108     int (*init)(struct weston_compositor *ec);
109                                         /* enter function of loaded plugin          */
110
111     uifw_info("ico_plugin_loader: Load(path=%s entry=%s)", path, entry);
112
113     /* get module informations                      */
114     module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
115
116     if (module) {
117         /* plugin module already loaded             */
118         dlclose(module);
119         uifw_error("ico_plugin_loader: Load Error(%s already loaded)", path);
120         return;
121     }
122
123     /* load plugin module                           */
124     uifw_trace("ico_plugin_loader: %s loading", path);
125
126     module = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
127
128     if (! module)   {
129         /* plugin module dose not exist             */
130         uifw_error("ico_plugin_loader: Load Error(%s error<%s>)", path, dlerror());
131         return;
132     }
133
134     /* find initialize function                     */
135     if (entry)  {
136         init = dlsym(module, entry);
137         if (! init) {
138             uifw_error("ico_plugin_loader: Load Error(%s, function %s dose not exist(%s))",
139                        path, entry, dlerror());
140         }
141         else    {
142             /* call initialize function             */
143             uifw_trace("ico_plugin_loader: Call %s:%s(%08x)", path, entry, (int)init);
144             init(ec);
145             uifw_info("ico_plugin_loader: %s Loaded", path);
146         }
147     }
148 }
149
150 /*--------------------------------------------------------------------------*/
151 /**
152  * @brief   module_init: initialize function of ico_plugin_loader
153  *                       called from weston compositor.
154  *
155  * @param[in]   ec          weston compositor(from weston)
156  * @return      result
157  * @retval      0           sccess
158  * @retval      -1          error
159  */
160 /*--------------------------------------------------------------------------*/
161 WL_EXPORT int
162 module_init(struct weston_compositor *ec)
163 {
164     int     config_fd;
165     char    *p;
166     char    *end;
167     char    buffer[256];
168
169     uifw_info("ico_plugin_loader: Enter(module_init)");
170
171     /* get plugin module name from config file(weston_ivi_plugin.ini)   */
172     config_fd = open_config_file(ICO_IVI_PLUGIN_CONFIG);
173     parse_config_file(config_fd, conf_plugin, ARRAY_LENGTH(conf_plugin), NULL);
174     parse_config_file(config_fd, conf_debug, ARRAY_LENGTH(conf_debug), NULL);
175     close(config_fd);
176
177     if (modules == NULL)    {
178         uifw_error("ico_plugin_loader: Leave(No Plugin in config)");
179         return -1;
180     }
181     moddir = getenv("WESTON_IVI_PLUGIN_DIR");
182
183     p = modules;
184     while (*p) {
185         end = strchrnul(p, ',');
186         if (*p == '/')  {
187             snprintf(buffer, sizeof(buffer), "%.*s", (int) (end - p), p);
188         }
189         else if (moddir)    {
190             snprintf(buffer, sizeof(buffer), "%s/%.*s", moddir, (int) (end - p), p);
191         }
192         else    {
193             snprintf(buffer, sizeof(buffer), "%s/%.*s", MODULEDIR, (int) (end - p), p);
194         }
195         load_module(ec, buffer, "module_init");
196         p = end;
197         while (*p == ',')   {
198             p++;
199         }
200     }
201
202     uifw_info("ico_plugin_loader: Leave(module_init)");
203
204     return 0;
205 }
206