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