]> git.sesse.net Git - vlc/blob - src/libvlc.c
4fa06feb26340a458d529c16cb2c84ef9f6d980f
[vlc] / src / libvlc.c
1 /*****************************************************************************
2  * libvlc.c: main libvlc source
3  *****************************************************************************
4  * Copyright (C) 1998-2002 VideoLAN
5  * $Id: libvlc.c,v 1.53 2002/12/14 19:43:25 gbazin Exp $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Pretend we are a builtin module
28  *****************************************************************************/
29 #define MODULE_NAME main
30 #define MODULE_PATH main
31 #define __BUILTIN__
32
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36 #include <vlc/vlc.h>
37
38 #ifdef HAVE_ERRNO_H
39 #   include <errno.h>                                              /* ENOMEM */
40 #endif
41 #include <stdio.h>                                              /* sprintf() */
42 #include <string.h>                                            /* strerror() */
43 #include <stdlib.h>                                                /* free() */
44
45 #ifndef WIN32
46 #   include <netinet/in.h>                            /* BSD: struct in_addr */
47 #endif
48
49 #ifdef HAVE_UNISTD_H
50 #   include <unistd.h>
51 #elif defined( _MSC_VER ) && defined( _WIN32 ) && !defined( UNDER_CE )
52 #   include <io.h>
53 #endif
54
55 #ifdef WIN32                       /* optind, getopt(), included in unistd.h */
56 #   include "extras/getopt.h"
57 #endif
58
59 #ifdef HAVE_LOCALE_H
60 #   include <locale.h>
61 #endif
62
63 #include "vlc_cpu.h"                                        /* CPU detection */
64 #include "os_specific.h"
65
66 #include "error.h"
67 #include "netutils.h"                                 /* network_ChannelJoin */
68
69 #include "stream_control.h"
70 #include "input_ext-intf.h"
71
72 #include "vlc_playlist.h"
73 #include "interface.h"
74
75 #include "audio_output.h"
76
77 #include "video.h"
78 #include "video_output.h"
79
80 #include "libvlc.h"
81
82 /*****************************************************************************
83  * The evil global variable. We handle it with care, don't worry.
84  *****************************************************************************/
85 static libvlc_t libvlc;
86 static vlc_t *  p_static_vlc;
87
88 /*****************************************************************************
89  * Local prototypes
90  *****************************************************************************/
91 static void SetLanguage   ( char const * );
92 static int  GetFilenames  ( vlc_t *, int, char *[] );
93 static void Usage         ( vlc_t *, char const *psz_module_name );
94 static void ListModules   ( vlc_t * );
95 static void Version       ( void );
96
97 #ifdef WIN32
98 static void ShowConsole   ( void );
99 #endif
100
101 /*****************************************************************************
102  * VLC_Version: return the libvlc version.
103  *****************************************************************************
104  * This function returns full version string (numeric version and codename).
105  *****************************************************************************/
106 char const * VLC_Version( void )
107 {
108     return VERSION_MESSAGE;
109 }
110
111 /*****************************************************************************
112  * VLC_Error: strerror() equivalent
113  *****************************************************************************
114  * This function returns full version string (numeric version and codename).
115  *****************************************************************************/
116 char const * VLC_Error( int i_err )
117 {
118     return vlc_error( i_err );
119 }
120
121 /*****************************************************************************
122  * VLC_Create: allocate a vlc_t structure, and initialize libvlc if needed.
123  *****************************************************************************
124  * This function allocates a vlc_t structure and returns a negative value
125  * in case of failure. Also, the thread system is initialized.
126  *****************************************************************************/
127 int VLC_Create( void )
128 {
129     int i_ret;
130     vlc_t * p_vlc = NULL;
131     vlc_value_t lockval;
132
133     /* vlc_threads_init *must* be the first internal call! No other call is
134      * allowed before the thread system has been initialized. */
135     i_ret = vlc_threads_init( &libvlc );
136     if( i_ret < 0 )
137     {
138         return i_ret;
139     }
140
141     /* Now that the thread system is initialized, we don't have much, but
142      * at least we have var_Create */
143     var_Create( &libvlc, "libvlc", VLC_VAR_MUTEX );
144     var_Get( &libvlc, "libvlc", &lockval );
145     vlc_mutex_lock( lockval.p_address );
146     if( !libvlc.b_ready )
147     {
148         char *psz_env;
149
150         /* Guess what CPU we have */
151         libvlc.i_cpu = CPUCapabilities();
152
153         /* Find verbosity from VLC_VERBOSE environment variable */
154         psz_env = getenv( "VLC_VERBOSE" );
155         libvlc.i_verbose = psz_env ? atoi( psz_env ) : -1;
156
157 #ifdef HAVE_ISATTY
158         libvlc.b_color = isatty( 2 ); /* 2 is for stderr */
159 #else
160         libvlc.b_color = VLC_FALSE;
161 #endif
162
163         /* Initialize message queue */
164         msg_Create( &libvlc );
165
166         /* Announce who we are */
167         msg_Dbg( &libvlc, COPYRIGHT_MESSAGE );
168         msg_Dbg( &libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
169
170         /* Initialize the module bank and load the configuration of the
171          * main module. We need to do this at this stage to be able to display
172          * a short help if required by the user. (short help == main module
173          * options) */
174         module_InitBank( &libvlc );
175         module_LoadMain( &libvlc );
176
177         libvlc.b_ready = VLC_TRUE;
178     }
179     vlc_mutex_unlock( lockval.p_address );
180     var_Destroy( &libvlc, "libvlc" );
181
182     /* Allocate a vlc object */
183     p_vlc = vlc_object_create( &libvlc, VLC_OBJECT_VLC );
184     if( p_vlc == NULL )
185     {
186         return VLC_EGENERIC;
187     }
188
189     p_vlc->psz_object_name = "root";
190
191     /* Initialize mutexes */
192     vlc_mutex_init( p_vlc, &p_vlc->config_lock );
193
194     /* Store our newly allocated structure in the global list */
195     vlc_object_attach( p_vlc, &libvlc );
196
197     /* Store data for the non-reentrant API */
198     p_static_vlc = p_vlc;
199
200     return p_vlc->i_object_id;
201 }
202
203 /*****************************************************************************
204  * VLC_Init: initialize a vlc_t structure.
205  *****************************************************************************
206  * This function initializes a previously allocated vlc_t structure:
207  *  - CPU detection
208  *  - gettext initialization
209  *  - message queue, module bank and playlist initialization
210  *  - configuration and commandline parsing
211  *****************************************************************************/
212 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
213 {
214     char         p_capabilities[200];
215     char *       p_tmp;
216     vlc_bool_t   b_exit;
217     vlc_t *      p_vlc;
218     module_t    *p_help_module;
219     playlist_t  *p_playlist;
220
221     p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
222
223     if( !p_vlc )
224     {
225         return VLC_ENOOBJ;
226     }
227
228     /*
229      * Support for gettext
230      */
231     SetLanguage( "" );
232
233     /*
234      * System specific initialization code
235      */
236     system_Init( p_vlc, &i_argc, ppsz_argv );
237
238     /* Get the executable name (similar to the basename command) */
239     if( i_argc > 0 )
240     {
241         p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
242         while( *p_tmp )
243         {
244             if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
245             else ++p_tmp;
246         }
247     }
248     else
249     {
250         p_vlc->psz_object_name = "vlc";
251     }
252
253     /* Hack: insert the help module here */
254     p_help_module = vlc_object_create( p_vlc, VLC_OBJECT_MODULE );
255     if( p_help_module == NULL )
256     {
257         //module_EndBank( p_vlc );
258         if( i_object ) vlc_object_release( p_vlc );
259         return VLC_EGENERIC;
260     }
261     p_help_module->psz_object_name = "help";
262     config_Duplicate( p_help_module, p_help_config );
263     vlc_object_attach( p_help_module, libvlc.p_module_bank );
264     /* End hack */
265
266     if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_TRUE ) )
267     {
268         vlc_object_detach( p_help_module );
269         config_Free( p_help_module );
270         vlc_object_destroy( p_help_module );
271         //module_EndBank( p_vlc );
272         if( i_object ) vlc_object_release( p_vlc );
273         return VLC_EGENERIC;
274     }
275
276     b_exit = VLC_FALSE;
277
278     /* Check for short help option */
279     if( config_GetInt( p_vlc, "help" ) )
280     {
281         fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
282                          p_vlc->psz_object_name );
283         Usage( p_vlc, "main" );
284         Usage( p_vlc, "help" );
285         b_exit = VLC_TRUE;
286     }
287     /* Check for version option */
288     else if( config_GetInt( p_vlc, "version" ) )
289     {
290         Version();
291         b_exit = VLC_TRUE;
292     }
293
294     /* Hack: remove the help module here */
295     vlc_object_detach( p_help_module );
296     /* End hack */
297
298     if( b_exit )
299     {
300         config_Free( p_help_module );
301         vlc_object_destroy( p_help_module );
302         //module_EndBank( p_vlc );
303         if( i_object ) vlc_object_release( p_vlc );
304         return VLC_EEXIT;
305     }
306
307     /*
308      * Load the builtins and plugins into the module_bank.
309      * We have to do it before config_Load*() because this also gets the
310      * list of configuration options exported by each module and loads their
311      * default values.
312      */
313     module_LoadBuiltins( &libvlc );
314     module_LoadPlugins( &libvlc );
315     msg_Dbg( p_vlc, "module bank initialized, found %i modules",
316                     libvlc.p_module_bank->i_children );
317
318     /* Hack: insert the help module here */
319     vlc_object_attach( p_help_module, libvlc.p_module_bank );
320     /* End hack */
321
322     /* Check for help on modules */
323     if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
324     {
325         Usage( p_vlc, p_tmp );
326         free( p_tmp );
327         b_exit = VLC_TRUE;
328     }
329     /* Check for long help option */
330     else if( config_GetInt( p_vlc, "longhelp" ) )
331     {
332         Usage( p_vlc, NULL );
333         b_exit = VLC_TRUE;
334     }
335     /* Check for module list option */
336     else if( config_GetInt( p_vlc, "list" ) )
337     {
338         ListModules( p_vlc );
339         b_exit = VLC_TRUE;
340     }
341
342     /* Hack: remove the help module here */
343     vlc_object_detach( p_help_module );
344     config_Free( p_help_module );
345     vlc_object_destroy( p_help_module );
346     /* End hack */
347
348     if( b_exit )
349     {
350         //module_EndBank( p_vlc );
351         if( i_object ) vlc_object_release( p_vlc );
352         return VLC_EEXIT;
353     }
354
355     /*
356      * Override default configuration with config file settings
357      */
358     p_vlc->psz_homedir = config_GetHomeDir();
359     config_LoadConfigFile( p_vlc, NULL );
360
361     /*
362      * Override configuration with command line settings
363      */
364     if( config_LoadCmdLine( p_vlc, &i_argc, ppsz_argv, VLC_FALSE ) )
365     {
366 #ifdef WIN32
367         ShowConsole();
368         /* Pause the console because it's destroyed when we exit */
369         fprintf( stderr, "The command line options couldn't be loaded, check "
370                  "that they are valid.\nPress the RETURN key to continue..." );
371         getchar();
372 #endif
373         //module_EndBank( p_vlc );
374         if( i_object ) vlc_object_release( p_vlc );
375         return VLC_EGENERIC;
376     }
377
378     /*
379      * System specific configuration
380      */
381     system_Configure( p_vlc );
382
383     /*
384      * Message queue options
385      */
386     if( config_GetInt( p_vlc, "quiet" ) )
387     {
388         libvlc.i_verbose = -1;
389     }
390     else
391     {
392         int i_tmp = config_GetInt( p_vlc, "verbose" );
393         if( i_tmp >= 0 )
394         {
395             libvlc.i_verbose = __MIN( i_tmp, 2 );
396         }
397     }
398     libvlc.b_color = libvlc.b_color || config_GetInt( p_vlc, "color" );
399
400     /*
401      * Output messages that may still be in the queue
402      */
403     msg_Flush( p_vlc );
404
405     /* p_vlc inititalization. FIXME ? */
406     p_vlc->i_desync = config_GetInt( p_vlc, "desync" ) * (mtime_t)1000;
407
408 #if defined( __i386__ )
409     if( !config_GetInt( p_vlc, "mmx" ) )
410         libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
411     if( !config_GetInt( p_vlc, "3dn" ) )
412         libvlc.i_cpu &= ~CPU_CAPABILITY_3DNOW;
413     if( !config_GetInt( p_vlc, "mmxext" ) )
414         libvlc.i_cpu &= ~CPU_CAPABILITY_MMXEXT;
415     if( !config_GetInt( p_vlc, "sse" ) )
416         libvlc.i_cpu &= ~CPU_CAPABILITY_SSE;
417 #endif
418 #if defined( __powerpc__ ) || defined( SYS_DARWIN )
419     if( !config_GetInt( p_vlc, "altivec" ) )
420         libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
421 #endif
422
423 #define PRINT_CAPABILITY( capability, string )                              \
424     if( libvlc.i_cpu & capability )                                         \
425     {                                                                       \
426         strncat( p_capabilities, string " ",                                \
427                  sizeof(p_capabilities) - strlen(p_capabilities) );         \
428         p_capabilities[sizeof(p_capabilities) - 1] = '\0';                  \
429     }
430
431     p_capabilities[0] = '\0';
432     PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
433     PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
434     PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
435     PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
436     PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
437     PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
438     PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
439     PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "AltiVec" );
440     PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
441     msg_Dbg( p_vlc, "CPU has capabilities %s", p_capabilities );
442
443     /*
444      * Choose the best memcpy module
445      */
446     p_vlc->p_memcpy_module = module_Need( p_vlc, "memcpy", "$memcpy" );
447
448     if( p_vlc->pf_memcpy == NULL )
449     {
450         p_vlc->pf_memcpy = memcpy;
451     }
452
453     if( p_vlc->pf_memset == NULL )
454     {
455         p_vlc->pf_memset = memset;
456     }
457
458     /*
459      * Initialize shared resources and libraries
460      */
461     if( config_GetInt( p_vlc, "network-channel" )
462          && network_ChannelCreate( p_vlc ) )
463     {
464         /* On error during Channels initialization, switch off channels */
465         msg_Warn( p_vlc,
466                   "channels initialization failed, deactivating channels" );
467         config_PutInt( p_vlc, "network-channel", VLC_FALSE );
468     }
469
470     /*
471      * Initialize playlist and get commandline files
472      */
473     p_playlist = playlist_Create( p_vlc );
474     if( !p_playlist )
475     {
476         msg_Err( p_vlc, "playlist initialization failed" );
477         if( p_vlc->p_memcpy_module != NULL )
478         {
479             module_Unneed( p_vlc, p_vlc->p_memcpy_module );
480         }
481         //module_EndBank( p_vlc );
482         if( i_object ) vlc_object_release( p_vlc );
483         return VLC_EGENERIC;
484     }
485
486     /*
487      * Get input filenames given as commandline arguments
488      */
489     GetFilenames( p_vlc, i_argc, ppsz_argv );
490
491     if( i_object ) vlc_object_release( p_vlc );
492     return VLC_SUCCESS;
493 }
494
495 /*****************************************************************************
496  * VLC_AddIntf: add an interface
497  *****************************************************************************
498  * This function opens an interface plugin and runs it. If b_block is set
499  * to 0, VLC_AddIntf will return immediately and let the interface run in a
500  * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
501  * user requests to quit.
502  *****************************************************************************/
503 int VLC_AddIntf( int i_object, char const *psz_module, vlc_bool_t b_block )
504 {
505     int i_err;
506     intf_thread_t *p_intf;
507     vlc_t *p_vlc;
508     char *psz_oldmodule = NULL;
509
510     p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
511
512     if( !p_vlc )
513     {
514         return VLC_ENOOBJ;
515     }
516
517     if( psz_module )
518     {
519         psz_oldmodule = config_GetPsz( p_vlc, "intf" );
520         config_PutPsz( p_vlc, "intf", psz_module );
521     }
522
523     /* Try to create the interface */
524     p_intf = intf_Create( p_vlc );
525
526     if( psz_module )
527     {
528         config_PutPsz( p_vlc, "intf", psz_oldmodule );
529         if( psz_oldmodule )
530         {
531             free( psz_oldmodule );
532         }
533     }
534
535     if( p_intf == NULL )
536     {
537         msg_Err( p_vlc, "interface initialization failed" );
538         if( i_object ) vlc_object_release( p_vlc );
539         return VLC_EGENERIC;
540     }
541
542     /* Try to run the interface */
543     p_intf->b_block = b_block;
544     i_err = intf_RunThread( p_intf );
545     if( i_err )
546     {
547         vlc_object_detach( p_intf );
548         intf_Destroy( p_intf );
549         if( i_object ) vlc_object_release( p_vlc );
550         return i_err;
551     }
552
553     if( i_object ) vlc_object_release( p_vlc );
554     return VLC_SUCCESS;
555 }
556
557 /*****************************************************************************
558  * VLC_Destroy: stop playing and destroy everything.
559  *****************************************************************************
560  * This function requests the running threads to finish, waits for their
561  * termination, and destroys their structure.
562  *****************************************************************************/
563 int VLC_Destroy( int i_object )
564 {
565     vlc_t *p_vlc;
566
567     p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
568
569     if( !p_vlc )
570     {
571         return VLC_ENOOBJ;
572     }
573
574     /*
575      * Go back into channel 0 which is the network
576      */
577     if( config_GetInt( p_vlc, "network-channel" ) && p_vlc->p_channel )
578     {
579         network_ChannelJoin( p_vlc, COMMON_CHANNEL );
580     }
581     
582     /*
583      * Free allocated memory
584      */
585     if( p_vlc->p_memcpy_module )
586     {
587         module_Unneed( p_vlc, p_vlc->p_memcpy_module );
588         p_vlc->p_memcpy_module = NULL;
589     }
590
591     if( p_vlc->psz_homedir )
592     {
593         free( p_vlc->psz_homedir );
594         p_vlc->psz_homedir = NULL;
595     }
596
597     /*
598      * XXX: Free module bank !
599      */
600     //module_EndBank( p_vlc );
601     
602     /*
603      * System specific cleaning code
604      */
605     system_End( p_vlc );
606     
607     /* Destroy mutexes */
608     vlc_mutex_destroy( &p_vlc->config_lock );
609
610     vlc_object_detach( p_vlc );
611
612     vlc_object_destroy( p_vlc );
613
614     /* Stop thread system: last one out please shut the door! */
615     vlc_threads_end( &libvlc );
616
617     if( i_object ) vlc_object_release( p_vlc );
618     return VLC_SUCCESS;
619 }
620
621 /*****************************************************************************
622  * VLC_Die: ask vlc to die.
623  *****************************************************************************
624  * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
625  * task. It is your duty to call vlc_end and VLC_Destroy afterwards.
626  *****************************************************************************/
627 int VLC_Die( int i_object )
628 {
629     vlc_t *p_vlc;
630
631     p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
632
633     if( !p_vlc )
634     {
635         return VLC_ENOOBJ;
636     }
637
638     p_vlc->b_die = VLC_TRUE;
639
640     if( i_object ) vlc_object_release( p_vlc );
641     return VLC_SUCCESS;
642 }
643
644 /*****************************************************************************
645  * VLC_AddTarget: adds a target for playing.
646  *****************************************************************************
647  * This function adds psz_target to the current playlist. If a playlist does
648  * not exist, it will create one.
649  *****************************************************************************/
650 int VLC_AddTarget( int i_object, char const *psz_target, int i_mode, int i_pos )
651 {
652     int i_err;
653     playlist_t *p_playlist;
654     vlc_t *p_vlc;
655
656     p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
657
658     if( !p_vlc )
659     {
660         return VLC_ENOOBJ;
661     }
662
663     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
664
665     if( p_playlist == NULL )
666     {
667         msg_Dbg( p_vlc, "no playlist present, creating one" );
668         p_playlist = playlist_Create( p_vlc );
669
670         if( p_playlist == NULL )
671         {
672             if( i_object ) vlc_object_release( p_vlc );
673             return VLC_EGENERIC;
674         }
675
676         vlc_object_yield( p_playlist );
677     }
678
679     i_err = playlist_Add( p_playlist, psz_target, i_mode, i_pos );
680
681     vlc_object_release( p_playlist );
682
683     if( i_object ) vlc_object_release( p_vlc );
684     return i_err;
685 }
686
687 /*****************************************************************************
688  * VLC_Set: set a vlc variable
689  *****************************************************************************
690  *
691  *****************************************************************************/
692 int VLC_Set( int i_object, char const *psz_var, vlc_value_t value )
693 {
694     vlc_t *p_vlc;
695     int i_ret;
696
697     p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
698
699     if( !p_vlc )
700     {
701         return VLC_ENOOBJ;
702     }
703
704     /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
705      * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
706     if( !strncmp( psz_var, "conf::", 6 ) )
707     {
708         module_config_t *p_item;
709         char const *psz_newvar = psz_var + 6;
710
711         p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
712
713         if( p_item )
714         {
715             switch( p_item->i_type )
716             {
717                 case CONFIG_ITEM_BOOL:
718                     config_PutInt( p_vlc, psz_newvar, value.b_bool );
719                     break;
720                 case CONFIG_ITEM_INTEGER:
721                     config_PutInt( p_vlc, psz_newvar, value.i_int );
722                     break;
723                 case CONFIG_ITEM_FLOAT:
724                     config_PutFloat( p_vlc, psz_newvar, value.f_float );
725                     break;
726                 default:
727                     config_PutPsz( p_vlc, psz_newvar, value.psz_string );
728                     break;
729             }
730             if( i_object ) vlc_object_release( p_vlc );
731             return VLC_SUCCESS;
732         }
733     }
734
735     i_ret = var_Set( p_vlc, psz_var, value );
736
737     if( i_object ) vlc_object_release( p_vlc );
738     return i_ret;
739 }
740
741 /*****************************************************************************
742  * VLC_Get: get a vlc variable
743  *****************************************************************************
744  *
745  *****************************************************************************/
746 int VLC_Get( int i_object, char const *psz_var, vlc_value_t *p_value )
747 {
748     vlc_t *p_vlc;
749     int i_ret;
750
751     p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
752
753     if( !p_vlc )
754     {
755         return VLC_ENOOBJ;
756     }
757
758     i_ret = var_Get( p_vlc, psz_var, p_value );
759
760     if( i_object ) vlc_object_release( p_vlc );
761     return i_ret;
762 }
763
764 /* FIXME: temporary hacks */
765
766 /*****************************************************************************
767  * VLC_Play: play
768  *****************************************************************************/
769 int VLC_Play( int i_object )
770 {
771     playlist_t * p_playlist;
772     vlc_t *p_vlc;
773
774     p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
775
776     /* Check that the handle is valid */
777     if( !p_vlc )
778     {
779         return VLC_ENOOBJ;
780     }
781     
782     /* add pseudo sap interface; non blocking */
783     if( config_GetInt( p_vlc, "sap" ) )
784     {
785         msg_Dbg( p_vlc, "adding sap interface" );
786         VLC_AddIntf( 0, "sap", VLC_FALSE );
787     }
788
789     vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
790
791     p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_CHILD );
792
793     if( !p_playlist )
794     {
795         if( i_object ) vlc_object_release( p_vlc );
796         return VLC_ENOOBJ;
797     }
798
799     vlc_mutex_lock( &p_playlist->object_lock );
800     if( p_playlist->i_size )
801     {
802         vlc_mutex_unlock( &p_playlist->object_lock );
803         playlist_Play( p_playlist );
804     }
805     else
806     {
807         vlc_mutex_unlock( &p_playlist->object_lock );
808     }
809
810     vlc_object_release( p_playlist );
811
812     if( i_object ) vlc_object_release( p_vlc );
813     return VLC_SUCCESS;
814 }
815
816 /*****************************************************************************
817  * VLC_Stop: stop
818  *****************************************************************************/
819 int VLC_Stop( int i_object )
820 {
821     intf_thread_t *   p_intf;
822     playlist_t    *   p_playlist;
823     vout_thread_t *   p_vout;
824     aout_instance_t * p_aout;
825     vlc_t *p_vlc;
826
827     p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
828
829     /* Check that the handle is valid */
830     if( !p_vlc )
831     {
832         return VLC_ENOOBJ;
833     }
834
835     /*
836      * Ask the interfaces to stop and destroy them
837      */
838     msg_Dbg( p_vlc, "removing all interfaces" );
839     while( (p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_CHILD )) )
840     {
841         intf_StopThread( p_intf );
842         vlc_object_detach( p_intf );
843         vlc_object_release( p_intf );
844         intf_Destroy( p_intf );
845     }
846
847     /*
848      * Free playlists
849      */
850     msg_Dbg( p_vlc, "removing all playlists" );
851     while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
852                                           FIND_CHILD )) )
853     {
854         vlc_object_detach( p_playlist );
855         vlc_object_release( p_playlist );
856         playlist_Destroy( p_playlist );
857     }
858
859     /*
860      * Free video outputs
861      */
862     msg_Dbg( p_vlc, "removing all video outputs" );
863     while( (p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD )) )
864     {
865         vlc_object_detach( p_vout );
866         vlc_object_release( p_vout );
867         vout_Destroy( p_vout );
868     }
869
870     /*
871      * Free audio outputs
872      */
873     msg_Dbg( p_vlc, "removing all audio outputs" );
874     while( (p_aout = vlc_object_find( p_vlc, VLC_OBJECT_AOUT, FIND_CHILD )) )
875     {
876         vlc_object_detach( (vlc_object_t *)p_aout );
877         vlc_object_release( (vlc_object_t *)p_aout );
878         aout_Delete( p_aout );
879     }
880
881     if( i_object ) vlc_object_release( p_vlc );
882     return VLC_SUCCESS;
883 }
884
885 /*****************************************************************************
886  * VLC_Pause: toggle pause
887  *****************************************************************************/
888 int VLC_Pause( int i_object )
889 {
890     input_thread_t *p_input;
891     vlc_t *p_vlc;
892
893     p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
894
895     if( !p_vlc )
896     {
897         return VLC_ENOOBJ;
898     }
899
900     p_input = vlc_object_find( p_vlc, VLC_OBJECT_INPUT, FIND_CHILD );
901
902     if( !p_input )
903     {
904         if( i_object ) vlc_object_release( p_vlc );
905         return VLC_ENOOBJ;
906     }
907
908     input_SetStatus( p_input, INPUT_STATUS_PAUSE );
909     vlc_object_release( p_input );
910
911     if( i_object ) vlc_object_release( p_vlc );
912     return VLC_SUCCESS;
913 }
914
915 /*****************************************************************************
916  * VLC_FullScreen: toggle fullscreen mode
917  *****************************************************************************/
918 int VLC_FullScreen( int i_object )
919 {
920     vout_thread_t *p_vout;
921     vlc_t *p_vlc;
922
923     p_vlc = i_object ? vlc_object_get( &libvlc, i_object ) : p_static_vlc;
924
925     if( !p_vlc )
926     {
927         return VLC_ENOOBJ;
928     }
929
930     p_vout = vlc_object_find( p_vlc, VLC_OBJECT_VOUT, FIND_CHILD );
931
932     if( !p_vout )
933     {
934         if( i_object ) vlc_object_release( p_vlc );
935         return VLC_ENOOBJ;
936     }
937
938     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
939     vlc_object_release( p_vout );
940
941     if( i_object ) vlc_object_release( p_vlc );
942     return VLC_SUCCESS;
943 }
944
945 /* following functions are local */
946
947 /*****************************************************************************
948  * SetLanguage: set the interface language.
949  *****************************************************************************
950  * We set the LC_MESSAGES locale category for interface messages and buttons,
951  * as well as the LC_CTYPE category for string sorting and possible wide
952  * character support.
953  *****************************************************************************/
954 static void SetLanguage ( char const *psz_lang )
955 {
956 #if defined( ENABLE_NLS ) \
957      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
958
959 #   if defined( HAVE_INCLUDED_GETTEXT ) && !defined( HAVE_LC_MESSAGES )
960     if( *psz_lang )
961     {
962         /* We set LC_ALL manually because it is the only way to set
963          * the language at runtime under eg. Windows. Beware that this
964          * makes the environment unconsistent when libvlc is unloaded and
965          * should probably be moved to a safer place like vlc.c. */
966         static char psz_lcall[20];
967         snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
968         psz_lcall[19] = '\0';
969         putenv( psz_lcall );
970     }
971 #   endif
972
973 #   if defined( HAVE_LC_MESSAGES )
974     setlocale( LC_MESSAGES, psz_lang );
975 #   endif
976     setlocale( LC_CTYPE, psz_lang );
977
978     /* Specify where to find the locales for current domain */
979     if( !bindtextdomain( PACKAGE, LOCALEDIR ) )
980     {
981         fprintf( stderr, "warning: no domain %s in directory %s\n",
982                  PACKAGE, LOCALEDIR );
983     }
984
985     /* Set the default domain */
986     textdomain( PACKAGE );
987 #endif
988 }
989
990 /*****************************************************************************
991  * GetFilenames: parse command line options which are not flags
992  *****************************************************************************
993  * Parse command line for input files.
994  *****************************************************************************/
995 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
996 {
997     int i_opt;
998
999     /* We assume that the remaining parameters are filenames */
1000     for( i_opt = i_argc - 1; i_opt > optind; i_opt-- )
1001     {
1002         /* TODO: write an internal function of this one, to avoid
1003          *       unnecessary lookups. */
1004         VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1005                        PLAYLIST_INSERT, 0 );
1006     }
1007
1008     /* If there is at least one target, play it */
1009     if( i_argc > optind )
1010     {
1011         VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ optind ],
1012                        PLAYLIST_INSERT | PLAYLIST_GO, 0 );
1013     }
1014
1015     return VLC_SUCCESS;
1016 }
1017
1018 /*****************************************************************************
1019  * Usage: print program usage
1020  *****************************************************************************
1021  * Print a short inline help. Message interface is initialized at this stage.
1022  *****************************************************************************/
1023 static void Usage( vlc_t *p_this, char const *psz_module_name )
1024 {
1025 #define FORMAT_STRING "      --%s%s%s%s%s%s%s %s%s\n"
1026     /* option name -------------'     | | | |  | |
1027      * <bra --------------------------' | | |  | |
1028      * option type or "" ---------------' | |  | |
1029      * ket> ------------------------------' |  | |
1030      * padding spaces ----------------------'  | |
1031      * comment --------------------------------' |
1032      * comment suffix ---------------------------'
1033      *
1034      * The purpose of having bra and ket is that we might i18n them as well.
1035      */
1036 #define LINE_START 8
1037 #define PADDING_SPACES 25
1038     vlc_list_t list;
1039     module_t *p_parser;
1040     module_config_t *p_item;
1041     char psz_spaces[PADDING_SPACES+LINE_START+1];
1042     char psz_format[sizeof(FORMAT_STRING)];
1043     int i_index;
1044
1045     memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1046     psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1047
1048     strcpy( psz_format, FORMAT_STRING );
1049
1050 #ifdef WIN32
1051     ShowConsole();
1052 #endif
1053
1054     /* List all modules */
1055     list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1056
1057     /* Enumerate the config for each module */
1058     for( i_index = 0; i_index < list.i_count; i_index++ )
1059     {
1060         vlc_bool_t b_help_module;
1061
1062         p_parser = (module_t *)list.p_values[i_index].p_object ;
1063
1064         if( psz_module_name && strcmp( psz_module_name,
1065                                        p_parser->psz_object_name ) )
1066         {
1067             continue;
1068         }
1069
1070         /* Ignore modules without config options */
1071         if( !p_parser->i_config_items )
1072         {
1073             continue;
1074         }
1075
1076         b_help_module = !strcmp( "help", p_parser->psz_object_name );
1077
1078         /* Print module options */
1079         for( p_item = p_parser->p_config;
1080              p_item->i_type != CONFIG_HINT_END;
1081              p_item++ )
1082         {
1083             char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1084             char *psz_suf = "", *psz_prefix = NULL;
1085             int i;
1086
1087             switch( p_item->i_type )
1088             {
1089             case CONFIG_HINT_CATEGORY:
1090             case CONFIG_HINT_USAGE:
1091                 fprintf( stdout, " %s\n", p_item->psz_text );
1092                 break;
1093
1094             case CONFIG_ITEM_STRING:
1095             case CONFIG_ITEM_FILE:
1096             case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1097                 if( !p_item->ppsz_list )
1098                 {
1099                     psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1100                     break;
1101                 }
1102                 else
1103                 {
1104                     psz_bra = " [";
1105                     psz_type = malloc( 1000 );
1106                     memset( psz_type, 0, 1000 );
1107                     for( i=0; p_item->ppsz_list[i]; i++ )
1108                     {
1109                         strcat( psz_type, p_item->ppsz_list[i] );
1110                         strcat( psz_type, "|" );
1111                     }
1112                     psz_type[ strlen( psz_type ) - 1 ] = '\0';
1113                     psz_ket = "]";
1114                     break;
1115                 }
1116             case CONFIG_ITEM_INTEGER:
1117                 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1118                 break;
1119             case CONFIG_ITEM_FLOAT:
1120                 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1121                 break;
1122             case CONFIG_ITEM_BOOL:
1123                 psz_bra = ""; psz_type = ""; psz_ket = "";
1124                 if( !b_help_module )
1125                 {
1126                     psz_suf = p_item->i_value ? _(" (default enabled)") :
1127                                                 _(" (default disabled)");
1128                 }
1129                 break;
1130             }
1131
1132             /* Add short option if any */
1133             if( p_item->i_short )
1134             {
1135                 psz_format[2] = '-';
1136                 psz_format[3] = p_item->i_short;
1137                 psz_format[4] = ',';
1138             }
1139             else
1140             {
1141                 psz_format[2] = ' ';
1142                 psz_format[3] = ' ';
1143                 psz_format[4] = ' ';
1144             }
1145
1146             if( psz_type )
1147             {
1148                 i = PADDING_SPACES - strlen( p_item->psz_name )
1149                      - strlen( psz_bra ) - strlen( psz_type )
1150                      - strlen( psz_ket ) - 1;
1151                 if( p_item->i_type == CONFIG_ITEM_BOOL
1152                      && !b_help_module )
1153                 {
1154                     /* If option is of type --foo-bar, we print its counterpart
1155                      * as --no-foo-bar, but if it is of type --foobar (without
1156                      * dashes in the name) we print it as --nofoobar. Both
1157                      * values are of course valid, only the display changes. */
1158                     vlc_bool_t b_dash = VLC_FALSE;
1159                     psz_prefix = p_item->psz_name;
1160                     while( *psz_prefix )
1161                     {
1162                         if( *psz_prefix++ == '-' )
1163                         {
1164                             b_dash = VLC_TRUE;
1165                             break;
1166                         }
1167                     }
1168
1169                     if( b_dash )
1170                     {
1171                         psz_prefix = ", --no-";
1172                         i -= strlen( p_item->psz_name ) + strlen( ", --no-" );
1173                     }
1174                     else
1175                     {
1176                         psz_prefix = ", --no";
1177                         i -= strlen( p_item->psz_name ) + strlen( ", --no" );
1178                     }
1179                 }
1180
1181                 if( i < 0 )
1182                 {
1183                     i = 0;
1184                     psz_spaces[i] = '\n';
1185                 }
1186                 else
1187                 {
1188                     psz_spaces[i] = '\0';
1189                 }
1190
1191                 if( p_item->i_type == CONFIG_ITEM_BOOL &&
1192                     !b_help_module )
1193                 {
1194                     fprintf( stdout, psz_format, p_item->psz_name, psz_prefix,
1195                              p_item->psz_name, psz_bra, psz_type, psz_ket,
1196                              psz_spaces, p_item->psz_text, psz_suf );
1197                 }
1198                 else
1199                 {
1200                     fprintf( stdout, psz_format, p_item->psz_name, "", "",
1201                              psz_bra, psz_type, psz_ket, psz_spaces,
1202                              p_item->psz_text, psz_suf );
1203                 }
1204                 psz_spaces[i] = ' ';
1205                 if ( p_item->ppsz_list )
1206                 {
1207                     free( psz_type );
1208                 }
1209             }
1210         }
1211     }
1212
1213     /* Release the module list */
1214     vlc_list_release( &list );
1215
1216 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1217         fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1218         getchar();
1219 #endif
1220 }
1221
1222 /*****************************************************************************
1223  * ListModules: list the available modules with their description
1224  *****************************************************************************
1225  * Print a list of all available modules (builtins and plugins) and a short
1226  * description for each one.
1227  *****************************************************************************/
1228 static void ListModules( vlc_t *p_this )
1229 {
1230     vlc_list_t list;
1231     module_t *p_parser;
1232     char psz_spaces[22];
1233     int i_index;
1234
1235     memset( psz_spaces, ' ', 22 );
1236
1237 #ifdef WIN32
1238     ShowConsole();
1239 #endif
1240
1241     /* Usage */
1242     fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1243                      p_this->p_vlc->psz_object_name );
1244
1245     fprintf( stdout, _("[module]              [description]\n") );
1246
1247     /* List all modules */
1248     list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1249
1250     /* Enumerate each module */
1251     for( i_index = 0; i_index < list.i_count; i_index++ )
1252     {
1253         int i;
1254
1255         p_parser = (module_t *)list.p_values[i_index].p_object ;
1256
1257         /* Nasty hack, but right now I'm too tired to think about a nice
1258          * solution */
1259         i = 22 - strlen( p_parser->psz_object_name ) - 1;
1260         if( i < 0 ) i = 0;
1261         psz_spaces[i] = 0;
1262
1263         fprintf( stdout, "  %s%s %s\n", p_parser->psz_object_name,
1264                          psz_spaces, p_parser->psz_longname );
1265
1266         psz_spaces[i] = ' ';
1267     }
1268
1269     vlc_list_release( &list );
1270
1271 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1272         fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1273         getchar();
1274 #endif
1275 }
1276
1277 /*****************************************************************************
1278  * Version: print complete program version
1279  *****************************************************************************
1280  * Print complete program version and build number.
1281  *****************************************************************************/
1282 static void Version( void )
1283 {
1284 #ifdef WIN32
1285     ShowConsole();
1286 #endif
1287
1288     fprintf( stdout, VERSION_MESSAGE "\n" );
1289     fprintf( stdout,
1290       _("This program comes with NO WARRANTY, to the extent permitted by "
1291         "law.\nYou may redistribute it under the terms of the GNU General "
1292         "Public License;\nsee the file named COPYING for details.\n"
1293         "Written by the VideoLAN team at Ecole Centrale, Paris.\n") );
1294
1295 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1296     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1297     getchar();
1298 #endif
1299 }
1300
1301 /*****************************************************************************
1302  * ShowConsole: On Win32, create an output console for debug messages
1303  *****************************************************************************
1304  * This function is useful only on Win32.
1305  *****************************************************************************/
1306 #ifdef WIN32 /*  */
1307 static void ShowConsole( void )
1308 {
1309 #   ifndef UNDER_CE
1310     AllocConsole();
1311     freopen( "CONOUT$", "w", stdout );
1312     freopen( "CONOUT$", "w", stderr );
1313     freopen( "CONIN$", "r", stdin );
1314 #   endif
1315     return;
1316 }
1317 #endif