]> git.sesse.net Git - vlc/blobdiff - src/libvlc.c
got the value for PROTECTION_LEVEL_* hard-coded
[vlc] / src / libvlc.c
index 2d577cb652558c12fddfbcd7b18c08717449f32e..9fcf4970a5f2f67e04960d9516b7cf8aad4e02e7 100644 (file)
@@ -92,6 +92,8 @@ 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 );
@@ -202,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" );
@@ -253,7 +258,10 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
     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 )
     {
@@ -285,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") );
 
@@ -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") );
@@ -965,6 +981,9 @@ int VLC_Destroy( int i_object )
     msg_Flush( p_vlc );
     msg_Destroy( p_libvlc );
 
+    /* Destroy global iconv */
+    LocaleDeinit();
+
     /* Destroy mutexes */
     vlc_mutex_destroy( &p_vlc->config_lock );
 
@@ -1244,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 );
 
@@ -1260,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 );
@@ -1817,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.
  *****************************************************************************
@@ -1860,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 */
@@ -1883,121 +1942,6 @@ static void SetLanguage ( char const *psz_lang )
 #endif
 }
 
-/*****************************************************************************
- * FromLocale: converts a locale string to UTF-8
- *****************************************************************************/
-/* FIXME FIXME: it really has to be made quicker */
-char *FromLocale( const char *locale )
-{
-    char *psz_charset;
-
-    if( locale == NULL )
-        return NULL;
-
-    if( !vlc_current_charset( &psz_charset ) )
-    {
-        char *iptr = (ICONV_CONST char *)locale, *output, *optr;
-        size_t inb, outb;
-
-        /* cannot fail (unless vlc_current_charset sucks) */
-        vlc_iconv_t hd = vlc_iconv_open( "UTF-8", psz_charset );
-        free( psz_charset );
-
-        /*
-         * 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);
-        while( vlc_iconv( hd, &iptr, &inb, &optr, &outb ) == (size_t)-1 )
-            *iptr = '?'; /* should not happen, and yes, it sucks */
-
-        vlc_iconv_close( hd );
-        return realloc( output, strlen( output ) + 1 );
-    }
-    free( psz_charset );
-    return (char *)locale;
-}
-
-/*****************************************************************************
- * ToLocale: converts an UTF-8 string to locale
- *****************************************************************************/
-/* FIXME FIXME: it really has to be made quicker */
-char *ToLocale( const char *utf8 )
-{
-    char *psz_charset;
-
-    if( utf8 == NULL )
-        return NULL;
-
-    if( !vlc_current_charset( &psz_charset ) )
-    {
-        char *iptr = (ICONV_CONST char *)utf8, *output, *optr;
-        size_t inb, outb;
-
-        /* cannot fail (unless vlc_current_charset sucks) */
-        vlc_iconv_t hd = vlc_iconv_open( psz_charset, "UTF-8" );
-        free( psz_charset );
-
-        /*
-         * 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 );
-        while( vlc_iconv( hd, &iptr, &inb, &optr, &outb ) == (size_t)-1 )
-            *iptr = '?'; /* should not happen, and yes, it sucks */
-
-        vlc_iconv_close( hd );
-        return realloc( output, strlen( output ) + 1 );
-    }
-    free( psz_charset );
-    return (char *)utf8;
-}
-
-void LocaleFree( const char *str )
-{
-    if( str != NULL )
-    {
-        /* FIXME: this deserve a price for the most inefficient peice of code */
-        char *psz_charset;
-    
-        if( !vlc_current_charset( &psz_charset ) )
-            free( (char *)str );
-    
-        free( psz_charset );
-    }
-}
-
-/* FIXME: don't use iconv at all */
-char *EnsureUTF8( char *str )
-{
-    vlc_iconv_t hd;
-    size_t inb, outb;
-    char *ostr, *istr;
-
-    if( str == NULL )
-        return NULL;
-
-    ostr = istr = str;
-
-    inb = outb = strlen( str );
-    hd = vlc_iconv_open( "UTF-8", "UTF-8" );
-    while( vlc_iconv( hd, &istr, &inb, &ostr, &outb ) == (size_t)-1 )
-        *istr = '?';
-
-    vlc_iconv_close( hd );
-    return str;
-}
-
 /*****************************************************************************
  * GetFilenames: parse command line options which are not flags
  *****************************************************************************
@@ -2158,7 +2102,7 @@ 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;
-            size_t i;
+            signed int i;
 
             /* Skip deprecated options */
             if( p_item->psz_current )
@@ -2561,3 +2505,90 @@ static void InitDeviceValues( vlc_t *p_vlc )
     }
 #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 );
+}