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