]> git.sesse.net Git - vlc/blobdiff - src/misc/darwin_specific.c
Removes trailing spaces. Removes tabs.
[vlc] / src / misc / darwin_specific.c
index 275b32f349a135d35f43944f8913ae7ff2b69aab..f14d54b8ce8d575eddc85d2fc7eff0a6f1700f2f 100644 (file)
@@ -1,16 +1,18 @@
 /*****************************************************************************
- * darwin_specific.c: Darwin specific features 
+ * darwin_specific.m: Darwin specific features
  *****************************************************************************
- * Copyright (C) 2001 VideoLAN
- * $Id: darwin_specific.c,v 1.16 2002/12/27 00:17:49 massiot Exp $
+ * Copyright (C) 2001-2007 the VideoLAN team
+ * $Id$
  *
- * Authors: Samuel Hocevar <sam@zoy.org>
+ * Authors: Sam Hocevar <sam@zoy.org>
+ *          Christophe Massiot <massiot@via.ecp.fr>
+ *          Pierre d'Herbemont <pdherbemont@free.fr>
  *
  * 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
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *
  * 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 <string.h>                                              /* strdup() */
-#include <stdlib.h>                                                /* free() */
 
 #include <vlc/vlc.h>
+#include "../libvlc.h"
+#include <dirent.h>                                                /* *dir() */
 
-/*****************************************************************************
- * Static vars
- *****************************************************************************/
-static char * psz_program_path;
+#include <CoreFoundation/CoreFoundation.h>
+
+#ifdef HAVE_LOCALE_H
+#   include <locale.h>
+#endif
+
+/* CFLocaleCopyAvailableLocaleIdentifiers is present only on post-10.4 */
+extern CFArrayRef CFLocaleCopyAvailableLocaleIdentifiers(void) __attribute__((weak_import));
+
+/* emulate CFLocaleCopyAvailableLocaleIdentifiers on pre-10.4 */
+static CFArrayRef copy_all_locale_indentifiers(void)
+{
+    CFMutableArrayRef available_locales;
+    DIR * dir;
+    struct dirent *file;
+
+    dir = opendir( "/usr/share/locale" );
+    available_locales = CFArrayCreateMutable( kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks );
+
+    while ( (file = readdir(dir)) )
+    {
+        /* we should probably filter out garbage */
+        /* we can't use CFStringCreateWithFileSystemRepresentation as it is
+         * supported only on post-10.4 (and this function is only for pre-10.4) */
+        CFStringRef locale = CFStringCreateWithCString( kCFAllocatorDefault, file->d_name, kCFStringEncodingUTF8 );
+        CFArrayAppendValue( available_locales, (void*)locale );
+        CFRelease( locale );
+    }
+
+    closedir( dir );
+    return available_locales;
+}
 
 /*****************************************************************************
- * system_Init: fill in program path.
+ * system_Init: fill in program path & retrieve language
  *****************************************************************************/
-void system_Init( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
+void system_Init( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
 {
     char i_dummy;
-    char *p_char, *p_oldchar = &i_dummy;
+    char *p_char = NULL;
+    char *p_oldchar = &i_dummy;
+    int i;
 
     /* Get the full program path and name */
-    p_char = psz_program_path = strdup( ppsz_argv[ 0 ] );
+
+    /* First try to see if we are linked to the framework */
+    for (i = 0; i < _dyld_image_count(); i++)
+    {
+        char * psz_img_name = _dyld_get_image_name(i);
+        if( strstr( psz_img_name, "VLC.framework/Version/Current/VLC" ) )
+            p_char = strdup( psz_img_name );
+    }
+
+    if( !p_char )
+    {
+        /* We are not linked to the VLC.framework, return the executable path */
+        p_char = strdup( ppsz_argv[ 0 ] );
+    }
+
+    vlc_global()->psz_vlcpath = p_char;
 
     /* Remove trailing program name */
     for( ; *p_char ; )
@@ -53,12 +100,46 @@ void system_Init( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
 
         p_char++;
     }
+
+    /* Check if $LANG is set. */
+    if ( (p_char = getenv("LANG")) == NULL )
+    {
+        /*
+           Retrieve the preferred language as chosen in  System Preferences.app
+           (note that CFLocaleCopyCurrent() is not used because it returns the
+            prefered locale not language)
+        */
+        CFArrayRef all_locales, preferred_locales;
+        char psz_locale[50];
+
+        if( CFLocaleCopyAvailableLocaleIdentifiers )
+            all_locales = CFLocaleCopyAvailableLocaleIdentifiers();
+        else
+            all_locales = copy_all_locale_indentifiers();
+
+        preferred_locales = CFBundleCopyLocalizationsForPreferences( all_locales, NULL );
+
+        if ( preferred_locales )
+        {
+            if ( CFArrayGetCount( preferred_locales ) )
+            {
+                CFStringRef user_language_string_ref = CFArrayGetValueAtIndex( preferred_locales, 0 );
+                CFStringGetCString( user_language_string_ref, psz_locale, sizeof(psz_locale), kCFStringEncodingUTF8 );
+                setenv( "LANG", psz_locale, 1 );
+            }
+            CFRelease( preferred_locales );
+        }
+        CFRelease( all_locales );
+    }
+
+    vlc_mutex_init( p_this, &vlc_global()->iconv_lock );
+    vlc_global()->iconv_macosx = vlc_iconv_open( "UTF-8", "UTF-8-MAC" );
 }
 
 /*****************************************************************************
  * system_Configure: check for system specific configuration options.
  *****************************************************************************/
-void system_Configure( vlc_t *p_this )
+void system_Configure( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
 {
 
 }
@@ -66,16 +147,12 @@ void system_Configure( vlc_t *p_this )
 /*****************************************************************************
  * system_End: free the program path.
  *****************************************************************************/
-void system_End( vlc_t *p_this )
+void system_End( libvlc_int_t *p_this )
 {
-    free( psz_program_path );
-}
+    free( vlc_global()->psz_vlcpath );
 
-/*****************************************************************************
- * system_GetProgramPath: get the full path to the program.
- *****************************************************************************/
-char * system_GetProgramPath( void )
-{
-    return( psz_program_path );
+    if ( vlc_global()->iconv_macosx != (vlc_iconv_t)-1 )
+        vlc_iconv_close( vlc_global()->iconv_macosx );
+    vlc_mutex_destroy( &vlc_global()->iconv_lock );
 }