]> git.sesse.net Git - vlc/blob - src/libvlc.c
Win32: add --clock-source configuration item
[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_UNISTD_H
53 #   include <unistd.h> /* isatty() */
54 #endif
55
56 #ifdef HAVE_DBUS
57 /* used for one-instance mode */
58 #   include <dbus/dbus.h>
59 #endif
60
61
62 #include <vlc_media_library.h>
63 #include <vlc_playlist.h>
64 #include <vlc_interface.h>
65
66 #include <vlc_charset.h>
67 #include <vlc_fs.h>
68 #include <vlc_cpu.h>
69 #include <vlc_url.h>
70 #include <vlc_modules.h>
71
72 #include "libvlc.h"
73
74 #include "playlist/playlist_internal.h"
75
76 #include <vlc_vlm.h>
77
78 #ifdef __APPLE__
79 # include <libkern/OSAtomic.h>
80 #endif
81
82 #include <assert.h>
83
84 /*****************************************************************************
85  * The evil global variables. We handle them with care, don't worry.
86  *****************************************************************************/
87
88 #if !defined(WIN32) && !defined(__OS2__)
89 static bool b_daemon = false;
90 #endif
91
92 /*****************************************************************************
93  * Local prototypes
94  *****************************************************************************/
95 static void GetFilenames  ( libvlc_int_t *, unsigned, const char *const [] );
96
97 /**
98  * Allocate a libvlc instance, initialize global data if needed
99  * It also initializes the threading system
100  */
101 libvlc_int_t * libvlc_InternalCreate( void )
102 {
103     libvlc_int_t *p_libvlc;
104     libvlc_priv_t *priv;
105
106     /* Now that the thread system is initialized, we don't have much, but
107      * at least we have variables */
108     /* Allocate a libvlc instance object */
109     p_libvlc = vlc_custom_create( (vlc_object_t *)NULL, sizeof (*priv),
110                                   "libvlc" );
111     if( p_libvlc == NULL )
112         return NULL;
113
114     priv = libvlc_priv (p_libvlc);
115     priv->p_playlist = NULL;
116     priv->p_ml = NULL;
117     priv->p_dialog_provider = NULL;
118     priv->p_vlm = NULL;
119     priv->i_verbose = 3; /* initial value until config is loaded */
120 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
121     priv->b_color = isatty( STDERR_FILENO ); /* 2 is for stderr */
122 #else
123     priv->b_color = false;
124 #endif
125
126     /* Initialize mutexes */
127     vlc_mutex_init( &priv->ml_lock );
128     vlc_ExitInit( &priv->exit );
129
130     return p_libvlc;
131 }
132
133 /**
134  * Initialize a libvlc instance
135  * This function initializes a previously allocated libvlc instance:
136  *  - CPU detection
137  *  - gettext initialization
138  *  - message queue, module bank and playlist initialization
139  *  - configuration and commandline parsing
140  */
141 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
142                          const char *ppsz_argv[] )
143 {
144     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
145     char *       psz_modules = NULL;
146     char *       psz_parser = NULL;
147     char *       psz_control = NULL;
148     playlist_t  *p_playlist = NULL;
149     char        *psz_val;
150
151     /* System specific initialization code */
152     system_Init();
153
154     /* Initialize the module bank and load the configuration of the
155      * main module. We need to do this at this stage to be able to display
156      * a short help if required by the user. (short help == main module
157      * options) */
158     module_InitBank ();
159
160     /* Get command line options that affect module loading. */
161     if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
162     {
163         module_EndBank (false);
164         return VLC_EGENERIC;
165     }
166     priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
167
168     /* Find verbosity from VLC_VERBOSE environment variable */
169     {
170         char *env = getenv( "VLC_VERBOSE" );
171         if( env != NULL )
172             priv->i_verbose = atoi( env );
173     }
174
175     /* Announce who we are (TODO: only first instance?) */
176     msg_Dbg( p_libvlc, "VLC media player - %s", VERSION_MESSAGE );
177     msg_Dbg( p_libvlc, "%s", COPYRIGHT_MESSAGE );
178     msg_Dbg( p_libvlc, "revision %s", psz_vlc_changeset );
179     msg_Dbg( p_libvlc, "configured with %s", CONFIGURE_LINE );
180 #ifdef WIN32
181     SelectClockSource (VLC_OBJECT(p_libvlc));
182 #endif
183
184     /* Load the builtins and plugins into the module_bank.
185      * We have to do it before config_Load*() because this also gets the
186      * list of configuration options exported by each module and loads their
187      * default values. */
188     size_t module_count = module_LoadPlugins (p_libvlc);
189
190     /*
191      * Override default configuration with config file settings
192      */
193     if( !var_InheritBool( p_libvlc, "ignore-config" ) )
194     {
195         if( var_InheritBool( p_libvlc, "reset-config" ) )
196             config_SaveConfigFile( p_libvlc ); /* Save default config */
197         else
198             config_LoadConfigFile( p_libvlc );
199     }
200
201     /*
202      * Override configuration with command line settings
203      */
204     int vlc_optind;
205     if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
206     {
207 #ifdef WIN32
208         MessageBox (NULL, TEXT("The command line options could not be parsed.\n"
209                     "Make sure they are valid."), TEXT("VLC media player"),
210                     MB_OK|MB_ICONERROR);
211 #endif
212         module_EndBank (true);
213         return VLC_EGENERIC;
214     }
215     priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
216
217     /*
218      * Support for gettext
219      */
220 #if defined( ENABLE_NLS ) \
221      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
222     vlc_bindtextdomain (PACKAGE_NAME);
223 #endif
224     /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
225     msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
226
227     if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
228     {
229         module_EndBank (true);
230         return VLC_EEXITSUCCESS;
231     }
232
233     if( module_count <= 1 )
234     {
235         msg_Err( p_libvlc, "No plugins found! Check your VLC installation.");
236         module_EndBank (true);
237         return VLC_ENOITEM;
238     }
239
240 #ifdef HAVE_DAEMON
241     /* Check for daemon mode */
242     if( var_InheritBool( p_libvlc, "daemon" ) )
243     {
244         char *psz_pidfile = NULL;
245
246         if( daemon( 1, 0) != 0 )
247         {
248             msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
249             module_EndBank (true);
250             return VLC_EEXIT;
251         }
252         b_daemon = true;
253
254         /* lets check if we need to write the pidfile */
255         psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
256         if( psz_pidfile != NULL )
257         {
258             FILE *pidfile;
259             pid_t i_pid = getpid ();
260             msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
261                                i_pid, psz_pidfile );
262             pidfile = vlc_fopen( psz_pidfile,"w" );
263             if( pidfile != NULL )
264             {
265                 utf8_fprintf( pidfile, "%d", (int)i_pid );
266                 fclose( pidfile );
267             }
268             else
269             {
270                 msg_Err( p_libvlc, "cannot open pid file for writing: %s (%m)",
271                          psz_pidfile );
272             }
273         }
274         free( psz_pidfile );
275     }
276 #endif
277
278 /* FIXME: could be replaced by using Unix sockets */
279 #ifdef HAVE_DBUS
280
281 #define MPRIS_APPEND "/org/mpris/MediaPlayer2/TrackList/Append"
282 #define MPRIS_BUS_NAME "org.mpris.MediaPlayer2.vlc"
283 #define MPRIS_OBJECT_PATH "/org/mpris/MediaPlayer2"
284 #define MPRIS_TRACKLIST_INTERFACE "org.mpris.MediaPlayer2.TrackList"
285
286     if( var_InheritBool( p_libvlc, "one-instance" )
287     || ( var_InheritBool( p_libvlc, "one-instance-when-started-from-file" )
288       && var_InheritBool( p_libvlc, "started-from-file" ) ) )
289     {
290         for( int i = vlc_optind; i < i_argc; i++ )
291             if( ppsz_argv[i][0] == ':' )
292             {
293                 msg_Err( p_libvlc, "item option %s incompatible with single instance",
294                          ppsz_argv[i] );
295                 goto dbus_out;
296             }
297
298         /* Initialise D-Bus interface, check for other instances */
299         dbus_threads_init_default();
300
301         DBusError err;
302         dbus_error_init( &err );
303
304         /* connect to the session bus */
305         DBusConnection  *conn = dbus_bus_get( DBUS_BUS_SESSION, &err );
306         if( conn == NULL )
307         {
308             msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
309                     err.message );
310             dbus_error_free( &err );
311             goto dbus_out;
312         }
313
314         /* check if VLC is available on the bus
315          * if not: D-Bus control is not enabled on the other
316          * instance and we can't pass MRLs to it */
317         /* FIXME: This check is totally brain-dead and buggy. */
318         if( !dbus_bus_name_has_owner( conn, MPRIS_BUS_NAME, &err ) )
319         {
320             dbus_connection_unref( conn );
321             if( dbus_error_is_set( &err ) )
322             {
323                 msg_Err( p_libvlc, "D-Bus error: %s", err.message );
324             }
325             else
326                 msg_Dbg( p_libvlc, "No media player running. Continuing normally." );
327             dbus_error_free( &err );
328             goto dbus_out;
329         }
330
331         const dbus_bool_t play = !var_InheritBool( p_libvlc, "playlist-enqueue" );
332
333         msg_Warn( p_libvlc, "media player running. Exiting...");
334         for( int i = vlc_optind; i < i_argc; i++ )
335         {
336             DBusMessage *msg = dbus_message_new_method_call(
337                MPRIS_BUS_NAME, MPRIS_OBJECT_PATH, MPRIS_TRACKLIST_INTERFACE, "AddTrack" );
338             if( unlikely(msg == NULL) )
339                 continue;
340
341             /* We need to resolve relative paths in this instance */
342             char *mrl;
343             if( strstr( ppsz_argv[i], "://" ) )
344                 mrl = strdup( ppsz_argv[i] );
345             else
346                 mrl = vlc_path2uri( ppsz_argv[i], NULL );
347             if( mrl == NULL )
348             {
349                 dbus_message_unref( msg );
350                 continue;
351             }
352
353             const char *after_track = MPRIS_APPEND;
354
355             /* append MRLs */
356             if( !dbus_message_append_args( msg, DBUS_TYPE_STRING, &mrl,
357                                                 DBUS_TYPE_STRING, &after_track,
358                                                 DBUS_TYPE_BOOLEAN, &play,
359                                                 DBUS_TYPE_INVALID ) )
360             {
361                  dbus_message_unref( msg );
362                  msg = NULL;
363             }
364             free( mrl );
365             if( unlikely(msg == NULL) )
366                 continue;
367
368             msg_Dbg( p_libvlc, "Adds %s to the running media player", mrl );
369
370             /* send message and get a handle for a reply */
371             DBusMessage *reply = dbus_connection_send_with_reply_and_block( conn, msg, -1,
372                                                                             &err );
373             dbus_message_unref( msg );
374             if( reply == NULL )
375             {
376                 msg_Err( p_libvlc, "D-Bus error: %s", err.message );
377                 continue;
378             }
379             dbus_message_unref( reply );
380         }
381         /* we unreference the connection when we've finished with it */
382         dbus_connection_unref( conn );
383         exit( 1 );
384     }
385 #undef MPRIS_APPEND
386 #undef MPRIS_BUS_NAME
387 #undef MPRIS_OBJECT_PATH
388 #undef MPRIS_TRACKLIST_INTERFACE
389 dbus_out:
390 #endif // HAVE_DBUS
391
392     /*
393      * Message queue options
394      */
395     /* Last chance to set the verbosity. Once we start interfaces and other
396      * threads, verbosity becomes read-only. */
397     var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
398     if( var_InheritBool( p_libvlc, "quiet" ) )
399     {
400         var_SetInteger( p_libvlc, "verbose", -1 );
401         priv->i_verbose = -1;
402     }
403     vlc_threads_setup( p_libvlc );
404
405     if( priv->b_color )
406         priv->b_color = var_InheritBool( p_libvlc, "color" );
407
408     vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
409     vlc_object_set_name( p_libvlc, "main" );
410
411     priv->b_stats = var_InheritBool( p_libvlc, "stats" );
412
413     /*
414      * Initialize hotkey handling
415      */
416     priv->actions = vlc_InitActions( p_libvlc );
417
418     /* Create a variable for showing the fullscreen interface */
419     var_Create( p_libvlc, "intf-toggle-fscontrol", VLC_VAR_BOOL );
420     var_SetBool( p_libvlc, "intf-toggle-fscontrol", true );
421
422     /* Create a variable for the Boss Key */
423     var_Create( p_libvlc, "intf-boss", VLC_VAR_VOID );
424
425     /* Create a variable for showing the main interface */
426     var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL );
427
428     /* Create a variable for showing the right click menu */
429     var_Create( p_libvlc, "intf-popupmenu", VLC_VAR_BOOL );
430
431     /* variables for signalling creation of new files */
432     var_Create( p_libvlc, "snapshot-file", VLC_VAR_STRING );
433     var_Create( p_libvlc, "record-file", VLC_VAR_STRING );
434
435     /* some default internal settings */
436     var_Create( p_libvlc, "window", VLC_VAR_STRING );
437     var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
438     var_SetString( p_libvlc, "user-agent", "(LibVLC "VERSION")" );
439
440     /* Initialize playlist and get commandline files */
441     p_playlist = playlist_Create( VLC_OBJECT(p_libvlc) );
442     if( !p_playlist )
443     {
444         msg_Err( p_libvlc, "playlist initialization failed" );
445         module_EndBank (true);
446         return VLC_EGENERIC;
447     }
448
449     /* System specific configuration */
450     system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
451
452 #if defined(MEDIA_LIBRARY)
453     /* Get the ML */
454     if( var_GetBool( p_libvlc, "load-media-library-on-startup" ) )
455     {
456         priv->p_ml = ml_Create( VLC_OBJECT( p_libvlc ), NULL );
457         if( !priv->p_ml )
458         {
459             msg_Err( p_libvlc, "ML initialization failed" );
460             return VLC_EGENERIC;
461         }
462     }
463     else
464     {
465         priv->p_ml = NULL;
466     }
467 #endif
468
469     /* Add service discovery modules */
470     psz_modules = var_InheritString( p_libvlc, "services-discovery" );
471     if( psz_modules )
472     {
473         char *p = psz_modules, *m;
474         while( ( m = strsep( &p, " :," ) ) != NULL )
475             playlist_ServicesDiscoveryAdd( p_playlist, m );
476         free( psz_modules );
477     }
478
479 #ifdef ENABLE_VLM
480     /* Initialize VLM if vlm-conf is specified */
481     psz_parser = var_CreateGetNonEmptyString( p_libvlc, "vlm-conf" );
482     if( psz_parser )
483     {
484         priv->p_vlm = vlm_New( p_libvlc );
485         if( !priv->p_vlm )
486             msg_Err( p_libvlc, "VLM initialization failed" );
487     }
488     free( psz_parser );
489 #endif
490
491     /*
492      * Load background interfaces
493      */
494     psz_modules = var_CreateGetNonEmptyString( p_libvlc, "extraintf" );
495     psz_control = var_CreateGetNonEmptyString( p_libvlc, "control" );
496
497     if( psz_modules && psz_control )
498     {
499         char* psz_tmp;
500         if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
501         {
502             free( psz_modules );
503             psz_modules = psz_tmp;
504         }
505     }
506     else if( psz_control )
507     {
508         free( psz_modules );
509         psz_modules = strdup( psz_control );
510     }
511
512     psz_parser = psz_modules;
513     while ( psz_parser && *psz_parser )
514     {
515         char *psz_module, *psz_temp;
516         psz_module = psz_parser;
517         psz_parser = strchr( psz_module, ':' );
518         if ( psz_parser )
519         {
520             *psz_parser = '\0';
521             psz_parser++;
522         }
523         if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
524         {
525             intf_Create( p_libvlc, psz_temp );
526             free( psz_temp );
527         }
528     }
529     free( psz_modules );
530     free( psz_control );
531
532     /*
533      * Always load the hotkeys interface if it exists
534      */
535     intf_Create( p_libvlc, "hotkeys,none" );
536
537     if( var_InheritBool( p_libvlc, "file-logging" )
538 #ifdef HAVE_SYSLOG_H
539         && !var_InheritBool( p_libvlc, "syslog" )
540 #endif
541         )
542     {
543         intf_Create( p_libvlc, "logger,none" );
544     }
545 #ifdef HAVE_SYSLOG_H
546     if( var_InheritBool( p_libvlc, "syslog" ) )
547     {
548         char *logmode = var_CreateGetNonEmptyString( p_libvlc, "logmode" );
549         var_SetString( p_libvlc, "logmode", "syslog" );
550         intf_Create( p_libvlc, "logger,none" );
551
552         if( logmode )
553         {
554             var_SetString( p_libvlc, "logmode", logmode );
555             free( logmode );
556         }
557         var_Destroy( p_libvlc, "logmode" );
558     }
559 #endif
560
561     if( var_InheritBool( p_libvlc, "network-synchronisation") )
562     {
563         intf_Create( p_libvlc, "netsync,none" );
564     }
565
566 #ifdef __APPLE__
567     var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
568     var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
569     var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
570     var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
571     var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
572     var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
573     var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
574     var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
575     var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
576 #endif
577 #if defined (WIN32) || defined (__OS2__)
578     var_Create( p_libvlc, "drawable-hwnd", VLC_VAR_INTEGER );
579 #endif
580
581     /*
582      * Get input filenames given as commandline arguments.
583      * We assume that the remaining parameters are filenames
584      * and their input options.
585      */
586     GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
587
588     /*
589      * Get --open argument
590      */
591     psz_val = var_InheritString( p_libvlc, "open" );
592     if ( psz_val != NULL )
593     {
594         playlist_AddExt( p_playlist, psz_val, NULL, PLAYLIST_INSERT, 0,
595                          -1, 0, NULL, 0, true, pl_Unlocked );
596         free( psz_val );
597     }
598
599     return VLC_SUCCESS;
600 }
601
602 /**
603  * Cleanup a libvlc instance. The instance is not completely deallocated
604  * \param p_libvlc the instance to clean
605  */
606 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
607 {
608     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
609     playlist_t    *p_playlist = libvlc_priv (p_libvlc)->p_playlist;
610
611     /* Deactivate the playlist */
612     msg_Dbg( p_libvlc, "deactivating the playlist" );
613     pl_Deactivate( p_libvlc );
614
615     /* Remove all services discovery */
616     msg_Dbg( p_libvlc, "removing all services discovery tasks" );
617     playlist_ServicesDiscoveryKillAll( p_playlist );
618
619     /* Ask the interfaces to stop and destroy them */
620     msg_Dbg( p_libvlc, "removing all interfaces" );
621     libvlc_Quit( p_libvlc );
622     intf_DestroyAll( p_libvlc );
623
624 #ifdef ENABLE_VLM
625     /* Destroy VLM if created in libvlc_InternalInit */
626     if( priv->p_vlm )
627     {
628         vlm_Delete( priv->p_vlm );
629     }
630 #endif
631
632 #if defined(MEDIA_LIBRARY)
633     media_library_t* p_ml = priv->p_ml;
634     if( p_ml )
635     {
636         ml_Destroy( VLC_OBJECT( p_ml ) );
637         vlc_object_release( p_ml );
638         libvlc_priv(p_playlist->p_libvlc)->p_ml = NULL;
639     }
640 #endif
641
642     /* Free playlist now, all threads are gone */
643     playlist_Destroy( p_playlist );
644
645     msg_Dbg( p_libvlc, "removing stats" );
646
647 #if !defined( WIN32 ) && !defined( __OS2__ )
648     char* psz_pidfile = NULL;
649
650     if( b_daemon )
651     {
652         psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
653         if( psz_pidfile != NULL )
654         {
655             msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
656             if( unlink( psz_pidfile ) == -1 )
657             {
658                 msg_Dbg( p_libvlc, "removing pid file %s: %m",
659                         psz_pidfile );
660             }
661         }
662         free( psz_pidfile );
663     }
664 #endif
665
666     /* Save the configuration */
667     if( !var_InheritBool( p_libvlc, "ignore-config" ) )
668         config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
669
670     /* Free module bank. It is refcounted, so we call this each time  */
671     module_EndBank (true);
672
673     vlc_DeinitActions( p_libvlc, priv->actions );
674 #ifdef WIN32
675     system_End( );
676 #endif
677 }
678
679 /**
680  * Destroy everything.
681  * This function requests the running threads to finish, waits for their
682  * termination, and destroys their structure.
683  * It stops the thread systems: no instance can run after this has run
684  * \param p_libvlc the instance to destroy
685  */
686 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
687 {
688     libvlc_priv_t *priv = libvlc_priv( p_libvlc );
689
690     /* Destroy mutexes */
691     vlc_ExitDestroy( &priv->exit );
692     vlc_mutex_destroy( &priv->ml_lock );
693
694 #ifndef NDEBUG /* Hack to dump leaked objects tree */
695     if( vlc_internals( p_libvlc )->i_refcount > 1 )
696         while( vlc_internals( p_libvlc )->i_refcount > 0 )
697             vlc_object_release( p_libvlc );
698 #endif
699
700     assert( vlc_internals( p_libvlc )->i_refcount == 1 );
701     vlc_object_release( p_libvlc );
702 }
703
704 /**
705  * Add an interface plugin and run it
706  */
707 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc, char const *psz_module )
708 {
709     if( !p_libvlc )
710         return VLC_EGENERIC;
711
712     if( !psz_module ) /* requesting the default interface */
713     {
714         char *psz_interface = var_CreateGetNonEmptyString( p_libvlc, "intf" );
715         if( !psz_interface ) /* "intf" has not been set */
716         {
717 #if !defined( WIN32 ) && !defined( __OS2__ )
718             if( b_daemon )
719                  /* Daemon mode hack.
720                   * We prefer the dummy interface if none is specified. */
721                 psz_module = "dummy";
722             else
723 #endif
724                 msg_Info( p_libvlc, "%s",
725                           _("Running vlc with the default interface. "
726                             "Use 'cvlc' to use vlc without interface.") );
727         }
728         free( psz_interface );
729         var_Destroy( p_libvlc, "intf" );
730     }
731
732     /* Try to create the interface */
733     int ret = intf_Create( p_libvlc, psz_module ? psz_module : "$intf" );
734     if( ret )
735         msg_Err( p_libvlc, "interface \"%s\" initialization failed",
736                  psz_module ? psz_module : "default" );
737     return ret;
738 }
739
740 /*****************************************************************************
741  * GetFilenames: parse command line options which are not flags
742  *****************************************************************************
743  * Parse command line for input files as well as their associated options.
744  * An option always follows its associated input and begins with a ":".
745  *****************************************************************************/
746 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
747                           const char *const args[] )
748 {
749     while( n > 0 )
750     {
751         /* Count the input options */
752         unsigned i_options = 0;
753
754         while( args[--n][0] == ':' )
755         {
756             i_options++;
757             if( n == 0 )
758             {
759                 msg_Warn( p_vlc, "options %s without item", args[n] );
760                 return; /* syntax!? */
761             }
762         }
763
764         char *mrl = NULL;
765         if( strstr( args[n], "://" ) == NULL )
766         {
767             mrl = vlc_path2uri( args[n], NULL );
768             if( !mrl )
769                 continue;
770         }
771
772         playlist_AddExt( pl_Get( p_vlc ), (mrl != NULL) ? mrl : args[n], NULL,
773                          PLAYLIST_INSERT, 0, -1, i_options,
774                          ( i_options ? &args[n + 1] : NULL ),
775                          VLC_INPUT_OPTION_TRUSTED, true, pl_Unlocked );
776         free( mrl );
777     }
778 }