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