]> git.sesse.net Git - vlc/blob - src/libvlc.c
* first stab at vlc daemon mode (-d, --daemon )
[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             //VLC_AddIntf( 0, "logger,none", VLC_FALSE, VLC_FALSE );
599         }
600     }
601
602     /*
603      * Initialize hotkey handling
604      */
605     var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
606     p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
607     /* Do a copy (we don't need to modify the strings) */
608     memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
609
610     /*
611      * Initialize playlist and get commandline files
612      */
613     p_playlist = playlist_Create( p_vlc );
614     if( !p_playlist )
615     {
616         msg_Err( p_vlc, "playlist initialization failed" );
617         if( p_vlc->p_memcpy_module != NULL )
618         {
619             module_Unneed( p_vlc, p_vlc->p_memcpy_module );
620         }
621         module_EndBank( p_vlc );
622         if( i_object ) vlc_object_release( p_vlc );
623         return VLC_EGENERIC;
624     }
625
626     /*
627      * Load background interfaces
628      */
629     psz_modules = config_GetPsz( p_vlc, "extraintf" );
630     psz_parser = psz_modules;
631     while ( psz_parser && *psz_parser )
632     {
633         char *psz_module, *psz_temp;
634         psz_module = psz_parser;
635         psz_parser = strchr( psz_module, ',' );
636         if ( psz_parser )
637         {
638             *psz_parser = '\0';
639             psz_parser++;
640         }
641         psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
642         if( psz_temp )
643         {
644             sprintf( psz_temp, "%s,none", psz_module );
645             VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
646             free( psz_temp );
647         }
648     }
649     if ( psz_modules )
650     {
651         free( psz_modules );
652     }
653
654     /*
655      * Allways load the hotkeys interface if it exists
656      */
657     VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
658
659     /*
660      * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
661      */
662     var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
663     var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
664     var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
665     var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
666     var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
667     var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
668     var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
669     var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
670     var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
671     var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
672     var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
673     var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
674
675     /*
676      * Get input filenames given as commandline arguments
677      */
678     GetFilenames( p_vlc, i_argc, ppsz_argv );
679
680     if( i_object ) vlc_object_release( p_vlc );
681     return VLC_SUCCESS;
682 }
683
684 /*****************************************************************************
685  * VLC_AddIntf: add an interface
686  *****************************************************************************
687  * This function opens an interface plugin and runs it. If b_block is set
688  * to 0, VLC_AddIntf will return immediately and let the interface run in a
689  * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
690  * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
691  * the playlist when it is completely initialised.
692  *****************************************************************************/
693 int VLC_AddIntf( int i_object, char const *psz_module,
694                  vlc_bool_t b_block, vlc_bool_t b_play )
695 {
696     int i_err;
697     intf_thread_t *p_intf;
698     vlc_t *p_vlc = vlc_current_object( i_object );
699
700     if( !p_vlc )
701     {
702         return VLC_ENOOBJ;
703     }
704
705     /* Try to create the interface */
706     p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
707
708     if( p_intf == NULL )
709     {
710         msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
711         if( i_object ) vlc_object_release( p_vlc );
712         return VLC_EGENERIC;
713     }
714
715     /* Interface doesn't handle play on start so do it ourselves */
716     if( !p_intf->b_play && b_play ) VLC_Play( i_object );
717
718     /* Try to run the interface */
719     p_intf->b_play = b_play;
720     p_intf->b_block = b_block;
721     i_err = intf_RunThread( p_intf );
722     if( i_err )
723     {
724         vlc_object_detach( p_intf );
725         intf_Destroy( p_intf );
726         if( i_object ) vlc_object_release( p_vlc );
727         return i_err;
728     }
729
730     if( i_object ) vlc_object_release( p_vlc );
731     return VLC_SUCCESS;
732 }
733
734 /*****************************************************************************
735  * VLC_Die: ask vlc to die.
736  *****************************************************************************
737  * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
738  * task. It is your duty to call VLC_CleanUp and VLC_Destroy afterwards.
739  *****************************************************************************/
740 int VLC_Die( int i_object )
741 {
742     vlc_t *p_vlc = vlc_current_object( i_object );
743
744     if( !p_vlc )
745     {
746         return VLC_ENOOBJ;
747     }
748
749     p_vlc->b_die = VLC_TRUE;
750
751     if( i_object ) vlc_object_release( p_vlc );
752     return VLC_SUCCESS;
753 }
754
755 /*****************************************************************************
756  * VLC_CleanUp: CleanUp all the intf, playlist, vout, aout
757  *****************************************************************************/
758 int VLC_CleanUp( int i_object )
759 {
760     intf_thread_t      * p_intf;
761     playlist_t         * p_playlist;
762     vout_thread_t      * p_vout;
763     aout_instance_t    * p_aout;
764     announce_handler_t * p_announce;
765     vlc_t *p_vlc = vlc_current_object( i_object );
766
767     /* Check that the handle is valid */
768     if( !p_vlc )
769     {
770         return VLC_ENOOBJ;
771     }
772
773     /*
774      * Ask the interfaces to stop and destroy them
775      */
776     msg_Dbg( p_vlc, "removing all interfaces" );
777     while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
778     {
779         intf_StopThread( p_intf );
780         vlc_object_detach( p_intf );
781         vlc_object_release( p_intf );
782         intf_Destroy( p_intf );
783     }
784
785     /*
786      * Free playlists
787      */
788     msg_Dbg( p_vlc, "removing all playlists" );
789     while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
790                                           FIND_CHILD )) )
791     {
792         vlc_object_detach( p_playlist );
793         vlc_object_release( p_playlist );
794         playlist_Destroy( p_playlist );
795     }
796
797     /*
798      * Free video outputs
799      */
800     msg_Dbg( p_vlc, "removing all video outputs" );
801     while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
802     {
803         vlc_object_detach( p_vout );
804         vlc_object_release( p_vout );
805         vout_Destroy( p_vout );
806     }
807
808     /*
809      * Free audio outputs
810      */
811     msg_Dbg( p_vlc, "removing all audio outputs" );
812     while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
813     {
814         vlc_object_detach( (vlc_object_t *)p_aout );
815         vlc_object_release( (vlc_object_t *)p_aout );
816         aout_Delete( p_aout );
817     }
818
819     /*
820      * Free announce handler(s?)
821      */
822     msg_Dbg( p_vlc, "removing announce handler" );
823     while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
824                                                  FIND_CHILD ) ) )
825    {
826         vlc_object_detach( p_announce );
827         vlc_object_release( p_announce );
828         announce_HandlerDestroy( p_announce );
829    }
830
831     if( i_object ) vlc_object_release( p_vlc );
832     return VLC_SUCCESS;
833 }
834
835 /*****************************************************************************
836  * VLC_Destroy: Destroy everything.
837  *****************************************************************************
838  * This function requests the running threads to finish, waits for their
839  * termination, and destroys their structure.
840  *****************************************************************************/
841 int VLC_Destroy( int i_object )
842 {
843     vlc_t *p_vlc = vlc_current_object( i_object );
844
845     if( !p_vlc )
846     {
847         return VLC_ENOOBJ;
848     }
849
850     /*
851      * Free allocated memory
852      */
853     if( p_vlc->p_memcpy_module )
854     {
855         module_Unneed( p_vlc, p_vlc->p_memcpy_module );
856         p_vlc->p_memcpy_module = NULL;
857     }
858
859     /*
860      * Free module bank !
861      */
862     module_EndBank( p_vlc );
863
864     if( p_vlc->psz_homedir )
865     {
866         free( p_vlc->psz_homedir );
867         p_vlc->psz_homedir = NULL;
868     }
869
870     if( p_vlc->psz_configfile )
871     {
872         free( p_vlc->psz_configfile );
873         p_vlc->psz_configfile = NULL;
874     }
875
876     if( p_vlc->p_hotkeys )
877     {
878         free( p_vlc->p_hotkeys );
879         p_vlc->p_hotkeys = NULL;
880     }
881
882     /*
883      * System specific cleaning code
884      */
885     system_End( p_vlc );
886
887     /* Destroy mutexes */
888     vlc_mutex_destroy( &p_vlc->config_lock );
889
890     vlc_object_detach( p_vlc );
891
892     /* Release object before destroying it */
893     if( i_object ) vlc_object_release( p_vlc );
894
895     vlc_object_destroy( p_vlc );
896
897     /* Stop thread system: last one out please shut the door! */
898     vlc_threads_end( p_libvlc );
899
900     return VLC_SUCCESS;
901 }
902
903 /*****************************************************************************
904  * VLC_VariableSet: set a vlc variable
905  *****************************************************************************/
906 int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
907 {
908     vlc_t *p_vlc = vlc_current_object( i_object );
909     int i_ret;
910
911     if( !p_vlc )
912     {
913         return VLC_ENOOBJ;
914     }
915
916     /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
917      * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
918     if( !strncmp( psz_var, "conf::", 6 ) )
919     {
920         module_config_t *p_item;
921         char const *psz_newvar = psz_var + 6;
922
923         p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
924
925         if( p_item )
926         {
927             switch( p_item->i_type )
928             {
929                 case CONFIG_ITEM_BOOL:
930                     config_PutInt( p_vlc, psz_newvar, value.b_bool );
931                     break;
932                 case CONFIG_ITEM_INTEGER:
933                     config_PutInt( p_vlc, psz_newvar, value.i_int );
934                     break;
935                 case CONFIG_ITEM_FLOAT:
936                     config_PutFloat( p_vlc, psz_newvar, value.f_float );
937                     break;
938                 default:
939                     config_PutPsz( p_vlc, psz_newvar, value.psz_string );
940                     break;
941             }
942             if( i_object ) vlc_object_release( p_vlc );
943             return VLC_SUCCESS;
944         }
945     }
946
947     i_ret = var_Set( p_vlc, psz_var, value );
948
949     if( i_object ) vlc_object_release( p_vlc );
950     return i_ret;
951 }
952
953 /*****************************************************************************
954  * VLC_VariableGet: get a vlc variable
955  *****************************************************************************/
956 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
957 {
958     vlc_t *p_vlc = vlc_current_object( i_object );
959     int i_ret;
960
961     if( !p_vlc )
962     {
963         return VLC_ENOOBJ;
964     }
965
966     i_ret = var_Get( p_vlc, psz_var, p_value );
967
968     if( i_object ) vlc_object_release( p_vlc );
969     return i_ret;
970 }
971
972 /*****************************************************************************
973  * VLC_AddTarget: adds a target for playing.
974  *****************************************************************************
975  * This function adds psz_target to the current playlist. If a playlist does
976  * not exist, it will create one.
977  *****************************************************************************/
978 int VLC_AddTarget( int i_object, char const *psz_target,
979                    char const **ppsz_options, int i_options,
980                    int i_mode, int i_pos )
981 {
982     int i_err;
983     playlist_t *p_playlist;
984     vlc_t *p_vlc = vlc_current_object( i_object );
985
986     if( !p_vlc )
987     {
988         return VLC_ENOOBJ;
989     }
990
991     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
992
993     if( p_playlist == NULL )
994     {
995         msg_Dbg( p_vlc, "no playlist present, creating one" );
996         p_playlist = playlist_Create( p_vlc );
997
998         if( p_playlist == NULL )
999         {
1000             if( i_object ) vlc_object_release( p_vlc );
1001             return VLC_EGENERIC;
1002         }
1003
1004         vlc_object_yield( p_playlist );
1005     }
1006
1007     i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
1008                              i_mode, i_pos, -1, ppsz_options, i_options);
1009
1010     vlc_object_release( p_playlist );
1011
1012     if( i_object ) vlc_object_release( p_vlc );
1013     return i_err;
1014 }
1015
1016 /*****************************************************************************
1017  * VLC_Play: play
1018  *****************************************************************************/
1019 int VLC_Play( int i_object )
1020 {
1021     playlist_t * p_playlist;
1022     vlc_t *p_vlc = vlc_current_object( i_object );
1023
1024     /* Check that the handle is valid */
1025     if( !p_vlc )
1026     {
1027         return VLC_ENOOBJ;
1028     }
1029
1030     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1031
1032     if( !p_playlist )
1033     {
1034         if( i_object ) vlc_object_release( p_vlc );
1035         return VLC_ENOOBJ;
1036     }
1037
1038     playlist_Play( p_playlist );
1039     vlc_object_release( p_playlist );
1040
1041     if( i_object ) vlc_object_release( p_vlc );
1042     return VLC_SUCCESS;
1043 }
1044
1045 /*****************************************************************************
1046  * VLC_Pause: toggle pause
1047  *****************************************************************************/
1048 int VLC_Pause( int i_object )
1049 {
1050     playlist_t * p_playlist;
1051     vlc_t *p_vlc = vlc_current_object( i_object );
1052
1053     /* Check that the handle is valid */
1054     if( !p_vlc )
1055     {
1056         return VLC_ENOOBJ;
1057     }
1058
1059     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1060
1061     if( !p_playlist )
1062     {
1063         if( i_object ) vlc_object_release( p_vlc );
1064         return VLC_ENOOBJ;
1065     }
1066
1067     playlist_Pause( p_playlist );
1068     vlc_object_release( p_playlist );
1069
1070     if( i_object ) vlc_object_release( p_vlc );
1071     return VLC_SUCCESS;
1072 }
1073
1074 /*****************************************************************************
1075  * VLC_Pause: toggle pause
1076  *****************************************************************************/
1077 int VLC_Stop( int i_object )
1078 {
1079     playlist_t * p_playlist;
1080     vlc_t *p_vlc = vlc_current_object( i_object );
1081
1082     /* Check that the handle is valid */
1083     if( !p_vlc )
1084     {
1085         return VLC_ENOOBJ;
1086     }
1087
1088     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1089
1090     if( !p_playlist )
1091     {
1092         if( i_object ) vlc_object_release( p_vlc );
1093         return VLC_ENOOBJ;
1094     }
1095
1096     playlist_Stop( p_playlist );
1097     vlc_object_release( p_playlist );
1098
1099     if( i_object ) vlc_object_release( p_vlc );
1100     return VLC_SUCCESS;
1101 }
1102
1103 /*****************************************************************************
1104  * VLC_IsPlaying: Query for Playlist Status
1105  *****************************************************************************/
1106 vlc_bool_t VLC_IsPlaying( int i_object )
1107 {
1108     playlist_t * p_playlist;
1109     vlc_bool_t   b_playing;
1110
1111     vlc_t *p_vlc = vlc_current_object( i_object );
1112
1113     /* Check that the handle is valid */
1114     if( !p_vlc )
1115     {
1116         return VLC_ENOOBJ;
1117     }
1118
1119     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1120
1121     if( !p_playlist )
1122     {
1123         if( i_object ) vlc_object_release( p_vlc );
1124         return VLC_ENOOBJ;
1125     }
1126
1127     b_playing = playlist_IsPlaying( p_playlist );
1128     vlc_object_release( p_playlist );
1129
1130     if( i_object ) vlc_object_release( p_vlc );
1131     return b_playing;
1132 }
1133
1134 /**
1135  * Get the current position in a input
1136  *
1137  * Return the current position as a float
1138  * \note For some inputs, this will be unknown.
1139  *
1140  * \param i_object a vlc object id
1141  * \return a float in the range of 0.0 - 1.0
1142  */
1143 float VLC_PositionGet( int i_object )
1144 {
1145     input_thread_t *p_input;
1146     vlc_value_t val;
1147     vlc_t *p_vlc = vlc_current_object( i_object );
1148
1149     /* Check that the handle is valid */
1150     if( !p_vlc )
1151     {
1152         return VLC_ENOOBJ;
1153     }
1154
1155     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1156
1157     if( !p_input )
1158     {
1159         if( i_object ) vlc_object_release( p_vlc );
1160         return VLC_ENOOBJ;
1161     }
1162
1163     var_Get( p_input, "position", &val );
1164     vlc_object_release( p_input );
1165
1166     if( i_object ) vlc_object_release( p_vlc );
1167     return val.f_float;
1168 }
1169
1170 /**
1171  * Set the current position in a input
1172  *
1173  * Set the current position in a input and then return
1174  * the current position as a float.
1175  * \note For some inputs, this will be unknown.
1176  *
1177  * \param i_object a vlc object id
1178  * \param i_position a float in the range of 0.0 - 1.0
1179  * \return a float in the range of 0.0 - 1.0
1180  */
1181 float VLC_PositionSet( int i_object, float i_position )
1182 {
1183     input_thread_t *p_input;
1184     vlc_value_t val;
1185     vlc_t *p_vlc = vlc_current_object( i_object );
1186
1187     /* Check that the handle is valid */
1188     if( !p_vlc )
1189     {
1190         return VLC_ENOOBJ;
1191     }
1192
1193     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1194
1195     if( !p_input )
1196     {
1197         if( i_object ) vlc_object_release( p_vlc );
1198         return VLC_ENOOBJ;
1199     }
1200
1201     val.f_float = i_position;
1202     var_Set( p_input, "position", val );
1203     var_Get( p_input, "position", &val );
1204     vlc_object_release( p_input );
1205
1206     if( i_object ) vlc_object_release( p_vlc );
1207     return val.f_float;
1208 }
1209
1210 /**
1211  * Get the current position in a input
1212  *
1213  * Return the current position in seconds from the start.
1214  * \note For some inputs, this will be unknown.
1215  *
1216  * \param i_object a vlc object id
1217  * \return the offset from 0:00 in seconds
1218  */
1219 int VLC_TimeGet( int i_object )
1220 {
1221     input_thread_t *p_input;
1222     vlc_value_t val;
1223     vlc_t *p_vlc = vlc_current_object( i_object );
1224
1225     /* Check that the handle is valid */
1226     if( !p_vlc )
1227     {
1228         return VLC_ENOOBJ;
1229     }
1230
1231     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1232
1233     if( !p_input )
1234     {
1235         if( i_object ) vlc_object_release( p_vlc );
1236         return VLC_ENOOBJ;
1237     }
1238
1239     var_Get( p_input, "time", &val );
1240     vlc_object_release( p_input );
1241
1242     if( i_object ) vlc_object_release( p_vlc );
1243     return val.i_time  / 1000000;
1244 }
1245
1246 /**
1247  * Seek to a position in the current input
1248  *
1249  * Seek i_seconds in the current input. If b_relative is set,
1250  * then the seek will be relative to the current position, otherwise
1251  * it will seek to i_seconds from the beginning of the input.
1252  * \note For some inputs, this will be unknown.
1253  *
1254  * \param i_object a vlc object id
1255  * \param i_seconds seconds from current position or from beginning of input
1256  * \param b_relative seek relative from current position
1257  * \return VLC_SUCCESS on success
1258  */
1259 int VLC_TimeSet( int i_object, int i_seconds, vlc_bool_t b_relative )
1260 {
1261     input_thread_t *p_input;
1262     vlc_value_t val;
1263     vlc_t *p_vlc = vlc_current_object( i_object );
1264
1265     /* Check that the handle is valid */
1266     if( !p_vlc )
1267     {
1268         return VLC_ENOOBJ;
1269     }
1270
1271     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1272
1273     if( !p_input )
1274     {
1275         if( i_object ) vlc_object_release( p_vlc );
1276         return VLC_ENOOBJ;
1277     }
1278
1279     if( b_relative )
1280     {
1281         val.i_time = i_seconds * 1000000;
1282         var_Set( p_input, "time-offset", val );
1283     }
1284     else
1285     {
1286         val.i_time = i_seconds * 1000000;
1287         var_Set( p_input, "time", val );
1288     }
1289     vlc_object_release( p_input );
1290
1291     if( i_object ) vlc_object_release( p_vlc );
1292     return VLC_SUCCESS;
1293 }
1294
1295 /**
1296  * Get the total length of a input
1297  *
1298  * Return the total length in seconds from the current input.
1299  * \note For some inputs, this will be unknown.
1300  *
1301  * \param i_object a vlc object id
1302  * \return the length in seconds
1303  */
1304 int VLC_LengthGet( int i_object )
1305 {
1306     input_thread_t *p_input;
1307     vlc_value_t val;
1308     vlc_t *p_vlc = vlc_current_object( i_object );
1309
1310     /* Check that the handle is valid */
1311     if( !p_vlc )
1312     {
1313         return VLC_ENOOBJ;
1314     }
1315
1316     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1317
1318     if( !p_input )
1319     {
1320         if( i_object ) vlc_object_release( p_vlc );
1321         return VLC_ENOOBJ;
1322     }
1323
1324     var_Get( p_input, "length", &val );
1325     vlc_object_release( p_input );
1326
1327     if( i_object ) vlc_object_release( p_vlc );
1328     return val.i_time  / 1000000;
1329 }
1330
1331 /**
1332  * Play the input faster than realtime
1333  *
1334  * 2x, 4x, 8x faster than realtime
1335  * \note For some inputs, this will be impossible.
1336  *
1337  * \param i_object a vlc object id
1338  * \return the current speedrate
1339  */
1340 float VLC_SpeedFaster( int i_object )
1341 {
1342     input_thread_t *p_input;
1343     vlc_value_t val;
1344     vlc_t *p_vlc = vlc_current_object( i_object );
1345
1346     /* Check that the handle is valid */
1347     if( !p_vlc )
1348     {
1349         return VLC_ENOOBJ;
1350     }
1351
1352     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1353
1354     if( !p_input )
1355     {
1356         if( i_object ) vlc_object_release( p_vlc );
1357         return VLC_ENOOBJ;
1358     }
1359
1360     val.b_bool = VLC_TRUE;
1361     var_Set( p_input, "rate-faster", val );
1362     var_Get( p_input, "rate", &val );
1363     vlc_object_release( p_input );
1364
1365     if( i_object ) vlc_object_release( p_vlc );
1366     return val.f_float / INPUT_RATE_DEFAULT;
1367 }
1368
1369 /**
1370  * Play the input slower than realtime
1371  *
1372  * 1/2x, 1/4x, 1/8x slower than realtime
1373  * \note For some inputs, this will be impossible.
1374  *
1375  * \param i_object a vlc object id
1376  * \return the current speedrate
1377  */
1378 float VLC_SpeedSlower( int i_object )
1379 {
1380     input_thread_t *p_input;
1381     vlc_value_t val;
1382     vlc_t *p_vlc = vlc_current_object( i_object );
1383
1384     /* Check that the handle is valid */
1385     if( !p_vlc )
1386     {
1387         return VLC_ENOOBJ;
1388     }
1389
1390     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1391
1392     if( !p_input )
1393     {
1394         if( i_object ) vlc_object_release( p_vlc );
1395         return VLC_ENOOBJ;
1396     }
1397
1398     val.b_bool = VLC_TRUE;
1399     var_Set( p_input, "rate-slower", val );
1400     var_Get( p_input, "rate", &val );
1401     vlc_object_release( p_input );
1402
1403     if( i_object ) vlc_object_release( p_vlc );
1404     return val.f_float / INPUT_RATE_DEFAULT;
1405 }
1406
1407 /**
1408  * Return the current playlist item
1409  *
1410  * Returns the index of the playlistitem that is currently selected for play.
1411  * This is valid even if nothing is currently playing.
1412  *
1413  * \param i_object a vlc object id
1414  * \return the current index
1415  */
1416 int VLC_PlaylistIndex( int i_object )
1417 {
1418     int i_index;
1419     playlist_t * p_playlist;
1420     vlc_t *p_vlc = vlc_current_object( i_object );
1421
1422     /* Check that the handle is valid */
1423     if( !p_vlc )
1424     {
1425         return VLC_ENOOBJ;
1426     }
1427
1428     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1429
1430     if( !p_playlist )
1431     {
1432         if( i_object ) vlc_object_release( p_vlc );
1433         return VLC_ENOOBJ;
1434     }
1435
1436     i_index = p_playlist->i_index;
1437     vlc_object_release( p_playlist );
1438
1439     if( i_object ) vlc_object_release( p_vlc );
1440     return i_index;
1441 }
1442
1443 /**
1444  * Total amount of items in the playlist
1445  *
1446  * \param i_object a vlc object id
1447  * \return amount of playlist items
1448  */
1449 int VLC_PlaylistNumberOfItems( int i_object )
1450 {
1451     int i_size;
1452     playlist_t * p_playlist;
1453     vlc_t *p_vlc = vlc_current_object( i_object );
1454
1455     /* Check that the handle is valid */
1456     if( !p_vlc )
1457     {
1458         return VLC_ENOOBJ;
1459     }
1460
1461     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1462
1463     if( !p_playlist )
1464     {
1465         if( i_object ) vlc_object_release( p_vlc );
1466         return VLC_ENOOBJ;
1467     }
1468
1469     i_size = p_playlist->i_size;
1470     vlc_object_release( p_playlist );
1471
1472     if( i_object ) vlc_object_release( p_vlc );
1473     return i_size;
1474 }
1475
1476 /**
1477  * Next playlist item
1478  *
1479  * Skip to the next playlistitem and play it.
1480  *
1481  * \param i_object a vlc object id
1482  * \return VLC_SUCCESS on success
1483  */
1484 int VLC_PlaylistNext( int i_object )
1485 {
1486     playlist_t * p_playlist;
1487     vlc_t *p_vlc = vlc_current_object( i_object );
1488
1489     /* Check that the handle is valid */
1490     if( !p_vlc )
1491     {
1492         return VLC_ENOOBJ;
1493     }
1494
1495     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1496
1497     if( !p_playlist )
1498     {
1499         if( i_object ) vlc_object_release( p_vlc );
1500         return VLC_ENOOBJ;
1501     }
1502
1503     playlist_Next( p_playlist );
1504     vlc_object_release( p_playlist );
1505
1506     if( i_object ) vlc_object_release( p_vlc );
1507     return VLC_SUCCESS;
1508 }
1509
1510 /**
1511  * Previous playlist item
1512  *
1513  * Skip to the previous playlistitem and play it.
1514  *
1515  * \param i_object a vlc object id
1516  * \return VLC_SUCCESS on success
1517  */
1518 int VLC_PlaylistPrev( int i_object )
1519 {
1520     playlist_t * p_playlist;
1521     vlc_t *p_vlc = vlc_current_object( i_object );
1522
1523     /* Check that the handle is valid */
1524     if( !p_vlc )
1525     {
1526         return VLC_ENOOBJ;
1527     }
1528
1529     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1530
1531     if( !p_playlist )
1532     {
1533         if( i_object ) vlc_object_release( p_vlc );
1534         return VLC_ENOOBJ;
1535     }
1536
1537     playlist_Prev( p_playlist );
1538     vlc_object_release( p_playlist );
1539
1540     if( i_object ) vlc_object_release( p_vlc );
1541     return VLC_SUCCESS;
1542 }
1543
1544
1545 /*****************************************************************************
1546  * VLC_PlaylistClear: Empty the playlist
1547  *****************************************************************************/
1548 int VLC_PlaylistClear( int i_object )
1549 {
1550     int i_err;
1551     playlist_t * p_playlist;
1552     vlc_t *p_vlc = vlc_current_object( i_object );
1553
1554     /* Check that the handle is valid */
1555     if( !p_vlc )
1556     {
1557         return VLC_ENOOBJ;
1558     }
1559
1560     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1561
1562     if( !p_playlist )
1563     {
1564         if( i_object ) vlc_object_release( p_vlc );
1565         return VLC_ENOOBJ;
1566     }
1567
1568     i_err = playlist_Clear( p_playlist );
1569
1570     vlc_object_release( p_playlist );
1571
1572     if( i_object ) vlc_object_release( p_vlc );
1573     return i_err;
1574 }
1575
1576 /**
1577  * Change the volume
1578  *
1579  * \param i_object a vlc object id
1580  * \param i_volume something in a range from 0-200
1581  * \return the new volume (range 0-200 %)
1582  */
1583 int VLC_VolumeSet( int i_object, int i_volume )
1584 {
1585     audio_volume_t i_vol = 0;
1586     vlc_t *p_vlc = vlc_current_object( i_object );
1587
1588     /* Check that the handle is valid */
1589     if( !p_vlc )
1590     {
1591         return VLC_ENOOBJ;
1592     }
1593
1594     if( i_volume >= 0 && i_volume <= 200 )
1595     {
1596         i_vol = i_volume * AOUT_VOLUME_MAX / 200;
1597         aout_VolumeSet( p_vlc, i_vol );
1598     }
1599
1600     if( i_object ) vlc_object_release( p_vlc );
1601     return i_vol * 200 / AOUT_VOLUME_MAX;
1602 }
1603
1604 /**
1605  * Get the current volume
1606  *
1607  * Retrieve the current volume.
1608  *
1609  * \param i_object a vlc object id
1610  * \return the current volume (range 0-200 %)
1611  */
1612 int VLC_VolumeGet( int i_object )
1613 {
1614     audio_volume_t i_volume;
1615     vlc_t *p_vlc = vlc_current_object( i_object );
1616
1617     /* Check that the handle is valid */
1618     if( !p_vlc )
1619     {
1620         return VLC_ENOOBJ;
1621     }
1622
1623     aout_VolumeGet( p_vlc, &i_volume );
1624
1625     if( i_object ) vlc_object_release( p_vlc );
1626     return i_volume*200/AOUT_VOLUME_MAX;
1627 }
1628
1629 /**
1630  * Mute/Unmute the volume
1631  *
1632  * \param i_object a vlc object id
1633  * \return VLC_SUCCESS on success
1634  */
1635 int VLC_VolumeMute( int i_object )
1636 {
1637     vlc_t *p_vlc = vlc_current_object( i_object );
1638
1639     /* Check that the handle is valid */
1640     if( !p_vlc )
1641     {
1642         return VLC_ENOOBJ;
1643     }
1644
1645     aout_VolumeMute( p_vlc, NULL );
1646
1647     if( i_object ) vlc_object_release( p_vlc );
1648     return VLC_SUCCESS;
1649 }
1650
1651 /*****************************************************************************
1652  * VLC_FullScreen: toggle fullscreen mode
1653  *****************************************************************************/
1654 int VLC_FullScreen( int i_object )
1655 {
1656     vout_thread_t *p_vout;
1657     vlc_t *p_vlc = vlc_current_object( i_object );
1658
1659     if( !p_vlc )
1660     {
1661         return VLC_ENOOBJ;
1662     }
1663
1664     p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1665
1666     if( !p_vout )
1667     {
1668         if( i_object ) vlc_object_release( p_vlc );
1669         return VLC_ENOOBJ;
1670     }
1671
1672     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1673     vlc_object_release( p_vout );
1674
1675     if( i_object ) vlc_object_release( p_vlc );
1676     return VLC_SUCCESS;
1677 }
1678
1679 /* following functions are local */
1680
1681 /*****************************************************************************
1682  * SetLanguage: set the interface language.
1683  *****************************************************************************
1684  * We set the LC_MESSAGES locale category for interface messages and buttons,
1685  * as well as the LC_CTYPE category for string sorting and possible wide
1686  * character support.
1687  *****************************************************************************/
1688 static void SetLanguage ( char const *psz_lang )
1689 {
1690 #if defined( ENABLE_NLS ) \
1691      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1692
1693     char *          psz_path;
1694 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1695     char            psz_tmp[1024];
1696 #endif
1697
1698     if( psz_lang && !*psz_lang )
1699     {
1700 #   if defined( HAVE_LC_MESSAGES )
1701         setlocale( LC_MESSAGES, psz_lang );
1702 #   endif
1703         setlocale( LC_CTYPE, psz_lang );
1704     }
1705     else if( psz_lang )
1706     {
1707 #ifdef SYS_DARWIN
1708         /* I need that under Darwin, please check it doesn't disturb
1709          * other platforms. --Meuuh */
1710         setenv( "LANG", psz_lang, 1 );
1711
1712 #elif defined( SYS_BEOS ) || defined( WIN32 )
1713         /* We set LC_ALL manually because it is the only way to set
1714          * the language at runtime under eg. Windows. Beware that this
1715          * makes the environment unconsistent when libvlc is unloaded and
1716          * should probably be moved to a safer place like vlc.c. */
1717         static char psz_lcall[20];
1718         snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1719         psz_lcall[19] = '\0';
1720         putenv( psz_lcall );
1721 #endif
1722
1723         setlocale( LC_ALL, psz_lang );
1724     }
1725
1726     /* Specify where to find the locales for current domain */
1727 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1728     psz_path = LOCALEDIR;
1729 #else
1730     snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1731               "locale" );
1732     psz_path = psz_tmp;
1733 #endif
1734     if( !bindtextdomain( PACKAGE, psz_path ) )
1735     {
1736         fprintf( stderr, "warning: no domain %s in directory %s\n",
1737                  PACKAGE, psz_path );
1738     }
1739
1740     /* Set the default domain */
1741     textdomain( PACKAGE );
1742
1743 #if defined( ENABLE_UTF8 )
1744     bind_textdomain_codeset( PACKAGE, "UTF-8" );
1745 #endif
1746
1747 #endif
1748 }
1749
1750 /*****************************************************************************
1751  * GetFilenames: parse command line options which are not flags
1752  *****************************************************************************
1753  * Parse command line for input files as well as their associated options.
1754  * An option always follows its associated input and begins with a ":".
1755  *****************************************************************************/
1756 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1757 {
1758     int i_opt, i_options;
1759
1760     /* We assume that the remaining parameters are filenames
1761      * and their input options */
1762     for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1763     {
1764         i_options = 0;
1765
1766         /* Count the input options */
1767         while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1768         {
1769             i_options++;
1770             i_opt--;
1771         }
1772
1773         /* TODO: write an internal function of this one, to avoid
1774          *       unnecessary lookups. */
1775         VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1776                        (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1777                                         NULL ), i_options,
1778                        PLAYLIST_INSERT, 0 );
1779     }
1780
1781     return VLC_SUCCESS;
1782 }
1783
1784 /*****************************************************************************
1785  * Usage: print program usage
1786  *****************************************************************************
1787  * Print a short inline help. Message interface is initialized at this stage.
1788  *****************************************************************************/
1789 static void Usage( vlc_t *p_this, char const *psz_module_name )
1790 {
1791 #define FORMAT_STRING "  %s --%s%s%s%s%s%s%s "
1792     /* short option ------'    |     | | | |  | |
1793      * option name ------------'     | | | |  | |
1794      * <bra -------------------------' | | |  | |
1795      * option type or "" --------------' | |  | |
1796      * ket> -----------------------------' |  | |
1797      * padding spaces ---------------------'  | |
1798      * comment -------------------------------' |
1799      * comment suffix --------------------------'
1800      *
1801      * The purpose of having bra and ket is that we might i18n them as well.
1802      */
1803 #define LINE_START 8
1804 #define PADDING_SPACES 25
1805     vlc_list_t *p_list;
1806     module_t *p_parser;
1807     module_config_t *p_item;
1808     char psz_spaces[PADDING_SPACES+LINE_START+1];
1809     char psz_format[sizeof(FORMAT_STRING)];
1810     char psz_buffer[1000];
1811     char psz_short[4];
1812     int i_index;
1813     int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1814     vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
1815
1816     memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1817     psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1818
1819     strcpy( psz_format, FORMAT_STRING );
1820
1821 #ifdef WIN32
1822     ShowConsole();
1823 #endif
1824
1825     /* List all modules */
1826     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1827
1828     /* Enumerate the config for each module */
1829     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1830     {
1831         vlc_bool_t b_help_module;
1832
1833         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1834
1835         if( psz_module_name && strcmp( psz_module_name,
1836                                        p_parser->psz_object_name ) )
1837         {
1838             continue;
1839         }
1840
1841         /* Ignore modules without config options */
1842         if( !p_parser->i_config_items )
1843         {
1844             continue;
1845         }
1846
1847         /* Ignore modules with only advanced config options if requested */
1848         if( !b_advanced )
1849         {
1850             for( p_item = p_parser->p_config;
1851                  p_item->i_type != CONFIG_HINT_END;
1852                  p_item++ )
1853             {
1854                 if( (p_item->i_type & CONFIG_ITEM) &&
1855                     !p_item->b_advanced ) break;
1856             }
1857             if( p_item->i_type == CONFIG_HINT_END ) continue;
1858         }
1859
1860         /* Print name of module */
1861         if( strcmp( "main", p_parser->psz_object_name ) )
1862         fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1863
1864         b_help_module = !strcmp( "help", p_parser->psz_object_name );
1865
1866         /* Print module options */
1867         for( p_item = p_parser->p_config;
1868              p_item->i_type != CONFIG_HINT_END;
1869              p_item++ )
1870         {
1871             char *psz_text;
1872             char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1873             char *psz_suf = "", *psz_prefix = NULL;
1874             int i;
1875
1876             /* Skip advanced options if requested */
1877             if( p_item->b_advanced && !b_advanced )
1878             {
1879                 continue;
1880             }
1881
1882             switch( p_item->i_type )
1883             {
1884             case CONFIG_HINT_CATEGORY:
1885             case CONFIG_HINT_USAGE:
1886                 if( !strcmp( "main", p_parser->psz_object_name ) )
1887                 fprintf( stdout, "\n %s\n", p_item->psz_text );
1888                 break;
1889
1890             case CONFIG_ITEM_STRING:
1891             case CONFIG_ITEM_FILE:
1892             case CONFIG_ITEM_DIRECTORY:
1893             case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1894                 if( !p_item->ppsz_list )
1895                 {
1896                     psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1897                     break;
1898                 }
1899                 else
1900                 {
1901                     psz_bra = " {";
1902                     psz_type = psz_buffer;
1903                     psz_type[0] = '\0';
1904                     for( i = 0; p_item->ppsz_list[i]; i++ )
1905                     {
1906                         if( i ) strcat( psz_type, "," );
1907                         strcat( psz_type, p_item->ppsz_list[i] );
1908                     }
1909                     psz_ket = "}";
1910                     break;
1911                 }
1912             case CONFIG_ITEM_INTEGER:
1913             case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1914                 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1915                 break;
1916             case CONFIG_ITEM_FLOAT:
1917                 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1918                 break;
1919             case CONFIG_ITEM_BOOL:
1920                 psz_bra = ""; psz_type = ""; psz_ket = "";
1921                 if( !b_help_module )
1922                 {
1923                     psz_suf = p_item->i_value ? _(" (default enabled)") :
1924                                                 _(" (default disabled)");
1925                 }
1926                 break;
1927             }
1928
1929             if( !psz_type )
1930             {
1931                 continue;
1932             }
1933
1934             /* Add short option if any */
1935             if( p_item->i_short )
1936             {
1937                 sprintf( psz_short, "-%c,", p_item->i_short );
1938             }
1939             else
1940             {
1941                 strcpy( psz_short, "   " );
1942             }
1943
1944             i = PADDING_SPACES - strlen( p_item->psz_name )
1945                  - strlen( psz_bra ) - strlen( psz_type )
1946                  - strlen( psz_ket ) - 1;
1947
1948             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1949             {
1950                 /* If option is of type --foo-bar, we print its counterpart
1951                  * as --no-foo-bar, but if it is of type --foobar (without
1952                  * dashes in the name) we print it as --nofoobar. Both
1953                  * values are of course valid, only the display changes. */
1954                 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1955                                                              : ", --no";
1956                 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1957             }
1958
1959             if( i < 0 )
1960             {
1961                 psz_spaces[0] = '\n';
1962                 i = 0;
1963             }
1964             else
1965             {
1966                 psz_spaces[i] = '\0';
1967             }
1968
1969             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1970             {
1971                 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1972                          psz_prefix, p_item->psz_name, psz_bra, psz_type,
1973                          psz_ket, psz_spaces );
1974             }
1975             else
1976             {
1977                 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1978                          "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1979             }
1980
1981             psz_spaces[i] = ' ';
1982
1983             /* We wrap the rest of the output */
1984             sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1985             psz_text = psz_buffer;
1986             while( *psz_text )
1987             {
1988                 char *psz_parser, *psz_word;
1989                 int i_end = strlen( psz_text );
1990
1991                 /* If the remaining text fits in a line, print it. */
1992                 if( i_end <= i_width )
1993                 {
1994                     fprintf( stdout, "%s\n", psz_text );
1995                     break;
1996                 }
1997
1998                 /* Otherwise, eat as many words as possible */
1999                 psz_parser = psz_text;
2000                 do
2001                 {
2002                     psz_word = psz_parser;
2003                     psz_parser = strchr( psz_word, ' ' );
2004                     /* If no space was found, we reached the end of the text
2005                      * block; otherwise, we skip the space we just found. */
2006                     psz_parser = psz_parser ? psz_parser + 1
2007                                             : psz_text + i_end;
2008
2009                 } while( psz_parser - psz_text <= i_width );
2010
2011                 /* We cut a word in one of these cases:
2012                  *  - it's the only word in the line and it's too long.
2013                  *  - we used less than 80% of the width and the word we are
2014                  *    going to wrap is longer than 40% of the width, and even
2015                  *    if the word would have fit in the next line. */
2016                 if( psz_word == psz_text
2017                      || ( psz_word - psz_text < 80 * i_width / 100
2018                            && psz_parser - psz_word > 40 * i_width / 100 ) )
2019                 {
2020                     char c = psz_text[i_width];
2021                     psz_text[i_width] = '\0';
2022                     fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2023                     psz_text += i_width;
2024                     psz_text[0] = c;
2025                 }
2026                 else
2027                 {
2028                     psz_word[-1] = '\0';
2029                     fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2030                     psz_text = psz_word;
2031                 }
2032             }
2033         }
2034     }
2035
2036     /* Release the module list */
2037     vlc_list_release( p_list );
2038
2039 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
2040     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
2041     getchar();
2042 #endif
2043 }
2044
2045 /*****************************************************************************
2046  * ListModules: list the available modules with their description
2047  *****************************************************************************
2048  * Print a list of all available modules (builtins and plugins) and a short
2049  * description for each one.
2050  *****************************************************************************/
2051 static void ListModules( vlc_t *p_this )
2052 {
2053     vlc_list_t *p_list;
2054     module_t *p_parser;
2055     char psz_spaces[22];
2056     int i_index;
2057
2058     memset( psz_spaces, ' ', 22 );
2059
2060 #ifdef WIN32
2061     ShowConsole();
2062 #endif
2063
2064     /* Usage */
2065     fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
2066                      p_this->p_vlc->psz_object_name );
2067
2068     fprintf( stdout, _("[module]              [description]\n") );
2069
2070     /* List all modules */
2071     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2072
2073     /* Enumerate each module */
2074     for( i_index = 0; i_index < p_list->i_count; i_index++ )
2075     {
2076         int i;
2077
2078         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2079
2080         /* Nasty hack, but right now I'm too tired to think about a nice
2081          * solution */
2082         i = 22 - strlen( p_parser->psz_object_name ) - 1;
2083         if( i < 0 ) i = 0;
2084         psz_spaces[i] = 0;
2085
2086         fprintf( stdout, "  %s%s %s\n", p_parser->psz_object_name,
2087                          psz_spaces, p_parser->psz_longname );
2088
2089         psz_spaces[i] = ' ';
2090     }
2091
2092     vlc_list_release( p_list );
2093
2094 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
2095     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
2096     getchar();
2097 #endif
2098 }
2099
2100 /*****************************************************************************
2101  * Version: print complete program version
2102  *****************************************************************************
2103  * Print complete program version and build number.
2104  *****************************************************************************/
2105 static void Version( void )
2106 {
2107 #ifdef WIN32
2108     ShowConsole();
2109 #endif
2110
2111     fprintf( stdout, VERSION_MESSAGE "\n" );
2112     fprintf( stdout,
2113       _("This program comes with NO WARRANTY, to the extent permitted by "
2114         "law.\nYou may redistribute it under the terms of the GNU General "
2115         "Public License;\nsee the file named COPYING for details.\n"
2116         "Written by the VideoLAN team; see the AUTHORS file.\n") );
2117
2118 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
2119     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
2120     getchar();
2121 #endif
2122 }
2123
2124 /*****************************************************************************
2125  * ShowConsole: On Win32, create an output console for debug messages
2126  *****************************************************************************
2127  * This function is useful only on Win32.
2128  *****************************************************************************/
2129 #ifdef WIN32 /*  */
2130 static void ShowConsole( void )
2131 {
2132 #   ifndef UNDER_CE
2133     AllocConsole();
2134     freopen( "CONOUT$", "w", stdout );
2135     freopen( "CONOUT$", "w", stderr );
2136     freopen( "CONIN$", "r", stdin );
2137 #   endif
2138     return;
2139 }
2140 #endif
2141
2142 /*****************************************************************************
2143  * ConsoleWidth: Return the console width in characters
2144  *****************************************************************************
2145  * We use the stty shell command to get the console width; if this fails or
2146  * if the width is less than 80, we default to 80.
2147  *****************************************************************************/
2148 static int ConsoleWidth( void )
2149 {
2150     int i_width = 80;
2151
2152 #ifndef WIN32
2153     char buf[20], *psz_parser;
2154     FILE *file;
2155     int i_ret;
2156
2157     file = popen( "stty size 2>/dev/null", "r" );
2158     if( file )
2159     {
2160         i_ret = fread( buf, 1, 20, file );
2161         if( i_ret > 0 )
2162         {
2163             buf[19] = '\0';
2164             psz_parser = strchr( buf, ' ' );
2165             if( psz_parser )
2166             {
2167                 i_ret = atoi( psz_parser + 1 );
2168                 if( i_ret >= 80 )
2169                 {
2170                     i_width = i_ret;
2171                 }
2172             }
2173         }
2174
2175         pclose( file );
2176     }
2177 #endif
2178
2179     return i_width;
2180 }
2181
2182 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2183                      vlc_value_t old_val, vlc_value_t new_val, void *param)
2184 {
2185     vlc_t *p_vlc = (vlc_t *)p_this;
2186
2187     if( new_val.i_int >= -1 )
2188     {
2189         p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
2190     }
2191     return VLC_SUCCESS;
2192 }