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