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