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