]> git.sesse.net Git - vlc/blob - src/libvlc-common.c
psz_object_name should not be const! (else module name aliasing cannot work)
[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  * Preamble
34  *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <vlc/vlc.h>
40 #include "control/libvlc_internal.h"
41 #include <vlc_input.h>
42
43 #include "modules/modules.h"
44 #include "config/configuration.h"
45 #include "interface/interface.h"
46
47 #include <errno.h>                                                 /* ENOMEM */
48 #include <stdio.h>                                              /* sprintf() */
49 #include <string.h>
50 #include <stdlib.h>                                                /* free() */
51
52 #ifndef WIN32
53 #   include <netinet/in.h>                            /* BSD: struct in_addr */
54 #endif
55
56 #ifdef HAVE_UNISTD_H
57 #   include <unistd.h>
58 #elif defined( WIN32 ) && !defined( UNDER_CE )
59 #   include <io.h>
60 #endif
61
62 #ifdef WIN32                       /* optind, getopt(), included in unistd.h */
63 #   include "extras/getopt.h"
64 #endif
65
66 #ifdef HAVE_LOCALE_H
67 #   include <locale.h>
68 #endif
69
70 #ifdef HAVE_DBUS_3
71 /* used for one-instance mode */
72 #   include <dbus/dbus.h>
73 #endif
74
75 #ifdef HAVE_HAL
76 #   include <hal/libhal.h>
77 #endif
78
79 #include <vlc_playlist.h>
80 #include <vlc_interface.h>
81
82 #include <vlc_aout.h>
83 #include "audio_output/aout_internal.h"
84
85 #include <vlc_vout.h>
86
87 #include <vlc_sout.h>
88 #include "stream_output/stream_output.h"
89
90 #include <vlc_charset.h>
91
92 #include "libvlc.h"
93
94 #include "playlist/playlist_internal.h"
95
96 #include <vlc_vlm.h>
97
98 /*****************************************************************************
99  * The evil global variable. We handle it with care, don't worry.
100  *****************************************************************************/
101 static libvlc_global_data_t   libvlc_global;
102 static libvlc_global_data_t *p_libvlc_global = &libvlc_global;
103 static libvlc_int_t *    p_static_vlc = NULL;
104 static volatile unsigned int i_instances = 0;
105
106 /*****************************************************************************
107  * Local prototypes
108  *****************************************************************************/
109 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
110     ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
111 static void SetLanguage   ( char const * );
112 #endif
113 static inline int LoadMessages (void);
114 static int  GetFilenames  ( libvlc_int_t *, int, const char *[] );
115 static void Help          ( libvlc_int_t *, char const *psz_help_name );
116 static void Usage         ( libvlc_int_t *, char const *psz_module_name );
117 static void ListModules   ( libvlc_int_t *, bool );
118 static void Version       ( void );
119
120 #ifdef WIN32
121 static void ShowConsole   ( bool );
122 static void PauseConsole  ( void );
123 #endif
124 static int  ConsoleWidth  ( void );
125
126 static int  VerboseCallback( vlc_object_t *, char const *,
127                              vlc_value_t, vlc_value_t, void * );
128
129 static void InitDeviceValues( libvlc_int_t * );
130
131 libvlc_global_data_t *vlc_global( void )
132 {
133     return p_libvlc_global;
134 }
135
136 /*****************************************************************************
137  * vlc_current_object: return the current object.
138  *****************************************************************************
139  * If i_object is non-zero, return the corresponding object. Otherwise,
140  * return the statically allocated p_vlc object.
141  *****************************************************************************/
142 libvlc_int_t * vlc_current_object( int i_object )
143 {
144     return i_object ? vlc_object_get( i_object ) : p_static_vlc;
145 }
146
147
148 /**
149  * Allocate a libvlc instance, initialize global data if needed
150  * It also initializes the threading system
151  */
152 libvlc_int_t * libvlc_InternalCreate( void )
153 {
154     int i_ret;
155     libvlc_int_t * p_libvlc = NULL;
156     char *psz_env = NULL;
157
158     /* vlc_threads_init *must* be the first internal call! No other call is
159      * allowed before the thread system has been initialized. */
160     i_ret = vlc_threads_init( p_libvlc_global );
161     if( i_ret < 0 ) return NULL;
162
163     /* Now that the thread system is initialized, we don't have much, but
164      * at least we have variables */
165     vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
166
167     i_instances++;
168
169     if( !p_libvlc_global->b_ready )
170     {
171         /* Guess what CPU we have */
172         cpu_flags = CPUCapabilities();
173        /* The module bank will be initialized later */
174         p_libvlc_global->p_module_bank = NULL;
175
176         p_libvlc_global->b_ready = true;
177     }
178     vlc_mutex_unlock( lock );
179
180     /* Allocate a libvlc instance object */
181     p_libvlc = vlc_object_create( p_libvlc_global, VLC_OBJECT_LIBVLC );
182     if( p_libvlc == NULL )
183     {
184         i_instances--;
185         return NULL;
186     }
187     p_libvlc->p_playlist = NULL;
188     p_libvlc->p_interaction = NULL;
189     p_libvlc->p_vlm = NULL;
190     p_libvlc->psz_object_name = strdup( "libvlc" );
191
192     /* Initialize message queue */
193     msg_Create( p_libvlc );
194
195     /* Find verbosity from VLC_VERBOSE environment variable */
196     psz_env = getenv( "VLC_VERBOSE" );
197     if( psz_env != NULL )
198         p_libvlc->i_verbose = atoi( psz_env );
199     else
200         p_libvlc->i_verbose = 3;
201 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
202     p_libvlc->b_color = isatty( 2 ); /* 2 is for stderr */
203 #else
204     p_libvlc->b_color = false;
205 #endif
206
207     /* Announce who we are - Do it only for first instance ? */
208     msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
209     msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
210
211     /* Initialize mutexes */
212     vlc_mutex_init( p_libvlc, &p_libvlc->timer_lock );
213     vlc_mutex_init( p_libvlc, &p_libvlc->config_lock );
214 #ifdef __APPLE__
215     vlc_mutex_init( p_libvlc, &p_libvlc->quicktime_lock );
216     vlc_thread_set_priority( p_libvlc, VLC_THREAD_PRIORITY_LOW );
217 #endif
218     /* Store data for the non-reentrant API */
219     p_static_vlc = p_libvlc;
220
221     return p_libvlc;
222 }
223
224 /**
225  * Initialize a libvlc instance
226  * This function initializes a previously allocated libvlc instance:
227  *  - CPU detection
228  *  - gettext initialization
229  *  - message queue, module bank and playlist initialization
230  *  - configuration and commandline parsing
231  */
232 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
233                          const char *ppsz_argv[] )
234 {
235     char         p_capabilities[200];
236     char *       p_tmp = NULL;
237     char *       psz_modules = NULL;
238     char *       psz_parser = NULL;
239     char *       psz_control = NULL;
240     bool   b_exit = false;
241     int          i_ret = VLC_EEXIT;
242     playlist_t  *p_playlist = NULL;
243     vlc_value_t  val;
244 #if defined( ENABLE_NLS ) \
245      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
246 # if defined (WIN32) || defined (__APPLE__)
247     char *       psz_language;
248 #endif
249 #endif
250
251     /* System specific initialization code */
252     system_Init( p_libvlc, &i_argc, ppsz_argv );
253
254     /* Get the executable name (similar to the basename command) */
255     if( i_argc > 0 )
256     {
257         const char *exe = strdup( ppsz_argv[0] );
258         const char *tmp = exe;
259         while( *exe )
260         {
261             if( *exe++ == '/' )
262                 tmp = exe;
263         }
264         p_libvlc->psz_object_name = strdup( tmp );
265     }
266     else
267     {
268         p_libvlc->psz_object_name = strdup( "vlc" );
269     }
270
271     /*
272      * Support for gettext
273      */
274     LoadMessages ();
275
276     /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
277     msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
278
279     /* Initialize the module bank and load the configuration of the
280      * main module. We need to do this at this stage to be able to display
281      * a short help if required by the user. (short help == main module
282      * options) */
283     module_InitBank( p_libvlc );
284
285     if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true ) )
286     {
287         module_EndBank( p_libvlc );
288         return VLC_EGENERIC;
289     }
290
291     /* Check for short help option */
292     if( config_GetInt( p_libvlc, "help" ) > 0 )
293     {
294         Help( p_libvlc, "help" );
295         b_exit = true;
296         i_ret = VLC_EEXITSUCCESS;
297     }
298     /* Check for version option */
299     else if( config_GetInt( p_libvlc, "version" ) > 0 )
300     {
301         Version();
302         b_exit = true;
303         i_ret = VLC_EEXITSUCCESS;
304     }
305
306     /* Set the config file stuff */
307     p_libvlc->psz_homedir    = config_GetHomeDir();
308     p_libvlc->psz_configdir  = config_GetConfigDir( p_libvlc );
309     p_libvlc->psz_datadir    = config_GetUserDataDir( p_libvlc );
310     p_libvlc->psz_cachedir   = config_GetCacheDir( p_libvlc );
311     p_libvlc->psz_configfile = config_GetCustomConfigFile( p_libvlc );
312
313     /* Check for plugins cache options */
314     if( config_GetInt( p_libvlc, "reset-plugins-cache" ) > 0 )
315     {
316         p_libvlc_global->p_module_bank->b_cache_delete = true;
317     }
318
319     /* Will be re-done properly later on */
320     p_libvlc->i_verbose = config_GetInt( p_libvlc, "verbose" );
321
322     /* Check for daemon mode */
323 #ifndef WIN32
324     if( config_GetInt( p_libvlc, "daemon" ) )
325     {
326 #ifdef HAVE_DAEMON
327         char *psz_pidfile = NULL;
328
329         if( daemon( 1, 0) != 0 )
330         {
331             msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
332             b_exit = true;
333         }
334         p_libvlc_global->b_daemon = true;
335
336         /* lets check if we need to write the pidfile */
337         psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
338         if( psz_pidfile != NULL )
339         {
340             FILE *pidfile;
341             pid_t i_pid = getpid ();
342             msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
343                                i_pid, psz_pidfile );
344             pidfile = utf8_fopen( psz_pidfile,"w" );
345             if( pidfile != NULL )
346             {
347                 utf8_fprintf( pidfile, "%d", (int)i_pid );
348                 fclose( pidfile );
349             }
350             else
351             {
352                 msg_Err( p_libvlc, "cannot open pid file for writing: %s (%m)",
353                          psz_pidfile );
354             }
355         }
356         free( psz_pidfile );
357
358 #else
359         pid_t i_pid;
360
361         if( ( i_pid = fork() ) < 0 )
362         {
363             msg_Err( p_libvlc, "unable to fork vlc to daemon mode" );
364             b_exit = true;
365         }
366         else if( i_pid )
367         {
368             /* This is the parent, exit right now */
369             msg_Dbg( p_libvlc, "closing parent process" );
370             b_exit = true;
371             i_ret = VLC_EEXITSUCCESS;
372         }
373         else
374         {
375             /* We are the child */
376             msg_Dbg( p_libvlc, "daemon spawned" );
377             close( STDIN_FILENO );
378             close( STDOUT_FILENO );
379             close( STDERR_FILENO );
380
381             p_libvlc_global->b_daemon = true;
382         }
383 #endif
384     }
385 #endif
386
387     if( b_exit )
388     {
389         module_EndBank( p_libvlc );
390         return i_ret;
391     }
392
393     /* Check for translation config option */
394 #if defined( ENABLE_NLS ) \
395      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
396 # if defined (WIN32) || defined (__APPLE__)
397     /* This ain't really nice to have to reload the config here but it seems
398      * the only way to do it. */
399
400     if( !config_GetInt( p_libvlc, "ignore-config" ) )
401         config_LoadConfigFile( p_libvlc, "main" );
402     config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
403
404     /* Check if the user specified a custom language */
405     psz_language = config_GetPsz( p_libvlc, "language" );
406     if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
407     {
408         bool b_cache_delete = p_libvlc_global->p_module_bank->b_cache_delete;
409
410         /* Reset the default domain */
411         SetLanguage( psz_language );
412
413         /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
414         msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
415
416         module_EndBank( p_libvlc );
417         module_InitBank( p_libvlc );
418         if( !config_GetInt( p_libvlc, "ignore-config" ) )
419             config_LoadConfigFile( p_libvlc, "main" );
420         config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
421         p_libvlc_global->p_module_bank->b_cache_delete = b_cache_delete;
422     }
423     free( psz_language );
424 # endif
425 #endif
426
427     /*
428      * Load the builtins and plugins into the module_bank.
429      * We have to do it before config_Load*() because this also gets the
430      * list of configuration options exported by each module and loads their
431      * default values.
432      */
433     module_LoadBuiltins( p_libvlc );
434     module_LoadPlugins( p_libvlc );
435     if( p_libvlc->b_die )
436     {
437         b_exit = true;
438     }
439
440     msg_Dbg( p_libvlc, "module bank initialized, found %i modules",
441              p_libvlc_global->p_module_bank->i_children );
442
443     /* Check for help on modules */
444     if( (p_tmp = config_GetPsz( p_libvlc, "module" )) )
445     {
446         Help( p_libvlc, p_tmp );
447         free( p_tmp );
448         b_exit = true;
449         i_ret = VLC_EEXITSUCCESS;
450     }
451     /* Check for long help option */
452     else if( config_GetInt( p_libvlc, "longhelp" ) > 0 )
453     {
454         Help( p_libvlc, "longhelp" );
455         b_exit = true;
456         i_ret = VLC_EEXITSUCCESS;
457     }
458     /* Check for module list option */
459     else if( config_GetInt( p_libvlc, "list" ) > 0 )
460     {
461         ListModules( p_libvlc, false );
462         b_exit = true;
463         i_ret = VLC_EEXITSUCCESS;
464     }
465     else if( config_GetInt( p_libvlc, "list-verbose" ) > 0 )
466     {
467         ListModules( p_libvlc, true );
468         b_exit = true;
469         i_ret = VLC_EEXITSUCCESS;
470     }
471
472     /* Check for config file options */
473     if( !config_GetInt( p_libvlc, "ignore-config" ) )
474     {
475         if( config_GetInt( p_libvlc, "reset-config" ) > 0 )
476         {
477             config_ResetAll( p_libvlc );
478             config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
479             config_SaveConfigFile( p_libvlc, NULL );
480         }
481         if( config_GetInt( p_libvlc, "save-config" ) > 0 )
482         {
483             config_LoadConfigFile( p_libvlc, NULL );
484             config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
485             config_SaveConfigFile( p_libvlc, NULL );
486         }
487     }
488
489     if( b_exit )
490     {
491         module_EndBank( p_libvlc );
492         return i_ret;
493     }
494
495     /*
496      * Init device values
497      */
498     InitDeviceValues( p_libvlc );
499
500     /*
501      * Override default configuration with config file settings
502      */
503     if( !config_GetInt( p_libvlc, "ignore-config" ) )
504         config_LoadConfigFile( p_libvlc, NULL );
505
506     /*
507      * Override configuration with command line settings
508      */
509     if( config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, false ) )
510     {
511 #ifdef WIN32
512         ShowConsole( false );
513         /* Pause the console because it's destroyed when we exit */
514         fprintf( stderr, "The command line options couldn't be loaded, check "
515                  "that they are valid.\n" );
516         PauseConsole();
517 #endif
518         module_EndBank( p_libvlc );
519         return VLC_EGENERIC;
520     }
521
522     /*
523      * System specific configuration
524      */
525     system_Configure( p_libvlc, &i_argc, ppsz_argv );
526
527 /* FIXME: could be replaced by using Unix sockets */
528 #ifdef HAVE_DBUS_3
529     dbus_threads_init_default();
530
531     if( config_GetInt( p_libvlc, "one-instance" ) > 0 )
532     {
533         /* Initialise D-Bus interface, check for other instances */
534         DBusConnection  *p_conn = NULL;
535         DBusError       dbus_error;
536
537         dbus_error_init( &dbus_error );
538
539         /* connect to the session bus */
540         p_conn = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
541         if( !p_conn )
542         {
543             msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
544                     dbus_error.message );
545             dbus_error_free( &dbus_error );
546         }
547         else
548         {
549             /* check if VLC is available on the bus
550              * if not: D-Bus control is not enabled on the other
551              * instance and we can't pass MRLs to it */
552             DBusMessage *p_test_msg = NULL;
553             DBusMessage *p_test_reply = NULL;
554             p_test_msg =  dbus_message_new_method_call(
555                     "org.mpris.vlc", "/",
556                     "org.freedesktop.MediaPlayer", "Identity" );
557             /* block until a reply arrives */
558             p_test_reply = dbus_connection_send_with_reply_and_block(
559                     p_conn, p_test_msg, -1, &dbus_error );
560             dbus_message_unref( p_test_msg );
561             if( p_test_reply == NULL )
562             {
563                 dbus_error_free( &dbus_error );
564                 msg_Dbg( p_libvlc, "No Media Player is running. "
565                         "Continuing normally." );
566             }
567             else
568             {
569                 int i_input;
570                 DBusMessage* p_dbus_msg = NULL;
571                 DBusMessageIter dbus_args;
572                 DBusPendingCall* p_dbus_pending = NULL;
573                 dbus_bool_t b_play;
574
575                 dbus_message_unref( p_test_reply );
576                 msg_Warn( p_libvlc, "Another Media Player is running. Exiting");
577
578                 for( i_input = optind;i_input < i_argc;i_input++ )
579                 {
580                     msg_Dbg( p_libvlc, "Adds %s to the running Media Player",
581                             ppsz_argv[i_input] );
582
583                     p_dbus_msg = dbus_message_new_method_call(
584                             "org.mpris.vlc", "/TrackList",
585                             "org.freedesktop.MediaPlayer", "AddTrack" );
586
587                     if ( NULL == p_dbus_msg )
588                     {
589                         msg_Err( p_libvlc, "D-Bus problem" );
590                         system_End( p_libvlc );
591                         exit( VLC_ETIMEOUT );
592                     }
593
594                     /* append MRLs */
595                     dbus_message_iter_init_append( p_dbus_msg, &dbus_args );
596                     if ( !dbus_message_iter_append_basic( &dbus_args,
597                                 DBUS_TYPE_STRING, &ppsz_argv[i_input] ) )
598                     {
599                         msg_Err( p_libvlc, "Out of memory" );
600                         dbus_message_unref( p_dbus_msg );
601                         system_End( p_libvlc );
602                         exit( VLC_ENOMEM );
603                     }
604                     b_play = TRUE;
605                     if( config_GetInt( p_libvlc, "playlist-enqueue" ) > 0 )
606                         b_play = FALSE;
607                     if ( !dbus_message_iter_append_basic( &dbus_args,
608                                 DBUS_TYPE_BOOLEAN, &b_play ) )
609                     {
610                         msg_Err( p_libvlc, "Out of memory" );
611                         dbus_message_unref( p_dbus_msg );
612                         system_End( p_libvlc );
613                         exit( VLC_ENOMEM );
614                     }
615
616                     /* send message and get a handle for a reply */
617                     if ( !dbus_connection_send_with_reply ( p_conn,
618                                 p_dbus_msg, &p_dbus_pending, -1 ) )
619                     {
620                         msg_Err( p_libvlc, "D-Bus problem" );
621                         dbus_message_unref( p_dbus_msg );
622                         system_End( p_libvlc );
623                         exit( VLC_ETIMEOUT );
624                     }
625
626                     if ( NULL == p_dbus_pending )
627                     {
628                         msg_Err( p_libvlc, "D-Bus problem" );
629                         dbus_message_unref( p_dbus_msg );
630                         system_End( p_libvlc );
631                         exit( VLC_ETIMEOUT );
632                     }
633                     dbus_connection_flush( p_conn );
634                     dbus_message_unref( p_dbus_msg );
635                     /* block until we receive a reply */
636                     dbus_pending_call_block( p_dbus_pending );
637                     dbus_pending_call_unref( p_dbus_pending );
638                 } /* processes all command line MRLs */
639
640                 /* bye bye */
641                 system_End( p_libvlc );
642                 exit( VLC_SUCCESS );
643             }
644         }
645         /* we unreference the connection when we've finished with it */
646         if( p_conn ) dbus_connection_unref( p_conn );
647     }
648 #endif
649
650     /*
651      * Message queue options
652      */
653
654     var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
655     if( config_GetInt( p_libvlc, "quiet" ) > 0 )
656     {
657         val.i_int = -1;
658         var_Set( p_libvlc, "verbose", val );
659     }
660     var_AddCallback( p_libvlc, "verbose", VerboseCallback, NULL );
661     var_Change( p_libvlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
662
663     if( p_libvlc->b_color )
664         p_libvlc->b_color = config_GetInt( p_libvlc, "color" ) > 0;
665
666     /*
667      * Output messages that may still be in the queue
668      */
669     msg_Flush( p_libvlc );
670
671     if( !config_GetInt( p_libvlc, "fpu" ) )
672         cpu_flags &= ~CPU_CAPABILITY_FPU;
673
674 #if defined( __i386__ ) || defined( __x86_64__ )
675     if( !config_GetInt( p_libvlc, "mmx" ) )
676         cpu_flags &= ~CPU_CAPABILITY_MMX;
677     if( !config_GetInt( p_libvlc, "3dn" ) )
678         cpu_flags &= ~CPU_CAPABILITY_3DNOW;
679     if( !config_GetInt( p_libvlc, "mmxext" ) )
680         cpu_flags &= ~CPU_CAPABILITY_MMXEXT;
681     if( !config_GetInt( p_libvlc, "sse" ) )
682         cpu_flags &= ~CPU_CAPABILITY_SSE;
683     if( !config_GetInt( p_libvlc, "sse2" ) )
684         cpu_flags &= ~CPU_CAPABILITY_SSE2;
685 #endif
686 #if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
687     if( !config_GetInt( p_libvlc, "altivec" ) )
688         cpu_flags &= ~CPU_CAPABILITY_ALTIVEC;
689 #endif
690
691 #define PRINT_CAPABILITY( capability, string )                              \
692     if( vlc_CPU() & capability )                                            \
693     {                                                                       \
694         strncat( p_capabilities, string " ",                                \
695                  sizeof(p_capabilities) - strlen(p_capabilities) );         \
696         p_capabilities[sizeof(p_capabilities) - 1] = '\0';                  \
697     }
698
699     p_capabilities[0] = '\0';
700     PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
701     PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
702     PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
703     PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
704     PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
705     PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
706     PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
707     PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
708     PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
709     PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
710     msg_Dbg( p_libvlc, "CPU has capabilities %s", p_capabilities );
711
712     /*
713      * Choose the best memcpy module
714      */
715     p_libvlc->p_memcpy_module = module_Need( p_libvlc, "memcpy", "$memcpy", 0 );
716
717     if( p_libvlc->pf_memcpy == NULL )
718     {
719         p_libvlc->pf_memcpy = memcpy;
720     }
721
722     if( p_libvlc->pf_memset == NULL )
723     {
724         p_libvlc->pf_memset = memset;
725     }
726
727     p_libvlc->b_stats = config_GetInt( p_libvlc, "stats" ) > 0;
728     p_libvlc->i_timers = 0;
729     p_libvlc->pp_timers = NULL;
730
731     /* Init stats */
732     p_libvlc->p_stats = (global_stats_t *)malloc( sizeof( global_stats_t ) );
733     if( !p_libvlc->p_stats )
734     {
735         vlc_object_release( p_libvlc );
736         return VLC_ENOMEM;
737     }
738     vlc_mutex_init( p_libvlc, &p_libvlc->p_stats->lock );
739     p_libvlc->p_stats_computer = NULL;
740
741     /* Init the array that holds every input item */
742     ARRAY_INIT( p_libvlc->input_items );
743     p_libvlc->i_last_input_id = 0;
744
745     /*
746      * Initialize hotkey handling
747      */
748     var_Create( p_libvlc, "key-pressed", VLC_VAR_INTEGER );
749     var_Create( p_libvlc, "key-action", VLC_VAR_INTEGER );
750     p_libvlc->p_hotkeys = malloc( libvlc_hotkeys_size );
751     /* Do a copy (we don't need to modify the strings) */
752     memcpy( p_libvlc->p_hotkeys, libvlc_hotkeys, libvlc_hotkeys_size );
753     var_AddCallback( p_libvlc, "key-pressed", vlc_key_to_action,
754                      p_libvlc->p_hotkeys );
755
756     /* Initialize interaction */
757     p_libvlc->p_interaction = interaction_Init( p_libvlc );
758
759     /* Initialize playlist and get commandline files */
760     playlist_ThreadCreate( p_libvlc );
761     if( !p_libvlc->p_playlist )
762     {
763         msg_Err( p_libvlc, "playlist initialization failed" );
764         if( p_libvlc->p_memcpy_module != NULL )
765         {
766             module_Unneed( p_libvlc, p_libvlc->p_memcpy_module );
767         }
768         module_EndBank( p_libvlc );
769         return VLC_EGENERIC;
770     }
771     p_playlist = p_libvlc->p_playlist;
772
773     psz_modules = config_GetPsz( p_playlist, "services-discovery" );
774     if( psz_modules && *psz_modules )
775     {
776         /* Add service discovery modules */
777         playlist_ServicesDiscoveryAdd( p_playlist, psz_modules );
778     }
779     free( psz_modules );
780
781     /* Initialize VLM if vlm-conf is specified */
782     psz_parser = config_GetPsz( p_libvlc, "vlm-conf" );
783     if( psz_parser && *psz_parser )
784     {
785         p_libvlc->p_vlm = vlm_New( p_libvlc );
786         if( !p_libvlc->p_vlm )
787             msg_Err( p_libvlc, "VLM initialization failed" );
788     }
789     free( psz_parser );
790
791     /*
792      * Load background interfaces
793      */
794     psz_modules = config_GetPsz( p_libvlc, "extraintf" );
795     psz_control = config_GetPsz( p_libvlc, "control" );
796
797     if( psz_modules && *psz_modules && psz_control && *psz_control )
798     {
799         psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
800                                                     strlen( psz_control ) + 1 );
801         sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
802     }
803     else if( psz_control && *psz_control )
804     {
805         free( psz_modules );
806         psz_modules = strdup( psz_control );
807     }
808
809     psz_parser = psz_modules;
810     while ( psz_parser && *psz_parser )
811     {
812         char *psz_module, *psz_temp;
813         psz_module = psz_parser;
814         psz_parser = strchr( psz_module, ':' );
815         if ( psz_parser )
816         {
817             *psz_parser = '\0';
818             psz_parser++;
819         }
820         psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
821         if( psz_temp )
822         {
823             sprintf( psz_temp, "%s,none", psz_module );
824             VLC_AddIntf( 0, psz_temp, false, false );
825             free( psz_temp );
826         }
827     }
828     free( psz_modules );
829     free( psz_control );
830
831     /*
832      * Always load the hotkeys interface if it exists
833      */
834     VLC_AddIntf( 0, "hotkeys,none", false, false );
835
836 #ifdef HAVE_DBUS_3
837     /* loads dbus control interface if in one-instance mode
838      * we do it only when playlist exists, because dbus module needs it */
839     if( config_GetInt( p_libvlc, "one-instance" ) > 0 )
840         VLC_AddIntf( 0, "dbus,none", false, false );
841
842     /* Prevents the power management daemon to suspend the computer
843      * when VLC is active */
844     if( config_GetInt( p_libvlc, "inhibit" ) > 0 )
845         VLC_AddIntf( 0, "inhibit,none", false, false );
846 #endif
847
848     /*
849      * If needed, load the Xscreensaver interface
850      * Currently, only for X
851      */
852 #ifdef HAVE_X11_XLIB_H
853     if( config_GetInt( p_libvlc, "disable-screensaver" ) )
854     {
855         VLC_AddIntf( 0, "screensaver,none", false, false );
856     }
857 #endif
858
859     if( config_GetInt( p_libvlc, "file-logging" ) > 0 )
860     {
861         VLC_AddIntf( 0, "logger,none", false, false );
862     }
863 #ifdef HAVE_SYSLOG_H
864     if( config_GetInt( p_libvlc, "syslog" ) > 0 )
865     {
866         const char *psz_logmode = "logmode=syslog";
867         libvlc_InternalAddIntf( p_libvlc, "logger,none", false, false,
868                                 1, &psz_logmode );
869     }
870 #endif
871
872     if( config_GetInt( p_libvlc, "show-intf" ) > 0 )
873     {
874         VLC_AddIntf( 0, "showintf,none", false, false );
875     }
876
877     if( config_GetInt( p_libvlc, "network-synchronisation") > 0 )
878     {
879         VLC_AddIntf( 0, "netsync,none", false, false );
880     }
881
882 #ifdef WIN32
883     if( config_GetInt( p_libvlc, "prefer-system-codecs") > 0 )
884     {
885         char *psz_codecs = config_GetPsz( p_playlist, "codec" );
886         if( psz_codecs )
887         {
888             char *psz_morecodecs;
889             asprintf(&psz_morecodecs, "%s,dmo,quicktime", psz_codecs);
890             if( psz_morecodecs )
891             {
892                 config_PutPsz( p_libvlc, "codec", psz_morecodecs);
893                 free( psz_morecodecs );
894             }
895         }
896         else
897             config_PutPsz( p_libvlc, "codec", "dmo,quicktime");
898         free( psz_codecs );
899     }
900 #endif
901
902     /*
903      * FIXME: kludge to use a p_libvlc-local variable for the Mozilla plugin
904      */
905     var_Create( p_libvlc, "drawable", VLC_VAR_INTEGER );
906     var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
907     var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
908     var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
909     var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
910     var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
911     var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
912     var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
913     var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
914
915     /* Create volume callback system. */
916     var_Create( p_libvlc, "volume-change", VLC_VAR_BOOL );
917
918     /*
919      * Get input filenames given as commandline arguments
920      */
921     GetFilenames( p_libvlc, i_argc, ppsz_argv );
922
923     /*
924      * Get --open argument
925      */
926     var_Create( p_libvlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
927     var_Get( p_libvlc, "open", &val );
928     if ( val.psz_string != NULL && *val.psz_string )
929     {
930         VLC_AddTarget( p_libvlc->i_object_id, val.psz_string, NULL, 0,
931                        PLAYLIST_INSERT, 0 );
932     }
933     free( val.psz_string );
934
935     return VLC_SUCCESS;
936 }
937
938 /**
939  * Cleanup a libvlc instance. The instance is not completely deallocated
940  * \param p_libvlc the instance to clean
941  */
942 int libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
943 {
944     intf_thread_t      * p_intf = NULL;
945     vout_thread_t      * p_vout = NULL;
946     aout_instance_t    * p_aout = NULL;
947     announce_handler_t * p_announce = NULL;
948     sout_instance_t    * p_sout = NULL;
949
950     /* Ask the interfaces to stop and destroy them */
951     msg_Dbg( p_libvlc, "removing all interfaces" );
952     while( (p_intf = vlc_object_find( p_libvlc, VLC_OBJECT_INTF, FIND_CHILD )) )
953     {
954         intf_StopThread( p_intf );
955         vlc_object_detach( p_intf );
956         vlc_object_release( p_intf );
957         intf_Destroy( p_intf );
958         p_intf = NULL;
959     }
960
961     /* Free playlist */
962     msg_Dbg( p_libvlc, "removing playlist" );
963     playlist_ThreadDestroy( p_libvlc->p_playlist );
964
965     /* Free video outputs */
966     msg_Dbg( p_libvlc, "removing all video outputs" );
967     while( (p_vout = vlc_object_find( p_libvlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
968     {
969         vlc_object_detach( p_vout );
970         vlc_object_release( p_vout );
971         vout_Destroy( p_vout );
972     }
973
974     /* Free audio outputs */
975     msg_Dbg( p_libvlc, "removing all audio outputs" );
976     while( (p_aout = vlc_object_find( p_libvlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
977     {
978         vlc_object_detach( (vlc_object_t *)p_aout );
979         vlc_object_release( (vlc_object_t *)p_aout );
980         aout_Delete( p_aout );
981     }
982
983     p_sout = vlc_object_find( p_libvlc, VLC_OBJECT_SOUT, FIND_CHILD );
984     if( p_sout )
985     {
986         msg_Dbg( p_sout, "removing kept stream output" );
987         vlc_object_detach( (vlc_object_t*)p_sout );
988         vlc_object_release( (vlc_object_t*)p_sout );
989         sout_DeleteInstance( p_sout );
990     }
991
992     /* Destroy VLM if created in libvlc_InternalInit */
993     if( p_libvlc->p_vlm )
994     {
995         vlm_Delete( p_libvlc->p_vlm );
996     }
997
998     /* Free interaction */
999     msg_Dbg( p_libvlc, "removing interaction" );
1000     vlc_object_release( p_libvlc->p_interaction );
1001
1002     stats_TimersDumpAll( p_libvlc );
1003     stats_TimersCleanAll( p_libvlc );
1004
1005     /* Free announce handler(s?) */
1006     while( (p_announce = vlc_object_find( p_libvlc, VLC_OBJECT_ANNOUNCE,
1007                                                  FIND_CHILD ) ) )
1008     {
1009         msg_Dbg( p_libvlc, "removing announce handler" );
1010         vlc_object_detach( p_announce );
1011         vlc_object_release( p_announce );
1012         announce_HandlerDestroy( p_announce );
1013     }
1014
1015     bool b_clean = true;
1016     FOREACH_ARRAY( input_item_t *p_del, p_libvlc->input_items )
1017         msg_Err( p_libvlc, "input item %p has not been deleted properly: refcount %d, name %s",
1018             p_del, p_del->i_gc_refcount, p_del->psz_name ? p_del->psz_name : "(null)" );
1019         b_clean = false;
1020     FOREACH_END();
1021     assert( b_clean );
1022     ARRAY_RESET( p_libvlc->input_items );
1023
1024     msg_Dbg( p_libvlc, "removing stats" );
1025     vlc_mutex_destroy( &p_libvlc->p_stats->lock );
1026     FREENULL( p_libvlc->p_stats );
1027
1028     return VLC_SUCCESS;
1029 }
1030
1031 /**
1032  * Destroy everything.
1033  * This function requests the running threads to finish, waits for their
1034  * termination, and destroys their structure.
1035  * It stops the thread systems: no instance can run after this has run
1036  * \param p_libvlc the instance to destroy
1037  * \param b_release whether we should do a release on the instance
1038  */
1039 int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, bool b_release )
1040 {
1041     if( !p_libvlc )
1042         return VLC_EGENERIC;
1043
1044 #ifndef WIN32
1045     char* psz_pidfile = NULL;
1046
1047     if( config_GetInt( p_libvlc, "daemon" ) > 0 )
1048     {
1049         psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
1050         if( psz_pidfile != NULL )
1051         {
1052             msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
1053             if( unlink( psz_pidfile ) == -1 )
1054             {
1055                 msg_Dbg( p_libvlc, "removing pid file %s: %m",
1056                         psz_pidfile );
1057             }
1058         }
1059         free( psz_pidfile );
1060     }
1061 #endif
1062
1063     if( p_libvlc->p_memcpy_module )
1064     {
1065         module_Unneed( p_libvlc, p_libvlc->p_memcpy_module );
1066         p_libvlc->p_memcpy_module = NULL;
1067     }
1068
1069     /* Free module bank. It is refcounted, so we call this each time  */
1070     module_EndBank( p_libvlc );
1071
1072     FREENULL( p_libvlc->psz_homedir );
1073     FREENULL( p_libvlc->psz_configdir );
1074     FREENULL( p_libvlc->psz_datadir );
1075     FREENULL( p_libvlc->psz_cachedir );
1076     FREENULL( p_libvlc->psz_configfile );
1077     var_DelCallback( p_libvlc, "key-pressed", vlc_key_to_action,
1078                      p_libvlc->p_hotkeys );
1079     FREENULL( p_libvlc->p_hotkeys );
1080
1081     vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
1082     i_instances--;
1083
1084     if( i_instances == 0 )
1085     {
1086         /* System specific cleaning code */
1087         system_End( p_libvlc );
1088     }
1089     vlc_mutex_unlock( lock );
1090
1091     msg_Flush( p_libvlc );
1092     msg_Destroy( p_libvlc );
1093
1094     /* Destroy mutexes */
1095     vlc_mutex_destroy( &p_libvlc->config_lock );
1096     vlc_mutex_destroy( &p_libvlc->timer_lock );
1097
1098     if( b_release ) vlc_object_release( p_libvlc );
1099     vlc_object_release( p_libvlc );
1100     p_libvlc = NULL;
1101
1102     /* Stop thread system: last one out please shut the door!
1103      * The number of initializations of the thread system is counted, we
1104      * can call this each time */
1105     vlc_threads_end( p_libvlc_global );
1106
1107     return VLC_SUCCESS;
1108 }
1109
1110 /**
1111  * Add an interface plugin and run it
1112  */
1113 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc,
1114                             char const *psz_module,
1115                             bool b_block, bool b_play,
1116                             int i_options, const char *const *ppsz_options )
1117 {
1118     int i_err;
1119     intf_thread_t *p_intf = NULL;
1120
1121     if( !p_libvlc )
1122         return VLC_EGENERIC;
1123
1124     if( !psz_module ) /* requesting the default interface */
1125     {
1126         char *psz_interface = config_GetPsz( p_libvlc, "intf" );
1127         if( !psz_interface || !*psz_interface ) /* "intf" has not been set */
1128             msg_Info( p_libvlc, _("Running vlc with the default interface. Use 'cvlc' to use vlc without interface.") );
1129         free( psz_interface );
1130     }
1131
1132 #ifndef WIN32
1133     if( p_libvlc_global->b_daemon && b_block && !psz_module )
1134     {
1135         /* Daemon mode hack.
1136          * We prefer the dummy interface if none is specified. */
1137         char *psz_interface = config_GetPsz( p_libvlc, "intf" );
1138         if( !psz_interface || !*psz_interface ) psz_module = "dummy";
1139         free( psz_interface );
1140     }
1141 #endif
1142
1143     /* Try to create the interface */
1144     p_intf = intf_Create( p_libvlc, psz_module ? psz_module : "$intf",
1145                           i_options, ppsz_options );
1146     if( p_intf == NULL )
1147     {
1148         msg_Err( p_libvlc, "interface \"%s\" initialization failed",
1149                  psz_module );
1150         return VLC_EGENERIC;
1151     }
1152
1153     /* Interface doesn't handle play on start so do it ourselves */
1154     if( !p_intf->b_play && b_play )
1155         playlist_Play( p_libvlc->p_playlist );
1156
1157     /* Try to run the interface */
1158     p_intf->b_play = b_play;
1159     i_err = intf_RunThread( p_intf );
1160     if( i_err || p_intf->b_should_run_on_first_thread )
1161     {
1162         vlc_object_detach( p_intf );
1163         intf_Destroy( p_intf );
1164         p_intf = NULL;
1165         return i_err;
1166     }
1167
1168     if( b_block )
1169     {
1170         /* FIXME: should be moved to interface/interface.c */
1171         if( p_intf->pf_run )
1172             vlc_thread_join( p_intf );
1173         else
1174             while( vlc_object_lock_and_wait( p_intf ) == 0 );
1175
1176         vlc_object_detach( p_intf );
1177         intf_Destroy( p_intf );
1178     }
1179
1180     return VLC_SUCCESS;
1181 };
1182
1183 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
1184     ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1185 /*****************************************************************************
1186  * SetLanguage: set the interface language.
1187  *****************************************************************************
1188  * We set the LC_MESSAGES locale category for interface messages and buttons,
1189  * as well as the LC_CTYPE category for string sorting and possible wide
1190  * character support.
1191  *****************************************************************************/
1192 static void SetLanguage ( const char *psz_lang )
1193 {
1194 #ifdef __APPLE__
1195     /* I need that under Darwin, please check it doesn't disturb
1196      * other platforms. --Meuuh */
1197     setenv( "LANG", psz_lang, 1 );
1198
1199 #else
1200     /* We set LC_ALL manually because it is the only way to set
1201      * the language at runtime under eg. Windows. Beware that this
1202      * makes the environment unconsistent when libvlc is unloaded and
1203      * should probably be moved to a safer place like vlc.c. */
1204     static char psz_lcall[20];
1205     snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1206     psz_lcall[19] = '\0';
1207     putenv( psz_lcall );
1208 #endif
1209
1210     setlocale( LC_ALL, psz_lang );
1211 }
1212 #endif
1213
1214
1215 static inline int LoadMessages (void)
1216 {
1217 #if defined( ENABLE_NLS ) \
1218      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1219     /* Specify where to find the locales for current domain */
1220 #if !defined( __APPLE__ ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1221     static const char psz_path[] = LOCALEDIR;
1222 #else
1223     char psz_path[1024];
1224     if (snprintf (psz_path, sizeof (psz_path), "%s/%s",
1225                   p_libvlc_global->psz_vlcpath, "locale")
1226                      >= (int)sizeof (psz_path))
1227         return -1;
1228
1229 #endif
1230     if (bindtextdomain (PACKAGE_NAME, psz_path) == NULL)
1231     {
1232         fprintf (stderr, "Warning: cannot bind text domain "PACKAGE_NAME
1233                          " to directory %s\n", psz_path);
1234         return -1;
1235     }
1236
1237     /* LibVLC wants all messages in UTF-8.
1238      * Unfortunately, we cannot ask UTF-8 for strerror_r(), strsignal_r()
1239      * and other functions that are not part of our text domain.
1240      */
1241     if (bind_textdomain_codeset (PACKAGE_NAME, "UTF-8") == NULL)
1242     {
1243         fprintf (stderr, "Error: cannot set Unicode encoding for text domain "
1244                          PACKAGE_NAME"\n");
1245         // Unbinds the text domain to avoid broken encoding
1246         bindtextdomain (PACKAGE_NAME, "DOES_NOT_EXIST");
1247         return -1;
1248     }
1249
1250     /* LibVLC does NOT set the default textdomain, since it is a library.
1251      * This could otherwise break programs using LibVLC (other than VLC).
1252      * textdomain (PACKAGE_NAME);
1253      */
1254 #endif
1255     return 0;
1256 }
1257
1258 /*****************************************************************************
1259  * GetFilenames: parse command line options which are not flags
1260  *****************************************************************************
1261  * Parse command line for input files as well as their associated options.
1262  * An option always follows its associated input and begins with a ":".
1263  *****************************************************************************/
1264 static int GetFilenames( libvlc_int_t *p_vlc, int i_argc, const char *ppsz_argv[] )
1265 {
1266     int i_opt, i_options;
1267
1268     /* We assume that the remaining parameters are filenames
1269      * and their input options */
1270     for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1271     {
1272         i_options = 0;
1273
1274         /* Count the input options */
1275         while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1276         {
1277             i_options++;
1278             i_opt--;
1279         }
1280
1281         /* TODO: write an internal function of this one, to avoid
1282          *       unnecessary lookups. */
1283
1284         VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[i_opt],
1285                        ( i_options ? &ppsz_argv[i_opt + 1] :
1286                                         NULL ), i_options,
1287                        PLAYLIST_INSERT, 0 );
1288     }
1289
1290     return VLC_SUCCESS;
1291 }
1292
1293 /*****************************************************************************
1294  * Help: print program help
1295  *****************************************************************************
1296  * Print a short inline help. Message interface is initialized at this stage.
1297  *****************************************************************************/
1298 static void Help( libvlc_int_t *p_this, char const *psz_help_name )
1299 {
1300 #ifdef WIN32
1301     ShowConsole( true );
1302 #endif
1303
1304     if( psz_help_name && !strcmp( psz_help_name, "help" ) )
1305     {
1306         utf8_fprintf( stdout, vlc_usage, p_this->psz_object_name );
1307         Usage( p_this, "help" );
1308         Usage( p_this, "main" );
1309     }
1310     else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
1311     {
1312         utf8_fprintf( stdout, vlc_usage, p_this->psz_object_name );
1313         Usage( p_this, NULL );
1314     }
1315     else if( psz_help_name )
1316     {
1317         Usage( p_this, psz_help_name );
1318     }
1319
1320 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1321     PauseConsole();
1322 #endif
1323 }
1324
1325 /*****************************************************************************
1326  * Usage: print module usage
1327  *****************************************************************************
1328  * Print a short inline help. Message interface is initialized at this stage.
1329  *****************************************************************************/
1330 static void Usage( libvlc_int_t *p_this, char const *psz_module_name )
1331 {
1332 #define FORMAT_STRING "  %s --%s%s%s%s%s%s%s "
1333     /* short option ------'    | | | | | | |
1334      * option name ------------' | | | | | |
1335      * <bra ---------------------' | | | | |
1336      * option type or "" ----------' | | | |
1337      * ket> -------------------------' | | |
1338      * padding spaces -----------------' | |
1339      * comment --------------------------' |
1340      * comment suffix ---------------------'
1341      *
1342      * The purpose of having bra and ket is that we might i18n them as well.
1343      */
1344
1345 #   define COL(x)  "\033[" #x ";1m"
1346 #   define RED     COL(31)
1347 #   define GREEN   COL(32)
1348 #   define YELLOW  COL(33)
1349 #   define BLUE    COL(34)
1350 #   define MAGENTA COL(35)
1351 #   define CYAN    COL(36)
1352 #   define WHITE   COL(0)
1353 #   define GRAY    "\033[0m"
1354 #define COLOR_FORMAT_STRING (WHITE"  %s --%s"YELLOW"%s%s%s%s%s%s "GRAY)
1355 #define COLOR_FORMAT_STRING_BOOL (WHITE"  %s --%s%s%s%s%s%s%s "GRAY)
1356
1357 #define LINE_START 8
1358 #define PADDING_SPACES 25
1359 #ifdef WIN32
1360 #   define OPTION_VALUE_SEP "="
1361 #else
1362 #   define OPTION_VALUE_SEP " "
1363 #endif
1364     vlc_list_t *p_list = NULL;
1365     char psz_spaces_text[PADDING_SPACES+LINE_START+1];
1366     char psz_spaces_longtext[LINE_START+3];
1367     char psz_format[sizeof(COLOR_FORMAT_STRING)];
1368     char psz_format_bool[sizeof(COLOR_FORMAT_STRING_BOOL)];
1369     char psz_buffer[10000];
1370     char psz_short[4];
1371     int i_index;
1372     int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1373     int i_width_description = i_width + PADDING_SPACES - 1;
1374     bool b_advanced    = config_GetInt( p_this, "advanced" ) > 0;
1375     bool b_description = config_GetInt( p_this, "help-verbose" ) > 0;
1376     bool b_description_hack;
1377     bool b_color       = config_GetInt( p_this, "color" ) > 0;
1378     bool b_has_advanced = false;
1379
1380     memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
1381     psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
1382     memset( psz_spaces_longtext, ' ', LINE_START+2 );
1383     psz_spaces_longtext[LINE_START+2] = '\0';
1384 #ifdef WIN32
1385     b_color = false; // don't put color control codes in a .txt file
1386 #endif
1387
1388     if( b_color )
1389     {
1390         strcpy( psz_format, COLOR_FORMAT_STRING );
1391         strcpy( psz_format_bool, COLOR_FORMAT_STRING_BOOL );
1392     }
1393     else
1394     {
1395         strcpy( psz_format, FORMAT_STRING );
1396         strcpy( psz_format_bool, FORMAT_STRING );
1397     }
1398
1399     /* List all modules */
1400     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1401
1402     /* Ugly hack to make sure that the help options always come first
1403      * (part 1) */
1404     if( !psz_module_name )
1405         Usage( p_this, "help" );
1406
1407     /* Enumerate the config for each module */
1408     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1409     {
1410         bool b_help_module;
1411         module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
1412         module_config_t *p_item = NULL;
1413         module_config_t *p_end = p_parser->p_config + p_parser->confsize;
1414
1415         if( psz_module_name && strcmp( psz_module_name,
1416                                        p_parser->psz_object_name ) )
1417         {
1418             const char *const *pp_shortcut = p_parser->pp_shortcuts;
1419             while( *pp_shortcut )
1420             {
1421                 if( !strcmp( psz_module_name, *pp_shortcut ) )
1422                     break;
1423                 pp_shortcut ++;
1424             }
1425             if( !*pp_shortcut )
1426                 continue;
1427         }
1428
1429         /* Ignore modules without config options */
1430         if( !p_parser->i_config_items )
1431         {
1432             continue;
1433         }
1434
1435         b_help_module = !strcmp( "help", p_parser->psz_object_name );
1436         /* Ugly hack to make sure that the help options always come first
1437          * (part 2) */
1438         if( !psz_module_name && b_help_module )
1439             continue;
1440
1441         /* Ignore modules with only advanced config options if requested */
1442         if( !b_advanced )
1443         {
1444             for( p_item = p_parser->p_config;
1445                  p_item < p_end;
1446                  p_item++ )
1447             {
1448                 if( (p_item->i_type & CONFIG_ITEM) &&
1449                     !p_item->b_advanced ) break;
1450             }
1451         }
1452
1453         /* Print name of module */
1454         if( strcmp( "main", p_parser->psz_object_name ) )
1455         {
1456             if( b_color )
1457                 utf8_fprintf( stdout, "\n " GREEN "%s" GRAY "\n",
1458                               p_parser->psz_longname );
1459             else
1460                 utf8_fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1461         }
1462         if( p_parser->psz_help )
1463         {
1464             if( b_color )
1465                 utf8_fprintf( stdout, CYAN" %s\n"GRAY, p_parser->psz_help );
1466             else
1467                 utf8_fprintf( stdout, " %s\n", p_parser->psz_help );
1468         }
1469
1470         /* Print module options */
1471         for( p_item = p_parser->p_config;
1472              p_item < p_end;
1473              p_item++ )
1474         {
1475             char *psz_text, *psz_spaces = psz_spaces_text;
1476             const char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1477             const char *psz_suf = "", *psz_prefix = NULL;
1478             signed int i;
1479             size_t i_cur_width;
1480
1481             /* Skip removed options */
1482             if( p_item->b_removed )
1483             {
1484                 continue;
1485             }
1486             /* Skip advanced options if requested */
1487             if( p_item->b_advanced && !b_advanced )
1488             {
1489                 b_has_advanced = true;
1490                 continue;
1491             }
1492
1493             switch( p_item->i_type )
1494             {
1495             case CONFIG_HINT_CATEGORY:
1496             case CONFIG_HINT_USAGE:
1497                 if( !strcmp( "main", p_parser->psz_object_name ) )
1498                 {
1499                     if( b_color )
1500                         utf8_fprintf( stdout, GREEN "\n %s\n" GRAY,
1501                                       p_item->psz_text );
1502                     else
1503                         utf8_fprintf( stdout, "\n %s\n", p_item->psz_text );
1504                 }
1505                 if( b_description && p_item->psz_longtext )
1506                 {
1507                     if( b_color )
1508                         utf8_fprintf( stdout, CYAN " %s\n" GRAY,
1509                                       p_item->psz_longtext );
1510                     else
1511                         utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1512                 }
1513                 break;
1514
1515             case CONFIG_HINT_SUBCATEGORY:
1516                 if( strcmp( "main", p_parser->psz_object_name ) )
1517                 break;
1518             case CONFIG_SECTION:
1519                 if( b_color )
1520                 {
1521                     utf8_fprintf( stdout, RED"   %s:\n"GRAY,
1522                                   p_item->psz_text );
1523                     if( b_description && p_item->psz_longtext )
1524                         utf8_fprintf( stdout, MAGENTA"   %s\n"GRAY,
1525                                       p_item->psz_longtext );
1526                 }
1527                 else
1528                 {
1529                     utf8_fprintf( stdout, "   %s:\n", p_item->psz_text );
1530                     if( b_description && p_item->psz_longtext )
1531                         utf8_fprintf( stdout, "   %s\n", p_item->psz_longtext );
1532                 }
1533                 break;
1534
1535             case CONFIG_ITEM_STRING:
1536             case CONFIG_ITEM_FILE:
1537             case CONFIG_ITEM_DIRECTORY:
1538             case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1539             case CONFIG_ITEM_MODULE_CAT:
1540             case CONFIG_ITEM_MODULE_LIST:
1541             case CONFIG_ITEM_MODULE_LIST_CAT:
1542             case CONFIG_ITEM_FONT:
1543             case CONFIG_ITEM_PASSWORD:
1544                 psz_bra = OPTION_VALUE_SEP "<";
1545                 psz_type = _("string");
1546                 psz_ket = ">";
1547
1548                 if( p_item->ppsz_list )
1549                 {
1550                     psz_bra = OPTION_VALUE_SEP "{";
1551                     psz_type = psz_buffer;
1552                     psz_buffer[0] = '\0';
1553                     for( i = 0; p_item->ppsz_list[i]; i++ )
1554                     {
1555                         if( i ) strcat( psz_buffer, "," );
1556                         strcat( psz_buffer, p_item->ppsz_list[i] );
1557                     }
1558                     psz_ket = "}";
1559                 }
1560                 break;
1561             case CONFIG_ITEM_INTEGER:
1562             case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1563                 psz_bra = OPTION_VALUE_SEP "<";
1564                 psz_type = _("integer");
1565                 psz_ket = ">";
1566
1567                 if( p_item->min.i || p_item->max.i )
1568                 {
1569                     sprintf( psz_buffer, "%s [%i .. %i]", psz_type,
1570                              p_item->min.i, p_item->max.i );
1571                     psz_type = psz_buffer;
1572                 }
1573
1574                 if( p_item->i_list )
1575                 {
1576                     psz_bra = OPTION_VALUE_SEP "{";
1577                     psz_type = psz_buffer;
1578                     psz_buffer[0] = '\0';
1579                     for( i = 0; p_item->ppsz_list_text[i]; i++ )
1580                     {
1581                         if( i ) strcat( psz_buffer, ", " );
1582                         sprintf( psz_buffer + strlen(psz_buffer), "%i (%s)",
1583                                  p_item->pi_list[i],
1584                                  p_item->ppsz_list_text[i] );
1585                     }
1586                     psz_ket = "}";
1587                 }
1588                 break;
1589             case CONFIG_ITEM_FLOAT:
1590                 psz_bra = OPTION_VALUE_SEP "<";
1591                 psz_type = _("float");
1592                 psz_ket = ">";
1593                 if( p_item->min.f || p_item->max.f )
1594                 {
1595                     sprintf( psz_buffer, "%s [%f .. %f]", psz_type,
1596                              p_item->min.f, p_item->max.f );
1597                     psz_type = psz_buffer;
1598                 }
1599                 break;
1600             case CONFIG_ITEM_BOOL:
1601                 psz_bra = ""; psz_type = ""; psz_ket = "";
1602                 if( !b_help_module )
1603                 {
1604                     psz_suf = p_item->value.i ? _(" (default enabled)") :
1605                                                 _(" (default disabled)");
1606                 }
1607                 break;
1608             }
1609
1610             if( !psz_type )
1611             {
1612                 continue;
1613             }
1614
1615             /* Add short option if any */
1616             if( p_item->i_short )
1617             {
1618                 sprintf( psz_short, "-%c,", p_item->i_short );
1619             }
1620             else
1621             {
1622                 strcpy( psz_short, "   " );
1623             }
1624
1625             i = PADDING_SPACES - strlen( p_item->psz_name )
1626                  - strlen( psz_bra ) - strlen( psz_type )
1627                  - strlen( psz_ket ) - 1;
1628
1629             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1630             {
1631                 psz_prefix =  ", --no-";
1632                 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1633             }
1634
1635             if( i < 0 )
1636             {
1637                 psz_spaces[0] = '\n';
1638                 i = 0;
1639             }
1640             else
1641             {
1642                 psz_spaces[i] = '\0';
1643             }
1644
1645             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1646             {
1647                 utf8_fprintf( stdout, psz_format_bool, psz_short, 
1648                               p_item->psz_name, psz_prefix, p_item->psz_name,
1649                               psz_bra, psz_type, psz_ket, psz_spaces );
1650             }
1651             else
1652             {
1653                 utf8_fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1654                          "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1655             }
1656
1657             psz_spaces[i] = ' ';
1658
1659             /* We wrap the rest of the output */
1660             sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1661             b_description_hack = b_description;
1662
1663  description:
1664             psz_text = psz_buffer;
1665             i_cur_width = b_description && !b_description_hack
1666                           ? i_width_description
1667                           : i_width;
1668             while( *psz_text )
1669             {
1670                 char *psz_parser, *psz_word;
1671                 size_t i_end = strlen( psz_text );
1672
1673                 /* If the remaining text fits in a line, print it. */
1674                 if( i_end <= i_cur_width )
1675                 {
1676                     if( b_color )
1677                     {
1678                         if( !b_description || b_description_hack )
1679                             utf8_fprintf( stdout, BLUE"%s\n"GRAY, psz_text );
1680                         else
1681                             utf8_fprintf( stdout, "%s\n", psz_text );
1682                     }
1683                     else
1684                     {
1685                         utf8_fprintf( stdout, "%s\n", psz_text );
1686                     }
1687                     break;
1688                 }
1689
1690                 /* Otherwise, eat as many words as possible */
1691                 psz_parser = psz_text;
1692                 do
1693                 {
1694                     psz_word = psz_parser;
1695                     psz_parser = strchr( psz_word, ' ' );
1696                     /* If no space was found, we reached the end of the text
1697                      * block; otherwise, we skip the space we just found. */
1698                     psz_parser = psz_parser ? psz_parser + 1
1699                                             : psz_text + i_end;
1700
1701                 } while( (size_t)(psz_parser - psz_text) <= i_cur_width );
1702
1703                 /* We cut a word in one of these cases:
1704                  *  - it's the only word in the line and it's too long.
1705                  *  - we used less than 80% of the width and the word we are
1706                  *    going to wrap is longer than 40% of the width, and even
1707                  *    if the word would have fit in the next line. */
1708                 if( psz_word == psz_text
1709              || ( (size_t)(psz_word - psz_text) < 80 * i_cur_width / 100
1710              && (size_t)(psz_parser - psz_word) > 40 * i_cur_width / 100 ) )
1711                 {
1712                     char c = psz_text[i_cur_width];
1713                     psz_text[i_cur_width] = '\0';
1714                     if( b_color )
1715                     {
1716                         if( !b_description || b_description_hack )
1717                             utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1718                                           psz_text, psz_spaces );
1719                         else
1720                             utf8_fprintf( stdout, "%s\n%s",
1721                                           psz_text, psz_spaces );
1722                     }
1723                     else
1724                     {
1725                         utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1726                     }
1727                     psz_text += i_cur_width;
1728                     psz_text[0] = c;
1729                 }
1730                 else
1731                 {
1732                     psz_word[-1] = '\0';
1733                     if( b_color )
1734                     {
1735                         if( !b_description || b_description_hack )
1736                             utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1737                                           psz_text, psz_spaces );
1738                         else
1739                             utf8_fprintf( stdout, "%s\n%s",
1740                                           psz_text, psz_spaces );
1741                     }
1742                     else
1743                     {
1744                         utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1745                     }
1746                     psz_text = psz_word;
1747                 }
1748             }
1749
1750             if( b_description_hack && p_item->psz_longtext )
1751             {
1752                 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
1753                 b_description_hack = false;
1754                 psz_spaces = psz_spaces_longtext;
1755                 utf8_fprintf( stdout, "%s", psz_spaces );
1756                 goto description;
1757             }
1758         }
1759     }
1760
1761     if( b_has_advanced ) 
1762     {
1763         if( b_color )
1764             utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " %s\n", _( "Note:" ),
1765            _( "add --advanced to your command line to see advanced options."));
1766         else
1767             utf8_fprintf( stdout, "\n %s %s\n", _( "Note:" ),
1768            _( "add --advanced to your command line to see advanced options."));
1769     }
1770
1771     /* Release the module list */
1772     vlc_list_release( p_list );
1773 }
1774
1775 /*****************************************************************************
1776  * ListModules: list the available modules with their description
1777  *****************************************************************************
1778  * Print a list of all available modules (builtins and plugins) and a short
1779  * description for each one.
1780  *****************************************************************************/
1781 static void ListModules( libvlc_int_t *p_this, bool b_verbose )
1782 {
1783     vlc_list_t *p_list = NULL;
1784     module_t *p_parser = NULL;
1785     char psz_spaces[22];
1786     int i_index;
1787
1788     bool b_color = config_GetInt( p_this, "color" ) > 0;
1789
1790     memset( psz_spaces, ' ', 22 );
1791
1792 #ifdef WIN32
1793     ShowConsole( true );
1794 #endif
1795
1796     /* List all modules */
1797     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1798
1799     /* Enumerate each module */
1800     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1801     {
1802         int i;
1803
1804         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1805
1806         /* Nasty hack, but right now I'm too tired to think about a nice
1807          * solution */
1808         i = 22 - strlen( p_parser->psz_object_name ) - 1;
1809         if( i < 0 ) i = 0;
1810         psz_spaces[i] = 0;
1811
1812         if( b_color )
1813             utf8_fprintf( stdout, GREEN"  %s%s "WHITE"%s\n"GRAY,
1814                           p_parser->psz_object_name,
1815                           psz_spaces,
1816                           p_parser->psz_longname );
1817         else
1818             utf8_fprintf( stdout, "  %s%s %s\n",
1819                           p_parser->psz_object_name,
1820                           psz_spaces, p_parser->psz_longname );
1821
1822         if( b_verbose )
1823         {
1824             const char *const *pp_shortcut = p_parser->pp_shortcuts;
1825             while( *pp_shortcut )
1826             {
1827                 if( strcmp( *pp_shortcut, p_parser->psz_object_name ) )
1828                 {
1829                     if( b_color )
1830                         utf8_fprintf( stdout, CYAN"   s %s\n"GRAY,
1831                                       *pp_shortcut );
1832                     else
1833                         utf8_fprintf( stdout, "   s %s\n",
1834                                       *pp_shortcut );
1835                 }
1836                 pp_shortcut++;
1837             }
1838             if( p_parser->psz_capability )
1839             {
1840                 if( b_color )
1841                     utf8_fprintf( stdout, MAGENTA"   c %s (%d)\n"GRAY,
1842                                   p_parser->psz_capability,
1843                                   p_parser->i_score );
1844                 else
1845                     utf8_fprintf( stdout, "   c %s (%d)\n",
1846                                   p_parser->psz_capability,
1847                                   p_parser->i_score );
1848             }
1849         }
1850
1851         psz_spaces[i] = ' ';
1852     }
1853
1854     vlc_list_release( p_list );
1855
1856 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1857     PauseConsole();
1858 #endif
1859 }
1860
1861 /*****************************************************************************
1862  * Version: print complete program version
1863  *****************************************************************************
1864  * Print complete program version and build number.
1865  *****************************************************************************/
1866 static void Version( void )
1867 {
1868 #ifdef WIN32
1869     ShowConsole( true );
1870 #endif
1871
1872     utf8_fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
1873     utf8_fprintf( stdout, _("Compiled by %s@%s.%s\n"),
1874              VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
1875     utf8_fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
1876     if( strcmp( VLC_Changeset(), "exported" ) )
1877         utf8_fprintf( stdout, _("Based upon Git commit [%s]\n"),
1878                  VLC_Changeset() );
1879     utf8_fprintf( stdout, LICENSE_MSG );
1880
1881 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1882     PauseConsole();
1883 #endif
1884 }
1885
1886 /*****************************************************************************
1887  * ShowConsole: On Win32, create an output console for debug messages
1888  *****************************************************************************
1889  * This function is useful only on Win32.
1890  *****************************************************************************/
1891 #ifdef WIN32 /*  */
1892 static void ShowConsole( bool b_dofile )
1893 {
1894 #   ifndef UNDER_CE
1895     FILE *f_help = NULL;
1896
1897     if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1898
1899     AllocConsole();
1900     /* Use the ANSI code page (e.g. Windows-1252) as expected by the LibVLC
1901      * Unicode/locale subsystem. By default, we have the obsolecent OEM code
1902      * page (e.g. CP437 or CP850). */
1903     SetConsoleOutputCP (GetACP ());
1904     SetConsoleTitle ("VLC media player version "PACKAGE_VERSION);
1905
1906     freopen( "CONOUT$", "w", stderr );
1907     freopen( "CONIN$", "r", stdin );
1908
1909     if( b_dofile && (f_help = fopen( "vlc-help.txt", "wt" )) )
1910     {
1911         fclose( f_help );
1912         freopen( "vlc-help.txt", "wt", stdout );
1913         utf8_fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
1914     }
1915     else freopen( "CONOUT$", "w", stdout );
1916
1917 #   endif
1918 }
1919 #endif
1920
1921 /*****************************************************************************
1922  * PauseConsole: On Win32, wait for a key press before closing the console
1923  *****************************************************************************
1924  * This function is useful only on Win32.
1925  *****************************************************************************/
1926 #ifdef WIN32 /*  */
1927 static void PauseConsole( void )
1928 {
1929 #   ifndef UNDER_CE
1930
1931     if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1932
1933     utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1934     getchar();
1935     fclose( stdout );
1936
1937 #   endif
1938 }
1939 #endif
1940
1941 /*****************************************************************************
1942  * ConsoleWidth: Return the console width in characters
1943  *****************************************************************************
1944  * We use the stty shell command to get the console width; if this fails or
1945  * if the width is less than 80, we default to 80.
1946  *****************************************************************************/
1947 static int ConsoleWidth( void )
1948 {
1949     unsigned i_width = 80;
1950
1951 #ifndef WIN32
1952     FILE *file = popen( "stty size 2>/dev/null", "r" );
1953     if (file != NULL)
1954     {
1955         if (fscanf (file, "%*u %u", &i_width) <= 0)
1956             i_width = 80;
1957         pclose( file );
1958     }
1959 #else
1960     CONSOLE_SCREEN_BUFFER_INFO buf;
1961
1962     if (GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &buf))
1963         i_width = buf.dwSize.X;
1964 #endif
1965
1966     return i_width;
1967 }
1968
1969 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
1970                      vlc_value_t old_val, vlc_value_t new_val, void *param)
1971 {
1972     libvlc_int_t *p_libvlc = (libvlc_int_t *)p_this;
1973     (void)psz_variable;
1974     (void)old_val;
1975     (void)param;
1976
1977     if( new_val.i_int >= -1 )
1978     {
1979         p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
1980     }
1981     return VLC_SUCCESS;
1982 }
1983
1984 /*****************************************************************************
1985  * InitDeviceValues: initialize device values
1986  *****************************************************************************
1987  * This function inits the dvd, vcd and cd-audio values
1988  *****************************************************************************/
1989 static void InitDeviceValues( libvlc_int_t *p_vlc )
1990 {
1991 #ifdef HAVE_HAL
1992     LibHalContext * ctx = NULL;
1993     int i, i_devices;
1994     char **devices = NULL;
1995     char *block_dev = NULL;
1996     dbus_bool_t b_dvd;
1997
1998 #ifdef HAVE_HAL_1
1999     DBusConnection *p_connection = NULL;
2000     DBusError       error;
2001
2002     ctx = libhal_ctx_new();
2003     if( !ctx ) return;
2004     dbus_error_init( &error );
2005     p_connection = dbus_bus_get ( DBUS_BUS_SYSTEM, &error );
2006     if( dbus_error_is_set( &error ) || !p_connection )
2007     {
2008         dbus_error_free( &error );
2009         return;
2010     }
2011     libhal_ctx_set_dbus_connection( ctx, p_connection );
2012     if( libhal_ctx_init( ctx, &error ) )
2013 #else
2014     ctx = hal_initialize( NULL, FALSE );
2015     if( ctx )
2016 #endif
2017
2018     {
2019 #ifdef HAVE_HAL_1
2020         if( ( devices = libhal_get_all_devices( ctx, &i_devices, NULL ) ) )
2021 #else
2022         if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
2023 #endif
2024         {
2025             for( i = 0; i < i_devices; i++ )
2026             {
2027 #ifdef HAVE_HAL_1
2028                 if( !libhal_device_property_exists( ctx, devices[i],
2029                                                 "storage.cdrom.dvd", NULL ) )
2030 #else
2031                 if( !hal_device_property_exists( ctx, devices[ i ],
2032                                                 "storage.cdrom.dvd" ) )
2033 #endif
2034                 {
2035                     continue;
2036                 }
2037 #ifdef HAVE_HAL_1
2038                 b_dvd = libhal_device_get_property_bool( ctx, devices[ i ],
2039                                                  "storage.cdrom.dvd", NULL  );
2040                 block_dev = libhal_device_get_property_string( ctx,
2041                                 devices[ i ], "block.device" , NULL );
2042 #else
2043                 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
2044                                                       "storage.cdrom.dvd" );
2045                 block_dev = hal_device_get_property_string( ctx, devices[ i ],
2046                                                             "block.device" );
2047 #endif
2048                 if( b_dvd )
2049                 {
2050                     config_PutPsz( p_vlc, "dvd", block_dev );
2051                 }
2052
2053                 config_PutPsz( p_vlc, "vcd", block_dev );
2054                 config_PutPsz( p_vlc, "cd-audio", block_dev );
2055 #ifdef HAVE_HAL_1
2056                 libhal_free_string( block_dev );
2057 #else
2058                 hal_free_string( block_dev );
2059 #endif
2060             }
2061 #ifdef HAVE_HAL_1
2062             libhal_free_string_array( devices );
2063 #else
2064             hal_free_string_array( devices );
2065 #endif
2066         }
2067
2068 #ifdef HAVE_HAL_1
2069         libhal_ctx_shutdown( ctx, NULL );
2070         dbus_connection_unref( p_connection );
2071         libhal_ctx_free( ctx );
2072 #else
2073         hal_shutdown( ctx );
2074 #endif
2075     }
2076     else
2077     {
2078         msg_Warn( p_vlc, "Unable to get HAL device properties" );
2079     }
2080 #else
2081     (void)p_vlc;
2082 #endif /* HAVE_HAL */
2083 }