1 /*****************************************************************************
2 * entry.c : Callbacks for module entry point
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
5 * Copyright © 2007-2008 Rémi Denis-Courmont
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
26 #include <vlc_common.h>
27 #include <vlc_plugin.h>
28 #include <vlc_memory.h>
33 #include "modules/modules.h"
34 #include "config/configuration.h"
37 static char *strdup_null (const char *str)
39 return (str != NULL) ? strdup (str) : NULL;
42 module_t *vlc_module_create (module_t *parent)
44 module_t *module = malloc (sizeof (*module));
48 /* TODO: replace module/submodules with plugin/modules */
52 module->parent = NULL;
56 module->next = parent->submodule;
57 parent->submodule = module;
58 parent->submodule_count++;
59 module->parent = parent;
62 module->submodule = NULL;
63 module->submodule_count = 0;
65 module->psz_shortname = NULL;
66 module->psz_longname = NULL;
67 module->psz_help = NULL;
68 module->pp_shortcuts = NULL;
69 module->i_shortcuts = 0;
70 module->psz_capability = NULL;
71 module->i_score = (parent != NULL) ? parent->i_score : 1;
72 module->b_loaded = false;
73 module->b_unloadable = parent == NULL;
74 module->pf_activate = NULL;
75 module->pf_deactivate = NULL;
76 module->p_config = NULL;
78 module->i_config_items = 0;
79 module->i_bool_items = 0;
80 /*module->handle = garbage */
81 module->psz_filename = NULL;
82 module->domain = NULL;
86 void vlc_module_destroy (module_t *module)
88 for (module_t *m = module->submodule, *next; m != NULL; m = next)
91 vlc_module_destroy (m);
94 free (module->domain);
95 free (module->psz_filename);
96 for (unsigned i = 0; i < module->i_shortcuts; i++)
97 free (module->pp_shortcuts[i]);
98 free (module->pp_shortcuts);
99 free (module->psz_capability);
100 free (module->psz_help);
101 free (module->psz_longname);
102 free (module->psz_shortname);
106 static module_config_t *vlc_config_create (module_t *module, int type)
108 unsigned confsize = module->confsize;
109 module_config_t *tab = module->p_config;
111 if ((confsize & 0xf) == 0)
113 tab = realloc_or_free (tab, (confsize + 17) * sizeof (*tab));
117 module->p_config = tab;
120 memset (tab + confsize, 0, sizeof (tab[confsize]));
121 if (IsConfigIntegerType (type))
123 tab[confsize].max.i = INT_MAX;
124 tab[confsize].min.i = INT_MIN;
126 tab[confsize].i_type = type;
128 if (CONFIG_ITEM(type))
130 module->i_config_items++;
131 if (type == CONFIG_ITEM_BOOL)
132 module->i_bool_items++;
136 return tab + confsize;
140 int vlc_plugin_set (module_t *module, module_config_t *item, int propid, ...)
145 va_start (ap, propid);
148 case VLC_MODULE_CREATE:
150 module_t **pp = va_arg (ap, module_t **);
151 module_t *submodule = vlc_module_create (module);
153 if (unlikely(submodule == NULL))
162 /* Inheritance. Ugly!! */
163 submodule->pp_shortcuts = xmalloc (sizeof (char **));
164 submodule->pp_shortcuts[0] = strdup_null (module->pp_shortcuts[0]);
165 submodule->i_shortcuts = 1; /* object name */
167 submodule->psz_shortname = strdup_null (module->psz_shortname);
168 submodule->psz_longname = strdup_null (module->psz_longname);
169 submodule->psz_capability = strdup_null (module->psz_capability);
173 case VLC_CONFIG_CREATE:
175 int type = va_arg (ap, int);
176 module_config_t **pp = va_arg (ap, module_config_t **);
177 *pp = vlc_config_create (module, type);
183 case VLC_MODULE_SHORTCUT:
185 unsigned i_shortcuts = va_arg (ap, unsigned);
186 unsigned index = module->i_shortcuts;
187 /* The cache loader accept only a small number of shortcuts */
188 assert(i_shortcuts + index <= MODULE_SHORTCUT_MAX);
190 const char *const *tab = va_arg (ap, const char *const *);
191 char **pp = realloc (module->pp_shortcuts,
192 sizeof (pp[0]) * (index + i_shortcuts));
193 if (unlikely(pp == NULL))
198 module->pp_shortcuts = pp;
199 module->i_shortcuts = index + i_shortcuts;
201 for (unsigned i = 0; i < i_shortcuts; i++)
202 pp[i] = strdup (tab[i]);
206 case VLC_MODULE_CAPABILITY:
207 free (module->psz_capability);
208 module->psz_capability = strdup (va_arg (ap, char *));
211 case VLC_MODULE_SCORE:
212 module->i_score = va_arg (ap, int);
215 case VLC_MODULE_CB_OPEN:
216 module->pf_activate = va_arg (ap, void *);
219 case VLC_MODULE_CB_CLOSE:
220 module->pf_deactivate = va_arg (ap, void *);
223 case VLC_MODULE_NO_UNLOAD:
224 assert (module->parent == NULL);
225 module->b_unloadable = false;
228 case VLC_MODULE_NAME:
230 const char *value = va_arg (ap, const char *);
232 assert (module->i_shortcuts == 0);
233 module->pp_shortcuts = malloc( sizeof( char ** ) );
234 module->pp_shortcuts[0] = strdup (value);
235 module->i_shortcuts = 1;
237 assert (module->psz_longname == NULL);
238 module->psz_longname = strdup (value);
242 case VLC_MODULE_SHORTNAME:
243 assert (module->psz_shortname == NULL || module->parent != NULL);
244 free (module->psz_shortname);
245 module->psz_shortname = strdup (va_arg (ap, char *));
248 case VLC_MODULE_DESCRIPTION:
249 // TODO: do not set this in VLC_MODULE_NAME
250 free (module->psz_longname);
251 module->psz_longname = strdup (va_arg (ap, char *));
254 case VLC_MODULE_HELP:
255 assert (module->parent == NULL);
256 assert (module->psz_help == NULL);
257 module->psz_help = strdup (va_arg (ap, char *));
260 case VLC_MODULE_TEXTDOMAIN:
261 assert (module->parent == NULL);
262 assert (module->domain == NULL);
263 module->domain = strdup (va_arg (ap, char *));
266 case VLC_CONFIG_NAME:
268 const char *name = va_arg (ap, const char *);
270 assert (name != NULL);
271 item->psz_name = strdup (name);
275 case VLC_CONFIG_VALUE:
277 if (IsConfigIntegerType (item->i_type)
278 || !CONFIG_ITEM(item->i_type))
281 item->value.i = va_arg (ap, int64_t);
284 if (IsConfigFloatType (item->i_type))
287 item->value.f = va_arg (ap, double);
290 if (IsConfigStringType (item->i_type))
292 const char *value = va_arg (ap, const char *);
293 item->value.psz = value ? strdup (value) : NULL;
294 item->orig.psz = value ? strdup (value) : NULL;
299 case VLC_CONFIG_RANGE:
301 if (IsConfigIntegerType (item->i_type)
302 || !CONFIG_ITEM(item->i_type))
304 item->min.i = va_arg (ap, int64_t);
305 item->max.i = va_arg (ap, int64_t);
308 if (IsConfigFloatType (item->i_type))
310 item->min.f = va_arg (ap, double);
311 item->max.f = va_arg (ap, double);
316 case VLC_CONFIG_ADVANCED:
317 item->b_advanced = true;
320 case VLC_CONFIG_VOLATILE:
321 item->b_unsaveable = true;
324 case VLC_CONFIG_PRIVATE:
325 item->b_internal = true;
328 case VLC_CONFIG_REMOVED:
329 item->b_removed = true;
332 case VLC_CONFIG_CAPABILITY:
334 const char *cap = va_arg (ap, const char *);
335 item->psz_type = cap ? strdup (cap) : NULL;
339 case VLC_CONFIG_SHORTCUT:
340 item->i_short = va_arg (ap, int);
343 case VLC_CONFIG_OLDNAME:
345 const char *oldname = va_arg (ap, const char *);
346 assert (item->psz_oldname == NULL);
347 item->psz_oldname = oldname ? strdup (oldname) : NULL;
351 case VLC_CONFIG_SAFE:
355 case VLC_CONFIG_DESC:
357 const char *text = va_arg (ap, const char *);
358 const char *longtext = va_arg (ap, const char *);
360 item->psz_text = text ? strdup (text) : NULL;
361 item->psz_longtext = longtext ? strdup (longtext) : NULL;
365 case VLC_CONFIG_LIST:
367 size_t len = va_arg (ap, size_t);
370 if (IsConfigIntegerType (item->i_type))
372 const int *src = va_arg (ap, const int *);
373 int *dst = malloc (sizeof (int) * (len + 1));
377 memcpy (dst, src, sizeof (int) * len);
383 if (IsConfigStringType (item->i_type))
385 const char *const *src = va_arg (ap, const char *const *);
386 char **dst = malloc (sizeof (char *) * (len + 1));
390 for (size_t i = 0; i < len; i++)
391 dst[i] = src[i] ? strdup (src[i]) : NULL;
394 item->ppsz_list = dst;
399 /* Copy textual descriptions */
400 const char *const *text = va_arg (ap, const char *const *);
403 char **dtext = malloc (sizeof (char *) * (len + 1));
406 for (size_t i = 0; i < len; i++)
407 dtext[i] = text[i] ? strdup (text[i]) : NULL;
410 item->ppsz_list_text = dtext;
413 item->ppsz_list_text = NULL;
416 item->pf_update_list = va_arg (ap, vlc_callback_t);
420 case VLC_CONFIG_ADD_ACTION:
422 vlc_callback_t cb = va_arg (ap, vlc_callback_t), *tabcb;
423 const char *name = va_arg (ap, const char *);
426 tabcb = realloc (item->ppf_action,
427 (item->i_action + 2) * sizeof (cb));
430 item->ppf_action = tabcb;
431 tabcb[item->i_action] = cb;
432 tabcb[item->i_action + 1] = NULL;
434 tabtext = realloc (item->ppsz_action_text,
435 (item->i_action + 2) * sizeof (name));
438 item->ppsz_action_text = tabtext;
441 tabtext[item->i_action] = strdup (name);
443 tabtext[item->i_action] = NULL;
444 tabtext[item->i_action + 1] = NULL;
451 fprintf (stderr, "LibVLC: unknown module property %d\n", propid);
452 fprintf (stderr, "LibVLC: too old to use this module?\n");