]> git.sesse.net Git - vlc/blob - src/misc/darwin_specific.c
Add an error message
[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         /* Check for "VLC.framework/Versions/Current/VLC",
80          * as well as "VLC.framework/Versions/A/VLC" and
81          * "VLC.framework/Versions/B/VLC" */
82         if( p_char = strstr( psz_img_name, "VLC.framework/Versions/" ))
83         {
84             /* Look for the next forward slash */
85             p_char += 23; /* p_char += strlen(" VLC.framework/Versions/" ) */
86             while( *p_char != '\0' && *p_char != '/')
87                 p_char++;
88             
89             /* If the string ends with VLC then we've found a winner */
90             if ( !strcmp( p_char, "/VLC" ) )
91             {
92                 p_char = strdup( psz_img_name );
93                 break;
94             }
95             else
96                 p_char = NULL;
97         }
98     }
99
100     if( !p_char )
101     {
102         /* We are not linked to the VLC.framework, return the executable path */
103         p_char = strdup( ppsz_argv[ 0 ] );
104     }
105
106     vlc_global()->psz_vlcpath = p_char;
107
108     /* Remove trailing program name */
109     for( ; *p_char ; )
110     {
111         if( *p_char == '/' )
112         {
113             *p_oldchar = '/';
114             *p_char = '\0';
115             p_oldchar = p_char;
116         }
117
118         p_char++;
119     }
120
121     /* Check if $LANG is set. */
122     if ( (p_char = getenv("LANG")) == NULL )
123     {
124         /*
125            Retrieve the preferred language as chosen in  System Preferences.app
126            (note that CFLocaleCopyCurrent() is not used because it returns the
127             prefered locale not language)
128         */
129         CFArrayRef all_locales, preferred_locales;
130         char psz_locale[50];
131
132         if( CFLocaleCopyAvailableLocaleIdentifiers )
133             all_locales = CFLocaleCopyAvailableLocaleIdentifiers();
134         else
135             all_locales = copy_all_locale_indentifiers();
136
137         preferred_locales = CFBundleCopyLocalizationsForPreferences( all_locales, NULL );
138
139         if ( preferred_locales )
140         {
141             if ( CFArrayGetCount( preferred_locales ) )
142             {
143                 CFStringRef user_language_string_ref = CFArrayGetValueAtIndex( preferred_locales, 0 );
144                 CFStringGetCString( user_language_string_ref, psz_locale, sizeof(psz_locale), kCFStringEncodingUTF8 );
145                 setenv( "LANG", psz_locale, 1 );
146             }
147             CFRelease( preferred_locales );
148         }
149         CFRelease( all_locales );
150     }
151
152     vlc_mutex_init( p_this, &vlc_global()->iconv_lock );
153     vlc_global()->iconv_macosx = vlc_iconv_open( "UTF-8", "UTF-8-MAC" );
154 }
155
156 /*****************************************************************************
157  * system_Configure: check for system specific configuration options.
158  *****************************************************************************/
159 void system_Configure( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
160 {
161
162 }
163
164 /*****************************************************************************
165  * system_End: free the program path.
166  *****************************************************************************/
167 void system_End( libvlc_int_t *p_this )
168 {
169     free( vlc_global()->psz_vlcpath );
170
171     if ( vlc_global()->iconv_macosx != (vlc_iconv_t)-1 )
172         vlc_iconv_close( vlc_global()->iconv_macosx );
173     vlc_mutex_destroy( &vlc_global()->iconv_lock );
174 }
175