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