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