]> git.sesse.net Git - vlc/blob - src/libvlc.c
* ./src/libvlc.c: added a debug message to test the translation system.
[vlc] / src / libvlc.c
1 /*****************************************************************************
2  * libvlc.c: main libvlc source
3  *****************************************************************************
4  * Copyright (C) 1998-2002 VideoLAN
5  * $Id: libvlc.c,v 1.54 2002/12/25 23:39:01 sam 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     msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
233
234     /*
235      * System specific initialization code
236      */
237     system_Init( p_vlc, &i_argc, ppsz_argv );
238
239     /* Get the executable name (similar to the basename command) */
240     if( i_argc > 0 )
241     {
242         p_vlc->psz_object_name = p_tmp = ppsz_argv[ 0 ];
243         while( *p_tmp )
244         {
245             if( *p_tmp == '/' ) p_vlc->psz_object_name = ++p_tmp;
246             else ++p_tmp;
247         }
248     }
249     else
250     {
251         p_vlc->psz_object_name = "vlc";
252     }
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 #   if defined( HAVE_INCLUDED_GETTEXT ) && !defined( HAVE_LC_MESSAGES )
961     if( *psz_lang )
962     {
963         /* We set LC_ALL manually because it is the only way to set
964          * the language at runtime under eg. Windows. Beware that this
965          * makes the environment unconsistent when libvlc is unloaded and
966          * should probably be moved to a safer place like vlc.c. */
967         static char psz_lcall[20];
968         snprintf( psz_lcall, 19, "LC_ALL=%s", psz_lang );
969         psz_lcall[19] = '\0';
970         putenv( psz_lcall );
971     }
972 #   endif
973
974 #   if defined( HAVE_LC_MESSAGES )
975     setlocale( LC_MESSAGES, psz_lang );
976 #   endif
977     setlocale( LC_CTYPE, psz_lang );
978
979     /* Specify where to find the locales for current domain */
980     if( !bindtextdomain( PACKAGE, LOCALEDIR ) )
981     {
982         fprintf( stderr, "warning: no domain %s in directory %s\n",
983                  PACKAGE, LOCALEDIR );
984     }
985
986     /* Set the default domain */
987     textdomain( PACKAGE );
988 #endif
989 }
990
991 /*****************************************************************************
992  * GetFilenames: parse command line options which are not flags
993  *****************************************************************************
994  * Parse command line for input files.
995  *****************************************************************************/
996 static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
997 {
998     int i_opt;
999
1000     /* We assume that the remaining parameters are filenames */
1001     for( i_opt = i_argc - 1; i_opt > optind; i_opt-- )
1002     {
1003         /* TODO: write an internal function of this one, to avoid
1004          *       unnecessary lookups. */
1005         VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
1006                        PLAYLIST_INSERT, 0 );
1007     }
1008
1009     /* If there is at least one target, play it */
1010     if( i_argc > optind )
1011     {
1012         VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ optind ],
1013                        PLAYLIST_INSERT | PLAYLIST_GO, 0 );
1014     }
1015
1016     return VLC_SUCCESS;
1017 }
1018
1019 /*****************************************************************************
1020  * Usage: print program usage
1021  *****************************************************************************
1022  * Print a short inline help. Message interface is initialized at this stage.
1023  *****************************************************************************/
1024 static void Usage( vlc_t *p_this, char const *psz_module_name )
1025 {
1026 #define FORMAT_STRING "      --%s%s%s%s%s%s%s %s%s\n"
1027     /* option name -------------'     | | | |  | |
1028      * <bra --------------------------' | | |  | |
1029      * option type or "" ---------------' | |  | |
1030      * ket> ------------------------------' |  | |
1031      * padding spaces ----------------------'  | |
1032      * comment --------------------------------' |
1033      * comment suffix ---------------------------'
1034      *
1035      * The purpose of having bra and ket is that we might i18n them as well.
1036      */
1037 #define LINE_START 8
1038 #define PADDING_SPACES 25
1039     vlc_list_t list;
1040     module_t *p_parser;
1041     module_config_t *p_item;
1042     char psz_spaces[PADDING_SPACES+LINE_START+1];
1043     char psz_format[sizeof(FORMAT_STRING)];
1044     int i_index;
1045
1046     memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
1047     psz_spaces[PADDING_SPACES+LINE_START] = '\0';
1048
1049     strcpy( psz_format, FORMAT_STRING );
1050
1051 #ifdef WIN32
1052     ShowConsole();
1053 #endif
1054
1055     /* List all modules */
1056     list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1057
1058     /* Enumerate the config for each module */
1059     for( i_index = 0; i_index < list.i_count; i_index++ )
1060     {
1061         vlc_bool_t b_help_module;
1062
1063         p_parser = (module_t *)list.p_values[i_index].p_object ;
1064
1065         if( psz_module_name && strcmp( psz_module_name,
1066                                        p_parser->psz_object_name ) )
1067         {
1068             continue;
1069         }
1070
1071         /* Ignore modules without config options */
1072         if( !p_parser->i_config_items )
1073         {
1074             continue;
1075         }
1076
1077         b_help_module = !strcmp( "help", p_parser->psz_object_name );
1078
1079         /* Print module options */
1080         for( p_item = p_parser->p_config;
1081              p_item->i_type != CONFIG_HINT_END;
1082              p_item++ )
1083         {
1084             char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
1085             char *psz_suf = "", *psz_prefix = NULL;
1086             int i;
1087
1088             switch( p_item->i_type )
1089             {
1090             case CONFIG_HINT_CATEGORY:
1091             case CONFIG_HINT_USAGE:
1092                 fprintf( stdout, " %s\n", p_item->psz_text );
1093                 break;
1094
1095             case CONFIG_ITEM_STRING:
1096             case CONFIG_ITEM_FILE:
1097             case CONFIG_ITEM_MODULE: /* We could also have "=<" here */
1098                 if( !p_item->ppsz_list )
1099                 {
1100                     psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
1101                     break;
1102                 }
1103                 else
1104                 {
1105                     psz_bra = " [";
1106                     psz_type = malloc( 1000 );
1107                     memset( psz_type, 0, 1000 );
1108                     for( i=0; p_item->ppsz_list[i]; i++ )
1109                     {
1110                         strcat( psz_type, p_item->ppsz_list[i] );
1111                         strcat( psz_type, "|" );
1112                     }
1113                     psz_type[ strlen( psz_type ) - 1 ] = '\0';
1114                     psz_ket = "]";
1115                     break;
1116                 }
1117             case CONFIG_ITEM_INTEGER:
1118                 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
1119                 break;
1120             case CONFIG_ITEM_FLOAT:
1121                 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
1122                 break;
1123             case CONFIG_ITEM_BOOL:
1124                 psz_bra = ""; psz_type = ""; psz_ket = "";
1125                 if( !b_help_module )
1126                 {
1127                     psz_suf = p_item->i_value ? _(" (default enabled)") :
1128                                                 _(" (default disabled)");
1129                 }
1130                 break;
1131             }
1132
1133             /* Add short option if any */
1134             if( p_item->i_short )
1135             {
1136                 psz_format[2] = '-';
1137                 psz_format[3] = p_item->i_short;
1138                 psz_format[4] = ',';
1139             }
1140             else
1141             {
1142                 psz_format[2] = ' ';
1143                 psz_format[3] = ' ';
1144                 psz_format[4] = ' ';
1145             }
1146
1147             if( psz_type )
1148             {
1149                 i = PADDING_SPACES - strlen( p_item->psz_name )
1150                      - strlen( psz_bra ) - strlen( psz_type )
1151                      - strlen( psz_ket ) - 1;
1152                 if( p_item->i_type == CONFIG_ITEM_BOOL
1153                      && !b_help_module )
1154                 {
1155                     /* If option is of type --foo-bar, we print its counterpart
1156                      * as --no-foo-bar, but if it is of type --foobar (without
1157                      * dashes in the name) we print it as --nofoobar. Both
1158                      * values are of course valid, only the display changes. */
1159                     vlc_bool_t b_dash = VLC_FALSE;
1160                     psz_prefix = p_item->psz_name;
1161                     while( *psz_prefix )
1162                     {
1163                         if( *psz_prefix++ == '-' )
1164                         {
1165                             b_dash = VLC_TRUE;
1166                             break;
1167                         }
1168                     }
1169
1170                     if( b_dash )
1171                     {
1172                         psz_prefix = ", --no-";
1173                         i -= strlen( p_item->psz_name ) + strlen( ", --no-" );
1174                     }
1175                     else
1176                     {
1177                         psz_prefix = ", --no";
1178                         i -= strlen( p_item->psz_name ) + strlen( ", --no" );
1179                     }
1180                 }
1181
1182                 if( i < 0 )
1183                 {
1184                     i = 0;
1185                     psz_spaces[i] = '\n';
1186                 }
1187                 else
1188                 {
1189                     psz_spaces[i] = '\0';
1190                 }
1191
1192                 if( p_item->i_type == CONFIG_ITEM_BOOL &&
1193                     !b_help_module )
1194                 {
1195                     fprintf( stdout, psz_format, p_item->psz_name, psz_prefix,
1196                              p_item->psz_name, psz_bra, psz_type, psz_ket,
1197                              psz_spaces, p_item->psz_text, psz_suf );
1198                 }
1199                 else
1200                 {
1201                     fprintf( stdout, psz_format, p_item->psz_name, "", "",
1202                              psz_bra, psz_type, psz_ket, psz_spaces,
1203                              p_item->psz_text, psz_suf );
1204                 }
1205                 psz_spaces[i] = ' ';
1206                 if ( p_item->ppsz_list )
1207                 {
1208                     free( psz_type );
1209                 }
1210             }
1211         }
1212     }
1213
1214     /* Release the module list */
1215     vlc_list_release( &list );
1216
1217 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1218         fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1219         getchar();
1220 #endif
1221 }
1222
1223 /*****************************************************************************
1224  * ListModules: list the available modules with their description
1225  *****************************************************************************
1226  * Print a list of all available modules (builtins and plugins) and a short
1227  * description for each one.
1228  *****************************************************************************/
1229 static void ListModules( vlc_t *p_this )
1230 {
1231     vlc_list_t list;
1232     module_t *p_parser;
1233     char psz_spaces[22];
1234     int i_index;
1235
1236     memset( psz_spaces, ' ', 22 );
1237
1238 #ifdef WIN32
1239     ShowConsole();
1240 #endif
1241
1242     /* Usage */
1243     fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
1244                      p_this->p_vlc->psz_object_name );
1245
1246     fprintf( stdout, _("[module]              [description]\n") );
1247
1248     /* List all modules */
1249     list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1250
1251     /* Enumerate each module */
1252     for( i_index = 0; i_index < list.i_count; i_index++ )
1253     {
1254         int i;
1255
1256         p_parser = (module_t *)list.p_values[i_index].p_object ;
1257
1258         /* Nasty hack, but right now I'm too tired to think about a nice
1259          * solution */
1260         i = 22 - strlen( p_parser->psz_object_name ) - 1;
1261         if( i < 0 ) i = 0;
1262         psz_spaces[i] = 0;
1263
1264         fprintf( stdout, "  %s%s %s\n", p_parser->psz_object_name,
1265                          psz_spaces, p_parser->psz_longname );
1266
1267         psz_spaces[i] = ' ';
1268     }
1269
1270     vlc_list_release( &list );
1271
1272 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1273         fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1274         getchar();
1275 #endif
1276 }
1277
1278 /*****************************************************************************
1279  * Version: print complete program version
1280  *****************************************************************************
1281  * Print complete program version and build number.
1282  *****************************************************************************/
1283 static void Version( void )
1284 {
1285 #ifdef WIN32
1286     ShowConsole();
1287 #endif
1288
1289     fprintf( stdout, VERSION_MESSAGE "\n" );
1290     fprintf( stdout,
1291       _("This program comes with NO WARRANTY, to the extent permitted by "
1292         "law.\nYou may redistribute it under the terms of the GNU General "
1293         "Public License;\nsee the file named COPYING for details.\n"
1294         "Written by the VideoLAN team at Ecole Centrale, Paris.\n") );
1295
1296 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
1297     fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
1298     getchar();
1299 #endif
1300 }
1301
1302 /*****************************************************************************
1303  * ShowConsole: On Win32, create an output console for debug messages
1304  *****************************************************************************
1305  * This function is useful only on Win32.
1306  *****************************************************************************/
1307 #ifdef WIN32 /*  */
1308 static void ShowConsole( void )
1309 {
1310 #   ifndef UNDER_CE
1311     AllocConsole();
1312     freopen( "CONOUT$", "w", stdout );
1313     freopen( "CONOUT$", "w", stderr );
1314     freopen( "CONIN$", "r", stdin );
1315 #   endif
1316     return;
1317 }
1318 #endif