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