]> git.sesse.net Git - vlc/blobdiff - src/libvlc.c
got the value for PROTECTION_LEVEL_* hard-coded
[vlc] / src / libvlc.c
index 2f76b55549a7ed3908b8595e156cc8164140091e..9fcf4970a5f2f67e04960d9516b7cf8aad4e02e7 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * libvlc.c: main libvlc source
  *****************************************************************************
- * Copyright (C) 1998-2004 VideoLAN
+ * Copyright (C) 1998-2004 the VideoLAN team
  * $Id$
  *
  * Authors: Vincent Seguin <seguin@via.ecp.fr>
@@ -78,6 +78,7 @@
 #include "video_output.h"
 
 #include "stream_output.h"
+#include "charset.h"
 
 #include "libvlc.h"
 
@@ -91,8 +92,11 @@ static vlc_t *    p_static_vlc;
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
+static void LocaleInit( void );
+static void LocaleDeinit( void );
 static void SetLanguage   ( char const * );
 static int  GetFilenames  ( vlc_t *, int, char *[] );
+static void Help          ( vlc_t *, char const *psz_help_name );
 static void Usage         ( vlc_t *, char const *psz_module_name );
 static void ListModules   ( vlc_t * );
 static void Version       ( void );
@@ -200,6 +204,9 @@ int VLC_Create( void )
         libvlc.p_module_bank = NULL;
 
         libvlc.b_ready = VLC_TRUE;
+
+        /* UTF-8 convertor are initialized after the locale */
+        libvlc.from_locale = libvlc.to_locale = (vlc_iconv_t)(-1);
     }
     vlc_mutex_unlock( lockval.p_address );
     var_Destroy( p_libvlc, "libvlc" );
@@ -246,11 +253,15 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
     char *       psz_modules;
     char *       psz_parser;
     char *       psz_control;
-    char *       psz_language;
     vlc_bool_t   b_exit = VLC_FALSE;
     vlc_t *      p_vlc = vlc_current_object( i_object );
     module_t    *p_help_module;
     playlist_t  *p_playlist;
