qt/qml_menu_wrapper: Add group support
[vlc.git] / src / libvlc.c
blob2633f837419b2afd2b69da6d48e4b0d6045c4bba
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;
104 static void libvlc_AddInterfaces(libvlc_int_t *libvlc, const char *varname)
106 char *str = var_InheritString(libvlc, varname);
107 if (str == NULL)
108 return;
110 char *state;
111 char *intf = strtok_r(str, ":", &state);
113 while (intf != NULL) {
114 libvlc_InternalAddIntf(libvlc, intf);
115 intf = strtok_r(NULL, ":", &state);
118 free(str);
122 * Initialize a libvlc instance
123 * This function initializes a previously allocated libvlc instance:
124 * - CPU detection
125 * - gettext initialization
126 * - message queue, module bank and playlist initialization
127 * - configuration and commandline parsing
129 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
130 const char *ppsz_argv[] )
132 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
133 char *psz_val;
134 int i_ret = VLC_EGENERIC;
136 if (unlikely(vlc_LogPreinit(p_libvlc)))
137 return VLC_ENOMEM;
139 /* System specific initialization code */
140 system_Init();
142 /* Initialize the module bank and load the configuration of the
143 * core module. We need to do this at this stage to be able to display
144 * a short help if required by the user. (short help == core module
145 * options) */
146 module_InitBank ();
148 /* Get command line options that affect module loading. */
149 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
151 module_EndBank (false);
152 return VLC_EGENERIC;
155 vlc_threads_setup (p_libvlc);
157 /* Load the builtins and plugins into the module_bank.
158 * We have to do it before config_Load*() because this also gets the
159 * list of configuration options exported by each module and loads their
160 * default values. */
161 module_LoadPlugins (p_libvlc);
164 * Override default configuration with config file settings
166 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
168 if( var_InheritBool( p_libvlc, "reset-config" ) )
169 config_SaveConfigFile( p_libvlc ); /* Save default config */
170 else
171 config_LoadConfigFile( p_libvlc );
175 * Override configuration with command line settings
177 int vlc_optind;
178 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
179 goto error;
181 vlc_LogInit(p_libvlc);
184 * Support for gettext
186 #if defined( ENABLE_NLS ) \
187 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
188 vlc_bindtextdomain (PACKAGE_NAME);
189 #endif
190 /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
191 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
193 if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
195 libvlc_InternalCleanup (p_libvlc);
196 exit(0);
199 i_ret = VLC_ENOMEM;
201 if( libvlc_InternalDialogInit( p_libvlc ) != VLC_SUCCESS )
202 goto error;
203 if( libvlc_InternalKeystoreInit( p_libvlc ) != VLC_SUCCESS )
204 msg_Warn( p_libvlc, "memory keystore init failed" );
206 vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
208 if( var_InheritBool( p_libvlc, "media-library") )
210 priv->p_media_library = libvlc_MlCreate( p_libvlc );
211 if ( priv->p_media_library == NULL )
212 msg_Warn( p_libvlc, "Media library initialization failed" );
215 priv->p_thumbnailer = vlc_thumbnailer_Create( VLC_OBJECT( p_libvlc ) );
216 if ( priv->p_thumbnailer == NULL )
217 msg_Warn( p_libvlc, "Failed to instantiate thumbnailer" );
220 * Initialize hotkey handling
222 if( libvlc_InternalActionsInit( p_libvlc ) != VLC_SUCCESS )
223 goto error;
226 * Meta data handling
228 priv->parser = input_preparser_New(VLC_OBJECT(p_libvlc));
229 if( !priv->parser )
230 goto error;
232 priv->media_source_provider = vlc_media_source_provider_New( VLC_OBJECT( p_libvlc ) );
233 if( !priv->media_source_provider )
234 goto error;
236 /* variables for signalling creation of new files */
237 var_Create( p_libvlc, "snapshot-file", VLC_VAR_STRING );
238 var_Create( p_libvlc, "record-file", VLC_VAR_STRING );
240 /* some default internal settings */
241 var_Create( p_libvlc, "window", VLC_VAR_STRING );
242 var_Create( p_libvlc, "vout-cb-type", VLC_VAR_INTEGER );
244 /* NOTE: Because the playlist and interfaces start before this function
245 * returns control to the application (DESIGN BUG!), all these variables
246 * must be created (in place of libvlc_new()) and set to VLC defaults
247 * (in place of VLC main()) *here*. */
248 var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
249 var_SetString( p_libvlc, "user-agent",
250 "VLC media player (LibVLC "VERSION")" );
251 var_Create( p_libvlc, "http-user-agent", VLC_VAR_STRING );
252 var_SetString( p_libvlc, "http-user-agent",
253 "VLC/"PACKAGE_VERSION" LibVLC/"PACKAGE_VERSION );
254 var_Create( p_libvlc, "app-icon-name", VLC_VAR_STRING );
255 var_SetString( p_libvlc, "app-icon-name", PACKAGE_NAME );
256 var_Create( p_libvlc, "app-id", VLC_VAR_STRING );
257 var_SetString( p_libvlc, "app-id", "org.VideoLAN.VLC" );
258 var_Create( p_libvlc, "app-version", VLC_VAR_STRING );
259 var_SetString( p_libvlc, "app-version", PACKAGE_VERSION );
261 /* System specific configuration */
262 system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
264 #ifdef ENABLE_VLM
265 /* Initialize VLM if vlm-conf is specified */
266 char *psz_parser = var_InheritString( p_libvlc, "vlm-conf" );
267 if( psz_parser )
269 priv->p_vlm = vlm_New( p_libvlc, psz_parser );
270 if( !priv->p_vlm )
271 msg_Err( p_libvlc, "VLM initialization failed" );
272 free( psz_parser );
274 #endif
277 * Load background interfaces
279 libvlc_AddInterfaces(p_libvlc, "extraintf");
280 libvlc_AddInterfaces(p_libvlc, "control");
282 #ifdef __APPLE__
283 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
284 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
285 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
286 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
287 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
288 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
289 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
290 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
291 var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
292 #endif
295 * Get input filenames given as commandline arguments.
296 * We assume that the remaining parameters are filenames
297 * and their input options.
299 GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
302 * Get --open argument
304 psz_val = var_InheritString( p_libvlc, "open" );
305 if ( psz_val != NULL )
307 intf_InsertItem( p_libvlc, psz_val, 0, NULL, 0 );
308 free( psz_val );
311 /* Callbacks between interfaces */
313 /* Create a variable for showing the right click menu */
314 var_Create(p_libvlc, "intf-popupmenu", VLC_VAR_BOOL);
316 /* Create a variable for showing the fullscreen interface */
317 var_Create(p_libvlc, "intf-toggle-fscontrol", VLC_VAR_VOID);
319 /* Create a variable for the Boss Key */
320 var_Create(p_libvlc, "intf-boss", VLC_VAR_VOID);
322 /* Create a variable for showing the main interface */
323 var_Create(p_libvlc, "intf-show", VLC_VAR_VOID);
325 return VLC_SUCCESS;
327 error:
328 libvlc_InternalCleanup( p_libvlc );
329 return i_ret;
333 * Cleanup a libvlc instance. The instance is not completely deallocated
334 * \param p_libvlc the instance to clean
336 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
338 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
340 if (priv->parser != NULL)
341 input_preparser_Deactivate(priv->parser);
343 /* Ask the interfaces to stop and destroy them */
344 msg_Dbg( p_libvlc, "removing all interfaces" );
345 intf_DestroyAll( p_libvlc );
347 if ( priv->p_thumbnailer )
348 vlc_thumbnailer_Release( priv->p_thumbnailer );
350 libvlc_InternalDialogClean( p_libvlc );
351 libvlc_InternalKeystoreClean( p_libvlc );
353 #ifdef ENABLE_VLM
354 /* Destroy VLM if created in libvlc_InternalInit */
355 if( priv->p_vlm )
357 vlm_Delete( priv->p_vlm );
359 #endif
361 #if !defined( _WIN32 ) && !defined( __OS2__ )
362 char *pidfile = var_InheritString( p_libvlc, "pidfile" );
363 if( pidfile != NULL )
365 msg_Dbg( p_libvlc, "removing PID file %s", pidfile );
366 if( unlink( pidfile ) )
367 msg_Warn( p_libvlc, "cannot remove PID file %s: %s",
368 pidfile, vlc_strerror_c(errno) );
369 free( pidfile );
371 #endif
373 if (priv->parser != NULL)
374 input_preparser_Delete(priv->parser);
376 if (priv->main_playlist)
377 vlc_playlist_Delete(priv->main_playlist);
379 if ( priv->p_media_library )
380 libvlc_MlRelease( priv->p_media_library );
382 if( priv->media_source_provider )
383 vlc_media_source_provider_Delete( priv->media_source_provider );
385 libvlc_InternalActionsClean( p_libvlc );
387 /* Save the configuration */
388 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
389 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
391 vlc_LogDestroy(p_libvlc->obj.logger);
392 /* Free module bank. It is refcounted, so we call this each time */
393 module_EndBank (true);
394 #if defined(_WIN32) || defined(__OS2__)
395 system_End( );
396 #endif
400 * Destroy everything.
401 * This function requests the running threads to finish, waits for their
402 * termination, and destroys their structure.
403 * It stops the thread systems: no instance can run after this has run
404 * \param p_libvlc the instance to destroy
406 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
408 vlc_object_delete(p_libvlc);
411 /*****************************************************************************
412 * GetFilenames: parse command line options which are not flags
413 *****************************************************************************
414 * Parse command line for input files as well as their associated options.
415 * An option always follows its associated input and begins with a ":".
416 *****************************************************************************/
417 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
418 const char *const args[] )
420 while( n > 0 )
422 /* Count the input options */
423 unsigned i_options = 0;
425 while( args[--n][0] == ':' )
427 i_options++;
428 if( n == 0 )
430 msg_Warn( p_vlc, "options %s without item", args[n] );
431 return; /* syntax!? */
435 char *mrl = NULL;
436 if( strstr( args[n], "://" ) == NULL )
438 mrl = vlc_path2uri( args[n], NULL );
439 if( !mrl )
440 continue;
443 intf_InsertItem( p_vlc, (mrl != NULL) ? mrl : args[n], i_options,
444 ( i_options ? &args[n + 1] : NULL ),
445 VLC_INPUT_OPTION_TRUSTED );
446 free( mrl );
450 int vlc_MetadataRequest(libvlc_int_t *libvlc, input_item_t *item,
451 input_item_meta_request_option_t i_options,
452 const input_preparser_callbacks_t *cbs,
453 void *cbs_userdata,
454 int timeout, void *id)
456 libvlc_priv_t *priv = libvlc_priv(libvlc);
458 if (unlikely(priv->parser == NULL))
459 return VLC_ENOMEM;
461 return input_preparser_Push( priv->parser, item, i_options, cbs,
462 cbs_userdata, timeout, id );
466 * Requests extraction of the meta data for an input item (a.k.a. preparsing).
467 * The actual extraction is asynchronous. It can be cancelled with
468 * libvlc_MetadataCancel()
470 int libvlc_MetadataRequest(libvlc_int_t *libvlc, input_item_t *item,
471 input_item_meta_request_option_t i_options,
472 const input_preparser_callbacks_t *cbs,
473 void *cbs_userdata,
474 int timeout, void *id)
476 libvlc_priv_t *priv = libvlc_priv(libvlc);
477 assert(i_options & META_REQUEST_OPTION_SCOPE_ANY);
479 if (unlikely(priv->parser == NULL))
480 return VLC_ENOMEM;
482 vlc_mutex_lock( &item->lock );
483 if( item->i_preparse_depth == 0 )
484 item->i_preparse_depth = 1;
485 vlc_mutex_unlock( &item->lock );
487 return vlc_MetadataRequest(libvlc, item, i_options, cbs, cbs_userdata, timeout, id);
491 * Requests retrieving/downloading art for an input item.
492 * The retrieval is performed asynchronously.
494 int libvlc_ArtRequest(libvlc_int_t *libvlc, input_item_t *item,
495 input_item_meta_request_option_t i_options,
496 const input_fetcher_callbacks_t *cbs,
497 void *cbs_userdata)
499 libvlc_priv_t *priv = libvlc_priv(libvlc);
500 assert(i_options & META_REQUEST_OPTION_FETCH_ANY);
502 if (unlikely(priv->parser == NULL))
503 return VLC_ENOMEM;
505 input_preparser_fetcher_Push(priv->parser, item, i_options,
506 cbs, cbs_userdata);
507 return VLC_SUCCESS;
511 * Cancels extraction of the meta data for an input item.
513 * This does nothing if the input item is already processed or if it was not
514 * added with libvlc_MetadataRequest()
516 void libvlc_MetadataCancel(libvlc_int_t *libvlc, void *id)
518 libvlc_priv_t *priv = libvlc_priv(libvlc);
520 if (unlikely(priv->parser == NULL))
521 return;
523 input_preparser_Cancel(priv->parser, id);