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