]> git.sesse.net Git - vlc/blobdiff - src/libvlc.c
Don't fail with no-debug
[vlc] / src / libvlc.c
index d6c646fba6aab26038a9d5c2694dd0f5c4116cc7..a81ba730e607ca0dc145e0be03652619bab888ac 100644 (file)
@@ -1,13 +1,14 @@
 /*****************************************************************************
  * libvlc.c: main libvlc source
  *****************************************************************************
- * Copyright (C) 1998-2004 the VideoLAN team
+ * Copyright (C) 1998-2006 the VideoLAN team
  * $Id$
  *
  * Authors: Vincent Seguin <seguin@via.ecp.fr>
  *          Samuel Hocevar <sam@zoy.org>
  *          Gildas Bazin <gbazin@videolan.org>
  *          Derk-Jan Hartman <hartman at videolan dot org>
+ *          RĂ©mi Denis-Courmont <rem # videolan : org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -21,7 +22,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
 #include <stdio.h>                                              /* sprintf() */
 #include <string.h>                                            /* strerror() */
 #include <stdlib.h>                                                /* free() */
+#ifdef HAVE_ASSERT
+# include <assert.h>
+#else
+# define assert( c ) ((void)0)
+#endif
 
 #ifndef WIN32
 #   include <netinet/in.h>                            /* BSD: struct in_addr */
@@ -92,6 +98,10 @@ static vlc_t *    p_static_vlc;
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
+static int AddIntfInternal( int i_object, char const *psz_module,
+                             vlc_bool_t b_block, vlc_bool_t b_play,
+                             int i_options, char **ppsz_options );
+
 static void LocaleInit( void );
 static void LocaleDeinit( void );
 static void SetLanguage   ( char const * );
@@ -244,7 +254,7 @@ int VLC_Create( void )
 
     /* Initialize mutexes */
     vlc_mutex_init( p_vlc, &p_vlc->config_lock );
-#ifdef SYS_DARWIN
+#ifdef __APPLE__
     vlc_mutex_init( p_vlc, &p_vlc->quicktime_lock );
     vlc_thread_set_priority( p_vlc, VLC_THREAD_PRIORITY_LOW );
 #endif
@@ -275,6 +285,7 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
     char *       psz_parser;
     char *       psz_control;
     vlc_bool_t   b_exit = VLC_FALSE;
+    int          i_ret = VLC_EEXIT;
     vlc_t *      p_vlc = vlc_current_object( i_object );
     module_t    *p_help_module;
     playlist_t  *p_playlist;
@@ -358,12 +369,14 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
     {
         Help( p_vlc, "help" );
         b_exit = VLC_TRUE;
+        i_ret = VLC_EEXITSUCCESS;
     }
     /* Check for version option */
     else if( config_GetInt( p_vlc, "version" ) )
     {
         Version();
         b_exit = VLC_TRUE;
+        i_ret = VLC_EEXITSUCCESS;
     }
 
     /* Set the config file stuff */
@@ -423,6 +436,7 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
             /* This is the parent, exit right now */
             msg_Dbg( p_vlc, "closing parent process" );
             b_exit = VLC_TRUE;
+            i_ret = VLC_EEXITSUCCESS;
         }
         else
         {
@@ -444,15 +458,13 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
         vlc_object_destroy( p_help_module );
         module_EndBank( p_vlc );
         if( i_object ) vlc_object_release( p_vlc );
-        return VLC_EEXIT;
+        return i_ret;
     }
 
     /* Check for translation config option */
 #if defined( ENABLE_NLS ) \
      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
 
-    setlocale( LC_NUMERIC, "" );
-
     /* This ain't really nice to have to reload the config here but it seems
      * the only way to do it. */
     config_LoadConfigFile( p_vlc, "main" );
@@ -507,18 +519,21 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
         Help( p_vlc, p_tmp );
         free( p_tmp );
         b_exit = VLC_TRUE;
+        i_ret = VLC_EEXITSUCCESS;
     }
     /* Check for long help option */
     else if( config_GetInt( p_vlc, "longhelp" ) )
     {
         Help( p_vlc, "longhelp" );
         b_exit = VLC_TRUE;
+        i_ret = VLC_EEXITSUCCESS;
     }
     /* Check for module list option */
     else if( config_GetInt( p_vlc, "list" ) )
     {
         ListModules( p_vlc );
         b_exit = VLC_TRUE;
+        i_ret = VLC_EEXITSUCCESS;
     }
 
     /* Check for config file options */
