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