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