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