@@ -549,7 +564,7 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
         vlc_object_destroy( p_help_module );
         module_EndBank( p_vlc );
         if( i_object ) vlc_object_release( p_vlc );
-        return VLC_EEXIT;
+        return i_ret;
     }
 
     /*
@@ -634,7 +649,7 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
     if( !config_GetInt( p_vlc, "sse2" ) )
         libvlc.i_cpu &= ~CPU_CAPABILITY_SSE2;
 #endif
-#if defined( __powerpc__ ) || defined( SYS_DARWIN )
+#if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
     if( !config_GetInt( p_vlc, "altivec" ) )
         libvlc.i_cpu &= ~CPU_CAPABILITY_ALTIVEC;
 #endif
@@ -675,6 +690,9 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
         p_vlc->pf_memset = memset;
     }
 
+    libvlc.b_stats = config_GetInt( p_vlc, "stats" );
+    libvlc.p_stats = NULL;
+
     /*
      * Initialize hotkey handling
      */
@@ -765,6 +783,28 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
     }
 #endif
 
+    if( config_GetInt( p_vlc, "file-logging" ) == 1 )
+    {
+        VLC_AddIntf( 0, "logger", VLC_FALSE, VLC_FALSE );
+    }
+#ifdef HAVE_SYSLOG_H
+    if( config_GetInt( p_vlc, "syslog" ) == 1 )
+    {
+        char *psz_logmode = "logmode=syslog";
+        AddIntfInternal( 0, "logger", VLC_FALSE, VLC_FALSE, 1, &psz_logmode );
+    }
+#endif
+
+    if( config_GetInt( p_vlc, "show-intf" ) == 1 )
+    {
+        VLC_AddIntf( 0, "showintf", VLC_FALSE, VLC_FALSE );
+    }
+
+    if( config_GetInt( p_vlc, "network-synchronisation") == 1 )
+    {
+        VLC_AddIntf( 0, "netsync", VLC_FALSE, VLC_FALSE );
+    }
+
     /*
      * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
      */
@@ -781,6 +821,9 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
     var_Create( p_vlc, "drawableportx", VLC_VAR_INTEGER );
     var_Create( p_vlc, "drawableporty", VLC_VAR_INTEGER );
 
+    /* Create volume callback system. */
+    var_Create( p_vlc, "volume-change", VLC_VAR_BOOL );
+
     /*
      * Get input filenames given as commandline arguments
      */
@@ -814,55 +857,10 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
 int VLC_AddIntf( int i_object, char const *psz_module,
                  vlc_bool_t b_block, vlc_bool_t b_play )
 {
-    int i_err;
-    intf_thread_t *p_intf;
-    vlc_t *p_vlc = vlc_current_object( i_object );
-
-    if( !p_vlc )
-    {
-        return VLC_ENOOBJ;
-    }
-
-#ifndef WIN32
-    if( p_vlc->p_libvlc->b_daemon && b_block && !psz_module )
-    {
-        /* Daemon mode hack.
-         * We prefer the dummy interface if none is specified. */
-        char *psz_interface = config_GetPsz( p_vlc, "intf" );
-        if( !psz_interface || !*psz_interface ) psz_module = "dummy";
-        if( psz_interface ) free( psz_interface );
-    }
-#endif
-
-    /* Try to create the interface */
-    p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf" );
-
-    if( p_intf == NULL )
-    {
-        msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
-        if( i_object ) vlc_object_release( p_vlc );
-        return VLC_EGENERIC;
-    }
-
-    /* Interface doesn't handle play on start so do it ourselves */
-    if( !p_intf->b_play && b_play ) VLC_Play( i_object );
-
-    /* Try to run the interface */
-    p_intf->b_play = b_play;
-    p_intf->b_block = b_block;
-    i_err = intf_RunThread( p_intf );
-    if( i_err )
-    {
-        vlc_object_detach( p_intf );
-        intf_Destroy( p_intf );
-        if( i_object ) vlc_object_release( p_vlc );
-        return i_err;
-    }
-
-    if( i_object ) vlc_object_release( p_vlc );
-    return VLC_SUCCESS;
+    return AddIntfInternal( i_object, psz_module, b_block, b_play, 0, NULL );
 }
 
