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