]> git.sesse.net Git - vlc/blob - src/libvlc.c
* ALL: changed "struct foo_s" into "struct foo_t" to make greppers happy.
[vlc] / src / libvlc.c
1 /*****************************************************************************
2  * libvlc.c: main libvlc source
3  * Includes the main() function for vlc. Parses command line, starts playlist
4  * and spawns threads.
5  *****************************************************************************
6  * Copyright (C) 1998-2001 VideoLAN
7  * $Id: libvlc.c,v 1.17 2002/07/20 18:01:43 sam Exp $
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *          Samuel Hocevar <sam@zoy.org>
11  *          Gildas Bazin <gbazin@netcourrier.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26  *****************************************************************************/
27
28 /*****************************************************************************
29  * Pretend we are a builtin module
30  *****************************************************************************/
31 #define MODULE_NAME main
32 #define __BUILTIN__
33
34 /*****************************************************************************
35  * Preamble
36  *****************************************************************************/
37 #include <errno.h>                                                 /* ENOMEM */
38 #include <stdio.h>                                              /* sprintf() */
39 #include <string.h>                                            /* strerror() */
40 #include <stdlib.h>                                                /* free() */
41 #include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
42
43 #include <vlc/vlc.h>
44
45 #ifdef HAVE_GETOPT_LONG
46 #   ifdef HAVE_GETOPT_H
47 #       include <getopt.h>                                       /* getopt() */
48 #   endif
49 #else
50 #   include "GNUgetopt/getopt.h"
51 #endif
52
53 #ifndef WIN32
54 #   include <netinet/in.h>                            /* BSD: struct in_addr */
55 #endif
56
57 #ifdef HAVE_UNISTD_H
58 #   include <unistd.h>
59 #elif defined( _MSC_VER ) && defined( _WIN32 )
60 #   include <io.h>
61 #endif
62
63 #ifdef HAVE_LOCALE_H
64 #   include <locale.h>
65 #endif
66
67 #include "vlc_cpu.h"                                        /* CPU detection */
68 #include "os_specific.h"
69
70 #include "netutils.h"                                 /* network_ChannelJoin */
71
72 #include "stream_control.h"
73 #include "input_ext-intf.h"
74
75 #include "vlc_playlist.h"
76 #include "interface.h"
77
78 #include "audio_output.h"
79
80 #include "video.h"
81 #include "video_output.h"
82
83 #include "libvlc.h"
84
85 /*****************************************************************************
86  * The evil global variables. We handle them with care, don't worry.
87  *****************************************************************************/
88
89 /* This global lock is used for critical sections - don't abuse it! */
90 static vlc_mutex_t global_lock;
91 void *             p_global_data;
92
93 /* A list of all the currently allocated vlc objects */
94 static int volatile i_vlc = 0;
95 static int volatile i_unique = 0;
96 static vlc_t ** volatile pp_vlc = NULL;
97
98 /*****************************************************************************
99  * Local prototypes
100  *****************************************************************************/
101 static int  GetFilenames  ( vlc_t *, int, char *[] );
102 static void Usage         ( vlc_t *, const char *psz_module_name );
103 static void ListModules   ( vlc_t * );
104 static void Version       ( void );
105 static void Build         ( void );
106
107 #ifndef WIN32
108 static void InitSignalHandler   ( void );
109 static void SimpleSignalHandler ( int i_signal );
110 static void FatalSignalHandler  ( int i_signal );
111 #endif
112
113 #ifdef WIN32
114 static void ShowConsole   ( void );
115 #endif
116
117 /*****************************************************************************
118  * vlc_create: allocate a vlc_t structure, and initialize libvlc if needed.
119  *****************************************************************************
120  * This function allocates a vlc_t structure and returns NULL in case of
121  * failure. Also, the thread system and the signal handlers are initialized.
122  *****************************************************************************/
123 vlc_error_t vlc_create( void )
124 {
125     vlc_t * p_vlc = vlc_create_r();
126     return p_vlc ? VLC_SUCCESS : VLC_EGENERIC;
127 }
128
129 vlc_t * vlc_create_r( void )
130 {
131     vlc_t * p_vlc = NULL;
132
133     /* Allocate the main structure */
134     p_vlc = vlc_object_create( p_vlc, VLC_OBJECT_ROOT );
135     if( p_vlc == NULL )
136     {
137         return NULL;
138     }
139
140     p_vlc->psz_object_name = "root";
141
142     p_vlc->p_global_lock = &global_lock;
143     p_vlc->pp_global_data = &p_global_data;
144
145     p_vlc->b_verbose = VLC_FALSE;
146     p_vlc->b_quiet = VLC_FALSE; /* FIXME: delay message queue output! */
147
148     /* Initialize the threads system */
149     vlc_threads_init( p_vlc );
150
151     /* Initialize mutexes */
152     vlc_mutex_init( p_vlc, &p_vlc->config_lock );
153     vlc_mutex_init( p_vlc, &p_vlc->structure_lock );
154
155     /* Set signal handling policy for all threads */
156 #ifndef WIN32
157     InitSignalHandler( );
158 #endif
159
160     /* Store our newly allocated structure in the global list */
161     vlc_mutex_lock( p_vlc->p_global_lock );
162     pp_vlc = realloc( pp_vlc, (i_vlc+1) * sizeof( vlc_t * ) );
163     pp_vlc[ i_vlc ] = p_vlc;
164     i_vlc++;
165     p_vlc->i_unique = i_unique;
166     i_unique++;
167     vlc_mutex_unlock( p_vlc->p_global_lock );
168
169     /* Update the handle status */
170     p_vlc->i_status = VLC_STATUS_CREATED;
171
172     return p_vlc;
173 }
174
175 /*****************************************************************************
176  * vlc_init: initialize a vlc_t structure.
177  *****************************************************************************
178  * This function initializes a previously allocated vlc_t structure:
179  *  - CPU detection
180  *  - gettext initialization
181  *  - message queue, module bank and playlist initialization
182  *  - configuration and commandline parsing
183  *****************************************************************************/
184 vlc_error_t vlc_init( int i_argc, char *ppsz_argv[] )
185 {
186     return vlc_init_r( ( i_vlc == 1 ) ? *pp_vlc : NULL, i_argc, ppsz_argv );
187 }
188
189 vlc_error_t vlc_init_r( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
190 {
191     char p_capabilities[200];
192     char *psz_module;
193     char *p_tmp;
194     module_t        *p_help_module;
195     playlist_t      *p_playlist;
196
197     /* Check that the handle is valid */
198     if( !p_vlc || p_vlc->i_status != VLC_STATUS_CREATED )
199     {
200         fprintf( stderr, "error: invalid status (!CREATED)\n" );
201         return VLC_ESTATUS;
202     }
203
204     fprintf( stderr, COPYRIGHT_MESSAGE "\n" );
205
206     /* Guess what CPU we have */
207     p_vlc->i_cpu_capabilities = CPUCapabilities( p_vlc );
208
209     /*
210      * Support for gettext
211      */
212 #if defined( ENABLE_NLS ) && defined ( HAVE_GETTEXT )
213 #   if defined( HAVE_LOCALE_H ) && defined( HAVE_LC_MESSAGES )
214     if( !setlocale( LC_MESSAGES, "" ) )
215     {
216         fprintf( stderr, "warning: unsupported locale settings\n" );
217     }
218
219     setlocale( LC_CTYPE, "" );
220 #   endif
221
222     if( !bindtextdomain( PACKAGE, LOCALEDIR ) )
223     {
224         fprintf( stderr, "warning: no domain %s in directory %s\n",
225                  PACKAGE, LOCALEDIR );
226     }
227
228     textdomain( PACKAGE );
229 #endif
230
231     /*
232      * System specific initialization code
233      */
234     system_Init( p_vlc, &i_argc, ppsz_argv );
235
236     /*
237      * Initialize message queue
238      */
239     msg_Create( p_vlc );
240
241     /* Get the executable name (similar to the basename command) */
242     if( i_argc > 0 )
243     {
244         p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
245         while( *p_tmp )
246         {
247             if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
248             else ++p_tmp;
249         }
250     }
251     else
252     {
253         p_vlc->psz_object_name = "vlc";
254     }
255
256     /* Announce who we are */
257     msg_Dbg( p_vlc, COPYRIGHT_MESSAGE );
258     msg_Dbg( p_vlc, "libvlc was configured with %s", CONFIGURE_LINE );
259
260     /*
261      * Initialize the module bank and and load the configuration of the main
262      * module. We need to do this at this stage to be able to display a short
263      * help if required by the user. (short help == main module options)
264      */
265     module_InitBank( p_vlc );
266     module_LoadMain( p_vlc );
267
268     /* Hack: insert the help module here */
269     p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
270     if( p_help_module == NULL )
271     {
272         module_EndBank( p_vlc );
273         msg_Destroy( p_vlc );
274         return VLC_EGENERIC;
275     }
276     p_help_module->psz_object_name = "help";
277     config_Duplicate( p_help_module, p_help_config );
278     p_help_module->next = p_vlc->module_bank.first;
279     p_vlc->module_bank.first = p_help_module;
280     /* End hack */
281
282     if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
283     {
284         p_vlc->module_bank.first = p_help_module->next;
285         config_Free( p_help_module );
286         vlc_object_destroy( p_help_module );
287         module_EndBank( p_vlc );
288         msg_Destroy( p_vlc );
289         return VLC_EGENERIC;
290     }
291
292     /* Check for short help option */
293     if( config_GetInt( p_vlc, "help" ) )
294     {
295         fprintf( stderr, _("Usage: %s [options] [parameters] [file]...\n"),
296                          p_vlc->psz_object_name );
297
298         Usage( p_vlc, "help" );
299         Usage( p_vlc, "main" );
300         p_vlc->module_bank.first = p_help_module->next;
301         config_Free( p_help_module );
302         vlc_object_destroy( p_help_module );
303         module_EndBank( p_vlc );
304         msg_Destroy( p_vlc );
305         return VLC_EEXIT;
306     }
307
308     /* Check for version option */
309     if( config_GetInt( p_vlc, "version" ) )
310     {
311         Version();
312         p_vlc->module_bank.first = p_help_module->next;
313         config_Free( p_help_module );
314         vlc_object_destroy( p_help_module );
315         module_EndBank( p_vlc );
316         msg_Destroy( p_vlc );
317         return VLC_EEXIT;
318     }
319
320     /* Check for build option */
321     if( config_GetInt( p_vlc, "build" ) )
322     {
323         Build();
324         p_vlc->module_bank.first = p_help_module->next;
325         config_Free( p_help_module );
326         vlc_object_destroy( p_help_module );
327         module_EndBank( p_vlc );
328         msg_Destroy( p_vlc );
329         return VLC_EEXIT;
330     }
331
332     /* Hack: remove the help module here */
333     p_vlc->module_bank.first = p_help_module->next;
334     /* End hack */
335
336     /*
337      * Load the builtins and plugins into the module_bank.
338      * We have to do it before config_Load*() because this also gets the
339      * list of configuration options exported by each module and loads their
340      * default values.
341      */
342     module_LoadBuiltins( p_vlc );
343     module_LoadPlugins( p_vlc );
344     msg_Dbg( p_vlc, "module bank initialized, found %i modules",
345                     p_vlc->module_bank.i_count );
346
347     /* Hack: insert the help module here */
348     p_help_module->next = p_vlc->module_bank.first;
349     p_vlc->module_bank.first = p_help_module;
350     /* End hack */
351
352     /* Check for help on modules */
353     if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
354     {
355         Usage( p_vlc, p_tmp );
356         free( p_tmp );
357         p_vlc->module_bank.first = p_help_module->next;
358         config_Free( p_help_module );
359         vlc_object_destroy( p_help_module );
360         module_EndBank( p_vlc );
361         msg_Destroy( p_vlc );
362         return VLC_EGENERIC;
363     }
364
365     /* Check for long help option */
366     if( config_GetInt( p_vlc, "longhelp" ) )
367     {
368         Usage( p_vlc, NULL );
369         p_vlc->module_bank.first = p_help_module->next;
370         config_Free( p_help_module );
371         vlc_object_destroy( p_help_module );
372         module_EndBank( p_vlc );
373         msg_Destroy( p_vlc );
374         return VLC_EEXIT;
375     }
376
377     /* Check for module list option */
378     if( config_GetInt( p_vlc, "list" ) )
379     {
380         ListModules( p_vlc );
381         p_vlc->module_bank.first = p_help_module->next;
382         config_Free( p_help_module );
383         vlc_object_destroy( p_help_module );
384         module_EndBank( p_vlc );
385         msg_Destroy( p_vlc );
386         return VLC_EEXIT;
387     }
388
389     /* Hack: remove the help module here */
390     p_vlc->module_bank.first = p_help_module->next;
391     config_Free( p_help_module );
392     vlc_object_destroy( p_help_module );
393     /* End hack */
394
395     /*
396      * Override default configuration with config file settings
397      */
398     p_vlc->psz_homedir = config_GetHomeDir();
399     config_LoadConfigFile( p_vlc, NULL );
400
401     /*
402      * Override configuration with command line settings
403      */
404     if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
405     {
406 #ifdef WIN32
407         ShowConsole();
408         /* Pause the console because it's destroyed when we exit */
409         fprintf( stderr, "The command line options couldn't be loaded, check "
410                  "that they are valid.\nPress the RETURN key to continue..." );
411         getchar();
412 #endif
413         module_EndBank( p_vlc );
414         msg_Destroy( p_vlc );
415         return VLC_EGENERIC;
416     }
417
418     /*
419      * System specific configuration
420      */
421     system_Configure( p_vlc );
422
423     /*
424      * Output messages that may still be in the queue
425      */
426     p_vlc->b_verbose = config_GetInt( p_vlc, "verbose" );
427     p_vlc->b_quiet = config_GetInt( p_vlc, "quiet" );
428     p_vlc->b_color = config_GetInt( p_vlc, "color" );
429     msg_Flush( p_vlc );
430
431     /* p_vlc inititalization. FIXME ? */
432     p_vlc->i_desync = config_GetInt( p_vlc, "desync" ) * (mtime_t)1000;
433     if( !config_GetInt( p_vlc, "mmx" ) )
434         p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_MMX;
435     if( !config_GetInt( p_vlc, "3dn" ) )
436         p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_3DNOW;
437     if( !config_GetInt( p_vlc, "mmxext" ) )
438         p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_MMXEXT;
439     if( !config_GetInt( p_vlc, "sse" ) )
440         p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_SSE;
441     if( !config_GetInt( p_vlc, "altivec" ) )
442         p_vlc->i_cpu_capabilities &= ~CPU_CAPABILITY_ALTIVEC;
443
444 #define PRINT_CAPABILITY( capability, string )                              \
445     if( p_vlc->i_cpu_capabilities & capability )                            \
446     {                                                                       \
447         strncat( p_capabilities, string " ",                                \
448                  sizeof(p_capabilities) - strlen(p_capabilities) );         \
449         p_capabilities[sizeof(p_capabilities) - 1] = '\0';                  \
450     }
451
452     p_capabilities[0] = '\0';
453     PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
454     PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
455     PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
456     PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
457     PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
458     PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
459     PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
460     PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
461     PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
462     msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
463
464     /*
465      * Choose the best memcpy module
466      */
467     psz_module = config_GetPsz( p_vlc, "memcpy" );
468     p_vlc->p_memcpy_module =
469             module_Need( p_vlc, MODULE_CAPABILITY_MEMCPY, psz_module, NULL );
470     if( psz_module ) free( psz_module );
471     if( p_vlc->p_memcpy_module == NULL )
472     {
473         msg_Err( p_vlc, "no suitable memcpy module, using libc default" );
474         p_vlc->pf_memcpy = memcpy;
475     }
476     else
477     {
478         p_vlc->pf_memcpy = p_vlc->p_memcpy_module->p_functions
479                                   ->memcpy.functions.memcpy.pf_memcpy;
480     }
481
482     /*
483      * Initialize shared resources and libraries
484      */
485     if( config_GetInt( p_vlc, "network-channel" )
486          && network_ChannelCreate( p_vlc ) )
487     {
488         /* On error during Channels initialization, switch off channels */
489         msg_Err( p_vlc,
490                  "channels initialization failed, deactivating channels" );
491         config_PutInt( p_vlc, "network-channel", VLC_FALSE );
492     }
493
494     /*
495      * Initialize playlist and get commandline files
496      */
497     p_playlist = playlist_Create( p_vlc );
498     if( !p_playlist )
499     {
500         msg_Err( p_vlc, "playlist initialization failed" );
501         if( p_vlc->p_memcpy_module != NULL )
502         {
503             module_Unneed( p_vlc->p_memcpy_module );
504         }
505         module_EndBank( p_vlc );
506         msg_Destroy( p_vlc );
507         return VLC_EGENERIC;
508     }
509
510     /* Update the handle status */
511     p_vlc->i_status = VLC_STATUS_STOPPED;
512
513     /*
514      * Get input filenames given as commandline arguments
515      */
516     GetFilenames( p_vlc, i_argc, ppsz_argv );
517
518     return VLC_SUCCESS;
519 }
520
521 /*****************************************************************************
522  * vlc_run: run vlc
523  *****************************************************************************
524  * XXX: This function opens an interface plugin and runs it. If b_block is set
525  * to 0, vlc_add_intf will return immediately and let the interface run in a
526  * separate thread. If b_block is set to 1, vlc_add_intf will continue until
527  * user requests to quit.
528  *****************************************************************************/
529 vlc_error_t vlc_run( void )
530 {
531     return vlc_run_r( ( i_vlc == 1 ) ? *pp_vlc : NULL );
532 }
533
534 vlc_error_t vlc_run_r( vlc_t *p_vlc )
535 {
536     /* Check that the handle is valid */
537     if( !p_vlc || p_vlc->i_status != VLC_STATUS_STOPPED )
538     {
539         fprintf( stderr, "error: invalid status (!STOPPED)\n" );
540         return VLC_ESTATUS;
541     }
542
543     /* Update the handle status */
544     p_vlc->i_status = VLC_STATUS_RUNNING;
545
546     return VLC_SUCCESS;
547 }
548
549 /*****************************************************************************
550  * vlc_add_intf: add an interface
551  *****************************************************************************
552  * This function opens an interface plugin and runs it. If b_block is set
553  * to 0, vlc_add_intf will return immediately and let the interface run in a
554  * separate thread. If b_block is set to 1, vlc_add_intf will continue until
555  * user requests to quit.
556  *****************************************************************************/
557 vlc_error_t vlc_add_intf( const char *psz_module, vlc_bool_t b_block )
558 {
559     return vlc_add_intf_r( ( i_vlc == 1 ) ? *pp_vlc : NULL,
560                            psz_module, b_block );
561 }
562
563 vlc_error_t vlc_add_intf_r( vlc_t *p_vlc, const char *psz_module,
564                                           vlc_bool_t b_block )
565 {
566     vlc_error_t err;
567     intf_thread_t *p_intf;
568     char *psz_oldmodule = NULL;
569
570     /* Check that the handle is valid */
571     if( !p_vlc || p_vlc->i_status != VLC_STATUS_RUNNING )
572     {
573         fprintf( stderr, "error: invalid status (!RUNNING)\n" );
574         return VLC_ESTATUS;
575     }
576
577     if( psz_module )
578     {
579         psz_oldmodule = config_GetPsz( p_vlc, "intf" );
580         config_PutPsz( p_vlc, "intf", psz_module );
581     }
582
583     /* Try to create the interface */
584     p_intf = intf_Create( p_vlc );
585
586     if( psz_module )
587     {
588         config_PutPsz( p_vlc, "intf", psz_oldmodule );
589         if( psz_oldmodule )
590         {
591             free( psz_oldmodule );
592         }
593     }
594
595     if( p_intf == NULL )
596     {
597         msg_Err( p_vlc, "interface initialization failed" );
598         return VLC_EGENERIC;
599     }
600
601     /* Try to run the interface */
602     p_intf->b_block = b_block;
603     err = intf_RunThread( p_intf );
604     if( err )
605     {
606         vlc_object_detach_all( p_intf );
607         intf_Destroy( p_intf );
608         return err;
609     }
610
611     return VLC_SUCCESS;
612 }
613
614 /*****************************************************************************
615  * vlc_stop: stop playing.
616  *****************************************************************************
617  * This function requests the interface threads to finish, waits for their
618  * termination, and destroys their structure.
619  *****************************************************************************/
620 vlc_error_t vlc_stop( void )
621 {
622     return vlc_stop_r( ( i_vlc == 1 ) ? *pp_vlc : NULL );
623 }
624
625 vlc_error_t vlc_stop_r( vlc_t *p_vlc )
626 {
627     intf_thread_t *p_intf;
628     playlist_t    *p_playlist;
629     vout_thread_t *p_vout;
630     aout_thread_t *p_aout;
631
632     /* Check that the handle is valid */
633     if( !p_vlc || p_vlc->i_status != VLC_STATUS_RUNNING )
634     {
635         fprintf( stderr, "error: invalid status (!RUNNING)\n" );
636         return VLC_ESTATUS;
637     }
638
639     /*
640      * Ask the interfaces to stop and destroy them
641      */
642     msg_Dbg( p_vlc, "removing all interfaces" );
643     while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
644     {
645         intf_StopThread( p_intf );
646         vlc_object_detach_all( p_intf );
647         vlc_object_release( p_intf );
648         intf_Destroy( p_intf );
649     }
650
651     /*
652      * Free playlists
653      */
654     msg_Dbg( p_vlc, "removing all playlists" );
655     while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
656                                           FIND_CHILD )) )
657     {
658         vlc_object_detach_all( p_playlist );
659         vlc_object_release( p_playlist );
660         playlist_Destroy( p_playlist );
661     }
662
663     /*
664      * Free video outputs
665      */
666     msg_Dbg( p_vlc, "removing all video outputs" );
667     while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
668     {
669         vlc_object_detach_all( p_vout );
670         vlc_object_release( p_vout );
671         vout_DestroyThread( p_vout );
672     }
673
674     /*
675      * Free audio outputs
676      */
677     msg_Dbg( p_vlc, "removing all audio outputs" );
678     while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
679     {
680         vlc_object_detach_all( p_aout );
681         vlc_object_release( p_aout );
682         aout_DestroyThread( p_aout );
683     }
684
685     /* Update the handle status */
686     p_vlc->i_status = VLC_STATUS_STOPPED;
687
688     return VLC_SUCCESS;
689 }
690
691 /*****************************************************************************
692  * vlc_end: uninitialize everything.
693  *****************************************************************************
694  * This function uninitializes every vlc component that was activated in
695  * vlc_init: audio and video outputs, playlist, module bank and message queue.
696  *****************************************************************************/
697 vlc_error_t vlc_end( void )
698 {
699     return vlc_end_r( ( i_vlc == 1 ) ? *pp_vlc : NULL );
700 }
701
702 vlc_error_t vlc_end_r( vlc_t *p_vlc )
703 {
704     /* Check that the handle is valid */
705     if( !p_vlc || p_vlc->i_status != VLC_STATUS_STOPPED )
706     {
707         fprintf( stderr, "error: invalid status (!STOPPED)\n" );
708         return VLC_ESTATUS;
709     }
710
711     /*
712      * Go back into channel 0 which is the network
713      */
714     if( config_GetInt( p_vlc, "network-channel" ) && p_vlc->p_channel )
715     {
716         network_ChannelJoin( p_vlc, COMMON_CHANNEL );
717     }
718
719     /*
720      * Free allocated memory
721      */
722     if( p_vlc->p_memcpy_module != NULL )
723     {
724         module_Unneed( p_vlc->p_memcpy_module );
725     }
726
727     free( p_vlc->psz_homedir );
728
729     /*
730      * Free module bank
731      */
732     module_EndBank( p_vlc );
733
734     /*
735      * System specific cleaning code
736      */
737     system_End( p_vlc );
738
739     /*
740      * Terminate messages interface and program
741      */
742     msg_Destroy( p_vlc );
743
744     /* Update the handle status */
745     p_vlc->i_status = VLC_STATUS_CREATED;
746
747     return VLC_SUCCESS;
748 }
749
750 /*****************************************************************************
751  * vlc_destroy: free allocated resources.
752  *****************************************************************************
753  * This function frees the previously allocated vlc_t structure.
754  *****************************************************************************/
755 vlc_error_t vlc_destroy( void )
756 {
757     return vlc_destroy_r( ( i_vlc == 1 ) ? *pp_vlc : NULL );
758 }
759
760 vlc_error_t vlc_destroy_r( vlc_t *p_vlc )
761 {
762     int i_index;
763
764     /* Check that the handle is valid */
765     if( !p_vlc || p_vlc->i_status != VLC_STATUS_CREATED )
766     {
767         fprintf( stderr, "error: invalid status (!CREATED)\n" );
768         return VLC_ESTATUS;
769     }
770
771     /* Update the handle status, just in case */
772     p_vlc->i_status = VLC_STATUS_NONE;
773
774     /* Remove our structure from the global list */
775     vlc_mutex_lock( p_vlc->p_global_lock );
776     for( i_index = 0 ; i_index < i_vlc ; i_index++ )
777     {
778         if( pp_vlc[ i_index ] == p_vlc )
779         {
780             break;
781         }
782     }
783
784     if( i_index == i_vlc )
785     {
786         fprintf( stderr, "error: trying to unregister %p which is not in "
787                          "the list\n", p_vlc );
788         vlc_mutex_unlock( p_vlc->p_global_lock );
789         vlc_object_destroy( p_vlc );
790         return VLC_EGENERIC;
791     }
792
793     for( i_index++ ; i_index < i_vlc ; i_index++ )
794     {
795         pp_vlc[ i_index - 1 ] = pp_vlc[ i_index ];
796     }
797
798     i_vlc--;
799     if( i_vlc )
800     {
801         pp_vlc = realloc( pp_vlc, i_vlc * sizeof( vlc_t * ) );
802     }
803     else
804     {
805         free( pp_vlc );
806         pp_vlc = NULL;
807     }
808     vlc_mutex_unlock( p_vlc->p_global_lock );
809
810     /* Stop thread system: last one out please shut the door! */
811     vlc_threads_end( p_vlc );
812
813     /* Destroy mutexes */
814     vlc_mutex_destroy( &p_vlc->structure_lock );
815     vlc_mutex_destroy( &p_vlc->config_lock );
816
817     vlc_object_destroy( p_vlc );
818
819     return VLC_SUCCESS;
820 }
821
822 vlc_status_t vlc_status( void )
823 {
824     return vlc_status_r( ( i_vlc == 1 ) ? *pp_vlc : NULL );
825 }
826
827 vlc_status_t vlc_status_r( vlc_t *p_vlc )
828 {
829     if( !p_vlc )
830     {
831         return VLC_STATUS_NONE;
832     }
833
834     return p_vlc->i_status;
835 }
836
837 vlc_error_t vlc_add_target( const char *psz_target, int i_mode, int i_pos )
838 {
839     return vlc_add_target_r( ( i_vlc == 1 ) ? *pp_vlc : NULL,
840                              psz_target, i_mode, i_pos );
841 }
842
843 vlc_error_t vlc_add_target_r( vlc_t *p_vlc, const char *psz_target,
844                                             int i_mode, int i_pos )
845 {
846     vlc_error_t err;
847     playlist_t *p_playlist;
848
849     if( !p_vlc || ( p_vlc->i_status != VLC_STATUS_STOPPED
850                      && p_vlc->i_status != VLC_STATUS_RUNNING ) )
851     {
852         fprintf( stderr, "error: invalid status (!STOPPED&&!RUNNING)\n" );
853         return VLC_ESTATUS;
854     }
855
856     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
857
858     if( p_playlist == NULL )
859     {
860         msg_Dbg( p_vlc, "no playlist present, creating one" );
861         p_playlist = playlist_Create( p_vlc );
862
863         if( p_playlist == NULL )
864         {
865             return VLC_EGENERIC;
866         }
867
868         vlc_object_yield( p_playlist );
869     }
870
871     err = playlist_Add( p_playlist, psz_target, i_mode, i_pos );
872
873     vlc_object_release( p_playlist );
874
875     return err;
876 }
877
878 /* following functions are local */
879
880 /*****************************************************************************
881  * GetFilenames: parse command line options which are not flags
882  *****************************************************************************
883  * Parse command line for input files.
884  *****************************************************************************/
885 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
886 {
887     int i_opt;
888
889     /* We assume that the remaining parameters are filenames */
890     for( i_opt = optind; i_opt < i_argc; i_opt++ )
891     {
892         vlc_add_target_r( p_vlc, ppsz_argv[ i_opt ],
893                           PLAYLIST_APPEND, PLAYLIST_END );
894     }
895
896     return VLC_SUCCESS;
897 }
898
899 /*****************************************************************************
900  * Usage: print program usage
901  *****************************************************************************
902  * Print a short inline help. Message interface is initialized at this stage.
903  *****************************************************************************/
904 static void Usage( vlc_t *p_this, const char *psz_module_name )
905 {
906 #define FORMAT_STRING "      --%s%s%s%s%s%s%s %s%s\n"
907     /* option name -------------'     | | | |  | |
908      * <bra --------------------------' | | |  | |
909      * option type or "" ---------------' | |  | |
910      * ket> ------------------------------' |  | |
911      * padding spaces ----------------------'  | |
912      * comment --------------------------------' |
913      * comment suffix ---------------------------'
914      *
915      * The purpose of having bra and ket is that we might i18n them as well.
916      */
917 #define LINE_START 8
918 #define PADDING_SPACES 25
919     module_t *p_module;
920     module_config_t *p_item;
921     char psz_spaces[PADDING_SPACES+LINE_START+1];
922     char psz_format[sizeof(FORMAT_STRING)];
923
924     memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
925     psz_spaces[PADDING_SPACES+LINE_START] = '\0';
926
927     strcpy( psz_format, FORMAT_STRING );
928
929 #ifdef WIN32
930     ShowConsole();
931 #endif
932
933     /* Enumerate the config for each module */
934     for( p_module = p_this->p_vlc->module_bank.first ;
935          p_module != NULL ;
936          p_module = p_module->next )
937     {
938         vlc_bool_t b_help_module = !strcmp( "help", p_module->psz_object_name );
939
940         if( psz_module_name && strcmp( psz_module_name,
941                                        p_module->psz_object_name ) )
942         {
943             continue;
944         }
945
946         /* Ignore modules without config options */
947         if( !p_module->i_config_items )
948         {
949             continue;
950         }
951
952         /* Print module name */
953         fprintf( stderr, _("%s module options:\n\n"),
954                          p_module->psz_object_name );
955
956         for( p_item = p_module->p_config;
957              p_item->i_type != CONFIG_HINT_END;
958              p_item++ )
959         {
960             char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
961             char *psz_suf = "", *psz_prefix = NULL;
962             int i;
963
964             switch( p_item->i_type )
965             {
966             case CONFIG_HINT_CATEGORY:
967             case CONFIG_HINT_USAGE:
968                 fprintf( stderr, " %s\n", p_item->psz_text );
969                 break;
970
971             case CONFIG_ITEM_STRING:
972             case CONFIG_ITEM_FILE:
973             case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
974                 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
975                 break;
976             case CONFIG_ITEM_INTEGER:
977                 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
978                 break;
979             case CONFIG_ITEM_FLOAT:
980                 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
981                 break;
982             case CONFIG_ITEM_BOOL:
983                 psz_bra = ""; psz_type = ""; psz_ket = "";
984                 if( !b_help_module )
985                 {
986                     psz_suf = p_item->i_value ? _(" (default enabled)") :
987                                                 _(" (default disabled)");
988                 }
989                 break;
990             }
991
992             /* Add short option */
993             if( p_item->i_short )
994             {
995                 psz_format[2] = '-';
996                 psz_format[3] = p_item->i_short;
997                 psz_format[4] = ',';
998             }
999             else
1000             {
1001                 psz_format[2] = ' ';
1002                 psz_format[3] = ' ';
1003                 psz_format[4] = ' ';
1004             }
1005
1006             if( psz_type )
1007             {
1008                 i = PADDING_SPACES - strlen( p_item->psz_name )
1009                      - strlen( psz_bra ) - strlen( psz_type )
1010                      - strlen( psz_ket ) - 1;
1011                 if( p_item->i_type == CONFIG_ITEM_BOOL
1012                      && !b_help_module )
1013                 {
1014                     vlc_bool_t b_dash = VLC_FALSE;
1015                     psz_prefix = p_item->psz_name;
1016                     while( *psz_prefix )
1017                     {
1018                         if( *psz_prefix++ == '-' )
1019                         {
1020                             b_dash = VLC_TRUE;
1021                             break;
1022                         }
1023                     }
1024
1025                     if( b_dash )
1026                     {
1027                         psz_prefix = ", --no-";
1028                         i -= strlen( p_item->psz_name ) + strlen( ", --no-" );
1029                     }
1030                     else
1031                     {
1032                         psz_prefix = ", --no";
1033                         i -= strlen( p_item->psz_name ) + strlen( ", --no" );
1034                     }
1035                 }
1036
1037                 if( i < 0 )
1038                 {
1039                     i = 0;
1040                     psz_spaces[i] = '\n';
1041                 }
1042                 else
1043                 {
1044                     psz_spaces[i] = '\0';
1045                 }
1046
1047                 if( p_item->i_type == CONFIG_ITEM_BOOL &&
1048                     !b_help_module )
1049                 {
1050                     fprintf( stderr, psz_format, p_item->psz_name, psz_prefix,
1051                              p_item->psz_name, psz_bra, psz_type, psz_ket,
1052                              psz_spaces, p_item->psz_text, psz_suf );
1053                 }
1054                 else
1055                 {
1056                     fprintf( stderr, psz_format, p_item->psz_name, "", "",
1057                              psz_bra, psz_type, psz_ket, psz_spaces,
1058                              p_item->psz_text, psz_suf );
1059                 }
1060                 psz_spaces[i] = ' ';
1061             }
1062         }
1063
1064         fprintf( stderr, "\n" );
1065
1066     }
1067
1068 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1069         fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1070         getchar();
1071 #endif
1072 }
1073
1074 /*****************************************************************************
1075  * ListModules: list the available modules with their description
1076  *****************************************************************************
1077  * Print a list of all available modules (builtins and plugins) and a short
1078  * description for each one.
1079  *****************************************************************************/
1080 static void ListModules( vlc_t *p_this )
1081 {
1082     module_t *p_module;
1083     char psz_spaces[22];
1084
1085     memset( psz_spaces, ' ', 22 );
1086
1087 #ifdef WIN32
1088     ShowConsole();
1089 #endif
1090
1091     /* Usage */
1092     fprintf( stderr, _("Usage: %s [options] [parameters] [file]...\n\n"),
1093                      p_this->p_vlc->psz_object_name );
1094
1095     fprintf( stderr, _("[module]              [description]\n") );
1096
1097     /* Enumerate each module */
1098     for( p_module = p_this->p_vlc->module_bank.first ;
1099          p_module != NULL ;
1100          p_module = p_module->next )
1101     {
1102         int i;
1103
1104         /* Nasty hack, but right now I'm too tired to think about a nice
1105          * solution */
1106         i = 22 - strlen( p_module->psz_object_name ) - 1;
1107         if( i < 0 ) i = 0;
1108         psz_spaces[i] = 0;
1109
1110         fprintf( stderr, "  %s%s %s\n", p_module->psz_object_name, psz_spaces,
1111                   p_module->psz_longname );
1112
1113         psz_spaces[i] = ' ';
1114
1115     }
1116
1117 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1118         fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1119         getchar();
1120 #endif
1121 }
1122
1123 /*****************************************************************************
1124  * Version: print complete program version
1125  *****************************************************************************
1126  * Print complete program version and build number.
1127  *****************************************************************************/
1128 static void Version( void )
1129 {
1130 #ifdef WIN32
1131     ShowConsole();
1132 #endif
1133
1134     fprintf( stderr, VERSION_MESSAGE "\n" );
1135     fprintf( stderr,
1136       _("This program comes with NO WARRANTY, to the extent permitted by "
1137         "law.\nYou may redistribute it under the terms of the GNU General "
1138         "Public License;\nsee the file named COPYING for details.\n"
1139         "Written by the VideoLAN team at Ecole Centrale, Paris.\n") );
1140
1141 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1142     fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1143     getchar();
1144 #endif
1145 }
1146
1147 /*****************************************************************************
1148  * Build: print information about the vlc build
1149  *****************************************************************************
1150  * Print the ./configure command line and other information.
1151  *****************************************************************************/
1152 static void Build( void )
1153 {
1154 #ifdef WIN32
1155     ShowConsole();
1156 #endif
1157
1158     fprintf( stderr, "configured with %s\n", CONFIGURE_LINE );
1159
1160 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1161     fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1162     getchar();
1163 #endif
1164 }
1165
1166 /*****************************************************************************
1167  * ShowConsole: On Win32, create an output console for debug messages
1168  *****************************************************************************
1169  * This function is useful only on Win32.
1170  *****************************************************************************/
1171 #ifdef WIN32 /*  */
1172 static void ShowConsole( void )
1173 {
1174     AllocConsole();
1175     freopen( "CONOUT$", "w", stdout );
1176     freopen( "CONOUT$", "w", stderr );
1177     freopen( "CONIN$", "r", stdin );
1178     return;
1179 }
1180 #endif
1181
1182 #ifndef WIN32
1183 /*****************************************************************************
1184  * InitSignalHandler: system signal handler initialization
1185  *****************************************************************************
1186  * Set the signal handlers. SIGTERM is not intercepted, because we need at
1187  * at least a method to kill the program when all other methods failed, and
1188  * when we don't want to use SIGKILL.
1189  *****************************************************************************/
1190 static void InitSignalHandler( void )
1191 {
1192     /* Termination signals */
1193     signal( SIGINT,  FatalSignalHandler );
1194     signal( SIGHUP,  FatalSignalHandler );
1195     signal( SIGQUIT, FatalSignalHandler );
1196
1197     /* Other signals */
1198     signal( SIGALRM, SimpleSignalHandler );
1199     signal( SIGPIPE, SimpleSignalHandler );
1200 }
1201
1202 /*****************************************************************************
1203  * SimpleSignalHandler: system signal handler
1204  *****************************************************************************
1205  * This function is called when a non fatal signal is received by the program.
1206  *****************************************************************************/
1207 static void SimpleSignalHandler( int i_signal )
1208 {
1209     int i_index;
1210
1211     /* Acknowledge the signal received and warn all the p_vlc structures */
1212     vlc_mutex_lock( &global_lock );
1213     for( i_index = 0 ; i_index < i_vlc ; i_index++ )
1214     {
1215         msg_Warn( pp_vlc[ i_index ], "ignoring signal %d", i_signal );
1216     }
1217     vlc_mutex_unlock( &global_lock );
1218 }
1219
1220 /*****************************************************************************
1221  * FatalSignalHandler: system signal handler
1222  *****************************************************************************
1223  * This function is called when a fatal signal is received by the program.
1224  * It tries to end the program in a clean way.
1225  *****************************************************************************/
1226 static void FatalSignalHandler( int i_signal )
1227 {
1228     static mtime_t abort_time = 0;
1229     static volatile vlc_bool_t b_die = VLC_FALSE;
1230     int i_index;
1231
1232     /* Once a signal has been trapped, the termination sequence will be
1233      * armed and following signals will be ignored to avoid sending messages
1234      * to an interface having been destroyed */
1235
1236     if( !b_die )
1237     {
1238         b_die = VLC_TRUE;
1239         abort_time = mdate();
1240
1241         fprintf( stderr, "signal %d received, terminating libvlc - do it "
1242                          "again in case your process gets stuck\n", i_signal );
1243
1244         /* Try to terminate everything - this is done by requesting the end of
1245          * all the p_vlc structures */
1246         for( i_index = 0 ; i_index < i_vlc ; i_index++ )
1247         {
1248             /* Acknowledge the signal received */
1249             pp_vlc[ i_index ]->b_die = VLC_TRUE;
1250         }
1251     }
1252     else if( mdate() > abort_time + 1000000 )
1253     {
1254         /* If user asks again 1 second later, die badly */
1255         signal( SIGINT,  SIG_IGN );
1256         signal( SIGHUP,  SIG_IGN );
1257         signal( SIGQUIT, SIG_IGN );
1258
1259         fprintf( stderr, "user insisted too much, dying badly\n" );
1260
1261         exit( 1 );
1262     }
1263 }
1264 #endif
1265