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