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