]> git.sesse.net Git - vlc/blob - src/misc/darwin_specific.c
Brazillian Portuguese l10n update by Sidney Doria
[vlc] / src / misc / darwin_specific.c
1 /*****************************************************************************
2  * darwin_specific.m: Darwin specific features
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Pierre d'Herbemont <pdherbemont@free.fr>
10  *
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.
15  *
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.
20  *
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
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc/vlc.h>
31 #include "../libvlc.h"
32 #include <dirent.h>                                                /* *dir() */
33
34 #include <CoreFoundation/CoreFoundation.h>
35
36 #ifdef HAVE_LOCALE_H
37 #   include <locale.h>
38 #endif
39
40 /* CFLocaleCopyAvailableLocaleIdentifiers is present only on post-10.4 */
41 extern CFArrayRef CFLocaleCopyAvailableLocaleIdentifiers(void) __attribute__((weak_import));
42
43 /* emulate CFLocaleCopyAvailableLocaleIdentifiers on pre-10.4 */
44 static CFArrayRef copy_all_locale_indentifiers(void)
45 {
46     CFMutableArrayRef available_locales;
47     DIR * dir;
48     struct dirent *file;
49
50     dir = opendir( "/usr/share/locale" );
51     available_locales = CFArrayCreateMutable( kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks );
52
53     while ( (file = readdir(dir)) )
54     {
55         /* we should probably filter out garbage */
56         /* we can't use CFStringCreateWithFileSystemRepresentation as it is
57          * supported only on post-10.4 (and this function is only for pre-10.4) */
58         CFStringRef locale = CFStringCreateWithCString( kCFAllocatorDefault, file->d_name, kCFStringEncodingUTF8 );
59         CFArrayAppendValue( available_locales, (void*)locale );
60         CFRelease( locale );
61     }
62
63     closedir( dir );
64     return available_locales;
65 }
66
67 /*****************************************************************************
68  * system_Init: fill in program path & retrieve language
69  *****************************************************************************/
70 void system_Init( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
71 {
72     char i_dummy;
73     char *p_char = NULL;
74     char *p_oldchar = &i_dummy;
75     int i;
76
77     /* Get the full program path and name */
78
79     /* First try to see if we are linked to the framework */
80     for (i = 0; i < _dyld_image_count(); i++)
81     {
82         char * psz_img_name = _dyld_get_image_name(i);
83         /* Check for "VLCKit.framework/Versions/Current/VLCKit",
84          * as well as "VLCKit.framework/Versions/A/VLCKit" and
85          * "VLC.framework/Versions/B/VLCKit" */
86         if( p_char = strstr( psz_img_name, "VLCKit.framework/Versions/" ))
87         {
88             /* Look for the next forward slash */
89             p_char += 26; /* p_char += strlen(" VLCKit.framework/Versions/" ) */
90             while( *p_char != '\0' && *p_char != '/')
91                 p_char++;
92             
93             /* If the string ends with VLC then we've found a winner */
94             if ( !strcmp( p_char, "/VLCKit" ) )
95             {
96                 p_char = strdup( psz_img_name );
97                 break;
98             }
99             else
100                 p_char = NULL;
101         }
102     }
103
104     if( !p_char )
105     {
106         /* We are not linked to the VLC.framework, return the executable path */
107         p_char = strdup( ppsz_argv[ 0 ] );
108     }
109
110     vlc_global()->psz_vlcpath = p_char;
111
112     /* Remove trailing program name */
113     for( ; *p_char ; )
114     {
115         if( *p_char == '/' )
116         {
117             *p_oldchar = '/';
118             *p_char = '\0';
119             p_oldchar = p_char;
120         }
121
122         p_char++;
123     }
124
125     /* Check if $LANG is set. */
126     if ( (p_char = getenv("LANG")) == NULL )
127     {
128         /*
129            Retrieve the preferred language as chosen in  System Preferences.app
130            (note that CFLocaleCopyCurrent() is not used because it returns the
131             prefered locale not language)
132         */
133         CFArrayRef all_locales, preferred_locales;
134         char psz_locale[50];
135
136         if( CFLocaleCopyAvailableLocaleIdentifiers )
137             all_locales = CFLocaleCopyAvailableLocaleIdentifiers();
138         else
139             all_locales = copy_all_locale_indentifiers();
140
141         preferred_locales = CFBundleCopyLocalizationsForPreferences( all_locales, NULL );
142
143         if ( preferred_locales )
144         {
145             if ( CFArrayGetCount( preferred_locales ) )
146             {
147                 CFStringRef user_language_string_ref = CFArrayGetValueAtIndex( preferred_locales, 0 );
148                 CFStringGetCString( user_language_string_ref, psz_locale, sizeof(psz_locale), kCFStringEncodingUTF8 );
149                 setenv( "LANG", psz_locale, 1 );
150             }
151             CFRelease( preferred_locales );
152         }
153         CFRelease( all_locales );
154     }
155
156     vlc_mutex_init( p_this, &vlc_global()->iconv_lock );
157     vlc_global()->iconv_macosx = vlc_iconv_open( "UTF-8", "UTF-8-MAC" );
158 }
159
160 /*****************************************************************************
161  * system_Configure: check for system specific configuration options.
162  *****************************************************************************/
163 void system_Configure( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
164 {
165
166 }
167
168 /*****************************************************************************
169  * system_End: free the program path.
170  *****************************************************************************/
171 void system_End( libvlc_int_t *p_this )
172 {
173     free( vlc_global()->psz_vlcpath );
174
175     if ( vlc_global()->iconv_macosx != (vlc_iconv_t)-1 )
176         vlc_iconv_close( vlc_global()->iconv_macosx );
177     vlc_mutex_destroy( &vlc_global()->iconv_lock );
178 }
179