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