+
 /*****************************************************************************
  * VLC_Die: ask vlc to die.
  *****************************************************************************
@@ -894,6 +892,7 @@ int VLC_CleanUp( int i_object )
     vout_thread_t      * p_vout;
     aout_instance_t    * p_aout;
     announce_handler_t * p_announce;
+    stats_handler_t    * p_stats;
     vlc_t *p_vlc = vlc_current_object( i_object );
 
     /* Check that the handle is valid */
@@ -915,9 +914,9 @@ int VLC_CleanUp( int i_object )
     }
 
     /*
-     * Free playlists
+     * Free playlist
      */
-    msg_Dbg( p_vlc, "removing all playlists" );
+    msg_Dbg( p_vlc, "removing playlist handler" );
     while( (p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST,
                                           FIND_CHILD )) )
     {
@@ -948,17 +947,26 @@ int VLC_CleanUp( int i_object )
         aout_Delete( p_aout );
     }
 
+    while( ( p_stats = vlc_object_find( p_vlc, VLC_OBJECT_STATS, FIND_CHILD) ))
+    {
+        stats_TimersDumpAll( p_vlc );
+        stats_HandlerDestroy( p_stats );
+        vlc_object_detach( (vlc_object_t*) p_stats );
+        vlc_object_release( (vlc_object_t *)p_stats );
+        // TODO: Delete it
+    }
+
     /*
      * Free announce handler(s?)
      */
-    msg_Dbg( p_vlc, "removing announce handler" );
     while( (p_announce = vlc_object_find( p_vlc, VLC_OBJECT_ANNOUNCE,
                                                  FIND_CHILD ) ) )
-   {
+    {
+        msg_Dbg( p_vlc, "removing announce handler" );
         vlc_object_detach( p_announce );
         vlc_object_release( p_announce );
         announce_HandlerDestroy( p_announce );
-   }
+    }
 
     if( i_object ) vlc_object_release( p_vlc );
     return VLC_SUCCESS;
