qml: restore the focus on the last album when navigating back to the album view
[vlc.git] / src / libvlc.c
blob2d370deab83941b2e8398f78ceb3050918630aa0
1 /*****************************************************************************
2 * libvlc.c: libvlc instances creation and deletion, interfaces handling
3 *****************************************************************************
4 * Copyright (C) 1998-2008 VLC authors and VideoLAN
6 * Authors: Vincent Seguin <seguin@via.ecp.fr>
7 * Samuel Hocevar <sam@zoy.org>
8 * Gildas Bazin <gbazin@videolan.org>
9 * Derk-Jan Hartman <hartman at videolan dot org>
10 * RĂ©mi Denis-Courmont
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /** \file
28 * This file contains functions to create and destroy libvlc instances
31 /*****************************************************************************
32 * Preamble
33 *****************************************************************************/
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
38 #include <vlc_common.h>
39 #include "../lib/libvlc_internal.h"
41 #include "modules/modules.h"
42 #include "config/configuration.h"
43 #include "preparser/preparser.h"
44 #include "media_source/media_source.h"
46 #include <stdio.h> /* sprintf() */
47 #include <string.h>
48 #include <stdlib.h> /* free() */
49 #include <errno.h>
51 #include "config/vlc_getopt.h"
53 #include <vlc_playlist.h>
54 #include <vlc_interface.h>
56 #include <vlc_actions.h>
57 #include <vlc_charset.h>
58 #include <vlc_dialog.h>
59 #include <vlc_keystore.h>
60 #include <vlc_fs.h>
61 #include <vlc_cpu.h>
62 #include <vlc_url.h>
63 #include <vlc_modules.h>
64 #include <vlc_media_library.h>
65 #include <vlc_thumbnailer.h>
67 #include "libvlc.h"
69 #include <vlc_vlm.h>
71 #include <assert.h>
73 /*****************************************************************************
74 * Local prototypes
75 *****************************************************************************/
76 static void GetFilenames ( libvlc_int_t *, unsigned, const char *const [] );
78 /**
79 * Allocate a blank libvlc instance, also setting the exit handler.
80 * Vlc's threading system must have been initialized first
82 libvlc_int_t * libvlc_InternalCreate( void )
84 libvlc_int_t *p_libvlc;
85 libvlc_priv_t *priv;
87 /* Allocate a libvlc instance object */
88 p_libvlc = (vlc_custom_create)( NULL, sizeof (*priv), "libvlc" );
89 if( p_libvlc == NULL )
90 return NULL;
92 priv = libvlc_priv (p_libvlc);
93 vlc_mutex_init(&priv->lock);
94 priv->interfaces = NULL;
95 priv->main_playlist = NULL;
96 priv->p_vlm = NULL;
97 priv->media_source_provider = NULL;
99 vlc_ExitInit( &priv->exit );
101 return p_libvlc;
105 * Initialize a libvlc instance
106 * This function initializes a previously allocated libvlc instance:
107 * - CPU detection
108 * - gettext initialization
109 * - message queue, module bank and playlist initialization
110 * - configuration and commandline parsing
112 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
113 const char *ppsz_argv[] )
115 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
116 char * psz_modules = NULL;
117 char * psz_parser = NULL;
118 char * psz_control = NULL;
119 char *psz_val;
120 int i_ret = VLC_EGENERIC;
122 if (unlikely(vlc_LogPreinit(p_libvlc)))
123 return VLC_ENOMEM;
125 /* System specific initialization code */
126 system_Init();
128 /* Initialize the module bank and load the configuration of the
129 * core module. We need to do this at this stage to be able to display
130 * a short help if required by the user. (short help == core module
131 * options) */
132 module_InitBank ();
134 /* Get command line options that affect module loading. */
135 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
137 module_EndBank (false);
138 return VLC_EGENERIC;
141 vlc_threads_setup (p_libvlc);
143 /* Load the builtins and plugins into the module_bank.
144 * We have to do it before config_Load*() because this also gets the
145 * list of configuration options exported by each module and loads their
146 * default values. */
147 module_LoadPlugins (p_libvlc);
150 * Override default configuration with config file settings
152 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
154 if( var_InheritBool( p_libvlc, "reset-config" ) )
155 config_SaveConfigFile( p_libvlc ); /* Save default config */
156 else
157 config_LoadConfigFile( p_libvlc );
161 * Override configuration with command line settings
163 int vlc_optind;
164 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
165 goto error;
167 vlc_LogInit(p_libvlc);
170 * Support for gettext
172 #if defined( ENABLE_NLS ) \
173 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
174 vlc_bindtextdomain (PACKAGE_NAME);
175 #endif
176 /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
177 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
179 if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
181 libvlc_InternalCleanup (p_libvlc);
182 exit(0);
185 #ifdef HAVE_DAEMON
186 /* Check for daemon mode */
187 if( var_InheritBool( p_libvlc, "daemon" ) )
189 if( daemon( 1, 0) != 0 )
191 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
192 goto error;
195 /* lets check if we need to write the pidfile */
196 char *pidfile = var_InheritString( p_libvlc, "pidfile" );
197 if( pidfile != NULL )
199 FILE *stream = vlc_fopen( pidfile, "w" );
200 if( stream != NULL )
202 fprintf( stream, "%d", (int)getpid() );
203 fclose( stream );
204 msg_Dbg( p_libvlc, "written PID file %s", pidfile );
206 else
207 msg_Err( p_libvlc, "cannot write PID file %s: %s",
208 pidfile, vlc_strerror_c(errno) );
209 free( pidfile );
212 #endif
214 i_ret = VLC_ENOMEM;
216 if( libvlc_InternalDialogInit( p_libvlc ) != VLC_SUCCESS )
217 goto error;
218 if( libvlc_InternalKeystoreInit( p_libvlc ) != VLC_SUCCESS )
219 msg_Warn( p_libvlc, "memory keystore init failed" );
221 vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
223 if( var_InheritBool( p_libvlc, "media-library") )
225 priv->p_media_library = libvlc_MlCreate( p_libvlc );
226 if ( priv->p_media_library == NULL )
227 msg_Warn( p_libvlc, "Media library initialization failed" );
230 priv->p_thumbnailer = vlc_thumbnailer_Create( VLC_OBJECT( p_libvlc ) );
231 if ( priv->p_thumbnailer == NULL )
232 msg_Warn( p_libvlc, "Failed to instantiate VLC thumbnailer" );
235 * Initialize hotkey handling
237 if( libvlc_InternalActionsInit( p_libvlc ) != VLC_SUCCESS )
238 goto error;
241 * Meta data handling
243 priv->parser = input_preparser_New(VLC_OBJECT(p_libvlc));
244 if( !priv->parser )
245 goto error;
247 priv->media_source_provider = vlc_media_source_provider_New( VLC_OBJECT( p_libvlc ) );
248 if( !priv->media_source_provider )
249 goto error;
251 /* variables for signalling creation of new files */
252 var_Create( p_libvlc, "snapshot-file", VLC_VAR_STRING );
253 var_Create( p_libvlc, "record-file", VLC_VAR_STRING );
255 /* some default internal settings */
256 var_Create( p_libvlc, "window", VLC_VAR_STRING );
257 /* NOTE: Because the playlist and interfaces start before this function
258 * returns control to the application (DESIGN BUG!), all these variables
259 * must be created (in place of libvlc_new()) and set to VLC defaults
260 * (in place of VLC main()) *here*. */
261 var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
262 var_SetString( p_libvlc, "user-agent",
263 "VLC media player (LibVLC "VERSION")" );
264 var_Create( p_libvlc, "http-user-agent", VLC_VAR_STRING );
265 var_SetString( p_libvlc, "http-user-agent",
266 "VLC/"PACKAGE_VERSION" LibVLC/"PACKAGE_VERSION );
267 var_Create( p_libvlc, "app-icon-name", VLC_VAR_STRING );
268 var_SetString( p_libvlc, "app-icon-name", PACKAGE_NAME );
269 var_Create( p_libvlc, "app-id", VLC_VAR_STRING );
270 var_SetString( p_libvlc, "app-id", "org.VideoLAN.VLC" );
271 var_Create( p_libvlc, "app-version", VLC_VAR_STRING );
272 var_SetString( p_libvlc, "app-version", PACKAGE_VERSION );
274 /* System specific configuration */
275 system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
277 #ifdef ENABLE_VLM
278 /* Initialize VLM if vlm-conf is specified */
279 psz_parser = var_InheritString( p_libvlc, "vlm-conf" );
280 if( psz_parser )
282 priv->p_vlm = vlm_New( p_libvlc, psz_parser );
283 if( !priv->p_vlm )
284 msg_Err( p_libvlc, "VLM initialization failed" );
285 free( psz_parser );
287 #endif
290 * Load background interfaces
292 psz_modules = var_InheritString( p_libvlc, "extraintf" );
293 psz_control = var_InheritString( p_libvlc, "control" );
295 if( psz_modules && psz_control )
297 char* psz_tmp;
298 if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
300 free( psz_modules );
301 psz_modules = psz_tmp;
304 else if( psz_control )
306 free( psz_modules );
307 psz_modules = strdup( psz_control );
310 psz_parser = psz_modules;
311 while ( psz_parser && *psz_parser )
313 char *psz_module, *psz_temp;
314 psz_module = psz_parser;
315 psz_parser = strchr( psz_module, ':' );
316 if ( psz_parser )
318 *psz_parser = '\0';
319 psz_parser++;
321 if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
323 libvlc_InternalAddIntf( p_libvlc, psz_temp );
324 free( psz_temp );
327 free( psz_modules );
328 free( psz_control );
330 if( var_InheritBool( p_libvlc, "network-synchronisation") )
331 libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
333 #ifdef __APPLE__
334 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
335 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
336 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
337 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
338 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
339 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
340 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
341 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
342 var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
343 #endif
346 * Get input filenames given as commandline arguments.
347 * We assume that the remaining parameters are filenames
348 * and their input options.
350 GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
353 * Get --open argument
355 psz_val = var_InheritString( p_libvlc, "open" );
356 if ( psz_val != NULL )
358 intf_InsertItem( p_libvlc, psz_val, 0, NULL, 0 );
359 free( psz_val );
362 /* Callbacks between interfaces */
364 /* Create a variable for showing the right click menu */
365 var_Create(p_libvlc, "intf-popupmenu", VLC_VAR_BOOL);
367 /* Create a variable for showing the fullscreen interface */
368 var_Create(p_libvlc, "intf-toggle-fscontrol", VLC_VAR_VOID);
370 /* Create a variable for the Boss Key */
371 var_Create(p_libvlc, "intf-boss", VLC_VAR_VOID);
373 /* Create a variable for showing the main interface */
374 var_Create(p_libvlc, "intf-show", VLC_VAR_VOID);
376 return VLC_SUCCESS;
378 error:
379 libvlc_InternalCleanup( p_libvlc );
380 return i_ret;
384 * Cleanup a libvlc instance. The instance is not completely deallocated
385 * \param p_libvlc the instance to clean
387 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
389 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
391 if (priv->parser != NULL)
392 input_preparser_Deactivate(priv->parser);
394 /* Ask the interfaces to stop and destroy them */
395 msg_Dbg( p_libvlc, "removing all interfaces" );
396 intf_DestroyAll( p_libvlc );
398 if ( priv->p_thumbnailer )
399 vlc_thumbnailer_Release( priv->p_thumbnailer );
401 if( priv->media_source_provider )
402 vlc_media_source_provider_Delete( priv->media_source_provider );
404 libvlc_InternalDialogClean( p_libvlc );
405 libvlc_InternalKeystoreClean( p_libvlc );
407 #ifdef ENABLE_VLM
408 /* Destroy VLM if created in libvlc_InternalInit */
409 if( priv->p_vlm )
411 vlm_Delete( priv->p_vlm );
413 #endif
415 #if !defined( _WIN32 ) && !defined( __OS2__ )
416 char *pidfile = var_InheritString( p_libvlc, "pidfile" );
417 if( pidfile != NULL )
419 msg_Dbg( p_libvlc, "removing PID file %s", pidfile );
420 if( unlink( pidfile ) )
421 msg_Warn( p_libvlc, "cannot remove PID file %s: %s",
422 pidfile, vlc_strerror_c(errno) );
423 free( pidfile );
425 #endif
427 if (priv->parser != NULL)
428 input_preparser_Delete(priv->parser);
430 if (priv->main_playlist)
431 vlc_playlist_Delete(priv->main_playlist);
433 if ( priv->p_media_library )
434 libvlc_MlRelease( priv->p_media_library );
436 libvlc_InternalActionsClean( p_libvlc );
438 /* Save the configuration */
439 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
440 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
442 vlc_LogDestroy(p_libvlc->obj.logger);
443 /* Free module bank. It is refcounted, so we call this each time */
444 module_EndBank (true);
445 #if defined(_WIN32) || defined(__OS2__)
446 system_End( );
447 #endif
451 * Destroy everything.
452 * This function requests the running threads to finish, waits for their
453 * termination, and destroys their structure.
454 * It stops the thread systems: no instance can run after this has run
455 * \param p_libvlc the instance to destroy
457 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
459 libvlc_priv_t *priv = libvlc_priv( p_libvlc );
461 vlc_ExitDestroy( &priv->exit );
463 vlc_mutex_destroy(&priv->lock);
464 vlc_object_delete(p_libvlc);
467 /*****************************************************************************
468 * GetFilenames: parse command line options which are not flags
469 *****************************************************************************
470 * Parse command line for input files as well as their associated options.
471 * An option always follows its associated input and begins with a ":".
472 *****************************************************************************/
473 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
474 const char *const args[] )
476 while( n > 0 )
478 /* Count the input options */
479 unsigned i_options = 0;
481 while( args[--n][0] == ':' )
483 i_options++;
484 if( n == 0 )
486 msg_Warn( p_vlc, "options %s without item", args[n] );
487 return; /* syntax!? */
491 char *mrl = NULL;
492 if( strstr( args[n], "://" ) == NULL )
494 mrl = vlc_path2uri( args[n], NULL );
495 if( !mrl )
496 continue;
499 intf_InsertItem( p_vlc, (mrl != NULL) ? mrl : args[n], i_options,
500 ( i_options ? &args[n + 1] : NULL ),
501 VLC_INPUT_OPTION_TRUSTED );
502 free( mrl );
506 int vlc_MetadataRequest(libvlc_int_t *libvlc, input_item_t *item,
507 input_item_meta_request_option_t i_options,
508 const input_preparser_callbacks_t *cbs,
509 void *cbs_userdata,
510 int timeout, void *id)
512 libvlc_priv_t *priv = libvlc_priv(libvlc);
514 if (unlikely(priv->parser == NULL))
515 return VLC_ENOMEM;
517 input_preparser_Push( priv->parser, item, i_options, cbs, cbs_userdata, timeout, id );
518 return VLC_SUCCESS;
523 * Requests extraction of the meta data for an input item (a.k.a. preparsing).
524 * The actual extraction is asynchronous. It can be cancelled with
525 * libvlc_MetadataCancel()
527 int libvlc_MetadataRequest(libvlc_int_t *libvlc, input_item_t *item,
528 input_item_meta_request_option_t i_options,
529 const input_preparser_callbacks_t *cbs,
530 void *cbs_userdata,
531 int timeout, void *id)
533 libvlc_priv_t *priv = libvlc_priv(libvlc);
534 assert(i_options & META_REQUEST_OPTION_SCOPE_ANY);
536 if (unlikely(priv->parser == NULL))
537 return VLC_ENOMEM;
539 vlc_mutex_lock( &item->lock );
540 if( item->i_preparse_depth == 0 )
541 item->i_preparse_depth = 1;
542 vlc_mutex_unlock( &item->lock );
544 return vlc_MetadataRequest(libvlc, item, i_options, cbs, cbs_userdata, timeout, id);
548 * Requests retrieving/downloading art for an input item.
549 * The retrieval is performed asynchronously.
551 int libvlc_ArtRequest(libvlc_int_t *libvlc, input_item_t *item,
552 input_item_meta_request_option_t i_options,
553 const input_fetcher_callbacks_t *cbs,
554 void *cbs_userdata)
556 libvlc_priv_t *priv = libvlc_priv(libvlc);
557 assert(i_options & META_REQUEST_OPTION_FETCH_ANY);
559 if (unlikely(priv->parser == NULL))
560 return VLC_ENOMEM;
562 input_preparser_fetcher_Push(priv->parser, item, i_options,
563 cbs, cbs_userdata);
564 return VLC_SUCCESS;
568 * Cancels extraction of the meta data for an input item.
570 * This does nothing if the input item is already processed or if it was not
571 * added with libvlc_MetadataRequest()
573 void libvlc_MetadataCancel(libvlc_int_t *libvlc, void *id)
575 libvlc_priv_t *priv = libvlc_priv(libvlc);
577 if (unlikely(priv->parser == NULL))
578 return;
580 input_preparser_Cancel(priv->parser, id);