]> git.sesse.net Git - vlc/blob - src/libvlc.c
* include/variables.h
[vlc] / src / libvlc.c
1 /*****************************************************************************
2  * libvlc.c: main libvlc source
3  *****************************************************************************
4  * Copyright (C) 1998-2004 VideoLAN
5  * $Id: libvlc.c,v 1.110 2004/01/09 20:36:21 hartman 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 MODULE_PATH main
31 #define __BUILTIN__
32
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36 #include <vlc/vlc.h>
37
38 #ifdef HAVE_ERRNO_H
39 #   include <errno.h>                                              /* ENOMEM */
40 #endif
41 #include <stdio.h>                                              /* sprintf() */
42 #include <string.h>                                            /* strerror() */
43 #include <stdlib.h>                                                /* free() */
44
45 #ifndef WIN32
46 #   include <netinet/in.h>                            /* BSD: struct in_addr */
47 #endif
48
49 #ifdef HAVE_UNISTD_H
50 #   include <unistd.h>
51 #elif defined( WIN32 ) && !defined( UNDER_CE )
52 #   include <io.h>
53 #endif
54
55 #ifdef WIN32                       /* optind, getopt(), included in unistd.h */
56 #   include "extras/getopt.h"
57 #endif
58
59 #ifdef HAVE_LOCALE_H
60 #   include <locale.h>
61 #endif
62
63 #include "vlc_cpu.h"                                        /* CPU detection */
64 #include "os_specific.h"
65
66 #include "vlc_error.h"
67
68 #include "stream_control.h"
69 #include "input_ext-intf.h"
70
71 #include "vlc_playlist.h"
72 #include "vlc_interface.h"
73
74 #include "audio_output.h"
75
76 #include "vlc_video.h"
77 #include "video_output.h"
78
79 #include "libvlc.h"
80
81 /*****************************************************************************
82  * The evil global variable. We handle it with care, don't worry.
83  *****************************************************************************/
84 static libvlc_t   libvlc;
85 static libvlc_t * p_libvlc;
86 static vlc_t *    p_static_vlc;
87
88 /*****************************************************************************
89  * Local prototypes
90  *****************************************************************************/
91 static void SetLanguage   ( char const * );
92 static int  GetFilenames  ( vlc_t *, int, char *[] );
93 static void Usage         ( vlc_t *, char const *psz_module_name );
94 static void ListModules   ( vlc_t * );
95 static void Version       ( void );
96
97 #ifdef WIN32
98 static void ShowConsole   ( void );
99 #endif
100 static int  ConsoleWidth  ( void );
101
102 static int  VerboseCallback( vlc_object_t *, char const *,
103                              vlc_value_t, vlc_value_t, void * );
104
105 /*****************************************************************************
106  * vlc_current_object: return the current object.
107  *****************************************************************************
108  * If i_object is non-zero, return the corresponding object. Otherwise,
109  * return the statically allocated p_vlc object.
110  *****************************************************************************/
111 vlc_t * vlc_current_object( int i_object )
112 {
113     if( i_object )
114     {
115          return vlc_object_get( p_libvlc, i_object );
116     }
117
118     return p_static_vlc;
119 }
120
121 /*****************************************************************************
122  * VLC_Version: return the libvlc version.
123  *****************************************************************************
124  * This function returns full version string (numeric version and codename).
125  *****************************************************************************/
126 char const * VLC_Version( void )
127 {
128     return VERSION_MESSAGE;
129 }
130
131 /*****************************************************************************
132  * VLC_Error: strerror() equivalent
133  *****************************************************************************
134  * This function returns full version string (numeric version and codename).
135  *****************************************************************************/
136 char const * VLC_Error( int i_err )
137 {
138     return vlc_error( i_err );
139 }
140
141 /*****************************************************************************
142  * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
143  *****************************************************************************
144  * This function allocates a vlc_t structure and returns a negative value
145  * in case of failure. Also, the thread system is initialized.
146  *****************************************************************************/
147 int VLC_Create( void )
148 {
149     int i_ret;
150     vlc_t * p_vlc = NULL;
151     vlc_value_t lockval;
152
153     /* &libvlc never changes, so we can safely call this multiple times. */
154     p_libvlc = &libvlc;
155
156     /* vlc_threads_init *must* be the first internal call! No other call is
157      * allowed before the thread system has been initialized. */
158     i_ret = vlc_threads_init( p_libvlc );
159     if( i_ret < 0 )
160     {
161         return i_ret;
162     }
163
164     /* Now that the thread system is initialized, we don't have much, but
165      * at least we have var_Create */
166     var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
167     var_Get( p_libvlc, "libvlc", &lockval );
168     vlc_mutex_lock( lockval.p_address );
169     if( !libvlc.b_ready )
170     {
171         char *psz_env;
172
173         /* Guess what CPU we have */
174         libvlc.i_cpu = CPUCapabilities();
175
176         /* Find verbosity from VLC_VERBOSE environment variable */
177         psz_env = getenv( "VLC_VERBOSE" );
178         libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
179
180 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
181         libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
182 #else
183         libvlc.b_color = VLC_FALSE;
184 #endif
185
186         /* Initialize message queue */
187         msg_Create( p_libvlc );
188
189         /* Announce who we are */
190         msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
191         msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
192
193         /* The module bank will be initialized later */
194         libvlc.p_module_bank = NULL;
195
196         libvlc.b_ready = VLC_TRUE;
197     }
198     vlc_mutex_unlock( lockval.p_address );
199     var_Destroy( p_libvlc, "libvlc" );
200
201     /* Allocate a vlc object */
202     p_vlc = vlc_object_create( p_libvlc, VLC_OBJECT_VLC );
203     if( p_vlc == NULL )
204     {
205         return VLC_EGENERIC;
206     }
207     vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
208
209     p_vlc->psz_object_name = "root";
210
211     /* Initialize mutexes */
212     vlc_mutex_init( p_vlc, &p_vlc->config_lock );
213 #ifdef SYS_DARWIN
214     vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
215 #endif
216
217     /* Store our newly allocated structure in the global list */
218     vlc_object_attach( p_vlc, p_libvlc );
219
220     /* Store data for the non-reentrant API */
221     p_static_vlc = p_vlc;
222
223     return p_vlc->i_object_id;
224 }
225
226 /*****************************************************************************
227  * VLC_Init: initialize a vlc_t structure.
228  *****************************************************************************
229  * This function initializes a previously allocated vlc_t structure:
230  *  - CPU detection
231  *  - gettext initialization
232  *  - message queue, module bank and playlist initialization
233  *  - configuration and commandline parsing
234  *****************************************************************************/
235 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
236 {
237     char         p_capabilities[200];
238     char *       p_tmp;
239     char *       psz_modules;
240     char *       psz_parser;
241     char *       psz_language;
242     vlc_bool_t   b_exit = VLC_FALSE;
243     vlc_t *      p_vlc = vlc_current_object( i_object );
244     module_t    *p_help_module;
245     playlist_t  *p_playlist;
246     vlc_value_t  lockval;
247
248     if( !p_vlc )
249     {
250         return VLC_ENOOBJ;
251     }
252
253     /*
254      * System specific initialization code
255      */
256     system_Init( p_vlc, &i_argc, ppsz_argv );
257
258     /* Get the executable name (similar to the basename command) */
259     if( i_argc > 0 )
260     {
261         p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
262         while( *p_tmp )
263         {
264             if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
265             else ++p_tmp;
266         }
267     }
268     else
269     {
270         p_vlc->psz_object_name = "vlc";
271     }
272
273     /*
274      * Support for gettext
275      */
276     SetLanguage( "" );
277
278     /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
279     msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
280
281     /* Initialize the module bank and load the configuration of the
282      * main module. We need to do this at this stage to be able to display
283      * a short help if required by the user. (short help == main module
284      * options) */
285     var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
286     var_Get( p_libvlc, "libvlc", &lockval );
287     vlc_mutex_lock( lockval.p_address );
288     if( libvlc.p_module_bank == NULL )
289     {
290         module_InitBank( p_vlc );
291         module_LoadMain( p_vlc );
292     }
293     vlc_mutex_unlock( lockval.p_address );
294     var_Destroy( p_libvlc, "libvlc" );
295
296     /* Hack: insert the help module here */
297     p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
298     if( p_help_module == NULL )
299     {
300         /*module_EndBank( p_vlc );*/
301         if( i_object ) vlc_object_release( p_vlc );
302         return VLC_EGENERIC;
303     }
304     p_help_module->psz_object_name = "help";
305     config_Duplicate( p_help_module, p_help_config );
306     vlc_object_attach( p_help_module, libvlc.p_module_bank );
307     /* End hack */
308
309     if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
310     {
311         vlc_object_detach( p_help_module );
312         config_Free( p_help_module );
313         vlc_object_destroy( p_help_module );
314         /*module_EndBank( p_vlc );*/
315         if( i_object ) vlc_object_release( p_vlc );
316         return VLC_EGENERIC;
317     }
318
319     /* Check for short help option */
320     if( config_GetInt( p_vlc, "help" ) )
321     {
322         fprintf( stdout, _("Usage: %s [options] [items]...\n"),
323                          p_vlc->psz_object_name );
324         Usage( p_vlc, "main" );
325         Usage( p_vlc, "help" );
326         b_exit = VLC_TRUE;
327     }
328     /* Check for version option */
329     else if( config_GetInt( p_vlc, "version" ) )
330     {
331         Version();
332         b_exit = VLC_TRUE;
333     }
334
335     /* Set the config file stuff */
336     p_vlc->psz_homedir = config_GetHomeDir();
337     p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
338
339     /* Hack: remove the help module here */
340     vlc_object_detach( p_help_module );
341     /* End hack */
342
343     if( b_exit )
344     {
345         config_Free( p_help_module );
346         vlc_object_destroy( p_help_module );
347         /*module_EndBank( p_vlc );*/
348         if( i_object ) vlc_object_release( p_vlc );
349         return VLC_EEXIT;
350     }
351
352     /* Check for translation config option */
353 #if defined( ENABLE_NLS ) \
354      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
355
356     /* This ain't really nice to have to reload the config here but it seems
357      * the only way to do it. */
358     config_LoadConfigFile( p_vlc, "main" );
359     config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
360
361     /* Check if the user specified a custom language */
362     psz_language = config_GetPsz( p_vlc, "language" );
363     if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
364     {
365         /* Reset the default domain */
366         SetLanguage( psz_language );
367
368         /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
369         msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
370
371         textdomain( PACKAGE );
372
373 #if defined( SYS_BEOS ) || defined ( SYS_DARWIN ) || \
374     ( defined( WIN32 ) && !defined( HAVE_INCLUDED_GETTEXT ) )
375         /* BeOS only support UTF8 strings */
376         /* Mac OS X prefers UTF8 */
377         bind_textdomain_codeset( PACKAGE, "UTF-8" );
378 #endif
379
380         module_EndBank( p_vlc );
381         module_InitBank( p_vlc );
382         module_LoadMain( p_vlc );
383         config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
384     }
385     if( psz_language ) free( psz_language );
386 #endif
387
388     /*
389      * Load the builtins and plugins into the module_bank.
390      * We have to do it before config_Load*() because this also gets the
391      * list of configuration options exported by each module and loads their
392      * default values.
393      */
394     module_LoadBuiltins( p_vlc );
395     module_LoadPlugins( p_vlc );
396     if( p_vlc->b_die )
397     {
398         b_exit = VLC_TRUE;
399     }
400
401     msg_Dbg( p_vlc, "module bank initialized, found %i modules",
402                     libvlc.p_module_bank->i_children );
403
404     /* Hack: insert the help module here */
405     vlc_object_attach( p_help_module, libvlc.p_module_bank );
406     /* End hack */
407
408     /* Check for help on modules */
409     if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
410     {
411         Usage( p_vlc, p_tmp );
412         free( p_tmp );
413         b_exit = VLC_TRUE;
414     }
415     /* Check for long help option */
416     else if( config_GetInt( p_vlc, "longhelp" ) )
417     {
418         Usage( p_vlc, NULL );
419         b_exit = VLC_TRUE;
420     }
421     /* Check for module list option */
422     else if( config_GetInt( p_vlc, "list" ) )
423     {
424         ListModules( p_vlc );
425         b_exit = VLC_TRUE;
426     }
427
428     /* Check for config file options */
429     if( config_GetInt( p_vlc, "reset-config" ) )
430     {
431         vlc_object_detach( p_help_module );
432         config_ResetAll( p_vlc );
433         config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
434         config_SaveConfigFile( p_vlc, NULL );
435         vlc_object_attach( p_help_module, libvlc.p_module_bank );
436     }
437     if( config_GetInt( p_vlc, "save-config" ) )
438     {
439         vlc_object_detach( p_help_module );
440         config_LoadConfigFile( p_vlc, NULL );
441         config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
442         config_SaveConfigFile( p_vlc, NULL );
443         vlc_object_attach( p_help_module, libvlc.p_module_bank );
444     }
445
446     /* Hack: remove the help module here */
447     vlc_object_detach( p_help_module );
448     /* End hack */
449
450     if( b_exit )
451     {
452         config_Free( p_help_module );
453         vlc_object_destroy( p_help_module );
454         /*module_EndBank( p_vlc );*/
455         if( i_object ) vlc_object_release( p_vlc );
456         return VLC_EEXIT;
457     }
458
459     /*
460      * Override default configuration with config file settings
461      */
462     config_LoadConfigFile( p_vlc, NULL );
463
464     /* Hack: insert the help module here */
465     vlc_object_attach( p_help_module, libvlc.p_module_bank );
466     /* End hack */
467
468     /*
469      * Override configuration with command line settings
470      */
471     if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
472     {
473 #ifdef WIN32
474         ShowConsole();
475         /* Pause the console because it's destroyed when we exit */
476         fprintf( stderr, "The command line options couldn't be loaded, check "
477                  "that they are valid.\nPress the RETURN key to continue..." );
478         getchar();
479 #endif
480         vlc_object_detach( p_help_module );
481         config_Free( p_help_module );
482         vlc_object_destroy( p_help_module );
483         /*module_EndBank( p_vlc );*/
484         if( i_object ) vlc_object_release( p_vlc );
485         return VLC_EGENERIC;
486     }
487
488     /* Hack: remove the help module here */
489     vlc_object_detach( p_help_module );
490     config_Free( p_help_module );
491     vlc_object_destroy( p_help_module );
492     /* End hack */
493
494     /*
495      * System specific configuration
496      */
497     system_Configure( p_vlc, &i_argc, ppsz_argv );
498
499     /*
500      * Message queue options
501      */
502
503     var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
504     if( config_GetInt( p_vlc, "quiet" ) )
505     {
506         vlc_value_t val;
507         val.i_int = -1;
508         var_Set( p_vlc, "verbose", val );
509     }
510     var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
511     var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
512     
513     libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
514
515     /*
516      * Output messages that may still be in the queue
517      */
518     msg_Flush( p_vlc );
519
520     /* p_vlc initialization. FIXME ? */
521
522 #if defined( __i386__ )
523     if( !config_GetInt( p_vlc, "mmx" ) )
524         libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
525     if( !config_GetInt( p_vlc, "3dn" ) )
526         libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
527     if( !config_GetInt( p_vlc, "mmxext" ) )
528         libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
529     if( !config_GetInt( p_vlc, "sse" ) )
530         libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
531 #endif
532 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
533     if( !config_GetInt( p_vlc, "altivec" ) )
534         libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
535 #endif
536
537 #define PRINT_CAPABILITY( capability, string )                              \
538     if( libvlc.i_cpu & capability )                                         \
539     {                                                                       \
540         strncat( p_capabilities, string " ",                                \
541                  sizeof(p_capabilities) - strlen(p_capabilities) );         \
542         p_capabilities[sizeof(p_capabilities) - 1] = '\0';                  \
543     }
544
545     p_capabilities[0] = '\0';
546     PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
547     PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
548     PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
549     PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
550     PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
551     PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
552     PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
553     PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
554     PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
555     msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
556
557     /*
558      * Choose the best memcpy module
559      */
560     p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy" );
561
562     if( p_vlc->pf_memcpy == NULL )
563     {
564         p_vlc->pf_memcpy = memcpy;
565     }
566
567     if( p_vlc->pf_memset == NULL )
568     {
569         p_vlc->pf_memset = memset;
570     }
571
572     /*
573      * Initialize hotkey handling
574      */
575     var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
576     p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
577     /* Do a copy (we don't need to modify the strings) */
578     memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
579
580     /*
581      * Initialize playlist and get commandline files
582      */
583     p_playlist = playlist_Create( p_vlc );
584     if( !p_playlist )
585     {
586         msg_Err( p_vlc, "playlist initialization failed" );
587         if( p_vlc->p_memcpy_module != NULL )
588         {
589             module_Unneed( p_vlc, p_vlc->p_memcpy_module );
590         }
591         /*module_EndBank( p_vlc );*/
592         if( i_object ) vlc_object_release( p_vlc );
593         return VLC_EGENERIC;
594     }
595
596     /*
597      * Load background interfaces
598      */
599     psz_modules = config_GetPsz( p_vlc, "extraintf" );
600     psz_parser = psz_modules;
601     while ( psz_parser && *psz_parser )
602     {
603         char *psz_module, *psz_temp;
604         psz_module = psz_parser;
605         psz_parser = strchr( psz_module, ',' );
606         if ( psz_parser )
607         {
608             *psz_parser = '\0';
609             psz_parser++;
610         }
611         psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
612         if( psz_temp )
613         {
614             sprintf( psz_temp, "%s,none", psz_module );
615             VLC_AddIntf( 0, psz_temp, VLC_FALSE );
616             free( psz_temp );
617         }
618     }
619     if ( psz_modules )
620     {
621         free( psz_modules );
622     }
623
624     /*
625      * Allways load the hotkeys interface if it exists
626      */
627     VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE );
628
629     /*
630      * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
631      */
632     var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
633     var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
634     var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
635     var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
636     var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
637     var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
638     var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
639     var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
640     var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
641     var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
642     var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
643     var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
644
645     /*
646      * Get input filenames given as commandline arguments
647      */
648     GetFilenames( p_vlc, i_argc, ppsz_argv );
649
650     if( i_object ) vlc_object_release( p_vlc );
651     return VLC_SUCCESS;
652 }
653
654 /*****************************************************************************
655  * VLC_AddIntf: add an interface
656  *****************************************************************************
657  * This function opens an interface plugin and runs it. If b_block is set
658  * to 0, VLC_AddIntf will return immediately and let the interface run in a
659  * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
660  * user requests to quit.
661  *****************************************************************************/
662 int VLC_AddIntf( int i_object, char const *psz_module, vlc_bool_t b_block )
663 {
664     int i_err;
665     intf_thread_t *p_intf;
666     vlc_t *p_vlc = vlc_current_object( i_object );
667
668     if( !p_vlc )
669     {
670         return VLC_ENOOBJ;
671     }
672
673     /* Try to create the interface */
674     p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
675
676     if( p_intf == NULL )
677     {
678         msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
679         if( i_object ) vlc_object_release( p_vlc );
680         return VLC_EGENERIC;
681     }
682
683     /* Try to run the interface */
684     p_intf->b_block = b_block;
685     i_err = intf_RunThread( p_intf );
686     if( i_err )
687     {
688         vlc_object_detach( p_intf );
689         intf_Destroy( p_intf );
690         if( i_object ) vlc_object_release( p_vlc );
691         return i_err;
692     }
693
694     if( i_object ) vlc_object_release( p_vlc );
695     return VLC_SUCCESS;
696 }
697
698 /*****************************************************************************
699  * VLC_Destroy: stop playing and destroy everything.
700  *****************************************************************************
701  * This function requests the running threads to finish, waits for their
702  * termination, and destroys their structure.
703  *****************************************************************************/
704 int VLC_Destroy( int i_object )
705 {
706     vlc_t *p_vlc = vlc_current_object( i_object );
707
708     if( !p_vlc )
709     {
710         return VLC_ENOOBJ;
711     }
712
713     /*
714      * Free allocated memory
715      */
716     if( p_vlc->p_memcpy_module )
717     {
718         module_Unneed( p_vlc, p_vlc->p_memcpy_module );
719         p_vlc->p_memcpy_module = NULL;
720     }
721
722     if( p_vlc->psz_homedir )
723     {
724         free( p_vlc->psz_homedir );
725         p_vlc->psz_homedir = NULL;
726     }
727
728     if( p_vlc->psz_configfile )
729     {
730         free( p_vlc->psz_configfile );
731         p_vlc->psz_configfile = NULL;
732     }
733
734     if( p_vlc->p_hotkeys )
735     {
736         free( p_vlc->p_hotkeys );
737         p_vlc->p_hotkeys = NULL;
738     }
739
740     /*
741      * XXX: Free module bank !
742      */
743     /*module_EndBank( p_vlc );*/
744
745     /*
746      * System specific cleaning code
747      */
748     system_End( p_vlc );
749
750     /* Destroy mutexes */
751     vlc_mutex_destroy( &p_vlc->config_lock );
752 #ifdef SYS_DARWIN
753     vlc_mutex_destroy( &p_vlc->quicktime_lock );
754 #endif
755
756     vlc_object_detach( p_vlc );
757
758     /* Release object before destroying it */
759     if( i_object ) vlc_object_release( p_vlc );
760
761     vlc_object_destroy( p_vlc );
762
763     /* Stop thread system: last one out please shut the door! */
764     vlc_threads_end( p_libvlc );
765
766     return VLC_SUCCESS;
767 }
768
769 /*****************************************************************************
770  * VLC_Die: ask vlc to die.
771  *****************************************************************************
772  * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
773  * task. It is your duty to call VLC_End and VLC_Destroy afterwards.
774  *****************************************************************************/
775 int VLC_Die( int i_object )
776 {
777     vlc_t *p_vlc = vlc_current_object( i_object );
778
779     if( !p_vlc )
780     {
781         return VLC_ENOOBJ;
782     }
783
784     p_vlc->b_die = VLC_TRUE;
785
786     if( i_object ) vlc_object_release( p_vlc );
787     return VLC_SUCCESS;
788 }
789
790 /*****************************************************************************
791  * VLC_AddTarget: adds a target for playing.
792  *****************************************************************************
793  * This function adds psz_target to the current playlist. If a playlist does
794  * not exist, it will create one.
795  *****************************************************************************/
796 int VLC_AddTarget( int i_object, char const *psz_target,
797                    char const **ppsz_options, int i_options,
798                    int i_mode, int i_pos )
799 {
800     int i;
801     int i_err;
802     playlist_t *p_playlist;
803     vlc_t *p_vlc = vlc_current_object( i_object );
804
805     if( !p_vlc )
806     {
807         return VLC_ENOOBJ;
808     }
809
810     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
811
812     if( p_playlist == NULL )
813     {
814         msg_Dbg( p_vlc, "no playlist present, creating one" );
815         p_playlist = playlist_Create( p_vlc );
816
817         if( p_playlist == NULL )
818         {
819             if( i_object ) vlc_object_release( p_vlc );
820             return VLC_EGENERIC;
821         }
822
823         vlc_object_yield( p_playlist );
824     }
825
826     i_err = playlist_Add( p_playlist, psz_target, psz_target,
827                           i_mode, i_pos );
828
829     for( i = 0 ; i< i_options ; i++ )
830     {
831         playlist_AddOption( p_playlist, i_err , ppsz_options[i] );
832     }
833
834     vlc_object_release( p_playlist );
835
836     if( i_object ) vlc_object_release( p_vlc );
837     return i_err;
838 }
839
840 /*****************************************************************************
841  * VLC_Set: set a vlc variable
842  *****************************************************************************
843  *
844  *****************************************************************************/
845 int VLC_Set( int i_object, char const *psz_var, vlc_value_t value )
846 {
847     vlc_t *p_vlc = vlc_current_object( i_object );
848     int i_ret;
849
850     if( !p_vlc )
851     {
852         return VLC_ENOOBJ;
853     }
854
855     /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
856      * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
857     if( !strncmp( psz_var, "conf::", 6 ) )
858     {
859         module_config_t *p_item;
860         char const *psz_newvar = psz_var + 6;
861
862         p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
863
864         if( p_item )
865         {
866             switch( p_item->i_type )
867             {
868                 case CONFIG_ITEM_BOOL:
869                     config_PutInt( p_vlc, psz_newvar, value.b_bool );
870                     break;
871                 case CONFIG_ITEM_INTEGER:
872                     config_PutInt( p_vlc, psz_newvar, value.i_int );
873                     break;
874                 case CONFIG_ITEM_FLOAT:
875                     config_PutFloat( p_vlc, psz_newvar, value.f_float );
876                     break;
877                 default:
878                     config_PutPsz( p_vlc, psz_newvar, value.psz_string );
879                     break;
880             }
881             if( i_object ) vlc_object_release( p_vlc );
882             return VLC_SUCCESS;
883         }
884     }
885
886     i_ret = var_Set( p_vlc, psz_var, value );
887
888     if( i_object ) vlc_object_release( p_vlc );
889     return i_ret;
890 }
891
892 /*****************************************************************************
893  * VLC_Get: get a vlc variable
894  *****************************************************************************
895  *
896  *****************************************************************************/
897 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
898 {
899     vlc_t *p_vlc = vlc_current_object( i_object );
900     int i_ret;
901
902     if( !p_vlc )
903     {
904         return VLC_ENOOBJ;
905     }
906
907     i_ret = var_Get( p_vlc, psz_var, p_value );
908
909     if( i_object ) vlc_object_release( p_vlc );
910     return i_ret;
911 }
912
913 /* FIXME: temporary hacks */
914
915
916 /*****************************************************************************
917  * VLC_IsPlaying: Query for Playlist Status
918  *****************************************************************************/
919 vlc_bool_t VLC_IsPlaying( int i_object )
920 {
921
922     playlist_t * p_playlist;
923     vlc_bool_t   playing;
924
925     vlc_t *p_vlc = vlc_current_object( i_object );
926
927     /* Check that the handle is valid */
928     if( !p_vlc )
929     {
930         return VLC_ENOOBJ;
931     }
932
933     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
934
935     if( !p_playlist )
936     {
937         if( i_object ) vlc_object_release( p_vlc );
938         return VLC_ENOOBJ;
939     }
940
941     playing = playlist_IsPlaying( p_playlist );
942
943     vlc_object_release( p_playlist );
944
945     if( i_object ) vlc_object_release( p_vlc );
946
947     return playing;
948
949 }
950
951
952 /*****************************************************************************
953  * VLC_ClearPlaylist: Query for Playlist Status
954  *
955  * return: 0
956  *****************************************************************************/
957 int VLC_ClearPlaylist( int i_object )
958 {
959
960     playlist_t * p_playlist;
961     vlc_t *p_vlc = vlc_current_object( i_object );
962
963     /* Check that the handle is valid */
964     if( !p_vlc )
965     {
966         return VLC_ENOOBJ;
967     }
968
969     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
970
971     if( !p_playlist )
972     {
973         if( i_object ) vlc_object_release( p_vlc );
974         return VLC_ENOOBJ;
975     }
976
977     playlist_Clear(p_playlist);
978
979     vlc_object_release( p_playlist );
980
981     if( i_object ) vlc_object_release( p_vlc );
982     return 0;
983 }
984
985
986 /*****************************************************************************
987  * VLC_Play: play
988  *****************************************************************************/
989 int VLC_Play( int i_object )
990 {
991     playlist_t * p_playlist;
992     vlc_t *p_vlc = vlc_current_object( i_object );
993
994     /* Check that the handle is valid */
995     if( !p_vlc )
996     {
997         return VLC_ENOOBJ;
998     }
999
1000     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1001
1002     if( !p_playlist )
1003     {
1004         if( i_object ) vlc_object_release( p_vlc );
1005         return VLC_ENOOBJ;
1006     }
1007
1008     vlc_mutex_lock( &p_playlist->object_lock );
1009     if( p_playlist->i_size )
1010     {
1011         vlc_mutex_unlock( &p_playlist->object_lock );
1012         playlist_Play( p_playlist );
1013     }
1014     else
1015     {
1016         vlc_mutex_unlock( &p_playlist->object_lock );
1017     }
1018
1019     vlc_object_release( p_playlist );
1020
1021     if( i_object ) vlc_object_release( p_vlc );
1022     return VLC_SUCCESS;
1023 }
1024
1025 /*****************************************************************************
1026  * VLC_Stop: stop
1027  *****************************************************************************/
1028 int VLC_Stop( int i_object )
1029 {
1030     intf_thread_t *   p_intf;
1031     playlist_t    *   p_playlist;
1032     vout_thread_t *   p_vout;
1033     aout_instance_t * p_aout;
1034     vlc_t *p_vlc = vlc_current_object( i_object );
1035
1036     /* Check that the handle is valid */
1037     if( !p_vlc )
1038     {
1039         return VLC_ENOOBJ;
1040     }
1041
1042     /*
1043      * Ask the interfaces to stop and destroy them
1044      */
1045     msg_Dbg( p_vlc, "removing all interfaces" );
1046
1047     while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
1048     {
1049         intf_StopThread( p_intf );
1050         vlc_object_detach( p_intf );
1051         vlc_object_release( p_intf );
1052         intf_Destroy( p_intf );
1053     }
1054
1055     /*
1056      * Free playlists
1057      */
1058     
1059     msg_Dbg( p_vlc, "removing all playlists" );
1060     while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
1061                                           FIND_CHILD )) )
1062     {
1063         vlc_object_detach( p_playlist );
1064         vlc_object_release( p_playlist );
1065         playlist_Destroy( p_playlist );
1066     }
1067     
1068     /*
1069      * Free video outputs
1070      */
1071     msg_Dbg( p_vlc, "removing all video outputs" );
1072     while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
1073     {
1074         vlc_object_detach( p_vout );
1075         vlc_object_release( p_vout );
1076         vout_Destroy( p_vout );
1077     }
1078
1079     /*
1080      * Free audio outputs
1081      */
1082     msg_Dbg( p_vlc, "removing all audio outputs" );
1083     while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
1084     {
1085         vlc_object_detach( (vlc_object_t *)p_aout );
1086         vlc_object_release( (vlc_object_t *)p_aout );
1087         aout_Delete( p_aout );
1088     }
1089
1090     if( i_object ) vlc_object_release( p_vlc );
1091     return VLC_SUCCESS;
1092 }
1093
1094 /*****************************************************************************
1095  * VLC_Pause: toggle pause
1096  *****************************************************************************/
1097 int VLC_Pause( int i_object )
1098 {
1099     input_thread_t *p_input;
1100     vlc_t *p_vlc = vlc_current_object( i_object );
1101
1102     if( !p_vlc )
1103     {
1104         return VLC_ENOOBJ;
1105     }
1106
1107     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1108
1109     if( !p_input )
1110     {
1111         if( i_object ) vlc_object_release( p_vlc );
1112         return VLC_ENOOBJ;
1113     }
1114
1115     input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1116     vlc_object_release( p_input );
1117
1118     if( i_object ) vlc_object_release( p_vlc );
1119     return VLC_SUCCESS;
1120 }
1121
1122 /*****************************************************************************
1123  * VLC_FullScreen: toggle fullscreen mode
1124  *****************************************************************************/
1125 int VLC_FullScreen( int i_object )
1126 {
1127     vout_thread_t *p_vout;
1128     vlc_t *p_vlc = vlc_current_object( i_object );
1129
1130     if( !p_vlc )
1131     {
1132         return VLC_ENOOBJ;
1133     }
1134
1135     p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1136
1137     if( !p_vout )
1138     {
1139         if( i_object ) vlc_object_release( p_vlc );
1140         return VLC_ENOOBJ;
1141     }
1142
1143     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1144     vlc_object_release( p_vout );
1145
1146     if( i_object ) vlc_object_release( p_vlc );
1147     return VLC_SUCCESS;
1148 }
1149
1150 /* following functions are local */
1151
1152 /*****************************************************************************
1153  * SetLanguage: set the interface language.
1154  *****************************************************************************
1155  * We set the LC_MESSAGES locale category for interface messages and buttons,
1156  * as well as the LC_CTYPE category for string sorting and possible wide
1157  * character support.
1158  *****************************************************************************/
1159 static void SetLanguage ( char const *psz_lang )
1160 {
1161 #if defined( ENABLE_NLS ) \
1162      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1163
1164     char *          psz_path;
1165 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1166     char            psz_tmp[1024];
1167 #endif
1168
1169     if( psz_lang && !*psz_lang )
1170     {
1171 #   if defined( HAVE_LC_MESSAGES )
1172         setlocale( LC_MESSAGES, psz_lang );
1173 #   endif
1174         setlocale( LC_CTYPE, psz_lang );
1175     }
1176     else if( psz_lang )
1177     {
1178 #ifdef SYS_DARWIN
1179         /* I need that under Darwin, please check it doesn't disturb
1180          * other platforms. --Meuuh */
1181         setenv( "LANG", psz_lang, 1 );
1182
1183 #elif defined( SYS_BEOS ) || defined( WIN32 )
1184         /* We set LC_ALL manually because it is the only way to set
1185          * the language at runtime under eg. Windows. Beware that this
1186          * makes the environment unconsistent when libvlc is unloaded and
1187          * should probably be moved to a safer place like vlc.c. */
1188         static char psz_lcall[20];
1189         snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1190         psz_lcall[19] = '\0';
1191         putenv( psz_lcall );
1192 #endif
1193
1194         setlocale( LC_ALL, psz_lang );
1195     }
1196
1197     /* Specify where to find the locales for current domain */
1198 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1199     psz_path = LOCALEDIR;
1200 #else
1201     snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1202               "locale" );
1203     psz_path = psz_tmp;
1204 #endif
1205     if( !bindtextdomain( PACKAGE, psz_path ) )
1206     {
1207         fprintf( stderr, "warning: no domain %s in directory %s\n",
1208                  PACKAGE, psz_path );
1209     }
1210
1211     /* Set the default domain */
1212     textdomain( PACKAGE );
1213
1214 #if defined( SYS_BEOS ) || defined ( SYS_DARWIN ) || \
1215     ( defined( WIN32 ) && !defined( HAVE_INCLUDED_GETTEXT ) )
1216     /* BeOS only support UTF8 strings */
1217     /* Mac OS X prefers UTF8 */
1218     bind_textdomain_codeset( PACKAGE, "UTF-8" );
1219 #endif
1220
1221 #endif
1222 }
1223
1224 /*****************************************************************************
1225  * GetFilenames: parse command line options which are not flags
1226  *****************************************************************************
1227  * Parse command line for input files as well as their associated options.
1228  * An option always follows its associated input and begins with a ":".
1229  *****************************************************************************/
1230 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1231 {
1232     int i_opt, i_options;
1233
1234     /* We assume that the remaining parameters are filenames
1235      * and their input options */
1236     for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1237     {
1238         i_options = 0;
1239
1240         /* Count the input options */
1241         while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1242         {
1243             i_options++;
1244             i_opt--;
1245         }
1246
1247         /* TODO: write an internal function of this one, to avoid
1248          *       unnecessary lookups. */
1249         VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1250                        (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1251                                         NULL ), i_options,
1252                        PLAYLIST_INSERT | (i_opt == optind ? PLAYLIST_GO : 0),
1253                        0 );
1254     }
1255
1256     return VLC_SUCCESS;
1257 }
1258
1259 /*****************************************************************************
1260  * Usage: print program usage
1261  *****************************************************************************
1262  * Print a short inline help. Message interface is initialized at this stage.
1263  *****************************************************************************/
1264 static void Usage( vlc_t *p_this, char const *psz_module_name )
1265 {
1266 #define FORMAT_STRING "  %s --%s%s%s%s%s%s%s "
1267     /* short option ------'    |     | | | |  | |
1268      * option name ------------'     | | | |  | |
1269      * <bra -------------------------' | | |  | |
1270      * option type or "" --------------' | |  | |
1271      * ket> -----------------------------' |  | |
1272      * padding spaces ---------------------'  | |
1273      * comment -------------------------------' |
1274      * comment suffix --------------------------'
1275      *
1276      * The purpose of having bra and ket is that we might i18n them as well.
1277      */
1278 #define LINE_START 8
1279 #define PADDING_SPACES 25
1280     vlc_list_t *p_list;
1281     module_t *p_parser;
1282     module_config_t *p_item;
1283     char psz_spaces[PADDING_SPACES+LINE_START+1];
1284     char psz_format[sizeof(FORMAT_STRING)];
1285     char psz_buffer[1000];
1286     char psz_short[4];
1287     int i_index;
1288     int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1289
1290     memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1291     psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1292
1293     strcpy( psz_format, FORMAT_STRING );
1294
1295 #ifdef WIN32
1296     ShowConsole();
1297 #endif
1298
1299     /* List all modules */
1300     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1301
1302     /* Enumerate the config for each module */
1303     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1304     {
1305         vlc_bool_t b_help_module;
1306
1307         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1308
1309         if( psz_module_name && strcmp( psz_module_name,
1310                                        p_parser->psz_object_name ) )
1311         {
1312             continue;
1313         }
1314
1315         /* Ignore modules without config options */
1316         if( !p_parser->i_config_items )
1317         {
1318             continue;
1319         }
1320
1321         b_help_module = !strcmp( "help", p_parser->psz_object_name );
1322
1323         /* Print module options */
1324         for( p_item = p_parser->p_config;
1325              p_item->i_type != CONFIG_HINT_END;
1326              p_item++ )
1327         {
1328             char *psz_text;
1329             char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1330             char *psz_suf = "", *psz_prefix = NULL;
1331             int i;
1332             if ( p_item->b_advanced && !config_GetInt( p_this, "advanced" ))
1333             {
1334                 continue;
1335             }
1336             switch( p_item->i_type )
1337             {
1338             case CONFIG_HINT_CATEGORY:
1339             case CONFIG_HINT_USAGE:
1340                 fprintf( stdout, "\n %s\n", p_item->psz_text );
1341                 break;
1342
1343             case CONFIG_ITEM_STRING:
1344             case CONFIG_ITEM_FILE:
1345             case CONFIG_ITEM_DIRECTORY:
1346             case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1347                 if( !p_item->ppsz_list )
1348                 {
1349                     psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1350                     break;
1351                 }
1352                 else
1353                 {
1354                     psz_bra = " {";
1355                     psz_type = psz_buffer;
1356                     psz_type[0] = '\0';
1357                     for( i = 0; p_item->ppsz_list[i]; i++ )
1358                     {
1359                         if( i ) strcat( psz_type, "," );
1360                         strcat( psz_type, p_item->ppsz_list[i] );
1361                     }
1362                     psz_ket = "}";
1363                     break;
1364                 }
1365             case CONFIG_ITEM_INTEGER:
1366             case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1367                 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1368                 break;
1369             case CONFIG_ITEM_FLOAT:
1370                 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1371                 break;
1372             case CONFIG_ITEM_BOOL:
1373                 psz_bra = ""; psz_type = ""; psz_ket = "";
1374                 if( !b_help_module )
1375                 {
1376                     psz_suf = p_item->i_value ? _(" (default enabled)") :
1377                                                 _(" (default disabled)");
1378                 }
1379                 break;
1380             }
1381
1382             if( !psz_type )
1383             {
1384                 continue;
1385             }
1386
1387             /* Add short option if any */
1388             if( p_item->i_short )
1389             {
1390                 sprintf( psz_short, "-%c,", p_item->i_short );
1391             }
1392             else
1393             {
1394                 strcpy( psz_short, "   " );
1395             }
1396
1397             i = PADDING_SPACES - strlen( p_item->psz_name )
1398                  - strlen( psz_bra ) - strlen( psz_type )
1399                  - strlen( psz_ket ) - 1;
1400
1401             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1402             {
1403                 /* If option is of type --foo-bar, we print its counterpart
1404                  * as --no-foo-bar, but if it is of type --foobar (without
1405                  * dashes in the name) we print it as --nofoobar. Both
1406                  * values are of course valid, only the display changes. */
1407                 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1408                                                              : ", --no";
1409                 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1410             }
1411
1412             if( i < 0 )
1413             {
1414                 psz_spaces[0] = '\n';
1415                 i = 0;
1416             }
1417             else
1418             {
1419                 psz_spaces[i] = '\0';
1420             }
1421
1422             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1423             {
1424                 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1425                          psz_prefix, p_item->psz_name, psz_bra, psz_type,
1426                          psz_ket, psz_spaces );
1427             }
1428             else
1429             {
1430                 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1431                          "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1432             }
1433
1434             psz_spaces[i] = ' ';
1435
1436             /* We wrap the rest of the output */
1437             sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1438             psz_text = psz_buffer;
1439             while( *psz_text )
1440             {
1441                 char *psz_parser, *psz_word;
1442                 int i_end = strlen( psz_text );
1443
1444                 /* If the remaining text fits in a line, print it. */
1445                 if( i_end <= i_width )
1446                 {
1447                     fprintf( stdout, "%s\n", psz_text );
1448                     break;
1449                 }
1450
1451                 /* Otherwise, eat as many words as possible */
1452                 psz_parser = psz_text;
1453                 do
1454                 {
1455                     psz_word = psz_parser;
1456                     psz_parser = strchr( psz_word, ' ' );
1457                     /* If no space was found, we reached the end of the text
1458                      * block; otherwise, we skip the space we just found. */
1459                     psz_parser = psz_parser ? psz_parser + 1
1460                                             : psz_text + i_end;
1461
1462                 } while( psz_parser - psz_text <= i_width );
1463
1464                 /* We cut a word in one of these cases:
1465                  *  - it's the only word in the line and it's too long.
1466                  *  - we used less than 80% of the width and the word we are
1467                  *    going to wrap is longer than 40% of the width, and even
1468                  *    if the word would have fit in the next line. */
1469                 if( psz_word == psz_text
1470                      || ( psz_word - psz_text < 80 * i_width / 100
1471                            && psz_parser - psz_word > 40 * i_width / 100 ) )
1472                 {
1473                     char c = psz_text[i_width];
1474                     psz_text[i_width] = '\0';
1475                     fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1476                     psz_text += i_width;
1477                     psz_text[0] = c;
1478                 }
1479                 else
1480                 {
1481                     psz_word[-1] = '\0';
1482                     fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1483                     psz_text = psz_word;
1484                 }
1485             }
1486         }
1487     }
1488
1489     /* Release the module list */
1490     vlc_list_release( p_list );
1491
1492 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1493     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1494     getchar();
1495 #endif
1496 }
1497
1498 /*****************************************************************************
1499  * ListModules: list the available modules with their description
1500  *****************************************************************************
1501  * Print a list of all available modules (builtins and plugins) and a short
1502  * description for each one.
1503  *****************************************************************************/
1504 static void ListModules( vlc_t *p_this )
1505 {
1506     vlc_list_t *p_list;
1507     module_t *p_parser;
1508     char psz_spaces[22];
1509     int i_index;
1510
1511     memset( psz_spaces, ' ', 22 );
1512
1513 #ifdef WIN32
1514     ShowConsole();
1515 #endif
1516
1517     /* Usage */
1518     fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1519                      p_this->p_vlc->psz_object_name );
1520
1521     fprintf( stdout, _("[module]              [description]\n") );
1522
1523     /* List all modules */
1524     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1525
1526     /* Enumerate each module */
1527     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1528     {
1529         int i;
1530
1531         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1532
1533         /* Nasty hack, but right now I'm too tired to think about a nice
1534          * solution */
1535         i = 22 - strlen( p_parser->psz_object_name ) - 1;
1536         if( i < 0 ) i = 0;
1537         psz_spaces[i] = 0;
1538
1539         fprintf( stdout, "  %s%s %s\n", p_parser->psz_object_name,
1540                          psz_spaces, p_parser->psz_longname );
1541
1542         psz_spaces[i] = ' ';
1543     }
1544
1545     vlc_list_release( p_list );
1546
1547 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1548     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1549     getchar();
1550 #endif
1551 }
1552
1553 /*****************************************************************************
1554  * Version: print complete program version
1555  *****************************************************************************
1556  * Print complete program version and build number.
1557  *****************************************************************************/
1558 static void Version( void )
1559 {
1560 #ifdef WIN32
1561     ShowConsole();
1562 #endif
1563
1564     fprintf( stdout, VERSION_MESSAGE "\n" );
1565     fprintf( stdout,
1566       _("This program comes with NO WARRANTY, to the extent permitted by "
1567         "law.\nYou may redistribute it under the terms of the GNU General "
1568         "Public License;\nsee the file named COPYING for details.\n"
1569         "Written by the VideoLAN team; see the AUTHORS file.\n") );
1570
1571 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1572     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1573     getchar();
1574 #endif
1575 }
1576
1577 /*****************************************************************************
1578  * ShowConsole: On Win32, create an output console for debug messages
1579  *****************************************************************************
1580  * This function is useful only on Win32.
1581  *****************************************************************************/
1582 #ifdef WIN32 /*  */
1583 static void ShowConsole( void )
1584 {
1585 #   ifndef UNDER_CE
1586     AllocConsole();
1587     freopen( "CONOUT$", "w", stdout );
1588     freopen( "CONOUT$", "w", stderr );
1589     freopen( "CONIN$", "r", stdin );
1590 #   endif
1591     return;
1592 }
1593 #endif
1594
1595 /*****************************************************************************
1596  * ConsoleWidth: Return the console width in characters
1597  *****************************************************************************
1598  * We use the stty shell command to get the console width; if this fails or
1599  * if the width is less than 80, we default to 80.
1600  *****************************************************************************/
1601 static int ConsoleWidth( void )
1602 {
1603     int i_width = 80;
1604
1605 #ifndef WIN32
1606     char buf[20], *psz_parser;
1607     FILE *file;
1608     int i_ret;
1609
1610     file = popen( "stty size 2>/dev/null", "r" );
1611     if( file )
1612     {
1613         i_ret = fread( buf, 1, 20, file );
1614         if( i_ret > 0 )
1615         {
1616             buf[19] = '\0';
1617             psz_parser = strchr( buf, ' ' );
1618             if( psz_parser )
1619             {
1620                 i_ret = atoi( psz_parser + 1 );
1621                 if( i_ret >= 80 )
1622                 {
1623                     i_width = i_ret;
1624                 }
1625             }
1626         }
1627
1628         pclose( file );
1629     }
1630 #endif
1631
1632     return i_width;
1633 }
1634
1635 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
1636                      vlc_value_t old_val, vlc_value_t new_val, void *param)
1637 {
1638     vlc_t *p_vlc = (vlc_t *)p_this;
1639
1640     if( new_val.i_int >= -1 )
1641     {
1642         p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
1643     }
1644     return VLC_SUCCESS;
1645 }