]> git.sesse.net Git - vlc/blob - src/libvlc-common.c
Don't compile sout code if sout is disabled
[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 #ifdef ENABLE_SOUT
782     /* Initialize VLM if vlm-conf is specified */
783     psz_parser = config_GetPsz( p_libvlc, "vlm-conf" );
784     if( psz_parser && *psz_parser )
785     {
786         p_libvlc->p_vlm = vlm_New( p_libvlc );
787         if( !p_libvlc->p_vlm )
788             msg_Err( p_libvlc, "VLM initialization failed" );
789     }
790     free( psz_parser );
791 #endif
792
793     /*
794      * Load background interfaces
795      */
796     psz_modules = config_GetPsz( p_libvlc, "extraintf" );
797     psz_control = config_GetPsz( p_libvlc, "control" );
798
799     if( psz_modules && *psz_modules && psz_control && *psz_control )
800     {
801         psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
802                                                     strlen( psz_control ) + 1 );
803         sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
804     }
805     else if( psz_control && *psz_control )
806     {
807         free( psz_modules );
808         psz_modules = strdup( psz_control );
809     }
810
811     psz_parser = psz_modules;
812     while ( psz_parser && *psz_parser )
813     {
814         char *psz_module, *psz_temp;
815         psz_module = psz_parser;
816         psz_parser = strchr( psz_module, ':' );
817         if ( psz_parser )
818         {
819             *psz_parser = '\0';
820             psz_parser++;
821         }
822         psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
823         if( psz_temp )
824         {
825             sprintf( psz_temp, "%s,none", psz_module );
826             VLC_AddIntf( 0, psz_temp, false, false );
827             free( psz_temp );
828         }
829     }
830     free( psz_modules );
831     free( psz_control );
832
833     /*
834      * Always load the hotkeys interface if it exists
835      */
836     VLC_AddIntf( 0, "hotkeys,none", false, false );
837
838 #ifdef HAVE_DBUS_3
839     /* loads dbus control interface if in one-instance mode
840      * we do it only when playlist exists, because dbus module needs it */
841     if( config_GetInt( p_libvlc, "one-instance" ) > 0 )
842         VLC_AddIntf( 0, "dbus,none", false, false );
843
844     /* Prevents the power management daemon to suspend the computer
845      * when VLC is active */
846     if( config_GetInt( p_libvlc, "inhibit" ) > 0 )
847         VLC_AddIntf( 0, "inhibit,none", false, false );
848 #endif
849
850     /*
851      * If needed, load the Xscreensaver interface
852      * Currently, only for X
853      */
854 #ifdef HAVE_X11_XLIB_H
855     if( config_GetInt( p_libvlc, "disable-screensaver" ) )
856     {
857         VLC_AddIntf( 0, "screensaver,none", false, false );
858     }
859 #endif
860
861     if( config_GetInt( p_libvlc, "file-logging" ) > 0 )
862     {
863         VLC_AddIntf( 0, "logger,none", false, false );
864     }
865 #ifdef HAVE_SYSLOG_H
866     if( config_GetInt( p_libvlc, "syslog" ) > 0 )
867     {
868         const char *psz_logmode = "logmode=syslog";
869         libvlc_InternalAddIntf( p_libvlc, "logger,none", false, false,
870                                 1, &psz_logmode );
871     }
872 #endif
873
874     if( config_GetInt( p_libvlc, "show-intf" ) > 0 )
875     {
876         VLC_AddIntf( 0, "showintf,none", false, false );
877     }
878
879     if( config_GetInt( p_libvlc, "network-synchronisation") > 0 )
880     {
881         VLC_AddIntf( 0, "netsync,none", false, false );
882     }
883
884 #ifdef WIN32
885     if( config_GetInt( p_libvlc, "prefer-system-codecs") > 0 )
886     {
887         char *psz_codecs = config_GetPsz( p_playlist, "codec" );
888         if( psz_codecs )
889         {
890             char *psz_morecodecs;
891             asprintf(&psz_morecodecs, "%s,dmo,quicktime", psz_codecs);
892             if( psz_morecodecs )
893             {
894                 config_PutPsz( p_libvlc, "codec", psz_morecodecs);
895                 free( psz_morecodecs );
896             }
897         }
898         else
899             config_PutPsz( p_libvlc, "codec", "dmo,quicktime");
900         free( psz_codecs );
901     }
902 #endif
903
904     /*
905      * FIXME: kludge to use a p_libvlc-local variable for the Mozilla plugin
906      */
907     var_Create( p_libvlc, "drawable", VLC_VAR_INTEGER );
908     var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
909     var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
910     var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
911     var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
912     var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
913     var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
914     var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
915     var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
916
917     /* Create volume callback system. */
918     var_Create( p_libvlc, "volume-change", VLC_VAR_BOOL );
919
920     /*
921      * Get input filenames given as commandline arguments
922      */
923     GetFilenames( p_libvlc, i_argc, ppsz_argv );
924
925     /*
926      * Get --open argument
927      */
928     var_Create( p_libvlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
929     var_Get( p_libvlc, "open", &val );
930     if ( val.psz_string != NULL && *val.psz_string )
931     {
932         VLC_AddTarget( p_libvlc->i_object_id, val.psz_string, NULL, 0,
933                        PLAYLIST_INSERT, 0 );
934     }
935     free( val.psz_string );
936
937     return VLC_SUCCESS;
938 }
939
940 /**
941  * Cleanup a libvlc instance. The instance is not completely deallocated
942  * \param p_libvlc the instance to clean
943  */
944 int libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
945 {
946     intf_thread_t      * p_intf = NULL;
947     vout_thread_t      * p_vout = NULL;
948     aout_instance_t    * p_aout = 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 #ifdef ENABLE_SOUT
984     sout_instance_t    * p_sout;
985
986     p_sout = vlc_object_find( p_libvlc, VLC_OBJECT_SOUT, FIND_CHILD );
987     if( p_sout )
988     {
989         msg_Dbg( p_sout, "removing kept stream output" );
990         vlc_object_detach( (vlc_object_t*)p_sout );
991         vlc_object_release( (vlc_object_t*)p_sout );
992         sout_DeleteInstance( p_sout );
993     }
994
995     /* Destroy VLM if created in libvlc_InternalInit */
996     if( p_libvlc->p_vlm )
997     {
998         vlm_Delete( p_libvlc->p_vlm );
999     }
1000 #endif
1001
1002     /* Free interaction */
1003     msg_Dbg( p_libvlc, "removing interaction" );
1004     vlc_object_release( p_libvlc->p_interaction );
1005
1006     stats_TimersDumpAll( p_libvlc );
1007     stats_TimersCleanAll( p_libvlc );
1008
1009 #ifdef ENABLE_SOUT
1010     announce_handler_t * p_announce;
1011
1012     /* Free announce handler(s?) */
1013     while( (p_announce = vlc_object_find( p_libvlc, VLC_OBJECT_ANNOUNCE,
1014                                                  FIND_CHILD ) ) )
1015     {
1016         msg_Dbg( p_libvlc, "removing announce handler" );
1017         vlc_object_detach( p_announce );
1018         vlc_object_release( p_announce );
1019         announce_HandlerDestroy( p_announce );
1020     }
1021 #endif
1022
1023     bool b_clean = true;
1024     FOREACH_ARRAY( input_item_t *p_del, p_libvlc->input_items )
1025         msg_Err( p_libvlc, "input item %p has not been deleted properly: refcount %d, name %s",
1026             p_del, p_del->i_gc_refcount, p_del->psz_name ? p_del->psz_name : "(null)" );
1027         b_clean = false;
1028     FOREACH_END();
1029     assert( b_clean );
1030     ARRAY_RESET( p_libvlc->input_items );
1031
1032     msg_Dbg( p_libvlc, "removing stats" );
1033     vlc_mutex_destroy( &p_libvlc->p_stats->lock );
1034     FREENULL( p_libvlc->p_stats );
1035
1036     return VLC_SUCCESS;
1037 }
1038
1039 /**
1040  * Destroy everything.
1041  * This function requests the running threads to finish, waits for their
1042  * termination, and destroys their structure.
1043  * It stops the thread systems: no instance can run after this has run
1044  * \param p_libvlc the instance to destroy
1045  * \param b_release whether we should do a release on the instance
1046  */
1047 int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, bool b_release )
1048 {
1049     if( !p_libvlc )
1050         return VLC_EGENERIC;
1051
1052 #ifndef WIN32
1053     char* psz_pidfile = NULL;
1054
1055     if( config_GetInt( p_libvlc, "daemon" ) > 0 )
1056     {
1057         psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
1058         if( psz_pidfile != NULL )
1059         {
1060             msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
1061             if( unlink( psz_pidfile ) == -1 )
1062             {
1063                 msg_Dbg( p_libvlc, "removing pid file %s: %m",
1064                         psz_pidfile );
1065             }
1066         }
1067         free( psz_pidfile );
1068     }
1069 #endif
1070
1071     if( p_libvlc->p_memcpy_module )
1072     {
1073         module_Unneed( p_libvlc, p_libvlc->p_memcpy_module );
1074         p_libvlc->p_memcpy_module = NULL;
1075     }
1076
1077     /* Free module bank. It is refcounted, so we call this each time  */
1078     module_EndBank( p_libvlc );
1079
1080     FREENULL( p_libvlc->psz_homedir );
1081     FREENULL( p_libvlc->psz_configdir );
1082     FREENULL( p_libvlc->psz_datadir );
1083     FREENULL( p_libvlc->psz_cachedir );
1084     FREENULL( p_libvlc->psz_configfile );
1085     var_DelCallback( p_libvlc, "key-pressed", vlc_key_to_action,
1086                      p_libvlc->p_hotkeys );
1087     FREENULL( p_libvlc->p_hotkeys );
1088
1089     vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
1090     i_instances--;
1091
1092     if( i_instances == 0 )
1093     {
1094         /* System specific cleaning code */
1095         system_End( p_libvlc );
1096     }
1097     vlc_mutex_unlock( lock );
1098
1099     msg_Flush( p_libvlc );
1100     msg_Destroy( p_libvlc );
1101
1102     /* Destroy mutexes */
1103     vlc_mutex_destroy( &p_libvlc->config_lock );
1104     vlc_mutex_destroy( &p_libvlc->timer_lock );
1105
1106     if( b_release ) vlc_object_release( p_libvlc );
1107     vlc_object_release( p_libvlc );
1108     p_libvlc = NULL;
1109
1110     /* Stop thread system: last one out please shut the door!
1111      * The number of initializations of the thread system is counted, we
1112      * can call this each time */
1113     vlc_threads_end( p_libvlc_global );
1114
1115     return VLC_SUCCESS;
1116 }
1117
1118 /**
1119  * Add an interface plugin and run it
1120  */
1121 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc,
1122                             char const *psz_module,
1123                             bool b_block, bool b_play,
1124                             int i_options, const char *const *ppsz_options )
1125 {
1126     int i_err;
1127     intf_thread_t *p_intf = NULL;
1128
1129     if( !p_libvlc )
1130         return VLC_EGENERIC;
1131
1132     if( !psz_module ) /* requesting the default interface */
1133     {
1134         char *psz_interface = config_GetPsz( p_libvlc, "intf" );
1135         if( !psz_interface || !*psz_interface ) /* "intf" has not been set */
1136             msg_Info( p_libvlc, _("Running vlc with the default interface. Use 'cvlc' to use vlc without interface.") );
1137         free( psz_interface );
1138     }
1139
1140 #ifndef WIN32
1141     if( p_libvlc_global->b_daemon && b_block && !psz_module )
1142     {
1143         /* Daemon mode hack.
1144          * We prefer the dummy interface if none is specified. */
1145         char *psz_interface = config_GetPsz( p_libvlc, "intf" );
1146         if( !psz_interface || !*psz_interface ) psz_module = "dummy";
1147         free( psz_interface );
1148     }
1149 #endif
1150
1151     /* Try to create the interface */
1152     p_intf = intf_Create( p_libvlc, psz_module ? psz_module : "$intf",
1153                           i_options, ppsz_options );
1154     if( p_intf == NULL )
1155     {
1156         msg_Err( p_libvlc, "interface \"%s\" initialization failed",
1157                  psz_module );
1158         return VLC_EGENERIC;
1159     }
1160
1161     /* Interface doesn't handle play on start so do it ourselves */
1162     if( !p_intf->b_play && b_play )
1163         playlist_Play( p_libvlc->p_playlist );
1164
1165     /* Try to run the interface */
1166     p_intf->b_play = b_play;
1167     i_err = intf_RunThread( p_intf );
1168     if( i_err || p_intf->b_should_run_on_first_thread )
1169     {
1170         vlc_object_detach( p_intf );
1171         intf_Destroy( p_intf );
1172         p_intf = NULL;
1173         return i_err;
1174     }
1175
1176     if( b_block )
1177     {
1178         /* FIXME: should be moved to interface/interface.c */
1179         if( p_intf->pf_run )
1180             vlc_thread_join( p_intf );
1181         else
1182             while( vlc_object_lock_and_wait( p_intf ) == 0 );
1183
1184         vlc_object_detach( p_intf );
1185         intf_Destroy( p_intf );
1186     }
1187
1188     return VLC_SUCCESS;
1189 };
1190
1191 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
1192     ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1193 /*****************************************************************************
1194  * SetLanguage: set the interface language.
1195  *****************************************************************************
1196  * We set the LC_MESSAGES locale category for interface messages and buttons,
1197  * as well as the LC_CTYPE category for string sorting and possible wide
1198  * character support.
1199  *****************************************************************************/
1200 static void SetLanguage ( const char *psz_lang )
1201 {
1202 #ifdef __APPLE__
1203     /* I need that under Darwin, please check it doesn't disturb
1204      * other platforms. --Meuuh */
1205     setenv( "LANG", psz_lang, 1 );
1206
1207 #else
1208     /* We set LC_ALL manually because it is the only way to set
1209      * the language at runtime under eg. Windows. Beware that this
1210      * makes the environment unconsistent when libvlc is unloaded and
1211      * should probably be moved to a safer place like vlc.c. */
1212     static char psz_lcall[20];
1213     snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1214     psz_lcall[19] = '\0';
1215     putenv( psz_lcall );
1216 #endif
1217
1218     setlocale( LC_ALL, psz_lang );
1219 }
1220 #endif
1221
1222
1223 static inline int LoadMessages (void)
1224 {
1225 #if defined( ENABLE_NLS ) \
1226      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1227     /* Specify where to find the locales for current domain */
1228 #if !defined( __APPLE__ ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1229     static const char psz_path[] = LOCALEDIR;
1230 #else
1231     char psz_path[1024];
1232     if (snprintf (psz_path, sizeof (psz_path), "%s/%s",
1233                   p_libvlc_global->psz_vlcpath, "locale")
1234                      >= (int)sizeof (psz_path))
1235         return -1;
1236
1237 #endif
1238     if (bindtextdomain (PACKAGE_NAME, psz_path) == NULL)
1239     {
1240         fprintf (stderr, "Warning: cannot bind text domain "PACKAGE_NAME
1241                          " to directory %s\n", psz_path);
1242         return -1;
1243     }
1244
1245     /* LibVLC wants all messages in UTF-8.
1246      * Unfortunately, we cannot ask UTF-8 for strerror_r(), strsignal_r()
1247      * and other functions that are not part of our text domain.
1248      */
1249     if (bind_textdomain_codeset (PACKAGE_NAME, "UTF-8") == NULL)
1250     {
1251         fprintf (stderr, "Error: cannot set Unicode encoding for text domain "
1252                          PACKAGE_NAME"\n");
1253         // Unbinds the text domain to avoid broken encoding
1254         bindtextdomain (PACKAGE_NAME, "DOES_NOT_EXIST");
1255         return -1;
1256     }
1257
1258     /* LibVLC does NOT set the default textdomain, since it is a library.
1259      * This could otherwise break programs using LibVLC (other than VLC).
1260      * textdomain (PACKAGE_NAME);
1261      */
1262 #endif
1263     return 0;
1264 }
1265
1266 /*****************************************************************************
1267  * GetFilenames: parse command line options which are not flags
1268  *****************************************************************************
1269  * Parse command line for input files as well as their associated options.
1270  * An option always follows its associated input and begins with a ":".
1271  *****************************************************************************/
1272 static int GetFilenames( libvlc_int_t *p_vlc, int i_argc, const char *ppsz_argv[] )
1273 {
1274     int i_opt, i_options;
1275
1276     /* We assume that the remaining parameters are filenames
1277      * and their input options */
1278     for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1279     {
1280         i_options = 0;
1281
1282         /* Count the input options */
1283         while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1284         {
1285             i_options++;
1286             i_opt--;
1287         }
1288
1289         /* TODO: write an internal function of this one, to avoid
1290          *       unnecessary lookups. */
1291
1292         VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[i_opt],
1293                        ( i_options ? &ppsz_argv[i_opt + 1] :
1294                                         NULL ), i_options,
1295                        PLAYLIST_INSERT, 0 );
1296     }
1297
1298     return VLC_SUCCESS;
1299 }
1300
1301 /*****************************************************************************
1302  * Help: print program help
1303  *****************************************************************************
1304  * Print a short inline help. Message interface is initialized at this stage.
1305  *****************************************************************************/
1306 static void Help( libvlc_int_t *p_this, char const *psz_help_name )
1307 {
1308 #ifdef WIN32
1309     ShowConsole( true );
1310 #endif
1311
1312     if( psz_help_name && !strcmp( psz_help_name, "help" ) )
1313     {
1314         utf8_fprintf( stdout, vlc_usage, p_this->psz_object_name );
1315         Usage( p_this, "help" );
1316         Usage( p_this, "main" );
1317     }
1318     else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
1319     {
1320         utf8_fprintf( stdout, vlc_usage, p_this->psz_object_name );
1321         Usage( p_this, NULL );
1322     }
1323     else if( psz_help_name )
1324     {
1325         Usage( p_this, psz_help_name );
1326     }
1327
1328 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1329     PauseConsole();
1330 #endif
1331 }
1332
1333 /*****************************************************************************
1334  * Usage: print module usage
1335  *****************************************************************************
1336  * Print a short inline help. Message interface is initialized at this stage.
1337  *****************************************************************************/
1338 static void Usage( libvlc_int_t *p_this, char const *psz_module_name )
1339 {
1340 #define FORMAT_STRING "  %s --%s%s%s%s%s%s%s "
1341     /* short option ------'    | | | | | | |
1342      * option name ------------' | | | | | |
1343      * <bra ---------------------' | | | | |
1344      * option type or "" ----------' | | | |
1345      * ket> -------------------------' | | |
1346      * padding spaces -----------------' | |
1347      * comment --------------------------' |
1348      * comment suffix ---------------------'
1349      *
1350      * The purpose of having bra and ket is that we might i18n them as well.
1351      */
1352
1353 #   define COL(x)  "\033[" #x ";1m"
1354 #   define RED     COL(31)
1355 #   define GREEN   COL(32)
1356 #   define YELLOW  COL(33)
1357 #   define BLUE    COL(34)
1358 #   define MAGENTA COL(35)
1359 #   define CYAN    COL(36)
1360 #   define WHITE   COL(0)
1361 #   define GRAY    "\033[0m"
1362 #define COLOR_FORMAT_STRING (WHITE"  %s --%s"YELLOW"%s%s%s%s%s%s "GRAY)
1363 #define COLOR_FORMAT_STRING_BOOL (WHITE"  %s --%s%s%s%s%s%s%s "GRAY)
1364
1365 #define LINE_START 8
1366 #define PADDING_SPACES 25
1367 #ifdef WIN32
1368 #   define OPTION_VALUE_SEP "="
1369 #else
1370 #   define OPTION_VALUE_SEP " "
1371 #endif
1372     vlc_list_t *p_list = NULL;
1373     char psz_spaces_text[PADDING_SPACES+LINE_START+1];
1374     char psz_spaces_longtext[LINE_START+3];
1375     char psz_format[sizeof(COLOR_FORMAT_STRING)];
1376     char psz_format_bool[sizeof(COLOR_FORMAT_STRING_BOOL)];
1377     char psz_buffer[10000];
1378     char psz_short[4];
1379     int i_index;
1380     int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1381     int i_width_description = i_width + PADDING_SPACES - 1;
1382     bool b_advanced    = config_GetInt( p_this, "advanced" ) > 0;
1383     bool b_description = config_GetInt( p_this, "help-verbose" ) > 0;
1384     bool b_description_hack;
1385     bool b_color       = config_GetInt( p_this, "color" ) > 0;
1386     bool b_has_advanced = false;
1387
1388     memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
1389     psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
1390     memset( psz_spaces_longtext, ' ', LINE_START+2 );
1391     psz_spaces_longtext[LINE_START+2] = '\0';
1392 #ifdef WIN32
1393     b_color = false; // don't put color control codes in a .txt file
1394 #endif
1395
1396     if( b_color )
1397     {
1398         strcpy( psz_format, COLOR_FORMAT_STRING );
1399         strcpy( psz_format_bool, COLOR_FORMAT_STRING_BOOL );
1400     }
1401     else
1402     {
1403         strcpy( psz_format, FORMAT_STRING );
1404         strcpy( psz_format_bool, FORMAT_STRING );
1405     }
1406
1407     /* List all modules */
1408     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1409
1410     /* Ugly hack to make sure that the help options always come first
1411      * (part 1) */
1412     if( !psz_module_name )
1413         Usage( p_this, "help" );
1414
1415     /* Enumerate the config for each module */
1416     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1417     {
1418         bool b_help_module;
1419         module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
1420         module_config_t *p_item = NULL;
1421         module_config_t *p_end = p_parser->p_config + p_parser->confsize;
1422
1423         if( psz_module_name && strcmp( psz_module_name,
1424                                        p_parser->psz_object_name ) )
1425         {
1426             const char *const *pp_shortcut = p_parser->pp_shortcuts;
1427             while( *pp_shortcut )
1428             {
1429                 if( !strcmp( psz_module_name, *pp_shortcut ) )
1430                     break;
1431                 pp_shortcut ++;
1432             }
1433             if( !*pp_shortcut )
1434                 continue;
1435         }
1436
1437         /* Ignore modules without config options */
1438         if( !p_parser->i_config_items )
1439         {
1440             continue;
1441         }
1442
1443         b_help_module = !strcmp( "help", p_parser->psz_object_name );
1444         /* Ugly hack to make sure that the help options always come first
1445          * (part 2) */
1446         if( !psz_module_name && b_help_module )
1447             continue;
1448
1449         /* Ignore modules with only advanced config options if requested */
1450         if( !b_advanced )
1451         {
1452             for( p_item = p_parser->p_config;
1453                  p_item < p_end;
1454                  p_item++ )
1455             {
1456                 if( (p_item->i_type & CONFIG_ITEM) &&
1457                     !p_item->b_advanced ) break;
1458             }
1459         }
1460
1461         /* Print name of module */
1462         if( strcmp( "main", p_parser->psz_object_name ) )
1463         {
1464             if( b_color )
1465                 utf8_fprintf( stdout, "\n " GREEN "%s" GRAY "\n",
1466                               p_parser->psz_longname );
1467             else
1468                 utf8_fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1469         }
1470         if( p_parser->psz_help )
1471         {
1472             if( b_color )
1473                 utf8_fprintf( stdout, CYAN" %s\n"GRAY, p_parser->psz_help );
1474             else
1475                 utf8_fprintf( stdout, " %s\n", p_parser->psz_help );
1476         }
1477
1478         /* Print module options */
1479         for( p_item = p_parser->p_config;
1480              p_item < p_end;
1481              p_item++ )
1482         {
1483             char *psz_text, *psz_spaces = psz_spaces_text;
1484             const char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1485             const char *psz_suf = "", *psz_prefix = NULL;
1486             signed int i;
1487             size_t i_cur_width;
1488
1489             /* Skip removed options */
1490             if( p_item->b_removed )
1491             {
1492                 continue;
1493             }
1494             /* Skip advanced options if requested */
1495             if( p_item->b_advanced && !b_advanced )
1496             {
1497                 b_has_advanced = true;
1498                 continue;
1499             }
1500
1501             switch( p_item->i_type )
1502             {
1503             case CONFIG_HINT_CATEGORY:
1504             case CONFIG_HINT_USAGE:
1505                 if( !strcmp( "main", p_parser->psz_object_name ) )
1506                 {
1507                     if( b_color )
1508                         utf8_fprintf( stdout, GREEN "\n %s\n" GRAY,
1509                                       p_item->psz_text );
1510                     else
1511                         utf8_fprintf( stdout, "\n %s\n", p_item->psz_text );
1512                 }
1513                 if( b_description && p_item->psz_longtext )
1514                 {
1515                     if( b_color )
1516                         utf8_fprintf( stdout, CYAN " %s\n" GRAY,
1517                                       p_item->psz_longtext );
1518                     else
1519                         utf8_fprintf( stdout, " %s\n", p_item->psz_longtext );
1520                 }
1521                 break;
1522
1523             case CONFIG_HINT_SUBCATEGORY:
1524                 if( strcmp( "main", p_parser->psz_object_name ) )
1525                 break;
1526             case CONFIG_SECTION:
1527                 if( b_color )
1528                 {
1529                     utf8_fprintf( stdout, RED"   %s:\n"GRAY,
1530                                   p_item->psz_text );
1531                     if( b_description && p_item->psz_longtext )
1532                         utf8_fprintf( stdout, MAGENTA"   %s\n"GRAY,
1533                                       p_item->psz_longtext );
1534                 }
1535                 else
1536                 {
1537                     utf8_fprintf( stdout, "   %s:\n", p_item->psz_text );
1538                     if( b_description && p_item->psz_longtext )
1539                         utf8_fprintf( stdout, "   %s\n", p_item->psz_longtext );
1540                 }
1541                 break;
1542
1543             case CONFIG_ITEM_STRING:
1544             case CONFIG_ITEM_FILE:
1545             case CONFIG_ITEM_DIRECTORY:
1546             case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1547             case CONFIG_ITEM_MODULE_CAT:
1548             case CONFIG_ITEM_MODULE_LIST:
1549             case CONFIG_ITEM_MODULE_LIST_CAT:
1550             case CONFIG_ITEM_FONT:
1551             case CONFIG_ITEM_PASSWORD:
1552                 psz_bra = OPTION_VALUE_SEP "<";
1553                 psz_type = _("string");
1554                 psz_ket = ">";
1555
1556                 if( p_item->ppsz_list )
1557                 {
1558                     psz_bra = OPTION_VALUE_SEP "{";
1559                     psz_type = psz_buffer;
1560                     psz_buffer[0] = '\0';
1561                     for( i = 0; p_item->ppsz_list[i]; i++ )
1562                     {
1563                         if( i ) strcat( psz_buffer, "," );
1564                         strcat( psz_buffer, p_item->ppsz_list[i] );
1565                     }
1566                     psz_ket = "}";
1567                 }
1568                 break;
1569             case CONFIG_ITEM_INTEGER:
1570             case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1571                 psz_bra = OPTION_VALUE_SEP "<";
1572                 psz_type = _("integer");
1573                 psz_ket = ">";
1574
1575                 if( p_item->min.i || p_item->max.i )
1576                 {
1577                     sprintf( psz_buffer, "%s [%i .. %i]", psz_type,
1578                              p_item->min.i, p_item->max.i );
1579                     psz_type = psz_buffer;
1580                 }
1581
1582                 if( p_item->i_list )
1583                 {
1584                     psz_bra = OPTION_VALUE_SEP "{";
1585                     psz_type = psz_buffer;
1586                     psz_buffer[0] = '\0';
1587                     for( i = 0; p_item->ppsz_list_text[i]; i++ )
1588                     {
1589                         if( i ) strcat( psz_buffer, ", " );
1590                         sprintf( psz_buffer + strlen(psz_buffer), "%i (%s)",
1591                                  p_item->pi_list[i],
1592                                  p_item->ppsz_list_text[i] );
1593                     }
1594                     psz_ket = "}";
1595                 }
1596                 break;
1597             case CONFIG_ITEM_FLOAT:
1598                 psz_bra = OPTION_VALUE_SEP "<";
1599                 psz_type = _("float");
1600                 psz_ket = ">";
1601                 if( p_item->min.f || p_item->max.f )
1602                 {
1603                     sprintf( psz_buffer, "%s [%f .. %f]", psz_type,
1604                              p_item->min.f, p_item->max.f );
1605                     psz_type = psz_buffer;
1606                 }
1607                 break;
1608             case CONFIG_ITEM_BOOL:
1609                 psz_bra = ""; psz_type = ""; psz_ket = "";
1610                 if( !b_help_module )
1611                 {
1612                     psz_suf = p_item->value.i ? _(" (default enabled)") :
1613                                                 _(" (default disabled)");
1614                 }
1615                 break;
1616             }
1617
1618             if( !psz_type )
1619             {
1620                 continue;
1621             }
1622
1623             /* Add short option if any */
1624             if( p_item->i_short )
1625             {
1626                 sprintf( psz_short, "-%c,", p_item->i_short );
1627             }
1628             else
1629             {
1630                 strcpy( psz_short, "   " );
1631             }
1632
1633             i = PADDING_SPACES - strlen( p_item->psz_name )
1634                  - strlen( psz_bra ) - strlen( psz_type )
1635                  - strlen( psz_ket ) - 1;
1636
1637             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1638             {
1639                 psz_prefix =  ", --no-";
1640                 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1641             }
1642
1643             if( i < 0 )
1644             {
1645                 psz_spaces[0] = '\n';
1646                 i = 0;
1647             }
1648             else
1649             {
1650                 psz_spaces[i] = '\0';
1651             }
1652
1653             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1654             {
1655                 utf8_fprintf( stdout, psz_format_bool, psz_short, 
1656                               p_item->psz_name, psz_prefix, p_item->psz_name,
1657                               psz_bra, psz_type, psz_ket, psz_spaces );
1658             }
1659             else
1660             {
1661                 utf8_fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1662                          "", "", psz_bra, psz_type, psz_ket, psz_spaces );
1663             }
1664
1665             psz_spaces[i] = ' ';
1666
1667             /* We wrap the rest of the output */
1668             sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
1669             b_description_hack = b_description;
1670
1671  description:
1672             psz_text = psz_buffer;
1673             i_cur_width = b_description && !b_description_hack
1674                           ? i_width_description
1675                           : i_width;
1676             while( *psz_text )
1677             {
1678                 char *psz_parser, *psz_word;
1679                 size_t i_end = strlen( psz_text );
1680
1681                 /* If the remaining text fits in a line, print it. */
1682                 if( i_end <= i_cur_width )
1683                 {
1684                     if( b_color )
1685                     {
1686                         if( !b_description || b_description_hack )
1687                             utf8_fprintf( stdout, BLUE"%s\n"GRAY, psz_text );
1688                         else
1689                             utf8_fprintf( stdout, "%s\n", psz_text );
1690                     }
1691                     else
1692                     {
1693                         utf8_fprintf( stdout, "%s\n", psz_text );
1694                     }
1695                     break;
1696                 }
1697
1698                 /* Otherwise, eat as many words as possible */
1699                 psz_parser = psz_text;
1700                 do
1701                 {
1702                     psz_word = psz_parser;
1703                     psz_parser = strchr( psz_word, ' ' );
1704                     /* If no space was found, we reached the end of the text
1705                      * block; otherwise, we skip the space we just found. */
1706                     psz_parser = psz_parser ? psz_parser + 1
1707                                             : psz_text + i_end;
1708
1709                 } while( (size_t)(psz_parser - psz_text) <= i_cur_width );
1710
1711                 /* We cut a word in one of these cases:
1712                  *  - it's the only word in the line and it's too long.
1713                  *  - we used less than 80% of the width and the word we are
1714                  *    going to wrap is longer than 40% of the width, and even
1715                  *    if the word would have fit in the next line. */
1716                 if( psz_word == psz_text
1717              || ( (size_t)(psz_word - psz_text) < 80 * i_cur_width / 100
1718              && (size_t)(psz_parser - psz_word) > 40 * i_cur_width / 100 ) )
1719                 {
1720                     char c = psz_text[i_cur_width];
1721                     psz_text[i_cur_width] = '\0';
1722                     if( b_color )
1723                     {
1724                         if( !b_description || b_description_hack )
1725                             utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1726                                           psz_text, psz_spaces );
1727                         else
1728                             utf8_fprintf( stdout, "%s\n%s",
1729                                           psz_text, psz_spaces );
1730                     }
1731                     else
1732                     {
1733                         utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1734                     }
1735                     psz_text += i_cur_width;
1736                     psz_text[0] = c;
1737                 }
1738                 else
1739                 {
1740                     psz_word[-1] = '\0';
1741                     if( b_color )
1742                     {
1743                         if( !b_description || b_description_hack )
1744                             utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
1745                                           psz_text, psz_spaces );
1746                         else
1747                             utf8_fprintf( stdout, "%s\n%s",
1748                                           psz_text, psz_spaces );
1749                     }
1750                     else
1751                     {
1752                         utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
1753                     }
1754                     psz_text = psz_word;
1755                 }
1756             }
1757
1758             if( b_description_hack && p_item->psz_longtext )
1759             {
1760                 sprintf( psz_buffer, "%s%s", p_item->psz_longtext, psz_suf );
1761                 b_description_hack = false;
1762                 psz_spaces = psz_spaces_longtext;
1763                 utf8_fprintf( stdout, "%s", psz_spaces );
1764                 goto description;
1765             }
1766         }
1767     }
1768
1769     if( b_has_advanced ) 
1770     {
1771         if( b_color )
1772             utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " %s\n", _( "Note:" ),
1773            _( "add --advanced to your command line to see advanced options."));
1774         else
1775             utf8_fprintf( stdout, "\n %s %s\n", _( "Note:" ),
1776            _( "add --advanced to your command line to see advanced options."));
1777     }
1778
1779     /* Release the module list */
1780     vlc_list_release( p_list );
1781 }
1782
1783 /*****************************************************************************
1784  * ListModules: list the available modules with their description
1785  *****************************************************************************
1786  * Print a list of all available modules (builtins and plugins) and a short
1787  * description for each one.
1788  *****************************************************************************/
1789 static void ListModules( libvlc_int_t *p_this, bool b_verbose )
1790 {
1791     vlc_list_t *p_list = NULL;
1792     module_t *p_parser = NULL;
1793     char psz_spaces[22];
1794     int i_index;
1795
1796     bool b_color = config_GetInt( p_this, "color" ) > 0;
1797
1798     memset( psz_spaces, ' ', 22 );
1799
1800 #ifdef WIN32
1801     ShowConsole( true );
1802 #endif
1803
1804     /* List all modules */
1805     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1806
1807     /* Enumerate each module */
1808     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1809     {
1810         int i;
1811
1812         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1813
1814         /* Nasty hack, but right now I'm too tired to think about a nice
1815          * solution */
1816         i = 22 - strlen( p_parser->psz_object_name ) - 1;
1817         if( i < 0 ) i = 0;
1818         psz_spaces[i] = 0;
1819
1820         if( b_color )
1821             utf8_fprintf( stdout, GREEN"  %s%s "WHITE"%s\n"GRAY,
1822                           p_parser->psz_object_name,
1823                           psz_spaces,
1824                           p_parser->psz_longname );
1825         else
1826             utf8_fprintf( stdout, "  %s%s %s\n",
1827                           p_parser->psz_object_name,
1828                           psz_spaces, p_parser->psz_longname );
1829
1830         if( b_verbose )
1831         {
1832             const char *const *pp_shortcut = p_parser->pp_shortcuts;
1833             while( *pp_shortcut )
1834             {
1835                 if( strcmp( *pp_shortcut, p_parser->psz_object_name ) )
1836                 {
1837                     if( b_color )
1838                         utf8_fprintf( stdout, CYAN"   s %s\n"GRAY,
1839                                       *pp_shortcut );
1840                     else
1841                         utf8_fprintf( stdout, "   s %s\n",
1842                                       *pp_shortcut );
1843                 }
1844                 pp_shortcut++;
1845             }
1846             if( p_parser->psz_capability )
1847             {
1848                 if( b_color )
1849                     utf8_fprintf( stdout, MAGENTA"   c %s (%d)\n"GRAY,
1850                                   p_parser->psz_capability,
1851                                   p_parser->i_score );
1852                 else
1853                     utf8_fprintf( stdout, "   c %s (%d)\n",
1854                                   p_parser->psz_capability,
1855                                   p_parser->i_score );
1856             }
1857         }
1858
1859         psz_spaces[i] = ' ';
1860     }
1861
1862     vlc_list_release( p_list );
1863
1864 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1865     PauseConsole();
1866 #endif
1867 }
1868
1869 /*****************************************************************************
1870  * Version: print complete program version
1871  *****************************************************************************
1872  * Print complete program version and build number.
1873  *****************************************************************************/
1874 static void Version( void )
1875 {
1876 #ifdef WIN32
1877     ShowConsole( true );
1878 #endif
1879
1880     utf8_fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
1881     utf8_fprintf( stdout, _("Compiled by %s@%s.%s\n"),
1882              VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
1883     utf8_fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
1884     if( strcmp( VLC_Changeset(), "exported" ) )
1885         utf8_fprintf( stdout, _("Based upon Git commit [%s]\n"),
1886                  VLC_Changeset() );
1887     utf8_fprintf( stdout, LICENSE_MSG );
1888
1889 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1890     PauseConsole();
1891 #endif
1892 }
1893
1894 /*****************************************************************************
1895  * ShowConsole: On Win32, create an output console for debug messages
1896  *****************************************************************************
1897  * This function is useful only on Win32.
1898  *****************************************************************************/
1899 #ifdef WIN32 /*  */
1900 static void ShowConsole( bool b_dofile )
1901 {
1902 #   ifndef UNDER_CE
1903     FILE *f_help = NULL;
1904
1905     if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1906
1907     AllocConsole();
1908     /* Use the ANSI code page (e.g. Windows-1252) as expected by the LibVLC
1909      * Unicode/locale subsystem. By default, we have the obsolecent OEM code
1910      * page (e.g. CP437 or CP850). */
1911     SetConsoleOutputCP (GetACP ());
1912     SetConsoleTitle ("VLC media player version "PACKAGE_VERSION);
1913
1914     freopen( "CONOUT$", "w", stderr );
1915     freopen( "CONIN$", "r", stdin );
1916
1917     if( b_dofile && (f_help = fopen( "vlc-help.txt", "wt" )) )
1918     {
1919         fclose( f_help );
1920         freopen( "vlc-help.txt", "wt", stdout );
1921         utf8_fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
1922     }
1923     else freopen( "CONOUT$", "w", stdout );
1924
1925 #   endif
1926 }
1927 #endif
1928
1929 /*****************************************************************************
1930  * PauseConsole: On Win32, wait for a key press before closing the console
1931  *****************************************************************************
1932  * This function is useful only on Win32.
1933  *****************************************************************************/
1934 #ifdef WIN32 /*  */
1935 static void PauseConsole( void )
1936 {
1937 #   ifndef UNDER_CE
1938
1939     if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
1940
1941     utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
1942     getchar();
1943     fclose( stdout );
1944
1945 #   endif
1946 }
1947 #endif
1948
1949 /*****************************************************************************
1950  * ConsoleWidth: Return the console width in characters
1951  *****************************************************************************
1952  * We use the stty shell command to get the console width; if this fails or
1953  * if the width is less than 80, we default to 80.
1954  *****************************************************************************/
1955 static int ConsoleWidth( void )
1956 {
1957     unsigned i_width = 80;
1958
1959 #ifndef WIN32
1960     FILE *file = popen( "stty size 2>/dev/null", "r" );
1961     if (file != NULL)
1962     {
1963         if (fscanf (file, "%*u %u", &i_width) <= 0)
1964             i_width = 80;
1965         pclose( file );
1966     }
1967 #else
1968     CONSOLE_SCREEN_BUFFER_INFO buf;
1969
1970     if (GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &buf))
1971         i_width = buf.dwSize.X;
1972 #endif
1973
1974     return i_width;
1975 }
1976
1977 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
1978                      vlc_value_t old_val, vlc_value_t new_val, void *param)
1979 {
1980     libvlc_int_t *p_libvlc = (libvlc_int_t *)p_this;
1981     (void)psz_variable;
1982     (void)old_val;
1983     (void)param;
1984
1985     if( new_val.i_int >= -1 )
1986     {
1987         p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
1988     }
1989     return VLC_SUCCESS;
1990 }
1991
1992 /*****************************************************************************
1993  * InitDeviceValues: initialize device values
1994  *****************************************************************************
1995  * This function inits the dvd, vcd and cd-audio values
1996  *****************************************************************************/
1997 static void InitDeviceValues( libvlc_int_t *p_vlc )
1998 {
1999 #ifdef HAVE_HAL
2000     LibHalContext * ctx = NULL;
2001     int i, i_devices;
2002     char **devices = NULL;
2003     char *block_dev = NULL;
2004     dbus_bool_t b_dvd;
2005
2006 #ifdef HAVE_HAL_1
2007     DBusConnection *p_connection = NULL;
2008     DBusError       error;
2009
2010     ctx = libhal_ctx_new();
2011     if( !ctx ) return;
2012     dbus_error_init( &error );
2013     p_connection = dbus_bus_get ( DBUS_BUS_SYSTEM, &error );
2014     if( dbus_error_is_set( &error ) || !p_connection )
2015     {
2016         dbus_error_free( &error );
2017         return;
2018     }
2019     libhal_ctx_set_dbus_connection( ctx, p_connection );
2020     if( libhal_ctx_init( ctx, &error ) )
2021 #else
2022     ctx = hal_initialize( NULL, FALSE );
2023     if( ctx )
2024 #endif
2025
2026     {
2027 #ifdef HAVE_HAL_1
2028         if( ( devices = libhal_get_all_devices( ctx, &i_devices, NULL ) ) )
2029 #else
2030         if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
2031 #endif
2032         {
2033             for( i = 0; i < i_devices; i++ )
2034             {
2035 #ifdef HAVE_HAL_1
2036                 if( !libhal_device_property_exists( ctx, devices[i],
2037                                                 "storage.cdrom.dvd", NULL ) )
2038 #else
2039                 if( !hal_device_property_exists( ctx, devices[ i ],
2040                                                 "storage.cdrom.dvd" ) )
2041 #endif
2042                 {
2043                     continue;
2044                 }
2045 #ifdef HAVE_HAL_1
2046                 b_dvd = libhal_device_get_property_bool( ctx, devices[ i ],
2047                                                  "storage.cdrom.dvd", NULL  );
2048                 block_dev = libhal_device_get_property_string( ctx,
2049                                 devices[ i ], "block.device" , NULL );
2050 #else
2051                 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
2052                                                       "storage.cdrom.dvd" );
2053                 block_dev = hal_device_get_property_string( ctx, devices[ i ],
2054                                                             "block.device" );
2055 #endif
2056                 if( b_dvd )
2057                 {
2058                     config_PutPsz( p_vlc, "dvd", block_dev );
2059                 }
2060
2061                 config_PutPsz( p_vlc, "vcd", block_dev );
2062                 config_PutPsz( p_vlc, "cd-audio", block_dev );
2063 #ifdef HAVE_HAL_1
2064                 libhal_free_string( block_dev );
2065 #else
2066                 hal_free_string( block_dev );
2067 #endif
2068             }
2069 #ifdef HAVE_HAL_1
2070             libhal_free_string_array( devices );
2071 #else
2072             hal_free_string_array( devices );
2073 #endif
2074         }
2075
2076 #ifdef HAVE_HAL_1
2077         libhal_ctx_shutdown( ctx, NULL );
2078         dbus_connection_unref( p_connection );
2079         libhal_ctx_free( ctx );
2080 #else
2081         hal_shutdown( ctx );
2082 #endif
2083     }
2084     else
2085     {
2086         msg_Warn( p_vlc, "Unable to get HAL device properties" );
2087     }
2088 #else
2089     (void)p_vlc;
2090 #endif /* HAVE_HAL */
2091 }