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