+    vlc_value_t  val;
+#if defined( ENABLE_NLS ) \
+     && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
+    char *       psz_language;
+#endif
 
     if( !p_vlc )
     {
@@ -282,6 +293,12 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
      */
     SetLanguage( "" );
 
+    /*
+     * Global iconv, must be done after setlocale()
+     * so that vlc_current_charset() works.
+     */
+    LocaleInit();
+
     /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
     msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
 
@@ -318,10 +335,7 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
     /* Check for short help option */
     if( config_GetInt( p_vlc, "help" ) )
     {
-        fprintf( stdout, _("Usage: %s [options] [items]...\n"),
-                         p_vlc->psz_object_name );
-        Usage( p_vlc, "main" );
-        Usage( p_vlc, "help" );
+        Help( p_vlc, "help" );
         b_exit = VLC_TRUE;
     }
     /* Check for version option */
@@ -415,6 +429,8 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
 
         /* Reset the default domain */
         SetLanguage( psz_language );
+        LocaleDeinit();
+        LocaleInit();
 
         /* Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
         msg_Dbg( p_vlc, "translation test: code is \"%s\"", _("C") );
@@ -457,14 +473,14 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
     /* Check for help on modules */
     if( (p_tmp = config_GetPsz( p_vlc, "module" )) )
     {
-        Usage( p_vlc, p_tmp );
+        Help( p_vlc, p_tmp );
         free( p_tmp );
         b_exit = VLC_TRUE;
     }
     /* Check for long help option */
     else if( config_GetInt( p_vlc, "longhelp" ) )
     {
-        Usage( p_vlc, NULL );
+        Help( p_vlc, "longhelp" );
         b_exit = VLC_TRUE;
     }
     /* Check for module list option */
@@ -557,7 +573,6 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
     var_Create( p_vlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
     if( config_GetInt( p_vlc, "quiet" ) )
     {
-        vlc_value_t val;
         val.i_int = -1;
         var_Set( p_vlc, "verbose", val );
     }
@@ -573,7 +588,10 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
 
     /* p_vlc initialization. FIXME ? */
 
-#if defined( __i386__ )
+    if( !config_GetInt( p_vlc, "fpu" ) )
+        libvlc.i_cpu &= ~CPU_CAPABILITY_FPU;
+
+#if defined( __i386__ ) || defined( __x86_64__ )
     if( !config_GetInt( p_vlc, "mmx" ) )
         libvlc.i_cpu &= ~CPU_CAPABILITY_MMX;
     if( !config_GetInt( p_vlc, "3dn" ) )
@@ -650,9 +668,13 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
         return VLC_EGENERIC;
     }
 
-    /* Add service discovery modules */
-    playlist_AddSDModules( p_playlist,
-                           config_GetPsz( p_playlist, "services-discovery" ) );
+    psz_modules = config_GetPsz( p_playlist, "services-discovery" );
+    if( psz_modules && *psz_modules )
+    {
+        /* Add service discovery modules */
+        playlist_AddSDModules( p_playlist, psz_modules );
+    }
+    if( psz_modules ) free( psz_modules );
 
     /*
      * Load background interfaces
@@ -664,7 +686,7 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
     {
         psz_modules = (char *)realloc( psz_modules, strlen( psz_modules ) +
                                                     strlen( psz_control ) + 1 );
-        sprintf( psz_modules, "%s,%s", psz_modules, psz_control );
+        sprintf( psz_modules, "%s:%s", psz_modules, psz_control );
     }
     else if( psz_control && *psz_control )
     {
@@ -677,7 +699,7 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
     {
         char *psz_module, *psz_temp;
         psz_module = psz_parser;
-        psz_parser = strchr( psz_module, ',' );
+        psz_parser = strchr( psz_module, ':' );
         if ( psz_parser )
         {
             *psz_parser = '\0';
@@ -722,6 +744,18 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
      */
     GetFilenames( p_vlc, i_argc, ppsz_argv );
 
+    /*
+     * Get --open argument
+     */
+    var_Create( p_vlc, "open", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+    var_Get( p_vlc, "open", &val );
+    if ( val.psz_string != NULL && *val.psz_string )
+    {
+        VLC_AddTarget( p_vlc->i_object_id, val.psz_string, NULL, 0,
+                       PLAYLIST_INSERT, 0 );
+    }
+    if ( val.psz_string != NULL ) free( val.psz_string );
+
     if( i_object ) vlc_object_release( p_vlc );
     return VLC_SUCCESS;
 }
@@ -940,6 +974,16 @@ int VLC_Destroy( int i_object )
      */
     system_End( p_vlc );
 
+    /*
+     * Free message queue.
+     * Nobody shall use msg_* afterward.
+     */
+    msg_Flush( p_vlc );
+    msg_Destroy( p_libvlc );
+
+    /* Destroy global iconv */
+    LocaleDeinit();
+
     /* Destroy mutexes */
     vlc_mutex_destroy( &p_vlc->config_lock );
 
@@ -1025,6 +1069,62 @@ int VLC_VariableGet( int i_object, char const *psz_var, vlc_value_t *p_value )
     return i_ret;
 }
 
