]> git.sesse.net Git - vlc/blob - src/libvlc.c
* src/libvlc.c:
[vlc] / src / libvlc.c
1 /*****************************************************************************
2  * libvlc.c: main libvlc source
3  *****************************************************************************
4  * Copyright (C) 1998-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *          Derk-Jan Hartman <hartman at videolan dot org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Pretend we are a builtin module
29  *****************************************************************************/
30 #define MODULE_NAME main
31 #define MODULE_PATH main
32 #define __BUILTIN__
33
34 /*****************************************************************************
35  * Preamble
36  *****************************************************************************/
37 #include <vlc/vlc.h>
38 #include <vlc/input.h>
39
40 #ifdef HAVE_ERRNO_H
41 #   include <errno.h>                                              /* ENOMEM */
42 #endif
43 #include <stdio.h>                                              /* sprintf() */
44 #include <string.h>                                            /* strerror() */
45 #include <stdlib.h>                                                /* free() */
46
47 #ifndef WIN32
48 #   include <netinet/in.h>                            /* BSD: struct in_addr */
49 #endif
50
51 #ifdef HAVE_UNISTD_H
52 #   include <unistd.h>
53 #elif defined( WIN32 ) && !defined( UNDER_CE )
54 #   include <io.h>
55 #endif
56
57 #ifdef WIN32                       /* optind, getopt(), included in unistd.h */
58 #   include "extras/getopt.h"
59 #endif
60
61 #ifdef HAVE_LOCALE_H
62 #   include <locale.h>
63 #endif
64
65 #include "vlc_cpu.h"                                        /* CPU detection */
66 #include "os_specific.h"
67
68 #include "vlc_error.h"
69
70 #include "vlc_playlist.h"
71 #include "vlc_interface.h"
72
73 #include "audio_output.h"
74
75 #include "vlc_video.h"
76 #include "video_output.h"
77
78 #include "stream_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 libvlc_t * p_libvlc;
87 static vlc_t *    p_static_vlc;
88
89 /*****************************************************************************
90  * Local prototypes
91  *****************************************************************************/
92 static void SetLanguage   ( char const * );
93 static int  GetFilenames  ( vlc_t *, int, char *[] );
94 static void Usage         ( vlc_t *, char const *psz_module_name );
95 static void ListModules   ( vlc_t * );
96 static void Version       ( void );
97
98 #ifdef WIN32
99 static void ShowConsole   ( void );
100 #endif
101 static int  ConsoleWidth  ( void );
102
103 static int  VerboseCallback( vlc_object_t *, char const *,
104                              vlc_value_t, vlc_value_t, void * );
105
106 /*****************************************************************************
107  * vlc_current_object: return the current object.
108  *****************************************************************************
109  * If i_object is non-zero, return the corresponding object. Otherwise,
110  * return the statically allocated p_vlc object.
111  *****************************************************************************/
112 vlc_t * vlc_current_object( int i_object )
113 {
114     if( i_object )
115     {
116          return vlc_object_get( p_libvlc, i_object );
117     }
118
119     return p_static_vlc;
120 }
121
122 /*****************************************************************************
123  * VLC_Version: return the libvlc version.
124  *****************************************************************************
125  * This function returns full version string (numeric version and codename).
126  *****************************************************************************/
127 char const * VLC_Version( void )
128 {
129     return VERSION_MESSAGE;
130 }
131
132 /*****************************************************************************
133  * VLC_Error: strerror() equivalent
134  *****************************************************************************
135  * This function returns full version string (numeric version and codename).
136  *****************************************************************************/
137 char const * VLC_Error( int i_err )
138 {
139     return vlc_error( i_err );
140 }
141
142 /*****************************************************************************
143  * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
144  *****************************************************************************
145  * This function allocates a vlc_t structure and returns a negative value
146  * in case of failure. Also, the thread system is initialized.
147  *****************************************************************************/
148 int VLC_Create( void )
149 {
150     int i_ret;
151     vlc_t * p_vlc = NULL;
152     vlc_value_t lockval;
153
154     /* &libvlc never changes, so we can safely call this multiple times. */
155     p_libvlc = &libvlc;
156
157     /* vlc_threads_init *must* be the first internal call! No other call is
158      * allowed before the thread system has been initialized. */
159     i_ret = vlc_threads_init( p_libvlc );
160     if( i_ret < 0 )
161     {
162         return i_ret;
163     }
164
165     /* Now that the thread system is initialized, we don't have much, but
166      * at least we have var_Create */
167     var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
168     var_Get( p_libvlc, "libvlc", &lockval );
169     vlc_mutex_lock( lockval.p_address );
170     if( !libvlc.b_ready )
171     {
172         char *psz_env;
173
174         /* Guess what CPU we have */
175         libvlc.i_cpu = CPUCapabilities();
176
177         /* Find verbosity from VLC_VERBOSE environment variable */
178         psz_env = getenv( "VLC_VERBOSE" );
179         libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
180
181 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
182         libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
183 #else
184         libvlc.b_color = VLC_FALSE;
185 #endif
186
187         /* Initialize message queue */
188         msg_Create( p_libvlc );
189
190         /* Announce who we are */
191         msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
192         msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
193
194         /* The module bank will be initialized later */
195         libvlc.p_module_bank = NULL;
196
197         libvlc.b_ready = VLC_TRUE;
198     }
199     vlc_mutex_unlock( lockval.p_address );
200     var_Destroy( p_libvlc, "libvlc" );
201
202     /* Allocate a vlc object */
203     p_vlc = vlc_object_create( p_libvlc, VLC_OBJECT_VLC );
204     if( p_vlc == NULL )
205     {
206         return VLC_EGENERIC;
207     }
208     p_vlc->thread_id = 0;
209     vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
210
211     p_vlc->psz_object_name = "root";
212
213     /* Initialize mutexes */
214     vlc_mutex_init( p_vlc, &p_vlc->config_lock );
215 #ifdef SYS_DARWIN
216     vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
217 #endif
218
219     /* Store our newly allocated structure in the global list */
220     vlc_object_attach( p_vlc, p_libvlc );
221
222     /* Store data for the non-reentrant API */
223     p_static_vlc = p_vlc;
224
225     return p_vlc->i_object_id;
226 }
227
228 /*****************************************************************************
229  * VLC_Init: initialize a vlc_t structure.
230  *****************************************************************************
231  * This function initializes a previously allocated vlc_t structure:
232  *  - CPU detection
233  *  - gettext initialization
234  *  - message queue, module bank and playlist initialization
235  *  - configuration and commandline parsing
236  *****************************************************************************/
237 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
238 {
239     char         p_capabilities[200];
240     char *       p_tmp;
241     char *       psz_modules;
242     char *       psz_parser;
243     char *       psz_language;
244     vlc_bool_t   b_exit = VLC_FALSE;
245     vlc_t *      p_vlc = vlc_current_object( i_object );
246     module_t    *p_help_module;
247     playlist_t  *p_playlist;
248
249     if( !p_vlc )
250     {
251         return VLC_ENOOBJ;
252     }
253
254     /*
255      * System specific initialization code
256      */
257     system_Init( p_vlc, &i_argc, ppsz_argv );
258
259     /* Get the executable name (similar to the basename command) */
260     if( i_argc > 0 )
261     {
262         p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
263         while( *p_tmp )
264         {
265             if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
266             else ++p_tmp;
267         }
268     }
269     else
270     {
271         p_vlc->psz_object_name = "vlc";
272     }
273
274     /*
275      * Support for gettext
276      */
277     SetLanguage( "" );
278
279     /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
280     msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
281
282     /* Initialize the module bank and load the configuration of the
283      * main module. We need to do this at this stage to be able to display
284      * a short help if required by the user. (short help == main module
285      * options) */
286     module_InitBank( p_vlc );
287
288     /* Hack: insert the help module here */
289     p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
290     if( p_help_module == NULL )
291     {
292         module_EndBank( p_vlc );
293         if( i_object ) vlc_object_release( p_vlc );
294         return VLC_EGENERIC;
295     }
296     p_help_module->psz_object_name = "help";
297     p_help_module->psz_longname = N_("Help options");
298     config_Duplicate( p_help_module, p_help_config );
299     vlc_object_attach( p_help_module, libvlc.p_module_bank );
300     /* End hack */
301
302     if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
303     {
304         vlc_object_detach( p_help_module );
305         config_Free( p_help_module );
306         vlc_object_destroy( p_help_module );
307         module_EndBank( p_vlc );
308         if( i_object ) vlc_object_release( p_vlc );
309         return VLC_EGENERIC;
310     }
311
312     /* Check for short help option */
313     if( config_GetInt( p_vlc, "help" ) )
314     {
315         fprintf( stdout, _("Usage: %s [options] [items]...\n"),
316                          p_vlc->psz_object_name );
317         Usage( p_vlc, "main" );
318         Usage( p_vlc, "help" );
319         b_exit = VLC_TRUE;
320     }
321     /* Check for version option */
322     else if( config_GetInt( p_vlc, "version" ) )
323     {
324         Version();
325         b_exit = VLC_TRUE;
326     }
327
328     /* Set the config file stuff */
329     p_vlc->psz_homedir = config_GetHomeDir();
330     p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
331
332     /* Check for plugins cache options */
333     if( config_GetInt( p_vlc, "reset-plugins-cache" ) )
334     {
335         libvlc.p_module_bank->b_cache_delete = VLC_TRUE;
336     }
337
338     /* Hack: remove the help module here */
339     vlc_object_detach( p_help_module );
340     /* End hack */
341
342     if( b_exit )
343     {
344         config_Free( p_help_module );
345         vlc_object_destroy( p_help_module );
346         module_EndBank( p_vlc );
347         if( i_object ) vlc_object_release( p_vlc );
348         return VLC_EEXIT;
349     }
350
351     /* Check for translation config option */
352 #if defined( ENABLE_NLS ) \
353      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
354
355     /* This ain't really nice to have to reload the config here but it seems
356      * the only way to do it. */
357     config_LoadConfigFile( p_vlc, "main" );
358     config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
359
360     /* Check if the user specified a custom language */
361     psz_language = config_GetPsz( p_vlc, "language" );
362     if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
363     {
364         vlc_bool_t b_cache_delete = libvlc.p_module_bank->b_cache_delete;
365
366         /* Reset the default domain */
367         SetLanguage( psz_language );
368
369         /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
370         msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
371
372         textdomain( PACKAGE );
373
374 #if defined( ENABLE_UTF8 )
375         bind_textdomain_codeset( PACKAGE, "UTF-8" );
376 #endif
377
378         module_EndBank( p_vlc );
379         module_InitBank( p_vlc );
380         config_LoadConfigFile( p_vlc, "main" );
381         config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
382         libvlc.p_module_bank->b_cache_delete = b_cache_delete;
383     }
384     if( psz_language ) free( psz_language );
385 #endif
386
387     /*
388      * Load the builtins and plugins into the module_bank.
389      * We have to do it before config_Load*() because this also gets the
390      * list of configuration options exported by each module and loads their
391      * default values.
392      */
393     module_LoadBuiltins( p_vlc );
394     module_LoadPlugins( p_vlc );
395     if( p_vlc->b_die )
396     {
397         b_exit = VLC_TRUE;
398     }
399
400     msg_Dbg( p_vlc, "module bank initialized, found %i modules",
401                     libvlc.p_module_bank->i_children );
402
403     /* Hack: insert the help module here */
404     vlc_object_attach( p_help_module, libvlc.p_module_bank );
405     /* End hack */
406
407     /* Check for help on modules */
408     if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
409     {
410         Usage( p_vlc, p_tmp );
411         free( p_tmp );
412         b_exit = VLC_TRUE;
413     }
414     /* Check for long help option */
415     else if( config_GetInt( p_vlc, "longhelp" ) )
416     {
417         Usage( p_vlc, NULL );
418         b_exit = VLC_TRUE;
419     }
420     /* Check for module list option */
421     else if( config_GetInt( p_vlc, "list" ) )
422     {
423         ListModules( p_vlc );
424         b_exit = VLC_TRUE;
425     }
426
427     /* Check for config file options */
428     if( config_GetInt( p_vlc, "reset-config" ) )
429     {
430         vlc_object_detach( p_help_module );
431         config_ResetAll( p_vlc );
432         config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
433         config_SaveConfigFile( p_vlc, NULL );
434         vlc_object_attach( p_help_module, libvlc.p_module_bank );
435     }
436     if( config_GetInt( p_vlc, "save-config" ) )
437     {
438         vlc_object_detach( p_help_module );
439         config_LoadConfigFile( p_vlc, NULL );
440         config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
441         config_SaveConfigFile( p_vlc, NULL );
442         vlc_object_attach( p_help_module, libvlc.p_module_bank );
443     }
444
445     /* Hack: remove the help module here */
446     vlc_object_detach( p_help_module );
447     /* End hack */
448
449     if( b_exit )
450     {
451         config_Free( p_help_module );
452         vlc_object_destroy( p_help_module );
453         module_EndBank( p_vlc );
454         if( i_object ) vlc_object_release( p_vlc );
455         return VLC_EEXIT;
456     }
457
458     /*
459      * Override default configuration with config file settings
460      */
461     config_LoadConfigFile( p_vlc, NULL );
462
463     /* Hack: insert the help module here */
464     vlc_object_attach( p_help_module, libvlc.p_module_bank );
465     /* End hack */
466
467     /*
468      * Override configuration with command line settings
469      */
470     if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
471     {
472 #ifdef WIN32
473         ShowConsole();
474         /* Pause the console because it's destroyed when we exit */
475         fprintf( stderr, "The command line options couldn't be loaded, check "
476                  "that they are valid.\nPress the RETURN key to continue..." );
477         getchar();
478 #endif
479         vlc_object_detach( p_help_module );
480         config_Free( p_help_module );
481         vlc_object_destroy( p_help_module );
482         module_EndBank( p_vlc );
483         if( i_object ) vlc_object_release( p_vlc );
484         return VLC_EGENERIC;
485     }
486
487     /* Hack: remove the help module here */
488     vlc_object_detach( p_help_module );
489     config_Free( p_help_module );
490     vlc_object_destroy( p_help_module );
491     /* End hack */
492
493     /*
494      * System specific configuration
495      */
496     system_Configure( p_vlc, &i_argc, ppsz_argv );
497
498     /*
499      * Message queue options
500      */
501
502     var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
503     if( config_GetInt( p_vlc, "quiet" ) )
504     {
505         vlc_value_t val;
506         val.i_int = -1;
507         var_Set( p_vlc, "verbose", val );
508     }
509     var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
510     var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
511
512     libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
513
514     /*
515      * Output messages that may still be in the queue
516      */
517     msg_Flush( p_vlc );
518
519     /* p_vlc initialization. FIXME ? */
520
521 #if defined( __i386__ )
522     if( !config_GetInt( p_vlc, "mmx" ) )
523         libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
524     if( !config_GetInt( p_vlc, "3dn" ) )
525         libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
526     if( !config_GetInt( p_vlc, "mmxext" ) )
527         libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
528     if( !config_GetInt( p_vlc, "sse" ) )
529         libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
530     if( !config_GetInt( p_vlc, "sse2" ) )
531         libvlc.i_cpu &= ~CPU_CAPABILITY_SSE2;
532 #endif
533 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
534     if( !config_GetInt( p_vlc, "altivec" ) )
535         libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
536 #endif
537
538 #define PRINT_CAPABILITY( capability, string )                              \
539     if( libvlc.i_cpu & capability )                                         \
540     {                                                                       \
541         strncat( p_capabilities, string " ",                                \
542                  sizeof(p_capabilities) - strlen(p_capabilities) );         \
543         p_capabilities[sizeof(p_capabilities) - 1] = '\0';                  \
544     }
545
546     p_capabilities[0] = '\0';
547     PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
548     PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
549     PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
550     PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
551     PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
552     PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
553     PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
554     PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
555     PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
556     PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
557     msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
558
559     /*
560      * Choose the best memcpy module
561      */
562     p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy", 0 );
563
564     if( p_vlc->pf_memcpy == NULL )
565     {
566         p_vlc->pf_memcpy = memcpy;
567     }
568
569     if( p_vlc->pf_memset == NULL )
570     {
571         p_vlc->pf_memset = memset;
572     }
573     
574     /* Check for daemon mode */
575     if( config_GetInt( p_vlc, "daemon" ) )
576     {
577         pid_t i_pid = 0;
578         if( ( i_pid = fork() ) < 0 )
579         {
580             msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
581             exit(1);
582         }
583         else if( i_pid )
584         {
585             /* This is the parent, exit right now */
586             msg_Dbg( p_vlc, "closing parent process" );
587             exit(0);
588         }
589         else
590         {
591             /* we are the child */
592             msg_Dbg( p_vlc, "we are the child !!!" );
593             close( 0 );
594             close( 1 );
595             close( 2 );
596
597             p_vlc->p_libvlc->b_daemon = VLC_TRUE;
598             var_Create( p_vlc, "interface", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
599             var_SetString( p_vlc, "interface", "dummy" );
600             VLC_AddIntf( 0, "logger,none", VLC_FALSE, VLC_FALSE );
601         }
602     }
603
604     /*
605      * Initialize hotkey handling
606      */
607     var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
608     p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
609     /* Do a copy (we don't need to modify the strings) */
610     memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
611
612     /*
613      * Initialize playlist and get commandline files
614      */
615     p_playlist = playlist_Create( p_vlc );
616     if( !p_playlist )
617     {
618         msg_Err( p_vlc, "playlist initialization failed" );
619         if( p_vlc->p_memcpy_module != NULL )
620         {
621             module_Unneed( p_vlc, p_vlc->p_memcpy_module );
622         }
623         module_EndBank( p_vlc );
624         if( i_object ) vlc_object_release( p_vlc );
625         return VLC_EGENERIC;
626     }
627
628     /*
629      * Load background interfaces
630      */
631     psz_modules = config_GetPsz( p_vlc, "extraintf" );
632     psz_parser = psz_modules;
633     while ( psz_parser && *psz_parser )
634     {
635         char *psz_module, *psz_temp;
636         psz_module = psz_parser;
637         psz_parser = strchr( psz_module, ',' );
638         if ( psz_parser )
639         {
640             *psz_parser = '\0';
641             psz_parser++;
642         }
643         psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
644         if( psz_temp )
645         {
646             sprintf( psz_temp, "%s,none", psz_module );
647             VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
648             free( psz_temp );
649         }
650     }
651     if ( psz_modules )
652     {
653         free( psz_modules );
654     }
655
656     /*
657      * Allways load the hotkeys interface if it exists
658      */
659     VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
660
661     /*
662      * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
663      */
664     var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
665     var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
666     var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
667     var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
668     var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
669     var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
670     var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
671     var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
672     var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
673     var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
674     var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
675     var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
676
677     /*
678      * Get input filenames given as commandline arguments
679      */
680     GetFilenames( p_vlc, i_argc, ppsz_argv );
681
682     if( i_object ) vlc_object_release( p_vlc );
683     return VLC_SUCCESS;
684 }
685
686 /*****************************************************************************
687  * VLC_AddIntf: add an interface
688  *****************************************************************************
689  * This function opens an interface plugin and runs it. If b_block is set
690  * to 0, VLC_AddIntf will return immediately and let the interface run in a
691  * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
692  * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
693  * the playlist when it is completely initialised.
694  *****************************************************************************/
695 int VLC_AddIntf( int i_object, char const *psz_module,
696                  vlc_bool_t b_block, vlc_bool_t b_play )
697 {
698     int i_err;
699     intf_thread_t *p_intf;
700     vlc_t *p_vlc = vlc_current_object( i_object );
701
702     if( !p_vlc )
703     {
704         return VLC_ENOOBJ;
705     }
706
707     /* Try to create the interface */
708     p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
709
710     if( p_intf == NULL )
711     {
712         msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
713         if( i_object ) vlc_object_release( p_vlc );
714         return VLC_EGENERIC;
715     }
716
717     /* Interface doesn't handle play on start so do it ourselves */
718     if( !p_intf->b_play && b_play ) VLC_Play( i_object );
719
720     /* Try to run the interface */
721     p_intf->b_play = b_play;
722     p_intf->b_block = b_block;
723     i_err = intf_RunThread( p_intf );
724     if( i_err )
725     {
726         vlc_object_detach( p_intf );
727         intf_Destroy( p_intf );
728         if( i_object ) vlc_object_release( p_vlc );
729         return i_err;
730     }
731
732     if( i_object ) vlc_object_release( p_vlc );
733     return VLC_SUCCESS;
734 }
735
736 /*****************************************************************************
737  * VLC_Die: ask vlc to die.
738  *****************************************************************************
739  * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
740  * task. It is your duty to call VLC_CleanUp and VLC_Destroy afterwards.
741  *****************************************************************************/
742 int VLC_Die( int i_object )
743 {
744     vlc_t *p_vlc = vlc_current_object( i_object );
745
746     if( !p_vlc )
747     {
748         return VLC_ENOOBJ;
749     }
750
751     p_vlc->b_die = VLC_TRUE;
752
753     if( i_object ) vlc_object_release( p_vlc );
754     return VLC_SUCCESS;
755 }
756
757 /*****************************************************************************
758  * VLC_CleanUp: CleanUp all the intf, playlist, vout, aout
759  *****************************************************************************/
760 int VLC_CleanUp( int i_object )
761 {
762     intf_thread_t      * p_intf;
763     playlist_t         * p_playlist;
764     vout_thread_t      * p_vout;
765     aout_instance_t    * p_aout;
766     announce_handler_t * p_announce;
767     vlc_t *p_vlc = vlc_current_object( i_object );
768
769     /* Check that the handle is valid */
770     if( !p_vlc )
771     {
772         return VLC_ENOOBJ;
773     }
774
775     /*
776      * Ask the interfaces to stop and destroy them
777      */
778     msg_Dbg( p_vlc, "removing all interfaces" );
779     while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
780     {
781         intf_StopThread( p_intf );
782         vlc_object_detach( p_intf );
783         vlc_object_release( p_intf );
784         intf_Destroy( p_intf );
785     }
786
787     /*
788      * Free playlists
789      */
790     msg_Dbg( p_vlc, "removing all playlists" );
791     while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
792                                           FIND_CHILD )) )
793     {
794         vlc_object_detach( p_playlist );
795         vlc_object_release( p_playlist );
796         playlist_Destroy( p_playlist );
797     }
798
799     /*
800      * Free video outputs
801      */
802     msg_Dbg( p_vlc, "removing all video outputs" );
803     while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
804     {
805         vlc_object_detach( p_vout );
806         vlc_object_release( p_vout );
807         vout_Destroy( p_vout );
808     }
809
810     /*
811      * Free audio outputs
812      */
813     msg_Dbg( p_vlc, "removing all audio outputs" );
814     while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
815     {
816         vlc_object_detach( (vlc_object_t *)p_aout );
817         vlc_object_release( (vlc_object_t *)p_aout );
818         aout_Delete( p_aout );
819     }
820
821     /*
822      * Free announce handler(s?)
823      */
824     msg_Dbg( p_vlc, "removing announce handler" );
825     while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
826                                                  FIND_CHILD ) ) )
827    {
828         vlc_object_detach( p_announce );
829         vlc_object_release( p_announce );
830         announce_HandlerDestroy( p_announce );
831    }
832
833     if( i_object ) vlc_object_release( p_vlc );
834     return VLC_SUCCESS;
835 }
836
837 /*****************************************************************************
838  * VLC_Destroy: Destroy everything.
839  *****************************************************************************
840  * This function requests the running threads to finish, waits for their
841  * termination, and destroys their structure.
842  *****************************************************************************/
843 int VLC_Destroy( int i_object )
844 {
845     vlc_t *p_vlc = vlc_current_object( i_object );
846
847     if( !p_vlc )
848     {
849         return VLC_ENOOBJ;
850     }
851
852     /*
853      * Free allocated memory
854      */
855     if( p_vlc->p_memcpy_module )
856     {
857         module_Unneed( p_vlc, p_vlc->p_memcpy_module );
858         p_vlc->p_memcpy_module = NULL;
859     }
860
861     /*
862      * Free module bank !
863      */
864     module_EndBank( p_vlc );
865
866     if( p_vlc->psz_homedir )
867     {
868         free( p_vlc->psz_homedir );
869         p_vlc->psz_homedir = NULL;
870     }
871
872     if( p_vlc->psz_configfile )
873     {
874         free( p_vlc->psz_configfile );
875         p_vlc->psz_configfile = NULL;
876     }
877
878     if( p_vlc->p_hotkeys )
879     {
880         free( p_vlc->p_hotkeys );
881         p_vlc->p_hotkeys = NULL;
882     }
883
884     /*
885      * System specific cleaning code
886      */
887     system_End( p_vlc );
888
889     /* Destroy mutexes */
890     vlc_mutex_destroy( &p_vlc->config_lock );
891
892     vlc_object_detach( p_vlc );
893
894     /* Release object before destroying it */
895     if( i_object ) vlc_object_release( p_vlc );
896
897     vlc_object_destroy( p_vlc );
898
899     /* Stop thread system: last one out please shut the door! */
900     vlc_threads_end( p_libvlc );
901
902     return VLC_SUCCESS;
903 }
904
905 /*****************************************************************************
906  * VLC_VariableSet: set a vlc variable
907  *****************************************************************************/
908 int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
909 {
910     vlc_t *p_vlc = vlc_current_object( i_object );
911     int i_ret;
912
913     if( !p_vlc )
914     {
915         return VLC_ENOOBJ;
916     }
917
918     /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
919      * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
920     if( !strncmp( psz_var, "conf::", 6 ) )
921     {
922         module_config_t *p_item;
923         char const *psz_newvar = psz_var + 6;
924
925         p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
926
927         if( p_item )
928         {
929             switch( p_item->i_type )
930             {
931                 case CONFIG_ITEM_BOOL:
932                     config_PutInt( p_vlc, psz_newvar, value.b_bool );
933                     break;
934                 case CONFIG_ITEM_INTEGER:
935                     config_PutInt( p_vlc, psz_newvar, value.i_int );
936                     break;
937                 case CONFIG_ITEM_FLOAT:
938                     config_PutFloat( p_vlc, psz_newvar, value.f_float );
939                     break;
940                 default:
941                     config_PutPsz( p_vlc, psz_newvar, value.psz_string );
942                     break;
943             }
944             if( i_object ) vlc_object_release( p_vlc );
945             return VLC_SUCCESS;
946         }
947     }
948
949     i_ret = var_Set( p_vlc, psz_var, value );
950
951     if( i_object ) vlc_object_release( p_vlc );
952     return i_ret;
953 }
954
955 /*****************************************************************************
956  * VLC_VariableGet: get a vlc variable
957  *****************************************************************************/
958 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
959 {
960     vlc_t *p_vlc = vlc_current_object( i_object );
961     int i_ret;
962
963     if( !p_vlc )
964     {
965         return VLC_ENOOBJ;
966     }
967
968     i_ret = var_Get( p_vlc, psz_var, p_value );
969
970     if( i_object ) vlc_object_release( p_vlc );
971     return i_ret;
972 }
973
974 /*****************************************************************************
975  * VLC_AddTarget: adds a target for playing.
976  *****************************************************************************
977  * This function adds psz_target to the current playlist. If a playlist does
978  * not exist, it will create one.
979  *****************************************************************************/
980 int VLC_AddTarget( int i_object, char const *psz_target,
981                    char const **ppsz_options, int i_options,
982                    int i_mode, int i_pos )
983 {
984     int i_err;
985     playlist_t *p_playlist;
986     vlc_t *p_vlc = vlc_current_object( i_object );
987
988     if( !p_vlc )
989     {
990         return VLC_ENOOBJ;
991     }
992
993     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
994
995     if( p_playlist == NULL )
996     {
997         msg_Dbg( p_vlc, "no playlist present, creating one" );
998         p_playlist = playlist_Create( p_vlc );
999
1000         if( p_playlist == NULL )
1001         {
1002             if( i_object ) vlc_object_release( p_vlc );
1003             return VLC_EGENERIC;
1004         }
1005
1006         vlc_object_yield( p_playlist );
1007     }
1008
1009     i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
1010                              i_mode, i_pos, -1, ppsz_options, i_options);
1011
1012     vlc_object_release( p_playlist );
1013
1014     if( i_object ) vlc_object_release( p_vlc );
1015     return i_err;
1016 }
1017
1018 /*****************************************************************************
1019  * VLC_Play: play
1020  *****************************************************************************/
1021 int VLC_Play( int i_object )
1022 {
1023     playlist_t * p_playlist;
1024     vlc_t *p_vlc = vlc_current_object( i_object );
1025
1026     /* Check that the handle is valid */
1027     if( !p_vlc )
1028     {
1029         return VLC_ENOOBJ;
1030     }
1031
1032     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1033
1034     if( !p_playlist )
1035     {
1036         if( i_object ) vlc_object_release( p_vlc );
1037         return VLC_ENOOBJ;
1038     }
1039
1040     playlist_Play( p_playlist );
1041     vlc_object_release( p_playlist );
1042
1043     if( i_object ) vlc_object_release( p_vlc );
1044     return VLC_SUCCESS;
1045 }
1046
1047 /*****************************************************************************
1048  * VLC_Pause: toggle pause
1049  *****************************************************************************/
1050 int VLC_Pause( int i_object )
1051 {
1052     playlist_t * p_playlist;
1053     vlc_t *p_vlc = vlc_current_object( i_object );
1054
1055     /* Check that the handle is valid */
1056     if( !p_vlc )
1057     {
1058         return VLC_ENOOBJ;
1059     }
1060
1061     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1062
1063     if( !p_playlist )
1064     {
1065         if( i_object ) vlc_object_release( p_vlc );
1066         return VLC_ENOOBJ;
1067     }
1068
1069     playlist_Pause( p_playlist );
1070     vlc_object_release( p_playlist );
1071
1072     if( i_object ) vlc_object_release( p_vlc );
1073     return VLC_SUCCESS;
1074 }
1075
1076 /*****************************************************************************
1077  * VLC_Pause: toggle pause
1078  *****************************************************************************/
1079 int VLC_Stop( int i_object )
1080 {
1081     playlist_t * p_playlist;
1082     vlc_t *p_vlc = vlc_current_object( i_object );
1083
1084     /* Check that the handle is valid */
1085     if( !p_vlc )
1086     {
1087         return VLC_ENOOBJ;
1088     }
1089
1090     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1091
1092     if( !p_playlist )
1093     {
1094         if( i_object ) vlc_object_release( p_vlc );
1095         return VLC_ENOOBJ;
1096     }
1097
1098     playlist_Stop( p_playlist );
1099     vlc_object_release( p_playlist );
1100
1101     if( i_object ) vlc_object_release( p_vlc );
1102     return VLC_SUCCESS;
1103 }
1104
1105 /*****************************************************************************
1106  * VLC_IsPlaying: Query for Playlist Status
1107  *****************************************************************************/
1108 vlc_bool_t VLC_IsPlaying( int i_object )
1109 {
1110     playlist_t * p_playlist;
1111     vlc_bool_t   b_playing;
1112
1113     vlc_t *p_vlc = vlc_current_object( i_object );
1114
1115     /* Check that the handle is valid */
1116     if( !p_vlc )
1117     {
1118         return VLC_ENOOBJ;
1119     }
1120
1121     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1122
1123     if( !p_playlist )
1124     {
1125         if( i_object ) vlc_object_release( p_vlc );
1126         return VLC_ENOOBJ;
1127     }
1128
1129     b_playing = playlist_IsPlaying( p_playlist );
1130     vlc_object_release( p_playlist );
1131
1132     if( i_object ) vlc_object_release( p_vlc );
1133     return b_playing;
1134 }
1135
1136 /**
1137  * Get the current position in a input
1138  *
1139  * Return the current position as a float
1140  * \note For some inputs, this will be unknown.
1141  *
1142  * \param i_object a vlc object id
1143  * \return a float in the range of 0.0 - 1.0
1144  */
1145 float VLC_PositionGet( int i_object )
1146 {
1147     input_thread_t *p_input;
1148     vlc_value_t val;
1149     vlc_t *p_vlc = vlc_current_object( i_object );
1150
1151     /* Check that the handle is valid */
1152     if( !p_vlc )
1153     {
1154         return VLC_ENOOBJ;
1155     }
1156
1157     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1158
1159     if( !p_input )
1160     {
1161         if( i_object ) vlc_object_release( p_vlc );
1162         return VLC_ENOOBJ;
1163     }
1164
1165     var_Get( p_input, "position", &val );
1166     vlc_object_release( p_input );
1167
1168     if( i_object ) vlc_object_release( p_vlc );
1169     return val.f_float;
1170 }
1171
1172 /**
1173  * Set the current position in a input
1174  *
1175  * Set the current position in a input and then return
1176  * the current position as a float.
1177  * \note For some inputs, this will be unknown.
1178  *
1179  * \param i_object a vlc object id
1180  * \param i_position a float in the range of 0.0 - 1.0
1181  * \return a float in the range of 0.0 - 1.0
1182  */
1183 float VLC_PositionSet( int i_object, float i_position )
1184 {
1185     input_thread_t *p_input;
1186     vlc_value_t val;
1187     vlc_t *p_vlc = vlc_current_object( i_object );
1188
1189     /* Check that the handle is valid */
1190     if( !p_vlc )
1191     {
1192         return VLC_ENOOBJ;
1193     }
1194
1195     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1196
1197     if( !p_input )
1198     {
1199         if( i_object ) vlc_object_release( p_vlc );
1200         return VLC_ENOOBJ;
1201     }
1202
1203     val.f_float = i_position;
1204     var_Set( p_input, "position", val );
1205     var_Get( p_input, "position", &val );
1206     vlc_object_release( p_input );
1207
1208     if( i_object ) vlc_object_release( p_vlc );
1209     return val.f_float;
1210 }
1211
1212 /**
1213  * Get the current position in a input
1214  *
1215  * Return the current position in seconds from the start.
1216  * \note For some inputs, this will be unknown.
1217  *
1218  * \param i_object a vlc object id
1219  * \return the offset from 0:00 in seconds
1220  */
1221 int VLC_TimeGet( int i_object )
1222 {
1223     input_thread_t *p_input;
1224     vlc_value_t val;
1225     vlc_t *p_vlc = vlc_current_object( i_object );
1226
1227     /* Check that the handle is valid */
1228     if( !p_vlc )
1229     {
1230         return VLC_ENOOBJ;
1231     }
1232
1233     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1234
1235     if( !p_input )
1236     {
1237         if( i_object ) vlc_object_release( p_vlc );
1238         return VLC_ENOOBJ;
1239     }
1240
1241     var_Get( p_input, "time", &val );
1242     vlc_object_release( p_input );
1243
1244     if( i_object ) vlc_object_release( p_vlc );
1245     return val.i_time  / 1000000;
1246 }
1247
1248 /**
1249  * Seek to a position in the current input
1250  *
1251  * Seek i_seconds in the current input. If b_relative is set,
1252  * then the seek will be relative to the current position, otherwise
1253  * it will seek to i_seconds from the beginning of the input.
1254  * \note For some inputs, this will be unknown.
1255  *
1256  * \param i_object a vlc object id
1257  * \param i_seconds seconds from current position or from beginning of input
1258  * \param b_relative seek relative from current position
1259  * \return VLC_SUCCESS on success
1260  */
1261 int VLC_TimeSet( int i_object, int i_seconds, vlc_bool_t b_relative )
1262 {
1263     input_thread_t *p_input;
1264     vlc_value_t val;
1265     vlc_t *p_vlc = vlc_current_object( i_object );
1266
1267     /* Check that the handle is valid */
1268     if( !p_vlc )
1269     {
1270         return VLC_ENOOBJ;
1271     }
1272
1273     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1274
1275     if( !p_input )
1276     {
1277         if( i_object ) vlc_object_release( p_vlc );
1278         return VLC_ENOOBJ;
1279     }
1280
1281     if( b_relative )
1282     {
1283         val.i_time = i_seconds * 1000000;
1284         var_Set( p_input, "time-offset", val );
1285     }
1286     else
1287     {
1288         val.i_time = i_seconds * 1000000;
1289         var_Set( p_input, "time", val );
1290     }
1291     vlc_object_release( p_input );
1292
1293     if( i_object ) vlc_object_release( p_vlc );
1294     return VLC_SUCCESS;
1295 }
1296
1297 /**
1298  * Get the total length of a input
1299  *
1300  * Return the total length in seconds from the current input.
1301  * \note For some inputs, this will be unknown.
1302  *
1303  * \param i_object a vlc object id
1304  * \return the length in seconds
1305  */
1306 int VLC_LengthGet( int i_object )
1307 {
1308     input_thread_t *p_input;
1309     vlc_value_t val;
1310     vlc_t *p_vlc = vlc_current_object( i_object );
1311
1312     /* Check that the handle is valid */
1313     if( !p_vlc )
1314     {
1315         return VLC_ENOOBJ;
1316     }
1317
1318     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1319
1320     if( !p_input )
1321     {
1322         if( i_object ) vlc_object_release( p_vlc );
1323         return VLC_ENOOBJ;
1324     }
1325
1326     var_Get( p_input, "length", &val );
1327     vlc_object_release( p_input );
1328
1329     if( i_object ) vlc_object_release( p_vlc );
1330     return val.i_time  / 1000000;
1331 }
1332
1333 /**
1334  * Play the input faster than realtime
1335  *
1336  * 2x, 4x, 8x faster than realtime
1337  * \note For some inputs, this will be impossible.
1338  *
1339  * \param i_object a vlc object id
1340  * \return the current speedrate
1341  */
1342 float VLC_SpeedFaster( int i_object )
1343 {
1344     input_thread_t *p_input;
1345     vlc_value_t val;
1346     vlc_t *p_vlc = vlc_current_object( i_object );
1347
1348     /* Check that the handle is valid */
1349     if( !p_vlc )
1350     {
1351         return VLC_ENOOBJ;
1352     }
1353
1354     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1355
1356     if( !p_input )
1357     {
1358         if( i_object ) vlc_object_release( p_vlc );
1359         return VLC_ENOOBJ;
1360     }
1361
1362     val.b_bool = VLC_TRUE;
1363     var_Set( p_input, "rate-faster", val );
1364     var_Get( p_input, "rate", &val );
1365     vlc_object_release( p_input );
1366
1367     if( i_object ) vlc_object_release( p_vlc );
1368     return val.f_float / INPUT_RATE_DEFAULT;
1369 }
1370
1371 /**
1372  * Play the input slower than realtime
1373  *
1374  * 1/2x, 1/4x, 1/8x slower than realtime
1375  * \note For some inputs, this will be impossible.
1376  *
1377  * \param i_object a vlc object id
1378  * \return the current speedrate
1379  */
1380 float VLC_SpeedSlower( int i_object )
1381 {
1382     input_thread_t *p_input;
1383     vlc_value_t val;
1384     vlc_t *p_vlc = vlc_current_object( i_object );
1385
1386     /* Check that the handle is valid */
1387     if( !p_vlc )
1388     {
1389         return VLC_ENOOBJ;
1390     }
1391
1392     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1393
1394     if( !p_input )
1395     {
1396         if( i_object ) vlc_object_release( p_vlc );
1397         return VLC_ENOOBJ;
1398     }
1399
1400     val.b_bool = VLC_TRUE;
1401     var_Set( p_input, "rate-slower", val );
1402     var_Get( p_input, "rate", &val );
1403     vlc_object_release( p_input );
1404
1405     if( i_object ) vlc_object_release( p_vlc );
1406     return val.f_float / INPUT_RATE_DEFAULT;
1407 }
1408
1409 /**
1410  * Return the current playlist item
1411  *
1412  * Returns the index of the playlistitem that is currently selected for play.
1413  * This is valid even if nothing is currently playing.
1414  *
1415  * \param i_object a vlc object id
1416  * \return the current index
1417  */
1418 int VLC_PlaylistIndex( int i_object )
1419 {
1420     int i_index;
1421     playlist_t * p_playlist;
1422     vlc_t *p_vlc = vlc_current_object( i_object );
1423
1424     /* Check that the handle is valid */
1425     if( !p_vlc )
1426     {
1427         return VLC_ENOOBJ;
1428     }
1429
1430     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1431
1432     if( !p_playlist )
1433     {
1434         if( i_object ) vlc_object_release( p_vlc );
1435         return VLC_ENOOBJ;
1436     }
1437
1438     i_index = p_playlist->i_index;
1439     vlc_object_release( p_playlist );
1440
1441     if( i_object ) vlc_object_release( p_vlc );
1442     return i_index;
1443 }
1444
1445 /**
1446  * Total amount of items in the playlist
1447  *
1448  * \param i_object a vlc object id
1449  * \return amount of playlist items
1450  */
1451 int VLC_PlaylistNumberOfItems( int i_object )
1452 {
1453     int i_size;
1454     playlist_t * p_playlist;
1455     vlc_t *p_vlc = vlc_current_object( i_object );
1456
1457     /* Check that the handle is valid */
1458     if( !p_vlc )
1459     {
1460         return VLC_ENOOBJ;
1461     }
1462
1463     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1464
1465     if( !p_playlist )
1466     {
1467         if( i_object ) vlc_object_release( p_vlc );
1468         return VLC_ENOOBJ;
1469     }
1470
1471     i_size = p_playlist->i_size;
1472     vlc_object_release( p_playlist );
1473
1474     if( i_object ) vlc_object_release( p_vlc );
1475     return i_size;
1476 }
1477
1478 /**
1479  * Next playlist item
1480  *
1481  * Skip to the next playlistitem and play it.
1482  *
1483  * \param i_object a vlc object id
1484  * \return VLC_SUCCESS on success
1485  */
1486 int VLC_PlaylistNext( int i_object )
1487 {
1488     playlist_t * p_playlist;
1489     vlc_t *p_vlc = vlc_current_object( i_object );
1490
1491     /* Check that the handle is valid */
1492     if( !p_vlc )
1493     {
1494         return VLC_ENOOBJ;
1495     }
1496
1497     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1498
1499     if( !p_playlist )
1500     {
1501         if( i_object ) vlc_object_release( p_vlc );
1502         return VLC_ENOOBJ;
1503     }
1504
1505     playlist_Next( p_playlist );
1506     vlc_object_release( p_playlist );
1507
1508     if( i_object ) vlc_object_release( p_vlc );
1509     return VLC_SUCCESS;
1510 }
1511
1512 /**
1513  * Previous playlist item
1514  *
1515  * Skip to the previous playlistitem and play it.
1516  *
1517  * \param i_object a vlc object id
1518  * \return VLC_SUCCESS on success
1519  */
1520 int VLC_PlaylistPrev( int i_object )
1521 {
1522     playlist_t * p_playlist;
1523     vlc_t *p_vlc = vlc_current_object( i_object );
1524
1525     /* Check that the handle is valid */
1526     if( !p_vlc )
1527     {
1528         return VLC_ENOOBJ;
1529     }
1530
1531     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1532
1533     if( !p_playlist )
1534     {
1535         if( i_object ) vlc_object_release( p_vlc );
1536         return VLC_ENOOBJ;
1537     }
1538
1539     playlist_Prev( p_playlist );
1540     vlc_object_release( p_playlist );
1541
1542     if( i_object ) vlc_object_release( p_vlc );
1543     return VLC_SUCCESS;
1544 }
1545
1546
1547 /*****************************************************************************
1548  * VLC_PlaylistClear: Empty the playlist
1549  *****************************************************************************/
1550 int VLC_PlaylistClear( int i_object )
1551 {
1552     int i_err;
1553     playlist_t * p_playlist;
1554     vlc_t *p_vlc = vlc_current_object( i_object );
1555
1556     /* Check that the handle is valid */
1557     if( !p_vlc )
1558     {
1559         return VLC_ENOOBJ;
1560     }
1561
1562     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1563
1564     if( !p_playlist )
1565     {
1566         if( i_object ) vlc_object_release( p_vlc );
1567         return VLC_ENOOBJ;
1568     }
1569
1570     i_err = playlist_Clear( p_playlist );
1571
1572     vlc_object_release( p_playlist );
1573
1574     if( i_object ) vlc_object_release( p_vlc );
1575     return i_err;
1576 }
1577
1578 /**
1579  * Change the volume
1580  *
1581  * \param i_object a vlc object id
1582  * \param i_volume something in a range from 0-200
1583  * \return the new volume (range 0-200 %)
1584  */
1585 int VLC_VolumeSet( int i_object, int i_volume )
1586 {
1587     audio_volume_t i_vol = 0;
1588     vlc_t *p_vlc = vlc_current_object( i_object );
1589
1590     /* Check that the handle is valid */
1591     if( !p_vlc )
1592     {
1593         return VLC_ENOOBJ;
1594     }
1595
1596     if( i_volume >= 0 && i_volume <= 200 )
1597     {
1598         i_vol = i_volume * AOUT_VOLUME_MAX / 200;
1599         aout_VolumeSet( p_vlc, i_vol );
1600     }
1601
1602     if( i_object ) vlc_object_release( p_vlc );
1603     return i_vol * 200 / AOUT_VOLUME_MAX;
1604 }
1605
1606 /**
1607  * Get the current volume
1608  *
1609  * Retrieve the current volume.
1610  *
1611  * \param i_object a vlc object id
1612  * \return the current volume (range 0-200 %)
1613  */
1614 int VLC_VolumeGet( int i_object )
1615 {
1616     audio_volume_t i_volume;
1617     vlc_t *p_vlc = vlc_current_object( i_object );
1618
1619     /* Check that the handle is valid */
1620     if( !p_vlc )
1621     {
1622         return VLC_ENOOBJ;
1623     }
1624
1625     aout_VolumeGet( p_vlc, &i_volume );
1626
1627     if( i_object ) vlc_object_release( p_vlc );
1628     return i_volume*200/AOUT_VOLUME_MAX;
1629 }
1630
1631 /**
1632  * Mute/Unmute the volume
1633  *
1634  * \param i_object a vlc object id
1635  * \return VLC_SUCCESS on success
1636  */
1637 int VLC_VolumeMute( int i_object )
1638 {
1639     vlc_t *p_vlc = vlc_current_object( i_object );
1640
1641     /* Check that the handle is valid */
1642     if( !p_vlc )
1643     {
1644         return VLC_ENOOBJ;
1645     }
1646
1647     aout_VolumeMute( p_vlc, NULL );
1648
1649     if( i_object ) vlc_object_release( p_vlc );
1650     return VLC_SUCCESS;
1651 }
1652
1653 /*****************************************************************************
1654  * VLC_FullScreen: toggle fullscreen mode
1655  *****************************************************************************/
1656 int VLC_FullScreen( int i_object )
1657 {
1658     vout_thread_t *p_vout;
1659     vlc_t *p_vlc = vlc_current_object( i_object );
1660
1661     if( !p_vlc )
1662     {
1663         return VLC_ENOOBJ;
1664     }
1665
1666     p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1667
1668     if( !p_vout )
1669     {
1670         if( i_object ) vlc_object_release( p_vlc );
1671         return VLC_ENOOBJ;
1672     }
1673
1674     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1675     vlc_object_release( p_vout );
1676
1677     if( i_object ) vlc_object_release( p_vlc );
1678     return VLC_SUCCESS;
1679 }
1680
1681 /* following functions are local */
1682
1683 /*****************************************************************************
1684  * SetLanguage: set the interface language.
1685  *****************************************************************************
1686  * We set the LC_MESSAGES locale category for interface messages and buttons,
1687  * as well as the LC_CTYPE category for string sorting and possible wide
1688  * character support.
1689  *****************************************************************************/
1690 static void SetLanguage ( char const *psz_lang )
1691 {
1692 #if defined( ENABLE_NLS ) \
1693      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1694
1695     char *          psz_path;
1696 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1697     char            psz_tmp[1024];
1698 #endif
1699
1700     if( psz_lang && !*psz_lang )
1701     {
1702 #   if defined( HAVE_LC_MESSAGES )
1703         setlocale( LC_MESSAGES, psz_lang );
1704 #   endif
1705         setlocale( LC_CTYPE, psz_lang );
1706     }
1707     else if( psz_lang )
1708     {
1709 #ifdef SYS_DARWIN
1710         /* I need that under Darwin, please check it doesn't disturb
1711          * other platforms. --Meuuh */
1712         setenv( "LANG", psz_lang, 1 );
1713
1714 #elif defined( SYS_BEOS ) || defined( WIN32 )
1715         /* We set LC_ALL manually because it is the only way to set
1716          * the language at runtime under eg. Windows. Beware that this
1717          * makes the environment unconsistent when libvlc is unloaded and
1718          * should probably be moved to a safer place like vlc.c. */
1719         static char psz_lcall[20];
1720         snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1721         psz_lcall[19] = '\0';
1722         putenv( psz_lcall );
1723 #endif
1724
1725         setlocale( LC_ALL, psz_lang );
1726     }
1727
1728     /* Specify where to find the locales for current domain */
1729 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1730     psz_path = LOCALEDIR;
1731 #else
1732     snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1733               "locale" );
1734     psz_path = psz_tmp;
1735 #endif
1736     if( !bindtextdomain( PACKAGE, psz_path ) )
1737     {
1738         fprintf( stderr, "warning: no domain %s in directory %s\n",
1739                  PACKAGE, psz_path );
1740     }
1741
1742     /* Set the default domain */
1743     textdomain( PACKAGE );
1744
1745 #if defined( ENABLE_UTF8 )
1746     bind_textdomain_codeset( PACKAGE, "UTF-8" );
1747 #endif
1748
1749 #endif
1750 }
1751
1752 /*****************************************************************************
1753  * GetFilenames: parse command line options which are not flags
1754  *****************************************************************************
1755  * Parse command line for input files as well as their associated options.
1756  * An option always follows its associated input and begins with a ":".
1757  *****************************************************************************/
1758 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1759 {
1760     int i_opt, i_options;
1761
1762     /* We assume that the remaining parameters are filenames
1763      * and their input options */
1764     for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1765     {
1766         i_options = 0;
1767
1768         /* Count the input options */
1769         while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1770         {
1771             i_options++;
1772             i_opt--;
1773         }
1774
1775         /* TODO: write an internal function of this one, to avoid
1776          *       unnecessary lookups. */
1777         VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1778                        (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1779                                         NULL ), i_options,
1780                        PLAYLIST_INSERT, 0 );
1781     }
1782
1783     return VLC_SUCCESS;
1784 }
1785
1786 /*****************************************************************************
1787  * Usage: print program usage
1788  *****************************************************************************
1789  * Print a short inline help. Message interface is initialized at this stage.
1790  *****************************************************************************/
1791 static void Usage( vlc_t *p_this, char const *psz_module_name )
1792 {
1793 #define FORMAT_STRING "  %s --%s%s%s%s%s%s%s "
1794     /* short option ------'    |     | | | |  | |
1795      * option name ------------'     | | | |  | |
1796      * <bra -------------------------' | | |  | |
1797      * option type or "" --------------' | |  | |
1798      * ket> -----------------------------' |  | |
1799      * padding spaces ---------------------'  | |
1800      * comment -------------------------------' |
1801      * comment suffix --------------------------'
1802      *
1803      * The purpose of having bra and ket is that we might i18n them as well.
1804      */
1805 #define LINE_START 8
1806 #define PADDING_SPACES 25
1807     vlc_list_t *p_list;
1808     module_t *p_parser;
1809     module_config_t *p_item;
1810     char psz_spaces[PADDING_SPACES+LINE_START+1];
1811     char psz_format[sizeof(FORMAT_STRING)];
1812     char psz_buffer[1000];
1813     char psz_short[4];
1814     int i_index;
1815     int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1816     vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
1817
1818     memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1819     psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1820
1821     strcpy( psz_format, FORMAT_STRING );
1822
1823 #ifdef WIN32
1824     ShowConsole();
1825 #endif
1826
1827     /* List all modules */
1828     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1829
1830     /* Enumerate the config for each module */
1831     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1832     {
1833         vlc_bool_t b_help_module;
1834
1835         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1836
1837         if( psz_module_name && strcmp( psz_module_name,
1838                                        p_parser->psz_object_name ) )
1839         {
1840             continue;
1841         }
1842
1843         /* Ignore modules without config options */
1844         if( !p_parser->i_config_items )
1845         {
1846             continue;
1847         }
1848
1849         /* Ignore modules with only advanced config options if requested */
1850         if( !b_advanced )
1851         {
1852             for( p_item = p_parser->p_config;
1853                  p_item->i_type != CONFIG_HINT_END;
1854                  p_item++ )
1855             {
1856                 if( (p_item->i_type & CONFIG_ITEM) &&
1857                     !p_item->b_advanced ) break;
1858             }
1859             if( p_item->i_type == CONFIG_HINT_END ) continue;
1860         }
1861
1862         /* Print name of module */
1863         if( strcmp( "main", p_parser->psz_object_name ) )
1864         fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1865
1866         b_help_module = !strcmp( "help", p_parser->psz_object_name );
1867
1868         /* Print module options */
1869         for( p_item = p_parser->p_config;
1870              p_item->i_type != CONFIG_HINT_END;
1871              p_item++ )
1872         {
1873             char *psz_text;
1874             char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1875             char *psz_suf = "", *psz_prefix = NULL;
1876             int i;
1877
1878             /* Skip advanced options if requested */
1879             if( p_item->b_advanced && !b_advanced )
1880             {
1881                 continue;
1882             }
1883
1884             switch( p_item->i_type )
1885             {
1886             case CONFIG_HINT_CATEGORY:
1887             case CONFIG_HINT_USAGE:
1888                 if( !strcmp( "main", p_parser->psz_object_name ) )
1889                 fprintf( stdout, "\n %s\n", p_item->psz_text );
1890                 break;
1891
1892             case CONFIG_ITEM_STRING:
1893             case CONFIG_ITEM_FILE:
1894             case CONFIG_ITEM_DIRECTORY:
1895             case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1896                 if( !p_item->ppsz_list )
1897                 {
1898                     psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1899                     break;
1900                 }
1901                 else
1902                 {
1903                     psz_bra = " {";
1904                     psz_type = psz_buffer;
1905                     psz_type[0] = '\0';
1906                     for( i = 0; p_item->ppsz_list[i]; i++ )
1907                     {
1908                         if( i ) strcat( psz_type, "," );
1909                         strcat( psz_type, p_item->ppsz_list[i] );
1910                     }
1911                     psz_ket = "}";
1912                     break;
1913                 }
1914             case CONFIG_ITEM_INTEGER:
1915             case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1916                 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1917                 break;
1918             case CONFIG_ITEM_FLOAT:
1919                 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1920                 break;
1921             case CONFIG_ITEM_BOOL:
1922                 psz_bra = ""; psz_type = ""; psz_ket = "";
1923                 if( !b_help_module )
1924                 {
1925                     psz_suf = p_item->i_value ? _(" (default enabled)") :
1926                                                 _(" (default disabled)");
1927                 }
1928                 break;
1929             }
1930
1931             if( !psz_type )
1932             {
1933                 continue;
1934             }
1935
1936             /* Add short option if any */
1937             if( p_item->i_short )
1938             {
1939                 sprintf( psz_short, "-%c,", p_item->i_short );
1940             }
1941             else
1942             {
1943                 strcpy( psz_short, "   " );
1944             }
1945
1946             i = PADDING_SPACES - strlen( p_item->psz_name )
1947                  - strlen( psz_bra ) - strlen( psz_type )
1948                  - strlen( psz_ket ) - 1;
1949
1950             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1951             {
1952                 /* If option is of type --foo-bar, we print its counterpart
1953                  * as --no-foo-bar, but if it is of type --foobar (without
1954                  * dashes in the name) we print it as --nofoobar. Both
1955                  * values are of course valid, only the display changes. */
1956                 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1957                                                              : ", --no";
1958                 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1959             }
1960
1961             if( i < 0 )
1962             {
1963                 psz_spaces[0] = '\n';
1964                 i = 0;
1965             }
1966             else
1967             {
1968                 psz_spaces[i] = '\0';
1969             }
1970
1971             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1972             {
1973                 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1974                          psz_prefix, p_item->psz_name, psz_bra, psz_type,
1975                          psz_ket, psz_spaces );
1976             }
1977             else
1978             {
1979                 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1980                          "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1981             }
1982
1983             psz_spaces[i] = ' ';
1984
1985             /* We wrap the rest of the output */
1986             sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1987             psz_text = psz_buffer;
1988             while( *psz_text )
1989             {
1990                 char *psz_parser, *psz_word;
1991                 int i_end = strlen( psz_text );
1992
1993                 /* If the remaining text fits in a line, print it. */
1994                 if( i_end <= i_width )
1995                 {
1996                     fprintf( stdout, "%s\n", psz_text );
1997                     break;
1998                 }
1999
2000                 /* Otherwise, eat as many words as possible */
2001                 psz_parser = psz_text;
2002                 do
2003                 {
2004                     psz_word = psz_parser;
2005                     psz_parser = strchr( psz_word, ' ' );
2006                     /* If no space was found, we reached the end of the text
2007                      * block; otherwise, we skip the space we just found. */
2008                     psz_parser = psz_parser ? psz_parser + 1
2009                                             : psz_text + i_end;
2010
2011                 } while( psz_parser - psz_text <= i_width );
2012
2013                 /* We cut a word in one of these cases:
2014                  *  - it's the only word in the line and it's too long.
2015                  *  - we used less than 80% of the width and the word we are
2016                  *    going to wrap is longer than 40% of the width, and even
2017                  *    if the word would have fit in the next line. */
2018                 if( psz_word == psz_text
2019                      || ( psz_word - psz_text < 80 * i_width / 100
2020                            && psz_parser - psz_word > 40 * i_width / 100 ) )
2021                 {
2022                     char c = psz_text[i_width];
2023                     psz_text[i_width] = '\0';
2024                     fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2025                     psz_text += i_width;
2026                     psz_text[0] = c;
2027                 }
2028                 else
2029                 {
2030                     psz_word[-1] = '\0';
2031                     fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2032                     psz_text = psz_word;
2033                 }
2034             }
2035         }
2036     }
2037
2038     /* Release the module list */
2039     vlc_list_release( p_list );
2040
2041 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
2042     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
2043     getchar();
2044 #endif
2045 }
2046
2047 /*****************************************************************************
2048  * ListModules: list the available modules with their description
2049  *****************************************************************************
2050  * Print a list of all available modules (builtins and plugins) and a short
2051  * description for each one.
2052  *****************************************************************************/
2053 static void ListModules( vlc_t *p_this )
2054 {
2055     vlc_list_t *p_list;
2056     module_t *p_parser;
2057     char psz_spaces[22];
2058     int i_index;
2059
2060     memset( psz_spaces, ' ', 22 );
2061
2062 #ifdef WIN32
2063     ShowConsole();
2064 #endif
2065
2066     /* Usage */
2067     fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
2068                      p_this->p_vlc->psz_object_name );
2069
2070     fprintf( stdout, _("[module]              [description]\n") );
2071
2072     /* List all modules */
2073     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2074
2075     /* Enumerate each module */
2076     for( i_index = 0; i_index < p_list->i_count; i_index++ )
2077     {
2078         int i;
2079
2080         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2081
2082         /* Nasty hack, but right now I'm too tired to think about a nice
2083          * solution */
2084         i = 22 - strlen( p_parser->psz_object_name ) - 1;
2085         if( i < 0 ) i = 0;
2086         psz_spaces[i] = 0;
2087
2088         fprintf( stdout, "  %s%s %s\n", p_parser->psz_object_name,
2089                          psz_spaces, p_parser->psz_longname );
2090
2091         psz_spaces[i] = ' ';
2092     }
2093
2094     vlc_list_release( p_list );
2095
2096 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
2097     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
2098     getchar();
2099 #endif
2100 }
2101
2102 /*****************************************************************************
2103  * Version: print complete program version
2104  *****************************************************************************
2105  * Print complete program version and build number.
2106  *****************************************************************************/
2107 static void Version( void )
2108 {
2109 #ifdef WIN32
2110     ShowConsole();
2111 #endif
2112
2113     fprintf( stdout, VERSION_MESSAGE "\n" );
2114     fprintf( stdout,
2115       _("This program comes with NO WARRANTY, to the extent permitted by "
2116         "law.\nYou may redistribute it under the terms of the GNU General "
2117         "Public License;\nsee the file named COPYING for details.\n"
2118         "Written by the VideoLAN team; see the AUTHORS file.\n") );
2119
2120 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
2121     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
2122     getchar();
2123 #endif
2124 }
2125
2126 /*****************************************************************************
2127  * ShowConsole: On Win32, create an output console for debug messages
2128  *****************************************************************************
2129  * This function is useful only on Win32.
2130  *****************************************************************************/
2131 #ifdef WIN32 /*  */
2132 static void ShowConsole( void )
2133 {
2134 #   ifndef UNDER_CE
2135     AllocConsole();
2136     freopen( "CONOUT$", "w", stdout );
2137     freopen( "CONOUT$", "w", stderr );
2138     freopen( "CONIN$", "r", stdin );
2139 #   endif
2140     return;
2141 }
2142 #endif
2143
2144 /*****************************************************************************
2145  * ConsoleWidth: Return the console width in characters
2146  *****************************************************************************
2147  * We use the stty shell command to get the console width; if this fails or
2148  * if the width is less than 80, we default to 80.
2149  *****************************************************************************/
2150 static int ConsoleWidth( void )
2151 {
2152     int i_width = 80;
2153
2154 #ifndef WIN32
2155     char buf[20], *psz_parser;
2156     FILE *file;
2157     int i_ret;
2158
2159     file = popen( "stty size 2>/dev/null", "r" );
2160     if( file )
2161     {
2162         i_ret = fread( buf, 1, 20, file );
2163         if( i_ret > 0 )
2164         {
2165             buf[19] = '\0';
2166             psz_parser = strchr( buf, ' ' );
2167             if( psz_parser )
2168             {
2169                 i_ret = atoi( psz_parser + 1 );
2170                 if( i_ret >= 80 )
2171                 {
2172                     i_width = i_ret;
2173                 }
2174             }
2175         }
2176
2177         pclose( file );
2178     }
2179 #endif
2180
2181     return i_width;
2182 }
2183
2184 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2185                      vlc_value_t old_val, vlc_value_t new_val, void *param)
2186 {
2187     vlc_t *p_vlc = (vlc_t *)p_this;
2188
2189     if( new_val.i_int >= -1 )
2190     {
2191         p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
2192     }
2193     return VLC_SUCCESS;
2194 }