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