]> git.sesse.net Git - vlc/blob - src/modules/entry.c
Partially cleanup old module_list_cat hack
[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              || item->i_type == CONFIG_ITEM_MODULE_LIST_CAT
304              || item->i_type == CONFIG_ITEM_MODULE_CAT)
305             {
306                 item->min.i = va_arg (ap, int);
307                 item->max.i = va_arg (ap, int);
308                 ret = 0;
309             }
310             else
311             if (IsConfigFloatType (item->i_type))
312             {
313                 item->min.f = va_arg (ap, double);
314                 item->max.f = va_arg (ap, double);
315                 ret = 0;
316             }
317             break;
318         }
319
320         case VLC_CONFIG_ADVANCED:
321             item->b_advanced = true;
322             ret = 0;
323             break;
324
325         case VLC_CONFIG_VOLATILE:
326             item->b_unsaveable = true;
327             ret = 0;
328             break;
329
330         case VLC_CONFIG_PERSISTENT:
331             item->b_autosave = true;
332             ret = 0;
333             break;
334
335         case VLC_CONFIG_RESTART:
336             item->b_restart = true;
337             ret = 0;
338             break;
339
340         case VLC_CONFIG_PRIVATE:
341             item->b_internal = true;
342             ret = 0;
343             break;
344
345         case VLC_CONFIG_REMOVED:
346             item->b_removed = true;
347             ret = 0;
348             break;
349
350         case VLC_CONFIG_CAPABILITY:
351         {
352             const char *cap = va_arg (ap, const char *);
353             item->psz_type = cap ? strdup (cap) : NULL;
354             ret = 0;
355             break;
356         }
357
358         case VLC_CONFIG_SHORTCUT:
359             item->i_short = va_arg (ap, int);
360             ret = 0;
361             break;
362
363         case VLC_CONFIG_OLDNAME:
364         {
365             const char *oldname = va_arg (ap, const char *);
366             item->psz_oldname = oldname ? strdup (oldname) : NULL;
367             ret = 0;
368             break;
369         }
370
371         case VLC_CONFIG_SAFE:
372             item->b_safe = true;
373             ret = 0;
374             break;
375
376         case VLC_CONFIG_DESC:
377         {
378             const char *domain = va_arg (ap, const char *);
379             const char *text = va_arg (ap, const char *);
380             const char *longtext = va_arg (ap, const char *);
381
382             if (domain == NULL)
383                 domain = PACKAGE;
384             item->psz_text = text ? strdup (dgettext (domain, text)) : NULL;
385             item->psz_longtext =
386                 longtext ? strdup (dgettext (domain, longtext)) : NULL;
387             ret = 0;
388             break;
389         }
390
391         case VLC_CONFIG_LIST:
392         {
393             const char *domain = va_arg (ap, const char *);
394             size_t len = va_arg (ap, size_t);
395
396             /* Copy values */
397             if (IsConfigIntegerType (item->i_type))
398             {
399                 const int *src = va_arg (ap, const int *);
400                 int *dst = malloc (sizeof (int) * (len + 1));
401
402                 if (dst != NULL)
403                 {
404                     memcpy (dst, src, sizeof (int) * len);
405                     dst[len] = 0;
406                 }
407                 item->pi_list = dst;
408             }
409             else
410 #if 0
411             if (IsConfigFloatType (item->i_type))
412             {
413                 const float *src = va_arg (ap, const float *);
414                 float *dst = malloc (sizeof (float) * (len + 1));
415
416                 if (dst != NULL)
417                 {
418                     memcpy (dst, src, sizeof (float) * len);
419                     dst[len] = 0.;
420                 }
421                 item->pf_list = dst;
422             }
423             else
424 #endif
425             if (IsConfigStringType (item->i_type))
426             {
427                 const char *const *src = va_arg (ap, const char *const *);
428                 char **dst = malloc (sizeof (char *) * (len + 1));
429
430                 if (dst != NULL)
431                 {
432                     for (size_t i = 0; i < len; i++)
433                         dst[i] = src[i] ? strdup (src[i]) : NULL;
434                     dst[len] = NULL;
435                 }
436                 item->ppsz_list = dst;
437             }
438             else
439                 break;
440
441             /* Copy textual descriptions */
442             if (domain == NULL)
443                 domain = PACKAGE;
444
445             const char *const *text = va_arg (ap, const char *const *);
446             if (text != NULL)
447             {
448                 char **dtext = malloc (sizeof (char *) * (len + 1));
449                 if( dtext != NULL )
450                 {
451                     for (size_t i = 0; i < len; i++)
452                         dtext[i] = text[i] ?
453                                         strdup( dgettext( domain, text[i] ) ) :
454                                         NULL;
455                     dtext[len] = NULL;
456                 }
457                 item->ppsz_list_text = dtext;
458             }
459             else
460                 item->ppsz_list_text = NULL;
461
462             item->i_list = len;
463             item->pf_update_list = va_arg (ap, vlc_callback_t);
464             ret = 0;
465             break;
466         }
467
468         case VLC_CONFIG_ADD_ACTION:
469         {
470             const char *domain = va_arg (ap, const char *);
471             vlc_callback_t cb = va_arg (ap, vlc_callback_t), *tabcb;
472             const char *name = va_arg (ap, const char *);
473             char **tabtext;
474
475             tabcb = realloc (item->ppf_action,
476                              (item->i_action + 2) * sizeof (cb));
477             if (tabcb == NULL)
478                 break;
479             item->ppf_action = tabcb;
480             tabcb[item->i_action] = cb;
481             tabcb[item->i_action + 1] = NULL;
482
483             tabtext = realloc (item->ppsz_action_text,
484                                (item->i_action + 2) * sizeof (name));
485             if (tabtext == NULL)
486                 break;
487             item->ppsz_action_text = tabtext;
488
489             if (domain == NULL)
490                 domain = PACKAGE;
491             if (name)
492                 tabtext[item->i_action] = strdup (dgettext (domain, name));
493             else
494                 tabtext[item->i_action] = NULL;
495             tabtext[item->i_action + 1] = NULL;
496
497             item->i_action++;
498             ret = 0;
499             break;
500         }
501     }
502
503     va_end (ap);
504     return ret;
505 }