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