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