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