]> git.sesse.net Git - vlc/blob - src/modules/entry.c
1b5dba6f4f889fcd673f095de218aa3ed1e88fa8
[vlc] / src / modules / entry.c
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
6  *
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.
11  *
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.
16  *
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  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <vlc_common.h>
27 #include <vlc_plugin.h>
28 #include <vlc_memory.h>
29 #include <assert.h>
30 #include <stdarg.h>
31
32 #include "modules/modules.h"
33 #include "config/configuration.h"
34 #include "libvlc.h"
35
36 static void vlc_module_destruct (gc_object_t *obj)
37 {
38     module_t *module = vlc_priv (obj, module_t);
39
40     vlc_mutex_destroy (&module->lock);
41     free (module->psz_object_name);
42     free (module);
43 }
44
45 static const char default_name[] = "unnamed";
46
47 module_t *vlc_module_create (vlc_object_t *obj)
48 {
49     module_t *module = malloc (sizeof (*module));
50     if (module == NULL)
51         return NULL;
52
53     module->psz_object_name = strdup( default_name );
54     module->next = NULL;
55     module->submodule = NULL;
56     module->parent = NULL;
57     module->submodule_count = 0;
58     vlc_gc_init (module, vlc_module_destruct);
59     vlc_mutex_init (&module->lock);
60
61     module->psz_shortname = NULL;
62     module->psz_longname = (char*)default_name;
63     module->psz_help = NULL;
64     for (unsigned i = 0; i < MODULE_SHORTCUT_MAX; i++)
65         module->pp_shortcuts[i] = NULL;
66     module->psz_capability = (char*)"";
67     module->i_score = 1;
68     module->b_unloadable = true;
69     module->b_submodule = false;
70     module->pf_activate = NULL;
71     module->pf_deactivate = NULL;
72     module->p_config = NULL;
73     module->confsize = 0;
74     module->i_config_items = 0;
75     module->i_bool_items = 0;
76     /*module->handle = garbage */
77     module->psz_filename = NULL;
78     module->b_builtin = false;
79     module->b_loaded = false;
80
81     (void)obj;
82     return module;
83 }
84
85
86 static void vlc_submodule_destruct (gc_object_t *obj)
87 {
88     module_t *module = vlc_priv (obj, module_t);
89     free (module->psz_object_name);
90     free (module);
91 }
92
93 module_t *vlc_submodule_create (module_t *module)
94 {
95     assert (module != NULL);
96
97     module_t *submodule = calloc( 1, sizeof(*submodule) );
98     if( !submodule )
99         return NULL;
100
101     vlc_gc_init (submodule, vlc_submodule_destruct);
102
103     submodule->next = module->submodule;
104     submodule->parent = module;
105     module->submodule = submodule;
106     module->submodule_count++;
107
108     /* Muahahaha! Heritage! Polymorphism! Ugliness!! */
109     submodule->pp_shortcuts[0] = module->pp_shortcuts[0]; /* object name */
110     for (unsigned i = 1; i < MODULE_SHORTCUT_MAX; i++)
111         submodule->pp_shortcuts[i] = NULL;
112
113     submodule->psz_object_name = strdup( module->psz_object_name );
114     submodule->psz_shortname = module->psz_shortname;
115     submodule->psz_longname = module->psz_longname;
116     submodule->psz_capability = module->psz_capability;
117     submodule->i_score = module->i_score;
118     submodule->b_submodule = true;
119     return submodule;
120 }
121
122 static module_config_t *vlc_config_create (module_t *module, int type)
123 {
124     unsigned confsize = module->confsize;
125     module_config_t *tab = module->p_config;
126
127     if ((confsize & 0xf) == 0)
128     {
129         tab = realloc_or_free (tab, (confsize + 17) * sizeof (*tab));
130         if (tab == NULL)
131             return NULL;
132
133         module->p_config = tab;
134     }
135
136     memset (tab + confsize, 0, sizeof (tab[confsize]));
137     tab[confsize].i_type = type;
138     tab[confsize].p_lock = &module->lock;
139
140     if (type & CONFIG_ITEM)
141     {
142         module->i_config_items++;
143         if (type == CONFIG_ITEM_BOOL)
144             module->i_bool_items++;
145     }
146
147     module->confsize++;
148     return tab + confsize;
149 }
150
151
152 int vlc_plugin_set (module_t *module, module_config_t *item, int propid, ...)
153 {
154     va_list ap;
155     int ret = 0;
156
157     va_start (ap, propid);
158     switch (propid)
159     {
160         case VLC_SUBMODULE_CREATE:
161         {
162             module_t **pp = va_arg (ap, module_t **);
163             *pp = vlc_submodule_create (module);
164             if (*pp == NULL)
165                 ret = -1;
166             break;
167         }
168
169         case VLC_CONFIG_CREATE:
170         {
171             int type = va_arg (ap, int);
172             module_config_t **pp = va_arg (ap, module_config_t **);
173             *pp = vlc_config_create (module, type);
174             if (*pp == NULL)
175                 ret = -1;
176             break;
177         }
178
179         case VLC_MODULE_SHORTCUT:
180         {
181             unsigned i;
182             for (i = 0; module->pp_shortcuts[i] != NULL; i++);
183                 if (i >= (MODULE_SHORTCUT_MAX - 1))
184                     break;
185
186             module->pp_shortcuts[i] = va_arg (ap, char *);
187             break;
188         }
189
190         case VLC_MODULE_CAPABILITY:
191             module->psz_capability = va_arg (ap, char *);
192             break;
193
194         case VLC_MODULE_SCORE:
195             module->i_score = va_arg (ap, int);
196             break;
197
198         case VLC_MODULE_CB_OPEN:
199             module->pf_activate = va_arg (ap, int (*) (vlc_object_t *));
200             break;
201
202         case VLC_MODULE_CB_CLOSE:
203             module->pf_deactivate = va_arg (ap, void (*) (vlc_object_t *));
204             break;
205
206         case VLC_MODULE_NO_UNLOAD:
207             module->b_unloadable = false;
208             break;
209
210         case VLC_MODULE_NAME:
211         {
212             const char *value = va_arg (ap, const char *);
213             free( module->psz_object_name );
214             module->psz_object_name = strdup( value );
215             module->pp_shortcuts[0] = (char*)value; /* dooh! */
216             if (module->psz_longname == default_name)
217                 module->psz_longname = (char*)value; /* dooh! */
218             break;
219         }
220
221         case VLC_MODULE_SHORTNAME:
222             module->psz_shortname = va_arg (ap, char *);
223             break;
224
225         case VLC_MODULE_DESCRIPTION:
226             module->psz_longname = va_arg (ap, char *);
227             break;
228
229         case VLC_MODULE_HELP:
230             module->psz_help = va_arg (ap, char *);
231             break;
232
233         case VLC_MODULE_TEXTDOMAIN:
234             (void) va_arg (ap, const char *);
235             /* FIXME: not implemented */
236             break;
237
238         case VLC_CONFIG_NAME:
239         {
240             const char *name = va_arg (ap, const char *);
241             vlc_callback_t cb = va_arg (ap, vlc_callback_t);
242
243             assert (name != NULL);
244             item->psz_name = strdup (name);
245             item->pf_callback = cb;
246             break;
247         }
248
249         case VLC_CONFIG_VALUE:
250         {
251             if (IsConfigIntegerType (item->i_type))
252             {
253                 item->orig.i = item->saved.i =
254                 item->value.i = va_arg (ap, int);
255             }
256             else
257             if (IsConfigFloatType (item->i_type))
258             {
259                 item->orig.f = item->saved.f =
260                 item->value.f = va_arg (ap, double);
261             }
262             else
263             if (IsConfigStringType (item->i_type))
264             {
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;
269             }
270             break;
271         }
272
273         case VLC_CONFIG_RANGE:
274         {
275             if (IsConfigIntegerType (item->i_type)
276              || item->i_type == CONFIG_ITEM_MODULE_LIST_CAT
277              || item->i_type == CONFIG_ITEM_MODULE_CAT)
278             {
279                 item->min.i = va_arg (ap, int);
280                 item->max.i = va_arg (ap, int);
281             }
282             else
283             if (IsConfigFloatType (item->i_type))
284             {
285                 item->min.f = va_arg (ap, double);
286                 item->max.f = va_arg (ap, double);
287             }
288             break;
289         }
290
291         case VLC_CONFIG_ADVANCED:
292             item->b_advanced = true;
293             break;
294
295         case VLC_CONFIG_VOLATILE:
296             item->b_unsaveable = true;
297             break;
298
299         case VLC_CONFIG_PERSISTENT:
300             item->b_autosave = true;
301             break;
302
303         case VLC_CONFIG_RESTART:
304             item->b_restart = true;
305             break;
306
307         case VLC_CONFIG_PRIVATE:
308             item->b_internal = true;
309             break;
310
311         case VLC_CONFIG_REMOVED:
312             item->b_removed = true;
313             break;
314
315         case VLC_CONFIG_CAPABILITY:
316         {
317             const char *cap = va_arg (ap, const char *);
318             item->psz_type = cap ? strdup (cap) : NULL;
319             break;
320         }
321
322         case VLC_CONFIG_SHORTCUT:
323             item->i_short = va_arg (ap, int);
324             break;
325
326         case VLC_CONFIG_OLDNAME:
327         {
328             const char *oldname = va_arg (ap, const char *);
329             item->psz_oldname = oldname ? strdup (oldname) : NULL;
330             break;
331         }
332
333         case VLC_CONFIG_SAFE:
334             item->b_safe = true;
335             break;
336
337         case VLC_CONFIG_DESC:
338         {
339             const char *text = va_arg (ap, const char *);
340             const char *longtext = va_arg (ap, const char *);
341
342             item->psz_text = text ? strdup (text) : NULL;
343             item->psz_longtext = longtext ? strdup (longtext) : NULL;
344             break;
345         }
346
347         case VLC_CONFIG_LIST:
348         {
349             size_t len = va_arg (ap, size_t);
350
351             /* Copy values */
352             if (IsConfigIntegerType (item->i_type))
353             {
354                 const int *src = va_arg (ap, const int *);
355                 int *dst = malloc (sizeof (int) * (len + 1));
356
357                 if (dst != NULL)
358                 {
359                     memcpy (dst, src, sizeof (int) * len);
360                     dst[len] = 0;
361                 }
362                 item->pi_list = dst;
363             }
364             else
365             if (IsConfigStringType (item->i_type))
366             {
367                 const char *const *src = va_arg (ap, const char *const *);
368                 char **dst = malloc (sizeof (char *) * (len + 1));
369
370                 if (dst != NULL)
371                 {
372                     for (size_t i = 0; i < len; i++)
373                         dst[i] = src[i] ? strdup (src[i]) : NULL;
374                     dst[len] = NULL;
375                 }
376                 item->ppsz_list = dst;
377             }
378             else
379                 break;
380
381             /* Copy textual descriptions */
382             const char *const *text = va_arg (ap, const char *const *);
383             if (text != NULL)
384             {
385                 char **dtext = malloc (sizeof (char *) * (len + 1));
386                 if( dtext != NULL )
387                 {
388                     for (size_t i = 0; i < len; i++)
389                         dtext[i] = text[i] ? strdup (text[i]) : NULL;
390                     dtext[len] = NULL;
391                 }
392                 item->ppsz_list_text = dtext;
393             }
394             else
395                 item->ppsz_list_text = NULL;
396
397             item->i_list = len;
398             item->pf_update_list = va_arg (ap, vlc_callback_t);
399             break;
400         }
401
402         case VLC_CONFIG_ADD_ACTION:
403         {
404             vlc_callback_t cb = va_arg (ap, vlc_callback_t), *tabcb;
405             const char *name = va_arg (ap, const char *);
406             char **tabtext;
407
408             tabcb = realloc (item->ppf_action,
409                              (item->i_action + 2) * sizeof (cb));
410             if (tabcb == NULL)
411                 break;
412             item->ppf_action = tabcb;
413             tabcb[item->i_action] = cb;
414             tabcb[item->i_action + 1] = NULL;
415
416             tabtext = realloc (item->ppsz_action_text,
417                                (item->i_action + 2) * sizeof (name));
418             if (tabtext == NULL)
419                 break;
420             item->ppsz_action_text = tabtext;
421
422             if (name)
423                 tabtext[item->i_action] = strdup (name);
424             else
425                 tabtext[item->i_action] = NULL;
426             tabtext[item->i_action + 1] = NULL;
427
428             item->i_action++;
429             break;
430         }
431
432         default:
433             fprintf (stderr, "LibVLC: unknown module property %d\n", propid);
434             fprintf (stderr, "LibVLC: too old to use this module?\n");
435             ret = -1;
436             break;
437     }
438
439     va_end (ap);
440     return ret;
441 }