+/*****************************************************************************
+ * VLC_VariableType: get a vlc variable type
+ *****************************************************************************/
+int VLC_VariableType( int i_object, char const *psz_var, int *pi_type )
+{
+    int i_type;
+    vlc_t *p_vlc = vlc_current_object( i_object );
+
+    if( !p_vlc )
+    {
+        return VLC_ENOOBJ;
+    }
+
+    /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
+     * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
+    if( !strncmp( psz_var, "conf::", 6 ) )
+    {
+        module_config_t *p_item;
+        char const *psz_newvar = psz_var + 6;
+
+        p_item = config_FindConfig( VLC_OBJECT(p_vlc), psz_newvar );
+
+        if( p_item )
+        {
+            switch( p_item->i_type )
+            {
+                case CONFIG_ITEM_BOOL:
+                    i_type = VLC_VAR_BOOL;
+                    break;
+                case CONFIG_ITEM_INTEGER:
+                    i_type = VLC_VAR_INTEGER;
+                    break;
+                case CONFIG_ITEM_FLOAT:
+                    i_type = VLC_VAR_FLOAT;
+                    break;
+                default:
+                    i_type = VLC_VAR_STRING;
+                    break;
+            }
+        }
+        else
+            i_type = 0;
+    }
+    else
+        i_type = VLC_VAR_TYPE & var_Type( p_vlc , psz_var );
+
+    if( i_object ) vlc_object_release( p_vlc );
+
+    if( i_type > 0 )
+    {
+        *pi_type = i_type;
+        return VLC_SUCCESS;
+    }
+    return VLC_ENOVAR;
+}
+
 /*****************************************************************************
  * VLC_AddTarget: adds a target for playing.
  *****************************************************************************
@@ -1163,6 +1263,7 @@ 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 );
 
@@ -1179,8 +1280,13 @@ vlc_bool_t VLC_IsPlaying( int i_object )
         if( i_object ) vlc_object_release( p_vlc );
         return VLC_ENOOBJ;
     }
-
-    b_playing = playlist_IsPlaying( p_playlist );
+    if( !p_playlist->p_input )
+    {
+        if( i_object ) vlc_object_release( p_vlc );
+        return VLC_ENOOBJ;
+    }
+    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 );
@@ -1334,12 +1440,14 @@ int VLC_TimeSet( int i_object, int i_seconds, vlc_bool_t b_relative )
 
     if( b_relative )
     {
-        val.i_time = i_seconds * 1000000;
+        val.i_time = i_seconds;
+        val.i_time = val.i_time * 1000000L;
         var_Set( p_input, "time-offset", val );
     }
     else
     {
-        val.i_time = i_seconds * 1000000;
+        val.i_time = i_seconds;
+        val.i_time = val.i_time * 1000000L;
         var_Set( p_input, "time", val );
     }
     vlc_object_release( p_input );
@@ -1381,7 +1489,7 @@ int VLC_LengthGet( int i_object )
     vlc_object_release( p_input );
 
     if( i_object ) vlc_object_release( p_vlc );
-    return val.i_time  / 1000000;
+    return val.i_time  / 1000000L;
 }
 
 /**
@@ -1734,6 +1842,39 @@ int VLC_FullScreen( int i_object )
 
 /* following functions are local */
 
+static void LocaleInit( void )
+{
+    char *psz_charset;
+
+    if( !vlc_current_charset( &psz_charset ) )
+    {
+        vlc_mutex_init( p_libvlc, &libvlc.from_locale_lock );
+        vlc_mutex_init( p_libvlc, &libvlc.to_locale_lock );
+        libvlc.from_locale = vlc_iconv_open( "UTF-8", psz_charset );
+        libvlc.to_locale = vlc_iconv_open( psz_charset, "UTF-8" );
+        if( !libvlc.to_locale )
+        {
+            // Not sure it is the right thing to do, but at least it
+            // doesn't make vlc crash with msvc !
+            libvlc.to_locale = (vlc_iconv_t)(-1);
+        }
+    }
+    else
+        libvlc.from_locale = libvlc.to_locale = (vlc_iconv_t)(-1);
+    free( psz_charset );
+}
+
+static void LocaleDeinit( void )
+{
+    if( libvlc.to_locale != (vlc_iconv_t)(-1) )
+    {
+        vlc_mutex_destroy( &libvlc.from_locale_lock );
+        vlc_mutex_destroy( &libvlc.to_locale_lock );
+        vlc_iconv_close( libvlc.from_locale );
+        vlc_iconv_close( libvlc.to_locale );
+    }
+}
+
 /*****************************************************************************
  * SetLanguage: set the interface language.
  *****************************************************************************
@@ -1777,6 +1918,7 @@ static void SetLanguage ( char const *psz_lang )
 #endif
 
         setlocale( LC_ALL, psz_lang );
+        setlocale(LC_NUMERIC, "C" );
     }
 
     /* Specify where to find the locales for current domain */
