os2: implement vlc_strerror() and vlc_strerror_c()
[vlc.git] / src / libvlc.c
blob71e7faf484946117a60e20c980cf611f324b526f
1 /*****************************************************************************
2 * libvlc.c: libvlc instances creation and deletion, interfaces handling
3 *****************************************************************************
4 * Copyright (C) 1998-2008 VLC authors and VideoLAN
5 * $Id$
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 *****************************************************************************/
28 /** \file
29 * This file contains functions to create and destroy libvlc instances
32 /*****************************************************************************
33 * Preamble
34 *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
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"
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 #ifdef HAVE_DBUS
54 /* used for one-instance mode */
55 # include <dbus/dbus.h>
56 #endif
59 #include <vlc_playlist.h>
60 #include <vlc_interface.h>
62 #include <vlc_charset.h>
63 #include <vlc_fs.h>
64 #include <vlc_cpu.h>
65 #include <vlc_url.h>
66 #include <vlc_modules.h>
68 #include "libvlc.h"
69 #include "playlist/playlist_internal.h"
70 #include "misc/variables.h"
72 #include <vlc_vlm.h>
74 #ifdef __APPLE__
75 # include <libkern/OSAtomic.h>
76 #endif
78 #include <assert.h>
80 /*****************************************************************************
81 * The evil global variables. We handle them with care, don't worry.
82 *****************************************************************************/
84 #if !defined(_WIN32) && !defined(__OS2__)
85 static bool b_daemon = false;
86 #endif
88 /*****************************************************************************
89 * Local prototypes
90 *****************************************************************************/
91 static void GetFilenames ( libvlc_int_t *, unsigned, const char *const [] );
93 /**
94 * Allocate a libvlc instance, initialize global data if needed
95 * It also initializes the threading system
97 libvlc_int_t * libvlc_InternalCreate( void )
99 libvlc_int_t *p_libvlc;
100 libvlc_priv_t *priv;
102 /* Now that the thread system is initialized, we don't have much, but
103 * at least we have variables */
104 /* Allocate a libvlc instance object */
105 p_libvlc = vlc_custom_create( (vlc_object_t *)NULL, sizeof (*priv),
106 "libvlc" );
107 if( p_libvlc == NULL )
108 return NULL;
110 priv = libvlc_priv (p_libvlc);
111 priv->p_playlist = NULL;
112 priv->p_dialog_provider = NULL;
113 priv->p_vlm = NULL;
115 vlc_ExitInit( &priv->exit );
117 return p_libvlc;
121 * Initialize a libvlc instance
122 * This function initializes a previously allocated libvlc instance:
123 * - CPU detection
124 * - gettext initialization
125 * - message queue, module bank and playlist initialization
126 * - configuration and commandline parsing
128 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
129 const char *ppsz_argv[] )
131 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
132 char * psz_modules = NULL;
133 char * psz_parser = NULL;
134 char * psz_control = NULL;
135 char *psz_val;
137 /* System specific initialization code */
138 system_Init();
140 /* Initialize the module bank and load the configuration of the
141 * core module. We need to do this at this stage to be able to display
142 * a short help if required by the user. (short help == core module
143 * options) */
144 module_InitBank ();
146 /* Get command line options that affect module loading. */
147 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
149 module_EndBank (false);
150 return VLC_EGENERIC;
153 vlc_LogInit (p_libvlc);
154 vlc_threads_setup (p_libvlc);
156 /* Load the builtins and plugins into the module_bank.
157 * We have to do it before config_Load*() because this also gets the
158 * list of configuration options exported by each module and loads their
159 * default values. */
160 size_t module_count = module_LoadPlugins (p_libvlc);
163 * Override default configuration with config file settings
165 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
167 if( var_InheritBool( p_libvlc, "reset-config" ) )
168 config_SaveConfigFile( p_libvlc ); /* Save default config */
169 else
170 config_LoadConfigFile( p_libvlc );
174 * Override configuration with command line settings
176 int vlc_optind;
177 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
179 module_EndBank (true);
180 vlc_LogDeinit (p_libvlc);
181 return VLC_EGENERIC;
185 * Support for gettext
187 #if defined( ENABLE_NLS ) \
188 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
189 vlc_bindtextdomain (PACKAGE_NAME);
190 #endif
191 /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
192 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
194 if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
196 module_EndBank (true);
197 exit(0);
200 if( module_count <= 1 )
202 msg_Err( p_libvlc, "No plugins found! Check your VLC installation.");
203 module_EndBank (true);
204 vlc_LogDeinit (p_libvlc);
205 return VLC_ENOMOD;
208 #ifdef HAVE_DAEMON
209 /* Check for daemon mode */
210 if( var_InheritBool( p_libvlc, "daemon" ) )
212 char *psz_pidfile = NULL;
214 if( daemon( 1, 0) != 0 )
216 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
217 module_EndBank (true);
218 vlc_LogDeinit (p_libvlc);
219 return VLC_ENOMEM;
221 b_daemon = true;
223 /* lets check if we need to write the pidfile */
224 psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
225 if( psz_pidfile != NULL )
227 FILE *pidfile;
228 pid_t i_pid = getpid ();
229 msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
230 i_pid, psz_pidfile );
231 pidfile = vlc_fopen( psz_pidfile,"w" );
232 if( pidfile != NULL )
234 utf8_fprintf( pidfile, "%d", (int)i_pid );
235 fclose( pidfile );
237 else
239 msg_Err( p_libvlc, "cannot open pid file %s for writing: %s",
240 psz_pidfile, vlc_strerror_c(errno) );
243 free( psz_pidfile );
245 #endif
247 /* FIXME: could be replaced by using Unix sockets */
248 #ifdef HAVE_DBUS
250 #define MPRIS_APPEND "/org/mpris/MediaPlayer2/TrackList/Append"
251 #define MPRIS_BUS_NAME "org.mpris.MediaPlayer2.vlc"
252 #define MPRIS_OBJECT_PATH "/org/mpris/MediaPlayer2"
253 #define MPRIS_TRACKLIST_INTERFACE "org.mpris.MediaPlayer2.TrackList"
255 if( var_InheritBool( p_libvlc, "one-instance" )
256 || ( var_InheritBool( p_libvlc, "one-instance-when-started-from-file" )
257 && var_InheritBool( p_libvlc, "started-from-file" ) ) )
259 for( int i = vlc_optind; i < i_argc; i++ )
260 if( ppsz_argv[i][0] == ':' )
262 msg_Err( p_libvlc, "item option %s incompatible with single instance",
263 ppsz_argv[i] );
264 goto dbus_out;
267 /* Initialise D-Bus interface, check for other instances */
268 dbus_threads_init_default();
270 DBusError err;
271 dbus_error_init( &err );
273 /* connect to the session bus */
274 DBusConnection *conn = dbus_bus_get( DBUS_BUS_SESSION, &err );
275 if( conn == NULL )
277 msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
278 err.message );
279 dbus_error_free( &err );
280 goto dbus_out;
283 /* check if VLC is available on the bus
284 * if not: D-Bus control is not enabled on the other
285 * instance and we can't pass MRLs to it */
286 /* FIXME: This check is totally brain-dead and buggy. */
287 if( !dbus_bus_name_has_owner( conn, MPRIS_BUS_NAME, &err ) )
289 dbus_connection_unref( conn );
290 if( dbus_error_is_set( &err ) )
292 msg_Err( p_libvlc, "D-Bus error: %s", err.message );
294 else
295 msg_Dbg( p_libvlc, "No media player running. Continuing normally." );
296 dbus_error_free( &err );
297 goto dbus_out;
300 const dbus_bool_t play = !var_InheritBool( p_libvlc, "playlist-enqueue" );
302 msg_Warn( p_libvlc, "media player running. Exiting...");
303 for( int i = vlc_optind; i < i_argc; i++ )
305 DBusMessage *msg = dbus_message_new_method_call(
306 MPRIS_BUS_NAME, MPRIS_OBJECT_PATH, MPRIS_TRACKLIST_INTERFACE, "AddTrack" );
307 if( unlikely(msg == NULL) )
308 continue;
310 /* We need to resolve relative paths in this instance */
311 char *mrl;
312 if( strstr( ppsz_argv[i], "://" ) )
313 mrl = strdup( ppsz_argv[i] );
314 else
315 mrl = vlc_path2uri( ppsz_argv[i], NULL );
316 if( mrl == NULL )
318 dbus_message_unref( msg );
319 continue;
322 const char *after_track = MPRIS_APPEND;
324 /* append MRLs */
325 if( !dbus_message_append_args( msg, DBUS_TYPE_STRING, &mrl,
326 DBUS_TYPE_OBJECT_PATH, &after_track,
327 DBUS_TYPE_BOOLEAN, &play,
328 DBUS_TYPE_INVALID ) )
330 dbus_message_unref( msg );
331 msg = NULL;
332 free( mrl );
333 continue;
336 msg_Dbg( p_libvlc, "Adds %s to the running media player", mrl );
337 free( mrl );
339 /* send message and get a handle for a reply */
340 DBusMessage *reply = dbus_connection_send_with_reply_and_block( conn, msg, -1,
341 &err );
342 dbus_message_unref( msg );
343 if( reply == NULL )
345 msg_Err( p_libvlc, "D-Bus error: %s", err.message );
346 continue;
348 dbus_message_unref( reply );
350 /* we unreference the connection when we've finished with it */
351 dbus_connection_unref( conn );
352 exit( 1 );
354 #undef MPRIS_APPEND
355 #undef MPRIS_BUS_NAME
356 #undef MPRIS_OBJECT_PATH
357 #undef MPRIS_TRACKLIST_INTERFACE
358 dbus_out:
359 #endif // HAVE_DBUS
361 vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
363 priv->b_stats = var_InheritBool( p_libvlc, "stats" );
366 * Initialize hotkey handling
368 priv->actions = vlc_InitActions( p_libvlc );
370 /* Create a variable for showing the fullscreen interface */
371 var_Create( p_libvlc, "intf-toggle-fscontrol", VLC_VAR_BOOL );
372 var_SetBool( p_libvlc, "intf-toggle-fscontrol", true );
374 /* Create a variable for the Boss Key */
375 var_Create( p_libvlc, "intf-boss", VLC_VAR_VOID );
377 /* Create a variable for showing the main interface */
378 var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL );
380 /* Create a variable for showing the right click menu */
381 var_Create( p_libvlc, "intf-popupmenu", VLC_VAR_BOOL );
383 /* variables for signalling creation of new files */
384 var_Create( p_libvlc, "snapshot-file", VLC_VAR_STRING );
385 var_Create( p_libvlc, "record-file", VLC_VAR_STRING );
387 /* some default internal settings */
388 var_Create( p_libvlc, "window", VLC_VAR_STRING );
389 /* NOTE: Because the playlist and interfaces start before this function
390 * returns control to the application (DESIGN BUG!), all these variables
391 * must be created (in place of libvlc_new()) and set to VLC defaults
392 * (in place of VLC main()) *here*. */
393 var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
394 var_SetString( p_libvlc, "user-agent",
395 "VLC media player (LibVLC "VERSION")" );
396 var_Create( p_libvlc, "http-user-agent", VLC_VAR_STRING );
397 var_SetString( p_libvlc, "http-user-agent",
398 "VLC/"PACKAGE_VERSION" LibVLC/"PACKAGE_VERSION );
399 var_Create( p_libvlc, "app-icon-name", VLC_VAR_STRING );
400 var_SetString( p_libvlc, "app-icon-name", PACKAGE_NAME );
401 var_Create( p_libvlc, "app-id", VLC_VAR_STRING );
402 var_SetString( p_libvlc, "app-id", "org.VideoLAN.VLC" );
403 var_Create( p_libvlc, "app-version", VLC_VAR_STRING );
404 var_SetString( p_libvlc, "app-version", PACKAGE_VERSION );
406 /* System specific configuration */
407 system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
409 #ifdef ENABLE_VLM
410 /* Initialize VLM if vlm-conf is specified */
411 psz_parser = var_CreateGetNonEmptyString( p_libvlc, "vlm-conf" );
412 if( psz_parser )
414 priv->p_vlm = vlm_New( p_libvlc );
415 if( !priv->p_vlm )
416 msg_Err( p_libvlc, "VLM initialization failed" );
418 free( psz_parser );
419 #endif
422 * Load background interfaces
424 psz_modules = var_CreateGetNonEmptyString( p_libvlc, "extraintf" );
425 psz_control = var_CreateGetNonEmptyString( p_libvlc, "control" );
427 if( psz_modules && psz_control )
429 char* psz_tmp;
430 if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
432 free( psz_modules );
433 psz_modules = psz_tmp;
436 else if( psz_control )
438 free( psz_modules );
439 psz_modules = strdup( psz_control );
442 psz_parser = psz_modules;
443 while ( psz_parser && *psz_parser )
445 char *psz_module, *psz_temp;
446 psz_module = psz_parser;
447 psz_parser = strchr( psz_module, ':' );
448 if ( psz_parser )
450 *psz_parser = '\0';
451 psz_parser++;
453 if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
455 intf_Create( p_libvlc, psz_temp );
456 free( psz_temp );
459 free( psz_modules );
460 free( psz_control );
462 if( var_InheritBool( p_libvlc, "file-logging" )
463 #ifdef HAVE_SYSLOG_H
464 && !var_InheritBool( p_libvlc, "syslog" )
465 #endif
468 intf_Create( p_libvlc, "logger,none" );
470 #ifdef HAVE_SYSLOG_H
471 if( var_InheritBool( p_libvlc, "syslog" ) )
473 char *logmode = var_CreateGetNonEmptyString( p_libvlc, "logmode" );
474 var_SetString( p_libvlc, "logmode", "syslog" );
475 intf_Create( p_libvlc, "logger,none" );
477 if( logmode )
479 var_SetString( p_libvlc, "logmode", logmode );
480 free( logmode );
482 var_Destroy( p_libvlc, "logmode" );
484 #endif
486 if( var_InheritBool( p_libvlc, "network-synchronisation") )
488 intf_Create( p_libvlc, "netsync,none" );
491 #ifdef __APPLE__
492 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
493 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
494 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
495 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
496 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
497 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
498 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
499 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
500 var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
501 #endif
502 #if defined (_WIN32) || defined (__OS2__)
503 var_Create( p_libvlc, "drawable-hwnd", VLC_VAR_INTEGER );
504 #endif
507 * Get input filenames given as commandline arguments.
508 * We assume that the remaining parameters are filenames
509 * and their input options.
511 GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
514 * Get --open argument
516 psz_val = var_InheritString( p_libvlc, "open" );
517 if ( psz_val != NULL )
519 playlist_AddExt( pl_Get(p_libvlc), psz_val, NULL, PLAYLIST_INSERT, 0,
520 -1, 0, NULL, 0, true, pl_Unlocked );
521 free( psz_val );
524 return VLC_SUCCESS;
528 * Cleanup a libvlc instance. The instance is not completely deallocated
529 * \param p_libvlc the instance to clean
531 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
533 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
535 /* Ask the interfaces to stop and destroy them */
536 msg_Dbg( p_libvlc, "removing all interfaces" );
537 libvlc_Quit( p_libvlc );
538 intf_DestroyAll( p_libvlc );
540 #ifdef ENABLE_VLM
541 /* Destroy VLM if created in libvlc_InternalInit */
542 if( priv->p_vlm )
544 vlm_Delete( priv->p_vlm );
546 #endif
548 /* Free playlist now, all threads are gone */
549 playlist_t *p_playlist = libvlc_priv (p_libvlc)->p_playlist;
550 if( p_playlist != NULL )
551 playlist_Destroy( p_playlist );
553 msg_Dbg( p_libvlc, "removing stats" );
555 #if !defined( _WIN32 ) && !defined( __OS2__ )
556 char* psz_pidfile = NULL;
558 if( b_daemon )
560 psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
561 if( psz_pidfile != NULL )
563 msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
564 if( unlink( psz_pidfile ) == -1 )
566 msg_Dbg( p_libvlc, "removing pid file %s: %s",
567 psz_pidfile, vlc_strerror_c(errno) );
570 free( psz_pidfile );
572 #endif
574 vlc_DeinitActions( p_libvlc, priv->actions );
576 /* Save the configuration */
577 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
578 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
580 /* Free module bank. It is refcounted, so we call this each time */
581 module_EndBank (true);
582 vlc_LogDeinit (p_libvlc);
583 #if defined(_WIN32) || defined(__OS2__)
584 system_End( );
585 #endif
589 * Destroy everything.
590 * This function requests the running threads to finish, waits for their
591 * termination, and destroys their structure.
592 * It stops the thread systems: no instance can run after this has run
593 * \param p_libvlc the instance to destroy
595 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
597 libvlc_priv_t *priv = libvlc_priv( p_libvlc );
599 vlc_ExitDestroy( &priv->exit );
601 assert( atomic_load(&(vlc_internals(p_libvlc)->refs)) == 1 );
602 vlc_object_release( p_libvlc );
606 * Add an interface plugin and run it
608 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc, char const *psz_module )
610 if( !p_libvlc )
611 return VLC_EGENERIC;
613 if( !psz_module ) /* requesting the default interface */
615 char *psz_interface = var_CreateGetNonEmptyString( p_libvlc, "intf" );
616 if( !psz_interface ) /* "intf" has not been set */
618 #if !defined( _WIN32 ) && !defined( __OS2__ )
619 if( b_daemon )
620 /* Daemon mode hack.
621 * We prefer the dummy interface if none is specified. */
622 psz_module = "dummy";
623 else
624 #endif
625 msg_Info( p_libvlc, "%s",
626 _("Running vlc with the default interface. "
627 "Use 'cvlc' to use vlc without interface.") );
629 free( psz_interface );
630 var_Destroy( p_libvlc, "intf" );
633 /* Try to create the interface */
634 int ret = intf_Create( p_libvlc, psz_module ? psz_module : "$intf" );
635 if( ret )
636 msg_Err( p_libvlc, "interface \"%s\" initialization failed",
637 psz_module ? psz_module : "default" );
638 return ret;
641 /*****************************************************************************
642 * GetFilenames: parse command line options which are not flags
643 *****************************************************************************
644 * Parse command line for input files as well as their associated options.
645 * An option always follows its associated input and begins with a ":".
646 *****************************************************************************/
647 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
648 const char *const args[] )
650 while( n > 0 )
652 /* Count the input options */
653 unsigned i_options = 0;
655 while( args[--n][0] == ':' )
657 i_options++;
658 if( n == 0 )
660 msg_Warn( p_vlc, "options %s without item", args[n] );
661 return; /* syntax!? */
665 char *mrl = NULL;
666 if( strstr( args[n], "://" ) == NULL )
668 mrl = vlc_path2uri( args[n], NULL );
669 if( !mrl )
670 continue;
673 playlist_AddExt( pl_Get( p_vlc ), (mrl != NULL) ? mrl : args[n], NULL,
674 PLAYLIST_INSERT, 0, -1, i_options,
675 ( i_options ? &args[n + 1] : NULL ),
676 VLC_INPUT_OPTION_TRUSTED, true, pl_Unlocked );
677 free( mrl );