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