]> git.sesse.net Git - vlc/blob - src/libvlc-common.c
Fix another bunch of warnings
[vlc] / src / libvlc-common.c
1 /*****************************************************************************
2  * libvlc-common.c: libvlc instances creation and deletion, interfaces handling
3  *****************************************************************************
4  * Copyright (C) 1998-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *          Derk-Jan Hartman <hartman at videolan dot org>
11  *          RĂ©mi Denis-Courmont <rem # videolan : org>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 /** \file
29  * This file contains functions to create and destroy libvlc instances
30  */
31
32 /*****************************************************************************
33  * Pretend we are a builtin module
34  *****************************************************************************/
35 #define MODULE_NAME main
36 #define MODULE_PATH main
37 #define __BUILTIN__
38
39 /*****************************************************************************
40  * Preamble
41  *****************************************************************************/
42 #include <vlc/vlc.h>
43 #include <libvlc_internal.h>
44 #include <vlc/input.h>
45
46 #include <errno.h>                                                 /* ENOMEM */
47 #include <stdio.h>                                              /* sprintf() */
48 #include <string.h>                                            /* strerror() */
49 #include <stdlib.h>                                                /* free() */
50
51 #ifndef WIN32
52 #   include <netinet/in.h>                            /* BSD: struct in_addr */
53 #endif
54
55 #ifdef HAVE_UNISTD_H
56 #   include <unistd.h>
57 #elif defined( WIN32 ) && !defined( UNDER_CE )
58 #   include <io.h>
59 #endif
60
61 #ifdef WIN32                       /* optind, getopt(), included in unistd.h */
62 #   include "extras/getopt.h"
63 #endif
64
65 #ifdef HAVE_LOCALE_H
66 #   include <locale.h>
67 #endif
68
69 #ifdef HAVE_HAL
70 #   include <hal/libhal.h>
71 #endif
72
73 #include "vlc_cpu.h"                                        /* CPU detection */
74 #include "os_specific.h"
75
76 #include "vlc_error.h"
77
78 #include "vlc_playlist.h"
79 #include "vlc_interface.h"
80
81 #include "audio_output.h"
82
83 #include "vlc_video.h"
84 #include "video_output.h"
85
86 #include "stream_output.h"
87 #include "charset.h"
88
89 #include "libvlc.h"
90
91 /*****************************************************************************
92  * The evil global variable. We handle it with care, don't worry.
93  *****************************************************************************/
94 static libvlc_global_data_t   libvlc_global;
95 static libvlc_global_data_t * p_libvlc_global;
96 static libvlc_int_t *    p_static_vlc;
97 static volatile unsigned int i_instances = 0;
98
99 /*****************************************************************************
100  * Local prototypes
101  *****************************************************************************/
102 void LocaleInit( vlc_object_t * );
103 void LocaleDeinit( void );
104 static void SetLanguage   ( char const * );
105 static int  GetFilenames  ( libvlc_int_t *, int, char *[] );
106 static void Help          ( libvlc_int_t *, char const *psz_help_name );
107 static void Usage         ( libvlc_int_t *, char const *psz_module_name );
108 static void ListModules   ( libvlc_int_t * );
109 static void Version       ( void );
110
111 #ifdef WIN32
112 static void ShowConsole   ( vlc_bool_t );
113 static void PauseConsole  ( void );
114 #endif
115 static int  ConsoleWidth  ( void );
116
117 static int  VerboseCallback( vlc_object_t *, char const *,
118                              vlc_value_t, vlc_value_t, void * );
119
120 static void InitDeviceValues( libvlc_int_t * );
121
122 /*****************************************************************************
123  * vlc_current_object: return the current object.
124  *****************************************************************************
125  * If i_object is non-zero, return the corresponding object. Otherwise,
126  * return the statically allocated p_vlc object.
127  *****************************************************************************/
128 libvlc_int_t * vlc_current_object( int i_object )
129 {
130     if( i_object )
131     {
132          return vlc_object_get( p_libvlc_global, i_object );
133     }
134
135     return p_static_vlc;
136 }
137
138
139 /**
140  * Allocate a libvlc instance, initialize global data if needed
141  * It also initializes the threading system
142  */
143 libvlc_int_t * libvlc_InternalCreate( void )
144 {
145     int i_ret;
146     libvlc_int_t * p_libvlc = NULL;
147     vlc_value_t lockval;
148     char *psz_env;
149
150     /* &libvlc_global never changes,
151      * so we can safely call this multiple times. */
152     p_libvlc_global = &libvlc_global;
153
154     /* vlc_threads_init *must* be the first internal call! No other call is
155      * allowed before the thread system has been initialized. */
156     i_ret = vlc_threads_init( p_libvlc_global );
157     if( i_ret < 0 ) return NULL;
158
159     /* Now that the thread system is initialized, we don't have much, but
160      * at least we have var_Create */
161     var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
162     var_Get( p_libvlc_global, "libvlc", &lockval );
163     vlc_mutex_lock( lockval.p_address );
164
165     i_instances++;
166
167     if( !libvlc_global.b_ready )
168     {
169         /* Guess what CPU we have */
170         libvlc_global.i_cpu = CPUCapabilities();
171        /* The module bank will be initialized later */
172         libvlc_global.p_module_bank = NULL;
173
174         libvlc_global.b_ready = VLC_TRUE;
175     }
176     vlc_mutex_unlock( lockval.p_address );
177     var_Destroy( p_libvlc_global, "libvlc" );
178
179     /* Allocate a libvlc instance object */
180     p_libvlc = vlc_object_create( p_libvlc_global, VLC_OBJECT_LIBVLC );
181     if( p_libvlc == NULL ) { i_instances--; return NULL; }
182     p_libvlc->thread_id = 0;
183     p_libvlc->p_playlist = NULL;
184     p_libvlc->psz_object_name = "libvlc";
185
186     /* Initialize message queue */
187     msg_Create( p_libvlc );
188     /* Announce who we are - Do it only for first instance ? */
189     msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
190     msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
191
192     /* Find verbosity from VLC_VERBOSE environment variable */
193     psz_env = getenv( "VLC_VERBOSE" );
194     p_libvlc->i_verbose = psz_env ? atoi( psz_env ) : -1;
195
196 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
197     p_libvlc->b_color = isatty( 2 ); /* 2 is for stderr */
198 #else
199     p_libvlc->b_color = VLC_FALSE;
200 #endif
201
202     /* Initialize mutexes */
203     vlc_mutex_init( p_libvlc, &p_libvlc->config_lock );
204 #ifdef __APPLE__
205     vlc_mutex_init( p_libvlc, &p_libvlc->quicktime_lock );
206     vlc_thread_set_priority( p_libvlc, VLC_THREAD_PRIORITY_LOW );
207 #endif
208     /* Fake attachment */
209     p_libvlc->b_attached = VLC_TRUE;
210     /* Store data for the non-reentrant API */
211     p_static_vlc = p_libvlc;
212
213     return p_libvlc;
214 }
215
216 /**
217  * Initialize a libvlc instance
218  * This function initializes a previously allocated libvlc instance:
219  *  - CPU detection
220  *  - gettext initialization
221  *  - message queue, module bank and playlist initialization
222  *  - configuration and commandline parsing
223  */
224 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc, char *ppsz_argv[] )
225 {
226     char         p_capabilities[200];
227     char *       p_tmp;
228     char *       psz_modules;
229     char *       psz_parser;
230     char *       psz_control;
231     vlc_bool_t   b_exit = VLC_FALSE;
232     int          i_ret = VLC_EEXIT;
233     module_t    *p_help_module;
234     playlist_t  *p_playlist;
235     vlc_value_t  val;
236 #if defined( ENABLE_NLS ) \
237      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
238 # if defined (WIN32) || defined (__APPLE__)
239     char *       psz_language;
240 #endif
241 #endif
242
243     /* System specific initialization code */
244     system_Init( p_libvlc, &i_argc, ppsz_argv );
245
246     /* Get the executable name (similar to the basename command) */
247     if( i_argc > 0 )
248     {
249         p_libvlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
250         while( *p_tmp )
251         {
252             if( *p_tmp == '/' ) p_libvlc->psz_object_name = ++p_tmp;
253             else ++p_tmp;
254         }
255     }
256     else
257     {
258         p_libvlc->psz_object_name = "vlc";
259     }
260
261     /*
262      * Support for gettext
263      */
264     SetLanguage( "" );
265
266     /*
267      * Global iconv, must be done after setlocale()
268      * so that vlc_current_charset() works.
269      */
270     LocaleInit( (vlc_object_t *)p_libvlc );
271
272     /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
273     msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
274
275     /* Initialize the module bank and load the configuration of the
276      * main module. We need to do this at this stage to be able to display
277      * a short help if required by the user. (short help == main module
278      * options) */
279     module_InitBank( p_libvlc );
280
281     /* Hack: insert the help module here */
282     p_help_module = vlc_object_create( p_libvlc, VLC_OBJECT_MODULE );
283     if( p_help_module == NULL )
284     {
285         module_EndBank( p_libvlc );
286         return VLC_EGENERIC;
287     }
288     p_help_module->psz_object_name = "help";
289     p_help_module->psz_longname = N_("Help options");
290     config_Duplicate( p_help_module, p_help_config );
291     vlc_object_attach( p_help_module, libvlc_global.p_module_bank );
292     /* End hack */
293
294     if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE ) )
295     {
296         vlc_object_detach( p_help_module );
297         config_Free( p_help_module );
298         vlc_object_destroy( p_help_module );
299         module_EndBank( p_libvlc );
300         return VLC_EGENERIC;
301     }
302
303     /* Check for short help option */
304     if( config_GetInt( p_libvlc, "help" ) )
305     {
306         Help( p_libvlc, "help" );
307         b_exit = VLC_TRUE;
308         i_ret = VLC_EEXITSUCCESS;
309     }
310     /* Check for version option */
311     else if( config_GetInt( p_libvlc, "version" ) )
312     {
313         Version();
314         b_exit = VLC_TRUE;
315         i_ret = VLC_EEXITSUCCESS;
316     }
317
318     /* Set the config file stuff */
319     p_libvlc->psz_homedir = config_GetHomeDir();
320     p_libvlc->psz_userdir = config_GetUserDir();
321     if( p_libvlc->psz_userdir == NULL )
322         p_libvlc->psz_userdir = strdup(p_libvlc->psz_homedir);
323     p_libvlc->psz_configfile = config_GetPsz( p_libvlc, "config" );
324     if( p_libvlc->psz_configfile != NULL && p_libvlc->psz_configfile[0] == '~'
325          && p_libvlc->psz_configfile[1] == '/' )
326     {
327         char *psz = malloc( strlen(p_libvlc->psz_userdir)
328                              + strlen(p_libvlc->psz_configfile) );
329         /* This is incomplete : we should also support the ~cmassiot/ syntax. */
330         sprintf( psz, "%s/%s", p_libvlc->psz_userdir,
331                                p_libvlc->psz_configfile + 2 );
332         free( p_libvlc->psz_configfile );
333         p_libvlc->psz_configfile = psz;
334     }
335
336     /* Check for plugins cache options */
337     if( config_GetInt( p_libvlc, "reset-plugins-cache" ) )
338     {
339         libvlc_global.p_module_bank->b_cache_delete = VLC_TRUE;
340     }
341
342     /* Hack: remove the help module here */
343     vlc_object_detach( p_help_module );
344     /* End hack */
345
346     /* Will be re-done properly later on */
347     p_libvlc->i_verbose = config_GetInt( p_libvlc, "verbose" );
348
349     /* Check for daemon mode */
350 #ifndef WIN32
351     if( config_GetInt( p_libvlc, "daemon" ) )
352     {
353 #if HAVE_DAEMON
354         if( daemon( 1, 0) != 0 )
355         {
356             msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
357             b_exit = VLC_TRUE;
358         }
359
360         p_libvlc->p_libvlc_global->b_daemon = VLC_TRUE;
361
362         /* lets check if we need to write the pidfile */
363         char * psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
364
365         if( psz_pidfile != NULL )
366         {
367             FILE *pidfile;
368             pid_t i_pid = getpid ();
369             msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
370                                i_pid, psz_pidfile );
371             pidfile = utf8_fopen( psz_pidfile,"w" );
372             if( pidfile != NULL )
373             {
374                 utf8_fprintf( pidfile, "%d", (int)i_pid );
375                 fclose( pidfile );
376             }
377             else
378             {
379                 msg_Err( p_libvlc, "cannot open pid file for writing: %s (%s)",
380                          psz_pidfile, strerror(errno) );
381             }
382         }
383         free( psz_pidfile );
384
385 #else
386         pid_t i_pid;
387
388         if( ( i_pid = fork() ) < 0 )
389         {
390             msg_Err( p_libvlc, "unable to fork vlc to daemon mode" );
391             b_exit = VLC_TRUE;
392         }
393         else if( i_pid )
394         {
395             /* This is the parent, exit right now */
396             msg_Dbg( p_libvlc, "closing parent process" );
397             b_exit = VLC_TRUE;
398             i_ret = VLC_EEXITSUCCESS;
399         }
400         else
401         {
402             /* We are the child */
403             msg_Dbg( p_libvlc, "daemon spawned" );
404             close( STDIN_FILENO );
405             close( STDOUT_FILENO );
406             close( STDERR_FILENO );
407
408             p_libvlc->p_libvlc_global->b_daemon = VLC_TRUE;
409         }
410 #endif
411     }
412 #endif
413
414     if( b_exit )
415     {
416         config_Free( p_help_module );
417         vlc_object_destroy( p_help_module );
418         module_EndBank( p_libvlc );
419         return i_ret;
420     }
421
422     /* Check for translation config option */
423 #if defined( ENABLE_NLS ) \
424      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
425 # if defined (WIN32) || defined (__APPLE__)
426     /* This ain't really nice to have to reload the config here but it seems
427      * the only way to do it. */
428     config_LoadConfigFile( p_libvlc, "main" );
429     config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE );
430
431     /* Check if the user specified a custom language */
432     psz_language = config_GetPsz( p_libvlc, "language" );
433     if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
434     {
435         vlc_bool_t b_cache_delete = libvlc_global.p_module_bank->b_cache_delete;
436
437         /* Reset the default domain */
438         SetLanguage( psz_language );
439
440         /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
441         msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
442
443         module_EndBank( p_libvlc );
444         module_InitBank( p_libvlc );
445         config_LoadConfigFile( p_libvlc, "main" );
446         config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE );
447         libvlc_global.p_module_bank->b_cache_delete = b_cache_delete;
448     }
449     if( psz_language ) free( psz_language );
450 # endif
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_libvlc );
460     module_LoadPlugins( p_libvlc );
461     if( p_libvlc->b_die )
462     {
463         b_exit = VLC_TRUE;
464     }
465
466     msg_Dbg( p_libvlc, "module bank initialized, found %i modules",
467                     libvlc_global.p_module_bank->i_children );
468
469     /* Hack: insert the help module here */
470     vlc_object_attach( p_help_module, libvlc_global.p_module_bank );
471     /* End hack */
472
473     /* Check for help on modules */
474     if( (p_tmp = config_GetPsz( p_libvlc, "module" )) )
475     {
476         Help( p_libvlc, p_tmp );
477         free( p_tmp );
478         b_exit = VLC_TRUE;
479         i_ret = VLC_EEXITSUCCESS;
480     }
481     /* Check for long help option */
482     else if( config_GetInt( p_libvlc, "longhelp" ) )
483     {
484         Help( p_libvlc, "longhelp" );
485         b_exit = VLC_TRUE;
486         i_ret = VLC_EEXITSUCCESS;
487     }
488     /* Check for module list option */
489     else if( config_GetInt( p_libvlc, "list" ) )
490     {
491         ListModules( p_libvlc );
492         b_exit = VLC_TRUE;
493         i_ret = VLC_EEXITSUCCESS;
494     }
495
496     /* Check for config file options */
497     if( config_GetInt( p_libvlc, "reset-config" ) )
498     {
499         vlc_object_detach( p_help_module );
500         config_ResetAll( p_libvlc );
501         config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE );
502         config_SaveConfigFile( p_libvlc, NULL );
503         vlc_object_attach( p_help_module, libvlc_global.p_module_bank );
504     }
505     if( config_GetInt( p_libvlc, "save-config" ) )
506     {
507         vlc_object_detach( p_help_module );
508         config_LoadConfigFile( p_libvlc, NULL );
509         config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE );
510         config_SaveConfigFile( p_libvlc, NULL );
511         vlc_object_attach( p_help_module, libvlc_global.p_module_bank );
512     }
513
514     /* Hack: remove the help module here */
515     vlc_object_detach( p_help_module );
516     /* End hack */
517
518     if( b_exit )
519     {
520         config_Free( p_help_module );
521         vlc_object_destroy( p_help_module );
522         module_EndBank( p_libvlc );
523         return i_ret;
524     }
525
526     /*
527      * Init device values
528      */
529     InitDeviceValues( p_libvlc );
530
531     /*
532      * Override default configuration with config file settings
533      */
534     config_LoadConfigFile( p_libvlc, NULL );
535
536     /* Hack: insert the help module here */
537     vlc_object_attach( p_help_module, libvlc_global.p_module_bank );
538     /* End hack */
539
540     /*
541      * Override configuration with command line settings
542      */
543     if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_FALSE ) )
544     {
545 #ifdef WIN32
546         ShowConsole( VLC_FALSE );
547         /* Pause the console because it's destroyed when we exit */
548         fprintf( stderr, "The command line options couldn't be loaded, check "
549                  "that they are valid.\n" );
550         PauseConsole();
551 #endif
552         vlc_object_detach( p_help_module );
553         config_Free( p_help_module );
554         vlc_object_destroy( p_help_module );
555         module_EndBank( p_libvlc );
556         return VLC_EGENERIC;
557     }
558
559     /* Hack: remove the help module here */
560     vlc_object_detach( p_help_module );
561     config_Free( p_help_module );
562     vlc_object_destroy( p_help_module );
563     /* End hack */
564
565     /*
566      * System specific configuration
567      */
568     system_Configure( p_libvlc, &i_argc, ppsz_argv );
569
570     /*
571      * Message queue options
572      */
573
574     var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
575     if( config_GetInt( p_libvlc, "quiet" ) )
576     {
577         val.i_int = -1;
578         var_Set( p_libvlc, "verbose", val );
579     }
580     var_AddCallback( p_libvlc, "verbose", VerboseCallback, NULL );
581     var_Change( p_libvlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
582
583     p_libvlc->b_color = p_libvlc->b_color && config_GetInt( p_libvlc, "color" );
584
585     /*
586      * Output messages that may still be in the queue
587      */
588     msg_Flush( p_libvlc );
589
590     /* p_libvlc initialization. FIXME ? */
591
592     if( !config_GetInt( p_libvlc, "fpu" ) )
593         libvlc_global.i_cpu &= ~CPU_CAPABILITY_FPU;
594
595 #if defined( __i386__ ) || defined( __x86_64__ )
596     if( !config_GetInt( p_libvlc, "mmx" ) )
597         libvlc_global.i_cpu &= ~CPU_CAPABILITY_MMX;
598     if( !config_GetInt( p_libvlc, "3dn" ) )
599         libvlc_global.i_cpu &= ~CPU_CAPABILITY_3DNOW;
600     if( !config_GetInt( p_libvlc, "mmxext" ) )
601         libvlc_global.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
602     if( !config_GetInt( p_libvlc, "sse" ) )
603         libvlc_global.i_cpu &= ~CPU_CAPABILITY_SSE;
604     if( !config_GetInt( p_libvlc, "sse2" ) )
605         libvlc_global.i_cpu &= ~CPU_CAPABILITY_SSE2;
606 #endif
607 #if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
608     if( !config_GetInt( p_libvlc, "altivec" ) )
609         libvlc_global.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
610 #endif
611
612 #define PRINT_CAPABILITY( capability, string )                              \
613     if( libvlc_global.i_cpu & capability )                                         \
614     {                                                                       \
615         strncat( p_capabilities, string " ",                                \
616                  sizeof(p_capabilities) - strlen(p_capabilities) );         \
617         p_capabilities[sizeof(p_capabilities) - 1] = '\0';                  \
618     }
619
620     p_capabilities[0] = '\0';
621     PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
622     PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
623     PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
624     PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
625     PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
626     PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
627     PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
628     PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
629     PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
630     PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
631     msg_Dbg( p_libvlc, "CPU has capabilities %s", p_capabilities );
632
633     /*
634      * Choose the best memcpy module
635      */
636     p_libvlc->p_memcpy_module = module_Need( p_libvlc, "memcpy", "$memcpy", 0 );
637
638     if( p_libvlc->pf_memcpy == NULL )
639     {
640         p_libvlc->pf_memcpy = memcpy;
641     }
642
643     if( p_libvlc->pf_memset == NULL )
644     {
645         p_libvlc->pf_memset = memset;
646     }
647
648     p_libvlc->b_stats = config_GetInt( p_libvlc, "stats" );
649     p_libvlc->i_timers = 0;
650     p_libvlc->pp_timers = NULL;
651     vlc_mutex_init( p_libvlc, &p_libvlc->timer_lock );
652
653     /*
654      * Initialize hotkey handling
655      */
656     var_Create( p_libvlc, "key-pressed", VLC_VAR_INTEGER );
657     p_libvlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
658     /* Do a copy (we don't need to modify the strings) */
659     memcpy( p_libvlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
660
661     /* Initialize playlist and get commandline files */
662     playlist_ThreadCreate( p_libvlc );
663     if( !p_libvlc->p_playlist )
664     {
665         msg_Err( p_libvlc, "playlist initialization failed" );
666         if( p_libvlc->p_memcpy_module != NULL )
667         {
668             module_Unneed( p_libvlc, p_libvlc->p_memcpy_module );
669         }
670         module_EndBank( p_libvlc );
671         return VLC_EGENERIC;
672     }
673     p_playlist = p_libvlc->p_playlist;
674
675     psz_modules = config_GetPsz( p_playlist, "services-discovery" );
676     if( psz_modules && *psz_modules )
677     {
678         /* Add service discovery modules */
679         playlist_AddSDModules( p_playlist, psz_modules );
680     }
681     if( psz_modules ) free( psz_modules );
682
683     /*
684      * Load background interfaces
685      */
686     psz_modules = config_GetPsz( p_libvlc, "extraintf" );
687     psz_control = config_GetPsz( p_libvlc, "control" );
688
689     if( psz_modules && *psz_modules && psz_control && *psz_control )
690     {
691         psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
692                                                     strlen( psz_control ) + 1 );
693         sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
694     }
695     else if( psz_control && *psz_control )
696     {
697         if( psz_modules ) free( psz_modules );
698         psz_modules = strdup( psz_control );
699     }
700
701     psz_parser = psz_modules;
702     while ( psz_parser && *psz_parser )
703     {
704         char *psz_module, *psz_temp;
705         psz_module = psz_parser;
706         psz_parser = strchr( psz_module, ':' );
707         if ( psz_parser )
708         {
709             *psz_parser = '\0';
710             psz_parser++;
711         }
712         psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
713         if( psz_temp )
714         {
715             sprintf( psz_temp, "%s,none", psz_module );
716             VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
717             free( psz_temp );
718         }
719     }
720     if ( psz_modules )
721     {
722         free( psz_modules );
723     }
724
725     /*
726      * Always load the hotkeys interface if it exists
727      */
728     VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
729
730     /*
731      * If needed, load the Xscreensaver interface
732      * Currently, only for X
733      */
734 #ifdef HAVE_X11_XLIB_H
735     if( config_GetInt( p_libvlc, "disable-screensaver" ) == 1 )
736     {
737         VLC_AddIntf( 0, "screensaver,none", VLC_FALSE, VLC_FALSE );
738     }
739 #endif
740
741     if( config_GetInt( p_libvlc, "file-logging" ) == 1 )
742     {
743         VLC_AddIntf( 0, "logger,none", VLC_FALSE, VLC_FALSE );
744     }
745 #ifdef HAVE_SYSLOG_H
746     if( config_GetInt( p_libvlc, "syslog" ) == 1 )
747     {
748         const char *psz_logmode = "logmode=syslog";
749         libvlc_InternalAddIntf( 0, "logger,none", VLC_FALSE, VLC_FALSE,
750                                 1, &psz_logmode );
751     }
752 #endif
753
754     if( config_GetInt( p_libvlc, "show-intf" ) == 1 )
755     {
756         VLC_AddIntf( 0, "showintf,none", VLC_FALSE, VLC_FALSE );
757     }
758
759     if( config_GetInt( p_libvlc, "network-synchronisation") == 1 )
760     {
761         VLC_AddIntf( 0, "netsync,none", VLC_FALSE, VLC_FALSE );
762     }
763
764     /*
765      * FIXME: kludge to use a p_libvlc-local variable for the Mozilla plugin
766      */
767     var_Create( p_libvlc, "drawable", VLC_VAR_INTEGER );
768     var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
769     var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
770     var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
771     var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
772     var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
773     var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
774     var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
775     var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
776
777     /* Create volume callback system. */
778     var_Create( p_libvlc, "volume-change", VLC_VAR_BOOL );
779
780     /*
781      * Get input filenames given as commandline arguments
782      */
783     GetFilenames( p_libvlc, i_argc, ppsz_argv );
784
785     /*
786      * Get --open argument
787      */
788     var_Create( p_libvlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
789     var_Get( p_libvlc, "open", &val );
790     if ( val.psz_string != NULL && *val.psz_string )
791     {
792         VLC_AddTarget( p_libvlc->i_object_id, val.psz_string, NULL, 0,
793                        PLAYLIST_INSERT, 0 );
794     }
795     if ( val.psz_string != NULL ) free( val.psz_string );
796
797     return VLC_SUCCESS;
798 }
799
800 /**
801  * Cleanup a libvlc instance. The instance is not completely deallocated
802  * \param p_libvlc the instance to clean
803  */
804 int libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
805 {
806     intf_thread_t      * p_intf;
807     vout_thread_t      * p_vout;
808     aout_instance_t    * p_aout;
809     announce_handler_t * p_announce;
810
811     /* Ask the interfaces to stop and destroy them */
812     msg_Dbg( p_libvlc, "removing all interfaces" );
813     while( (p_intf = vlc_object_find( p_libvlc, VLC_OBJECT_INTF, FIND_CHILD )) )
814     {
815         intf_StopThread( p_intf );
816         vlc_object_detach( p_intf );
817         vlc_object_release( p_intf );
818         intf_Destroy( p_intf );
819     }
820
821     /* Free playlist */
822     msg_Dbg( p_libvlc, "removing playlist" );
823     playlist_ThreadDestroy( p_libvlc->p_playlist );
824
825     /* Free video outputs */
826     msg_Dbg( p_libvlc, "removing all video outputs" );
827     while( (p_vout = vlc_object_find( p_libvlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
828     {
829         vlc_object_detach( p_vout );
830         vlc_object_release( p_vout );
831         vout_Destroy( p_vout );
832     }
833
834     /* Free audio outputs */
835     msg_Dbg( p_libvlc, "removing all audio outputs" );
836     while( (p_aout = vlc_object_find( p_libvlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
837     {
838         vlc_object_detach( (vlc_object_t *)p_aout );
839         vlc_object_release( (vlc_object_t *)p_aout );
840         aout_Delete( p_aout );
841     }
842
843     stats_TimersDumpAll( p_libvlc );
844     stats_TimersClean( p_libvlc );
845
846     /* Free announce handler(s?) */
847     while( (p_announce = vlc_object_find( p_libvlc, VLC_OBJECT_ANNOUNCE,
848                                                  FIND_CHILD ) ) )
849     {
850         msg_Dbg( p_libvlc, "removing announce handler" );
851         vlc_object_detach( p_announce );
852         vlc_object_release( p_announce );
853         announce_HandlerDestroy( p_announce );
854     }
855     return VLC_SUCCESS;
856 }
857
858 /**
859  * Destroy everything.
860  * This function requests the running threads to finish, waits for their
861  * termination, and destroys their structure.
862  * It stops the thread systems: no instance can run after this has run
863  * \param p_libvlc the instance to destroy
864  * \param b_release whether we should do a release on the instance
865  */
866 int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, vlc_bool_t b_release )
867 {
868     vlc_value_t lockval;
869
870     if( p_libvlc->p_memcpy_module )
871     {
872         module_Unneed( p_libvlc, p_libvlc->p_memcpy_module );
873         p_libvlc->p_memcpy_module = NULL;
874     }
875
876     /* Free module bank. It is refcounted, so we call this each time  */
877     module_EndBank( p_libvlc );
878
879     FREENULL( p_libvlc->psz_homedir );
880     FREENULL( p_libvlc->psz_userdir );
881     FREENULL( p_libvlc->psz_configfile );
882     FREENULL( p_libvlc->p_hotkeys );
883
884     var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
885     var_Get( p_libvlc_global, "libvlc", &lockval );
886     vlc_mutex_lock( lockval.p_address );
887     i_instances--;
888
889     if( i_instances == 0 )
890     {
891         /* System specific cleaning code */
892         system_End( p_libvlc );
893
894        /* Destroy global iconv */
895         LocaleDeinit();
896     }
897     vlc_mutex_unlock( lockval.p_address );
898     var_Destroy( p_libvlc_global, "libvlc" );
899
900     msg_Flush( p_libvlc );
901     msg_Destroy( p_libvlc );
902
903     /* Destroy mutexes */
904     vlc_mutex_destroy( &p_libvlc->config_lock );
905
906     if( b_release ) vlc_object_release( p_libvlc );
907     vlc_object_destroy( p_libvlc );
908
909     /* Stop thread system: last one out please shut the door!
910      * The number of initializations of the thread system is counted, we 
911      * can call this each time */
912     vlc_threads_end( p_libvlc_global );
913
914     return VLC_SUCCESS;
915 }
916
917 /**
918  * Add an interface plugin and run it
919  */
920 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc,
921                             char const *psz_module,
922                             vlc_bool_t b_block, vlc_bool_t b_play,
923                             int i_options, const char *const *ppsz_options )
924 {
925     int i_err;
926     intf_thread_t *p_intf;
927
928 #ifndef WIN32
929     if( p_libvlc->p_libvlc_global->b_daemon && b_block && !psz_module )
930     {
931         /* Daemon mode hack.
932          * We prefer the dummy interface if none is specified. */
933         char *psz_interface = config_GetPsz( p_libvlc, "intf" );
934         if( !psz_interface || !*psz_interface ) psz_module = "dummy";
935         if( psz_interface ) free( psz_interface );
936     }
937 #endif
938
939     /* Try to create the interface */
940     p_intf = intf_Create( p_libvlc, psz_module ? psz_module : "$intf",
941                           i_options, ppsz_options );
942
943     if( p_intf == NULL )
944     {
945         msg_Err( p_libvlc, "interface \"%s\" initialization failed",
946                  psz_module );
947         return VLC_EGENERIC;
948     }
949
950     /* Interface doesn't handle play on start so do it ourselves */
951     if( !p_intf->b_play && b_play )
952         playlist_Play( p_libvlc->p_playlist );
953
954     /* Try to run the interface */
955     p_intf->b_play = b_play;
956     p_intf->b_block = b_block;
957     i_err = intf_RunThread( p_intf );
958     if( i_err )
959     {
960         vlc_object_detach( p_intf );
961         intf_Destroy( p_intf );
962         return i_err;
963     }
964     return VLC_SUCCESS;
965 };
966
967
968 /*****************************************************************************
969  * SetLanguage: set the interface language.
970  *****************************************************************************
971  * We set the LC_MESSAGES locale category for interface messages and buttons,
972  * as well as the LC_CTYPE category for string sorting and possible wide
973  * character support.
974  *****************************************************************************/
975 static void SetLanguage ( char const *psz_lang )
976 {
977 #if defined( ENABLE_NLS ) \
978      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
979
980     const char *          psz_path;
981 #if defined( __APPLE__ ) || defined ( WIN32 ) || defined( SYS_BEOS )
982     char            psz_tmp[1024];
983 #endif
984
985     if( psz_lang && !*psz_lang )
986     {
987 #   if defined( HAVE_LC_MESSAGES )
988         setlocale( LC_MESSAGES, psz_lang );
989 #   endif
990         setlocale( LC_CTYPE, psz_lang );
991     }
992     else if( psz_lang )
993     {
994 #ifdef __APPLE__
995         /* I need that under Darwin, please check it doesn't disturb
996          * other platforms. --Meuuh */
997         setenv( "LANG", psz_lang, 1 );
998
999 #elif defined( SYS_BEOS ) || defined( WIN32 )
1000         /* We set LC_ALL manually because it is the only way to set
1001          * the language at runtime under eg. Windows. Beware that this
1002          * makes the environment unconsistent when libvlc is unloaded and
1003          * should probably be moved to a safer place like vlc.c. */
1004         static char psz_lcall[20];
1005         snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1006         psz_lcall[19] = '\0';
1007         putenv( psz_lcall );
1008 #endif
1009
1010         setlocale( LC_ALL, psz_lang );
1011     }
1012
1013     /* Specify where to find the locales for current domain */
1014 #if !defined( __APPLE__ ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1015     psz_path = LOCALEDIR;
1016 #else
1017     snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc_global.psz_vlcpath,
1018               "locale" );
1019     psz_path = psz_tmp;
1020 #endif
1021     if( !bindtextdomain( PACKAGE_NAME, psz_path ) )
1022     {
1023         fprintf( stderr, "warning: couldn't bind domain %s in directory %s\n",
1024                  PACKAGE_NAME, psz_path );
1025     }
1026
1027     /* Set the default domain */
1028     bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
1029 #endif
1030 }
1031
1032 /*****************************************************************************
1033  * GetFilenames: parse command line options which are not flags
1034  *****************************************************************************
1035  * Parse command line for input files as well as their associated options.
1036  * An option always follows its associated input and begins with a ":".
1037  *****************************************************************************/
1038 static int GetFilenames( libvlc_int_t *p_vlc, int i_argc, char *ppsz_argv[] )
1039 {
1040     int i_opt, i_options;
1041
1042     /* We assume that the remaining parameters are filenames
1043      * and their input options */
1044     for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1045     {
1046         const char *psz_target;
1047         i_options = 0;
1048
1049         /* Count the input options */
1050         while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1051         {
1052             i_options++;
1053             i_opt--;
1054         }
1055
1056         /* TODO: write an internal function of this one, to avoid
1057          *       unnecessary lookups. */
1058         /* FIXME: should we convert options to UTF-8 as well ?? */
1059
1060 #ifdef WIN32
1061         if( GetVersion() < 0x80000000 )
1062         {
1063             VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[i_opt],
1064                        (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1065                                         NULL ), i_options,
1066                        PLAYLIST_INSERT, 0 );
1067         }
1068         else
1069 #endif
1070         {
1071             psz_target = FromLocale( ppsz_argv[ i_opt ] );
1072             VLC_AddTarget( p_vlc->i_object_id, psz_target,
1073                        (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1074                                         NULL ), i_options,
1075                        PLAYLIST_INSERT, 0 );
1076             LocaleFree( psz_target );
1077         }
1078     }
1079
1080     return VLC_SUCCESS;
1081 }
1082
1083 /*****************************************************************************
1084  * Help: print program help
1085  *****************************************************************************
1086  * Print a short inline help. Message interface is initialized at this stage.
1087  *****************************************************************************/
1088 static void Help( libvlc_int_t *p_this, char const *psz_help_name )
1089 {
1090 #ifdef WIN32
1091     ShowConsole( VLC_TRUE );
1092 #endif
1093
1094     if( psz_help_name && !strcmp( psz_help_name, "help" ) )
1095     {
1096         utf8_fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
1097         Usage( p_this, "help" );
1098         Usage( p_this, "main" );
1099     }
1100     else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
1101     {
1102         utf8_fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
1103         Usage( p_this, NULL );
1104     }
1105     else if( psz_help_name )
1106     {
1107         Usage( p_this, psz_help_name );
1108     }
1109
1110 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1111     PauseConsole();
1112 #endif
1113 }
1114
1115 /*****************************************************************************
1116  * Usage: print module usage
1117  *****************************************************************************
1118  * Print a short inline help. Message interface is initialized at this stage.
1119  *****************************************************************************/
1120 static void Usage( libvlc_int_t *p_this, char const *psz_module_name )
1121 {
1122 #define FORMAT_STRING "  %s --%s%s%s%s%s%s%s "
1123     /* short option ------'    | | | | | | |
1124      * option name ------------' | | | | | |
1125      * <bra ---------------------' | | | | |
1126      * option type or "" ----------' | | | |
1127      * ket> -------------------------' | | |
1128      * padding spaces -----------------' | |
1129      * comment --------------------------' |
1130      * comment suffix ---------------------'
1131      *
1132      * The purpose of having bra and ket is that we might i18n them as well.
1133      */
1134 #define LINE_START 8
1135 #define PADDING_SPACES 25
1136 #ifdef WIN32
1137 #   define OPTION_VALUE_SEP "="
1138 #else
1139 #   define OPTION_VALUE_SEP " "
1140 #endif
1141     vlc_list_t *p_list;
1142     module_t *p_parser;
1143     module_config_t *p_item;
1144     char psz_spaces_text[PADDING_SPACES+LINE_START+1];
1145     char psz_spaces_longtext[LINE_START+3];
1146     char psz_format[sizeof(FORMAT_STRING)];
1147     char psz_buffer[10000];
1148     char psz_short[4];
1149     int i_index;
1150     int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1151     vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
1152     vlc_bool_t b_description;
1153
1154     memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
1155     psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
1156     memset( psz_spaces_longtext, ' ', LINE_START+2 );
1157     psz_spaces_longtext[LINE_START+2] = '\0';
1158
1159     strcpy( psz_format, FORMAT_STRING );
1160
1161     /* List all modules */
1162     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1163
1164     /* Enumerate the config for each module */
1165     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1166     {
1167         vlc_bool_t b_help_module;
1168
1169         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1170
1171         if( psz_module_name && strcmp( psz_module_name,
1172                                        p_parser->psz_object_name ) )
1173         {
1174             continue;
1175         }
1176
1177         /* Ignore modules without config options */
1178         if( !p_parser->i_config_items )
1179         {
1180             continue;
1181         }
1182
1183         /* Ignore modules with only advanced config options if requested */
1184         if( !b_advanced )
1185         {
1186             for( p_item = p_parser->p_config;
1187                  p_item->i_type != CONFIG_HINT_END;
1188                  p_item++ )
1189             {
1190                 if( (p_item->i_type & CONFIG_ITEM) &&
1191                     !p_item->b_advanced ) break;
1192             }
1193             if( p_item->i_type == CONFIG_HINT_END ) continue;
1194         }
1195
1196         /* Print name of module */
1197         if( strcmp( "main", p_parser->psz_object_name ) )
1198         utf8_fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1199
1200         b_help_module = !strcmp( "help", p_parser->psz_object_name );
1201
1202         /* Print module options */
1203         for( p_item = p_parser->p_config;
1204              p_item->i_type != CONFIG_HINT_END;
1205              p_item++ )
1206         {
1207             char *psz_text, *psz_spaces = psz_spaces_text;
1208             const char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1209             const char *psz_suf = "", *psz_prefix = NULL;
1210             signed int i;
1211
1212             /* Skip deprecated options */
1213             if( p_item->psz_current )
1214             {
1215                 continue;
1216             }
1217             /* Skip advanced options if requested */
1218             if( p_item->b_advanced && !b_advanced )
1219             {
1220                 continue;
1221             }
1222
1223             switch( p_item->i_type )
1224             {
1225             case CONFIG_HINT_CATEGORY:
1226             case CONFIG_HINT_USAGE:
1227                 if( !strcmp( "main", p_parser->psz_object_name ) )
1228                 utf8_fprintf( stdout, "\n %s\n", p_item->psz_text );
1229                 break;
1230
1231             case CONFIG_ITEM_STRING:
1232             case CONFIG_ITEM_FILE:
1233             case CONFIG_ITEM_DIRECTORY:
1234             case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1235             case CONFIG_ITEM_MODULE_CAT:
1236             case CONFIG_ITEM_MODULE_LIST:
1237             case CONFIG_ITEM_MODULE_LIST_CAT:
1238                 psz_bra = OPTION_VALUE_SEP "<";
1239                 psz_type = _("string");
1240                 psz_ket = ">";
1241
1242                 if( p_item->ppsz_list )
1243                 {
1244                     psz_bra = OPTION_VALUE_SEP "{";
1245                     psz_type = psz_buffer;
1246                     psz_buffer[0] = '\0';
1247                     for( i = 0; p_item->ppsz_list[i]; i++ )
1248                     {
1249                         if( i ) strcat( psz_buffer, "," );
1250                         strcat( psz_buffer, p_item->ppsz_list[i] );
1251                     }
1252                     psz_ket = "}";
1253                 }
1254                 break;
1255             case CONFIG_ITEM_INTEGER:
1256             case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1257                 psz_bra = OPTION_VALUE_SEP "<";
1258                 psz_type = _("integer");
1259                 psz_ket = ">";
1260
1261                 if( p_item->i_list )
1262                 {
1263                     psz_bra = OPTION_VALUE_SEP "{";
1264                     psz_type = psz_buffer;
1265                     psz_buffer[0] = '\0';
1266                     for( i = 0; p_item->ppsz_list_text[i]; i++ )
1267                     {
1268                         if( i ) strcat( psz_buffer, ", " );
1269                         sprintf( psz_buffer + strlen(psz_buffer), "%i (%s)",
1270                                  p_item->pi_list[i],
1271                                  p_item->ppsz_list_text[i] );
1272                     }
1273                     psz_ket = "}";
1274                 }
1275                 break;
1276             case CONFIG_ITEM_FLOAT:
1277                 psz_bra = OPTION_VALUE_SEP "<";
1278                 psz_type = _("float");
1279                 psz_ket = ">";
1280                 break;
1281             case CONFIG_ITEM_BOOL:
1282                 psz_bra = ""; psz_type = ""; psz_ket = "";
1283                 if( !b_help_module )
1284                 {
1285                     psz_suf = p_item->i_value ? _(" (default enabled)") :
1286                                                 _(" (default disabled)");
1287                 }
1288                 break;
1289             }
1290
1291             if( !psz_type )
1292             {
1293                 continue;
1294             }
1295
1296             /* Add short option if any */
1297             if( p_item->i_short )
1298             {
1299                 sprintf( psz_short, "-%c,", p_item->i_short );
1300             }
1301             else
1302             {
1303                 strcpy( psz_short, "   " );
1304             }
1305
1306             i = PADDING_SPACES - strlen( p_item->psz_name )
1307                  - strlen( psz_bra ) - strlen( psz_type )
1308                  - strlen( psz_ket ) - 1;
1309
1310             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1311             {
1312                 psz_prefix =  ", --no-";
1313                 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1314             }
1315
1316             if( i < 0 )
1317             {
1318                 psz_spaces[0] = '\n';
1319                 i = 0;
1320             }
1321             else
1322             {
1323                 psz_spaces[i] = '\0';
1324             }
1325
1326             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1327             {
1328                 utf8_fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1329                          psz_prefix, p_item->psz_name, psz_bra, psz_type,
1330                          psz_ket, psz_spaces );
1331             }
1332             else
1333             {
1334                 utf8_fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1335                          "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1336             }
1337
1338             psz_spaces[i] = ' ';
1339
1340             /* We wrap the rest of the output */
1341             sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1342             b_description = config_GetInt( p_this, "help-verbose" );
1343
1344  description:
1345             psz_text = psz_buffer;
1346             while( *psz_text )
1347             {
1348                 char *psz_parser, *psz_word;
1349                 size_t i_end = strlen( psz_text );
1350
1351                 /* If the remaining text fits in a line, print it. */
1352                 if( i_end <= (size_t)i_width )
1353                 {
1354                     utf8_fprintf( stdout, "%s\n", psz_text );
1355                     break;
1356                 }
1357
1358                 /* Otherwise, eat as many words as possible */
1359                 psz_parser = psz_text;
1360                 do
1361                 {
1362                     psz_word = psz_parser;
1363                     psz_parser = strchr( psz_word, ' ' );
1364                     /* If no space was found, we reached the end of the text
1365                      * block; otherwise, we skip the space we just found. */
1366                     psz_parser = psz_parser ? psz_parser + 1
1367                                             : psz_text + i_end;
1368
1369                 } while( psz_parser - psz_text <= i_width );
1370
1371                 /* We cut a word in one of these cases:
1372                  *  - it's the only word in the line and it's too long.
1373                  *  - we used less than 80% of the width and the word we are
1374                  *    going to wrap is longer than 40% of the width, and even
1375                  *    if the word would have fit in the next line. */
1376                 if( psz_word == psz_text
1377                      || ( psz_word - psz_text < 80 * i_width / 100
1378                            && psz_parser - psz_word > 40 * i_width / 100 ) )
1379                 {
1380                     char c = psz_text[i_width];
1381                     psz_text[i_width] = '\0';
1382                     utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1383                     psz_text += i_width;
1384                     psz_text[0] = c;
1385                 }
1386                 else
1387                 {
1388                     psz_word[-1] = '\0';
1389                     utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1390                     psz_text = psz_word;
1391                 }
1392             }
1393
1394             if( b_description && p_item->psz_longtext )
1395             {
1396                 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
1397                 b_description = VLC_FALSE;
1398                 psz_spaces = psz_spaces_longtext;
1399                 utf8_fprintf( stdout, "%s", psz_spaces );
1400                 goto description;
1401             }
1402         }
1403     }
1404
1405     /* Release the module list */
1406     vlc_list_release( p_list );
1407 }
1408
1409 /*****************************************************************************
1410  * ListModules: list the available modules with their description
1411  *****************************************************************************
1412  * Print a list of all available modules (builtins and plugins) and a short
1413  * description for each one.
1414  *****************************************************************************/
1415 static void ListModules( libvlc_int_t *p_this )
1416 {
1417     vlc_list_t *p_list;
1418     module_t *p_parser;
1419     char psz_spaces[22];
1420     int i_index;
1421
1422     memset( psz_spaces, ' ', 22 );
1423
1424 #ifdef WIN32
1425     ShowConsole( VLC_TRUE );
1426 #endif
1427
1428     /* List all modules */
1429     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1430
1431     /* Enumerate each module */
1432     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1433     {
1434         int i;
1435
1436         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1437
1438         /* Nasty hack, but right now I'm too tired to think about a nice
1439          * solution */
1440         i = 22 - strlen( p_parser->psz_object_name ) - 1;
1441         if( i < 0 ) i = 0;
1442         psz_spaces[i] = 0;
1443
1444         utf8_fprintf( stdout, "  %s%s %s\n", p_parser->psz_object_name,
1445                          psz_spaces, p_parser->psz_longname );
1446
1447         psz_spaces[i] = ' ';
1448     }
1449
1450     vlc_list_release( p_list );
1451
1452 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1453     PauseConsole();
1454 #endif
1455 }
1456
1457 /*****************************************************************************
1458  * Version: print complete program version
1459  *****************************************************************************
1460  * Print complete program version and build number.
1461  *****************************************************************************/
1462 static void Version( void )
1463 {
1464 #ifdef WIN32
1465     ShowConsole( VLC_TRUE );
1466 #endif
1467
1468     utf8_fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
1469     utf8_fprintf( stdout, _("Compiled by %s@%s.%s\n"),
1470              VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
1471     utf8_fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
1472 #ifndef HAVE_SHARED_LIBVLC
1473     if( strcmp( VLC_Changeset(), "exported" ) )
1474         utf8_fprintf( stdout, _("Based upon svn changeset [%s]\n"),
1475                  VLC_Changeset() );
1476 #endif
1477     utf8_fprintf( stdout, LICENSE_MSG );
1478
1479 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1480     PauseConsole();
1481 #endif
1482 }
1483
1484 /*****************************************************************************
1485  * ShowConsole: On Win32, create an output console for debug messages
1486  *****************************************************************************
1487  * This function is useful only on Win32.
1488  *****************************************************************************/
1489 #ifdef WIN32 /*  */
1490 static void ShowConsole( vlc_bool_t b_dofile )
1491 {
1492 #   ifndef UNDER_CE
1493     FILE *f_help;
1494
1495     if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1496
1497     AllocConsole();
1498
1499     freopen( "CONOUT$", "w", stderr );
1500     freopen( "CONIN$", "r", stdin );
1501
1502     if( b_dofile && (f_help = fopen( "vlc-help.txt", "wt" )) )
1503     {
1504         fclose( f_help );
1505         freopen( "vlc-help.txt", "wt", stdout );
1506         utf8_fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
1507     }
1508
1509     else freopen( "CONOUT$", "w", stdout );
1510
1511 #   endif
1512 }
1513 #endif
1514
1515 /*****************************************************************************
1516  * PauseConsole: On Win32, wait for a key press before closing the console
1517  *****************************************************************************
1518  * This function is useful only on Win32.
1519  *****************************************************************************/
1520 #ifdef WIN32 /*  */
1521 static void PauseConsole( void )
1522 {
1523 #   ifndef UNDER_CE
1524
1525     if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1526
1527     utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1528     getchar();
1529     fclose( stdout );
1530
1531 #   endif
1532 }
1533 #endif
1534
1535 /*****************************************************************************
1536  * ConsoleWidth: Return the console width in characters
1537  *****************************************************************************
1538  * We use the stty shell command to get the console width; if this fails or
1539  * if the width is less than 80, we default to 80.
1540  *****************************************************************************/
1541 static int ConsoleWidth( void )
1542 {
1543     int i_width = 80;
1544
1545 #ifndef WIN32
1546     char buf[20], *psz_parser;
1547     FILE *file;
1548     int i_ret;
1549
1550     file = popen( "stty size 2>/dev/null", "r" );
1551     if( file )
1552     {
1553         i_ret = fread( buf, 1, 20, file );
1554         if( i_ret > 0 )
1555         {
1556             buf[19] = '\0';
1557             psz_parser = strchr( buf, ' ' );
1558             if( psz_parser )
1559             {
1560                 i_ret = atoi( psz_parser + 1 );
1561                 if( i_ret >= 80 )
1562                 {
1563                     i_width = i_ret;
1564                 }
1565             }
1566         }
1567
1568         pclose( file );
1569     }
1570 #endif
1571
1572     return i_width;
1573 }
1574
1575 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
1576                      vlc_value_t old_val, vlc_value_t new_val, void *param)
1577 {
1578     libvlc_int_t *p_libvlc = (libvlc_int_t *)p_this;
1579
1580     if( new_val.i_int >= -1 )
1581     {
1582         p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
1583     }
1584     return VLC_SUCCESS;
1585 }
1586
1587 /*****************************************************************************
1588  * InitDeviceValues: initialize device values
1589  *****************************************************************************
1590  * This function inits the dvd, vcd and cd-audio values
1591  *****************************************************************************/
1592 static void InitDeviceValues( libvlc_int_t *p_vlc )
1593 {
1594 #ifdef HAVE_HAL
1595     LibHalContext * ctx;
1596     int i, i_devices;
1597     char **devices;
1598     char *block_dev;
1599     dbus_bool_t b_dvd;
1600     DBusConnection *p_connection;
1601     DBusError       error;
1602
1603 #ifdef HAVE_HAL_1
1604     ctx =  libhal_ctx_new();
1605     if( !ctx ) return;
1606     dbus_error_init( &error );
1607     p_connection = dbus_bus_get ( DBUS_BUS_SYSTEM, &error );
1608     if( dbus_error_is_set( &error ) )
1609     {
1610         dbus_error_free( &error );
1611         return;
1612     }
1613     libhal_ctx_set_dbus_connection( ctx, p_connection );
1614     if( libhal_ctx_init( ctx, &error ) )
1615 #else
1616     if( ( ctx = hal_initialize( NULL, FALSE ) ) )
1617 #endif
1618     {
1619 #ifdef HAVE_HAL_1
1620         if( ( devices = libhal_get_all_devices( ctx, &i_devices, NULL ) ) )
1621 #else
1622         if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
1623 #endif
1624         {
1625             for( i = 0; i < i_devices; i++ )
1626             {
1627 #ifdef HAVE_HAL_1
1628                 if( !libhal_device_property_exists( ctx, devices[i],
1629                                                 "storage.cdrom.dvd", NULL ) )
1630 #else
1631                 if( !hal_device_property_exists( ctx, devices[ i ],
1632                                                 "storage.cdrom.dvd" ) )
1633 #endif
1634                 {
1635                     continue;
1636                 }
1637 #ifdef HAVE_HAL_1
1638                 b_dvd = libhal_device_get_property_bool( ctx, devices[ i ],
1639                                                  "storage.cdrom.dvd", NULL  );
1640                 block_dev = libhal_device_get_property_string( ctx,
1641                                 devices[ i ], "block.device" , NULL );
1642 #else
1643                 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
1644                                                       "storage.cdrom.dvd" );
1645                 block_dev = hal_device_get_property_string( ctx, devices[ i ],
1646                                                             "block.device" );
1647 #endif
1648                 if( b_dvd )
1649                 {
1650                     config_PutPsz( p_vlc, "dvd", block_dev );
1651                 }
1652
1653                 config_PutPsz( p_vlc, "vcd", block_dev );
1654                 config_PutPsz( p_vlc, "cd-audio", block_dev );
1655 #ifdef HAVE_HAL_1
1656                 libhal_free_string( block_dev );
1657 #else
1658                 hal_free_string( block_dev );
1659 #endif
1660             }
1661 #ifdef HAVE_HAL_1
1662             libhal_free_string_array( devices );
1663 #else
1664             hal_free_string_array( devices );
1665 #endif
1666         }
1667
1668 #ifdef HAVE_HAL_1
1669         libhal_ctx_shutdown( ctx, NULL );
1670 #else
1671         hal_shutdown( ctx );
1672 #endif
1673     }
1674     else
1675     {
1676         msg_Warn( p_vlc, "Unable to get HAL device properties" );
1677     }
1678 #endif
1679 }