]> git.sesse.net Git - vlc/blob - src/misc/darwin_specific.c
* renamed it, since it's plain C now and doesn't contain any Obj-C anymore
[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 #include <string.h>                                              /* strdup(), strstr() */
26 #include <stdlib.h>                                                /* free() */
27 #include <dirent.h>                                                /* *dir() */
28
29 #include <vlc/vlc.h>
30
31 #include <CoreFoundation/CoreFoundation.h>
32
33 #ifdef HAVE_LOCALE_H
34 #   include <locale.h>
35 #endif
36
37 /* CFLocaleCopyAvailableLocaleIdentifiers is present only on post-10.4 */
38 extern CFArrayRef CFLocaleCopyAvailableLocaleIdentifiers(void) __attribute__((weak_import));
39
40 /* emulate CFLocaleCopyAvailableLocaleIdentifiers on pre-10.4 */
41 static CFArrayRef copy_all_locale_indentifiers(void)
42 {
43     CFMutableArrayRef available_locales;
44     DIR * dir;
45     struct dirent *file;
46
47     dir = opendir( "/usr/share/locale" );
48     available_locales = CFArrayCreateMutable( kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks );
49
50     while ( (file = readdir(dir)) )
51     {
52         /* we should probably filter out garbage */
53         /* we can't use CFStringCreateWithFileSystemRepresentation as it is 
54          * supported only on post-10.4 (and this function is only for pre-10.4) */
55         CFStringRef locale = CFStringCreateWithCString( kCFAllocatorDefault, file->d_name, kCFStringEncodingUTF8 );
56         CFArrayAppendValue( available_locales, (void*)locale );
57         CFRelease( locale );
58     }
59
60     closedir( dir );
61     return available_locales;
62 }
63
64 /*****************************************************************************
65  * system_Init: fill in program path & retrieve language
66  *****************************************************************************/
67 void system_Init( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
68 {
69     char i_dummy;
70     char *p_char, *p_oldchar = &i_dummy;
71
72     /* Get the full program path and name */
73     p_char = p_this->p_libvlc_global->psz_vlcpath = strdup( ppsz_argv[ 0 ] );
74
75     /* Remove trailing program name */
76     for( ; *p_char ; )
77     {
78         if( *p_char == '/' )
79         {
80             *p_oldchar = '/';
81             *p_char = '\0';
82             p_oldchar = p_char;
83         }
84
85         p_char++;
86     }
87
88     /* Check if $LANG is set. */
89     if ( (p_char = getenv("LANG")) == NULL )
90     {
91         /*
92            Retrieve the preferred language as chosen in  System Preferences.app
93            (note that CFLocaleCopyCurrent() is not used because it returns the
94             prefered locale not language) 
95         */
96         CFArrayRef all_locales, preferred_locales;
97         char psz_locale[50];
98
99         if( CFLocaleCopyAvailableLocaleIdentifiers )
100             all_locales = CFLocaleCopyAvailableLocaleIdentifiers();
101         else
102             all_locales = copy_all_locale_indentifiers();
103
104         preferred_locales = CFBundleCopyLocalizationsForPreferences( all_locales, NULL );
105
106         if ( preferred_locales )
107         {
108             if ( CFArrayGetCount( preferred_locales ) )
109             {
110                 CFStringRef user_language_string_ref = CFArrayGetValueAtIndex( preferred_locales, 0 );
111                 CFStringGetCString( user_language_string_ref, psz_locale, sizeof(psz_locale), kCFStringEncodingUTF8 );
112                 setenv( "LANG", psz_locale, 1 );
113             }
114             CFRelease( preferred_locales );
115         }
116         CFRelease( all_locales );
117     }
118
119     vlc_mutex_init( p_this, &p_this->p_libvlc_global->iconv_lock );
120     p_this->p_libvlc_global->iconv_macosx = vlc_iconv_open( "UTF-8", "UTF-8-MAC" );
121 }
122
123 /*****************************************************************************
124  * system_Configure: check for system specific configuration options.
125  *****************************************************************************/
126 void system_Configure( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
127 {
128
129 }
130
131 /*****************************************************************************
132  * system_End: free the program path.
133  *****************************************************************************/
134 void system_End( libvlc_int_t *p_this )
135 {
136     free( p_this->p_libvlc_global->psz_vlcpath );
137
138     if ( p_this->p_libvlc_global->iconv_macosx != (vlc_iconv_t)-1 )
139         vlc_iconv_close( p_this->p_libvlc_global->iconv_macosx );
140     vlc_mutex_destroy( &p_this->p_libvlc_global->iconv_lock );
141 }
142