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