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