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