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