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