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