@@ -1796,11 +1938,8 @@ static void SetLanguage ( char const *psz_lang )
     /* Set the default domain */
     textdomain( PACKAGE_NAME );
 
-#if defined( ENABLE_UTF8 )
     bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
 #endif
-
-#endif
 }
 
 /*****************************************************************************
@@ -1817,6 +1956,7 @@ static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
      * and their input options */
     for( i_opt = i_argc - 1; i_opt >= optind; i_opt-- )
     {
+        const char *psz_target;
         i_options = 0;
 
         /* Count the input options */
@@ -1828,17 +1968,52 @@ static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
 
         /* TODO: write an internal function of this one, to avoid
          *       unnecessary lookups. */
-        VLC_AddTarget( p_vlc->i_object_id, ppsz_argv[ i_opt ],
+        /* FIXME: should we convert options to UTF-8 as well ?? */
+        psz_target = FromLocale( ppsz_argv[ i_opt ] );
+        VLC_AddTarget( p_vlc->i_object_id, psz_target,
                        (char const **)( i_options ? &ppsz_argv[i_opt + 1] :
                                         NULL ), i_options,
                        PLAYLIST_INSERT, 0 );
+        LocaleFree( psz_target );
     }
 
     return VLC_SUCCESS;
 }
 
 /*****************************************************************************
- * Usage: print program usage
+ * Help: print program help
+ *****************************************************************************
+ * Print a short inline help. Message interface is initialized at this stage.
+ *****************************************************************************/
+static void Help( vlc_t *p_this, char const *psz_help_name )
+{
+#ifdef WIN32
+    ShowConsole();
+#endif
+
+    if( psz_help_name && !strcmp( psz_help_name, "help" ) )
+    {
+        fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
+        Usage( p_this, "help" );
+        Usage( p_this, "main" );
+    }
+    else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
+    {
+        fprintf( stdout, VLC_USAGE, p_this->psz_object_name );
+        Usage( p_this, NULL );
+    }
+    else if( psz_help_name )
+    {
+        Usage( p_this, psz_help_name );
+    }
+
+#ifdef WIN32        /* Pause the console because it's destroyed when we exit */
+    PauseConsole();
+#endif
+}
+
+/*****************************************************************************
+ * Usage: print module usage
  *****************************************************************************
  * Print a short inline help. Message interface is initialized at this stage.
  *****************************************************************************/
@@ -1878,10 +2053,6 @@ static void Usage( vlc_t *p_this, char const *psz_module_name )
 
     strcpy( psz_format, FORMAT_STRING );
 
-#ifdef WIN32
-    ShowConsole();
-#endif
-
     /* List all modules */
     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
 
@@ -1931,8 +2102,13 @@ static void Usage( vlc_t *p_this, char const *psz_module_name )
             char *psz_text, *psz_spaces = psz_spaces_text;
             char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
             char *psz_suf = "", *psz_prefix = NULL;
-            int i;
+            signed int i;
 
