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