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