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