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