+            /* Skip deprecated options */
+            if( p_item->psz_current )
+            {
+                continue;
+            }
             /* Skip advanced options if requested */
             if( p_item->b_advanced && !b_advanced )
             {
@@ -2022,12 +2198,7 @@ static void Usage( vlc_t *p_this, char const *psz_module_name )
 
             if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
             {
-                /* If option is of type --foo-bar, we print its counterpart
-                 * as --no-foo-bar, but if it is of type --foobar (without
-                 * dashes in the name) we print it as --nofoobar. Both
-                 * values are of course valid, only the display changes. */
-                psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
-                                                             : ", --no";
+                psz_prefix =  ", --no-";
                 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
             }
 
@@ -2064,10 +2235,10 @@ static void Usage( vlc_t *p_this, char const *psz_module_name )
             while( *psz_text )
             {
                 char *psz_parser, *psz_word;
-                int i_end = strlen( psz_text );
+                size_t i_end = strlen( psz_text );
 
                 /* If the remaining text fits in a line, print it. */
-                if( i_end <= i_width )
+                if( i_end <= (size_t)i_width )
                 {
                     fprintf( stdout, "%s\n", psz_text );
                     break;
@@ -2122,10 +2293,6 @@ static void Usage( vlc_t *p_this, char const *psz_module_name )
 
     /* Release the module list */
     vlc_list_release( p_list );
-
-#ifdef WIN32        /* Pause the console because it's destroyed when we exit */
-    PauseConsole();
-#endif
 }
 
 /*****************************************************************************
@@ -2147,12 +2314,6 @@ static void ListModules( vlc_t *p_this )
     ShowConsole();
 #endif
 
-    /* Usage */
-    fprintf( stdout, _("Usage: %s [options] [items]...\n\n"),
-                     p_this->p_vlc->psz_object_name );
-
-    fprintf( stdout, _("[module]              [description]\n") );
-
     /* List all modules */
     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
 
@@ -2337,9 +2498,97 @@ static void InitDeviceValues( vlc_t *p_vlc )
 
                 hal_free_string( block_dev );
             }
+            hal_free_string_array( devices );
         }
 
         hal_shutdown( ctx );
     }
 #endif
 }
+
+/*****************************************************************************
+ * FromLocale: converts a locale string to UTF-8
+ *****************************************************************************/
+char *FromLocale( const char *locale )
+{
+    if( locale == NULL )
+        return NULL;
+
+    if( libvlc.from_locale != (vlc_iconv_t)(-1) )
+    {
+        char *iptr = (char *)locale, *output, *optr;
+        size_t inb, outb;
+
+        /*
+         * We are not allowed to modify the locale pointer, even if we cast it
+         * 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) */
+        optr = output = calloc( outb , 1);
+
+        vlc_mutex_lock( &libvlc.from_locale_lock );
+        vlc_iconv( libvlc.from_locale, NULL, NULL, NULL, NULL );
+
+        while( vlc_iconv( libvlc.from_locale, &iptr, &inb, &optr, &outb )
+                                                               == (size_t)-1 )
+        {
+            *optr++ = '?';
+            *iptr++;
+            vlc_iconv( libvlc.from_locale, NULL, NULL, NULL, NULL );
+        }
+        vlc_mutex_unlock( &libvlc.from_locale_lock );
+
+        return realloc( output, strlen( output ) + 1 );
+    }
+    return (char *)locale;
+}
+
+/*****************************************************************************
+ * ToLocale: converts an UTF-8 string to locale
+ *****************************************************************************/
+char *ToLocale( const char *utf8 )
+{
+    if( utf8 == NULL )
+        return NULL;
+
+    if( libvlc.to_locale != (vlc_iconv_t)(-1) )
+    {
+        char *iptr = (char *)utf8, *output, *optr;
+        size_t inb, outb;
+
+        /*
+         * We are not allowed to modify the locale pointer, even if we cast it
+         * to non-const.
+         */
+        inb = strlen( utf8 );
+        /* FIXME: I'm not sure about the value for the multiplication
+         * (for western people, multiplication is not needed) */
+        outb = inb * 2 + 1;
+
+        optr = output = calloc( outb, 1 );
+        vlc_mutex_lock( &libvlc.to_locale_lock );
+        vlc_iconv( libvlc.to_locale, NULL, NULL, NULL, NULL );
+
+        while( vlc_iconv( libvlc.to_locale, &iptr, &inb, &optr, &outb )
+                                                               == (size_t)-1 )
+        {
+            *optr++ = '?'; /* should not happen, and yes, it sucks */
+            *iptr++;
+            vlc_iconv( libvlc.to_locale, NULL, NULL, NULL, NULL );
+        }
+        vlc_mutex_unlock( &libvlc.to_locale_lock );
+
+        return realloc( output, strlen( output ) + 1 );
+    }
+    return (char *)utf8;
+}
+
+void LocaleFree( const char *str )
+{
+    if( ( str != NULL ) && ( libvlc.to_locale != (vlc_iconv_t)(-1) ) )
+        free( (char *)str );
+}