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