]> git.sesse.net Git - vlc/blobdiff - src/libvlc.c
Remove pl_Release, and rename pl_Hold back to pl_Get
[vlc] / src / libvlc.c
index cd31fe65311420aaf03151168e908dbd95fb2126..cbc575e498584cd04a0b44cb9c955063d941cc2f 100644 (file)
 #   include <locale.h>
 #endif
 
-#ifdef ENABLE_NLS
-# include <libintl.h> /* bindtextdomain */
-#endif
-
 #ifdef HAVE_DBUS
 /* used for one-instance mode */
 #   include <dbus/dbus.h>
@@ -81,6 +77,7 @@
 #include "audio_output/aout_internal.h"
 
 #include <vlc_charset.h>
+#include <vlc_fs.h>
 #include <vlc_cpu.h>
 #include <vlc_url.h>
 
@@ -209,7 +206,6 @@ void vlc_release (gc_object_t *p_gc)
     ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
 static void SetLanguage   ( char const * );
 #endif
-static inline int LoadMessages (void);
 static int  GetFilenames  ( libvlc_int_t *, int, const char *[] );
 static void Help          ( libvlc_int_t *, char const *psz_help_name );
 static void Usage         ( libvlc_int_t *, char const *psz_search );
@@ -245,7 +241,7 @@ libvlc_int_t * libvlc_InternalCreate( void )
     }
 
     /* Allocate a libvlc instance object */
-    p_libvlc = __vlc_custom_create( NULL, sizeof (*priv),
+    p_libvlc = vlc_custom_create( (vlc_object_t *)NULL, sizeof (*priv),
                                   VLC_OBJECT_GENERIC, "libvlc" );
     if( p_libvlc != NULL )
         i_instances++;
@@ -260,7 +256,9 @@ libvlc_int_t * libvlc_InternalCreate( void )
     priv->p_vlm = NULL;
 
     /* Initialize message queue */
-    msg_Create( p_libvlc );
+    priv->msg_bank = msg_Create ();
+    if (unlikely(priv->msg_bank == NULL))
+        goto error;
 
     /* Find verbosity from VLC_VERBOSE environment variable */
     psz_env = getenv( "VLC_VERBOSE" );
@@ -278,6 +276,9 @@ libvlc_int_t * libvlc_InternalCreate( void )
     vlc_mutex_init( &priv->timer_lock );
 
     return p_libvlc;
+error:
+    vlc_object_release (p_libvlc);
+    return NULL;
 }
 
 /**
@@ -313,7 +314,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     /*
      * Support for gettext
      */
-    LoadMessages ();
+    vlc_bindtextdomain (PACKAGE_NAME);
 
     /* Initialize the module bank and load the configuration of the
      * main module. We need to do this at this stage to be able to display
@@ -349,9 +350,6 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
         i_ret = VLC_EEXITSUCCESS;
     }
 
-    /* Check for plugins cache options */
-    bool b_cache_delete = var_InheritBool( p_libvlc, "reset-plugins-cache" );
-
     /* Check for daemon mode */
 #ifndef WIN32
     if( var_InheritBool( p_libvlc, "daemon" ) )
@@ -374,7 +372,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
             pid_t i_pid = getpid ();
             msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
                                i_pid, psz_pidfile );
-            pidfile = utf8_fopen( psz_pidfile,"w" );
+            pidfile = vlc_fopen( psz_pidfile,"w" );
             if( pidfile != NULL )
             {
                 utf8_fprintf( pidfile, "%d", (int)i_pid );
@@ -427,12 +425,8 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
 #if defined( ENABLE_NLS ) \
      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
 # if defined (WIN32) || defined (__APPLE__)
-    /* This ain't really nice to have to reload the config here but it seems
-     * the only way to do it. */
-
     if( !var_InheritBool( p_libvlc, "ignore-config" ) )
         config_LoadConfigFile( p_libvlc, "main" );
-    config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
     priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
 
     /* Check if the user specified a custom language */
@@ -444,13 +438,6 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
 
         /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
         msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
-
-        module_EndBank( p_libvlc, false );
-        module_InitBank( p_libvlc );
-        if( !var_InheritBool( p_libvlc, "ignore-config" ) )
-            config_LoadConfigFile( p_libvlc, "main" );
-        config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
-        priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
     }
     free( psz_language );
 # endif
