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