]> git.sesse.net Git - vlc/blobdiff - src/libvlc.c
* modules/video_output/directx/directx.c,glwin32.c: revert backport of 13050 for...
[vlc] / src / libvlc.c
index 19ff14741a3ccc79d15ae0917906a8691b570a51..2462e084fe0f5dac685a37baa88cbaa761eb2b69 100644 (file)
@@ -78,6 +78,7 @@
 #include "video_output.h"
 
 #include "stream_output.h"
+#include "charset.h"
 
 #include "libvlc.h"
 
@@ -91,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 );
@@ -135,6 +138,27 @@ char const * VLC_Version( void )
     return VERSION_MESSAGE;
 }
 
+/*****************************************************************************
+ * VLC_CompileBy, VLC_CompileHost, VLC_CompileDomain,
+ * VLC_Compiler, VLC_Changeset
+ *****************************************************************************/
+#define DECLARE_VLC_VERSION( func, var )                                    \
+char const * VLC_##func ( void )                                            \
+{                                                                           \
+    return VLC_##var ;                                                      \
+}
+
+DECLARE_VLC_VERSION( CompileBy, COMPILE_BY );
+DECLARE_VLC_VERSION( CompileHost, COMPILE_HOST );
+DECLARE_VLC_VERSION( CompileDomain, COMPILE_DOMAIN );
+DECLARE_VLC_VERSION( Compiler, COMPILER );
+
+extern const char psz_vlc_changeset[];
+char const * VLC_Changeset( void )
+{
+    return psz_vlc_changeset;
+}
+
 /*****************************************************************************
  * VLC_Error: strerror() equivalent
  *****************************************************************************
@@ -201,6 +225,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" );
@@ -252,7 +279,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 )
     {
@@ -284,6 +314,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") );
 
@@ -332,7 +368,21 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
 
     /* Set the config file stuff */
     p_vlc->psz_homedir = config_GetHomeDir();
+    p_vlc->psz_userdir = config_GetUserDir();
+    if( p_vlc->psz_userdir == NULL )
+        p_vlc->psz_userdir = strdup(p_vlc->psz_homedir);
     p_vlc->psz_configfile = config_GetPsz( p_vlc, "config" );
+    if( p_vlc->psz_configfile != NULL && p_vlc->psz_configfile[0] == '~'
+         && p_vlc->psz_configfile[1] == '/' )
+    {
+        char *psz = malloc( strlen(p_vlc->psz_userdir)
+                             + strlen(p_vlc->psz_configfile) );
+        /* This is incomplete : we should also support the ~cmassiot/ syntax. */
+        sprintf( psz, "%s/%s", p_vlc->psz_userdir,
+                               p_vlc->psz_configfile + 2 );
+        free( p_vlc->psz_configfile );
+        p_vlc->psz_configfile = psz;
+    }
 
     /* Check for plugins cache options */
     if( config_GetInt( p_vlc, "reset-plugins-cache" ) )
@@ -352,7 +402,7 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
     if( config_GetInt( p_vlc, "daemon" ) )
     {
 #if HAVE_DAEMON
-        if( daemon( 0, 0) != 0 )
+        if( daemon( 1, 0) != 0 )
         {
             msg_Err( p_vlc, "Unable to fork vlc to daemon mode" );
             b_exit = VLC_TRUE;
@@ -414,16 +464,12 @@ 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") );
 
-        textdomain( PACKAGE_NAME );
-
-#if defined( ENABLE_UTF8 )
-        bind_textdomain_codeset( PACKAGE_NAME, "UTF-8" );
-#endif
-
         module_EndBank( p_vlc );
         module_InitBank( p_vlc );
         config_LoadConfigFile( p_vlc, "main" );
@@ -702,10 +748,21 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
     }
 
     /*
-     * Allways load the hotkeys interface if it exists
+     * Always load the hotkeys interface if it exists
      */
     VLC_AddIntf( 0, "hotkeys,none", VLC_FALSE, VLC_FALSE );
 
+    /*
+     * If needed, load the Xscreensaver interface
+     * Currently, only for X
+     */
+#ifdef HAVE_X11_XLIB_H
+    if( config_GetInt( p_vlc, "disable-screensaver" ) == 1 )
+    {
+        VLC_AddIntf( 0, "screensaver", VLC_FALSE, VLC_FALSE );
+    }
+#endif
+
     /*
      * FIXME: kludge to use a p_vlc-local variable for the Mozilla plugin
      */
@@ -940,6 +997,12 @@ int VLC_Destroy( int i_object )
         p_vlc->psz_homedir = NULL;
     }
 
