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 const char *const *tab = va_arg (ap, const char *const *);
193 const char **pp = realloc (module->pp_shortcuts,
194 sizeof (pp[0]) * (index + i_shortcuts));
195 if (unlikely(pp == NULL))
200 module->pp_shortcuts = pp;
201 module->i_shortcuts = index + i_shortcuts;
202 memcpy (pp + index, tab, sizeof (pp[0]) * i_shortcuts);
206 case VLC_MODULE_CAPABILITY:
207 module->psz_capability = va_arg (ap, char *);
210 case VLC_MODULE_SCORE:
211 module->i_score = va_arg (ap, int);
214 case VLC_MODULE_CB_OPEN:
215 module->pf_activate = va_arg (ap, int_fp_vlcobjectt);
218 case VLC_MODULE_CB_CLOSE:
219 module->pf_deactivate = va_arg (ap, void_fp_vlcobjectt);
222 case VLC_MODULE_NO_UNLOAD:
223 module->b_unloadable = false;
226 case VLC_MODULE_NAME:
228 const char *value = va_arg (ap, const char *);
229 free( module->psz_object_name );
230 module->psz_object_name = strdup( value );
231 module->pp_shortcuts = malloc( sizeof( char ** ) );
232 module->pp_shortcuts[0] = (char*)value; /* dooh! */
233 module->i_shortcuts = 1;
235 if (module->psz_longname == default_name)
236 module->psz_longname = (char*)value; /* dooh! */
240 case VLC_MODULE_SHORTNAME:
241 module->psz_shortname = va_arg (ap, char *);
244 case VLC_MODULE_DESCRIPTION:
245 module->psz_longname = va_arg (ap, char *);
248 case VLC_MODULE_HELP:
249 module->psz_help = va_arg (ap, char *);
252 case VLC_MODULE_TEXTDOMAIN:
253 module->domain = va_arg (ap, char *);
256 case VLC_CONFIG_NAME:
258 const char *name = va_arg (ap, const char *);
259 vlc_callback_t cb = va_arg (ap, vlc_callback_t);
261 assert (name != NULL);
262 item->psz_name = strdup (name);
263 item->pf_callback = cb;
267 case VLC_CONFIG_VALUE:
269 if (IsConfigIntegerType (item->i_type))
271 item->orig.i = item->saved.i =
272 item->value.i = va_arg (ap, int64_t);
275 if (IsConfigFloatType (item->i_type))
277 item->orig.f = item->saved.f =
278 item->value.f = va_arg (ap, double);
281 if (IsConfigStringType (item->i_type))
283 const char *value = va_arg (ap, const char *);
284 item->value.psz = value ? strdup (value) : NULL;
285 item->orig.psz = value ? strdup (value) : NULL;
286 item->saved.psz = value ? strdup (value) : NULL;
291 case VLC_CONFIG_RANGE:
293 if (IsConfigIntegerType (item->i_type)
294 || item->i_type == CONFIG_ITEM_MODULE_LIST_CAT
295 || item->i_type == CONFIG_ITEM_MODULE_CAT)
297 item->min.i = va_arg (ap, int64_t);
298 item->max.i = va_arg (ap, int64_t);
301 if (IsConfigFloatType (item->i_type))
303 item->min.f = va_arg (ap, double);
304 item->max.f = va_arg (ap, double);
309 case VLC_CONFIG_ADVANCED:
310 item->b_advanced = true;
313 case VLC_CONFIG_VOLATILE:
314 item->b_unsaveable = true;
317 case VLC_CONFIG_PERSISTENT:
318 item->b_autosave = true;
321 case VLC_CONFIG_RESTART:
322 item->b_restart = true;
325 case VLC_CONFIG_PRIVATE:
326 item->b_internal = true;
329 case VLC_CONFIG_REMOVED:
330 item->b_removed = true;
333 case VLC_CONFIG_CAPABILITY:
335 const char *cap = va_arg (ap, const char *);
336 item->psz_type = cap ? strdup (cap) : NULL;
340 case VLC_CONFIG_SHORTCUT:
341 item->i_short = va_arg (ap, int);
344 case VLC_CONFIG_OLDNAME:
346 const char *oldname = va_arg (ap, const char *);
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");