@@ -462,7 +449,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
      * list of configuration options exported by each module and loads their
      * default values.
      */
-    module_LoadPlugins( p_libvlc, b_cache_delete );
+    module_LoadPlugins( p_libvlc );
     if( p_libvlc->b_die )
     {
         b_exit = true;
@@ -484,8 +471,10 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     /* Check for full help option */
     else if( var_InheritBool( p_libvlc, "full-help" ) )
     {
-        config_PutInt( p_libvlc, "advanced", 1);
-        config_PutInt( p_libvlc, "help-verbose", 1);
+        var_Create( p_libvlc, "advanced", VLC_VAR_BOOL );
+        var_SetBool( p_libvlc, "advanced", true );
+        var_Create( p_libvlc, "help-verbose", VLC_VAR_BOOL );
+        var_SetBool( p_libvlc, "help-verbose", true );
         Help( p_libvlc, "full-help" );
         b_exit = true;
         i_ret = VLC_EEXITSUCCESS;
@@ -517,7 +506,6 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
         if( var_InheritBool( p_libvlc, "reset-config" ) )
         {
             config_ResetAll( p_libvlc );
-            config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, true );
             config_SaveConfigFile( p_libvlc, NULL );
         }
     }
@@ -822,18 +810,16 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
         module_EndBank( p_libvlc, true );
         return VLC_EGENERIC;
     }
-    playlist_Activate( p_playlist );
-    vlc_object_attach( p_playlist, p_libvlc );
 
     /* Add service discovery modules */
-    psz_modules = var_CreateGetNonEmptyString( p_playlist, "services-discovery" );
+    psz_modules = var_InheritString( p_libvlc, "services-discovery" );
     if( psz_modules )
     {
         char *p = psz_modules, *m;
         while( ( m = strsep( &p, " :," ) ) != NULL )
             playlist_ServicesDiscoveryAdd( p_playlist, m );
+        free( psz_modules );
     }
-    free( psz_modules );
 
 #ifdef ENABLE_VLM
     /* Initialize VLM if vlm-conf is specified */
@@ -943,7 +929,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
 #ifdef WIN32
     if( var_InheritBool( p_libvlc, "prefer-system-codecs") )
     {
-        char *psz_codecs = var_CreateGetNonEmptyString( p_playlist, "codec" );
+        char *psz_codecs = var_CreateGetNonEmptyString( p_libvlc, "codec" );
         if( psz_codecs )
         {
             char *psz_morecodecs;
@@ -984,15 +970,13 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     /*
      * Get --open argument
      */
-    psz_val = var_CreateGetNonEmptyString( p_libvlc, "open" );
+    psz_val = var_InheritString( p_libvlc, "open" );
     if ( psz_val != NULL )
     {
-        playlist_t *p_playlist = pl_Hold( p_libvlc );
         playlist_AddExt( p_playlist, psz_val, NULL, PLAYLIST_INSERT, 0,
                          -1, 0, NULL, 0, true, pl_Unlocked );
-        pl_Release( p_libvlc );
+        free( psz_val );
     }
-    free( psz_val );
 
     return VLC_SUCCESS;
 }
@@ -1004,11 +988,11 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
 {
     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
-    playlist_t    *p_playlist = priv->p_playlist;
+    playlist_t    *p_playlist = libvlc_priv (p_libvlc)->p_playlist;
 
     /* Deactivate the playlist */
     msg_Dbg( p_libvlc, "deactivating the playlist" );
-    playlist_Deactivate( p_playlist );
+    pl_Deactivate( p_libvlc );
 
     /* Remove all services discovery */
     msg_Dbg( p_libvlc, "removing all services discovery tasks" );
@@ -1027,14 +1011,8 @@ void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
     }
 #endif
 
-    /* Free playlist */
-    /* Any thread still running must not assume pl_Hold() succeeds. */
-    msg_Dbg( p_libvlc, "removing playlist" );
-
-    libvlc_priv(p_libvlc)->p_playlist = NULL;
-    barrier();  /* FIXME is that correct ? */
-
-    vlc_object_release( p_playlist );
+    /* Free playlist now, all threads are gone */
+    playlist_Destroy( p_playlist );
 
     stats_TimersDumpAll( p_libvlc );
     stats_TimersCleanAll( p_libvlc );
@@ -1093,7 +1071,7 @@ void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
     }
     vlc_mutex_unlock( &global_lock );
 
