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