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