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