@@ -1218,7 +1226,7 @@ int VLC_AddTarget( int i_object, char const *psz_target,
 }
 
 /*****************************************************************************
- * VLC_Play: play
+ * VLC_Play: play the playlist
  *****************************************************************************/
 int VLC_Play( int i_object )
 {
@@ -1311,7 +1319,6 @@ vlc_bool_t VLC_IsPlaying( int i_object )
 {
     playlist_t * p_playlist;
     vlc_bool_t   b_playing;
-    vlc_value_t  val;
 
     vlc_t *p_vlc = vlc_current_object( i_object );
 
@@ -1328,13 +1335,18 @@ vlc_bool_t VLC_IsPlaying( int i_object )
         if( i_object ) vlc_object_release( p_vlc );
         return VLC_ENOOBJ;
     }
-    if( !p_playlist->p_input )
+
+    if( p_playlist->p_input )
     {
-        if( i_object ) vlc_object_release( p_vlc );
-        return VLC_ENOOBJ;
+        vlc_value_t  val;
+        var_Get( p_playlist->p_input, "state", &val );
+        b_playing = ( val.i_int == PLAYING_S );
+    }
+    else
+    {
+        msg_Dbg(p_vlc, "polling playlist_IsPlaying");
+        b_playing = playlist_IsPlaying( p_playlist );
     }
-    var_Get( p_playlist->p_input, "state", &val );
-    b_playing = ( val.i_int == PLAYING_S );
     vlc_object_release( p_playlist );
 
     if( i_object ) vlc_object_release( p_vlc );
@@ -1890,6 +1902,66 @@ int VLC_FullScreen( int i_object )
 
 /* following functions are local */
 
+
+static int  AddIntfInternal( int i_object, char const *psz_module,
+                             vlc_bool_t b_block, vlc_bool_t b_play,
+                             int i_options, char **ppsz_options )
+{
+    int i_err,i;
+    intf_thread_t *p_intf;
+    vlc_t *p_vlc = vlc_current_object( i_object );
+
+    if( !p_vlc )
+    {
+        return VLC_ENOOBJ;
+    }
+
+#ifndef WIN32
+    if( p_vlc->p_libvlc->b_daemon && b_block && !psz_module )
+    {
+        /* Daemon mode hack.
+         * We prefer the dummy interface if none is specified. */
+        char *psz_interface = config_GetPsz( p_vlc, "intf" );
+        if( !psz_interface || !*psz_interface ) psz_module = "dummy";
+        if( psz_interface ) free( psz_interface );
+    }
+#endif
+
+    /* Try to create the interface */
+    p_intf = intf_Create( p_vlc, psz_module ? psz_module : "$intf",
+                          i_options, ppsz_options );
+
+    if( p_intf == NULL )
+    {
+        msg_Err( p_vlc, "interface \"%s\" initialization failed", psz_module );
+        if( i_object ) vlc_object_release( p_vlc );
+        return VLC_EGENERIC;
+    }
+
+    /* Interface doesn't handle play on start so do it ourselves */
+    if( !p_intf->b_play && b_play ) VLC_Play( i_object );
+
+    /* Try to run the interface */
+    p_intf->b_play = b_play;
+    p_intf->b_block = b_block;
+    i_err = intf_RunThread( p_intf );
+    if( i_err )
+    {
+        vlc_object_detach( p_intf );
+        intf_Destroy( p_intf );
+        if( i_object ) vlc_object_release( p_vlc );
+        return i_err;
+    }
+
+    for( i = 0  ; i< i_options ; i++ )
+    {
+        
+    }
+
+    if( i_object ) vlc_object_release( p_vlc );
+    return VLC_SUCCESS;
+};
+
 static void LocaleInit( void )
 {
     char *psz_charset;
@@ -1946,7 +2018,7 @@ static void SetLanguage ( char const *psz_lang )
      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
 
     char *          psz_path;
-#if defined( SYS_DARWIN ) || defined ( WIN32 ) || defined( SYS_BEOS )
+#if defined( __APPLE__ ) || defined ( WIN32 ) || defined( SYS_BEOS )
     char            psz_tmp[1024];
 #endif
 
@@ -1959,7 +2031,7 @@ static void SetLanguage ( char const *psz_lang )
     }
     else if( psz_lang )
     {
-#ifdef SYS_DARWIN
+#ifdef __APPLE__
         /* I need that under Darwin, please check it doesn't disturb
          * other platforms. --Meuuh */
         setenv( "LANG", psz_lang, 1 );
@@ -1983,7 +2055,7 @@ static void SetLanguage ( char const *psz_lang )
     }
 
     /* Specify where to find the locales for current domain */
-#if !defined( SYS_DARWIN ) && !defined( WIN32 ) && !defined( SYS_BEOS )
+#if !defined( __APPLE__ ) && !defined( WIN32 ) && !defined( SYS_BEOS )
     psz_path = LOCALEDIR;
 #else
     snprintf( psz_tmp, sizeof(psz_tmp), "%s/%s", libvlc.psz_vlcpath,
@@ -1992,7 +2064,7 @@ static void SetLanguage ( char const *psz_lang )
 #endif
     if( !bindtextdomain( PACKAGE_NAME, psz_path ) )
     {
-        fprintf( stderr, "warning: no domain %s in directory %s\n",
+        fprintf( stderr, "warning: couldn't bind domain %s in directory %s\n",
                  PACKAGE_NAME, psz_path );
     }
 
@@ -2421,11 +2493,7 @@ static void Version( void )
     if( strcmp( VLC_Changeset(), "exported" ) )
         fprintf( stdout, _("Based upon svn changeset [%s]\n"),
                  VLC_Changeset() );
-    fprintf( stdout,
-      _("This program comes with NO WARRANTY, to the extent permitted by "
-        "law.\nYou may redistribute it under the terms of the GNU General "
-        "Public License;\nsee the file named COPYING for details.\n"
-        "Written by the VideoLAN team; see the AUTHORS file.\n") );
+    fprintf( stdout, LICENSE_MSG );
 
 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
     PauseConsole();
@@ -2548,24 +2616,54 @@ static void InitDeviceValues( vlc_t *p_vlc )
     char **devices;
     char *block_dev;
     dbus_bool_t b_dvd;
+    DBusConnection *p_connection;
+    DBusError       error;
 
+#ifdef HAVE_HAL_1
+    ctx =  libhal_ctx_new();
+    if( !ctx ) return;
+    dbus_error_init( &error );
+    p_connection = dbus_bus_get ( DBUS_BUS_SYSTEM, &error );
+    if( dbus_error_is_set( &error ) )
+    {
+        dbus_error_free( &error );
+        return;
+    }
+    libhal_ctx_set_dbus_connection( ctx, p_connection );
+    if( libhal_ctx_init( ctx, &error ) )
+#else
     if( ( ctx = hal_initialize( NULL, FALSE ) ) )
+#endif
     {
+#ifdef HAVE_HAL_1
+        if( ( devices = libhal_get_all_devices( ctx, &i_devices, NULL ) ) )
+#else
         if( ( devices = hal_get_all_devices( ctx, &i_devices ) ) )
+#endif
         {
             for( i = 0; i < i_devices; i++ )
             {
+#ifdef HAVE_HAL_1
+                if( !libhal_device_property_exists( ctx, devices[i],
+                                                "storage.cdrom.dvd", NULL ) )
+#else
                 if( !hal_device_property_exists( ctx, devices[ i ],
                                                 "storage.cdrom.dvd" ) )
+#endif
                 {
                     continue;
                 }
-
+#ifdef HAVE_HAL_1
+                b_dvd = libhal_device_get_property_bool( ctx, devices[ i ],
+                                                 "storage.cdrom.dvd", NULL  );
+                block_dev = libhal_device_get_property_string( ctx,
+                                devices[ i ], "block.device" , NULL );
+#else
                 b_dvd = hal_device_get_property_bool( ctx, devices[ i ],
                                                       "storage.cdrom.dvd" );
                 block_dev = hal_device_get_property_string( ctx, devices[ i ],
                                                             "block.device" );
-
+#endif
                 if( b_dvd )
                 {
                     config_PutPsz( p_vlc, "dvd", block_dev );
@@ -2573,13 +2671,28 @@ static void InitDeviceValues( vlc_t *p_vlc )
 
                 config_PutPsz( p_vlc, "vcd", block_dev );
                 config_PutPsz( p_vlc, "cd-audio", block_dev );
-
+#ifdef HAVE_HAL_1
+                libhal_free_string( block_dev );
+#else
                 hal_free_string( block_dev );
+#endif
             }
+#ifdef HAVE_HAL_1
+            libhal_free_string_array( devices );
+#else
             hal_free_string_array( devices );
+#endif
         }
 
+#ifdef HAVE_HAL_1
+        libhal_ctx_shutdown( ctx, NULL );
+#else
         hal_shutdown( ctx );
+#endif
+    }
+    else
+    {
+        msg_Warn( p_vlc, "Unable to get HAL device properties" );
     }
 #endif
 }
@@ -2602,10 +2715,10 @@ char *FromLocale( const char *locale )
          * to non-const.
          */
         inb = strlen( locale );
-        outb = inb * 6 + 1;
-
         /* FIXME: I'm not sure about the value for the multiplication
          * (for western people, multiplication by 3 (Latin9) is sufficient) */
+        outb = inb * 6 + 1;
+
         optr = output = calloc( outb , 1);
 
         vlc_mutex_lock( &libvlc.from_locale_lock );
@@ -2614,14 +2727,19 @@ char *FromLocale( const char *locale )
         while( vlc_iconv( libvlc.from_locale, &iptr, &inb, &optr, &outb )
                                                                == (size_t)-1 )
         {
-            *optr = '?';
-            optr++;
+            *optr++ = '?';
+            outb--;
             iptr++;
+            inb--;
             vlc_iconv( libvlc.from_locale, NULL, NULL, NULL, NULL );
         }
-        vlc_mutex_unlock( &libvlc.from_locale_lock );
+       vlc_mutex_unlock( &libvlc.from_locale_lock );
 
-        return realloc( output, strlen( output ) + 1 );
+        assert (inb == 0);
+        assert (*iptr == '\0');
+        assert (*optr == '\0');
+        assert (strlen( output ) == (size_t)(optr - output));
+        return realloc( output, optr - output + 1 );
     }
     return (char *)locale;
 }
@@ -2655,14 +2773,19 @@ char *ToLocale( const char *utf8 )
         while( vlc_iconv( libvlc.to_locale, &iptr, &inb, &optr, &outb )
                                                                == (size_t)-1 )
         {
-            *optr = '?'; /* should not happen, and yes, it sucks */
-            optr++;
+            *optr++ = '?'; /* should not happen, and yes, it sucks */
+            outb--;
             iptr++;
+            inb--;
             vlc_iconv( libvlc.to_locale, NULL, NULL, NULL, NULL );
         }
         vlc_mutex_unlock( &libvlc.to_locale_lock );
 
-        return realloc( output, strlen( output ) + 1 );
+        assert (inb == 0);
+        assert (*iptr == '\0');
+        assert (*optr == '\0');
+        assert (strlen( output ) == (size_t)(optr - output));
+       return realloc( output, optr - output + 1 );
     }
     return (char *)utf8;
 }