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