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>
32 #include "modules/modules.h"
33 #include "config/configuration.h"
36 static void vlc_module_destruct (gc_object_t *obj)
38 module_t *module = vlc_priv (obj, module_t);
40 free (module->psz_object_name);
44 static const char default_name[] = "unnamed";
46 module_t *vlc_module_create (vlc_object_t *obj)
48 module_t *module = malloc (sizeof (*module));
52 module->psz_object_name = strdup( default_name );
54 module->submodule = NULL;
55 module->parent = NULL;
56 module->submodule_count = 0;
57 vlc_gc_init (module, vlc_module_destruct);
59 module->psz_shortname = NULL;
60 module->psz_longname = (char*)default_name;
61 module->psz_help = NULL;
62 for (unsigned i = 0; i < MODULE_SHORTCUT_MAX; i++)
63 module->pp_shortcuts[i] = NULL;
64 module->psz_capability = (char*)"";
66 module->b_unloadable = true;
67 module->b_submodule = false;
68 module->pf_activate = NULL;
69 module->pf_deactivate = NULL;
70 module->p_config = NULL;
72 module->i_config_items = 0;
73 module->i_bool_items = 0;
74 /*module->handle = garbage */
75 module->psz_filename = NULL;
76 module->domain = NULL;
77 module->b_builtin = false;
78 module->b_loaded = false;
85 static void vlc_submodule_destruct (gc_object_t *obj)
87 module_t *module = vlc_priv (obj, module_t);
88 free (module->psz_object_name);
92 module_t *vlc_submodule_create (module_t *module)
94 assert (module != NULL);
96 module_t *submodule = calloc( 1, sizeof(*submodule) );
100 vlc_gc_init (submodule, vlc_submodule_destruct);
102 submodule->next = module->submodule;
103 submodule->parent = module;
104 module->submodule = submodule;
105 module->submodule_count++;
107 /* Muahahaha! Heritage! Polymorphism! Ugliness!! */
108 submodule->pp_shortcuts[0] = module->pp_shortcuts[0]; /* object name */
109 for (unsigned i = 1; i < MODULE_SHORTCUT_MAX; i++)
110 submodule->pp_shortcuts[i] = NULL;
112 submodule->psz_object_name = strdup( module->psz_object_name );
113 submodule->psz_shortname = module->psz_shortname;
114 submodule->psz_longname = module->psz_longname;
115 submodule->psz_capability = module->psz_capability;
116 submodule->i_score = module->i_score;
117 submodule->b_submodule = true;
118 submodule->domain = module->domain;
122 static module_config_t *vlc_config_create (module_t *module, int type)
124 unsigned confsize = module->confsize;
125 module_config_t *tab = module->p_config;
127 if ((confsize & 0xf) == 0)
129 tab = realloc_or_free (tab, (confsize + 17) * sizeof (*tab));
133 module->p_config = tab;
136 memset (tab + confsize, 0, sizeof (tab[confsize]));
137 tab[confsize].i_type = type;
139 if (type & CONFIG_ITEM)
141 module->i_config_items++;
142 if (type == CONFIG_ITEM_BOOL)
143 module->i_bool_items++;
147 return tab + confsize;
151 int vlc_plugin_set (module_t *module, module_config_t *item, int propid, ...)
155 typedef int(*int_fp_vlcobjectt)(vlc_object_t *) ;
156 typedef void(*void_fp_vlcobjectt)(vlc_object_t *);
158 va_start (ap, propid);
161 case VLC_SUBMODULE_CREATE:
163 module_t **pp = va_arg (ap, module_t **);
164 *pp = vlc_submodule_create (module);
170 case VLC_CONFIG_CREATE:
172 int type = va_arg (ap, int);
173 module_config_t **pp = va_arg (ap, module_config_t **);
174 *pp = vlc_config_create (module, type);
180 case VLC_MODULE_SHORTCUT:
183 for (i = 0; module->pp_shortcuts[i] != NULL; i++);
184 if (i >= (MODULE_SHORTCUT_MAX - 1))
187 module->pp_shortcuts[i] = va_arg (ap, char *);
191 case VLC_MODULE_CAPABILITY:
192 module->psz_capability = va_arg (ap, char *);
195 case VLC_MODULE_SCORE:
196 module->i_score = va_arg (ap, int);
199 case VLC_MODULE_CB_OPEN:
200 module->pf_activate = va_arg (ap, int_fp_vlcobjectt);
203 case VLC_MODULE_CB_CLOSE:
204 module->pf_deactivate = va_arg (ap, void_fp_vlcobjectt);
207 case VLC_MODULE_NO_UNLOAD:
208 module->b_unloadable = false;
211 case VLC_MODULE_NAME:
213 const char *value = va_arg (ap, const char *);
214 free( module->psz_object_name );
215 module->psz_object_name = strdup( value );
216 module->pp_shortcuts[0] = (char*)value; /* dooh! */
217 if (module->psz_longname == default_name)
218 module->psz_longname = (char*)value; /* dooh! */
222 case VLC_MODULE_SHORTNAME:
223 module->psz_shortname = va_arg (ap, char *);
226 case VLC_MODULE_DESCRIPTION:
227 module->psz_longname = va_arg (ap, char *);
230 case VLC_MODULE_HELP:
231 module->psz_help = va_arg (ap, char *);
234 case VLC_MODULE_TEXTDOMAIN:
235 module->domain = va_arg (ap, char *);
238 case VLC_CONFIG_NAME:
240 const char *name = va_arg (ap, const char *);
241 vlc_callback_t cb = va_arg (ap, vlc_callback_t);
243 assert (name != NULL);
244 item->psz_name = strdup (name);
245 item->pf_callback = cb;
249 case VLC_CONFIG_VALUE:
251 if (IsConfigIntegerType (item->i_type))
253 item->orig.i = item->saved.i =
254 item->value.i = va_arg (ap, int);
257 if (IsConfigFloatType (item->i_type))
259 item->orig.f = item->saved.f =
260 item->value.f = va_arg (ap, double);
263 if (IsConfigStringType (item->i_type))
265 const char *value = va_arg (ap, const char *);
266 item->value.psz = value ? strdup (value) : NULL;
267 item->orig.psz = value ? strdup (value) : NULL;
268 item->saved.psz = value ? strdup (value) : NULL;
273 case VLC_CONFIG_RANGE:
275 if (IsConfigIntegerType (item->i_type)
276 || item->i_type == CONFIG_ITEM_MODULE_LIST_CAT
277 || item->i_type == CONFIG_ITEM_MODULE_CAT)
279 item->min.i = va_arg (ap, int);
280 item->max.i = va_arg (ap, int);
283 if (IsConfigFloatType (item->i_type))
285 item->min.f = va_arg (ap, double);
286 item->max.f = va_arg (ap, double);
291 case VLC_CONFIG_ADVANCED:
292 item->b_advanced = true;
295 case VLC_CONFIG_VOLATILE:
296 item->b_unsaveable = true;
299 case VLC_CONFIG_PERSISTENT:
300 item->b_autosave = true;
303 case VLC_CONFIG_RESTART:
304 item->b_restart = true;
307 case VLC_CONFIG_PRIVATE:
308 item->b_internal = true;
311 case VLC_CONFIG_REMOVED:
312 item->b_removed = true;
315 case VLC_CONFIG_CAPABILITY:
317 const char *cap = va_arg (ap, const char *);
318 item->psz_type = cap ? strdup (cap) : NULL;
322 case VLC_CONFIG_SHORTCUT:
323 item->i_short = va_arg (ap, int);
326 case VLC_CONFIG_OLDNAME:
328 const char *oldname = va_arg (ap, const char *);
329 item->psz_oldname = oldname ? strdup (oldname) : NULL;
333 case VLC_CONFIG_SAFE:
337 case VLC_CONFIG_DESC:
339 const char *text = va_arg (ap, const char *);
340 const char *longtext = va_arg (ap, const char *);
342 item->psz_text = text ? strdup (text) : NULL;
343 item->psz_longtext = longtext ? strdup (longtext) : NULL;
347 case VLC_CONFIG_LIST:
349 size_t len = va_arg (ap, size_t);
352 if (IsConfigIntegerType (item->i_type))
354 const int *src = va_arg (ap, const int *);
355 int *dst = malloc (sizeof (int) * (len + 1));
359 memcpy (dst, src, sizeof (int) * len);
365 if (IsConfigStringType (item->i_type))
367 const char *const *src = va_arg (ap, const char *const *);
368 char **dst = malloc (sizeof (char *) * (len + 1));
372 for (size_t i = 0; i < len; i++)
373 dst[i] = src[i] ? strdup (src[i]) : NULL;
376 item->ppsz_list = dst;
381 /* Copy textual descriptions */
382 const char *const *text = va_arg (ap, const char *const *);
385 char **dtext = malloc (sizeof (char *) * (len + 1));
388 for (size_t i = 0; i < len; i++)
389 dtext[i] = text[i] ? strdup (text[i]) : NULL;
392 item->ppsz_list_text = dtext;
395 item->ppsz_list_text = NULL;
398 item->pf_update_list = va_arg (ap, vlc_callback_t);
402 case VLC_CONFIG_ADD_ACTION:
404 vlc_callback_t cb = va_arg (ap, vlc_callback_t), *tabcb;
405 const char *name = va_arg (ap, const char *);
408 tabcb = realloc (item->ppf_action,
409 (item->i_action + 2) * sizeof (cb));
412 item->ppf_action = tabcb;
413 tabcb[item->i_action] = cb;
414 tabcb[item->i_action + 1] = NULL;
416 tabtext = realloc (item->ppsz_action_text,
417 (item->i_action + 2) * sizeof (name));
420 item->ppsz_action_text = tabtext;
423 tabtext[item->i_action] = strdup (name);
425 tabtext[item->i_action] = NULL;
426 tabtext[item->i_action + 1] = NULL;
433 fprintf (stderr, "LibVLC: unknown module property %d\n", propid);
434 fprintf (stderr, "LibVLC: too old to use this module?\n");