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