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