]> git.sesse.net Git - vlc/blob - src/libvlc.c
Added 'Boss Key' support to the core.
[vlc] / src / libvlc.c
1 /*****************************************************************************
2  * libvlc.c: libvlc instances creation and deletion, interfaces handling
3  *****************************************************************************
4  * Copyright (C) 1998-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *          Derk-Jan Hartman <hartman at videolan dot org>
11  *          RĂ©mi Denis-Courmont <rem # videolan : org>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 /** \file
29  * This file contains functions to create and destroy libvlc instances
30  */
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <vlc_common.h>
40 #include "../lib/libvlc_internal.h"
41 #include <vlc_input.h>
42
43 #include "modules/modules.h"
44 #include "config/configuration.h"
45
46 #include <stdio.h>                                              /* sprintf() */
47 #include <string.h>
48 #include <stdlib.h>                                                /* free() */
49
50 #ifdef HAVE_UNISTD_H
51 #   include <unistd.h>
52 #elif defined( WIN32 ) && !defined( UNDER_CE )
53 #   include <io.h>
54 #endif
55
56 #include "config/vlc_getopt.h"
57
58 #ifdef HAVE_LOCALE_H
59 #   include <locale.h>
60 #endif
61
62 #ifdef HAVE_DBUS
63 /* used for one-instance mode */
64 #   include <dbus/dbus.h>
65 #endif
66
67
68 #include <vlc_media_library.h>
69 #include <vlc_playlist.h>
70 #include <vlc_interface.h>
71
72 #include <vlc_aout.h>
73 #include "audio_output/aout_internal.h"
74
75 #include <vlc_charset.h>
76 #include <vlc_fs.h>
77 #include <vlc_cpu.h>
78 #include <vlc_url.h>
79 #include <vlc_atomic.h>
80 #include <vlc_modules.h>
81
82 #include "libvlc.h"
83
84 #include "playlist/playlist_internal.h"
85
86 #include <vlc_vlm.h>
87
88 #ifdef __APPLE__
89 # include <libkern/OSAtomic.h>
90 #endif
91
92 #include <assert.h>
93
94 /*****************************************************************************
95  * The evil global variables. We handle them with care, don't worry.
96  *****************************************************************************/
97
98 #ifndef WIN32
99 static bool b_daemon = false;
100 #endif
101
102 #undef vlc_gc_init
103 #undef vlc_hold
104 #undef vlc_release
105
106 /**
107  * Atomically set the reference count to 1.
108  * @param p_gc reference counted object
109  * @param pf_destruct destruction calback
110  * @return p_gc.
111  */
112 void *vlc_gc_init (gc_object_t *p_gc, void (*pf_destruct) (gc_object_t *))
113 {
114     /* There is no point in using the GC if there is no destructor... */
115     assert (pf_destruct);
116     p_gc->pf_destructor = pf_destruct;
117
118     vlc_atomic_set (&p_gc->refs, 1);
119     return p_gc;
120 }
121
122 /**
123  * Atomically increment the reference count.
124  * @param p_gc reference counted object
125  * @return p_gc.
126  */
127 void *vlc_hold (gc_object_t * p_gc)
128 {
129     uintptr_t refs;
130
131     assert( p_gc );
132     refs = vlc_atomic_inc (&p_gc->refs);
133     assert (refs != 1); /* there had to be a reference already */
134     return p_gc;
135 }
136
137 /**
138  * Atomically decrement the reference count and, if it reaches zero, destroy.
139  * @param p_gc reference counted object.
140  */
141 void vlc_release (gc_object_t *p_gc)
142 {
143     uintptr_t refs;
144
145     assert( p_gc );
146     refs = vlc_atomic_dec (&p_gc->refs);
147     assert (refs != (uintptr_t)(-1)); /* reference underflow?! */
148     if (refs == 0)
149         p_gc->pf_destructor (p_gc);
150 }
151
152 /*****************************************************************************
153  * Local prototypes
154  *****************************************************************************/
155 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
156     ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
157 static void SetLanguage   ( char const * );
158 #endif
159 static void GetFilenames  ( libvlc_int_t *, unsigned, const char *const [] );
160
161 /**
162  * Allocate a libvlc instance, initialize global data if needed
163  * It also initializes the threading system
164  */
165 libvlc_int_t * libvlc_InternalCreate( void )
166 {
167     libvlc_int_t *p_libvlc;
168     libvlc_priv_t *priv;
169     char *psz_env = NULL;
170
171     /* Now that the thread system is initialized, we don't have much, but
172      * at least we have variables */
173     /* Allocate a libvlc instance object */
174     p_libvlc = vlc_custom_create( (vlc_object_t *)NULL, sizeof (*priv),
175                                   "libvlc" );
176     if( p_libvlc == NULL )
177         return NULL;
178
179     priv = libvlc_priv (p_libvlc);
180     priv->p_playlist = NULL;
181     priv->p_ml = NULL;
182     priv->p_dialog_provider = NULL;
183     priv->p_vlm = NULL;
184
185     /* Find verbosity from VLC_VERBOSE environment variable */
186     psz_env = getenv( "VLC_VERBOSE" );
187     if( psz_env != NULL )
188         priv->i_verbose = atoi( psz_env );
189     else
190         priv->i_verbose = 3;
191 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
192     priv->b_color = isatty( 2 ); /* 2 is for stderr */
193 #else
194     priv->b_color = false;
195 #endif
196
197     /* Initialize mutexes */
198     vlc_mutex_init( &priv->ml_lock );
199     vlc_mutex_init( &priv->timer_lock );
200     vlc_ExitInit( &priv->exit );
201
202     return p_libvlc;
203 }
204
205 /**
206  * Initialize a libvlc instance
207  * This function initializes a previously allocated libvlc instance:
208  *  - CPU detection
209  *  - gettext initialization
210  *  - message queue, module bank and playlist initialization
211  *  - configuration and commandline parsing
212  */
213 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
214                          const char *ppsz_argv[] )
215 {
216     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
217     char *       psz_modules = NULL;
218     char *       psz_parser = NULL;
219     char *       psz_control = NULL;
220     playlist_t  *p_playlist = NULL;
221     char        *psz_val;
222
223     /* System specific initialization code */
224     system_Init();
225
226     /* Initialize the module bank and load the configuration of the
227      * main module. We need to do this at this stage to be able to display
228      * a short help if required by the user. (short help == main module
229      * options) */
230     module_InitBank ();
231
232     /* Get command line options that affect module loading. */
233     if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
234     {
235         module_EndBank (false);
236         return VLC_EGENERIC;
237     }
238     priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
239
240     /* Announce who we are (TODO: only first instance?) */
241     msg_Dbg( p_libvlc, "VLC media player - %s", VERSION_MESSAGE );
242     msg_Dbg( p_libvlc, "%s", COPYRIGHT_MESSAGE );
243     msg_Dbg( p_libvlc, "revision %s", psz_vlc_changeset );
244     msg_Dbg( p_libvlc, "configured with %s", CONFIGURE_LINE );
245
246     /* Load the builtins and plugins into the module_bank.
247      * We have to do it before config_Load*() because this also gets the
248      * list of configuration options exported by each module and loads their
249      * default values. */
250     size_t module_count = module_LoadPlugins (p_libvlc);
251
252     /*
253      * Override default configuration with config file settings
254      */
255     if( !var_InheritBool( p_libvlc, "ignore-config" ) )
256     {
257         if( var_InheritBool( p_libvlc, "reset-config" ) )
258             config_SaveConfigFile( p_libvlc ); /* Save default config */
259         else
260             config_LoadConfigFile( p_libvlc );
261     }
262
263     /*
264      * Override configuration with command line settings
265      */
266     int vlc_optind;
267     if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
268     {
269 #ifdef WIN32
270         MessageBox (NULL, TEXT("The command line options could not be parsed.\n"
271                     "Make sure they are valid."), TEXT("VLC media player"),
272                     MB_OK|MB_ICONERROR);
273 #endif
274         module_EndBank (true);
275         return VLC_EGENERIC;
276     }
277     priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
278
279     /*
280      * Support for gettext
281      */
282 #if defined( ENABLE_NLS ) \
283      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
284 # if defined (WIN32) || defined (__APPLE__)
285     /* Check if the user specified a custom language */
286     char *lang = var_InheritString (p_libvlc, "language");
287     if (lang != NULL && strcmp (lang, "auto"))
288         SetLanguage (lang);
289     free (lang);
290 # endif
291     vlc_bindtextdomain (PACKAGE_NAME);
292 #endif
293     /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
294     msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
295
296     if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
297     {
298         module_EndBank (true);
299         return VLC_EEXITSUCCESS;
300     }
301
302     if( module_count <= 1 )
303     {
304         msg_Err( p_libvlc, "No plugins found! Check your VLC installation.");
305         module_EndBank (true);
306         return VLC_ENOITEM;
307     }
308
309 #ifdef HAVE_DAEMON
310     /* Check for daemon mode */
311     if( var_InheritBool( p_libvlc, "daemon" ) )
312     {
313         char *psz_pidfile = NULL;
314
315         if( daemon( 1, 0) != 0 )
316         {
317             msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
318             module_EndBank (true);
319             return VLC_EEXIT;
320         }
321         b_daemon = true;
322
323         /* lets check if we need to write the pidfile */
324         psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
325         if( psz_pidfile != NULL )
326         {
327             FILE *pidfile;
328             pid_t i_pid = getpid ();
329             msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
330                                i_pid, psz_pidfile );
331             pidfile = vlc_fopen( psz_pidfile,"w" );
332             if( pidfile != NULL )
333             {
334                 utf8_fprintf( pidfile, "%d", (int)i_pid );
335                 fclose( pidfile );
336             }
337             else
338             {
339                 msg_Err( p_libvlc, "cannot open pid file for writing: %s (%m)",
340                          psz_pidfile );
341             }
342         }
343         free( psz_pidfile );
344     }
345 #endif
346
347 /* FIXME: could be replaced by using Unix sockets */
348 #ifdef HAVE_DBUS
349     dbus_threads_init_default();
350
351     if( var_InheritBool( p_libvlc, "one-instance" )
352     || ( var_InheritBool( p_libvlc, "one-instance-when-started-from-file" )
353       && var_InheritBool( p_libvlc, "started-from-file" ) ) )
354     {
355         /* Initialise D-Bus interface, check for other instances */
356         DBusConnection  *p_conn = NULL;
357         DBusError       dbus_error;
358
359         dbus_error_init( &dbus_error );
360
361         /* connect to the session bus */
362         p_conn = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
363         if( !p_conn )
364         {
365             msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
366                     dbus_error.message );
367             dbus_error_free( &dbus_error );
368         }
369         else
370         {
371             /* check if VLC is available on the bus
372              * if not: D-Bus control is not enabled on the other
373              * instance and we can't pass MRLs to it */
374             DBusMessage *p_test_msg   = NULL;
375             DBusMessage *p_test_reply = NULL;
376
377             p_test_msg =  dbus_message_new_method_call(
378                     "org.mpris.MediaPlayer2.vlc", "/org/mpris/MediaPlayer2",
379                     "org.freedesktop.DBus.Introspectable", "Introspect" );
380
381             /* block until a reply arrives */
382             p_test_reply = dbus_connection_send_with_reply_and_block(
383                     p_conn, p_test_msg, -1, &dbus_error );
384             dbus_message_unref( p_test_msg );
385             if( p_test_reply == NULL )
386             {
387                 dbus_error_free( &dbus_error );
388                 msg_Dbg( p_libvlc, "No Media Player is running. "
389                         "Continuing normally." );
390             }
391             else
392             {
393                 int i_input;
394                 DBusMessage* p_dbus_msg = NULL;
395                 DBusMessageIter dbus_args;
396                 DBusPendingCall* p_dbus_pending = NULL;
397                 dbus_bool_t b_play;
398
399                 dbus_message_unref( p_test_reply );
400                 msg_Warn( p_libvlc, "Another Media Player is running. Exiting");
401
402                 for( i_input = vlc_optind; i_input < i_argc;i_input++ )
403                 {
404                     /* Skip input options, we can't pass them through D-Bus */
405                     if( ppsz_argv[i_input][0] == ':' )
406                     {
407                         msg_Warn( p_libvlc, "Ignoring option %s",
408                                   ppsz_argv[i_input] );
409                         continue;
410                     }
411
412                     /* We need to resolve relative paths in this instance */
413                     char *psz_mrl = make_URI( ppsz_argv[i_input], NULL );
414                     const char *psz_after_track = "/";
415
416                     if( psz_mrl == NULL )
417                         continue;
418                     msg_Dbg( p_libvlc, "Adds %s to the running Media Player",
419                              psz_mrl );
420
421                     p_dbus_msg = dbus_message_new_method_call(
422                         "org.mpris.MediaPlayer2.vlc", "/org/mpris/MediaPlayer2",
423                         "org.mpris.MediaPlayer2.TrackList", "AddTrack" );
424
425                     if ( NULL == p_dbus_msg )
426                     {
427                         msg_Err( p_libvlc, "D-Bus problem" );
428                         free( psz_mrl );
429                         system_End( );
430                         exit( 1 );
431                     }
432
433                     /* append MRLs */
434                     dbus_message_iter_init_append( p_dbus_msg, &dbus_args );
435                     if ( !dbus_message_iter_append_basic( &dbus_args,
436                                 DBUS_TYPE_STRING, &psz_mrl ) )
437                     {
438                         dbus_message_unref( p_dbus_msg );
439                         free( psz_mrl );
440                         system_End( );
441                         exit( 1 );
442                     }
443                     free( psz_mrl );
444
445                     if( !dbus_message_iter_append_basic( &dbus_args,
446                                 DBUS_TYPE_OBJECT_PATH, &psz_after_track ) )
447                     {
448                         dbus_message_unref( p_dbus_msg );
449                         system_End( );
450                         exit( 1 );
451                     }
452
453                     b_play = TRUE;
454                     if( var_InheritBool( p_libvlc, "playlist-enqueue" ) )
455                         b_play = FALSE;
456
457                     if ( !dbus_message_iter_append_basic( &dbus_args,
458                                 DBUS_TYPE_BOOLEAN, &b_play ) )
459                     {
460                         dbus_message_unref( p_dbus_msg );
461                         system_End( );
462                         exit( 1 );
463                     }
464
465                     /* send message and get a handle for a reply */
466                     if ( !dbus_connection_send_with_reply ( p_conn,
467                                 p_dbus_msg, &p_dbus_pending, -1 ) )
468                     {
469                         msg_Err( p_libvlc, "D-Bus problem" );
470                         dbus_message_unref( p_dbus_msg );
471                         system_End( );
472                         exit( 1 );
473                     }
474
475                     if ( NULL == p_dbus_pending )
476                     {
477                         msg_Err( p_libvlc, "D-Bus problem" );
478                         dbus_message_unref( p_dbus_msg );
479                         system_End( );
480                         exit( 1 );
481                     }
482                     dbus_connection_flush( p_conn );
483                     dbus_message_unref( p_dbus_msg );
484                     /* block until we receive a reply */
485                     dbus_pending_call_block( p_dbus_pending );
486                     dbus_pending_call_unref( p_dbus_pending );
487                 } /* processes all command line MRLs */
488
489                 /* bye bye */
490                 system_End( );
491                 exit( 0 );
492             }
493         }
494         /* we unreference the connection when we've finished with it */
495         if( p_conn ) dbus_connection_unref( p_conn );
496     }
497 #endif
498
499     /*
500      * Message queue options
501      */
502     /* Last chance to set the verbosity. Once we start interfaces and other
503      * threads, verbosity becomes read-only. */
504     var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
505     if( var_InheritBool( p_libvlc, "quiet" ) )
506     {
507         var_SetInteger( p_libvlc, "verbose", -1 );
508         priv->i_verbose = -1;
509     }
510     vlc_threads_setup( p_libvlc );
511
512     if( priv->b_color )
513         priv->b_color = var_InheritBool( p_libvlc, "color" );
514
515     vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
516     /*
517      * Choose the best memcpy module
518      */
519     priv->p_memcpy_module = module_need( p_libvlc, "memcpy", "$memcpy", false );
520     /* Avoid being called "memcpy":*/
521     vlc_object_set_name( p_libvlc, "main" );
522
523     priv->b_stats = var_InheritBool( p_libvlc, "stats" );
524     priv->i_timers = 0;
525     priv->pp_timers = NULL;
526
527     /*
528      * Initialize hotkey handling
529      */
530     priv->actions = vlc_InitActions( p_libvlc );
531
532     /* Create a variable for showing the fullscreen interface */
533     var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL );
534     var_SetBool( p_libvlc, "intf-show", true );
535
536     /* Create a variable for the Boss Key */
537     var_Create( p_libvlc, "intf-boss", VLC_VAR_VOID );
538
539     /* Create a variable for showing the right click menu */
540     var_Create( p_libvlc, "intf-popupmenu", VLC_VAR_BOOL );
541
542     /* variables for signalling creation of new files */
543     var_Create( p_libvlc, "snapshot-file", VLC_VAR_STRING );
544     var_Create( p_libvlc, "record-file", VLC_VAR_STRING );
545
546     /* some default internal settings */
547     var_Create( p_libvlc, "window", VLC_VAR_STRING );
548     var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
549     var_SetString( p_libvlc, "user-agent", "(LibVLC "VERSION")" );
550
551     /* Initialize playlist and get commandline files */
552     p_playlist = playlist_Create( VLC_OBJECT(p_libvlc) );
553     if( !p_playlist )
554     {
555         msg_Err( p_libvlc, "playlist initialization failed" );
556         if( priv->p_memcpy_module != NULL )
557         {
558             module_unneed( p_libvlc, priv->p_memcpy_module );
559         }
560         module_EndBank (true);
561         return VLC_EGENERIC;
562     }
563
564     /* System specific configuration */
565     system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
566
567 #if defined(MEDIA_LIBRARY)
568     /* Get the ML */
569     if( var_GetBool( p_libvlc, "load-media-library-on-startup" ) )
570     {
571         priv->p_ml = ml_Create( VLC_OBJECT( p_libvlc ), NULL );
572         if( !priv->p_ml )
573         {
574             msg_Err( p_libvlc, "ML initialization failed" );
575             return VLC_EGENERIC;
576         }
577     }
578     else
579     {
580         priv->p_ml = NULL;
581     }
582 #endif
583
584     /* Add service discovery modules */
585     psz_modules = var_InheritString( p_libvlc, "services-discovery" );
586     if( psz_modules )
587     {
588         char *p = psz_modules, *m;
589         while( ( m = strsep( &p, " :," ) ) != NULL )
590             playlist_ServicesDiscoveryAdd( p_playlist, m );
591         free( psz_modules );
592     }
593
594 #ifdef ENABLE_VLM
595     /* Initialize VLM if vlm-conf is specified */
596     psz_parser = var_CreateGetNonEmptyString( p_libvlc, "vlm-conf" );
597     if( psz_parser )
598     {
599         priv->p_vlm = vlm_New( p_libvlc );
600         if( !priv->p_vlm )
601             msg_Err( p_libvlc, "VLM initialization failed" );
602     }
603     free( psz_parser );
604 #endif
605
606     /*
607      * Load background interfaces
608      */
609     psz_modules = var_CreateGetNonEmptyString( p_libvlc, "extraintf" );
610     psz_control = var_CreateGetNonEmptyString( p_libvlc, "control" );
611
612     if( psz_modules && psz_control )
613     {
614         char* psz_tmp;
615         if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
616         {
617             free( psz_modules );
618             psz_modules = psz_tmp;
619         }
620     }
621     else if( psz_control )
622     {
623         free( psz_modules );
624         psz_modules = strdup( psz_control );
625     }
626
627     psz_parser = psz_modules;
628     while ( psz_parser && *psz_parser )
629     {
630         char *psz_module, *psz_temp;
631         psz_module = psz_parser;
632         psz_parser = strchr( psz_module, ':' );
633         if ( psz_parser )
634         {
635             *psz_parser = '\0';
636             psz_parser++;
637         }
638         if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
639         {
640             intf_Create( p_libvlc, psz_temp );
641             free( psz_temp );
642         }
643     }
644     free( psz_modules );
645     free( psz_control );
646
647     /*
648      * Always load the hotkeys interface if it exists
649      */
650     intf_Create( p_libvlc, "hotkeys,none" );
651
652 #ifdef HAVE_DBUS
653     /* loads dbus control interface if in one-instance mode
654      * we do it only when playlist exists, because dbus module needs it */
655     if( var_InheritBool( p_libvlc, "one-instance" )
656      || ( var_InheritBool( p_libvlc, "one-instance-when-started-from-file" )
657        && var_InheritBool( p_libvlc, "started-from-file" ) ) )
658         intf_Create( p_libvlc, "dbus,none" );
659
660 # if !defined (HAVE_MAEMO)
661     /* Prevents the power management daemon from suspending the system
662      * when VLC is active */
663     if( var_InheritBool( p_libvlc, "inhibit" ) > 0 )
664         intf_Create( p_libvlc, "inhibit,none" );
665 # endif
666 #endif
667
668     if( var_InheritBool( p_libvlc, "file-logging" ) &&
669         !var_InheritBool( p_libvlc, "syslog" ) )
670     {
671         intf_Create( p_libvlc, "logger,none" );
672     }
673 #ifdef HAVE_SYSLOG_H
674     if( var_InheritBool( p_libvlc, "syslog" ) )
675     {
676         char *logmode = var_CreateGetNonEmptyString( p_libvlc, "logmode" );
677         var_SetString( p_libvlc, "logmode", "syslog" );
678         intf_Create( p_libvlc, "logger,none" );
679
680         if( logmode )
681         {
682             var_SetString( p_libvlc, "logmode", logmode );
683             free( logmode );
684         }
685         var_Destroy( p_libvlc, "logmode" );
686     }
687 #endif
688
689     if( var_InheritBool( p_libvlc, "network-synchronisation") )
690     {
691         intf_Create( p_libvlc, "netsync,none" );
692     }
693
694 #ifdef __APPLE__
695     var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
696     var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
697     var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
698     var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
699     var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
700     var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
701     var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
702     var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
703     var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
704 #endif
705 #ifdef WIN32
706     var_Create( p_libvlc, "drawable-hwnd", VLC_VAR_INTEGER );
707 #endif
708
709     /*
710      * Get input filenames given as commandline arguments.
711      * We assume that the remaining parameters are filenames
712      * and their input options.
713      */
714     GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
715
716     /*
717      * Get --open argument
718      */
719     psz_val = var_InheritString( p_libvlc, "open" );
720     if ( psz_val != NULL )
721     {
722         playlist_AddExt( p_playlist, psz_val, NULL, PLAYLIST_INSERT, 0,
723                          -1, 0, NULL, 0, true, pl_Unlocked );
724         free( psz_val );
725     }
726
727     return VLC_SUCCESS;
728 }
729
730 /**
731  * Cleanup a libvlc instance. The instance is not completely deallocated
732  * \param p_libvlc the instance to clean
733  */
734 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
735 {
736     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
737     playlist_t    *p_playlist = libvlc_priv (p_libvlc)->p_playlist;
738
739     /* Deactivate the playlist */
740     msg_Dbg( p_libvlc, "deactivating the playlist" );
741     pl_Deactivate( p_libvlc );
742
743     /* Remove all services discovery */
744     msg_Dbg( p_libvlc, "removing all services discovery tasks" );
745     playlist_ServicesDiscoveryKillAll( p_playlist );
746
747     /* Ask the interfaces to stop and destroy them */
748     msg_Dbg( p_libvlc, "removing all interfaces" );
749     libvlc_Quit( p_libvlc );
750     intf_DestroyAll( p_libvlc );
751
752 #ifdef ENABLE_VLM
753     /* Destroy VLM if created in libvlc_InternalInit */
754     if( priv->p_vlm )
755     {
756         vlm_Delete( priv->p_vlm );
757     }
758 #endif
759
760 #if defined(MEDIA_LIBRARY)
761     media_library_t* p_ml = priv->p_ml;
762     if( p_ml )
763     {
764         ml_Destroy( VLC_OBJECT( p_ml ) );
765         vlc_object_release( p_ml );
766         libvlc_priv(p_playlist->p_libvlc)->p_ml = NULL;
767     }
768 #endif
769
770     /* Free playlist now, all threads are gone */
771     playlist_Destroy( p_playlist );
772     stats_TimersDumpAll( p_libvlc );
773     stats_TimersCleanAll( p_libvlc );
774
775     msg_Dbg( p_libvlc, "removing stats" );
776
777 #ifndef WIN32
778     char* psz_pidfile = NULL;
779
780     if( b_daemon )
781     {
782         psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
783         if( psz_pidfile != NULL )
784         {
785             msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
786             if( unlink( psz_pidfile ) == -1 )
787             {
788                 msg_Dbg( p_libvlc, "removing pid file %s: %m",
789                         psz_pidfile );
790             }
791         }
792         free( psz_pidfile );
793     }
794 #endif
795
796     if( priv->p_memcpy_module )
797     {
798         module_unneed( p_libvlc, priv->p_memcpy_module );
799         priv->p_memcpy_module = NULL;
800     }
801
802     /* Save the configuration */
803     if( !var_InheritBool( p_libvlc, "ignore-config" ) )
804         config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
805
806     /* Free module bank. It is refcounted, so we call this each time  */
807     module_EndBank (true);
808
809     vlc_DeinitActions( p_libvlc, priv->actions );
810 }
811
812 /**
813  * Destroy everything.
814  * This function requests the running threads to finish, waits for their
815  * termination, and destroys their structure.
816  * It stops the thread systems: no instance can run after this has run
817  * \param p_libvlc the instance to destroy
818  */
819 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
820 {
821     libvlc_priv_t *priv = libvlc_priv( p_libvlc );
822
823     system_End( );
824
825     /* Destroy mutexes */
826     vlc_ExitDestroy( &priv->exit );
827     vlc_mutex_destroy( &priv->timer_lock );
828     vlc_mutex_destroy( &priv->ml_lock );
829
830 #ifndef NDEBUG /* Hack to dump leaked objects tree */
831     if( vlc_internals( p_libvlc )->i_refcount > 1 )
832         while( vlc_internals( p_libvlc )->i_refcount > 0 )
833             vlc_object_release( p_libvlc );
834 #endif
835
836     assert( vlc_internals( p_libvlc )->i_refcount == 1 );
837     vlc_object_release( p_libvlc );
838 }
839
840 /**
841  * Add an interface plugin and run it
842  */
843 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc, char const *psz_module )
844 {
845     if( !p_libvlc )
846         return VLC_EGENERIC;
847
848     if( !psz_module ) /* requesting the default interface */
849     {
850         char *psz_interface = var_CreateGetNonEmptyString( p_libvlc, "intf" );
851         if( !psz_interface ) /* "intf" has not been set */
852         {
853 #ifndef WIN32
854             if( b_daemon )
855                  /* Daemon mode hack.
856                   * We prefer the dummy interface if none is specified. */
857                 psz_module = "dummy";
858             else
859 #endif
860                 msg_Info( p_libvlc, "%s",
861                           _("Running vlc with the default interface. "
862                             "Use 'cvlc' to use vlc without interface.") );
863         }
864         free( psz_interface );
865         var_Destroy( p_libvlc, "intf" );
866     }
867
868     /* Try to create the interface */
869     int ret = intf_Create( p_libvlc, psz_module ? psz_module : "$intf" );
870     if( ret )
871         msg_Err( p_libvlc, "interface \"%s\" initialization failed",
872                  psz_module ? psz_module : "default" );
873     return ret;
874 }
875
876 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
877     ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
878 /*****************************************************************************
879  * SetLanguage: set the interface language.
880  *****************************************************************************
881  * We set the LC_MESSAGES locale category for interface messages and buttons,
882  * as well as the LC_CTYPE category for string sorting and possible wide
883  * character support.
884  *****************************************************************************/
885 static void SetLanguage ( const char *psz_lang )
886 {
887 #ifdef __APPLE__
888     /* I need that under Darwin, please check it doesn't disturb
889      * other platforms. --Meuuh */
890     setenv( "LANG", psz_lang, 1 );
891
892 #else
893     /* We set LC_ALL manually because it is the only way to set
894      * the language at runtime under eg. Windows. Beware that this
895      * makes the environment unconsistent when libvlc is unloaded and
896      * should probably be moved to a safer place like vlc.c. */
897     setenv( "LC_ALL", psz_lang, 1 );
898
899 #endif
900
901     setlocale( LC_ALL, psz_lang );
902 }
903 #endif
904
905 /*****************************************************************************
906  * GetFilenames: parse command line options which are not flags
907  *****************************************************************************
908  * Parse command line for input files as well as their associated options.
909  * An option always follows its associated input and begins with a ":".
910  *****************************************************************************/
911 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
912                           const char *const args[] )
913 {
914     while( n > 0 )
915     {
916         /* Count the input options */
917         unsigned i_options = 0;
918
919         while( args[--n][0] == ':' )
920         {
921             i_options++;
922             if( n == 0 )
923             {
924                 msg_Warn( p_vlc, "options %s without item", args[n] );
925                 return; /* syntax!? */
926             }
927         }
928
929         char *mrl = make_URI( args[n], NULL );
930         if( !mrl )
931             continue;
932
933         playlist_AddExt( pl_Get( p_vlc ), mrl, NULL, PLAYLIST_INSERT,
934                 0, -1, i_options, ( i_options ? &args[n + 1] : NULL ),
935                 VLC_INPUT_OPTION_TRUSTED, true, pl_Unlocked );
936         free( mrl );
937     }
938 }