-    msg_Destroy( p_libvlc );
+    msg_Destroy (priv->msg_bank);
 
     /* Destroy mutexes */
     vlc_mutex_destroy( &priv->timer_lock );
@@ -1209,55 +1187,6 @@ static void SetLanguage ( const char *psz_lang )
 }
 #endif
 
-
-static inline int LoadMessages (void)
-{
-#if defined( ENABLE_NLS ) \
-     && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
-    /* Specify where to find the locales for current domain */
-#if !defined( __APPLE__ ) && !defined( WIN32 ) && !defined( SYS_BEOS )
-    static const char psz_path[] = LOCALEDIR;
-#else
-    char psz_path[1024];
-    char *datadir = config_GetDataDirDefault();
-    int ret;
-
-    if (unlikely(datadir == NULL))
-        return -1;
-    ret = snprintf (psz_path, sizeof (psz_path), "%s" DIR_SEP "locale",
-                    datadir);
-    free (datadir);
-    if (ret >= (int)sizeof (psz_path))
-        return -1;
-#endif
-    if (bindtextdomain (PACKAGE_NAME, psz_path) == NULL)
-    {
-        fprintf (stderr, "Warning: cannot bind text domain "PACKAGE_NAME
-                         " to directory %s\n", psz_path);
-        return -1;
-    }
-
-    /* LibVLC wants all messages in UTF-8.
-     * Unfortunately, we cannot ask UTF-8 for strerror_r(), strsignal_r()
-     * and other functions that are not part of our text domain.
-     */
-    if (bind_textdomain_codeset (PACKAGE_NAME, "UTF-8") == NULL)
-    {
-        fprintf (stderr, "Error: cannot set Unicode encoding for text domain "
-                         PACKAGE_NAME"\n");
-        // Unbinds the text domain to avoid broken encoding
-        bindtextdomain (PACKAGE_NAME, "DOES_NOT_EXIST");
-        return -1;
-    }
-
-    /* LibVLC does NOT set the default textdomain, since it is a library.
-     * This could otherwise break programs using LibVLC (other than VLC).
-     * textdomain (PACKAGE_NAME);
-     */
-#endif
-    return 0;
-}
-
 /*****************************************************************************
  * GetFilenames: parse command line options which are not flags
  *****************************************************************************
@@ -1287,11 +1216,9 @@ static int GetFilenames( libvlc_int_t *p_vlc, int i_argc, const char *ppsz_argv[
         if( !mrl )
             continue;
 
-        playlist_t *p_playlist = pl_Hold( p_vlc );
-        playlist_AddExt( p_playlist, mrl, NULL, PLAYLIST_INSERT,
+        playlist_AddExt( pl_Get( p_vlc ), mrl, NULL, PLAYLIST_INSERT,
                 0, -1, i_options, ( i_options ? &ppsz_argv[i_opt + 1] : NULL ),
                 VLC_INPUT_OPTION_TRUSTED, true, pl_Unlocked );
-        pl_Release( p_vlc );
         free( mrl );
     }
 
@@ -1389,21 +1316,26 @@ static void Help( libvlc_int_t *p_this, char const *psz_help_name )
 #   define CYAN    COL(36)
 #   define WHITE   COL(0)
 #   define GRAY    "\033[0m"
-static void print_help_section( module_config_t *p_item, bool b_color, bool b_description )
+static void
+print_help_section( const module_t *m, const module_config_t *p_item,
+                    bool b_color, bool b_description )
 {
     if( !p_item ) return;
     if( b_color )
     {
-        utf8_fprintf( stdout, RED"   %s:\n"GRAY, _( p_item->psz_text ) );
-        if( b_description && p_item->psz_longtext )
+        utf8_fprintf( stdout, RED"   %s:\n"GRAY,
+                      module_gettext( m, p_item->psz_text ) );
+        if( b_description && p_item->psz_longtext && *p_item->psz_longtext )
             utf8_fprintf( stdout, MAGENTA"   %s\n"GRAY,
-                          _( p_item->psz_longtext ) );
+                          module_gettext( m, p_item->psz_longtext ) );
     }
     else
     {
-        utf8_fprintf( stdout, "   %s:\n", _( p_item->psz_text ) );
-        if( b_description && p_item->psz_longtext )
-            utf8_fprintf( stdout, "   %s\n", _( p_item->psz_longtext ) );
+        utf8_fprintf( stdout, "   %s:\n",
+                      module_gettext( m, p_item->psz_text ) );
+        if( b_description && p_item->psz_longtext && *p_item->psz_longtext )
+            utf8_fprintf( stdout, "   %s\n",
+                          module_gettext(m, p_item->psz_longtext ) );
     }
 }
 
@@ -1543,18 +1475,20 @@ static void Usage( libvlc_int_t *p_this, char const *psz_search )
         {
             if( b_color )
                 utf8_fprintf( stdout, "\n " GREEN "%s" GRAY " (%s)\n",
-                              _( p_parser->psz_longname ),
+                              module_gettext( p_parser, p_parser->psz_longname ),
                               p_parser->psz_object_name );
             else
-                utf8_fprintf( stdout, "\n %s\n", _( p_parser->psz_longname ) );
+                utf8_fprintf( stdout, "\n %s\n",
+                              module_gettext(p_parser, p_parser->psz_longname ) );
         }
         if( p_parser->psz_help )
         {
             if( b_color )
                 utf8_fprintf( stdout, CYAN" %s\n"GRAY,
-                              _( p_parser->psz_help ) );
+                              module_gettext( p_parser, p_parser->psz_help ) );
             else
-                utf8_fprintf( stdout, " %s\n",  _( p_parser->psz_help ) );
+                utf8_fprintf( stdout, " %s\n",
+                              module_gettext( p_parser, p_parser->psz_help ) );
         }
 
         /* Print module options */
