1 /*****************************************************************************
2 * darwin_specific.m: Darwin specific features
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Sam Hocevar <sam@zoy.org>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * Pierre d'Herbemont <pdherbemont@free.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
25 #include <string.h> /* strdup(), strstr() */
26 #include <stdlib.h> /* free() */
27 #include <dirent.h> /* *dir() */
30 #include "../libvlc.h"
32 #include <CoreFoundation/CoreFoundation.h>
38 /* CFLocaleCopyAvailableLocaleIdentifiers is present only on post-10.4 */
39 extern CFArrayRef CFLocaleCopyAvailableLocaleIdentifiers(void) __attribute__((weak_import));
41 /* emulate CFLocaleCopyAvailableLocaleIdentifiers on pre-10.4 */
42 static CFArrayRef copy_all_locale_indentifiers(void)
44 CFMutableArrayRef available_locales;
48 dir = opendir( "/usr/share/locale" );
49 available_locales = CFArrayCreateMutable( kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks );
51 while ( (file = readdir(dir)) )
53 /* we should probably filter out garbage */
54 /* we can't use CFStringCreateWithFileSystemRepresentation as it is
55 * supported only on post-10.4 (and this function is only for pre-10.4) */
56 CFStringRef locale = CFStringCreateWithCString( kCFAllocatorDefault, file->d_name, kCFStringEncodingUTF8 );
57 CFArrayAppendValue( available_locales, (void*)locale );
62 return available_locales;
65 /*****************************************************************************
66 * system_Init: fill in program path & retrieve language
67 *****************************************************************************/
68 void system_Init( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
71 char *p_char, *p_oldchar = &i_dummy;
73 /* Get the full program path and name */
74 p_char = vlc_global( p_this )->psz_vlcpath = strdup( ppsz_argv[ 0 ] );
76 /* Remove trailing program name */
89 /* Check if $LANG is set. */
90 if ( (p_char = getenv("LANG")) == NULL )
93 Retrieve the preferred language as chosen in System Preferences.app
94 (note that CFLocaleCopyCurrent() is not used because it returns the
95 prefered locale not language)
97 CFArrayRef all_locales, preferred_locales;
100 if( CFLocaleCopyAvailableLocaleIdentifiers )
101 all_locales = CFLocaleCopyAvailableLocaleIdentifiers();
103 all_locales = copy_all_locale_indentifiers();
105 preferred_locales = CFBundleCopyLocalizationsForPreferences( all_locales, NULL );
107 if ( preferred_locales )
109 if ( CFArrayGetCount( preferred_locales ) )
111 CFStringRef user_language_string_ref = CFArrayGetValueAtIndex( preferred_locales, 0 );
112 CFStringGetCString( user_language_string_ref, psz_locale, sizeof(psz_locale), kCFStringEncodingUTF8 );
113 setenv( "LANG", psz_locale, 1 );
115 CFRelease( preferred_locales );
117 CFRelease( all_locales );
120 vlc_mutex_init( p_this, &vlc_global( p_this )->iconv_lock );
121 vlc_global( p_this )->iconv_macosx = vlc_iconv_open( "UTF-8", "UTF-8-MAC" );
124 /*****************************************************************************
125 * system_Configure: check for system specific configuration options.
126 *****************************************************************************/
127 void system_Configure( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
132 /*****************************************************************************
133 * system_End: free the program path.
134 *****************************************************************************/
135 void system_End( libvlc_int_t *p_this )
137 free( vlc_global( p_this )->psz_vlcpath );
139 if ( vlc_global( p_this )->iconv_macosx != (vlc_iconv_t)-1 )
140 vlc_iconv_close( vlc_global( p_this )->iconv_macosx );
141 vlc_mutex_destroy( &vlc_global( p_this )->iconv_lock );