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