@@ -1588,19 +1522,20 @@ static void Usage( libvlc_int_t *p_this, char const *psz_search )
                 {
                     if( b_color )
                         utf8_fprintf( stdout, GREEN "\n %s\n" GRAY,
-                                      _( p_item->psz_text ) );
+                                      module_gettext( p_parser, p_item->psz_text ) );
                     else
                         utf8_fprintf( stdout, "\n %s\n",
-                                      _( p_item->psz_text ) );
+                                      module_gettext( p_parser, p_item->psz_text ) );
                 }
-                if( b_description && p_item->psz_longtext )
+                if( b_description && p_item->psz_longtext
+                 && *p_item->psz_longtext )
                 {
                     if( b_color )
                         utf8_fprintf( stdout, CYAN " %s\n" GRAY,
-                                      _( p_item->psz_longtext ) );
+                                      module_gettext( p_parser, p_item->psz_longtext ) );
                     else
                         utf8_fprintf( stdout, " %s\n",
-                                      _( p_item->psz_longtext ) );
+                                      module_gettext( p_parser, p_item->psz_longtext ) );
                 }
                 break;
 
@@ -1620,7 +1555,8 @@ static void Usage( libvlc_int_t *p_this, char const *psz_search )
             case CONFIG_ITEM_MODULE_LIST_CAT:
             case CONFIG_ITEM_FONT:
             case CONFIG_ITEM_PASSWORD:
-                print_help_section( p_section, b_color, b_description );
+                print_help_section( p_parser, p_section, b_color,
+                                    b_description );
                 p_section = NULL;
                 psz_bra = OPTION_VALUE_SEP "<";
                 psz_type = _("string");
