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>
31 #include "modules/modules.h"
32 #include "config/configuration.h"
35 # define dgettext(d, m) ((char *)(m))
38 static void vlc_module_destruct (gc_object_t *obj)
40 module_t *module = vlc_priv (obj, module_t);
42 vlc_mutex_destroy (&module->lock);
43 free (module->psz_object_name);
47 static const char default_name[] = "unnamed";
49 module_t *vlc_module_create (vlc_object_t *obj)
51 module_t *module = malloc (sizeof (*module));
55 module->psz_object_name = strdup( default_name );
57 module->submodule = NULL;
58 module->parent = NULL;
59 module->submodule_count = 0;
60 vlc_gc_init (module, vlc_module_destruct);
61 vlc_mutex_init (&module->lock);
63 module->psz_shortname = NULL;
64 module->psz_longname = (char*)default_name;
65 module->psz_help = NULL;
66 for (unsigned i = 0; i < MODULE_SHORTCUT_MAX; i++)
67 module->pp_shortcuts[i] = NULL;
68 module->psz_capability = (char*)"";
71 module->b_unloadable = true;
72 module->b_reentrant = true;
73 module->b_submodule = false;
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->b_builtin = false;
83 module->b_loaded = false;
90 static void vlc_submodule_destruct (gc_object_t *obj)
92 module_t *module = vlc_priv (obj, module_t);
93 free (module->psz_object_name);
97 module_t *vlc_submodule_create (module_t *module)
99 assert (module != NULL);
101 module_t *submodule = calloc( 1, sizeof(*submodule) );
105 vlc_gc_init (submodule, vlc_submodule_destruct);
107 submodule->next = module->submodule;
108 submodule->parent = module;
109 module->submodule = submodule;
110 module->submodule_count++;
112 /* Muahahaha! Heritage! Polymorphism! Ugliness!! */
113 memcpy (submodule->pp_shortcuts, module->pp_shortcuts,
114 sizeof (submodule->pp_shortcuts));
116 submodule->psz_object_name = strdup( module->psz_object_name );
117 submodule->psz_shortname = module->psz_shortname;
118 submodule->psz_longname = module->psz_longname;
119 submodule->psz_capability = module->psz_capability;
120 submodule->i_score = module->i_score;
121 submodule->i_cpu = module->i_cpu;
122 submodule->b_submodule = true;
126 static module_config_t *vlc_config_create (module_t *module, int type)
128 unsigned confsize = module->confsize;
129 module_config_t *tab = module->p_config;
131 if ((confsize & 0xf) == 0)
133 tab = realloc (tab, (confsize + 17) * sizeof (*tab));
137 module->p_config = tab;
140 memset (tab + confsize, 0, sizeof (tab[confsize]));
141 tab[confsize].i_type = type;
142 tab[confsize].p_lock = &module->lock;
144 if (type & CONFIG_ITEM)
146 module->i_config_items++;
147 if (type == CONFIG_ITEM_BOOL)
148 module->i_bool_items++;
152 return tab + confsize;
156 int vlc_plugin_set (module_t *module, module_config_t *item, int propid, ...)
161 va_start (ap, propid);
164 case VLC_SUBMODULE_CREATE:
166 module_t **pp = va_arg (ap, module_t **);
167 *pp = vlc_submodule_create (module);
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_CPU_REQUIREMENT:
184 assert (!module->b_submodule);
185 module->i_cpu |= va_arg (ap, int);
188 case VLC_MODULE_SHORTCUT:
191 for (i = 0; module->pp_shortcuts[i] != NULL; i++);
192 if (i >= (MODULE_SHORTCUT_MAX - 1))
195 module->pp_shortcuts[i] = va_arg (ap, char *);
199 case VLC_MODULE_CAPABILITY:
200 module->psz_capability = va_arg (ap, char *);
203 case VLC_MODULE_SCORE:
204 module->i_score = va_arg (ap, int);
207 case VLC_MODULE_CB_OPEN:
208 module->pf_activate = va_arg (ap, int (*) (vlc_object_t *));
211 case VLC_MODULE_CB_CLOSE:
212 module->pf_deactivate = va_arg (ap, void (*) (vlc_object_t *));
215 case VLC_MODULE_NO_UNLOAD:
216 module->b_unloadable = false;
219 case VLC_MODULE_NAME:
221 const char *value = va_arg (ap, const char *);
222 free( module->psz_object_name );
223 module->psz_object_name = strdup( value );
224 module->pp_shortcuts[0] = (char*)value; /* dooh! */
225 if (module->psz_longname == default_name)
226 module->psz_longname = (char*)value; /* dooh! */
230 case VLC_MODULE_SHORTNAME:
232 const char *domain = va_arg (ap, const char *);
235 module->psz_shortname = dgettext (domain, va_arg (ap, char *));
239 case VLC_MODULE_DESCRIPTION:
241 const char *domain = va_arg (ap, const char *);
244 module->psz_longname = dgettext (domain, va_arg (ap, char *));
248 case VLC_MODULE_HELP:
250 const char *domain = va_arg (ap, const char *);
253 module->psz_help = dgettext (domain, va_arg (ap, char *));
257 case VLC_CONFIG_NAME:
259 const char *name = va_arg (ap, const char *);
260 vlc_callback_t cb = va_arg (ap, vlc_callback_t);
262 assert (name != NULL);
263 item->psz_name = strdup (name);
264 item->pf_callback = cb;
268 case VLC_CONFIG_VALUE:
270 if (IsConfigIntegerType (item->i_type))
272 item->orig.i = item->saved.i =
273 item->value.i = va_arg (ap, int);
276 if (IsConfigFloatType (item->i_type))
278 item->orig.f = item->saved.f =
279 item->value.f = va_arg (ap, double);
282 if (IsConfigStringType (item->i_type))
284 const char *value = va_arg (ap, const char *);
285 item->value.psz = value ? strdup (value) : NULL;
286 item->orig.psz = value ? strdup (value) : NULL;
287 item->saved.psz = value ? strdup (value) : NULL;
292 case VLC_CONFIG_RANGE:
294 if (IsConfigIntegerType (item->i_type)
295 || item->i_type == CONFIG_ITEM_MODULE_LIST_CAT
296 || item->i_type == CONFIG_ITEM_MODULE_CAT)
298 item->min.i = va_arg (ap, int);
299 item->max.i = va_arg (ap, int);
302 if (IsConfigFloatType (item->i_type))
304 item->min.f = va_arg (ap, double);
305 item->max.f = va_arg (ap, double);
310 case VLC_CONFIG_ADVANCED:
311 item->b_advanced = true;
314 case VLC_CONFIG_VOLATILE:
315 item->b_unsaveable = true;
318 case VLC_CONFIG_PERSISTENT:
319 item->b_autosave = true;
322 case VLC_CONFIG_RESTART:
323 item->b_restart = true;
326 case VLC_CONFIG_PRIVATE:
327 item->b_internal = true;
330 case VLC_CONFIG_REMOVED:
331 item->b_removed = true;
334 case VLC_CONFIG_CAPABILITY:
336 const char *cap = va_arg (ap, const char *);
337 item->psz_type = cap ? strdup (cap) : NULL;
341 case VLC_CONFIG_SHORTCUT:
342 item->i_short = va_arg (ap, int);
345 case VLC_CONFIG_OLDNAME:
347 const char *oldname = va_arg (ap, const char *);
348 item->psz_oldname = oldname ? strdup (oldname) : NULL;
352 case VLC_CONFIG_SAFE:
356 case VLC_CONFIG_DESC:
358 const char *domain = va_arg (ap, const char *);
359 const char *text = va_arg (ap, const char *);
360 const char *longtext = va_arg (ap, const char *);
364 item->psz_text = text ? strdup (dgettext (domain, text)) : NULL;
366 longtext ? strdup (dgettext (domain, longtext)) : NULL;
370 case VLC_CONFIG_LIST:
372 const char *domain = va_arg (ap, const char *);
373 size_t len = va_arg (ap, size_t);
376 if (IsConfigIntegerType (item->i_type))
378 const int *src = va_arg (ap, const int *);
379 int *dst = malloc (sizeof (int) * (len + 1));
383 memcpy (dst, src, sizeof (int) * len);
389 if (IsConfigStringType (item->i_type))
391 const char *const *src = va_arg (ap, const char *const *);
392 char **dst = malloc (sizeof (char *) * (len + 1));
396 for (size_t i = 0; i < len; i++)
397 dst[i] = src[i] ? strdup (src[i]) : NULL;
400 item->ppsz_list = dst;
405 /* Copy textual descriptions */
409 const char *const *text = va_arg (ap, const char *const *);
412 char **dtext = malloc (sizeof (char *) * (len + 1));
415 for (size_t i = 0; i < len; i++)
417 strdup( dgettext( domain, text[i] ) ) :
421 item->ppsz_list_text = dtext;
424 item->ppsz_list_text = NULL;
427 item->pf_update_list = va_arg (ap, vlc_callback_t);
431 case VLC_CONFIG_ADD_ACTION:
433 const char *domain = va_arg (ap, const char *);
434 vlc_callback_t cb = va_arg (ap, vlc_callback_t), *tabcb;
435 const char *name = va_arg (ap, const char *);
438 tabcb = realloc (item->ppf_action,
439 (item->i_action + 2) * sizeof (cb));
442 item->ppf_action = tabcb;
443 tabcb[item->i_action] = cb;
444 tabcb[item->i_action + 1] = NULL;
446 tabtext = realloc (item->ppsz_action_text,
447 (item->i_action + 2) * sizeof (name));
450 item->ppsz_action_text = tabtext;
455 tabtext[item->i_action] = strdup (dgettext (domain, name));
457 tabtext[item->i_action] = NULL;
458 tabtext[item->i_action + 1] = NULL;
465 fprintf (stderr, "LibVLC: unknown module property %d\n", propid);
466 fprintf (stderr, "LibVLC: too old to use this module?\n");