1 /*****************************************************************************
2 * libvlc.c: libvlc instances creation and deletion, interfaces handling
3 *****************************************************************************
4 * Copyright (C) 1998-2008 VLC authors and VideoLAN
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>
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.
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.
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 *****************************************************************************/
29 * This file contains functions to create and destroy libvlc instances
32 /*****************************************************************************
34 *****************************************************************************/
39 #include <vlc_common.h>
40 #include "../lib/libvlc_internal.h"
41 #include <vlc_input.h>
43 #include "modules/modules.h"
44 #include "config/configuration.h"
45 #include "playlist/preparser.h"
47 #include <stdio.h> /* sprintf() */
49 #include <stdlib.h> /* free() */
52 #include "config/vlc_getopt.h"
54 #include <vlc_playlist.h>
55 #include <vlc_interface.h>
57 #include <vlc_actions.h>
58 #include <vlc_charset.h>
59 #include <vlc_dialog.h>
60 #include <vlc_keystore.h>
64 #include <vlc_modules.h>
67 #include "playlist/playlist_internal.h"
68 #include "misc/variables.h"
74 /*****************************************************************************
76 *****************************************************************************/
77 static void GetFilenames ( libvlc_int_t
*, unsigned, const char *const [] );
80 * Allocate a blank libvlc instance, also setting the exit handler.
81 * Vlc's threading system must have been initialized first
83 libvlc_int_t
* libvlc_InternalCreate( void )
85 libvlc_int_t
*p_libvlc
;
88 /* Allocate a libvlc instance object */
89 p_libvlc
= (vlc_custom_create
)( NULL
, sizeof (*priv
), "libvlc" );
90 if( p_libvlc
== NULL
)
93 priv
= libvlc_priv (p_libvlc
);
94 priv
->playlist
= NULL
;
97 vlc_ExitInit( &priv
->exit
);
103 * Initialize a libvlc instance
104 * This function initializes a previously allocated libvlc instance:
106 * - gettext initialization
107 * - message queue, module bank and playlist initialization
108 * - configuration and commandline parsing
110 int libvlc_InternalInit( libvlc_int_t
*p_libvlc
, int i_argc
,
111 const char *ppsz_argv
[] )
113 libvlc_priv_t
*priv
= libvlc_priv (p_libvlc
);
114 char * psz_modules
= NULL
;
115 char * psz_parser
= NULL
;
116 char * psz_control
= NULL
;
118 int i_ret
= VLC_EGENERIC
;
120 /* System specific initialization code */
123 vlc_LogPreinit(p_libvlc
);
125 /* Initialize the module bank and load the configuration of the
126 * core module. We need to do this at this stage to be able to display
127 * a short help if required by the user. (short help == core module
131 /* Get command line options that affect module loading. */
132 if( config_LoadCmdLine( p_libvlc
, i_argc
, ppsz_argv
, NULL
) )
134 module_EndBank (false);
138 vlc_threads_setup (p_libvlc
);
140 /* Load the builtins and plugins into the module_bank.
141 * We have to do it before config_Load*() because this also gets the
142 * list of configuration options exported by each module and loads their
144 size_t module_count
= module_LoadPlugins (p_libvlc
);
147 * Override default configuration with config file settings
149 if( !var_InheritBool( p_libvlc
, "ignore-config" ) )
151 if( var_InheritBool( p_libvlc
, "reset-config" ) )
152 config_SaveConfigFile( p_libvlc
); /* Save default config */
154 config_LoadConfigFile( p_libvlc
);
158 * Override configuration with command line settings
161 if( config_LoadCmdLine( p_libvlc
, i_argc
, ppsz_argv
, &vlc_optind
) )
164 vlc_LogInit(p_libvlc
);
167 * Support for gettext
169 #if defined( ENABLE_NLS ) \
170 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
171 vlc_bindtextdomain (PACKAGE_NAME
);
173 /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
174 msg_Dbg( p_libvlc
, "translation test: code is \"%s\"", _("C") );
176 if (config_PrintHelp (VLC_OBJECT(p_libvlc
)))
178 libvlc_InternalCleanup (p_libvlc
);
182 if( module_count
<= 1 )
184 msg_Err( p_libvlc
, "No plugins found! Check your VLC installation.");
190 /* Check for daemon mode */
191 if( var_InheritBool( p_libvlc
, "daemon" ) )
193 if( daemon( 1, 0) != 0 )
195 msg_Err( p_libvlc
, "Unable to fork vlc to daemon mode" );
199 /* lets check if we need to write the pidfile */
200 char *pidfile
= var_InheritString( p_libvlc
, "pidfile" );
201 if( pidfile
!= NULL
)
203 FILE *stream
= vlc_fopen( pidfile
, "w" );
206 fprintf( stream
, "%d", (int)getpid() );
208 msg_Dbg( p_libvlc
, "written PID file %s", pidfile
);
211 msg_Err( p_libvlc
, "cannot write PID file %s: %s",
212 pidfile
, vlc_strerror_c(errno
) );
218 var_Create( p_libvlc
, "pidfile", VLC_VAR_STRING
);
219 var_SetString( p_libvlc
, "pidfile", "" );
225 if( libvlc_InternalDialogInit( p_libvlc
) != VLC_SUCCESS
)
227 if( libvlc_InternalKeystoreInit( p_libvlc
) != VLC_SUCCESS
)
228 msg_Warn( p_libvlc
, "memory keystore init failed" );
230 vlc_CPU_dump( VLC_OBJECT(p_libvlc
) );
233 * Initialize hotkey handling
235 if( libvlc_InternalActionsInit( p_libvlc
) != VLC_SUCCESS
)
241 priv
->parser
= playlist_preparser_New(VLC_OBJECT(p_libvlc
));
245 /* Create a variable for showing the fullscreen interface */
246 var_Create( p_libvlc
, "intf-toggle-fscontrol", VLC_VAR_BOOL
);
247 var_SetBool( p_libvlc
, "intf-toggle-fscontrol", true );
249 /* Create a variable for the Boss Key */
250 var_Create( p_libvlc
, "intf-boss", VLC_VAR_VOID
);
252 /* Create a variable for showing the main interface */
253 var_Create( p_libvlc
, "intf-show", VLC_VAR_BOOL
);
255 /* Create a variable for showing the right click menu */
256 var_Create( p_libvlc
, "intf-popupmenu", VLC_VAR_BOOL
);
258 /* variables for signalling creation of new files */
259 var_Create( p_libvlc
, "snapshot-file", VLC_VAR_STRING
);
260 var_Create( p_libvlc
, "record-file", VLC_VAR_STRING
);
262 /* some default internal settings */
263 var_Create( p_libvlc
, "window", VLC_VAR_STRING
);
264 /* NOTE: Because the playlist and interfaces start before this function
265 * returns control to the application (DESIGN BUG!), all these variables
266 * must be created (in place of libvlc_new()) and set to VLC defaults
267 * (in place of VLC main()) *here*. */
268 var_Create( p_libvlc
, "user-agent", VLC_VAR_STRING
);
269 var_SetString( p_libvlc
, "user-agent",
270 "VLC media player (LibVLC "VERSION
")" );
271 var_Create( p_libvlc
, "http-user-agent", VLC_VAR_STRING
);
272 var_SetString( p_libvlc
, "http-user-agent",
273 "VLC/"PACKAGE_VERSION
" LibVLC/"PACKAGE_VERSION
);
274 var_Create( p_libvlc
, "app-icon-name", VLC_VAR_STRING
);
275 var_SetString( p_libvlc
, "app-icon-name", PACKAGE_NAME
);
276 var_Create( p_libvlc
, "app-id", VLC_VAR_STRING
);
277 var_SetString( p_libvlc
, "app-id", "org.VideoLAN.VLC" );
278 var_Create( p_libvlc
, "app-version", VLC_VAR_STRING
);
279 var_SetString( p_libvlc
, "app-version", PACKAGE_VERSION
);
281 /* System specific configuration */
282 system_Configure( p_libvlc
, i_argc
- vlc_optind
, ppsz_argv
+ vlc_optind
);
285 /* Initialize VLM if vlm-conf is specified */
286 psz_parser
= var_CreateGetNonEmptyString( p_libvlc
, "vlm-conf" );
289 priv
->p_vlm
= vlm_New( p_libvlc
);
291 msg_Err( p_libvlc
, "VLM initialization failed" );
297 * Load background interfaces
299 psz_modules
= var_CreateGetNonEmptyString( p_libvlc
, "extraintf" );
300 psz_control
= var_CreateGetNonEmptyString( p_libvlc
, "control" );
302 if( psz_modules
&& psz_control
)
305 if( asprintf( &psz_tmp
, "%s:%s", psz_modules
, psz_control
) != -1 )
308 psz_modules
= psz_tmp
;
311 else if( psz_control
)
314 psz_modules
= strdup( psz_control
);
317 psz_parser
= psz_modules
;
318 while ( psz_parser
&& *psz_parser
)
320 char *psz_module
, *psz_temp
;
321 psz_module
= psz_parser
;
322 psz_parser
= strchr( psz_module
, ':' );
328 if( asprintf( &psz_temp
, "%s,none", psz_module
) != -1)
330 libvlc_InternalAddIntf( p_libvlc
, psz_temp
);
337 if( var_InheritBool( p_libvlc
, "network-synchronisation") )
338 libvlc_InternalAddIntf( p_libvlc
, "netsync,none" );
341 var_Create( p_libvlc
, "drawable-view-top", VLC_VAR_INTEGER
);
342 var_Create( p_libvlc
, "drawable-view-left", VLC_VAR_INTEGER
);
343 var_Create( p_libvlc
, "drawable-view-bottom", VLC_VAR_INTEGER
);
344 var_Create( p_libvlc
, "drawable-view-right", VLC_VAR_INTEGER
);
345 var_Create( p_libvlc
, "drawable-clip-top", VLC_VAR_INTEGER
);
346 var_Create( p_libvlc
, "drawable-clip-left", VLC_VAR_INTEGER
);
347 var_Create( p_libvlc
, "drawable-clip-bottom", VLC_VAR_INTEGER
);
348 var_Create( p_libvlc
, "drawable-clip-right", VLC_VAR_INTEGER
);
349 var_Create( p_libvlc
, "drawable-nsobject", VLC_VAR_ADDRESS
);
353 * Get input filenames given as commandline arguments.
354 * We assume that the remaining parameters are filenames
355 * and their input options.
357 GetFilenames( p_libvlc
, i_argc
- vlc_optind
, ppsz_argv
+ vlc_optind
);
360 * Get --open argument
362 psz_val
= var_InheritString( p_libvlc
, "open" );
363 if ( psz_val
!= NULL
)
365 intf_InsertItem( p_libvlc
, psz_val
, 0, NULL
, 0 );
372 libvlc_InternalCleanup( p_libvlc
);
377 * Cleanup a libvlc instance. The instance is not completely deallocated
378 * \param p_libvlc the instance to clean
380 void libvlc_InternalCleanup( libvlc_int_t
*p_libvlc
)
382 libvlc_priv_t
*priv
= libvlc_priv (p_libvlc
);
384 if (priv
->parser
!= NULL
)
385 playlist_preparser_Deactivate(priv
->parser
);
387 /* Ask the interfaces to stop and destroy them */
388 msg_Dbg( p_libvlc
, "removing all interfaces" );
389 intf_DestroyAll( p_libvlc
);
391 libvlc_InternalDialogClean( p_libvlc
);
392 libvlc_InternalKeystoreClean( p_libvlc
);
395 /* Destroy VLM if created in libvlc_InternalInit */
398 vlm_Delete( priv
->p_vlm
);
402 #if !defined( _WIN32 ) && !defined( __OS2__ )
403 char *pidfile
= var_InheritString( p_libvlc
, "pidfile" );
404 if( pidfile
!= NULL
)
406 msg_Dbg( p_libvlc
, "removing PID file %s", pidfile
);
407 if( unlink( pidfile
) )
408 msg_Warn( p_libvlc
, "cannot remove PID file %s: %s",
409 pidfile
, vlc_strerror_c(errno
) );
414 if (priv
->parser
!= NULL
)
415 playlist_preparser_Delete(priv
->parser
);
417 libvlc_InternalActionsClean( p_libvlc
);
419 /* Save the configuration */
420 if( !var_InheritBool( p_libvlc
, "ignore-config" ) )
421 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc
) );
423 /* Free module bank. It is refcounted, so we call this each time */
424 vlc_LogDeinit (p_libvlc
);
425 module_EndBank (true);
426 #if defined(_WIN32) || defined(__OS2__)
432 * Destroy everything.
433 * This function requests the running threads to finish, waits for their
434 * termination, and destroys their structure.
435 * It stops the thread systems: no instance can run after this has run
436 * \param p_libvlc the instance to destroy
438 void libvlc_InternalDestroy( libvlc_int_t
*p_libvlc
)
440 libvlc_priv_t
*priv
= libvlc_priv( p_libvlc
);
442 vlc_ExitDestroy( &priv
->exit
);
444 assert( atomic_load(&(vlc_internals(p_libvlc
)->refs
)) == 1 );
445 vlc_object_release( p_libvlc
);
448 /*****************************************************************************
449 * GetFilenames: parse command line options which are not flags
450 *****************************************************************************
451 * Parse command line for input files as well as their associated options.
452 * An option always follows its associated input and begins with a ":".
453 *****************************************************************************/
454 static void GetFilenames( libvlc_int_t
*p_vlc
, unsigned n
,
455 const char *const args
[] )
459 /* Count the input options */
460 unsigned i_options
= 0;
462 while( args
[--n
][0] == ':' )
467 msg_Warn( p_vlc
, "options %s without item", args
[n
] );
468 return; /* syntax!? */
473 if( strstr( args
[n
], "://" ) == NULL
)
475 mrl
= vlc_path2uri( args
[n
], NULL
);
480 intf_InsertItem( p_vlc
, (mrl
!= NULL
) ? mrl
: args
[n
], i_options
,
481 ( i_options
? &args
[n
+ 1] : NULL
),
482 VLC_INPUT_OPTION_TRUSTED
);
488 * Requests extraction of the meta data for an input item (a.k.a. preparsing).
489 * The actual extraction is asynchronous. It can be cancelled with
490 * libvlc_MetadataCancel()
492 int libvlc_MetadataRequest(libvlc_int_t
*libvlc
, input_item_t
*item
,
493 input_item_meta_request_option_t i_options
,
494 int timeout
, void *id
)
496 libvlc_priv_t
*priv
= libvlc_priv(libvlc
);
498 if (unlikely(priv
->parser
== NULL
))
501 vlc_mutex_lock( &item
->lock
);
502 if( item
->i_preparse_depth
== 0 )
503 item
->i_preparse_depth
= 1;
504 if( i_options
& META_REQUEST_OPTION_DO_INTERACT
)
505 item
->b_preparse_interact
= true;
506 vlc_mutex_unlock( &item
->lock
);
507 playlist_preparser_Push( priv
->parser
, item
, i_options
, timeout
, id
);
512 * Requests retrieving/downloading art for an input item.
513 * The retrieval is performed asynchronously.
515 int libvlc_ArtRequest(libvlc_int_t
*libvlc
, input_item_t
*item
,
516 input_item_meta_request_option_t i_options
)
518 libvlc_priv_t
*priv
= libvlc_priv(libvlc
);
520 if (unlikely(priv
->parser
== NULL
))
523 playlist_preparser_fetcher_Push(priv
->parser
, item
, i_options
);
528 * Cancels extraction of the meta data for an input item.
530 * This does nothing if the input item is already processed or if it was not
531 * added with libvlc_MetadataRequest()
533 void libvlc_MetadataCancel(libvlc_int_t
*libvlc
, void *id
)
535 libvlc_priv_t
*priv
= libvlc_priv(libvlc
);
537 if (unlikely(priv
->parser
== NULL
))
540 playlist_preparser_Cancel(priv
->parser
, id
);