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