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