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