]> git.sesse.net Git - vlc/blob - src/libvlc.c
* modules/misc/dummy/renderer.c:
[vlc] / src / libvlc.c
1 /*****************************************************************************
2  * libvlc.c: main libvlc source
3  *****************************************************************************
4  * Copyright (C) 1998-2002 VideoLAN
5  * $Id: libvlc.c,v 1.105 2003/12/04 16:49:45 sam Exp $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Pretend we are a builtin module
28  *****************************************************************************/
29 #define MODULE_NAME main
30 #define 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_vlc );
289         module_LoadMain( p_vlc );
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_vlc );
380         module_LoadMain( p_vlc );
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_vlc );
393     module_LoadPlugins( p_vlc );
394     if( p_vlc->b_die )
395     {
396         b_exit = VLC_TRUE;
397     }
398
399     msg_Dbg( p_vlc, "module bank initialized, found %i modules",
400                     libvlc.p_module_bank->i_children );
401
402     /* Hack: insert the help module here */
403     vlc_object_attach( p_help_module, libvlc.p_module_bank );
404     /* End hack */
405
406     /* Check for help on modules */
407     if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
408     {
409         Usage( p_vlc, p_tmp );
410         free( p_tmp );
411         b_exit = VLC_TRUE;
412     }
413     /* Check for long help option */
414     else if( config_GetInt( p_vlc, "longhelp" ) )
415     {
416         Usage( p_vlc, NULL );
417         b_exit = VLC_TRUE;
418     }
419     /* Check for module list option */
420     else if( config_GetInt( p_vlc, "list" ) )
421     {
422         ListModules( p_vlc );
423         b_exit = VLC_TRUE;
424     }
425
426     /* Check for config file options */
427     if( config_GetInt( p_vlc, "reset-config" ) )
428     {
429         vlc_object_detach( p_help_module );
430         config_ResetAll( p_vlc );
431         config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
432         config_SaveConfigFile( p_vlc, NULL );
433         vlc_object_attach( p_help_module, libvlc.p_module_bank );
434     }
435     if( config_GetInt( p_vlc, "save-config" ) )
436     {
437         vlc_object_detach( p_help_module );
438         config_LoadConfigFile( p_vlc, NULL );
439         config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
440         config_SaveConfigFile( p_vlc, NULL );
441         vlc_object_attach( p_help_module, libvlc.p_module_bank );
442     }
443
444     /* Hack: remove the help module here */
445     vlc_object_detach( p_help_module );
446     /* End hack */
447
448     if( b_exit )
449     {
450         config_Free( p_help_module );
451         vlc_object_destroy( p_help_module );
452         /*module_EndBank( p_vlc );*/
453         if( i_object ) vlc_object_release( p_vlc );
454         return VLC_EEXIT;
455     }
456
457     /*
458      * Override default configuration with config file settings
459      */
460     config_LoadConfigFile( p_vlc, NULL );
461
462     /* Hack: insert the help module here */
463     vlc_object_attach( p_help_module, libvlc.p_module_bank );
464     /* End hack */
465
466     /*
467      * Override configuration with command line settings
468      */
469     if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
470     {
471 #ifdef WIN32
472         ShowConsole();
473         /* Pause the console because it's destroyed when we exit */
474         fprintf( stderr, "The command line options couldn't be loaded, check "
475                  "that they are valid.\nPress the RETURN key to continue..." );
476         getchar();
477 #endif
478         vlc_object_detach( p_help_module );
479         config_Free( p_help_module );
480         vlc_object_destroy( p_help_module );
481         /*module_EndBank( p_vlc );*/
482         if( i_object ) vlc_object_release( p_vlc );
483         return VLC_EGENERIC;
484     }
485
486     /* Hack: remove the help module here */
487     vlc_object_detach( p_help_module );
488     config_Free( p_help_module );
489     vlc_object_destroy( p_help_module );
490     /* End hack */
491
492     /*
493      * System specific configuration
494      */
495     system_Configure( p_vlc, &i_argc, ppsz_argv );
496
497     /*
498      * Message queue options
499      */
500     if( config_GetInt( p_vlc, "quiet" ) )
501     {
502         libvlc.i_verbose = -1;
503     }
504     else
505     {
506         int i_tmp = config_GetInt( p_vlc, "verbose" );
507         if( i_tmp >= 0 )
508         {
509             libvlc.i_verbose = __MIN( i_tmp, 2 );
510         }
511     }
512     libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
513
514     /*
515      * Output messages that may still be in the queue
516      */
517     msg_Flush( p_vlc );
518
519     /* p_vlc initialization. FIXME ? */
520
521 #if defined( __i386__ )
522     if( !config_GetInt( p_vlc, "mmx" ) )
523         libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
524     if( !config_GetInt( p_vlc, "3dn" ) )
525         libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
526     if( !config_GetInt( p_vlc, "mmxext" ) )
527         libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
528     if( !config_GetInt( p_vlc, "sse" ) )
529         libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
530 #endif
531 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
532     if( !config_GetInt( p_vlc, "altivec" ) )
533         libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
534 #endif
535
536 #define PRINT_CAPABILITY( capability, string )                              \
537     if( libvlc.i_cpu & capability )                                         \
538     {                                                                       \
539         strncat( p_capabilities, string " ",                                \
540                  sizeof(p_capabilities) - strlen(p_capabilities) );         \
541         p_capabilities[sizeof(p_capabilities) - 1] = '\0';                  \
542     }
543
544     p_capabilities[0] = '\0';
545     PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
546     PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
547     PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
548     PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
549     PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
550     PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
551     PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
552     PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
553     PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
554     msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
555
556     /*
557      * Choose the best memcpy module
558      */
559     p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy" );
560
561     if( p_vlc->pf_memcpy == NULL )
562     {
563         p_vlc->pf_memcpy = memcpy;
564     }
565
566     if( p_vlc->pf_memset == NULL )
567     {
568         p_vlc->pf_memset = memset;
569     }
570
571     /*
572      * Initialize hotkey handling
573      */
574     var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
575     p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
576     /* Do a copy (we don't need to modify the strings) */
577     memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
578
579     /*
580      * Initialize playlist and get commandline files
581      */
582     p_playlist = playlist_Create( p_vlc );
583     if( !p_playlist )
584     {
585         msg_Err( p_vlc, "playlist initialization failed" );
586         if( p_vlc->p_memcpy_module != NULL )
587         {
588             module_Unneed( p_vlc, p_vlc->p_memcpy_module );
589         }
590         /*module_EndBank( p_vlc );*/
591         if( i_object ) vlc_object_release( p_vlc );
592         return VLC_EGENERIC;
593     }
594
595     /*
596      * Load background interfaces
597      */
598     psz_modules = config_GetPsz( p_vlc, "extraintf" );
599     psz_parser = psz_modules;
600     while ( psz_parser && *psz_parser )
601     {
602         char *psz_module, *psz_temp;
603         psz_module = psz_parser;
604         psz_parser = strchr( psz_module, ',' );
605         if ( psz_parser )
606         {
607             *psz_parser = '\0';
608             psz_parser++;
609         }
610         psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
611         if( psz_temp )
612         {
613             sprintf( psz_temp, "%s,none", psz_module );
614             VLC_AddIntf( 0, psz_temp, VLC_FALSE );
615             free( psz_temp );
616         }
617     }
618     if ( psz_modules )
619     {
620         free( psz_modules );
621     }
622
623     /*
624      * Allways load the hotkeys interface if it exists
625      */
626     VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE );
627
628     /*
629      * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
630      */
631     var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
632     var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
633     var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
634     var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
635     var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
636     var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
637     var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
638     var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
639     var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
640     var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
641     var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
642     var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
643
644     /*
645      * Get input filenames given as commandline arguments
646      */
647     GetFilenames( p_vlc, i_argc, ppsz_argv );
648
649     if( i_object ) vlc_object_release( p_vlc );
650     return VLC_SUCCESS;
651 }
652
653 /*****************************************************************************
654  * VLC_AddIntf: add an interface
655  *****************************************************************************
656  * This function opens an interface plugin and runs it. If b_block is set
657  * to 0, VLC_AddIntf will return immediately and let the interface run in a
658  * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
659  * user requests to quit.
660  *****************************************************************************/
661 int VLC_AddIntf( int i_object, char const *psz_module, vlc_bool_t b_block )
662 {
663     int i_err;
664     intf_thread_t *p_intf;
665     vlc_t *p_vlc = vlc_current_object( i_object );
666
667     if( !p_vlc )
668     {
669         return VLC_ENOOBJ;
670     }
671
672     /* Try to create the interface */
673     p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
674
675     if( p_intf == NULL )
676     {
677         msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
678         if( i_object ) vlc_object_release( p_vlc );
679         return VLC_EGENERIC;
680     }
681
682     /* Try to run the interface */
683     p_intf->b_block = b_block;
684     i_err = intf_RunThread( p_intf );
685     if( i_err )
686     {
687         vlc_object_detach( p_intf );
688         intf_Destroy( p_intf );
689         if( i_object ) vlc_object_release( p_vlc );
690         return i_err;
691     }
692
693     if( i_object ) vlc_object_release( p_vlc );
694     return VLC_SUCCESS;
695 }
696
697 /*****************************************************************************
698  * VLC_Destroy: stop playing and destroy everything.
699  *****************************************************************************
700  * This function requests the running threads to finish, waits for their
701  * termination, and destroys their structure.
702  *****************************************************************************/
703 int VLC_Destroy( int i_object )
704 {
705     vlc_t *p_vlc = vlc_current_object( i_object );
706
707     if( !p_vlc )
708     {
709         return VLC_ENOOBJ;
710     }
711
712     /*
713      * Free allocated memory
714      */
715     if( p_vlc->p_memcpy_module )
716     {
717         module_Unneed( p_vlc, p_vlc->p_memcpy_module );
718         p_vlc->p_memcpy_module = NULL;
719     }
720
721     if( p_vlc->psz_homedir )
722     {
723         free( p_vlc->psz_homedir );
724         p_vlc->psz_homedir = NULL;
725     }
726
727     if( p_vlc->psz_configfile )
728     {
729         free( p_vlc->psz_configfile );
730         p_vlc->psz_configfile = NULL;
731     }
732
733     if( p_vlc->p_hotkeys )
734     {
735         free( p_vlc->p_hotkeys );
736         p_vlc->p_hotkeys = NULL;
737     }
738
739     /*
740      * XXX: Free module bank !
741      */
742     /*module_EndBank( p_vlc );*/
743
744     /*
745      * System specific cleaning code
746      */
747     system_End( p_vlc );
748
749     /* Destroy mutexes */
750     vlc_mutex_destroy( &p_vlc->config_lock );
751 #ifdef SYS_DARWIN
752     vlc_mutex_destroy( &p_vlc->quicktime_lock );
753 #endif
754
755     vlc_object_detach( p_vlc );
756
757     /* Release object before destroying it */
758     if( i_object ) vlc_object_release( p_vlc );
759
760     vlc_object_destroy( p_vlc );
761
762     /* Stop thread system: last one out please shut the door! */
763     vlc_threads_end( p_libvlc );
764
765     return VLC_SUCCESS;
766 }
767
768 /*****************************************************************************
769  * VLC_Die: ask vlc to die.
770  *****************************************************************************
771  * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
772  * task. It is your duty to call VLC_End and VLC_Destroy afterwards.
773  *****************************************************************************/
774 int VLC_Die( int i_object )
775 {
776     vlc_t *p_vlc = vlc_current_object( i_object );
777
778     if( !p_vlc )
779     {
780         return VLC_ENOOBJ;
781     }
782
783     p_vlc->b_die = VLC_TRUE;
784
785     if( i_object ) vlc_object_release( p_vlc );
786     return VLC_SUCCESS;
787 }
788
789 /*****************************************************************************
790  * VLC_AddTarget: adds a target for playing.
791  *****************************************************************************
792  * This function adds psz_target to the current playlist. If a playlist does
793  * not exist, it will create one.
794  *****************************************************************************/
795 int VLC_AddTarget( int i_object, char const *psz_target,
796                    char const **ppsz_options, int i_options,
797                    int i_mode, int i_pos )
798 {
799     int i_err;
800     playlist_t *p_playlist;
801     vlc_t *p_vlc = vlc_current_object( i_object );
802
803     if( !p_vlc )
804     {
805         return VLC_ENOOBJ;
806     }
807
808     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
809
810     if( p_playlist == NULL )
811     {
812         msg_Dbg( p_vlc, "no playlist present, creating one" );
813         p_playlist = playlist_Create( p_vlc );
814
815         if( p_playlist == NULL )
816         {
817             if( i_object ) vlc_object_release( p_vlc );
818             return VLC_EGENERIC;
819         }
820
821         vlc_object_yield( p_playlist );
822     }
823
824     i_err = playlist_Add( p_playlist, psz_target, ppsz_options, i_options,
825                           i_mode, i_pos );
826
827     vlc_object_release( p_playlist );
828
829     if( i_object ) vlc_object_release( p_vlc );
830     return i_err;
831 }
832
833 /*****************************************************************************
834  * VLC_Set: set a vlc variable
835  *****************************************************************************
836  *
837  *****************************************************************************/
838 int VLC_Set( int i_object, char const *psz_var, vlc_value_t value )
839 {
840     vlc_t *p_vlc = vlc_current_object( i_object );
841     int i_ret;
842
843     if( !p_vlc )
844     {
845         return VLC_ENOOBJ;
846     }
847
848     /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
849      * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
850     if( !strncmp( psz_var, "conf::", 6 ) )
851     {
852         module_config_t *p_item;
853         char const *psz_newvar = psz_var + 6;
854
855         p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
856
857         if( p_item )
858         {
859             switch( p_item->i_type )
860             {
861                 case CONFIG_ITEM_BOOL:
862                     config_PutInt( p_vlc, psz_newvar, value.b_bool );
863                     break;
864                 case CONFIG_ITEM_INTEGER:
865                     config_PutInt( p_vlc, psz_newvar, value.i_int );
866                     break;
867                 case CONFIG_ITEM_FLOAT:
868                     config_PutFloat( p_vlc, psz_newvar, value.f_float );
869                     break;
870                 default:
871                     config_PutPsz( p_vlc, psz_newvar, value.psz_string );
872                     break;
873             }
874             if( i_object ) vlc_object_release( p_vlc );
875             return VLC_SUCCESS;
876         }
877     }
878
879     i_ret = var_Set( p_vlc, psz_var, value );
880
881     if( i_object ) vlc_object_release( p_vlc );
882     return i_ret;
883 }
884
885 /*****************************************************************************
886  * VLC_Get: get a vlc variable
887  *****************************************************************************
888  *
889  *****************************************************************************/
890 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
891 {
892     vlc_t *p_vlc = vlc_current_object( i_object );
893     int i_ret;
894
895     if( !p_vlc )
896     {
897         return VLC_ENOOBJ;
898     }
899
900     i_ret = var_Get( p_vlc, psz_var, p_value );
901
902     if( i_object ) vlc_object_release( p_vlc );
903     return i_ret;
904 }
905
906 /* FIXME: temporary hacks */
907
908 /*****************************************************************************
909  * VLC_Play: play
910  *****************************************************************************/
911 int VLC_Play( int i_object )
912 {
913     playlist_t * p_playlist;
914     vlc_t *p_vlc = vlc_current_object( i_object );
915
916     /* Check that the handle is valid */
917     if( !p_vlc )
918     {
919         return VLC_ENOOBJ;
920     }
921
922     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
923
924     if( !p_playlist )
925     {
926         if( i_object ) vlc_object_release( p_vlc );
927         return VLC_ENOOBJ;
928     }
929
930     vlc_mutex_lock( &p_playlist->object_lock );
931     if( p_playlist->i_size )
932     {
933         vlc_mutex_unlock( &p_playlist->object_lock );
934         playlist_Play( p_playlist );
935     }
936     else
937     {
938         vlc_mutex_unlock( &p_playlist->object_lock );
939     }
940
941     vlc_object_release( p_playlist );
942
943     if( i_object ) vlc_object_release( p_vlc );
944     return VLC_SUCCESS;
945 }
946
947 /*****************************************************************************
948  * VLC_Stop: stop
949  *****************************************************************************/
950 int VLC_Stop( int i_object )
951 {
952     intf_thread_t *   p_intf;
953     playlist_t    *   p_playlist;
954     vout_thread_t *   p_vout;
955     aout_instance_t * p_aout;
956     vlc_t *p_vlc = vlc_current_object( i_object );
957
958     /* Check that the handle is valid */
959     if( !p_vlc )
960     {
961         return VLC_ENOOBJ;
962     }
963
964     /*
965      * Ask the interfaces to stop and destroy them
966      */
967     msg_Dbg( p_vlc, "removing all interfaces" );
968
969     while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
970     {
971         intf_StopThread( p_intf );
972         vlc_object_detach( p_intf );
973         vlc_object_release( p_intf );
974         intf_Destroy( p_intf );
975     }
976
977     /*
978      * Free playlists
979      */
980     
981     msg_Dbg( p_vlc, "removing all playlists" );
982     while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
983                                           FIND_CHILD )) )
984     {
985         vlc_object_detach( p_playlist );
986         vlc_object_release( p_playlist );
987         playlist_Destroy( p_playlist );
988     }
989     
990     /*
991      * Free video outputs
992      */
993     msg_Dbg( p_vlc, "removing all video outputs" );
994     while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
995     {
996         vlc_object_detach( p_vout );
997         vlc_object_release( p_vout );
998         vout_Destroy( p_vout );
999     }
1000
1001     /*
1002      * Free audio outputs
1003      */
1004     msg_Dbg( p_vlc, "removing all audio outputs" );
1005     while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
1006     {
1007         vlc_object_detach( (vlc_object_t *)p_aout );
1008         vlc_object_release( (vlc_object_t *)p_aout );
1009         aout_Delete( p_aout );
1010     }
1011
1012     if( i_object ) vlc_object_release( p_vlc );
1013     return VLC_SUCCESS;
1014 }
1015
1016 /*****************************************************************************
1017  * VLC_Pause: toggle pause
1018  *****************************************************************************/
1019 int VLC_Pause( int i_object )
1020 {
1021     input_thread_t *p_input;
1022     vlc_t *p_vlc = vlc_current_object( i_object );
1023
1024     if( !p_vlc )
1025     {
1026         return VLC_ENOOBJ;
1027     }
1028
1029     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1030
1031     if( !p_input )
1032     {
1033         if( i_object ) vlc_object_release( p_vlc );
1034         return VLC_ENOOBJ;
1035     }
1036
1037     input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1038     vlc_object_release( p_input );
1039
1040     if( i_object ) vlc_object_release( p_vlc );
1041     return VLC_SUCCESS;
1042 }
1043
1044 /*****************************************************************************
1045  * VLC_FullScreen: toggle fullscreen mode
1046  *****************************************************************************/
1047 int VLC_FullScreen( int i_object )
1048 {
1049     vout_thread_t *p_vout;
1050     vlc_t *p_vlc = vlc_current_object( i_object );
1051
1052     if( !p_vlc )
1053     {
1054         return VLC_ENOOBJ;
1055     }
1056
1057     p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1058
1059     if( !p_vout )
1060     {
1061         if( i_object ) vlc_object_release( p_vlc );
1062         return VLC_ENOOBJ;
1063     }
1064
1065     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1066     vlc_object_release( p_vout );
1067
1068     if( i_object ) vlc_object_release( p_vlc );
1069     return VLC_SUCCESS;
1070 }
1071
1072 /* following functions are local */
1073
1074 /*****************************************************************************
1075  * SetLanguage: set the interface language.
1076  *****************************************************************************
1077  * We set the LC_MESSAGES locale category for interface messages and buttons,
1078  * as well as the LC_CTYPE category for string sorting and possible wide
1079  * character support.
1080  *****************************************************************************/
1081 static void SetLanguage ( char const *psz_lang )
1082 {
1083 #if defined( ENABLE_NLS ) \
1084      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1085
1086     char *          psz_path;
1087 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1088     char            psz_tmp[1024];
1089 #endif
1090
1091     if( psz_lang && !*psz_lang )
1092     {
1093 #   if defined( HAVE_LC_MESSAGES )
1094         setlocale( LC_MESSAGES, psz_lang );
1095 #   endif
1096         setlocale( LC_CTYPE, psz_lang );
1097     }
1098     else if( psz_lang )
1099     {
1100 #ifdef SYS_DARWIN
1101         /* I need that under Darwin, please check it doesn't disturb
1102          * other platforms. --Meuuh */
1103         setenv( "LANG", psz_lang, 1 );
1104
1105 #elif defined( SYS_BEOS ) || defined( WIN32 )
1106         /* We set LC_ALL manually because it is the only way to set
1107          * the language at runtime under eg. Windows. Beware that this
1108          * makes the environment unconsistent when libvlc is unloaded and
1109          * should probably be moved to a safer place like vlc.c. */
1110         static char psz_lcall[20];
1111         snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1112         psz_lcall[19] = '\0';
1113         putenv( psz_lcall );
1114 #endif
1115
1116         setlocale( LC_ALL, psz_lang );
1117     }
1118
1119     /* Specify where to find the locales for current domain */
1120 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1121     psz_path = LOCALEDIR;
1122 #else
1123     snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1124               "locale" );
1125     psz_path = psz_tmp;
1126 #endif
1127     if( !bindtextdomain( PACKAGE, psz_path ) )
1128     {
1129         fprintf( stderr, "warning: no domain %s in directory %s\n",
1130                  PACKAGE, psz_path );
1131     }
1132
1133     /* Set the default domain */
1134     textdomain( PACKAGE );
1135
1136 #if defined( SYS_BEOS ) || defined ( SYS_DARWIN ) || \
1137     ( defined( WIN32 ) && !defined( HAVE_INCLUDED_GETTEXT ) )
1138     /* BeOS only support UTF8 strings */
1139     /* Mac OS X prefers UTF8 */
1140     bind_textdomain_codeset( PACKAGE, "UTF-8" );
1141 #endif
1142
1143 #endif
1144 }
1145
1146 /*****************************************************************************
1147  * GetFilenames: parse command line options which are not flags
1148  *****************************************************************************
1149  * Parse command line for input files as well as their associated options.
1150  * An option always follows its associated input and begins with a ":".
1151  *****************************************************************************/
1152 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1153 {
1154     int i_opt, i_options;
1155
1156     /* We assume that the remaining parameters are filenames
1157      * and their input options */
1158     for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1159     {
1160         i_options = 0;
1161
1162         /* Count the input options */
1163         while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1164         {
1165             i_options++;
1166             i_opt--;
1167         }
1168
1169         /* TODO: write an internal function of this one, to avoid
1170          *       unnecessary lookups. */
1171         VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1172                        (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1173                                         NULL ), i_options,
1174                        PLAYLIST_INSERT | (i_opt == optind ? PLAYLIST_GO : 0),
1175                        0 );
1176     }
1177
1178     return VLC_SUCCESS;
1179 }
1180
1181 /*****************************************************************************
1182  * Usage: print program usage
1183  *****************************************************************************
1184  * Print a short inline help. Message interface is initialized at this stage.
1185  *****************************************************************************/
1186 static void Usage( vlc_t *p_this, char const *psz_module_name )
1187 {
1188 #define FORMAT_STRING "  %s --%s%s%s%s%s%s%s "
1189     /* short option ------'    |     | | | |  | |
1190      * option name ------------'     | | | |  | |
1191      * <bra -------------------------' | | |  | |
1192      * option type or "" --------------' | |  | |
1193      * ket> -----------------------------' |  | |
1194      * padding spaces ---------------------'  | |
1195      * comment -------------------------------' |
1196      * comment suffix --------------------------'
1197      *
1198      * The purpose of having bra and ket is that we might i18n them as well.
1199      */
1200 #define LINE_START 8
1201 #define PADDING_SPACES 25
1202     vlc_list_t *p_list;
1203     module_t *p_parser;
1204     module_config_t *p_item;
1205     char psz_spaces[PADDING_SPACES+LINE_START+1];
1206     char psz_format[sizeof(FORMAT_STRING)];
1207     char psz_buffer[1000];
1208     char psz_short[4];
1209     int i_index;
1210     int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1211
1212     memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1213     psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1214
1215     strcpy( psz_format, FORMAT_STRING );
1216
1217 #ifdef WIN32
1218     ShowConsole();
1219 #endif
1220
1221     /* List all modules */
1222     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1223
1224     /* Enumerate the config for each module */
1225     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1226     {
1227         vlc_bool_t b_help_module;
1228
1229         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1230
1231         if( psz_module_name && strcmp( psz_module_name,
1232                                        p_parser->psz_object_name ) )
1233         {
1234             continue;
1235         }
1236
1237         /* Ignore modules without config options */
1238         if( !p_parser->i_config_items )
1239         {
1240             continue;
1241         }
1242
1243         b_help_module = !strcmp( "help", p_parser->psz_object_name );
1244
1245         /* Print module options */
1246         for( p_item = p_parser->p_config;
1247              p_item->i_type != CONFIG_HINT_END;
1248              p_item++ )
1249         {
1250             char *psz_text;
1251             char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1252             char *psz_suf = "", *psz_prefix = NULL;
1253             int i;
1254             if ( p_item->b_advanced && !config_GetInt( p_this, "advanced" ))
1255             {
1256                 continue;
1257             }
1258             switch( p_item->i_type )
1259             {
1260             case CONFIG_HINT_CATEGORY:
1261             case CONFIG_HINT_USAGE:
1262                 fprintf( stdout, " %s\n", p_item->psz_text );
1263                 break;
1264
1265             case CONFIG_ITEM_STRING:
1266             case CONFIG_ITEM_FILE:
1267             case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1268                 if( !p_item->ppsz_list )
1269                 {
1270                     psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1271                     break;
1272                 }
1273                 else
1274                 {
1275                     psz_bra = " {";
1276                     psz_type = psz_buffer;
1277                     psz_type[0] = '\0';
1278                     for( i = 0; p_item->ppsz_list[i]; i++ )
1279                     {
1280                         if( i ) strcat( psz_type, "," );
1281                         strcat( psz_type, p_item->ppsz_list[i] );
1282                     }
1283                     psz_ket = "}";
1284                     break;
1285                 }
1286             case CONFIG_ITEM_INTEGER:
1287                 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1288                 break;
1289             case CONFIG_ITEM_FLOAT:
1290                 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1291                 break;
1292             case CONFIG_ITEM_BOOL:
1293                 psz_bra = ""; psz_type = ""; psz_ket = "";
1294                 if( !b_help_module )
1295                 {
1296                     psz_suf = p_item->i_value ? _(" (default enabled)") :
1297                                                 _(" (default disabled)");
1298                 }
1299                 break;
1300             }
1301
1302             if( !psz_type )
1303             {
1304                 continue;
1305             }
1306
1307             /* Add short option if any */
1308             if( p_item->i_short )
1309             {
1310                 sprintf( psz_short, "-%c,", p_item->i_short );
1311             }
1312             else
1313             {
1314                 strcpy( psz_short, "   " );
1315             }
1316
1317             i = PADDING_SPACES - strlen( p_item->psz_name )
1318                  - strlen( psz_bra ) - strlen( psz_type )
1319                  - strlen( psz_ket ) - 1;
1320
1321             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1322             {
1323                 /* If option is of type --foo-bar, we print its counterpart
1324                  * as --no-foo-bar, but if it is of type --foobar (without
1325                  * dashes in the name) we print it as --nofoobar. Both
1326                  * values are of course valid, only the display changes. */
1327                 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1328                                                              : ", --no";
1329                 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1330             }
1331
1332             if( i < 0 )
1333             {
1334                 psz_spaces[0] = '\n';
1335                 i = 0;
1336             }
1337             else
1338             {
1339                 psz_spaces[i] = '\0';
1340             }
1341
1342             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1343             {
1344                 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1345                          psz_prefix, p_item->psz_name, psz_bra, psz_type,
1346                          psz_ket, psz_spaces );
1347             }
1348             else
1349             {
1350                 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1351                          "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1352             }
1353
1354             psz_spaces[i] = ' ';
1355
1356             /* We wrap the rest of the output */
1357             sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1358             psz_text = psz_buffer;
1359             while( *psz_text )
1360             {
1361                 char *psz_parser, *psz_word;
1362                 int i_end = strlen( psz_text );
1363
1364                 /* If the remaining text fits in a line, print it. */
1365                 if( i_end <= i_width )
1366                 {
1367                     fprintf( stdout, "%s\n", psz_text );
1368                     break;
1369                 }
1370
1371                 /* Otherwise, eat as many words as possible */
1372                 psz_parser = psz_text;
1373                 do
1374                 {
1375                     psz_word = psz_parser;
1376                     psz_parser = strchr( psz_word, ' ' );
1377                     /* If no space was found, we reached the end of the text
1378                      * block; otherwise, we skip the space we just found. */
1379                     psz_parser = psz_parser ? psz_parser + 1
1380                                             : psz_text + i_end;
1381
1382                 } while( psz_parser - psz_text <= i_width );
1383
1384                 /* We cut a word in one of these cases:
1385                  *  - it's the only word in the line and it's too long.
1386                  *  - we used less than 80% of the width and the word we are
1387                  *    going to wrap is longer than 40% of the width, and even
1388                  *    if the word would have fit in the next line. */
1389                 if( psz_word == psz_text
1390                      || ( psz_word - psz_text < 80 * i_width / 100
1391                            && psz_parser - psz_word > 40 * i_width / 100 ) )
1392                 {
1393                     char c = psz_text[i_width];
1394                     psz_text[i_width] = '\0';
1395                     fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1396                     psz_text += i_width;
1397                     psz_text[0] = c;
1398                 }
1399                 else
1400                 {
1401                     psz_word[-1] = '\0';
1402                     fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1403                     psz_text = psz_word;
1404                 }
1405             }
1406         }
1407     }
1408
1409     /* Release the module list */
1410     vlc_list_release( p_list );
1411
1412 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1413     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1414     getchar();
1415 #endif
1416 }
1417
1418 /*****************************************************************************
1419  * ListModules: list the available modules with their description
1420  *****************************************************************************
1421  * Print a list of all available modules (builtins and plugins) and a short
1422  * description for each one.
1423  *****************************************************************************/
1424 static void ListModules( vlc_t *p_this )
1425 {
1426     vlc_list_t *p_list;
1427     module_t *p_parser;
1428     char psz_spaces[22];
1429     int i_index;
1430
1431     memset( psz_spaces, ' ', 22 );
1432
1433 #ifdef WIN32
1434     ShowConsole();
1435 #endif
1436
1437     /* Usage */
1438     fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1439                      p_this->p_vlc->psz_object_name );
1440
1441     fprintf( stdout, _("[module]              [description]\n") );
1442
1443     /* List all modules */
1444     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1445
1446     /* Enumerate each module */
1447     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1448     {
1449         int i;
1450
1451         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1452
1453         /* Nasty hack, but right now I'm too tired to think about a nice
1454          * solution */
1455         i = 22 - strlen( p_parser->psz_object_name ) - 1;
1456         if( i < 0 ) i = 0;
1457         psz_spaces[i] = 0;
1458
1459         fprintf( stdout, "  %s%s %s\n", p_parser->psz_object_name,
1460                          psz_spaces, p_parser->psz_longname );
1461
1462         psz_spaces[i] = ' ';
1463     }
1464
1465     vlc_list_release( p_list );
1466
1467 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1468     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1469     getchar();
1470 #endif
1471 }
1472
1473 /*****************************************************************************
1474  * Version: print complete program version
1475  *****************************************************************************
1476  * Print complete program version and build number.
1477  *****************************************************************************/
1478 static void Version( void )
1479 {
1480 #ifdef WIN32
1481     ShowConsole();
1482 #endif
1483
1484     fprintf( stdout, VERSION_MESSAGE "\n" );
1485     fprintf( stdout,
1486       _("This program comes with NO WARRANTY, to the extent permitted by "
1487         "law.\nYou may redistribute it under the terms of the GNU General "
1488         "Public License;\nsee the file named COPYING for details.\n"
1489         "Written by the VideoLAN team; see the AUTHORS file.\n") );
1490
1491 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1492     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1493     getchar();
1494 #endif
1495 }
1496
1497 /*****************************************************************************
1498  * ShowConsole: On Win32, create an output console for debug messages
1499  *****************************************************************************
1500  * This function is useful only on Win32.
1501  *****************************************************************************/
1502 #ifdef WIN32 /*  */
1503 static void ShowConsole( void )
1504 {
1505 #   ifndef UNDER_CE
1506     AllocConsole();
1507     freopen( "CONOUT$", "w", stdout );
1508     freopen( "CONOUT$", "w", stderr );
1509     freopen( "CONIN$", "r", stdin );
1510 #   endif
1511     return;
1512 }
1513 #endif
1514
1515 /*****************************************************************************
1516  * ConsoleWidth: Return the console width in characters
1517  *****************************************************************************
1518  * We use the stty shell command to get the console width; if this fails or
1519  * if the width is less than 80, we default to 80.
1520  *****************************************************************************/
1521 static int ConsoleWidth( void )
1522 {
1523     int i_width = 80;
1524
1525 #ifndef WIN32
1526     char buf[20], *psz_parser;
1527     FILE *file;
1528     int i_ret;
1529
1530     file = popen( "stty size 2>/dev/null", "r" );
1531     if( file )
1532     {
1533         i_ret = fread( buf, 1, 20, file );
1534         if( i_ret > 0 )
1535         {
1536             buf[19] = '\0';
1537             psz_parser = strchr( buf, ' ' );
1538             if( psz_parser )
1539             {
1540                 i_ret = atoi( psz_parser + 1 );
1541                 if( i_ret >= 80 )
1542                 {
1543                     i_width = i_ret;
1544                 }
1545             }
1546         }
1547
1548         pclose( file );
1549     }
1550 #endif
1551
1552     return i_width;
1553 }