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