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