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