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