]> git.sesse.net Git - vlc/blob - src/libvlc.c
Updated copyrights in libvlc
[vlc] / src / libvlc.c
1 /*****************************************************************************
2  * libvlc.c: main libvlc source
3  *****************************************************************************
4  * Copyright (C) 1998-2004 VideoLAN
5  * $Id: libvlc.c,v 1.109 2004/01/06 12:02:05 zorglub 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"),
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;
800     int i_err;
801     playlist_t *p_playlist;
802     vlc_t *p_vlc = vlc_current_object( i_object );
803
804     if( !p_vlc )
805     {
806         return VLC_ENOOBJ;
807     }
808
809     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
810
811     if( p_playlist == NULL )
812     {
813         msg_Dbg( p_vlc, "no playlist present, creating one" );
814         p_playlist = playlist_Create( p_vlc );
815
816         if( p_playlist == NULL )
817         {
818             if( i_object ) vlc_object_release( p_vlc );
819             return VLC_EGENERIC;
820         }
821
822         vlc_object_yield( p_playlist );
823     }
824
825     i_err = playlist_Add( p_playlist, psz_target, psz_target,
826                           i_mode, i_pos );
827
828     for( i = 0 ; i< i_options ; i++ )
829     {
830         playlist_AddOption( p_playlist, i_err , ppsz_options[i] );
831     }
832
833     vlc_object_release( p_playlist );
834
835     if( i_object ) vlc_object_release( p_vlc );
836     return i_err;
837 }
838
839 /*****************************************************************************
840  * VLC_Set: set a vlc variable
841  *****************************************************************************
842  *
843  *****************************************************************************/
844 int VLC_Set( int i_object, char const *psz_var, vlc_value_t value )
845 {
846     vlc_t *p_vlc = vlc_current_object( i_object );
847     int i_ret;
848
849     if( !p_vlc )
850     {
851         return VLC_ENOOBJ;
852     }
853
854     /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
855      * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
856     if( !strncmp( psz_var, "conf::", 6 ) )
857     {
858         module_config_t *p_item;
859         char const *psz_newvar = psz_var + 6;
860
861         p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
862
863         if( p_item )
864         {
865             switch( p_item->i_type )
866             {
867                 case CONFIG_ITEM_BOOL:
868                     config_PutInt( p_vlc, psz_newvar, value.b_bool );
869                     break;
870                 case CONFIG_ITEM_INTEGER:
871                     config_PutInt( p_vlc, psz_newvar, value.i_int );
872                     break;
873                 case CONFIG_ITEM_FLOAT:
874                     config_PutFloat( p_vlc, psz_newvar, value.f_float );
875                     break;
876                 default:
877                     config_PutPsz( p_vlc, psz_newvar, value.psz_string );
878                     break;
879             }
880             if( i_object ) vlc_object_release( p_vlc );
881             return VLC_SUCCESS;
882         }
883     }
884
885     i_ret = var_Set( p_vlc, psz_var, value );
886
887     if( i_object ) vlc_object_release( p_vlc );
888     return i_ret;
889 }
890
891 /*****************************************************************************
892  * VLC_Get: get a vlc variable
893  *****************************************************************************
894  *
895  *****************************************************************************/
896 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
897 {
898     vlc_t *p_vlc = vlc_current_object( i_object );
899     int i_ret;
900
901     if( !p_vlc )
902     {
903         return VLC_ENOOBJ;
904     }
905
906     i_ret = var_Get( p_vlc, psz_var, p_value );
907
908     if( i_object ) vlc_object_release( p_vlc );
909     return i_ret;
910 }
911
912 /* FIXME: temporary hacks */
913
914
915 /*****************************************************************************
916  * VLC_IsPlaying: Query for Playlist Status
917  *****************************************************************************/
918 vlc_bool_t VLC_IsPlaying( int i_object )
919 {
920
921     playlist_t * p_playlist;
922     vlc_bool_t   playing;
923
924     vlc_t *p_vlc = vlc_current_object( i_object );
925
926     /* Check that the handle is valid */
927     if( !p_vlc )
928     {
929         return VLC_ENOOBJ;
930     }
931
932     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
933
934     if( !p_playlist )
935     {
936         if( i_object ) vlc_object_release( p_vlc );
937         return VLC_ENOOBJ;
938     }
939
940     playing = playlist_IsPlaying( p_playlist );
941
942     vlc_object_release( p_playlist );
943
944     if( i_object ) vlc_object_release( p_vlc );
945
946     return playing;
947
948 }
949
950
951 /*****************************************************************************
952  * VLC_ClearPlaylist: Query for Playlist Status
953  *
954  * return: 0
955  *****************************************************************************/
956 int VLC_ClearPlaylist( int i_object )
957 {
958
959     playlist_t * p_playlist;
960     vlc_t *p_vlc = vlc_current_object( i_object );
961
962     /* Check that the handle is valid */
963     if( !p_vlc )
964     {
965         return VLC_ENOOBJ;
966     }
967
968     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
969
970     if( !p_playlist )
971     {
972         if( i_object ) vlc_object_release( p_vlc );
973         return VLC_ENOOBJ;
974     }
975
976     playlist_Clear(p_playlist);
977
978     vlc_object_release( p_playlist );
979
980     if( i_object ) vlc_object_release( p_vlc );
981     return 0;
982 }
983
984
985 /*****************************************************************************
986  * VLC_Play: play
987  *****************************************************************************/
988 int VLC_Play( int i_object )
989 {
990     playlist_t * p_playlist;
991     vlc_t *p_vlc = vlc_current_object( i_object );
992
993     /* Check that the handle is valid */
994     if( !p_vlc )
995     {
996         return VLC_ENOOBJ;
997     }
998
999     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1000
1001     if( !p_playlist )
1002     {
1003         if( i_object ) vlc_object_release( p_vlc );
1004         return VLC_ENOOBJ;
1005     }
1006
1007     vlc_mutex_lock( &p_playlist->object_lock );
1008     if( p_playlist->i_size )
1009     {
1010         vlc_mutex_unlock( &p_playlist->object_lock );
1011         playlist_Play( p_playlist );
1012     }
1013     else
1014     {
1015         vlc_mutex_unlock( &p_playlist->object_lock );
1016     }
1017
1018     vlc_object_release( p_playlist );
1019
1020     if( i_object ) vlc_object_release( p_vlc );
1021     return VLC_SUCCESS;
1022 }
1023
1024 /*****************************************************************************
1025  * VLC_Stop: stop
1026  *****************************************************************************/
1027 int VLC_Stop( int i_object )
1028 {
1029     intf_thread_t *   p_intf;
1030     playlist_t    *   p_playlist;
1031     vout_thread_t *   p_vout;
1032     aout_instance_t * p_aout;
1033     vlc_t *p_vlc = vlc_current_object( i_object );
1034
1035     /* Check that the handle is valid */
1036     if( !p_vlc )
1037     {
1038         return VLC_ENOOBJ;
1039     }
1040
1041     /*
1042      * Ask the interfaces to stop and destroy them
1043      */
1044     msg_Dbg( p_vlc, "removing all interfaces" );
1045
1046     while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
1047     {
1048         intf_StopThread( p_intf );
1049         vlc_object_detach( p_intf );
1050         vlc_object_release( p_intf );
1051         intf_Destroy( p_intf );
1052     }
1053
1054     /*
1055      * Free playlists
1056      */
1057     
1058     msg_Dbg( p_vlc, "removing all playlists" );
1059     while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
1060                                           FIND_CHILD )) )
1061     {
1062         vlc_object_detach( p_playlist );
1063         vlc_object_release( p_playlist );
1064         playlist_Destroy( p_playlist );
1065     }
1066     
1067     /*
1068      * Free video outputs
1069      */
1070     msg_Dbg( p_vlc, "removing all video outputs" );
1071     while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
1072     {
1073         vlc_object_detach( p_vout );
1074         vlc_object_release( p_vout );
1075         vout_Destroy( p_vout );
1076     }
1077
1078     /*
1079      * Free audio outputs
1080      */
1081     msg_Dbg( p_vlc, "removing all audio outputs" );
1082     while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
1083     {
1084         vlc_object_detach( (vlc_object_t *)p_aout );
1085         vlc_object_release( (vlc_object_t *)p_aout );
1086         aout_Delete( p_aout );
1087     }
1088
1089     if( i_object ) vlc_object_release( p_vlc );
1090     return VLC_SUCCESS;
1091 }
1092
1093 /*****************************************************************************
1094  * VLC_Pause: toggle pause
1095  *****************************************************************************/
1096 int VLC_Pause( int i_object )
1097 {
1098     input_thread_t *p_input;
1099     vlc_t *p_vlc = vlc_current_object( i_object );
1100
1101     if( !p_vlc )
1102     {
1103         return VLC_ENOOBJ;
1104     }
1105
1106     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1107
1108     if( !p_input )
1109     {
1110         if( i_object ) vlc_object_release( p_vlc );
1111         return VLC_ENOOBJ;
1112     }
1113
1114     input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1115     vlc_object_release( p_input );
1116
1117     if( i_object ) vlc_object_release( p_vlc );
1118     return VLC_SUCCESS;
1119 }
1120
1121 /*****************************************************************************
1122  * VLC_FullScreen: toggle fullscreen mode
1123  *****************************************************************************/
1124 int VLC_FullScreen( int i_object )
1125 {
1126     vout_thread_t *p_vout;
1127     vlc_t *p_vlc = vlc_current_object( i_object );
1128
1129     if( !p_vlc )
1130     {
1131         return VLC_ENOOBJ;
1132     }
1133
1134     p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1135
1136     if( !p_vout )
1137     {
1138         if( i_object ) vlc_object_release( p_vlc );
1139         return VLC_ENOOBJ;
1140     }
1141
1142     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1143     vlc_object_release( p_vout );
1144
1145     if( i_object ) vlc_object_release( p_vlc );
1146     return VLC_SUCCESS;
1147 }
1148
1149 /* following functions are local */
1150
1151 /*****************************************************************************
1152  * SetLanguage: set the interface language.
1153  *****************************************************************************
1154  * We set the LC_MESSAGES locale category for interface messages and buttons,
1155  * as well as the LC_CTYPE category for string sorting and possible wide
1156  * character support.
1157  *****************************************************************************/
1158 static void SetLanguage ( char const *psz_lang )
1159 {
1160 #if defined( ENABLE_NLS ) \
1161      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1162
1163     char *          psz_path;
1164 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1165     char            psz_tmp[1024];
1166 #endif
1167
1168     if( psz_lang && !*psz_lang )
1169     {
1170 #   if defined( HAVE_LC_MESSAGES )
1171         setlocale( LC_MESSAGES, psz_lang );
1172 #   endif
1173         setlocale( LC_CTYPE, psz_lang );
1174     }
1175     else if( psz_lang )
1176     {
1177 #ifdef SYS_DARWIN
1178         /* I need that under Darwin, please check it doesn't disturb
1179          * other platforms. --Meuuh */
1180         setenv( "LANG", psz_lang, 1 );
1181
1182 #elif defined( SYS_BEOS ) || defined( WIN32 )
1183         /* We set LC_ALL manually because it is the only way to set
1184          * the language at runtime under eg. Windows. Beware that this
1185          * makes the environment unconsistent when libvlc is unloaded and
1186          * should probably be moved to a safer place like vlc.c. */
1187         static char psz_lcall[20];
1188         snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1189         psz_lcall[19] = '\0';
1190         putenv( psz_lcall );
1191 #endif
1192
1193         setlocale( LC_ALL, psz_lang );
1194     }
1195
1196     /* Specify where to find the locales for current domain */
1197 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1198     psz_path = LOCALEDIR;
1199 #else
1200     snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1201               "locale" );
1202     psz_path = psz_tmp;
1203 #endif
1204     if( !bindtextdomain( PACKAGE, psz_path ) )
1205     {
1206         fprintf( stderr, "warning: no domain %s in directory %s\n",
1207                  PACKAGE, psz_path );
1208     }
1209
1210     /* Set the default domain */
1211     textdomain( PACKAGE );
1212
1213 #if defined( SYS_BEOS ) || defined ( SYS_DARWIN ) || \
1214     ( defined( WIN32 ) && !defined( HAVE_INCLUDED_GETTEXT ) )
1215     /* BeOS only support UTF8 strings */
1216     /* Mac OS X prefers UTF8 */
1217     bind_textdomain_codeset( PACKAGE, "UTF-8" );
1218 #endif
1219
1220 #endif
1221 }
1222
1223 /*****************************************************************************
1224  * GetFilenames: parse command line options which are not flags
1225  *****************************************************************************
1226  * Parse command line for input files as well as their associated options.
1227  * An option always follows its associated input and begins with a ":".
1228  *****************************************************************************/
1229 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1230 {
1231     int i_opt, i_options;
1232
1233     /* We assume that the remaining parameters are filenames
1234      * and their input options */
1235     for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1236     {
1237         i_options = 0;
1238
1239         /* Count the input options */
1240         while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1241         {
1242             i_options++;
1243             i_opt--;
1244         }
1245
1246         /* TODO: write an internal function of this one, to avoid
1247          *       unnecessary lookups. */
1248         VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1249                        (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1250                                         NULL ), i_options,
1251                        PLAYLIST_INSERT | (i_opt == optind ? PLAYLIST_GO : 0),
1252                        0 );
1253     }
1254
1255     return VLC_SUCCESS;
1256 }
1257
1258 /*****************************************************************************
1259  * Usage: print program usage
1260  *****************************************************************************
1261  * Print a short inline help. Message interface is initialized at this stage.
1262  *****************************************************************************/
1263 static void Usage( vlc_t *p_this, char const *psz_module_name )
1264 {
1265 #define FORMAT_STRING "  %s --%s%s%s%s%s%s%s "
1266     /* short option ------'    |     | | | |  | |
1267      * option name ------------'     | | | |  | |
1268      * <bra -------------------------' | | |  | |
1269      * option type or "" --------------' | |  | |
1270      * ket> -----------------------------' |  | |
1271      * padding spaces ---------------------'  | |
1272      * comment -------------------------------' |
1273      * comment suffix --------------------------'
1274      *
1275      * The purpose of having bra and ket is that we might i18n them as well.
1276      */
1277 #define LINE_START 8
1278 #define PADDING_SPACES 25
1279     vlc_list_t *p_list;
1280     module_t *p_parser;
1281     module_config_t *p_item;
1282     char psz_spaces[PADDING_SPACES+LINE_START+1];
1283     char psz_format[sizeof(FORMAT_STRING)];
1284     char psz_buffer[1000];
1285     char psz_short[4];
1286     int i_index;
1287     int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1288
1289     memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1290     psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1291
1292     strcpy( psz_format, FORMAT_STRING );
1293
1294 #ifdef WIN32
1295     ShowConsole();
1296 #endif
1297
1298     /* List all modules */
1299     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1300
1301     /* Enumerate the config for each module */
1302     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1303     {
1304         vlc_bool_t b_help_module;
1305
1306         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1307
1308         if( psz_module_name && strcmp( psz_module_name,
1309                                        p_parser->psz_object_name ) )
1310         {
1311             continue;
1312         }
1313
1314         /* Ignore modules without config options */
1315         if( !p_parser->i_config_items )
1316         {
1317             continue;
1318         }
1319
1320         b_help_module = !strcmp( "help", p_parser->psz_object_name );
1321
1322         /* Print module options */
1323         for( p_item = p_parser->p_config;
1324              p_item->i_type != CONFIG_HINT_END;
1325              p_item++ )
1326         {
1327             char *psz_text;
1328             char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1329             char *psz_suf = "", *psz_prefix = NULL;
1330             int i;
1331             if ( p_item->b_advanced && !config_GetInt( p_this, "advanced" ))
1332             {
1333                 continue;
1334             }
1335             switch( p_item->i_type )
1336             {
1337             case CONFIG_HINT_CATEGORY:
1338             case CONFIG_HINT_USAGE:
1339                 fprintf( stdout, "\n %s\n", p_item->psz_text );
1340                 break;
1341
1342             case CONFIG_ITEM_STRING:
1343             case CONFIG_ITEM_FILE:
1344             case CONFIG_ITEM_DIRECTORY:
1345             case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1346                 if( !p_item->ppsz_list )
1347                 {
1348                     psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1349                     break;
1350                 }
1351                 else
1352                 {
1353                     psz_bra = " {";
1354                     psz_type = psz_buffer;
1355                     psz_type[0] = '\0';
1356                     for( i = 0; p_item->ppsz_list[i]; i++ )
1357                     {
1358                         if( i ) strcat( psz_type, "," );
1359                         strcat( psz_type, p_item->ppsz_list[i] );
1360                     }
1361                     psz_ket = "}";
1362                     break;
1363                 }
1364             case CONFIG_ITEM_INTEGER:
1365             case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1366                 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1367                 break;
1368             case CONFIG_ITEM_FLOAT:
1369                 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1370                 break;
1371             case CONFIG_ITEM_BOOL:
1372                 psz_bra = ""; psz_type = ""; psz_ket = "";
1373                 if( !b_help_module )
1374                 {
1375                     psz_suf = p_item->i_value ? _(" (default enabled)") :
1376                                                 _(" (default disabled)");
1377                 }
1378                 break;
1379             }
1380
1381             if( !psz_type )
1382             {
1383                 continue;
1384             }
1385
1386             /* Add short option if any */
1387             if( p_item->i_short )
1388             {
1389                 sprintf( psz_short, "-%c,", p_item->i_short );
1390             }
1391             else
1392             {
1393                 strcpy( psz_short, "   " );
1394             }
1395
1396             i = PADDING_SPACES - strlen( p_item->psz_name )
1397                  - strlen( psz_bra ) - strlen( psz_type )
1398                  - strlen( psz_ket ) - 1;
1399
1400             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1401             {
1402                 /* If option is of type --foo-bar, we print its counterpart
1403                  * as --no-foo-bar, but if it is of type --foobar (without
1404                  * dashes in the name) we print it as --nofoobar. Both
1405                  * values are of course valid, only the display changes. */
1406                 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1407                                                              : ", --no";
1408                 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1409             }
1410
1411             if( i < 0 )
1412             {
1413                 psz_spaces[0] = '\n';
1414                 i = 0;
1415             }
1416             else
1417             {
1418                 psz_spaces[i] = '\0';
1419             }
1420
1421             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1422             {
1423                 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1424                          psz_prefix, p_item->psz_name, psz_bra, psz_type,
1425                          psz_ket, psz_spaces );
1426             }
1427             else
1428             {
1429                 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1430                          "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1431             }
1432
1433             psz_spaces[i] = ' ';
1434
1435             /* We wrap the rest of the output */
1436             sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1437             psz_text = psz_buffer;
1438             while( *psz_text )
1439             {
1440                 char *psz_parser, *psz_word;
1441                 int i_end = strlen( psz_text );
1442
1443                 /* If the remaining text fits in a line, print it. */
1444                 if( i_end <= i_width )
1445                 {
1446                     fprintf( stdout, "%s\n", psz_text );
1447                     break;
1448                 }
1449
1450                 /* Otherwise, eat as many words as possible */
1451                 psz_parser = psz_text;
1452                 do
1453                 {
1454                     psz_word = psz_parser;
1455                     psz_parser = strchr( psz_word, ' ' );
1456                     /* If no space was found, we reached the end of the text
1457                      * block; otherwise, we skip the space we just found. */
1458                     psz_parser = psz_parser ? psz_parser + 1
1459                                             : psz_text + i_end;
1460
1461                 } while( psz_parser - psz_text <= i_width );
1462
1463                 /* We cut a word in one of these cases:
1464                  *  - it's the only word in the line and it's too long.
1465                  *  - we used less than 80% of the width and the word we are
1466                  *    going to wrap is longer than 40% of the width, and even
1467                  *    if the word would have fit in the next line. */
1468                 if( psz_word == psz_text
1469                      || ( psz_word - psz_text < 80 * i_width / 100
1470                            && psz_parser - psz_word > 40 * i_width / 100 ) )
1471                 {
1472                     char c = psz_text[i_width];
1473                     psz_text[i_width] = '\0';
1474                     fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1475                     psz_text += i_width;
1476                     psz_text[0] = c;
1477                 }
1478                 else
1479                 {
1480                     psz_word[-1] = '\0';
1481                     fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1482                     psz_text = psz_word;
1483                 }
1484             }
1485         }
1486     }
1487
1488     /* Release the module list */
1489     vlc_list_release( p_list );
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  * ListModules: list the available modules with their description
1499  *****************************************************************************
1500  * Print a list of all available modules (builtins and plugins) and a short
1501  * description for each one.
1502  *****************************************************************************/
1503 static void ListModules( vlc_t *p_this )
1504 {
1505     vlc_list_t *p_list;
1506     module_t *p_parser;
1507     char psz_spaces[22];
1508     int i_index;
1509
1510     memset( psz_spaces, ' ', 22 );
1511
1512 #ifdef WIN32
1513     ShowConsole();
1514 #endif
1515
1516     /* Usage */
1517     fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1518                      p_this->p_vlc->psz_object_name );
1519
1520     fprintf( stdout, _("[module]              [description]\n") );
1521
1522     /* List all modules */
1523     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1524
1525     /* Enumerate each module */
1526     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1527     {
1528         int i;
1529
1530         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1531
1532         /* Nasty hack, but right now I'm too tired to think about a nice
1533          * solution */
1534         i = 22 - strlen( p_parser->psz_object_name ) - 1;
1535         if( i < 0 ) i = 0;
1536         psz_spaces[i] = 0;
1537
1538         fprintf( stdout, "  %s%s %s\n", p_parser->psz_object_name,
1539                          psz_spaces, p_parser->psz_longname );
1540
1541         psz_spaces[i] = ' ';
1542     }
1543
1544     vlc_list_release( p_list );
1545
1546 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1547     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1548     getchar();
1549 #endif
1550 }
1551
1552 /*****************************************************************************
1553  * Version: print complete program version
1554  *****************************************************************************
1555  * Print complete program version and build number.
1556  *****************************************************************************/
1557 static void Version( void )
1558 {
1559 #ifdef WIN32
1560     ShowConsole();
1561 #endif
1562
1563     fprintf( stdout, VERSION_MESSAGE "\n" );
1564     fprintf( stdout,
1565       _("This program comes with NO WARRANTY, to the extent permitted by "
1566         "law.\nYou may redistribute it under the terms of the GNU General "
1567         "Public License;\nsee the file named COPYING for details.\n"
1568         "Written by the VideoLAN team; see the AUTHORS file.\n") );
1569
1570 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1571     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1572     getchar();
1573 #endif
1574 }
1575
1576 /*****************************************************************************
1577  * ShowConsole: On Win32, create an output console for debug messages
1578  *****************************************************************************
1579  * This function is useful only on Win32.
1580  *****************************************************************************/
1581 #ifdef WIN32 /*  */
1582 static void ShowConsole( void )
1583 {
1584 #   ifndef UNDER_CE
1585     AllocConsole();
1586     freopen( "CONOUT$", "w", stdout );
1587     freopen( "CONOUT$", "w", stderr );
1588     freopen( "CONIN$", "r", stdin );
1589 #   endif
1590     return;
1591 }
1592 #endif
1593
1594 /*****************************************************************************
1595  * ConsoleWidth: Return the console width in characters
1596  *****************************************************************************
1597  * We use the stty shell command to get the console width; if this fails or
1598  * if the width is less than 80, we default to 80.
1599  *****************************************************************************/
1600 static int ConsoleWidth( void )
1601 {
1602     int i_width = 80;
1603
1604 #ifndef WIN32
1605     char buf[20], *psz_parser;
1606     FILE *file;
1607     int i_ret;
1608
1609     file = popen( "stty size 2>/dev/null", "r" );
1610     if( file )
1611     {
1612         i_ret = fread( buf, 1, 20, file );
1613         if( i_ret > 0 )
1614         {
1615             buf[19] = '\0';
1616             psz_parser = strchr( buf, ' ' );
1617             if( psz_parser )
1618             {
1619                 i_ret = atoi( psz_parser + 1 );
1620                 if( i_ret >= 80 )
1621                 {
1622                     i_width = i_ret;
1623                 }
1624             }
1625         }
1626
1627         pclose( file );
1628     }
1629 #endif
1630
1631     return i_width;
1632 }