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