@@ -1641,7 +1577,8 @@ static void Usage( libvlc_int_t *p_this, char const *psz_search )
                 break;
             case CONFIG_ITEM_INTEGER:
             case CONFIG_ITEM_KEY: /* FIXME: do something a bit more clever */
-                print_help_section( p_section, b_color, b_description );
+                print_help_section( p_parser, p_section, b_color,
+                                    b_description );
                 p_section = NULL;
                 psz_bra = OPTION_VALUE_SEP "<";
                 psz_type = _("integer");
@@ -1664,13 +1601,14 @@ static void Usage( libvlc_int_t *p_this, char const *psz_search )
                         if( i ) strcat( psz_buffer, ", " );
                         sprintf( psz_buffer + strlen(psz_buffer), "%i (%s)",
                                  p_item->pi_list[i],
-                                 _( p_item->ppsz_list_text[i] ) );
+                                 module_gettext( p_parser, p_item->ppsz_list_text[i] ) );
                     }
                     psz_ket = "}";
                 }
                 break;
             case CONFIG_ITEM_FLOAT:
-                print_help_section( p_section, b_color, b_description );
+                print_help_section( p_parser, p_section, b_color,
+                                    b_description );
                 p_section = NULL;
                 psz_bra = OPTION_VALUE_SEP "<";
                 psz_type = _("float");
@@ -1683,7 +1621,8 @@ static void Usage( libvlc_int_t *p_this, char const *psz_search )
                 }
                 break;
             case CONFIG_ITEM_BOOL:
-                print_help_section( p_section, b_color, b_description );
+                print_help_section( p_parser, p_section, b_color,
+                                    b_description );
                 p_section = NULL;
                 psz_bra = ""; psz_type = ""; psz_ket = "";
                 if( !b_help_module )
@@ -1744,7 +1683,7 @@ static void Usage( libvlc_int_t *p_this, char const *psz_search )
             psz_spaces[i] = ' ';
 
             /* We wrap the rest of the output */
-            sprintf( psz_buffer, "%s%s", _( p_item->psz_text ),
+            sprintf( psz_buffer, "%s%s", module_gettext( p_parser, p_item->psz_text ),
                      psz_suf );
             b_description_hack = b_description;
 
@@ -1835,9 +1774,11 @@ static void Usage( libvlc_int_t *p_this, char const *psz_search )
                 }
             }
 
-            if( b_description_hack && p_item->psz_longtext )
+            if( b_description_hack && p_item->psz_longtext
+             && *p_item->psz_longtext )
             {
-                sprintf( psz_buffer, "%s%s", _( p_item->psz_longtext ),
+                sprintf( psz_buffer, "%s%s",
+                         module_gettext( p_parser, p_item->psz_longtext ),
                          psz_suf );
                 b_description_hack = false;
                 psz_spaces = psz_spaces_longtext;
@@ -1915,11 +1856,11 @@ static void ListModules( libvlc_int_t *p_this, bool b_verbose )
         if( b_color )
             utf8_fprintf( stdout, GREEN"  %-22s "WHITE"%s\n"GRAY,
                           p_parser->psz_object_name,
-                          _( p_parser->psz_longname ) );
+                          module_gettext( p_parser, p_parser->psz_longname ) );
         else
             utf8_fprintf( stdout, "  %-22s %s\n",
                           p_parser->psz_object_name,
-                          _( p_parser->psz_longname ) );
+                          module_gettext( p_parser, p_parser->psz_longname ) );
 
         if( b_verbose )
         {
@@ -1971,8 +1912,8 @@ static void Version( void )
 
     utf8_fprintf( stdout, _("VLC version %s (%s)\n"), VLC_Version(),
                   psz_vlc_changeset );
-    utf8_fprintf( stdout, _("Compiled by %s on %s\n"),
-             VLC_CompileBy(), VLC_CompileHost() );
+    utf8_fprintf( stdout, _("Compiled by %s on %s (%s)\n"),
+             VLC_CompileBy(), VLC_CompileHost(), __DATE__" "__TIME__ );
     utf8_fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
     utf8_fprintf( stdout, "%s", LICENSE_MSG );