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>
33 #include "modules/modules.h"
34 #include "config/configuration.h"
37 # define dgettext(d, m) ((char *)(m))
40 static void vlc_module_destruct (gc_object_t *obj)
42 module_t *module = vlc_priv (obj, module_t);
44 vlc_mutex_destroy (&module->lock);
45 free (module->psz_object_name);
49 static const char default_name[] = "unnamed";
51 module_t *vlc_module_create (vlc_object_t *obj)
53 module_t *module = malloc (sizeof (*module));
57 module->psz_object_name = strdup( default_name );
59 module->submodule = NULL;
60 module->parent = NULL;
61 module->submodule_count = 0;
62 vlc_gc_init (module, vlc_module_destruct);
63 vlc_mutex_init (&module->lock);
65 module->psz_shortname = NULL;
66 module->psz_longname = (char*)default_name;
67 module->psz_help = NULL;
68 for (unsigned i = 0; i < MODULE_SHORTCUT_MAX; i++)
69 module->pp_shortcuts[i] = NULL;
70 module->psz_capability = (char*)"";
73 module->b_unloadable = true;
74 module->b_reentrant = true;
75 module->b_submodule = false;
76 module->pf_activate = NULL;
77 module->pf_deactivate = NULL;
78 module->p_config = NULL;
80 module->i_config_items = 0;
81 module->i_bool_items = 0;
82 /*module->handle = garbage */
83 module->psz_filename = NULL;
84 module->b_builtin = false;
85 module->b_loaded = false;
92 static void vlc_submodule_destruct (gc_object_t *obj)
94 module_t *module = vlc_priv (obj, module_t);
95 free (module->psz_object_name);
99 module_t *vlc_submodule_create (module_t *module)
101 assert (module != NULL);
103 module_t *submodule = calloc( 1, sizeof(*submodule) );
107 vlc_gc_init (submodule, vlc_submodule_destruct);
109 submodule->next = module->submodule;
110 submodule->parent = module;
111 module->submodule = submodule;
112 module->submodule_count++;
114 /* Muahahaha! Heritage! Polymorphism! Ugliness!! */
115 memcpy (submodule->pp_shortcuts, module->pp_shortcuts,
116 sizeof (submodule->pp_shortcuts));
118 submodule->psz_object_name = strdup( module->psz_object_name );
119 submodule->psz_shortname = module->psz_shortname;
120 submodule->psz_longname = module->psz_longname;
121 submodule->psz_capability = module->psz_capability;
122 submodule->i_score = module->i_score;
123 submodule->i_cpu = module->i_cpu;
124 submodule->b_submodule = true;
129 static int vlc_module_set (module_t *module, int propid, va_list ap)
131 int ret = VLC_SUCCESS;
135 case VLC_MODULE_CPU_REQUIREMENT:
136 assert (!module->b_submodule);
137 module->i_cpu |= va_arg (ap, int);
140 case VLC_MODULE_SHORTCUT:
143 for (i = 0; module->pp_shortcuts[i] != NULL; i++);
144 if (i >= (MODULE_SHORTCUT_MAX - 1))
150 module->pp_shortcuts[i] = va_arg (ap, char *);
154 case VLC_MODULE_CAPABILITY:
155 module->psz_capability = va_arg (ap, char *);
158 case VLC_MODULE_SCORE:
159 module->i_score = va_arg (ap, int);
162 case VLC_MODULE_CB_OPEN:
163 module->pf_activate = va_arg (ap, int (*) (vlc_object_t *));
166 case VLC_MODULE_CB_CLOSE:
167 module->pf_deactivate = va_arg (ap, void (*) (vlc_object_t *));
170 case VLC_MODULE_NO_UNLOAD:
171 module->b_unloadable = false;
174 case VLC_MODULE_NAME:
176 const char *value = va_arg (ap, const char *);
177 free( module->psz_object_name );
178 module->psz_object_name = strdup( value );
179 module->pp_shortcuts[0] = (char*)value; /* dooh! */
180 if (module->psz_longname == default_name)
181 module->psz_longname = (char*)value; /* dooh! */
185 case VLC_MODULE_SHORTNAME:
187 const char *domain = va_arg (ap, const char *);
190 module->psz_shortname = dgettext (domain, va_arg (ap, char *));
194 case VLC_MODULE_DESCRIPTION:
196 const char *domain = va_arg (ap, const char *);
199 module->psz_longname = dgettext (domain, va_arg (ap, char *));
203 case VLC_MODULE_HELP:
205 const char *domain = va_arg (ap, const char *);
208 module->psz_help = dgettext (domain, va_arg (ap, char *));
213 fprintf (stderr, "LibVLC: unknown module property %d", propid);
214 fprintf (stderr, "LibVLC: too old to use this module?");
221 module_config_t *vlc_config_create (module_t *module, int type)
223 unsigned confsize = module->confsize;
224 module_config_t *tab = module->p_config;
226 if ((confsize & 0xf) == 0)
228 tab = realloc (tab, (confsize + 17) * sizeof (*tab));
232 module->p_config = tab;
235 memset (tab + confsize, 0, sizeof (tab[confsize]));
236 tab[confsize].i_type = type;
237 tab[confsize].p_lock = &module->lock;
239 if (type & CONFIG_ITEM)
241 module->i_config_items++;
242 if (type == CONFIG_ITEM_BOOL)
243 module->i_bool_items++;
247 return tab + confsize;
250 static int vlc_config_set (module_config_t *restrict item, int id, va_list ap)
254 assert (item != NULL);
258 case VLC_CONFIG_NAME:
260 const char *name = va_arg (ap, const char *);
261 vlc_callback_t cb = va_arg (ap, vlc_callback_t);
263 assert (name != NULL);
264 item->psz_name = strdup (name);
265 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, int);
279 if (IsConfigFloatType (item->i_type))
281 item->orig.f = item->saved.f =
282 item->value.f = va_arg (ap, double);
286 if (IsConfigStringType (item->i_type))
288 const char *value = va_arg (ap, const char *);
289 item->value.psz = value ? strdup (value) : NULL;
290 item->orig.psz = value ? strdup (value) : NULL;
291 item->saved.psz = value ? strdup (value) : NULL;
297 case VLC_CONFIG_RANGE:
299 if (IsConfigIntegerType (item->i_type)
300 || item->i_type == CONFIG_ITEM_MODULE_LIST_CAT
301 || item->i_type == CONFIG_ITEM_MODULE_CAT)
303 item->min.i = va_arg (ap, int);
304 item->max.i = va_arg (ap, int);
308 if (IsConfigFloatType (item->i_type))
310 item->min.f = va_arg (ap, double);
311 item->max.f = va_arg (ap, double);
317 case VLC_CONFIG_ADVANCED:
318 item->b_advanced = true;
322 case VLC_CONFIG_VOLATILE:
323 item->b_unsaveable = true;
327 case VLC_CONFIG_PERSISTENT:
328 item->b_autosave = true;
332 case VLC_CONFIG_RESTART:
333 item->b_restart = true;
337 case VLC_CONFIG_PRIVATE:
338 item->b_internal = true;
342 case VLC_CONFIG_REMOVED:
343 item->b_removed = true;
347 case VLC_CONFIG_CAPABILITY:
349 const char *cap = va_arg (ap, const char *);
350 item->psz_type = cap ? strdup (cap) : NULL;
355 case VLC_CONFIG_SHORTCUT:
356 item->i_short = va_arg (ap, int);
360 case VLC_CONFIG_OLDNAME:
362 const char *oldname = va_arg (ap, const char *);
363 item->psz_oldname = oldname ? strdup (oldname) : NULL;
368 case VLC_CONFIG_SAFE:
373 case VLC_CONFIG_DESC:
375 const char *domain = va_arg (ap, const char *);
376 const char *text = va_arg (ap, const char *);
377 const char *longtext = va_arg (ap, const char *);
381 item->psz_text = text ? strdup (dgettext (domain, text)) : NULL;
383 longtext ? strdup (dgettext (domain, longtext)) : NULL;
388 case VLC_CONFIG_LIST:
390 const char *domain = va_arg (ap, const char *);
391 size_t len = va_arg (ap, size_t);
394 if (IsConfigIntegerType (item->i_type))
396 const int *src = va_arg (ap, const int *);
397 int *dst = malloc (sizeof (int) * (len + 1));
401 memcpy (dst, src, sizeof (int) * len);
408 if (IsConfigFloatType (item->i_type))
410 const float *src = va_arg (ap, const float *);
411 float *dst = malloc (sizeof (float) * (len + 1));
415 memcpy (dst, src, sizeof (float) * len);
422 if (IsConfigStringType (item->i_type))
424 const char *const *src = va_arg (ap, const char *const *);
425 char **dst = malloc (sizeof (char *) * (len + 1));
429 for (size_t i = 0; i < len; i++)
430 dst[i] = src[i] ? strdup (src[i]) : NULL;
433 item->ppsz_list = dst;
438 /* Copy textual descriptions */
442 const char *const *text = va_arg (ap, const char *const *);
445 char **dtext = malloc (sizeof (char *) * (len + 1));
448 for (size_t i = 0; i < len; i++)
450 strdup( dgettext( domain, text[i] ) ) :
454 item->ppsz_list_text = dtext;
457 item->ppsz_list_text = NULL;
460 item->pf_update_list = va_arg (ap, vlc_callback_t);
465 case VLC_CONFIG_ADD_ACTION:
467 const char *domain = va_arg (ap, const char *);
468 vlc_callback_t cb = va_arg (ap, vlc_callback_t), *tabcb;
469 const char *name = va_arg (ap, const char *);
472 tabcb = realloc (item->ppf_action,
473 (item->i_action + 2) * sizeof (cb));
476 item->ppf_action = tabcb;
477 tabcb[item->i_action] = cb;
478 tabcb[item->i_action + 1] = NULL;
480 tabtext = realloc (item->ppsz_action_text,
481 (item->i_action + 2) * sizeof (name));
484 item->ppsz_action_text = tabtext;
489 tabtext[item->i_action] = strdup (dgettext (domain, name));
491 tabtext[item->i_action] = NULL;
492 tabtext[item->i_action + 1] = NULL;
502 int vlc_plugin_set (module_t *module, module_config_t *cfg, int id, ...)
509 ret = vlc_module_set (module, id, ap);
510 else if (cfg != NULL)
511 ret = vlc_config_set (cfg, id, ap);