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