]> git.sesse.net Git - vlc/blob - src/libvlc.c
Remove write-only timer statistics
[vlc] / src / libvlc.c
1 /*****************************************************************************
2  * libvlc.c: libvlc instances creation and deletion, interfaces handling
3  *****************************************************************************
4  * Copyright (C) 1998-2008 VLC authors and VideoLAN
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 it
14  * under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * 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 #include "config/vlc_getopt.h"
51
52 #ifdef HAVE_LOCALE_H
53 #   include <locale.h>
54 #endif
55 #ifdef HAVE_UNISTD_H
56 #   include <unistd.h> /* isatty() */
57 #endif
58
59 #ifdef HAVE_DBUS
60 /* used for one-instance mode */
61 #   include <dbus/dbus.h>
62 #endif
63
64
65 #include <vlc_media_library.h>
66 #include <vlc_playlist.h>
67 #include <vlc_interface.h>
68
69 #include <vlc_aout.h>
70 #include "audio_output/aout_internal.h"
71
72 #include <vlc_charset.h>
73 #include <vlc_fs.h>
74 #include <vlc_cpu.h>
75 #include <vlc_url.h>
76 #include <vlc_atomic.h>
77 #include <vlc_modules.h>
78
79 #include "libvlc.h"
80
81 #include "playlist/playlist_internal.h"
82
83 #include <vlc_vlm.h>
84
85 #ifdef __APPLE__
86 # include <libkern/OSAtomic.h>
87 #endif
88
89 #include <assert.h>
90
91 /*****************************************************************************
92  * The evil global variables. We handle them with care, don't worry.
93  *****************************************************************************/
94
95 #if !defined(WIN32) && !defined(__OS2__)
96 static bool b_daemon = false;
97 #endif
98
99 #undef vlc_gc_init
100 #undef vlc_hold
101 #undef vlc_release
102
103 /**
104  * Atomically set the reference count to 1.
105  * @param p_gc reference counted object
106  * @param pf_destruct destruction calback
107  * @return p_gc.
108  */
109 void *vlc_gc_init (gc_object_t *p_gc, void (*pf_destruct) (gc_object_t *))
110 {
111     /* There is no point in using the GC if there is no destructor... */
112     assert (pf_destruct);
113     p_gc->pf_destructor = pf_destruct;
114
115     vlc_atomic_set (&p_gc->refs, 1);
116     return p_gc;
117 }
118
119 /**
120  * Atomically increment the reference count.
121  * @param p_gc reference counted object
122  * @return p_gc.
123  */
124 void *vlc_hold (gc_object_t * p_gc)
125 {
126     uintptr_t refs;
127
128     assert( p_gc );
129     refs = vlc_atomic_inc (&p_gc->refs);
130     assert (refs != 1); /* there had to be a reference already */
131     return p_gc;
132 }
133
134 /**
135  * Atomically decrement the reference count and, if it reaches zero, destroy.
136  * @param p_gc reference counted object.
137  */
138 void vlc_release (gc_object_t *p_gc)
139 {
140     uintptr_t refs;
141
142     assert( p_gc );
143     refs = vlc_atomic_dec (&p_gc->refs);
144     assert (refs != (uintptr_t)(-1)); /* reference underflow?! */
145     if (refs == 0)
146         p_gc->pf_destructor (p_gc);
147 }
148
149 /*****************************************************************************
150  * Local prototypes
151  *****************************************************************************/
152 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
153     ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
154 static void SetLanguage   ( char const * );
155 #endif
156 static void GetFilenames  ( libvlc_int_t *, unsigned, const char *const [] );
157
158 /**
159  * Allocate a libvlc instance, initialize global data if needed
160  * It also initializes the threading system
161  */
162 libvlc_int_t * libvlc_InternalCreate( void )
163 {
164     libvlc_int_t *p_libvlc;
165     libvlc_priv_t *priv;
166     char *psz_env = NULL;
167
168     /* Now that the thread system is initialized, we don't have much, but
169      * at least we have variables */
170     /* Allocate a libvlc instance object */
171     p_libvlc = vlc_custom_create( (vlc_object_t *)NULL, sizeof (*priv),
172                                   "libvlc" );
173     if( p_libvlc == NULL )
174         return NULL;
175
176     priv = libvlc_priv (p_libvlc);
177     priv->p_playlist = NULL;
178     priv->p_ml = NULL;
179     priv->p_dialog_provider = NULL;
180     priv->p_vlm = NULL;
181
182     /* Find verbosity from VLC_VERBOSE environment variable */
183     psz_env = getenv( "VLC_VERBOSE" );
184     if( psz_env != NULL )
185         priv->i_verbose = atoi( psz_env );
186     else
187         priv->i_verbose = 3;
188 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
189     priv->b_color = isatty( 2 ); /* 2 is for stderr */
190 #else
191     priv->b_color = false;
192 #endif
193
194     /* Initialize mutexes */
195     vlc_mutex_init( &priv->ml_lock );
196     vlc_mutex_init( &priv->timer_lock );
197     vlc_ExitInit( &priv->exit );
198
199     return p_libvlc;
200 }
201
202 /**
203  * Initialize a libvlc instance
204  * This function initializes a previously allocated libvlc instance:
205  *  - CPU detection
206  *  - gettext initialization
207  *  - message queue, module bank and playlist initialization
208  *  - configuration and commandline parsing
209  */
210 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
211                          const char *ppsz_argv[] )
212 {
213     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
214     char *       psz_modules = NULL;
215     char *       psz_parser = NULL;
216     char *       psz_control = NULL;
217     playlist_t  *p_playlist = NULL;
218     char        *psz_val;
219
220     /* System specific initialization code */
221     system_Init();
222
223     /* Initialize the module bank and load the configuration of the
224      * main module. We need to do this at this stage to be able to display
225      * a short help if required by the user. (short help == main module
226      * options) */
227     module_InitBank ();
228
229     /* Get command line options that affect module loading. */
230     if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
231     {
232         module_EndBank (false);
233         return VLC_EGENERIC;
234     }
235     priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
236
237     /* Announce who we are (TODO: only first instance?) */
238     msg_Dbg( p_libvlc, "VLC media player - %s", VERSION_MESSAGE );
239     msg_Dbg( p_libvlc, "%s", COPYRIGHT_MESSAGE );
240     msg_Dbg( p_libvlc, "revision %s", psz_vlc_changeset );
241     msg_Dbg( p_libvlc, "configured with %s", CONFIGURE_LINE );
242
243     /* Load the builtins and plugins into the module_bank.
244      * We have to do it before config_Load*() because this also gets the
245      * list of configuration options exported by each module and loads their
246      * default values. */
247     size_t module_count = module_LoadPlugins (p_libvlc);
248
249     /*
250      * Override default configuration with config file settings
251      */
252     if( !var_InheritBool( p_libvlc, "ignore-config" ) )
253     {
254         if( var_InheritBool( p_libvlc, "reset-config" ) )
255             config_SaveConfigFile( p_libvlc ); /* Save default config */
256         else
257             config_LoadConfigFile( p_libvlc );
258     }
259
260     /*
261      * Override configuration with command line settings
262      */
263     int vlc_optind;
264     if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
265     {
266 #ifdef WIN32
267         MessageBox (NULL, TEXT("The command line options could not be parsed.\n"
268                     "Make sure they are valid."), TEXT("VLC media player"),
269                     MB_OK|MB_ICONERROR);
270 #endif
271         module_EndBank (true);
272         return VLC_EGENERIC;
273     }
274     priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
275
276     /*
277      * Support for gettext
278      */
279 #if defined( ENABLE_NLS ) \
280      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
281 # if defined (WIN32) || defined (__APPLE__)
282     /* Check if the user specified a custom language */
283     char *lang = var_InheritString (p_libvlc, "language");
284     if (lang != NULL && strcmp (lang, "auto"))
285         SetLanguage (lang);
286     free (lang);
287 # endif
288     vlc_bindtextdomain (PACKAGE_NAME);
289 #endif
290     /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
291     msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
292
293     if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
294     {
295         module_EndBank (true);
296         return VLC_EEXITSUCCESS;
297     }
298
299     if( module_count <= 1 )
300     {
301         msg_Err( p_libvlc, "No plugins found! Check your VLC installation.");
302         module_EndBank (true);
303         return VLC_ENOITEM;
304     }
305
306 #ifdef HAVE_DAEMON
307     /* Check for daemon mode */
308     if( var_InheritBool( p_libvlc, "daemon" ) )
309     {
310         char *psz_pidfile = NULL;
311
312         if( daemon( 1, 0) != 0 )
313         {
314             msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
315             module_EndBank (true);
316             return VLC_EEXIT;
317         }
318         b_daemon = true;
319
320         /* lets check if we need to write the pidfile */
321         psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
322         if( psz_pidfile != NULL )
323         {
324             FILE *pidfile;
325             pid_t i_pid = getpid ();
326             msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
327                                i_pid, psz_pidfile );
328             pidfile = vlc_fopen( psz_pidfile,"w" );
329             if( pidfile != NULL )
330             {
331                 utf8_fprintf( pidfile, "%d", (int)i_pid );
332                 fclose( pidfile );
333             }
334             else
335             {
336                 msg_Err( p_libvlc, "cannot open pid file for writing: %s (%m)",
337                          psz_pidfile );
338             }
339         }
340         free( psz_pidfile );
341     }
342 #endif
343
344 /* FIXME: could be replaced by using Unix sockets */
345 #ifdef HAVE_DBUS
346     dbus_threads_init_default();
347
348     if( var_InheritBool( p_libvlc, "one-instance" )
349     || ( var_InheritBool( p_libvlc, "one-instance-when-started-from-file" )
350       && var_InheritBool( p_libvlc, "started-from-file" ) ) )
351     {
352         /* Initialise D-Bus interface, check for other instances */
353         DBusConnection  *p_conn = NULL;
354         DBusError       dbus_error;
355
356         dbus_error_init( &dbus_error );
357
358         /* connect to the session bus */
359         p_conn = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
360         if( !p_conn )
361         {
362             msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
363                     dbus_error.message );
364             dbus_error_free( &dbus_error );
365         }
366         else
367         {
368             /* check if VLC is available on the bus
369              * if not: D-Bus control is not enabled on the other
370              * instance and we can't pass MRLs to it */
371             DBusMessage *p_test_msg   = NULL;
372             DBusMessage *p_test_reply = NULL;
373
374             p_test_msg =  dbus_message_new_method_call(
375                     "org.mpris.MediaPlayer2.vlc", "/org/mpris/MediaPlayer2",
376                     "org.freedesktop.DBus.Introspectable", "Introspect" );
377
378             /* block until a reply arrives */
379             p_test_reply = dbus_connection_send_with_reply_and_block(
380                     p_conn, p_test_msg, -1, &dbus_error );
381             dbus_message_unref( p_test_msg );
382             if( p_test_reply == NULL )
383             {
384                 dbus_error_free( &dbus_error );
385                 msg_Dbg( p_libvlc, "No Media Player is running. "
386                         "Continuing normally." );
387             }
388             else
389             {
390                 int i_input;
391                 DBusMessage* p_dbus_msg = NULL;
392                 DBusMessageIter dbus_args;
393                 DBusPendingCall* p_dbus_pending = NULL;
394                 dbus_bool_t b_play;
395
396                 dbus_message_unref( p_test_reply );
397                 msg_Warn( p_libvlc, "Another Media Player is running. Exiting");
398
399                 for( i_input = vlc_optind; i_input < i_argc;i_input++ )
400                 {
401                     /* Skip input options, we can't pass them through D-Bus */
402                     if( ppsz_argv[i_input][0] == ':' )
403                     {
404                         msg_Warn( p_libvlc, "Ignoring option %s",
405                                   ppsz_argv[i_input] );
406                         continue;
407                     }
408
409                     /* We need to resolve relative paths in this instance */
410                     char *psz_mrl = make_URI( ppsz_argv[i_input], NULL );
411                     const char *psz_after_track = "/";
412
413                     if( psz_mrl == NULL )
414                         continue;
415                     msg_Dbg( p_libvlc, "Adds %s to the running Media Player",
416                              psz_mrl );
417
418                     p_dbus_msg = dbus_message_new_method_call(
419                         "org.mpris.MediaPlayer2.vlc", "/org/mpris/MediaPlayer2",
420                         "org.mpris.MediaPlayer2.TrackList", "AddTrack" );
421
422                     if ( NULL == p_dbus_msg )
423                     {
424                         msg_Err( p_libvlc, "D-Bus problem" );
425                         free( psz_mrl );
426                         system_End( );
427                         exit( 1 );
428                     }
429
430                     /* append MRLs */
431                     dbus_message_iter_init_append( p_dbus_msg, &dbus_args );
432                     if ( !dbus_message_iter_append_basic( &dbus_args,
433                                 DBUS_TYPE_STRING, &psz_mrl ) )
434                     {
435                         dbus_message_unref( p_dbus_msg );
436                         free( psz_mrl );
437                         system_End( );
438                         exit( 1 );
439                     }
440                     free( psz_mrl );
441
442                     if( !dbus_message_iter_append_basic( &dbus_args,
443                                 DBUS_TYPE_OBJECT_PATH, &psz_after_track ) )
444                     {
445                         dbus_message_unref( p_dbus_msg );
446                         system_End( );
447                         exit( 1 );
448                     }
449
450                     b_play = TRUE;
451                     if( var_InheritBool( p_libvlc, "playlist-enqueue" ) )
452                         b_play = FALSE;
453
454                     if ( !dbus_message_iter_append_basic( &dbus_args,
455                                 DBUS_TYPE_BOOLEAN, &b_play ) )
456                     {
457                         dbus_message_unref( p_dbus_msg );
458                         system_End( );
459                         exit( 1 );
460                     }
461
462                     /* send message and get a handle for a reply */
463                     if ( !dbus_connection_send_with_reply ( p_conn,
464                                 p_dbus_msg, &p_dbus_pending, -1 ) )
465                     {
466                         msg_Err( p_libvlc, "D-Bus problem" );
467                         dbus_message_unref( p_dbus_msg );
468                         system_End( );
469                         exit( 1 );
470                     }
471
472                     if ( NULL == p_dbus_pending )
473                     {
474                         msg_Err( p_libvlc, "D-Bus problem" );
475                         dbus_message_unref( p_dbus_msg );
476                         system_End( );
477                         exit( 1 );
478                     }
479                     dbus_connection_flush( p_conn );
480                     dbus_message_unref( p_dbus_msg );
481                     /* block until we receive a reply */
482                     dbus_pending_call_block( p_dbus_pending );
483                     dbus_pending_call_unref( p_dbus_pending );
484                 } /* processes all command line MRLs */
485
486                 /* bye bye */
487                 system_End( );
488                 exit( 0 );
489             }
490         }
491         /* we unreference the connection when we've finished with it */
492         if( p_conn ) dbus_connection_unref( p_conn );
493     }
494 #endif
495
496     /*
497      * Message queue options
498      */
499     /* Last chance to set the verbosity. Once we start interfaces and other
500      * threads, verbosity becomes read-only. */
501     var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
502     if( var_InheritBool( p_libvlc, "quiet" ) )
503     {
504         var_SetInteger( p_libvlc, "verbose", -1 );
505         priv->i_verbose = -1;
506     }
507     vlc_threads_setup( p_libvlc );
508
509     if( priv->b_color )
510         priv->b_color = var_InheritBool( p_libvlc, "color" );
511
512     vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
513     /*
514      * Choose the best memcpy module
515      */
516     priv->p_memcpy_module = module_need( p_libvlc, "memcpy", "$memcpy", false );
517     /* Avoid being called "memcpy":*/
518     vlc_object_set_name( p_libvlc, "main" );
519
520     priv->b_stats = var_InheritBool( p_libvlc, "stats" );
521     priv->i_timers = 0;
522     priv->pp_timers = NULL;
523
524     /*
525      * Initialize hotkey handling
526      */
527     priv->actions = vlc_InitActions( p_libvlc );
528
529     /* Create a variable for showing the fullscreen interface */
530     var_Create( p_libvlc, "intf-toggle-fscontrol", VLC_VAR_BOOL );
531     var_SetBool( p_libvlc, "intf-toggle-fscontrol", true );
532
533     /* Create a variable for the Boss Key */
534     var_Create( p_libvlc, "intf-boss", VLC_VAR_VOID );
535
536     /* Create a variable for showing the main interface */
537     var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL );
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 #if defined (WIN32) || defined (__OS2__)
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
773     msg_Dbg( p_libvlc, "removing stats" );
774
775 #if !defined( WIN32 ) && !defined( __OS2__ )
776     char* psz_pidfile = NULL;
777
778     if( b_daemon )
779     {
780         psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
781         if( psz_pidfile != NULL )
782         {
783             msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
784             if( unlink( psz_pidfile ) == -1 )
785             {
786                 msg_Dbg( p_libvlc, "removing pid file %s: %m",
787                         psz_pidfile );
788             }
789         }
790         free( psz_pidfile );
791     }
792 #endif
793
794     if( priv->p_memcpy_module )
795     {
796         module_unneed( p_libvlc, priv->p_memcpy_module );
797         priv->p_memcpy_module = NULL;
798     }
799
800     /* Save the configuration */
801     if( !var_InheritBool( p_libvlc, "ignore-config" ) )
802         config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
803
804     /* Free module bank. It is refcounted, so we call this each time  */
805     module_EndBank (true);
806
807     vlc_DeinitActions( p_libvlc, priv->actions );
808 }
809
810 /**
811  * Destroy everything.
812  * This function requests the running threads to finish, waits for their
813  * termination, and destroys their structure.
814  * It stops the thread systems: no instance can run after this has run
815  * \param p_libvlc the instance to destroy
816  */
817 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
818 {
819     libvlc_priv_t *priv = libvlc_priv( p_libvlc );
820
821     system_End( );
822
823     /* Destroy mutexes */
824     vlc_ExitDestroy( &priv->exit );
825     vlc_mutex_destroy( &priv->timer_lock );
826     vlc_mutex_destroy( &priv->ml_lock );
827
828 #ifndef NDEBUG /* Hack to dump leaked objects tree */
829     if( vlc_internals( p_libvlc )->i_refcount > 1 )
830         while( vlc_internals( p_libvlc )->i_refcount > 0 )
831             vlc_object_release( p_libvlc );
832 #endif
833
834     assert( vlc_internals( p_libvlc )->i_refcount == 1 );
835     vlc_object_release( p_libvlc );
836 }
837
838 /**
839  * Add an interface plugin and run it
840  */
841 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc, char const *psz_module )
842 {
843     if( !p_libvlc )
844         return VLC_EGENERIC;
845
846     if( !psz_module ) /* requesting the default interface */
847     {
848         char *psz_interface = var_CreateGetNonEmptyString( p_libvlc, "intf" );
849         if( !psz_interface ) /* "intf" has not been set */
850         {
851 #if !defined( WIN32 ) && !defined( __OS2__ )
852             if( b_daemon )
853                  /* Daemon mode hack.
854                   * We prefer the dummy interface if none is specified. */
855                 psz_module = "dummy";
856             else
857 #endif
858                 msg_Info( p_libvlc, "%s",
859                           _("Running vlc with the default interface. "
860                             "Use 'cvlc' to use vlc without interface.") );
861         }
862         free( psz_interface );
863         var_Destroy( p_libvlc, "intf" );
864     }
865
866     /* Try to create the interface */
867     int ret = intf_Create( p_libvlc, psz_module ? psz_module : "$intf" );
868     if( ret )
869         msg_Err( p_libvlc, "interface \"%s\" initialization failed",
870                  psz_module ? psz_module : "default" );
871     return ret;
872 }
873
874 #if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
875     ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
876 /*****************************************************************************
877  * SetLanguage: set the interface language.
878  *****************************************************************************
879  * We set the LC_MESSAGES locale category for interface messages and buttons,
880  * as well as the LC_CTYPE category for string sorting and possible wide
881  * character support.
882  *****************************************************************************/
883 static void SetLanguage ( const char *psz_lang )
884 {
885 #ifdef __APPLE__
886     /* I need that under Darwin, please check it doesn't disturb
887      * other platforms. --Meuuh */
888     setenv( "LANG", psz_lang, 1 );
889
890 #else
891     /* We set LC_ALL manually because it is the only way to set
892      * the language at runtime under eg. Windows. Beware that this
893      * makes the environment unconsistent when libvlc is unloaded and
894      * should probably be moved to a safer place like vlc.c. */
895     setenv( "LC_ALL", psz_lang, 1 );
896
897 #endif
898
899     setlocale( LC_ALL, psz_lang );
900 }
901 #endif
902
903 /*****************************************************************************
904  * GetFilenames: parse command line options which are not flags
905  *****************************************************************************
906  * Parse command line for input files as well as their associated options.
907  * An option always follows its associated input and begins with a ":".
908  *****************************************************************************/
909 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
910                           const char *const args[] )
911 {
912     while( n > 0 )
913     {
914         /* Count the input options */
915         unsigned i_options = 0;
916
917         while( args[--n][0] == ':' )
918         {
919             i_options++;
920             if( n == 0 )
921             {
922                 msg_Warn( p_vlc, "options %s without item", args[n] );
923                 return; /* syntax!? */
924             }
925         }
926
927         char *mrl = make_URI( args[n], NULL );
928         if( !mrl )
929             continue;
930
931         playlist_AddExt( pl_Get( p_vlc ), mrl, NULL, PLAYLIST_INSERT,
932                 0, -1, i_options, ( i_options ? &args[n + 1] : NULL ),
933                 VLC_INPUT_OPTION_TRUSTED, true, pl_Unlocked );
934         free( mrl );
935     }
936 }