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