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 void vlc_module_destruct (gc_object_t *obj)
39 module_t *module = vlc_priv (obj, module_t);
41 free (module->pp_shortcuts);
42 free (module->psz_object_name);
46 static const char default_name[] = "unnamed";
48 module_t *vlc_module_create (vlc_object_t *obj)
50 module_t *module = malloc (sizeof (*module));
54 module->psz_object_name = strdup( default_name );
56 module->submodule = NULL;
57 module->parent = NULL;
58 module->submodule_count = 0;
59 vlc_gc_init (module, vlc_module_destruct);
61 module->psz_shortname = NULL;
62 module->psz_longname = (char*)default_name;
63 module->psz_help = NULL;
64 module->pp_shortcuts = NULL;
65 module->i_shortcuts = 0;
66 module->psz_capability = (char*)"";
68 module->b_unloadable = true;
69 module->b_submodule = false;
70 module->pf_activate = NULL;
71 module->pf_deactivate = NULL;
72 module->p_config = NULL;
74 module->i_config_items = 0;
75 module->i_bool_items = 0;
76 /*module->handle = garbage */
77 module->psz_filename = NULL;
78 module->domain = NULL;
79 module->b_builtin = false;
80 module->b_loaded = false;
87 static void vlc_submodule_destruct (gc_object_t *obj)
89 module_t *module = vlc_priv (obj, module_t);
90 free (module->pp_shortcuts);
91 free (module->psz_object_name);
95 module_t *vlc_submodule_create (module_t *module)
97 assert (module != NULL);
99 module_t *submodule = calloc( 1, sizeof(*submodule) );
103 vlc_gc_init (submodule, vlc_submodule_destruct);
105 submodule->next = module->submodule;
106 submodule->parent = module;
107 module->submodule = submodule;
108 module->submodule_count++;
110 /* Muahahaha! Heritage! Polymorphism! Ugliness!! */
111 submodule->pp_shortcuts = malloc( sizeof( char ** ) );
112 submodule->pp_shortcuts[0] = module->pp_shortcuts[0]; /* object name */
113 submodule->i_shortcuts = 1;
115 submodule->psz_object_name = strdup( module->psz_object_name );
116 submodule->psz_shortname = module->psz_shortname;
117 submodule->psz_longname = module->psz_longname;
118 submodule->psz_capability = module->psz_capability;
119 submodule->i_score = module->i_score;
120 submodule->b_submodule = true;
121 submodule->domain = module->domain;
125 static module_config_t *vlc_config_create (module_t *module, int type)
127 unsigned confsize = module->confsize;
128 module_config_t *tab = module->p_config;
130 if ((confsize & 0xf) == 0)
132 tab = realloc_or_free (tab, (confsize + 17) * sizeof (*tab));
136 module->p_config = tab;
139 memset (tab + confsize, 0, sizeof (tab[confsize]));
140 if (IsConfigIntegerType (type))
142 tab[confsize].max.i = INT_MAX;
143 tab[confsize].min.i = INT_MIN;
145 tab[confsize].i_type = type;
147 if (type & CONFIG_ITEM)
149 module->i_config_items++;
150 if (type == CONFIG_ITEM_BOOL)
151 module->i_bool_items++;
155 return tab + confsize;
159 int vlc_plugin_set (module_t *module, module_config_t *item, int propid, ...)
163 typedef int(*int_fp_vlcobjectt)(vlc_object_t *) ;
164 typedef void(*void_fp_vlcobjectt)(vlc_object_t *);
166 va_start (ap, propid);
169 case VLC_SUBMODULE_CREATE:
171 module_t **pp = va_arg (ap, module_t **);
172 *pp = vlc_submodule_create (module);
178 case VLC_CONFIG_CREATE:
180 int type = va_arg (ap, int);
181 module_config_t **pp = va_arg (ap, module_config_t **);
182 *pp = vlc_config_create (module, type);
188 case VLC_MODULE_SHORTCUT:
190 unsigned i_shortcuts = va_arg (ap, unsigned);
191 unsigned index = module->i_shortcuts;
192 /* The cache loader accept only a small number of shortcuts */
193 assert(i_shortcuts + index <= MODULE_SHORTCUT_MAX);
195 const char *const *tab = va_arg (ap, const char *const *);
196 const char **pp = realloc (module->pp_shortcuts,
197 sizeof (pp[0]) * (index + i_shortcuts));
198 if (unlikely(pp == NULL))
203 module->pp_shortcuts = pp;
204 module->i_shortcuts = index + i_shortcuts;
205 memcpy (pp + index, tab, sizeof (pp[0]) * i_shortcuts);
209 case VLC_MODULE_CAPABILITY:
210 module->psz_capability = va_arg (ap, char *);
213 case VLC_MODULE_SCORE:
214 module->i_score = va_arg (ap, int);
217 case VLC_MODULE_CB_OPEN:
218 module->pf_activate = va_arg (ap, int_fp_vlcobjectt);
221 case VLC_MODULE_CB_CLOSE:
222 module->pf_deactivate = va_arg (ap, void_fp_vlcobjectt);
225 case VLC_MODULE_NO_UNLOAD:
226 module->b_unloadable = false;
229 case VLC_MODULE_NAME:
231 const char *value = va_arg (ap, const char *);
232 free( module->psz_object_name );
233 module->psz_object_name = strdup( value );
234 module->pp_shortcuts = malloc( sizeof( char ** ) );
235 module->pp_shortcuts[0] = (char*)value; /* dooh! */
236 module->i_shortcuts = 1;
238 if (module->psz_longname == default_name)
239 module->psz_longname = (char*)value; /* dooh! */
243 case VLC_MODULE_SHORTNAME:
244 module->psz_shortname = va_arg (ap, char *);
247 case VLC_MODULE_DESCRIPTION:
248 module->psz_longname = va_arg (ap, char *);
251 case VLC_MODULE_HELP:
252 module->psz_help = va_arg (ap, char *);
255 case VLC_MODULE_TEXTDOMAIN:
256 module->domain = va_arg (ap, char *);
259 case VLC_CONFIG_NAME:
261 const char *name = va_arg (ap, const char *);
262 vlc_callback_t cb = va_arg (ap, vlc_callback_t);
264 assert (name != NULL);
265 item->psz_name = strdup (name);
266 item->pf_callback = cb;
270 case VLC_CONFIG_VALUE:
272 if (IsConfigIntegerType (item->i_type))
274 item->orig.i = item->saved.i =
275 item->value.i = va_arg (ap, int64_t);
278 if (IsConfigFloatType (item->i_type))
280 item->orig.f = item->saved.f =
281 item->value.f = va_arg (ap, double);
284 if (IsConfigStringType (item->i_type))
286 const char *value = va_arg (ap, const char *);
287 item->value.psz = value ? strdup (value) : NULL;
288 item->orig.psz = value ? strdup (value) : NULL;
289 item->saved.psz = value ? strdup (value) : NULL;
294 case VLC_CONFIG_RANGE:
296 if (IsConfigIntegerType (item->i_type)
297 || item->i_type == CONFIG_ITEM_MODULE_LIST_CAT
298 || item->i_type == CONFIG_ITEM_MODULE_CAT)
300 item->min.i = va_arg (ap, int64_t);
301 item->max.i = va_arg (ap, int64_t);
304 if (IsConfigFloatType (item->i_type))
306 item->min.f = va_arg (ap, double);
307 item->max.f = va_arg (ap, double);
312 case VLC_CONFIG_ADVANCED:
313 item->b_advanced = true;
316 case VLC_CONFIG_VOLATILE:
317 item->b_unsaveable = true;
320 case VLC_CONFIG_PERSISTENT:
321 item->b_autosave = true;
324 case VLC_CONFIG_RESTART:
325 item->b_restart = true;
328 case VLC_CONFIG_PRIVATE:
329 item->b_internal = true;
332 case VLC_CONFIG_REMOVED:
333 item->b_removed = true;
336 case VLC_CONFIG_CAPABILITY:
338 const char *cap = va_arg (ap, const char *);
339 item->psz_type = cap ? strdup (cap) : NULL;
343 case VLC_CONFIG_SHORTCUT:
344 item->i_short = va_arg (ap, int);
347 case VLC_CONFIG_OLDNAME:
349 const char *oldname = va_arg (ap, const char *);
350 item->psz_oldname = oldname ? strdup (oldname) : NULL;
354 case VLC_CONFIG_SAFE:
358 case VLC_CONFIG_DESC:
360 const char *text = va_arg (ap, const char *);
361 const char *longtext = va_arg (ap, const char *);
363 item->psz_text = text ? strdup (text) : NULL;
364 item->psz_longtext = longtext ? strdup (longtext) : NULL;
368 case VLC_CONFIG_LIST:
370 size_t len = va_arg (ap, size_t);
373 if (IsConfigIntegerType (item->i_type))
375 const int *src = va_arg (ap, const int *);
376 int *dst = malloc (sizeof (int) * (len + 1));
380 memcpy (dst, src, sizeof (int) * len);
386 if (IsConfigStringType (item->i_type))
388 const char *const *src = va_arg (ap, const char *const *);
389 char **dst = malloc (sizeof (char *) * (len + 1));
393 for (size_t i = 0; i < len; i++)
394 dst[i] = src[i] ? strdup (src[i]) : NULL;
397 item->ppsz_list = dst;
402 /* Copy textual descriptions */
403 const char *const *text = va_arg (ap, const char *const *);
406 char **dtext = malloc (sizeof (char *) * (len + 1));
409 for (size_t i = 0; i < len; i++)
410 dtext[i] = text[i] ? strdup (text[i]) : NULL;
413 item->ppsz_list_text = dtext;
416 item->ppsz_list_text = NULL;
419 item->pf_update_list = va_arg (ap, vlc_callback_t);
423 case VLC_CONFIG_ADD_ACTION:
425 vlc_callback_t cb = va_arg (ap, vlc_callback_t), *tabcb;
426 const char *name = va_arg (ap, const char *);
429 tabcb = realloc (item->ppf_action,
430 (item->i_action + 2) * sizeof (cb));
433 item->ppf_action = tabcb;
434 tabcb[item->i_action] = cb;
435 tabcb[item->i_action + 1] = NULL;
437 tabtext = realloc (item->ppsz_action_text,
438 (item->i_action + 2) * sizeof (name));
441 item->ppsz_action_text = tabtext;
444 tabtext[item->i_action] = strdup (name);
446 tabtext[item->i_action] = NULL;
447 tabtext[item->i_action + 1] = NULL;
454 fprintf (stderr, "LibVLC: unknown module property %d\n", propid);
455 fprintf (stderr, "LibVLC: too old to use this module?\n");