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