]> git.sesse.net Git - vlc/blob - src/misc/modules.c
41a6f29bc22540485f06a3812aed5c2d9743c388
[vlc] / src / misc / modules.c
1 /*****************************************************************************
2  * modules.c : Builtin and plugin modules management functions
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: modules.c,v 1.76 2002/08/04 12:18:41 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Ethan C. Baldridge <BaldridgeE@cadmus.com>
9  *          Hans-Peter Jansen <hpj@urpla.net>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
27  * is set to 64. Don't try to be cleverer. */
28 #ifdef _FILE_OFFSET_BITS
29 #undef _FILE_OFFSET_BITS
30 #endif
31
32 #include <stdlib.h>                                      /* free(), strtol() */
33 #include <stdio.h>                                              /* sprintf() */
34 #include <string.h>                                              /* strdup() */
35
36 #include <vlc/vlc.h>
37
38 #include <dirent.h>
39
40 #if defined(HAVE_DLFCN_H)                                /* Linux, BSD, Hurd */
41 #   include <dlfcn.h>                        /* dlopen(), dlsym(), dlclose() */
42 #   define HAVE_DYNAMIC_PLUGINS
43 #elif defined(HAVE_IMAGE_H)                                          /* BeOS */
44 #   include <image.h>
45 #   define HAVE_DYNAMIC_PLUGINS
46 #elif defined(WIN32)
47 #   define HAVE_DYNAMIC_PLUGINS
48 #else
49 #   undef HAVE_DYNAMIC_PLUGINS
50 #endif
51
52
53 #include "netutils.h"
54
55 #include "interface.h"
56 #include "vlc_playlist.h"
57 #include "intf_eject.h"
58
59 #include "stream_control.h"
60 #include "input_ext-intf.h"
61 #include "input_ext-dec.h"
62 #include "input_ext-plugins.h"
63
64 #include "video.h"
65 #include "video_output.h"
66
67 #include "audio_output.h"
68
69 #include "iso_lang.h"
70
71 #ifdef HAVE_DYNAMIC_PLUGINS
72 #   include "modules_plugin.h"
73 #endif
74
75 #if !defined( _MSC_VER )
76 #    include "modules_builtin.h"
77 #else
78 #    include "modules_builtin_msvc.h"
79 #endif
80
81 /*****************************************************************************
82  * Local prototypes
83  *****************************************************************************/
84 #ifdef HAVE_DYNAMIC_PLUGINS
85 static void AllocateAllPlugins   ( vlc_object_t * );
86 static void AllocatePluginDir    ( vlc_object_t *, const char * );
87 static int  AllocatePluginFile   ( vlc_object_t *, char * );
88 #endif
89 static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
90 static int  DeleteModule ( module_t * );
91 static int  LockModule   ( module_t * );
92 static int  UnlockModule ( module_t * );
93 #ifdef HAVE_DYNAMIC_PLUGINS
94 static int  HideModule   ( module_t * );
95 static void DupModule    ( module_t * );
96 static void UndupModule  ( module_t * );
97 static int  CallEntry    ( module_t * );
98 #endif
99
100 /*****************************************************************************
101  * module_InitBank: create the module bank.
102  *****************************************************************************
103  * This function creates a module bank structure which will be filled later
104  * on with all the modules found.
105  *****************************************************************************/
106 void __module_InitBank( vlc_object_t *p_this )
107 {
108     module_bank_t *p_bank;
109
110     p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
111     p_bank->psz_object_name = "module bank";
112
113     p_bank->first = NULL;
114     p_bank->i_count = 0;
115     vlc_mutex_init( p_this, &p_bank->lock );
116
117     /*
118      * Store the symbols to be exported
119      */
120 #ifdef HAVE_DYNAMIC_PLUGINS
121     STORE_SYMBOLS( &p_bank->symbols );
122 #endif
123
124     /* Everything worked, attach the object */
125     p_this->p_vlc->p_module_bank = p_bank;
126     vlc_object_attach( p_bank, p_this->p_vlc );
127
128     return;
129 }
130
131 /*****************************************************************************
132  * module_ResetBank: reset the module bank.
133  *****************************************************************************
134  * This function resets the module bank by unloading all unused plugin
135  * modules.
136  *****************************************************************************/
137 void __module_ResetBank( vlc_object_t *p_this )
138 {
139     msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
140     return;
141 }
142
143 /*****************************************************************************
144  * module_EndBank: empty the module bank.
145  *****************************************************************************
146  * This function unloads all unused plugin modules and empties the module
147  * bank in case of success.
148  *****************************************************************************/
149 void __module_EndBank( vlc_object_t *p_this )
150 {
151     module_t * p_next;
152
153     vlc_object_detach_all( p_this->p_vlc->p_module_bank );
154
155     while( p_this->p_vlc->p_module_bank->first != NULL )
156     {
157         if( DeleteModule( p_this->p_vlc->p_module_bank->first ) )
158         {
159             /* Module deletion failed */
160             msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
161                      p_this->p_vlc->p_module_bank->first->psz_object_name );
162
163             /* We just free the module by hand. Niahahahahaha. */
164             p_next = p_this->p_vlc->p_module_bank->first->next;
165             free( p_this->p_vlc->p_module_bank->first );
166             p_this->p_vlc->p_module_bank->first = p_next;
167         }
168     }
169
170     /* Destroy the lock */
171     vlc_mutex_destroy( &p_this->p_vlc->p_module_bank->lock );
172
173     vlc_object_destroy( p_this->p_vlc->p_module_bank );
174
175     return;
176 }
177
178 /*****************************************************************************
179  * module_LoadMain: load the main program info into the module bank.
180  *****************************************************************************
181  * This function fills the module bank structure with the main module infos.
182  * This is very useful as it will allow us to consider the main program just
183  * as another module, and for instance the configuration options of main will
184  * be available in the module bank structure just as for every other module.
185  *****************************************************************************/
186 void __module_LoadMain( vlc_object_t *p_this )
187 {
188     AllocateBuiltinModule( p_this, vlc_entry__main );
189 }
190
191 /*****************************************************************************
192  * module_LoadBuiltins: load all modules which we built with.
193  *****************************************************************************
194  * This function fills the module bank structure with the builtin modules.
195  *****************************************************************************/
196 void __module_LoadBuiltins( vlc_object_t * p_this )
197 {
198     msg_Dbg( p_this, "checking builtin modules" );
199     ALLOCATE_ALL_BUILTINS();
200 }
201
202 /*****************************************************************************
203  * module_LoadPlugins: load all plugin modules we can find.
204  *****************************************************************************
205  * This function fills the module bank structure with the plugin modules.
206  *****************************************************************************/
207 void __module_LoadPlugins( vlc_object_t * p_this )
208 {
209 #ifdef HAVE_DYNAMIC_PLUGINS
210     msg_Dbg( p_this, "checking plugin modules" );
211     AllocateAllPlugins( p_this );
212 #endif
213 }
214
215 /*****************************************************************************
216  * module_ManageBank: manage the module bank.
217  *****************************************************************************
218  * This function parses the module bank and hides modules that have been
219  * unused for a while.
220  *****************************************************************************/
221 void __module_ManageBank( vlc_object_t *p_this )
222 {
223 #ifdef HAVE_DYNAMIC_PLUGINS
224     module_t * p_module;
225
226     /* We take the global lock */
227     vlc_mutex_lock( &p_this->p_vlc->p_module_bank->lock );
228
229     /* Parse the module list to see if any modules need to be unloaded */
230     for( p_module = p_this->p_vlc->p_module_bank->first ;
231          p_module != NULL ;
232          p_module = p_module->next )
233     {
234         /* If the module is unused and if it is a plugin module... */
235         if( p_module->i_usage == 0 && !p_module->b_builtin )
236         {
237             if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
238             {
239                 p_module->i_unused_delay++;
240             }
241             else
242             {
243                 msg_Dbg( p_this, "hiding unused plugin module \"%s\"",
244                                  p_module->psz_object_name );
245                 HideModule( p_module );
246
247                 /* Break here, so that we only hide one module at a time */
248                 break;
249             }
250         }
251     }
252
253     /* We release the global lock */
254     vlc_mutex_unlock( &p_this->p_vlc->p_module_bank->lock );
255 #endif /* HAVE_DYNAMIC_PLUGINS */
256
257     return;
258 }
259
260 /*****************************************************************************
261  * module_Need: return the best module function, given a capability list.
262  *****************************************************************************
263  * This function returns the module that best fits the asked capabilities.
264  *****************************************************************************/
265 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
266                           const char *psz_name )
267 {
268     typedef struct module_list_t module_list_t;
269
270     struct module_list_t
271     {
272         module_t *p_module;
273         int i_score;
274         module_list_t *p_next;
275     };
276
277     module_list_t *p_list, *p_first, *p_tmp;
278
279     int i_index = 0;
280     vlc_bool_t b_intf = VLC_FALSE;
281
282     module_t *p_module;
283
284     int   i_shortcuts = 0;
285     char *psz_shortcuts = NULL;
286
287     msg_Dbg( p_this, "looking for %s module", psz_capability );
288
289     /* Count how many different shortcuts were asked for */
290     if( psz_name && *psz_name )
291     {
292         char *psz_parser;
293
294         /* If the user wants none, give him none. */
295         if( !strcmp( psz_name, "none" ) )
296         {
297             return NULL;
298         }
299
300         i_shortcuts++;
301         psz_shortcuts = strdup( psz_name );
302
303         for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
304         {
305             if( *psz_parser == ',' )
306             {
307                  *psz_parser = '\0';
308                  i_shortcuts++;
309             }
310         }
311     }
312
313     /* We take the global lock */
314     vlc_mutex_lock( &p_this->p_vlc->p_module_bank->lock );
315
316     /* Sort the modules and test them */
317     p_list = malloc( p_this->p_vlc->p_module_bank->i_count
318                       * sizeof( module_list_t ) );
319     p_first = NULL;
320
321     /* Parse the module list for capabilities and probe each of them */
322     for( p_module = p_this->p_vlc->p_module_bank->first ;
323          p_module != NULL ;
324          p_module = p_module->next )
325     {
326         module_t * p_submodule = NULL;
327         int i_shortcut_bonus = 0, i_submodule;
328
329         /* Test that this module can do what we need */
330         if( strcmp( p_module->psz_capability, psz_capability ) )
331         {
332             for( i_submodule = 0;
333                  i_submodule < p_module->i_children;
334                  i_submodule++ )
335             {
336                 if( !strcmp( ((module_t*)p_module->pp_children[ i_submodule ])
337                                            ->psz_capability, psz_capability ) )
338                 {
339                     p_submodule =
340                             (module_t*)p_module->pp_children[ i_submodule ];
341                     p_submodule->next = p_module->next;
342                     break;
343                 }
344             }
345
346             if( p_submodule == NULL )
347             {
348                 continue;
349             }
350
351             p_module = p_submodule;
352         }
353
354         /* Test if we have the required CPU */
355         if( (p_module->i_cpu & p_this->p_vlc->i_cpu) != p_module->i_cpu )
356         {
357             continue;
358         }
359
360         /* If we required a shortcut, check this plugin provides it. */
361         if( i_shortcuts )
362         {
363             vlc_bool_t b_trash = VLC_TRUE;
364             int i_dummy, i_short = i_shortcuts;
365             char *psz_name = psz_shortcuts;
366
367             while( i_short )
368             {
369                 for( i_dummy = 0;
370                      b_trash && p_module->pp_shortcuts[i_dummy];
371                      i_dummy++ )
372                 {
373                     b_trash = ( strcmp(psz_name, "any") || !p_module->i_score )
374                         && strcmp( psz_name, p_module->pp_shortcuts[i_dummy] );
375                 }
376
377                 if( !b_trash )
378                 {
379                     i_shortcut_bonus = i_short * 10000;
380                     break;
381                 }
382
383                 /* Go to the next shortcut... This is so lame! */
384                 while( *psz_name )
385                 {
386                     psz_name++;
387                 }
388                 psz_name++;
389                 i_short--;
390             }
391
392             if( b_trash )
393             {
394                 continue;
395             }
396         }
397         /* If we didn't require a shortcut, trash zero-scored plugins */
398         else if( !p_module->i_score )
399         {
400             continue;
401         }
402
403         /* Special case: test if we requested a particular intf plugin */
404         if( p_module->psz_program
405              && !strcmp( p_module->psz_program,
406                          p_this->p_vlc->psz_object_name ) )
407         {
408             if( !b_intf ) 
409             {
410                 /* Remove previous non-matching plugins */
411                 i_index = 0;
412                 b_intf = VLC_TRUE;
413             }
414         }
415         else if( b_intf )
416         {
417             /* This one doesn't match */
418             continue;
419         }
420
421         /* Store this new module */
422         p_list[ i_index ].p_module = p_module;
423         p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
424
425         /* Add it to the modules-to-probe list */
426         if( i_index == 0 )
427         {
428             p_list[ 0 ].p_next = NULL;
429             p_first = p_list;
430         }
431         else
432         {
433             /* Ok, so at school you learned that quicksort is quick, and
434              * bubble sort sucks raw eggs. But that's when dealing with
435              * thousands of items. Here we have barely 50. */
436             module_list_t *p_newlist = p_first;
437
438             if( p_first->i_score < p_list[ i_index ].i_score )
439             {
440                 p_list[ i_index ].p_next = p_first;
441                 p_first = &p_list[ i_index ];
442             }
443             else
444             {
445                 while( p_newlist->p_next != NULL &&
446                     p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
447                 {
448                     p_newlist = p_newlist->p_next;
449                 }
450
451                 p_list[ i_index ].p_next = p_newlist->p_next;
452                 p_newlist->p_next = &p_list[ i_index ];
453             }
454         }
455
456         i_index++;
457     }
458
459     msg_Dbg( p_this, "probing %i candidate%s",
460                      i_index, i_index == 1 ? "" : "s" );
461
462     /* Lock all selected modules */
463     p_tmp = p_first;
464     while( p_tmp != NULL )
465     {
466         LockModule( p_tmp->p_module );
467         p_tmp = p_tmp->p_next;
468     }
469
470     /* We can release the global lock, module refcounts were incremented */
471     vlc_mutex_unlock( &p_this->p_vlc->p_module_bank->lock );
472
473     /* Parse the linked list and use the first successful module */
474     p_tmp = p_first;
475     while( p_tmp != NULL )
476     {
477         if( p_tmp->p_module->pf_activate
478              && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
479         {
480             break;
481         }
482
483         UnlockModule( p_tmp->p_module );
484         p_tmp = p_tmp->p_next;
485     }
486
487     /* Store the locked module value */
488     if( p_tmp != NULL )
489     {
490         p_module = p_tmp->p_module;
491         p_tmp = p_tmp->p_next;
492     }
493     else
494     {
495         p_module = NULL;
496     }
497
498     /* Unlock the remaining modules */
499     while( p_tmp != NULL )
500     {
501         UnlockModule( p_tmp->p_module );
502         p_tmp = p_tmp->p_next;
503     }
504
505     free( p_list );
506
507     if( p_module != NULL )
508     {
509         msg_Info( p_module, "using %s module \"%s\"",
510                   psz_capability, p_module->psz_object_name );
511     }
512     else if( p_first == NULL )
513     {
514         msg_Err( p_this, "no %s module matched \"%s\"",
515                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
516     }
517     else if( psz_name != NULL && *psz_name )
518     {
519         msg_Err( p_this, "no %s module matching \"%s\" could be loaded",
520                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
521     }
522
523     if( psz_shortcuts )
524     {
525         free( psz_shortcuts );
526     }
527
528     /* Don't forget that the module is still locked */
529     return p_module;
530 }
531
532 /*****************************************************************************
533  * module_Unneed: decrease the usage count of a module.
534  *****************************************************************************
535  * This function must be called by the thread that called module_Need, to
536  * decrease the reference count and allow for hiding of modules.
537  *****************************************************************************/
538 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
539 {
540     /* Use the close method */
541     if( p_module->pf_deactivate )
542     {
543         p_module->pf_deactivate( p_this );
544     }
545
546     /* We take the global lock */
547     vlc_mutex_lock( &p_module->p_vlc->p_module_bank->lock );
548
549     /* Just unlock the module - we can't do anything if it fails,
550      * so there is no need to check the return value. */
551     UnlockModule( p_module );
552
553     msg_Info( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
554
555     /* We release the global lock */
556     vlc_mutex_unlock( &p_module->p_vlc->p_module_bank->lock );
557
558     return;
559 }
560
561 /*****************************************************************************
562  * Following functions are local.
563  *****************************************************************************/
564
565 /*****************************************************************************
566  * AllocateAllPlugins: load all plugin modules we can find.
567  *****************************************************************************/
568 #ifdef HAVE_DYNAMIC_PLUGINS
569 static void AllocateAllPlugins( vlc_object_t *p_this )
570 {
571     /* Yes, there are two NULLs because we replace one with "plugin-path". */
572     char *          path[] = { "plugins", PLUGIN_PATH, NULL, NULL };
573
574     char **         ppsz_path = path;
575     char *          psz_fullpath;
576 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
577     char *          psz_vlcpath = system_GetProgramPath();
578     int             i_vlclen = strlen( psz_vlcpath );
579     vlc_bool_t      b_notinroot;
580 #endif
581
582     /* If the user provided a plugin path, we add it to the list */
583     path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
584                                                             "plugin-path" );
585
586     for( ; *ppsz_path != NULL ; ppsz_path++ )
587     {
588 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
589         /* Store strlen(*ppsz_path) for later use. */
590         int i_dirlen = strlen( *ppsz_path );
591
592         b_notinroot = VLC_FALSE;
593         /* Under BeOS, we need to add beos_GetProgramPath() to access
594          * files under the current directory */
595         if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
596         {
597             i_dirlen += i_vlclen + 2;
598             b_notinroot = VLC_TRUE;
599
600             psz_fullpath = malloc( i_dirlen );
601             if( psz_fullpath == NULL )
602             {
603                 continue;
604             }
605             sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
606         }
607         else
608 #endif
609         {
610             psz_fullpath = *ppsz_path;
611         }
612
613         msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
614
615         AllocatePluginDir( p_this, psz_fullpath );
616
617 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
618         if( b_notinroot )
619         {
620             free( psz_fullpath );
621         }
622 #endif
623     }
624 }
625
626 /*****************************************************************************
627  * AllocatePluginDir: recursively parse a directory to look for plugins
628  *****************************************************************************/
629 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir )
630 {
631 #define PLUGIN_EXT ".so"
632     DIR *           dir;
633     int             i_dirlen = strlen( psz_dir );
634     char *          psz_file;
635     struct dirent * file;
636
637     if( (dir = opendir( psz_dir )) )
638     {
639         /* Parse the directory and try to load all files it contains. */
640         while( (file = readdir( dir )) )
641         {
642             int i_len = strlen( file->d_name );
643
644             /* Skip ".", ".." and anything starting with "." */
645             if( !*file->d_name || *file->d_name == '.' )
646             {
647                 continue;
648             }
649
650             if( file->d_type == DT_DIR )
651             {
652                 psz_file = malloc( i_dirlen + 1 /* / */ + i_len + 1 /* \0 */ );
653                 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
654                 AllocatePluginDir( p_this, psz_file );
655                 free( psz_file );
656             }
657             else if( i_len > strlen( PLUGIN_EXT )
658                       /* We only load files ending with ".so" */
659                       && !strncmp( file->d_name + i_len - strlen( PLUGIN_EXT ),
660                                    PLUGIN_EXT, strlen( PLUGIN_EXT ) ) )
661             {
662                 psz_file = malloc( i_dirlen + 1 /* / */ + i_len + 1 /* \0 */ );
663                 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
664                 AllocatePluginFile( p_this, psz_file );
665                 free( psz_file );
666             }
667         }
668
669         /* Close the directory if successfully opened */
670         closedir( dir );
671     }
672 }
673
674 /*****************************************************************************
675  * AllocatePluginFile: load a module into memory and initialize it.
676  *****************************************************************************
677  * This function loads a dynamically loadable module and allocates a structure
678  * for its information data. The module can then be handled by module_Need,
679  * module_Unneed and HideModule. It can be removed by DeleteModule.
680  *****************************************************************************/
681 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file )
682 {
683     module_t * p_module;
684     module_handle_t handle;
685
686     /* Try to dynamically load the module. */
687     if( module_load( psz_file, &handle ) )
688     {
689         char psz_buffer[256];
690
691         /* The plugin module couldn't be opened */
692         msg_Warn( p_this, "cannot open `%s' (%s)",
693                   psz_file, module_error( psz_buffer ) );
694         return -1;
695     }
696
697     /* Now that we have successfully loaded the module, we can
698      * allocate a structure for it */ 
699     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
700     if( p_module == NULL )
701     {
702         msg_Err( p_this, "out of memory" );
703         module_unload( handle );
704         return -1;
705     }
706
707     /* We need to fill these since they may be needed by CallEntry() */
708     p_module->psz_filename = psz_file;
709     p_module->handle = handle;
710     p_module->p_symbols = &p_this->p_vlc->p_module_bank->symbols;
711
712     /* Initialize the module: fill p_module->psz_object_name, default config */
713     if( CallEntry( p_module ) != 0 )
714     {
715         /* We couldn't call module_init() */
716         vlc_object_destroy( p_module );
717         module_unload( handle );
718         return -1;
719     }
720
721     DupModule( p_module );
722     p_module->psz_filename = strdup( p_module->psz_filename );
723     p_module->psz_longname = strdup( p_module->psz_longname );
724
725     /* Everything worked fine ! The module is ready to be added to the list. */
726     p_module->i_usage = 0;
727     p_module->i_unused_delay = 0;
728
729     p_module->b_builtin = VLC_FALSE;
730
731     /* Link module into the linked list */
732     if( p_this->p_vlc->p_module_bank->first != NULL )
733     {
734         p_this->p_vlc->p_module_bank->first->prev = p_module;
735     }
736     p_module->next = p_this->p_vlc->p_module_bank->first;
737     p_module->prev = NULL;
738     p_this->p_vlc->p_module_bank->first = p_module;
739     p_this->p_vlc->p_module_bank->i_count++;
740
741     msg_Dbg( p_this, "plugin \"%s\", %s",
742              p_module->psz_object_name, p_module->psz_longname );
743
744     vlc_object_attach( p_module, p_this->p_vlc->p_module_bank );
745
746     return 0;
747 }
748
749 /*****************************************************************************
750  * DupModule: make a plugin module standalone.
751  *****************************************************************************
752  * This function duplicates all strings in the module, so that the dynamic
753  * object can be unloaded. It acts recursively on submodules.
754  *****************************************************************************/
755 static void DupModule( module_t *p_module )
756 {
757     char **pp_shortcut;
758     int i_submodule;
759
760     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
761     {
762         *pp_shortcut = strdup( *pp_shortcut );
763     }
764
765     /* We strdup() these entries so that they are still valid when the
766      * module is unloaded. */
767     p_module->psz_object_name = strdup( p_module->psz_object_name );
768     p_module->psz_capability = strdup( p_module->psz_capability );
769
770     if( p_module->psz_program != NULL )
771     {
772         p_module->psz_program = strdup( p_module->psz_program );
773     }
774
775     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
776     {
777         DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
778     }
779 }
780
781 /*****************************************************************************
782  * UndupModule: free a duplicated module.
783  *****************************************************************************
784  * This function frees the allocations done in DupModule().
785  *****************************************************************************/
786 static void UndupModule( module_t *p_module )
787 {
788     char **pp_shortcut;
789     int i_submodule;
790
791     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
792     {
793         UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
794     }
795
796     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
797     {
798         free( *pp_shortcut );
799     }
800
801     free( p_module->psz_object_name );
802     free( p_module->psz_capability );
803
804     if( p_module->psz_program != NULL )
805     {
806         free( p_module->psz_program );
807     }
808 }
809
810 #endif /* HAVE_DYNAMIC_PLUGINS */
811
812 /*****************************************************************************
813  * AllocateBuiltinModule: initialize a builtin module.
814  *****************************************************************************
815  * This function registers a builtin module and allocates a structure
816  * for its information data. The module can then be handled by module_Need,
817  * module_Unneed and HideModule. It can be removed by DeleteModule.
818  *****************************************************************************/
819 static int AllocateBuiltinModule( vlc_object_t * p_this,
820                                   int ( *pf_entry ) ( module_t * ) )
821 {
822     module_t * p_module;
823
824     /* Now that we have successfully loaded the module, we can
825      * allocate a structure for it */ 
826     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
827     if( p_module == NULL )
828     {
829         msg_Err( p_this, "out of memory" );
830         return -1;
831     }
832
833     /* Initialize the module : fill p_module->psz_object_name, etc. */
834     if( pf_entry( p_module ) != 0 )
835     {
836         /* With a well-written module we shouldn't have to print an
837          * additional error message here, but just make sure. */
838         msg_Err( p_this, "failed calling entry point in builtin module" );
839         vlc_object_destroy( p_module );
840         return -1;
841     }
842
843     /* Everything worked fine ! The module is ready to be added to the list. */
844     p_module->i_usage = 0;
845     p_module->i_unused_delay = 0;
846
847     p_module->b_builtin = VLC_TRUE;
848
849     /* Link module into the linked list */
850     if( p_this->p_vlc->p_module_bank->first != NULL )
851     {
852         p_this->p_vlc->p_module_bank->first->prev = p_module;
853     }
854     p_module->next = p_this->p_vlc->p_module_bank->first;
855     p_module->prev = NULL;
856     p_this->p_vlc->p_module_bank->first = p_module;
857     p_this->p_vlc->p_module_bank->i_count++;
858
859     msg_Dbg( p_this, "builtin \"%s\", %s",
860              p_module->psz_object_name, p_module->psz_longname );
861
862     vlc_object_attach( p_module, p_this->p_vlc->p_module_bank );
863
864     return 0;
865 }
866
867 /*****************************************************************************
868  * DeleteModule: delete a module and its structure.
869  *****************************************************************************
870  * This function can only be called if i_usage <= 0.
871  *****************************************************************************/
872 static int DeleteModule( module_t * p_module )
873 {
874     /* If the module is not in use but is still in memory, we first have
875      * to hide it and remove it from memory before we can free the
876      * data structure. */
877     if( p_module->b_builtin )
878     {
879         if( p_module->i_usage != 0 )
880         {
881             msg_Err( p_module, "trying to free builtin module \"%s\" with "
882                      "usage %i", p_module->psz_object_name, p_module->i_usage );
883             return -1;
884         }
885     }
886 #ifdef HAVE_DYNAMIC_PLUGINS
887     else
888     {
889         if( p_module->i_usage >= 1 )
890         {
891             msg_Err( p_module, "trying to free module \"%s\" which is "
892                                "still in use", p_module->psz_object_name );
893             return -1;
894         }
895
896         /* Two possibilities here: i_usage == -1 and the module is already
897          * unloaded, we can continue, or i_usage == 0, and we have to hide
898          * the module before going on. */
899         if( p_module->i_usage == 0 )
900         {
901             if( HideModule( p_module ) != 0 )
902             {
903                 return -1;
904             }
905         }
906     }
907 #endif
908
909     vlc_object_detach_all( p_module );
910
911     /* Unlink the module from the linked list. */
912     if( p_module->prev != NULL )
913     {
914         p_module->prev->next = p_module->next;
915     }
916     else
917     {
918         p_module->p_vlc->p_module_bank->first = p_module->next;
919     }
920
921     if( p_module->next != NULL )
922     {
923         p_module->next->prev = p_module->prev;
924     }
925
926     p_module->p_vlc->p_module_bank->i_count--;
927
928     /* We free the structures that we strdup()ed in Allocate*Module(). */
929 #ifdef HAVE_DYNAMIC_PLUGINS
930     if( !p_module->b_builtin )
931     {
932         UndupModule( p_module );
933         free( p_module->psz_filename );
934         free( p_module->psz_longname );
935     }
936 #endif
937
938     /* Free and detach the object's children */
939     while( p_module->i_children )
940     {
941         vlc_object_t *p_this = p_module->pp_children[0];
942         vlc_object_detach_all( p_this );
943         vlc_object_destroy( p_this );
944     }
945
946     config_Free( p_module );
947     vlc_object_destroy( p_module );
948
949     return 0;
950 }
951
952 /*****************************************************************************
953  * LockModule: increase the usage count of a module and load it if needed.
954  *****************************************************************************
955  * This function has to be called before a thread starts using a module. If
956  * the module is already loaded, we just increase its usage count. If it isn't
957  * loaded, we have to dynamically open it and initialize it.
958  * If you successfully call LockModule() at any moment, be careful to call
959  * UnlockModule() when you don't need it anymore.
960  *****************************************************************************/
961 static int LockModule( module_t * p_module )
962 {
963     if( p_module->i_usage >= 0 )
964     {
965         /* This module is already loaded and activated, we can return */
966         p_module->i_usage++;
967         return 0;
968     }
969
970     if( p_module->b_builtin )
971     {
972         /* A builtin module should always have a refcount >= 0 ! */
973         msg_Err( p_module, "builtin module \"%s\" has refcount %i",
974                            p_module->psz_object_name, p_module->i_usage );
975         return -1;
976     }
977
978 #ifdef HAVE_DYNAMIC_PLUGINS
979     if( p_module->i_usage != -1 )
980     {
981         /* This shouldn't happen. Ever. We have serious problems here. */
982         msg_Err( p_module, "plugin module \"%s\" has refcount %i",
983                            p_module->psz_object_name, p_module->i_usage );
984         return -1;
985     }
986
987     /* i_usage == -1, which means that the module isn't in memory */
988     if( module_load( p_module->psz_filename, &p_module->handle ) )
989     {
990         char psz_buffer[256];
991
992         /* The plugin module couldn't be opened */
993         msg_Err( p_module, "cannot open `%s' (%s)",
994                  p_module->psz_filename, module_error(psz_buffer) );
995         return -1;
996     }
997
998     /* FIXME: what to do if the guy modified the plugin while it was
999      * unloaded ? It makes XMMS crash nastily, perhaps we should try
1000      * to be a bit more clever here. */
1001
1002     /* Everything worked fine ! The module is ready to be used */
1003     p_module->i_usage = 1;
1004 #endif /* HAVE_DYNAMIC_PLUGINS */
1005
1006     return 0;
1007 }
1008
1009 /*****************************************************************************
1010  * UnlockModule: decrease the usage count of a module.
1011  *****************************************************************************
1012  * We decrease the usage count of a module so that we know when a module
1013  * becomes unused and can be hidden.
1014  *****************************************************************************/
1015 static int UnlockModule( module_t * p_module )
1016 {
1017     if( p_module->i_usage <= 0 )
1018     {
1019         /* This shouldn't happen. Ever. We have serious problems here. */
1020         msg_Err( p_module, "trying to call module_Unneed() on \"%s\" "
1021                            "which is not in use", p_module->psz_object_name );
1022         return -1;
1023     }
1024
1025     /* This module is still in use, we can return */
1026     p_module->i_usage--;
1027     p_module->i_unused_delay = 0;
1028
1029     return 0;
1030 }
1031
1032 #ifdef HAVE_DYNAMIC_PLUGINS
1033 /*****************************************************************************
1034  * HideModule: remove a module from memory but keep its structure.
1035  *****************************************************************************
1036  * This function can only be called if i_usage == 0. It will make a call
1037  * to the module's inner module_deactivate() symbol, and then unload it
1038  * from memory. A call to module_Need() will automagically load it again.
1039  *****************************************************************************/
1040 static int HideModule( module_t * p_module )
1041 {
1042     if( p_module->b_builtin )
1043     {
1044         /* A builtin module should never be hidden. */
1045         msg_Err( p_module, "trying to hide builtin module \"%s\"",
1046                            p_module->psz_object_name );
1047         return -1;
1048     }
1049
1050     if( p_module->i_usage >= 1 )
1051     {
1052         msg_Err( p_module, "trying to hide module \"%s\" which is still "
1053                            "in use", p_module->psz_object_name );
1054         return -1;
1055     }
1056
1057     if( p_module->i_usage <= -1 )
1058     {
1059         msg_Err( p_module, "trying to hide module \"%s\" which is already "
1060                            "hidden", p_module->psz_object_name );
1061         return -1;
1062     }
1063
1064     /* Everything worked fine, we can safely unload the module. */
1065     module_unload( p_module->handle );
1066     p_module->i_usage = -1;
1067
1068     return 0;
1069 }
1070
1071 /*****************************************************************************
1072  * CallEntry: call an entry point.
1073  *****************************************************************************
1074  * This function calls a symbol given its name and a module structure. The
1075  * symbol MUST refer to a function returning int and taking a module_t* as
1076  * an argument.
1077  *****************************************************************************/
1078 static int CallEntry( module_t * p_module )
1079 {
1080     static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1081     int (* pf_symbol) ( module_t * p_module );
1082
1083     /* Try to resolve the symbol */
1084     pf_symbol = module_getsymbol( p_module->handle, psz_name );
1085
1086     if( pf_symbol == NULL )
1087     {
1088         char psz_buffer[256];
1089
1090         /* We couldn't load the symbol */
1091         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1092                             psz_name, p_module->psz_filename,
1093                             module_error( psz_buffer ) );
1094         return -1;
1095     }
1096
1097     /* We can now try to call the symbol */
1098     if( pf_symbol( p_module ) != 0 )
1099     {
1100         /* With a well-written module we shouldn't have to print an
1101          * additional error message here, but just make sure. */
1102         msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1103                            psz_name, p_module->psz_filename );
1104         return -1;
1105     }
1106
1107     /* Everything worked fine, we can return */
1108     return 0;
1109 }
1110 #endif /* HAVE_DYNAMIC_PLUGINS */
1111