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