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