+    if( p_vlc->psz_userdir )
+    {
+        free( p_vlc->psz_userdir );
+        p_vlc->psz_userdir = NULL;
+    }
+
     if( p_vlc->psz_configfile )
     {
         free( p_vlc->psz_configfile );
@@ -964,6 +1027,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 );
 
@@ -1243,6 +1309,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 );
 
@@ -1259,8 +1326,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 );
@@ -1816,6 +1888,49 @@ int VLC_FullScreen( int i_object )
 
 /* following functions are local */
 
+static void LocaleInit( void )
+{
+    char *psz_charset;
+
+    if( !vlc_current_charset( &psz_charset ) )
+    {
+        char *psz_conv = psz_charset;
+
+        /*
+         * Still allow non-ASCII characters when the locale is not set.
+         * Western Europeans are being favored for historical reasons.
+         */
+        psz_conv = strcmp( psz_charset, "ASCII" )
+            ? psz_charset
+            : "ISO-8859-15";
+
+        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.
  *****************************************************************************
@@ -1859,6 +1974,10 @@ static void SetLanguage ( char const *psz_lang )
 #endif
 
         setlocale( LC_ALL, psz_lang );
+        /* many code paths assume that float numbers are formatted according
+         * to the US standard (ie. with dot as decimal point), so we keep
+         * C for LC_NUMERIC. */
+        setlocale(LC_NUMERIC, "C" );
     }
 
     /* Specify where to find the locales for current domain */
@@ -1877,12 +1996,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
 }
 
 /*****************************************************************************
@@ -1899,6 +2014,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 */
@@ -1910,10 +2026,13 @@ 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;
@@ -2041,7 +2160,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 )
@@ -2293,7 +2412,13 @@ static void Version( void )
     ShowConsole();
 #endif
 
-    fprintf( stdout, VERSION_MESSAGE "\n" );
+    fprintf( stdout, _("VLC version %s\n"), VLC_Version() );
+    fprintf( stdout, _("Compiled by %s@%s.%s\n"),
+             VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
+    fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
+    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 "
@@ -2314,14 +2439,24 @@ static void Version( void )
 static void ShowConsole( void )
 {
 #   ifndef UNDER_CE
+    FILE *f_help;
 
     if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
 
     AllocConsole();
-    freopen( "CONOUT$", "w", stdout );
+
     freopen( "CONOUT$", "w", stderr );
     freopen( "CONIN$", "r", stdin );
 
+    if( (f_help = fopen( "vlc-help.txt", "wt" )) )
+    {
+        fclose( f_help );
+        freopen( "vlc-help.txt", "wt", stdout );
+        fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
+    }
+
+    else freopen( "CONOUT$", "w", stdout );
+
 #   endif
 }
 #endif
@@ -2337,8 +2472,10 @@ static void PauseConsole( void )
 #   ifndef UNDER_CE
 
     if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
-    fprintf( stdout, _("\nPress the RETURN key to continue...\n") );
+
+    fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
     getchar();
+    fclose( stdout );
 
 #   endif
 }
@@ -2444,3 +2581,92 @@ 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 = '?';
+            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 */
+            optr++;
+            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 );
+}