]> git.sesse.net Git - vlc/blob - src/libvlc.c
* include/vlc_common.h:
[vlc] / src / libvlc.c
1 /*****************************************************************************
2  * libvlc.c: main libvlc source
3  *****************************************************************************
4  * Copyright (C) 1998-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *          Derk-Jan Hartman <hartman at videolan dot org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Pretend we are a builtin module
29  *****************************************************************************/
30 #define MODULE_NAME main
31 #define MODULE_PATH main
32 #define __BUILTIN__
33
34 /*****************************************************************************
35  * Preamble
36  *****************************************************************************/
37 #include <vlc/vlc.h>
38 #include <vlc/input.h>
39
40 #ifdef HAVE_ERRNO_H
41 #   include <errno.h>                                              /* ENOMEM */
42 #endif
43 #include <stdio.h>                                              /* sprintf() */
44 #include <string.h>                                            /* strerror() */
45 #include <stdlib.h>                                                /* free() */
46
47 #ifndef WIN32
48 #   include <netinet/in.h>                            /* BSD: struct in_addr */
49 #endif
50
51 #ifdef HAVE_UNISTD_H
52 #   include <unistd.h>
53 #elif defined( WIN32 ) && !defined( UNDER_CE )
54 #   include <io.h>
55 #endif
56
57 #ifdef WIN32                       /* optind, getopt(), included in unistd.h */
58 #   include "extras/getopt.h"
59 #endif
60
61 #ifdef HAVE_LOCALE_H
62 #   include <locale.h>
63 #endif
64
65 #ifdef HAVE_HAL
66 #   include <hal/libhal.h>
67 #endif
68
69 #include "vlc_cpu.h"                                        /* CPU detection */
70 #include "os_specific.h"
71
72 #include "vlc_error.h"
73
74 #include "vlc_playlist.h"
75 #include "vlc_interface.h"
76
77 #include "audio_output.h"
78
79 #include "vlc_video.h"
80 #include "video_output.h"
81
82 #include "stream_output.h"
83
84 #include "libvlc.h"
85
86 /*****************************************************************************
87  * The evil global variable. We handle it with care, don't worry.
88  *****************************************************************************/
89 static libvlc_t   libvlc;
90 static libvlc_t * p_libvlc;
91 static vlc_t *    p_static_vlc;
92
93 /*****************************************************************************
94  * Local prototypes
95  *****************************************************************************/
96 static void SetLanguage   ( char const * );
97 static int  GetFilenames  ( vlc_t *, int, char *[] );
98 static void Usage         ( vlc_t *, char const *psz_module_name );
99 static void ListModules   ( vlc_t * );
100 static void Version       ( void );
101
102 #ifdef WIN32
103 static void ShowConsole   ( void );
104 #endif
105 static int  ConsoleWidth  ( void );
106
107 static int  VerboseCallback( vlc_object_t *, char const *,
108                              vlc_value_t, vlc_value_t, void * );
109
110 static void InitDeviceValues( vlc_t * );
111
112 /*****************************************************************************
113  * vlc_current_object: return the current object.
114  *****************************************************************************
115  * If i_object is non-zero, return the corresponding object. Otherwise,
116  * return the statically allocated p_vlc object.
117  *****************************************************************************/
118 vlc_t * vlc_current_object( int i_object )
119 {
120     if( i_object )
121     {
122          return vlc_object_get( p_libvlc, i_object );
123     }
124
125     return p_static_vlc;
126 }
127
128 /*****************************************************************************
129  * VLC_Version: return the libvlc version.
130  *****************************************************************************
131  * This function returns full version string (numeric version and codename).
132  *****************************************************************************/
133 char const * VLC_Version( void )
134 {
135     return VERSION_MESSAGE;
136 }
137
138 /*****************************************************************************
139  * VLC_Error: strerror() equivalent
140  *****************************************************************************
141  * This function returns full version string (numeric version and codename).
142  *****************************************************************************/
143 char const * VLC_Error( int i_err )
144 {
145     return vlc_error( i_err );
146 }
147
148 /*****************************************************************************
149  * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
150  *****************************************************************************
151  * This function allocates a vlc_t structure and returns a negative value
152  * in case of failure. Also, the thread system is initialized.
153  *****************************************************************************/
154 int VLC_Create( void )
155 {
156     int i_ret;
157     vlc_t * p_vlc = NULL;
158     vlc_value_t lockval;
159
160     /* &libvlc never changes, so we can safely call this multiple times. */
161     p_libvlc = &libvlc;
162
163     /* vlc_threads_init *must* be the first internal call! No other call is
164      * allowed before the thread system has been initialized. */
165     i_ret = vlc_threads_init( p_libvlc );
166     if( i_ret < 0 )
167     {
168         return i_ret;
169     }
170
171     /* Now that the thread system is initialized, we don't have much, but
172      * at least we have var_Create */
173     var_Create( p_libvlc, "libvlc", VLC_VAR_MUTEX );
174     var_Get( p_libvlc, "libvlc", &lockval );
175     vlc_mutex_lock( lockval.p_address );
176     if( !libvlc.b_ready )
177     {
178         char *psz_env;
179
180         /* Guess what CPU we have */
181         libvlc.i_cpu = CPUCapabilities();
182
183         /* Find verbosity from VLC_VERBOSE environment variable */
184         psz_env = getenv( "VLC_VERBOSE" );
185         libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
186
187 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
188         libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
189 #else
190         libvlc.b_color = VLC_FALSE;
191 #endif
192
193         /* Initialize message queue */
194         msg_Create( p_libvlc );
195
196         /* Announce who we are */
197         msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
198         msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
199
200         /* The module bank will be initialized later */
201         libvlc.p_module_bank = NULL;
202
203         libvlc.b_ready = VLC_TRUE;
204     }
205     vlc_mutex_unlock( lockval.p_address );
206     var_Destroy( p_libvlc, "libvlc" );
207
208     /* Allocate a vlc object */
209     p_vlc = vlc_object_create( p_libvlc, VLC_OBJECT_VLC );
210     if( p_vlc == NULL )
211     {
212         return VLC_EGENERIC;
213     }
214     p_vlc->thread_id = 0;
215     vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
216
217     p_vlc->psz_object_name = "root";
218
219     /* Initialize mutexes */
220     vlc_mutex_init( p_vlc, &p_vlc->config_lock );
221 #ifdef SYS_DARWIN
222     vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
223 #endif
224
225     /* Store our newly allocated structure in the global list */
226     vlc_object_attach( p_vlc, p_libvlc );
227
228     /* Store data for the non-reentrant API */
229     p_static_vlc = p_vlc;
230
231     return p_vlc->i_object_id;
232 }
233
234 /*****************************************************************************
235  * VLC_Init: initialize a vlc_t structure.
236  *****************************************************************************
237  * This function initializes a previously allocated vlc_t structure:
238  *  - CPU detection
239  *  - gettext initialization
240  *  - message queue, module bank and playlist initialization
241  *  - configuration and commandline parsing
242  *****************************************************************************/
243 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
244 {
245     char         p_capabilities[200];
246     char *       p_tmp;
247     char *       psz_modules;
248     char *       psz_parser;
249     char *       psz_language;
250     vlc_bool_t   b_exit = VLC_FALSE;
251     vlc_t *      p_vlc = vlc_current_object( i_object );
252     module_t    *p_help_module;
253     playlist_t  *p_playlist;
254
255     if( !p_vlc )
256     {
257         return VLC_ENOOBJ;
258     }
259
260     /*
261      * System specific initialization code
262      */
263     system_Init( p_vlc, &i_argc, ppsz_argv );
264
265     /* Get the executable name (similar to the basename command) */
266     if( i_argc > 0 )
267     {
268         p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
269         while( *p_tmp )
270         {
271             if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
272             else ++p_tmp;
273         }
274     }
275     else
276     {
277         p_vlc->psz_object_name = "vlc";
278     }
279
280     /*
281      * Support for gettext
282      */
283     SetLanguage( "" );
284
285     /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
286     msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
287
288     /* Initialize the module bank and load the configuration of the
289      * main module. We need to do this at this stage to be able to display
290      * a short help if required by the user. (short help == main module
291      * options) */
292     module_InitBank( p_vlc );
293
294     /* Hack: insert the help module here */
295     p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
296     if( p_help_module == NULL )
297     {
298         module_EndBank( p_vlc );
299         if( i_object ) vlc_object_release( p_vlc );
300         return VLC_EGENERIC;
301     }
302     p_help_module->psz_object_name = "help";
303     p_help_module->psz_longname = N_("Help options");
304     config_Duplicate( p_help_module, p_help_config );
305     vlc_object_attach( p_help_module, libvlc.p_module_bank );
306     /* End hack */
307
308     if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
309     {
310         vlc_object_detach( p_help_module );
311         config_Free( p_help_module );
312         vlc_object_destroy( p_help_module );
313         module_EndBank( p_vlc );
314         if( i_object ) vlc_object_release( p_vlc );
315         return VLC_EGENERIC;
316     }
317
318     /* Check for short help option */
319     if( config_GetInt( p_vlc, "help" ) )
320     {
321         fprintf( stdout, _("Usage: %s [options] [items]...\n"),
322                          p_vlc->psz_object_name );
323         Usage( p_vlc, "main" );
324         Usage( p_vlc, "help" );
325         b_exit = VLC_TRUE;
326     }
327     /* Check for version option */
328     else if( config_GetInt( p_vlc, "version" ) )
329     {
330         Version();
331         b_exit = VLC_TRUE;
332     }
333
334     /* Set the config file stuff */
335     p_vlc->psz_homedir = config_GetHomeDir();
336     p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
337
338     /* Check for plugins cache options */
339     if( config_GetInt( p_vlc, "reset-plugins-cache" ) )
340     {
341         libvlc.p_module_bank->b_cache_delete = VLC_TRUE;
342     }
343
344     /* Hack: remove the help module here */
345     vlc_object_detach( p_help_module );
346     /* End hack */
347
348     /* Will be re-done properly later on */
349     p_vlc->p_libvlc->i_verbose = config_GetInt( p_vlc, "verbose" );
350
351     /* Check for daemon mode */
352 #ifndef WIN32
353     if( config_GetInt( p_vlc, "daemon" ) )
354     {
355         pid_t i_pid = 0;
356
357         if( ( i_pid = fork() ) < 0 )
358         {
359             msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
360             b_exit = VLC_TRUE;
361         }
362         else if( i_pid )
363         {
364             /* This is the parent, exit right now */
365             msg_Dbg( p_vlc, "closing parent process" );
366             b_exit = VLC_TRUE;
367         }
368         else
369         {
370             /* We are the child */
371             msg_Dbg( p_vlc, "daemon spawned" );
372             close( 0 );
373             close( 1 );
374             close( 2 );
375
376             p_vlc->p_libvlc->b_daemon = VLC_TRUE;
377         }
378     }
379 #endif
380
381     if( b_exit )
382     {
383         config_Free( p_help_module );
384         vlc_object_destroy( p_help_module );
385         module_EndBank( p_vlc );
386         if( i_object ) vlc_object_release( p_vlc );
387         return VLC_EEXIT;
388     }
389
390     /* Check for translation config option */
391 #if defined( ENABLE_NLS ) \
392      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
393
394     /* This ain't really nice to have to reload the config here but it seems
395      * the only way to do it. */
396     config_LoadConfigFile( p_vlc, "main" );
397     config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
398
399     /* Check if the user specified a custom language */
400     psz_language = config_GetPsz( p_vlc, "language" );
401     if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
402     {
403         vlc_bool_t b_cache_delete = libvlc.p_module_bank->b_cache_delete;
404
405         /* Reset the default domain */
406         SetLanguage( psz_language );
407
408         /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
409         msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
410
411         textdomain( PACKAGE_NAME );
412
413 #if defined( ENABLE_UTF8 )
414         bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
415 #endif
416
417         module_EndBank( p_vlc );
418         module_InitBank( p_vlc );
419         config_LoadConfigFile( p_vlc, "main" );
420         config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
421         libvlc.p_module_bank->b_cache_delete = b_cache_delete;
422     }
423     if( psz_language ) free( psz_language );
424 #endif
425
426     /*
427      * Load the builtins and plugins into the module_bank.
428      * We have to do it before config_Load*() because this also gets the
429      * list of configuration options exported by each module and loads their
430      * default values.
431      */
432     module_LoadBuiltins( p_vlc );
433     module_LoadPlugins( p_vlc );
434     if( p_vlc->b_die )
435     {
436         b_exit = VLC_TRUE;
437     }
438
439     msg_Dbg( p_vlc, "module bank initialized, found %i modules",
440                     libvlc.p_module_bank->i_children );
441
442     /* Hack: insert the help module here */
443     vlc_object_attach( p_help_module, libvlc.p_module_bank );
444     /* End hack */
445
446     /* Check for help on modules */
447     if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
448     {
449         Usage( p_vlc, p_tmp );
450         free( p_tmp );
451         b_exit = VLC_TRUE;
452     }
453     /* Check for long help option */
454     else if( config_GetInt( p_vlc, "longhelp" ) )
455     {
456         Usage( p_vlc, NULL );
457         b_exit = VLC_TRUE;
458     }
459     /* Check for module list option */
460     else if( config_GetInt( p_vlc, "list" ) )
461     {
462         ListModules( p_vlc );
463         b_exit = VLC_TRUE;
464     }
465
466     /* Check for config file options */
467     if( config_GetInt( p_vlc, "reset-config" ) )
468     {
469         vlc_object_detach( p_help_module );
470         config_ResetAll( p_vlc );
471         config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
472         config_SaveConfigFile( p_vlc, NULL );
473         vlc_object_attach( p_help_module, libvlc.p_module_bank );
474     }
475     if( config_GetInt( p_vlc, "save-config" ) )
476     {
477         vlc_object_detach( p_help_module );
478         config_LoadConfigFile( p_vlc, NULL );
479         config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE );
480         config_SaveConfigFile( p_vlc, NULL );
481         vlc_object_attach( p_help_module, libvlc.p_module_bank );
482     }
483
484     /* Hack: remove the help module here */
485     vlc_object_detach( p_help_module );
486     /* End hack */
487
488     if( b_exit )
489     {
490         config_Free( p_help_module );
491         vlc_object_destroy( p_help_module );
492         module_EndBank( p_vlc );
493         if( i_object ) vlc_object_release( p_vlc );
494         return VLC_EEXIT;
495     }
496
497     /*
498      * Init device values
499      */
500     InitDeviceValues( p_vlc );
501
502     /*
503      * Override default configuration with config file settings
504      */
505     config_LoadConfigFile( p_vlc, NULL );
506
507     /* Hack: insert the help module here */
508     vlc_object_attach( p_help_module, libvlc.p_module_bank );
509     /* End hack */
510
511     /*
512      * Override configuration with command line settings
513      */
514     if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
515     {
516 #ifdef WIN32
517         ShowConsole();
518         /* Pause the console because it's destroyed when we exit */
519         fprintf( stderr, "The command line options couldn't be loaded, check "
520                  "that they are valid.\nPress the RETURN key to continue..." );
521         getchar();
522 #endif
523         vlc_object_detach( p_help_module );
524         config_Free( p_help_module );
525         vlc_object_destroy( p_help_module );
526         module_EndBank( p_vlc );
527         if( i_object ) vlc_object_release( p_vlc );
528         return VLC_EGENERIC;
529     }
530
531     /* Hack: remove the help module here */
532     vlc_object_detach( p_help_module );
533     config_Free( p_help_module );
534     vlc_object_destroy( p_help_module );
535     /* End hack */
536
537     /*
538      * System specific configuration
539      */
540     system_Configure( p_vlc, &i_argc, ppsz_argv );
541
542     /*
543      * Message queue options
544      */
545
546     var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
547     if( config_GetInt( p_vlc, "quiet" ) )
548     {
549         vlc_value_t val;
550         val.i_int = -1;
551         var_Set( p_vlc, "verbose", val );
552     }
553     var_AddCallback( p_vlc, "verbose", VerboseCallback, NULL );
554     var_Change( p_vlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
555
556     libvlc.b_color = libvlc.b_color && config_GetInt( p_vlc, "color" );
557
558     /*
559      * Output messages that may still be in the queue
560      */
561     msg_Flush( p_vlc );
562
563     /* p_vlc initialization. FIXME ? */
564
565 #if defined( __i386__ )
566     if( !config_GetInt( p_vlc, "mmx" ) )
567         libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
568     if( !config_GetInt( p_vlc, "3dn" ) )
569         libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
570     if( !config_GetInt( p_vlc, "mmxext" ) )
571         libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
572     if( !config_GetInt( p_vlc, "sse" ) )
573         libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
574     if( !config_GetInt( p_vlc, "sse2" ) )
575         libvlc.i_cpu &= ~CPU_CAPABILITY_SSE2;
576 #endif
577 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
578     if( !config_GetInt( p_vlc, "altivec" ) )
579         libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
580 #endif
581
582 #define PRINT_CAPABILITY( capability, string )                              \
583     if( libvlc.i_cpu & capability )                                         \
584     {                                                                       \
585         strncat( p_capabilities, string " ",                                \
586                  sizeof(p_capabilities) - strlen(p_capabilities) );         \
587         p_capabilities[sizeof(p_capabilities) - 1] = '\0';                  \
588     }
589
590     p_capabilities[0] = '\0';
591     PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
592     PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
593     PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
594     PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
595     PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
596     PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
597     PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
598     PRINT_CAPABILITY( CPU_CAPABILITY_SSE2, "SSE2" );
599     PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
600     PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
601     msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
602
603     /*
604      * Choose the best memcpy module
605      */
606     p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy", 0 );
607
608     if( p_vlc->pf_memcpy == NULL )
609     {
610         p_vlc->pf_memcpy = memcpy;
611     }
612
613     if( p_vlc->pf_memset == NULL )
614     {
615         p_vlc->pf_memset = memset;
616     }
617
618     /*
619      * Initialize hotkey handling
620      */
621     var_Create( p_vlc, "key-pressed", VLC_VAR_INTEGER );
622     p_vlc->p_hotkeys = malloc( sizeof(p_hotkeys) );
623     /* Do a copy (we don't need to modify the strings) */
624     memcpy( p_vlc->p_hotkeys, p_hotkeys, sizeof(p_hotkeys) );
625
626     /*
627      * Initialize playlist and get commandline files
628      */
629     p_playlist = playlist_Create( p_vlc );
630     if( !p_playlist )
631     {
632         msg_Err( p_vlc, "playlist initialization failed" );
633         if( p_vlc->p_memcpy_module != NULL )
634         {
635             module_Unneed( p_vlc, p_vlc->p_memcpy_module );
636         }
637         module_EndBank( p_vlc );
638         if( i_object ) vlc_object_release( p_vlc );
639         return VLC_EGENERIC;
640     }
641
642     /*
643      * Load background interfaces
644      */
645     psz_modules = config_GetPsz( p_vlc, "extraintf" );
646     psz_parser = psz_modules;
647     while ( psz_parser && *psz_parser )
648     {
649         char *psz_module, *psz_temp;
650         psz_module = psz_parser;
651         psz_parser = strchr( psz_module, ',' );
652         if ( psz_parser )
653         {
654             *psz_parser = '\0';
655             psz_parser++;
656         }
657         psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
658         if( psz_temp )
659         {
660             sprintf( psz_temp, "%s,none", psz_module );
661             VLC_AddIntf( 0, psz_temp, VLC_FALSE, VLC_FALSE );
662             free( psz_temp );
663         }
664     }
665     if ( psz_modules )
666     {
667         free( psz_modules );
668     }
669
670     /*
671      * Allways load the hotkeys interface if it exists
672      */
673     VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
674
675     /*
676      * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
677      */
678     var_Create( p_vlc, "drawable", VLC_VAR_INTEGER );
679     var_Create( p_vlc, "drawableredraw", VLC_VAR_INTEGER );
680     var_Create( p_vlc, "drawablet", VLC_VAR_INTEGER );
681     var_Create( p_vlc, "drawablel", VLC_VAR_INTEGER );
682     var_Create( p_vlc, "drawableb", VLC_VAR_INTEGER );
683     var_Create( p_vlc, "drawabler", VLC_VAR_INTEGER );
684     var_Create( p_vlc, "drawablex", VLC_VAR_INTEGER );
685     var_Create( p_vlc, "drawabley", VLC_VAR_INTEGER );
686     var_Create( p_vlc, "drawablew", VLC_VAR_INTEGER );
687     var_Create( p_vlc, "drawableh", VLC_VAR_INTEGER );
688     var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
689     var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
690
691     /*
692      * Get input filenames given as commandline arguments
693      */
694     GetFilenames( p_vlc, i_argc, ppsz_argv );
695
696     if( i_object ) vlc_object_release( p_vlc );
697     return VLC_SUCCESS;
698 }
699
700 /*****************************************************************************
701  * VLC_AddIntf: add an interface
702  *****************************************************************************
703  * This function opens an interface plugin and runs it. If b_block is set
704  * to 0, VLC_AddIntf will return immediately and let the interface run in a
705  * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
706  * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
707  * the playlist when it is completely initialised.
708  *****************************************************************************/
709 int VLC_AddIntf( int i_object, char const *psz_module,
710                  vlc_bool_t b_block, vlc_bool_t b_play )
711 {
712     int i_err;
713     intf_thread_t *p_intf;
714     vlc_t *p_vlc = vlc_current_object( i_object );
715
716     if( !p_vlc )
717     {
718         return VLC_ENOOBJ;
719     }
720
721 #ifndef WIN32
722     if( p_vlc->p_libvlc->b_daemon && b_block && !psz_module )
723     {
724         /* Daemon mode hack.
725          * We prefer the dummy interface if none is specified. */
726         char *psz_interface = config_GetPsz( p_vlc, "intf" );
727         if( !psz_interface || !*psz_interface ) psz_module = "dummy";
728         if( psz_interface ) free( psz_interface );
729     }
730 #endif
731
732     /* Try to create the interface */
733     p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
734
735     if( p_intf == NULL )
736     {
737         msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
738         if( i_object ) vlc_object_release( p_vlc );
739         return VLC_EGENERIC;
740     }
741
742     /* Interface doesn't handle play on start so do it ourselves */
743     if( !p_intf->b_play && b_play ) VLC_Play( i_object );
744
745     /* Try to run the interface */
746     p_intf->b_play = b_play;
747     p_intf->b_block = b_block;
748     i_err = intf_RunThread( p_intf );
749     if( i_err )
750     {
751         vlc_object_detach( p_intf );
752         intf_Destroy( p_intf );
753         if( i_object ) vlc_object_release( p_vlc );
754         return i_err;
755     }
756
757     if( i_object ) vlc_object_release( p_vlc );
758     return VLC_SUCCESS;
759 }
760
761 /*****************************************************************************
762  * VLC_Die: ask vlc to die.
763  *****************************************************************************
764  * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
765  * task. It is your duty to call VLC_CleanUp and VLC_Destroy afterwards.
766  *****************************************************************************/
767 int VLC_Die( int i_object )
768 {
769     vlc_t *p_vlc = vlc_current_object( i_object );
770
771     if( !p_vlc )
772     {
773         return VLC_ENOOBJ;
774     }
775
776     p_vlc->b_die = VLC_TRUE;
777
778     if( i_object ) vlc_object_release( p_vlc );
779     return VLC_SUCCESS;
780 }
781
782 /*****************************************************************************
783  * VLC_CleanUp: CleanUp all the intf, playlist, vout, aout
784  *****************************************************************************/
785 int VLC_CleanUp( int i_object )
786 {
787     intf_thread_t      * p_intf;
788     playlist_t         * p_playlist;
789     vout_thread_t      * p_vout;
790     aout_instance_t    * p_aout;
791     announce_handler_t * p_announce;
792     vlc_t *p_vlc = vlc_current_object( i_object );
793
794     /* Check that the handle is valid */
795     if( !p_vlc )
796     {
797         return VLC_ENOOBJ;
798     }
799
800     /*
801      * Ask the interfaces to stop and destroy them
802      */
803     msg_Dbg( p_vlc, "removing all interfaces" );
804     while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
805     {
806         intf_StopThread( p_intf );
807         vlc_object_detach( p_intf );
808         vlc_object_release( p_intf );
809         intf_Destroy( p_intf );
810     }
811
812     /*
813      * Free playlists
814      */
815     msg_Dbg( p_vlc, "removing all playlists" );
816     while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
817                                           FIND_CHILD )) )
818     {
819         vlc_object_detach( p_playlist );
820         vlc_object_release( p_playlist );
821         playlist_Destroy( p_playlist );
822     }
823
824     /*
825      * Free video outputs
826      */
827     msg_Dbg( p_vlc, "removing all video outputs" );
828     while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
829     {
830         vlc_object_detach( p_vout );
831         vlc_object_release( p_vout );
832         vout_Destroy( p_vout );
833     }
834
835     /*
836      * Free audio outputs
837      */
838     msg_Dbg( p_vlc, "removing all audio outputs" );
839     while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
840     {
841         vlc_object_detach( (vlc_object_t *)p_aout );
842         vlc_object_release( (vlc_object_t *)p_aout );
843         aout_Delete( p_aout );
844     }
845
846     /*
847      * Free announce handler(s?)
848      */
849     msg_Dbg( p_vlc, "removing announce handler" );
850     while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
851                                                  FIND_CHILD ) ) )
852    {
853         vlc_object_detach( p_announce );
854         vlc_object_release( p_announce );
855         announce_HandlerDestroy( p_announce );
856    }
857
858     if( i_object ) vlc_object_release( p_vlc );
859     return VLC_SUCCESS;
860 }
861
862 /*****************************************************************************
863  * VLC_Destroy: Destroy everything.
864  *****************************************************************************
865  * This function requests the running threads to finish, waits for their
866  * termination, and destroys their structure.
867  *****************************************************************************/
868 int VLC_Destroy( int i_object )
869 {
870     vlc_t *p_vlc = vlc_current_object( i_object );
871
872     if( !p_vlc )
873     {
874         return VLC_ENOOBJ;
875     }
876
877     /*
878      * Free allocated memory
879      */
880     if( p_vlc->p_memcpy_module )
881     {
882         module_Unneed( p_vlc, p_vlc->p_memcpy_module );
883         p_vlc->p_memcpy_module = NULL;
884     }
885
886     /*
887      * Free module bank !
888      */
889     module_EndBank( p_vlc );
890
891     if( p_vlc->psz_homedir )
892     {
893         free( p_vlc->psz_homedir );
894         p_vlc->psz_homedir = NULL;
895     }
896
897     if( p_vlc->psz_configfile )
898     {
899         free( p_vlc->psz_configfile );
900         p_vlc->psz_configfile = NULL;
901     }
902
903     if( p_vlc->p_hotkeys )
904     {
905         free( p_vlc->p_hotkeys );
906         p_vlc->p_hotkeys = NULL;
907     }
908
909     /*
910      * System specific cleaning code
911      */
912     system_End( p_vlc );
913
914     /* Destroy mutexes */
915     vlc_mutex_destroy( &p_vlc->config_lock );
916
917     vlc_object_detach( p_vlc );
918
919     /* Release object before destroying it */
920     if( i_object ) vlc_object_release( p_vlc );
921
922     vlc_object_destroy( p_vlc );
923
924     /* Stop thread system: last one out please shut the door! */
925     vlc_threads_end( p_libvlc );
926
927     return VLC_SUCCESS;
928 }
929
930 /*****************************************************************************
931  * VLC_VariableSet: set a vlc variable
932  *****************************************************************************/
933 int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
934 {
935     vlc_t *p_vlc = vlc_current_object( i_object );
936     int i_ret;
937
938     if( !p_vlc )
939     {
940         return VLC_ENOOBJ;
941     }
942
943     /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
944      * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
945     if( !strncmp( psz_var, "conf::", 6 ) )
946     {
947         module_config_t *p_item;
948         char const *psz_newvar = psz_var + 6;
949
950         p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
951
952         if( p_item )
953         {
954             switch( p_item->i_type )
955             {
956                 case CONFIG_ITEM_BOOL:
957                     config_PutInt( p_vlc, psz_newvar, value.b_bool );
958                     break;
959                 case CONFIG_ITEM_INTEGER:
960                     config_PutInt( p_vlc, psz_newvar, value.i_int );
961                     break;
962                 case CONFIG_ITEM_FLOAT:
963                     config_PutFloat( p_vlc, psz_newvar, value.f_float );
964                     break;
965                 default:
966                     config_PutPsz( p_vlc, psz_newvar, value.psz_string );
967                     break;
968             }
969             if( i_object ) vlc_object_release( p_vlc );
970             return VLC_SUCCESS;
971         }
972     }
973
974     i_ret = var_Set( p_vlc, psz_var, value );
975
976     if( i_object ) vlc_object_release( p_vlc );
977     return i_ret;
978 }
979
980 /*****************************************************************************
981  * VLC_VariableGet: get a vlc variable
982  *****************************************************************************/
983 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
984 {
985     vlc_t *p_vlc = vlc_current_object( i_object );
986     int i_ret;
987
988     if( !p_vlc )
989     {
990         return VLC_ENOOBJ;
991     }
992
993     i_ret = var_Get( p_vlc, psz_var, p_value );
994
995     if( i_object ) vlc_object_release( p_vlc );
996     return i_ret;
997 }
998
999 /*****************************************************************************
1000  * VLC_AddTarget: adds a target for playing.
1001  *****************************************************************************
1002  * This function adds psz_target to the current playlist. If a playlist does
1003  * not exist, it will create one.
1004  *****************************************************************************/
1005 int VLC_AddTarget( int i_object, char const *psz_target,
1006                    char const **ppsz_options, int i_options,
1007                    int i_mode, int i_pos )
1008 {
1009     int i_err;
1010     playlist_t *p_playlist;
1011     vlc_t *p_vlc = vlc_current_object( i_object );
1012
1013     if( !p_vlc )
1014     {
1015         return VLC_ENOOBJ;
1016     }
1017
1018     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1019
1020     if( p_playlist == NULL )
1021     {
1022         msg_Dbg( p_vlc, "no playlist present, creating one" );
1023         p_playlist = playlist_Create( p_vlc );
1024
1025         if( p_playlist == NULL )
1026         {
1027             if( i_object ) vlc_object_release( p_vlc );
1028             return VLC_EGENERIC;
1029         }
1030
1031         vlc_object_yield( p_playlist );
1032     }
1033
1034     i_err = playlist_AddExt( p_playlist, psz_target, psz_target,
1035                              i_mode, i_pos, -1, ppsz_options, i_options);
1036
1037     vlc_object_release( p_playlist );
1038
1039     if( i_object ) vlc_object_release( p_vlc );
1040     return i_err;
1041 }
1042
1043 /*****************************************************************************
1044  * VLC_Play: play
1045  *****************************************************************************/
1046 int VLC_Play( int i_object )
1047 {
1048     playlist_t * p_playlist;
1049     vlc_t *p_vlc = vlc_current_object( i_object );
1050
1051     /* Check that the handle is valid */
1052     if( !p_vlc )
1053     {
1054         return VLC_ENOOBJ;
1055     }
1056
1057     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1058
1059     if( !p_playlist )
1060     {
1061         if( i_object ) vlc_object_release( p_vlc );
1062         return VLC_ENOOBJ;
1063     }
1064
1065     playlist_Play( p_playlist );
1066     vlc_object_release( p_playlist );
1067
1068     if( i_object ) vlc_object_release( p_vlc );
1069     return VLC_SUCCESS;
1070 }
1071
1072 /*****************************************************************************
1073  * VLC_Pause: toggle pause
1074  *****************************************************************************/
1075 int VLC_Pause( int i_object )
1076 {
1077     playlist_t * p_playlist;
1078     vlc_t *p_vlc = vlc_current_object( i_object );
1079
1080     /* Check that the handle is valid */
1081     if( !p_vlc )
1082     {
1083         return VLC_ENOOBJ;
1084     }
1085
1086     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1087
1088     if( !p_playlist )
1089     {
1090         if( i_object ) vlc_object_release( p_vlc );
1091         return VLC_ENOOBJ;
1092     }
1093
1094     playlist_Pause( p_playlist );
1095     vlc_object_release( p_playlist );
1096
1097     if( i_object ) vlc_object_release( p_vlc );
1098     return VLC_SUCCESS;
1099 }
1100
1101 /*****************************************************************************
1102  * VLC_Pause: toggle pause
1103  *****************************************************************************/
1104 int VLC_Stop( int i_object )
1105 {
1106     playlist_t * p_playlist;
1107     vlc_t *p_vlc = vlc_current_object( i_object );
1108
1109     /* Check that the handle is valid */
1110     if( !p_vlc )
1111     {
1112         return VLC_ENOOBJ;
1113     }
1114
1115     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1116
1117     if( !p_playlist )
1118     {
1119         if( i_object ) vlc_object_release( p_vlc );
1120         return VLC_ENOOBJ;
1121     }
1122
1123     playlist_Stop( p_playlist );
1124     vlc_object_release( p_playlist );
1125
1126     if( i_object ) vlc_object_release( p_vlc );
1127     return VLC_SUCCESS;
1128 }
1129
1130 /*****************************************************************************
1131  * VLC_IsPlaying: Query for Playlist Status
1132  *****************************************************************************/
1133 vlc_bool_t VLC_IsPlaying( int i_object )
1134 {
1135     playlist_t * p_playlist;
1136     vlc_bool_t   b_playing;
1137
1138     vlc_t *p_vlc = vlc_current_object( i_object );
1139
1140     /* Check that the handle is valid */
1141     if( !p_vlc )
1142     {
1143         return VLC_ENOOBJ;
1144     }
1145
1146     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1147
1148     if( !p_playlist )
1149     {
1150         if( i_object ) vlc_object_release( p_vlc );
1151         return VLC_ENOOBJ;
1152     }
1153
1154     b_playing = playlist_IsPlaying( p_playlist );
1155     vlc_object_release( p_playlist );
1156
1157     if( i_object ) vlc_object_release( p_vlc );
1158     return b_playing;
1159 }
1160
1161 /**
1162  * Get the current position in a input
1163  *
1164  * Return the current position as a float
1165  * \note For some inputs, this will be unknown.
1166  *
1167  * \param i_object a vlc object id
1168  * \return a float in the range of 0.0 - 1.0
1169  */
1170 float VLC_PositionGet( int i_object )
1171 {
1172     input_thread_t *p_input;
1173     vlc_value_t val;
1174     vlc_t *p_vlc = vlc_current_object( i_object );
1175
1176     /* Check that the handle is valid */
1177     if( !p_vlc )
1178     {
1179         return VLC_ENOOBJ;
1180     }
1181
1182     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1183
1184     if( !p_input )
1185     {
1186         if( i_object ) vlc_object_release( p_vlc );
1187         return VLC_ENOOBJ;
1188     }
1189
1190     var_Get( p_input, "position", &val );
1191     vlc_object_release( p_input );
1192
1193     if( i_object ) vlc_object_release( p_vlc );
1194     return val.f_float;
1195 }
1196
1197 /**
1198  * Set the current position in a input
1199  *
1200  * Set the current position in a input and then return
1201  * the current position as a float.
1202  * \note For some inputs, this will be unknown.
1203  *
1204  * \param i_object a vlc object id
1205  * \param i_position a float in the range of 0.0 - 1.0
1206  * \return a float in the range of 0.0 - 1.0
1207  */
1208 float VLC_PositionSet( int i_object, float i_position )
1209 {
1210     input_thread_t *p_input;
1211     vlc_value_t val;
1212     vlc_t *p_vlc = vlc_current_object( i_object );
1213
1214     /* Check that the handle is valid */
1215     if( !p_vlc )
1216     {
1217         return VLC_ENOOBJ;
1218     }
1219
1220     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1221
1222     if( !p_input )
1223     {
1224         if( i_object ) vlc_object_release( p_vlc );
1225         return VLC_ENOOBJ;
1226     }
1227
1228     val.f_float = i_position;
1229     var_Set( p_input, "position", val );
1230     var_Get( p_input, "position", &val );
1231     vlc_object_release( p_input );
1232
1233     if( i_object ) vlc_object_release( p_vlc );
1234     return val.f_float;
1235 }
1236
1237 /**
1238  * Get the current position in a input
1239  *
1240  * Return the current position in seconds from the start.
1241  * \note For some inputs, this will be unknown.
1242  *
1243  * \param i_object a vlc object id
1244  * \return the offset from 0:00 in seconds
1245  */
1246 int VLC_TimeGet( int i_object )
1247 {
1248     input_thread_t *p_input;
1249     vlc_value_t val;
1250     vlc_t *p_vlc = vlc_current_object( i_object );
1251
1252     /* Check that the handle is valid */
1253     if( !p_vlc )
1254     {
1255         return VLC_ENOOBJ;
1256     }
1257
1258     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1259
1260     if( !p_input )
1261     {
1262         if( i_object ) vlc_object_release( p_vlc );
1263         return VLC_ENOOBJ;
1264     }
1265
1266     var_Get( p_input, "time", &val );
1267     vlc_object_release( p_input );
1268
1269     if( i_object ) vlc_object_release( p_vlc );
1270     return val.i_time  / 1000000;
1271 }
1272
1273 /**
1274  * Seek to a position in the current input
1275  *
1276  * Seek i_seconds in the current input. If b_relative is set,
1277  * then the seek will be relative to the current position, otherwise
1278  * it will seek to i_seconds from the beginning of the input.
1279  * \note For some inputs, this will be unknown.
1280  *
1281  * \param i_object a vlc object id
1282  * \param i_seconds seconds from current position or from beginning of input
1283  * \param b_relative seek relative from current position
1284  * \return VLC_SUCCESS on success
1285  */
1286 int VLC_TimeSet( int i_object, int i_seconds, vlc_bool_t b_relative )
1287 {
1288     input_thread_t *p_input;
1289     vlc_value_t val;
1290     vlc_t *p_vlc = vlc_current_object( i_object );
1291
1292     /* Check that the handle is valid */
1293     if( !p_vlc )
1294     {
1295         return VLC_ENOOBJ;
1296     }
1297
1298     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1299
1300     if( !p_input )
1301     {
1302         if( i_object ) vlc_object_release( p_vlc );
1303         return VLC_ENOOBJ;
1304     }
1305
1306     if( b_relative )
1307     {
1308         val.i_time = i_seconds * 1000000;
1309         var_Set( p_input, "time-offset", val );
1310     }
1311     else
1312     {
1313         val.i_time = i_seconds * 1000000;
1314         var_Set( p_input, "time", val );
1315     }
1316     vlc_object_release( p_input );
1317
1318     if( i_object ) vlc_object_release( p_vlc );
1319     return VLC_SUCCESS;
1320 }
1321
1322 /**
1323  * Get the total length of a input
1324  *
1325  * Return the total length in seconds from the current input.
1326  * \note For some inputs, this will be unknown.
1327  *
1328  * \param i_object a vlc object id
1329  * \return the length in seconds
1330  */
1331 int VLC_LengthGet( int i_object )
1332 {
1333     input_thread_t *p_input;
1334     vlc_value_t val;
1335     vlc_t *p_vlc = vlc_current_object( i_object );
1336
1337     /* Check that the handle is valid */
1338     if( !p_vlc )
1339     {
1340         return VLC_ENOOBJ;
1341     }
1342
1343     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1344
1345     if( !p_input )
1346     {
1347         if( i_object ) vlc_object_release( p_vlc );
1348         return VLC_ENOOBJ;
1349     }
1350
1351     var_Get( p_input, "length", &val );
1352     vlc_object_release( p_input );
1353
1354     if( i_object ) vlc_object_release( p_vlc );
1355     return val.i_time  / 1000000;
1356 }
1357
1358 /**
1359  * Play the input faster than realtime
1360  *
1361  * 2x, 4x, 8x faster than realtime
1362  * \note For some inputs, this will be impossible.
1363  *
1364  * \param i_object a vlc object id
1365  * \return the current speedrate
1366  */
1367 float VLC_SpeedFaster( int i_object )
1368 {
1369     input_thread_t *p_input;
1370     vlc_value_t val;
1371     vlc_t *p_vlc = vlc_current_object( i_object );
1372
1373     /* Check that the handle is valid */
1374     if( !p_vlc )
1375     {
1376         return VLC_ENOOBJ;
1377     }
1378
1379     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1380
1381     if( !p_input )
1382     {
1383         if( i_object ) vlc_object_release( p_vlc );
1384         return VLC_ENOOBJ;
1385     }
1386
1387     val.b_bool = VLC_TRUE;
1388     var_Set( p_input, "rate-faster", val );
1389     var_Get( p_input, "rate", &val );
1390     vlc_object_release( p_input );
1391
1392     if( i_object ) vlc_object_release( p_vlc );
1393     return val.f_float / INPUT_RATE_DEFAULT;
1394 }
1395
1396 /**
1397  * Play the input slower than realtime
1398  *
1399  * 1/2x, 1/4x, 1/8x slower than realtime
1400  * \note For some inputs, this will be impossible.
1401  *
1402  * \param i_object a vlc object id
1403  * \return the current speedrate
1404  */
1405 float VLC_SpeedSlower( int i_object )
1406 {
1407     input_thread_t *p_input;
1408     vlc_value_t val;
1409     vlc_t *p_vlc = vlc_current_object( i_object );
1410
1411     /* Check that the handle is valid */
1412     if( !p_vlc )
1413     {
1414         return VLC_ENOOBJ;
1415     }
1416
1417     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
1418
1419     if( !p_input )
1420     {
1421         if( i_object ) vlc_object_release( p_vlc );
1422         return VLC_ENOOBJ;
1423     }
1424
1425     val.b_bool = VLC_TRUE;
1426     var_Set( p_input, "rate-slower", val );
1427     var_Get( p_input, "rate", &val );
1428     vlc_object_release( p_input );
1429
1430     if( i_object ) vlc_object_release( p_vlc );
1431     return val.f_float / INPUT_RATE_DEFAULT;
1432 }
1433
1434 /**
1435  * Return the current playlist item
1436  *
1437  * Returns the index of the playlistitem that is currently selected for play.
1438  * This is valid even if nothing is currently playing.
1439  *
1440  * \param i_object a vlc object id
1441  * \return the current index
1442  */
1443 int VLC_PlaylistIndex( int i_object )
1444 {
1445     int i_index;
1446     playlist_t * p_playlist;
1447     vlc_t *p_vlc = vlc_current_object( i_object );
1448
1449     /* Check that the handle is valid */
1450     if( !p_vlc )
1451     {
1452         return VLC_ENOOBJ;
1453     }
1454
1455     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1456
1457     if( !p_playlist )
1458     {
1459         if( i_object ) vlc_object_release( p_vlc );
1460         return VLC_ENOOBJ;
1461     }
1462
1463     i_index = p_playlist->i_index;
1464     vlc_object_release( p_playlist );
1465
1466     if( i_object ) vlc_object_release( p_vlc );
1467     return i_index;
1468 }
1469
1470 /**
1471  * Total amount of items in the playlist
1472  *
1473  * \param i_object a vlc object id
1474  * \return amount of playlist items
1475  */
1476 int VLC_PlaylistNumberOfItems( int i_object )
1477 {
1478     int i_size;
1479     playlist_t * p_playlist;
1480     vlc_t *p_vlc = vlc_current_object( i_object );
1481
1482     /* Check that the handle is valid */
1483     if( !p_vlc )
1484     {
1485         return VLC_ENOOBJ;
1486     }
1487
1488     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1489
1490     if( !p_playlist )
1491     {
1492         if( i_object ) vlc_object_release( p_vlc );
1493         return VLC_ENOOBJ;
1494     }
1495
1496     i_size = p_playlist->i_size;
1497     vlc_object_release( p_playlist );
1498
1499     if( i_object ) vlc_object_release( p_vlc );
1500     return i_size;
1501 }
1502
1503 /**
1504  * Next playlist item
1505  *
1506  * Skip to the next playlistitem and play it.
1507  *
1508  * \param i_object a vlc object id
1509  * \return VLC_SUCCESS on success
1510  */
1511 int VLC_PlaylistNext( int i_object )
1512 {
1513     playlist_t * p_playlist;
1514     vlc_t *p_vlc = vlc_current_object( i_object );
1515
1516     /* Check that the handle is valid */
1517     if( !p_vlc )
1518     {
1519         return VLC_ENOOBJ;
1520     }
1521
1522     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1523
1524     if( !p_playlist )
1525     {
1526         if( i_object ) vlc_object_release( p_vlc );
1527         return VLC_ENOOBJ;
1528     }
1529
1530     playlist_Next( p_playlist );
1531     vlc_object_release( p_playlist );
1532
1533     if( i_object ) vlc_object_release( p_vlc );
1534     return VLC_SUCCESS;
1535 }
1536
1537 /**
1538  * Previous playlist item
1539  *
1540  * Skip to the previous playlistitem and play it.
1541  *
1542  * \param i_object a vlc object id
1543  * \return VLC_SUCCESS on success
1544  */
1545 int VLC_PlaylistPrev( int i_object )
1546 {
1547     playlist_t * p_playlist;
1548     vlc_t *p_vlc = vlc_current_object( i_object );
1549
1550     /* Check that the handle is valid */
1551     if( !p_vlc )
1552     {
1553         return VLC_ENOOBJ;
1554     }
1555
1556     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1557
1558     if( !p_playlist )
1559     {
1560         if( i_object ) vlc_object_release( p_vlc );
1561         return VLC_ENOOBJ;
1562     }
1563
1564     playlist_Prev( p_playlist );
1565     vlc_object_release( p_playlist );
1566
1567     if( i_object ) vlc_object_release( p_vlc );
1568     return VLC_SUCCESS;
1569 }
1570
1571
1572 /*****************************************************************************
1573  * VLC_PlaylistClear: Empty the playlist
1574  *****************************************************************************/
1575 int VLC_PlaylistClear( int i_object )
1576 {
1577     int i_err;
1578     playlist_t * p_playlist;
1579     vlc_t *p_vlc = vlc_current_object( i_object );
1580
1581     /* Check that the handle is valid */
1582     if( !p_vlc )
1583     {
1584         return VLC_ENOOBJ;
1585     }
1586
1587     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
1588
1589     if( !p_playlist )
1590     {
1591         if( i_object ) vlc_object_release( p_vlc );
1592         return VLC_ENOOBJ;
1593     }
1594
1595     i_err = playlist_Clear( p_playlist );
1596
1597     vlc_object_release( p_playlist );
1598
1599     if( i_object ) vlc_object_release( p_vlc );
1600     return i_err;
1601 }
1602
1603 /**
1604  * Change the volume
1605  *
1606  * \param i_object a vlc object id
1607  * \param i_volume something in a range from 0-200
1608  * \return the new volume (range 0-200 %)
1609  */
1610 int VLC_VolumeSet( int i_object, int i_volume )
1611 {
1612     audio_volume_t i_vol = 0;
1613     vlc_t *p_vlc = vlc_current_object( i_object );
1614
1615     /* Check that the handle is valid */
1616     if( !p_vlc )
1617     {
1618         return VLC_ENOOBJ;
1619     }
1620
1621     if( i_volume >= 0 && i_volume <= 200 )
1622     {
1623         i_vol = i_volume * AOUT_VOLUME_MAX / 200;
1624         aout_VolumeSet( p_vlc, i_vol );
1625     }
1626
1627     if( i_object ) vlc_object_release( p_vlc );
1628     return i_vol * 200 / AOUT_VOLUME_MAX;
1629 }
1630
1631 /**
1632  * Get the current volume
1633  *
1634  * Retrieve the current volume.
1635  *
1636  * \param i_object a vlc object id
1637  * \return the current volume (range 0-200 %)
1638  */
1639 int VLC_VolumeGet( int i_object )
1640 {
1641     audio_volume_t i_volume;
1642     vlc_t *p_vlc = vlc_current_object( i_object );
1643
1644     /* Check that the handle is valid */
1645     if( !p_vlc )
1646     {
1647         return VLC_ENOOBJ;
1648     }
1649
1650     aout_VolumeGet( p_vlc, &i_volume );
1651
1652     if( i_object ) vlc_object_release( p_vlc );
1653     return i_volume*200/AOUT_VOLUME_MAX;
1654 }
1655
1656 /**
1657  * Mute/Unmute the volume
1658  *
1659  * \param i_object a vlc object id
1660  * \return VLC_SUCCESS on success
1661  */
1662 int VLC_VolumeMute( int i_object )
1663 {
1664     vlc_t *p_vlc = vlc_current_object( i_object );
1665
1666     /* Check that the handle is valid */
1667     if( !p_vlc )
1668     {
1669         return VLC_ENOOBJ;
1670     }
1671
1672     aout_VolumeMute( p_vlc, NULL );
1673
1674     if( i_object ) vlc_object_release( p_vlc );
1675     return VLC_SUCCESS;
1676 }
1677
1678 /*****************************************************************************
1679  * VLC_FullScreen: toggle fullscreen mode
1680  *****************************************************************************/
1681 int VLC_FullScreen( int i_object )
1682 {
1683     vout_thread_t *p_vout;
1684     vlc_t *p_vlc = vlc_current_object( i_object );
1685
1686     if( !p_vlc )
1687     {
1688         return VLC_ENOOBJ;
1689     }
1690
1691     p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
1692
1693     if( !p_vout )
1694     {
1695         if( i_object ) vlc_object_release( p_vlc );
1696         return VLC_ENOOBJ;
1697     }
1698
1699     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1700     vlc_object_release( p_vout );
1701
1702     if( i_object ) vlc_object_release( p_vlc );
1703     return VLC_SUCCESS;
1704 }
1705
1706 /* following functions are local */
1707
1708 /*****************************************************************************
1709  * SetLanguage: set the interface language.
1710  *****************************************************************************
1711  * We set the LC_MESSAGES locale category for interface messages and buttons,
1712  * as well as the LC_CTYPE category for string sorting and possible wide
1713  * character support.
1714  *****************************************************************************/
1715 static void SetLanguage ( char const *psz_lang )
1716 {
1717 #if defined( ENABLE_NLS ) \
1718      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
1719
1720     char *          psz_path;
1721 #if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
1722     char            psz_tmp[1024];
1723 #endif
1724
1725     if( psz_lang && !*psz_lang )
1726     {
1727 #   if defined( HAVE_LC_MESSAGES )
1728         setlocale( LC_MESSAGES, psz_lang );
1729 #   endif
1730         setlocale( LC_CTYPE, psz_lang );
1731     }
1732     else if( psz_lang )
1733     {
1734 #ifdef SYS_DARWIN
1735         /* I need that under Darwin, please check it doesn't disturb
1736          * other platforms. --Meuuh */
1737         setenv( "LANG", psz_lang, 1 );
1738
1739 #elif defined( SYS_BEOS ) || defined( WIN32 )
1740         /* We set LC_ALL manually because it is the only way to set
1741          * the language at runtime under eg. Windows. Beware that this
1742          * makes the environment unconsistent when libvlc is unloaded and
1743          * should probably be moved to a safer place like vlc.c. */
1744         static char psz_lcall[20];
1745         snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
1746         psz_lcall[19] = '\0';
1747         putenv( psz_lcall );
1748 #endif
1749
1750         setlocale( LC_ALL, psz_lang );
1751     }
1752
1753     /* Specify where to find the locales for current domain */
1754 #if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
1755     psz_path = LOCALEDIR;
1756 #else
1757     snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
1758               "locale" );
1759     psz_path = psz_tmp;
1760 #endif
1761     if( !bindtextdomain( PACKAGE_NAME, psz_path ) )
1762     {
1763         fprintf( stderr, "warning: no domain %s in directory %s\n",
1764                  PACKAGE_NAME, psz_path );
1765     }
1766
1767     /* Set the default domain */
1768     textdomain( PACKAGE_NAME );
1769
1770 #if defined( ENABLE_UTF8 )
1771     bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
1772 #endif
1773
1774 #endif
1775 }
1776
1777 /*****************************************************************************
1778  * GetFilenames: parse command line options which are not flags
1779  *****************************************************************************
1780  * Parse command line for input files as well as their associated options.
1781  * An option always follows its associated input and begins with a ":".
1782  *****************************************************************************/
1783 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
1784 {
1785     int i_opt, i_options;
1786
1787     /* We assume that the remaining parameters are filenames
1788      * and their input options */
1789     for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
1790     {
1791         i_options = 0;
1792
1793         /* Count the input options */
1794         while( *ppsz_argv[ i_opt ] == ':' && i_opt > optind )
1795         {
1796             i_options++;
1797             i_opt--;
1798         }
1799
1800         /* TODO: write an internal function of this one, to avoid
1801          *       unnecessary lookups. */
1802         VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1803                        (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
1804                                         NULL ), i_options,
1805                        PLAYLIST_INSERT, 0 );
1806     }
1807
1808     return VLC_SUCCESS;
1809 }
1810
1811 /*****************************************************************************
1812  * Usage: print program usage
1813  *****************************************************************************
1814  * Print a short inline help. Message interface is initialized at this stage.
1815  *****************************************************************************/
1816 static void Usage( vlc_t *p_this, char const *psz_module_name )
1817 {
1818 #define FORMAT_STRING "  %s --%s%s%s%s%s%s%s "
1819     /* short option ------'    |     | | | |  | |
1820      * option name ------------'     | | | |  | |
1821      * <bra -------------------------' | | |  | |
1822      * option type or "" --------------' | |  | |
1823      * ket> -----------------------------' |  | |
1824      * padding spaces ---------------------'  | |
1825      * comment -------------------------------' |
1826      * comment suffix --------------------------'
1827      *
1828      * The purpose of having bra and ket is that we might i18n them as well.
1829      */
1830 #define LINE_START 8
1831 #define PADDING_SPACES 25
1832     vlc_list_t *p_list;
1833     module_t *p_parser;
1834     module_config_t *p_item;
1835     char psz_spaces[PADDING_SPACES+LINE_START+1];
1836     char psz_format[sizeof(FORMAT_STRING)];
1837     char psz_buffer[1000];
1838     char psz_short[4];
1839     int i_index;
1840     int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
1841     vlc_bool_t b_advanced = config_GetInt( p_this, "advanced" );
1842
1843     memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1844     psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1845
1846     strcpy( psz_format, FORMAT_STRING );
1847
1848 #ifdef WIN32
1849     ShowConsole();
1850 #endif
1851
1852     /* List all modules */
1853     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1854
1855     /* Enumerate the config for each module */
1856     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1857     {
1858         vlc_bool_t b_help_module;
1859
1860         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1861
1862         if( psz_module_name && strcmp( psz_module_name,
1863                                        p_parser->psz_object_name ) )
1864         {
1865             continue;
1866         }
1867
1868         /* Ignore modules without config options */
1869         if( !p_parser->i_config_items )
1870         {
1871             continue;
1872         }
1873
1874         /* Ignore modules with only advanced config options if requested */
1875         if( !b_advanced )
1876         {
1877             for( p_item = p_parser->p_config;
1878                  p_item->i_type != CONFIG_HINT_END;
1879                  p_item++ )
1880             {
1881                 if( (p_item->i_type & CONFIG_ITEM) &&
1882                     !p_item->b_advanced ) break;
1883             }
1884             if( p_item->i_type == CONFIG_HINT_END ) continue;
1885         }
1886
1887         /* Print name of module */
1888         if( strcmp( "main", p_parser->psz_object_name ) )
1889         fprintf( stdout, "\n %s\n", p_parser->psz_longname );
1890
1891         b_help_module = !strcmp( "help", p_parser->psz_object_name );
1892
1893         /* Print module options */
1894         for( p_item = p_parser->p_config;
1895              p_item->i_type != CONFIG_HINT_END;
1896              p_item++ )
1897         {
1898             char *psz_text;
1899             char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1900             char *psz_suf = "", *psz_prefix = NULL;
1901             int i;
1902
1903             /* Skip advanced options if requested */
1904             if( p_item->b_advanced && !b_advanced )
1905             {
1906                 continue;
1907             }
1908
1909             switch( p_item->i_type )
1910             {
1911             case CONFIG_HINT_CATEGORY:
1912             case CONFIG_HINT_USAGE:
1913                 if( !strcmp( "main", p_parser->psz_object_name ) )
1914                 fprintf( stdout, "\n %s\n", p_item->psz_text );
1915                 break;
1916
1917             case CONFIG_ITEM_STRING:
1918             case CONFIG_ITEM_FILE:
1919             case CONFIG_ITEM_DIRECTORY:
1920             case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1921                 if( !p_item->ppsz_list )
1922                 {
1923                     psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1924                     break;
1925                 }
1926                 else
1927                 {
1928                     psz_bra = " {";
1929                     psz_type = psz_buffer;
1930                     psz_type[0] = '\0';
1931                     for( i = 0; p_item->ppsz_list[i]; i++ )
1932                     {
1933                         if( i ) strcat( psz_type, "," );
1934                         strcat( psz_type, p_item->ppsz_list[i] );
1935                     }
1936                     psz_ket = "}";
1937                     break;
1938                 }
1939             case CONFIG_ITEM_INTEGER:
1940             case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
1941                 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1942                 break;
1943             case CONFIG_ITEM_FLOAT:
1944                 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1945                 break;
1946             case CONFIG_ITEM_BOOL:
1947                 psz_bra = ""; psz_type = ""; psz_ket = "";
1948                 if( !b_help_module )
1949                 {
1950                     psz_suf = p_item->i_value ? _(" (default enabled)") :
1951                                                 _(" (default disabled)");
1952                 }
1953                 break;
1954             }
1955
1956             if( !psz_type )
1957             {
1958                 continue;
1959             }
1960
1961             /* Add short option if any */
1962             if( p_item->i_short )
1963             {
1964                 sprintf( psz_short, "-%c,", p_item->i_short );
1965             }
1966             else
1967             {
1968                 strcpy( psz_short, "   " );
1969             }
1970
1971             i = PADDING_SPACES - strlen( p_item->psz_name )
1972                  - strlen( psz_bra ) - strlen( psz_type )
1973                  - strlen( psz_ket ) - 1;
1974
1975             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1976             {
1977                 /* If option is of type --foo-bar, we print its counterpart
1978                  * as --no-foo-bar, but if it is of type --foobar (without
1979                  * dashes in the name) we print it as --nofoobar. Both
1980                  * values are of course valid, only the display changes. */
1981                 psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
1982                                                              : ", --no";
1983                 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
1984             }
1985
1986             if( i < 0 )
1987             {
1988                 psz_spaces[0] = '\n';
1989                 i = 0;
1990             }
1991             else
1992             {
1993                 psz_spaces[i] = '\0';
1994             }
1995
1996             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
1997             {
1998                 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
1999                          psz_prefix, p_item->psz_name, psz_bra, psz_type,
2000                          psz_ket, psz_spaces );
2001             }
2002             else
2003             {
2004                 fprintf( stdout, psz_format, psz_short, p_item->psz_name,
2005                          "", "", psz_bra, psz_type, psz_ket, psz_spaces );
2006             }
2007
2008             psz_spaces[i] = ' ';
2009
2010             /* We wrap the rest of the output */
2011             sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
2012             psz_text = psz_buffer;
2013             while( *psz_text )
2014             {
2015                 char *psz_parser, *psz_word;
2016                 int i_end = strlen( psz_text );
2017
2018                 /* If the remaining text fits in a line, print it. */
2019                 if( i_end <= i_width )
2020                 {
2021                     fprintf( stdout, "%s\n", psz_text );
2022                     break;
2023                 }
2024
2025                 /* Otherwise, eat as many words as possible */
2026                 psz_parser = psz_text;
2027                 do
2028                 {
2029                     psz_word = psz_parser;
2030                     psz_parser = strchr( psz_word, ' ' );
2031                     /* If no space was found, we reached the end of the text
2032                      * block; otherwise, we skip the space we just found. */
2033                     psz_parser = psz_parser ? psz_parser + 1
2034                                             : psz_text + i_end;
2035
2036                 } while( psz_parser - psz_text <= i_width );
2037
2038                 /* We cut a word in one of these cases:
2039                  *  - it's the only word in the line and it's too long.
2040                  *  - we used less than 80% of the width and the word we are
2041                  *    going to wrap is longer than 40% of the width, and even
2042                  *    if the word would have fit in the next line. */
2043                 if( psz_word == psz_text
2044                      || ( psz_word - psz_text < 80 * i_width / 100
2045                            && psz_parser - psz_word > 40 * i_width / 100 ) )
2046                 {
2047                     char c = psz_text[i_width];
2048                     psz_text[i_width] = '\0';
2049                     fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2050                     psz_text += i_width;
2051                     psz_text[0] = c;
2052                 }
2053                 else
2054                 {
2055                     psz_word[-1] = '\0';
2056                     fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
2057                     psz_text = psz_word;
2058                 }
2059             }
2060         }
2061     }
2062
2063     /* Release the module list */
2064     vlc_list_release( p_list );
2065
2066 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
2067     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
2068     getchar();
2069 #endif
2070 }
2071
2072 /*****************************************************************************
2073  * ListModules: list the available modules with their description
2074  *****************************************************************************
2075  * Print a list of all available modules (builtins and plugins) and a short
2076  * description for each one.
2077  *****************************************************************************/
2078 static void ListModules( vlc_t *p_this )
2079 {
2080     vlc_list_t *p_list;
2081     module_t *p_parser;
2082     char psz_spaces[22];
2083     int i_index;
2084
2085     memset( psz_spaces, ' ', 22 );
2086
2087 #ifdef WIN32
2088     ShowConsole();
2089 #endif
2090
2091     /* Usage */
2092     fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
2093                      p_this->p_vlc->psz_object_name );
2094
2095     fprintf( stdout, _("[module]              [description]\n") );
2096
2097     /* List all modules */
2098     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
2099
2100     /* Enumerate each module */
2101     for( i_index = 0; i_index < p_list->i_count; i_index++ )
2102     {
2103         int i;
2104
2105         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
2106
2107         /* Nasty hack, but right now I'm too tired to think about a nice
2108          * solution */
2109         i = 22 - strlen( p_parser->psz_object_name ) - 1;
2110         if( i < 0 ) i = 0;
2111         psz_spaces[i] = 0;
2112
2113         fprintf( stdout, "  %s%s %s\n", p_parser->psz_object_name,
2114                          psz_spaces, p_parser->psz_longname );
2115
2116         psz_spaces[i] = ' ';
2117     }
2118
2119     vlc_list_release( p_list );
2120
2121 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
2122     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
2123     getchar();
2124 #endif
2125 }
2126
2127 /*****************************************************************************
2128  * Version: print complete program version
2129  *****************************************************************************
2130  * Print complete program version and build number.
2131  *****************************************************************************/
2132 static void Version( void )
2133 {
2134 #ifdef WIN32
2135     ShowConsole();
2136 #endif
2137
2138     fprintf( stdout, VERSION_MESSAGE "\n" );
2139     fprintf( stdout,
2140       _("This program comes with NO WARRANTY, to the extent permitted by "
2141         "law.\nYou may redistribute it under the terms of the GNU General "
2142         "Public License;\nsee the file named COPYING for details.\n"
2143         "Written by the VideoLAN team; see the AUTHORS file.\n") );
2144
2145 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
2146     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
2147     getchar();
2148 #endif
2149 }
2150
2151 /*****************************************************************************
2152  * ShowConsole: On Win32, create an output console for debug messages
2153  *****************************************************************************
2154  * This function is useful only on Win32.
2155  *****************************************************************************/
2156 #ifdef WIN32 /*  */
2157 static void ShowConsole( void )
2158 {
2159 #   ifndef UNDER_CE
2160     AllocConsole();
2161     freopen( "CONOUT$", "w", stdout );
2162     freopen( "CONOUT$", "w", stderr );
2163     freopen( "CONIN$", "r", stdin );
2164 #   endif
2165     return;
2166 }
2167 #endif
2168
2169 /*****************************************************************************
2170  * ConsoleWidth: Return the console width in characters
2171  *****************************************************************************
2172  * We use the stty shell command to get the console width; if this fails or
2173  * if the width is less than 80, we default to 80.
2174  *****************************************************************************/
2175 static int ConsoleWidth( void )
2176 {
2177     int i_width = 80;
2178
2179 #ifndef WIN32
2180     char buf[20], *psz_parser;
2181     FILE *file;
2182     int i_ret;
2183
2184     file = popen( "stty size 2>/dev/null", "r" );
2185     if( file )
2186     {
2187         i_ret = fread( buf, 1, 20, file );
2188         if( i_ret > 0 )
2189         {
2190             buf[19] = '\0';
2191             psz_parser = strchr( buf, ' ' );
2192             if( psz_parser )
2193             {
2194                 i_ret = atoi( psz_parser + 1 );
2195                 if( i_ret >= 80 )
2196                 {
2197                     i_width = i_ret;
2198                 }
2199             }
2200         }
2201
2202         pclose( file );
2203     }
2204 #endif
2205
2206     return i_width;
2207 }
2208
2209 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
2210                      vlc_value_t old_val, vlc_value_t new_val, void *param)
2211 {
2212     vlc_t *p_vlc = (vlc_t *)p_this;
2213
2214     if( new_val.i_int >= -1 )
2215     {
2216         p_vlc->p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
2217     }
2218     return VLC_SUCCESS;
2219 }
2220
2221 /*****************************************************************************
2222  * InitDeviceValues: initialize device values
2223  *****************************************************************************
2224  * This function inits the dvd, vcd and cd-audio values
2225  *****************************************************************************/
2226 static void InitDeviceValues( vlc_t *p_vlc )
2227 {
2228 #ifdef HAVE_HAL
2229     LibHalContext * ctx;
2230     int i, i_devices;
2231     char **devices;
2232     char *block_dev;
2233     dbus_bool_t b_dvd;
2234
2235     if( ( ctx = hal_initialize( NULL, FALSE ) ) )
2236     {
2237         if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
2238         {
2239             for( i = 0; i < i_devices; i++ )
2240             {
2241                 if( !hal_device_property_exists( ctx, devices[ i ],
2242                                                 "storage.cdrom.dvd" ) )
2243                 {
2244                     continue;
2245                 }
2246
2247                 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
2248                                                       "storage.cdrom.dvd" );
2249                 block_dev = hal_device_get_property_string( ctx, devices[ i ],
2250                                                             "block.device" );
2251
2252                 if( b_dvd )
2253                 {
2254                     config_PutPsz( p_vlc, "dvd", block_dev );
2255                 }
2256
2257                 config_PutPsz( p_vlc, "vcd", block_dev );
2258                 config_PutPsz( p_vlc, "cd-audio", block_dev );
2259
2260                 hal_free_string( block_dev );
2261             }
2262         }
2263
2264         hal_shutdown( ctx );
2265     }
2266 #endif
2267 }