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