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