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->pp_shortcuts);
41 free (module->psz_object_name);
45 static const char default_name[] = "unnamed";
47 module_t *vlc_module_create (vlc_object_t *obj)
49 module_t *module = malloc (sizeof (*module));
53 module->psz_object_name = strdup( default_name );
55 module->submodule = NULL;
56 module->parent = NULL;
57 module->submodule_count = 0;
58 vlc_gc_init (module, vlc_module_destruct);
60 module->psz_shortname = NULL;
61 module->psz_longname = (char*)default_name;
62 module->psz_help = NULL;
63 module->pp_shortcuts = NULL;
64 module->i_shortcuts = 0;
65 module->psz_capability = (char*)"";
67 module->b_unloadable = true;
68 module->b_submodule = false;
69 module->pf_activate = NULL;
70 module->pf_deactivate = NULL;
71 module->p_config = NULL;
73 module->i_config_items = 0;
74 module->i_bool_items = 0;
75 /*module->handle = garbage */
76 module->psz_filename = NULL;
77 module->domain = NULL;
78 module->b_builtin = false;
79 module->b_loaded = false;
86 static void vlc_submodule_destruct (gc_object_t *obj)
88 module_t *module = vlc_priv (obj, module_t);
89 free (module->pp_shortcuts);
90 free (module->psz_object_name);
94 module_t *vlc_submodule_create (module_t *module)
96 assert (module != NULL);
98 module_t *submodule = calloc( 1, sizeof(*submodule) );
102 vlc_gc_init (submodule, vlc_submodule_destruct);
104 submodule->next = module->submodule;
105 submodule->parent = module;
106 module->submodule = submodule;
107 module->submodule_count++;
109 /* Muahahaha! Heritage! Polymorphism! Ugliness!! */
110 submodule->pp_shortcuts = malloc( sizeof( char ** ) );
111 submodule->pp_shortcuts[0] = module->pp_shortcuts[0]; /* object name */
112 submodule->i_shortcuts = 1;
114 submodule->psz_object_name = strdup( module->psz_object_name );
115 submodule->psz_shortname = module->psz_shortname;
116 submodule->psz_longname = module->psz_longname;
117 submodule->psz_capability = module->psz_capability;
118 submodule->i_score = module->i_score;
119 submodule->b_submodule = true;
120 submodule->domain = module->domain;
124 static module_config_t *vlc_config_create (module_t *module, int type)
126 unsigned confsize = module->confsize;
127 module_config_t *tab = module->p_config;
129 if ((confsize & 0xf) == 0)
131 tab = realloc_or_free (tab, (confsize + 17) * sizeof (*tab));
135 module->p_config = tab;
138 memset (tab + confsize, 0, sizeof (tab[confsize]));
139 tab[confsize].i_type = type;
141 if (type & CONFIG_ITEM)
143 module->i_config_items++;
144 if (type == CONFIG_ITEM_BOOL)
145 module->i_bool_items++;
149 return tab + confsize;
153 int vlc_plugin_set (module_t *module, module_config_t *item, int propid, ...)
157 typedef int(*int_fp_vlcobjectt)(vlc_object_t *) ;
158 typedef void(*void_fp_vlcobjectt)(vlc_object_t *);
160 va_start (ap, propid);
163 case VLC_SUBMODULE_CREATE:
165 module_t **pp = va_arg (ap, module_t **);
166 *pp = vlc_submodule_create (module);
172 case VLC_CONFIG_CREATE:
174 int type = va_arg (ap, int);
175 module_config_t **pp = va_arg (ap, module_config_t **);
176 *pp = vlc_config_create (module, type);
182 case VLC_MODULE_SHORTCUT:
184 unsigned i_shortcuts = va_arg (ap, unsigned);
185 unsigned index = module->i_shortcuts;
186 module->i_shortcuts += i_shortcuts;
188 module->pp_shortcuts = realloc (module->pp_shortcuts, sizeof( char ** ) * module->i_shortcuts);
189 for (; index < module->i_shortcuts; index++)
191 const char *psz_new = va_arg (ap, const char*);
192 module->pp_shortcuts[index] = psz_new;
197 case VLC_MODULE_CAPABILITY:
198 module->psz_capability = va_arg (ap, char *);
201 case VLC_MODULE_SCORE:
202 module->i_score = va_arg (ap, int);
205 case VLC_MODULE_CB_OPEN:
206 module->pf_activate = va_arg (ap, int_fp_vlcobjectt);
209 case VLC_MODULE_CB_CLOSE:
210 module->pf_deactivate = va_arg (ap, void_fp_vlcobjectt);
213 case VLC_MODULE_NO_UNLOAD:
214 module->b_unloadable = false;
217 case VLC_MODULE_NAME:
219 const char *value = va_arg (ap, const char *);
220 free( module->psz_object_name );
221 module->psz_object_name = strdup( value );
222 module->pp_shortcuts = malloc( sizeof( char ** ) );
223 module->pp_shortcuts[0] = (char*)value; /* dooh! */
224 module->i_shortcuts = 1;
226 if (module->psz_longname == default_name)
227 module->psz_longname = (char*)value; /* dooh! */
231 case VLC_MODULE_SHORTNAME:
232 module->psz_shortname = va_arg (ap, char *);
235 case VLC_MODULE_DESCRIPTION:
236 module->psz_longname = va_arg (ap, char *);
239 case VLC_MODULE_HELP:
240 module->psz_help = va_arg (ap, char *);
243 case VLC_MODULE_TEXTDOMAIN:
244 module->domain = va_arg (ap, char *);
247 case VLC_CONFIG_NAME:
249 const char *name = va_arg (ap, const char *);
250 vlc_callback_t cb = va_arg (ap, vlc_callback_t);
252 assert (name != NULL);
253 item->psz_name = strdup (name);
254 item->pf_callback = cb;
258 case VLC_CONFIG_VALUE:
260 if (IsConfigIntegerType (item->i_type))
262 item->orig.i = item->saved.i =
263 item->value.i = va_arg (ap, int);
266 if (IsConfigFloatType (item->i_type))
268 item->orig.f = item->saved.f =
269 item->value.f = va_arg (ap, double);
272 if (IsConfigStringType (item->i_type))
274 const char *value = va_arg (ap, const char *);
275 item->value.psz = value ? strdup (value) : NULL;
276 item->orig.psz = value ? strdup (value) : NULL;
277 item->saved.psz = value ? strdup (value) : NULL;
282 case VLC_CONFIG_RANGE:
284 if (IsConfigIntegerType (item->i_type)
285 || item->i_type == CONFIG_ITEM_MODULE_LIST_CAT
286 || item->i_type == CONFIG_ITEM_MODULE_CAT)
288 item->min.i = va_arg (ap, int);
289 item->max.i = va_arg (ap, int);
292 if (IsConfigFloatType (item->i_type))
294 item->min.f = va_arg (ap, double);
295 item->max.f = va_arg (ap, double);
300 case VLC_CONFIG_ADVANCED:
301 item->b_advanced = true;
304 case VLC_CONFIG_VOLATILE:
305 item->b_unsaveable = true;
308 case VLC_CONFIG_PERSISTENT:
309 item->b_autosave = true;
312 case VLC_CONFIG_RESTART:
313 item->b_restart = true;
316 case VLC_CONFIG_PRIVATE:
317 item->b_internal = true;
320 case VLC_CONFIG_REMOVED:
321 item->b_removed = true;
324 case VLC_CONFIG_CAPABILITY:
326 const char *cap = va_arg (ap, const char *);
327 item->psz_type = cap ? strdup (cap) : NULL;
331 case VLC_CONFIG_SHORTCUT:
332 item->i_short = va_arg (ap, int);
335 case VLC_CONFIG_OLDNAME:
337 const char *oldname = va_arg (ap, const char *);
338 item->psz_oldname = oldname ? strdup (oldname) : NULL;
342 case VLC_CONFIG_SAFE:
346 case VLC_CONFIG_DESC:
348 const char *text = va_arg (ap, const char *);
349 const char *longtext = va_arg (ap, const char *);
351 item->psz_text = text ? strdup (text) : NULL;
352 item->psz_longtext = longtext ? strdup (longtext) : NULL;
356 case VLC_CONFIG_LIST:
358 size_t len = va_arg (ap, size_t);
361 if (IsConfigIntegerType (item->i_type))
363 const int *src = va_arg (ap, const int *);
364 int *dst = malloc (sizeof (int) * (len + 1));
368 memcpy (dst, src, sizeof (int) * len);
374 if (IsConfigStringType (item->i_type))
376 const char *const *src = va_arg (ap, const char *const *);
377 char **dst = malloc (sizeof (char *) * (len + 1));
381 for (size_t i = 0; i < len; i++)
382 dst[i] = src[i] ? strdup (src[i]) : NULL;
385 item->ppsz_list = dst;
390 /* Copy textual descriptions */
391 const char *const *text = va_arg (ap, const char *const *);
394 char **dtext = malloc (sizeof (char *) * (len + 1));
397 for (size_t i = 0; i < len; i++)
398 dtext[i] = text[i] ? strdup (text[i]) : NULL;
401 item->ppsz_list_text = dtext;
404 item->ppsz_list_text = NULL;
407 item->pf_update_list = va_arg (ap, vlc_callback_t);
411 case VLC_CONFIG_ADD_ACTION:
413 vlc_callback_t cb = va_arg (ap, vlc_callback_t), *tabcb;
414 const char *name = va_arg (ap, const char *);
417 tabcb = realloc (item->ppf_action,
418 (item->i_action + 2) * sizeof (cb));
421 item->ppf_action = tabcb;
422 tabcb[item->i_action] = cb;
423 tabcb[item->i_action + 1] = NULL;
425 tabtext = realloc (item->ppsz_action_text,
426 (item->i_action + 2) * sizeof (name));
429 item->ppsz_action_text = tabtext;
432 tabtext[item->i_action] = strdup (name);
434 tabtext[item->i_action] = NULL;
435 tabtext[item->i_action + 1] = NULL;
442 fprintf (stderr, "LibVLC: unknown module property %d\n", propid);
443 fprintf (stderr, "LibVLC: too old to use this module?\n");