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