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);
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);
60 vlc_mutex_init (&module->lock);
62 module->psz_shortname = NULL;
63 module->psz_longname = (char*)default_name;
64 module->psz_help = NULL;
65 for (unsigned i = 0; i < MODULE_SHORTCUT_MAX; i++)
66 module->pp_shortcuts[i] = NULL;
67 module->psz_capability = (char*)"";
70 module->b_unloadable = true;
71 module->b_reentrant = true;
72 module->b_submodule = false;
73 module->pf_activate = NULL;
74 module->pf_deactivate = NULL;
75 module->p_config = NULL;
77 module->i_config_items = 0;
78 module->i_bool_items = 0;
79 /*module->handle = garbage */
80 module->psz_filename = NULL;
81 module->b_builtin = false;
82 module->b_loaded = false;
89 static void vlc_submodule_destruct (gc_object_t *obj)
91 module_t *module = vlc_priv (obj, module_t);
95 module_t *vlc_submodule_create (module_t *module)
97 assert (module != NULL);
99 module_t *submodule = malloc (sizeof (*submodule));
100 if (submodule == NULL)
103 memset (submodule, 0, sizeof (*submodule));
104 vlc_gc_init (submodule, vlc_submodule_destruct);
106 submodule->next = module->submodule;
107 submodule->parent = module;
108 module->submodule = submodule;
109 module->submodule_count++;
111 /* Muahahaha! Heritage! Polymorphism! Ugliness!! */
112 memcpy (submodule->pp_shortcuts, module->pp_shortcuts,
113 sizeof (submodule->pp_shortcuts));
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->i_cpu = module->i_cpu;
121 submodule->b_submodule = true;
126 int vlc_module_set (module_t *module, int propid, ...)
129 int ret = VLC_SUCCESS;
131 va_start (ap, propid);
134 case VLC_MODULE_CPU_REQUIREMENT:
135 assert (!module->b_submodule);
136 module->i_cpu |= va_arg (ap, int);
139 case VLC_MODULE_SHORTCUT:
142 for (i = 0; module->pp_shortcuts[i] != NULL; i++);
143 if (i >= (MODULE_SHORTCUT_MAX - 1))
149 module->pp_shortcuts[i] = va_arg (ap, char *);
153 case VLC_MODULE_SHORTNAME_NODOMAIN:
155 const char *name = va_arg (ap, char *);
156 ret = vlc_module_set (module, VLC_MODULE_SHORTNAME, NULL, name);
160 case VLC_MODULE_DESCRIPTION_NODOMAIN:
162 const char *desc = va_arg (ap, char *);
163 ret = vlc_module_set (module, VLC_MODULE_DESCRIPTION, NULL, desc);
167 case VLC_MODULE_HELP_NODOMAIN:
169 const char *help = va_arg (ap, char *);
170 ret = vlc_module_set (module, VLC_MODULE_HELP, NULL, help);
174 case VLC_MODULE_CAPABILITY:
175 module->psz_capability = va_arg (ap, char *);
178 case VLC_MODULE_SCORE:
179 module->i_score = va_arg (ap, int);
182 case VLC_MODULE_PROGRAM:
183 fprintf (stderr, "deprecated module property %d", propid);
186 case VLC_MODULE_CB_OPEN:
187 module->pf_activate = va_arg (ap, int (*) (vlc_object_t *));
190 case VLC_MODULE_CB_CLOSE:
191 module->pf_deactivate = va_arg (ap, void (*) (vlc_object_t *));
194 case VLC_MODULE_NO_UNLOAD:
195 module->b_unloadable = false;
198 case VLC_MODULE_NAME:
200 const char *value = va_arg (ap, const char *);
201 free( module->psz_object_name );
202 module->psz_object_name = strdup( value );
203 module->pp_shortcuts[0] = (char*)value; /* dooh! */
204 if (module->psz_longname == default_name)
205 module->psz_longname = (char*)value; /* dooh! */
209 case VLC_MODULE_SHORTNAME:
211 const char *domain = va_arg (ap, const char *);
214 module->psz_shortname = dgettext (domain, va_arg (ap, char *));
218 case VLC_MODULE_DESCRIPTION:
220 const char *domain = va_arg (ap, const char *);
223 module->psz_longname = dgettext (domain, va_arg (ap, char *));
227 case VLC_MODULE_HELP:
229 const char *domain = va_arg (ap, const char *);
232 module->psz_help = dgettext (domain, va_arg (ap, char *));
237 fprintf (stderr, "LibVLC: unknown module property %d", propid);
238 fprintf (stderr, "LibVLC: too old to use this module?");
246 module_config_t *vlc_config_create (module_t *module, int type)
248 unsigned confsize = module->confsize;
249 module_config_t *tab = module->p_config;
251 if ((confsize & 0xf) == 0)
253 tab = realloc (tab, (confsize + 17) * sizeof (*tab));
257 module->p_config = tab;
260 memset (tab + confsize, 0, sizeof (tab[confsize]));
261 tab[confsize].i_type = type;
262 tab[confsize].p_lock = &module->lock;
264 if (type & CONFIG_ITEM)
266 module->i_config_items++;
267 if (type == CONFIG_ITEM_BOOL)
268 module->i_bool_items++;
272 return tab + confsize;
275 int vlc_config_set (module_config_t *restrict item, int id, ...)
280 assert (item != NULL);
285 case VLC_CONFIG_NAME:
287 const char *name = va_arg (ap, const char *);
288 vlc_callback_t cb = va_arg (ap, vlc_callback_t);
290 assert (name != NULL);
291 item->psz_name = strdup (name);
292 item->pf_callback = cb;
297 case VLC_CONFIG_DESC_NODOMAIN:
299 const char *text = va_arg (ap, const char *);
300 const char *longtext = va_arg (ap, const char *);
301 ret = vlc_config_set (item, VLC_CONFIG_DESC, NULL, text, longtext);
305 case VLC_CONFIG_VALUE:
307 if (IsConfigIntegerType (item->i_type))
309 item->orig.i = item->saved.i =
310 item->value.i = va_arg (ap, int);
314 if (IsConfigFloatType (item->i_type))
316 item->orig.f = item->saved.f =
317 item->value.f = va_arg (ap, double);
321 if (IsConfigStringType (item->i_type))
323 const char *value = va_arg (ap, const char *);
324 item->value.psz = value ? strdup (value) : NULL;
325 item->orig.psz = value ? strdup (value) : NULL;
326 item->saved.psz = value ? strdup (value) : NULL;
332 case VLC_CONFIG_RANGE:
334 if (IsConfigIntegerType (item->i_type))
336 item->min.i = va_arg (ap, int);
337 item->max.i = va_arg (ap, int);
341 if (IsConfigFloatType (item->i_type))
343 item->min.f = va_arg (ap, double);
344 item->max.f = va_arg (ap, double);
350 case VLC_CONFIG_ADVANCED:
351 item->b_advanced = true;
355 case VLC_CONFIG_VOLATILE:
356 item->b_unsaveable = true;
360 case VLC_CONFIG_PERSISTENT:
361 item->b_autosave = true;
365 case VLC_CONFIG_RESTART:
366 item->b_restart = true;
370 case VLC_CONFIG_PRIVATE:
371 item->b_internal = true;
375 case VLC_CONFIG_REMOVED:
376 item->b_removed = true;
380 case VLC_CONFIG_CAPABILITY:
382 const char *cap = va_arg (ap, const char *);
383 item->psz_type = cap ? strdup (cap) : NULL;
388 case VLC_CONFIG_SHORTCUT:
389 item->i_short = va_arg (ap, int);
393 case VLC_CONFIG_LIST_NODOMAIN:
395 size_t len = va_arg (ap, size_t);
396 if (IsConfigIntegerType (item->i_type))
398 const int *src = va_arg (ap, const int *);
399 const char *const *text = va_arg (ap, const char *const *);
400 ret = vlc_config_set (item, VLC_CONFIG_LIST, NULL, len, src,
404 if (IsConfigStringType (item->i_type))
406 const char *const *src = va_arg (ap, const char *const *);
407 const char *const *text = va_arg (ap, const char *const *);
408 ret = vlc_config_set (item, VLC_CONFIG_LIST, NULL, len, src,
414 case VLC_CONFIG_ADD_ACTION_NODOMAIN:
416 vlc_callback_t cb = va_arg (ap, vlc_callback_t);
417 const char *name = va_arg (ap, const char *);
418 ret = vlc_config_set (item, VLC_CONFIG_ADD_ACTION, NULL, cb, name);
422 case VLC_CONFIG_OLDNAME:
424 const char *oldname = va_arg (ap, const char *);
425 item->psz_oldname = oldname ? strdup (oldname) : NULL;
430 case VLC_CONFIG_SAFE:
435 case VLC_CONFIG_DESC:
437 const char *domain = va_arg (ap, const char *);
438 const char *text = va_arg (ap, const char *);
439 const char *longtext = va_arg (ap, const char *);
443 item->psz_text = text ? strdup (dgettext (domain, text)) : NULL;
445 longtext ? strdup (dgettext (domain, longtext)) : NULL;
450 case VLC_CONFIG_LIST:
452 const char *domain = va_arg (ap, const char *);
453 size_t len = va_arg (ap, size_t);
454 char **dtext = malloc (sizeof (char *) * (len + 1));
460 if (IsConfigIntegerType (item->i_type))
462 const int *src = va_arg (ap, const int *);
463 int *dst = malloc (sizeof (int) * (len + 1));
467 memcpy (dst, src, sizeof (int) * len);
474 if (IsConfigFloatType (item->i_type))
476 const float *src = va_arg (ap, const float *);
477 float *dst = malloc (sizeof (float) * (len + 1));
481 memcpy (dst, src, sizeof (float) * len);
488 if (IsConfigStringType (item->i_type))
490 const char *const *src = va_arg (ap, const char *const *);
491 char **dst = malloc (sizeof (char *) * (len + 1));
495 for (size_t i = 0; i < len; i++)
496 dst[i] = src[i] ? strdup (src[i]) : NULL;
499 item->ppsz_list = dst;
504 /* Copy textual descriptions */
508 const char *const *text = va_arg (ap, const char *const *);
511 for (size_t i = 0; i < len; i++)
513 text[i] ? strdup (dgettext (domain, text[i])) : NULL;
516 item->ppsz_list_text = dtext;
521 item->ppsz_list_text = NULL;
525 item->pf_update_list = va_arg (ap, vlc_callback_t);
530 case VLC_CONFIG_ADD_ACTION:
532 const char *domain = va_arg (ap, const char *);
533 vlc_callback_t cb = va_arg (ap, vlc_callback_t), *tabcb;
534 const char *name = va_arg (ap, const char *);
537 tabcb = realloc (item->ppf_action,
538 (item->i_action + 2) * sizeof (cb));
541 item->ppf_action = tabcb;
542 tabcb[item->i_action] = cb;
543 tabcb[item->i_action + 1] = NULL;
545 tabtext = realloc (item->ppsz_action_text,
546 (item->i_action + 2) * sizeof (name));
549 item->ppsz_action_text = tabtext;
554 tabtext[item->i_action] = strdup (dgettext (domain, name));
556 tabtext[item->i_action] = NULL;
557 tabtext[item->i_action + 1] = NULL;